Skip to content

Commit d945ea5

Browse files
authored
Merge pull request #429 from dgageot/improve-events-code
Improve events code
2 parents 9810cce + e894217 commit d945ea5

2 files changed

Lines changed: 24 additions & 69 deletions

File tree

cmd/root/run.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -361,17 +361,18 @@ func runWithoutTUI(ctx context.Context, agentFilename string, rt runtime.Runtime
361361
llmIsTyping := false
362362
var lastConfirmedToolCallID string
363363
for event := range rt.RunStream(loopCtx, sess) {
364-
if event.GetAgentName() != "" && (firstLoop || lastAgent != event.GetAgentName()) {
364+
agentName := event.GetAgentName()
365+
if agentName != "" && (firstLoop || lastAgent != agentName) {
365366
if !firstLoop {
366367
if llmIsTyping {
367368
fmt.Println()
368369
llmIsTyping = false
369370
}
370371
fmt.Println()
371372
}
372-
printAgentName(event.GetAgentName())
373+
printAgentName(agentName)
373374
firstLoop = false
374-
lastAgent = event.GetAgentName()
375+
lastAgent = agentName
375376
}
376377
switch e := event.(type) {
377378
case *runtime.AgentChoiceEvent:

pkg/runtime/event.go

Lines changed: 20 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
)
66

77
type Event interface {
8-
isEvent()
98
GetAgentName() string
109
}
1110

@@ -23,19 +22,15 @@ type UserMessageEvent struct {
2322
Message string `json:"message"`
2423
}
2524

25+
func (e *UserMessageEvent) GetAgentName() string { return "" }
26+
2627
func UserMessage(message string) Event {
2728
return &UserMessageEvent{
2829
Type: "user_message",
2930
Message: message,
3031
}
3132
}
3233

33-
func (e *UserMessageEvent) GetAgentName() string {
34-
return ""
35-
}
36-
37-
func (e *UserMessageEvent) isEvent() {}
38-
3934
// PartialToolCallEvent is sent when a tool call is first received (partial/complete)
4035
type PartialToolCallEvent struct {
4136
Type string `json:"type"`
@@ -53,8 +48,6 @@ func PartialToolCall(toolCall tools.ToolCall, toolDefinition tools.Tool, agentNa
5348
}
5449
}
5550

56-
func (e *PartialToolCallEvent) isEvent() {}
57-
5851
// ToolCallEvent is sent when a tool call is received
5952
type ToolCallEvent struct {
6053
Type string `json:"type"`
@@ -71,8 +64,6 @@ func ToolCall(toolCall tools.ToolCall, toolDefinition tools.Tool, agentName stri
7164
}
7265
}
7366

74-
func (e *ToolCallEvent) isEvent() {}
75-
7667
type ToolCallConfirmationEvent struct {
7768
Type string `json:"type"`
7869
ToolCall tools.ToolCall `json:"tool_call"`
@@ -88,7 +79,6 @@ func ToolCallConfirmation(toolCall tools.ToolCall, toolDefinition tools.Tool, ag
8879
AgentContext: AgentContext{AgentName: agentName},
8980
}
9081
}
91-
func (e *ToolCallConfirmationEvent) isEvent() {}
9282

9383
type ToolCallResponseEvent struct {
9484
Type string `json:"type"`
@@ -105,7 +95,6 @@ func ToolCallResponse(toolCall tools.ToolCall, response, agentName string) Event
10595
AgentContext: AgentContext{AgentName: agentName},
10696
}
10797
}
108-
func (e *ToolCallResponseEvent) isEvent() {}
10998

11099
type StreamStartedEvent struct {
111100
Type string `json:"type"`
@@ -121,41 +110,33 @@ func StreamStarted(sessionID, agentName string) Event {
121110
}
122111
}
123112

124-
func (e *StreamStartedEvent) GetAgentName() string {
125-
return e.AgentName
126-
}
127-
128-
func (e *StreamStartedEvent) isEvent() {}
129-
130113
type AgentChoiceEvent struct {
131114
Type string `json:"type"`
132115
Content string `json:"content"`
133116
AgentContext
134117
}
135118

136-
func AgentChoice(agentName, content string) Event { //nolint:gocritic
119+
func AgentChoice(agentName, content string) Event {
137120
return &AgentChoiceEvent{
138121
Type: "agent_choice",
139122
Content: content,
140123
AgentContext: AgentContext{AgentName: agentName},
141124
}
142125
}
143-
func (e *AgentChoiceEvent) isEvent() {}
144126

145127
type AgentChoiceReasoningEvent struct {
146128
Type string `json:"type"`
147129
Content string `json:"content"`
148130
AgentContext
149131
}
150132

151-
func AgentChoiceReasoning(agentName, content string) Event { //nolint:gocritic
133+
func AgentChoiceReasoning(agentName, content string) Event {
152134
return &AgentChoiceReasoningEvent{
153135
Type: "agent_choice_reasoning",
154136
Content: content,
155137
AgentContext: AgentContext{AgentName: agentName},
156138
}
157139
}
158-
func (e *AgentChoiceReasoningEvent) isEvent() {}
159140

160141
type ErrorEvent struct {
161142
Type string `json:"type"`
@@ -169,21 +150,20 @@ func Error(msg string) Event {
169150
Error: msg,
170151
}
171152
}
172-
func (e *ErrorEvent) isEvent() {}
173153

174154
type ShellOutputEvent struct {
175155
Type string `json:"type"`
176156
Output string `json:"error"`
177157
}
178158

159+
func (e *ShellOutputEvent) GetAgentName() string { return "" }
160+
179161
func ShellOutput(output string) Event {
180162
return &ShellOutputEvent{
181163
Type: "shell",
182164
Output: output,
183165
}
184166
}
185-
func (e *ShellOutputEvent) isEvent() {}
186-
func (e *ShellOutputEvent) GetAgentName() string { return "" }
187167

188168
type TokenUsageEvent struct {
189169
Type string `json:"type"`
@@ -211,7 +191,6 @@ func TokenUsage(inputTokens, outputTokens, contextLength, contextLimit int, cost
211191
},
212192
}
213193
}
214-
func (e *TokenUsageEvent) isEvent() {}
215194

216195
type SessionTitleEvent struct {
217196
Type string `json:"type"`
@@ -222,15 +201,12 @@ type SessionTitleEvent struct {
222201

223202
func SessionTitle(sessionID, title, agentName string) Event {
224203
return &SessionTitleEvent{
225-
Type: "session_title",
226-
SessionID: sessionID,
227-
Title: title,
228-
AgentContext: AgentContext{
229-
AgentName: agentName,
230-
},
204+
Type: "session_title",
205+
SessionID: sessionID,
206+
Title: title,
207+
AgentContext: AgentContext{AgentName: agentName},
231208
}
232209
}
233-
func (e *SessionTitleEvent) isEvent() {}
234210

235211
type SessionSummaryEvent struct {
236212
Type string `json:"type"`
@@ -241,15 +217,12 @@ type SessionSummaryEvent struct {
241217

242218
func SessionSummary(sessionID, summary, agentName string) Event {
243219
return &SessionSummaryEvent{
244-
Type: "session_summary",
245-
SessionID: sessionID,
246-
Summary: summary,
247-
AgentContext: AgentContext{
248-
AgentName: agentName,
249-
},
220+
Type: "session_summary",
221+
SessionID: sessionID,
222+
Summary: summary,
223+
AgentContext: AgentContext{AgentName: agentName},
250224
}
251225
}
252-
func (e *SessionSummaryEvent) isEvent() {}
253226

254227
type SessionCompactionEvent struct {
255228
Type string `json:"type"`
@@ -260,15 +233,12 @@ type SessionCompactionEvent struct {
260233

261234
func SessionCompaction(sessionID, status, agentName string) Event {
262235
return &SessionCompactionEvent{
263-
Type: "session_compaction",
264-
SessionID: sessionID,
265-
Status: status,
266-
AgentContext: AgentContext{
267-
AgentName: agentName,
268-
},
236+
Type: "session_compaction",
237+
SessionID: sessionID,
238+
Status: status,
239+
AgentContext: AgentContext{AgentName: agentName},
269240
}
270241
}
271-
func (e *SessionCompactionEvent) isEvent() {}
272242

273243
type StreamStoppedEvent struct {
274244
Type string `json:"type"`
@@ -284,12 +254,6 @@ func StreamStopped(sessionID, agentName string) Event {
284254
}
285255
}
286256

287-
func (e *StreamStoppedEvent) GetAgentName() string {
288-
return e.AgentName
289-
}
290-
291-
func (e *StreamStoppedEvent) isEvent() {}
292-
293257
type AuthorizationRequiredEvent struct {
294258
Type string `json:"type"`
295259
ServerURL string `json:"server_url"`
@@ -298,6 +262,8 @@ type AuthorizationRequiredEvent struct {
298262
AgentContext
299263
}
300264

265+
func (e *AuthorizationRequiredEvent) GetAgentName() string { return "" }
266+
301267
func AuthorizationRequired(serverURL, serverType, confirmation, agentName string) Event {
302268
return &AuthorizationRequiredEvent{
303269
Type: "authorization_required",
@@ -308,12 +274,6 @@ func AuthorizationRequired(serverURL, serverType, confirmation, agentName string
308274
}
309275
}
310276

311-
func (e *AuthorizationRequiredEvent) isEvent() {}
312-
313-
func (e *AuthorizationRequiredEvent) GetAgentName() string {
314-
return ""
315-
}
316-
317277
type MaxIterationsReachedEvent struct {
318278
Type string `json:"type"`
319279
MaxIterations int `json:"max_iterations"`
@@ -326,9 +286,3 @@ func MaxIterationsReached(maxIterations int) Event {
326286
MaxIterations: maxIterations,
327287
}
328288
}
329-
330-
func (e *MaxIterationsReachedEvent) isEvent() {}
331-
332-
func (e *MaxIterationsReachedEvent) GetAgentName() string {
333-
return e.AgentName
334-
}

0 commit comments

Comments
 (0)