Skip nil handoffs to prevent panic during run
Signed-off-by: Bryan Frimin <bryan@getprobo.com>
This commit is contained in:
@@ -178,14 +178,20 @@ func WithTools(tools ...Tool) Option {
|
|||||||
func WithHandoffs(agents ...*Agent) Option {
|
func WithHandoffs(agents ...*Agent) Option {
|
||||||
return func(a *Agent) {
|
return func(a *Agent) {
|
||||||
for _, ag := range agents {
|
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 {
|
func WithHandoffConfigs(handoffs ...*Handoff) Option {
|
||||||
return func(a *Agent) {
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3103,3 +3103,113 @@ func TestResume_HandoffWithInputFilter(t *testing.T) {
|
|||||||
assert.Equal(t, "Filtered specialist here.", result.FinalMessage().Text())
|
assert.Equal(t, "Filtered specialist here.", result.FinalMessage().Text())
|
||||||
assert.Equal(t, "specialist", result.LastAgent.Name())
|
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())
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user