fix(agent): clear routed agent session

This commit is contained in:
Ethan1918 committed 2026-07-04 04:14:03 +08:00
1 parent 4c5adcd78e
commit c4fb7a2001
3 files changed
+139 -2

No files matched your search

+35 -1
View File
@@ -333,7 +333,7 @@ func (al *AgentLoop) buildCommandsRuntime(
if opts == nil {
return fmt.Errorf("process options not available")
}
return al.contextManager.Clear(ctx, opts.SessionKey)
return al.clearAgentSessionContext(ctx, agent, opts.SessionKey)
}
rt.AskSideQuestion = func(ctx context.Context, question string) (string, error) {
@@ -363,6 +363,40 @@ func (al *AgentLoop) buildCommandsRuntime(
return rt
}
type contextStoreClearer interface {
ClearContextStore(ctx context.Context, sessionKey string) error
}
func (al *AgentLoop) clearAgentSessionContext(
ctx context.Context,
agent *AgentInstance,
sessionKey string,
) error {
if agent == nil || agent.Sessions == nil {
return fmt.Errorf("sessions not initialized")
}
if al != nil && al.registry != nil && agent == al.registry.GetDefaultAgent() {
if al.contextManager != nil {
return al.contextManager.Clear(ctx, sessionKey)
}
return clearAgentSessionStore(agent, sessionKey)
}
if al != nil && al.contextManager != nil {
if clearer, ok := al.contextManager.(contextStoreClearer); ok {
if err := clearer.ClearContextStore(ctx, sessionKey); err != nil {
return err
}
}
}
return clearAgentSessionStore(agent, sessionKey)
}
func clearAgentSessionStore(agent *AgentInstance, sessionKey string) error {
agent.Sessions.SetHistory(sessionKey, []providers.Message{})
agent.Sessions.SetSummary(sessionKey, "")
return agent.Sessions.Save(sessionKey)
}
func summarizeMCPToolParameters(schema any) []commands.MCPToolParameterInfo {
schemaMap := normalizeMCPSchema(schema)
properties, ok := schemaMap["properties"].(map[string]any)