yao/agent/caller/jsapi_test.go
Max 3a9f32af12 Enhance Agent Context with JSAPI Initialization and Message Handling
- Added initialization for the Agent JSAPI factory to support ctx.agent.* methods, improving agent interaction capabilities.
- Introduced a new agent object in the JSAPI context for calling other agents, enhancing modularity.
- Implemented an OnMessage callback in the context options to handle messages sent via ctx.Send(), allowing for more flexible message processing.
2026-01-25 19:42:56 +08:00

145 lines
3.6 KiB
Go

package caller_test
import (
stdContext "context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/yaoapp/yao/agent/caller"
"github.com/yaoapp/yao/agent/context"
)
func TestNewJSAPI(t *testing.T) {
ctx := context.New(stdContext.Background(), nil, "test-chat")
api := caller.NewJSAPI(ctx)
require.NotNil(t, api)
}
func TestJSAPI_Call_NoAgentGetter(t *testing.T) {
// Reset AgentGetterFunc
originalGetter := caller.AgentGetterFunc
caller.AgentGetterFunc = nil
defer func() { caller.AgentGetterFunc = originalGetter }()
ctx := context.New(stdContext.Background(), nil, "test-chat")
api := caller.NewJSAPI(ctx)
messages := []interface{}{
map[string]interface{}{
"role": "user",
"content": "Hello",
},
}
result := api.Call("test-agent", messages, nil)
require.NotNil(t, result)
r, ok := result.(*caller.Result)
require.True(t, ok)
assert.Equal(t, "test-agent", r.AgentID)
assert.Contains(t, r.Error, "agent getter not initialized")
}
func TestJSAPI_All_Empty(t *testing.T) {
ctx := context.New(stdContext.Background(), nil, "test-chat")
api := caller.NewJSAPI(ctx)
results := api.All([]interface{}{})
assert.Len(t, results, 0)
}
func TestJSAPI_Any_Empty(t *testing.T) {
ctx := context.New(stdContext.Background(), nil, "test-chat")
api := caller.NewJSAPI(ctx)
results := api.Any([]interface{}{})
assert.Len(t, results, 0)
}
func TestJSAPI_Race_Empty(t *testing.T) {
ctx := context.New(stdContext.Background(), nil, "test-chat")
api := caller.NewJSAPI(ctx)
results := api.Race([]interface{}{})
assert.Len(t, results, 0)
}
func TestJSAPI_All_InvalidRequests(t *testing.T) {
ctx := context.New(stdContext.Background(), nil, "test-chat")
api := caller.NewJSAPI(ctx)
// Mix of invalid and valid requests
requests := []interface{}{
"invalid", // Not a map
map[string]interface{}{
"messages": []interface{}{}, // Missing agent
},
map[string]interface{}{
"agent": "test-agent", // Missing messages
},
}
results := api.All(requests)
// None should produce a result (all invalid)
assert.Len(t, results, 0)
}
func TestJSAPI_Call_WithOptions(t *testing.T) {
// Reset AgentGetterFunc
originalGetter := caller.AgentGetterFunc
caller.AgentGetterFunc = nil
defer func() { caller.AgentGetterFunc = originalGetter }()
ctx := context.New(stdContext.Background(), nil, "test-chat")
api := caller.NewJSAPI(ctx)
messages := []interface{}{
map[string]interface{}{
"role": "user",
"content": "Hello",
},
}
opts := map[string]interface{}{
"connector": "gpt4",
"mode": "chat",
"metadata": map[string]interface{}{
"key": "value",
},
"skip": map[string]interface{}{
"history": true,
"trace": true,
},
}
result := api.Call("test-agent", messages, opts)
require.NotNil(t, result)
r, ok := result.(*caller.Result)
require.True(t, ok)
assert.Equal(t, "test-agent", r.AgentID)
// Still errors because AgentGetterFunc is nil
assert.Contains(t, r.Error, "agent getter not initialized")
}
func TestSetJSAPIFactory(t *testing.T) {
// Reset factory
context.AgentAPIFactory = nil
// Set factory
caller.SetJSAPIFactory()
// Verify factory is set
require.NotNil(t, context.AgentAPIFactory)
// Create a mock context
ctx := context.New(stdContext.Background(), nil, "test-chat")
// Get agent API
agentAPI := context.AgentAPIFactory(ctx)
require.NotNil(t, agentAPI)
}
func TestJSAPI_ImplementsAgentAPI(t *testing.T) {
// Verify JSAPI implements context.AgentAPI interface
ctx := context.New(stdContext.Background(), nil, "test-chat")
var _ context.AgentAPI = caller.NewJSAPI(ctx)
}