yao/agent/test/context.go
Max d21f9c3769 Enhance Logging Functionality in RequestLogger
- Added a `noop` check in multiple logging methods (`LLMComplete`, `ToolStart`, `ToolComplete`, `HookStart`, `HookComplete`, and `HistoryLoad`) to prevent logging when the logger is in no-operation mode.
- Improved command handling in `root.go` by removing minimum argument requirements for commands and providing help output when no arguments are given.
- Introduced an `agent` command for better organization of agent-related functionalities in the CLI.
- Implemented automatic detection of the application root directory in `run.go` to streamline the application startup process.
- Cleaned up debug print statements in `config.go` to reduce clutter in the output.
2025-12-17 12:33:34 +08:00

63 lines
1.9 KiB
Go

package test
import (
stdContext "context"
"github.com/yaoapp/yao/agent/context"
"github.com/yaoapp/yao/agent/output/message"
"github.com/yaoapp/yao/openapi/oauth/types"
)
// NewTestContext creates a new context for testing
// This is similar to newAgentNextTestContext in agent_next_test.go
// but configurable via Environment
func NewTestContext(chatID, assistantID string, env *Environment) *context.Context {
// Build authorized info from environment
authorized := &types.AuthorizedInfo{
Subject: env.UserID,
UserID: env.UserID,
TenantID: env.TeamID,
}
// Create context with standard initialization
ctx := context.New(stdContext.Background(), authorized, chatID)
ctx.ID = chatID
ctx.AssistantID = assistantID
ctx.Locale = env.Locale
ctx.Client = context.Client{
Type: env.ClientType,
UserAgent: "yao-agent-test/1.0",
IP: env.ClientIP,
}
ctx.Referer = env.Referer
ctx.Accept = context.AcceptStandard
ctx.IDGenerator = message.NewIDGenerator()
ctx.Metadata = make(map[string]interface{})
// Initialize interrupt controller
ctx.Interrupt = context.NewInterruptController()
// Close the default logger created by context.New() and use noop logger
// to suppress LLM debug output during tests
if ctx.Logger != nil {
ctx.Logger.Close()
}
ctx.Logger = context.Noop()
return ctx
}
// NewTestContextFromOptions creates a test context from test options and test case
func NewTestContextFromOptions(chatID, assistantID string, opts *Options, tc *Case) *context.Context {
// Get environment from test case (with options override)
env := tc.GetEnvironment(opts)
return NewTestContext(chatID, assistantID, env)
}
// GenerateChatID generates a unique chat ID for testing
func GenerateChatID(testID string, runNumber int) string {
if runNumber > 1 {
return "test-" + testID + "-run" + string(rune('0'+runNumber))
}
return "test-" + testID
}