refactor(agent): update buildAgentContext to include assistantID

- Modified the buildAgentContext function to accept assistantID as a parameter, enhancing the context building process for agent calls.
- Updated multiple Call methods to pass the assistantID, ensuring proper context management during agent interactions.
This commit is contained in:
Max 2026-03-25 18:03:48 +08:00
parent abde3adf13
commit 2b0ac0d270

View file

@ -191,7 +191,7 @@ func (c *AgentCaller) Call(ctx *robottypes.Context, assistantID string, messages
} }
// Convert robot context to agent context // Convert robot context to agent context
agentCtx := c.buildAgentContext(ctx) agentCtx := c.buildAgentContext(ctx, assistantID)
defer agentCtx.Release() // IMPORTANT: Release agent context to prevent resource leaks defer agentCtx.Release() // IMPORTANT: Release agent context to prevent resource leaks
// Call assistant with streaming // Call assistant with streaming
@ -294,7 +294,7 @@ func (c *AgentCaller) CallStream(ctx *robottypes.Context, assistantID string, me
} }
} }
agentCtx := c.buildAgentContext(ctx) agentCtx := c.buildAgentContext(ctx, assistantID)
defer agentCtx.Release() defer agentCtx.Release()
response, err := ast.Stream(agentCtx, messages, opts) response, err := ast.Stream(agentCtx, messages, opts)
@ -353,7 +353,7 @@ func (c *AgentCaller) CallStreamRaw(ctx *robottypes.Context, assistantID string,
opts.OnMessage = onMessage opts.OnMessage = onMessage
} }
agentCtx := c.buildAgentContext(ctx) agentCtx := c.buildAgentContext(ctx, assistantID)
defer agentCtx.Release() defer agentCtx.Release()
response, err := ast.Stream(agentCtx, messages, opts) response, err := ast.Stream(agentCtx, messages, opts)
@ -390,7 +390,7 @@ func (c *AgentCaller) CallWithMessagesStreamRaw(ctx *robottypes.Context, assista
} }
// buildAgentContext converts robot context to agent context // buildAgentContext converts robot context to agent context
func (c *AgentCaller) buildAgentContext(ctx *robottypes.Context) *agentcontext.Context { func (c *AgentCaller) buildAgentContext(ctx *robottypes.Context, assistantID string) *agentcontext.Context {
// Build authorized info for agent context // Build authorized info for agent context
var authorized *oauthtypes.AuthorizedInfo var authorized *oauthtypes.AuthorizedInfo
if ctx.Auth != nil { if ctx.Auth != nil {
@ -403,6 +403,7 @@ func (c *AgentCaller) buildAgentContext(ctx *robottypes.Context) *agentcontext.C
// Create a new agent context // Create a new agent context
// Use ChatID for multi-turn conversations, empty for single calls // Use ChatID for multi-turn conversations, empty for single calls
agentCtx := agentcontext.New(ctx.Context, authorized, c.ChatID) agentCtx := agentcontext.New(ctx.Context, authorized, c.ChatID)
agentCtx.AssistantID = assistantID
// Propagate locale to agent context; fall back to "en" so that // Propagate locale to agent context; fall back to "en" so that
// i18n.Tr / buildBoxDisplayName always resolve {{name}} templates. // i18n.Tr / buildBoxDisplayName always resolve {{name}} templates.