fix: use immutable JSON string for tool call event args, improve tests
This commit is contained in:
parent
61cb340147
commit
ec28399775
3 changed files with 38 additions and 12 deletions
|
|
@ -29,7 +29,7 @@ type AgentEvent struct {
|
||||||
type ToolCallStartedData struct {
|
type ToolCallStartedData struct {
|
||||||
ID string
|
ID string
|
||||||
Name string
|
Name string
|
||||||
Args map[string]any
|
Args string
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToolCallCompletedData carries information about a completed tool call
|
// ToolCallCompletedData carries information about a completed tool call
|
||||||
|
|
|
||||||
|
|
@ -75,7 +75,7 @@ func TestMockEventListener_ReceivesEvents(t *testing.T) {
|
||||||
Data: ToolCallStartedData{
|
Data: ToolCallStartedData{
|
||||||
ID: "call_123",
|
ID: "call_123",
|
||||||
Name: "exec",
|
Name: "exec",
|
||||||
Args: map[string]any{"command": "ls"},
|
Args: `{"command":"ls"}`,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
checkData: func(t *testing.T, data any) {
|
checkData: func(t *testing.T, data any) {
|
||||||
|
|
@ -89,8 +89,8 @@ func TestMockEventListener_ReceivesEvents(t *testing.T) {
|
||||||
if d.Name != "exec" {
|
if d.Name != "exec" {
|
||||||
t.Errorf("expected Name 'exec', got %q", d.Name)
|
t.Errorf("expected Name 'exec', got %q", d.Name)
|
||||||
}
|
}
|
||||||
if d.Args["command"] != "ls" {
|
if d.Args != `{"command":"ls"}` {
|
||||||
t.Errorf("expected Args[command]='ls', got %v", d.Args["command"])
|
t.Errorf("expected Args `{\"command\":\"ls\"}`, got %q", d.Args)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
@ -205,12 +205,38 @@ func TestMockEventListener_ReceivesEvents(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAgentEventListener_NilSafe(t *testing.T) {
|
func TestFireEvent_WithListener(t *testing.T) {
|
||||||
// Verify that fireEvent with nil listener doesn't panic
|
// Create a minimal AgentLoop with just the eventListener field set
|
||||||
// This is tested indirectly through the AgentLoop, but we can
|
listener := &mockEventListener{}
|
||||||
// verify the interface contract here
|
al := &AgentLoop{eventListener: listener}
|
||||||
var listener AgentEventListener
|
|
||||||
if listener != nil {
|
al.fireEvent(AgentEvent{Type: EventThinkingStarted})
|
||||||
t.Error("expected nil listener")
|
al.fireEvent(AgentEvent{
|
||||||
|
Type: EventToolCallStarted,
|
||||||
|
Data: ToolCallStartedData{ID: "1", Name: "exec", Args: `{"command":"ls"}`},
|
||||||
|
})
|
||||||
|
|
||||||
|
events := listener.getEvents()
|
||||||
|
if len(events) != 2 {
|
||||||
|
t.Fatalf("expected 2 events, got %d", len(events))
|
||||||
|
}
|
||||||
|
if events[0].Type != EventThinkingStarted {
|
||||||
|
t.Errorf("event 0: expected EventThinkingStarted, got %d", events[0].Type)
|
||||||
|
}
|
||||||
|
if events[1].Type != EventToolCallStarted {
|
||||||
|
t.Errorf("event 1: expected EventToolCallStarted, got %d", events[1].Type)
|
||||||
|
}
|
||||||
|
d, ok := events[1].Data.(ToolCallStartedData)
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("event 1: expected ToolCallStartedData, got %T", events[1].Data)
|
||||||
|
}
|
||||||
|
if d.ID != "1" || d.Name != "exec" || d.Args != `{"command":"ls"}` {
|
||||||
|
t.Errorf("event 1: unexpected data: %+v", d)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFireEvent_WithoutListener(t *testing.T) {
|
||||||
|
al := &AgentLoop{} // No listener set
|
||||||
|
// Should not panic
|
||||||
|
al.fireEvent(AgentEvent{Type: EventThinkingStarted})
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -722,7 +722,7 @@ func (al *AgentLoop) runLLMIteration(
|
||||||
Data: ToolCallStartedData{
|
Data: ToolCallStartedData{
|
||||||
ID: tc.ID,
|
ID: tc.ID,
|
||||||
Name: tc.Name,
|
Name: tc.Name,
|
||||||
Args: tc.Arguments,
|
Args: string(argsJSON),
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue