From e3b5c7687757eb4300e591f69eafc716397385f2 Mon Sep 17 00:00:00 2001 From: shikihane Date: Wed, 4 Mar 2026 10:59:41 +0800 Subject: [PATCH] refactor(agent): add ForEachTool to AgentRegistry for cross-agent tool lookup Extract the pattern of iterating agents to find a named tool into AgentRegistry.ForEachTool, simplifying SetMediaStore propagation. Co-Authored-By: Claude Opus 4.6 --- pkg/agent/loop.go | 14 ++++---------- pkg/agent/registry.go | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/pkg/agent/loop.go b/pkg/agent/loop.go index a5202f1b9..099625dcc 100644 --- a/pkg/agent/loop.go +++ b/pkg/agent/loop.go @@ -378,17 +378,11 @@ func (al *AgentLoop) SetMediaStore(s media.MediaStore) { al.mediaStore = s // Propagate store to send_file tools in all agents. - for _, id := range al.registry.ListAgentIDs() { - agent, ok := al.registry.GetAgent(id) - if !ok { - continue + al.registry.ForEachTool("send_file", func(t tools.Tool) { + if sf, ok := t.(*tools.SendFileTool); ok { + sf.SetMediaStore(s) } - if tool, ok := agent.Tools.Get("send_file"); ok { - if sf, ok := tool.(*tools.SendFileTool); ok { - sf.SetMediaStore(s) - } - } - } + }) } // SetTranscriber injects a voice transcriber for agent-level audio transcription. diff --git a/pkg/agent/registry.go b/pkg/agent/registry.go index 77b846832..0e7973dc3 100644 --- a/pkg/agent/registry.go +++ b/pkg/agent/registry.go @@ -7,6 +7,7 @@ import ( "github.com/sipeed/picoclaw/pkg/logger" "github.com/sipeed/picoclaw/pkg/providers" "github.com/sipeed/picoclaw/pkg/routing" + "github.com/sipeed/picoclaw/pkg/tools" ) // AgentRegistry manages multiple agent instances and routes messages to them. @@ -100,6 +101,19 @@ func (r *AgentRegistry) CanSpawnSubagent(parentAgentID, targetAgentID string) bo return false } +// ForEachTool calls fn for every tool registered under the given name +// across all agents. This is useful for propagating dependencies (e.g. +// MediaStore) to tools after registry construction. +func (r *AgentRegistry) ForEachTool(name string, fn func(tools.Tool)) { + r.mu.RLock() + defer r.mu.RUnlock() + for _, agent := range r.agents { + if t, ok := agent.Tools.Get(name); ok { + fn(t) + } + } +} + // GetDefaultAgent returns the default agent instance. func (r *AgentRegistry) GetDefaultAgent() *AgentInstance { r.mu.RLock()