@@ -23,8 +23,14 @@ type Tool struct {
2323
2424// Event represents a step in the agent's reasoning.
2525type Event struct {
26- Type string
27- Content string
26+ Type string
27+ Content string
28+ ToolCallID string
29+ ToolName string
30+ Args map [string ]interface {}
31+ Result string
32+ IsError bool
33+ Thinking string
2834}
2935
3036// Agent is the core reasoning loop.
@@ -75,14 +81,15 @@ func (a *Agent) Run(ctx context.Context, systemPrompt, userMessage string) (stri
7581 const maxIterations = 20
7682 for i := 0 ; i < maxIterations ; i ++ {
7783 a .logger .Info ("agent iteration" , "step" , i + 1 )
84+ a .emit (Event {Type : string (EventTurnStart ), Content : fmt .Sprintf ("turn %d" , i + 1 )})
7885
7986 response , err := a .provider .Complete (ctx , messages )
8087 if err != nil {
81- a .emit (Event {Type : "error" , Content : err .Error ()})
88+ a .emit (Event {Type : string ( EventError ) , Content : err .Error (), IsError : true })
8289 return "" , fmt .Errorf ("provider error at step %d: %w" , i + 1 , err )
8390 }
8491
85- a .emit (Event {Type : "thought" , Content : response })
92+ a .emit (Event {Type : string ( EventMessageUpdate ) , Content : response })
8693
8794 messages = append (messages , Message {
8895 Role : "assistant" ,
@@ -91,7 +98,8 @@ func (a *Agent) Run(ctx context.Context, systemPrompt, userMessage string) (stri
9198
9299 calls := ParseToolCalls (response )
93100 if len (calls ) == 0 {
94- a .emit (Event {Type : "done" , Content : response })
101+ a .emit (Event {Type : string (EventMessageEnd ), Content : response })
102+ a .emit (Event {Type : string (EventTurnEnd ), Content : "" })
95103 return response , nil
96104 }
97105
@@ -101,26 +109,43 @@ func (a *Agent) Run(ctx context.Context, systemPrompt, userMessage string) (stri
101109 if ! ok {
102110 result := fmt .Sprintf ("unknown tool: %s" , call .Tool )
103111 toolResults .WriteString (fmt .Sprintf ("Tool %s: %s\n " , call .Tool , result ))
104- a .emit (Event {Type : "tool_result" , Content : result })
112+ a .emit (Event {Type : string ( EventToolExecutionEnd ), ToolName : call . Tool , Result : result , IsError : true })
105113 continue
106114 }
107115
108- a .emit (Event {Type : "tool_call" , Content : fmt .Sprintf ("%s(%v)" , call .Tool , call .Args )})
116+ argsMap := make (map [string ]interface {})
117+ for k , v := range call .Args {
118+ argsMap [k ] = v
119+ }
120+ a .emit (Event {
121+ Type : string (EventToolExecutionStart ),
122+ ToolName : call .Tool ,
123+ ToolCallID : call .Tool + "-" + fmt .Sprintf ("%d" , i ),
124+ Args : argsMap ,
125+ })
109126 a .logger .Info ("executing tool" , "tool" , call .Tool , "args" , call .Args )
110127
111128 result , err := tool .Execute (ctx , call .Args )
129+ isError := false
112130 if err != nil {
113131 result = fmt .Sprintf ("ERROR: %s\n Output: %s" , err .Error (), result )
132+ isError = true
114133 }
115134
116- a .emit (Event {Type : "tool_result" , Content : result })
135+ a .emit (Event {
136+ Type : string (EventToolExecutionEnd ),
137+ ToolName : call .Tool ,
138+ Result : result ,
139+ IsError : isError ,
140+ })
117141 toolResults .WriteString (fmt .Sprintf ("Tool %s result:\n %s\n \n " , call .Tool , result ))
118142 }
119143
120144 messages = append (messages , Message {
121145 Role : "user" ,
122146 Content : toolResults .String (),
123147 })
148+ a .emit (Event {Type : string (EventTurnEnd ), Content : "" })
124149 }
125150
126151 return "" , fmt .Errorf ("agent exceeded max iterations (%d)" , maxIterations )
0 commit comments