mirror of
https://github.com/sipeed/picoclaw.git
synced 2026-06-12 18:08:54 +00:00
c368b5b359
* feat(feishu): implement SendMedia and add send_file tool Add outbound media support for the Feishu channel so the agent can send images and files to users via the MediaStore pipeline. Feishu channel: - SendMedia dispatches media parts as image or file uploads - sendImage uploads via Image.Create then sends image message - sendFile uploads via File.Create then sends file message - feishuFileType maps extensions to Feishu file_type values send_file tool: - New tool lets the LLM send a local file to the current chat - Validates path, registers file in MediaStore, returns media ref - Agent loop wires tool registration, MediaStore propagation, and context updates Tested on Radxa Cubie A7A (arm64) with Feishu websocket channel. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix(agent): publish outbound media regardless of SendResponse flag The SendResponse flag controls whether the agent loop publishes the final text response (callers that publish it themselves set this to false). However, the media publish path was also gated behind this flag, which meant tool-produced media was silently dropped for normal channel messages. Media should be published immediately when a tool returns media refs, independent of how the text response is delivered. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix(tools): use magic-bytes MIME detection and add file size limit to send_file - Replace hardcoded extension-to-MIME map with h2non/filetype (magic bytes) + mime.TypeByExtension fallback, consistent with the vision pipeline in resolveMediaRefs - Add configurable max file size check (defaults to config.DefaultMaxMediaSize, 20 MB) to prevent oversized uploads - Add tests for magic-bytes detection, extension fallback, size limit, and default max size Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * 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 <noreply@anthropic.com> * 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 <noreply@anthropic.com> * feat(tools): support toggling send_file tool via config Add SendFileConfig with Enabled field to ToolsConfig, defaulting to true. Wrap send_file tool registration in loop.go with the config check, consistent with the pattern used by other tools. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
129 lines
3.4 KiB
Go
129 lines
3.4 KiB
Go
package agent
|
|
|
|
import (
|
|
"sync"
|
|
|
|
"github.com/sipeed/picoclaw/pkg/config"
|
|
"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.
|
|
type AgentRegistry struct {
|
|
agents map[string]*AgentInstance
|
|
resolver *routing.RouteResolver
|
|
mu sync.RWMutex
|
|
}
|
|
|
|
// NewAgentRegistry creates a registry from config, instantiating all agents.
|
|
func NewAgentRegistry(
|
|
cfg *config.Config,
|
|
provider providers.LLMProvider,
|
|
) *AgentRegistry {
|
|
registry := &AgentRegistry{
|
|
agents: make(map[string]*AgentInstance),
|
|
resolver: routing.NewRouteResolver(cfg),
|
|
}
|
|
|
|
agentConfigs := cfg.Agents.List
|
|
if len(agentConfigs) == 0 {
|
|
implicitAgent := &config.AgentConfig{
|
|
ID: "main",
|
|
Default: true,
|
|
}
|
|
instance := NewAgentInstance(implicitAgent, &cfg.Agents.Defaults, cfg, provider)
|
|
registry.agents["main"] = instance
|
|
logger.InfoCF("agent", "Created implicit main agent (no agents.list configured)", nil)
|
|
} else {
|
|
for i := range agentConfigs {
|
|
ac := &agentConfigs[i]
|
|
id := routing.NormalizeAgentID(ac.ID)
|
|
instance := NewAgentInstance(ac, &cfg.Agents.Defaults, cfg, provider)
|
|
registry.agents[id] = instance
|
|
logger.InfoCF("agent", "Registered agent",
|
|
map[string]any{
|
|
"agent_id": id,
|
|
"name": ac.Name,
|
|
"workspace": instance.Workspace,
|
|
"model": instance.Model,
|
|
})
|
|
}
|
|
}
|
|
|
|
return registry
|
|
}
|
|
|
|
// GetAgent returns the agent instance for a given ID.
|
|
func (r *AgentRegistry) GetAgent(agentID string) (*AgentInstance, bool) {
|
|
r.mu.RLock()
|
|
defer r.mu.RUnlock()
|
|
id := routing.NormalizeAgentID(agentID)
|
|
agent, ok := r.agents[id]
|
|
return agent, ok
|
|
}
|
|
|
|
// ResolveRoute determines which agent handles the message.
|
|
func (r *AgentRegistry) ResolveRoute(input routing.RouteInput) routing.ResolvedRoute {
|
|
return r.resolver.ResolveRoute(input)
|
|
}
|
|
|
|
// ListAgentIDs returns all registered agent IDs.
|
|
func (r *AgentRegistry) ListAgentIDs() []string {
|
|
r.mu.RLock()
|
|
defer r.mu.RUnlock()
|
|
ids := make([]string, 0, len(r.agents))
|
|
for id := range r.agents {
|
|
ids = append(ids, id)
|
|
}
|
|
return ids
|
|
}
|
|
|
|
// CanSpawnSubagent checks if parentAgentID is allowed to spawn targetAgentID.
|
|
func (r *AgentRegistry) CanSpawnSubagent(parentAgentID, targetAgentID string) bool {
|
|
parent, ok := r.GetAgent(parentAgentID)
|
|
if !ok {
|
|
return false
|
|
}
|
|
if parent.Subagents == nil || parent.Subagents.AllowAgents == nil {
|
|
return false
|
|
}
|
|
targetNorm := routing.NormalizeAgentID(targetAgentID)
|
|
for _, allowed := range parent.Subagents.AllowAgents {
|
|
if allowed == "*" {
|
|
return true
|
|
}
|
|
if routing.NormalizeAgentID(allowed) == targetNorm {
|
|
return true
|
|
}
|
|
}
|
|
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()
|
|
defer r.mu.RUnlock()
|
|
if agent, ok := r.agents["main"]; ok {
|
|
return agent
|
|
}
|
|
for _, agent := range r.agents {
|
|
return agent
|
|
}
|
|
return nil
|
|
}
|