File size: 1,700 Bytes
1f0fa9b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from schemas.agent_spec import AgentSpec
from .context import ExecutorContext
from tools.query_tool import run_query_tool
from tools.summarize_tool import run_summarize_tool

# Tool registry linking spec names to python functions
TOOLS = {
    "query_tool": run_query_tool,
    "summarize_tool": run_summarize_tool
}

def execute_spec(spec: AgentSpec) -> list:
    """
    Executes the given AgentSpec synchronously.
    Returns a log list detailing execution.
    """
    context = ExecutorContext()
    log = []
    
    log.append(f"Starting execution of Agent: {spec.name}")
    log.append(f"Description: {spec.description}")
    
    for step in spec.steps:
        tool_name = step.tool.name
        raw_params = step.tool.parameters
        
        if tool_name not in TOOLS:
            log.append(f"Step '{step.id}' FAILED: Unknown tool '{tool_name}' installed in runner.")
            break
            
        log.append(f"--> Running Step '{step.id}': {step.description}")
        
        # Resolve any dependency outputs in parameters using context
        try:
            resolved_params = context.resolve_dict(raw_params)
        except Exception as e:
            log.append(f"Step '{step.id}' FAILED during parameter resolution: {e}")
            break
            
        # Execute tool
        try:
            tool_func = TOOLS[tool_name]
            result = tool_func(resolved_params)
            context.set_output(step.id, result)
            log.append(f"<-- Output of '{step.id}':\n{result}")
        except Exception as e:
            log.append(f"Step '{step.id}' FAILED during execution: {e}")
            break

    log.append("Execution Complete.")
    return log