The source headers, LICENSE files, and license metadata had drifted apart. Align the entire project to MIT: - Convert every source-file header to the MIT text across all comment styles (Go, TS, TSX, JS, MJS, SQL, CSS, GraphQL, shell), including SPDX-License-Identifier tags - Set the root and cookie-banner LICENSE files to the MIT text with a "MIT License" title line - Switch the package.json license fields, Docker image label, and cookie-banner README to MIT - Update docs and the genmodels header generator accordingly - Normalize copyright lines to a single format (Copyright (c) <year(s)> Probo Inc <hello@probo.com>.): unify the hello@getprobo.com and hello@probo.inc emails to hello@probo.com and the comma-separated years to a hyphenated range Genuine third-party references are intentionally left untouched: the Lucide icon attributions (Lucide is ISC) and the trivy dependency license allowlist. Signed-off-by: Sacha Al Himdani <sacha@probo.com>
316 lines
7.1 KiB
Go
316 lines
7.1 KiB
Go
// Copyright (c) 2026 Probo Inc <hello@probo.com>.
|
|
//
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
// in the Software without restriction, including without limitation the rights
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
// furnished to do so, subject to the following conditions:
|
|
//
|
|
// The above copyright notice and this permission notice shall be included in
|
|
// all copies or substantial portions of the Software.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
// SOFTWARE.
|
|
|
|
package agent_test
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"go.probo.inc/probo/pkg/agent"
|
|
"go.probo.inc/probo/pkg/llm"
|
|
)
|
|
|
|
func TestHandoffTo(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
t.Run(
|
|
"sets the target agent",
|
|
func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
target := agent.New(
|
|
"billing",
|
|
newTestClient(&mockProvider{}),
|
|
)
|
|
|
|
h := agent.HandoffTo(target)
|
|
|
|
assert.Equal(t, target, h.Agent)
|
|
},
|
|
)
|
|
|
|
t.Run(
|
|
"defaults have zero values",
|
|
func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
target := agent.New(
|
|
"support",
|
|
newTestClient(&mockProvider{}),
|
|
)
|
|
|
|
h := agent.HandoffTo(target)
|
|
|
|
assert.Empty(t, h.ToolName)
|
|
assert.Empty(t, h.ToolDescription)
|
|
assert.Nil(t, h.InputFilter)
|
|
assert.Nil(t, h.OnHandoff)
|
|
},
|
|
)
|
|
|
|
t.Run(
|
|
"applies multiple options",
|
|
func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
target := agent.New(
|
|
"escalation",
|
|
newTestClient(&mockProvider{}),
|
|
)
|
|
|
|
filter := func(data agent.HandoffInputData) []llm.Message {
|
|
return data.NewItems
|
|
}
|
|
|
|
callback := func(_ context.Context) error {
|
|
return nil
|
|
}
|
|
|
|
h := agent.HandoffTo(
|
|
target,
|
|
agent.WithHandoffToolName("escalate"),
|
|
agent.WithHandoffToolDescription("Escalate to senior agent"),
|
|
agent.WithHandoffInputFilter(filter),
|
|
agent.WithOnHandoff(callback),
|
|
)
|
|
|
|
assert.Equal(t, target, h.Agent)
|
|
assert.Equal(t, "escalate", h.ToolName)
|
|
assert.Equal(t, "Escalate to senior agent", h.ToolDescription)
|
|
assert.NotNil(t, h.InputFilter)
|
|
assert.NotNil(t, h.OnHandoff)
|
|
},
|
|
)
|
|
}
|
|
|
|
func TestWithHandoffToolName(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
target := agent.New("billing", newTestClient(&mockProvider{}))
|
|
h := agent.HandoffTo(target, agent.WithHandoffToolName("ask_billing"))
|
|
|
|
assert.Equal(t, "ask_billing", h.ToolName)
|
|
}
|
|
|
|
func TestWithHandoffToolDescription(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
target := agent.New("billing", newTestClient(&mockProvider{}))
|
|
h := agent.HandoffTo(
|
|
target,
|
|
agent.WithHandoffToolDescription("Route billing questions"),
|
|
)
|
|
|
|
assert.Equal(t, "Route billing questions", h.ToolDescription)
|
|
}
|
|
|
|
func TestWithHandoffInputFilter(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
t.Run(
|
|
"sets the filter function",
|
|
func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
target := agent.New("specialist", newTestClient(&mockProvider{}))
|
|
h := agent.HandoffTo(
|
|
target,
|
|
agent.WithHandoffInputFilter(func(data agent.HandoffInputData) []llm.Message {
|
|
return data.NewItems
|
|
}),
|
|
)
|
|
|
|
assert.NotNil(t, h.InputFilter)
|
|
},
|
|
)
|
|
|
|
t.Run(
|
|
"filter receives correct data and returns filtered messages",
|
|
func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
history := []llm.Message{
|
|
userMessage("old message"),
|
|
assistantMessage("old reply"),
|
|
}
|
|
newItems := []llm.Message{
|
|
userMessage("new question"),
|
|
}
|
|
|
|
target := agent.New("specialist", newTestClient(&mockProvider{}))
|
|
h := agent.HandoffTo(
|
|
target,
|
|
agent.WithHandoffInputFilter(func(data agent.HandoffInputData) []llm.Message {
|
|
var filtered []llm.Message
|
|
|
|
for _, m := range data.NewItems {
|
|
if m.Role == llm.RoleUser {
|
|
filtered = append(filtered, m)
|
|
}
|
|
}
|
|
|
|
return filtered
|
|
}),
|
|
)
|
|
|
|
result := h.InputFilter(agent.HandoffInputData{
|
|
InputHistory: history,
|
|
NewItems: newItems,
|
|
})
|
|
|
|
require.Len(t, result, 1)
|
|
assert.Equal(t, llm.RoleUser, result[0].Role)
|
|
},
|
|
)
|
|
|
|
t.Run(
|
|
"filter can combine history and new items",
|
|
func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
history := []llm.Message{
|
|
userMessage("context"),
|
|
}
|
|
newItems := []llm.Message{
|
|
userMessage("question"),
|
|
}
|
|
|
|
target := agent.New("specialist", newTestClient(&mockProvider{}))
|
|
h := agent.HandoffTo(
|
|
target,
|
|
agent.WithHandoffInputFilter(func(data agent.HandoffInputData) []llm.Message {
|
|
all := make([]llm.Message, 0, len(data.InputHistory)+len(data.NewItems))
|
|
all = append(all, data.InputHistory...)
|
|
all = append(all, data.NewItems...)
|
|
|
|
return all
|
|
}),
|
|
)
|
|
|
|
result := h.InputFilter(agent.HandoffInputData{
|
|
InputHistory: history,
|
|
NewItems: newItems,
|
|
})
|
|
|
|
assert.Len(t, result, 2)
|
|
},
|
|
)
|
|
|
|
t.Run(
|
|
"filter can return empty slice",
|
|
func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
target := agent.New("specialist", newTestClient(&mockProvider{}))
|
|
h := agent.HandoffTo(
|
|
target,
|
|
agent.WithHandoffInputFilter(func(_ agent.HandoffInputData) []llm.Message {
|
|
return nil
|
|
}),
|
|
)
|
|
|
|
result := h.InputFilter(agent.HandoffInputData{
|
|
InputHistory: []llm.Message{userMessage("hello")},
|
|
NewItems: []llm.Message{userMessage("world")},
|
|
})
|
|
|
|
assert.Empty(t, result)
|
|
},
|
|
)
|
|
}
|
|
|
|
func TestWithOnHandoff(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
t.Run(
|
|
"sets the callback",
|
|
func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
target := agent.New("billing", newTestClient(&mockProvider{}))
|
|
h := agent.HandoffTo(
|
|
target,
|
|
agent.WithOnHandoff(func(_ context.Context) error {
|
|
return nil
|
|
}),
|
|
)
|
|
|
|
assert.NotNil(t, h.OnHandoff)
|
|
},
|
|
)
|
|
|
|
t.Run(
|
|
"callback is invocable",
|
|
func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
var called bool
|
|
|
|
target := agent.New("billing", newTestClient(&mockProvider{}))
|
|
h := agent.HandoffTo(
|
|
target,
|
|
agent.WithOnHandoff(func(_ context.Context) error {
|
|
called = true
|
|
return nil
|
|
}),
|
|
)
|
|
|
|
err := h.OnHandoff(context.Background())
|
|
require.NoError(t, err)
|
|
assert.True(t, called)
|
|
},
|
|
)
|
|
|
|
t.Run(
|
|
"callback propagates errors",
|
|
func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
target := agent.New("billing", newTestClient(&mockProvider{}))
|
|
h := agent.HandoffTo(
|
|
target,
|
|
agent.WithOnHandoff(func(_ context.Context) error {
|
|
return assert.AnError
|
|
}),
|
|
)
|
|
|
|
err := h.OnHandoff(context.Background())
|
|
assert.ErrorIs(t, err, assert.AnError)
|
|
},
|
|
)
|
|
}
|
|
|
|
func TestHandoffTo_OptionOrder(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
target := agent.New("billing", newTestClient(&mockProvider{}))
|
|
|
|
h := agent.HandoffTo(
|
|
target,
|
|
agent.WithHandoffToolName("first_name"),
|
|
agent.WithHandoffToolName("second_name"),
|
|
)
|
|
|
|
assert.Equal(t, "second_name", h.ToolName, "last option wins")
|
|
}
|