Partial tool group implementation

This commit is contained in:
Cyber MacGeddon 2025-09-03 16:16:30 +01:00
parent 8374e5ed1a
commit c165bb3522
3 changed files with 58 additions and 4 deletions

View file

@ -16,8 +16,8 @@ class AgentStep(Record):
class AgentRequest(Record): class AgentRequest(Record):
question = String() question = String()
plan = String()
state = String() state = String()
group = Array(String())
history = Array(AgentStep()) history = Array(AgentStep())
class AgentResponse(Record): class AgentResponse(Record):

View file

@ -63,6 +63,9 @@ def set_tool(
collection : str, collection : str,
template : str, template : str,
arguments : List[Argument], arguments : List[Argument],
group : List[str],
state : str,
available_in_states : List[str],
): ):
api = Api(url).config() api = Api(url).config()
@ -93,6 +96,12 @@ def set_tool(
for a in arguments for a in arguments
] ]
if group: object["group"] = group
if state: object["state"] = state
if available_in_states: object["available_in_states"] = available_in_states
values = api.put([ values = api.put([
ConfigValue( ConfigValue(
type="tool", key=f"{id}", value=json.dumps(object) type="tool", key=f"{id}", value=json.dumps(object)
@ -179,6 +188,23 @@ def main():
help=f'Tool arguments in the form: name:type:description (can specify multiple)', help=f'Tool arguments in the form: name:type:description (can specify multiple)',
) )
parser.add_argument(
'--group',
nargs="*",
help=f'Tool groups (e.g., read-only, knowledge, admin)',
)
parser.add_argument(
'--state',
help=f'State to transition to after successful execution',
)
parser.add_argument(
'--available-in-states',
nargs="*",
help=f'States in which this tool is available',
)
args = parser.parse_args() args = parser.parse_args()
try: try:
@ -219,6 +245,9 @@ def main():
collection=args.collection, collection=args.collection,
template=args.template, template=args.template,
arguments=arguments, arguments=arguments,
group=args.group or [],
state=args.state,
available_in_states=getattr(args, 'available_in_states', None) or [],
) )
except Exception as e: except Exception as e:

View file

@ -18,6 +18,7 @@ from ... schema import AgentRequest, AgentResponse, AgentStep, Error
from . tools import KnowledgeQueryImpl, TextCompletionImpl, McpToolImpl, PromptImpl from . tools import KnowledgeQueryImpl, TextCompletionImpl, McpToolImpl, PromptImpl
from . agent_manager import AgentManager from . agent_manager import AgentManager
from ..tool_filter import validate_tool_config, filter_tools_by_group_and_state, get_next_state
from . types import Final, Action, Tool, Argument from . types import Final, Action, Tool, Argument
@ -142,6 +143,9 @@ class Processor(AgentService):
f"Tool type {impl_id} not known" f"Tool type {impl_id} not known"
) )
# Validate tool configuration
validate_tool_config(data)
tools[name] = Tool( tools[name] = Tool(
name=name, name=name,
description=data.get("description"), description=data.get("description"),
@ -219,9 +223,24 @@ class Processor(AgentService):
await respond(r) await respond(r)
# Apply tool filtering based on request groups and state
filtered_tools = filter_tools_by_group_and_state(
tools=self.agent.tools,
requested_groups=getattr(request, 'group', None),
current_state=getattr(request, 'state', None)
)
logger.info(f"Filtered from {len(self.agent.tools)} to {len(filtered_tools)} available tools")
# Create temporary agent with filtered tools
temp_agent = AgentManager(
tools=filtered_tools,
additional_context=self.agent.additional_context
)
logger.debug("Call React") logger.debug("Call React")
act = await self.agent.react( act = await temp_agent.react(
question = request.question, question = request.question,
history = history, history = history,
think = think, think = think,
@ -255,11 +274,17 @@ class Processor(AgentService):
logger.debug("Send next...") logger.debug("Send next...")
history.append(act) history.append(act)
# Handle state transitions if tool execution was successful
next_state = request.state
if act.name in filtered_tools:
executed_tool = filtered_tools[act.name]
next_state = get_next_state(executed_tool, request.state or "undefined")
r = AgentRequest( r = AgentRequest(
question=request.question, question=request.question,
plan=request.plan, state=next_state,
state=request.state, group=getattr(request, 'group', []),
history=[ history=[
AgentStep( AgentStep(
thought=h.thought, thought=h.thought,