From 32e0d10a594dfe2e4cfec05cf977c7a2f4c298bd Mon Sep 17 00:00:00 2001 From: shikihane Date: Thu, 5 Mar 2026 10:26:54 +0800 Subject: [PATCH] fix(agent,tools): adapt send_file to ctx-based channel injection after upstream refactor Replace ContextualTool interface (removed upstream) with direct ctx reading in SendFileTool.Execute, using ToolChannel/ToolChatID helpers. Remove updateToolContexts which is no longer needed since ExecuteWithContext already injects channel/chatID into ctx for all tools. Co-Authored-By: Claude Opus 4.6 --- pkg/agent/loop.go | 25 ------------------------- pkg/tools/send_file.go | 15 ++++++++++++--- 2 files changed, 12 insertions(+), 28 deletions(-) diff --git a/pkg/agent/loop.go b/pkg/agent/loop.go index 615cd6937..7bee94369 100644 --- a/pkg/agent/loop.go +++ b/pkg/agent/loop.go @@ -1185,31 +1185,6 @@ func (al *AgentLoop) runLLMIteration( return finalContent, iteration, nil } -// updateToolContexts updates the context for tools that need channel/chatID info. -func (al *AgentLoop) updateToolContexts(agent *AgentInstance, channel, chatID string) { - // Use ContextualTool interface instead of type assertions - if tool, ok := agent.Tools.Get("message"); ok { - if mt, ok := tool.(tools.ContextualTool); ok { - mt.SetContext(channel, chatID) - } - } - if tool, ok := agent.Tools.Get("spawn"); ok { - if st, ok := tool.(tools.ContextualTool); ok { - st.SetContext(channel, chatID) - } - } - if tool, ok := agent.Tools.Get("subagent"); ok { - if st, ok := tool.(tools.ContextualTool); ok { - st.SetContext(channel, chatID) - } - } - if tool, ok := agent.Tools.Get("send_file"); ok { - if sf, ok := tool.(tools.ContextualTool); ok { - sf.SetContext(channel, chatID) - } - } -} - // maybeSummarize triggers summarization if the session history exceeds thresholds. func (al *AgentLoop) maybeSummarize(agent *AgentInstance, sessionKey, channel, chatID string) { newHistory := agent.Sessions.GetHistory(sessionKey) diff --git a/pkg/tools/send_file.go b/pkg/tools/send_file.go index e54f86acc..1a03e58ed 100644 --- a/pkg/tools/send_file.go +++ b/pkg/tools/send_file.go @@ -69,13 +69,22 @@ func (t *SendFileTool) SetMediaStore(store media.MediaStore) { t.mediaStore = store } -func (t *SendFileTool) Execute(_ context.Context, args map[string]any) *ToolResult { +func (t *SendFileTool) Execute(ctx context.Context, args map[string]any) *ToolResult { path, _ := args["path"].(string) if strings.TrimSpace(path) == "" { return ErrorResult("path is required") } - if t.defaultChannel == "" || t.defaultChatID == "" { + // Prefer context-injected channel/chatID (set by ExecuteWithContext), fall back to SetContext values. + channel := ToolChannel(ctx) + if channel == "" { + channel = t.defaultChannel + } + chatID := ToolChatID(ctx) + if chatID == "" { + chatID = t.defaultChatID + } + if channel == "" || chatID == "" { return ErrorResult("no target channel/chat available") } @@ -108,7 +117,7 @@ func (t *SendFileTool) Execute(_ context.Context, args map[string]any) *ToolResu } mediaType := detectMediaType(resolved) - scope := fmt.Sprintf("tool:send_file:%s:%s", t.defaultChannel, t.defaultChatID) + scope := fmt.Sprintf("tool:send_file:%s:%s", channel, chatID) ref, err := t.mediaStore.Store(resolved, media.MediaMeta{ Filename: filename,