Skip nil handoffs to prevent panic during run

Signed-off-by: Bryan Frimin <bryan@getprobo.com>
This commit is contained in:
Bryan Frimin
2026-03-13 19:31:09 +01:00
parent cb4b893dfc
commit 0c3893ee8b
2 changed files with 118 additions and 2 deletions

View File

@@ -178,14 +178,20 @@ func WithTools(tools ...Tool) Option {
func WithHandoffs(agents ...*Agent) Option {
return func(a *Agent) {
for _, ag := range agents {
a.handoffs = append(a.handoffs, &Handoff{Agent: ag})
if ag != nil {
a.handoffs = append(a.handoffs, &Handoff{Agent: ag})
}
}
}
}
func WithHandoffConfigs(handoffs ...*Handoff) Option {
return func(a *Agent) {
a.handoffs = append(a.handoffs, handoffs...)
for _, h := range handoffs {
if h != nil && h.Agent != nil {
a.handoffs = append(a.handoffs, h)
}
}
}
}

View File

@@ -3103,3 +3103,113 @@ func TestResume_HandoffWithInputFilter(t *testing.T) {
assert.Equal(t, "Filtered specialist here.", result.FinalMessage().Text())
assert.Equal(t, "specialist", result.LastAgent.Name())
}
func TestRun_NilHandoffsAreSkipped(t *testing.T) {
t.Parallel()
t.Run(
"nil agent in WithHandoffs",
func(t *testing.T) {
t.Parallel()
provider := &mockProvider{
responses: []*llm.ChatCompletionResponse{
stopResponse("Hello."),
},
}
client := newTestClient(provider)
billing := agent.New(
"billing",
client,
agent.WithModel("test-model"),
)
a := agent.New(
"triage",
client,
agent.WithModel("test-model"),
agent.WithHandoffs(nil, billing, nil),
)
result, err := a.Run(
context.Background(),
[]llm.Message{userMessage("hi")},
)
require.NoError(t, err)
assert.Equal(t, "Hello.", result.FinalMessage().Text())
},
)
t.Run(
"nil handoff in WithHandoffConfigs",
func(t *testing.T) {
t.Parallel()
provider := &mockProvider{
responses: []*llm.ChatCompletionResponse{
stopResponse("Hello."),
},
}
client := newTestClient(provider)
billing := agent.New(
"billing",
client,
agent.WithModel("test-model"),
)
a := agent.New(
"triage",
client,
agent.WithModel("test-model"),
agent.WithHandoffConfigs(
nil,
agent.HandoffTo(billing),
nil,
),
)
result, err := a.Run(
context.Background(),
[]llm.Message{userMessage("hi")},
)
require.NoError(t, err)
assert.Equal(t, "Hello.", result.FinalMessage().Text())
},
)
t.Run(
"HandoffTo with nil agent in WithHandoffConfigs",
func(t *testing.T) {
t.Parallel()
provider := &mockProvider{
responses: []*llm.ChatCompletionResponse{
stopResponse("Hello."),
},
}
client := newTestClient(provider)
a := agent.New(
"triage",
client,
agent.WithModel("test-model"),
agent.WithHandoffConfigs(agent.HandoffTo(nil)),
)
result, err := a.Run(
context.Background(),
[]llm.Message{userMessage("hi")},
)
require.NoError(t, err)
assert.Equal(t, "Hello.", result.FinalMessage().Text())
},
)
}