picoclaw/pkg/agent/agent_inject.go
Dark aura 1fc04f7c77 feat: implement permission tool usage from frontend to backend
Complete end-to-end permission flow for tool execution:
- Add POST /api/permission/grant endpoint with gateway proxy
- Add permission checks for list_dir, edit_file, append_file tools
- Fix frontend PermissionPrompt to call grant API instead of chat messages
- Add grantPermission() to frontend API client
- Expose PermissionCache via AgentInstance for API access
- Wire up permission grant callback through gateway to agent loop

Inspired by opencode's permission system architecture.
2026-05-06 04:21:22 +01:00

121 lines
2.8 KiB
Go

// PicoClaw - Ultra-lightweight personal AI agent
package agent
import (
"fmt"
"github.com/sipeed/picoclaw/pkg/audio/asr"
"github.com/sipeed/picoclaw/pkg/channels"
"github.com/sipeed/picoclaw/pkg/config"
"github.com/sipeed/picoclaw/pkg/media"
"github.com/sipeed/picoclaw/pkg/tools"
)
func (al *AgentLoop) RegisterTool(tool tools.Tool) {
registry := al.GetRegistry()
for _, agentID := range registry.ListAgentIDs() {
if agent, ok := registry.GetAgent(agentID); ok {
agent.Tools.Register(tool)
}
}
}
func (al *AgentLoop) SetChannelManager(cm *channels.Manager) {
al.channelManager = cm
}
func (al *AgentLoop) GetRegistry() *AgentRegistry {
al.mu.RLock()
defer al.mu.RUnlock()
return al.registry
}
// GrantPermission grants permission for a path to a specific agent.
// agentID is the normalized agent ID (e.g. "main"), path is the file path,
// duration is "once" or "session".
// Returns an error if the agent is not found or the permission cache is not initialized.
func (al *AgentLoop) GrantPermission(agentID, path, duration string) error {
registry := al.GetRegistry()
if registry == nil {
return fmt.Errorf("agent registry not initialized")
}
agent, ok := registry.GetAgent(agentID)
if !ok {
return fmt.Errorf("agent %q not found", agentID)
}
return agent.GrantPermission(path, duration)
}
func (al *AgentLoop) GetConfig() *config.Config {
al.mu.RLock()
defer al.mu.RUnlock()
return al.cfg
}
func (al *AgentLoop) SetMediaStore(s media.MediaStore) {
al.mediaStore = s
// Propagate store to all registered tools that can emit media.
registry := al.GetRegistry()
for _, agentID := range registry.ListAgentIDs() {
if agent, ok := registry.GetAgent(agentID); ok {
agent.Tools.SetMediaStore(s)
}
}
registry.ForEachTool("send_tts", func(t tools.Tool) {
if st, ok := t.(*tools.SendTTSTool); ok {
st.SetMediaStore(s)
}
})
}
func (al *AgentLoop) SetTranscriber(t asr.Transcriber) {
al.transcriber = t
}
func (al *AgentLoop) SetReloadFunc(fn func() error) {
al.reloadFunc = fn
}
func (al *AgentLoop) RecordLastChannel(channel string) error {
if al.state == nil {
return nil
}
return al.state.SetLastChannel(channel)
}
func (al *AgentLoop) RecordLastChatID(chatID string) error {
if al.state == nil {
return nil
}
return al.state.SetLastChatID(chatID)
}
func (al *AgentLoop) GetStartupInfo() map[string]any {
info := make(map[string]any)
registry := al.GetRegistry()
agent := registry.GetDefaultAgent()
if agent == nil {
return info
}
// Tools info
toolsList := agent.Tools.List()
info["tools"] = map[string]any{
"count": len(toolsList),
"names": toolsList,
}
// Skills info
info["skills"] = agent.ContextBuilder.GetSkillsInfo()
// Agents info
info["agents"] = map[string]any{
"count": len(registry.ListAgentIDs()),
"ids": registry.ListAgentIDs(),
}
return info
}