From 4b76196e2ca7f1d6364bb7e8b27410b271cb4007 Mon Sep 17 00:00:00 2001 From: wenjie Date: Thu, 16 Apr 2026 16:47:23 +0800 Subject: [PATCH 1/8] refactor(web): secure Pico websocket access behind launcher auth - stop exposing the raw Pico token to the frontend - add /api/pico/info for non-secret Pico connection metadata - proxy /pico/ws through the launcher with same-origin and dashboard auth checks - inject the upstream Pico websocket protocol server-side - update frontend chat connection flow and Vite websocket proxy path - refresh related docs and tests --- docs/guides/docker.md | 2 +- pkg/channels/pico/protocol.go | 2 - pkg/gateway/gateway.go | 24 +-- web/backend/api/config.go | 4 - web/backend/api/gateway.go | 32 +-- web/backend/api/gateway_host_test.go | 12 +- web/backend/api/gateway_test.go | 12 ++ web/backend/api/pico.go | 191 ++++++++++++------ web/backend/api/pico_test.go | 91 ++++++--- .../middleware/launcher_dashboard_auth.go | 4 + .../launcher_dashboard_auth_test.go | 20 ++ web/frontend/src/api/pico.ts | 16 +- web/frontend/src/features/chat/controller.ts | 12 +- web/frontend/vite.config.ts | 2 +- 14 files changed, 253 insertions(+), 171 deletions(-) diff --git a/docs/guides/docker.md b/docs/guides/docker.md index 6c32879a6..3ccc7a2a7 100644 --- a/docs/guides/docker.md +++ b/docs/guides/docker.md @@ -27,7 +27,7 @@ docker compose -f docker/docker-compose.yml --profile gateway up -d > **Docker Users**: By default, the Gateway listens on `127.0.0.1` which is not accessible from the host. If you need to access the health endpoints or expose ports, set `PICOCLAW_GATEWAY_HOST=0.0.0.0` in your environment or update `config.json`. > [!NOTE] -> The `gateway` profile only serves the webhook handlers (including Pico when enabled) and health endpoints on the gateway port, so it does not expose generic REST chat endpoints such as `/chat` or `/a2a`. Launcher mode adds the browser UI plus `/api/pico/token` and a `/pico/ws` proxy on the launcher port, but `/pico/ws` is also available directly on the gateway whenever the Pico channel is enabled. +> The `gateway` profile only serves the webhook handlers (including Pico when enabled) and health endpoints on the gateway port, so it does not expose generic REST chat endpoints such as `/chat` or `/a2a`. Launcher mode adds the browser UI plus `/api/pico/info` and an authenticated `/pico/ws` proxy on the launcher port, but `/pico/ws` is also available directly on the gateway whenever the Pico channel is enabled. ```bash # 5. Check logs diff --git a/pkg/channels/pico/protocol.go b/pkg/channels/pico/protocol.go index ecdc2d140..051beed1b 100644 --- a/pkg/channels/pico/protocol.go +++ b/pkg/channels/pico/protocol.go @@ -18,8 +18,6 @@ const ( TypeError = "error" TypePong = "pong" - PicoTokenPrefix = "pico-" - PayloadKeyContent = "content" PayloadKeyThought = "thought" diff --git a/pkg/gateway/gateway.go b/pkg/gateway/gateway.go index 039f45075..f58590d5b 100644 --- a/pkg/gateway/gateway.go +++ b/pkg/gateway/gateway.go @@ -9,7 +9,6 @@ import ( "path/filepath" "sort" "strconv" - "strings" "sync" "sync/atomic" "syscall" @@ -27,7 +26,7 @@ import ( _ "github.com/sipeed/picoclaw/pkg/channels/line" _ "github.com/sipeed/picoclaw/pkg/channels/maixcam" _ "github.com/sipeed/picoclaw/pkg/channels/onebot" - "github.com/sipeed/picoclaw/pkg/channels/pico" + _ "github.com/sipeed/picoclaw/pkg/channels/pico" _ "github.com/sipeed/picoclaw/pkg/channels/qq" _ "github.com/sipeed/picoclaw/pkg/channels/slack" _ "github.com/sipeed/picoclaw/pkg/channels/teams_webhook" @@ -316,8 +315,6 @@ func executeReload( ) error { defer runningServices.reloading.Store(false) - overridePicoToken(newCfg, runningServices.authToken) - return handleConfigReload(ctx, agentLoop, newCfg, provider, runningServices, msgBus, allowEmptyStartup, debug) } @@ -386,8 +383,6 @@ func setupAndStartServices( fms.Start() } - overridePicoToken(cfg, authToken) - runningServices.ChannelManager, err = channels.NewManager(cfg, msgBus, runningServices.MediaStore) if err != nil { if fms, ok := runningServices.MediaStore.(*media.FileMediaStore); ok { @@ -788,23 +783,6 @@ func setupCronTool( return cronService, nil } -// overridePicoToken replaces the pico channel token with the one from the PID file. -// The PID file is the single source of truth for the pico auth token; -// it is generated once at gateway startup and remains unchanged across reloads. -func overridePicoToken(cfg *config.Config, token string) { - picoBC := cfg.Channels.GetByType(config.ChannelPico) - if picoBC == nil || !picoBC.Enabled { - return - } - var picoCfg config.PicoSettings - picoBC.Decode(&picoCfg) - picoToken := picoCfg.Token.String() - if picoToken == "" || strings.HasPrefix(picoToken, pico.PicoTokenPrefix) { - return - } - picoCfg.SetToken(pico.PicoTokenPrefix + token + picoToken) -} - func createHeartbeatHandler(agentLoop *agent.AgentLoop) func(prompt, channel, chatID string) *tools.ToolResult { return func(prompt, channel, chatID string) *tools.ToolResult { if channel == "" || chatID == "" { diff --git a/web/backend/api/config.go b/web/backend/api/config.go index 80ab80f35..c7bd21197 100644 --- a/web/backend/api/config.go +++ b/web/backend/api/config.go @@ -94,8 +94,6 @@ func (h *Handler) handleUpdateConfig(w http.ResponseWriter, r *http.Request) { return } - // Refresh cached pico token in case user changed it. - refreshPicoToken(&cfg) h.applyRuntimeLogLevel() logger.Infof("configuration updated successfully") @@ -193,8 +191,6 @@ func (h *Handler) handlePatchConfig(w http.ResponseWriter, r *http.Request) { return } - // Refresh cached pico token in case user changed it. - refreshPicoToken(&newCfg) h.applyRuntimeLogLevel() logger.Infof("configuration updated successfully") diff --git a/web/backend/api/gateway.go b/web/backend/api/gateway.go index fa5652323..ea43789d3 100644 --- a/web/backend/api/gateway.go +++ b/web/backend/api/gateway.go @@ -17,7 +17,6 @@ import ( "syscall" "time" - "github.com/sipeed/picoclaw/pkg/channels/pico" "github.com/sipeed/picoclaw/pkg/config" "github.com/sipeed/picoclaw/pkg/health" "github.com/sipeed/picoclaw/pkg/logger" @@ -37,28 +36,12 @@ var gateway = struct { startupDeadline time.Time logs *LogBuffer pidData *ppid.PidFileData // pid file data read from picoclaw.pid.json - picoToken string // cached pico token from config (for proxy auth validation) + picoToken string // cached raw pico token for upstream gateway proxy injection }{ runtimeStatus: "stopped", logs: NewLogBuffer(200), } -// refreshPicoToken updates gateway.picoToken from cfg -func refreshPicoToken(cfg *config.Config) { - gateway.mu.Lock() - defer gateway.mu.Unlock() - var picoCfg config.PicoSettings - if bc := cfg.Channels.GetByType(config.ChannelPico); bc != nil { - decoded, err := bc.GetDecoded() - if err == nil && decoded != nil { - if p, ok := decoded.(*config.PicoSettings); ok { - picoCfg = *p - } - } - } - gateway.picoToken = picoCfg.Token.String() -} - // refreshPicoTokensLocked reads the pico token from config and caches it. // Caller must hold gateway.mu (or be sole writer). func refreshPicoTokensLocked(configPath string) { @@ -101,18 +84,15 @@ const ( tokenPrefix = "token." ) -// picoComposedToken returns "pico-"+pidToken+picoToken for gateway auth. -func picoComposedToken(token string) string { +// picoGatewayProtocol returns the gateway-facing pico subprotocol that the +// launcher should inject when proxying browser traffic upstream. +func picoGatewayProtocol() string { gateway.mu.Lock() defer gateway.mu.Unlock() - // if not initial pico token, don't allow gateway auth - if gateway.picoToken == "" || gateway.pidData == nil { + if gateway.picoToken == "" { return "" } - if tokenPrefix+gateway.picoToken != token { - return "" - } - return pico.PicoTokenPrefix + gateway.pidData.Token + gateway.picoToken + return tokenPrefix + gateway.picoToken } var ( diff --git a/web/backend/api/gateway_host_test.go b/web/backend/api/gateway_host_test.go index d0fc26d7b..c9802b30b 100644 --- a/web/backend/api/gateway_host_test.go +++ b/web/backend/api/gateway_host_test.go @@ -50,7 +50,7 @@ func TestBuildWsURLUsesRequestHostWhenLauncherPublicSaved(t *testing.T) { cfg.Gateway.Host = "127.0.0.1" cfg.Gateway.Port = 18790 - req := httptest.NewRequest("GET", "http://launcher.local/api/pico/token", nil) + req := httptest.NewRequest("GET", "http://launcher.local/api/pico/info", nil) req.Host = "192.168.1.9:18800" if got := h.buildWsURL(req); got != "ws://192.168.1.9:18800/pico/ws" { @@ -181,7 +181,7 @@ func TestBuildWsURLUsesWSSWhenForwardedProtoIsHTTPS(t *testing.T) { cfg.Gateway.Host = "0.0.0.0" cfg.Gateway.Port = 18790 - req := httptest.NewRequest("GET", "http://launcher.local/api/pico/token", nil) + req := httptest.NewRequest("GET", "http://launcher.local/api/pico/info", nil) req.Host = "chat.example.com" req.Header.Set("X-Forwarded-Proto", "https") @@ -198,7 +198,7 @@ func TestBuildWsURLUsesWSSWhenRequestIsTLS(t *testing.T) { cfg.Gateway.Host = "0.0.0.0" cfg.Gateway.Port = 18790 - req := httptest.NewRequest("GET", "https://launcher.local/api/pico/token", nil) + req := httptest.NewRequest("GET", "https://launcher.local/api/pico/info", nil) req.Host = "secure.example.com" req.TLS = &tls.ConnectionState{} @@ -224,7 +224,7 @@ func TestBuildPicoURLsPreferXForwardedHost(t *testing.T) { cfg.Gateway.Host = "0.0.0.0" cfg.Gateway.Port = 18790 - req := httptest.NewRequest("GET", "http://127.0.0.1:18800/api/pico/token", nil) + req := httptest.NewRequest("GET", "http://127.0.0.1:18800/api/pico/info", nil) req.Host = "127.0.0.1:18800" req.Header.Set("X-Forwarded-Host", "vscode-tunnel.example.com") req.Header.Set("X-Forwarded-Proto", "https") @@ -249,7 +249,7 @@ func TestBuildWsURLPrefersForwardedHTTPOverTLS(t *testing.T) { cfg.Gateway.Host = "0.0.0.0" cfg.Gateway.Port = 18790 - req := httptest.NewRequest("GET", "https://launcher.local/api/pico/token", nil) + req := httptest.NewRequest("GET", "https://launcher.local/api/pico/info", nil) req.Host = "chat.example.com" req.TLS = &tls.ConnectionState{} req.Header.Set("X-Forwarded-Proto", "http") @@ -264,7 +264,7 @@ func TestBuildWsURLUsesRequestHostNotGatewayBindLoopback(t *testing.T) { h := NewHandler(configPath) h.SetServerOptions(18800, false, false, nil) - req := httptest.NewRequest("GET", "http://localhost:18800/api/pico/token", nil) + req := httptest.NewRequest("GET", "http://localhost:18800/api/pico/info", nil) req.Host = "localhost:18800" if got := h.buildWsURL(req); got != "ws://localhost:18800/pico/ws" { diff --git a/web/backend/api/gateway_test.go b/web/backend/api/gateway_test.go index 78bf34a63..998ed3317 100644 --- a/web/backend/api/gateway_test.go +++ b/web/backend/api/gateway_test.go @@ -121,6 +121,18 @@ func resetGatewayTestState(t *testing.T) { }) } +func TestPicoGatewayProtocol(t *testing.T) { + resetGatewayTestState(t) + + gateway.mu.Lock() + gateway.picoToken = "ui-token" + gateway.mu.Unlock() + + if got := picoGatewayProtocol(); got != tokenPrefix+"ui-token" { + t.Fatalf("picoGatewayProtocol() = %q, want %q", got, tokenPrefix+"ui-token") + } +} + type gatewayStartEnvSnapshot struct { GatewayHost string `json:"gateway_host"` GatewayHostSet bool `json:"gateway_host_set"` diff --git a/web/backend/api/pico.go b/web/backend/api/pico.go index 00ffb8bb2..5e4848b01 100644 --- a/web/backend/api/pico.go +++ b/web/backend/api/pico.go @@ -5,8 +5,11 @@ import ( "encoding/hex" "encoding/json" "fmt" + "net" "net/http" "net/http/httputil" + "net/url" + "strings" "time" "github.com/sipeed/picoclaw/pkg/config" @@ -16,7 +19,7 @@ import ( // registerPicoRoutes binds Pico Channel management endpoints to the ServeMux. func (h *Handler) registerPicoRoutes(mux *http.ServeMux) { - mux.HandleFunc("GET /api/pico/token", h.handleGetPicoToken) + mux.HandleFunc("GET /api/pico/info", h.handleGetPicoInfo) mux.HandleFunc("POST /api/pico/token", h.handleRegenPicoToken) mux.HandleFunc("POST /api/pico/setup", h.handlePicoSetup) @@ -28,12 +31,15 @@ func (h *Handler) registerPicoRoutes(mux *http.ServeMux) { // createWsProxy creates a reverse proxy to the current gateway WebSocket endpoint. // The gateway bind host and port are resolved from the latest configuration. -func (h *Handler) createWsProxy(origProtocol string, token string) *httputil.ReverseProxy { +func (h *Handler) createWsProxy(origProtocol string, upstreamProtocol string) *httputil.ReverseProxy { wsProxy := &httputil.ReverseProxy{ Rewrite: func(r *httputil.ProxyRequest) { target := h.gatewayProxyURL() r.SetURL(target) - r.Out.Header.Set(protocolKey, tokenPrefix+token) + r.Out.Header.Del(protocolKey) + if upstreamProtocol != "" { + r.Out.Header.Set(protocolKey, upstreamProtocol) + } }, ModifyResponse: func(r *http.Response) error { if prot := r.Header.Values(protocolKey); len(prot) > 0 { @@ -52,10 +58,104 @@ func (h *Handler) createWsProxy(origProtocol string, token string) *httputil.Rev return wsProxy } +func canonicalOrigin(raw string) (string, bool) { + u, err := url.Parse(strings.TrimSpace(raw)) + if err != nil || u == nil { + return "", false + } + + scheme := strings.ToLower(strings.TrimSpace(u.Scheme)) + if scheme != "http" && scheme != "https" { + return "", false + } + + host := strings.TrimSpace(u.Hostname()) + if host == "" { + return "", false + } + + port := u.Port() + if port == "" { + if scheme == "https" { + port = "443" + } else { + port = "80" + } + } + + return scheme + "://" + net.JoinHostPort(host, port), true +} + +func (h *Handler) expectedPicoProxyOrigin(r *http.Request) string { + return requestHTTPScheme(r) + "://" + h.picoWebUIAddr(r) +} + +func (h *Handler) validPicoProxyOrigin(r *http.Request) bool { + want, ok := canonicalOrigin(h.expectedPicoProxyOrigin(r)) + if !ok { + return false + } + + got, ok := canonicalOrigin(r.Header.Get("Origin")) + if !ok { + return false + } + + return got == want +} + +func decodePicoSettings(cfg *config.Config) (config.PicoSettings, bool) { + if cfg == nil { + return config.PicoSettings{}, false + } + + bc := cfg.Channels.GetByType(config.ChannelPico) + if bc == nil { + return config.PicoSettings{}, false + } + + var picoCfg config.PicoSettings + if err := bc.Decode(&picoCfg); err != nil { + return config.PicoSettings{}, false + } + + return picoCfg, bc.Enabled +} + +func (h *Handler) writePicoInfoResponse( + w http.ResponseWriter, + r *http.Request, + cfg *config.Config, + changed *bool, +) { + picoCfg, enabled := decodePicoSettings(cfg) + + resp := map[string]any{ + "ws_url": h.buildWsURL(r), + "enabled": enabled, + } + if changed != nil { + resp["changed"] = *changed + } + if picoCfg.Token.String() != "" { + resp["configured"] = true + } + + w.Header().Set("Content-Type", "application/json") + _ = json.NewEncoder(w).Encode(resp) +} + // handleWebSocketProxy wraps a reverse proxy to handle WebSocket connections. -// It validates the client token before forwarding; rejects immediately on failure. +// It relies on launcher dashboard auth and same-origin browser access, then +// injects the raw pico token only on the upstream gateway request. func (h *Handler) handleWebSocketProxy() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { + if !h.validPicoProxyOrigin(r) { + logger.Warnf("Invalid Pico WebSocket origin: %q", r.Header.Get("Origin")) + http.Error(w, "Forbidden", http.StatusForbidden) + return + } + gateway.mu.Lock() ensurePicoTokenCachedLocked(h.configPath) cachedPID := gateway.pidData @@ -91,51 +191,38 @@ func (h *Handler) handleWebSocketProxy() http.HandlerFunc { http.Error(w, "Gateway not available", http.StatusServiceUnavailable) return } - prot := r.Header.Values(protocolKey) - if len(prot) > 0 { - origProtocol := prot[0] - newToken := picoComposedToken(prot[0]) - if newToken != "" { - h.createWsProxy(origProtocol, newToken).ServeHTTP(w, r) - return - } + + upstreamProtocol := picoGatewayProtocol() + if upstreamProtocol == "" { + logger.Warn("Pico token unavailable for WebSocket proxy") + http.Error(w, "Pico channel not configured", http.StatusServiceUnavailable) + return } - logger.Warnf("Invalid Pico token: %v", prot) - http.Error(w, "Invalid Pico token", http.StatusForbidden) + var origProtocol string + if prot := r.Header.Values(protocolKey); len(prot) > 0 { + origProtocol = prot[0] + } + + h.createWsProxy(origProtocol, upstreamProtocol).ServeHTTP(w, r) } } -// handleGetPicoToken returns the current WS token and URL for the frontend. +// handleGetPicoInfo returns non-secret Pico connection info for the launcher UI. // -// GET /api/pico/token -func (h *Handler) handleGetPicoToken(w http.ResponseWriter, r *http.Request) { +// GET /api/pico/info +func (h *Handler) handleGetPicoInfo(w http.ResponseWriter, r *http.Request) { cfg, err := config.LoadConfig(h.configPath) if err != nil { http.Error(w, fmt.Sprintf("Failed to load config: %v", err), http.StatusInternalServerError) return } - wsURL := h.buildWsURL(r) - - w.Header().Set("Content-Type", "application/json") - bc := cfg.Channels.GetByType(config.ChannelPico) - var picoCfg config.PicoSettings - if bc != nil { - bc.Decode(&picoCfg) - } - enabled := false - if bc != nil { - enabled = bc.Enabled - } - json.NewEncoder(w).Encode(map[string]any{ - "token": picoCfg.Token.String(), - "ws_url": wsURL, - "enabled": enabled, - }) + h.writePicoInfoResponse(w, r, cfg, nil) } -// handleRegenPicoToken generates a new Pico WebSocket token and saves it. +// handleRegenPicoToken rotates the raw Pico WebSocket token and returns +// non-secret connection info for the launcher UI. // // POST /api/pico/token func (h *Handler) handleRegenPicoToken(w http.ResponseWriter, r *http.Request) { @@ -160,18 +247,7 @@ func (h *Handler) handleRegenPicoToken(w http.ResponseWriter, r *http.Request) { return } - // Refresh cached pico token. - gateway.mu.Lock() - gateway.picoToken = token - gateway.mu.Unlock() - - wsURL := h.buildWsURL(r) - - w.Header().Set("Content-Type", "application/json") - json.NewEncoder(w).Encode(map[string]any{ - "token": token, - "ws_url": wsURL, - }) + h.writePicoInfoResponse(w, r, cfg, nil) } // EnsurePicoChannel enables the Pico channel with sane defaults if it isn't @@ -234,31 +310,14 @@ func (h *Handler) handlePicoSetup(w http.ResponseWriter, r *http.Request) { return } - // Reload config (EnsurePicoChannel may have modified it) and refresh cache. + // Reload config (EnsurePicoChannel may have modified it). cfg, err := config.LoadConfig(h.configPath) if err != nil { http.Error(w, fmt.Sprintf("Failed to load config: %v", err), http.StatusInternalServerError) return } - if changed { - refreshPicoToken(cfg) - } - wsURL := h.buildWsURL(r) - - var picoCfg2 config.PicoSettings - if bc := cfg.Channels.GetByType(config.ChannelPico); bc != nil { - if decoded, err := bc.GetDecoded(); err == nil && decoded != nil { - picoCfg2 = *decoded.(*config.PicoSettings) - } - } - w.Header().Set("Content-Type", "application/json") - json.NewEncoder(w).Encode(map[string]any{ - "token": picoCfg2.Token.String(), - "ws_url": wsURL, - "enabled": true, - "changed": changed, - }) + h.writePicoInfoResponse(w, r, cfg, &changed) } // generateSecureToken creates a random 32-character hex string. diff --git a/web/backend/api/pico_test.go b/web/backend/api/pico_test.go index 807c796dc..146f9e697 100644 --- a/web/backend/api/pico_test.go +++ b/web/backend/api/pico_test.go @@ -11,11 +11,16 @@ import ( "strconv" "testing" - "github.com/sipeed/picoclaw/pkg/channels/pico" "github.com/sipeed/picoclaw/pkg/config" ppid "github.com/sipeed/picoclaw/pkg/pid" ) +func newPicoProxyRequest(method, path string) *http.Request { + req := httptest.NewRequest(method, "http://launcher.local:18800"+path, nil) + req.Header.Set("Origin", "http://launcher.local:18800") + return req +} + func TestEnsurePicoChannel_FreshConfig(t *testing.T) { configPath := filepath.Join(t.TempDir(), "config.json") h := NewHandler(configPath) @@ -365,8 +370,8 @@ func TestHandlePicoSetup_Response(t *testing.T) { t.Fatalf("failed to decode response: %v", err) } - if resp["token"] == nil || resp["token"] == "" { - t.Error("response should contain a non-empty token") + if _, ok := resp["token"]; ok { + t.Error("response must not expose the raw pico token") } if resp["ws_url"] == nil || resp["ws_url"] == "" { t.Error("response should contain ws_url") @@ -377,6 +382,45 @@ func TestHandlePicoSetup_Response(t *testing.T) { if resp["changed"] != true { t.Error("response should have changed=true on first setup") } + if resp["configured"] != true { + t.Error("response should have configured=true") + } +} + +func TestHandleGetPicoInfo_OmitsToken(t *testing.T) { + configPath := filepath.Join(t.TempDir(), "config.json") + h := NewHandler(configPath) + + if _, err := h.EnsurePicoChannel(""); err != nil { + t.Fatalf("EnsurePicoChannel() error = %v", err) + } + + req := httptest.NewRequest(http.MethodGet, "http://launcher.local/api/pico/info", nil) + rec := httptest.NewRecorder() + + h.handleGetPicoInfo(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("status = %d, want %d", rec.Code, http.StatusOK) + } + + var resp map[string]any + if err := json.NewDecoder(rec.Body).Decode(&resp); err != nil { + t.Fatalf("failed to decode response: %v", err) + } + + if _, ok := resp["token"]; ok { + t.Fatal("info response must not expose the raw pico token") + } + if resp["enabled"] != true { + t.Fatalf("enabled = %#v, want true", resp["enabled"]) + } + if resp["configured"] != true { + t.Fatalf("configured = %#v, want true", resp["configured"]) + } + if resp["ws_url"] == nil || resp["ws_url"] == "" { + t.Fatal("response should contain ws_url") + } } func TestHandleWebSocketProxyReloadsGatewayTargetFromConfig(t *testing.T) { @@ -438,20 +482,10 @@ func TestHandleWebSocketProxyReloadsGatewayTargetFromConfig(t *testing.T) { gateway.pidData = &ppid.PidFileData{} gateway.picoToken = "pico" - req1 := httptest.NewRequest(http.MethodGet, "/pico/ws", nil) - req1.Header.Set(protocolKey, tokenPrefix+"wrong_token") + req1 := newPicoProxyRequest(http.MethodGet, "/pico/ws") rec1 := httptest.NewRecorder() handler(rec1, req1) - if rec1.Code != http.StatusForbidden { - t.Fatalf("first status = %d, want %d", rec1.Code, http.StatusForbidden) - } - - req1 = httptest.NewRequest(http.MethodGet, "/pico/ws", nil) - req1.Header.Set(protocolKey, tokenPrefix+"pico") - rec1 = httptest.NewRecorder() - handler(rec1, req1) - if rec1.Code != http.StatusOK { t.Fatalf("first status = %d, want %d", rec1.Code, http.StatusOK) } @@ -464,8 +498,7 @@ func TestHandleWebSocketProxyReloadsGatewayTargetFromConfig(t *testing.T) { t.Fatalf("SaveConfig() error = %v", err) } - req2 := httptest.NewRequest(http.MethodGet, "/pico/ws", nil) - req2.Header.Set(protocolKey, tokenPrefix+"pico") + req2 := newPicoProxyRequest(http.MethodGet, "/pico/ws") rec2 := httptest.NewRecorder() handler(rec2, req2) @@ -539,8 +572,7 @@ func TestHandleWebSocketProxyLoadsCachedPicoTokenWhenMissing(t *testing.T) { gateway.pidData = &ppid.PidFileData{} gateway.picoToken = "" - req := httptest.NewRequest(http.MethodGet, "/pico/ws?session_id=test-session", nil) - req.Header.Set(protocolKey, tokenPrefix+"cached-token") + req := newPicoProxyRequest(http.MethodGet, "/pico/ws?session_id=test-session") rec := httptest.NewRecorder() handler(rec, req) @@ -625,8 +657,7 @@ func TestHandleWebSocketProxyLoadsPidDataOnDemand(t *testing.T) { setGatewayRuntimeStatusLocked("stopped") gateway.mu.Unlock() - req := httptest.NewRequest(http.MethodGet, "/pico/ws?session_id=test-session", nil) - req.Header.Set(protocolKey, tokenPrefix+"ui-token") + req := newPicoProxyRequest(http.MethodGet, "/pico/ws?session_id=test-session") rec := httptest.NewRecorder() handler(rec, req) @@ -634,7 +665,7 @@ func TestHandleWebSocketProxyLoadsPidDataOnDemand(t *testing.T) { t.Fatalf("status = %d, want %d", rec.Code, http.StatusOK) } - expected := tokenPrefix + pico.PicoTokenPrefix + pidData.Token + "ui-token" + expected := tokenPrefix + "ui-token" if got := rec.Body.String(); got != expected { t.Fatalf("forwarded protocol = %q, want %q", got, expected) } @@ -696,8 +727,7 @@ func TestHandleWebSocketProxyRejectsStalePidDataAfterProcessExit(t *testing.T) { setGatewayRuntimeStatusLocked("running") gateway.mu.Unlock() - req := httptest.NewRequest(http.MethodGet, "/pico/ws?session_id=test-session", nil) - req.Header.Set(protocolKey, tokenPrefix+"ui-token") + req := newPicoProxyRequest(http.MethodGet, "/pico/ws?session_id=test-session") rec := httptest.NewRecorder() handler(rec, req) @@ -711,6 +741,21 @@ func TestHandleWebSocketProxyRejectsStalePidDataAfterProcessExit(t *testing.T) { } } +func TestHandleWebSocketProxyRejectsInvalidOrigin(t *testing.T) { + configPath := filepath.Join(t.TempDir(), "config.json") + h := NewHandler(configPath) + handler := h.handleWebSocketProxy() + + req := httptest.NewRequest(http.MethodGet, "http://launcher.local/pico/ws", nil) + req.Header.Set("Origin", "http://evil.example") + rec := httptest.NewRecorder() + handler(rec, req) + + if rec.Code != http.StatusForbidden { + t.Fatalf("status = %d, want %d", rec.Code, http.StatusForbidden) + } +} + func mustGatewayTestPort(t *testing.T, rawURL string) int { t.Helper() diff --git a/web/backend/middleware/launcher_dashboard_auth.go b/web/backend/middleware/launcher_dashboard_auth.go index c1c4c19c6..d72bd0f00 100644 --- a/web/backend/middleware/launcher_dashboard_auth.go +++ b/web/backend/middleware/launcher_dashboard_auth.go @@ -218,6 +218,10 @@ func validLauncherDashboardAuth(r *http.Request, cfg LauncherDashboardAuthConfig } func rejectLauncherDashboardAuth(w http.ResponseWriter, r *http.Request, canonicalPath string) { + if canonicalPath == "/pico/ws" { + http.Error(w, "unauthorized", http.StatusUnauthorized) + return + } if strings.HasPrefix(canonicalPath, "/api/") { w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusUnauthorized) diff --git a/web/backend/middleware/launcher_dashboard_auth_test.go b/web/backend/middleware/launcher_dashboard_auth_test.go index 1b919bf96..7b7418998 100644 --- a/web/backend/middleware/launcher_dashboard_auth_test.go +++ b/web/backend/middleware/launcher_dashboard_auth_test.go @@ -40,6 +40,7 @@ func TestLauncherDashboardAuth_AllowsPublicPaths(t *testing.T) { {http.MethodPost, "/api/auth/logout", http.StatusTeapot}, {http.MethodGet, "/api/auth/logout", http.StatusUnauthorized}, {http.MethodGet, "/api/config", http.StatusUnauthorized}, + {http.MethodGet, "/pico/ws", http.StatusUnauthorized}, } { rec := httptest.NewRecorder() req := httptest.NewRequest(tc.method, tc.path, nil) @@ -160,3 +161,22 @@ func TestLauncherDashboardAuth_CookieAndBearer(t *testing.T) { t.Fatalf("bearer auth: status = %d", rec2.Code) } } + +func TestLauncherDashboardAuth_WebSocketUnauthorizedDoesNotRedirect(t *testing.T) { + cfg := LauncherDashboardAuthConfig{ExpectedCookie: "deadbeef", Token: "x"} + next := http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) { + t.Fatal("next handler should not run without auth") + }) + h := LauncherDashboardAuth(cfg, next) + + rec := httptest.NewRecorder() + req := httptest.NewRequest(http.MethodGet, "/pico/ws", nil) + h.ServeHTTP(rec, req) + + if rec.Code != http.StatusUnauthorized { + t.Fatalf("status = %d, want %d", rec.Code, http.StatusUnauthorized) + } + if got := rec.Header().Get("Location"); got != "" { + t.Fatalf("Location = %q, want empty", got) + } +} diff --git a/web/frontend/src/api/pico.ts b/web/frontend/src/api/pico.ts index 6b8ceb49a..ca98a06da 100644 --- a/web/frontend/src/api/pico.ts +++ b/web/frontend/src/api/pico.ts @@ -2,16 +2,16 @@ import { launcherFetch } from "@/api/http" // API client for Pico Channel configuration. -interface PicoTokenResponse { - token: string +interface PicoInfoResponse { ws_url: string enabled: boolean + configured?: boolean } interface PicoSetupResponse { - token: string ws_url: string enabled: boolean + configured?: boolean changed: boolean } @@ -25,16 +25,16 @@ async function request(path: string, options?: RequestInit): Promise { return res.json() as Promise } -export async function getPicoToken(): Promise { - return request("/api/pico/token") +export async function getPicoInfo(): Promise { + return request("/api/pico/info") } -export async function regenPicoToken(): Promise { - return request("/api/pico/token", { method: "POST" }) +export async function regenPicoToken(): Promise { + return request("/api/pico/token", { method: "POST" }) } export async function setupPico(): Promise { return request("/api/pico/setup", { method: "POST" }) } -export type { PicoTokenResponse, PicoSetupResponse } +export type { PicoInfoResponse, PicoSetupResponse } diff --git a/web/frontend/src/features/chat/controller.ts b/web/frontend/src/features/chat/controller.ts index 28ef491fa..183b1ba6f 100644 --- a/web/frontend/src/features/chat/controller.ts +++ b/web/frontend/src/features/chat/controller.ts @@ -1,7 +1,6 @@ import { getDefaultStore } from "jotai" import { toast } from "sonner" -import { getPicoToken } from "@/api/pico" import { loadSessionMessages, mergeHistoryMessages, @@ -131,7 +130,6 @@ export async function connectChat() { updateChatStore({ connectionState: "connecting" }) try { - const { token } = await getPicoToken() const sessionId = activeSessionIdRef if (generation !== connectionGeneration) { @@ -139,18 +137,10 @@ export async function connectChat() { return } - if (!token) { - console.error("No pico token available") - updateChatStore({ connectionState: "error" }) - isConnecting = false - scheduleReconnect(generation, sessionId) - return - } - const wsScheme = window.location.protocol === "https:" ? "wss:" : "ws:" const wsUrl = `${wsScheme}//${window.location.host}/pico/ws` const url = `${wsUrl}?session_id=${encodeURIComponent(sessionId)}` - const socket = new WebSocket(url, [`token.${token}`]) + const socket = new WebSocket(url) if (generation !== connectionGeneration) { isConnecting = false diff --git a/web/frontend/vite.config.ts b/web/frontend/vite.config.ts index 0ef4e1415..57512c8b9 100644 --- a/web/frontend/vite.config.ts +++ b/web/frontend/vite.config.ts @@ -29,7 +29,7 @@ export default defineConfig({ target: "http://localhost:18800", changeOrigin: true, }, - "/ws": { + "/pico/ws": { target: "ws://localhost:18800", ws: true, }, From d002e1517ba1670d094a3752a6ff469e7c8cd00e Mon Sep 17 00:00:00 2001 From: wenjie Date: Thu, 16 Apr 2026 18:31:42 +0800 Subject: [PATCH 2/8] fix(web): improve Pico URL and origin handling behind proxies - read client scheme from X-Forwarded-Proto and RFC 7239 Forwarded - derive client-visible ports from forwarded host information - add coverage for HTTPS origins without explicit ports - verify behavior when proxies omit forwarded protocol headers --- web/backend/api/gateway_host.go | 50 ++++++++++++++++++++-------- web/backend/api/gateway_host_test.go | 29 ++++++++++++---- web/backend/api/pico_test.go | 27 +++++++++++++++ 3 files changed, 86 insertions(+), 20 deletions(-) diff --git a/web/backend/api/gateway_host.go b/web/backend/api/gateway_host.go index c6c2073e2..03af7a9d3 100644 --- a/web/backend/api/gateway_host.go +++ b/web/backend/api/gateway_host.go @@ -85,8 +85,22 @@ func requestHostName(r *http.Request) string { return netbind.ResolveAdaptiveLoopbackHost() } +func forwardedProtoFirst(r *http.Request) string { + raw := strings.TrimSpace(r.Header.Get("X-Forwarded-Proto")) + if raw == "" { + raw = forwardedRFC7239Proto(r) + } + if raw == "" { + return "" + } + if i := strings.IndexByte(raw, ','); i >= 0 { + raw = strings.TrimSpace(raw[:i]) + } + return strings.ToLower(raw) +} + func requestWSScheme(r *http.Request) string { - if forwarded := strings.TrimSpace(r.Header.Get("X-Forwarded-Proto")); forwarded != "" { + if forwarded := forwardedProtoFirst(r); forwarded != "" { proto := strings.ToLower(strings.TrimSpace(strings.Split(forwarded, ",")[0])) if proto == "https" || proto == "wss" { return "wss" @@ -105,7 +119,7 @@ func requestWSScheme(r *http.Request) string { // requestHTTPScheme returns http or https for URLs that are not WebSockets (e.g. SSE). func requestHTTPScheme(r *http.Request) string { - if forwarded := strings.TrimSpace(r.Header.Get("X-Forwarded-Proto")); forwarded != "" { + if forwarded := forwardedProtoFirst(r); forwarded != "" { proto := strings.ToLower(strings.TrimSpace(strings.Split(forwarded, ",")[0])) if proto == "https" || proto == "wss" { return "https" @@ -117,6 +131,7 @@ func requestHTTPScheme(r *http.Request) string { if r.TLS != nil { return "https" } + return "http" } @@ -138,6 +153,14 @@ func forwardedHostFirst(r *http.Request) string { // forwardedRFC7239Host parses host= from the first Forwarded header element (RFC 7239). func forwardedRFC7239Host(r *http.Request) string { + return forwardedRFC7239Param(r, "host") +} + +func forwardedRFC7239Proto(r *http.Request) string { + return forwardedRFC7239Param(r, "proto") +} + +func forwardedRFC7239Param(r *http.Request, key string) string { v := strings.TrimSpace(r.Header.Get("Forwarded")) if v == "" { return "" @@ -146,7 +169,7 @@ func forwardedRFC7239Host(r *http.Request) string { for _, part := range strings.Split(first, ";") { part = strings.TrimSpace(part) low := strings.ToLower(part) - if !strings.HasPrefix(low, "host=") { + if !strings.HasPrefix(low, key+"=") { continue } val := strings.TrimSpace(part[strings.IndexByte(part, '=')+1:]) @@ -177,13 +200,21 @@ func clientVisiblePort(r *http.Request, serverListenPort int) string { if p := forwardedPortFirst(r); p != "" { return p } + if fwdHost := forwardedHostFirst(r); fwdHost != "" { + if _, port, err := net.SplitHostPort(fwdHost); err == nil && port != "" { + return port + } + } if _, port, err := net.SplitHostPort(r.Host); err == nil && port != "" { return port } + if strings.TrimSpace(r.Host) == "" && forwardedHostFirst(r) == "" { + return strconv.Itoa(serverListenPort) + } if requestHTTPScheme(r) == "https" { return "443" } - return strconv.Itoa(serverListenPort) + return "80" } // joinClientVisibleHostPort builds host:port for absolute URLs returned to the browser. @@ -205,16 +236,7 @@ func (h *Handler) picoWebUIAddr(r *http.Request) string { if fwdHost := forwardedHostFirst(r); fwdHost != "" { return joinClientVisibleHostPort(r, fwdHost, wsPort) } - host := requestHostName(r) - // Use clientVisiblePort only when an explicit port is present in headers - // or Host header — do not infer from TLS/scheme, as serverPort takes priority. - if p := forwardedPortFirst(r); p != "" { - return net.JoinHostPort(host, p) - } - if _, port, err := net.SplitHostPort(r.Host); err == nil && port != "" { - return net.JoinHostPort(host, port) - } - return net.JoinHostPort(host, strconv.Itoa(wsPort)) + return joinClientVisibleHostPort(r, requestHostName(r), wsPort) } func (h *Handler) buildWsURL(r *http.Request) string { diff --git a/web/backend/api/gateway_host_test.go b/web/backend/api/gateway_host_test.go index c9802b30b..54d1010d2 100644 --- a/web/backend/api/gateway_host_test.go +++ b/web/backend/api/gateway_host_test.go @@ -185,8 +185,8 @@ func TestBuildWsURLUsesWSSWhenForwardedProtoIsHTTPS(t *testing.T) { req.Host = "chat.example.com" req.Header.Set("X-Forwarded-Proto", "https") - if got := h.buildWsURL(req); got != "wss://chat.example.com:18800/pico/ws" { - t.Fatalf("buildWsURL() = %q, want %q", got, "wss://chat.example.com:18800/pico/ws") + if got := h.buildWsURL(req); got != "wss://chat.example.com:443/pico/ws" { + t.Fatalf("buildWsURL() = %q, want %q", got, "wss://chat.example.com:443/pico/ws") } } @@ -202,8 +202,8 @@ func TestBuildWsURLUsesWSSWhenRequestIsTLS(t *testing.T) { req.Host = "secure.example.com" req.TLS = &tls.ConnectionState{} - if got := h.buildWsURL(req); got != "wss://secure.example.com:18800/pico/ws" { - t.Fatalf("buildWsURL() = %q, want %q", got, "wss://secure.example.com:18800/pico/ws") + if got := h.buildWsURL(req); got != "wss://secure.example.com:443/pico/ws" { + t.Fatalf("buildWsURL() = %q, want %q", got, "wss://secure.example.com:443/pico/ws") } } @@ -254,8 +254,25 @@ func TestBuildWsURLPrefersForwardedHTTPOverTLS(t *testing.T) { req.TLS = &tls.ConnectionState{} req.Header.Set("X-Forwarded-Proto", "http") - if got := h.buildWsURL(req); got != "ws://chat.example.com:18800/pico/ws" { - t.Fatalf("buildWsURL() = %q, want %q", got, "ws://chat.example.com:18800/pico/ws") + if got := h.buildWsURL(req); got != "ws://chat.example.com:80/pico/ws" { + t.Fatalf("buildWsURL() = %q, want %q", got, "ws://chat.example.com:80/pico/ws") + } +} + +func TestBuildWsURLDoesNotTrustOriginWhenProxyOmitsForwardedProto(t *testing.T) { + configPath := filepath.Join(t.TempDir(), "config.json") + h := NewHandler(configPath) + + req := httptest.NewRequest("GET", "http://launcher.local/api/pico/info", nil) + req.Host = "fs-952210-xwj.picoclaw.lan.sipeed.com" + req.Header.Set("Origin", "https://fs-952210-xwj.picoclaw.lan.sipeed.com") + + if got := h.buildWsURL(req); got != "ws://fs-952210-xwj.picoclaw.lan.sipeed.com:80/pico/ws" { + t.Fatalf( + "buildWsURL() = %q, want %q", + got, + "ws://fs-952210-xwj.picoclaw.lan.sipeed.com:80/pico/ws", + ) } } diff --git a/web/backend/api/pico_test.go b/web/backend/api/pico_test.go index 146f9e697..34b011127 100644 --- a/web/backend/api/pico_test.go +++ b/web/backend/api/pico_test.go @@ -756,6 +756,33 @@ func TestHandleWebSocketProxyRejectsInvalidOrigin(t *testing.T) { } } +func TestValidPicoProxyOriginAcceptsHTTPSOriginWithoutExplicitPort(t *testing.T) { + configPath := filepath.Join(t.TempDir(), "config.json") + h := NewHandler(configPath) + + req := httptest.NewRequest(http.MethodGet, "http://launcher.local/pico/ws", nil) + req.Host = "fs-952210-xwj.picoclaw.lan.sipeed.com" + req.Header.Set("X-Forwarded-Proto", "https") + req.Header.Set("Origin", "https://fs-952210-xwj.picoclaw.lan.sipeed.com") + + if !h.validPicoProxyOrigin(req) { + t.Fatal("validPicoProxyOrigin() = false, want true") + } +} + +func TestValidPicoProxyOriginRejectsHTTPSOriginWhenProxyOmitsForwardedProto(t *testing.T) { + configPath := filepath.Join(t.TempDir(), "config.json") + h := NewHandler(configPath) + + req := httptest.NewRequest(http.MethodGet, "http://launcher.local/pico/ws", nil) + req.Host = "fs-952210-xwj.picoclaw.lan.sipeed.com" + req.Header.Set("Origin", "https://fs-952210-xwj.picoclaw.lan.sipeed.com") + + if h.validPicoProxyOrigin(req) { + t.Fatal("validPicoProxyOrigin() = true, want false") + } +} + func mustGatewayTestPort(t *testing.T, rawURL string) int { t.Helper() From f8190f04b7db62556a8e0cdb63a0a552b60752ce Mon Sep 17 00:00:00 2001 From: wenjie Date: Thu, 16 Apr 2026 19:04:47 +0800 Subject: [PATCH 3/8] fix(web): stop pinning Pico WebSocket origins during setup - remove request-origin seeding from `EnsurePicoChannel` - keep `allow_origins` empty by default for auto-configured Pico channels - relax launcher Pico WebSocket proxy origin validation - update Pico backend tests for the new setup and proxy behavior --- web/backend/api/gateway.go | 2 +- web/backend/api/pico.go | 74 +--------------- web/backend/api/pico_test.go | 159 +++++++++++++++++------------------ web/backend/main.go | 2 +- 4 files changed, 85 insertions(+), 152 deletions(-) diff --git a/web/backend/api/gateway.go b/web/backend/api/gateway.go index ea43789d3..201000ff3 100644 --- a/web/backend/api/gateway.go +++ b/web/backend/api/gateway.go @@ -732,7 +732,7 @@ func (h *Handler) startGatewayLocked(initialStatus string, existingPid int) (int gateway.logs.Reset() // Ensure Pico Channel is configured before starting gateway - changed, err := h.EnsurePicoChannel("") + changed, err := h.EnsurePicoChannel() if err != nil { logger.ErrorC("gateway", fmt.Sprintf("Warning: failed to ensure pico channel: %v", err)) // Non-fatal: gateway can still start without pico channel diff --git a/web/backend/api/pico.go b/web/backend/api/pico.go index 5e4848b01..ffd0796c7 100644 --- a/web/backend/api/pico.go +++ b/web/backend/api/pico.go @@ -5,11 +5,8 @@ import ( "encoding/hex" "encoding/json" "fmt" - "net" "net/http" "net/http/httputil" - "net/url" - "strings" "time" "github.com/sipeed/picoclaw/pkg/config" @@ -58,52 +55,6 @@ func (h *Handler) createWsProxy(origProtocol string, upstreamProtocol string) *h return wsProxy } -func canonicalOrigin(raw string) (string, bool) { - u, err := url.Parse(strings.TrimSpace(raw)) - if err != nil || u == nil { - return "", false - } - - scheme := strings.ToLower(strings.TrimSpace(u.Scheme)) - if scheme != "http" && scheme != "https" { - return "", false - } - - host := strings.TrimSpace(u.Hostname()) - if host == "" { - return "", false - } - - port := u.Port() - if port == "" { - if scheme == "https" { - port = "443" - } else { - port = "80" - } - } - - return scheme + "://" + net.JoinHostPort(host, port), true -} - -func (h *Handler) expectedPicoProxyOrigin(r *http.Request) string { - return requestHTTPScheme(r) + "://" + h.picoWebUIAddr(r) -} - -func (h *Handler) validPicoProxyOrigin(r *http.Request) bool { - want, ok := canonicalOrigin(h.expectedPicoProxyOrigin(r)) - if !ok { - return false - } - - got, ok := canonicalOrigin(r.Header.Get("Origin")) - if !ok { - return false - } - - return got == want -} - func decodePicoSettings(cfg *config.Config) (config.PicoSettings, bool) { if cfg == nil { return config.PicoSettings{}, false @@ -146,16 +97,10 @@ func (h *Handler) writePicoInfoResponse( } // handleWebSocketProxy wraps a reverse proxy to handle WebSocket connections. -// It relies on launcher dashboard auth and same-origin browser access, then -// injects the raw pico token only on the upstream gateway request. +// It relies on launcher dashboard auth, then injects the raw pico token only +// on the upstream gateway request. func (h *Handler) handleWebSocketProxy() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - if !h.validPicoProxyOrigin(r) { - logger.Warnf("Invalid Pico WebSocket origin: %q", r.Header.Get("Origin")) - http.Error(w, "Forbidden", http.StatusForbidden) - return - } - gateway.mu.Lock() ensurePicoTokenCachedLocked(h.configPath) cachedPID := gateway.pidData @@ -252,12 +197,7 @@ func (h *Handler) handleRegenPicoToken(w http.ResponseWriter, r *http.Request) { // EnsurePicoChannel enables the Pico channel with sane defaults if it isn't // already configured. Returns true when the config was modified. -// -// callerOrigin is the Origin header from the setup request. If non-empty and -// no origins are configured yet, it's written as the allowed origin so the -// WebSocket handshake works for whatever host the caller is on (LAN, custom -// port, etc.). Pass "" when there's no request context. -func (h *Handler) EnsurePicoChannel(callerOrigin string) (bool, error) { +func (h *Handler) EnsurePicoChannel() (bool, error) { cfg, err := config.LoadConfig(h.configPath) if err != nil { return false, fmt.Errorf("failed to load config: %w", err) @@ -282,12 +222,6 @@ func (h *Handler) EnsurePicoChannel(callerOrigin string) (bool, error) { picoCfg.Token = *config.NewSecureString(generateSecureToken()) changed = true } - - // Seed origins from the request instead of hardcoding ports. - if len(picoCfg.AllowOrigins) == 0 && callerOrigin != "" { - picoCfg.AllowOrigins = []string{callerOrigin} - changed = true - } } } @@ -304,7 +238,7 @@ func (h *Handler) EnsurePicoChannel(callerOrigin string) (bool, error) { // // POST /api/pico/setup func (h *Handler) handlePicoSetup(w http.ResponseWriter, r *http.Request) { - changed, err := h.EnsurePicoChannel(r.Header.Get("Origin")) + changed, err := h.EnsurePicoChannel() if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return diff --git a/web/backend/api/pico_test.go b/web/backend/api/pico_test.go index 34b011127..a56cd9ba2 100644 --- a/web/backend/api/pico_test.go +++ b/web/backend/api/pico_test.go @@ -25,7 +25,7 @@ func TestEnsurePicoChannel_FreshConfig(t *testing.T) { configPath := filepath.Join(t.TempDir(), "config.json") h := NewHandler(configPath) - changed, err := h.EnsurePicoChannel("") + changed, err := h.EnsurePicoChannel() if err != nil { t.Fatalf("EnsurePicoChannel() error = %v", err) } @@ -56,7 +56,7 @@ func TestEnsurePicoChannel_DoesNotEnableTokenQuery(t *testing.T) { configPath := filepath.Join(t.TempDir(), "config.json") h := NewHandler(configPath) - if _, err := h.EnsurePicoChannel(""); err != nil { + if _, err := h.EnsurePicoChannel(); err != nil { t.Fatalf("EnsurePicoChannel() error = %v", err) } @@ -76,11 +76,11 @@ func TestEnsurePicoChannel_DoesNotEnableTokenQuery(t *testing.T) { } } -func TestEnsurePicoChannel_DoesNotSetWildcardOrigins(t *testing.T) { +func TestEnsurePicoChannel_LeavesAllowOriginsEmptyByDefault(t *testing.T) { configPath := filepath.Join(t.TempDir(), "config.json") h := NewHandler(configPath) - if _, err := h.EnsurePicoChannel("http://localhost:18800"); err != nil { + if _, err := h.EnsurePicoChannel(); err != nil { t.Fatalf("EnsurePicoChannel() error = %v", err) } @@ -95,45 +95,16 @@ func TestEnsurePicoChannel_DoesNotSetWildcardOrigins(t *testing.T) { t.Fatalf("GetDecoded() error = %v", err) } picoCfg := decoded.(*config.PicoSettings) - for _, origin := range picoCfg.AllowOrigins { - if origin == "*" { - t.Error("setup must not set wildcard origin '*'") - } - } -} - -func TestEnsurePicoChannel_NoOriginWithoutCaller(t *testing.T) { - configPath := filepath.Join(t.TempDir(), "config.json") - h := NewHandler(configPath) - - if _, err := h.EnsurePicoChannel(""); err != nil { - t.Fatalf("EnsurePicoChannel() error = %v", err) - } - - cfg, err := config.LoadConfig(configPath) - if err != nil { - t.Fatalf("LoadConfig() error = %v", err) - } - - bc := cfg.Channels["pico"] - decoded, err := bc.GetDecoded() - if err != nil { - t.Fatalf("GetDecoded() error = %v", err) - } - picoCfg := decoded.(*config.PicoSettings) - // Without a caller origin, allow_origins stays empty (CheckOrigin - // allows all when the list is empty, so the channel still works). if len(picoCfg.AllowOrigins) != 0 { - t.Errorf("allow_origins = %v, want empty when no caller origin", picoCfg.AllowOrigins) + t.Errorf("allow_origins = %v, want empty", picoCfg.AllowOrigins) } } -func TestEnsurePicoChannel_SetsCallerOrigin(t *testing.T) { +func TestEnsurePicoChannel_NoOriginConfigurationRequired(t *testing.T) { configPath := filepath.Join(t.TempDir(), "config.json") h := NewHandler(configPath) - lanOrigin := "http://192.168.1.9:18800" - if _, err := h.EnsurePicoChannel(lanOrigin); err != nil { + if _, err := h.EnsurePicoChannel(); err != nil { t.Fatalf("EnsurePicoChannel() error = %v", err) } @@ -148,8 +119,8 @@ func TestEnsurePicoChannel_SetsCallerOrigin(t *testing.T) { t.Fatalf("GetDecoded() error = %v", err) } picoCfg := decoded.(*config.PicoSettings) - if len(picoCfg.AllowOrigins) != 1 || picoCfg.AllowOrigins[0] != lanOrigin { - t.Errorf("allow_origins = %v, want [%s]", picoCfg.AllowOrigins, lanOrigin) + if len(picoCfg.AllowOrigins) != 0 { + t.Errorf("allow_origins = %v, want empty", picoCfg.AllowOrigins) } } @@ -174,7 +145,7 @@ func TestEnsurePicoChannel_PreservesUserSettings(t *testing.T) { h := NewHandler(configPath) - changed, err := h.EnsurePicoChannel("") + changed, err := h.EnsurePicoChannel() if err != nil { t.Fatalf("EnsurePicoChannel() error = %v", err) } @@ -218,7 +189,7 @@ func TestEnsurePicoChannel_ExistingConfigWithoutSecurityFile(t *testing.T) { h := NewHandler(configPath) - changed, err := h.EnsurePicoChannel("") + changed, err := h.EnsurePicoChannel() if err != nil { t.Fatalf("EnsurePicoChannel() error = %v", err) } @@ -258,7 +229,7 @@ func TestEnsurePicoChannel_ConfiguresPicoWithoutGateway(t *testing.T) { } h := NewHandler(configPath) - if _, err := h.EnsurePicoChannel(""); err != nil { + if _, err := h.EnsurePicoChannel(); err != nil { t.Fatalf("EnsurePicoChannel() error = %v", err) } @@ -285,10 +256,8 @@ func TestEnsurePicoChannel_Idempotent(t *testing.T) { configPath := filepath.Join(t.TempDir(), "config.json") h := NewHandler(configPath) - origin := "http://localhost:18800" - // First call sets things up - if _, err := h.EnsurePicoChannel(origin); err != nil { + if _, err := h.EnsurePicoChannel(); err != nil { t.Fatalf("first EnsurePicoChannel() error = %v", err) } @@ -302,7 +271,7 @@ func TestEnsurePicoChannel_Idempotent(t *testing.T) { token1 := picoCfg.Token.String() // Second call should be a no-op - changed, err := h.EnsurePicoChannel(origin) + changed, err := h.EnsurePicoChannel() if err != nil { t.Fatalf("second EnsurePicoChannel() error = %v", err) } @@ -322,7 +291,7 @@ func TestEnsurePicoChannel_Idempotent(t *testing.T) { } } -func TestHandlePicoSetup_IncludesRequestOrigin(t *testing.T) { +func TestHandlePicoSetup_DoesNotPersistRequestOrigin(t *testing.T) { configPath := filepath.Join(t.TempDir(), "config.json") h := NewHandler(configPath) @@ -347,8 +316,8 @@ func TestHandlePicoSetup_IncludesRequestOrigin(t *testing.T) { t.Fatalf("GetDecoded() error = %v", err) } picoCfg := decoded.(*config.PicoSettings) - if len(picoCfg.AllowOrigins) != 1 || picoCfg.AllowOrigins[0] != "http://10.0.0.5:3000" { - t.Errorf("allow_origins = %v, want [http://10.0.0.5:3000]", picoCfg.AllowOrigins) + if len(picoCfg.AllowOrigins) != 0 { + t.Errorf("allow_origins = %v, want empty", picoCfg.AllowOrigins) } } @@ -391,7 +360,7 @@ func TestHandleGetPicoInfo_OmitsToken(t *testing.T) { configPath := filepath.Join(t.TempDir(), "config.json") h := NewHandler(configPath) - if _, err := h.EnsurePicoChannel(""); err != nil { + if _, err := h.EnsurePicoChannel(); err != nil { t.Fatalf("EnsurePicoChannel() error = %v", err) } @@ -741,45 +710,75 @@ func TestHandleWebSocketProxyRejectsStalePidDataAfterProcessExit(t *testing.T) { } } -func TestHandleWebSocketProxyRejectsInvalidOrigin(t *testing.T) { +func TestHandleWebSocketProxy_AllowsArbitraryOrigin(t *testing.T) { + origMatcher := gatewayProcessMatcher + gatewayProcessMatcher = func(int) (bool, bool) { return true, true } + t.Cleanup(func() { gatewayProcessMatcher = origMatcher }) + + home := t.TempDir() + t.Setenv("PICOCLAW_HOME", home) + configPath := filepath.Join(t.TempDir(), "config.json") h := NewHandler(configPath) handler := h.handleWebSocketProxy() - req := httptest.NewRequest(http.MethodGet, "http://launcher.local/pico/ws", nil) + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/pico/ws" { + t.Fatalf("path = %q, want %q", r.URL.Path, "/pico/ws") + } + w.WriteHeader(http.StatusOK) + _, _ = io.WriteString(w, "proxied") + })) + defer server.Close() + + cfg := config.DefaultConfig() + cfg.Gateway.Host = "127.0.0.1" + cfg.Gateway.Port = mustGatewayTestPort(t, server.URL) + bc := cfg.Channels["pico"] + bc.Enabled = true + decoded, err := bc.GetDecoded() + if err != nil { + t.Fatalf("GetDecoded() error = %v", err) + } + decoded.(*config.PicoSettings).SetToken("ui-token") + if err := config.SaveConfig(configPath, cfg); err != nil { + t.Fatalf("SaveConfig() error = %v", err) + } + + cmd := startGatewayLikeProcess(t) + t.Cleanup(func() { + if cmd.Process != nil { + _ = cmd.Process.Kill() + } + _ = cmd.Wait() + }) + writeTestPidFile(t, ppid.PidFileData{ + PID: cmd.Process.Pid, + Token: "test-token", + Host: cfg.Gateway.Host, + Port: cfg.Gateway.Port, + }) + t.Cleanup(func() { + ppid.RemovePidFile(globalConfigDir()) + }) + + origPidData := gateway.pidData + origPicoToken := gateway.picoToken + t.Cleanup(func() { + gateway.pidData = origPidData + gateway.picoToken = origPicoToken + }) + + gateway.pidData = &ppid.PidFileData{} + gateway.picoToken = "ui-token" + + req := httptest.NewRequest(http.MethodGet, "http://launcher.local/pico/ws?session_id=test-session", nil) req.Header.Set("Origin", "http://evil.example") rec := httptest.NewRecorder() handler(rec, req) - if rec.Code != http.StatusForbidden { - t.Fatalf("status = %d, want %d", rec.Code, http.StatusForbidden) - } -} - -func TestValidPicoProxyOriginAcceptsHTTPSOriginWithoutExplicitPort(t *testing.T) { - configPath := filepath.Join(t.TempDir(), "config.json") - h := NewHandler(configPath) - - req := httptest.NewRequest(http.MethodGet, "http://launcher.local/pico/ws", nil) - req.Host = "fs-952210-xwj.picoclaw.lan.sipeed.com" - req.Header.Set("X-Forwarded-Proto", "https") - req.Header.Set("Origin", "https://fs-952210-xwj.picoclaw.lan.sipeed.com") - - if !h.validPicoProxyOrigin(req) { - t.Fatal("validPicoProxyOrigin() = false, want true") - } -} - -func TestValidPicoProxyOriginRejectsHTTPSOriginWhenProxyOmitsForwardedProto(t *testing.T) { - configPath := filepath.Join(t.TempDir(), "config.json") - h := NewHandler(configPath) - - req := httptest.NewRequest(http.MethodGet, "http://launcher.local/pico/ws", nil) - req.Host = "fs-952210-xwj.picoclaw.lan.sipeed.com" - req.Header.Set("Origin", "https://fs-952210-xwj.picoclaw.lan.sipeed.com") - - if h.validPicoProxyOrigin(req) { - t.Fatal("validPicoProxyOrigin() = true, want false") + if rec.Code != http.StatusOK { + t.Fatalf("status = %d, want %d", rec.Code, http.StatusOK) } } diff --git a/web/backend/main.go b/web/backend/main.go index 01ef5edf0..e42558398 100644 --- a/web/backend/main.go +++ b/web/backend/main.go @@ -544,7 +544,7 @@ func main() { // API Routes (e.g. /api/status) apiHandler = api.NewHandler(absPath) apiHandler.SetDebug(debug) - if _, err = apiHandler.EnsurePicoChannel(""); err != nil { + if _, err = apiHandler.EnsurePicoChannel(); err != nil { logger.ErrorC("web", fmt.Sprintf("Warning: failed to ensure pico channel on startup: %v", err)) } apiHandler.SetServerOptions(portNum, effectivePublic, explicitPublic, launcherCfg.AllowedCIDRs) From 8461c996e5ad2f20801622a8eeec931f8966a066 Mon Sep 17 00:00:00 2001 From: wenjie Date: Mon, 20 Apr 2026 11:18:42 +0800 Subject: [PATCH 4/8] chore(web): update linting and router dependencies (#2592) Bump TanStack Router, ESLint, React Hooks plugin, TypeScript ESLint, and Prettier packages. Disable the react-hooks/set-state-in-effect rule in the frontend ESLint config. --- web/frontend/eslint.config.js | 1 + web/frontend/package.json | 14 +- web/frontend/pnpm-lock.yaml | 334 +++++++++++++++++----------------- 3 files changed, 171 insertions(+), 178 deletions(-) diff --git a/web/frontend/eslint.config.js b/web/frontend/eslint.config.js index 85d380c4f..884649e41 100644 --- a/web/frontend/eslint.config.js +++ b/web/frontend/eslint.config.js @@ -22,6 +22,7 @@ export default defineConfig([ globals: globals.browser, }, rules: { + "react-hooks/set-state-in-effect": "off", "react-refresh/only-export-components": [ "warn", { allowConstantExport: true }, diff --git a/web/frontend/package.json b/web/frontend/package.json index ad8ccbf26..835682617 100644 --- a/web/frontend/package.json +++ b/web/frontend/package.json @@ -21,8 +21,8 @@ "@tabler/icons-react": "^3.40.0", "@tailwindcss/vite": "^4.2.2", "@tanstack/react-query": "^5.99.0", - "@tanstack/react-router": "^1.168.22", - "@tanstack/react-router-devtools": "^1.163.3", + "@tanstack/react-router": "^1.168.23", + "@tanstack/react-router-devtools": "^1.166.13", "class-variance-authority": "^0.7.1", "clsx": "^2.1.1", "dayjs": "^1.11.20", @@ -55,17 +55,17 @@ "@types/node": "^25.6.0", "@types/react": "^19.2.7", "@types/react-dom": "^19.2.3", - "@typescript-eslint/eslint-plugin": "^8.57.1", + "@typescript-eslint/eslint-plugin": "^8.58.2", "@vitejs/plugin-react": "^6.0.1", - "eslint": "^10.1.0", + "eslint": "^10.2.1", "eslint-config-prettier": "^10.1.8", - "eslint-plugin-react-hooks": "^7.0.1", + "eslint-plugin-react-hooks": "^7.1.1", "eslint-plugin-react-refresh": "^0.5.2", "globals": "^17.5.0", - "prettier": "^3.8.1", + "prettier": "^3.8.3", "prettier-plugin-tailwindcss": "^0.7.2", "typescript": "~5.9.3", - "typescript-eslint": "^8.57.1", + "typescript-eslint": "^8.58.2", "vite": "^8.0.8" } } diff --git a/web/frontend/pnpm-lock.yaml b/web/frontend/pnpm-lock.yaml index 6f01c8003..210c111c5 100644 --- a/web/frontend/pnpm-lock.yaml +++ b/web/frontend/pnpm-lock.yaml @@ -21,11 +21,11 @@ importers: specifier: ^5.99.0 version: 5.99.0(react@19.2.5) '@tanstack/react-router': - specifier: ^1.168.22 - version: 1.168.22(react-dom@19.2.5(react@19.2.5))(react@19.2.5) + specifier: ^1.168.23 + version: 1.168.23(react-dom@19.2.5(react@19.2.5))(react@19.2.5) '@tanstack/react-router-devtools': - specifier: ^1.163.3 - version: 1.166.11(@tanstack/react-router@1.168.22(react-dom@19.2.5(react@19.2.5))(react@19.2.5))(@tanstack/router-core@1.168.15)(csstype@3.2.3)(react-dom@19.2.5(react@19.2.5))(react@19.2.5) + specifier: ^1.166.13 + version: 1.166.13(@tanstack/react-router@1.168.23(react-dom@19.2.5(react@19.2.5))(react@19.2.5))(@tanstack/router-core@1.168.15)(csstype@3.2.3)(react-dom@19.2.5(react@19.2.5))(react@19.2.5) class-variance-authority: specifier: ^0.7.1 version: 0.7.1 @@ -98,16 +98,16 @@ importers: devDependencies: '@eslint/js': specifier: ^10.0.1 - version: 10.0.1(eslint@10.1.0(jiti@2.6.1)) + version: 10.0.1(eslint@10.2.1(jiti@2.6.1)) '@tailwindcss/typography': specifier: ^0.5.19 version: 0.5.19(tailwindcss@4.2.2) '@tanstack/router-plugin': specifier: ^1.164.0 - version: 1.167.9(@tanstack/react-router@1.168.22(react-dom@19.2.5(react@19.2.5))(react@19.2.5))(vite@8.0.8(@types/node@25.6.0)(esbuild@0.27.4)(jiti@2.6.1)(tsx@4.21.0)) + version: 1.167.9(@tanstack/react-router@1.168.23(react-dom@19.2.5(react@19.2.5))(react@19.2.5))(vite@8.0.8(@types/node@25.6.0)(esbuild@0.27.4)(jiti@2.6.1)(tsx@4.21.0)) '@trivago/prettier-plugin-sort-imports': specifier: ^6.0.2 - version: 6.0.2(prettier@3.8.1) + version: 6.0.2(prettier@3.8.3) '@types/node': specifier: ^25.6.0 version: 25.6.0 @@ -118,38 +118,38 @@ importers: specifier: ^19.2.3 version: 19.2.3(@types/react@19.2.14) '@typescript-eslint/eslint-plugin': - specifier: ^8.57.1 - version: 8.57.2(@typescript-eslint/parser@8.57.2(eslint@10.1.0(jiti@2.6.1))(typescript@5.9.3))(eslint@10.1.0(jiti@2.6.1))(typescript@5.9.3) + specifier: ^8.58.2 + version: 8.58.2(@typescript-eslint/parser@8.58.2(eslint@10.2.1(jiti@2.6.1))(typescript@5.9.3))(eslint@10.2.1(jiti@2.6.1))(typescript@5.9.3) '@vitejs/plugin-react': specifier: ^6.0.1 version: 6.0.1(vite@8.0.8(@types/node@25.6.0)(esbuild@0.27.4)(jiti@2.6.1)(tsx@4.21.0)) eslint: - specifier: ^10.1.0 - version: 10.1.0(jiti@2.6.1) + specifier: ^10.2.1 + version: 10.2.1(jiti@2.6.1) eslint-config-prettier: specifier: ^10.1.8 - version: 10.1.8(eslint@10.1.0(jiti@2.6.1)) + version: 10.1.8(eslint@10.2.1(jiti@2.6.1)) eslint-plugin-react-hooks: - specifier: ^7.0.1 - version: 7.0.1(eslint@10.1.0(jiti@2.6.1)) + specifier: ^7.1.1 + version: 7.1.1(eslint@10.2.1(jiti@2.6.1)) eslint-plugin-react-refresh: specifier: ^0.5.2 - version: 0.5.2(eslint@10.1.0(jiti@2.6.1)) + version: 0.5.2(eslint@10.2.1(jiti@2.6.1)) globals: specifier: ^17.5.0 version: 17.5.0 prettier: - specifier: ^3.8.1 - version: 3.8.1 + specifier: ^3.8.3 + version: 3.8.3 prettier-plugin-tailwindcss: specifier: ^0.7.2 - version: 0.7.2(@trivago/prettier-plugin-sort-imports@6.0.2(prettier@3.8.1))(prettier@3.8.1) + version: 0.7.2(@trivago/prettier-plugin-sort-imports@6.0.2(prettier@3.8.3))(prettier@3.8.3) typescript: specifier: ~5.9.3 version: 5.9.3 typescript-eslint: - specifier: ^8.57.1 - version: 8.57.2(eslint@10.1.0(jiti@2.6.1))(typescript@5.9.3) + specifier: ^8.58.2 + version: 8.58.2(eslint@10.2.1(jiti@2.6.1))(typescript@5.9.3) vite: specifier: ^8.0.8 version: 8.0.8(@types/node@25.6.0)(esbuild@0.27.4)(jiti@2.6.1)(tsx@4.21.0) @@ -474,16 +474,16 @@ packages: resolution: {integrity: sha512-EriSTlt5OC9/7SXkRSCAhfSxxoSUgBm33OH+IkwbdpgoqsSsUg7y3uh+IICI/Qg4BBWr3U2i39RpmycbxMq4ew==} engines: {node: ^12.0.0 || ^14.0.0 || >=16.0.0} - '@eslint/config-array@0.23.3': - resolution: {integrity: sha512-j+eEWmB6YYLwcNOdlwQ6L2OsptI/LO6lNBuLIqe5R7RetD658HLoF+Mn7LzYmAWWNNzdC6cqP+L6r8ujeYXWLw==} + '@eslint/config-array@0.23.5': + resolution: {integrity: sha512-Y3kKLvC1dvTOT+oGlqNQ1XLqK6D1HU2YXPc52NmAlJZbMMWDzGYXMiPRJ8TYD39muD/OTjlZmNJ4ib7dvSrMBA==} engines: {node: ^20.19.0 || ^22.13.0 || >=24} - '@eslint/config-helpers@0.5.3': - resolution: {integrity: sha512-lzGN0onllOZCGroKJmRwY6QcEHxbjBw1gwB8SgRSqK8YbbtEXMvKynsXc3553ckIEBxsbMBU7oOZXKIPGZNeZw==} + '@eslint/config-helpers@0.5.5': + resolution: {integrity: sha512-eIJYKTCECbP/nsKaaruF6LW967mtbQbsw4JTtSVkUQc9MneSkbrgPJAbKl9nWr0ZeowV8BfsarBmPpBzGelA2w==} engines: {node: ^20.19.0 || ^22.13.0 || >=24} - '@eslint/core@1.1.1': - resolution: {integrity: sha512-QUPblTtE51/7/Zhfv8BDwO0qkkzQL7P/aWWbqcf4xWLEYn1oKjdO0gglQBB4GAsu7u6wjijbCmzsUTy6mnk6oQ==} + '@eslint/core@1.2.1': + resolution: {integrity: sha512-MwcE1P+AZ4C6DWlpin/OmOA54mmIZ/+xZuJiQd4SyB29oAJjN30UW9wkKNptW2ctp4cEsvhlLY/CsQ1uoHDloQ==} engines: {node: ^20.19.0 || ^22.13.0 || >=24} '@eslint/js@10.0.1': @@ -495,12 +495,12 @@ packages: eslint: optional: true - '@eslint/object-schema@3.0.3': - resolution: {integrity: sha512-iM869Pugn9Nsxbh/YHRqYiqd23AmIbxJOcpUMOuWCVNdoQJ5ZtwL6h3t0bcZzJUlC3Dq9jCFCESBZnX0GTv7iQ==} + '@eslint/object-schema@3.0.5': + resolution: {integrity: sha512-vqTaUEgxzm+YDSdElad6PiRoX4t8VGDjCtt05zn4nU810UIx/uNEV7/lZJ6KwFThKZOzOxzXy48da+No7HZaMw==} engines: {node: ^20.19.0 || ^22.13.0 || >=24} - '@eslint/plugin-kit@0.6.1': - resolution: {integrity: sha512-iH1B076HoAshH1mLpHMgwdGeTs0CYwL0SPMkGuSebZrwBp16v415e9NZXg2jtrqPVQjf6IANe2Vtlr5KswtcZQ==} + '@eslint/plugin-kit@0.7.1': + resolution: {integrity: sha512-rZAP3aVgB9ds9KOeUSL+zZ21hPmo8dh6fnIFwRQj5EAZl9gzR7wxYbYXYysAM8CTqGmUGyp2S4kUdV17MnGuWQ==} engines: {node: ^20.19.0 || ^22.13.0 || >=24} '@floating-ui/core@1.7.5': @@ -1570,20 +1570,20 @@ packages: peerDependencies: react: ^18 || ^19 - '@tanstack/react-router-devtools@1.166.11': - resolution: {integrity: sha512-WYR3q4Xui5yPT/5PXtQh8i03iUA7q8dONBjWpV3nsGdM8Cs1FxpfhLstW0wZO1dOvSyElscwTRCJ6nO5N8r3Lg==} + '@tanstack/react-router-devtools@1.166.13': + resolution: {integrity: sha512-6yKRFFJrEEOiGp5RAAuGCYsl81M4XAhJmLcu9PKj+HZle4A3dsP60lwHoqQYWHMK9nKKFkdXR+D8qxzxqtQbEA==} engines: {node: '>=20.19'} peerDependencies: - '@tanstack/react-router': ^1.168.2 - '@tanstack/router-core': ^1.168.2 + '@tanstack/react-router': ^1.168.15 + '@tanstack/router-core': ^1.168.11 react: '>=18.0.0 || >=19.0.0' react-dom: '>=18.0.0 || >=19.0.0' peerDependenciesMeta: '@tanstack/router-core': optional: true - '@tanstack/react-router@1.168.22': - resolution: {integrity: sha512-W2LyfkfJtDCf//jOjZeUBWwOVl8iDRVTECpGHa2M28MT3T5/VVnjgicYNHR/ax0Filk1iU67MRjcjHheTYvK1Q==} + '@tanstack/react-router@1.168.23': + resolution: {integrity: sha512-+GblieDnutG6oipJJPNtRJjrWF8QTZEG/l0532+BngFkVK48oHNOcvIkSoAFYftK1egAwM7KBxXsb0Ou+X6/MQ==} engines: {node: '>=20.19'} peerDependencies: react: '>=18.0.0 || >=19.0.0' @@ -1605,11 +1605,11 @@ packages: engines: {node: '>=20.19'} hasBin: true - '@tanstack/router-devtools-core@1.167.1': - resolution: {integrity: sha512-ECMM47J4KmifUvJguGituSiBpfN8SyCUEoxQks5RY09hpIBfR2eswCv2e6cJimjkKwBQXOVTPkTUk/yRvER+9w==} + '@tanstack/router-devtools-core@1.167.3': + resolution: {integrity: sha512-fJ1VMhyQgnoashTrP763c2HRc9kofgF61L7Jb3F6eTHAmCKtGVx8BRtiFt37sr3U0P0jmaaiiSPGP6nT5JtVNg==} engines: {node: '>=20.19'} peerDependencies: - '@tanstack/router-core': ^1.168.2 + '@tanstack/router-core': ^1.168.11 csstype: ^3.0.10 peerDependenciesMeta: csstype: @@ -1728,63 +1728,63 @@ packages: '@types/validate-npm-package-name@4.0.2': resolution: {integrity: sha512-lrpDziQipxCEeK5kWxvljWYhUvOiB2A9izZd9B2AFarYAkqZshb4lPbRs7zKEic6eGtH8V/2qJW+dPp9OtF6bw==} - '@typescript-eslint/eslint-plugin@8.57.2': - resolution: {integrity: sha512-NZZgp0Fm2IkD+La5PR81sd+g+8oS6JwJje+aRWsDocxHkjyRw0J5L5ZTlN3LI1LlOcGL7ph3eaIUmTXMIjLk0w==} + '@typescript-eslint/eslint-plugin@8.58.2': + resolution: {integrity: sha512-aC2qc5thQahutKjP+cl8cgN9DWe3ZUqVko30CMSZHnFEHyhOYoZSzkGtAI2mcwZ38xeImDucI4dnqsHiOYuuCw==} engines: {node: ^18.18.0 || ^20.9.0 || >=21.1.0} peerDependencies: - '@typescript-eslint/parser': ^8.57.2 + '@typescript-eslint/parser': ^8.58.2 eslint: ^8.57.0 || ^9.0.0 || ^10.0.0 - typescript: '>=4.8.4 <6.0.0' + typescript: '>=4.8.4 <6.1.0' - '@typescript-eslint/parser@8.57.2': - resolution: {integrity: sha512-30ScMRHIAD33JJQkgfGW1t8CURZtjc2JpTrq5n2HFhOefbAhb7ucc7xJwdWcrEtqUIYJ73Nybpsggii6GtAHjA==} + '@typescript-eslint/parser@8.58.2': + resolution: {integrity: sha512-/Zb/xaIDfxeJnvishjGdcR4jmr7S+bda8PKNhRGdljDM+elXhlvN0FyPSsMnLmJUrVG9aPO6dof80wjMawsASg==} engines: {node: ^18.18.0 || ^20.9.0 || >=21.1.0} peerDependencies: eslint: ^8.57.0 || ^9.0.0 || ^10.0.0 - typescript: '>=4.8.4 <6.0.0' + typescript: '>=4.8.4 <6.1.0' - '@typescript-eslint/project-service@8.57.2': - resolution: {integrity: sha512-FuH0wipFywXRTHf+bTTjNyuNQQsQC3qh/dYzaM4I4W0jrCqjCVuUh99+xd9KamUfmCGPvbO8NDngo/vsnNVqgw==} + '@typescript-eslint/project-service@8.58.2': + resolution: {integrity: sha512-Cq6UfpZZk15+r87BkIh5rDpi38W4b+Sjnb8wQCPPDDweS/LRCFjCyViEbzHk5Ck3f2QDfgmlxqSa7S7clDtlfg==} engines: {node: ^18.18.0 || ^20.9.0 || >=21.1.0} peerDependencies: - typescript: '>=4.8.4 <6.0.0' + typescript: '>=4.8.4 <6.1.0' - '@typescript-eslint/scope-manager@8.57.2': - resolution: {integrity: sha512-snZKH+W4WbWkrBqj4gUNRIGb/jipDW3qMqVJ4C9rzdFc+wLwruxk+2a5D+uoFcKPAqyqEnSb4l2ULuZf95eSkw==} + '@typescript-eslint/scope-manager@8.58.2': + resolution: {integrity: sha512-SgmyvDPexWETQek+qzZnrG6844IaO02UVyOLhI4wpo82dpZJY9+6YZCKAMFzXb7qhx37mFK1QcPQ18tud+vo6Q==} engines: {node: ^18.18.0 || ^20.9.0 || >=21.1.0} - '@typescript-eslint/tsconfig-utils@8.57.2': - resolution: {integrity: sha512-3Lm5DSM+DCowsUOJC+YqHHnKEfFh5CoGkj5Z31NQSNF4l5wdOwqGn99wmwN/LImhfY3KJnmordBq/4+VDe2eKw==} + '@typescript-eslint/tsconfig-utils@8.58.2': + resolution: {integrity: sha512-3SR+RukipDvkkKp/d0jP0dyzuls3DbGmwDpVEc5wqk5f38KFThakqAAO0XMirWAE+kT00oTauTbzMFGPoAzB0A==} engines: {node: ^18.18.0 || ^20.9.0 || >=21.1.0} peerDependencies: - typescript: '>=4.8.4 <6.0.0' + typescript: '>=4.8.4 <6.1.0' - '@typescript-eslint/type-utils@8.57.2': - resolution: {integrity: sha512-Co6ZCShm6kIbAM/s+oYVpKFfW7LBc6FXoPXjTRQ449PPNBY8U0KZXuevz5IFuuUj2H9ss40atTaf9dlGLzbWZg==} + '@typescript-eslint/type-utils@8.58.2': + resolution: {integrity: sha512-Z7EloNR/B389FvabdGeTo2XMs4W9TjtPiO9DAsmT0yom0bwlPyRjkJ1uCdW1DvrrrYP50AJZ9Xc3sByZA9+dcg==} engines: {node: ^18.18.0 || ^20.9.0 || >=21.1.0} peerDependencies: eslint: ^8.57.0 || ^9.0.0 || ^10.0.0 - typescript: '>=4.8.4 <6.0.0' + typescript: '>=4.8.4 <6.1.0' - '@typescript-eslint/types@8.57.2': - resolution: {integrity: sha512-/iZM6FnM4tnx9csuTxspMW4BOSegshwX5oBDznJ7S4WggL7Vczz5d2W11ecc4vRrQMQHXRSxzrCsyG5EsPPTbA==} + '@typescript-eslint/types@8.58.2': + resolution: {integrity: sha512-9TukXyATBQf/Jq9AMQXfvurk+G5R2MwfqQGDR2GzGz28HvY/lXNKGhkY+6IOubwcquikWk5cjlgPvD2uAA7htQ==} engines: {node: ^18.18.0 || ^20.9.0 || >=21.1.0} - '@typescript-eslint/typescript-estree@8.57.2': - resolution: {integrity: sha512-2MKM+I6g8tJxfSmFKOnHv2t8Sk3T6rF20A1Puk0svLK+uVapDZB/4pfAeB7nE83uAZrU6OxW+HmOd5wHVdXwXA==} + '@typescript-eslint/typescript-estree@8.58.2': + resolution: {integrity: sha512-ELGuoofuhhoCvNbQjFFiobFcGgcDCEm0ThWdmO4Z0UzLqPXS3KFvnEZ+SHewwOYHjM09tkzOWXNTv9u6Gqtyuw==} engines: {node: ^18.18.0 || ^20.9.0 || >=21.1.0} peerDependencies: - typescript: '>=4.8.4 <6.0.0' + typescript: '>=4.8.4 <6.1.0' - '@typescript-eslint/utils@8.57.2': - resolution: {integrity: sha512-krRIbvPK1ju1WBKIefiX+bngPs+odIQUtR7kymzPfo1POVw3jlF+nLkmexdSSd4UCbDcQn+wMBATOOmpBbqgKg==} + '@typescript-eslint/utils@8.58.2': + resolution: {integrity: sha512-QZfjHNEzPY8+l0+fIXMvuQ2sJlplB4zgDZvA+NmvZsZv3EQwOcc1DuIU1VJUTWZ/RKouBMhDyNaBMx4sWvrzRA==} engines: {node: ^18.18.0 || ^20.9.0 || >=21.1.0} peerDependencies: eslint: ^8.57.0 || ^9.0.0 || ^10.0.0 - typescript: '>=4.8.4 <6.0.0' + typescript: '>=4.8.4 <6.1.0' - '@typescript-eslint/visitor-keys@8.57.2': - resolution: {integrity: sha512-zhahknjobV2FiD6Ee9iLbS7OV9zi10rG26odsQdfBO/hjSzUQbkIYgda+iNKK1zNiW2ey+Lf8MU5btN17V3dUw==} + '@typescript-eslint/visitor-keys@8.58.2': + resolution: {integrity: sha512-f1WO2Lx8a9t8DARmcWAUPJbu0G20bJlj8L4z72K00TMeJAoyLr/tHhI/pzYBLrR4dXWkcxO1cWYZEOX8DKHTqA==} engines: {node: ^18.18.0 || ^20.9.0 || >=21.1.0} '@ungap/structured-clone@1.3.0': @@ -2205,11 +2205,11 @@ packages: peerDependencies: eslint: '>=7.0.0' - eslint-plugin-react-hooks@7.0.1: - resolution: {integrity: sha512-O0d0m04evaNzEPoSW+59Mezf8Qt0InfgGIBJnpC0h3NH/WjUAR7BIKUfysC6todmtiZ/A0oUVS8Gce0WhBrHsA==} + eslint-plugin-react-hooks@7.1.1: + resolution: {integrity: sha512-f2I7Gw6JbvCexzIInuSbZpfdQ44D7iqdWX01FKLvrPgqxoE7oMj8clOfto8U6vYiz4yd5oKu39rRSVOe1zRu0g==} engines: {node: '>=18'} peerDependencies: - eslint: ^3.0.0 || ^4.0.0 || ^5.0.0 || ^6.0.0 || ^7.0.0 || ^8.0.0-0 || ^9.0.0 + eslint: ^3.0.0 || ^4.0.0 || ^5.0.0 || ^6.0.0 || ^7.0.0 || ^8.0.0-0 || ^9.0.0 || ^10.0.0 eslint-plugin-react-refresh@0.5.2: resolution: {integrity: sha512-hmgTH57GfzoTFjVN0yBwTggnsVUF2tcqi7RJZHqi9lIezSs4eFyAMktA68YD4r5kNw1mxyY4dmkyoFDb3FIqrA==} @@ -2228,8 +2228,8 @@ packages: resolution: {integrity: sha512-tD40eHxA35h0PEIZNeIjkHoDR4YjjJp34biM0mDvplBe//mB+IHCqHDGV7pxF+7MklTvighcCPPZC7ynWyjdTA==} engines: {node: ^20.19.0 || ^22.13.0 || >=24} - eslint@10.1.0: - resolution: {integrity: sha512-S9jlY/ELKEUwwQnqWDO+f+m6sercqOPSqXM5Go94l7DOmxHVDgmSFGWEzeE/gwgTAr0W103BWt0QLe/7mabIvA==} + eslint@10.2.1: + resolution: {integrity: sha512-wiyGaKsDgqXvF40P8mDwiUp/KQjE1FdrIEJsM8PZ3XCiniTMXS3OHWWUe5FI5agoCnr8x4xPrTDZuxsBlNHl+Q==} engines: {node: ^20.19.0 || ^22.13.0 || >=24} hasBin: true peerDependencies: @@ -3040,10 +3040,6 @@ packages: resolution: {integrity: sha512-VP79XUPxV2CigYP3jWwAUFSku2aKqBH7uTAapFWCBqutsbmDo96KY5o8uh6U+/YSIn5OxJnXp73beVkpqMIGhA==} engines: {node: '>=18'} - minimatch@10.2.4: - resolution: {integrity: sha512-oRjTw/97aTBN0RHbYCdtF1MQfvusSIBQM0IZEgzl6426+8jSC0nF1a/GmnVLpfB9yyr6g6FTqWqiZVbxrtaCIg==} - engines: {node: 18 || 20 || >=22} - minimatch@10.2.5: resolution: {integrity: sha512-MULkVLfKGYDFYejP07QOurDLLQpcjk7Fw+7jXS2R2czRQzR56yHRveU5NDJEOviH+hETZKSkIk5c+T23GjFUMg==} engines: {node: 18 || 20 || >=22} @@ -3304,8 +3300,8 @@ packages: prettier-plugin-svelte: optional: true - prettier@3.8.1: - resolution: {integrity: sha512-UOnG6LftzbdaHZcKoPFtOcCKztrQ57WkHDeRD9t/PTQtmT0NHSeWWepj6pS0z/N7+08BHFDQVUrfmfMRcZwbMg==} + prettier@3.8.3: + resolution: {integrity: sha512-7igPTM53cGHMW8xWuVTydi2KO233VFiTNyF5hLJqpilHfmn8C8gPf+PS7dUT64YcXFbiMGZxS9pCSxL/Dxm/Jw==} engines: {node: '>=14'} hasBin: true @@ -3740,12 +3736,12 @@ packages: resolution: {integrity: sha512-OZs6gsjF4vMp32qrCbiVSkrFmXtG/AZhY3t0iAMrMBiAZyV9oALtXO8hsrHbMXF9x6L3grlFuwW2oAz7cav+Gw==} engines: {node: '>= 0.6'} - typescript-eslint@8.57.2: - resolution: {integrity: sha512-VEPQ0iPgWO/sBaZOU1xo4nuNdODVOajPnTIbog2GKYr31nIlZ0fWPoCQgGfF3ETyBl1vn63F/p50Um9Z4J8O8A==} + typescript-eslint@8.58.2: + resolution: {integrity: sha512-V8iSng9mRbdZjl54VJ9NKr6ZB+dW0J3TzRXRGcSbLIej9jV86ZRtlYeTKDR/QLxXykocJ5icNzbsl2+5TzIvcQ==} engines: {node: ^18.18.0 || ^20.9.0 || >=21.1.0} peerDependencies: eslint: ^8.57.0 || ^9.0.0 || ^10.0.0 - typescript: '>=4.8.4 <6.0.0' + typescript: '>=4.8.4 <6.1.0' typescript@5.9.3: resolution: {integrity: sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw==} @@ -4310,38 +4306,38 @@ snapshots: '@esbuild/win32-x64@0.27.4': optional: true - '@eslint-community/eslint-utils@4.9.1(eslint@10.1.0(jiti@2.6.1))': + '@eslint-community/eslint-utils@4.9.1(eslint@10.2.1(jiti@2.6.1))': dependencies: - eslint: 10.1.0(jiti@2.6.1) + eslint: 10.2.1(jiti@2.6.1) eslint-visitor-keys: 3.4.3 '@eslint-community/regexpp@4.12.2': {} - '@eslint/config-array@0.23.3': + '@eslint/config-array@0.23.5': dependencies: - '@eslint/object-schema': 3.0.3 + '@eslint/object-schema': 3.0.5 debug: 4.4.3 - minimatch: 10.2.4 + minimatch: 10.2.5 transitivePeerDependencies: - supports-color - '@eslint/config-helpers@0.5.3': + '@eslint/config-helpers@0.5.5': dependencies: - '@eslint/core': 1.1.1 + '@eslint/core': 1.2.1 - '@eslint/core@1.1.1': + '@eslint/core@1.2.1': dependencies: '@types/json-schema': 7.0.15 - '@eslint/js@10.0.1(eslint@10.1.0(jiti@2.6.1))': + '@eslint/js@10.0.1(eslint@10.2.1(jiti@2.6.1))': optionalDependencies: - eslint: 10.1.0(jiti@2.6.1) + eslint: 10.2.1(jiti@2.6.1) - '@eslint/object-schema@3.0.3': {} + '@eslint/object-schema@3.0.5': {} - '@eslint/plugin-kit@0.6.1': + '@eslint/plugin-kit@0.7.1': dependencies: - '@eslint/core': 1.1.1 + '@eslint/core': 1.2.1 levn: 0.4.1 '@floating-ui/core@1.7.5': @@ -5388,10 +5384,10 @@ snapshots: '@tanstack/query-core': 5.99.0 react: 19.2.5 - '@tanstack/react-router-devtools@1.166.11(@tanstack/react-router@1.168.22(react-dom@19.2.5(react@19.2.5))(react@19.2.5))(@tanstack/router-core@1.168.15)(csstype@3.2.3)(react-dom@19.2.5(react@19.2.5))(react@19.2.5)': + '@tanstack/react-router-devtools@1.166.13(@tanstack/react-router@1.168.23(react-dom@19.2.5(react@19.2.5))(react@19.2.5))(@tanstack/router-core@1.168.15)(csstype@3.2.3)(react-dom@19.2.5(react@19.2.5))(react@19.2.5)': dependencies: - '@tanstack/react-router': 1.168.22(react-dom@19.2.5(react@19.2.5))(react@19.2.5) - '@tanstack/router-devtools-core': 1.167.1(@tanstack/router-core@1.168.15)(csstype@3.2.3) + '@tanstack/react-router': 1.168.23(react-dom@19.2.5(react@19.2.5))(react@19.2.5) + '@tanstack/router-devtools-core': 1.167.3(@tanstack/router-core@1.168.15)(csstype@3.2.3) react: 19.2.5 react-dom: 19.2.5(react@19.2.5) optionalDependencies: @@ -5399,7 +5395,7 @@ snapshots: transitivePeerDependencies: - csstype - '@tanstack/react-router@1.168.22(react-dom@19.2.5(react@19.2.5))(react@19.2.5)': + '@tanstack/react-router@1.168.23(react-dom@19.2.5(react@19.2.5))(react@19.2.5)': dependencies: '@tanstack/history': 1.161.6 '@tanstack/react-store': 0.9.3(react-dom@19.2.5(react@19.2.5))(react@19.2.5) @@ -5429,7 +5425,7 @@ snapshots: seroval: 1.5.1 seroval-plugins: 1.5.1(seroval@1.5.1) - '@tanstack/router-devtools-core@1.167.1(@tanstack/router-core@1.168.15)(csstype@3.2.3)': + '@tanstack/router-devtools-core@1.167.3(@tanstack/router-core@1.168.15)(csstype@3.2.3)': dependencies: '@tanstack/router-core': 1.168.15 clsx: 2.1.1 @@ -5442,7 +5438,7 @@ snapshots: '@tanstack/router-core': 1.168.7 '@tanstack/router-utils': 1.161.6 '@tanstack/virtual-file-routes': 1.161.7 - prettier: 3.8.1 + prettier: 3.8.3 recast: 0.23.11 source-map: 0.7.6 tsx: 4.21.0 @@ -5450,7 +5446,7 @@ snapshots: transitivePeerDependencies: - supports-color - '@tanstack/router-plugin@1.167.9(@tanstack/react-router@1.168.22(react-dom@19.2.5(react@19.2.5))(react@19.2.5))(vite@8.0.8(@types/node@25.6.0)(esbuild@0.27.4)(jiti@2.6.1)(tsx@4.21.0))': + '@tanstack/router-plugin@1.167.9(@tanstack/react-router@1.168.23(react-dom@19.2.5(react@19.2.5))(react@19.2.5))(vite@8.0.8(@types/node@25.6.0)(esbuild@0.27.4)(jiti@2.6.1)(tsx@4.21.0))': dependencies: '@babel/core': 7.29.0 '@babel/plugin-syntax-jsx': 7.28.6(@babel/core@7.29.0) @@ -5466,7 +5462,7 @@ snapshots: unplugin: 2.3.11 zod: 3.25.76 optionalDependencies: - '@tanstack/react-router': 1.168.22(react-dom@19.2.5(react@19.2.5))(react@19.2.5) + '@tanstack/react-router': 1.168.23(react-dom@19.2.5(react@19.2.5))(react@19.2.5) vite: 8.0.8(@types/node@25.6.0)(esbuild@0.27.4)(jiti@2.6.1)(tsx@4.21.0) transitivePeerDependencies: - supports-color @@ -5489,7 +5485,7 @@ snapshots: '@tanstack/virtual-file-routes@1.161.7': {} - '@trivago/prettier-plugin-sort-imports@6.0.2(prettier@3.8.1)': + '@trivago/prettier-plugin-sort-imports@6.0.2(prettier@3.8.3)': dependencies: '@babel/generator': 7.29.1 '@babel/parser': 7.29.2 @@ -5499,7 +5495,7 @@ snapshots: lodash-es: 4.17.23 minimatch: 9.0.9 parse-imports-exports: 0.2.4 - prettier: 3.8.1 + prettier: 3.8.3 transitivePeerDependencies: - supports-color @@ -5562,15 +5558,15 @@ snapshots: '@types/validate-npm-package-name@4.0.2': {} - '@typescript-eslint/eslint-plugin@8.57.2(@typescript-eslint/parser@8.57.2(eslint@10.1.0(jiti@2.6.1))(typescript@5.9.3))(eslint@10.1.0(jiti@2.6.1))(typescript@5.9.3)': + '@typescript-eslint/eslint-plugin@8.58.2(@typescript-eslint/parser@8.58.2(eslint@10.2.1(jiti@2.6.1))(typescript@5.9.3))(eslint@10.2.1(jiti@2.6.1))(typescript@5.9.3)': dependencies: '@eslint-community/regexpp': 4.12.2 - '@typescript-eslint/parser': 8.57.2(eslint@10.1.0(jiti@2.6.1))(typescript@5.9.3) - '@typescript-eslint/scope-manager': 8.57.2 - '@typescript-eslint/type-utils': 8.57.2(eslint@10.1.0(jiti@2.6.1))(typescript@5.9.3) - '@typescript-eslint/utils': 8.57.2(eslint@10.1.0(jiti@2.6.1))(typescript@5.9.3) - '@typescript-eslint/visitor-keys': 8.57.2 - eslint: 10.1.0(jiti@2.6.1) + '@typescript-eslint/parser': 8.58.2(eslint@10.2.1(jiti@2.6.1))(typescript@5.9.3) + '@typescript-eslint/scope-manager': 8.58.2 + '@typescript-eslint/type-utils': 8.58.2(eslint@10.2.1(jiti@2.6.1))(typescript@5.9.3) + '@typescript-eslint/utils': 8.58.2(eslint@10.2.1(jiti@2.6.1))(typescript@5.9.3) + '@typescript-eslint/visitor-keys': 8.58.2 + eslint: 10.2.1(jiti@2.6.1) ignore: 7.0.5 natural-compare: 1.4.0 ts-api-utils: 2.5.0(typescript@5.9.3) @@ -5578,58 +5574,58 @@ snapshots: transitivePeerDependencies: - supports-color - '@typescript-eslint/parser@8.57.2(eslint@10.1.0(jiti@2.6.1))(typescript@5.9.3)': + '@typescript-eslint/parser@8.58.2(eslint@10.2.1(jiti@2.6.1))(typescript@5.9.3)': dependencies: - '@typescript-eslint/scope-manager': 8.57.2 - '@typescript-eslint/types': 8.57.2 - '@typescript-eslint/typescript-estree': 8.57.2(typescript@5.9.3) - '@typescript-eslint/visitor-keys': 8.57.2 + '@typescript-eslint/scope-manager': 8.58.2 + '@typescript-eslint/types': 8.58.2 + '@typescript-eslint/typescript-estree': 8.58.2(typescript@5.9.3) + '@typescript-eslint/visitor-keys': 8.58.2 debug: 4.4.3 - eslint: 10.1.0(jiti@2.6.1) + eslint: 10.2.1(jiti@2.6.1) typescript: 5.9.3 transitivePeerDependencies: - supports-color - '@typescript-eslint/project-service@8.57.2(typescript@5.9.3)': + '@typescript-eslint/project-service@8.58.2(typescript@5.9.3)': dependencies: - '@typescript-eslint/tsconfig-utils': 8.57.2(typescript@5.9.3) - '@typescript-eslint/types': 8.57.2 + '@typescript-eslint/tsconfig-utils': 8.58.2(typescript@5.9.3) + '@typescript-eslint/types': 8.58.2 debug: 4.4.3 typescript: 5.9.3 transitivePeerDependencies: - supports-color - '@typescript-eslint/scope-manager@8.57.2': + '@typescript-eslint/scope-manager@8.58.2': dependencies: - '@typescript-eslint/types': 8.57.2 - '@typescript-eslint/visitor-keys': 8.57.2 + '@typescript-eslint/types': 8.58.2 + '@typescript-eslint/visitor-keys': 8.58.2 - '@typescript-eslint/tsconfig-utils@8.57.2(typescript@5.9.3)': + '@typescript-eslint/tsconfig-utils@8.58.2(typescript@5.9.3)': dependencies: typescript: 5.9.3 - '@typescript-eslint/type-utils@8.57.2(eslint@10.1.0(jiti@2.6.1))(typescript@5.9.3)': + '@typescript-eslint/type-utils@8.58.2(eslint@10.2.1(jiti@2.6.1))(typescript@5.9.3)': dependencies: - '@typescript-eslint/types': 8.57.2 - '@typescript-eslint/typescript-estree': 8.57.2(typescript@5.9.3) - '@typescript-eslint/utils': 8.57.2(eslint@10.1.0(jiti@2.6.1))(typescript@5.9.3) + '@typescript-eslint/types': 8.58.2 + '@typescript-eslint/typescript-estree': 8.58.2(typescript@5.9.3) + '@typescript-eslint/utils': 8.58.2(eslint@10.2.1(jiti@2.6.1))(typescript@5.9.3) debug: 4.4.3 - eslint: 10.1.0(jiti@2.6.1) + eslint: 10.2.1(jiti@2.6.1) ts-api-utils: 2.5.0(typescript@5.9.3) typescript: 5.9.3 transitivePeerDependencies: - supports-color - '@typescript-eslint/types@8.57.2': {} + '@typescript-eslint/types@8.58.2': {} - '@typescript-eslint/typescript-estree@8.57.2(typescript@5.9.3)': + '@typescript-eslint/typescript-estree@8.58.2(typescript@5.9.3)': dependencies: - '@typescript-eslint/project-service': 8.57.2(typescript@5.9.3) - '@typescript-eslint/tsconfig-utils': 8.57.2(typescript@5.9.3) - '@typescript-eslint/types': 8.57.2 - '@typescript-eslint/visitor-keys': 8.57.2 + '@typescript-eslint/project-service': 8.58.2(typescript@5.9.3) + '@typescript-eslint/tsconfig-utils': 8.58.2(typescript@5.9.3) + '@typescript-eslint/types': 8.58.2 + '@typescript-eslint/visitor-keys': 8.58.2 debug: 4.4.3 - minimatch: 10.2.4 + minimatch: 10.2.5 semver: 7.7.4 tinyglobby: 0.2.16 ts-api-utils: 2.5.0(typescript@5.9.3) @@ -5637,20 +5633,20 @@ snapshots: transitivePeerDependencies: - supports-color - '@typescript-eslint/utils@8.57.2(eslint@10.1.0(jiti@2.6.1))(typescript@5.9.3)': + '@typescript-eslint/utils@8.58.2(eslint@10.2.1(jiti@2.6.1))(typescript@5.9.3)': dependencies: - '@eslint-community/eslint-utils': 4.9.1(eslint@10.1.0(jiti@2.6.1)) - '@typescript-eslint/scope-manager': 8.57.2 - '@typescript-eslint/types': 8.57.2 - '@typescript-eslint/typescript-estree': 8.57.2(typescript@5.9.3) - eslint: 10.1.0(jiti@2.6.1) + '@eslint-community/eslint-utils': 4.9.1(eslint@10.2.1(jiti@2.6.1)) + '@typescript-eslint/scope-manager': 8.58.2 + '@typescript-eslint/types': 8.58.2 + '@typescript-eslint/typescript-estree': 8.58.2(typescript@5.9.3) + eslint: 10.2.1(jiti@2.6.1) typescript: 5.9.3 transitivePeerDependencies: - supports-color - '@typescript-eslint/visitor-keys@8.57.2': + '@typescript-eslint/visitor-keys@8.58.2': dependencies: - '@typescript-eslint/types': 8.57.2 + '@typescript-eslint/types': 8.58.2 eslint-visitor-keys: 5.0.1 '@ungap/structured-clone@1.3.0': {} @@ -6013,24 +6009,24 @@ snapshots: escape-string-regexp@5.0.0: {} - eslint-config-prettier@10.1.8(eslint@10.1.0(jiti@2.6.1)): + eslint-config-prettier@10.1.8(eslint@10.2.1(jiti@2.6.1)): dependencies: - eslint: 10.1.0(jiti@2.6.1) + eslint: 10.2.1(jiti@2.6.1) - eslint-plugin-react-hooks@7.0.1(eslint@10.1.0(jiti@2.6.1)): + eslint-plugin-react-hooks@7.1.1(eslint@10.2.1(jiti@2.6.1)): dependencies: '@babel/core': 7.29.0 '@babel/parser': 7.29.2 - eslint: 10.1.0(jiti@2.6.1) + eslint: 10.2.1(jiti@2.6.1) hermes-parser: 0.25.1 zod: 4.3.6 zod-validation-error: 4.0.2(zod@4.3.6) transitivePeerDependencies: - supports-color - eslint-plugin-react-refresh@0.5.2(eslint@10.1.0(jiti@2.6.1)): + eslint-plugin-react-refresh@0.5.2(eslint@10.2.1(jiti@2.6.1)): dependencies: - eslint: 10.1.0(jiti@2.6.1) + eslint: 10.2.1(jiti@2.6.1) eslint-scope@9.1.2: dependencies: @@ -6043,14 +6039,14 @@ snapshots: eslint-visitor-keys@5.0.1: {} - eslint@10.1.0(jiti@2.6.1): + eslint@10.2.1(jiti@2.6.1): dependencies: - '@eslint-community/eslint-utils': 4.9.1(eslint@10.1.0(jiti@2.6.1)) + '@eslint-community/eslint-utils': 4.9.1(eslint@10.2.1(jiti@2.6.1)) '@eslint-community/regexpp': 4.12.2 - '@eslint/config-array': 0.23.3 - '@eslint/config-helpers': 0.5.3 - '@eslint/core': 1.1.1 - '@eslint/plugin-kit': 0.6.1 + '@eslint/config-array': 0.23.5 + '@eslint/config-helpers': 0.5.5 + '@eslint/core': 1.2.1 + '@eslint/plugin-kit': 0.7.1 '@humanfs/node': 0.16.7 '@humanwhocodes/module-importer': 1.0.1 '@humanwhocodes/retry': 0.4.3 @@ -6072,7 +6068,7 @@ snapshots: imurmurhash: 0.1.4 is-glob: 4.0.3 json-stable-stringify-without-jsonify: 1.0.1 - minimatch: 10.2.4 + minimatch: 10.2.5 natural-compare: 1.4.0 optionator: 0.9.4 optionalDependencies: @@ -7070,10 +7066,6 @@ snapshots: mimic-function@5.0.1: {} - minimatch@10.2.4: - dependencies: - brace-expansion: 5.0.5 - minimatch@10.2.5: dependencies: brace-expansion: 5.0.5 @@ -7285,13 +7277,13 @@ snapshots: prelude-ls@1.2.1: {} - prettier-plugin-tailwindcss@0.7.2(@trivago/prettier-plugin-sort-imports@6.0.2(prettier@3.8.1))(prettier@3.8.1): + prettier-plugin-tailwindcss@0.7.2(@trivago/prettier-plugin-sort-imports@6.0.2(prettier@3.8.3))(prettier@3.8.3): dependencies: - prettier: 3.8.1 + prettier: 3.8.3 optionalDependencies: - '@trivago/prettier-plugin-sort-imports': 6.0.2(prettier@3.8.1) + '@trivago/prettier-plugin-sort-imports': 6.0.2(prettier@3.8.3) - prettier@3.8.1: {} + prettier@3.8.3: {} pretty-ms@9.3.0: dependencies: @@ -7856,13 +7848,13 @@ snapshots: media-typer: 1.1.0 mime-types: 3.0.2 - typescript-eslint@8.57.2(eslint@10.1.0(jiti@2.6.1))(typescript@5.9.3): + typescript-eslint@8.58.2(eslint@10.2.1(jiti@2.6.1))(typescript@5.9.3): dependencies: - '@typescript-eslint/eslint-plugin': 8.57.2(@typescript-eslint/parser@8.57.2(eslint@10.1.0(jiti@2.6.1))(typescript@5.9.3))(eslint@10.1.0(jiti@2.6.1))(typescript@5.9.3) - '@typescript-eslint/parser': 8.57.2(eslint@10.1.0(jiti@2.6.1))(typescript@5.9.3) - '@typescript-eslint/typescript-estree': 8.57.2(typescript@5.9.3) - '@typescript-eslint/utils': 8.57.2(eslint@10.1.0(jiti@2.6.1))(typescript@5.9.3) - eslint: 10.1.0(jiti@2.6.1) + '@typescript-eslint/eslint-plugin': 8.58.2(@typescript-eslint/parser@8.58.2(eslint@10.2.1(jiti@2.6.1))(typescript@5.9.3))(eslint@10.2.1(jiti@2.6.1))(typescript@5.9.3) + '@typescript-eslint/parser': 8.58.2(eslint@10.2.1(jiti@2.6.1))(typescript@5.9.3) + '@typescript-eslint/typescript-estree': 8.58.2(typescript@5.9.3) + '@typescript-eslint/utils': 8.58.2(eslint@10.2.1(jiti@2.6.1))(typescript@5.9.3) + eslint: 10.2.1(jiti@2.6.1) typescript: 5.9.3 transitivePeerDependencies: - supports-color From e556a816e4db4c158ab3a455693018f452f63eba Mon Sep 17 00:00:00 2001 From: lxowalle <83055338+lxowalle@users.noreply.github.com> Date: Mon, 20 Apr 2026 15:20:26 +0800 Subject: [PATCH 5/8] Feat/channel tool feedback animation (#2569) * feat(channels): unify tool feedback animation across discord telegram and feishu * fix(tool-feedback): unify fallback and single-message delivery * fix(channels): finalize tool feedback in place * fix ci * feat: improve tool feedback --- cmd/picoclaw/internal/auth/wecom_test.go | 20 +- docs/channels/discord/README.md | 44 +- pkg/agent/loop.go | 1 + pkg/agent/loop_test.go | 366 ++++++++++++++- pkg/agent/loop_turn.go | 51 ++- pkg/agent/loop_utils.go | 93 ++++ pkg/channels/discord/discord.go | 190 +++++++- pkg/channels/discord/discord_test.go | 245 ++++++++++ pkg/channels/feishu/feishu_64.go | 175 +++++++- pkg/channels/feishu/feishu_64_test.go | 85 ++++ pkg/channels/manager.go | 112 ++++- pkg/channels/manager_test.go | 424 +++++++++++++++++- pkg/channels/matrix/matrix.go | 133 +++++- pkg/channels/matrix/matrix_test.go | 29 ++ pkg/channels/pico/pico.go | 118 ++++- pkg/channels/pico/pico_test.go | 28 ++ pkg/channels/telegram/command_registration.go | 6 +- .../telegram/command_registration_test.go | 16 +- pkg/channels/telegram/telegram.go | 180 +++++++- .../telegram_group_command_filter_test.go | 2 +- pkg/channels/telegram/telegram_test.go | 102 ++++- pkg/channels/tool_feedback_animator.go | 240 ++++++++++ pkg/channels/tool_feedback_animator_test.go | 121 +++++ pkg/config/config.go | 2 +- pkg/providers/cli/toolcall_utils.go | 17 +- pkg/providers/common/common.go | 93 +++- pkg/providers/common/common_test.go | 119 +++++ pkg/providers/protocoltypes/types.go | 3 +- pkg/providers/toolcall_utils_test.go | 24 + pkg/utils/tool_feedback.go | 58 ++- pkg/utils/tool_feedback_test.go | 42 +- web/backend/api/session.go | 89 +++- web/backend/api/session_test.go | 246 +++++++++- web/frontend/src/i18n/locales/en.json | 6 +- web/frontend/src/i18n/locales/zh.json | 6 +- 35 files changed, 3317 insertions(+), 169 deletions(-) create mode 100644 pkg/channels/tool_feedback_animator.go create mode 100644 pkg/channels/tool_feedback_animator_test.go create mode 100644 pkg/providers/toolcall_utils_test.go diff --git a/cmd/picoclaw/internal/auth/wecom_test.go b/cmd/picoclaw/internal/auth/wecom_test.go index c152481be..aafd39e69 100644 --- a/cmd/picoclaw/internal/auth/wecom_test.go +++ b/cmd/picoclaw/internal/auth/wecom_test.go @@ -3,6 +3,7 @@ package auth import ( "bytes" "context" + "net" "net/http" "net/http/httptest" "net/url" @@ -19,6 +20,19 @@ import ( "github.com/sipeed/picoclaw/pkg/config" ) +func newIPv4TestServer(t *testing.T, handler http.Handler) *httptest.Server { + t.Helper() + + server := httptest.NewUnstartedServer(handler) + listener, err := net.Listen("tcp4", "127.0.0.1:0") + require.NoError(t, err) + + server.Listener = listener + server.Start() + t.Cleanup(server.Close) + return server +} + func TestNewWeComCommand(t *testing.T) { cmd := newWeComCommand() @@ -53,7 +67,7 @@ func TestBuildWeComQRCodePageURL(t *testing.T) { } func TestFetchWeComQRCode(t *testing.T) { - server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + server := newIPv4TestServer(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, "/generate", r.URL.Path) assert.Equal(t, wecomQRSourceID, r.URL.Query().Get("source")) assert.Equal(t, wecomQRSourceID, r.URL.Query().Get("sourceID")) @@ -61,7 +75,6 @@ func TestFetchWeComQRCode(t *testing.T) { w.Header().Set("Content-Type", "application/json") _, _ = w.Write([]byte(`{"data":{"scode":"scode-1","auth_url":"https://example.com/qr"}}`)) })) - defer server.Close() opts := normalizeWeComQRFlowOptions(wecomQRFlowOptions{ HTTPClient: server.Client(), @@ -78,7 +91,7 @@ func TestFetchWeComQRCode(t *testing.T) { func TestPollWeComQRCodeResult(t *testing.T) { var calls atomic.Int32 - server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + server := newIPv4TestServer(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { call := calls.Add(1) assert.Equal(t, "/query", r.URL.Path) assert.Equal(t, "scode-1", r.URL.Query().Get("scode")) @@ -92,7 +105,6 @@ func TestPollWeComQRCodeResult(t *testing.T) { _, _ = w.Write([]byte(`{"data":{"status":"success","bot_info":{"botid":"bot-1","secret":"secret-1"}}}`)) } })) - defer server.Close() var output bytes.Buffer opts := normalizeWeComQRFlowOptions(wecomQRFlowOptions{ diff --git a/docs/channels/discord/README.md b/docs/channels/discord/README.md index 771289d28..741bc64a1 100644 --- a/docs/channels/discord/README.md +++ b/docs/channels/discord/README.md @@ -8,26 +8,56 @@ Discord is a free voice, video, and text chat application designed for communiti ```json { + "agents": { + "defaults": { + "tool_feedback": { + "enabled": true, + "max_args_length": 300 + } + } + }, "channel_list": { "discord": { "enabled": true, "type": "discord", "token": "YOUR_BOT_TOKEN", "allow_from": ["YOUR_USER_ID"], + "placeholder": { + "enabled": true, + "text": ["Thinking... 💭"] + }, "group_trigger": { "mention_only": false - } + }, + "reasoning_channel_id": "" } } } ``` -| Field | Type | Required | Description | -| ------------- | ------ | -------- | --------------------------------------------------------------------------- | -| enabled | bool | Yes | Whether to enable the Discord channel | -| token | string | Yes | Discord Bot Token | -| allow_from | array | No | Allowlist of user IDs; empty means all users are allowed | -| group_trigger | object | No | Group trigger settings (example: { "mention_only": false }) | +| Field | Type | Required | Description | +| -------------------- | ------ | -------- | --------------------------------------------------------------------------- | +| enabled | bool | Yes | Whether to enable the Discord channel | +| token | string | Yes | Discord Bot Token | +| allow_from | array | No | Allowlist of user IDs; empty means all users are allowed | +| placeholder | object | No | Placeholder message config shown while the agent is working | +| group_trigger | object | No | Group trigger settings (example: { "mention_only": false }) | +| reasoning_channel_id | string | No | Optional target channel ID for reasoning/thinking output | + +## Visible Execution Feedback + +Discord can show three different kinds of "working" feedback: + +1. Typing indicator: automatic, no extra config needed. +2. Placeholder message: enable `channel_list.discord.placeholder.enabled` to send a visible `Thinking...` message that is later edited into the final reply. +3. Tool execution feedback: enable `agents.defaults.tool_feedback.enabled` to send a short message before each tool call, for example: + +```text +🔧 `web_search` +Checking the latest PicoClaw release notes before I answer. +``` + +If you only see `Bot is typing`, check that `placeholder.enabled` or `tool_feedback.enabled` is actually set in your runtime config. ## Setup diff --git a/pkg/agent/loop.go b/pkg/agent/loop.go index fb6f95edf..f0c287ee2 100644 --- a/pkg/agent/loop.go +++ b/pkg/agent/loop.go @@ -112,6 +112,7 @@ const ( pendingTurnPrefix = "pending-" metadataKeyMessageKind = "message_kind" messageKindThought = "thought" + messageKindToolFeedback = "tool_feedback" metadataKeyAccountID = "account_id" metadataKeyGuildID = "guild_id" metadataKeyTeamID = "team_id" diff --git a/pkg/agent/loop_test.go b/pkg/agent/loop_test.go index 5cdac186c..a2d4ea7aa 100644 --- a/pkg/agent/loop_test.go +++ b/pkg/agent/loop_test.go @@ -24,6 +24,7 @@ import ( "github.com/sipeed/picoclaw/pkg/routing" "github.com/sipeed/picoclaw/pkg/session" "github.com/sipeed/picoclaw/pkg/tools" + "github.com/sipeed/picoclaw/pkg/utils" ) type fakeChannel struct{ id string } @@ -1758,6 +1759,157 @@ func (m *toolFeedbackProvider) GetDefaultModel() string { return "heartbeat-tool-feedback-model" } +type toolFeedbackReasoningProvider struct { + filePath string + calls int +} + +func (m *toolFeedbackReasoningProvider) Chat( + ctx context.Context, + messages []providers.Message, + tools []providers.ToolDefinition, + model string, + opts map[string]any, +) (*providers.LLMResponse, error) { + m.calls++ + if m.calls == 1 { + return &providers.LLMResponse{ + ReasoningContent: "Read README.md first to confirm the context that needs to be changed.", + ToolCalls: []providers.ToolCall{{ + ID: "call_reasoning_read_file", + Type: "function", + Name: "read_file", + Arguments: map[string]any{"path": m.filePath}, + }}, + }, nil + } + + return &providers.LLMResponse{ + Content: "DONE", + ToolCalls: []providers.ToolCall{}, + }, nil +} + +func (m *toolFeedbackReasoningProvider) GetDefaultModel() string { + return "tool-feedback-reasoning-model" +} + +func TestToolFeedbackExplanationFromResponse_UsesCurrentContentFirst(t *testing.T) { + response := &providers.LLMResponse{ + Content: "Read README.md first", + ReasoningContent: "current reasoning fallback", + } + messages := []providers.Message{ + {Role: "user", Content: "check file"}, + {Role: "assistant", Content: "Previous turn explanation"}, + {Role: "tool", Content: "tool output", ToolCallID: "call_1"}, + } + + got := toolFeedbackExplanationFromResponse(response, messages, 300) + if got != "Read README.md first" { + t.Fatalf("toolFeedbackExplanationFromResponse() = %q, want current content", got) + } +} + +func TestToolFeedbackExplanationFromResponse_UsesExplicitToolCallExtraContent(t *testing.T) { + response := &providers.LLMResponse{ + ToolCalls: []providers.ToolCall{{ + ID: "call_1", + Name: "read_file", + ExtraContent: &providers.ExtraContent{ + ToolFeedbackExplanation: "Read README.md first to confirm the current project structure.", + }, + }}, + } + messages := []providers.Message{ + {Role: "user", Content: "check file"}, + {Role: "assistant", Content: ""}, + {Role: "tool", Content: "tool output", ToolCallID: "call_1"}, + } + + got := toolFeedbackExplanationFromResponse(response, messages, 300) + if got != "Read README.md first to confirm the current project structure." { + t.Fatalf("toolFeedbackExplanationFromResponse() = %q, want explicit tool feedback explanation", got) + } +} + +func TestToolFeedbackExplanationForToolCall_PrefersToolSpecificExtraContent(t *testing.T) { + response := &providers.LLMResponse{ + Content: "Shared explanation", + ToolCalls: []providers.ToolCall{ + { + ID: "call_1", + Name: "read_file", + ExtraContent: &providers.ExtraContent{ + ToolFeedbackExplanation: "Read README.md first.", + }, + }, + { + ID: "call_2", + Name: "edit_file", + ExtraContent: &providers.ExtraContent{ + ToolFeedbackExplanation: "Update config example after reading it.", + }, + }, + }, + } + + got1 := toolFeedbackExplanationForToolCall(response, response.ToolCalls[0], nil, 300) + got2 := toolFeedbackExplanationForToolCall(response, response.ToolCalls[1], nil, 300) + if got1 != "Read README.md first." { + t.Fatalf("toolFeedbackExplanationForToolCall() first = %q, want tool-specific explanation", got1) + } + if got2 != "Update config example after reading it." { + t.Fatalf("toolFeedbackExplanationForToolCall() second = %q, want tool-specific explanation", got2) + } +} + +func TestToolFeedbackExplanationForToolCall_DoesNotReuseAnotherToolCallExplanation(t *testing.T) { + response := &providers.LLMResponse{ + ToolCalls: []providers.ToolCall{ + { + ID: "call_1", + Name: "read_file", + }, + { + ID: "call_2", + Name: "edit_file", + ExtraContent: &providers.ExtraContent{ + ToolFeedbackExplanation: "Update config example after reading it.", + }, + }, + }, + } + messages := []providers.Message{ + {Role: "user", Content: "inspect the config and update the example"}, + } + + got := toolFeedbackExplanationForToolCall(response, response.ToolCalls[0], messages, 300) + want := utils.ToolFeedbackContinuationHint + ": inspect the config and update the example" + if got != want { + t.Fatalf("toolFeedbackExplanationForToolCall() = %q, want %q", got, want) + } +} + +func TestToolFeedbackExplanationFromResponse_DoesNotUseReasoningContent(t *testing.T) { + response := &providers.LLMResponse{ + Content: "", + ReasoningContent: "hidden reasoning should not be shown", + } + messages := []providers.Message{ + {Role: "user", Content: "check file"}, + {Role: "assistant", Content: "Previous turn explanation"}, + {Role: "user", Content: "Inspect README.md and update the config example."}, + {Role: "tool", Content: "tool output", ToolCallID: "call_1"}, + } + + got := toolFeedbackExplanationFromResponse(response, messages, 300) + want := utils.ToolFeedbackContinuationHint + ": Inspect README.md and update the config example." + if got != want { + t.Fatalf("toolFeedbackExplanationFromResponse() = %q, want latest user content fallback", got) + } +} + type picoInterleavedContentProvider struct { calls int } @@ -3656,7 +3808,16 @@ func TestProcessMessage_PublishesToolFeedbackWhenEnabled(t *testing.T) { t.Fatalf("unexpected tool feedback context: %+v", outbound.Context) } if !strings.Contains(outbound.Content, "`read_file`") { - t.Fatalf("tool feedback content = %q, want read_file preview", outbound.Content) + t.Fatalf("tool feedback content = %q, want read_file summary", outbound.Content) + } + if !strings.Contains(outbound.Content, utils.ToolFeedbackContinuationHint) { + t.Fatalf("tool feedback content = %q, want continuation hint fallback", outbound.Content) + } + if !strings.Contains(outbound.Content, "check tool feedback") { + t.Fatalf("tool feedback content = %q, want current user intent fallback", outbound.Content) + } + if strings.Contains(outbound.Content, "Previous turn explanation") { + t.Fatalf("tool feedback content = %q, want no previous assistant fallback", outbound.Content) } if outbound.AgentID != "main" { t.Fatalf("tool feedback agent_id = %q, want main", outbound.AgentID) @@ -3672,6 +3833,130 @@ func TestProcessMessage_PublishesToolFeedbackWhenEnabled(t *testing.T) { } } +func TestProcessMessage_DoesNotLeakReasoningContentInToolFeedback(t *testing.T) { + tmpDir := t.TempDir() + heartbeatFile := filepath.Join(tmpDir, "tool-feedback-reasoning.txt") + if err := os.WriteFile(heartbeatFile, []byte("tool feedback task"), 0o644); err != nil { + t.Fatalf("WriteFile() error = %v", err) + } + + cfg := &config.Config{ + Agents: config.AgentsConfig{ + Defaults: config.AgentDefaults{ + Workspace: tmpDir, + ModelName: "test-model", + MaxTokens: 4096, + MaxToolIterations: 10, + ToolFeedback: config.ToolFeedbackConfig{ + Enabled: true, + MaxArgsLength: 300, + }, + }, + }, + Tools: config.ToolsConfig{ + ReadFile: config.ReadFileToolConfig{ + Enabled: true, + }, + }, + } + + msgBus := bus.NewMessageBus() + provider := &toolFeedbackReasoningProvider{filePath: heartbeatFile} + al := NewAgentLoop(cfg, msgBus, provider) + + response, err := al.processMessage(context.Background(), testInboundMessage(bus.InboundMessage{ + Channel: "telegram", + SenderID: "user-1", + ChatID: "chat-1", + Content: "check reasoning fallback", + })) + if err != nil { + t.Fatalf("processMessage() error = %v", err) + } + if response != "DONE" { + t.Fatalf("processMessage() response = %q, want %q", response, "DONE") + } + + select { + case outbound := <-msgBus.OutboundChan(): + if !strings.Contains(outbound.Content, "`read_file`") { + t.Fatalf("tool feedback content = %q, want read_file summary", outbound.Content) + } + if !strings.Contains(outbound.Content, utils.ToolFeedbackContinuationHint) { + t.Fatalf("tool feedback content = %q, want continuation hint fallback", outbound.Content) + } + if !strings.Contains(outbound.Content, "check reasoning fallback") { + t.Fatalf("tool feedback content = %q, want current user intent fallback", outbound.Content) + } + if strings.Contains(outbound.Content, "Read README.md first") { + t.Fatalf("tool feedback content = %q, should not leak hidden reasoning", outbound.Content) + } + case <-time.After(2 * time.Second): + t.Fatal("expected outbound tool feedback without leaking reasoning") + } +} + +func TestProcessMessage_DoesNotPublishToolFeedbackForDiscordWhenDisabled(t *testing.T) { + assertToolFeedbackNotPublishedWhenDisabled(t, "discord") +} + +func assertToolFeedbackNotPublishedWhenDisabled(t *testing.T, channel string) { + t.Helper() + + tmpDir := t.TempDir() + heartbeatFile := filepath.Join(tmpDir, "tool-feedback-"+channel+".txt") + if err := os.WriteFile(heartbeatFile, []byte("tool feedback task"), 0o644); err != nil { + t.Fatalf("WriteFile() error = %v", err) + } + + cfg := &config.Config{ + Agents: config.AgentsConfig{ + Defaults: config.AgentDefaults{ + Workspace: tmpDir, + ModelName: "test-model", + MaxTokens: 4096, + MaxToolIterations: 10, + }, + }, + Tools: config.ToolsConfig{ + ReadFile: config.ReadFileToolConfig{ + Enabled: true, + }, + }, + } + + msgBus := bus.NewMessageBus() + provider := &toolFeedbackProvider{filePath: heartbeatFile} + al := NewAgentLoop(cfg, msgBus, provider) + + response, err := al.processMessage(context.Background(), testInboundMessage(bus.InboundMessage{ + Channel: channel, + SenderID: "user-1", + ChatID: "chat-1", + Content: "check tool feedback", + })) + if err != nil { + t.Fatalf("processMessage() error = %v", err) + } + if response != "HEARTBEAT_OK" { + t.Fatalf("processMessage() response = %q, want %q", response, "HEARTBEAT_OK") + } + + select { + case outbound := <-msgBus.OutboundChan(): + t.Fatalf("expected no outbound tool feedback for %s when disabled, got %+v", channel, outbound) + case <-time.After(200 * time.Millisecond): + } +} + +func TestProcessMessage_DoesNotPublishToolFeedbackForTelegramWhenDisabled(t *testing.T) { + assertToolFeedbackNotPublishedWhenDisabled(t, "telegram") +} + +func TestProcessMessage_DoesNotPublishToolFeedbackForFeishuWhenDisabled(t *testing.T) { + assertToolFeedbackNotPublishedWhenDisabled(t, "feishu") +} + func TestProcessMessage_MessageToolPublishesOutboundWithTurnMetadata(t *testing.T) { cfg := config.DefaultConfig() cfg.Agents.Defaults.Workspace = t.TempDir() @@ -3846,6 +4131,85 @@ func TestRunAgentLoop_PicoSkipsInterimPublishWhenNotAllowed(t *testing.T) { } } +func TestRun_PicoToolFeedbackSuppressesDuplicateInterimAssistantContent(t *testing.T) { + tmpDir := t.TempDir() + + cfg := &config.Config{ + Agents: config.AgentsConfig{ + Defaults: config.AgentDefaults{ + Workspace: tmpDir, + ModelName: "test-model", + MaxTokens: 4096, + MaxToolIterations: 10, + ToolFeedback: config.ToolFeedbackConfig{ + Enabled: true, + }, + }, + }, + } + + msgBus := bus.NewMessageBus() + provider := &picoInterleavedContentProvider{} + al := NewAgentLoop(cfg, msgBus, provider) + + agent := al.GetRegistry().GetDefaultAgent() + if agent == nil { + t.Fatal("expected default agent") + } + agent.Tools.Register(&toolLimitTestTool{}) + + runCtx, runCancel := context.WithCancel(context.Background()) + defer runCancel() + + runDone := make(chan error, 1) + go func() { + runDone <- al.Run(runCtx) + }() + + if err := msgBus.PublishInbound(context.Background(), bus.InboundMessage{ + Channel: "pico", + SenderID: "user-1", + ChatID: "session-1", + Content: "run with tools", + }); err != nil { + t.Fatalf("PublishInbound() error = %v", err) + } + + outputs := make([]string, 0, 2) + deadline := time.After(2 * time.Second) + for len(outputs) < 2 { + select { + case outbound := <-msgBus.OutboundChan(): + outputs = append(outputs, outbound.Content) + case <-deadline: + t.Fatalf("timed out waiting for pico outputs, got %v", outputs) + } + } + + if outputs[0] != "🔧 `tool_limit_test_tool`\nintermediate model text" { + t.Fatalf("first outbound content = %q, want tool feedback summary", outputs[0]) + } + if outputs[1] != "final model text" { + t.Fatalf("second outbound content = %q, want %q", outputs[1], "final model text") + } + + runCancel() + select { + case err := <-runDone: + if err != nil { + t.Fatalf("Run() error = %v", err) + } + case <-time.After(2 * time.Second): + t.Fatal("timed out waiting for Run() to exit") + } + + select { + case outbound := <-msgBus.OutboundChan(): + t.Fatalf("unexpected extra pico output after tool feedback + final reply: %+v", outbound) + case <-time.After(200 * time.Millisecond): + } +} + func TestResolveMediaRefs_ResolvesToBase64(t *testing.T) { store := media.NewFileMediaStore() dir := t.TempDir() diff --git a/pkg/agent/loop_turn.go b/pkg/agent/loop_turn.go index 1085ddeae..406120e46 100644 --- a/pkg/agent/loop_turn.go +++ b/pkg/agent/loop_turn.go @@ -635,7 +635,11 @@ turnLoop: } logger.DebugCF("agent", "LLM response", llmResponseFields) - if al.bus != nil && ts.channel == "pico" && len(response.ToolCalls) > 0 && ts.opts.AllowInterimPicoPublish { + if al.bus != nil && + ts.channel == "pico" && + len(response.ToolCalls) > 0 && + ts.opts.AllowInterimPicoPublish && + !shouldPublishToolFeedback(al.cfg, ts) { if strings.TrimSpace(response.Content) != "" { outCtx, outCancel := context.WithTimeout(turnCtx, 3*time.Second) err := al.bus.PublishOutbound(outCtx, bus.OutboundMessage{ @@ -705,7 +709,19 @@ turnLoop: } for _, tc := range normalizedToolCalls { argumentsJSON, _ := json.Marshal(tc.Arguments) + toolFeedbackExplanation := toolFeedbackExplanationForToolCall( + response, + tc, + messages, + al.cfg.Agents.Defaults.GetToolFeedbackMaxArgsLength(), + ) extraContent := tc.ExtraContent + if strings.TrimSpace(toolFeedbackExplanation) != "" { + if extraContent == nil { + extraContent = &providers.ExtraContent{} + } + extraContent.ToolFeedbackExplanation = toolFeedbackExplanation + } thoughtSignature := "" if tc.Function != nil { thoughtSignature = tc.Function.ThoughtSignature @@ -783,21 +799,16 @@ turnLoop: ) // Send tool feedback to chat channel if enabled (same as normal tool execution) - if al.cfg.Agents.Defaults.IsToolFeedbackEnabled() && - ts.channel != "" && - !ts.opts.SuppressToolFeedback { - argsJSON, _ := json.Marshal(toolArgs) - feedbackPreview := utils.Truncate( - string(argsJSON), + if shouldPublishToolFeedback(al.cfg, ts) { + toolFeedbackExplanation := toolFeedbackExplanationForToolCall( + response, + tc, + messages, al.cfg.Agents.Defaults.GetToolFeedbackMaxArgsLength(), ) - feedbackMsg := utils.FormatToolFeedbackMessage(toolName, feedbackPreview) + feedbackMsg := utils.FormatToolFeedbackMessage(toolName, toolFeedbackExplanation) fbCtx, fbCancel := context.WithTimeout(turnCtx, 3*time.Second) - _ = al.bus.PublishOutbound(fbCtx, bus.OutboundMessage{ - Channel: ts.channel, - ChatID: ts.chatID, - Content: feedbackMsg, - }) + _ = al.bus.PublishOutbound(fbCtx, outboundMessageForTurnWithKind(ts, feedbackMsg, messageKindToolFeedback)) fbCancel() } @@ -1067,16 +1078,16 @@ turnLoop: ) // Send tool feedback to chat channel if enabled (from HEAD) - if al.cfg.Agents.Defaults.IsToolFeedbackEnabled() && - ts.channel != "" && - !ts.opts.SuppressToolFeedback { - feedbackPreview := utils.Truncate( - string(argsJSON), + if shouldPublishToolFeedback(al.cfg, ts) { + toolFeedbackExplanation := toolFeedbackExplanationForToolCall( + response, + tc, + messages, al.cfg.Agents.Defaults.GetToolFeedbackMaxArgsLength(), ) - feedbackMsg := utils.FormatToolFeedbackMessage(tc.Name, feedbackPreview) + feedbackMsg := utils.FormatToolFeedbackMessage(tc.Name, toolFeedbackExplanation) fbCtx, fbCancel := context.WithTimeout(turnCtx, 3*time.Second) - _ = al.bus.PublishOutbound(fbCtx, outboundMessageForTurn(ts, feedbackMsg)) + _ = al.bus.PublishOutbound(fbCtx, outboundMessageForTurnWithKind(ts, feedbackMsg, messageKindToolFeedback)) fbCancel() } diff --git a/pkg/agent/loop_utils.go b/pkg/agent/loop_utils.go index 2574f0222..ff98dad68 100644 --- a/pkg/agent/loop_utils.go +++ b/pkg/agent/loop_utils.go @@ -11,6 +11,7 @@ import ( "github.com/sipeed/picoclaw/pkg/bus" "github.com/sipeed/picoclaw/pkg/commands" + "github.com/sipeed/picoclaw/pkg/config" "github.com/sipeed/picoclaw/pkg/providers" "github.com/sipeed/picoclaw/pkg/session" "github.com/sipeed/picoclaw/pkg/utils" @@ -84,6 +85,98 @@ func outboundMessageForTurn(ts *turnState, content string) bus.OutboundMessage { } } +func outboundMessageForTurnWithKind(ts *turnState, content, kind string) bus.OutboundMessage { + msg := outboundMessageForTurn(ts, content) + if strings.TrimSpace(kind) == "" { + return msg + } + if msg.Context.Raw == nil { + msg.Context.Raw = make(map[string]string, 1) + } + msg.Context.Raw[metadataKeyMessageKind] = kind + return msg +} + +func latestUserContent(messages []providers.Message) string { + for i := len(messages) - 1; i >= 0; i-- { + msg := messages[i] + if msg.Role != "user" { + continue + } + if content := strings.TrimSpace(msg.Content); content != "" { + return content + } + } + return "" +} + +func toolFeedbackExplanationFromResponse( + response *providers.LLMResponse, + messages []providers.Message, + maxLen int, +) string { + if response == nil { + return "" + } + explanation := strings.TrimSpace(response.Content) + if explanation == "" { + explanation = toolFeedbackExplanationFromToolCalls(response.ToolCalls) + } + if explanation == "" { + explanation = toolFeedbackExplanationFromMessages(messages) + } + return utils.Truncate(explanation, maxLen) +} + +func toolFeedbackExplanationFromToolCalls(toolCalls []providers.ToolCall) string { + for _, tc := range toolCalls { + if tc.ExtraContent == nil { + continue + } + if explanation := strings.TrimSpace(tc.ExtraContent.ToolFeedbackExplanation); explanation != "" { + return explanation + } + } + return "" +} + +func toolFeedbackExplanationForToolCall( + response *providers.LLMResponse, + toolCall providers.ToolCall, + messages []providers.Message, + maxLen int, +) string { + if toolCall.ExtraContent != nil { + if explanation := strings.TrimSpace(toolCall.ExtraContent.ToolFeedbackExplanation); explanation != "" { + return utils.Truncate(explanation, maxLen) + } + } + if response == nil { + return utils.Truncate(toolFeedbackExplanationFromMessages(messages), maxLen) + } + + explanation := strings.TrimSpace(response.Content) + if explanation == "" { + explanation = toolFeedbackExplanationFromMessages(messages) + } + return utils.Truncate(explanation, maxLen) +} + +func toolFeedbackExplanationFromMessages(messages []providers.Message) string { + explanation := latestUserContent(messages) + if explanation != "" { + return utils.ToolFeedbackContinuationHint + ": " + explanation + } + return "" +} + +func shouldPublishToolFeedback(cfg *config.Config, ts *turnState) bool { + if ts == nil || ts.channel == "" || ts.opts.SuppressToolFeedback { + return false + } + return cfg != nil && cfg.Agents.Defaults.IsToolFeedbackEnabled() +} + func cloneEventArguments(args map[string]any) map[string]any { if len(args) == 0 { return nil diff --git a/pkg/channels/discord/discord.go b/pkg/channels/discord/discord.go index 28f7277d3..514b9b3b1 100644 --- a/pkg/channels/discord/discord.go +++ b/pkg/channels/discord/discord.go @@ -45,9 +45,12 @@ type DiscordChannel struct { cancel context.CancelFunc typingMu sync.Mutex typingStop map[string]chan struct{} // chatID → stop signal - botUserID string // stored for mention checking + progress *channels.ToolFeedbackAnimator + botUserID string // stored for mention checking bus *bus.MessageBus tts tts.TTSProvider + playTTSFn func(context.Context, *discordgo.VoiceConnection, string, uint64) + ttsVoiceFn func(string) (*discordgo.VoiceConnection, bool) voiceMu sync.RWMutex voiceSSRC map[string]map[uint32]string // guildID -> ssrc -> userID @@ -84,7 +87,7 @@ func NewDiscordChannel( channels.WithReasoningChannelID(bc.ReasoningChannelID), ) - return &DiscordChannel{ + ch := &DiscordChannel{ BaseChannel: base, bc: bc, session: session, @@ -93,7 +96,11 @@ func NewDiscordChannel( typingStop: make(map[string]chan struct{}), bus: bus, voiceSSRC: make(map[string]map[uint32]string), - }, nil + } + ch.playTTSFn = ch.playTTS + ch.ttsVoiceFn = ch.voiceConnectionForTTS + ch.progress = channels.NewToolFeedbackAnimator(ch.EditMessage) + return ch, nil } func (c *DiscordChannel) Start(ctx context.Context) error { @@ -142,6 +149,9 @@ func (c *DiscordChannel) Stop(ctx context.Context) error { if c.cancel != nil { c.cancel() } + if c.progress != nil { + c.progress.StopAll() + } if err := c.session.Close(); err != nil { return fmt.Errorf("failed to close discord session: %w", err) @@ -164,32 +174,88 @@ func (c *DiscordChannel) Send(ctx context.Context, msg bus.OutboundMessage) ([]s return nil, nil } - if c.tts != nil { - if ch, err := c.session.State.Channel(channelID); err == nil && ch.GuildID != "" { - if vc, ok := c.session.VoiceConnections[ch.GuildID]; ok && vc != nil { - // Cancel any previous TTS playback - c.ttsMu.Lock() - if c.cancelTTS != nil { - c.cancelTTS() - } - ttsCtx, ttsCancel := context.WithCancel(c.ctx) - c.ttsPlayID++ - playID := c.ttsPlayID - c.cancelTTS = ttsCancel - c.ttsMu.Unlock() - - go c.playTTS(ttsCtx, vc, msg.Content, playID) + isToolFeedback := outboundMessageIsToolFeedback(msg) + if isToolFeedback { + if msgID, handled, err := c.progress.Update(ctx, channelID, msg.Content); handled { + if err != nil { + return nil, err } + return []string{msgID}, nil + } + } + trackedMsgID, hasTrackedMsg := c.currentToolFeedbackMessage(channelID) + c.maybeStartTTS(channelID, msg.Content, isToolFeedback) + if !isToolFeedback { + if msgIDs, handled := c.FinalizeToolFeedbackMessage(ctx, msg); handled { + return msgIDs, nil } } - msgID, err := c.sendChunk(ctx, channelID, msg.Content, msg.ReplyToMessageID) + content := msg.Content + if isToolFeedback { + content = channels.InitialAnimatedToolFeedbackContent(msg.Content) + } + msgID, err := c.sendChunk(ctx, channelID, content, msg.ReplyToMessageID) if err != nil { return nil, err } + if isToolFeedback { + c.RecordToolFeedbackMessage(channelID, msgID, msg.Content) + } else if hasTrackedMsg { + c.dismissTrackedToolFeedbackMessage(ctx, channelID, trackedMsgID) + } return []string{msgID}, nil } +func (c *DiscordChannel) maybeStartTTS(channelID, content string, isToolFeedback bool) { + if c.tts == nil || isToolFeedback { + return + } + + voiceFn := c.ttsVoiceFn + if voiceFn == nil { + voiceFn = c.voiceConnectionForTTS + } + vc, ok := voiceFn(channelID) + if !ok || vc == nil { + return + } + + // Cancel any previous TTS playback. + c.ttsMu.Lock() + if c.cancelTTS != nil { + c.cancelTTS() + } + ttsCtx, ttsCancel := context.WithCancel(c.ctx) + c.ttsPlayID++ + playID := c.ttsPlayID + c.cancelTTS = ttsCancel + playFn := c.playTTSFn + c.ttsMu.Unlock() + + if playFn == nil { + playFn = c.playTTS + } + go playFn(ttsCtx, vc, content, playID) +} + +func (c *DiscordChannel) voiceConnectionForTTS(channelID string) (*discordgo.VoiceConnection, bool) { + if c.session == nil || c.session.State == nil { + return nil, false + } + + ch, err := c.session.State.Channel(channelID) + if err != nil || ch == nil || ch.GuildID == "" { + return nil, false + } + + vc, ok := c.session.VoiceConnections[ch.GuildID] + if !ok || vc == nil { + return nil, false + } + return vc, true +} + // SendMedia implements the channels.MediaSender interface. func (c *DiscordChannel) SendMedia(ctx context.Context, msg bus.OutboundMediaMessage) ([]string, error) { if !c.IsRunning() { @@ -200,6 +266,7 @@ func (c *DiscordChannel) SendMedia(ctx context.Context, msg bus.OutboundMediaMes if channelID == "" { return nil, fmt.Errorf("channel ID is empty") } + trackedMsgID, hasTrackedMsg := c.currentToolFeedbackMessage(channelID) store := c.GetMediaStore() if store == nil { @@ -281,6 +348,9 @@ func (c *DiscordChannel) SendMedia(ctx context.Context, msg bus.OutboundMediaMes if r.err != nil { return nil, fmt.Errorf("discord send media: %w", channels.ErrTemporary) } + if hasTrackedMsg { + c.dismissTrackedToolFeedbackMessage(ctx, channelID, trackedMsgID) + } return []string{r.id}, nil case <-sendCtx.Done(): // Close all file readers @@ -295,10 +365,15 @@ func (c *DiscordChannel) SendMedia(ctx context.Context, msg bus.OutboundMediaMes // EditMessage implements channels.MessageEditor. func (c *DiscordChannel) EditMessage(ctx context.Context, chatID string, messageID string, content string) error { - _, err := c.session.ChannelMessageEdit(chatID, messageID, content) + _, err := c.session.ChannelMessageEdit(chatID, messageID, content, discordgo.WithContext(ctx)) return err } +// DeleteMessage implements channels.MessageDeleter. +func (c *DiscordChannel) DeleteMessage(ctx context.Context, chatID string, messageID string) error { + return c.session.ChannelMessageDelete(chatID, messageID, discordgo.WithContext(ctx)) +} + // SendPlaceholder implements channels.PlaceholderCapable. // It sends a placeholder message that will later be edited to the actual // response via EditMessage (channels.MessageEditor). @@ -317,6 +392,81 @@ func (c *DiscordChannel) SendPlaceholder(ctx context.Context, chatID string) (st return msg.ID, nil } +func outboundMessageIsToolFeedback(msg bus.OutboundMessage) bool { + if len(msg.Context.Raw) == 0 { + return false + } + return strings.EqualFold(strings.TrimSpace(msg.Context.Raw["message_kind"]), "tool_feedback") +} + +func (c *DiscordChannel) currentToolFeedbackMessage(chatID string) (string, bool) { + if c.progress == nil { + return "", false + } + return c.progress.Current(chatID) +} + +func (c *DiscordChannel) takeToolFeedbackMessage(chatID string) (string, string, bool) { + if c.progress == nil { + return "", "", false + } + return c.progress.Take(chatID) +} + +func (c *DiscordChannel) RecordToolFeedbackMessage(chatID, messageID, content string) { + if c.progress == nil { + return + } + c.progress.Record(chatID, messageID, content) +} + +func (c *DiscordChannel) ClearToolFeedbackMessage(chatID string) { + if c.progress == nil { + return + } + c.progress.Clear(chatID) +} + +func (c *DiscordChannel) DismissToolFeedbackMessage(ctx context.Context, chatID string) { + msgID, ok := c.currentToolFeedbackMessage(chatID) + if !ok { + return + } + c.dismissTrackedToolFeedbackMessage(ctx, chatID, msgID) +} + +func (c *DiscordChannel) dismissTrackedToolFeedbackMessage(ctx context.Context, chatID, messageID string) { + if strings.TrimSpace(chatID) == "" || strings.TrimSpace(messageID) == "" { + return + } + c.ClearToolFeedbackMessage(chatID) + _ = c.DeleteMessage(ctx, chatID, messageID) +} + +func (c *DiscordChannel) finalizeTrackedToolFeedbackMessage( + ctx context.Context, + chatID string, + content string, + editFn func(context.Context, string, string, string) error, +) ([]string, bool) { + msgID, baseContent, ok := c.takeToolFeedbackMessage(chatID) + if !ok || editFn == nil { + return nil, false + } + if err := editFn(ctx, chatID, msgID, content); err != nil { + c.RecordToolFeedbackMessage(chatID, msgID, baseContent) + return nil, false + } + return []string{msgID}, true +} + +func (c *DiscordChannel) FinalizeToolFeedbackMessage(ctx context.Context, msg bus.OutboundMessage) ([]string, bool) { + if outboundMessageIsToolFeedback(msg) { + return nil, false + } + return c.finalizeTrackedToolFeedbackMessage(ctx, msg.ChatID, msg.Content, c.EditMessage) +} + func (c *DiscordChannel) sendChunk(ctx context.Context, channelID, content, replyToID string) (string, error) { // Use the passed ctx for timeout control sendCtx, cancel := context.WithTimeout(ctx, sendTimeout) diff --git a/pkg/channels/discord/discord_test.go b/pkg/channels/discord/discord_test.go index 0cd5328f4..d42b0bc52 100644 --- a/pkg/channels/discord/discord_test.go +++ b/pkg/channels/discord/discord_test.go @@ -1,13 +1,37 @@ package discord import ( + "context" + "io" "net/http" + "net/http/httptest" "net/url" + "reflect" + "sync" "testing" + "time" "github.com/bwmarrin/discordgo" + + "github.com/sipeed/picoclaw/pkg/audio/tts" + "github.com/sipeed/picoclaw/pkg/bus" + "github.com/sipeed/picoclaw/pkg/channels" ) +type stubTTSProvider struct{} + +func (stubTTSProvider) Name() string { return "stub-tts" } + +func (stubTTSProvider) Synthesize(context.Context, string) (io.ReadCloser, error) { + return io.NopCloser(&noopReader{}), nil +} + +type noopReader struct{} + +func (*noopReader) Read(p []byte) (int, error) { + return 0, io.EOF +} + func TestApplyDiscordProxy_CustomProxy(t *testing.T) { session, err := discordgo.New("Bot test-token") if err != nil { @@ -89,3 +113,224 @@ func TestApplyDiscordProxy_InvalidProxyURL(t *testing.T) { t.Fatal("applyDiscordProxy() expected error for invalid proxy URL, got nil") } } + +func TestSend_NonToolFeedbackDeletesTrackedProgressMessage(t *testing.T) { + var ( + mu sync.Mutex + requests []string + ) + + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + mu.Lock() + requests = append(requests, r.Method+" "+r.URL.Path) + mu.Unlock() + + switch { + case r.Method == http.MethodPatch && r.URL.Path == "/channels/chat-1/messages/prog-1": + w.Header().Set("Content-Type", "application/json") + _, _ = io.WriteString(w, `{"id":"prog-1"}`) + default: + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path) + } + })) + defer server.Close() + + origChannels := discordgo.EndpointChannels + discordgo.EndpointChannels = server.URL + "/channels/" + defer func() { + discordgo.EndpointChannels = origChannels + }() + + session, err := discordgo.New("Bot test-token") + if err != nil { + t.Fatalf("discordgo.New() error: %v", err) + } + session.Client = server.Client() + + ch := &DiscordChannel{ + BaseChannel: channels.NewBaseChannel("discord", nil, bus.NewMessageBus(), nil), + session: session, + ctx: context.Background(), + typingStop: make(map[string]chan struct{}), + voiceSSRC: make(map[string]map[uint32]string), + } + ch.progress = channels.NewToolFeedbackAnimator(ch.EditMessage) + ch.SetRunning(true) + ch.RecordToolFeedbackMessage("chat-1", "prog-1", "🔧 `read_file`") + + ids, err := ch.Send(context.Background(), bus.OutboundMessage{ + ChatID: "chat-1", + Content: "final reply", + Context: bus.InboundContext{ + Channel: "discord", + ChatID: "chat-1", + }, + }) + if err != nil { + t.Fatalf("Send() error = %v", err) + } + if got, want := ids, []string{"prog-1"}; !reflect.DeepEqual(got, want) { + t.Fatalf("Send() ids = %v, want %v", got, want) + } + if _, ok := ch.currentToolFeedbackMessage("chat-1"); ok { + t.Fatal("expected tracked tool feedback message to be cleared") + } + + mu.Lock() + defer mu.Unlock() + wantRequests := []string{ + "PATCH /channels/chat-1/messages/prog-1", + } + if !reflect.DeepEqual(requests, wantRequests) { + t.Fatalf("requests = %v, want %v", requests, wantRequests) + } +} + +func TestEditMessage_UsesContextCancellation(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + select { + case <-r.Context().Done(): + return + case <-time.After(time.Second): + w.Header().Set("Content-Type", "application/json") + _, _ = io.WriteString(w, `{"id":"msg-1"}`) + } + })) + defer server.Close() + + origChannels := discordgo.EndpointChannels + discordgo.EndpointChannels = server.URL + "/channels/" + defer func() { + discordgo.EndpointChannels = origChannels + }() + + session, err := discordgo.New("Bot test-token") + if err != nil { + t.Fatalf("discordgo.New() error: %v", err) + } + session.Client = server.Client() + + ch := &DiscordChannel{ + BaseChannel: channels.NewBaseChannel("discord", nil, bus.NewMessageBus(), nil), + session: session, + } + + ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond) + defer cancel() + + start := time.Now() + err = ch.EditMessage(ctx, "chat-1", "msg-1", "still running") + elapsed := time.Since(start) + + if err == nil { + t.Fatal("expected EditMessage() to fail when context times out") + } + if elapsed >= 500*time.Millisecond { + t.Fatalf("EditMessage() ignored context timeout, elapsed=%v", elapsed) + } +} + +func TestFinalizeTrackedToolFeedbackMessage_StopsTrackingBeforeEdit(t *testing.T) { + ch := &DiscordChannel{ + progress: channels.NewToolFeedbackAnimator(nil), + } + ch.RecordToolFeedbackMessage("chat-1", "msg-1", "🔧 `read_file`") + + msgIDs, handled := ch.finalizeTrackedToolFeedbackMessage( + context.Background(), + "chat-1", + "final reply", + func(_ context.Context, chatID, messageID, content string) error { + if _, ok := ch.currentToolFeedbackMessage(chatID); ok { + t.Fatal("expected tracked tool feedback to be stopped before edit") + } + if chatID != "chat-1" || messageID != "msg-1" || content != "final reply" { + t.Fatalf("unexpected edit args: %s %s %s", chatID, messageID, content) + } + return nil + }, + ) + if !handled { + t.Fatal("expected finalizeTrackedToolFeedbackMessage to handle tracked message") + } + if got, want := msgIDs, []string{"msg-1"}; !reflect.DeepEqual(got, want) { + t.Fatalf("finalizeTrackedToolFeedbackMessage() ids = %v, want %v", got, want) + } +} + +func TestSend_NonToolFeedbackFinalizerStillStartsTTS(t *testing.T) { + var ( + mu sync.Mutex + requests []string + ) + + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + mu.Lock() + requests = append(requests, r.Method+" "+r.URL.Path) + mu.Unlock() + + switch { + case r.Method == http.MethodPatch && r.URL.Path == "/channels/chat-1/messages/prog-1": + w.Header().Set("Content-Type", "application/json") + _, _ = io.WriteString(w, `{"id":"prog-1"}`) + default: + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path) + } + })) + defer server.Close() + + origChannels := discordgo.EndpointChannels + discordgo.EndpointChannels = server.URL + "/channels/" + defer func() { + discordgo.EndpointChannels = origChannels + }() + + session, err := discordgo.New("Bot test-token") + if err != nil { + t.Fatalf("discordgo.New() error: %v", err) + } + session.Client = server.Client() + + ttsStarted := make(chan string, 1) + ch := &DiscordChannel{ + BaseChannel: channels.NewBaseChannel("discord", nil, bus.NewMessageBus(), nil), + session: session, + ctx: context.Background(), + typingStop: make(map[string]chan struct{}), + voiceSSRC: make(map[string]map[uint32]string), + tts: tts.TTSProvider(stubTTSProvider{}), + } + ch.ttsVoiceFn = func(string) (*discordgo.VoiceConnection, bool) { + return &discordgo.VoiceConnection{}, true + } + ch.playTTSFn = func(_ context.Context, _ *discordgo.VoiceConnection, text string, _ uint64) { + ttsStarted <- text + } + ch.progress = channels.NewToolFeedbackAnimator(ch.EditMessage) + ch.SetRunning(true) + ch.RecordToolFeedbackMessage("chat-1", "prog-1", "🔧 `read_file`") + + ids, err := ch.Send(context.Background(), bus.OutboundMessage{ + ChatID: "chat-1", + Content: "final reply", + Context: bus.InboundContext{ + Channel: "discord", + ChatID: "chat-1", + }, + }) + if err != nil { + t.Fatalf("Send() error = %v", err) + } + if got, want := ids, []string{"prog-1"}; !reflect.DeepEqual(got, want) { + t.Fatalf("Send() ids = %v, want %v", got, want) + } + + select { + case got := <-ttsStarted: + if got != "final reply" { + t.Fatalf("TTS content = %q, want final reply", got) + } + case <-time.After(2 * time.Second): + t.Fatal("expected TTS to start for finalized tracked tool feedback reply") + } +} diff --git a/pkg/channels/feishu/feishu_64.go b/pkg/channels/feishu/feishu_64.go index 02ee47d69..49b8dd8e5 100644 --- a/pkg/channels/feishu/feishu_64.go +++ b/pkg/channels/feishu/feishu_64.go @@ -49,6 +49,8 @@ type FeishuChannel struct { mu sync.Mutex cancel context.CancelFunc + + progress *channels.ToolFeedbackAnimator } type cachedMessage struct { @@ -74,6 +76,7 @@ func NewFeishuChannel(bc *config.Channel, cfg *config.FeishuSettings, bus *bus.M tokenCache: tc, client: lark.NewClient(cfg.AppID, cfg.AppSecret.String(), opts...), } + ch.progress = channels.NewToolFeedbackAnimator(ch.EditMessage) ch.SetOwner(ch) return ch, nil } @@ -132,6 +135,9 @@ func (c *FeishuChannel) Stop(ctx context.Context) error { } c.wsClient = nil c.mu.Unlock() + if c.progress != nil { + c.progress.StopAll() + } c.SetRunning(false) logger.InfoC("feishu", "Feishu channel stopped") @@ -149,17 +155,50 @@ func (c *FeishuChannel) Send(ctx context.Context, msg bus.OutboundMessage) ([]st return nil, fmt.Errorf("chat ID is empty: %w", channels.ErrSendFailed) } + isToolFeedback := outboundMessageIsToolFeedback(msg) + trackedMsgID, hasTrackedMsg := c.currentToolFeedbackMessage(msg.ChatID) + if isToolFeedback { + if msgID, handled, err := c.progress.Update(ctx, msg.ChatID, msg.Content); handled { + if err != nil { + return nil, err + } + return []string{msgID}, nil + } + } else { + if msgIDs, handled := c.FinalizeToolFeedbackMessage(ctx, msg); handled { + return msgIDs, nil + } + } + // Build interactive card with markdown content - cardContent, err := buildMarkdownCard(msg.Content) + sendContent := msg.Content + if isToolFeedback { + sendContent = channels.InitialAnimatedToolFeedbackContent(msg.Content) + } + cardContent, err := buildMarkdownCard(sendContent) if err != nil { // If card build fails, fall back to plain text - return nil, c.sendText(ctx, msg.ChatID, msg.Content) + msgID, sendErr := c.sendText(ctx, msg.ChatID, sendContent) + if sendErr != nil { + return nil, sendErr + } + if isToolFeedback { + c.RecordToolFeedbackMessage(msg.ChatID, msgID, msg.Content) + } else if hasTrackedMsg { + c.dismissTrackedToolFeedbackMessage(ctx, msg.ChatID, trackedMsgID) + } + return []string{msgID}, nil } // First attempt: try sending as interactive card - err = c.sendCard(ctx, msg.ChatID, cardContent) + msgID, err := c.sendCard(ctx, msg.ChatID, cardContent) if err == nil { - return nil, nil + if isToolFeedback { + c.RecordToolFeedbackMessage(msg.ChatID, msgID, msg.Content) + } else if hasTrackedMsg { + c.dismissTrackedToolFeedbackMessage(ctx, msg.ChatID, trackedMsgID) + } + return []string{msgID}, nil } // Check if error is due to card table limit (error code 11310) @@ -174,9 +213,14 @@ func (c *FeishuChannel) Send(ctx context.Context, msg bus.OutboundMessage) ([]st }) // Second attempt: fall back to plain text message - textErr := c.sendText(ctx, msg.ChatID, msg.Content) + msgID, textErr := c.sendText(ctx, msg.ChatID, sendContent) if textErr == nil { - return nil, nil + if isToolFeedback { + c.RecordToolFeedbackMessage(msg.ChatID, msgID, msg.Content) + } else if hasTrackedMsg { + c.dismissTrackedToolFeedbackMessage(ctx, msg.ChatID, trackedMsgID) + } + return []string{msgID}, nil } // If text also fails, return the text error return nil, textErr @@ -210,6 +254,23 @@ func (c *FeishuChannel) EditMessage(ctx context.Context, chatID, messageID, cont return nil } +// DeleteMessage implements channels.MessageDeleter. +func (c *FeishuChannel) DeleteMessage(ctx context.Context, chatID, messageID string) error { + req := larkim.NewDeleteMessageReqBuilder(). + MessageId(messageID). + Build() + + resp, err := c.client.Im.V1.Message.Delete(ctx, req) + if err != nil { + return fmt.Errorf("feishu delete: %w", err) + } + if !resp.Success() { + c.invalidateTokenOnAuthError(resp.Code) + return fmt.Errorf("feishu delete api error (code=%d msg=%s)", resp.Code, resp.Msg) + } + return nil +} + // SendPlaceholder implements channels.PlaceholderCapable. // Sends an interactive card with placeholder text and returns its message ID. func (c *FeishuChannel) SendPlaceholder(ctx context.Context, chatID string) (string, error) { @@ -251,6 +312,81 @@ func (c *FeishuChannel) SendPlaceholder(ctx context.Context, chatID string) (str return "", nil } +func outboundMessageIsToolFeedback(msg bus.OutboundMessage) bool { + if len(msg.Context.Raw) == 0 { + return false + } + return strings.EqualFold(strings.TrimSpace(msg.Context.Raw["message_kind"]), "tool_feedback") +} + +func (c *FeishuChannel) currentToolFeedbackMessage(chatID string) (string, bool) { + if c.progress == nil { + return "", false + } + return c.progress.Current(chatID) +} + +func (c *FeishuChannel) takeToolFeedbackMessage(chatID string) (string, string, bool) { + if c.progress == nil { + return "", "", false + } + return c.progress.Take(chatID) +} + +func (c *FeishuChannel) RecordToolFeedbackMessage(chatID, messageID, content string) { + if c.progress == nil { + return + } + c.progress.Record(chatID, messageID, content) +} + +func (c *FeishuChannel) ClearToolFeedbackMessage(chatID string) { + if c.progress == nil { + return + } + c.progress.Clear(chatID) +} + +func (c *FeishuChannel) DismissToolFeedbackMessage(ctx context.Context, chatID string) { + msgID, ok := c.currentToolFeedbackMessage(chatID) + if !ok { + return + } + c.dismissTrackedToolFeedbackMessage(ctx, chatID, msgID) +} + +func (c *FeishuChannel) dismissTrackedToolFeedbackMessage(ctx context.Context, chatID, messageID string) { + if strings.TrimSpace(chatID) == "" || strings.TrimSpace(messageID) == "" { + return + } + c.ClearToolFeedbackMessage(chatID) + _ = c.DeleteMessage(ctx, chatID, messageID) +} + +func (c *FeishuChannel) finalizeTrackedToolFeedbackMessage( + ctx context.Context, + chatID string, + content string, + editFn func(context.Context, string, string, string) error, +) ([]string, bool) { + msgID, baseContent, ok := c.takeToolFeedbackMessage(chatID) + if !ok || editFn == nil { + return nil, false + } + if err := editFn(ctx, chatID, msgID, content); err != nil { + c.RecordToolFeedbackMessage(chatID, msgID, baseContent) + return nil, false + } + return []string{msgID}, true +} + +func (c *FeishuChannel) FinalizeToolFeedbackMessage(ctx context.Context, msg bus.OutboundMessage) ([]string, bool) { + if outboundMessageIsToolFeedback(msg) { + return nil, false + } + return c.finalizeTrackedToolFeedbackMessage(ctx, msg.ChatID, msg.Content, c.EditMessage) +} + // ReactToMessage implements channels.ReactionCapable. // Adds a reaction (randomly chosen from config) and returns an undo function to remove it. func (c *FeishuChannel) ReactToMessage(ctx context.Context, chatID, messageID string) (func(), error) { @@ -323,6 +459,7 @@ func (c *FeishuChannel) SendMedia(ctx context.Context, msg bus.OutboundMediaMess if !c.IsRunning() { return nil, channels.ErrNotRunning } + trackedMsgID, hasTrackedMsg := c.currentToolFeedbackMessage(msg.ChatID) if msg.ChatID == "" { return nil, fmt.Errorf("chat ID is empty: %w", channels.ErrSendFailed) @@ -339,6 +476,10 @@ func (c *FeishuChannel) SendMedia(ctx context.Context, msg bus.OutboundMediaMess } } + if hasTrackedMsg { + c.dismissTrackedToolFeedbackMessage(ctx, msg.ChatID, trackedMsgID) + } + return nil, nil } @@ -801,7 +942,7 @@ func appendMediaTags(content, messageType string, mediaRefs []string) string { } // sendCard sends an interactive card message to a chat. -func (c *FeishuChannel) sendCard(ctx context.Context, chatID, cardContent string) error { +func (c *FeishuChannel) sendCard(ctx context.Context, chatID, cardContent string) (string, error) { req := larkim.NewCreateMessageReqBuilder(). ReceiveIdType(larkim.ReceiveIdTypeChatId). Body(larkim.NewCreateMessageReqBodyBuilder(). @@ -813,23 +954,26 @@ func (c *FeishuChannel) sendCard(ctx context.Context, chatID, cardContent string resp, err := c.client.Im.V1.Message.Create(ctx, req) if err != nil { - return fmt.Errorf("feishu send card: %w", channels.ErrTemporary) + return "", fmt.Errorf("feishu send card: %w", channels.ErrTemporary) } if !resp.Success() { c.invalidateTokenOnAuthError(resp.Code) - return fmt.Errorf("feishu api error (code=%d msg=%s): %w", resp.Code, resp.Msg, channels.ErrTemporary) + return "", fmt.Errorf("feishu api error (code=%d msg=%s): %w", resp.Code, resp.Msg, channels.ErrTemporary) } logger.DebugCF("feishu", "Feishu card message sent", map[string]any{ "chat_id": chatID, }) - return nil + if resp.Data != nil && resp.Data.MessageId != nil { + return *resp.Data.MessageId, nil + } + return "", nil } // sendText sends a plain text message to a chat (fallback when card fails). -func (c *FeishuChannel) sendText(ctx context.Context, chatID, text string) error { +func (c *FeishuChannel) sendText(ctx context.Context, chatID, text string) (string, error) { content, _ := json.Marshal(map[string]string{"text": text}) req := larkim.NewCreateMessageReqBuilder(). @@ -843,18 +987,21 @@ func (c *FeishuChannel) sendText(ctx context.Context, chatID, text string) error resp, err := c.client.Im.V1.Message.Create(ctx, req) if err != nil { - return fmt.Errorf("feishu send text: %w", channels.ErrTemporary) + return "", fmt.Errorf("feishu send text: %w", channels.ErrTemporary) } if !resp.Success() { - return fmt.Errorf("feishu text api error (code=%d msg=%s): %w", resp.Code, resp.Msg, channels.ErrTemporary) + return "", fmt.Errorf("feishu text api error (code=%d msg=%s): %w", resp.Code, resp.Msg, channels.ErrTemporary) } logger.DebugCF("feishu", "Feishu text message sent (fallback)", map[string]any{ "chat_id": chatID, }) - return nil + if resp.Data != nil && resp.Data.MessageId != nil { + return *resp.Data.MessageId, nil + } + return "", nil } // sendImage uploads an image and sends it as a message. diff --git a/pkg/channels/feishu/feishu_64_test.go b/pkg/channels/feishu/feishu_64_test.go index 9010abf69..0bdac0352 100644 --- a/pkg/channels/feishu/feishu_64_test.go +++ b/pkg/channels/feishu/feishu_64_test.go @@ -3,9 +3,13 @@ package feishu import ( + "context" + "errors" "testing" larkim "github.com/larksuite/oapi-sdk-go/v3/service/im/v1" + + "github.com/sipeed/picoclaw/pkg/channels" ) func TestExtractContent(t *testing.T) { @@ -279,3 +283,84 @@ func TestExtractFeishuSenderID(t *testing.T) { }) } } + +func TestFinalizeTrackedToolFeedbackMessage_ClearAfterSuccessfulEdit(t *testing.T) { + ch := &FeishuChannel{ + progress: channels.NewToolFeedbackAnimator(nil), + } + ch.RecordToolFeedbackMessage("chat-1", "msg-1", "🔧 `read_file`") + + msgIDs, handled := ch.finalizeTrackedToolFeedbackMessage( + context.Background(), + "chat-1", + "final reply", + func(_ context.Context, chatID, messageID, content string) error { + if chatID != "chat-1" || messageID != "msg-1" || content != "final reply" { + t.Fatalf("unexpected edit args: %s %s %s", chatID, messageID, content) + } + return nil + }, + ) + if !handled { + t.Fatal("expected finalizeTrackedToolFeedbackMessage to handle tracked message") + } + if len(msgIDs) != 1 || msgIDs[0] != "msg-1" { + t.Fatalf("unexpected msgIDs: %v", msgIDs) + } + if _, ok := ch.currentToolFeedbackMessage("chat-1"); ok { + t.Fatal("expected tracked tool feedback to be cleared after successful edit") + } +} + +func TestFinalizeTrackedToolFeedbackMessage_StopsTrackingBeforeEdit(t *testing.T) { + ch := &FeishuChannel{ + progress: channels.NewToolFeedbackAnimator(nil), + } + ch.RecordToolFeedbackMessage("chat-1", "msg-1", "🔧 `read_file`") + + msgIDs, handled := ch.finalizeTrackedToolFeedbackMessage( + context.Background(), + "chat-1", + "final reply", + func(_ context.Context, chatID, messageID, content string) error { + if _, ok := ch.currentToolFeedbackMessage(chatID); ok { + t.Fatal("expected tracked tool feedback to be stopped before edit") + } + if chatID != "chat-1" || messageID != "msg-1" || content != "final reply" { + t.Fatalf("unexpected edit args: %s %s %s", chatID, messageID, content) + } + return nil + }, + ) + if !handled { + t.Fatal("expected finalizeTrackedToolFeedbackMessage to handle tracked message") + } + if len(msgIDs) != 1 || msgIDs[0] != "msg-1" { + t.Fatalf("unexpected msgIDs: %v", msgIDs) + } +} + +func TestFinalizeTrackedToolFeedbackMessage_EditFailureKeepsTrackedMessage(t *testing.T) { + ch := &FeishuChannel{ + progress: channels.NewToolFeedbackAnimator(nil), + } + ch.RecordToolFeedbackMessage("chat-1", "msg-1", "🔧 `read_file`") + + msgIDs, handled := ch.finalizeTrackedToolFeedbackMessage( + context.Background(), + "chat-1", + "final reply", + func(context.Context, string, string, string) error { + return errors.New("edit failed") + }, + ) + if handled { + t.Fatal("expected finalizeTrackedToolFeedbackMessage to report unhandled on edit failure") + } + if len(msgIDs) != 0 { + t.Fatalf("unexpected msgIDs: %v", msgIDs) + } + if msgID, ok := ch.currentToolFeedbackMessage("chat-1"); !ok || msgID != "msg-1" { + t.Fatalf("expected tracked tool feedback to remain after failed edit, got (%q, %v)", msgID, ok) + } +} diff --git a/pkg/channels/manager.go b/pkg/channels/manager.go index 928676cbc..6aec966d6 100644 --- a/pkg/channels/manager.go +++ b/pkg/channels/manager.go @@ -14,6 +14,7 @@ import ( "net" "net/http" "sort" + "strings" "sync" "time" @@ -25,6 +26,7 @@ import ( "github.com/sipeed/picoclaw/pkg/health" "github.com/sipeed/picoclaw/pkg/logger" "github.com/sipeed/picoclaw/pkg/media" + "github.com/sipeed/picoclaw/pkg/utils" ) const ( @@ -96,6 +98,15 @@ type Manager struct { channelHashes map[string]string // channel name → config hash } +type toolFeedbackMessageTracker interface { + RecordToolFeedbackMessage(chatID, messageID, content string) + ClearToolFeedbackMessage(chatID string) +} + +type toolFeedbackMessageCleaner interface { + DismissToolFeedbackMessage(ctx context.Context, chatID string) +} + type asyncTask struct { cancel context.CancelFunc } @@ -108,6 +119,13 @@ func outboundMessageChatID(msg bus.OutboundMessage) string { return msg.ChatID } +func outboundMessageIsToolFeedback(msg bus.OutboundMessage) bool { + if len(msg.Context.Raw) == 0 { + return false + } + return strings.EqualFold(strings.TrimSpace(msg.Context.Raw["message_kind"]), "tool_feedback") +} + func outboundMediaChannel(msg bus.OutboundMediaMessage) string { return msg.Context.Channel } @@ -116,6 +134,16 @@ func outboundMediaChatID(msg bus.OutboundMediaMessage) string { return msg.ChatID } +func dismissTrackedToolFeedbackMessage(ctx context.Context, ch Channel, chatID string) { + if cleaner, ok := ch.(toolFeedbackMessageCleaner); ok { + cleaner.DismissToolFeedbackMessage(ctx, chatID) + return + } + if tracker, ok := ch.(toolFeedbackMessageTracker); ok { + tracker.ClearToolFeedbackMessage(chatID) + } +} + // RecordPlaceholder registers a placeholder message for later editing. // Implements PlaceholderRecorder. func (m *Manager) RecordPlaceholder(channel, chatID, placeholderID string) { @@ -196,7 +224,19 @@ func (m *Manager) preSend(ctx context.Context, name string, msg bus.OutboundMess } } - // 3. If a stream already finalized this message, delete the placeholder and skip send + isToolFeedback := outboundMessageIsToolFeedback(msg) + + // 3. If a stream already finalized this chat, stale tool feedback must be + // dropped without consuming the final-response marker. Streaming finalization + // bypasses the worker queue, so older queued feedback can arrive before the + // normal final outbound message that cleans up the marker and placeholder. + if isToolFeedback { + if _, loaded := m.streamActive.Load(key); loaded { + return nil, true + } + } + + // 4. If a stream already finalized this message, delete the placeholder and skip send if _, loaded := m.streamActive.LoadAndDelete(key); loaded { if v, loaded := m.placeholders.LoadAndDelete(key); loaded { if entry, ok := v.(placeholderEntry); ok && entry.id != "" { @@ -208,14 +248,26 @@ func (m *Manager) preSend(ctx context.Context, name string, msg bus.OutboundMess } } } + if !isToolFeedback { + dismissTrackedToolFeedbackMessage(ctx, ch, chatID) + } return nil, true } - // 4. Try editing placeholder + // 5. Try editing placeholder if v, loaded := m.placeholders.LoadAndDelete(key); loaded { if entry, ok := v.(placeholderEntry); ok && entry.id != "" { if editor, ok := ch.(MessageEditor); ok { - if err := editor.EditMessage(ctx, chatID, entry.id, msg.Content); err == nil { + content := msg.Content + if isToolFeedback { + content = InitialAnimatedToolFeedbackContent(msg.Content) + } + if err := editor.EditMessage(ctx, chatID, entry.id, content); err == nil { + if tracker, ok := ch.(toolFeedbackMessageTracker); ok && isToolFeedback { + tracker.RecordToolFeedbackMessage(chatID, entry.id, msg.Content) + } else if !isToolFeedback { + dismissTrackedToolFeedbackMessage(ctx, ch, chatID) + } return []string{entry.id}, true } // edit failed → fall through to normal Send @@ -312,22 +364,27 @@ func (m *Manager) GetStreamer(ctx context.Context, channelName, chatID string) ( // Mark streamActive on Finalize so preSend knows to clean up the placeholder key := channelName + ":" + chatID return &finalizeHookStreamer{ - Streamer: streamer, - onFinalize: func() { m.streamActive.Store(key, true) }, + Streamer: streamer, + onFinalize: func(finalizeCtx context.Context) { + dismissTrackedToolFeedbackMessage(finalizeCtx, ch, chatID) + m.streamActive.Store(key, true) + }, }, true } // finalizeHookStreamer wraps a Streamer to run a hook on Finalize. type finalizeHookStreamer struct { Streamer - onFinalize func() + onFinalize func(context.Context) } func (s *finalizeHookStreamer) Finalize(ctx context.Context, content string) error { if err := s.Streamer.Finalize(ctx, content); err != nil { return err } - s.onFinalize() + if s.onFinalize != nil { + s.onFinalize(ctx) + } return nil } @@ -769,18 +826,21 @@ func (m *Manager) runWorker(ctx context.Context, name string, w *channelWorker) // Collect all message chunks to send var chunks []string - // Step 1: Try marker-based splitting if enabled - if m.config != nil && m.config.Agents.Defaults.SplitOnMarker { + // Step 1: Try marker-based splitting if enabled. + // Tool feedback must stay a single message, so it skips marker splitting. + if m.config != nil && m.config.Agents.Defaults.SplitOnMarker && !outboundMessageIsToolFeedback(msg) { if markerChunks := SplitByMarker(msg.Content); len(markerChunks) > 1 { for _, chunk := range markerChunks { - chunks = append(chunks, splitByLength(chunk, maxLen)...) + chunkMsg := msg + chunkMsg.Content = chunk + chunks = append(chunks, splitOutboundMessageContent(chunkMsg, maxLen)...) } } } // Step 2: Fallback to length-based splitting if no chunks from marker if len(chunks) == 0 { - chunks = splitByLength(msg.Content, maxLen) + chunks = splitOutboundMessageContent(msg, maxLen) } // Step 3: Send all chunks @@ -795,12 +855,25 @@ func (m *Manager) runWorker(ctx context.Context, name string, w *channelWorker) } } -// splitByLength splits content by maxLen if needed, otherwise returns single chunk. -func splitByLength(content string, maxLen int) []string { - if maxLen > 0 && len([]rune(content)) > maxLen { - return SplitMessage(content, maxLen) +// splitOutboundMessageContent splits regular outbound content by maxLen, but +// keeps tool feedback in a single message by truncating the explanation body. +func splitOutboundMessageContent(msg bus.OutboundMessage, maxLen int) []string { + if maxLen > 0 { + if outboundMessageIsToolFeedback(msg) { + animationSafeLen := maxLen - MaxToolFeedbackAnimationFrameLength() + if animationSafeLen <= 0 { + animationSafeLen = maxLen + } + if len([]rune(msg.Content)) > animationSafeLen { + return []string{utils.FitToolFeedbackMessage(msg.Content, animationSafeLen)} + } + return []string{msg.Content} + } + if len([]rune(msg.Content)) > maxLen { + return SplitMessage(msg.Content, maxLen) + } } - return []string{content} + return []string{msg.Content} } // sendWithRetry sends a message through the channel with rate limiting and @@ -1264,13 +1337,16 @@ func (m *Manager) SendMessage(ctx context.Context, msg bus.OutboundMessage) erro if mlp, ok := w.ch.(MessageLengthProvider); ok { maxLen = mlp.MaxMessageLength() } - if maxLen > 0 && len([]rune(msg.Content)) > maxLen { - for _, chunk := range SplitMessage(msg.Content, maxLen) { + if chunks := splitOutboundMessageContent(msg, maxLen); len(chunks) > 1 { + for _, chunk := range chunks { chunkMsg := msg chunkMsg.Content = chunk m.sendWithRetry(ctx, channelName, w, chunkMsg) } } else { + if len(chunks) == 1 { + msg.Content = chunks[0] + } m.sendWithRetry(ctx, channelName, w, msg) } return nil diff --git a/pkg/channels/manager_test.go b/pkg/channels/manager_test.go index 881993d9c..4f6a7dcf4 100644 --- a/pkg/channels/manager_test.go +++ b/pkg/channels/manager_test.go @@ -13,6 +13,8 @@ import ( "golang.org/x/time/rate" "github.com/sipeed/picoclaw/pkg/bus" + "github.com/sipeed/picoclaw/pkg/config" + "github.com/sipeed/picoclaw/pkg/utils" ) // mockChannel is a test double that delegates Send to a configurable function. @@ -76,8 +78,9 @@ func (m *mockMediaChannel) SendMedia(ctx context.Context, msg bus.OutboundMediaM type mockDeletingMediaChannel struct { mockMediaChannel - deleteCalls int - lastDeleted struct { + deleteCalls int + dismissedChatID string + lastDeleted struct { chatID string messageID string } @@ -94,6 +97,37 @@ func (m *mockDeletingMediaChannel) DeleteMessage( return nil } +func (m *mockDeletingMediaChannel) DismissToolFeedbackMessage(_ context.Context, chatID string) { + m.dismissedChatID = chatID +} + +type mockStreamer struct { + finalizeFn func(context.Context, string) error +} + +func (m *mockStreamer) Update(context.Context, string) error { return nil } + +func (m *mockStreamer) Finalize(ctx context.Context, content string) error { + if m.finalizeFn != nil { + return m.finalizeFn(ctx, content) + } + return nil +} + +func (m *mockStreamer) Cancel(context.Context) {} + +type mockStreamingChannel struct { + mockMessageEditor + streamer Streamer +} + +func (m *mockStreamingChannel) BeginStream(context.Context, string) (Streamer, error) { + if m.streamer == nil { + return nil, errors.New("missing streamer") + } + return m.streamer, nil +} + // newTestManager creates a minimal Manager suitable for unit tests. func newTestManager() *Manager { return &Manager{ @@ -715,13 +749,43 @@ func TestSendWithRetry_ExponentialBackoff(t *testing.T) { // mockMessageEditor is a channel that supports MessageEditor. type mockMessageEditor struct { mockChannel - editFn func(ctx context.Context, chatID, messageID, content string) error + editFn func(ctx context.Context, chatID, messageID, content string) error + finalizeFn func(ctx context.Context, msg bus.OutboundMessage) ([]string, bool) + finalizeCalled bool + recordedChatID string + recordedMessageID string + clearedChatID string + dismissedChatID string } func (m *mockMessageEditor) EditMessage(ctx context.Context, chatID, messageID, content string) error { return m.editFn(ctx, chatID, messageID, content) } +func (m *mockMessageEditor) RecordToolFeedbackMessage(chatID, messageID, _ string) { + m.recordedChatID = chatID + m.recordedMessageID = messageID +} + +func (m *mockMessageEditor) ClearToolFeedbackMessage(chatID string) { + m.clearedChatID = chatID +} + +func (m *mockMessageEditor) DismissToolFeedbackMessage(_ context.Context, chatID string) { + m.dismissedChatID = chatID +} + +func (m *mockMessageEditor) FinalizeToolFeedbackMessage( + ctx context.Context, + msg bus.OutboundMessage, +) ([]string, bool) { + m.finalizeCalled = true + if m.finalizeFn == nil { + return nil, false + } + return m.finalizeFn(ctx, msg) +} + func TestPreSend_PlaceholderEditSuccess(t *testing.T) { m := newTestManager() var sendCalled bool @@ -766,6 +830,360 @@ func TestPreSend_PlaceholderEditSuccess(t *testing.T) { } } +func TestPreSend_ToolFeedbackPlaceholderEditRecordsTrackedMessage(t *testing.T) { + m := newTestManager() + + ch := &mockMessageEditor{ + editFn: func(_ context.Context, chatID, messageID, content string) error { + if chatID != "123" || messageID != "456" || content != "hello" { + t.Fatalf("unexpected edit args: %s %s %s", chatID, messageID, content) + } + return nil + }, + } + + m.RecordPlaceholder("test", "123", "456") + + msg := testOutboundMessage(bus.OutboundMessage{ + Channel: "test", + ChatID: "123", + Content: "hello", + Context: bus.InboundContext{ + Channel: "test", + ChatID: "123", + Raw: map[string]string{ + "message_kind": "tool_feedback", + }, + }, + }) + _, edited := m.preSend(context.Background(), "test", msg, ch) + if !edited { + t.Fatal("expected preSend to edit placeholder") + } + if ch.recordedChatID != "123" || ch.recordedMessageID != "456" { + t.Fatalf("expected tracked message 123/456, got %q/%q", ch.recordedChatID, ch.recordedMessageID) + } +} + +func TestPreSend_NonToolFeedbackLeavesTrackedMessageForChannelSend(t *testing.T) { + m := newTestManager() + ch := &mockMessageEditor{} + + msg := testOutboundMessage(bus.OutboundMessage{ + Channel: "test", + ChatID: "123", + Content: "final reply", + Context: bus.InboundContext{ + Channel: "test", + ChatID: "123", + }, + }) + + _, edited := m.preSend(context.Background(), "test", msg, ch) + if edited { + t.Fatal("expected preSend to fall through when no placeholder exists") + } + if ch.dismissedChatID != "" { + t.Fatalf("expected tracked tool feedback cleanup to be deferred to channel send, got %q", ch.dismissedChatID) + } +} + +func TestPreSend_NonToolFeedbackDefersTrackedMessageFinalizationToChannelSend(t *testing.T) { + m := newTestManager() + ch := &mockMessageEditor{ + finalizeFn: func(_ context.Context, msg bus.OutboundMessage) ([]string, bool) { + if msg.ChatID != "123" || msg.Content != "final reply" { + t.Fatalf("unexpected finalize msg: %+v", msg) + } + return []string{"tool-msg-1"}, true + }, + } + + msg := testOutboundMessage(bus.OutboundMessage{ + Channel: "test", + ChatID: "123", + Content: "final reply", + Context: bus.InboundContext{ + Channel: "test", + ChatID: "123", + }, + }) + + msgIDs, handled := m.preSend(context.Background(), "test", msg, ch) + if handled { + t.Fatalf("expected preSend to defer to channel Send, got msgIDs=%v", msgIDs) + } + if len(msgIDs) != 0 { + t.Fatalf("expected no msgIDs from preSend, got %v", msgIDs) + } + if ch.dismissedChatID != "" { + t.Fatalf("expected tracked cleanup to remain in channel Send, got %q", ch.dismissedChatID) + } + if ch.finalizeCalled { + t.Fatal("expected preSend to skip channel tool feedback finalization") + } +} + +func TestPreSend_StaleToolFeedbackDoesNotConsumeStreamActiveMarker(t *testing.T) { + m := newTestManager() + m.streamActive.Store("test:123", true) + m.RecordPlaceholder("test", "123", "placeholder-1") + + var editedContent string + ch := &mockMessageEditor{ + editFn: func(_ context.Context, chatID, messageID, content string) error { + if chatID != "123" || messageID != "placeholder-1" { + t.Fatalf("unexpected edit target: %s/%s", chatID, messageID) + } + editedContent = content + return nil + }, + } + + toolFeedback := testOutboundMessage(bus.OutboundMessage{ + Channel: "test", + ChatID: "123", + Content: "🔧 `read_file`\nReading config", + Context: bus.InboundContext{ + Channel: "test", + ChatID: "123", + Raw: map[string]string{ + "message_kind": "tool_feedback", + }, + }, + }) + + msgIDs, handled := m.preSend(context.Background(), "test", toolFeedback, ch) + if !handled { + t.Fatal("expected stale tool feedback to be dropped after stream finalize") + } + if len(msgIDs) != 0 { + t.Fatalf("expected no delivered message IDs for stale feedback, got %v", msgIDs) + } + if _, ok := m.streamActive.Load("test:123"); !ok { + t.Fatal("expected streamActive marker to remain for the final outbound message") + } + if _, ok := m.placeholders.Load("test:123"); !ok { + t.Fatal("expected placeholder cleanup to remain deferred to the final outbound message") + } + if ch.editedMessages != 0 { + t.Fatalf("expected no placeholder edit for stale feedback, got %d edits", ch.editedMessages) + } + + finalMsg := testOutboundMessage(bus.OutboundMessage{ + Channel: "test", + ChatID: "123", + Content: "final streamed reply", + Context: bus.InboundContext{ + Channel: "test", + ChatID: "123", + }, + }) + + _, handled = m.preSend(context.Background(), "test", finalMsg, ch) + if !handled { + t.Fatal("expected final outbound message to consume streamActive marker") + } + if _, ok := m.streamActive.Load("test:123"); ok { + t.Fatal("expected streamActive marker to be cleared by final outbound message") + } + if _, ok := m.placeholders.Load("test:123"); ok { + t.Fatal("expected placeholder to be cleaned up by final outbound message") + } + if editedContent != "final streamed reply" { + t.Fatalf("editedContent = %q, want final streamed reply", editedContent) + } +} + +func TestPreSendMedia_LeavesTrackedMessageForChannelSend(t *testing.T) { + m := newTestManager() + ch := &mockDeletingMediaChannel{} + + m.preSendMedia(context.Background(), "test", bus.OutboundMediaMessage{ + ChatID: "123", + Context: bus.InboundContext{ + Channel: "test", + ChatID: "123", + }, + }, ch) + + if ch.dismissedChatID != "" { + t.Fatalf( + "expected tracked tool feedback cleanup to be deferred to channel media send, got %q", + ch.dismissedChatID, + ) + } +} + +func TestSplitOutboundMessageContent_ToolFeedbackTruncatesInsteadOfSplitting(t *testing.T) { + msg := testOutboundMessage(bus.OutboundMessage{ + Channel: "test", + ChatID: "123", + Content: "\U0001f527 `read_file`\nRead README.md first to confirm the current project structure before editing the config example.", + Context: bus.InboundContext{ + Channel: "test", + ChatID: "123", + Raw: map[string]string{ + "message_kind": "tool_feedback", + }, + }, + }) + + chunks := splitOutboundMessageContent(msg, 40) + if len(chunks) != 1 { + t.Fatalf("len(chunks) = %d, want 1", len(chunks)) + } + want := utils.FitToolFeedbackMessage(msg.Content, 40-MaxToolFeedbackAnimationFrameLength()) + if chunks[0] != want { + t.Fatalf("chunk = %q, want %q", chunks[0], want) + } +} + +func TestSplitOutboundMessageContent_ToolFeedbackReservesAnimationFrame(t *testing.T) { + msg := testOutboundMessage(bus.OutboundMessage{ + Channel: "test", + ChatID: "123", + Content: "🔧 `read_file`\n1234567890", + Context: bus.InboundContext{ + Channel: "test", + ChatID: "123", + Raw: map[string]string{ + "message_kind": "tool_feedback", + }, + }, + }) + + chunks := splitOutboundMessageContent(msg, len([]rune(msg.Content))) + if len(chunks) != 1 { + t.Fatalf("len(chunks) = %d, want 1", len(chunks)) + } + + animated := formatAnimatedToolFeedbackContent(chunks[0], strings.Repeat(".", MaxToolFeedbackAnimationFrameLength())) + if got, maxLen := len([]rune(animated)), len([]rune(msg.Content)); got > maxLen { + t.Fatalf("animated len = %d, want <= %d; content=%q", got, maxLen, animated) + } +} + +func TestGetStreamer_FinalizeDismissesTrackedToolFeedback(t *testing.T) { + m := newTestManager() + ch := &mockStreamingChannel{ + mockMessageEditor: mockMessageEditor{}, + streamer: &mockStreamer{ + finalizeFn: func(_ context.Context, content string) error { + if content != "final reply" { + t.Fatalf("unexpected finalize content: %q", content) + } + return nil + }, + }, + } + m.channels["test"] = ch + + streamer, ok := m.GetStreamer(context.Background(), "test", "123") + if !ok { + t.Fatal("expected streamer to be available") + } + if err := streamer.Finalize(context.Background(), "final reply"); err != nil { + t.Fatalf("Finalize() error = %v", err) + } + if ch.dismissedChatID != "123" { + t.Fatalf("expected tracked tool feedback to be dismissed for chat 123, got %q", ch.dismissedChatID) + } + if _, ok := m.streamActive.Load("test:123"); !ok { + t.Fatal("expected streamActive marker to be recorded after finalize") + } +} + +func TestGetStreamer_FinalizeFailureDoesNotDismissTrackedToolFeedback(t *testing.T) { + m := newTestManager() + ch := &mockStreamingChannel{ + mockMessageEditor: mockMessageEditor{}, + streamer: &mockStreamer{ + finalizeFn: func(context.Context, string) error { + return errors.New("finalize failed") + }, + }, + } + m.channels["test"] = ch + + streamer, ok := m.GetStreamer(context.Background(), "test", "123") + if !ok { + t.Fatal("expected streamer to be available") + } + if err := streamer.Finalize(context.Background(), "final reply"); err == nil { + t.Fatal("expected Finalize() to fail") + } + if ch.dismissedChatID != "" { + t.Fatalf("expected no tool feedback dismissal on finalize failure, got %q", ch.dismissedChatID) + } + if _, ok := m.streamActive.Load("test:123"); ok { + t.Fatal("expected no streamActive marker after finalize failure") + } +} + +func TestRunWorker_ToolFeedbackSkipsMarkerSplitting(t *testing.T) { + m := newTestManager() + m.config = &config.Config{ + Agents: config.AgentsConfig{ + Defaults: config.AgentDefaults{ + SplitOnMarker: true, + }, + }, + } + + var ( + mu sync.Mutex + received []string + ) + ch := &mockChannelWithLength{ + mockChannel: mockChannel{ + sendFn: func(_ context.Context, msg bus.OutboundMessage) error { + mu.Lock() + received = append(received, msg.Content) + mu.Unlock() + return nil + }, + }, + maxLen: 200, + } + + w := &channelWorker{ + ch: ch, + queue: make(chan bus.OutboundMessage, 1), + done: make(chan struct{}), + limiter: rate.NewLimiter(rate.Inf, 1), + } + + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + go m.runWorker(ctx, "test", w) + + content := "🔧 `read_file`\nRead current config first.<|[SPLIT]|>Then update the example." + w.queue <- testOutboundMessage(bus.OutboundMessage{ + Channel: "test", + ChatID: "123", + Content: content, + Context: bus.InboundContext{ + Channel: "test", + ChatID: "123", + Raw: map[string]string{ + "message_kind": "tool_feedback", + }, + }, + }) + + time.Sleep(100 * time.Millisecond) + + mu.Lock() + defer mu.Unlock() + if len(received) != 1 { + t.Fatalf("len(received) = %d, want 1", len(received)) + } + if received[0] != content { + t.Fatalf("received[0] = %q, want %q", received[0], content) + } +} + func TestPreSend_PlaceholderEditFails_FallsThrough(t *testing.T) { m := newTestManager() diff --git a/pkg/channels/matrix/matrix.go b/pkg/channels/matrix/matrix.go index 40e1b0a36..04599d6d2 100644 --- a/pkg/channels/matrix/matrix.go +++ b/pkg/channels/matrix/matrix.go @@ -46,6 +46,13 @@ const ( var matrixMentionHrefRegexp = regexp.MustCompile(`(?i)]+href=["']([^"']+)["']`) +func outboundMessageIsToolFeedback(msg bus.OutboundMessage) bool { + if len(msg.Context.Raw) == 0 { + return false + } + return strings.EqualFold(strings.TrimSpace(msg.Context.Raw["message_kind"]), "tool_feedback") +} + type roomKindCacheEntry struct { isGroup bool expiresAt time.Time @@ -192,6 +199,7 @@ type MatrixChannel struct { cryptoHelper *cryptohelper.CryptoHelper cryptoDbPath string + progress *channels.ToolFeedbackAnimator } func NewMatrixChannel( @@ -236,7 +244,7 @@ func NewMatrixChannel( channels.WithReasoningChannelID(bc.ReasoningChannelID), ) - return &MatrixChannel{ + ch := &MatrixChannel{ BaseChannel: base, bc: bc, client: client, @@ -248,7 +256,9 @@ func NewMatrixChannel( localpartMentionR: localpartMentionRegexp(matrixLocalpart(client.UserID)), typingMu: sync.Mutex{}, cryptoDbPath: cryptoDatabasePath, - }, nil + } + ch.progress = channels.NewToolFeedbackAnimator(ch.EditMessage) + return ch, nil } func (c *MatrixChannel) Start(ctx context.Context) error { @@ -297,6 +307,9 @@ func (c *MatrixChannel) Stop(ctx context.Context) error { c.cancel() } c.stopTypingSessions(ctx) + if c.progress != nil { + c.progress.StopAll() + } // Close crypto helper if initialized if c.cryptoHelper != nil { @@ -398,11 +411,36 @@ func (c *MatrixChannel) Send(ctx context.Context, msg bus.OutboundMessage) ([]st return nil, nil } + isToolFeedback := outboundMessageIsToolFeedback(msg) + if isToolFeedback { + if msgID, handled, err := c.progress.Update(ctx, msg.ChatID, content); handled { + if err != nil { + return nil, err + } + return []string{msgID}, nil + } + } + trackedMsgID, hasTrackedMsg := c.currentToolFeedbackMessage(msg.ChatID) + if !isToolFeedback { + if msgIDs, handled := c.FinalizeToolFeedbackMessage(ctx, msg); handled { + return msgIDs, nil + } + } + if isToolFeedback { + content = channels.InitialAnimatedToolFeedbackContent(content) + } + resp, err := c.client.SendMessageEvent(ctx, roomID, event.EventMessage, c.messageContent(content)) if err != nil { return nil, fmt.Errorf("matrix send: %w", channels.ErrTemporary) } - return []string{resp.EventID.String()}, nil + msgID := resp.EventID.String() + if isToolFeedback { + c.RecordToolFeedbackMessage(msg.ChatID, msgID, msg.Content) + } else if hasTrackedMsg { + c.dismissTrackedToolFeedbackMessage(ctx, msg.ChatID, trackedMsgID) + } + return []string{msgID}, nil } func (c *MatrixChannel) messageContent(text string) *event.MessageEventContent { @@ -419,6 +457,8 @@ func (c *MatrixChannel) SendMedia(ctx context.Context, msg bus.OutboundMediaMess if !c.IsRunning() { return nil, channels.ErrNotRunning } + trackedMsgID, hasTrackedMsg := c.currentToolFeedbackMessage(msg.ChatID) + sendCtx := ctx if sendCtx == nil { sendCtx = context.Background() @@ -529,6 +569,10 @@ func (c *MatrixChannel) SendMedia(ctx context.Context, msg bus.OutboundMediaMess } } + if hasTrackedMsg { + c.dismissTrackedToolFeedbackMessage(ctx, msg.ChatID, trackedMsgID) + } + return eventIDs, nil } @@ -612,6 +656,89 @@ func (c *MatrixChannel) EditMessage(ctx context.Context, chatID string, messageI return err } +// DeleteMessage implements channels.MessageDeleter. +func (c *MatrixChannel) DeleteMessage(ctx context.Context, chatID string, messageID string) error { + roomID := id.RoomID(strings.TrimSpace(chatID)) + if roomID == "" { + return fmt.Errorf("matrix room ID is empty") + } + eventID := id.EventID(strings.TrimSpace(messageID)) + if eventID == "" { + return fmt.Errorf("matrix message ID is empty") + } + + _, err := c.client.RedactEvent(ctx, roomID, eventID) + return err +} + +func (c *MatrixChannel) currentToolFeedbackMessage(chatID string) (string, bool) { + if c.progress == nil { + return "", false + } + return c.progress.Current(chatID) +} + +func (c *MatrixChannel) takeToolFeedbackMessage(chatID string) (string, string, bool) { + if c.progress == nil { + return "", "", false + } + return c.progress.Take(chatID) +} + +func (c *MatrixChannel) RecordToolFeedbackMessage(chatID, messageID, content string) { + if c.progress == nil { + return + } + c.progress.Record(chatID, messageID, content) +} + +func (c *MatrixChannel) ClearToolFeedbackMessage(chatID string) { + if c.progress == nil { + return + } + c.progress.Clear(chatID) +} + +func (c *MatrixChannel) DismissToolFeedbackMessage(ctx context.Context, chatID string) { + msgID, ok := c.currentToolFeedbackMessage(chatID) + if !ok { + return + } + c.dismissTrackedToolFeedbackMessage(ctx, chatID, msgID) +} + +func (c *MatrixChannel) dismissTrackedToolFeedbackMessage(ctx context.Context, chatID, messageID string) { + if strings.TrimSpace(chatID) == "" || strings.TrimSpace(messageID) == "" { + return + } + c.ClearToolFeedbackMessage(chatID) + _ = c.DeleteMessage(ctx, chatID, messageID) +} + +func (c *MatrixChannel) finalizeTrackedToolFeedbackMessage( + ctx context.Context, + chatID string, + content string, + editFn func(context.Context, string, string, string) error, +) ([]string, bool) { + msgID, baseContent, ok := c.takeToolFeedbackMessage(chatID) + if !ok || editFn == nil { + return nil, false + } + if err := editFn(ctx, chatID, msgID, content); err != nil { + c.RecordToolFeedbackMessage(chatID, msgID, baseContent) + return nil, false + } + return []string{msgID}, true +} + +func (c *MatrixChannel) FinalizeToolFeedbackMessage(ctx context.Context, msg bus.OutboundMessage) ([]string, bool) { + if outboundMessageIsToolFeedback(msg) { + return nil, false + } + return c.finalizeTrackedToolFeedbackMessage(ctx, msg.ChatID, msg.Content, c.EditMessage) +} + func (c *MatrixChannel) handleMemberEvent(ctx context.Context, evt *event.Event) { if !c.config.JoinOnInvite { return diff --git a/pkg/channels/matrix/matrix_test.go b/pkg/channels/matrix/matrix_test.go index 07f08f32b..066f08059 100644 --- a/pkg/channels/matrix/matrix_test.go +++ b/pkg/channels/matrix/matrix_test.go @@ -14,6 +14,7 @@ import ( "maunium.net/go/mautrix/event" "maunium.net/go/mautrix/id" + "github.com/sipeed/picoclaw/pkg/channels" "github.com/sipeed/picoclaw/pkg/config" "github.com/sipeed/picoclaw/pkg/media" ) @@ -41,6 +42,34 @@ func TestMatrixLocalpartMentionRegexp(t *testing.T) { } } +func TestFinalizeTrackedToolFeedbackMessage_StopsTrackingBeforeEdit(t *testing.T) { + ch := &MatrixChannel{ + progress: channels.NewToolFeedbackAnimator(nil), + } + ch.RecordToolFeedbackMessage("!room:matrix.org", "$event1", "🔧 `read_file`") + + msgIDs, handled := ch.finalizeTrackedToolFeedbackMessage( + context.Background(), + "!room:matrix.org", + "final reply", + func(_ context.Context, chatID, messageID, content string) error { + if _, ok := ch.currentToolFeedbackMessage(chatID); ok { + t.Fatal("expected tracked tool feedback to be stopped before edit") + } + if chatID != "!room:matrix.org" || messageID != "$event1" || content != "final reply" { + t.Fatalf("unexpected edit args: %s %s %s", chatID, messageID, content) + } + return nil + }, + ) + if !handled { + t.Fatal("expected finalizeTrackedToolFeedbackMessage to handle tracked message") + } + if len(msgIDs) != 1 || msgIDs[0] != "$event1" { + t.Fatalf("finalizeTrackedToolFeedbackMessage() ids = %v, want [$event1]", msgIDs) + } +} + func TestStripUserMention(t *testing.T) { userID := id.UserID("@picoclaw:matrix.org") diff --git a/pkg/channels/pico/pico.go b/pkg/channels/pico/pico.go index f998712c8..5d7bd0fa1 100644 --- a/pkg/channels/pico/pico.go +++ b/pkg/channels/pico/pico.go @@ -46,6 +46,13 @@ func outboundMessageIsThought(msg bus.OutboundMessage) bool { return strings.EqualFold(strings.TrimSpace(msg.Context.Raw["message_kind"]), MessageKindThought) } +func outboundMessageIsToolFeedback(msg bus.OutboundMessage) bool { + if len(msg.Context.Raw) == 0 { + return false + } + return strings.EqualFold(strings.TrimSpace(msg.Context.Raw["message_kind"]), "tool_feedback") +} + // writeJSON sends a JSON message to the connection with write locking. func (pc *picoConn) writeJSON(v any) error { if pc.closed.Load() { @@ -78,6 +85,7 @@ type PicoChannel struct { connsMu sync.RWMutex ctx context.Context cancel context.CancelFunc + progress *channels.ToolFeedbackAnimator } // NewPicoChannel creates a new Pico Protocol channel. @@ -106,7 +114,7 @@ func NewPicoChannel( return false } - return &PicoChannel{ + ch := &PicoChannel{ BaseChannel: base, bc: bc, config: cfg, @@ -117,7 +125,9 @@ func NewPicoChannel( }, connections: make(map[string]*picoConn), sessionConnections: make(map[string]map[string]*picoConn), - }, nil + } + ch.progress = channels.NewToolFeedbackAnimator(ch.EditMessage) + return ch, nil } // createAndAddConnection checks MaxConnections and registers a connection atomically. @@ -235,6 +245,9 @@ func (c *PicoChannel) Stop(ctx context.Context) error { if c.cancel != nil { c.cancel() } + if c.progress != nil { + c.progress.StopAll() + } logger.InfoC("pico", "Pico Protocol channel stopped") return nil @@ -261,13 +274,43 @@ func (c *PicoChannel) Send(ctx context.Context, msg bus.OutboundMessage) ([]stri return nil, channels.ErrNotRunning } isThought := outboundMessageIsThought(msg) + isToolFeedback := outboundMessageIsToolFeedback(msg) + if isToolFeedback { + if msgID, handled, err := c.progress.Update(ctx, msg.ChatID, msg.Content); handled { + if err != nil { + return nil, err + } + return []string{msgID}, nil + } + } + trackedMsgID, hasTrackedMsg := c.currentToolFeedbackMessage(msg.ChatID) + if !isToolFeedback { + if msgIDs, handled := c.FinalizeToolFeedbackMessage(ctx, msg); handled { + return msgIDs, nil + } + } + + content := msg.Content + if isToolFeedback { + content = channels.InitialAnimatedToolFeedbackContent(msg.Content) + } + msgID := uuid.New().String() outMsg := newMessage(TypeMessageCreate, map[string]any{ - PayloadKeyContent: msg.Content, + PayloadKeyContent: content, PayloadKeyThought: isThought, + "message_id": msgID, }) - return nil, c.broadcastToSession(msg.ChatID, outMsg) + if err := c.broadcastToSession(msg.ChatID, outMsg); err != nil { + return nil, err + } + if isToolFeedback { + c.RecordToolFeedbackMessage(msg.ChatID, msgID, msg.Content) + } else if hasTrackedMsg { + c.dismissTrackedToolFeedbackMessage(ctx, msg.ChatID, trackedMsgID) + } + return []string{msgID}, nil } // EditMessage implements channels.MessageEditor. @@ -279,6 +322,73 @@ func (c *PicoChannel) EditMessage(ctx context.Context, chatID string, messageID return c.broadcastToSession(chatID, outMsg) } +func (c *PicoChannel) currentToolFeedbackMessage(chatID string) (string, bool) { + if c.progress == nil { + return "", false + } + return c.progress.Current(chatID) +} + +func (c *PicoChannel) takeToolFeedbackMessage(chatID string) (string, string, bool) { + if c.progress == nil { + return "", "", false + } + return c.progress.Take(chatID) +} + +func (c *PicoChannel) RecordToolFeedbackMessage(chatID, messageID, content string) { + if c.progress == nil { + return + } + c.progress.Record(chatID, messageID, content) +} + +func (c *PicoChannel) ClearToolFeedbackMessage(chatID string) { + if c.progress == nil { + return + } + c.progress.Clear(chatID) +} + +func (c *PicoChannel) DismissToolFeedbackMessage(ctx context.Context, chatID string) { + msgID, ok := c.currentToolFeedbackMessage(chatID) + if !ok { + return + } + c.dismissTrackedToolFeedbackMessage(ctx, chatID, msgID) +} + +func (c *PicoChannel) dismissTrackedToolFeedbackMessage(ctx context.Context, chatID, messageID string) { + if strings.TrimSpace(chatID) == "" || strings.TrimSpace(messageID) == "" { + return + } + c.ClearToolFeedbackMessage(chatID) +} + +func (c *PicoChannel) finalizeTrackedToolFeedbackMessage( + ctx context.Context, + chatID string, + content string, + editFn func(context.Context, string, string, string) error, +) ([]string, bool) { + msgID, baseContent, ok := c.takeToolFeedbackMessage(chatID) + if !ok || editFn == nil { + return nil, false + } + if err := editFn(ctx, chatID, msgID, content); err != nil { + c.RecordToolFeedbackMessage(chatID, msgID, baseContent) + return nil, false + } + return []string{msgID}, true +} + +func (c *PicoChannel) FinalizeToolFeedbackMessage(ctx context.Context, msg bus.OutboundMessage) ([]string, bool) { + if outboundMessageIsToolFeedback(msg) { + return nil, false + } + return c.finalizeTrackedToolFeedbackMessage(ctx, msg.ChatID, msg.Content, c.EditMessage) +} + // StartTyping implements channels.TypingCapable. func (c *PicoChannel) StartTyping(ctx context.Context, chatID string) (func(), error) { startMsg := newMessage(TypeTypingStart, nil) diff --git a/pkg/channels/pico/pico_test.go b/pkg/channels/pico/pico_test.go index 59db705eb..77a146f34 100644 --- a/pkg/channels/pico/pico_test.go +++ b/pkg/channels/pico/pico_test.go @@ -27,6 +27,34 @@ func newTestPicoChannel(t *testing.T) *PicoChannel { return ch } +func TestFinalizeTrackedToolFeedbackMessage_StopsTrackingBeforeEdit(t *testing.T) { + ch := &PicoChannel{ + progress: channels.NewToolFeedbackAnimator(nil), + } + ch.RecordToolFeedbackMessage("pico:chat-1", "msg-1", "🔧 `read_file`") + + msgIDs, handled := ch.finalizeTrackedToolFeedbackMessage( + context.Background(), + "pico:chat-1", + "final reply", + func(_ context.Context, chatID, messageID, content string) error { + if _, ok := ch.currentToolFeedbackMessage(chatID); ok { + t.Fatal("expected tracked tool feedback to be stopped before edit") + } + if chatID != "pico:chat-1" || messageID != "msg-1" || content != "final reply" { + t.Fatalf("unexpected edit args: %s %s %s", chatID, messageID, content) + } + return nil + }, + ) + if !handled { + t.Fatal("expected finalizeTrackedToolFeedbackMessage to handle tracked message") + } + if len(msgIDs) != 1 || msgIDs[0] != "msg-1" { + t.Fatalf("finalizeTrackedToolFeedbackMessage() ids = %v, want [msg-1]", msgIDs) + } +} + func TestCreateAndAddConnection_RespectsMaxConnectionsConcurrently(t *testing.T) { ch := newTestPicoChannel(t) diff --git a/pkg/channels/telegram/command_registration.go b/pkg/channels/telegram/command_registration.go index d3152ec3d..c6b362601 100644 --- a/pkg/channels/telegram/command_registration.go +++ b/pkg/channels/telegram/command_registration.go @@ -66,6 +66,10 @@ func (c *TelegramChannel) startCommandRegistration(ctx context.Context, defs []c if register == nil { register = c.RegisterCommands } + delayFn := c.commandRegDelayFn + if delayFn == nil { + delayFn = commandRegistrationDelay + } regCtx, cancel := context.WithCancel(ctx) c.commandRegCancel = cancel @@ -91,7 +95,7 @@ func (c *TelegramChannel) startCommandRegistration(ctx context.Context, defs []c return } - delay := commandRegistrationDelay(attempt) + delay := delayFn(attempt) logger.WarnCF("telegram", "Telegram command registration failed; will retry", map[string]any{ "error": err.Error(), "retry_after": delay.String(), diff --git a/pkg/channels/telegram/command_registration_test.go b/pkg/channels/telegram/command_registration_test.go index 26f891b2e..c30c6f68d 100644 --- a/pkg/channels/telegram/command_registration_test.go +++ b/pkg/channels/telegram/command_registration_test.go @@ -31,14 +31,12 @@ func TestStartCommandRegistration_DoesNotBlock(t *testing.T) { } func TestStartCommandRegistration_RetriesUntilSuccessThenStops(t *testing.T) { - ch := &TelegramChannel{} + ch := &TelegramChannel{ + commandRegDelayFn: func(int) time.Duration { return 5 * time.Millisecond }, + } ctx, cancel := context.WithCancel(context.Background()) defer cancel() - origBackoff := commandRegistrationBackoff - commandRegistrationBackoff = []time.Duration{5 * time.Millisecond} - defer func() { commandRegistrationBackoff = origBackoff }() - var attempts atomic.Int32 ch.registerFunc = func(context.Context, []commands.Definition) error { n := attempts.Add(1) @@ -69,12 +67,10 @@ func TestStartCommandRegistration_RetriesUntilSuccessThenStops(t *testing.T) { } func TestStartCommandRegistration_StopsAfterCancel(t *testing.T) { - ch := &TelegramChannel{} + ch := &TelegramChannel{ + commandRegDelayFn: func(int) time.Duration { return 5 * time.Millisecond }, + } ctx, cancel := context.WithCancel(context.Background()) - - origBackoff := commandRegistrationBackoff - commandRegistrationBackoff = []time.Duration{5 * time.Millisecond} - defer func() { commandRegistrationBackoff = origBackoff }() defer cancel() var attempts atomic.Int32 diff --git a/pkg/channels/telegram/telegram.go b/pkg/channels/telegram/telegram.go index 2a9cfe4ae..8bec7856d 100644 --- a/pkg/channels/telegram/telegram.go +++ b/pkg/channels/telegram/telegram.go @@ -45,16 +45,18 @@ var ( type TelegramChannel struct { *channels.BaseChannel - bot *telego.Bot - bh *th.BotHandler - bc *config.Channel - chatIDs map[string]int64 - ctx context.Context - cancel context.CancelFunc - tgCfg *config.TelegramSettings + bot *telego.Bot + bh *th.BotHandler + bc *config.Channel + chatIDs map[string]int64 + ctx context.Context + cancel context.CancelFunc + tgCfg *config.TelegramSettings + progress *channels.ToolFeedbackAnimator - registerFunc func(context.Context, []commands.Definition) error - commandRegCancel context.CancelFunc + registerFunc func(context.Context, []commands.Definition) error + commandRegDelayFn func(int) time.Duration + commandRegCancel context.CancelFunc } func NewTelegramChannel( @@ -104,13 +106,15 @@ func NewTelegramChannel( channels.WithReasoningChannelID(bc.ReasoningChannelID), ) - return &TelegramChannel{ + ch := &TelegramChannel{ BaseChannel: base, bot: bot, bc: bc, chatIDs: make(map[string]int64), tgCfg: telegramCfg, - }, nil + } + ch.progress = channels.NewToolFeedbackAnimator(ch.EditMessage) + return ch, nil } func (c *TelegramChannel) Start(ctx context.Context) error { @@ -168,6 +172,9 @@ func (c *TelegramChannel) Stop(ctx context.Context) error { if c.cancel != nil { c.cancel() } + if c.progress != nil { + c.progress.StopAll() + } if c.commandRegCancel != nil { c.commandRegCancel() } @@ -191,12 +198,35 @@ func (c *TelegramChannel) Send(ctx context.Context, msg bus.OutboundMessage) ([] return nil, nil } + isToolFeedback := outboundMessageIsToolFeedback(msg) + toolFeedbackContent := msg.Content + if isToolFeedback { + toolFeedbackContent = fitToolFeedbackForTelegram(msg.Content, useMarkdownV2, 4096) + } + if isToolFeedback { + if msgID, handled, err := c.progress.Update(ctx, msg.ChatID, toolFeedbackContent); handled { + if err != nil { + return nil, err + } + return []string{msgID}, nil + } + } + trackedMsgID, hasTrackedMsg := c.currentToolFeedbackMessage(msg.ChatID) + if !isToolFeedback { + if msgIDs, handled := c.FinalizeToolFeedbackMessage(ctx, msg); handled { + return msgIDs, nil + } + } + // The Manager already splits messages to ≤4000 chars (WithMaxMessageLength), // so msg.Content is guaranteed to be within that limit. We still need to // check if HTML expansion pushes it beyond Telegram's 4096-char API limit. replyToID := msg.ReplyToMessageID var messageIDs []string queue := []string{msg.Content} + if isToolFeedback { + queue = []string{channels.InitialAnimatedToolFeedbackContent(toolFeedbackContent)} + } for len(queue) > 0 { chunk := queue[0] queue = queue[1:] @@ -204,6 +234,13 @@ func (c *TelegramChannel) Send(ctx context.Context, msg bus.OutboundMessage) ([] content := parseContent(chunk, useMarkdownV2) if len([]rune(content)) > 4096 { + if isToolFeedback { + fittedChunk := fitToolFeedbackForTelegram(chunk, useMarkdownV2, 4096) + if fittedChunk != "" && fittedChunk != chunk { + queue = append([]string{fittedChunk}, queue...) + continue + } + } runeChunk := []rune(chunk) ratio := float64(len(runeChunk)) / float64(len([]rune(content))) smallerLen := int(float64(4096) * ratio * 0.95) // 5% safety margin @@ -270,6 +307,12 @@ func (c *TelegramChannel) Send(ctx context.Context, msg bus.OutboundMessage) ([] replyToID = "" } + if isToolFeedback && len(messageIDs) > 0 { + c.RecordToolFeedbackMessage(msg.ChatID, messageIDs[0], toolFeedbackContent) + } else if !isToolFeedback && hasTrackedMsg { + c.dismissTrackedToolFeedbackMessage(ctx, msg.ChatID, trackedMsgID) + } + return messageIDs, nil } @@ -437,6 +480,81 @@ func (c *TelegramChannel) DeleteMessage(ctx context.Context, chatID string, mess }) } +func outboundMessageIsToolFeedback(msg bus.OutboundMessage) bool { + if len(msg.Context.Raw) == 0 { + return false + } + return strings.EqualFold(strings.TrimSpace(msg.Context.Raw["message_kind"]), "tool_feedback") +} + +func (c *TelegramChannel) currentToolFeedbackMessage(chatID string) (string, bool) { + if c.progress == nil { + return "", false + } + return c.progress.Current(chatID) +} + +func (c *TelegramChannel) takeToolFeedbackMessage(chatID string) (string, string, bool) { + if c.progress == nil { + return "", "", false + } + return c.progress.Take(chatID) +} + +func (c *TelegramChannel) RecordToolFeedbackMessage(chatID, messageID, content string) { + if c.progress == nil { + return + } + c.progress.Record(chatID, messageID, content) +} + +func (c *TelegramChannel) ClearToolFeedbackMessage(chatID string) { + if c.progress == nil { + return + } + c.progress.Clear(chatID) +} + +func (c *TelegramChannel) DismissToolFeedbackMessage(ctx context.Context, chatID string) { + msgID, ok := c.currentToolFeedbackMessage(chatID) + if !ok { + return + } + c.dismissTrackedToolFeedbackMessage(ctx, chatID, msgID) +} + +func (c *TelegramChannel) dismissTrackedToolFeedbackMessage(ctx context.Context, chatID, messageID string) { + if strings.TrimSpace(chatID) == "" || strings.TrimSpace(messageID) == "" { + return + } + c.ClearToolFeedbackMessage(chatID) + _ = c.DeleteMessage(ctx, chatID, messageID) +} + +func (c *TelegramChannel) finalizeTrackedToolFeedbackMessage( + ctx context.Context, + chatID string, + content string, + editFn func(context.Context, string, string, string) error, +) ([]string, bool) { + msgID, baseContent, ok := c.takeToolFeedbackMessage(chatID) + if !ok || editFn == nil { + return nil, false + } + if err := editFn(ctx, chatID, msgID, content); err != nil { + c.RecordToolFeedbackMessage(chatID, msgID, baseContent) + return nil, false + } + return []string{msgID}, true +} + +func (c *TelegramChannel) FinalizeToolFeedbackMessage(ctx context.Context, msg bus.OutboundMessage) ([]string, bool) { + if outboundMessageIsToolFeedback(msg) { + return nil, false + } + return c.finalizeTrackedToolFeedbackMessage(ctx, msg.ChatID, msg.Content, c.EditMessage) +} + // SendPlaceholder implements channels.PlaceholderCapable. // It sends a placeholder message (e.g. "Thinking... 💭") that will later be // edited to the actual response via EditMessage (channels.MessageEditor). @@ -468,6 +586,7 @@ func (c *TelegramChannel) SendMedia(ctx context.Context, msg bus.OutboundMediaMe if !c.IsRunning() { return nil, channels.ErrNotRunning } + trackedMsgID, hasTrackedMsg := c.currentToolFeedbackMessage(msg.ChatID) chatID, threadID, err := resolveTelegramOutboundTarget(msg.ChatID, &msg.Context) if err != nil { @@ -576,6 +695,10 @@ func (c *TelegramChannel) SendMedia(ctx context.Context, msg bus.OutboundMediaMe } } + if hasTrackedMsg { + c.dismissTrackedToolFeedbackMessage(ctx, msg.ChatID, trackedMsgID) + } + return messageIDs, nil } @@ -947,6 +1070,41 @@ func parseContent(text string, useMarkdownV2 bool) string { return markdownToTelegramHTML(text) } +func fitToolFeedbackForTelegram(content string, useMarkdownV2 bool, maxParsedLen int) string { + content = strings.TrimSpace(content) + if content == "" || maxParsedLen <= 0 { + return "" + } + animationSafeLen := maxParsedLen - channels.MaxToolFeedbackAnimationFrameLength() + if animationSafeLen <= 0 { + animationSafeLen = maxParsedLen + } + if len([]rune(parseContent(content, useMarkdownV2))) <= animationSafeLen { + return content + } + + low := 1 + high := len([]rune(content)) + best := utils.Truncate(content, 1) + + for low <= high { + mid := (low + high) / 2 + candidate := utils.FitToolFeedbackMessage(content, mid) + if candidate == "" { + high = mid - 1 + continue + } + if len([]rune(parseContent(candidate, useMarkdownV2))) <= animationSafeLen { + best = candidate + low = mid + 1 + continue + } + high = mid - 1 + } + + return best +} + // parseTelegramChatID splits "chatID/threadID" into its components. // Returns threadID=0 when no "/" is present (non-forum messages). func parseTelegramChatID(chatID string) (int64, int, error) { diff --git a/pkg/channels/telegram/telegram_group_command_filter_test.go b/pkg/channels/telegram/telegram_group_command_filter_test.go index 614b2ca7f..20b2004a9 100644 --- a/pkg/channels/telegram/telegram_group_command_filter_test.go +++ b/pkg/channels/telegram/telegram_group_command_filter_test.go @@ -108,7 +108,7 @@ func TestHandleMessage_GroupMentionOnly_BotCommandEntity(t *testing.T) { t.Fatalf("handleMessage error: %v", err) } - ctx, cancel := context.WithTimeout(context.Background(), 200*time.Microsecond) + ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond) defer cancel() select { case <-ctx.Done(): diff --git a/pkg/channels/telegram/telegram_test.go b/pkg/channels/telegram/telegram_test.go index 3d147b337..f3974723d 100644 --- a/pkg/channels/telegram/telegram_test.go +++ b/pkg/channels/telegram/telegram_test.go @@ -98,8 +98,12 @@ func (s *multipartRecordingConstructor) MultipartRequest( // successResponse returns a ta.Response that telego will treat as a successful SendMessage. func successResponse(t *testing.T) *ta.Response { + return successResponseWithMessageID(t, 1) +} + +func successResponseWithMessageID(t *testing.T, messageID int) *ta.Response { t.Helper() - msg := &telego.Message{MessageID: 1} + msg := &telego.Message{MessageID: messageID} b, err := json.Marshal(msg) require.NoError(t, err) return &ta.Response{Ok: true, Result: b} @@ -142,6 +146,7 @@ func newTestChannelWithConstructor( chatIDs: make(map[string]int64), bc: &config.Channel{Type: config.ChannelTelegram, Enabled: true}, tgCfg: &config.TelegramSettings{}, + progress: channels.NewToolFeedbackAnimator(nil), } } @@ -266,6 +271,101 @@ func TestSend_ShortMessage_SingleCall(t *testing.T) { assert.Len(t, caller.calls, 1, "short message should result in exactly one SendMessage call") } +func TestSend_NonToolFeedbackDeletesTrackedProgressMessage(t *testing.T) { + caller := &stubCaller{ + callFn: func(ctx context.Context, url string, data *ta.RequestData) (*ta.Response, error) { + switch { + case strings.Contains(url, "editMessageText"): + return successResponseWithMessageID(t, 1), nil + default: + t.Fatalf("unexpected API call: %s", url) + return nil, nil + } + }, + } + ch := newTestChannel(t, caller) + ch.RecordToolFeedbackMessage("12345", "1", "🔧 `read_file`") + + ids, err := ch.Send(context.Background(), bus.OutboundMessage{ + ChatID: "12345", + Content: "final reply", + }) + + assert.NoError(t, err) + assert.Equal(t, []string{"1"}, ids) + require.Len(t, caller.calls, 1) + assert.Contains(t, caller.calls[0].URL, "editMessageText") + _, ok := ch.currentToolFeedbackMessage("12345") + assert.False(t, ok, "tracked tool feedback should be cleared after final reply") +} + +func TestFinalizeTrackedToolFeedbackMessage_StopsTrackingBeforeEdit(t *testing.T) { + ch := newTestChannel(t, &stubCaller{ + callFn: func(context.Context, string, *ta.RequestData) (*ta.Response, error) { + t.Fatal("unexpected API call") + return nil, nil + }, + }) + ch.RecordToolFeedbackMessage("12345", "1", "🔧 `read_file`") + + msgIDs, handled := ch.finalizeTrackedToolFeedbackMessage( + context.Background(), + "12345", + "final reply", + func(_ context.Context, chatID, messageID, content string) error { + _, ok := ch.currentToolFeedbackMessage(chatID) + assert.False(t, ok, "tracked tool feedback should be stopped before edit") + assert.Equal(t, "12345", chatID) + assert.Equal(t, "1", messageID) + assert.Equal(t, "final reply", content) + return nil + }, + ) + + assert.True(t, handled) + assert.Equal(t, []string{"1"}, msgIDs) +} + +func TestSend_ToolFeedbackStaysSingleMessageAfterHTMLExpansion(t *testing.T) { + caller := &stubCaller{ + callFn: func(ctx context.Context, url string, data *ta.RequestData) (*ta.Response, error) { + return successResponse(t), nil + }, + } + ch := newTestChannel(t, caller) + + _, err := ch.Send(context.Background(), bus.OutboundMessage{ + ChatID: "12345", + Content: "🔧 `read_file`\n" + strings.Repeat("<", 2000), + Context: bus.InboundContext{ + Channel: "telegram", + ChatID: "12345", + Raw: map[string]string{ + "message_kind": "tool_feedback", + }, + }, + }) + + assert.NoError(t, err) + assert.Len(t, caller.calls, 1, "tool feedback should stay a single Telegram message after HTML escaping") +} + +func TestFitToolFeedbackForTelegram_ReservesAnimationFrame(t *testing.T) { + content := "🔧 `read_file`\n" + strings.Repeat("a", 4096) + + fitted := fitToolFeedbackForTelegram(content, false, 4096) + animated := strings.Replace( + fitted, + "`\n", + strings.Repeat(".", channels.MaxToolFeedbackAnimationFrameLength())+"`\n", + 1, + ) + + if got := len([]rune(parseContent(animated, false))); got > 4096 { + t.Fatalf("animated parsed length = %d, want <= 4096", got) + } +} + func TestSend_LongMessage_SingleCall(t *testing.T) { // With WithMaxMessageLength(4000), the Manager pre-splits messages before // they reach Send(). A message at exactly 4000 chars should go through diff --git a/pkg/channels/tool_feedback_animator.go b/pkg/channels/tool_feedback_animator.go new file mode 100644 index 000000000..b424612bf --- /dev/null +++ b/pkg/channels/tool_feedback_animator.go @@ -0,0 +1,240 @@ +package channels + +import ( + "context" + "strings" + "sync" + "time" +) + +const toolFeedbackAnimationInterval = 3 * time.Second + +const initialToolFeedbackAnimationFrame = "" + +var toolFeedbackAnimationFrames = []string{"..", "."} + +// MaxToolFeedbackAnimationFrameLength returns the largest frame suffix length +// so callers can reserve room before sending messages to length-limited APIs. +func MaxToolFeedbackAnimationFrameLength() int { + maxLen := len([]rune(initialToolFeedbackAnimationFrame)) + for _, frame := range toolFeedbackAnimationFrames { + if frameLen := len([]rune(frame)); frameLen > maxLen { + maxLen = frameLen + } + } + return maxLen +} + +type toolFeedbackAnimationState struct { + messageID string + baseContent string + stop chan struct{} + done chan struct{} +} + +type ToolFeedbackAnimator struct { + mu sync.Mutex + editFn func(ctx context.Context, chatID, messageID, content string) error + entries map[string]*toolFeedbackAnimationState +} + +func NewToolFeedbackAnimator( + editFn func(ctx context.Context, chatID, messageID, content string) error, +) *ToolFeedbackAnimator { + return &ToolFeedbackAnimator{ + editFn: editFn, + entries: make(map[string]*toolFeedbackAnimationState), + } +} + +func (a *ToolFeedbackAnimator) Current(chatID string) (string, bool) { + if a == nil || strings.TrimSpace(chatID) == "" { + return "", false + } + a.mu.Lock() + defer a.mu.Unlock() + entry, ok := a.entries[chatID] + if !ok || strings.TrimSpace(entry.messageID) == "" { + return "", false + } + return entry.messageID, true +} + +func (a *ToolFeedbackAnimator) Record(chatID, messageID, content string) { + if a == nil { + return + } + chatID = strings.TrimSpace(chatID) + messageID = strings.TrimSpace(messageID) + content = strings.TrimSpace(content) + if chatID == "" || messageID == "" || content == "" { + return + } + + entry := &toolFeedbackAnimationState{ + messageID: messageID, + baseContent: content, + stop: make(chan struct{}), + done: make(chan struct{}), + } + + var previous *toolFeedbackAnimationState + a.mu.Lock() + if old, ok := a.entries[chatID]; ok { + previous = old + } + a.entries[chatID] = entry + a.mu.Unlock() + + stopToolFeedbackAnimation(previous) + go a.run(chatID, entry) +} + +func (a *ToolFeedbackAnimator) Clear(chatID string) { + if a == nil || strings.TrimSpace(chatID) == "" { + return + } + entry := a.detach(chatID) + stopToolFeedbackAnimation(entry) +} + +func (a *ToolFeedbackAnimator) Take(chatID string) (string, string, bool) { + if a == nil || strings.TrimSpace(chatID) == "" { + return "", "", false + } + entry := a.detach(chatID) + if entry == nil || strings.TrimSpace(entry.messageID) == "" { + return "", "", false + } + stopToolFeedbackAnimation(entry) + return entry.messageID, entry.baseContent, true +} + +// Update edits an existing tracked feedback message. If the edit fails, the +// previous feedback state is restored so callers can retry without orphaning +// the old progress message. +func (a *ToolFeedbackAnimator) Update(ctx context.Context, chatID, content string) (string, bool, error) { + if a == nil || a.editFn == nil { + return "", false, nil + } + msgID, baseContent, ok := a.Take(chatID) + if !ok { + return "", false, nil + } + + animatedContent := InitialAnimatedToolFeedbackContent(content) + if err := a.editFn(ctx, strings.TrimSpace(chatID), msgID, animatedContent); err != nil { + a.Record(chatID, msgID, baseContent) + return "", true, err + } + + a.Record(chatID, msgID, content) + return msgID, true, nil +} + +func (a *ToolFeedbackAnimator) StopAll() { + if a == nil { + return + } + a.mu.Lock() + entries := make([]*toolFeedbackAnimationState, 0, len(a.entries)) + for chatID, entry := range a.entries { + entries = append(entries, entry) + delete(a.entries, chatID) + } + a.mu.Unlock() + + for _, entry := range entries { + stopToolFeedbackAnimation(entry) + } +} + +func (a *ToolFeedbackAnimator) detach(chatID string) *toolFeedbackAnimationState { + if a == nil || strings.TrimSpace(chatID) == "" { + return nil + } + a.mu.Lock() + defer a.mu.Unlock() + entry := a.entries[chatID] + delete(a.entries, chatID) + return entry +} + +func (a *ToolFeedbackAnimator) run(chatID string, entry *toolFeedbackAnimationState) { + defer close(entry.done) + + ticker := time.NewTicker(toolFeedbackAnimationInterval) + defer ticker.Stop() + + frameIdx := 1 + + for { + select { + case <-entry.stop: + return + case <-ticker.C: + if a.editFn == nil { + continue + } + frame := toolFeedbackAnimationFrames[frameIdx%len(toolFeedbackAnimationFrames)] + content := formatAnimatedToolFeedbackContent(entry.baseContent, frame) + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + _ = a.editFn(ctx, chatID, entry.messageID, content) + cancel() + frameIdx++ + } + } +} + +func InitialAnimatedToolFeedbackContent(baseContent string) string { + return formatAnimatedToolFeedbackContent(baseContent, initialToolFeedbackAnimationFrame) +} + +func formatAnimatedToolFeedbackContent(baseContent, frame string) string { + baseContent = strings.TrimSpace(baseContent) + frame = strings.TrimSpace(frame) + if baseContent == "" { + return "" + } + if frame == "" { + return baseContent + } + lineBreak := strings.IndexByte(baseContent, '\n') + if lineBreak < 0 { + return appendToolFeedbackFrame(baseContent, frame) + } + return appendToolFeedbackFrame(baseContent[:lineBreak], frame) + baseContent[lineBreak:] +} + +func appendToolFeedbackFrame(firstLine, frame string) string { + firstLine = strings.TrimSpace(firstLine) + frame = strings.TrimSpace(frame) + if firstLine == "" { + return "" + } + if frame == "" { + return firstLine + } + + openTick := strings.IndexByte(firstLine, '`') + if openTick >= 0 { + if closeOffset := strings.IndexByte(firstLine[openTick+1:], '`'); closeOffset >= 0 { + closeTick := openTick + 1 + closeOffset + return firstLine[:closeTick] + frame + firstLine[closeTick:] + } + } + + return firstLine + frame +} + +func stopToolFeedbackAnimation(entry *toolFeedbackAnimationState) { + if entry == nil { + return + } + select { + case <-entry.stop: + default: + close(entry.stop) + } + <-entry.done +} diff --git a/pkg/channels/tool_feedback_animator_test.go b/pkg/channels/tool_feedback_animator_test.go new file mode 100644 index 000000000..a23284548 --- /dev/null +++ b/pkg/channels/tool_feedback_animator_test.go @@ -0,0 +1,121 @@ +package channels + +import ( + "context" + "errors" + "testing" +) + +func TestFormatAnimatedToolFeedbackContent(t *testing.T) { + got := formatAnimatedToolFeedbackContent("🔧 `read_file`\nReading config file", "running..") + want := "🔧 `read_filerunning..`\nReading config file" + if got != want { + t.Fatalf("formatAnimatedToolFeedbackContent() = %q, want %q", got, want) + } +} + +func TestInitialAnimatedToolFeedbackContent(t *testing.T) { + got := InitialAnimatedToolFeedbackContent("🔧 `exec`\nRunning command") + want := "🔧 `exec`\nRunning command" + if got != want { + t.Fatalf("InitialAnimatedToolFeedbackContent() = %q, want %q", got, want) + } +} + +func TestFormatAnimatedToolFeedbackContent_WithoutCodeSpan(t *testing.T) { + got := formatAnimatedToolFeedbackContent("hello", "running..") + want := "hellorunning.." + if got != want { + t.Fatalf("formatAnimatedToolFeedbackContent() without code span = %q, want %q", got, want) + } +} + +func TestToolFeedbackAnimator_RecordCurrentAndClear(t *testing.T) { + animator := NewToolFeedbackAnimator(nil) + animator.Record("chat-1", "msg-1", "🔧 `read_file`") + + msgID, ok := animator.Current("chat-1") + if !ok || msgID != "msg-1" { + t.Fatalf("Current() = (%q, %v), want (msg-1, true)", msgID, ok) + } + + animator.Clear("chat-1") + + msgID, ok = animator.Current("chat-1") + if ok || msgID != "" { + t.Fatalf("Current() after Clear = (%q, %v), want (\"\", false)", msgID, ok) + } +} + +func TestToolFeedbackAnimator_TakeStopsTrackingAndReturnsState(t *testing.T) { + animator := NewToolFeedbackAnimator(nil) + animator.Record("chat-1", "msg-1", "🔧 `read_file`\nChecking config") + + msgID, baseContent, ok := animator.Take("chat-1") + if !ok { + t.Fatal("Take() = not found, want tracked message") + } + if msgID != "msg-1" { + t.Fatalf("Take() msgID = %q, want msg-1", msgID) + } + if baseContent != "🔧 `read_file`\nChecking config" { + t.Fatalf("Take() baseContent = %q", baseContent) + } + if _, ok := animator.Current("chat-1"); ok { + t.Fatal("expected tracked message to be removed after Take()") + } +} + +func TestToolFeedbackAnimator_UpdateStopsTrackingBeforeEdit(t *testing.T) { + var animator *ToolFeedbackAnimator + animator = NewToolFeedbackAnimator(func(_ context.Context, chatID, messageID, content string) error { + if _, ok := animator.Current(chatID); ok { + t.Fatal("expected tracked tool feedback to be stopped before edit") + } + if messageID != "msg-1" { + t.Fatalf("messageID = %q, want msg-1", messageID) + } + if content != "🔧 `write_file`\nUpdating config" { + t.Fatalf("content = %q, want updated animated content", content) + } + return nil + }) + defer animator.StopAll() + + animator.Record("chat-1", "msg-1", "🔧 `read_file`\nChecking config") + + msgID, handled, err := animator.Update(context.Background(), "chat-1", "🔧 `write_file`\nUpdating config") + if err != nil { + t.Fatalf("Update() error = %v", err) + } + if !handled { + t.Fatal("Update() handled = false, want true") + } + if msgID != "msg-1" { + t.Fatalf("Update() msgID = %q, want msg-1", msgID) + } +} + +func TestToolFeedbackAnimator_UpdateFailureRestoresTracking(t *testing.T) { + editErr := errors.New("edit failed") + animator := NewToolFeedbackAnimator(func(context.Context, string, string, string) error { + return editErr + }) + defer animator.StopAll() + + animator.Record("chat-1", "msg-1", "🔧 `read_file`\nChecking config") + + msgID, handled, err := animator.Update(context.Background(), "chat-1", "🔧 `write_file`\nUpdating config") + if !handled { + t.Fatal("Update() handled = false, want true") + } + if !errors.Is(err, editErr) { + t.Fatalf("Update() error = %v, want editErr", err) + } + if msgID != "" { + t.Fatalf("Update() msgID = %q, want empty on failed edit", msgID) + } + if currentID, ok := animator.Current("chat-1"); !ok || currentID != "msg-1" { + t.Fatalf("Current() after failed Update = (%q, %v), want (msg-1, true)", currentID, ok) + } +} diff --git a/pkg/config/config.go b/pkg/config/config.go index 5bc96fb12..547060bd6 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -286,7 +286,7 @@ func (d *AgentDefaults) GetMaxMediaSize() int { return DefaultMaxMediaSize } -// GetToolFeedbackMaxArgsLength returns the max args preview length for tool feedback messages. +// GetToolFeedbackMaxArgsLength returns the max visible text length for tool feedback messages. func (d *AgentDefaults) GetToolFeedbackMaxArgsLength() int { if d.ToolFeedback.MaxArgsLength > 0 { return d.ToolFeedback.MaxArgsLength diff --git a/pkg/providers/cli/toolcall_utils.go b/pkg/providers/cli/toolcall_utils.go index b480082eb..1f58c9a26 100644 --- a/pkg/providers/cli/toolcall_utils.go +++ b/pkg/providers/cli/toolcall_utils.go @@ -55,6 +55,12 @@ func buildCLIToolsPrompt(tools []ToolDefinition) string { func NormalizeToolCall(tc ToolCall) ToolCall { normalized := tc + if normalized.ThoughtSignature == "" && + normalized.ExtraContent != nil && + normalized.ExtraContent.Google != nil { + normalized.ThoughtSignature = normalized.ExtraContent.Google.ThoughtSignature + } + // Ensure Name is populated from Function if not set if normalized.Name == "" && normalized.Function != nil { normalized.Name = normalized.Function.Name @@ -77,8 +83,9 @@ func NormalizeToolCall(tc ToolCall) ToolCall { argsJSON, _ := json.Marshal(normalized.Arguments) if normalized.Function == nil { normalized.Function = &FunctionCall{ - Name: normalized.Name, - Arguments: string(argsJSON), + Name: normalized.Name, + Arguments: string(argsJSON), + ThoughtSignature: normalized.ThoughtSignature, } } else { if normalized.Function.Name == "" { @@ -90,6 +97,12 @@ func NormalizeToolCall(tc ToolCall) ToolCall { if normalized.Function.Arguments == "" { normalized.Function.Arguments = string(argsJSON) } + if normalized.Function.ThoughtSignature == "" { + normalized.Function.ThoughtSignature = normalized.ThoughtSignature + } + if normalized.ThoughtSignature == "" { + normalized.ThoughtSignature = normalized.Function.ThoughtSignature + } } return normalized diff --git a/pkg/providers/common/common.go b/pkg/providers/common/common.go index 90142fb8b..c167b1ffd 100644 --- a/pkg/providers/common/common.go +++ b/pkg/providers/common/common.go @@ -70,11 +70,23 @@ func NewHTTPClient(proxy string) *http.Client { // It mirrors protocoltypes.Message but omits SystemParts, which is an // internal field that would be unknown to third-party endpoints. type openaiMessage struct { - Role string `json:"role"` - Content string `json:"content"` - ReasoningContent string `json:"reasoning_content,omitempty"` - ToolCalls []ToolCall `json:"tool_calls,omitempty"` - ToolCallID string `json:"tool_call_id,omitempty"` + Role string `json:"role"` + Content string `json:"content"` + ReasoningContent string `json:"reasoning_content,omitempty"` + ToolCalls []openaiToolCall `json:"tool_calls,omitempty"` + ToolCallID string `json:"tool_call_id,omitempty"` +} + +type openaiToolCall struct { + ID string `json:"id"` + Type string `json:"type,omitempty"` + Function *openaiFunctionCall `json:"function,omitempty"` +} + +type openaiFunctionCall struct { + Name string `json:"name"` + Arguments string `json:"arguments"` + ThoughtSignature string `json:"thought_signature,omitempty"` } // SerializeMessages converts internal Message structs to the OpenAI wire format. @@ -84,12 +96,13 @@ type openaiMessage struct { func SerializeMessages(messages []Message) []any { out := make([]any, 0, len(messages)) for _, m := range messages { + toolCalls := serializeToolCalls(m.ToolCalls) if len(m.Media) == 0 { out = append(out, openaiMessage{ Role: m.Role, Content: m.Content, ReasoningContent: m.ReasoningContent, - ToolCalls: m.ToolCalls, + ToolCalls: toolCalls, ToolCallID: m.ToolCallID, }) continue @@ -132,8 +145,8 @@ func SerializeMessages(messages []Message) []any { if m.ToolCallID != "" { msg["tool_call_id"] = m.ToolCallID } - if len(m.ToolCalls) > 0 { - msg["tool_calls"] = m.ToolCalls + if len(toolCalls) > 0 { + msg["tool_calls"] = toolCalls } if m.ReasoningContent != "" { msg["reasoning_content"] = m.ReasoningContent @@ -143,6 +156,55 @@ func SerializeMessages(messages []Message) []any { return out } +func serializeToolCalls(toolCalls []ToolCall) []openaiToolCall { + if len(toolCalls) == 0 { + return nil + } + + out := make([]openaiToolCall, 0, len(toolCalls)) + for _, tc := range toolCalls { + wireCall := openaiToolCall{ + ID: tc.ID, + Type: tc.Type, + } + + if tc.Function != nil { + thoughtSignature := tc.Function.ThoughtSignature + if thoughtSignature == "" { + thoughtSignature = tc.ThoughtSignature + } + if thoughtSignature == "" && tc.ExtraContent != nil && tc.ExtraContent.Google != nil { + thoughtSignature = tc.ExtraContent.Google.ThoughtSignature + } + wireCall.Function = &openaiFunctionCall{ + Name: tc.Function.Name, + Arguments: tc.Function.Arguments, + ThoughtSignature: thoughtSignature, + } + } else if tc.Name != "" || len(tc.Arguments) > 0 || tc.ThoughtSignature != "" { + thoughtSignature := tc.ThoughtSignature + if thoughtSignature == "" && tc.ExtraContent != nil && tc.ExtraContent.Google != nil { + thoughtSignature = tc.ExtraContent.Google.ThoughtSignature + } + argsJSON := "{}" + if len(tc.Arguments) > 0 { + if encoded, err := json.Marshal(tc.Arguments); err == nil { + argsJSON = string(encoded) + } + } + wireCall.Function = &openaiFunctionCall{ + Name: tc.Name, + Arguments: argsJSON, + ThoughtSignature: thoughtSignature, + } + } + + out = append(out, wireCall) + } + + return out +} + func parseDataAudioURL(mediaURL string) (format, data string, ok bool) { if !strings.HasPrefix(mediaURL, "data:audio/") { return "", "", false @@ -185,6 +247,7 @@ func ParseResponse(body io.Reader) (*LLMResponse, error) { Google *struct { ThoughtSignature string `json:"thought_signature"` } `json:"google"` + ToolFeedbackExplanation string `json:"tool_feedback_explanation"` } `json:"extra_content"` } `json:"tool_calls"` } `json:"message"` @@ -228,11 +291,17 @@ func ParseResponse(body io.Reader) (*LLMResponse, error) { ThoughtSignature: thoughtSignature, } - if thoughtSignature != "" { - toolCall.ExtraContent = &ExtraContent{ - Google: &GoogleExtra{ + if tc.ExtraContent != nil { + extraContent := &ExtraContent{ + ToolFeedbackExplanation: tc.ExtraContent.ToolFeedbackExplanation, + } + if thoughtSignature != "" { + extraContent.Google = &GoogleExtra{ ThoughtSignature: thoughtSignature, - }, + } + } + if extraContent.Google != nil || strings.TrimSpace(extraContent.ToolFeedbackExplanation) != "" { + toolCall.ExtraContent = extraContent } } diff --git a/pkg/providers/common/common_test.go b/pkg/providers/common/common_test.go index c107bb665..affb91e6f 100644 --- a/pkg/providers/common/common_test.go +++ b/pkg/providers/common/common_test.go @@ -162,6 +162,104 @@ func TestSerializeMessages_StripsSystemParts(t *testing.T) { } } +func TestSerializeMessages_StripsInternalToolCallExtraContent(t *testing.T) { + messages := []Message{ + { + Role: "assistant", + ToolCalls: []ToolCall{{ + ID: "call_1", + Type: "function", + Function: &FunctionCall{ + Name: "read_file", + Arguments: `{"path":"README.md"}`, + ThoughtSignature: "sig-1", + }, + ExtraContent: &ExtraContent{ + Google: &GoogleExtra{ + ThoughtSignature: "sig-ignored-here", + }, + ToolFeedbackExplanation: "Read README.md first.", + }, + }}, + }, + } + + result := SerializeMessages(messages) + + data, err := json.Marshal(result) + if err != nil { + t.Fatalf("json.Marshal() error = %v", err) + } + payload := string(data) + if strings.Contains(payload, "extra_content") { + t.Fatalf("serialized payload should not include internal extra_content: %s", payload) + } + if !strings.Contains(payload, "thought_signature") { + t.Fatalf("serialized payload should preserve function thought_signature: %s", payload) + } +} + +func TestSerializeMessages_PreservesTopLevelThoughtSignature(t *testing.T) { + messages := []Message{ + { + Role: "assistant", + ToolCalls: []ToolCall{{ + ID: "call_1", + Type: "function", + ThoughtSignature: "sig-1", + Function: &FunctionCall{ + Name: "read_file", + Arguments: `{"path":"README.md"}`, + }, + }}, + }, + } + + result := SerializeMessages(messages) + + data, err := json.Marshal(result) + if err != nil { + t.Fatalf("json.Marshal() error = %v", err) + } + payload := string(data) + if !strings.Contains(payload, `"thought_signature":"sig-1"`) { + t.Fatalf("serialized payload should preserve top-level thought signature: %s", payload) + } +} + +func TestSerializeMessages_PreservesGoogleExtraThoughtSignature(t *testing.T) { + messages := []Message{ + { + Role: "assistant", + ToolCalls: []ToolCall{{ + ID: "call_1", + Type: "function", + Function: &FunctionCall{ + Name: "read_file", + Arguments: `{"path":"README.md"}`, + }, + ExtraContent: &ExtraContent{ + Google: &GoogleExtra{ThoughtSignature: "sig-1"}, + }, + }}, + }, + } + + result := SerializeMessages(messages) + + data, err := json.Marshal(result) + if err != nil { + t.Fatalf("json.Marshal() error = %v", err) + } + payload := string(data) + if strings.Contains(payload, "extra_content") { + t.Fatalf("serialized payload should not include extra_content: %s", payload) + } + if !strings.Contains(payload, `"thought_signature":"sig-1"`) { + t.Fatalf("serialized payload should preserve google thought signature: %s", payload) + } +} + // --- ParseResponse tests --- func TestParseResponse_BasicContent(t *testing.T) { @@ -234,6 +332,27 @@ func TestParseResponse_WithReasoningContent(t *testing.T) { } } +func TestParseResponse_WithToolFeedbackExplanationExtraContent(t *testing.T) { + body := `{"choices":[{"message":{"content":"","tool_calls":[{"id":"call_1","type":"function","function":{"name":"test_tool","arguments":"{}"},"extra_content":{"tool_feedback_explanation":"Check the current config before editing."}}]},"finish_reason":"tool_calls"}]}` + out, err := ParseResponse(strings.NewReader(body)) + if err != nil { + t.Fatalf("ParseResponse() error = %v", err) + } + if len(out.ToolCalls) != 1 { + t.Fatalf("len(ToolCalls) = %d, want 1", len(out.ToolCalls)) + } + if out.ToolCalls[0].ExtraContent == nil { + t.Fatal("ExtraContent is nil") + } + if out.ToolCalls[0].ExtraContent.ToolFeedbackExplanation != "Check the current config before editing." { + t.Fatalf( + "ToolFeedbackExplanation = %q, want %q", + out.ToolCalls[0].ExtraContent.ToolFeedbackExplanation, + "Check the current config before editing.", + ) + } +} + func TestParseResponse_InvalidJSON(t *testing.T) { _, err := ParseResponse(strings.NewReader("not json")) if err == nil { diff --git a/pkg/providers/protocoltypes/types.go b/pkg/providers/protocoltypes/types.go index 194c1aa6f..1189577f1 100644 --- a/pkg/providers/protocoltypes/types.go +++ b/pkg/providers/protocoltypes/types.go @@ -11,7 +11,8 @@ type ToolCall struct { } type ExtraContent struct { - Google *GoogleExtra `json:"google,omitempty"` + Google *GoogleExtra `json:"google,omitempty"` + ToolFeedbackExplanation string `json:"tool_feedback_explanation,omitempty"` } type GoogleExtra struct { diff --git a/pkg/providers/toolcall_utils_test.go b/pkg/providers/toolcall_utils_test.go new file mode 100644 index 000000000..a4bb03c2e --- /dev/null +++ b/pkg/providers/toolcall_utils_test.go @@ -0,0 +1,24 @@ +package providers + +import "testing" + +func TestNormalizeToolCall_PreservesExtraContentGoogleThoughtSignature(t *testing.T) { + tc := NormalizeToolCall(ToolCall{ + ID: "call_1", + Name: "search", + Arguments: map[string]any{"q": "pico"}, + ExtraContent: &ExtraContent{ + Google: &GoogleExtra{ThoughtSignature: "sig-1"}, + }, + }) + + if tc.ThoughtSignature != "sig-1" { + t.Fatalf("ThoughtSignature = %q, want sig-1", tc.ThoughtSignature) + } + if tc.Function == nil { + t.Fatal("Function is nil") + } + if tc.Function.ThoughtSignature != "sig-1" { + t.Fatalf("Function.ThoughtSignature = %q, want sig-1", tc.Function.ThoughtSignature) + } +} diff --git a/pkg/utils/tool_feedback.go b/pkg/utils/tool_feedback.go index a6c8895b8..1a8b6c747 100644 --- a/pkg/utils/tool_feedback.go +++ b/pkg/utils/tool_feedback.go @@ -1,9 +1,57 @@ package utils -import "fmt" +import ( + "fmt" + "strings" +) -// FormatToolFeedbackMessage renders the tool name and arguments preview in the -// same markdown shape used by live tool feedback and session reconstruction. -func FormatToolFeedbackMessage(toolName, argsPreview string) string { - return fmt.Sprintf("\U0001f527 `%s`\n```\n%s\n```", toolName, argsPreview) +const ToolFeedbackContinuationHint = "Continuing the current task." + +// FormatToolFeedbackMessage renders the model-provided explanation for why a +// tool is being executed. When the model does not provide one, it keeps only +// the tool line and does not expose raw arguments or fallback text. +func FormatToolFeedbackMessage(toolName, explanation string) string { + toolName = strings.TrimSpace(toolName) + explanation = strings.TrimSpace(explanation) + + if toolName == "" { + return explanation + } + if explanation == "" { + return fmt.Sprintf("\U0001f527 `%s`", toolName) + } + + return fmt.Sprintf("\U0001f527 `%s`\n%s", toolName, explanation) +} + +// FitToolFeedbackMessage keeps tool feedback within a single outbound message. +// It preserves the first line when possible and truncates the explanation body +// instead of letting the message be split into multiple chunks. +func FitToolFeedbackMessage(content string, maxLen int) string { + content = strings.TrimSpace(content) + if content == "" || maxLen <= 0 { + return "" + } + if len([]rune(content)) <= maxLen { + return content + } + + firstLine, rest, hasRest := strings.Cut(content, "\n") + firstLine = strings.TrimSpace(firstLine) + rest = strings.TrimSpace(rest) + + if !hasRest || rest == "" { + return Truncate(firstLine, maxLen) + } + + if len([]rune(firstLine)) >= maxLen { + return Truncate(firstLine, maxLen) + } + + remaining := maxLen - len([]rune(firstLine)) - 1 + if remaining <= 0 { + return Truncate(firstLine, maxLen) + } + + return firstLine + "\n" + Truncate(rest, remaining) } diff --git a/pkg/utils/tool_feedback_test.go b/pkg/utils/tool_feedback_test.go index d7a55ce6b..316ce2408 100644 --- a/pkg/utils/tool_feedback_test.go +++ b/pkg/utils/tool_feedback_test.go @@ -3,9 +3,47 @@ package utils import "testing" func TestFormatToolFeedbackMessage(t *testing.T) { - got := FormatToolFeedbackMessage("read_file", "{\"path\":\"README.md\"}") - want := "\U0001f527 `read_file`\n```\n{\"path\":\"README.md\"}\n```" + got := FormatToolFeedbackMessage( + "read_file", + "I will read README.md first to confirm the current project structure.", + ) + want := "\U0001f527 `read_file`\nI will read README.md first to confirm the current project structure." if got != want { t.Fatalf("FormatToolFeedbackMessage() = %q, want %q", got, want) } } + +func TestFormatToolFeedbackMessage_EmptyExplanationKeepsOnlyToolLine(t *testing.T) { + got := FormatToolFeedbackMessage("read_file", "") + want := "\U0001f527 `read_file`" + if got != want { + t.Fatalf("FormatToolFeedbackMessage() = %q, want %q", got, want) + } +} + +func TestFormatToolFeedbackMessage_EmptyToolNameOmitsToolLine(t *testing.T) { + got := FormatToolFeedbackMessage("", "Continue drafting the final response.") + want := "Continue drafting the final response." + if got != want { + t.Fatalf("FormatToolFeedbackMessage() = %q, want %q", got, want) + } +} + +func TestFitToolFeedbackMessage_TruncatesBodyWithinSingleMessage(t *testing.T) { + got := FitToolFeedbackMessage( + "\U0001f527 `read_file`\nRead README.md first to confirm the current project structure.", + 40, + ) + want := "\U0001f527 `read_file`\nRead README.md first to..." + if got != want { + t.Fatalf("FitToolFeedbackMessage() = %q, want %q", got, want) + } +} + +func TestFitToolFeedbackMessage_TruncatesSingleLineMessage(t *testing.T) { + got := FitToolFeedbackMessage("\U0001f527 `read_file`", 10) + want := "\U0001f527 `read..." + if got != want { + t.Fatalf("FitToolFeedbackMessage() = %q, want %q", got, want) + } +} diff --git a/web/backend/api/session.go b/web/backend/api/session.go index 054b78b73..2a16fe183 100644 --- a/web/backend/api/session.go +++ b/web/backend/api/session.go @@ -486,6 +486,15 @@ func visibleSessionMessages(messages []providers.Message, toolFeedbackMaxArgsLen transcript = append(transcript, visibleToolMessages...) } + // When assistant content exactly matches the rendered tool summary or + // tool-delivered message, skip it to avoid duplicates. Distinct content + // must remain visible in restored session history. + if len(msg.ToolCalls) > 0 && + len(msg.Media) == 0 && + assistantToolCallContentDuplicated(msg.Content, toolSummaryMessages, visibleToolMessages) { + continue + } + // Pico web chat can persist both visible `message` tool output and a // later plain assistant reply in the same turn. Hide only the fixed // internal summary that marks handled tool delivery. @@ -504,6 +513,43 @@ func visibleSessionMessages(messages []providers.Message, toolFeedbackMaxArgsLen return transcript } +func assistantToolCallContentDuplicated( + content string, + toolSummaryMessages []sessionChatMessage, + visibleToolMessages []sessionChatMessage, +) bool { + content = strings.TrimSpace(content) + if content == "" { + return false + } + + for _, msg := range toolSummaryMessages { + if toolSummaryContainsContent(msg.Content, content) { + return true + } + } + for _, msg := range visibleToolMessages { + if strings.TrimSpace(msg.Content) == content { + return true + } + } + return false +} + +func toolSummaryContainsContent(summary, content string) bool { + summary = strings.TrimSpace(summary) + content = strings.TrimSpace(content) + if summary == "" || content == "" { + return false + } + if summary == content { + return true + } + + _, body, hasBody := strings.Cut(summary, "\n") + return hasBody && strings.TrimSpace(body) == content +} + func assistantMessageTransientThought(msg providers.Message) bool { return strings.TrimSpace(msg.Content) == "" && strings.TrimSpace(msg.ReasoningContent) != "" && @@ -529,38 +575,51 @@ func visibleAssistantToolSummaryMessages( messages := make([]sessionChatMessage, 0, len(toolCalls)) for _, tc := range toolCalls { name := tc.Name - argsJSON := "" if tc.Function != nil { if name == "" { name = tc.Function.Name } - argsJSON = tc.Function.Arguments } if strings.TrimSpace(name) == "" { continue } - if strings.TrimSpace(argsJSON) == "" && len(tc.Arguments) > 0 { - if encodedArgs, err := json.Marshal(tc.Arguments); err == nil { - argsJSON = string(encodedArgs) - } - } - - argsPreview := strings.TrimSpace(argsJSON) - if argsPreview == "" { - argsPreview = "{}" - } - messages = append(messages, sessionChatMessage{ - Role: "assistant", - Content: utils.FormatToolFeedbackMessage(name, utils.Truncate(argsPreview, toolFeedbackMaxArgsLength)), + Role: "assistant", + Content: utils.FormatToolFeedbackMessage( + name, + visibleAssistantToolSummaryText(tc, toolFeedbackMaxArgsLength), + ), }) } return messages } +func visibleAssistantToolSummaryText( + tc providers.ToolCall, + toolFeedbackMaxArgsLength int, +) string { + if tc.ExtraContent != nil { + if explanation := strings.TrimSpace(tc.ExtraContent.ToolFeedbackExplanation); explanation != "" { + return utils.Truncate(explanation, toolFeedbackMaxArgsLength) + } + } + + argsJSON := "" + if tc.Function != nil { + argsJSON = tc.Function.Arguments + } + if strings.TrimSpace(argsJSON) == "" && len(tc.Arguments) > 0 { + if encodedArgs, err := json.Marshal(tc.Arguments); err == nil { + argsJSON = string(encodedArgs) + } + } + + return utils.Truncate(strings.TrimSpace(argsJSON), toolFeedbackMaxArgsLength) +} + func visibleAssistantToolMessages(toolCalls []providers.ToolCall) []sessionChatMessage { if len(toolCalls) == 0 { return nil diff --git a/web/backend/api/session_test.go b/web/backend/api/session_test.go index e40a8c77c..b0bab0baa 100644 --- a/web/backend/api/session_test.go +++ b/web/backend/api/session_test.go @@ -540,7 +540,7 @@ func TestHandleListSessions_MessageCountUsesVisibleTranscript(t *testing.T) { } } -func TestHandleGetSession_PreservesToolSummaryAndAssistantContent(t *testing.T) { +func TestHandleGetSession_DoesNotDuplicateAssistantToolCallContent(t *testing.T) { configPath, cleanup := setupOAuthTestEnv(t) defer cleanup() @@ -555,7 +555,7 @@ func TestHandleGetSession_PreservesToolSummaryAndAssistantContent(t *testing.T) {Role: "user", Content: "check file"}, { Role: "assistant", - Content: "model final reply", + Content: "Read the file before replying.", ToolCalls: []providers.ToolCall{ { ID: "call_1", @@ -564,6 +564,9 @@ func TestHandleGetSession_PreservesToolSummaryAndAssistantContent(t *testing.T) Name: "read_file", Arguments: `{"path":"README.md","start_line":1,"end_line":10}`, }, + ExtraContent: &providers.ExtraContent{ + ToolFeedbackExplanation: "Read the file before replying.", + }, }, }, }, @@ -594,8 +597,8 @@ func TestHandleGetSession_PreservesToolSummaryAndAssistantContent(t *testing.T) if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil { t.Fatalf("Unmarshal() error = %v", err) } - if len(resp.Messages) != 3 { - t.Fatalf("len(resp.Messages) = %d, want 3", len(resp.Messages)) + if len(resp.Messages) != 2 { + t.Fatalf("len(resp.Messages) = %d, want 2", len(resp.Messages)) } if resp.Messages[0].Role != "user" || resp.Messages[0].Content != "check file" { t.Fatalf("first message = %#v, want user/check file", resp.Messages[0]) @@ -603,8 +606,153 @@ func TestHandleGetSession_PreservesToolSummaryAndAssistantContent(t *testing.T) if !strings.Contains(resp.Messages[1].Content, "`read_file`") { t.Fatalf("tool summary message = %#v, want read_file summary", resp.Messages[1]) } - if resp.Messages[2].Role != "assistant" || resp.Messages[2].Content != "model final reply" { - t.Fatalf("assistant message = %#v, want model final reply", resp.Messages[2]) + if !strings.Contains(resp.Messages[1].Content, "Read the file before replying.") { + t.Fatalf("tool summary message = %#v, want tool explanation", resp.Messages[1]) + } +} + +func TestHandleGetSession_PreservesDistinctAssistantToolCallContent(t *testing.T) { + configPath, cleanup := setupOAuthTestEnv(t) + defer cleanup() + + dir := sessionsTestDir(t, configPath) + store, err := memory.NewJSONLStore(dir) + if err != nil { + t.Fatalf("NewJSONLStore() error = %v", err) + } + + sessionKey := picoSessionPrefix + "detail-tool-summary-distinct-content" + for _, msg := range []providers.Message{ + {Role: "user", Content: "check file"}, + { + Role: "assistant", + Content: "I will summarize the findings after reading the file.", + ToolCalls: []providers.ToolCall{ + { + ID: "call_1", + Type: "function", + Function: &providers.FunctionCall{ + Name: "read_file", + Arguments: `{"path":"README.md","start_line":1,"end_line":10}`, + }, + ExtraContent: &providers.ExtraContent{ + ToolFeedbackExplanation: "Read the file before replying.", + }, + }, + }, + }, + } { + if err := store.AddFullMessage(nil, sessionKey, msg); err != nil { + t.Fatalf("AddFullMessage() error = %v", err) + } + } + + h := NewHandler(configPath) + mux := http.NewServeMux() + h.RegisterRoutes(mux) + + rec := httptest.NewRecorder() + req := httptest.NewRequest(http.MethodGet, "/api/sessions/detail-tool-summary-distinct-content", nil) + mux.ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("status = %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String()) + } + + var resp struct { + Messages []struct { + Role string `json:"role"` + Content string `json:"content"` + } `json:"messages"` + } + if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil { + t.Fatalf("Unmarshal() error = %v", err) + } + if len(resp.Messages) != 3 { + t.Fatalf("len(resp.Messages) = %d, want 3", len(resp.Messages)) + } + if !strings.Contains(resp.Messages[1].Content, "`read_file`") { + t.Fatalf("tool summary message = %#v, want read_file summary", resp.Messages[1]) + } + if resp.Messages[2].Role != "assistant" || + resp.Messages[2].Content != "I will summarize the findings after reading the file." { + t.Fatalf("assistant content = %#v, want preserved distinct content", resp.Messages[2]) + } +} + +func TestHandleGetSession_PreservesMediaWhenAssistantToolCallContentDuplicatesSummary(t *testing.T) { + configPath, cleanup := setupOAuthTestEnv(t) + defer cleanup() + + dir := sessionsTestDir(t, configPath) + store, err := memory.NewJSONLStore(dir) + if err != nil { + t.Fatalf("NewJSONLStore() error = %v", err) + } + + sessionKey := picoSessionPrefix + "detail-tool-summary-duplicate-content-with-media" + for _, msg := range []providers.Message{ + {Role: "user", Content: "check screenshot"}, + { + Role: "assistant", + Content: "Reviewing the generated screenshot.", + Media: []string{"data:image/png;base64,abc123"}, + ToolCalls: []providers.ToolCall{ + { + ID: "call_1", + Type: "function", + Function: &providers.FunctionCall{ + Name: "view_image", + Arguments: `{"path":"artifact.png"}`, + }, + ExtraContent: &providers.ExtraContent{ + ToolFeedbackExplanation: "Reviewing the generated screenshot.", + }, + }, + }, + }, + } { + if err := store.AddFullMessage(nil, sessionKey, msg); err != nil { + t.Fatalf("AddFullMessage() error = %v", err) + } + } + + h := NewHandler(configPath) + mux := http.NewServeMux() + h.RegisterRoutes(mux) + + rec := httptest.NewRecorder() + req := httptest.NewRequest(http.MethodGet, "/api/sessions/detail-tool-summary-duplicate-content-with-media", nil) + mux.ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("status = %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String()) + } + + var resp struct { + Messages []struct { + Role string `json:"role"` + Content string `json:"content"` + Media []string `json:"media"` + } `json:"messages"` + } + if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil { + t.Fatalf("Unmarshal() error = %v", err) + } + if len(resp.Messages) != 3 { + t.Fatalf("len(resp.Messages) = %d, want 3", len(resp.Messages)) + } + if !strings.Contains(resp.Messages[1].Content, "`view_image`") { + t.Fatalf("tool summary message = %#v, want view_image summary", resp.Messages[1]) + } + if resp.Messages[2].Role != "assistant" { + t.Fatalf("assistant message role = %q, want assistant", resp.Messages[2].Role) + } + if resp.Messages[2].Content != "Reviewing the generated screenshot." { + t.Fatalf("assistant content = %q, want preserved duplicated content with media", resp.Messages[2].Content) + } + if len(resp.Messages[2].Media) != 1 || resp.Messages[2].Media[0] != "data:image/png;base64,abc123" { + t.Fatalf("assistant media = %#v, want preserved media", resp.Messages[2].Media) } } @@ -629,6 +777,7 @@ func TestHandleGetSession_UsesConfiguredToolFeedbackMaxArgsLength(t *testing.T) } argsJSON := `{"path":"README.md","start_line":1,"end_line":10,"extra":"abcdefghijklmnopqrstuvwxyz"}` + explanation := "Read README.md first to confirm the current project structure before editing the config example." sessionKey := picoSessionPrefix + "detail-tool-summary-max-args" err = store.AddFullMessage(nil, sessionKey, providers.Message{Role: "user", Content: "check file"}) if err != nil { @@ -643,6 +792,9 @@ func TestHandleGetSession_UsesConfiguredToolFeedbackMaxArgsLength(t *testing.T) Name: "read_file", Arguments: argsJSON, }, + ExtraContent: &providers.ExtraContent{ + ToolFeedbackExplanation: explanation, + }, }}, }) if err != nil { @@ -675,13 +827,93 @@ func TestHandleGetSession_UsesConfiguredToolFeedbackMaxArgsLength(t *testing.T) t.Fatalf("len(resp.Messages) = %d, want at least 2", len(resp.Messages)) } - wantPreview := utils.Truncate(argsJSON, 20) + wantPreview := utils.Truncate(explanation, 20) if !strings.Contains(resp.Messages[1].Content, wantPreview) { t.Fatalf("tool summary = %q, want preview %q", resp.Messages[1].Content, wantPreview) } if strings.Contains(resp.Messages[1].Content, argsJSON) { t.Fatalf("tool summary = %q, expected configured truncation", resp.Messages[1].Content) } + if !strings.Contains(resp.Messages[1].Content, "`read_file`") { + t.Fatalf("tool summary = %q, want read_file summary", resp.Messages[1].Content) + } +} + +func TestHandleGetSession_FallsBackToLegacyToolArgumentsWhenExplanationMissing(t *testing.T) { + configPath, cleanup := setupOAuthTestEnv(t) + defer cleanup() + + cfg, err := config.LoadConfig(configPath) + if err != nil { + t.Fatalf("LoadConfig() error = %v", err) + } + cfg.Agents.Defaults.ToolFeedback.MaxArgsLength = 20 + err = config.SaveConfig(configPath, cfg) + if err != nil { + t.Fatalf("SaveConfig() error = %v", err) + } + + dir := sessionsTestDir(t, configPath) + store, err := memory.NewJSONLStore(dir) + if err != nil { + t.Fatalf("NewJSONLStore() error = %v", err) + } + + argsJSON := `{"path":"README.md","start_line":1,"end_line":10,"extra":"abcdefghijklmnopqrstuvwxyz"}` + sessionKey := picoSessionPrefix + "detail-tool-summary-legacy-args" + if err := store.AddFullMessage( + nil, + sessionKey, + providers.Message{Role: "user", Content: "check file"}, + ); err != nil { + t.Fatalf("AddFullMessage(user) error = %v", err) + } + if err := store.AddFullMessage(nil, sessionKey, providers.Message{ + Role: "assistant", + ToolCalls: []providers.ToolCall{{ + ID: "call_1", + Type: "function", + Function: &providers.FunctionCall{ + Name: "read_file", + Arguments: argsJSON, + }, + }}, + }); err != nil { + t.Fatalf("AddFullMessage(assistant) error = %v", err) + } + + h := NewHandler(configPath) + mux := http.NewServeMux() + h.RegisterRoutes(mux) + + rec := httptest.NewRecorder() + req := httptest.NewRequest(http.MethodGet, "/api/sessions/detail-tool-summary-legacy-args", nil) + mux.ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("status = %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String()) + } + + var resp struct { + Messages []struct { + Role string `json:"role"` + Content string `json:"content"` + } `json:"messages"` + } + if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil { + t.Fatalf("Unmarshal() error = %v", err) + } + if len(resp.Messages) < 2 { + t.Fatalf("len(resp.Messages) = %d, want at least 2", len(resp.Messages)) + } + + wantPreview := utils.Truncate(argsJSON, 20) + if !strings.Contains(resp.Messages[1].Content, "`read_file`") { + t.Fatalf("tool summary = %q, want read_file summary", resp.Messages[1].Content) + } + if !strings.Contains(resp.Messages[1].Content, wantPreview) { + t.Fatalf("tool summary = %q, want legacy args preview %q", resp.Messages[1].Content, wantPreview) + } } func TestHandleGetSession_IncludesMediaOnlyMessages(t *testing.T) { diff --git a/web/frontend/src/i18n/locales/en.json b/web/frontend/src/i18n/locales/en.json index c96d4b71b..7a5c58b30 100644 --- a/web/frontend/src/i18n/locales/en.json +++ b/web/frontend/src/i18n/locales/en.json @@ -592,9 +592,9 @@ "split_on_marker": "Chatty Mode", "split_on_marker_hint": "Split long messages into short ones like real human chatting.", "tool_feedback_enabled": "Tool Feedback", - "tool_feedback_enabled_hint": "Send a short tool-call preview into the current chat before each tool execution.", - "tool_feedback_max_args_length": "Tool Feedback Args Preview Length", - "tool_feedback_max_args_length_hint": "Maximum number of argument characters shown in each tool feedback message. Set to 0 to use the default.", + "tool_feedback_enabled_hint": "Send a short execution note into the current chat before each tool runs.", + "tool_feedback_max_args_length": "Tool Feedback Length", + "tool_feedback_max_args_length_hint": "Maximum number of characters shown in each tool feedback message. Set to 0 to use the default.", "exec_enabled": "Allow Commands", "exec_enabled_hint": "Enable or disable command execution for the app. When disabled, no command requests will run.", "allow_remote": "Allow Remote Commands", diff --git a/web/frontend/src/i18n/locales/zh.json b/web/frontend/src/i18n/locales/zh.json index 4a9e59cf4..aaebfa625 100644 --- a/web/frontend/src/i18n/locales/zh.json +++ b/web/frontend/src/i18n/locales/zh.json @@ -592,9 +592,9 @@ "split_on_marker": "连续短消息", "split_on_marker_hint": "像真人聊天一样,把长难句拆成多条短消息快速发出", "tool_feedback_enabled": "工具反馈", - "tool_feedback_enabled_hint": "在每次执行工具前,先向当前会话发送一条简短的工具调用预览", - "tool_feedback_max_args_length": "工具反馈参数预览长度", - "tool_feedback_max_args_length_hint": "每条工具反馈消息中展示的参数字符上限。设为 0 时使用默认值", + "tool_feedback_enabled_hint": "在每次执行工具前,先向当前会话发送一条简短的执行说明", + "tool_feedback_max_args_length": "工具反馈长度", + "tool_feedback_max_args_length_hint": "每条工具反馈消息中展示的字符上限。设为 0 时使用默认值", "exec_enabled": "允许命令执行", "exec_enabled_hint": "控制应用是否允许执行命令。关闭后,所有命令请求都不会执行", "allow_remote": "允许远程命令执行", From 6421f146a99df1bebcd4b1ca8de2a289dfca3622 Mon Sep 17 00:00:00 2001 From: lxowalle <83055338+lxowalle@users.noreply.github.com> Date: Mon, 20 Apr 2026 18:30:29 +0800 Subject: [PATCH 6/8] Revert "Feat/channel tool feedback animation (#2569)" (#2596) This reverts commit e556a816e4db4c158ab3a455693018f452f63eba. --- cmd/picoclaw/internal/auth/wecom_test.go | 20 +- docs/channels/discord/README.md | 44 +- pkg/agent/loop.go | 1 - pkg/agent/loop_test.go | 366 +-------------- pkg/agent/loop_turn.go | 51 +-- pkg/agent/loop_utils.go | 93 ---- pkg/channels/discord/discord.go | 190 +------- pkg/channels/discord/discord_test.go | 245 ---------- pkg/channels/feishu/feishu_64.go | 175 +------- pkg/channels/feishu/feishu_64_test.go | 85 ---- pkg/channels/manager.go | 112 +---- pkg/channels/manager_test.go | 424 +----------------- pkg/channels/matrix/matrix.go | 133 +----- pkg/channels/matrix/matrix_test.go | 29 -- pkg/channels/pico/pico.go | 118 +---- pkg/channels/pico/pico_test.go | 28 -- pkg/channels/telegram/command_registration.go | 6 +- .../telegram/command_registration_test.go | 16 +- pkg/channels/telegram/telegram.go | 180 +------- .../telegram_group_command_filter_test.go | 2 +- pkg/channels/telegram/telegram_test.go | 102 +---- pkg/channels/tool_feedback_animator.go | 240 ---------- pkg/channels/tool_feedback_animator_test.go | 121 ----- pkg/config/config.go | 2 +- pkg/providers/cli/toolcall_utils.go | 17 +- pkg/providers/common/common.go | 93 +--- pkg/providers/common/common_test.go | 119 ----- pkg/providers/protocoltypes/types.go | 3 +- pkg/providers/toolcall_utils_test.go | 24 - pkg/utils/tool_feedback.go | 58 +-- pkg/utils/tool_feedback_test.go | 42 +- web/backend/api/session.go | 89 +--- web/backend/api/session_test.go | 246 +--------- web/frontend/src/i18n/locales/en.json | 6 +- web/frontend/src/i18n/locales/zh.json | 6 +- 35 files changed, 169 insertions(+), 3317 deletions(-) delete mode 100644 pkg/channels/tool_feedback_animator.go delete mode 100644 pkg/channels/tool_feedback_animator_test.go delete mode 100644 pkg/providers/toolcall_utils_test.go diff --git a/cmd/picoclaw/internal/auth/wecom_test.go b/cmd/picoclaw/internal/auth/wecom_test.go index aafd39e69..c152481be 100644 --- a/cmd/picoclaw/internal/auth/wecom_test.go +++ b/cmd/picoclaw/internal/auth/wecom_test.go @@ -3,7 +3,6 @@ package auth import ( "bytes" "context" - "net" "net/http" "net/http/httptest" "net/url" @@ -20,19 +19,6 @@ import ( "github.com/sipeed/picoclaw/pkg/config" ) -func newIPv4TestServer(t *testing.T, handler http.Handler) *httptest.Server { - t.Helper() - - server := httptest.NewUnstartedServer(handler) - listener, err := net.Listen("tcp4", "127.0.0.1:0") - require.NoError(t, err) - - server.Listener = listener - server.Start() - t.Cleanup(server.Close) - return server -} - func TestNewWeComCommand(t *testing.T) { cmd := newWeComCommand() @@ -67,7 +53,7 @@ func TestBuildWeComQRCodePageURL(t *testing.T) { } func TestFetchWeComQRCode(t *testing.T) { - server := newIPv4TestServer(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, "/generate", r.URL.Path) assert.Equal(t, wecomQRSourceID, r.URL.Query().Get("source")) assert.Equal(t, wecomQRSourceID, r.URL.Query().Get("sourceID")) @@ -75,6 +61,7 @@ func TestFetchWeComQRCode(t *testing.T) { w.Header().Set("Content-Type", "application/json") _, _ = w.Write([]byte(`{"data":{"scode":"scode-1","auth_url":"https://example.com/qr"}}`)) })) + defer server.Close() opts := normalizeWeComQRFlowOptions(wecomQRFlowOptions{ HTTPClient: server.Client(), @@ -91,7 +78,7 @@ func TestFetchWeComQRCode(t *testing.T) { func TestPollWeComQRCodeResult(t *testing.T) { var calls atomic.Int32 - server := newIPv4TestServer(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { call := calls.Add(1) assert.Equal(t, "/query", r.URL.Path) assert.Equal(t, "scode-1", r.URL.Query().Get("scode")) @@ -105,6 +92,7 @@ func TestPollWeComQRCodeResult(t *testing.T) { _, _ = w.Write([]byte(`{"data":{"status":"success","bot_info":{"botid":"bot-1","secret":"secret-1"}}}`)) } })) + defer server.Close() var output bytes.Buffer opts := normalizeWeComQRFlowOptions(wecomQRFlowOptions{ diff --git a/docs/channels/discord/README.md b/docs/channels/discord/README.md index 741bc64a1..771289d28 100644 --- a/docs/channels/discord/README.md +++ b/docs/channels/discord/README.md @@ -8,56 +8,26 @@ Discord is a free voice, video, and text chat application designed for communiti ```json { - "agents": { - "defaults": { - "tool_feedback": { - "enabled": true, - "max_args_length": 300 - } - } - }, "channel_list": { "discord": { "enabled": true, "type": "discord", "token": "YOUR_BOT_TOKEN", "allow_from": ["YOUR_USER_ID"], - "placeholder": { - "enabled": true, - "text": ["Thinking... 💭"] - }, "group_trigger": { "mention_only": false - }, - "reasoning_channel_id": "" + } } } } ``` -| Field | Type | Required | Description | -| -------------------- | ------ | -------- | --------------------------------------------------------------------------- | -| enabled | bool | Yes | Whether to enable the Discord channel | -| token | string | Yes | Discord Bot Token | -| allow_from | array | No | Allowlist of user IDs; empty means all users are allowed | -| placeholder | object | No | Placeholder message config shown while the agent is working | -| group_trigger | object | No | Group trigger settings (example: { "mention_only": false }) | -| reasoning_channel_id | string | No | Optional target channel ID for reasoning/thinking output | - -## Visible Execution Feedback - -Discord can show three different kinds of "working" feedback: - -1. Typing indicator: automatic, no extra config needed. -2. Placeholder message: enable `channel_list.discord.placeholder.enabled` to send a visible `Thinking...` message that is later edited into the final reply. -3. Tool execution feedback: enable `agents.defaults.tool_feedback.enabled` to send a short message before each tool call, for example: - -```text -🔧 `web_search` -Checking the latest PicoClaw release notes before I answer. -``` - -If you only see `Bot is typing`, check that `placeholder.enabled` or `tool_feedback.enabled` is actually set in your runtime config. +| Field | Type | Required | Description | +| ------------- | ------ | -------- | --------------------------------------------------------------------------- | +| enabled | bool | Yes | Whether to enable the Discord channel | +| token | string | Yes | Discord Bot Token | +| allow_from | array | No | Allowlist of user IDs; empty means all users are allowed | +| group_trigger | object | No | Group trigger settings (example: { "mention_only": false }) | ## Setup diff --git a/pkg/agent/loop.go b/pkg/agent/loop.go index f0c287ee2..fb6f95edf 100644 --- a/pkg/agent/loop.go +++ b/pkg/agent/loop.go @@ -112,7 +112,6 @@ const ( pendingTurnPrefix = "pending-" metadataKeyMessageKind = "message_kind" messageKindThought = "thought" - messageKindToolFeedback = "tool_feedback" metadataKeyAccountID = "account_id" metadataKeyGuildID = "guild_id" metadataKeyTeamID = "team_id" diff --git a/pkg/agent/loop_test.go b/pkg/agent/loop_test.go index a2d4ea7aa..5cdac186c 100644 --- a/pkg/agent/loop_test.go +++ b/pkg/agent/loop_test.go @@ -24,7 +24,6 @@ import ( "github.com/sipeed/picoclaw/pkg/routing" "github.com/sipeed/picoclaw/pkg/session" "github.com/sipeed/picoclaw/pkg/tools" - "github.com/sipeed/picoclaw/pkg/utils" ) type fakeChannel struct{ id string } @@ -1759,157 +1758,6 @@ func (m *toolFeedbackProvider) GetDefaultModel() string { return "heartbeat-tool-feedback-model" } -type toolFeedbackReasoningProvider struct { - filePath string - calls int -} - -func (m *toolFeedbackReasoningProvider) Chat( - ctx context.Context, - messages []providers.Message, - tools []providers.ToolDefinition, - model string, - opts map[string]any, -) (*providers.LLMResponse, error) { - m.calls++ - if m.calls == 1 { - return &providers.LLMResponse{ - ReasoningContent: "Read README.md first to confirm the context that needs to be changed.", - ToolCalls: []providers.ToolCall{{ - ID: "call_reasoning_read_file", - Type: "function", - Name: "read_file", - Arguments: map[string]any{"path": m.filePath}, - }}, - }, nil - } - - return &providers.LLMResponse{ - Content: "DONE", - ToolCalls: []providers.ToolCall{}, - }, nil -} - -func (m *toolFeedbackReasoningProvider) GetDefaultModel() string { - return "tool-feedback-reasoning-model" -} - -func TestToolFeedbackExplanationFromResponse_UsesCurrentContentFirst(t *testing.T) { - response := &providers.LLMResponse{ - Content: "Read README.md first", - ReasoningContent: "current reasoning fallback", - } - messages := []providers.Message{ - {Role: "user", Content: "check file"}, - {Role: "assistant", Content: "Previous turn explanation"}, - {Role: "tool", Content: "tool output", ToolCallID: "call_1"}, - } - - got := toolFeedbackExplanationFromResponse(response, messages, 300) - if got != "Read README.md first" { - t.Fatalf("toolFeedbackExplanationFromResponse() = %q, want current content", got) - } -} - -func TestToolFeedbackExplanationFromResponse_UsesExplicitToolCallExtraContent(t *testing.T) { - response := &providers.LLMResponse{ - ToolCalls: []providers.ToolCall{{ - ID: "call_1", - Name: "read_file", - ExtraContent: &providers.ExtraContent{ - ToolFeedbackExplanation: "Read README.md first to confirm the current project structure.", - }, - }}, - } - messages := []providers.Message{ - {Role: "user", Content: "check file"}, - {Role: "assistant", Content: ""}, - {Role: "tool", Content: "tool output", ToolCallID: "call_1"}, - } - - got := toolFeedbackExplanationFromResponse(response, messages, 300) - if got != "Read README.md first to confirm the current project structure." { - t.Fatalf("toolFeedbackExplanationFromResponse() = %q, want explicit tool feedback explanation", got) - } -} - -func TestToolFeedbackExplanationForToolCall_PrefersToolSpecificExtraContent(t *testing.T) { - response := &providers.LLMResponse{ - Content: "Shared explanation", - ToolCalls: []providers.ToolCall{ - { - ID: "call_1", - Name: "read_file", - ExtraContent: &providers.ExtraContent{ - ToolFeedbackExplanation: "Read README.md first.", - }, - }, - { - ID: "call_2", - Name: "edit_file", - ExtraContent: &providers.ExtraContent{ - ToolFeedbackExplanation: "Update config example after reading it.", - }, - }, - }, - } - - got1 := toolFeedbackExplanationForToolCall(response, response.ToolCalls[0], nil, 300) - got2 := toolFeedbackExplanationForToolCall(response, response.ToolCalls[1], nil, 300) - if got1 != "Read README.md first." { - t.Fatalf("toolFeedbackExplanationForToolCall() first = %q, want tool-specific explanation", got1) - } - if got2 != "Update config example after reading it." { - t.Fatalf("toolFeedbackExplanationForToolCall() second = %q, want tool-specific explanation", got2) - } -} - -func TestToolFeedbackExplanationForToolCall_DoesNotReuseAnotherToolCallExplanation(t *testing.T) { - response := &providers.LLMResponse{ - ToolCalls: []providers.ToolCall{ - { - ID: "call_1", - Name: "read_file", - }, - { - ID: "call_2", - Name: "edit_file", - ExtraContent: &providers.ExtraContent{ - ToolFeedbackExplanation: "Update config example after reading it.", - }, - }, - }, - } - messages := []providers.Message{ - {Role: "user", Content: "inspect the config and update the example"}, - } - - got := toolFeedbackExplanationForToolCall(response, response.ToolCalls[0], messages, 300) - want := utils.ToolFeedbackContinuationHint + ": inspect the config and update the example" - if got != want { - t.Fatalf("toolFeedbackExplanationForToolCall() = %q, want %q", got, want) - } -} - -func TestToolFeedbackExplanationFromResponse_DoesNotUseReasoningContent(t *testing.T) { - response := &providers.LLMResponse{ - Content: "", - ReasoningContent: "hidden reasoning should not be shown", - } - messages := []providers.Message{ - {Role: "user", Content: "check file"}, - {Role: "assistant", Content: "Previous turn explanation"}, - {Role: "user", Content: "Inspect README.md and update the config example."}, - {Role: "tool", Content: "tool output", ToolCallID: "call_1"}, - } - - got := toolFeedbackExplanationFromResponse(response, messages, 300) - want := utils.ToolFeedbackContinuationHint + ": Inspect README.md and update the config example." - if got != want { - t.Fatalf("toolFeedbackExplanationFromResponse() = %q, want latest user content fallback", got) - } -} - type picoInterleavedContentProvider struct { calls int } @@ -3808,16 +3656,7 @@ func TestProcessMessage_PublishesToolFeedbackWhenEnabled(t *testing.T) { t.Fatalf("unexpected tool feedback context: %+v", outbound.Context) } if !strings.Contains(outbound.Content, "`read_file`") { - t.Fatalf("tool feedback content = %q, want read_file summary", outbound.Content) - } - if !strings.Contains(outbound.Content, utils.ToolFeedbackContinuationHint) { - t.Fatalf("tool feedback content = %q, want continuation hint fallback", outbound.Content) - } - if !strings.Contains(outbound.Content, "check tool feedback") { - t.Fatalf("tool feedback content = %q, want current user intent fallback", outbound.Content) - } - if strings.Contains(outbound.Content, "Previous turn explanation") { - t.Fatalf("tool feedback content = %q, want no previous assistant fallback", outbound.Content) + t.Fatalf("tool feedback content = %q, want read_file preview", outbound.Content) } if outbound.AgentID != "main" { t.Fatalf("tool feedback agent_id = %q, want main", outbound.AgentID) @@ -3833,130 +3672,6 @@ func TestProcessMessage_PublishesToolFeedbackWhenEnabled(t *testing.T) { } } -func TestProcessMessage_DoesNotLeakReasoningContentInToolFeedback(t *testing.T) { - tmpDir := t.TempDir() - heartbeatFile := filepath.Join(tmpDir, "tool-feedback-reasoning.txt") - if err := os.WriteFile(heartbeatFile, []byte("tool feedback task"), 0o644); err != nil { - t.Fatalf("WriteFile() error = %v", err) - } - - cfg := &config.Config{ - Agents: config.AgentsConfig{ - Defaults: config.AgentDefaults{ - Workspace: tmpDir, - ModelName: "test-model", - MaxTokens: 4096, - MaxToolIterations: 10, - ToolFeedback: config.ToolFeedbackConfig{ - Enabled: true, - MaxArgsLength: 300, - }, - }, - }, - Tools: config.ToolsConfig{ - ReadFile: config.ReadFileToolConfig{ - Enabled: true, - }, - }, - } - - msgBus := bus.NewMessageBus() - provider := &toolFeedbackReasoningProvider{filePath: heartbeatFile} - al := NewAgentLoop(cfg, msgBus, provider) - - response, err := al.processMessage(context.Background(), testInboundMessage(bus.InboundMessage{ - Channel: "telegram", - SenderID: "user-1", - ChatID: "chat-1", - Content: "check reasoning fallback", - })) - if err != nil { - t.Fatalf("processMessage() error = %v", err) - } - if response != "DONE" { - t.Fatalf("processMessage() response = %q, want %q", response, "DONE") - } - - select { - case outbound := <-msgBus.OutboundChan(): - if !strings.Contains(outbound.Content, "`read_file`") { - t.Fatalf("tool feedback content = %q, want read_file summary", outbound.Content) - } - if !strings.Contains(outbound.Content, utils.ToolFeedbackContinuationHint) { - t.Fatalf("tool feedback content = %q, want continuation hint fallback", outbound.Content) - } - if !strings.Contains(outbound.Content, "check reasoning fallback") { - t.Fatalf("tool feedback content = %q, want current user intent fallback", outbound.Content) - } - if strings.Contains(outbound.Content, "Read README.md first") { - t.Fatalf("tool feedback content = %q, should not leak hidden reasoning", outbound.Content) - } - case <-time.After(2 * time.Second): - t.Fatal("expected outbound tool feedback without leaking reasoning") - } -} - -func TestProcessMessage_DoesNotPublishToolFeedbackForDiscordWhenDisabled(t *testing.T) { - assertToolFeedbackNotPublishedWhenDisabled(t, "discord") -} - -func assertToolFeedbackNotPublishedWhenDisabled(t *testing.T, channel string) { - t.Helper() - - tmpDir := t.TempDir() - heartbeatFile := filepath.Join(tmpDir, "tool-feedback-"+channel+".txt") - if err := os.WriteFile(heartbeatFile, []byte("tool feedback task"), 0o644); err != nil { - t.Fatalf("WriteFile() error = %v", err) - } - - cfg := &config.Config{ - Agents: config.AgentsConfig{ - Defaults: config.AgentDefaults{ - Workspace: tmpDir, - ModelName: "test-model", - MaxTokens: 4096, - MaxToolIterations: 10, - }, - }, - Tools: config.ToolsConfig{ - ReadFile: config.ReadFileToolConfig{ - Enabled: true, - }, - }, - } - - msgBus := bus.NewMessageBus() - provider := &toolFeedbackProvider{filePath: heartbeatFile} - al := NewAgentLoop(cfg, msgBus, provider) - - response, err := al.processMessage(context.Background(), testInboundMessage(bus.InboundMessage{ - Channel: channel, - SenderID: "user-1", - ChatID: "chat-1", - Content: "check tool feedback", - })) - if err != nil { - t.Fatalf("processMessage() error = %v", err) - } - if response != "HEARTBEAT_OK" { - t.Fatalf("processMessage() response = %q, want %q", response, "HEARTBEAT_OK") - } - - select { - case outbound := <-msgBus.OutboundChan(): - t.Fatalf("expected no outbound tool feedback for %s when disabled, got %+v", channel, outbound) - case <-time.After(200 * time.Millisecond): - } -} - -func TestProcessMessage_DoesNotPublishToolFeedbackForTelegramWhenDisabled(t *testing.T) { - assertToolFeedbackNotPublishedWhenDisabled(t, "telegram") -} - -func TestProcessMessage_DoesNotPublishToolFeedbackForFeishuWhenDisabled(t *testing.T) { - assertToolFeedbackNotPublishedWhenDisabled(t, "feishu") -} - func TestProcessMessage_MessageToolPublishesOutboundWithTurnMetadata(t *testing.T) { cfg := config.DefaultConfig() cfg.Agents.Defaults.Workspace = t.TempDir() @@ -4131,85 +3846,6 @@ func TestRunAgentLoop_PicoSkipsInterimPublishWhenNotAllowed(t *testing.T) { } } -func TestRun_PicoToolFeedbackSuppressesDuplicateInterimAssistantContent(t *testing.T) { - tmpDir := t.TempDir() - - cfg := &config.Config{ - Agents: config.AgentsConfig{ - Defaults: config.AgentDefaults{ - Workspace: tmpDir, - ModelName: "test-model", - MaxTokens: 4096, - MaxToolIterations: 10, - ToolFeedback: config.ToolFeedbackConfig{ - Enabled: true, - }, - }, - }, - } - - msgBus := bus.NewMessageBus() - provider := &picoInterleavedContentProvider{} - al := NewAgentLoop(cfg, msgBus, provider) - - agent := al.GetRegistry().GetDefaultAgent() - if agent == nil { - t.Fatal("expected default agent") - } - agent.Tools.Register(&toolLimitTestTool{}) - - runCtx, runCancel := context.WithCancel(context.Background()) - defer runCancel() - - runDone := make(chan error, 1) - go func() { - runDone <- al.Run(runCtx) - }() - - if err := msgBus.PublishInbound(context.Background(), bus.InboundMessage{ - Channel: "pico", - SenderID: "user-1", - ChatID: "session-1", - Content: "run with tools", - }); err != nil { - t.Fatalf("PublishInbound() error = %v", err) - } - - outputs := make([]string, 0, 2) - deadline := time.After(2 * time.Second) - for len(outputs) < 2 { - select { - case outbound := <-msgBus.OutboundChan(): - outputs = append(outputs, outbound.Content) - case <-deadline: - t.Fatalf("timed out waiting for pico outputs, got %v", outputs) - } - } - - if outputs[0] != "🔧 `tool_limit_test_tool`\nintermediate model text" { - t.Fatalf("first outbound content = %q, want tool feedback summary", outputs[0]) - } - if outputs[1] != "final model text" { - t.Fatalf("second outbound content = %q, want %q", outputs[1], "final model text") - } - - runCancel() - select { - case err := <-runDone: - if err != nil { - t.Fatalf("Run() error = %v", err) - } - case <-time.After(2 * time.Second): - t.Fatal("timed out waiting for Run() to exit") - } - - select { - case outbound := <-msgBus.OutboundChan(): - t.Fatalf("unexpected extra pico output after tool feedback + final reply: %+v", outbound) - case <-time.After(200 * time.Millisecond): - } -} - func TestResolveMediaRefs_ResolvesToBase64(t *testing.T) { store := media.NewFileMediaStore() dir := t.TempDir() diff --git a/pkg/agent/loop_turn.go b/pkg/agent/loop_turn.go index 406120e46..1085ddeae 100644 --- a/pkg/agent/loop_turn.go +++ b/pkg/agent/loop_turn.go @@ -635,11 +635,7 @@ turnLoop: } logger.DebugCF("agent", "LLM response", llmResponseFields) - if al.bus != nil && - ts.channel == "pico" && - len(response.ToolCalls) > 0 && - ts.opts.AllowInterimPicoPublish && - !shouldPublishToolFeedback(al.cfg, ts) { + if al.bus != nil && ts.channel == "pico" && len(response.ToolCalls) > 0 && ts.opts.AllowInterimPicoPublish { if strings.TrimSpace(response.Content) != "" { outCtx, outCancel := context.WithTimeout(turnCtx, 3*time.Second) err := al.bus.PublishOutbound(outCtx, bus.OutboundMessage{ @@ -709,19 +705,7 @@ turnLoop: } for _, tc := range normalizedToolCalls { argumentsJSON, _ := json.Marshal(tc.Arguments) - toolFeedbackExplanation := toolFeedbackExplanationForToolCall( - response, - tc, - messages, - al.cfg.Agents.Defaults.GetToolFeedbackMaxArgsLength(), - ) extraContent := tc.ExtraContent - if strings.TrimSpace(toolFeedbackExplanation) != "" { - if extraContent == nil { - extraContent = &providers.ExtraContent{} - } - extraContent.ToolFeedbackExplanation = toolFeedbackExplanation - } thoughtSignature := "" if tc.Function != nil { thoughtSignature = tc.Function.ThoughtSignature @@ -799,16 +783,21 @@ turnLoop: ) // Send tool feedback to chat channel if enabled (same as normal tool execution) - if shouldPublishToolFeedback(al.cfg, ts) { - toolFeedbackExplanation := toolFeedbackExplanationForToolCall( - response, - tc, - messages, + if al.cfg.Agents.Defaults.IsToolFeedbackEnabled() && + ts.channel != "" && + !ts.opts.SuppressToolFeedback { + argsJSON, _ := json.Marshal(toolArgs) + feedbackPreview := utils.Truncate( + string(argsJSON), al.cfg.Agents.Defaults.GetToolFeedbackMaxArgsLength(), ) - feedbackMsg := utils.FormatToolFeedbackMessage(toolName, toolFeedbackExplanation) + feedbackMsg := utils.FormatToolFeedbackMessage(toolName, feedbackPreview) fbCtx, fbCancel := context.WithTimeout(turnCtx, 3*time.Second) - _ = al.bus.PublishOutbound(fbCtx, outboundMessageForTurnWithKind(ts, feedbackMsg, messageKindToolFeedback)) + _ = al.bus.PublishOutbound(fbCtx, bus.OutboundMessage{ + Channel: ts.channel, + ChatID: ts.chatID, + Content: feedbackMsg, + }) fbCancel() } @@ -1078,16 +1067,16 @@ turnLoop: ) // Send tool feedback to chat channel if enabled (from HEAD) - if shouldPublishToolFeedback(al.cfg, ts) { - toolFeedbackExplanation := toolFeedbackExplanationForToolCall( - response, - tc, - messages, + if al.cfg.Agents.Defaults.IsToolFeedbackEnabled() && + ts.channel != "" && + !ts.opts.SuppressToolFeedback { + feedbackPreview := utils.Truncate( + string(argsJSON), al.cfg.Agents.Defaults.GetToolFeedbackMaxArgsLength(), ) - feedbackMsg := utils.FormatToolFeedbackMessage(tc.Name, toolFeedbackExplanation) + feedbackMsg := utils.FormatToolFeedbackMessage(tc.Name, feedbackPreview) fbCtx, fbCancel := context.WithTimeout(turnCtx, 3*time.Second) - _ = al.bus.PublishOutbound(fbCtx, outboundMessageForTurnWithKind(ts, feedbackMsg, messageKindToolFeedback)) + _ = al.bus.PublishOutbound(fbCtx, outboundMessageForTurn(ts, feedbackMsg)) fbCancel() } diff --git a/pkg/agent/loop_utils.go b/pkg/agent/loop_utils.go index ff98dad68..2574f0222 100644 --- a/pkg/agent/loop_utils.go +++ b/pkg/agent/loop_utils.go @@ -11,7 +11,6 @@ import ( "github.com/sipeed/picoclaw/pkg/bus" "github.com/sipeed/picoclaw/pkg/commands" - "github.com/sipeed/picoclaw/pkg/config" "github.com/sipeed/picoclaw/pkg/providers" "github.com/sipeed/picoclaw/pkg/session" "github.com/sipeed/picoclaw/pkg/utils" @@ -85,98 +84,6 @@ func outboundMessageForTurn(ts *turnState, content string) bus.OutboundMessage { } } -func outboundMessageForTurnWithKind(ts *turnState, content, kind string) bus.OutboundMessage { - msg := outboundMessageForTurn(ts, content) - if strings.TrimSpace(kind) == "" { - return msg - } - if msg.Context.Raw == nil { - msg.Context.Raw = make(map[string]string, 1) - } - msg.Context.Raw[metadataKeyMessageKind] = kind - return msg -} - -func latestUserContent(messages []providers.Message) string { - for i := len(messages) - 1; i >= 0; i-- { - msg := messages[i] - if msg.Role != "user" { - continue - } - if content := strings.TrimSpace(msg.Content); content != "" { - return content - } - } - return "" -} - -func toolFeedbackExplanationFromResponse( - response *providers.LLMResponse, - messages []providers.Message, - maxLen int, -) string { - if response == nil { - return "" - } - explanation := strings.TrimSpace(response.Content) - if explanation == "" { - explanation = toolFeedbackExplanationFromToolCalls(response.ToolCalls) - } - if explanation == "" { - explanation = toolFeedbackExplanationFromMessages(messages) - } - return utils.Truncate(explanation, maxLen) -} - -func toolFeedbackExplanationFromToolCalls(toolCalls []providers.ToolCall) string { - for _, tc := range toolCalls { - if tc.ExtraContent == nil { - continue - } - if explanation := strings.TrimSpace(tc.ExtraContent.ToolFeedbackExplanation); explanation != "" { - return explanation - } - } - return "" -} - -func toolFeedbackExplanationForToolCall( - response *providers.LLMResponse, - toolCall providers.ToolCall, - messages []providers.Message, - maxLen int, -) string { - if toolCall.ExtraContent != nil { - if explanation := strings.TrimSpace(toolCall.ExtraContent.ToolFeedbackExplanation); explanation != "" { - return utils.Truncate(explanation, maxLen) - } - } - if response == nil { - return utils.Truncate(toolFeedbackExplanationFromMessages(messages), maxLen) - } - - explanation := strings.TrimSpace(response.Content) - if explanation == "" { - explanation = toolFeedbackExplanationFromMessages(messages) - } - return utils.Truncate(explanation, maxLen) -} - -func toolFeedbackExplanationFromMessages(messages []providers.Message) string { - explanation := latestUserContent(messages) - if explanation != "" { - return utils.ToolFeedbackContinuationHint + ": " + explanation - } - return "" -} - -func shouldPublishToolFeedback(cfg *config.Config, ts *turnState) bool { - if ts == nil || ts.channel == "" || ts.opts.SuppressToolFeedback { - return false - } - return cfg != nil && cfg.Agents.Defaults.IsToolFeedbackEnabled() -} - func cloneEventArguments(args map[string]any) map[string]any { if len(args) == 0 { return nil diff --git a/pkg/channels/discord/discord.go b/pkg/channels/discord/discord.go index 514b9b3b1..28f7277d3 100644 --- a/pkg/channels/discord/discord.go +++ b/pkg/channels/discord/discord.go @@ -45,12 +45,9 @@ type DiscordChannel struct { cancel context.CancelFunc typingMu sync.Mutex typingStop map[string]chan struct{} // chatID → stop signal - progress *channels.ToolFeedbackAnimator - botUserID string // stored for mention checking + botUserID string // stored for mention checking bus *bus.MessageBus tts tts.TTSProvider - playTTSFn func(context.Context, *discordgo.VoiceConnection, string, uint64) - ttsVoiceFn func(string) (*discordgo.VoiceConnection, bool) voiceMu sync.RWMutex voiceSSRC map[string]map[uint32]string // guildID -> ssrc -> userID @@ -87,7 +84,7 @@ func NewDiscordChannel( channels.WithReasoningChannelID(bc.ReasoningChannelID), ) - ch := &DiscordChannel{ + return &DiscordChannel{ BaseChannel: base, bc: bc, session: session, @@ -96,11 +93,7 @@ func NewDiscordChannel( typingStop: make(map[string]chan struct{}), bus: bus, voiceSSRC: make(map[string]map[uint32]string), - } - ch.playTTSFn = ch.playTTS - ch.ttsVoiceFn = ch.voiceConnectionForTTS - ch.progress = channels.NewToolFeedbackAnimator(ch.EditMessage) - return ch, nil + }, nil } func (c *DiscordChannel) Start(ctx context.Context) error { @@ -149,9 +142,6 @@ func (c *DiscordChannel) Stop(ctx context.Context) error { if c.cancel != nil { c.cancel() } - if c.progress != nil { - c.progress.StopAll() - } if err := c.session.Close(); err != nil { return fmt.Errorf("failed to close discord session: %w", err) @@ -174,88 +164,32 @@ func (c *DiscordChannel) Send(ctx context.Context, msg bus.OutboundMessage) ([]s return nil, nil } - isToolFeedback := outboundMessageIsToolFeedback(msg) - if isToolFeedback { - if msgID, handled, err := c.progress.Update(ctx, channelID, msg.Content); handled { - if err != nil { - return nil, err + if c.tts != nil { + if ch, err := c.session.State.Channel(channelID); err == nil && ch.GuildID != "" { + if vc, ok := c.session.VoiceConnections[ch.GuildID]; ok && vc != nil { + // Cancel any previous TTS playback + c.ttsMu.Lock() + if c.cancelTTS != nil { + c.cancelTTS() + } + ttsCtx, ttsCancel := context.WithCancel(c.ctx) + c.ttsPlayID++ + playID := c.ttsPlayID + c.cancelTTS = ttsCancel + c.ttsMu.Unlock() + + go c.playTTS(ttsCtx, vc, msg.Content, playID) } - return []string{msgID}, nil - } - } - trackedMsgID, hasTrackedMsg := c.currentToolFeedbackMessage(channelID) - c.maybeStartTTS(channelID, msg.Content, isToolFeedback) - if !isToolFeedback { - if msgIDs, handled := c.FinalizeToolFeedbackMessage(ctx, msg); handled { - return msgIDs, nil } } - content := msg.Content - if isToolFeedback { - content = channels.InitialAnimatedToolFeedbackContent(msg.Content) - } - msgID, err := c.sendChunk(ctx, channelID, content, msg.ReplyToMessageID) + msgID, err := c.sendChunk(ctx, channelID, msg.Content, msg.ReplyToMessageID) if err != nil { return nil, err } - if isToolFeedback { - c.RecordToolFeedbackMessage(channelID, msgID, msg.Content) - } else if hasTrackedMsg { - c.dismissTrackedToolFeedbackMessage(ctx, channelID, trackedMsgID) - } return []string{msgID}, nil } -func (c *DiscordChannel) maybeStartTTS(channelID, content string, isToolFeedback bool) { - if c.tts == nil || isToolFeedback { - return - } - - voiceFn := c.ttsVoiceFn - if voiceFn == nil { - voiceFn = c.voiceConnectionForTTS - } - vc, ok := voiceFn(channelID) - if !ok || vc == nil { - return - } - - // Cancel any previous TTS playback. - c.ttsMu.Lock() - if c.cancelTTS != nil { - c.cancelTTS() - } - ttsCtx, ttsCancel := context.WithCancel(c.ctx) - c.ttsPlayID++ - playID := c.ttsPlayID - c.cancelTTS = ttsCancel - playFn := c.playTTSFn - c.ttsMu.Unlock() - - if playFn == nil { - playFn = c.playTTS - } - go playFn(ttsCtx, vc, content, playID) -} - -func (c *DiscordChannel) voiceConnectionForTTS(channelID string) (*discordgo.VoiceConnection, bool) { - if c.session == nil || c.session.State == nil { - return nil, false - } - - ch, err := c.session.State.Channel(channelID) - if err != nil || ch == nil || ch.GuildID == "" { - return nil, false - } - - vc, ok := c.session.VoiceConnections[ch.GuildID] - if !ok || vc == nil { - return nil, false - } - return vc, true -} - // SendMedia implements the channels.MediaSender interface. func (c *DiscordChannel) SendMedia(ctx context.Context, msg bus.OutboundMediaMessage) ([]string, error) { if !c.IsRunning() { @@ -266,7 +200,6 @@ func (c *DiscordChannel) SendMedia(ctx context.Context, msg bus.OutboundMediaMes if channelID == "" { return nil, fmt.Errorf("channel ID is empty") } - trackedMsgID, hasTrackedMsg := c.currentToolFeedbackMessage(channelID) store := c.GetMediaStore() if store == nil { @@ -348,9 +281,6 @@ func (c *DiscordChannel) SendMedia(ctx context.Context, msg bus.OutboundMediaMes if r.err != nil { return nil, fmt.Errorf("discord send media: %w", channels.ErrTemporary) } - if hasTrackedMsg { - c.dismissTrackedToolFeedbackMessage(ctx, channelID, trackedMsgID) - } return []string{r.id}, nil case <-sendCtx.Done(): // Close all file readers @@ -365,15 +295,10 @@ func (c *DiscordChannel) SendMedia(ctx context.Context, msg bus.OutboundMediaMes // EditMessage implements channels.MessageEditor. func (c *DiscordChannel) EditMessage(ctx context.Context, chatID string, messageID string, content string) error { - _, err := c.session.ChannelMessageEdit(chatID, messageID, content, discordgo.WithContext(ctx)) + _, err := c.session.ChannelMessageEdit(chatID, messageID, content) return err } -// DeleteMessage implements channels.MessageDeleter. -func (c *DiscordChannel) DeleteMessage(ctx context.Context, chatID string, messageID string) error { - return c.session.ChannelMessageDelete(chatID, messageID, discordgo.WithContext(ctx)) -} - // SendPlaceholder implements channels.PlaceholderCapable. // It sends a placeholder message that will later be edited to the actual // response via EditMessage (channels.MessageEditor). @@ -392,81 +317,6 @@ func (c *DiscordChannel) SendPlaceholder(ctx context.Context, chatID string) (st return msg.ID, nil } -func outboundMessageIsToolFeedback(msg bus.OutboundMessage) bool { - if len(msg.Context.Raw) == 0 { - return false - } - return strings.EqualFold(strings.TrimSpace(msg.Context.Raw["message_kind"]), "tool_feedback") -} - -func (c *DiscordChannel) currentToolFeedbackMessage(chatID string) (string, bool) { - if c.progress == nil { - return "", false - } - return c.progress.Current(chatID) -} - -func (c *DiscordChannel) takeToolFeedbackMessage(chatID string) (string, string, bool) { - if c.progress == nil { - return "", "", false - } - return c.progress.Take(chatID) -} - -func (c *DiscordChannel) RecordToolFeedbackMessage(chatID, messageID, content string) { - if c.progress == nil { - return - } - c.progress.Record(chatID, messageID, content) -} - -func (c *DiscordChannel) ClearToolFeedbackMessage(chatID string) { - if c.progress == nil { - return - } - c.progress.Clear(chatID) -} - -func (c *DiscordChannel) DismissToolFeedbackMessage(ctx context.Context, chatID string) { - msgID, ok := c.currentToolFeedbackMessage(chatID) - if !ok { - return - } - c.dismissTrackedToolFeedbackMessage(ctx, chatID, msgID) -} - -func (c *DiscordChannel) dismissTrackedToolFeedbackMessage(ctx context.Context, chatID, messageID string) { - if strings.TrimSpace(chatID) == "" || strings.TrimSpace(messageID) == "" { - return - } - c.ClearToolFeedbackMessage(chatID) - _ = c.DeleteMessage(ctx, chatID, messageID) -} - -func (c *DiscordChannel) finalizeTrackedToolFeedbackMessage( - ctx context.Context, - chatID string, - content string, - editFn func(context.Context, string, string, string) error, -) ([]string, bool) { - msgID, baseContent, ok := c.takeToolFeedbackMessage(chatID) - if !ok || editFn == nil { - return nil, false - } - if err := editFn(ctx, chatID, msgID, content); err != nil { - c.RecordToolFeedbackMessage(chatID, msgID, baseContent) - return nil, false - } - return []string{msgID}, true -} - -func (c *DiscordChannel) FinalizeToolFeedbackMessage(ctx context.Context, msg bus.OutboundMessage) ([]string, bool) { - if outboundMessageIsToolFeedback(msg) { - return nil, false - } - return c.finalizeTrackedToolFeedbackMessage(ctx, msg.ChatID, msg.Content, c.EditMessage) -} - func (c *DiscordChannel) sendChunk(ctx context.Context, channelID, content, replyToID string) (string, error) { // Use the passed ctx for timeout control sendCtx, cancel := context.WithTimeout(ctx, sendTimeout) diff --git a/pkg/channels/discord/discord_test.go b/pkg/channels/discord/discord_test.go index d42b0bc52..0cd5328f4 100644 --- a/pkg/channels/discord/discord_test.go +++ b/pkg/channels/discord/discord_test.go @@ -1,37 +1,13 @@ package discord import ( - "context" - "io" "net/http" - "net/http/httptest" "net/url" - "reflect" - "sync" "testing" - "time" "github.com/bwmarrin/discordgo" - - "github.com/sipeed/picoclaw/pkg/audio/tts" - "github.com/sipeed/picoclaw/pkg/bus" - "github.com/sipeed/picoclaw/pkg/channels" ) -type stubTTSProvider struct{} - -func (stubTTSProvider) Name() string { return "stub-tts" } - -func (stubTTSProvider) Synthesize(context.Context, string) (io.ReadCloser, error) { - return io.NopCloser(&noopReader{}), nil -} - -type noopReader struct{} - -func (*noopReader) Read(p []byte) (int, error) { - return 0, io.EOF -} - func TestApplyDiscordProxy_CustomProxy(t *testing.T) { session, err := discordgo.New("Bot test-token") if err != nil { @@ -113,224 +89,3 @@ func TestApplyDiscordProxy_InvalidProxyURL(t *testing.T) { t.Fatal("applyDiscordProxy() expected error for invalid proxy URL, got nil") } } - -func TestSend_NonToolFeedbackDeletesTrackedProgressMessage(t *testing.T) { - var ( - mu sync.Mutex - requests []string - ) - - server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - mu.Lock() - requests = append(requests, r.Method+" "+r.URL.Path) - mu.Unlock() - - switch { - case r.Method == http.MethodPatch && r.URL.Path == "/channels/chat-1/messages/prog-1": - w.Header().Set("Content-Type", "application/json") - _, _ = io.WriteString(w, `{"id":"prog-1"}`) - default: - t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path) - } - })) - defer server.Close() - - origChannels := discordgo.EndpointChannels - discordgo.EndpointChannels = server.URL + "/channels/" - defer func() { - discordgo.EndpointChannels = origChannels - }() - - session, err := discordgo.New("Bot test-token") - if err != nil { - t.Fatalf("discordgo.New() error: %v", err) - } - session.Client = server.Client() - - ch := &DiscordChannel{ - BaseChannel: channels.NewBaseChannel("discord", nil, bus.NewMessageBus(), nil), - session: session, - ctx: context.Background(), - typingStop: make(map[string]chan struct{}), - voiceSSRC: make(map[string]map[uint32]string), - } - ch.progress = channels.NewToolFeedbackAnimator(ch.EditMessage) - ch.SetRunning(true) - ch.RecordToolFeedbackMessage("chat-1", "prog-1", "🔧 `read_file`") - - ids, err := ch.Send(context.Background(), bus.OutboundMessage{ - ChatID: "chat-1", - Content: "final reply", - Context: bus.InboundContext{ - Channel: "discord", - ChatID: "chat-1", - }, - }) - if err != nil { - t.Fatalf("Send() error = %v", err) - } - if got, want := ids, []string{"prog-1"}; !reflect.DeepEqual(got, want) { - t.Fatalf("Send() ids = %v, want %v", got, want) - } - if _, ok := ch.currentToolFeedbackMessage("chat-1"); ok { - t.Fatal("expected tracked tool feedback message to be cleared") - } - - mu.Lock() - defer mu.Unlock() - wantRequests := []string{ - "PATCH /channels/chat-1/messages/prog-1", - } - if !reflect.DeepEqual(requests, wantRequests) { - t.Fatalf("requests = %v, want %v", requests, wantRequests) - } -} - -func TestEditMessage_UsesContextCancellation(t *testing.T) { - server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - select { - case <-r.Context().Done(): - return - case <-time.After(time.Second): - w.Header().Set("Content-Type", "application/json") - _, _ = io.WriteString(w, `{"id":"msg-1"}`) - } - })) - defer server.Close() - - origChannels := discordgo.EndpointChannels - discordgo.EndpointChannels = server.URL + "/channels/" - defer func() { - discordgo.EndpointChannels = origChannels - }() - - session, err := discordgo.New("Bot test-token") - if err != nil { - t.Fatalf("discordgo.New() error: %v", err) - } - session.Client = server.Client() - - ch := &DiscordChannel{ - BaseChannel: channels.NewBaseChannel("discord", nil, bus.NewMessageBus(), nil), - session: session, - } - - ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond) - defer cancel() - - start := time.Now() - err = ch.EditMessage(ctx, "chat-1", "msg-1", "still running") - elapsed := time.Since(start) - - if err == nil { - t.Fatal("expected EditMessage() to fail when context times out") - } - if elapsed >= 500*time.Millisecond { - t.Fatalf("EditMessage() ignored context timeout, elapsed=%v", elapsed) - } -} - -func TestFinalizeTrackedToolFeedbackMessage_StopsTrackingBeforeEdit(t *testing.T) { - ch := &DiscordChannel{ - progress: channels.NewToolFeedbackAnimator(nil), - } - ch.RecordToolFeedbackMessage("chat-1", "msg-1", "🔧 `read_file`") - - msgIDs, handled := ch.finalizeTrackedToolFeedbackMessage( - context.Background(), - "chat-1", - "final reply", - func(_ context.Context, chatID, messageID, content string) error { - if _, ok := ch.currentToolFeedbackMessage(chatID); ok { - t.Fatal("expected tracked tool feedback to be stopped before edit") - } - if chatID != "chat-1" || messageID != "msg-1" || content != "final reply" { - t.Fatalf("unexpected edit args: %s %s %s", chatID, messageID, content) - } - return nil - }, - ) - if !handled { - t.Fatal("expected finalizeTrackedToolFeedbackMessage to handle tracked message") - } - if got, want := msgIDs, []string{"msg-1"}; !reflect.DeepEqual(got, want) { - t.Fatalf("finalizeTrackedToolFeedbackMessage() ids = %v, want %v", got, want) - } -} - -func TestSend_NonToolFeedbackFinalizerStillStartsTTS(t *testing.T) { - var ( - mu sync.Mutex - requests []string - ) - - server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - mu.Lock() - requests = append(requests, r.Method+" "+r.URL.Path) - mu.Unlock() - - switch { - case r.Method == http.MethodPatch && r.URL.Path == "/channels/chat-1/messages/prog-1": - w.Header().Set("Content-Type", "application/json") - _, _ = io.WriteString(w, `{"id":"prog-1"}`) - default: - t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path) - } - })) - defer server.Close() - - origChannels := discordgo.EndpointChannels - discordgo.EndpointChannels = server.URL + "/channels/" - defer func() { - discordgo.EndpointChannels = origChannels - }() - - session, err := discordgo.New("Bot test-token") - if err != nil { - t.Fatalf("discordgo.New() error: %v", err) - } - session.Client = server.Client() - - ttsStarted := make(chan string, 1) - ch := &DiscordChannel{ - BaseChannel: channels.NewBaseChannel("discord", nil, bus.NewMessageBus(), nil), - session: session, - ctx: context.Background(), - typingStop: make(map[string]chan struct{}), - voiceSSRC: make(map[string]map[uint32]string), - tts: tts.TTSProvider(stubTTSProvider{}), - } - ch.ttsVoiceFn = func(string) (*discordgo.VoiceConnection, bool) { - return &discordgo.VoiceConnection{}, true - } - ch.playTTSFn = func(_ context.Context, _ *discordgo.VoiceConnection, text string, _ uint64) { - ttsStarted <- text - } - ch.progress = channels.NewToolFeedbackAnimator(ch.EditMessage) - ch.SetRunning(true) - ch.RecordToolFeedbackMessage("chat-1", "prog-1", "🔧 `read_file`") - - ids, err := ch.Send(context.Background(), bus.OutboundMessage{ - ChatID: "chat-1", - Content: "final reply", - Context: bus.InboundContext{ - Channel: "discord", - ChatID: "chat-1", - }, - }) - if err != nil { - t.Fatalf("Send() error = %v", err) - } - if got, want := ids, []string{"prog-1"}; !reflect.DeepEqual(got, want) { - t.Fatalf("Send() ids = %v, want %v", got, want) - } - - select { - case got := <-ttsStarted: - if got != "final reply" { - t.Fatalf("TTS content = %q, want final reply", got) - } - case <-time.After(2 * time.Second): - t.Fatal("expected TTS to start for finalized tracked tool feedback reply") - } -} diff --git a/pkg/channels/feishu/feishu_64.go b/pkg/channels/feishu/feishu_64.go index 49b8dd8e5..02ee47d69 100644 --- a/pkg/channels/feishu/feishu_64.go +++ b/pkg/channels/feishu/feishu_64.go @@ -49,8 +49,6 @@ type FeishuChannel struct { mu sync.Mutex cancel context.CancelFunc - - progress *channels.ToolFeedbackAnimator } type cachedMessage struct { @@ -76,7 +74,6 @@ func NewFeishuChannel(bc *config.Channel, cfg *config.FeishuSettings, bus *bus.M tokenCache: tc, client: lark.NewClient(cfg.AppID, cfg.AppSecret.String(), opts...), } - ch.progress = channels.NewToolFeedbackAnimator(ch.EditMessage) ch.SetOwner(ch) return ch, nil } @@ -135,9 +132,6 @@ func (c *FeishuChannel) Stop(ctx context.Context) error { } c.wsClient = nil c.mu.Unlock() - if c.progress != nil { - c.progress.StopAll() - } c.SetRunning(false) logger.InfoC("feishu", "Feishu channel stopped") @@ -155,50 +149,17 @@ func (c *FeishuChannel) Send(ctx context.Context, msg bus.OutboundMessage) ([]st return nil, fmt.Errorf("chat ID is empty: %w", channels.ErrSendFailed) } - isToolFeedback := outboundMessageIsToolFeedback(msg) - trackedMsgID, hasTrackedMsg := c.currentToolFeedbackMessage(msg.ChatID) - if isToolFeedback { - if msgID, handled, err := c.progress.Update(ctx, msg.ChatID, msg.Content); handled { - if err != nil { - return nil, err - } - return []string{msgID}, nil - } - } else { - if msgIDs, handled := c.FinalizeToolFeedbackMessage(ctx, msg); handled { - return msgIDs, nil - } - } - // Build interactive card with markdown content - sendContent := msg.Content - if isToolFeedback { - sendContent = channels.InitialAnimatedToolFeedbackContent(msg.Content) - } - cardContent, err := buildMarkdownCard(sendContent) + cardContent, err := buildMarkdownCard(msg.Content) if err != nil { // If card build fails, fall back to plain text - msgID, sendErr := c.sendText(ctx, msg.ChatID, sendContent) - if sendErr != nil { - return nil, sendErr - } - if isToolFeedback { - c.RecordToolFeedbackMessage(msg.ChatID, msgID, msg.Content) - } else if hasTrackedMsg { - c.dismissTrackedToolFeedbackMessage(ctx, msg.ChatID, trackedMsgID) - } - return []string{msgID}, nil + return nil, c.sendText(ctx, msg.ChatID, msg.Content) } // First attempt: try sending as interactive card - msgID, err := c.sendCard(ctx, msg.ChatID, cardContent) + err = c.sendCard(ctx, msg.ChatID, cardContent) if err == nil { - if isToolFeedback { - c.RecordToolFeedbackMessage(msg.ChatID, msgID, msg.Content) - } else if hasTrackedMsg { - c.dismissTrackedToolFeedbackMessage(ctx, msg.ChatID, trackedMsgID) - } - return []string{msgID}, nil + return nil, nil } // Check if error is due to card table limit (error code 11310) @@ -213,14 +174,9 @@ func (c *FeishuChannel) Send(ctx context.Context, msg bus.OutboundMessage) ([]st }) // Second attempt: fall back to plain text message - msgID, textErr := c.sendText(ctx, msg.ChatID, sendContent) + textErr := c.sendText(ctx, msg.ChatID, msg.Content) if textErr == nil { - if isToolFeedback { - c.RecordToolFeedbackMessage(msg.ChatID, msgID, msg.Content) - } else if hasTrackedMsg { - c.dismissTrackedToolFeedbackMessage(ctx, msg.ChatID, trackedMsgID) - } - return []string{msgID}, nil + return nil, nil } // If text also fails, return the text error return nil, textErr @@ -254,23 +210,6 @@ func (c *FeishuChannel) EditMessage(ctx context.Context, chatID, messageID, cont return nil } -// DeleteMessage implements channels.MessageDeleter. -func (c *FeishuChannel) DeleteMessage(ctx context.Context, chatID, messageID string) error { - req := larkim.NewDeleteMessageReqBuilder(). - MessageId(messageID). - Build() - - resp, err := c.client.Im.V1.Message.Delete(ctx, req) - if err != nil { - return fmt.Errorf("feishu delete: %w", err) - } - if !resp.Success() { - c.invalidateTokenOnAuthError(resp.Code) - return fmt.Errorf("feishu delete api error (code=%d msg=%s)", resp.Code, resp.Msg) - } - return nil -} - // SendPlaceholder implements channels.PlaceholderCapable. // Sends an interactive card with placeholder text and returns its message ID. func (c *FeishuChannel) SendPlaceholder(ctx context.Context, chatID string) (string, error) { @@ -312,81 +251,6 @@ func (c *FeishuChannel) SendPlaceholder(ctx context.Context, chatID string) (str return "", nil } -func outboundMessageIsToolFeedback(msg bus.OutboundMessage) bool { - if len(msg.Context.Raw) == 0 { - return false - } - return strings.EqualFold(strings.TrimSpace(msg.Context.Raw["message_kind"]), "tool_feedback") -} - -func (c *FeishuChannel) currentToolFeedbackMessage(chatID string) (string, bool) { - if c.progress == nil { - return "", false - } - return c.progress.Current(chatID) -} - -func (c *FeishuChannel) takeToolFeedbackMessage(chatID string) (string, string, bool) { - if c.progress == nil { - return "", "", false - } - return c.progress.Take(chatID) -} - -func (c *FeishuChannel) RecordToolFeedbackMessage(chatID, messageID, content string) { - if c.progress == nil { - return - } - c.progress.Record(chatID, messageID, content) -} - -func (c *FeishuChannel) ClearToolFeedbackMessage(chatID string) { - if c.progress == nil { - return - } - c.progress.Clear(chatID) -} - -func (c *FeishuChannel) DismissToolFeedbackMessage(ctx context.Context, chatID string) { - msgID, ok := c.currentToolFeedbackMessage(chatID) - if !ok { - return - } - c.dismissTrackedToolFeedbackMessage(ctx, chatID, msgID) -} - -func (c *FeishuChannel) dismissTrackedToolFeedbackMessage(ctx context.Context, chatID, messageID string) { - if strings.TrimSpace(chatID) == "" || strings.TrimSpace(messageID) == "" { - return - } - c.ClearToolFeedbackMessage(chatID) - _ = c.DeleteMessage(ctx, chatID, messageID) -} - -func (c *FeishuChannel) finalizeTrackedToolFeedbackMessage( - ctx context.Context, - chatID string, - content string, - editFn func(context.Context, string, string, string) error, -) ([]string, bool) { - msgID, baseContent, ok := c.takeToolFeedbackMessage(chatID) - if !ok || editFn == nil { - return nil, false - } - if err := editFn(ctx, chatID, msgID, content); err != nil { - c.RecordToolFeedbackMessage(chatID, msgID, baseContent) - return nil, false - } - return []string{msgID}, true -} - -func (c *FeishuChannel) FinalizeToolFeedbackMessage(ctx context.Context, msg bus.OutboundMessage) ([]string, bool) { - if outboundMessageIsToolFeedback(msg) { - return nil, false - } - return c.finalizeTrackedToolFeedbackMessage(ctx, msg.ChatID, msg.Content, c.EditMessage) -} - // ReactToMessage implements channels.ReactionCapable. // Adds a reaction (randomly chosen from config) and returns an undo function to remove it. func (c *FeishuChannel) ReactToMessage(ctx context.Context, chatID, messageID string) (func(), error) { @@ -459,7 +323,6 @@ func (c *FeishuChannel) SendMedia(ctx context.Context, msg bus.OutboundMediaMess if !c.IsRunning() { return nil, channels.ErrNotRunning } - trackedMsgID, hasTrackedMsg := c.currentToolFeedbackMessage(msg.ChatID) if msg.ChatID == "" { return nil, fmt.Errorf("chat ID is empty: %w", channels.ErrSendFailed) @@ -476,10 +339,6 @@ func (c *FeishuChannel) SendMedia(ctx context.Context, msg bus.OutboundMediaMess } } - if hasTrackedMsg { - c.dismissTrackedToolFeedbackMessage(ctx, msg.ChatID, trackedMsgID) - } - return nil, nil } @@ -942,7 +801,7 @@ func appendMediaTags(content, messageType string, mediaRefs []string) string { } // sendCard sends an interactive card message to a chat. -func (c *FeishuChannel) sendCard(ctx context.Context, chatID, cardContent string) (string, error) { +func (c *FeishuChannel) sendCard(ctx context.Context, chatID, cardContent string) error { req := larkim.NewCreateMessageReqBuilder(). ReceiveIdType(larkim.ReceiveIdTypeChatId). Body(larkim.NewCreateMessageReqBodyBuilder(). @@ -954,26 +813,23 @@ func (c *FeishuChannel) sendCard(ctx context.Context, chatID, cardContent string resp, err := c.client.Im.V1.Message.Create(ctx, req) if err != nil { - return "", fmt.Errorf("feishu send card: %w", channels.ErrTemporary) + return fmt.Errorf("feishu send card: %w", channels.ErrTemporary) } if !resp.Success() { c.invalidateTokenOnAuthError(resp.Code) - return "", fmt.Errorf("feishu api error (code=%d msg=%s): %w", resp.Code, resp.Msg, channels.ErrTemporary) + return fmt.Errorf("feishu api error (code=%d msg=%s): %w", resp.Code, resp.Msg, channels.ErrTemporary) } logger.DebugCF("feishu", "Feishu card message sent", map[string]any{ "chat_id": chatID, }) - if resp.Data != nil && resp.Data.MessageId != nil { - return *resp.Data.MessageId, nil - } - return "", nil + return nil } // sendText sends a plain text message to a chat (fallback when card fails). -func (c *FeishuChannel) sendText(ctx context.Context, chatID, text string) (string, error) { +func (c *FeishuChannel) sendText(ctx context.Context, chatID, text string) error { content, _ := json.Marshal(map[string]string{"text": text}) req := larkim.NewCreateMessageReqBuilder(). @@ -987,21 +843,18 @@ func (c *FeishuChannel) sendText(ctx context.Context, chatID, text string) (stri resp, err := c.client.Im.V1.Message.Create(ctx, req) if err != nil { - return "", fmt.Errorf("feishu send text: %w", channels.ErrTemporary) + return fmt.Errorf("feishu send text: %w", channels.ErrTemporary) } if !resp.Success() { - return "", fmt.Errorf("feishu text api error (code=%d msg=%s): %w", resp.Code, resp.Msg, channels.ErrTemporary) + return fmt.Errorf("feishu text api error (code=%d msg=%s): %w", resp.Code, resp.Msg, channels.ErrTemporary) } logger.DebugCF("feishu", "Feishu text message sent (fallback)", map[string]any{ "chat_id": chatID, }) - if resp.Data != nil && resp.Data.MessageId != nil { - return *resp.Data.MessageId, nil - } - return "", nil + return nil } // sendImage uploads an image and sends it as a message. diff --git a/pkg/channels/feishu/feishu_64_test.go b/pkg/channels/feishu/feishu_64_test.go index 0bdac0352..9010abf69 100644 --- a/pkg/channels/feishu/feishu_64_test.go +++ b/pkg/channels/feishu/feishu_64_test.go @@ -3,13 +3,9 @@ package feishu import ( - "context" - "errors" "testing" larkim "github.com/larksuite/oapi-sdk-go/v3/service/im/v1" - - "github.com/sipeed/picoclaw/pkg/channels" ) func TestExtractContent(t *testing.T) { @@ -283,84 +279,3 @@ func TestExtractFeishuSenderID(t *testing.T) { }) } } - -func TestFinalizeTrackedToolFeedbackMessage_ClearAfterSuccessfulEdit(t *testing.T) { - ch := &FeishuChannel{ - progress: channels.NewToolFeedbackAnimator(nil), - } - ch.RecordToolFeedbackMessage("chat-1", "msg-1", "🔧 `read_file`") - - msgIDs, handled := ch.finalizeTrackedToolFeedbackMessage( - context.Background(), - "chat-1", - "final reply", - func(_ context.Context, chatID, messageID, content string) error { - if chatID != "chat-1" || messageID != "msg-1" || content != "final reply" { - t.Fatalf("unexpected edit args: %s %s %s", chatID, messageID, content) - } - return nil - }, - ) - if !handled { - t.Fatal("expected finalizeTrackedToolFeedbackMessage to handle tracked message") - } - if len(msgIDs) != 1 || msgIDs[0] != "msg-1" { - t.Fatalf("unexpected msgIDs: %v", msgIDs) - } - if _, ok := ch.currentToolFeedbackMessage("chat-1"); ok { - t.Fatal("expected tracked tool feedback to be cleared after successful edit") - } -} - -func TestFinalizeTrackedToolFeedbackMessage_StopsTrackingBeforeEdit(t *testing.T) { - ch := &FeishuChannel{ - progress: channels.NewToolFeedbackAnimator(nil), - } - ch.RecordToolFeedbackMessage("chat-1", "msg-1", "🔧 `read_file`") - - msgIDs, handled := ch.finalizeTrackedToolFeedbackMessage( - context.Background(), - "chat-1", - "final reply", - func(_ context.Context, chatID, messageID, content string) error { - if _, ok := ch.currentToolFeedbackMessage(chatID); ok { - t.Fatal("expected tracked tool feedback to be stopped before edit") - } - if chatID != "chat-1" || messageID != "msg-1" || content != "final reply" { - t.Fatalf("unexpected edit args: %s %s %s", chatID, messageID, content) - } - return nil - }, - ) - if !handled { - t.Fatal("expected finalizeTrackedToolFeedbackMessage to handle tracked message") - } - if len(msgIDs) != 1 || msgIDs[0] != "msg-1" { - t.Fatalf("unexpected msgIDs: %v", msgIDs) - } -} - -func TestFinalizeTrackedToolFeedbackMessage_EditFailureKeepsTrackedMessage(t *testing.T) { - ch := &FeishuChannel{ - progress: channels.NewToolFeedbackAnimator(nil), - } - ch.RecordToolFeedbackMessage("chat-1", "msg-1", "🔧 `read_file`") - - msgIDs, handled := ch.finalizeTrackedToolFeedbackMessage( - context.Background(), - "chat-1", - "final reply", - func(context.Context, string, string, string) error { - return errors.New("edit failed") - }, - ) - if handled { - t.Fatal("expected finalizeTrackedToolFeedbackMessage to report unhandled on edit failure") - } - if len(msgIDs) != 0 { - t.Fatalf("unexpected msgIDs: %v", msgIDs) - } - if msgID, ok := ch.currentToolFeedbackMessage("chat-1"); !ok || msgID != "msg-1" { - t.Fatalf("expected tracked tool feedback to remain after failed edit, got (%q, %v)", msgID, ok) - } -} diff --git a/pkg/channels/manager.go b/pkg/channels/manager.go index 6aec966d6..928676cbc 100644 --- a/pkg/channels/manager.go +++ b/pkg/channels/manager.go @@ -14,7 +14,6 @@ import ( "net" "net/http" "sort" - "strings" "sync" "time" @@ -26,7 +25,6 @@ import ( "github.com/sipeed/picoclaw/pkg/health" "github.com/sipeed/picoclaw/pkg/logger" "github.com/sipeed/picoclaw/pkg/media" - "github.com/sipeed/picoclaw/pkg/utils" ) const ( @@ -98,15 +96,6 @@ type Manager struct { channelHashes map[string]string // channel name → config hash } -type toolFeedbackMessageTracker interface { - RecordToolFeedbackMessage(chatID, messageID, content string) - ClearToolFeedbackMessage(chatID string) -} - -type toolFeedbackMessageCleaner interface { - DismissToolFeedbackMessage(ctx context.Context, chatID string) -} - type asyncTask struct { cancel context.CancelFunc } @@ -119,13 +108,6 @@ func outboundMessageChatID(msg bus.OutboundMessage) string { return msg.ChatID } -func outboundMessageIsToolFeedback(msg bus.OutboundMessage) bool { - if len(msg.Context.Raw) == 0 { - return false - } - return strings.EqualFold(strings.TrimSpace(msg.Context.Raw["message_kind"]), "tool_feedback") -} - func outboundMediaChannel(msg bus.OutboundMediaMessage) string { return msg.Context.Channel } @@ -134,16 +116,6 @@ func outboundMediaChatID(msg bus.OutboundMediaMessage) string { return msg.ChatID } -func dismissTrackedToolFeedbackMessage(ctx context.Context, ch Channel, chatID string) { - if cleaner, ok := ch.(toolFeedbackMessageCleaner); ok { - cleaner.DismissToolFeedbackMessage(ctx, chatID) - return - } - if tracker, ok := ch.(toolFeedbackMessageTracker); ok { - tracker.ClearToolFeedbackMessage(chatID) - } -} - // RecordPlaceholder registers a placeholder message for later editing. // Implements PlaceholderRecorder. func (m *Manager) RecordPlaceholder(channel, chatID, placeholderID string) { @@ -224,19 +196,7 @@ func (m *Manager) preSend(ctx context.Context, name string, msg bus.OutboundMess } } - isToolFeedback := outboundMessageIsToolFeedback(msg) - - // 3. If a stream already finalized this chat, stale tool feedback must be - // dropped without consuming the final-response marker. Streaming finalization - // bypasses the worker queue, so older queued feedback can arrive before the - // normal final outbound message that cleans up the marker and placeholder. - if isToolFeedback { - if _, loaded := m.streamActive.Load(key); loaded { - return nil, true - } - } - - // 4. If a stream already finalized this message, delete the placeholder and skip send + // 3. If a stream already finalized this message, delete the placeholder and skip send if _, loaded := m.streamActive.LoadAndDelete(key); loaded { if v, loaded := m.placeholders.LoadAndDelete(key); loaded { if entry, ok := v.(placeholderEntry); ok && entry.id != "" { @@ -248,26 +208,14 @@ func (m *Manager) preSend(ctx context.Context, name string, msg bus.OutboundMess } } } - if !isToolFeedback { - dismissTrackedToolFeedbackMessage(ctx, ch, chatID) - } return nil, true } - // 5. Try editing placeholder + // 4. Try editing placeholder if v, loaded := m.placeholders.LoadAndDelete(key); loaded { if entry, ok := v.(placeholderEntry); ok && entry.id != "" { if editor, ok := ch.(MessageEditor); ok { - content := msg.Content - if isToolFeedback { - content = InitialAnimatedToolFeedbackContent(msg.Content) - } - if err := editor.EditMessage(ctx, chatID, entry.id, content); err == nil { - if tracker, ok := ch.(toolFeedbackMessageTracker); ok && isToolFeedback { - tracker.RecordToolFeedbackMessage(chatID, entry.id, msg.Content) - } else if !isToolFeedback { - dismissTrackedToolFeedbackMessage(ctx, ch, chatID) - } + if err := editor.EditMessage(ctx, chatID, entry.id, msg.Content); err == nil { return []string{entry.id}, true } // edit failed → fall through to normal Send @@ -364,27 +312,22 @@ func (m *Manager) GetStreamer(ctx context.Context, channelName, chatID string) ( // Mark streamActive on Finalize so preSend knows to clean up the placeholder key := channelName + ":" + chatID return &finalizeHookStreamer{ - Streamer: streamer, - onFinalize: func(finalizeCtx context.Context) { - dismissTrackedToolFeedbackMessage(finalizeCtx, ch, chatID) - m.streamActive.Store(key, true) - }, + Streamer: streamer, + onFinalize: func() { m.streamActive.Store(key, true) }, }, true } // finalizeHookStreamer wraps a Streamer to run a hook on Finalize. type finalizeHookStreamer struct { Streamer - onFinalize func(context.Context) + onFinalize func() } func (s *finalizeHookStreamer) Finalize(ctx context.Context, content string) error { if err := s.Streamer.Finalize(ctx, content); err != nil { return err } - if s.onFinalize != nil { - s.onFinalize(ctx) - } + s.onFinalize() return nil } @@ -826,21 +769,18 @@ func (m *Manager) runWorker(ctx context.Context, name string, w *channelWorker) // Collect all message chunks to send var chunks []string - // Step 1: Try marker-based splitting if enabled. - // Tool feedback must stay a single message, so it skips marker splitting. - if m.config != nil && m.config.Agents.Defaults.SplitOnMarker && !outboundMessageIsToolFeedback(msg) { + // Step 1: Try marker-based splitting if enabled + if m.config != nil && m.config.Agents.Defaults.SplitOnMarker { if markerChunks := SplitByMarker(msg.Content); len(markerChunks) > 1 { for _, chunk := range markerChunks { - chunkMsg := msg - chunkMsg.Content = chunk - chunks = append(chunks, splitOutboundMessageContent(chunkMsg, maxLen)...) + chunks = append(chunks, splitByLength(chunk, maxLen)...) } } } // Step 2: Fallback to length-based splitting if no chunks from marker if len(chunks) == 0 { - chunks = splitOutboundMessageContent(msg, maxLen) + chunks = splitByLength(msg.Content, maxLen) } // Step 3: Send all chunks @@ -855,25 +795,12 @@ func (m *Manager) runWorker(ctx context.Context, name string, w *channelWorker) } } -// splitOutboundMessageContent splits regular outbound content by maxLen, but -// keeps tool feedback in a single message by truncating the explanation body. -func splitOutboundMessageContent(msg bus.OutboundMessage, maxLen int) []string { - if maxLen > 0 { - if outboundMessageIsToolFeedback(msg) { - animationSafeLen := maxLen - MaxToolFeedbackAnimationFrameLength() - if animationSafeLen <= 0 { - animationSafeLen = maxLen - } - if len([]rune(msg.Content)) > animationSafeLen { - return []string{utils.FitToolFeedbackMessage(msg.Content, animationSafeLen)} - } - return []string{msg.Content} - } - if len([]rune(msg.Content)) > maxLen { - return SplitMessage(msg.Content, maxLen) - } +// splitByLength splits content by maxLen if needed, otherwise returns single chunk. +func splitByLength(content string, maxLen int) []string { + if maxLen > 0 && len([]rune(content)) > maxLen { + return SplitMessage(content, maxLen) } - return []string{msg.Content} + return []string{content} } // sendWithRetry sends a message through the channel with rate limiting and @@ -1337,16 +1264,13 @@ func (m *Manager) SendMessage(ctx context.Context, msg bus.OutboundMessage) erro if mlp, ok := w.ch.(MessageLengthProvider); ok { maxLen = mlp.MaxMessageLength() } - if chunks := splitOutboundMessageContent(msg, maxLen); len(chunks) > 1 { - for _, chunk := range chunks { + if maxLen > 0 && len([]rune(msg.Content)) > maxLen { + for _, chunk := range SplitMessage(msg.Content, maxLen) { chunkMsg := msg chunkMsg.Content = chunk m.sendWithRetry(ctx, channelName, w, chunkMsg) } } else { - if len(chunks) == 1 { - msg.Content = chunks[0] - } m.sendWithRetry(ctx, channelName, w, msg) } return nil diff --git a/pkg/channels/manager_test.go b/pkg/channels/manager_test.go index 4f6a7dcf4..881993d9c 100644 --- a/pkg/channels/manager_test.go +++ b/pkg/channels/manager_test.go @@ -13,8 +13,6 @@ import ( "golang.org/x/time/rate" "github.com/sipeed/picoclaw/pkg/bus" - "github.com/sipeed/picoclaw/pkg/config" - "github.com/sipeed/picoclaw/pkg/utils" ) // mockChannel is a test double that delegates Send to a configurable function. @@ -78,9 +76,8 @@ func (m *mockMediaChannel) SendMedia(ctx context.Context, msg bus.OutboundMediaM type mockDeletingMediaChannel struct { mockMediaChannel - deleteCalls int - dismissedChatID string - lastDeleted struct { + deleteCalls int + lastDeleted struct { chatID string messageID string } @@ -97,37 +94,6 @@ func (m *mockDeletingMediaChannel) DeleteMessage( return nil } -func (m *mockDeletingMediaChannel) DismissToolFeedbackMessage(_ context.Context, chatID string) { - m.dismissedChatID = chatID -} - -type mockStreamer struct { - finalizeFn func(context.Context, string) error -} - -func (m *mockStreamer) Update(context.Context, string) error { return nil } - -func (m *mockStreamer) Finalize(ctx context.Context, content string) error { - if m.finalizeFn != nil { - return m.finalizeFn(ctx, content) - } - return nil -} - -func (m *mockStreamer) Cancel(context.Context) {} - -type mockStreamingChannel struct { - mockMessageEditor - streamer Streamer -} - -func (m *mockStreamingChannel) BeginStream(context.Context, string) (Streamer, error) { - if m.streamer == nil { - return nil, errors.New("missing streamer") - } - return m.streamer, nil -} - // newTestManager creates a minimal Manager suitable for unit tests. func newTestManager() *Manager { return &Manager{ @@ -749,43 +715,13 @@ func TestSendWithRetry_ExponentialBackoff(t *testing.T) { // mockMessageEditor is a channel that supports MessageEditor. type mockMessageEditor struct { mockChannel - editFn func(ctx context.Context, chatID, messageID, content string) error - finalizeFn func(ctx context.Context, msg bus.OutboundMessage) ([]string, bool) - finalizeCalled bool - recordedChatID string - recordedMessageID string - clearedChatID string - dismissedChatID string + editFn func(ctx context.Context, chatID, messageID, content string) error } func (m *mockMessageEditor) EditMessage(ctx context.Context, chatID, messageID, content string) error { return m.editFn(ctx, chatID, messageID, content) } -func (m *mockMessageEditor) RecordToolFeedbackMessage(chatID, messageID, _ string) { - m.recordedChatID = chatID - m.recordedMessageID = messageID -} - -func (m *mockMessageEditor) ClearToolFeedbackMessage(chatID string) { - m.clearedChatID = chatID -} - -func (m *mockMessageEditor) DismissToolFeedbackMessage(_ context.Context, chatID string) { - m.dismissedChatID = chatID -} - -func (m *mockMessageEditor) FinalizeToolFeedbackMessage( - ctx context.Context, - msg bus.OutboundMessage, -) ([]string, bool) { - m.finalizeCalled = true - if m.finalizeFn == nil { - return nil, false - } - return m.finalizeFn(ctx, msg) -} - func TestPreSend_PlaceholderEditSuccess(t *testing.T) { m := newTestManager() var sendCalled bool @@ -830,360 +766,6 @@ func TestPreSend_PlaceholderEditSuccess(t *testing.T) { } } -func TestPreSend_ToolFeedbackPlaceholderEditRecordsTrackedMessage(t *testing.T) { - m := newTestManager() - - ch := &mockMessageEditor{ - editFn: func(_ context.Context, chatID, messageID, content string) error { - if chatID != "123" || messageID != "456" || content != "hello" { - t.Fatalf("unexpected edit args: %s %s %s", chatID, messageID, content) - } - return nil - }, - } - - m.RecordPlaceholder("test", "123", "456") - - msg := testOutboundMessage(bus.OutboundMessage{ - Channel: "test", - ChatID: "123", - Content: "hello", - Context: bus.InboundContext{ - Channel: "test", - ChatID: "123", - Raw: map[string]string{ - "message_kind": "tool_feedback", - }, - }, - }) - _, edited := m.preSend(context.Background(), "test", msg, ch) - if !edited { - t.Fatal("expected preSend to edit placeholder") - } - if ch.recordedChatID != "123" || ch.recordedMessageID != "456" { - t.Fatalf("expected tracked message 123/456, got %q/%q", ch.recordedChatID, ch.recordedMessageID) - } -} - -func TestPreSend_NonToolFeedbackLeavesTrackedMessageForChannelSend(t *testing.T) { - m := newTestManager() - ch := &mockMessageEditor{} - - msg := testOutboundMessage(bus.OutboundMessage{ - Channel: "test", - ChatID: "123", - Content: "final reply", - Context: bus.InboundContext{ - Channel: "test", - ChatID: "123", - }, - }) - - _, edited := m.preSend(context.Background(), "test", msg, ch) - if edited { - t.Fatal("expected preSend to fall through when no placeholder exists") - } - if ch.dismissedChatID != "" { - t.Fatalf("expected tracked tool feedback cleanup to be deferred to channel send, got %q", ch.dismissedChatID) - } -} - -func TestPreSend_NonToolFeedbackDefersTrackedMessageFinalizationToChannelSend(t *testing.T) { - m := newTestManager() - ch := &mockMessageEditor{ - finalizeFn: func(_ context.Context, msg bus.OutboundMessage) ([]string, bool) { - if msg.ChatID != "123" || msg.Content != "final reply" { - t.Fatalf("unexpected finalize msg: %+v", msg) - } - return []string{"tool-msg-1"}, true - }, - } - - msg := testOutboundMessage(bus.OutboundMessage{ - Channel: "test", - ChatID: "123", - Content: "final reply", - Context: bus.InboundContext{ - Channel: "test", - ChatID: "123", - }, - }) - - msgIDs, handled := m.preSend(context.Background(), "test", msg, ch) - if handled { - t.Fatalf("expected preSend to defer to channel Send, got msgIDs=%v", msgIDs) - } - if len(msgIDs) != 0 { - t.Fatalf("expected no msgIDs from preSend, got %v", msgIDs) - } - if ch.dismissedChatID != "" { - t.Fatalf("expected tracked cleanup to remain in channel Send, got %q", ch.dismissedChatID) - } - if ch.finalizeCalled { - t.Fatal("expected preSend to skip channel tool feedback finalization") - } -} - -func TestPreSend_StaleToolFeedbackDoesNotConsumeStreamActiveMarker(t *testing.T) { - m := newTestManager() - m.streamActive.Store("test:123", true) - m.RecordPlaceholder("test", "123", "placeholder-1") - - var editedContent string - ch := &mockMessageEditor{ - editFn: func(_ context.Context, chatID, messageID, content string) error { - if chatID != "123" || messageID != "placeholder-1" { - t.Fatalf("unexpected edit target: %s/%s", chatID, messageID) - } - editedContent = content - return nil - }, - } - - toolFeedback := testOutboundMessage(bus.OutboundMessage{ - Channel: "test", - ChatID: "123", - Content: "🔧 `read_file`\nReading config", - Context: bus.InboundContext{ - Channel: "test", - ChatID: "123", - Raw: map[string]string{ - "message_kind": "tool_feedback", - }, - }, - }) - - msgIDs, handled := m.preSend(context.Background(), "test", toolFeedback, ch) - if !handled { - t.Fatal("expected stale tool feedback to be dropped after stream finalize") - } - if len(msgIDs) != 0 { - t.Fatalf("expected no delivered message IDs for stale feedback, got %v", msgIDs) - } - if _, ok := m.streamActive.Load("test:123"); !ok { - t.Fatal("expected streamActive marker to remain for the final outbound message") - } - if _, ok := m.placeholders.Load("test:123"); !ok { - t.Fatal("expected placeholder cleanup to remain deferred to the final outbound message") - } - if ch.editedMessages != 0 { - t.Fatalf("expected no placeholder edit for stale feedback, got %d edits", ch.editedMessages) - } - - finalMsg := testOutboundMessage(bus.OutboundMessage{ - Channel: "test", - ChatID: "123", - Content: "final streamed reply", - Context: bus.InboundContext{ - Channel: "test", - ChatID: "123", - }, - }) - - _, handled = m.preSend(context.Background(), "test", finalMsg, ch) - if !handled { - t.Fatal("expected final outbound message to consume streamActive marker") - } - if _, ok := m.streamActive.Load("test:123"); ok { - t.Fatal("expected streamActive marker to be cleared by final outbound message") - } - if _, ok := m.placeholders.Load("test:123"); ok { - t.Fatal("expected placeholder to be cleaned up by final outbound message") - } - if editedContent != "final streamed reply" { - t.Fatalf("editedContent = %q, want final streamed reply", editedContent) - } -} - -func TestPreSendMedia_LeavesTrackedMessageForChannelSend(t *testing.T) { - m := newTestManager() - ch := &mockDeletingMediaChannel{} - - m.preSendMedia(context.Background(), "test", bus.OutboundMediaMessage{ - ChatID: "123", - Context: bus.InboundContext{ - Channel: "test", - ChatID: "123", - }, - }, ch) - - if ch.dismissedChatID != "" { - t.Fatalf( - "expected tracked tool feedback cleanup to be deferred to channel media send, got %q", - ch.dismissedChatID, - ) - } -} - -func TestSplitOutboundMessageContent_ToolFeedbackTruncatesInsteadOfSplitting(t *testing.T) { - msg := testOutboundMessage(bus.OutboundMessage{ - Channel: "test", - ChatID: "123", - Content: "\U0001f527 `read_file`\nRead README.md first to confirm the current project structure before editing the config example.", - Context: bus.InboundContext{ - Channel: "test", - ChatID: "123", - Raw: map[string]string{ - "message_kind": "tool_feedback", - }, - }, - }) - - chunks := splitOutboundMessageContent(msg, 40) - if len(chunks) != 1 { - t.Fatalf("len(chunks) = %d, want 1", len(chunks)) - } - want := utils.FitToolFeedbackMessage(msg.Content, 40-MaxToolFeedbackAnimationFrameLength()) - if chunks[0] != want { - t.Fatalf("chunk = %q, want %q", chunks[0], want) - } -} - -func TestSplitOutboundMessageContent_ToolFeedbackReservesAnimationFrame(t *testing.T) { - msg := testOutboundMessage(bus.OutboundMessage{ - Channel: "test", - ChatID: "123", - Content: "🔧 `read_file`\n1234567890", - Context: bus.InboundContext{ - Channel: "test", - ChatID: "123", - Raw: map[string]string{ - "message_kind": "tool_feedback", - }, - }, - }) - - chunks := splitOutboundMessageContent(msg, len([]rune(msg.Content))) - if len(chunks) != 1 { - t.Fatalf("len(chunks) = %d, want 1", len(chunks)) - } - - animated := formatAnimatedToolFeedbackContent(chunks[0], strings.Repeat(".", MaxToolFeedbackAnimationFrameLength())) - if got, maxLen := len([]rune(animated)), len([]rune(msg.Content)); got > maxLen { - t.Fatalf("animated len = %d, want <= %d; content=%q", got, maxLen, animated) - } -} - -func TestGetStreamer_FinalizeDismissesTrackedToolFeedback(t *testing.T) { - m := newTestManager() - ch := &mockStreamingChannel{ - mockMessageEditor: mockMessageEditor{}, - streamer: &mockStreamer{ - finalizeFn: func(_ context.Context, content string) error { - if content != "final reply" { - t.Fatalf("unexpected finalize content: %q", content) - } - return nil - }, - }, - } - m.channels["test"] = ch - - streamer, ok := m.GetStreamer(context.Background(), "test", "123") - if !ok { - t.Fatal("expected streamer to be available") - } - if err := streamer.Finalize(context.Background(), "final reply"); err != nil { - t.Fatalf("Finalize() error = %v", err) - } - if ch.dismissedChatID != "123" { - t.Fatalf("expected tracked tool feedback to be dismissed for chat 123, got %q", ch.dismissedChatID) - } - if _, ok := m.streamActive.Load("test:123"); !ok { - t.Fatal("expected streamActive marker to be recorded after finalize") - } -} - -func TestGetStreamer_FinalizeFailureDoesNotDismissTrackedToolFeedback(t *testing.T) { - m := newTestManager() - ch := &mockStreamingChannel{ - mockMessageEditor: mockMessageEditor{}, - streamer: &mockStreamer{ - finalizeFn: func(context.Context, string) error { - return errors.New("finalize failed") - }, - }, - } - m.channels["test"] = ch - - streamer, ok := m.GetStreamer(context.Background(), "test", "123") - if !ok { - t.Fatal("expected streamer to be available") - } - if err := streamer.Finalize(context.Background(), "final reply"); err == nil { - t.Fatal("expected Finalize() to fail") - } - if ch.dismissedChatID != "" { - t.Fatalf("expected no tool feedback dismissal on finalize failure, got %q", ch.dismissedChatID) - } - if _, ok := m.streamActive.Load("test:123"); ok { - t.Fatal("expected no streamActive marker after finalize failure") - } -} - -func TestRunWorker_ToolFeedbackSkipsMarkerSplitting(t *testing.T) { - m := newTestManager() - m.config = &config.Config{ - Agents: config.AgentsConfig{ - Defaults: config.AgentDefaults{ - SplitOnMarker: true, - }, - }, - } - - var ( - mu sync.Mutex - received []string - ) - ch := &mockChannelWithLength{ - mockChannel: mockChannel{ - sendFn: func(_ context.Context, msg bus.OutboundMessage) error { - mu.Lock() - received = append(received, msg.Content) - mu.Unlock() - return nil - }, - }, - maxLen: 200, - } - - w := &channelWorker{ - ch: ch, - queue: make(chan bus.OutboundMessage, 1), - done: make(chan struct{}), - limiter: rate.NewLimiter(rate.Inf, 1), - } - - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - go m.runWorker(ctx, "test", w) - - content := "🔧 `read_file`\nRead current config first.<|[SPLIT]|>Then update the example." - w.queue <- testOutboundMessage(bus.OutboundMessage{ - Channel: "test", - ChatID: "123", - Content: content, - Context: bus.InboundContext{ - Channel: "test", - ChatID: "123", - Raw: map[string]string{ - "message_kind": "tool_feedback", - }, - }, - }) - - time.Sleep(100 * time.Millisecond) - - mu.Lock() - defer mu.Unlock() - if len(received) != 1 { - t.Fatalf("len(received) = %d, want 1", len(received)) - } - if received[0] != content { - t.Fatalf("received[0] = %q, want %q", received[0], content) - } -} - func TestPreSend_PlaceholderEditFails_FallsThrough(t *testing.T) { m := newTestManager() diff --git a/pkg/channels/matrix/matrix.go b/pkg/channels/matrix/matrix.go index 04599d6d2..40e1b0a36 100644 --- a/pkg/channels/matrix/matrix.go +++ b/pkg/channels/matrix/matrix.go @@ -46,13 +46,6 @@ const ( var matrixMentionHrefRegexp = regexp.MustCompile(`(?i)]+href=["']([^"']+)["']`) -func outboundMessageIsToolFeedback(msg bus.OutboundMessage) bool { - if len(msg.Context.Raw) == 0 { - return false - } - return strings.EqualFold(strings.TrimSpace(msg.Context.Raw["message_kind"]), "tool_feedback") -} - type roomKindCacheEntry struct { isGroup bool expiresAt time.Time @@ -199,7 +192,6 @@ type MatrixChannel struct { cryptoHelper *cryptohelper.CryptoHelper cryptoDbPath string - progress *channels.ToolFeedbackAnimator } func NewMatrixChannel( @@ -244,7 +236,7 @@ func NewMatrixChannel( channels.WithReasoningChannelID(bc.ReasoningChannelID), ) - ch := &MatrixChannel{ + return &MatrixChannel{ BaseChannel: base, bc: bc, client: client, @@ -256,9 +248,7 @@ func NewMatrixChannel( localpartMentionR: localpartMentionRegexp(matrixLocalpart(client.UserID)), typingMu: sync.Mutex{}, cryptoDbPath: cryptoDatabasePath, - } - ch.progress = channels.NewToolFeedbackAnimator(ch.EditMessage) - return ch, nil + }, nil } func (c *MatrixChannel) Start(ctx context.Context) error { @@ -307,9 +297,6 @@ func (c *MatrixChannel) Stop(ctx context.Context) error { c.cancel() } c.stopTypingSessions(ctx) - if c.progress != nil { - c.progress.StopAll() - } // Close crypto helper if initialized if c.cryptoHelper != nil { @@ -411,36 +398,11 @@ func (c *MatrixChannel) Send(ctx context.Context, msg bus.OutboundMessage) ([]st return nil, nil } - isToolFeedback := outboundMessageIsToolFeedback(msg) - if isToolFeedback { - if msgID, handled, err := c.progress.Update(ctx, msg.ChatID, content); handled { - if err != nil { - return nil, err - } - return []string{msgID}, nil - } - } - trackedMsgID, hasTrackedMsg := c.currentToolFeedbackMessage(msg.ChatID) - if !isToolFeedback { - if msgIDs, handled := c.FinalizeToolFeedbackMessage(ctx, msg); handled { - return msgIDs, nil - } - } - if isToolFeedback { - content = channels.InitialAnimatedToolFeedbackContent(content) - } - resp, err := c.client.SendMessageEvent(ctx, roomID, event.EventMessage, c.messageContent(content)) if err != nil { return nil, fmt.Errorf("matrix send: %w", channels.ErrTemporary) } - msgID := resp.EventID.String() - if isToolFeedback { - c.RecordToolFeedbackMessage(msg.ChatID, msgID, msg.Content) - } else if hasTrackedMsg { - c.dismissTrackedToolFeedbackMessage(ctx, msg.ChatID, trackedMsgID) - } - return []string{msgID}, nil + return []string{resp.EventID.String()}, nil } func (c *MatrixChannel) messageContent(text string) *event.MessageEventContent { @@ -457,8 +419,6 @@ func (c *MatrixChannel) SendMedia(ctx context.Context, msg bus.OutboundMediaMess if !c.IsRunning() { return nil, channels.ErrNotRunning } - trackedMsgID, hasTrackedMsg := c.currentToolFeedbackMessage(msg.ChatID) - sendCtx := ctx if sendCtx == nil { sendCtx = context.Background() @@ -569,10 +529,6 @@ func (c *MatrixChannel) SendMedia(ctx context.Context, msg bus.OutboundMediaMess } } - if hasTrackedMsg { - c.dismissTrackedToolFeedbackMessage(ctx, msg.ChatID, trackedMsgID) - } - return eventIDs, nil } @@ -656,89 +612,6 @@ func (c *MatrixChannel) EditMessage(ctx context.Context, chatID string, messageI return err } -// DeleteMessage implements channels.MessageDeleter. -func (c *MatrixChannel) DeleteMessage(ctx context.Context, chatID string, messageID string) error { - roomID := id.RoomID(strings.TrimSpace(chatID)) - if roomID == "" { - return fmt.Errorf("matrix room ID is empty") - } - eventID := id.EventID(strings.TrimSpace(messageID)) - if eventID == "" { - return fmt.Errorf("matrix message ID is empty") - } - - _, err := c.client.RedactEvent(ctx, roomID, eventID) - return err -} - -func (c *MatrixChannel) currentToolFeedbackMessage(chatID string) (string, bool) { - if c.progress == nil { - return "", false - } - return c.progress.Current(chatID) -} - -func (c *MatrixChannel) takeToolFeedbackMessage(chatID string) (string, string, bool) { - if c.progress == nil { - return "", "", false - } - return c.progress.Take(chatID) -} - -func (c *MatrixChannel) RecordToolFeedbackMessage(chatID, messageID, content string) { - if c.progress == nil { - return - } - c.progress.Record(chatID, messageID, content) -} - -func (c *MatrixChannel) ClearToolFeedbackMessage(chatID string) { - if c.progress == nil { - return - } - c.progress.Clear(chatID) -} - -func (c *MatrixChannel) DismissToolFeedbackMessage(ctx context.Context, chatID string) { - msgID, ok := c.currentToolFeedbackMessage(chatID) - if !ok { - return - } - c.dismissTrackedToolFeedbackMessage(ctx, chatID, msgID) -} - -func (c *MatrixChannel) dismissTrackedToolFeedbackMessage(ctx context.Context, chatID, messageID string) { - if strings.TrimSpace(chatID) == "" || strings.TrimSpace(messageID) == "" { - return - } - c.ClearToolFeedbackMessage(chatID) - _ = c.DeleteMessage(ctx, chatID, messageID) -} - -func (c *MatrixChannel) finalizeTrackedToolFeedbackMessage( - ctx context.Context, - chatID string, - content string, - editFn func(context.Context, string, string, string) error, -) ([]string, bool) { - msgID, baseContent, ok := c.takeToolFeedbackMessage(chatID) - if !ok || editFn == nil { - return nil, false - } - if err := editFn(ctx, chatID, msgID, content); err != nil { - c.RecordToolFeedbackMessage(chatID, msgID, baseContent) - return nil, false - } - return []string{msgID}, true -} - -func (c *MatrixChannel) FinalizeToolFeedbackMessage(ctx context.Context, msg bus.OutboundMessage) ([]string, bool) { - if outboundMessageIsToolFeedback(msg) { - return nil, false - } - return c.finalizeTrackedToolFeedbackMessage(ctx, msg.ChatID, msg.Content, c.EditMessage) -} - func (c *MatrixChannel) handleMemberEvent(ctx context.Context, evt *event.Event) { if !c.config.JoinOnInvite { return diff --git a/pkg/channels/matrix/matrix_test.go b/pkg/channels/matrix/matrix_test.go index 066f08059..07f08f32b 100644 --- a/pkg/channels/matrix/matrix_test.go +++ b/pkg/channels/matrix/matrix_test.go @@ -14,7 +14,6 @@ import ( "maunium.net/go/mautrix/event" "maunium.net/go/mautrix/id" - "github.com/sipeed/picoclaw/pkg/channels" "github.com/sipeed/picoclaw/pkg/config" "github.com/sipeed/picoclaw/pkg/media" ) @@ -42,34 +41,6 @@ func TestMatrixLocalpartMentionRegexp(t *testing.T) { } } -func TestFinalizeTrackedToolFeedbackMessage_StopsTrackingBeforeEdit(t *testing.T) { - ch := &MatrixChannel{ - progress: channels.NewToolFeedbackAnimator(nil), - } - ch.RecordToolFeedbackMessage("!room:matrix.org", "$event1", "🔧 `read_file`") - - msgIDs, handled := ch.finalizeTrackedToolFeedbackMessage( - context.Background(), - "!room:matrix.org", - "final reply", - func(_ context.Context, chatID, messageID, content string) error { - if _, ok := ch.currentToolFeedbackMessage(chatID); ok { - t.Fatal("expected tracked tool feedback to be stopped before edit") - } - if chatID != "!room:matrix.org" || messageID != "$event1" || content != "final reply" { - t.Fatalf("unexpected edit args: %s %s %s", chatID, messageID, content) - } - return nil - }, - ) - if !handled { - t.Fatal("expected finalizeTrackedToolFeedbackMessage to handle tracked message") - } - if len(msgIDs) != 1 || msgIDs[0] != "$event1" { - t.Fatalf("finalizeTrackedToolFeedbackMessage() ids = %v, want [$event1]", msgIDs) - } -} - func TestStripUserMention(t *testing.T) { userID := id.UserID("@picoclaw:matrix.org") diff --git a/pkg/channels/pico/pico.go b/pkg/channels/pico/pico.go index 5d7bd0fa1..f998712c8 100644 --- a/pkg/channels/pico/pico.go +++ b/pkg/channels/pico/pico.go @@ -46,13 +46,6 @@ func outboundMessageIsThought(msg bus.OutboundMessage) bool { return strings.EqualFold(strings.TrimSpace(msg.Context.Raw["message_kind"]), MessageKindThought) } -func outboundMessageIsToolFeedback(msg bus.OutboundMessage) bool { - if len(msg.Context.Raw) == 0 { - return false - } - return strings.EqualFold(strings.TrimSpace(msg.Context.Raw["message_kind"]), "tool_feedback") -} - // writeJSON sends a JSON message to the connection with write locking. func (pc *picoConn) writeJSON(v any) error { if pc.closed.Load() { @@ -85,7 +78,6 @@ type PicoChannel struct { connsMu sync.RWMutex ctx context.Context cancel context.CancelFunc - progress *channels.ToolFeedbackAnimator } // NewPicoChannel creates a new Pico Protocol channel. @@ -114,7 +106,7 @@ func NewPicoChannel( return false } - ch := &PicoChannel{ + return &PicoChannel{ BaseChannel: base, bc: bc, config: cfg, @@ -125,9 +117,7 @@ func NewPicoChannel( }, connections: make(map[string]*picoConn), sessionConnections: make(map[string]map[string]*picoConn), - } - ch.progress = channels.NewToolFeedbackAnimator(ch.EditMessage) - return ch, nil + }, nil } // createAndAddConnection checks MaxConnections and registers a connection atomically. @@ -245,9 +235,6 @@ func (c *PicoChannel) Stop(ctx context.Context) error { if c.cancel != nil { c.cancel() } - if c.progress != nil { - c.progress.StopAll() - } logger.InfoC("pico", "Pico Protocol channel stopped") return nil @@ -274,43 +261,13 @@ func (c *PicoChannel) Send(ctx context.Context, msg bus.OutboundMessage) ([]stri return nil, channels.ErrNotRunning } isThought := outboundMessageIsThought(msg) - isToolFeedback := outboundMessageIsToolFeedback(msg) - if isToolFeedback { - if msgID, handled, err := c.progress.Update(ctx, msg.ChatID, msg.Content); handled { - if err != nil { - return nil, err - } - return []string{msgID}, nil - } - } - trackedMsgID, hasTrackedMsg := c.currentToolFeedbackMessage(msg.ChatID) - if !isToolFeedback { - if msgIDs, handled := c.FinalizeToolFeedbackMessage(ctx, msg); handled { - return msgIDs, nil - } - } - - content := msg.Content - if isToolFeedback { - content = channels.InitialAnimatedToolFeedbackContent(msg.Content) - } - msgID := uuid.New().String() outMsg := newMessage(TypeMessageCreate, map[string]any{ - PayloadKeyContent: content, + PayloadKeyContent: msg.Content, PayloadKeyThought: isThought, - "message_id": msgID, }) - if err := c.broadcastToSession(msg.ChatID, outMsg); err != nil { - return nil, err - } - if isToolFeedback { - c.RecordToolFeedbackMessage(msg.ChatID, msgID, msg.Content) - } else if hasTrackedMsg { - c.dismissTrackedToolFeedbackMessage(ctx, msg.ChatID, trackedMsgID) - } - return []string{msgID}, nil + return nil, c.broadcastToSession(msg.ChatID, outMsg) } // EditMessage implements channels.MessageEditor. @@ -322,73 +279,6 @@ func (c *PicoChannel) EditMessage(ctx context.Context, chatID string, messageID return c.broadcastToSession(chatID, outMsg) } -func (c *PicoChannel) currentToolFeedbackMessage(chatID string) (string, bool) { - if c.progress == nil { - return "", false - } - return c.progress.Current(chatID) -} - -func (c *PicoChannel) takeToolFeedbackMessage(chatID string) (string, string, bool) { - if c.progress == nil { - return "", "", false - } - return c.progress.Take(chatID) -} - -func (c *PicoChannel) RecordToolFeedbackMessage(chatID, messageID, content string) { - if c.progress == nil { - return - } - c.progress.Record(chatID, messageID, content) -} - -func (c *PicoChannel) ClearToolFeedbackMessage(chatID string) { - if c.progress == nil { - return - } - c.progress.Clear(chatID) -} - -func (c *PicoChannel) DismissToolFeedbackMessage(ctx context.Context, chatID string) { - msgID, ok := c.currentToolFeedbackMessage(chatID) - if !ok { - return - } - c.dismissTrackedToolFeedbackMessage(ctx, chatID, msgID) -} - -func (c *PicoChannel) dismissTrackedToolFeedbackMessage(ctx context.Context, chatID, messageID string) { - if strings.TrimSpace(chatID) == "" || strings.TrimSpace(messageID) == "" { - return - } - c.ClearToolFeedbackMessage(chatID) -} - -func (c *PicoChannel) finalizeTrackedToolFeedbackMessage( - ctx context.Context, - chatID string, - content string, - editFn func(context.Context, string, string, string) error, -) ([]string, bool) { - msgID, baseContent, ok := c.takeToolFeedbackMessage(chatID) - if !ok || editFn == nil { - return nil, false - } - if err := editFn(ctx, chatID, msgID, content); err != nil { - c.RecordToolFeedbackMessage(chatID, msgID, baseContent) - return nil, false - } - return []string{msgID}, true -} - -func (c *PicoChannel) FinalizeToolFeedbackMessage(ctx context.Context, msg bus.OutboundMessage) ([]string, bool) { - if outboundMessageIsToolFeedback(msg) { - return nil, false - } - return c.finalizeTrackedToolFeedbackMessage(ctx, msg.ChatID, msg.Content, c.EditMessage) -} - // StartTyping implements channels.TypingCapable. func (c *PicoChannel) StartTyping(ctx context.Context, chatID string) (func(), error) { startMsg := newMessage(TypeTypingStart, nil) diff --git a/pkg/channels/pico/pico_test.go b/pkg/channels/pico/pico_test.go index 77a146f34..59db705eb 100644 --- a/pkg/channels/pico/pico_test.go +++ b/pkg/channels/pico/pico_test.go @@ -27,34 +27,6 @@ func newTestPicoChannel(t *testing.T) *PicoChannel { return ch } -func TestFinalizeTrackedToolFeedbackMessage_StopsTrackingBeforeEdit(t *testing.T) { - ch := &PicoChannel{ - progress: channels.NewToolFeedbackAnimator(nil), - } - ch.RecordToolFeedbackMessage("pico:chat-1", "msg-1", "🔧 `read_file`") - - msgIDs, handled := ch.finalizeTrackedToolFeedbackMessage( - context.Background(), - "pico:chat-1", - "final reply", - func(_ context.Context, chatID, messageID, content string) error { - if _, ok := ch.currentToolFeedbackMessage(chatID); ok { - t.Fatal("expected tracked tool feedback to be stopped before edit") - } - if chatID != "pico:chat-1" || messageID != "msg-1" || content != "final reply" { - t.Fatalf("unexpected edit args: %s %s %s", chatID, messageID, content) - } - return nil - }, - ) - if !handled { - t.Fatal("expected finalizeTrackedToolFeedbackMessage to handle tracked message") - } - if len(msgIDs) != 1 || msgIDs[0] != "msg-1" { - t.Fatalf("finalizeTrackedToolFeedbackMessage() ids = %v, want [msg-1]", msgIDs) - } -} - func TestCreateAndAddConnection_RespectsMaxConnectionsConcurrently(t *testing.T) { ch := newTestPicoChannel(t) diff --git a/pkg/channels/telegram/command_registration.go b/pkg/channels/telegram/command_registration.go index c6b362601..d3152ec3d 100644 --- a/pkg/channels/telegram/command_registration.go +++ b/pkg/channels/telegram/command_registration.go @@ -66,10 +66,6 @@ func (c *TelegramChannel) startCommandRegistration(ctx context.Context, defs []c if register == nil { register = c.RegisterCommands } - delayFn := c.commandRegDelayFn - if delayFn == nil { - delayFn = commandRegistrationDelay - } regCtx, cancel := context.WithCancel(ctx) c.commandRegCancel = cancel @@ -95,7 +91,7 @@ func (c *TelegramChannel) startCommandRegistration(ctx context.Context, defs []c return } - delay := delayFn(attempt) + delay := commandRegistrationDelay(attempt) logger.WarnCF("telegram", "Telegram command registration failed; will retry", map[string]any{ "error": err.Error(), "retry_after": delay.String(), diff --git a/pkg/channels/telegram/command_registration_test.go b/pkg/channels/telegram/command_registration_test.go index c30c6f68d..26f891b2e 100644 --- a/pkg/channels/telegram/command_registration_test.go +++ b/pkg/channels/telegram/command_registration_test.go @@ -31,12 +31,14 @@ func TestStartCommandRegistration_DoesNotBlock(t *testing.T) { } func TestStartCommandRegistration_RetriesUntilSuccessThenStops(t *testing.T) { - ch := &TelegramChannel{ - commandRegDelayFn: func(int) time.Duration { return 5 * time.Millisecond }, - } + ch := &TelegramChannel{} ctx, cancel := context.WithCancel(context.Background()) defer cancel() + origBackoff := commandRegistrationBackoff + commandRegistrationBackoff = []time.Duration{5 * time.Millisecond} + defer func() { commandRegistrationBackoff = origBackoff }() + var attempts atomic.Int32 ch.registerFunc = func(context.Context, []commands.Definition) error { n := attempts.Add(1) @@ -67,10 +69,12 @@ func TestStartCommandRegistration_RetriesUntilSuccessThenStops(t *testing.T) { } func TestStartCommandRegistration_StopsAfterCancel(t *testing.T) { - ch := &TelegramChannel{ - commandRegDelayFn: func(int) time.Duration { return 5 * time.Millisecond }, - } + ch := &TelegramChannel{} ctx, cancel := context.WithCancel(context.Background()) + + origBackoff := commandRegistrationBackoff + commandRegistrationBackoff = []time.Duration{5 * time.Millisecond} + defer func() { commandRegistrationBackoff = origBackoff }() defer cancel() var attempts atomic.Int32 diff --git a/pkg/channels/telegram/telegram.go b/pkg/channels/telegram/telegram.go index 8bec7856d..2a9cfe4ae 100644 --- a/pkg/channels/telegram/telegram.go +++ b/pkg/channels/telegram/telegram.go @@ -45,18 +45,16 @@ var ( type TelegramChannel struct { *channels.BaseChannel - bot *telego.Bot - bh *th.BotHandler - bc *config.Channel - chatIDs map[string]int64 - ctx context.Context - cancel context.CancelFunc - tgCfg *config.TelegramSettings - progress *channels.ToolFeedbackAnimator + bot *telego.Bot + bh *th.BotHandler + bc *config.Channel + chatIDs map[string]int64 + ctx context.Context + cancel context.CancelFunc + tgCfg *config.TelegramSettings - registerFunc func(context.Context, []commands.Definition) error - commandRegDelayFn func(int) time.Duration - commandRegCancel context.CancelFunc + registerFunc func(context.Context, []commands.Definition) error + commandRegCancel context.CancelFunc } func NewTelegramChannel( @@ -106,15 +104,13 @@ func NewTelegramChannel( channels.WithReasoningChannelID(bc.ReasoningChannelID), ) - ch := &TelegramChannel{ + return &TelegramChannel{ BaseChannel: base, bot: bot, bc: bc, chatIDs: make(map[string]int64), tgCfg: telegramCfg, - } - ch.progress = channels.NewToolFeedbackAnimator(ch.EditMessage) - return ch, nil + }, nil } func (c *TelegramChannel) Start(ctx context.Context) error { @@ -172,9 +168,6 @@ func (c *TelegramChannel) Stop(ctx context.Context) error { if c.cancel != nil { c.cancel() } - if c.progress != nil { - c.progress.StopAll() - } if c.commandRegCancel != nil { c.commandRegCancel() } @@ -198,35 +191,12 @@ func (c *TelegramChannel) Send(ctx context.Context, msg bus.OutboundMessage) ([] return nil, nil } - isToolFeedback := outboundMessageIsToolFeedback(msg) - toolFeedbackContent := msg.Content - if isToolFeedback { - toolFeedbackContent = fitToolFeedbackForTelegram(msg.Content, useMarkdownV2, 4096) - } - if isToolFeedback { - if msgID, handled, err := c.progress.Update(ctx, msg.ChatID, toolFeedbackContent); handled { - if err != nil { - return nil, err - } - return []string{msgID}, nil - } - } - trackedMsgID, hasTrackedMsg := c.currentToolFeedbackMessage(msg.ChatID) - if !isToolFeedback { - if msgIDs, handled := c.FinalizeToolFeedbackMessage(ctx, msg); handled { - return msgIDs, nil - } - } - // The Manager already splits messages to ≤4000 chars (WithMaxMessageLength), // so msg.Content is guaranteed to be within that limit. We still need to // check if HTML expansion pushes it beyond Telegram's 4096-char API limit. replyToID := msg.ReplyToMessageID var messageIDs []string queue := []string{msg.Content} - if isToolFeedback { - queue = []string{channels.InitialAnimatedToolFeedbackContent(toolFeedbackContent)} - } for len(queue) > 0 { chunk := queue[0] queue = queue[1:] @@ -234,13 +204,6 @@ func (c *TelegramChannel) Send(ctx context.Context, msg bus.OutboundMessage) ([] content := parseContent(chunk, useMarkdownV2) if len([]rune(content)) > 4096 { - if isToolFeedback { - fittedChunk := fitToolFeedbackForTelegram(chunk, useMarkdownV2, 4096) - if fittedChunk != "" && fittedChunk != chunk { - queue = append([]string{fittedChunk}, queue...) - continue - } - } runeChunk := []rune(chunk) ratio := float64(len(runeChunk)) / float64(len([]rune(content))) smallerLen := int(float64(4096) * ratio * 0.95) // 5% safety margin @@ -307,12 +270,6 @@ func (c *TelegramChannel) Send(ctx context.Context, msg bus.OutboundMessage) ([] replyToID = "" } - if isToolFeedback && len(messageIDs) > 0 { - c.RecordToolFeedbackMessage(msg.ChatID, messageIDs[0], toolFeedbackContent) - } else if !isToolFeedback && hasTrackedMsg { - c.dismissTrackedToolFeedbackMessage(ctx, msg.ChatID, trackedMsgID) - } - return messageIDs, nil } @@ -480,81 +437,6 @@ func (c *TelegramChannel) DeleteMessage(ctx context.Context, chatID string, mess }) } -func outboundMessageIsToolFeedback(msg bus.OutboundMessage) bool { - if len(msg.Context.Raw) == 0 { - return false - } - return strings.EqualFold(strings.TrimSpace(msg.Context.Raw["message_kind"]), "tool_feedback") -} - -func (c *TelegramChannel) currentToolFeedbackMessage(chatID string) (string, bool) { - if c.progress == nil { - return "", false - } - return c.progress.Current(chatID) -} - -func (c *TelegramChannel) takeToolFeedbackMessage(chatID string) (string, string, bool) { - if c.progress == nil { - return "", "", false - } - return c.progress.Take(chatID) -} - -func (c *TelegramChannel) RecordToolFeedbackMessage(chatID, messageID, content string) { - if c.progress == nil { - return - } - c.progress.Record(chatID, messageID, content) -} - -func (c *TelegramChannel) ClearToolFeedbackMessage(chatID string) { - if c.progress == nil { - return - } - c.progress.Clear(chatID) -} - -func (c *TelegramChannel) DismissToolFeedbackMessage(ctx context.Context, chatID string) { - msgID, ok := c.currentToolFeedbackMessage(chatID) - if !ok { - return - } - c.dismissTrackedToolFeedbackMessage(ctx, chatID, msgID) -} - -func (c *TelegramChannel) dismissTrackedToolFeedbackMessage(ctx context.Context, chatID, messageID string) { - if strings.TrimSpace(chatID) == "" || strings.TrimSpace(messageID) == "" { - return - } - c.ClearToolFeedbackMessage(chatID) - _ = c.DeleteMessage(ctx, chatID, messageID) -} - -func (c *TelegramChannel) finalizeTrackedToolFeedbackMessage( - ctx context.Context, - chatID string, - content string, - editFn func(context.Context, string, string, string) error, -) ([]string, bool) { - msgID, baseContent, ok := c.takeToolFeedbackMessage(chatID) - if !ok || editFn == nil { - return nil, false - } - if err := editFn(ctx, chatID, msgID, content); err != nil { - c.RecordToolFeedbackMessage(chatID, msgID, baseContent) - return nil, false - } - return []string{msgID}, true -} - -func (c *TelegramChannel) FinalizeToolFeedbackMessage(ctx context.Context, msg bus.OutboundMessage) ([]string, bool) { - if outboundMessageIsToolFeedback(msg) { - return nil, false - } - return c.finalizeTrackedToolFeedbackMessage(ctx, msg.ChatID, msg.Content, c.EditMessage) -} - // SendPlaceholder implements channels.PlaceholderCapable. // It sends a placeholder message (e.g. "Thinking... 💭") that will later be // edited to the actual response via EditMessage (channels.MessageEditor). @@ -586,7 +468,6 @@ func (c *TelegramChannel) SendMedia(ctx context.Context, msg bus.OutboundMediaMe if !c.IsRunning() { return nil, channels.ErrNotRunning } - trackedMsgID, hasTrackedMsg := c.currentToolFeedbackMessage(msg.ChatID) chatID, threadID, err := resolveTelegramOutboundTarget(msg.ChatID, &msg.Context) if err != nil { @@ -695,10 +576,6 @@ func (c *TelegramChannel) SendMedia(ctx context.Context, msg bus.OutboundMediaMe } } - if hasTrackedMsg { - c.dismissTrackedToolFeedbackMessage(ctx, msg.ChatID, trackedMsgID) - } - return messageIDs, nil } @@ -1070,41 +947,6 @@ func parseContent(text string, useMarkdownV2 bool) string { return markdownToTelegramHTML(text) } -func fitToolFeedbackForTelegram(content string, useMarkdownV2 bool, maxParsedLen int) string { - content = strings.TrimSpace(content) - if content == "" || maxParsedLen <= 0 { - return "" - } - animationSafeLen := maxParsedLen - channels.MaxToolFeedbackAnimationFrameLength() - if animationSafeLen <= 0 { - animationSafeLen = maxParsedLen - } - if len([]rune(parseContent(content, useMarkdownV2))) <= animationSafeLen { - return content - } - - low := 1 - high := len([]rune(content)) - best := utils.Truncate(content, 1) - - for low <= high { - mid := (low + high) / 2 - candidate := utils.FitToolFeedbackMessage(content, mid) - if candidate == "" { - high = mid - 1 - continue - } - if len([]rune(parseContent(candidate, useMarkdownV2))) <= animationSafeLen { - best = candidate - low = mid + 1 - continue - } - high = mid - 1 - } - - return best -} - // parseTelegramChatID splits "chatID/threadID" into its components. // Returns threadID=0 when no "/" is present (non-forum messages). func parseTelegramChatID(chatID string) (int64, int, error) { diff --git a/pkg/channels/telegram/telegram_group_command_filter_test.go b/pkg/channels/telegram/telegram_group_command_filter_test.go index 20b2004a9..614b2ca7f 100644 --- a/pkg/channels/telegram/telegram_group_command_filter_test.go +++ b/pkg/channels/telegram/telegram_group_command_filter_test.go @@ -108,7 +108,7 @@ func TestHandleMessage_GroupMentionOnly_BotCommandEntity(t *testing.T) { t.Fatalf("handleMessage error: %v", err) } - ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond) + ctx, cancel := context.WithTimeout(context.Background(), 200*time.Microsecond) defer cancel() select { case <-ctx.Done(): diff --git a/pkg/channels/telegram/telegram_test.go b/pkg/channels/telegram/telegram_test.go index f3974723d..3d147b337 100644 --- a/pkg/channels/telegram/telegram_test.go +++ b/pkg/channels/telegram/telegram_test.go @@ -98,12 +98,8 @@ func (s *multipartRecordingConstructor) MultipartRequest( // successResponse returns a ta.Response that telego will treat as a successful SendMessage. func successResponse(t *testing.T) *ta.Response { - return successResponseWithMessageID(t, 1) -} - -func successResponseWithMessageID(t *testing.T, messageID int) *ta.Response { t.Helper() - msg := &telego.Message{MessageID: messageID} + msg := &telego.Message{MessageID: 1} b, err := json.Marshal(msg) require.NoError(t, err) return &ta.Response{Ok: true, Result: b} @@ -146,7 +142,6 @@ func newTestChannelWithConstructor( chatIDs: make(map[string]int64), bc: &config.Channel{Type: config.ChannelTelegram, Enabled: true}, tgCfg: &config.TelegramSettings{}, - progress: channels.NewToolFeedbackAnimator(nil), } } @@ -271,101 +266,6 @@ func TestSend_ShortMessage_SingleCall(t *testing.T) { assert.Len(t, caller.calls, 1, "short message should result in exactly one SendMessage call") } -func TestSend_NonToolFeedbackDeletesTrackedProgressMessage(t *testing.T) { - caller := &stubCaller{ - callFn: func(ctx context.Context, url string, data *ta.RequestData) (*ta.Response, error) { - switch { - case strings.Contains(url, "editMessageText"): - return successResponseWithMessageID(t, 1), nil - default: - t.Fatalf("unexpected API call: %s", url) - return nil, nil - } - }, - } - ch := newTestChannel(t, caller) - ch.RecordToolFeedbackMessage("12345", "1", "🔧 `read_file`") - - ids, err := ch.Send(context.Background(), bus.OutboundMessage{ - ChatID: "12345", - Content: "final reply", - }) - - assert.NoError(t, err) - assert.Equal(t, []string{"1"}, ids) - require.Len(t, caller.calls, 1) - assert.Contains(t, caller.calls[0].URL, "editMessageText") - _, ok := ch.currentToolFeedbackMessage("12345") - assert.False(t, ok, "tracked tool feedback should be cleared after final reply") -} - -func TestFinalizeTrackedToolFeedbackMessage_StopsTrackingBeforeEdit(t *testing.T) { - ch := newTestChannel(t, &stubCaller{ - callFn: func(context.Context, string, *ta.RequestData) (*ta.Response, error) { - t.Fatal("unexpected API call") - return nil, nil - }, - }) - ch.RecordToolFeedbackMessage("12345", "1", "🔧 `read_file`") - - msgIDs, handled := ch.finalizeTrackedToolFeedbackMessage( - context.Background(), - "12345", - "final reply", - func(_ context.Context, chatID, messageID, content string) error { - _, ok := ch.currentToolFeedbackMessage(chatID) - assert.False(t, ok, "tracked tool feedback should be stopped before edit") - assert.Equal(t, "12345", chatID) - assert.Equal(t, "1", messageID) - assert.Equal(t, "final reply", content) - return nil - }, - ) - - assert.True(t, handled) - assert.Equal(t, []string{"1"}, msgIDs) -} - -func TestSend_ToolFeedbackStaysSingleMessageAfterHTMLExpansion(t *testing.T) { - caller := &stubCaller{ - callFn: func(ctx context.Context, url string, data *ta.RequestData) (*ta.Response, error) { - return successResponse(t), nil - }, - } - ch := newTestChannel(t, caller) - - _, err := ch.Send(context.Background(), bus.OutboundMessage{ - ChatID: "12345", - Content: "🔧 `read_file`\n" + strings.Repeat("<", 2000), - Context: bus.InboundContext{ - Channel: "telegram", - ChatID: "12345", - Raw: map[string]string{ - "message_kind": "tool_feedback", - }, - }, - }) - - assert.NoError(t, err) - assert.Len(t, caller.calls, 1, "tool feedback should stay a single Telegram message after HTML escaping") -} - -func TestFitToolFeedbackForTelegram_ReservesAnimationFrame(t *testing.T) { - content := "🔧 `read_file`\n" + strings.Repeat("a", 4096) - - fitted := fitToolFeedbackForTelegram(content, false, 4096) - animated := strings.Replace( - fitted, - "`\n", - strings.Repeat(".", channels.MaxToolFeedbackAnimationFrameLength())+"`\n", - 1, - ) - - if got := len([]rune(parseContent(animated, false))); got > 4096 { - t.Fatalf("animated parsed length = %d, want <= 4096", got) - } -} - func TestSend_LongMessage_SingleCall(t *testing.T) { // With WithMaxMessageLength(4000), the Manager pre-splits messages before // they reach Send(). A message at exactly 4000 chars should go through diff --git a/pkg/channels/tool_feedback_animator.go b/pkg/channels/tool_feedback_animator.go deleted file mode 100644 index b424612bf..000000000 --- a/pkg/channels/tool_feedback_animator.go +++ /dev/null @@ -1,240 +0,0 @@ -package channels - -import ( - "context" - "strings" - "sync" - "time" -) - -const toolFeedbackAnimationInterval = 3 * time.Second - -const initialToolFeedbackAnimationFrame = "" - -var toolFeedbackAnimationFrames = []string{"..", "."} - -// MaxToolFeedbackAnimationFrameLength returns the largest frame suffix length -// so callers can reserve room before sending messages to length-limited APIs. -func MaxToolFeedbackAnimationFrameLength() int { - maxLen := len([]rune(initialToolFeedbackAnimationFrame)) - for _, frame := range toolFeedbackAnimationFrames { - if frameLen := len([]rune(frame)); frameLen > maxLen { - maxLen = frameLen - } - } - return maxLen -} - -type toolFeedbackAnimationState struct { - messageID string - baseContent string - stop chan struct{} - done chan struct{} -} - -type ToolFeedbackAnimator struct { - mu sync.Mutex - editFn func(ctx context.Context, chatID, messageID, content string) error - entries map[string]*toolFeedbackAnimationState -} - -func NewToolFeedbackAnimator( - editFn func(ctx context.Context, chatID, messageID, content string) error, -) *ToolFeedbackAnimator { - return &ToolFeedbackAnimator{ - editFn: editFn, - entries: make(map[string]*toolFeedbackAnimationState), - } -} - -func (a *ToolFeedbackAnimator) Current(chatID string) (string, bool) { - if a == nil || strings.TrimSpace(chatID) == "" { - return "", false - } - a.mu.Lock() - defer a.mu.Unlock() - entry, ok := a.entries[chatID] - if !ok || strings.TrimSpace(entry.messageID) == "" { - return "", false - } - return entry.messageID, true -} - -func (a *ToolFeedbackAnimator) Record(chatID, messageID, content string) { - if a == nil { - return - } - chatID = strings.TrimSpace(chatID) - messageID = strings.TrimSpace(messageID) - content = strings.TrimSpace(content) - if chatID == "" || messageID == "" || content == "" { - return - } - - entry := &toolFeedbackAnimationState{ - messageID: messageID, - baseContent: content, - stop: make(chan struct{}), - done: make(chan struct{}), - } - - var previous *toolFeedbackAnimationState - a.mu.Lock() - if old, ok := a.entries[chatID]; ok { - previous = old - } - a.entries[chatID] = entry - a.mu.Unlock() - - stopToolFeedbackAnimation(previous) - go a.run(chatID, entry) -} - -func (a *ToolFeedbackAnimator) Clear(chatID string) { - if a == nil || strings.TrimSpace(chatID) == "" { - return - } - entry := a.detach(chatID) - stopToolFeedbackAnimation(entry) -} - -func (a *ToolFeedbackAnimator) Take(chatID string) (string, string, bool) { - if a == nil || strings.TrimSpace(chatID) == "" { - return "", "", false - } - entry := a.detach(chatID) - if entry == nil || strings.TrimSpace(entry.messageID) == "" { - return "", "", false - } - stopToolFeedbackAnimation(entry) - return entry.messageID, entry.baseContent, true -} - -// Update edits an existing tracked feedback message. If the edit fails, the -// previous feedback state is restored so callers can retry without orphaning -// the old progress message. -func (a *ToolFeedbackAnimator) Update(ctx context.Context, chatID, content string) (string, bool, error) { - if a == nil || a.editFn == nil { - return "", false, nil - } - msgID, baseContent, ok := a.Take(chatID) - if !ok { - return "", false, nil - } - - animatedContent := InitialAnimatedToolFeedbackContent(content) - if err := a.editFn(ctx, strings.TrimSpace(chatID), msgID, animatedContent); err != nil { - a.Record(chatID, msgID, baseContent) - return "", true, err - } - - a.Record(chatID, msgID, content) - return msgID, true, nil -} - -func (a *ToolFeedbackAnimator) StopAll() { - if a == nil { - return - } - a.mu.Lock() - entries := make([]*toolFeedbackAnimationState, 0, len(a.entries)) - for chatID, entry := range a.entries { - entries = append(entries, entry) - delete(a.entries, chatID) - } - a.mu.Unlock() - - for _, entry := range entries { - stopToolFeedbackAnimation(entry) - } -} - -func (a *ToolFeedbackAnimator) detach(chatID string) *toolFeedbackAnimationState { - if a == nil || strings.TrimSpace(chatID) == "" { - return nil - } - a.mu.Lock() - defer a.mu.Unlock() - entry := a.entries[chatID] - delete(a.entries, chatID) - return entry -} - -func (a *ToolFeedbackAnimator) run(chatID string, entry *toolFeedbackAnimationState) { - defer close(entry.done) - - ticker := time.NewTicker(toolFeedbackAnimationInterval) - defer ticker.Stop() - - frameIdx := 1 - - for { - select { - case <-entry.stop: - return - case <-ticker.C: - if a.editFn == nil { - continue - } - frame := toolFeedbackAnimationFrames[frameIdx%len(toolFeedbackAnimationFrames)] - content := formatAnimatedToolFeedbackContent(entry.baseContent, frame) - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - _ = a.editFn(ctx, chatID, entry.messageID, content) - cancel() - frameIdx++ - } - } -} - -func InitialAnimatedToolFeedbackContent(baseContent string) string { - return formatAnimatedToolFeedbackContent(baseContent, initialToolFeedbackAnimationFrame) -} - -func formatAnimatedToolFeedbackContent(baseContent, frame string) string { - baseContent = strings.TrimSpace(baseContent) - frame = strings.TrimSpace(frame) - if baseContent == "" { - return "" - } - if frame == "" { - return baseContent - } - lineBreak := strings.IndexByte(baseContent, '\n') - if lineBreak < 0 { - return appendToolFeedbackFrame(baseContent, frame) - } - return appendToolFeedbackFrame(baseContent[:lineBreak], frame) + baseContent[lineBreak:] -} - -func appendToolFeedbackFrame(firstLine, frame string) string { - firstLine = strings.TrimSpace(firstLine) - frame = strings.TrimSpace(frame) - if firstLine == "" { - return "" - } - if frame == "" { - return firstLine - } - - openTick := strings.IndexByte(firstLine, '`') - if openTick >= 0 { - if closeOffset := strings.IndexByte(firstLine[openTick+1:], '`'); closeOffset >= 0 { - closeTick := openTick + 1 + closeOffset - return firstLine[:closeTick] + frame + firstLine[closeTick:] - } - } - - return firstLine + frame -} - -func stopToolFeedbackAnimation(entry *toolFeedbackAnimationState) { - if entry == nil { - return - } - select { - case <-entry.stop: - default: - close(entry.stop) - } - <-entry.done -} diff --git a/pkg/channels/tool_feedback_animator_test.go b/pkg/channels/tool_feedback_animator_test.go deleted file mode 100644 index a23284548..000000000 --- a/pkg/channels/tool_feedback_animator_test.go +++ /dev/null @@ -1,121 +0,0 @@ -package channels - -import ( - "context" - "errors" - "testing" -) - -func TestFormatAnimatedToolFeedbackContent(t *testing.T) { - got := formatAnimatedToolFeedbackContent("🔧 `read_file`\nReading config file", "running..") - want := "🔧 `read_filerunning..`\nReading config file" - if got != want { - t.Fatalf("formatAnimatedToolFeedbackContent() = %q, want %q", got, want) - } -} - -func TestInitialAnimatedToolFeedbackContent(t *testing.T) { - got := InitialAnimatedToolFeedbackContent("🔧 `exec`\nRunning command") - want := "🔧 `exec`\nRunning command" - if got != want { - t.Fatalf("InitialAnimatedToolFeedbackContent() = %q, want %q", got, want) - } -} - -func TestFormatAnimatedToolFeedbackContent_WithoutCodeSpan(t *testing.T) { - got := formatAnimatedToolFeedbackContent("hello", "running..") - want := "hellorunning.." - if got != want { - t.Fatalf("formatAnimatedToolFeedbackContent() without code span = %q, want %q", got, want) - } -} - -func TestToolFeedbackAnimator_RecordCurrentAndClear(t *testing.T) { - animator := NewToolFeedbackAnimator(nil) - animator.Record("chat-1", "msg-1", "🔧 `read_file`") - - msgID, ok := animator.Current("chat-1") - if !ok || msgID != "msg-1" { - t.Fatalf("Current() = (%q, %v), want (msg-1, true)", msgID, ok) - } - - animator.Clear("chat-1") - - msgID, ok = animator.Current("chat-1") - if ok || msgID != "" { - t.Fatalf("Current() after Clear = (%q, %v), want (\"\", false)", msgID, ok) - } -} - -func TestToolFeedbackAnimator_TakeStopsTrackingAndReturnsState(t *testing.T) { - animator := NewToolFeedbackAnimator(nil) - animator.Record("chat-1", "msg-1", "🔧 `read_file`\nChecking config") - - msgID, baseContent, ok := animator.Take("chat-1") - if !ok { - t.Fatal("Take() = not found, want tracked message") - } - if msgID != "msg-1" { - t.Fatalf("Take() msgID = %q, want msg-1", msgID) - } - if baseContent != "🔧 `read_file`\nChecking config" { - t.Fatalf("Take() baseContent = %q", baseContent) - } - if _, ok := animator.Current("chat-1"); ok { - t.Fatal("expected tracked message to be removed after Take()") - } -} - -func TestToolFeedbackAnimator_UpdateStopsTrackingBeforeEdit(t *testing.T) { - var animator *ToolFeedbackAnimator - animator = NewToolFeedbackAnimator(func(_ context.Context, chatID, messageID, content string) error { - if _, ok := animator.Current(chatID); ok { - t.Fatal("expected tracked tool feedback to be stopped before edit") - } - if messageID != "msg-1" { - t.Fatalf("messageID = %q, want msg-1", messageID) - } - if content != "🔧 `write_file`\nUpdating config" { - t.Fatalf("content = %q, want updated animated content", content) - } - return nil - }) - defer animator.StopAll() - - animator.Record("chat-1", "msg-1", "🔧 `read_file`\nChecking config") - - msgID, handled, err := animator.Update(context.Background(), "chat-1", "🔧 `write_file`\nUpdating config") - if err != nil { - t.Fatalf("Update() error = %v", err) - } - if !handled { - t.Fatal("Update() handled = false, want true") - } - if msgID != "msg-1" { - t.Fatalf("Update() msgID = %q, want msg-1", msgID) - } -} - -func TestToolFeedbackAnimator_UpdateFailureRestoresTracking(t *testing.T) { - editErr := errors.New("edit failed") - animator := NewToolFeedbackAnimator(func(context.Context, string, string, string) error { - return editErr - }) - defer animator.StopAll() - - animator.Record("chat-1", "msg-1", "🔧 `read_file`\nChecking config") - - msgID, handled, err := animator.Update(context.Background(), "chat-1", "🔧 `write_file`\nUpdating config") - if !handled { - t.Fatal("Update() handled = false, want true") - } - if !errors.Is(err, editErr) { - t.Fatalf("Update() error = %v, want editErr", err) - } - if msgID != "" { - t.Fatalf("Update() msgID = %q, want empty on failed edit", msgID) - } - if currentID, ok := animator.Current("chat-1"); !ok || currentID != "msg-1" { - t.Fatalf("Current() after failed Update = (%q, %v), want (msg-1, true)", currentID, ok) - } -} diff --git a/pkg/config/config.go b/pkg/config/config.go index 547060bd6..5bc96fb12 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -286,7 +286,7 @@ func (d *AgentDefaults) GetMaxMediaSize() int { return DefaultMaxMediaSize } -// GetToolFeedbackMaxArgsLength returns the max visible text length for tool feedback messages. +// GetToolFeedbackMaxArgsLength returns the max args preview length for tool feedback messages. func (d *AgentDefaults) GetToolFeedbackMaxArgsLength() int { if d.ToolFeedback.MaxArgsLength > 0 { return d.ToolFeedback.MaxArgsLength diff --git a/pkg/providers/cli/toolcall_utils.go b/pkg/providers/cli/toolcall_utils.go index 1f58c9a26..b480082eb 100644 --- a/pkg/providers/cli/toolcall_utils.go +++ b/pkg/providers/cli/toolcall_utils.go @@ -55,12 +55,6 @@ func buildCLIToolsPrompt(tools []ToolDefinition) string { func NormalizeToolCall(tc ToolCall) ToolCall { normalized := tc - if normalized.ThoughtSignature == "" && - normalized.ExtraContent != nil && - normalized.ExtraContent.Google != nil { - normalized.ThoughtSignature = normalized.ExtraContent.Google.ThoughtSignature - } - // Ensure Name is populated from Function if not set if normalized.Name == "" && normalized.Function != nil { normalized.Name = normalized.Function.Name @@ -83,9 +77,8 @@ func NormalizeToolCall(tc ToolCall) ToolCall { argsJSON, _ := json.Marshal(normalized.Arguments) if normalized.Function == nil { normalized.Function = &FunctionCall{ - Name: normalized.Name, - Arguments: string(argsJSON), - ThoughtSignature: normalized.ThoughtSignature, + Name: normalized.Name, + Arguments: string(argsJSON), } } else { if normalized.Function.Name == "" { @@ -97,12 +90,6 @@ func NormalizeToolCall(tc ToolCall) ToolCall { if normalized.Function.Arguments == "" { normalized.Function.Arguments = string(argsJSON) } - if normalized.Function.ThoughtSignature == "" { - normalized.Function.ThoughtSignature = normalized.ThoughtSignature - } - if normalized.ThoughtSignature == "" { - normalized.ThoughtSignature = normalized.Function.ThoughtSignature - } } return normalized diff --git a/pkg/providers/common/common.go b/pkg/providers/common/common.go index c167b1ffd..90142fb8b 100644 --- a/pkg/providers/common/common.go +++ b/pkg/providers/common/common.go @@ -70,23 +70,11 @@ func NewHTTPClient(proxy string) *http.Client { // It mirrors protocoltypes.Message but omits SystemParts, which is an // internal field that would be unknown to third-party endpoints. type openaiMessage struct { - Role string `json:"role"` - Content string `json:"content"` - ReasoningContent string `json:"reasoning_content,omitempty"` - ToolCalls []openaiToolCall `json:"tool_calls,omitempty"` - ToolCallID string `json:"tool_call_id,omitempty"` -} - -type openaiToolCall struct { - ID string `json:"id"` - Type string `json:"type,omitempty"` - Function *openaiFunctionCall `json:"function,omitempty"` -} - -type openaiFunctionCall struct { - Name string `json:"name"` - Arguments string `json:"arguments"` - ThoughtSignature string `json:"thought_signature,omitempty"` + Role string `json:"role"` + Content string `json:"content"` + ReasoningContent string `json:"reasoning_content,omitempty"` + ToolCalls []ToolCall `json:"tool_calls,omitempty"` + ToolCallID string `json:"tool_call_id,omitempty"` } // SerializeMessages converts internal Message structs to the OpenAI wire format. @@ -96,13 +84,12 @@ type openaiFunctionCall struct { func SerializeMessages(messages []Message) []any { out := make([]any, 0, len(messages)) for _, m := range messages { - toolCalls := serializeToolCalls(m.ToolCalls) if len(m.Media) == 0 { out = append(out, openaiMessage{ Role: m.Role, Content: m.Content, ReasoningContent: m.ReasoningContent, - ToolCalls: toolCalls, + ToolCalls: m.ToolCalls, ToolCallID: m.ToolCallID, }) continue @@ -145,8 +132,8 @@ func SerializeMessages(messages []Message) []any { if m.ToolCallID != "" { msg["tool_call_id"] = m.ToolCallID } - if len(toolCalls) > 0 { - msg["tool_calls"] = toolCalls + if len(m.ToolCalls) > 0 { + msg["tool_calls"] = m.ToolCalls } if m.ReasoningContent != "" { msg["reasoning_content"] = m.ReasoningContent @@ -156,55 +143,6 @@ func SerializeMessages(messages []Message) []any { return out } -func serializeToolCalls(toolCalls []ToolCall) []openaiToolCall { - if len(toolCalls) == 0 { - return nil - } - - out := make([]openaiToolCall, 0, len(toolCalls)) - for _, tc := range toolCalls { - wireCall := openaiToolCall{ - ID: tc.ID, - Type: tc.Type, - } - - if tc.Function != nil { - thoughtSignature := tc.Function.ThoughtSignature - if thoughtSignature == "" { - thoughtSignature = tc.ThoughtSignature - } - if thoughtSignature == "" && tc.ExtraContent != nil && tc.ExtraContent.Google != nil { - thoughtSignature = tc.ExtraContent.Google.ThoughtSignature - } - wireCall.Function = &openaiFunctionCall{ - Name: tc.Function.Name, - Arguments: tc.Function.Arguments, - ThoughtSignature: thoughtSignature, - } - } else if tc.Name != "" || len(tc.Arguments) > 0 || tc.ThoughtSignature != "" { - thoughtSignature := tc.ThoughtSignature - if thoughtSignature == "" && tc.ExtraContent != nil && tc.ExtraContent.Google != nil { - thoughtSignature = tc.ExtraContent.Google.ThoughtSignature - } - argsJSON := "{}" - if len(tc.Arguments) > 0 { - if encoded, err := json.Marshal(tc.Arguments); err == nil { - argsJSON = string(encoded) - } - } - wireCall.Function = &openaiFunctionCall{ - Name: tc.Name, - Arguments: argsJSON, - ThoughtSignature: thoughtSignature, - } - } - - out = append(out, wireCall) - } - - return out -} - func parseDataAudioURL(mediaURL string) (format, data string, ok bool) { if !strings.HasPrefix(mediaURL, "data:audio/") { return "", "", false @@ -247,7 +185,6 @@ func ParseResponse(body io.Reader) (*LLMResponse, error) { Google *struct { ThoughtSignature string `json:"thought_signature"` } `json:"google"` - ToolFeedbackExplanation string `json:"tool_feedback_explanation"` } `json:"extra_content"` } `json:"tool_calls"` } `json:"message"` @@ -291,17 +228,11 @@ func ParseResponse(body io.Reader) (*LLMResponse, error) { ThoughtSignature: thoughtSignature, } - if tc.ExtraContent != nil { - extraContent := &ExtraContent{ - ToolFeedbackExplanation: tc.ExtraContent.ToolFeedbackExplanation, - } - if thoughtSignature != "" { - extraContent.Google = &GoogleExtra{ + if thoughtSignature != "" { + toolCall.ExtraContent = &ExtraContent{ + Google: &GoogleExtra{ ThoughtSignature: thoughtSignature, - } - } - if extraContent.Google != nil || strings.TrimSpace(extraContent.ToolFeedbackExplanation) != "" { - toolCall.ExtraContent = extraContent + }, } } diff --git a/pkg/providers/common/common_test.go b/pkg/providers/common/common_test.go index affb91e6f..c107bb665 100644 --- a/pkg/providers/common/common_test.go +++ b/pkg/providers/common/common_test.go @@ -162,104 +162,6 @@ func TestSerializeMessages_StripsSystemParts(t *testing.T) { } } -func TestSerializeMessages_StripsInternalToolCallExtraContent(t *testing.T) { - messages := []Message{ - { - Role: "assistant", - ToolCalls: []ToolCall{{ - ID: "call_1", - Type: "function", - Function: &FunctionCall{ - Name: "read_file", - Arguments: `{"path":"README.md"}`, - ThoughtSignature: "sig-1", - }, - ExtraContent: &ExtraContent{ - Google: &GoogleExtra{ - ThoughtSignature: "sig-ignored-here", - }, - ToolFeedbackExplanation: "Read README.md first.", - }, - }}, - }, - } - - result := SerializeMessages(messages) - - data, err := json.Marshal(result) - if err != nil { - t.Fatalf("json.Marshal() error = %v", err) - } - payload := string(data) - if strings.Contains(payload, "extra_content") { - t.Fatalf("serialized payload should not include internal extra_content: %s", payload) - } - if !strings.Contains(payload, "thought_signature") { - t.Fatalf("serialized payload should preserve function thought_signature: %s", payload) - } -} - -func TestSerializeMessages_PreservesTopLevelThoughtSignature(t *testing.T) { - messages := []Message{ - { - Role: "assistant", - ToolCalls: []ToolCall{{ - ID: "call_1", - Type: "function", - ThoughtSignature: "sig-1", - Function: &FunctionCall{ - Name: "read_file", - Arguments: `{"path":"README.md"}`, - }, - }}, - }, - } - - result := SerializeMessages(messages) - - data, err := json.Marshal(result) - if err != nil { - t.Fatalf("json.Marshal() error = %v", err) - } - payload := string(data) - if !strings.Contains(payload, `"thought_signature":"sig-1"`) { - t.Fatalf("serialized payload should preserve top-level thought signature: %s", payload) - } -} - -func TestSerializeMessages_PreservesGoogleExtraThoughtSignature(t *testing.T) { - messages := []Message{ - { - Role: "assistant", - ToolCalls: []ToolCall{{ - ID: "call_1", - Type: "function", - Function: &FunctionCall{ - Name: "read_file", - Arguments: `{"path":"README.md"}`, - }, - ExtraContent: &ExtraContent{ - Google: &GoogleExtra{ThoughtSignature: "sig-1"}, - }, - }}, - }, - } - - result := SerializeMessages(messages) - - data, err := json.Marshal(result) - if err != nil { - t.Fatalf("json.Marshal() error = %v", err) - } - payload := string(data) - if strings.Contains(payload, "extra_content") { - t.Fatalf("serialized payload should not include extra_content: %s", payload) - } - if !strings.Contains(payload, `"thought_signature":"sig-1"`) { - t.Fatalf("serialized payload should preserve google thought signature: %s", payload) - } -} - // --- ParseResponse tests --- func TestParseResponse_BasicContent(t *testing.T) { @@ -332,27 +234,6 @@ func TestParseResponse_WithReasoningContent(t *testing.T) { } } -func TestParseResponse_WithToolFeedbackExplanationExtraContent(t *testing.T) { - body := `{"choices":[{"message":{"content":"","tool_calls":[{"id":"call_1","type":"function","function":{"name":"test_tool","arguments":"{}"},"extra_content":{"tool_feedback_explanation":"Check the current config before editing."}}]},"finish_reason":"tool_calls"}]}` - out, err := ParseResponse(strings.NewReader(body)) - if err != nil { - t.Fatalf("ParseResponse() error = %v", err) - } - if len(out.ToolCalls) != 1 { - t.Fatalf("len(ToolCalls) = %d, want 1", len(out.ToolCalls)) - } - if out.ToolCalls[0].ExtraContent == nil { - t.Fatal("ExtraContent is nil") - } - if out.ToolCalls[0].ExtraContent.ToolFeedbackExplanation != "Check the current config before editing." { - t.Fatalf( - "ToolFeedbackExplanation = %q, want %q", - out.ToolCalls[0].ExtraContent.ToolFeedbackExplanation, - "Check the current config before editing.", - ) - } -} - func TestParseResponse_InvalidJSON(t *testing.T) { _, err := ParseResponse(strings.NewReader("not json")) if err == nil { diff --git a/pkg/providers/protocoltypes/types.go b/pkg/providers/protocoltypes/types.go index 1189577f1..194c1aa6f 100644 --- a/pkg/providers/protocoltypes/types.go +++ b/pkg/providers/protocoltypes/types.go @@ -11,8 +11,7 @@ type ToolCall struct { } type ExtraContent struct { - Google *GoogleExtra `json:"google,omitempty"` - ToolFeedbackExplanation string `json:"tool_feedback_explanation,omitempty"` + Google *GoogleExtra `json:"google,omitempty"` } type GoogleExtra struct { diff --git a/pkg/providers/toolcall_utils_test.go b/pkg/providers/toolcall_utils_test.go deleted file mode 100644 index a4bb03c2e..000000000 --- a/pkg/providers/toolcall_utils_test.go +++ /dev/null @@ -1,24 +0,0 @@ -package providers - -import "testing" - -func TestNormalizeToolCall_PreservesExtraContentGoogleThoughtSignature(t *testing.T) { - tc := NormalizeToolCall(ToolCall{ - ID: "call_1", - Name: "search", - Arguments: map[string]any{"q": "pico"}, - ExtraContent: &ExtraContent{ - Google: &GoogleExtra{ThoughtSignature: "sig-1"}, - }, - }) - - if tc.ThoughtSignature != "sig-1" { - t.Fatalf("ThoughtSignature = %q, want sig-1", tc.ThoughtSignature) - } - if tc.Function == nil { - t.Fatal("Function is nil") - } - if tc.Function.ThoughtSignature != "sig-1" { - t.Fatalf("Function.ThoughtSignature = %q, want sig-1", tc.Function.ThoughtSignature) - } -} diff --git a/pkg/utils/tool_feedback.go b/pkg/utils/tool_feedback.go index 1a8b6c747..a6c8895b8 100644 --- a/pkg/utils/tool_feedback.go +++ b/pkg/utils/tool_feedback.go @@ -1,57 +1,9 @@ package utils -import ( - "fmt" - "strings" -) +import "fmt" -const ToolFeedbackContinuationHint = "Continuing the current task." - -// FormatToolFeedbackMessage renders the model-provided explanation for why a -// tool is being executed. When the model does not provide one, it keeps only -// the tool line and does not expose raw arguments or fallback text. -func FormatToolFeedbackMessage(toolName, explanation string) string { - toolName = strings.TrimSpace(toolName) - explanation = strings.TrimSpace(explanation) - - if toolName == "" { - return explanation - } - if explanation == "" { - return fmt.Sprintf("\U0001f527 `%s`", toolName) - } - - return fmt.Sprintf("\U0001f527 `%s`\n%s", toolName, explanation) -} - -// FitToolFeedbackMessage keeps tool feedback within a single outbound message. -// It preserves the first line when possible and truncates the explanation body -// instead of letting the message be split into multiple chunks. -func FitToolFeedbackMessage(content string, maxLen int) string { - content = strings.TrimSpace(content) - if content == "" || maxLen <= 0 { - return "" - } - if len([]rune(content)) <= maxLen { - return content - } - - firstLine, rest, hasRest := strings.Cut(content, "\n") - firstLine = strings.TrimSpace(firstLine) - rest = strings.TrimSpace(rest) - - if !hasRest || rest == "" { - return Truncate(firstLine, maxLen) - } - - if len([]rune(firstLine)) >= maxLen { - return Truncate(firstLine, maxLen) - } - - remaining := maxLen - len([]rune(firstLine)) - 1 - if remaining <= 0 { - return Truncate(firstLine, maxLen) - } - - return firstLine + "\n" + Truncate(rest, remaining) +// FormatToolFeedbackMessage renders the tool name and arguments preview in the +// same markdown shape used by live tool feedback and session reconstruction. +func FormatToolFeedbackMessage(toolName, argsPreview string) string { + return fmt.Sprintf("\U0001f527 `%s`\n```\n%s\n```", toolName, argsPreview) } diff --git a/pkg/utils/tool_feedback_test.go b/pkg/utils/tool_feedback_test.go index 316ce2408..d7a55ce6b 100644 --- a/pkg/utils/tool_feedback_test.go +++ b/pkg/utils/tool_feedback_test.go @@ -3,47 +3,9 @@ package utils import "testing" func TestFormatToolFeedbackMessage(t *testing.T) { - got := FormatToolFeedbackMessage( - "read_file", - "I will read README.md first to confirm the current project structure.", - ) - want := "\U0001f527 `read_file`\nI will read README.md first to confirm the current project structure." + got := FormatToolFeedbackMessage("read_file", "{\"path\":\"README.md\"}") + want := "\U0001f527 `read_file`\n```\n{\"path\":\"README.md\"}\n```" if got != want { t.Fatalf("FormatToolFeedbackMessage() = %q, want %q", got, want) } } - -func TestFormatToolFeedbackMessage_EmptyExplanationKeepsOnlyToolLine(t *testing.T) { - got := FormatToolFeedbackMessage("read_file", "") - want := "\U0001f527 `read_file`" - if got != want { - t.Fatalf("FormatToolFeedbackMessage() = %q, want %q", got, want) - } -} - -func TestFormatToolFeedbackMessage_EmptyToolNameOmitsToolLine(t *testing.T) { - got := FormatToolFeedbackMessage("", "Continue drafting the final response.") - want := "Continue drafting the final response." - if got != want { - t.Fatalf("FormatToolFeedbackMessage() = %q, want %q", got, want) - } -} - -func TestFitToolFeedbackMessage_TruncatesBodyWithinSingleMessage(t *testing.T) { - got := FitToolFeedbackMessage( - "\U0001f527 `read_file`\nRead README.md first to confirm the current project structure.", - 40, - ) - want := "\U0001f527 `read_file`\nRead README.md first to..." - if got != want { - t.Fatalf("FitToolFeedbackMessage() = %q, want %q", got, want) - } -} - -func TestFitToolFeedbackMessage_TruncatesSingleLineMessage(t *testing.T) { - got := FitToolFeedbackMessage("\U0001f527 `read_file`", 10) - want := "\U0001f527 `read..." - if got != want { - t.Fatalf("FitToolFeedbackMessage() = %q, want %q", got, want) - } -} diff --git a/web/backend/api/session.go b/web/backend/api/session.go index 2a16fe183..054b78b73 100644 --- a/web/backend/api/session.go +++ b/web/backend/api/session.go @@ -486,15 +486,6 @@ func visibleSessionMessages(messages []providers.Message, toolFeedbackMaxArgsLen transcript = append(transcript, visibleToolMessages...) } - // When assistant content exactly matches the rendered tool summary or - // tool-delivered message, skip it to avoid duplicates. Distinct content - // must remain visible in restored session history. - if len(msg.ToolCalls) > 0 && - len(msg.Media) == 0 && - assistantToolCallContentDuplicated(msg.Content, toolSummaryMessages, visibleToolMessages) { - continue - } - // Pico web chat can persist both visible `message` tool output and a // later plain assistant reply in the same turn. Hide only the fixed // internal summary that marks handled tool delivery. @@ -513,43 +504,6 @@ func visibleSessionMessages(messages []providers.Message, toolFeedbackMaxArgsLen return transcript } -func assistantToolCallContentDuplicated( - content string, - toolSummaryMessages []sessionChatMessage, - visibleToolMessages []sessionChatMessage, -) bool { - content = strings.TrimSpace(content) - if content == "" { - return false - } - - for _, msg := range toolSummaryMessages { - if toolSummaryContainsContent(msg.Content, content) { - return true - } - } - for _, msg := range visibleToolMessages { - if strings.TrimSpace(msg.Content) == content { - return true - } - } - return false -} - -func toolSummaryContainsContent(summary, content string) bool { - summary = strings.TrimSpace(summary) - content = strings.TrimSpace(content) - if summary == "" || content == "" { - return false - } - if summary == content { - return true - } - - _, body, hasBody := strings.Cut(summary, "\n") - return hasBody && strings.TrimSpace(body) == content -} - func assistantMessageTransientThought(msg providers.Message) bool { return strings.TrimSpace(msg.Content) == "" && strings.TrimSpace(msg.ReasoningContent) != "" && @@ -575,51 +529,38 @@ func visibleAssistantToolSummaryMessages( messages := make([]sessionChatMessage, 0, len(toolCalls)) for _, tc := range toolCalls { name := tc.Name + argsJSON := "" if tc.Function != nil { if name == "" { name = tc.Function.Name } + argsJSON = tc.Function.Arguments } if strings.TrimSpace(name) == "" { continue } + if strings.TrimSpace(argsJSON) == "" && len(tc.Arguments) > 0 { + if encodedArgs, err := json.Marshal(tc.Arguments); err == nil { + argsJSON = string(encodedArgs) + } + } + + argsPreview := strings.TrimSpace(argsJSON) + if argsPreview == "" { + argsPreview = "{}" + } + messages = append(messages, sessionChatMessage{ - Role: "assistant", - Content: utils.FormatToolFeedbackMessage( - name, - visibleAssistantToolSummaryText(tc, toolFeedbackMaxArgsLength), - ), + Role: "assistant", + Content: utils.FormatToolFeedbackMessage(name, utils.Truncate(argsPreview, toolFeedbackMaxArgsLength)), }) } return messages } -func visibleAssistantToolSummaryText( - tc providers.ToolCall, - toolFeedbackMaxArgsLength int, -) string { - if tc.ExtraContent != nil { - if explanation := strings.TrimSpace(tc.ExtraContent.ToolFeedbackExplanation); explanation != "" { - return utils.Truncate(explanation, toolFeedbackMaxArgsLength) - } - } - - argsJSON := "" - if tc.Function != nil { - argsJSON = tc.Function.Arguments - } - if strings.TrimSpace(argsJSON) == "" && len(tc.Arguments) > 0 { - if encodedArgs, err := json.Marshal(tc.Arguments); err == nil { - argsJSON = string(encodedArgs) - } - } - - return utils.Truncate(strings.TrimSpace(argsJSON), toolFeedbackMaxArgsLength) -} - func visibleAssistantToolMessages(toolCalls []providers.ToolCall) []sessionChatMessage { if len(toolCalls) == 0 { return nil diff --git a/web/backend/api/session_test.go b/web/backend/api/session_test.go index b0bab0baa..e40a8c77c 100644 --- a/web/backend/api/session_test.go +++ b/web/backend/api/session_test.go @@ -540,7 +540,7 @@ func TestHandleListSessions_MessageCountUsesVisibleTranscript(t *testing.T) { } } -func TestHandleGetSession_DoesNotDuplicateAssistantToolCallContent(t *testing.T) { +func TestHandleGetSession_PreservesToolSummaryAndAssistantContent(t *testing.T) { configPath, cleanup := setupOAuthTestEnv(t) defer cleanup() @@ -555,7 +555,7 @@ func TestHandleGetSession_DoesNotDuplicateAssistantToolCallContent(t *testing.T) {Role: "user", Content: "check file"}, { Role: "assistant", - Content: "Read the file before replying.", + Content: "model final reply", ToolCalls: []providers.ToolCall{ { ID: "call_1", @@ -564,9 +564,6 @@ func TestHandleGetSession_DoesNotDuplicateAssistantToolCallContent(t *testing.T) Name: "read_file", Arguments: `{"path":"README.md","start_line":1,"end_line":10}`, }, - ExtraContent: &providers.ExtraContent{ - ToolFeedbackExplanation: "Read the file before replying.", - }, }, }, }, @@ -597,8 +594,8 @@ func TestHandleGetSession_DoesNotDuplicateAssistantToolCallContent(t *testing.T) if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil { t.Fatalf("Unmarshal() error = %v", err) } - if len(resp.Messages) != 2 { - t.Fatalf("len(resp.Messages) = %d, want 2", len(resp.Messages)) + if len(resp.Messages) != 3 { + t.Fatalf("len(resp.Messages) = %d, want 3", len(resp.Messages)) } if resp.Messages[0].Role != "user" || resp.Messages[0].Content != "check file" { t.Fatalf("first message = %#v, want user/check file", resp.Messages[0]) @@ -606,153 +603,8 @@ func TestHandleGetSession_DoesNotDuplicateAssistantToolCallContent(t *testing.T) if !strings.Contains(resp.Messages[1].Content, "`read_file`") { t.Fatalf("tool summary message = %#v, want read_file summary", resp.Messages[1]) } - if !strings.Contains(resp.Messages[1].Content, "Read the file before replying.") { - t.Fatalf("tool summary message = %#v, want tool explanation", resp.Messages[1]) - } -} - -func TestHandleGetSession_PreservesDistinctAssistantToolCallContent(t *testing.T) { - configPath, cleanup := setupOAuthTestEnv(t) - defer cleanup() - - dir := sessionsTestDir(t, configPath) - store, err := memory.NewJSONLStore(dir) - if err != nil { - t.Fatalf("NewJSONLStore() error = %v", err) - } - - sessionKey := picoSessionPrefix + "detail-tool-summary-distinct-content" - for _, msg := range []providers.Message{ - {Role: "user", Content: "check file"}, - { - Role: "assistant", - Content: "I will summarize the findings after reading the file.", - ToolCalls: []providers.ToolCall{ - { - ID: "call_1", - Type: "function", - Function: &providers.FunctionCall{ - Name: "read_file", - Arguments: `{"path":"README.md","start_line":1,"end_line":10}`, - }, - ExtraContent: &providers.ExtraContent{ - ToolFeedbackExplanation: "Read the file before replying.", - }, - }, - }, - }, - } { - if err := store.AddFullMessage(nil, sessionKey, msg); err != nil { - t.Fatalf("AddFullMessage() error = %v", err) - } - } - - h := NewHandler(configPath) - mux := http.NewServeMux() - h.RegisterRoutes(mux) - - rec := httptest.NewRecorder() - req := httptest.NewRequest(http.MethodGet, "/api/sessions/detail-tool-summary-distinct-content", nil) - mux.ServeHTTP(rec, req) - - if rec.Code != http.StatusOK { - t.Fatalf("status = %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String()) - } - - var resp struct { - Messages []struct { - Role string `json:"role"` - Content string `json:"content"` - } `json:"messages"` - } - if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil { - t.Fatalf("Unmarshal() error = %v", err) - } - if len(resp.Messages) != 3 { - t.Fatalf("len(resp.Messages) = %d, want 3", len(resp.Messages)) - } - if !strings.Contains(resp.Messages[1].Content, "`read_file`") { - t.Fatalf("tool summary message = %#v, want read_file summary", resp.Messages[1]) - } - if resp.Messages[2].Role != "assistant" || - resp.Messages[2].Content != "I will summarize the findings after reading the file." { - t.Fatalf("assistant content = %#v, want preserved distinct content", resp.Messages[2]) - } -} - -func TestHandleGetSession_PreservesMediaWhenAssistantToolCallContentDuplicatesSummary(t *testing.T) { - configPath, cleanup := setupOAuthTestEnv(t) - defer cleanup() - - dir := sessionsTestDir(t, configPath) - store, err := memory.NewJSONLStore(dir) - if err != nil { - t.Fatalf("NewJSONLStore() error = %v", err) - } - - sessionKey := picoSessionPrefix + "detail-tool-summary-duplicate-content-with-media" - for _, msg := range []providers.Message{ - {Role: "user", Content: "check screenshot"}, - { - Role: "assistant", - Content: "Reviewing the generated screenshot.", - Media: []string{"data:image/png;base64,abc123"}, - ToolCalls: []providers.ToolCall{ - { - ID: "call_1", - Type: "function", - Function: &providers.FunctionCall{ - Name: "view_image", - Arguments: `{"path":"artifact.png"}`, - }, - ExtraContent: &providers.ExtraContent{ - ToolFeedbackExplanation: "Reviewing the generated screenshot.", - }, - }, - }, - }, - } { - if err := store.AddFullMessage(nil, sessionKey, msg); err != nil { - t.Fatalf("AddFullMessage() error = %v", err) - } - } - - h := NewHandler(configPath) - mux := http.NewServeMux() - h.RegisterRoutes(mux) - - rec := httptest.NewRecorder() - req := httptest.NewRequest(http.MethodGet, "/api/sessions/detail-tool-summary-duplicate-content-with-media", nil) - mux.ServeHTTP(rec, req) - - if rec.Code != http.StatusOK { - t.Fatalf("status = %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String()) - } - - var resp struct { - Messages []struct { - Role string `json:"role"` - Content string `json:"content"` - Media []string `json:"media"` - } `json:"messages"` - } - if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil { - t.Fatalf("Unmarshal() error = %v", err) - } - if len(resp.Messages) != 3 { - t.Fatalf("len(resp.Messages) = %d, want 3", len(resp.Messages)) - } - if !strings.Contains(resp.Messages[1].Content, "`view_image`") { - t.Fatalf("tool summary message = %#v, want view_image summary", resp.Messages[1]) - } - if resp.Messages[2].Role != "assistant" { - t.Fatalf("assistant message role = %q, want assistant", resp.Messages[2].Role) - } - if resp.Messages[2].Content != "Reviewing the generated screenshot." { - t.Fatalf("assistant content = %q, want preserved duplicated content with media", resp.Messages[2].Content) - } - if len(resp.Messages[2].Media) != 1 || resp.Messages[2].Media[0] != "data:image/png;base64,abc123" { - t.Fatalf("assistant media = %#v, want preserved media", resp.Messages[2].Media) + if resp.Messages[2].Role != "assistant" || resp.Messages[2].Content != "model final reply" { + t.Fatalf("assistant message = %#v, want model final reply", resp.Messages[2]) } } @@ -777,7 +629,6 @@ func TestHandleGetSession_UsesConfiguredToolFeedbackMaxArgsLength(t *testing.T) } argsJSON := `{"path":"README.md","start_line":1,"end_line":10,"extra":"abcdefghijklmnopqrstuvwxyz"}` - explanation := "Read README.md first to confirm the current project structure before editing the config example." sessionKey := picoSessionPrefix + "detail-tool-summary-max-args" err = store.AddFullMessage(nil, sessionKey, providers.Message{Role: "user", Content: "check file"}) if err != nil { @@ -792,9 +643,6 @@ func TestHandleGetSession_UsesConfiguredToolFeedbackMaxArgsLength(t *testing.T) Name: "read_file", Arguments: argsJSON, }, - ExtraContent: &providers.ExtraContent{ - ToolFeedbackExplanation: explanation, - }, }}, }) if err != nil { @@ -827,93 +675,13 @@ func TestHandleGetSession_UsesConfiguredToolFeedbackMaxArgsLength(t *testing.T) t.Fatalf("len(resp.Messages) = %d, want at least 2", len(resp.Messages)) } - wantPreview := utils.Truncate(explanation, 20) + wantPreview := utils.Truncate(argsJSON, 20) if !strings.Contains(resp.Messages[1].Content, wantPreview) { t.Fatalf("tool summary = %q, want preview %q", resp.Messages[1].Content, wantPreview) } if strings.Contains(resp.Messages[1].Content, argsJSON) { t.Fatalf("tool summary = %q, expected configured truncation", resp.Messages[1].Content) } - if !strings.Contains(resp.Messages[1].Content, "`read_file`") { - t.Fatalf("tool summary = %q, want read_file summary", resp.Messages[1].Content) - } -} - -func TestHandleGetSession_FallsBackToLegacyToolArgumentsWhenExplanationMissing(t *testing.T) { - configPath, cleanup := setupOAuthTestEnv(t) - defer cleanup() - - cfg, err := config.LoadConfig(configPath) - if err != nil { - t.Fatalf("LoadConfig() error = %v", err) - } - cfg.Agents.Defaults.ToolFeedback.MaxArgsLength = 20 - err = config.SaveConfig(configPath, cfg) - if err != nil { - t.Fatalf("SaveConfig() error = %v", err) - } - - dir := sessionsTestDir(t, configPath) - store, err := memory.NewJSONLStore(dir) - if err != nil { - t.Fatalf("NewJSONLStore() error = %v", err) - } - - argsJSON := `{"path":"README.md","start_line":1,"end_line":10,"extra":"abcdefghijklmnopqrstuvwxyz"}` - sessionKey := picoSessionPrefix + "detail-tool-summary-legacy-args" - if err := store.AddFullMessage( - nil, - sessionKey, - providers.Message{Role: "user", Content: "check file"}, - ); err != nil { - t.Fatalf("AddFullMessage(user) error = %v", err) - } - if err := store.AddFullMessage(nil, sessionKey, providers.Message{ - Role: "assistant", - ToolCalls: []providers.ToolCall{{ - ID: "call_1", - Type: "function", - Function: &providers.FunctionCall{ - Name: "read_file", - Arguments: argsJSON, - }, - }}, - }); err != nil { - t.Fatalf("AddFullMessage(assistant) error = %v", err) - } - - h := NewHandler(configPath) - mux := http.NewServeMux() - h.RegisterRoutes(mux) - - rec := httptest.NewRecorder() - req := httptest.NewRequest(http.MethodGet, "/api/sessions/detail-tool-summary-legacy-args", nil) - mux.ServeHTTP(rec, req) - - if rec.Code != http.StatusOK { - t.Fatalf("status = %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String()) - } - - var resp struct { - Messages []struct { - Role string `json:"role"` - Content string `json:"content"` - } `json:"messages"` - } - if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil { - t.Fatalf("Unmarshal() error = %v", err) - } - if len(resp.Messages) < 2 { - t.Fatalf("len(resp.Messages) = %d, want at least 2", len(resp.Messages)) - } - - wantPreview := utils.Truncate(argsJSON, 20) - if !strings.Contains(resp.Messages[1].Content, "`read_file`") { - t.Fatalf("tool summary = %q, want read_file summary", resp.Messages[1].Content) - } - if !strings.Contains(resp.Messages[1].Content, wantPreview) { - t.Fatalf("tool summary = %q, want legacy args preview %q", resp.Messages[1].Content, wantPreview) - } } func TestHandleGetSession_IncludesMediaOnlyMessages(t *testing.T) { diff --git a/web/frontend/src/i18n/locales/en.json b/web/frontend/src/i18n/locales/en.json index 7a5c58b30..c96d4b71b 100644 --- a/web/frontend/src/i18n/locales/en.json +++ b/web/frontend/src/i18n/locales/en.json @@ -592,9 +592,9 @@ "split_on_marker": "Chatty Mode", "split_on_marker_hint": "Split long messages into short ones like real human chatting.", "tool_feedback_enabled": "Tool Feedback", - "tool_feedback_enabled_hint": "Send a short execution note into the current chat before each tool runs.", - "tool_feedback_max_args_length": "Tool Feedback Length", - "tool_feedback_max_args_length_hint": "Maximum number of characters shown in each tool feedback message. Set to 0 to use the default.", + "tool_feedback_enabled_hint": "Send a short tool-call preview into the current chat before each tool execution.", + "tool_feedback_max_args_length": "Tool Feedback Args Preview Length", + "tool_feedback_max_args_length_hint": "Maximum number of argument characters shown in each tool feedback message. Set to 0 to use the default.", "exec_enabled": "Allow Commands", "exec_enabled_hint": "Enable or disable command execution for the app. When disabled, no command requests will run.", "allow_remote": "Allow Remote Commands", diff --git a/web/frontend/src/i18n/locales/zh.json b/web/frontend/src/i18n/locales/zh.json index aaebfa625..4a9e59cf4 100644 --- a/web/frontend/src/i18n/locales/zh.json +++ b/web/frontend/src/i18n/locales/zh.json @@ -592,9 +592,9 @@ "split_on_marker": "连续短消息", "split_on_marker_hint": "像真人聊天一样,把长难句拆成多条短消息快速发出", "tool_feedback_enabled": "工具反馈", - "tool_feedback_enabled_hint": "在每次执行工具前,先向当前会话发送一条简短的执行说明", - "tool_feedback_max_args_length": "工具反馈长度", - "tool_feedback_max_args_length_hint": "每条工具反馈消息中展示的字符上限。设为 0 时使用默认值", + "tool_feedback_enabled_hint": "在每次执行工具前,先向当前会话发送一条简短的工具调用预览", + "tool_feedback_max_args_length": "工具反馈参数预览长度", + "tool_feedback_max_args_length_hint": "每条工具反馈消息中展示的参数字符上限。设为 0 时使用默认值", "exec_enabled": "允许命令执行", "exec_enabled_hint": "控制应用是否允许执行命令。关闭后,所有命令请求都不会执行", "allow_remote": "允许远程命令执行", From 36a583818286fa59eec95d17841b9c36fd8235e5 Mon Sep 17 00:00:00 2001 From: stevef Date: Mon, 20 Apr 2026 19:27:22 +0200 Subject: [PATCH 7/8] added freeride skill --- cmd/freeride-diag/main.go | 162 ++++ cmd/picoclaw/internal/freeride/command.go | 104 +++ .../internal/onboard/workspace/AGENT.md | 45 + .../internal/onboard/workspace/SOUL.md | 19 + .../internal/onboard/workspace/USER.md | 21 + .../onboard/workspace/memory/MEMORY.md | 21 + .../workspace/skills/agent-browser/SKILL.md | 129 +++ .../workspace/skills/freeride/SKILL.md | 17 + .../onboard/workspace/skills/github/SKILL.md | 48 + .../workspace/skills/hardware/SKILL.md | 64 ++ .../hardware/references/board-pinout.md | 131 +++ .../hardware/references/common-devices.md | 78 ++ .../workspace/skills/skill-creator/SKILL.md | 371 ++++++++ .../workspace/skills/summarize/SKILL.md | 67 ++ .../onboard/workspace/skills/tmux/SKILL.md | 121 +++ .../skills/tmux/scripts/find-sessions.sh | 112 +++ .../skills/tmux/scripts/wait-for-text.sh | 83 ++ .../onboard/workspace/skills/weather/SKILL.md | 59 ++ docs/guides/freeride.md | 134 +++ examples/freeride-config.json | 854 ++++++++++++++++++ pkg/agent/instance.go | 3 + pkg/agent/loop_init.go | 5 + pkg/agent/model_resolution.go | 16 +- pkg/config/config.go | 10 +- pkg/config/config_struct.go | 4 + pkg/config/envkeys.go | 7 + pkg/providers/cooldown.go | 62 +- pkg/providers/cooldown_test.go | 71 ++ pkg/providers/error_classifier.go | 2 + pkg/providers/error_classifier_test.go | 2 + pkg/providers/factory_provider.go | 41 +- pkg/providers/factory_provider_test.go | 15 +- pkg/providers/openai_compat/provider.go | 1 - pkg/providers/types.go | 1 + pkg/tools/freeride.go | 404 +++++++++ pkg/tools/freeride_test.go | 316 +++++++ scratch/check_paths.go | 28 + workspace/skills/freeride/SKILL.md | 17 + 38 files changed, 3616 insertions(+), 29 deletions(-) create mode 100644 cmd/freeride-diag/main.go create mode 100644 cmd/picoclaw/internal/freeride/command.go create mode 100644 cmd/picoclaw/internal/onboard/workspace/AGENT.md create mode 100644 cmd/picoclaw/internal/onboard/workspace/SOUL.md create mode 100644 cmd/picoclaw/internal/onboard/workspace/USER.md create mode 100644 cmd/picoclaw/internal/onboard/workspace/memory/MEMORY.md create mode 100644 cmd/picoclaw/internal/onboard/workspace/skills/agent-browser/SKILL.md create mode 100644 cmd/picoclaw/internal/onboard/workspace/skills/freeride/SKILL.md create mode 100644 cmd/picoclaw/internal/onboard/workspace/skills/github/SKILL.md create mode 100644 cmd/picoclaw/internal/onboard/workspace/skills/hardware/SKILL.md create mode 100644 cmd/picoclaw/internal/onboard/workspace/skills/hardware/references/board-pinout.md create mode 100644 cmd/picoclaw/internal/onboard/workspace/skills/hardware/references/common-devices.md create mode 100644 cmd/picoclaw/internal/onboard/workspace/skills/skill-creator/SKILL.md create mode 100644 cmd/picoclaw/internal/onboard/workspace/skills/summarize/SKILL.md create mode 100644 cmd/picoclaw/internal/onboard/workspace/skills/tmux/SKILL.md create mode 100755 cmd/picoclaw/internal/onboard/workspace/skills/tmux/scripts/find-sessions.sh create mode 100755 cmd/picoclaw/internal/onboard/workspace/skills/tmux/scripts/wait-for-text.sh create mode 100644 cmd/picoclaw/internal/onboard/workspace/skills/weather/SKILL.md create mode 100644 docs/guides/freeride.md create mode 100644 examples/freeride-config.json create mode 100644 pkg/tools/freeride.go create mode 100644 pkg/tools/freeride_test.go create mode 100644 scratch/check_paths.go create mode 100644 workspace/skills/freeride/SKILL.md diff --git a/cmd/freeride-diag/main.go b/cmd/freeride-diag/main.go new file mode 100644 index 000000000..927c40c7e --- /dev/null +++ b/cmd/freeride-diag/main.go @@ -0,0 +1,162 @@ +package main + +import ( + "bytes" + "encoding/json" + "fmt" + "io" + "net/http" + "os" + "sort" + "strings" + "time" +) + +type Model struct { + ID string `json:"id"` + Name string `json:"name"` + ContextLength int `json:"context_length"` + Pricing struct { + Prompt string `json:"prompt"` + Completion string `json:"completion"` + } `json:"pricing"` + Created int64 `json:"created"` + Score float64 + LastError string + IsReachable bool +} + +func main() { + apiKey := os.Getenv("OPENROUTER_API_KEY") + if apiKey == "" { + fmt.Println("❌ Error: OPENROUTER_API_KEY environment variable is not set.") + os.Exit(1) + } + + fmt.Println("🔍 Fetching all models from OpenRouter...") + models, err := fetchModels(apiKey) + if err != nil { + fmt.Printf("❌ Failed to fetch models: %v\n", err) + os.Exit(1) + } + + var freeModels []Model + for _, m := range models { + if m.Pricing.Prompt == "0" && m.Pricing.Completion == "0" { + // Scoring logic (same as tool) + score := 0.0 + score += float64(m.ContextLength) / 128000.0 * 0.4 + if m.Created > 0 { + ageInDays := float64(time.Now().Unix()-m.Created) / 86400.0 + if ageInDays < 365 { + score += (1.0 - ageInDays/365.0) * 0.2 + } + } + m.Score = score + freeModels = append(freeModels, m) + } + } + + sort.Slice(freeModels, func(i, j int) bool { + return freeModels[i].Score > freeModels[j].Score + }) + + fmt.Printf("✅ Found %d free models. Testing connectivity until we find 3 working ones...\n\n", len(freeModels)) + + successCount := 0 + for i := range freeModels { + if successCount >= 3 { + break + } + m := &freeModels[i] + fmt.Printf("[%d/%d] Testing %s... ", i+1, len(freeModels), m.ID) + + err := testModel(apiKey, m.ID) + if err == nil { + m.IsReachable = true + successCount++ + fmt.Println("✅ OK") + } else { + m.LastError = err.Error() + fmt.Printf("❌ FAIL (%v)\n", err) + } + } + + fmt.Println("\n--- FINAL RECOMMENDATIONS ---") + header := fmt.Sprintf("%-50s | %-15s | %-10s", "Model ID", "Context", "Status") + fmt.Println(header) + fmt.Println(strings.Repeat("-", len(header))) + + for i, m := range freeModels { + if i >= 10 { + break + } + status := "Unknown" + if i < 5 { + if m.IsReachable { + status = "✅ OK" + } else { + status = "❌ FAIL" + } + } + fmt.Printf("%-50s | %-15d | %-10s\n", m.ID, m.ContextLength, status) + } + + for _, m := range freeModels { + if m.IsReachable { + fmt.Printf( + "\n🚀 SUCCESS! Use this model for testing: \n go run cmd/picoclaw/main.go agent --model openrouter/%s\n", + m.ID, + ) + break + } + } +} + +func fetchModels(apiKey string) ([]Model, error) { + req, _ := http.NewRequest("GET", "https://openrouter.ai/api/v1/models", nil) + req.Header.Set("Authorization", "Bearer "+apiKey) + + resp, err := http.DefaultClient.Do(req) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + var result struct { + Data []Model `json:"data"` + } + if err := json.NewDecoder(resp.Body).Decode(&result); err != nil { + return nil, err + } + return result.Data, nil +} + +func testModel(apiKey, modelID string) error { + payload := map[string]any{ + "model": modelID, + "messages": []map[string]string{ + {"role": "user", "content": "ping"}, + }, + "max_tokens": 10, + } + body, _ := json.Marshal(payload) + + req, _ := http.NewRequest("POST", "https://openrouter.ai/api/v1/chat/completions", bytes.NewReader(body)) + req.Header.Set("Authorization", "Bearer "+apiKey) + req.Header.Set("Content-Type", "application/json") + + client := &http.Client{Timeout: 10 * time.Second} + resp, err := client.Do(req) + if err != nil { + return err + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + respBody, _ := io.ReadAll(resp.Body) + return fmt.Errorf("HTTP %d: %s", resp.StatusCode, string(respBody)) + } + + return nil +} diff --git a/cmd/picoclaw/internal/freeride/command.go b/cmd/picoclaw/internal/freeride/command.go new file mode 100644 index 000000000..ed765363c --- /dev/null +++ b/cmd/picoclaw/internal/freeride/command.go @@ -0,0 +1,104 @@ +package freeride + +import ( + "context" + "fmt" + + "github.com/spf13/cobra" + + "github.com/sipeed/picoclaw/cmd/picoclaw/internal" + "github.com/sipeed/picoclaw/pkg/tools" +) + +// NewFreerideCommand returns a new cobra.Command for managing OpenRouter free models. +func NewFreerideCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "freeride", + Short: "Manage OpenRouter free models and fallbacks", + Long: "FreeRide automatically discovers and configures OpenRouter's best free models as fallbacks for your PicoClaw agent.", + RunE: func(cmd *cobra.Command, args []string) error { + return cmd.Help() + }, + } + + cmd.AddCommand( + newListCommand(), + newAutoCommand(), + newStatusCommand(), + newSetTimeoutCommand(), + ) + + return cmd +} + +func newListCommand() *cobra.Command { + var limit int + cmd := &cobra.Command{ + Use: "list", + Short: "List available free models from OpenRouter", + RunE: func(cmd *cobra.Command, args []string) error { + t := tools.NewFreeRideTool(internal.GetConfigPath(), nil) + result := t.Execute(context.Background(), map[string]any{ + "command": "list", + "limit": float64(limit), + }) + fmt.Println(result.ForLLM) + return nil + }, + } + cmd.Flags().IntVarP(&limit, "limit", "l", 10, "Number of models to list") + return cmd +} + +func newAutoCommand() *cobra.Command { + var limit int + cmd := &cobra.Command{ + Use: "auto", + Short: "Automatically configure best free models as fallbacks", + RunE: func(cmd *cobra.Command, args []string) error { + t := tools.NewFreeRideTool(internal.GetConfigPath(), nil) + result := t.Execute(context.Background(), map[string]any{ + "command": "auto", + "limit": float64(limit), + }) + fmt.Println(result.ForLLM) + return nil + }, + } + cmd.Flags().IntVarP(&limit, "limit", "l", 5, "Number of fallbacks to configure") + return cmd +} + +func newStatusCommand() *cobra.Command { + return &cobra.Command{ + Use: "status", + Short: "Check current FreeRide configuration", + RunE: func(cmd *cobra.Command, args []string) error { + t := tools.NewFreeRideTool(internal.GetConfigPath(), nil) + result := t.Execute(context.Background(), map[string]any{ + "command": "status", + }) + fmt.Println(result.ForLLM) + return nil + }, + } +} + +func newSetTimeoutCommand() *cobra.Command { + var timeout int + cmd := &cobra.Command{ + Use: "settimeout", + Short: "Set request timeout for all OpenRouter models", + RunE: func(cmd *cobra.Command, args []string) error { + t := tools.NewFreeRideTool(internal.GetConfigPath(), nil) + result := t.Execute(context.Background(), map[string]any{ + "command": "settimeout", + "timeout": float64(timeout), + }) + fmt.Println(result.ForLLM) + return nil + }, + } + cmd.Flags().IntVarP(&timeout, "timeout", "t", 300, "Request timeout in seconds (default 300)") + return cmd +} diff --git a/cmd/picoclaw/internal/onboard/workspace/AGENT.md b/cmd/picoclaw/internal/onboard/workspace/AGENT.md new file mode 100644 index 000000000..08f55a1b7 --- /dev/null +++ b/cmd/picoclaw/internal/onboard/workspace/AGENT.md @@ -0,0 +1,45 @@ +--- +name: pico +description: > + The default general-purpose assistant for everyday conversation, problem + solving, and workspace help. +--- + +You are Pico, the default assistant for this workspace. +Your name is PicoClaw 🦞. +## Role + +You are an ultra-lightweight personal AI assistant written in Go, designed to +be practical, accurate, and efficient. + +## Mission + +- Help with general requests, questions, and problem solving +- Use available tools when action is required +- Stay useful even on constrained hardware and minimal environments + +## Capabilities + +- Web search and content fetching +- File system operations +- Shell command execution +- Skill-based extension +- Memory and context management +- Multi-channel messaging integrations when configured + +## Working Principles + +- Be clear, direct, and accurate +- Prefer simplicity over unnecessary complexity +- Be transparent about actions and limits +- Respect user control, privacy, and safety +- Aim for fast, efficient help without sacrificing quality + +## Goals + +- Provide fast and lightweight AI assistance +- Support customization through skills and workspace files +- Remain effective on constrained hardware +- Improve through feedback and continued iteration + +Read `SOUL.md` as part of your identity and communication style. diff --git a/cmd/picoclaw/internal/onboard/workspace/SOUL.md b/cmd/picoclaw/internal/onboard/workspace/SOUL.md new file mode 100644 index 000000000..8a6371ff9 --- /dev/null +++ b/cmd/picoclaw/internal/onboard/workspace/SOUL.md @@ -0,0 +1,19 @@ +# Soul + +I am PicoClaw: calm, helpful, and practical. + +## Personality + +- Helpful and friendly +- Concise and to the point +- Curious and eager to learn +- Honest and transparent +- Calm under uncertainty + +## Values + +- Accuracy over speed +- User privacy and safety +- Transparency in actions +- Continuous improvement +- Simplicity over unnecessary complexity diff --git a/cmd/picoclaw/internal/onboard/workspace/USER.md b/cmd/picoclaw/internal/onboard/workspace/USER.md new file mode 100644 index 000000000..9a3419d87 --- /dev/null +++ b/cmd/picoclaw/internal/onboard/workspace/USER.md @@ -0,0 +1,21 @@ +# User + +Information about the user goes here. + +## Preferences + +- Communication style: (casual/formal) +- Timezone: (your timezone) +- Language: (your preferred language) + +## Personal Information + +- Name: (optional) +- Location: (optional) +- Occupation: (optional) + +## Learning Goals + +- What the user wants to learn from AI +- Preferred interaction style +- Areas of interest diff --git a/cmd/picoclaw/internal/onboard/workspace/memory/MEMORY.md b/cmd/picoclaw/internal/onboard/workspace/memory/MEMORY.md new file mode 100644 index 000000000..265271db9 --- /dev/null +++ b/cmd/picoclaw/internal/onboard/workspace/memory/MEMORY.md @@ -0,0 +1,21 @@ +# Long-term Memory + +This file stores important information that should persist across sessions. + +## User Information + +(Important facts about user) + +## Preferences + +(User preferences learned over time) + +## Important Notes + +(Things to remember) + +## Configuration + +- Model preferences +- Channel settings +- Skills enabled \ No newline at end of file diff --git a/cmd/picoclaw/internal/onboard/workspace/skills/agent-browser/SKILL.md b/cmd/picoclaw/internal/onboard/workspace/skills/agent-browser/SKILL.md new file mode 100644 index 000000000..43505996d --- /dev/null +++ b/cmd/picoclaw/internal/onboard/workspace/skills/agent-browser/SKILL.md @@ -0,0 +1,129 @@ +--- +name: agent-browser +description: "Browser automation via agent-browser CLI. Use when the user needs to navigate websites, fill forms, click buttons, take screenshots, extract data, or test web apps." +metadata: {"nanobot":{"emoji":"🌐","requires":{"bins":["agent-browser"]},"install":[{"id":"npm","kind":"npm","package":"agent-browser","global":true,"bins":["agent-browser"],"label":"Install agent-browser (npm)"}]}} +--- + +# Agent Browser + +CLI browser automation via Chrome/Chromium CDP. Install: `npm i -g agent-browser && agent-browser install`. + +**Before using this skill**, verify the tool is available by running `which agent-browser`. If the command is not found, tell the user that browser automation requires the `agent-browser` CLI and Chromium, which are only available in the heavy container image. Do not attempt to install it at runtime. + +## Core Workflow + +1. `agent-browser open ` — navigate +2. `agent-browser snapshot -i` — get interactive elements with refs (`@e1`, `@e2`, ...) +3. Interact using refs — `click @e1`, `fill @e2 "text"` +4. Re-snapshot after any navigation or DOM change — refs are invalidated + +```bash +agent-browser open https://example.com/form +agent-browser snapshot -i +# @e1 [input] "Email", @e2 [input] "Password", @e3 [button] "Submit" +agent-browser fill @e1 "user@example.com" +agent-browser fill @e2 "secret" +agent-browser click @e3 +agent-browser wait --load networkidle +agent-browser snapshot -i +``` + +Chain commands with `&&` when you don't need intermediate output: +```bash +agent-browser open https://example.com && agent-browser wait --load networkidle && agent-browser snapshot -i +``` + +## Commands + +```bash +# Navigation +agent-browser open +agent-browser close + +# Snapshot +agent-browser snapshot -i # Interactive elements with refs +agent-browser snapshot -s "#selector" # Scope to CSS selector + +# Interaction (use @refs from snapshot) +agent-browser click @e1 +agent-browser fill @e2 "text" # Clear + type +agent-browser type @e2 "text" # Type without clearing +agent-browser select @e1 "option" +agent-browser check @e1 +agent-browser press Enter +agent-browser scroll down 500 + +# Get info +agent-browser get text @e1 +agent-browser get url +agent-browser get title + +# Wait +agent-browser wait @e1 # Wait for element +agent-browser wait --load networkidle # Wait for network idle +agent-browser wait --url "**/dashboard" # Wait for URL pattern +agent-browser wait --text "Welcome" # Wait for text +agent-browser wait 2000 # Wait ms + +# Capture +agent-browser screenshot # Screenshot to temp dir +agent-browser screenshot --full # Full page +agent-browser screenshot --annotate # With numbered element labels ([N] -> @eN) +agent-browser pdf output.pdf + +# Semantic locators (when refs unavailable) +agent-browser find text "Sign In" click +agent-browser find label "Email" fill "user@test.com" +agent-browser find role button click --name "Submit" +``` + +## Authentication + +```bash +# Option 1: Import from user's running Chrome +agent-browser --auto-connect state save ./auth.json +agent-browser --state ./auth.json open https://app.example.com + +# Option 2: Persistent profile +agent-browser --profile ~/.myapp open https://app.example.com/login +# ... login once, all future runs are authenticated + +# Option 3: Session name (auto-save/restore) +agent-browser --session-name myapp open https://app.example.com/login +# ... login, close, next run state is restored + +# Option 4: State file +agent-browser state save auth.json +agent-browser state load auth.json +``` + +## Iframes + +Iframe content is inlined in snapshots. Interact with iframe refs directly — no frame switch needed. + +## Parallel Sessions + +```bash +agent-browser --session s1 open https://site-a.com +agent-browser --session s2 open https://site-b.com +agent-browser session list +``` + +## JavaScript Eval + +```bash +agent-browser eval 'document.title' + +# Complex JS — use --stdin to avoid shell quoting issues +agent-browser eval --stdin <<'EVALEOF' +JSON.stringify(Array.from(document.querySelectorAll("a")).map(a => a.href)) +EVALEOF +``` + +## Cleanup + +Always close sessions when done: +```bash +agent-browser close +agent-browser --session s1 close +``` diff --git a/cmd/picoclaw/internal/onboard/workspace/skills/freeride/SKILL.md b/cmd/picoclaw/internal/onboard/workspace/skills/freeride/SKILL.md new file mode 100644 index 000000000..d95c292bc --- /dev/null +++ b/cmd/picoclaw/internal/onboard/workspace/skills/freeride/SKILL.md @@ -0,0 +1,17 @@ +# FreeRide Skill + +FreeRide gives you unlimited free AI in PicoClaw by automatically managing OpenRouter's free models. + +## Usage + +- `/freeride auto`: Auto-configure best model + fallbacks. +- `/freeride list`: See all 30+ free models ranked. +- `/freeride status`: Check your current setup. + +## How it works + +The skill uses the `freeride` tool to fetch free models from OpenRouter, ranks them by context length, capabilities, recency, and provider trust, and then updates your PicoClaw configuration with the best models as fallbacks. + +## Setup + +Ensure you have your OpenRouter API key set in your K3s secrets or environment variables as `OPENROUTER_API_KEY`. diff --git a/cmd/picoclaw/internal/onboard/workspace/skills/github/SKILL.md b/cmd/picoclaw/internal/onboard/workspace/skills/github/SKILL.md new file mode 100644 index 000000000..57d81278d --- /dev/null +++ b/cmd/picoclaw/internal/onboard/workspace/skills/github/SKILL.md @@ -0,0 +1,48 @@ +--- +name: github +description: "Interact with GitHub using the `gh` CLI. Use `gh issue`, `gh pr`, `gh run`, and `gh api` for issues, PRs, CI runs, and advanced queries." +metadata: {"nanobot":{"emoji":"🐙","requires":{"bins":["gh"]},"install":[{"id":"brew","kind":"brew","formula":"gh","bins":["gh"],"label":"Install GitHub CLI (brew)"},{"id":"apt","kind":"apt","package":"gh","bins":["gh"],"label":"Install GitHub CLI (apt)"}]}} +--- + +# GitHub Skill + +Use the `gh` CLI to interact with GitHub. Always specify `--repo owner/repo` when not in a git directory, or use URLs directly. + +## Pull Requests + +Check CI status on a PR: +```bash +gh pr checks 55 --repo owner/repo +``` + +List recent workflow runs: +```bash +gh run list --repo owner/repo --limit 10 +``` + +View a run and see which steps failed: +```bash +gh run view --repo owner/repo +``` + +View logs for failed steps only: +```bash +gh run view --repo owner/repo --log-failed +``` + +## API for Advanced Queries + +The `gh api` command is useful for accessing data not available through other subcommands. + +Get PR with specific fields: +```bash +gh api repos/owner/repo/pulls/55 --jq '.title, .state, .user.login' +``` + +## JSON Output + +Most commands support `--json` for structured output. You can use `--jq` to filter: + +```bash +gh issue list --repo owner/repo --json number,title --jq '.[] | "\(.number): \(.title)"' +``` diff --git a/cmd/picoclaw/internal/onboard/workspace/skills/hardware/SKILL.md b/cmd/picoclaw/internal/onboard/workspace/skills/hardware/SKILL.md new file mode 100644 index 000000000..e89d1b6e7 --- /dev/null +++ b/cmd/picoclaw/internal/onboard/workspace/skills/hardware/SKILL.md @@ -0,0 +1,64 @@ +--- +name: hardware +description: Read and control I2C and SPI peripherals on Sipeed boards (LicheeRV Nano, MaixCAM, NanoKVM). +homepage: https://wiki.sipeed.com/hardware/en/lichee/RV_Nano/1_intro.html +metadata: {"nanobot":{"emoji":"🔧","requires":{"tools":["i2c","spi"]}}} +--- + +# Hardware (I2C / SPI) + +Use the `i2c` and `spi` tools to interact with sensors, displays, and other peripherals connected to the board. + +## Quick Start + +``` +# 1. Find available buses +i2c detect + +# 2. Scan for connected devices +i2c scan (bus: "1") + +# 3. Read from a sensor (e.g. AHT20 temperature/humidity) +i2c read (bus: "1", address: 0x38, register: 0xAC, length: 6) + +# 4. SPI devices +spi list +spi read (device: "2.0", length: 4) +``` + +## Before You Start — Pinmux Setup + +Most I2C/SPI pins are shared with WiFi on Sipeed boards. You must configure pinmux before use. + +See `references/board-pinout.md` for board-specific commands. + +**Common steps:** +1. Stop WiFi if using shared pins: `/etc/init.d/S30wifi stop` +2. Load i2c-dev module: `modprobe i2c-dev` +3. Configure pinmux with `devmem` (board-specific) +4. Verify with `i2c detect` and `i2c scan` + +## Safety + +- **Write operations** require `confirm: true` — always confirm with the user first +- I2C addresses are validated to 7-bit range (0x03-0x77) +- SPI modes are validated (0-3 only) +- Maximum per-transaction: 256 bytes (I2C), 4096 bytes (SPI) + +## Common Devices + +See `references/common-devices.md` for register maps and usage of popular sensors: +AHT20, BME280, SSD1306 OLED, MPU6050 IMU, DS3231 RTC, INA219 power monitor, PCA9685 PWM, and more. + +## Troubleshooting + +| Problem | Solution | +|---------|----------| +| No I2C buses found | `modprobe i2c-dev` and check device tree | +| Permission denied | Run as root or add user to `i2c` group | +| No devices on scan | Check wiring, pull-up resistors (4.7k typical), and pinmux | +| Bus number changed | I2C adapter numbers can shift between boots; use `i2c detect` to find current assignment | +| WiFi stopped working | I2C-1/SPI-2 share pins with WiFi SDIO; can't use both simultaneously | +| `devmem` not found | Download separately or use `busybox devmem` | +| SPI transfer returns all zeros | Check MISO wiring and device power | +| SPI transfer returns all 0xFF | Device not responding; check CS pin and clock polarity (mode) | diff --git a/cmd/picoclaw/internal/onboard/workspace/skills/hardware/references/board-pinout.md b/cmd/picoclaw/internal/onboard/workspace/skills/hardware/references/board-pinout.md new file mode 100644 index 000000000..827dd0613 --- /dev/null +++ b/cmd/picoclaw/internal/onboard/workspace/skills/hardware/references/board-pinout.md @@ -0,0 +1,131 @@ +# Board Pinout & Pinmux Reference + +## LicheeRV Nano (SG2002) + +### I2C Buses + +| Bus | Pins | Notes | +|-----|------|-------| +| I2C-1 | P18 (SCL), P21 (SDA) | **Shared with WiFi SDIO** — must stop WiFi first | +| I2C-3 | Available on header | Check device tree for pin assignment | +| I2C-5 | Software (BitBang) | Slower but no pin conflicts | + +### SPI Buses + +| Bus | Pins | Notes | +|-----|------|-------| +| SPI-2 | P18 (CS), P21 (MISO), P22 (MOSI), P23 (SCK) | **Shared with WiFi** — must stop WiFi first | +| SPI-4 | Software (BitBang) | Slower but no pin conflicts | + +### Setup Steps for I2C-1 + +```bash +# 1. Stop WiFi (shares pins with I2C-1) +/etc/init.d/S30wifi stop + +# 2. Configure pinmux for I2C-1 +devmem 0x030010D0 b 0x2 # P18 → I2C1_SCL +devmem 0x030010DC b 0x2 # P21 → I2C1_SDA + +# 3. Load i2c-dev module +modprobe i2c-dev + +# 4. Verify +ls /dev/i2c-* +``` + +### Setup Steps for SPI-2 + +```bash +# 1. Stop WiFi (shares pins with SPI-2) +/etc/init.d/S30wifi stop + +# 2. Configure pinmux for SPI-2 +devmem 0x030010D0 b 0x1 # P18 → SPI2_CS +devmem 0x030010DC b 0x1 # P21 → SPI2_MISO +devmem 0x030010E0 b 0x1 # P22 → SPI2_MOSI +devmem 0x030010E4 b 0x1 # P23 → SPI2_SCK + +# 3. Verify +ls /dev/spidev* +``` + +### Max Tested SPI Speed +- SPI-2 hardware: tested up to **93 MHz** +- `spidev_test` is pre-installed on the official image for loopback testing + +--- + +## MaixCAM + +### I2C Buses + +| Bus | Pins | Notes | +|-----|------|-------| +| I2C-1 | Overlaps with WiFi | Not recommended | +| I2C-3 | Overlaps with WiFi | Not recommended | +| I2C-5 | A15 (SCL), A27 (SDA) | **Recommended** — software I2C, no conflicts | + +### Setup Steps for I2C-5 + +```bash +# Configure pins using pinmap utility +# (MaixCAM uses a pinmap tool instead of devmem) +# Refer to: https://wiki.sipeed.com/hardware/en/maixcam/gpio.html + +# Load i2c-dev +modprobe i2c-dev + +# Verify +ls /dev/i2c-* +``` + +--- + +## MaixCAM2 + +### I2C Buses + +| Bus | Pins | Notes | +|-----|------|-------| +| I2C-6 | A1 (SCL), A0 (SDA) | Available on header | +| I2C-7 | Available | Check device tree | + +### Setup Steps + +```bash +# Configure pinmap for I2C-6 +# A1 → I2C6_SCL, A0 → I2C6_SDA +# Refer to MaixCAM2 documentation for pinmap commands + +modprobe i2c-dev +ls /dev/i2c-* +``` + +--- + +## NanoKVM + +Uses the same SG2002 SoC as LicheeRV Nano. GPIO and I2C access follows the same pinmux procedure. Refer to the LicheeRV Nano section above. + +Check NanoKVM-specific pin headers for available I2C/SPI lines: +- https://wiki.sipeed.com/hardware/en/kvm/NanoKVM/introduction.html + +--- + +## Common Issues + +### devmem not found +The `devmem` utility may not be in the default image. Options: +- Use `busybox devmem` if busybox is installed +- Download devmem from the Sipeed package repository +- Cross-compile from source (single C file) + +### Dynamic bus numbering +I2C adapter numbers can change between boots depending on driver load order. Always use `i2c detect` to find current bus assignments rather than hardcoding bus numbers. + +### Permissions +`/dev/i2c-*` and `/dev/spidev*` typically require root access. Options: +- Run picoclaw as root +- Add user to `i2c` and `spi` groups +- Create udev rules: `SUBSYSTEM=="i2c-dev", MODE="0666"` diff --git a/cmd/picoclaw/internal/onboard/workspace/skills/hardware/references/common-devices.md b/cmd/picoclaw/internal/onboard/workspace/skills/hardware/references/common-devices.md new file mode 100644 index 000000000..715e8ab7f --- /dev/null +++ b/cmd/picoclaw/internal/onboard/workspace/skills/hardware/references/common-devices.md @@ -0,0 +1,78 @@ +# Common I2C/SPI Device Reference + +## I2C Devices + +### AHT20 — Temperature & Humidity +- **Address:** 0x38 +- **Init:** Write `[0xBE, 0x08, 0x00]` then wait 10ms +- **Measure:** Write `[0xAC, 0x33, 0x00]`, wait 80ms, read 6 bytes +- **Parse:** Status=byte[0], Humidity=(byte[1]<<12|byte[2]<<4|byte[3]>>4)/2^20*100, Temp=(byte[3]&0x0F<<16|byte[4]<<8|byte[5])/2^20*200-50 +- **Notes:** No register addressing — write command bytes directly (omit `register` param) + +### BME280 / BMP280 — Temperature, Humidity, Pressure +- **Address:** 0x76 or 0x77 (SDO pin selects) +- **Chip ID register:** 0xD0 → BMP280=0x58, BME280=0x60 +- **Data registers:** 0xF7-0xFE (pressure, temperature, humidity) +- **Config:** Write 0xF2 (humidity oversampling), 0xF4 (temp/press oversampling + mode), 0xF5 (standby, filter) +- **Forced measurement:** Write `[0x25]` to register 0xF4, wait 40ms, read 8 bytes from 0xF7 +- **Calibration:** Read 26 bytes from 0x88 and 7 bytes from 0xE1 for compensation formulas +- **Also available via SPI** (mode 0 or 3) + +### SSD1306 — 128x64 OLED Display +- **Address:** 0x3C (or 0x3D if SA0 high) +- **Command prefix:** 0x00 (write to register 0x00) +- **Data prefix:** 0x40 (write to register 0x40) +- **Init sequence:** `[0xAE, 0xD5, 0x80, 0xA8, 0x3F, 0xD3, 0x00, 0x40, 0x8D, 0x14, 0x20, 0x00, 0xA1, 0xC8, 0xDA, 0x12, 0x81, 0xCF, 0xD9, 0xF1, 0xDB, 0x40, 0xA4, 0xA6, 0xAF]` +- **Display on:** 0xAF, **Display off:** 0xAE +- **Also available via SPI** (faster, recommended for animations) + +### MPU6050 — 6-axis Accelerometer + Gyroscope +- **Address:** 0x68 (or 0x69 if AD0 high) +- **WHO_AM_I:** Register 0x75 → should return 0x68 +- **Wake up:** Write `[0x00]` to register 0x6B (clear sleep bit) +- **Read accel:** 6 bytes from register 0x3B (XH,XL,YH,YL,ZH,ZL) — signed 16-bit, default ±2g +- **Read gyro:** 6 bytes from register 0x43 — signed 16-bit, default ±250°/s +- **Read temp:** 2 bytes from register 0x41 — Temp°C = value/340 + 36.53 + +### DS3231 — Real-Time Clock +- **Address:** 0x68 +- **Read time:** 7 bytes from register 0x00 (seconds, minutes, hours, day, date, month, year) — BCD encoded +- **Set time:** Write 7 BCD bytes to register 0x00 +- **Temperature:** 2 bytes from register 0x11 (signed, 0.25°C resolution) +- **Status:** Register 0x0F — bit 2 = busy, bit 0 = alarm 1 flag + +### INA219 — Current & Power Monitor +- **Address:** 0x40-0x4F (A0,A1 pin selectable) +- **Config:** Register 0x00 — set voltage range, gain, ADC resolution +- **Shunt voltage:** Register 0x01 (signed 16-bit, LSB=10µV) +- **Bus voltage:** Register 0x02 (bits 15:3, LSB=4mV) +- **Power:** Register 0x03 (after calibration) +- **Current:** Register 0x04 (after calibration) +- **Calibration:** Register 0x05 — set based on shunt resistor value + +### PCA9685 — 16-Channel PWM / Servo Controller +- **Address:** 0x40-0x7F (A0-A5 selectable, default 0x40) +- **Mode 1:** Register 0x00 — bit 4=sleep, bit 5=auto-increment +- **Set PWM freq:** Sleep → write prescale to 0xFE → wake. Prescale = round(25MHz / (4096 × freq)) - 1 +- **Channel N on/off:** Registers 0x06+4*N to 0x09+4*N (ON_L, ON_H, OFF_L, OFF_H) +- **Servo 0°-180°:** ON=0, OFF=150-600 (at 50Hz). Typical: 0°=150, 90°=375, 180°=600 + +### AT24C256 — 256Kbit EEPROM +- **Address:** 0x50-0x57 (A0-A2 selectable) +- **Read:** Write 2-byte address (high, low), then read N bytes +- **Write:** Write 2-byte address + up to 64 bytes (page write), wait 5ms for write cycle +- **Page size:** 64 bytes. Writes that cross page boundary wrap around. + +## SPI Devices + +### MCP3008 — 8-Channel 10-bit ADC +- **Interface:** SPI mode 0, max 3.6 MHz @ 5V +- **Read channel N:** Send `[0x01, (0x80 | N<<4), 0x00]`, result in last 10 bits of bytes 1-2 +- **Formula:** value = ((byte[1] & 0x03) << 8) | byte[2] +- **Voltage:** value × Vref / 1024 + +### W25Q128 — 128Mbit SPI Flash +- **Interface:** SPI mode 0 or 3, up to 104 MHz +- **Read ID:** Send `[0x9F, 0, 0, 0]` → manufacturer + device ID +- **Read data:** Send `[0x03, addr_high, addr_mid, addr_low]` + N zero bytes +- **Status:** Send `[0x05, 0]` → bit 0 = BUSY diff --git a/cmd/picoclaw/internal/onboard/workspace/skills/skill-creator/SKILL.md b/cmd/picoclaw/internal/onboard/workspace/skills/skill-creator/SKILL.md new file mode 100644 index 000000000..9b5eb6fea --- /dev/null +++ b/cmd/picoclaw/internal/onboard/workspace/skills/skill-creator/SKILL.md @@ -0,0 +1,371 @@ +--- +name: skill-creator +description: Create or update AgentSkills. Use when designing, structuring, or packaging skills with scripts, references, and assets. +--- + +# Skill Creator + +This skill provides guidance for creating effective skills. + +## About Skills + +Skills are modular, self-contained packages that extend the agent's capabilities by providing +specialized knowledge, workflows, and tools. Think of them as "onboarding guides" for specific +domains or tasks—they transform the agent from a general-purpose agent into a specialized agent +equipped with procedural knowledge that no model can fully possess. + +### What Skills Provide + +1. Specialized workflows - Multi-step procedures for specific domains +2. Tool integrations - Instructions for working with specific file formats or APIs +3. Domain expertise - Company-specific knowledge, schemas, business logic +4. Bundled resources - Scripts, references, and assets for complex and repetitive tasks + +## Core Principles + +### Concise is Key + +The context window is a public good. Skills share the context window with everything else the agent needs: system prompt, conversation history, other Skills' metadata, and the actual user request. + +**Default assumption: the agent is already very smart.** Only add context the agent doesn't already have. Challenge each piece of information: "Does the agent really need this explanation?" and "Does this paragraph justify its token cost?" + +Prefer concise examples over verbose explanations. + +### Set Appropriate Degrees of Freedom + +Match the level of specificity to the task's fragility and variability: + +**High freedom (text-based instructions)**: Use when multiple approaches are valid, decisions depend on context, or heuristics guide the approach. + +**Medium freedom (pseudocode or scripts with parameters)**: Use when a preferred pattern exists, some variation is acceptable, or configuration affects behavior. + +**Low freedom (specific scripts, few parameters)**: Use when operations are fragile and error-prone, consistency is critical, or a specific sequence must be followed. + +Think of the agent as exploring a path: a narrow bridge with cliffs needs specific guardrails (low freedom), while an open field allows many routes (high freedom). + +### Anatomy of a Skill + +Every skill consists of a required SKILL.md file and optional bundled resources: + +``` +skill-name/ +├── SKILL.md (required) +│ ├── YAML frontmatter metadata (required) +│ │ ├── name: (required) +│ │ └── description: (required) +│ └── Markdown instructions (required) +└── Bundled Resources (optional) + ├── scripts/ - Executable code (Python/Bash/etc.) + ├── references/ - Documentation intended to be loaded into context as needed + └── assets/ - Files used in output (templates, icons, fonts, etc.) +``` + +#### SKILL.md (required) + +Every SKILL.md consists of: + +- **Frontmatter** (YAML): Contains `name` and `description` fields. These are the only fields that the agent reads to determine when the skill gets used, thus it is very important to be clear and comprehensive in describing what the skill is, and when it should be used. +- **Body** (Markdown): Instructions and guidance for using the skill. Only loaded AFTER the skill triggers (if at all). + +#### Bundled Resources (optional) + +##### Scripts (`scripts/`) + +Executable code (Python/Bash/etc.) for tasks that require deterministic reliability or are repeatedly rewritten. + +- **When to include**: When the same code is being rewritten repeatedly or deterministic reliability is needed +- **Example**: `scripts/rotate_pdf.py` for PDF rotation tasks +- **Benefits**: Token efficient, deterministic, may be executed without loading into context +- **Note**: Scripts may still need to be read by the agent for patching or environment-specific adjustments + +##### References (`references/`) + +Documentation and reference material intended to be loaded as needed into context to inform the agent's process and thinking. + +- **When to include**: For documentation that the agent should reference while working +- **Examples**: `references/finance.md` for financial schemas, `references/mnda.md` for company NDA template, `references/policies.md` for company policies, `references/api_docs.md` for API specifications +- **Use cases**: Database schemas, API documentation, domain knowledge, company policies, detailed workflow guides +- **Benefits**: Keeps SKILL.md lean, loaded only when the agent determines it's needed +- **Best practice**: If files are large (>10k words), include grep search patterns in SKILL.md +- **Avoid duplication**: Information should live in either SKILL.md or references files, not both. Prefer references files for detailed information unless it's truly core to the skill—this keeps SKILL.md lean while making information discoverable without hogging the context window. Keep only essential procedural instructions and workflow guidance in SKILL.md; move detailed reference material, schemas, and examples to references files. + +##### Assets (`assets/`) + +Files not intended to be loaded into context, but rather used within the output the agent produces. + +- **When to include**: When the skill needs files that will be used in the final output +- **Examples**: `assets/logo.png` for brand assets, `assets/slides.pptx` for PowerPoint templates, `assets/frontend-template/` for HTML/React boilerplate, `assets/font.ttf` for typography +- **Use cases**: Templates, images, icons, boilerplate code, fonts, sample documents that get copied or modified +- **Benefits**: Separates output resources from documentation, enables the agent to use files without loading them into context + +#### What to Not Include in a Skill + +A skill should only contain essential files that directly support its functionality. Do NOT create extraneous documentation or auxiliary files, including: + +- README.md +- INSTALLATION_GUIDE.md +- QUICK_REFERENCE.md +- CHANGELOG.md +- etc. + +The skill should only contain the information needed for an AI agent to do the job at hand. It should not contain auxiliary context about the process that went into creating it, setup and testing procedures, user-facing documentation, etc. Creating additional documentation files just adds clutter and confusion. + +### Progressive Disclosure Design Principle + +Skills use a three-level loading system to manage context efficiently: + +1. **Metadata (name + description)** - Always in context (~100 words) +2. **SKILL.md body** - When skill triggers (<5k words) +3. **Bundled resources** - As needed by the agent (Unlimited because scripts can be executed without reading into context window) + +#### Progressive Disclosure Patterns + +Keep SKILL.md body to the essentials and under 500 lines to minimize context bloat. Split content into separate files when approaching this limit. When splitting out content into other files, it is very important to reference them from SKILL.md and describe clearly when to read them, to ensure the reader of the skill knows they exist and when to use them. + +**Key principle:** When a skill supports multiple variations, frameworks, or options, keep only the core workflow and selection guidance in SKILL.md. Move variant-specific details (patterns, examples, configuration) into separate reference files. + +**Pattern 1: High-level guide with references** + +```markdown +# PDF Processing + +## Quick start + +Extract text with pdfplumber: +[code example] + +## Advanced features + +- **Form filling**: See [FORMS.md](FORMS.md) for complete guide +- **API reference**: See [REFERENCE.md](REFERENCE.md) for all methods +- **Examples**: See [EXAMPLES.md](EXAMPLES.md) for common patterns +``` + +the agent loads FORMS.md, REFERENCE.md, or EXAMPLES.md only when needed. + +**Pattern 2: Domain-specific organization** + +For Skills with multiple domains, organize content by domain to avoid loading irrelevant context: + +``` +bigquery-skill/ +├── SKILL.md (overview and navigation) +└── reference/ + ├── finance.md (revenue, billing metrics) + ├── sales.md (opportunities, pipeline) + ├── product.md (API usage, features) + └── marketing.md (campaigns, attribution) +``` + +When a user asks about sales metrics, the agent only reads sales.md. + +Similarly, for skills supporting multiple frameworks or variants, organize by variant: + +``` +cloud-deploy/ +├── SKILL.md (workflow + provider selection) +└── references/ + ├── aws.md (AWS deployment patterns) + ├── gcp.md (GCP deployment patterns) + └── azure.md (Azure deployment patterns) +``` + +When the user chooses AWS, the agent only reads aws.md. + +**Pattern 3: Conditional details** + +Show basic content, link to advanced content: + +```markdown +# DOCX Processing + +## Creating documents + +Use docx-js for new documents. See [DOCX-JS.md](DOCX-JS.md). + +## Editing documents + +For simple edits, modify the XML directly. + +**For tracked changes**: See [REDLINING.md](REDLINING.md) +**For OOXML details**: See [OOXML.md](OOXML.md) +``` + +the agent reads REDLINING.md or OOXML.md only when the user needs those features. + +**Important guidelines:** + +- **Avoid deeply nested references** - Keep references one level deep from SKILL.md. All reference files should link directly from SKILL.md. +- **Structure longer reference files** - For files longer than 100 lines, include a table of contents at the top so the agent can see the full scope when previewing. + +## Skill Creation Process + +Skill creation involves these steps: + +1. Understand the skill with concrete examples +2. Plan reusable skill contents (scripts, references, assets) +3. Initialize the skill (run init_skill.py) +4. Edit the skill (implement resources and write SKILL.md) +5. Package the skill (run package_skill.py) +6. Iterate based on real usage + +Follow these steps in order, skipping only if there is a clear reason why they are not applicable. + +### Skill Naming + +- Use lowercase letters, digits, and hyphens only; normalize user-provided titles to hyphen-case (e.g., "Plan Mode" -> `plan-mode`). +- When generating names, generate a name under 64 characters (letters, digits, hyphens). +- Prefer short, verb-led phrases that describe the action. +- Namespace by tool when it improves clarity or triggering (e.g., `gh-address-comments`, `linear-address-issue`). +- Name the skill folder exactly after the skill name. + +### Step 1: Understanding the Skill with Concrete Examples + +Skip this step only when the skill's usage patterns are already clearly understood. It remains valuable even when working with an existing skill. + +To create an effective skill, clearly understand concrete examples of how the skill will be used. This understanding can come from either direct user examples or generated examples that are validated with user feedback. + +For example, when building an image-editor skill, relevant questions include: + +- "What functionality should the image-editor skill support? Editing, rotating, anything else?" +- "Can you give some examples of how this skill would be used?" +- "I can imagine users asking for things like 'Remove the red-eye from this image' or 'Rotate this image'. Are there other ways you imagine this skill being used?" +- "What would a user say that should trigger this skill?" + +To avoid overwhelming users, avoid asking too many questions in a single message. Start with the most important questions and follow up as needed for better effectiveness. + +Conclude this step when there is a clear sense of the functionality the skill should support. + +### Step 2: Planning the Reusable Skill Contents + +To turn concrete examples into an effective skill, analyze each example by: + +1. Considering how to execute on the example from scratch +2. Identifying what scripts, references, and assets would be helpful when executing these workflows repeatedly + +Example: When building a `pdf-editor` skill to handle queries like "Help me rotate this PDF," the analysis shows: + +1. Rotating a PDF requires re-writing the same code each time +2. A `scripts/rotate_pdf.py` script would be helpful to store in the skill + +Example: When designing a `frontend-webapp-builder` skill for queries like "Build me a todo app" or "Build me a dashboard to track my steps," the analysis shows: + +1. Writing a frontend webapp requires the same boilerplate HTML/React each time +2. An `assets/hello-world/` template containing the boilerplate HTML/React project files would be helpful to store in the skill + +Example: When building a `big-query` skill to handle queries like "How many users have logged in today?" the analysis shows: + +1. Querying BigQuery requires re-discovering the table schemas and relationships each time +2. A `references/schema.md` file documenting the table schemas would be helpful to store in the skill + +To establish the skill's contents, analyze each concrete example to create a list of the reusable resources to include: scripts, references, and assets. + +### Step 3: Initializing the Skill + +At this point, it is time to actually create the skill. + +Skip this step only if the skill being developed already exists, and iteration or packaging is needed. In this case, continue to the next step. + +When creating a new skill from scratch, always run the `init_skill.py` script. The script conveniently generates a new template skill directory that automatically includes everything a skill requires, making the skill creation process much more efficient and reliable. + +Usage: + +```bash +scripts/init_skill.py --path [--resources scripts,references,assets] [--examples] +``` + +Examples: + +```bash +scripts/init_skill.py my-skill --path skills/public +scripts/init_skill.py my-skill --path skills/public --resources scripts,references +scripts/init_skill.py my-skill --path skills/public --resources scripts --examples +``` + +The script: + +- Creates the skill directory at the specified path +- Generates a SKILL.md template with proper frontmatter and TODO placeholders +- Optionally creates resource directories based on `--resources` +- Optionally adds example files when `--examples` is set + +After initialization, customize the SKILL.md and add resources as needed. If you used `--examples`, replace or delete placeholder files. + +### Step 4: Edit the Skill + +When editing the (newly-generated or existing) skill, remember that the skill is being created for another instance of the agent to use. Include information that would be beneficial and non-obvious to the agent. Consider what procedural knowledge, domain-specific details, or reusable assets would help another the agent instance execute these tasks more effectively. + +#### Learn Proven Design Patterns + +Consult these helpful guides based on your skill's needs: + +- **Multi-step processes**: See references/workflows.md for sequential workflows and conditional logic +- **Specific output formats or quality standards**: See references/output-patterns.md for template and example patterns + +These files contain established best practices for effective skill design. + +#### Start with Reusable Skill Contents + +To begin implementation, start with the reusable resources identified above: `scripts/`, `references/`, and `assets/` files. Note that this step may require user input. For example, when implementing a `brand-guidelines` skill, the user may need to provide brand assets or templates to store in `assets/`, or documentation to store in `references/`. + +Added scripts must be tested by actually running them to ensure there are no bugs and that the output matches what is expected. If there are many similar scripts, only a representative sample needs to be tested to ensure confidence that they all work while balancing time to completion. + +If you used `--examples`, delete any placeholder files that are not needed for the skill. Only create resource directories that are actually required. + +#### Update SKILL.md + +**Writing Guidelines:** Always use imperative/infinitive form. + +##### Frontmatter + +Write the YAML frontmatter with `name` and `description`: + +- `name`: The skill name +- `description`: This is the primary triggering mechanism for your skill, and helps the agent understand when to use the skill. + - Include both what the Skill does and specific triggers/contexts for when to use it. + - Include all "when to use" information here - Not in the body. The body is only loaded after triggering, so "When to Use This Skill" sections in the body are not helpful to the agent. + - Example description for a `docx` skill: "Comprehensive document creation, editing, and analysis with support for tracked changes, comments, formatting preservation, and text extraction. Use when the agent needs to work with professional documents (.docx files) for: (1) Creating new documents, (2) Modifying or editing content, (3) Working with tracked changes, (4) Adding comments, or any other document tasks" + +Do not include any other fields in YAML frontmatter. + +##### Body + +Write instructions for using the skill and its bundled resources. + +### Step 5: Packaging a Skill + +Once development of the skill is complete, it must be packaged into a distributable .skill file that gets shared with the user. The packaging process automatically validates the skill first to ensure it meets all requirements: + +```bash +scripts/package_skill.py +``` + +Optional output directory specification: + +```bash +scripts/package_skill.py ./dist +``` + +The packaging script will: + +1. **Validate** the skill automatically, checking: + + - YAML frontmatter format and required fields + - Skill naming conventions and directory structure + - Description completeness and quality + - File organization and resource references + +2. **Package** the skill if validation passes, creating a .skill file named after the skill (e.g., `my-skill.skill`) that includes all files and maintains the proper directory structure for distribution. The .skill file is a zip file with a .skill extension. + +If validation fails, the script will report the errors and exit without creating a package. Fix any validation errors and run the packaging command again. + +### Step 6: Iterate + +After testing the skill, users may request improvements. Often this happens right after using the skill, with fresh context of how the skill performed. + +**Iteration workflow:** + +1. Use the skill on real tasks +2. Notice struggles or inefficiencies +3. Identify how SKILL.md or bundled resources should be updated +4. Implement changes and test again diff --git a/cmd/picoclaw/internal/onboard/workspace/skills/summarize/SKILL.md b/cmd/picoclaw/internal/onboard/workspace/skills/summarize/SKILL.md new file mode 100644 index 000000000..ca7008e7a --- /dev/null +++ b/cmd/picoclaw/internal/onboard/workspace/skills/summarize/SKILL.md @@ -0,0 +1,67 @@ +--- +name: summarize +description: Summarize or extract text/transcripts from URLs, podcasts, and local files (great fallback for “transcribe this YouTube/video”). +homepage: https://summarize.sh +metadata: {"nanobot":{"emoji":"🧾","requires":{"bins":["summarize"]},"install":[{"id":"brew","kind":"brew","formula":"steipete/tap/summarize","bins":["summarize"],"label":"Install summarize (brew)"}]}} +--- + +# Summarize + +Fast CLI to summarize URLs, local files, and YouTube links. + +## When to use (trigger phrases) + +Use this skill immediately when the user asks any of: +- “use summarize.sh” +- “what’s this link/video about?” +- “summarize this URL/article” +- “transcribe this YouTube/video” (best-effort transcript extraction; no `yt-dlp` needed) + +## Quick start + +```bash +summarize "https://example.com" --model google/gemini-3-flash-preview +summarize "/path/to/file.pdf" --model google/gemini-3-flash-preview +summarize "https://youtu.be/dQw4w9WgXcQ" --youtube auto +``` + +## YouTube: summary vs transcript + +Best-effort transcript (URLs only): + +```bash +summarize "https://youtu.be/dQw4w9WgXcQ" --youtube auto --extract-only +``` + +If the user asked for a transcript but it’s huge, return a tight summary first, then ask which section/time range to expand. + +## Model + keys + +Set the API key for your chosen provider: +- OpenAI: `OPENAI_API_KEY` +- Anthropic: `ANTHROPIC_API_KEY` +- xAI: `XAI_API_KEY` +- Google: `GEMINI_API_KEY` (aliases: `GOOGLE_GENERATIVE_AI_API_KEY`, `GOOGLE_API_KEY`) + +Default model is `google/gemini-3-flash-preview` if none is set. + +## Useful flags + +- `--length short|medium|long|xl|xxl|` +- `--max-output-tokens ` +- `--extract-only` (URLs only) +- `--json` (machine readable) +- `--firecrawl auto|off|always` (fallback extraction) +- `--youtube auto` (Apify fallback if `APIFY_API_TOKEN` set) + +## Config + +Optional config file: `~/.summarize/config.json` + +```json +{ "model": "openai/gpt-5.4" } +``` + +Optional services: +- `FIRECRAWL_API_KEY` for blocked sites +- `APIFY_API_TOKEN` for YouTube fallback diff --git a/cmd/picoclaw/internal/onboard/workspace/skills/tmux/SKILL.md b/cmd/picoclaw/internal/onboard/workspace/skills/tmux/SKILL.md new file mode 100644 index 000000000..f2a3144d8 --- /dev/null +++ b/cmd/picoclaw/internal/onboard/workspace/skills/tmux/SKILL.md @@ -0,0 +1,121 @@ +--- +name: tmux +description: Remote-control tmux sessions for interactive CLIs by sending keystrokes and scraping pane output. +metadata: {"nanobot":{"emoji":"🧵","os":["darwin","linux"],"requires":{"bins":["tmux"]}}} +--- + +# tmux Skill + +Use tmux only when you need an interactive TTY. Prefer exec background mode for long-running, non-interactive tasks. + +## Quickstart (isolated socket, exec tool) + +```bash +SOCKET_DIR="${NANOBOT_TMUX_SOCKET_DIR:-${TMPDIR:-/tmp}/nanobot-tmux-sockets}" +mkdir -p "$SOCKET_DIR" +SOCKET="$SOCKET_DIR/nanobot.sock" +SESSION=nanobot-python + +tmux -S "$SOCKET" new -d -s "$SESSION" -n shell +tmux -S "$SOCKET" send-keys -t "$SESSION":0.0 -- 'PYTHON_BASIC_REPL=1 python3 -q' Enter +tmux -S "$SOCKET" capture-pane -p -J -t "$SESSION":0.0 -S -200 +``` + +After starting a session, always print monitor commands: + +``` +To monitor: + tmux -S "$SOCKET" attach -t "$SESSION" + tmux -S "$SOCKET" capture-pane -p -J -t "$SESSION":0.0 -S -200 +``` + +## Socket convention + +- Use `NANOBOT_TMUX_SOCKET_DIR` environment variable. +- Default socket path: `"$NANOBOT_TMUX_SOCKET_DIR/nanobot.sock"`. + +## Targeting panes and naming + +- Target format: `session:window.pane` (defaults to `:0.0`). +- Keep names short; avoid spaces. +- Inspect: `tmux -S "$SOCKET" list-sessions`, `tmux -S "$SOCKET" list-panes -a`. + +## Finding sessions + +- List sessions on your socket: `{baseDir}/scripts/find-sessions.sh -S "$SOCKET"`. +- Scan all sockets: `{baseDir}/scripts/find-sessions.sh --all` (uses `NANOBOT_TMUX_SOCKET_DIR`). + +## Sending input safely + +- Prefer literal sends: `tmux -S "$SOCKET" send-keys -t target -l -- "$cmd"`. +- Control keys: `tmux -S "$SOCKET" send-keys -t target C-c`. + +## Watching output + +- Capture recent history: `tmux -S "$SOCKET" capture-pane -p -J -t target -S -200`. +- Wait for prompts: `{baseDir}/scripts/wait-for-text.sh -t session:0.0 -p 'pattern'`. +- Attaching is OK; detach with `Ctrl+b d`. + +## Spawning processes + +- For python REPLs, set `PYTHON_BASIC_REPL=1` (non-basic REPL breaks send-keys flows). + +## Windows / WSL + +- tmux is supported on macOS/Linux. On Windows, use WSL and install tmux inside WSL. +- This skill is gated to `darwin`/`linux` and requires `tmux` on PATH. + +## Orchestrating Coding Agents (Codex, Claude Code) + +tmux excels at running multiple coding agents in parallel: + +```bash +SOCKET="${TMPDIR:-/tmp}/codex-army.sock" + +# Create multiple sessions +for i in 1 2 3 4 5; do + tmux -S "$SOCKET" new-session -d -s "agent-$i" +done + +# Launch agents in different workdirs +tmux -S "$SOCKET" send-keys -t agent-1 "cd /tmp/project1 && codex --yolo 'Fix bug X'" Enter +tmux -S "$SOCKET" send-keys -t agent-2 "cd /tmp/project2 && codex --yolo 'Fix bug Y'" Enter + +# Poll for completion (check if prompt returned) +for sess in agent-1 agent-2; do + if tmux -S "$SOCKET" capture-pane -p -t "$sess" -S -3 | grep -q "❯"; then + echo "$sess: DONE" + else + echo "$sess: Running..." + fi +done + +# Get full output from completed session +tmux -S "$SOCKET" capture-pane -p -t agent-1 -S -500 +``` + +**Tips:** +- Use separate git worktrees for parallel fixes (no branch conflicts) +- `pnpm install` first before running codex in fresh clones +- Check for shell prompt (`❯` or `$`) to detect completion +- Codex needs `--yolo` or `--full-auto` for non-interactive fixes + +## Cleanup + +- Kill a session: `tmux -S "$SOCKET" kill-session -t "$SESSION"`. +- Kill all sessions on a socket: `tmux -S "$SOCKET" list-sessions -F '#{session_name}' | xargs -r -n1 tmux -S "$SOCKET" kill-session -t`. +- Remove everything on the private socket: `tmux -S "$SOCKET" kill-server`. + +## Helper: wait-for-text.sh + +`{baseDir}/scripts/wait-for-text.sh` polls a pane for a regex (or fixed string) with a timeout. + +```bash +{baseDir}/scripts/wait-for-text.sh -t session:0.0 -p 'pattern' [-F] [-T 20] [-i 0.5] [-l 2000] +``` + +- `-t`/`--target` pane target (required) +- `-p`/`--pattern` regex to match (required); add `-F` for fixed string +- `-T` timeout seconds (integer, default 15) +- `-i` poll interval seconds (default 0.5) +- `-l` history lines to search (integer, default 1000) diff --git a/cmd/picoclaw/internal/onboard/workspace/skills/tmux/scripts/find-sessions.sh b/cmd/picoclaw/internal/onboard/workspace/skills/tmux/scripts/find-sessions.sh new file mode 100755 index 000000000..00552c684 --- /dev/null +++ b/cmd/picoclaw/internal/onboard/workspace/skills/tmux/scripts/find-sessions.sh @@ -0,0 +1,112 @@ +#!/usr/bin/env bash +set -euo pipefail + +usage() { + cat <<'USAGE' +Usage: find-sessions.sh [-L socket-name|-S socket-path|-A] [-q pattern] + +List tmux sessions on a socket (default tmux socket if none provided). + +Options: + -L, --socket tmux socket name (passed to tmux -L) + -S, --socket-path tmux socket path (passed to tmux -S) + -A, --all scan all sockets under NANOBOT_TMUX_SOCKET_DIR + -q, --query case-insensitive substring to filter session names + -h, --help show this help +USAGE +} + +socket_name="" +socket_path="" +query="" +scan_all=false +socket_dir="${NANOBOT_TMUX_SOCKET_DIR:-${TMPDIR:-/tmp}/nanobot-tmux-sockets}" + +while [[ $# -gt 0 ]]; do + case "$1" in + -L|--socket) socket_name="${2-}"; shift 2 ;; + -S|--socket-path) socket_path="${2-}"; shift 2 ;; + -A|--all) scan_all=true; shift ;; + -q|--query) query="${2-}"; shift 2 ;; + -h|--help) usage; exit 0 ;; + *) echo "Unknown option: $1" >&2; usage; exit 1 ;; + esac +done + +if [[ "$scan_all" == true && ( -n "$socket_name" || -n "$socket_path" ) ]]; then + echo "Cannot combine --all with -L or -S" >&2 + exit 1 +fi + +if [[ -n "$socket_name" && -n "$socket_path" ]]; then + echo "Use either -L or -S, not both" >&2 + exit 1 +fi + +if ! command -v tmux >/dev/null 2>&1; then + echo "tmux not found in PATH" >&2 + exit 1 +fi + +list_sessions() { + local label="$1"; shift + local tmux_cmd=(tmux "$@") + + if ! sessions="$("${tmux_cmd[@]}" list-sessions -F '#{session_name}\t#{session_attached}\t#{session_created_string}' 2>/dev/null)"; then + echo "No tmux server found on $label" >&2 + return 1 + fi + + if [[ -n "$query" ]]; then + sessions="$(printf '%s\n' "$sessions" | grep -i -- "$query" || true)" + fi + + if [[ -z "$sessions" ]]; then + echo "No sessions found on $label" + return 0 + fi + + echo "Sessions on $label:" + printf '%s\n' "$sessions" | while IFS=$'\t' read -r name attached created; do + attached_label=$([[ "$attached" == "1" ]] && echo "attached" || echo "detached") + printf ' - %s (%s, started %s)\n' "$name" "$attached_label" "$created" + done +} + +if [[ "$scan_all" == true ]]; then + if [[ ! -d "$socket_dir" ]]; then + echo "Socket directory not found: $socket_dir" >&2 + exit 1 + fi + + shopt -s nullglob + sockets=("$socket_dir"/*) + shopt -u nullglob + + if [[ "${#sockets[@]}" -eq 0 ]]; then + echo "No sockets found under $socket_dir" >&2 + exit 1 + fi + + exit_code=0 + for sock in "${sockets[@]}"; do + if [[ ! -S "$sock" ]]; then + continue + fi + list_sessions "socket path '$sock'" -S "$sock" || exit_code=$? + done + exit "$exit_code" +fi + +tmux_cmd=(tmux) +socket_label="default socket" + +if [[ -n "$socket_name" ]]; then + tmux_cmd+=(-L "$socket_name") + socket_label="socket name '$socket_name'" +elif [[ -n "$socket_path" ]]; then + tmux_cmd+=(-S "$socket_path") + socket_label="socket path '$socket_path'" +fi + +list_sessions "$socket_label" "${tmux_cmd[@]:1}" diff --git a/cmd/picoclaw/internal/onboard/workspace/skills/tmux/scripts/wait-for-text.sh b/cmd/picoclaw/internal/onboard/workspace/skills/tmux/scripts/wait-for-text.sh new file mode 100755 index 000000000..56354be83 --- /dev/null +++ b/cmd/picoclaw/internal/onboard/workspace/skills/tmux/scripts/wait-for-text.sh @@ -0,0 +1,83 @@ +#!/usr/bin/env bash +set -euo pipefail + +usage() { + cat <<'USAGE' +Usage: wait-for-text.sh -t target -p pattern [options] + +Poll a tmux pane for text and exit when found. + +Options: + -t, --target tmux target (session:window.pane), required + -p, --pattern regex pattern to look for, required + -F, --fixed treat pattern as a fixed string (grep -F) + -T, --timeout seconds to wait (integer, default: 15) + -i, --interval poll interval in seconds (default: 0.5) + -l, --lines number of history lines to inspect (integer, default: 1000) + -h, --help show this help +USAGE +} + +target="" +pattern="" +grep_flag="-E" +timeout=15 +interval=0.5 +lines=1000 + +while [[ $# -gt 0 ]]; do + case "$1" in + -t|--target) target="${2-}"; shift 2 ;; + -p|--pattern) pattern="${2-}"; shift 2 ;; + -F|--fixed) grep_flag="-F"; shift ;; + -T|--timeout) timeout="${2-}"; shift 2 ;; + -i|--interval) interval="${2-}"; shift 2 ;; + -l|--lines) lines="${2-}"; shift 2 ;; + -h|--help) usage; exit 0 ;; + *) echo "Unknown option: $1" >&2; usage; exit 1 ;; + esac +done + +if [[ -z "$target" || -z "$pattern" ]]; then + echo "target and pattern are required" >&2 + usage + exit 1 +fi + +if ! [[ "$timeout" =~ ^[0-9]+$ ]]; then + echo "timeout must be an integer number of seconds" >&2 + exit 1 +fi + +if ! [[ "$lines" =~ ^[0-9]+$ ]]; then + echo "lines must be an integer" >&2 + exit 1 +fi + +if ! command -v tmux >/dev/null 2>&1; then + echo "tmux not found in PATH" >&2 + exit 1 +fi + +# End time in epoch seconds (integer, good enough for polling) +start_epoch=$(date +%s) +deadline=$((start_epoch + timeout)) + +while true; do + # -J joins wrapped lines, -S uses negative index to read last N lines + pane_text="$(tmux capture-pane -p -J -t "$target" -S "-${lines}" 2>/dev/null || true)" + + if printf '%s\n' "$pane_text" | grep $grep_flag -- "$pattern" >/dev/null 2>&1; then + exit 0 + fi + + now=$(date +%s) + if (( now >= deadline )); then + echo "Timed out after ${timeout}s waiting for pattern: $pattern" >&2 + echo "Last ${lines} lines from $target:" >&2 + printf '%s\n' "$pane_text" >&2 + exit 1 + fi + + sleep "$interval" +done diff --git a/cmd/picoclaw/internal/onboard/workspace/skills/weather/SKILL.md b/cmd/picoclaw/internal/onboard/workspace/skills/weather/SKILL.md new file mode 100644 index 000000000..aa90a9b20 --- /dev/null +++ b/cmd/picoclaw/internal/onboard/workspace/skills/weather/SKILL.md @@ -0,0 +1,59 @@ +--- +name: weather +description: Get current weather and forecasts with verified location matching (no API key required). +homepage: https://wttr.in/:help +metadata: {"nanobot":{"emoji":"🌤️","requires":{"bins":["curl"]}}} +--- + +# Weather + +Use the most reliable location match first. For Chinese city names or other non-Latin input, prefer `wttr.in` with the original query because it resolves native names directly. Use Open-Meteo for structured current conditions and forecasts only after you have confirmed the exact city. + +## Accuracy Rules + +- Always restate the matched location, region/country, and observation time in the final answer. +- Do not trust the first geocoding hit blindly. Check `country`, `admin1`, `admin2`, and `population`. +- For Chinese city queries, do not send Hanzi directly to Open-Meteo geocoding unless the top result is obviously correct. Prefer `wttr.in` with the original Chinese name, or geocode the English/pinyin city name instead. +- If multiple plausible matches remain, ask a follow-up question or state the assumption clearly. +- Use `timezone=auto` when calling Open-Meteo so the reported time matches the location. + +## wttr.in (best for direct city-name queries) + +Quick current conditions: +```bash +curl -s "https://wttr.in/London?format=%l:+%c+%t+%h+%w" +``` + +Chinese city example: +```bash +curl -s "https://wttr.in/%E6%88%90%E9%83%BD?format=%l:+%c+%t+%h+%w" +curl -s "https://wttr.in/%E4%B8%8A%E6%B5%B7?format=%l:+%c+%t+%h+%w" +``` + +JSON output if you need more detail: +```bash +curl -s "https://wttr.in/Chengdu?format=j1" +``` + +Tips: +- URL-encode spaces: `New York` -> `New+York` +- URL-encode non-ASCII text before sending the request +- Use `?m` for metric units and `?u` for US units + +## Open-Meteo (best for structured forecasts) + +1. Geocode the city and verify the returned location metadata: +```bash +curl -s "https://geocoding-api.open-meteo.com/v1/search?name=Chengdu&count=3&language=en&format=json" +``` + +2. Query current weather and today's forecast with the verified coordinates: +```bash +curl -s "https://api.open-meteo.com/v1/forecast?latitude=30.66667&longitude=104.06667¤t=temperature_2m,relative_humidity_2m,weather_code,wind_speed_10m&daily=weather_code,temperature_2m_max,temperature_2m_min&forecast_days=1&timezone=auto" +``` + +Important: +- For Chinese inputs like `成都`, geocoding `name=%E6%88%90%E9%83%BD` may return smaller homonym locations first. Prefer `Chengdu` after verifying it matches Sichuan, China. +- If geocoding looks suspicious, fall back to `wttr.in` for the original city name instead of presenting a likely wrong result. + +Docs: https://open-meteo.com/en/docs diff --git a/docs/guides/freeride.md b/docs/guides/freeride.md new file mode 100644 index 000000000..daeacee05 --- /dev/null +++ b/docs/guides/freeride.md @@ -0,0 +1,134 @@ +# FreeRide 🦞 + +FreeRide is a dynamic model rotation and failover system for PicoClaw that leverages OpenRouter's free model pool. It ensures your agent stays alive even if individual free models become rate-limited or go offline. + +## Key Features + +- **Automatic Discovery**: Scans OpenRouter for the best currently available free models. +- **Dynamic Failover**: Automatically rotates through a pool of models when errors (like 429 Rate Limiting) occur. +- **Intelligent Ranking**: Models are scored and ranked based on context length, capabilities (tools/vision), and provider trust. +- **K3s Ready**: Designed to work seamlessly in Kubernetes environments with secure API key management. +- **Visual Provenance (🦞)**: Responses generated via a fallback model are clearly marked with a "lobster" emoji and the model name, providing transparency about which model handled your request. + +## Configuration + +FreeRide is implemented as a native PicoClaw tool. For production environments (especially in the **main branch**), ensure you follow the [Security Configuration](../security/security_configuration.md) to manage your API keys safely. + +### 1. Enable the Tool +Ensure the `skills` tool is enabled in your `config.json` (FreeRide is bundled with the skills system): + +```json +{ + "tools": { + "skills": { + "enabled": true + } + } +} +``` + +### 2. Set the API Key +FreeRide requires an OpenRouter API key. Even for free models, many providers require a key for identification and higher rate limits. + +PicoClaw supports dynamic environment variable resolution using the `env://` scheme. + +In **Local Mode** or **Docker**, set the environment variable: +```bash +export OPENROUTER_API_KEY="sk-or-v1-..." +``` + +Then in your `config.json`, use: +```json +{ + "api_keys": ["env://OPENROUTER_API_KEY"] +} +``` +*(Note: `freeride auto` will automatically configure this for you.)* + +In **K3s Mode**, add the secret to your cluster (see below). + +## Usage + +You can interact with FreeRide directly through the agent: + +### `freeride auto` +**The most important command.** This command: +1. Fetches the current list of ~28+ free models. +2. Ranks them by quality. +3. Automatically populates your `config.json`'s `model_list`. +4. Adds the top 5 models to your agent's `model_fallbacks` list. +5. Reloads the agent configuration instantly. + +### `freeride status` +Shows your current primary model and the active fallback rotation pool. + +### `freeride list [limit]` +Displays the current top-ranked free models available on OpenRouter without modifying your configuration. + +### `freeride settimeout [seconds]` +Sets the request timeout for all OpenRouter models. Default is 300 seconds (5 minutes). Use this if you need longer timeouts for complex tasks: +```bash +picoclaw freeride settimeout 600 # 10 minutes +``` + +## K3s Deployment & Secrets + +When running PicoClaw on K3s, follow these steps to manage your secrets safely. + +### Adding the Secret +If you are creating the secrets for the first time: +```bash +kubectl create secret generic picoclaw-secrets \ + --namespace agi \ + --from-literal=openrouter-api-key="YOUR_KEY_HERE" +``` + +### Updating Existing Secrets (Safe Patching) +If `picoclaw-secrets` already exists and you want to add the OpenRouter key without losing your Telegram or NVIDIA keys, use **`kubectl patch`**: + +```bash +kubectl patch secret picoclaw-secrets \ + --namespace agi \ + --type='json' \ + -p='[{"op": "add", "path": "/data/openrouter-api-key", "value":"'$(echo -n "YOUR_KEY_HERE" | base64 -w0)'"}]' +``` + +### Deployment Configuration +Ensure your `deployment.yaml` maps the secret to the environment variable: + +```yaml +env: + - name: OPENROUTER_API_KEY + valueFrom: + secretKeyRef: + name: picoclaw-secrets + key: openrouter-api-key +``` + +## Cooldown Persistence & Timing ❄️ + +To prevent the agent from "hanging" or retrying known-failed models, PicoClaw uses a two-pronged approach: + +### 1. Zero-Amnesia Persistence +Model failures (e.g., 429 Rate Limits) are saved to `~/.picoclaw/cooldowns.json`. This ensures that if you restart the agent, it **remembers** which models were saturated and skips them instantly. You no longer have to wait through a series of timeouts every time you restart. + +### 2. Generous 5 Minute Timeout +The default request timeout for LLM calls is **300 seconds (5 minutes)**. Free models can be slower than paid ones, and complex agentic tasks (multi-step reasoning, file operations, debugging) need time to complete. If a free model truly can't handle the request, it will return an error rather than hanging indefinitely - allowing the agent to fail over to the next fallback. + +## Troubleshooting + +- **404 Errors**: Ensure the model is still available on OpenRouter using `freeride list`. If it's gone, run `freeride auto` to refresh your fallback pool. +- **429 Rate Limiting**: This is common with free models. PicoClaw will automatically try the next model in your `model_fallbacks` list and persist the cooldown to `cooldowns.json`. + +--- + +## Legal & Responsible Use 🛡️ + +FreeRide is provided for **personal assistance, educational research, and infrastructure failover** purposes only. By using this capability, you acknowledge and agree to the following: + +1. **Terms of Service**: You are responsible for complying with [OpenRouter's Terms of Service](https://openrouter.ai/terms) and the individual "Acceptable Use Policies" of each model provider (e.g., Google, Meta, Mistral). +2. **No Guarantee of Service**: Free models are provided "as-is" by third parties. They may be withdrawn, rate-limited, or modified at any time without notice. +3. **No Reselling**: You should not use FreeRide to build commercial services that "resell" free model access in a way that violates provider licenses (check specific model licenses like Llama 3 Community or Qwen for commercial usage thresholds). +4. **Rate Limit Respect**: PicoClaw handles failover automatically, but users should not use FreeRide to intentionally overwhelm or evade the fair-use rate limits of providers. + +*PicoClaw is an independent tool and is not affiliated with OpenRouter or any specific LLM provider.* diff --git a/examples/freeride-config.json b/examples/freeride-config.json new file mode 100644 index 000000000..20dc82423 --- /dev/null +++ b/examples/freeride-config.json @@ -0,0 +1,854 @@ +{ + "session": { + "dimensions": [ + "chat" + ] + }, + "version": 3, + "isolation": {}, + "agents": { + "defaults": { + "workspace": "/home/stevef/.picoclaw/workspace", + "restrict_to_workspace": true, + "allow_read_outside_workspace": false, + "provider": "nvidia", + "model_name": "nemotron-120b", + "model_fallbacks": [ + "meta-llama-llama-3.3-70b-instruct:free", + "qwen-qwen3-coder:free", + "openrouter-elephant-alpha", + "google-gemma-4-26b-a4b-it:free", + "google-gemma-4-31b-it:free", + "nvidia-nemotron-3-super-120b-a12b:free", + "qwen-qwen3-next-80b-a3b-instruct:free", + "nvidia-nemotron-nano-9b-v2:free" + ], + "max_tokens": 32768, + "max_tool_iterations": 50, + "summarize_message_threshold": 20, + "summarize_token_percent": 75, + "steering_mode": "one-at-a-time", + "subturn": { + "max_depth": 10, + "max_concurrent": 5, + "default_timeout_minutes": 20, + "default_token_budget": 100000, + "concurrency_timeout_sec": 10 + }, + "tool_feedback": { + "enabled": true, + "max_args_length": 300 + }, + "split_on_marker": false + } + }, + "channel_list": { + "dingtalk": { + "enabled": false, + "type": "dingtalk", + "reasoning_channel_id": "", + "group_trigger": {}, + "typing": {}, + "placeholder": { + "enabled": false + }, + "settings": { + "client_id": "" + } + }, + "discord": { + "enabled": false, + "type": "discord", + "reasoning_channel_id": "", + "group_trigger": {}, + "typing": {}, + "placeholder": { + "enabled": false + }, + "settings": { + "proxy": "", + "mention_only": false + } + }, + "feishu": { + "enabled": false, + "type": "feishu", + "reasoning_channel_id": "", + "group_trigger": {}, + "typing": {}, + "placeholder": { + "enabled": false + }, + "settings": { + "app_id": "", + "random_reaction_emoji": [ + "" + ], + "is_lark": false + } + }, + "irc": { + "enabled": false, + "type": "irc", + "allow_from": [ + "" + ], + "reasoning_channel_id": "", + "group_trigger": {}, + "typing": {}, + "placeholder": { + "enabled": false + }, + "settings": { + "server": "", + "tls": false, + "nick": "", + "sasl_user": "", + "channels": [ + "" + ] + } + }, + "line": { + "enabled": false, + "type": "line", + "reasoning_channel_id": "", + "group_trigger": { + "mention_only": true + }, + "typing": {}, + "placeholder": { + "enabled": false + }, + "settings": { + "webhook_host": "0.0.0.0", + "webhook_port": 18791, + "webhook_path": "/webhook/line" + } + }, + "maixcam": { + "enabled": false, + "type": "maixcam", + "reasoning_channel_id": "", + "group_trigger": {}, + "typing": {}, + "placeholder": { + "enabled": false + }, + "settings": { + "host": "0.0.0.0", + "port": 18790 + } + }, + "matrix": { + "enabled": false, + "type": "matrix", + "reasoning_channel_id": "", + "group_trigger": { + "mention_only": true + }, + "typing": {}, + "placeholder": { + "enabled": true, + "text": [ + "Thinking... 💭" + ] + }, + "settings": { + "homeserver": "https://matrix.org", + "user_id": "", + "join_on_invite": true + } + }, + "onebot": { + "enabled": false, + "type": "onebot", + "reasoning_channel_id": "", + "group_trigger": {}, + "typing": {}, + "placeholder": { + "enabled": false + }, + "settings": { + "ws_url": "ws://127.0.0.1:3001", + "reconnect_interval": 5, + "group_trigger_prefix": null + } + }, + "pico": { + "enabled": false, + "type": "pico", + "reasoning_channel_id": "", + "group_trigger": {}, + "typing": {}, + "placeholder": { + "enabled": false + }, + "settings": { + "ping_interval": 30, + "read_timeout": 60, + "write_timeout": 10, + "max_connections": 100 + } + }, + "pico_client": { + "enabled": false, + "type": "pico_client", + "allow_from": [ + "" + ], + "reasoning_channel_id": "", + "group_trigger": {}, + "typing": {}, + "placeholder": { + "enabled": false + }, + "settings": { + "url": "" + } + }, + "qq": { + "enabled": false, + "type": "qq", + "reasoning_channel_id": "", + "group_trigger": {}, + "typing": {}, + "placeholder": { + "enabled": false + }, + "settings": { + "app_id": "", + "max_message_length": 2000, + "max_base64_file_size_mib": 0, + "send_markdown": false + } + }, + "slack": { + "enabled": false, + "type": "slack", + "reasoning_channel_id": "", + "group_trigger": {}, + "typing": {}, + "placeholder": { + "enabled": false + }, + "settings": {} + }, + "telegram": { + "enabled": true, + "type": "telegram", + "allow_from": [ + "-5274005272", + "8271300679" + ], + "reasoning_channel_id": "", + "group_trigger": {}, + "typing": { + "enabled": true + }, + "placeholder": { + "enabled": true, + "text": [ + "Thinking... 💭" + ] + }, + "settings": { + "base_url": "", + "proxy": "", + "streaming": { + "enabled": true, + "throttle_seconds": 3, + "min_growth_chars": 200 + }, + "use_markdown_v2": false + } + }, + "vk": { + "enabled": false, + "type": "vk", + "allow_from": [ + "" + ], + "reasoning_channel_id": "", + "group_trigger": {}, + "typing": {}, + "placeholder": { + "enabled": false + }, + "settings": { + "group_id": 0 + } + }, + "wecom": { + "enabled": false, + "type": "wecom", + "reasoning_channel_id": "", + "group_trigger": {}, + "typing": {}, + "placeholder": { + "enabled": false + }, + "settings": { + "bot_id": "", + "websocket_url": "wss://openws.work.weixin.qq.com", + "send_thinking_message": true + } + }, + "weixin": { + "enabled": false, + "type": "weixin", + "reasoning_channel_id": "", + "group_trigger": {}, + "typing": {}, + "placeholder": { + "enabled": false + }, + "settings": { + "base_url": "https://ilinkai.weixin.qq.com/", + "cdn_base_url": "https://novac2c.cdn.weixin.qq.com/c2c", + "proxy": "" + } + }, + "whatsapp": { + "enabled": false, + "type": "whatsapp", + "reasoning_channel_id": "", + "group_trigger": {}, + "typing": {}, + "placeholder": { + "enabled": false + }, + "settings": { + "bridge_url": "ws://localhost:3001", + "use_native": false, + "session_store_path": "" + } + } + }, + "model_list": [ + { + "model_name": "glm-4.7", + "model": "zhipu/glm-4.7", + "api_base": "https://open.bigmodel.cn/api/paas/v4" + }, + { + "model_name": "gpt-5.4", + "model": "openai/gpt-5.4", + "api_base": "https://api.openai.com/v1" + }, + { + "model_name": "claude-sonnet-4.6", + "model": "anthropic/claude-sonnet-4.6", + "api_base": "https://api.anthropic.com/v1" + }, + { + "model_name": "deepseek-chat", + "model": "deepseek/deepseek-chat", + "api_base": "https://api.deepseek.com/v1" + }, + { + "model_name": "gemini-2.0-flash", + "model": "gemini/gemini-2.0-flash-exp", + "api_base": "https://generativelanguage.googleapis.com/v1beta" + }, + { + "model_name": "qwen-plus", + "model": "qwen/qwen-plus", + "protocol": "openrouter", + "api_base": "https://dashscope.aliyuncs.com/compatible-mode/v1" + }, + { + "model_name": "moonshot-v1-8k", + "model": "moonshot/moonshot-v1-8k", + "api_base": "https://api.moonshot.cn/v1" + }, + { + "model_name": "llama-3.3-70b", + "model": "groq/llama-3.3-70b-versatile", + "api_base": "https://api.groq.com/openai/v1" + }, + { + "model_name": "openrouter-nemotron", + "model": "nvidia/nemotron-3-super-120b-a12b:free", + "protocol": "openrouter", + "api_base": "https://openrouter.ai/api/v1" + }, + { + "model_name": "openrouter-elephant", + "model": "openrouter/elephant-alpha", + "protocol": "openrouter", + "api_base": "https://openrouter.ai/api/v1" + }, + { + "model_name": "openrouter-free", + "model": "arcee-ai/trinity-large-preview:free", + "protocol": "openrouter", + "api_base": "https://openrouter.ai/api/v1" + }, + { + "model_name": "google-gemma-3-27b-it:free", + "model": "google/gemma-3-27b-it:free", + "protocol": "openrouter", + "api_base": "https://openrouter.ai/api/v1" + }, + { + "model_name": "qwen-qwen3-coder:free", + "model": "qwen/qwen3-coder:free", + "protocol": "openrouter", + "api_base": "https://openrouter.ai/api/v1" + }, + { + "model_name": "nvidia-nemotron-4-340b-instruct:free", + "model": "nvidia/nemotron-4-340b-instruct:free", + "protocol": "openrouter", + "api_base": "https://openrouter.ai/api/v1" + }, + { + "model_name": "mistralai-pixtral-12b:free", + "model": "mistralai/pixtral-12b:free", + "protocol": "openrouter", + "api_base": "https://openrouter.ai/api/v1" + }, + { + "model_name": "google-gemma-3-26b-a4b-it:free", + "model": "google/gemma-3-26b-a4b-it:free", + "protocol": "openrouter", + "api_base": "https://openrouter.ai/api/v1" + }, + { + "model_name": "openrouter-auto", + "model": "auto", + "protocol": "openrouter", + "api_base": "https://openrouter.ai/api/v1" + }, + { + "model_name": "openrouter-gpt-5.4", + "model": "openai/gpt-5.4", + "protocol": "openrouter", + "api_base": "https://openrouter.ai/api/v1" + }, + { + "model_name": "nvidia/llama-3.1-nemotron-70b-instruct", + "model": "nvidia/llama-3.1-nemotron-70b-instruct", + "protocol": "openrouter", + "api_base": "https://integrate.api.nvidia.com/v1" + }, + { + "model_name": "meta/llama-3.1-70b-instruct", + "model": "meta/llama-3.1-70b-instruct", + "api_base": "https://integrate.api.nvidia.com/v1" + }, + { + "model_name": "meta/llama-3.1-405b-instruct", + "model": "meta/llama-3.1-405b-instruct", + "api_base": "https://integrate.api.nvidia.com/v1" + }, + { + "model_name": "meta/llama-3.3-70b-instruct", + "model": "meta/llama-3.3-70b-instruct", + "api_base": "https://integrate.api.nvidia.com/v1" + }, + { + "model_name": "azure-grok", + "model": "openai/grok-4-fast-non-reasoning", + "api_base": "https://TestSJF.openai.azure.com/openai/v1/", + "enabled": true + }, + { + "model_name": "cerebras-llama-3.3-70b", + "model": "cerebras/llama-3.3-70b", + "api_base": "https://api.cerebras.ai/v1" + }, + { + "model_name": "vivgrid-auto", + "model": "vivgrid/auto", + "api_base": "https://api.vivgrid.com/v1" + }, + { + "model_name": "ark-code-latest", + "model": "volcengine/ark-code-latest", + "api_base": "https://ark.cn-beijing.volces.com/api/v3" + }, + { + "model_name": "doubao-pro", + "model": "volcengine/doubao-pro-32k", + "api_base": "https://ark.cn-beijing.volces.com/api/v3" + }, + { + "model_name": "deepseek-v3", + "model": "shengsuanyun/deepseek-v3", + "api_base": "https://api.shengsuanyun.com/v1" + }, + { + "model_name": "copilot-gpt-5.4", + "model": "github-copilot/gpt-5.4", + "api_base": "http://localhost:4321", + "auth_method": "oauth" + }, + { + "model_name": "llama3", + "model": "ollama/llama3", + "api_base": "http://localhost:11434/v1" + }, + { + "model_name": "mistral-small", + "model": "mistral/mistral-small-latest", + "api_base": "https://api.mistral.ai/v1" + }, + { + "model_name": "deepseek-v3.2", + "model": "avian/deepseek/deepseek-v3.2", + "api_base": "https://api.avian.io/v1" + }, + { + "model_name": "kimi-k2.5", + "model": "avian/moonshotai/kimi-k2.5", + "api_base": "https://api.avian.io/v1" + }, + { + "model_name": "MiniMax-M2.5", + "model": "minimax/MiniMax-M2.5", + "protocol": "openrouter", + "api_base": "https://api.minimaxi.com/v1", + "extra_body": { + "reasoning_split": true + } + }, + { + "model_name": "LongCat-Flash-Thinking", + "model": "longcat/LongCat-Flash-Thinking", + "api_base": "https://api.longcat.chat/openai" + }, + { + "model_name": "modelscope-qwen", + "model": "modelscope/Qwen/Qwen3-235B-A22B-Instruct-2507", + "api_base": "https://api-inference.modelscope.cn/v1" + }, + { + "model_name": "local-model", + "model": "vllm/custom-model", + "api_base": "http://localhost:8000/v1", + "enabled": true + }, + { + "model_name": "meta-llama-llama-3.3-70b-instruct:free", + "model": "meta-llama/llama-3.3-70b-instruct:free", + "protocol": "openrouter", + "enabled": true + }, + { + "model_name": "qwen-qwen3-coder:free", + "model": "qwen/qwen3-coder:free", + "protocol": "openrouter", + "enabled": true + }, + { + "model_name": "mistralai-pixtral-12b:free", + "model": "mistralai/pixtral-12b:free", + "protocol": "openrouter", + "enabled": true + }, + { + "model_name": "nvidia-nemotron-4-340b-instruct:free", + "model": "nvidia/nemotron-4-340b-instruct:free", + "protocol": "openrouter", + "enabled": true + }, + { + "model_name": "azure-gpt5", + "model": "azure/my-gpt5-deployment", + "api_base": "https://your-resource.openai.azure.com" + }, + { + "model_name": "google-gemma-3-26b-a4b-it:free", + "model": "google/gemma-3-26b-a4b-it:free", + "protocol": "openrouter", + "enabled": true + }, + { + "model_name": "google-gemma-3-31b-it:free", + "model": "google/gemma-3-31b-it:free", + "protocol": "openrouter", + "enabled": true + }, + { + "model_name": "nvidia-nemotron-3-super-120b-a12b:free", + "model": "nvidia/nemotron-3-super-120b-a12b:free", + "protocol": "openrouter", + "enabled": true + }, + { + "model_name": "nemotron-120b", + "model": "nvidia/nemotron-3-super-120b-a12b", + "protocol": "openrouter", + "api_base": "https://integrate.api.nvidia.com/v1", + "enabled": true + }, + { + "model_name": "qwen-qwen3-next-80b-a3b-instruct:free", + "model": "qwen/qwen3-next-80b-a3b-instruct:free", + "protocol": "openrouter", + "enabled": true + }, + { + "model_name": "nvidia-nemotron-nano-9b-v2:free", + "model": "nvidia/nemotron-nano-9b-v2:free", + "protocol": "openrouter", + "enabled": true + }, + { + "model_name": "openrouter-elephant-alpha", + "model": "openrouter/elephant-alpha", + "protocol": "openrouter", + "enabled": true + }, + { + "model_name": "minimax-minimax-m2.5:free", + "model": "minimax/minimax-m2.5:free", + "protocol": "openrouter", + "enabled": true + }, + { + "model_name": "arcee-ai-trinity-large-preview:free", + "model": "arcee-ai/trinity-large-preview:free", + "protocol": "openrouter", + "enabled": true + }, + { + "model_name": "google-lyria-3-pro-preview", + "model": "google/lyria-3-pro-preview", + "protocol": "openrouter", + "enabled": true + }, + { + "model_name": "google-lyria-3-clip-preview", + "model": "google/lyria-3-clip-preview", + "protocol": "openrouter", + "enabled": true + }, + { + "model_name": "nvidia-nemotron-3-nano-30b-a3b:free", + "model": "nvidia/nemotron-3-nano-30b-a3b:free", + "protocol": "openrouter", + "enabled": true + }, + { + "model_name": "nvidia-nemotron-nano-12b-v2-vl:free", + "model": "nvidia/nemotron-nano-12b-v2-vl:free", + "protocol": "openrouter", + "enabled": true + }, + { + "model_name": "openai-gpt-oss-120b:free", + "model": "openai/gpt-oss-120b:free", + "protocol": "openrouter", + "enabled": true + }, + { + "model_name": "openai-gpt-oss-20b:free", + "model": "openai/gpt-oss-20b:free", + "protocol": "openrouter", + "enabled": true + }, + { + "model_name": "google-gemma-4-26b-a4b-it:free", + "model": "google/gemma-4-26b-a4b-it:free", + "protocol": "openrouter", + "enabled": true + }, + { + "model_name": "google-gemma-4-31b-it:free", + "model": "google/gemma-4-31b-it:free", + "protocol": "openrouter", + "enabled": true + } + ], + "gateway": { + "host": "0.0.0.0", + "port": 18790, + "hot_reload": true, + "log_level": "info" + }, + "hooks": { + "enabled": false, + "defaults": { + "observer_timeout_ms": 500, + "interceptor_timeout_ms": 5000, + "approval_timeout_ms": 60000 + } + }, + "tools": { + "allow_read_paths": null, + "allow_write_paths": null, + "filter_sensitive_data": true, + "filter_min_length": 8, + "web": { + "enabled": true, + "brave": { + "enabled": false, + "max_results": 5 + }, + "tavily": { + "enabled": false, + "base_url": "", + "max_results": 5 + }, + "sogou": { + "enabled": true, + "max_results": 5 + }, + "duckduckgo": { + "enabled": true, + "max_results": 5 + }, + "perplexity": { + "enabled": false, + "max_results": 5 + }, + "searxng": { + "enabled": false, + "base_url": "", + "max_results": 5 + }, + "glm_search": { + "enabled": false, + "base_url": "https://open.bigmodel.cn/api/paas/v4/web_search", + "search_engine": "search_std", + "max_results": 5 + }, + "baidu_search": { + "enabled": false, + "base_url": "https://qianfan.baidubce.com/v2/ai_search/web_search", + "max_results": 10 + }, + "provider": "auto", + "prefer_native": true, + "fetch_limit_bytes": 10485760, + "format": "plaintext" + }, + "cron": { + "enabled": true, + "exec_timeout_minutes": 5, + "allow_command": true + }, + "exec": { + "enabled": true, + "enable_deny_patterns": true, + "allow_remote": true, + "custom_deny_patterns": null, + "custom_allow_patterns": null, + "timeout_seconds": 60 + }, + "skills": { + "enabled": true, + "registries": { + "clawhub": { + "base_url": "https://clawhub.ai", + "download_path": "", + "enabled": true, + "max_response_size": 0, + "max_zip_size": 0, + "search_path": "", + "skills_path": "", + "timeout": 0 + }, + "github": { + "base_url": "https://github.com", + "enabled": true + } + }, + "github": {}, + "max_concurrent_searches": 2, + "search_cache": { + "max_size": 50, + "ttl_seconds": 300 + } + }, + "media_cleanup": { + "enabled": true, + "max_age_minutes": 30, + "interval_minutes": 5 + }, + "mcp": { + "enabled": false, + "discovery": { + "enabled": false, + "ttl": 5, + "max_search_results": 5, + "use_bm25": true, + "use_regex": false + }, + "max_inline_text_chars": 16384 + }, + "append_file": { + "enabled": true + }, + "edit_file": { + "enabled": true + }, + "find_skills": { + "enabled": true + }, + "i2c": { + "enabled": false + }, + "install_skill": { + "enabled": true + }, + "list_dir": { + "enabled": true + }, + "message": { + "enabled": true + }, + "read_file": { + "enabled": true, + "mode": "bytes", + "max_read_file_size": 65536 + }, + "send_file": { + "enabled": true + }, + "send_tts": { + "enabled": false + }, + "spawn": { + "enabled": true + }, + "spawn_status": { + "enabled": false + }, + "spi": { + "enabled": false + }, + "subagent": { + "enabled": true + }, + "web_fetch": { + "enabled": true + }, + "write_file": { + "enabled": true + }, + "freeride": { + "enabled": true + } + }, + "heartbeat": { + "enabled": true, + "interval": 30 + }, + "devices": { + "enabled": false, + "monitor_usb": true + }, + "voice": { + "echo_transcription": false + }, + "build_info": { + "version": "0.1.0", + "git_commit": "054b55fd", + "build_time": "2026-03-23T10:15:13+0100", + "go_version": "go1.26.1" + } +} \ No newline at end of file diff --git a/pkg/agent/instance.go b/pkg/agent/instance.go index 5bcb83087..e380ead55 100644 --- a/pkg/agent/instance.go +++ b/pkg/agent/instance.go @@ -117,6 +117,9 @@ func NewAgentInstance( if cfg.Tools.IsToolEnabled("append_file") { toolsRegistry.Register(tools.NewAppendFileTool(workspace, restrict, allowWritePaths)) } + if cfg.Tools.IsToolEnabled("freeride") { + toolsRegistry.Register(tools.NewFreeRideTool(config.GetDefaultConfigPath(), nil)) + } sessionsDir := filepath.Join(workspace, "sessions") sessions := initSessionStore(sessionsDir) diff --git a/pkg/agent/loop_init.go b/pkg/agent/loop_init.go index 359dc8060..380197948 100644 --- a/pkg/agent/loop_init.go +++ b/pkg/agent/loop_init.go @@ -5,6 +5,7 @@ package agent import ( "context" "fmt" + "path/filepath" "time" "github.com/sipeed/picoclaw/pkg/audio/tts" @@ -44,6 +45,10 @@ func NewAgentLoop( var stateManager *state.Manager if defaultAgent != nil { stateManager = state.NewManager(defaultAgent.Workspace) + // Enable persistent cooldowns so that model rate limits/failures + // are remembered across agent restarts. + cooldownPath := filepath.Join(filepath.Dir(filepath.Clean(defaultAgent.Workspace)), "cooldowns.json") + _ = cooldown.SetPersistencePath(cooldownPath) } eventBus := NewEventBus() diff --git a/pkg/agent/model_resolution.go b/pkg/agent/model_resolution.go index 7cbf3a8d6..a65be3c4f 100644 --- a/pkg/agent/model_resolution.go +++ b/pkg/agent/model_resolution.go @@ -37,14 +37,20 @@ func candidateFromModelConfig( return providers.FallbackCandidate{}, false } - ref := providers.ParseModelRef(ensureProtocolModel(mc.Model), defaultProvider) - if ref == nil { - return providers.FallbackCandidate{}, false + provider := providers.NormalizeProvider(mc.Protocol) + model := mc.Model + if provider == "" { + ref := providers.ParseModelRef(ensureProtocolModel(model), defaultProvider) + if ref == nil { + return providers.FallbackCandidate{}, false + } + provider = ref.Provider + model = ref.Model } return providers.FallbackCandidate{ - Provider: ref.Provider, - Model: ref.Model, + Provider: provider, + Model: model, RPM: mc.RPM, IdentityKey: modelConfigIdentityKey(mc), }, true diff --git a/pkg/config/config.go b/pkg/config/config.go index 5bc96fb12..b69817a5e 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -530,8 +530,9 @@ type VoiceConfig struct { // Default protocol is "openai" if no prefix is specified. type ModelConfig struct { // Required fields - ModelName string `json:"model_name"` // User-facing alias for the model - Model string `json:"model"` // Protocol/model-identifier (e.g., "openai/gpt-4o", "anthropic/claude-sonnet-4.6") + ModelName string `json:"model_name"` // User-facing alias for the model + Model string `json:"model"` // Protocol/model-identifier (e.g., "openai/gpt-4o", "anthropic/claude-sonnet-4.6") + Protocol string `json:"protocol,omitempty"` // Explicit protocol (e.g., "openai", "openrouter", "anthropic") // HTTP-based providers APIBase string `json:"api_base,omitempty"` // API endpoint URL @@ -822,6 +823,7 @@ type ToolsConfig struct { Subagent ToolConfig `json:"subagent" yaml:"-" envPrefix:"PICOCLAW_TOOLS_SUBAGENT_"` WebFetch ToolConfig `json:"web_fetch" yaml:"-" envPrefix:"PICOCLAW_TOOLS_WEB_FETCH_"` WriteFile ToolConfig `json:"write_file" yaml:"-" envPrefix:"PICOCLAW_TOOLS_WRITE_FILE_"` + Freeride ToolConfig `json:"freeride" yaml:"-" envPrefix:"PICOCLAW_TOOLS_FREERIDE_"` } // IsFilterSensitiveDataEnabled returns true if sensitive data filtering is enabled @@ -1422,6 +1424,7 @@ func expandMultiKeyModels(models []*ModelConfig) []*ModelConfig { MaxTokensField: m.MaxTokensField, RequestTimeout: m.RequestTimeout, ThinkingLevel: m.ThinkingLevel, + Protocol: m.Protocol, ExtraBody: m.ExtraBody, CustomHeaders: m.CustomHeaders, UserAgent: m.UserAgent, @@ -1441,6 +1444,7 @@ func expandMultiKeyModels(models []*ModelConfig) []*ModelConfig { ConnectMode: m.ConnectMode, Workspace: m.Workspace, RPM: m.RPM, + Protocol: m.Protocol, MaxTokensField: m.MaxTokensField, RequestTimeout: m.RequestTimeout, ThinkingLevel: m.ThinkingLevel, @@ -1507,6 +1511,8 @@ func (t *ToolsConfig) IsToolEnabled(name string) bool { return t.SendTTS.Enabled case "write_file": return t.WriteFile.Enabled + case "freeride": + return t.Freeride.Enabled case "mcp": return t.MCP.Enabled default: diff --git a/pkg/config/config_struct.go b/pkg/config/config_struct.go index 6eaf32bc1..6250d5525 100644 --- a/pkg/config/config_struct.go +++ b/pkg/config/config_struct.go @@ -3,6 +3,7 @@ package config import ( "encoding/json" "fmt" + "os" "path/filepath" "runtime" "sort" @@ -300,6 +301,9 @@ func resolveKey(v string) (string, error) { if resolver == nil { resolver = credential.NewResolver("") } + if strings.HasPrefix(v, "env://") { + return os.Getenv(strings.TrimPrefix(v, "env://")), nil + } if strings.HasPrefix(v, "enc://") || strings.HasPrefix(v, "file://") { decrypted, err := resolver.Resolve(v) if err != nil { diff --git a/pkg/config/envkeys.go b/pkg/config/envkeys.go index 5a2590299..064d94192 100644 --- a/pkg/config/envkeys.go +++ b/pkg/config/envkeys.go @@ -55,3 +55,10 @@ func GetHome() string { } return homePath } + +func GetDefaultConfigPath() string { + if cfgPath := os.Getenv(EnvConfig); cfgPath != "" { + return cfgPath + } + return filepath.Join(GetHome(), "config.json") +} diff --git a/pkg/providers/cooldown.go b/pkg/providers/cooldown.go index b0d8608dc..41c764802 100644 --- a/pkg/providers/cooldown.go +++ b/pkg/providers/cooldown.go @@ -1,7 +1,9 @@ package providers import ( + "encoding/json" "math" + "os" "sync" "time" ) @@ -11,11 +13,12 @@ const ( ) // CooldownTracker manages per-provider cooldown state for the fallback chain. -// Thread-safe via sync.RWMutex. In-memory only (resets on restart). +// Thread-safe via sync.RWMutex. Supports persistence to disk. type CooldownTracker struct { mu sync.RWMutex entries map[string]*cooldownEntry failureWindow time.Duration + persistPath string nowFunc func() time.Time // for testing } @@ -63,6 +66,8 @@ func (ct *CooldownTracker) MarkFailure(provider string, reason FailoverReason) { } else { entry.CooldownEnd = now.Add(calculateStandardCooldown(entry.ErrorCount)) } + + ct.save() } // MarkSuccess resets all counters and cooldowns for a provider. @@ -80,6 +85,8 @@ func (ct *CooldownTracker) MarkSuccess(provider string) { entry.CooldownEnd = time.Time{} entry.DisabledUntil = time.Time{} entry.DisabledReason = "" + + ct.save() } // IsAvailable returns true if the provider is not in cooldown or disabled. @@ -162,6 +169,59 @@ func (ct *CooldownTracker) FailureCount(provider string, reason FailoverReason) return entry.FailureCounts[reason] } +// SetPersistencePath sets the path for state persistence and triggers an immediate load. +func (ct *CooldownTracker) SetPersistencePath(path string) error { + ct.mu.Lock() + defer ct.mu.Unlock() + + ct.persistPath = path + return ct.load() +} + +func (ct *CooldownTracker) save() { + if ct.persistPath == "" { + return + } + + data, err := json.MarshalIndent(ct.entries, "", " ") + if err != nil { + return + } + + _ = os.WriteFile(ct.persistPath, data, 0o644) +} + +func (ct *CooldownTracker) load() error { + if ct.persistPath == "" { + return nil + } + + data, err := os.ReadFile(ct.persistPath) + if err != nil { + if os.IsNotExist(err) { + return nil + } + return err + } + + var saved map[string]*cooldownEntry + if err := json.Unmarshal(data, &saved); err != nil { + return err + } + + // Filter out expired cooldowns during load + now := ct.nowFunc() + ct.entries = make(map[string]*cooldownEntry) + for k, v := range saved { + if (!v.CooldownEnd.IsZero() && now.Before(v.CooldownEnd)) || + (!v.DisabledUntil.IsZero() && now.Before(v.DisabledUntil)) { + ct.entries[k] = v + } + } + + return nil +} + func (ct *CooldownTracker) getOrCreate(provider string) *cooldownEntry { entry := ct.entries[provider] if entry == nil { diff --git a/pkg/providers/cooldown_test.go b/pkg/providers/cooldown_test.go index b517e7feb..a5a22865e 100644 --- a/pkg/providers/cooldown_test.go +++ b/pkg/providers/cooldown_test.go @@ -1,11 +1,82 @@ package providers import ( + "os" + "path/filepath" "sync" "testing" "time" ) +func TestCooldown_Persistence(t *testing.T) { + tempDir := t.TempDir() + persistPath := filepath.Join(tempDir, "cooldowns.json") + + now := time.Now() + ct, current := newTestTracker(now) + if err := ct.SetPersistencePath(persistPath); err != nil { + t.Fatalf("SetPersistencePath failed: %v", err) + } + + // 1. Mark a failure and verify it saves + ct.MarkFailure("openai", FailoverRateLimit) // 1 min cooldown + if ct.IsAvailable("openai") { + t.Error("openai should be in cooldown") + } + + if _, err := os.Stat(persistPath); os.IsNotExist(err) { + t.Fatal("persistence file was not created") + } + + // 2. Create a NEW tracker and load the file + ct2, _ := newTestTracker(now) + if err := ct2.SetPersistencePath(persistPath); err != nil { + t.Fatalf("SetPersistencePath on second tracker failed: %v", err) + } + + if ct2.IsAvailable("openai") { + t.Error("newly loaded tracker should still have openai in cooldown") + } + if ct2.ErrorCount("openai") != 1 { + t.Errorf("error count = %d, want 1", ct2.ErrorCount("openai")) + } + + // 3. Mark success and verify it clears and persists + ct2.MarkSuccess("openai") + if !ct2.IsAvailable("openai") { + t.Error("openai should be available after success") + } + + ct3, _ := newTestTracker(now) + if err := ct3.SetPersistencePath(persistPath); err != nil { + t.Fatalf("SetPersistencePath on third tracker failed: %v", err) + } + if !ct3.IsAvailable("openai") { + t.Error("fourth tracker should see openai as available after success was persisted") + } + + // 4. Verify expiration filtering + ct3.MarkFailure("anthropic", FailoverRateLimit) // 1 min cooldown + *current = now.Add(2 * time.Minute) // Advance time past expiration + + ct4, ct4Current := newTestTracker(*current) // ct4 sees the future + if err := ct4.SetPersistencePath(persistPath); err != nil { + t.Fatalf("SetPersistencePath on fourth tracker failed: %v", err) + } + + // Since current time (2 min later) is past the 1 min cooldown, it should be filtered out on load + if !ct4.IsAvailable("anthropic") { + t.Error("anthropic should be available (expired cooldown filtered on load)") + } + + // Verify that MarkFailure on ct4 uses the correct time + ct4.MarkFailure("groq", FailoverRateLimit) + if ct4.IsAvailable("groq") { + t.Error("groq should be in cooldown on ct4") + } + _ = ct4Current // keep compiler happy +} + func newTestTracker(now time.Time) (*CooldownTracker, *time.Time) { current := now ct := NewCooldownTracker() diff --git a/pkg/providers/error_classifier.go b/pkg/providers/error_classifier.go index 88c92a47d..21636570b 100644 --- a/pkg/providers/error_classifier.go +++ b/pkg/providers/error_classifier.go @@ -257,6 +257,8 @@ func classifyByStatus(status int) FailoverReason { return FailoverRateLimit case status == 400: return FailoverFormat + case status == 404: + return FailoverNotFound case transientStatusCodes[status]: return FailoverTimeout } diff --git a/pkg/providers/error_classifier_test.go b/pkg/providers/error_classifier_test.go index 571fb3882..44b38e1b6 100644 --- a/pkg/providers/error_classifier_test.go +++ b/pkg/providers/error_classifier_test.go @@ -63,6 +63,7 @@ func TestClassifyError_StatusCodes(t *testing.T) { {523, FailoverTimeout}, {524, FailoverTimeout}, {529, FailoverTimeout}, + {404, FailoverNotFound}, } for _, tt := range tests { @@ -427,6 +428,7 @@ func TestFailoverError_IsRetriable(t *testing.T) { {FailoverOverloaded, true}, {FailoverFormat, false}, {FailoverContextOverflow, false}, + {FailoverNotFound, true}, {FailoverUnknown, true}, } diff --git a/pkg/providers/factory_provider.go b/pkg/providers/factory_provider.go index ab68b326a..de5611489 100644 --- a/pkg/providers/factory_provider.go +++ b/pkg/providers/factory_provider.go @@ -84,19 +84,38 @@ func createCodexAuthProvider() (LLMProvider, error) { return NewCodexProviderWithTokenSource(cred.AccessToken, cred.AccountID, createCodexTokenSource()), nil } +func isKnownProtocol(p string) bool { + if _, ok := protocolMetaByName[p]; ok { + return true + } + switch p { + case "anthropic", "azure", "azure-openai", "bedrock", "github-copilot", "github-copilot-chat", "copilot", "claude": + return true + case "antigravity", "claude-cli", "codex-cli", "cli", "fs", "memory", "dummy": + return true + case "elevenlabs", "openai-tts": + return true + } + return false +} + // ExtractProtocol extracts the protocol prefix and model identifier from a model string. // If no prefix is specified, it defaults to "openai". -// Examples: -// - "openai/gpt-4o" -> ("openai", "gpt-4o") -// - "anthropic/claude-sonnet-4.6" -> ("anthropic", "claude-sonnet-4.6") -// - "gpt-4o" -> ("openai", "gpt-4o") // default protocol func ExtractProtocol(model string) (protocol, modelID string) { model = strings.TrimSpace(model) - protocol, modelID, found := strings.Cut(model, "/") + p, m, found := strings.Cut(model, "/") if !found { return "openai", model } - return protocol, modelID + + // Only treat as protocol if it's in our known list. + // This prevents organizational model IDs like "google/gemma" or "anthropic/claude" + // from having their prefixes stripped when used with OpenAI-compatible providers (OpenRouter). + if isKnownProtocol(p) { + return p, m + } + + return "openai", model } // ResolveAPIBase returns the configured API base, or the protocol default when @@ -128,6 +147,16 @@ func CreateProviderFromConfig(cfg *config.ModelConfig) (LLMProvider, string, err } protocol, modelID := ExtractProtocol(cfg.Model) + if cfg.Protocol != "" { + protocol = cfg.Protocol + // If protocol was explicitly set, modelID should be the full model string + // unless it was already prefixed with the SAME protocol. + if p, m, found := strings.Cut(cfg.Model, "/"); found && strings.EqualFold(p, protocol) { + modelID = m + } else { + modelID = cfg.Model + } + } userAgent := cfg.UserAgent if userAgent == "" { diff --git a/pkg/providers/factory_provider_test.go b/pkg/providers/factory_provider_test.go index 20cdd8a30..15180df62 100644 --- a/pkg/providers/factory_provider_test.go +++ b/pkg/providers/factory_provider_test.go @@ -60,7 +60,7 @@ func TestExtractProtocol(t *testing.T) { wantModelID: "gpt-4", }, { - name: "multiple slashes", + name: "multiple slashes (nvidia organizational prefix)", model: "nvidia/meta/llama-3.1-8b", wantProtocol: "nvidia", wantModelID: "meta/llama-3.1-8b", @@ -538,19 +538,6 @@ func TestCreateProviderFromConfig_MissingAPIKey(t *testing.T) { } } -func TestCreateProviderFromConfig_UnknownProtocol(t *testing.T) { - cfg := &config.ModelConfig{ - ModelName: "test-unknown", - Model: "unknown-protocol/model", - } - cfg.SetAPIKey("test-key") - - _, _, err := CreateProviderFromConfig(cfg) - if err == nil { - t.Fatal("CreateProviderFromConfig() expected error for unknown protocol") - } -} - func TestCreateProviderFromConfig_NilConfig(t *testing.T) { _, _, err := CreateProviderFromConfig(nil) if err == nil { diff --git a/pkg/providers/openai_compat/provider.go b/pkg/providers/openai_compat/provider.go index 98a70cfd2..a457d97fe 100644 --- a/pkg/providers/openai_compat/provider.go +++ b/pkg/providers/openai_compat/provider.go @@ -49,7 +49,6 @@ var stripModelPrefixProviders = map[string]struct{}{ "litellm": {}, "venice": {}, "moonshot": {}, - "nvidia": {}, "groq": {}, "ollama": {}, "deepseek": {}, diff --git a/pkg/providers/types.go b/pkg/providers/types.go index fae252d13..0ae692316 100644 --- a/pkg/providers/types.go +++ b/pkg/providers/types.go @@ -79,6 +79,7 @@ const ( FailoverFormat FailoverReason = "format" FailoverContextOverflow FailoverReason = "context_overflow" FailoverOverloaded FailoverReason = "overloaded" + FailoverNotFound FailoverReason = "not_found" FailoverUnknown FailoverReason = "unknown" ) diff --git a/pkg/tools/freeride.go b/pkg/tools/freeride.go new file mode 100644 index 000000000..ba92cc71c --- /dev/null +++ b/pkg/tools/freeride.go @@ -0,0 +1,404 @@ +package tools + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "sort" + "strings" + "time" + + "github.com/sipeed/picoclaw/pkg/config" +) + +// FreeRideTool adapts the FreeRide logic (from clawhub/free-ride) for PicoClaw. +// It manages OpenRouter's free models and configures them as fallbacks. +type FreeRideTool struct { + configPath string + reloadFunc func() error +} + +func NewFreeRideTool(configPath string, reloadFunc func() error) *FreeRideTool { + return &FreeRideTool{ + configPath: configPath, + reloadFunc: reloadFunc, + } +} + +func (t *FreeRideTool) Name() string { + return "freeride" +} + +func (t *FreeRideTool) Description() string { + return "FreeRide gives you unlimited free AI in PicoClaw by automatically managing OpenRouter's free models. " + + "Use 'auto' to configure best model + fallbacks, or 'list' to see available free models." +} + +func (t *FreeRideTool) Parameters() map[string]any { + return map[string]any{ + "type": "object", + "properties": map[string]any{ + "command": map[string]any{ + "type": "string", + "enum": []string{"auto", "list", "status", "settimeout"}, + "description": "The command to run: 'auto' (configures models), 'list' (shows free models), 'status' (checks current setup), 'settimeout' (sets request timeout)", + }, + "limit": map[string]any{ + "type": "integer", + "description": "For 'list', how many models to show. For 'auto', how many fallbacks to configure.", + "default": 5, + }, + "timeout": map[string]any{ + "type": "integer", + "description": "For 'settimeout', the request timeout in seconds (default 300)", + "default": 300, + }, + }, + "required": []string{"command"}, + } +} + +type openRouterModel struct { + ID string `json:"id"` + Name string `json:"name"` + ContextLength int `json:"context_length"` + Pricing struct { + Prompt string `json:"prompt"` + Completion string `json:"completion"` + } `json:"pricing"` + SupportedParameters []string `json:"supported_parameters"` + Created int64 `json:"created"` +} + +func (t *FreeRideTool) Execute(ctx context.Context, args map[string]any) *ToolResult { + cmd, _ := args["command"].(string) + limit := 5 + if l, ok := args["limit"].(float64); ok { + limit = int(l) + } + timeout := 300 + switch v := args["timeout"].(type) { + case float64: + timeout = int(v) + case int: + timeout = v + } + + switch cmd { + case "list": + return t.handleList(ctx, limit) + case "auto": + return t.handleAuto(ctx, limit) + case "status": + return t.handleStatus() + case "settimeout": + return t.handleSetTimeout(timeout) + default: + return ErrorResult(fmt.Sprintf("unknown command: %s", cmd)) + } +} + +func (t *FreeRideTool) fetchFreeModels(ctx context.Context) ([]openRouterModel, error) { + req, err := http.NewRequestWithContext(ctx, "GET", "https://openrouter.ai/api/v1/models", nil) + if err != nil { + return nil, err + } + + resp, err := http.DefaultClient.Do(req) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return nil, fmt.Errorf("OpenRouter API returned status %d", resp.StatusCode) + } + + var wrapper struct { + Data []openRouterModel `json:"data"` + } + if err := json.NewDecoder(resp.Body).Decode(&wrapper); err != nil { + return nil, err + } + + var freeModels []openRouterModel + for _, m := range wrapper.Data { + // Only consider free models + if m.Pricing.Prompt == "0" || m.Pricing.Prompt == "0.0" || m.Pricing.Prompt == "0.00" { + // CRITICAL: PeakClaw requires tool support for its steering logic. + // Filter out models that don't explicitly support function calling. + hasTools := false + for _, p := range m.SupportedParameters { + if p == "tools" { + hasTools = true + break + } + } + if !hasTools { + continue + } + + // Blacklist known tool-blind models with inaccurate metadata + lowerID := strings.ToLower(m.ID) + if strings.Contains(lowerID, "lyria") || strings.Contains(lowerID, "liquid") { + continue + } + + freeModels = append(freeModels, m) + } + } + + // Rank models + sort.Slice(freeModels, func(i, j int) bool { + return scoreModel(freeModels[i]) > scoreModel(freeModels[j]) + }) + + return freeModels, nil +} + +func scoreModel(m openRouterModel) float64 { + score := 0.0 + + // Context length (40%) - normalize against 128k + ctxScore := float64(m.ContextLength) / 128000.0 + if ctxScore > 1.0 { + ctxScore = 1.0 + } + score += ctxScore * 0.4 + + // Capabilities (30%) - tools, vision, prompt caching, etc. + capabilityScore := 0.0 + for _, p := range m.SupportedParameters { + if p == "tools" { + capabilityScore += 0.5 + } + if p == "response_format" { + capabilityScore += 0.5 + } + } + if capabilityScore > 1.0 { + capabilityScore = 1.0 + } + score += capabilityScore * 0.3 + + // Recency (20%) - newer is better + // Normalize against 2 years ago + twoYearsAgo := time.Now().AddDate(-2, 0, 0).Unix() + now := time.Now().Unix() + if m.Created > twoYearsAgo { + recencyScore := float64(m.Created-twoYearsAgo) / float64(now-twoYearsAgo) + score += recencyScore * 0.2 + } + + // Provider Trust (10%) - hardcoded list of trusted names + trustNames := []string{ + "google", + "meta", + "nvidia", + "mistral", + "anthropic", + "openai", + "microsoft", + "qwen", + "deepseek", + } + for _, name := range trustNames { + if strings.Contains(strings.ToLower(m.ID), name) { + score += 0.1 + break + } + } + + return score +} + +func (t *FreeRideTool) handleList(ctx context.Context, limit int) *ToolResult { + models, err := t.fetchFreeModels(ctx) + if err != nil { + return ErrorResult(fmt.Errorf("failed to fetch models: %w", err).Error()) + } + + if len(models) == 0 { + return SilentResult("No free models found on OpenRouter.") + } + + var sb strings.Builder + sb.WriteString(fmt.Sprintf("Found %d free models on OpenRouter (ranked by quality):\n\n", len(models))) + for i, m := range models { + if i >= limit { + break + } + sb.WriteString(fmt.Sprintf("%d. **%s** (%s)\n", i+1, m.Name, m.ID)) + sb.WriteString(fmt.Sprintf(" Context: %d tokens | Score: %.2f\n", m.ContextLength, scoreModel(m))) + sb.WriteString(fmt.Sprintf(" Parameters: %s\n\n", strings.Join(m.SupportedParameters, ", "))) + } + + return UserResult(sb.String()) +} + +func (t *FreeRideTool) handleAuto(ctx context.Context, limit int) *ToolResult { + models, err := t.fetchFreeModels(ctx) + if err != nil { + return ErrorResult(fmt.Errorf("failed to fetch models: %w", err).Error()) + } + + if len(models) == 0 { + return ErrorResult("No free models found on OpenRouter.") + } + + cfgObj, err := config.LoadConfig(t.configPath) + if err != nil { + return ErrorResult(fmt.Errorf("failed to load config: %w", err).Error()) + } + + // 1. Add models to ModelList if not present + // 2. Collect all valid free models for fallbacks (new AND existing) + var fallbackModels []string + for i, m := range models { + if i >= limit { + break + } + modelName := strings.ReplaceAll(m.ID, "/", "-") + if !modelExists(cfgObj, modelName) { + mc := &config.ModelConfig{ + ModelName: modelName, + Model: m.ID, + Protocol: "openrouter", + Enabled: true, + } + mc.SetAPIKey("env://OPENROUTER_API_KEY") + cfgObj.ModelList = append(cfgObj.ModelList, mc) + } + fallbackModels = append(fallbackModels, modelName) + } + + // 2. Set fallbacks for the default agent + if len(fallbackModels) > 0 { + // Update AgentDefaults fallbacks + cfgObj.Agents.Defaults.ModelFallbacks = append(cfgObj.Agents.Defaults.ModelFallbacks, fallbackModels...) + // Deduplicate fallbacks + cfgObj.Agents.Defaults.ModelFallbacks = uniqueStrings(cfgObj.Agents.Defaults.ModelFallbacks) + + if err := config.SaveConfig(t.configPath, cfgObj); err != nil { + return ErrorResult(fmt.Errorf("failed to save config: %w", err).Error()) + } + + msg := fmt.Sprintf( + "Success! Added %d free models as fallbacks: %s.\n", + len(fallbackModels), + strings.Join(fallbackModels, ", "), + ) + msg += "Re-loading configuration to apply changes..." + + if t.reloadFunc != nil { + if err := t.reloadFunc(); err != nil { + return ErrorResult(fmt.Sprintf("%s\nFailed to reload: %v", msg, err)) + } + } + + return UserResult(msg) + } + + return UserResult( + "No new free models to add. Your configuration is up to date.", + ) +} + +func (t *FreeRideTool) handleStatus() *ToolResult { + cfgObj, err := config.LoadConfig(t.configPath) + if err != nil { + return ErrorResult(fmt.Errorf("failed to load config: %w", err).Error()) + } + + var sb strings.Builder + sb.WriteString("FreeRide Status:\n") + sb.WriteString(fmt.Sprintf("- Primary Model: %s\n", cfgObj.Agents.Defaults.GetModelName())) + sb.WriteString(fmt.Sprintf("- Fallback Models: %s\n", strings.Join(cfgObj.Agents.Defaults.ModelFallbacks, ", "))) + + // Check for OpenRouter models in fallbacks + openRouterCount := 0 + for _, fb := range cfgObj.Agents.Defaults.ModelFallbacks { + if strings.Contains(strings.ToLower(fb), "openrouter") || isKnownOpenRouterAlias(cfgObj, fb) { + openRouterCount++ + } + } + sb.WriteString(fmt.Sprintf("- Managed Free Models: %d\n", openRouterCount)) + + return UserResult(sb.String()) +} + +func (t *FreeRideTool) handleSetTimeout(timeoutSeconds int) *ToolResult { + if timeoutSeconds < 30 { + return ErrorResult("timeout must be at least 30 seconds") + } + + cfgObj, err := config.LoadConfig(t.configPath) + if err != nil { + return ErrorResult(fmt.Errorf("failed to load config: %w", err).Error()) + } + + updated := 0 + for _, mc := range cfgObj.ModelList { + // Only update OpenRouter models (free models) + protocol := strings.ToLower(mc.Protocol) + if protocol == "openrouter" { + mc.RequestTimeout = timeoutSeconds + updated++ + } + } + + if updated == 0 { + return ErrorResult("no OpenRouter models found in config. Run 'freeride auto' first.") + } + + if err := config.SaveConfig(t.configPath, cfgObj); err != nil { + return ErrorResult(fmt.Errorf("failed to save config: %w", err).Error()) + } + + msg := fmt.Sprintf("Set request timeout to %d seconds for %d OpenRouter models.\n", timeoutSeconds, updated) + msg += "Re-loading configuration to apply changes..." + + if t.reloadFunc != nil { + if err := t.reloadFunc(); err != nil { + return ErrorResult(fmt.Sprintf("%s\nFailed to reload: %v", msg, err)) + } + } + + return UserResult(msg) +} + +func modelExists(cfg *config.Config, modelName string) bool { + for _, m := range cfg.ModelList { + if m.ModelName == modelName { + return true + } + } + return false +} + +func isKnownOpenRouterAlias(cfg *config.Config, modelName string) bool { + for _, m := range cfg.ModelList { + if m.ModelName == modelName { + if strings.HasPrefix(m.Model, "openrouter/") { + return true + } + if strings.ToLower(m.Protocol) == "openrouter" { + return true + } + } + } + return false +} + +func uniqueStrings(input []string) []string { + keys := make(map[string]bool) + list := []string{} + for _, entry := range input { + if _, value := keys[entry]; !value { + keys[entry] = true + list = append(list, entry) + } + } + return list +} diff --git a/pkg/tools/freeride_test.go b/pkg/tools/freeride_test.go new file mode 100644 index 000000000..b82597ab6 --- /dev/null +++ b/pkg/tools/freeride_test.go @@ -0,0 +1,316 @@ +package tools + +import ( + "context" + "encoding/json" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/sipeed/picoclaw/pkg/config" +) + +func TestFreeRideTool_List(t *testing.T) { + // Mock OpenRouter API + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(map[string]any{ + "data": []map[string]any{ + { + "id": "google/gemini-pro-1.5", + "name": "Gemini Pro 1.5", + "context_length": 128000, + "pricing": map[string]string{ + "prompt": "0", + "completion": "0", + }, + "created": 1700000000, + "supported_parameters": []string{"tools"}, + }, + { + "id": "meta-llama/llama-3-8b", + "name": "Llama 3 8B", + "context_length": 8000, + "pricing": map[string]string{ + "prompt": "0.0001", + "completion": "0.0001", + }, + "created": 1700000000, + "supported_parameters": []string{"tools"}, + }, + }, + }) + })) + defer server.Close() + + // Override default transport to use mock server + oldTransport := http.DefaultClient.Transport + http.DefaultClient.Transport = &mockTransport{server.URL} + defer func() { http.DefaultClient.Transport = oldTransport }() + + tool := NewFreeRideTool("config.json", nil) + result := tool.Execute(context.Background(), map[string]any{ + "command": "list", + }) + + if result.IsError { + t.Fatalf("Expected no error, got %s", result.ForLLM) + } + + if result.Silent { + t.Errorf("Expected non-silent result") + } + + output := result.ForLLM + if !contains(output, "Gemini Pro 1.5") { + t.Errorf("Expected Gemini Pro 1.5 in output, got %s", output) + } + if contains(output, "Llama 3 8B") { + t.Errorf("Did not expect paid model Llama 3 8B in output, got %s", output) + } +} + +func TestFreeRideTool_Auto(t *testing.T) { + os.Setenv("OPENROUTER_API_KEY", "sk-test-key") + defer os.Unsetenv("OPENROUTER_API_KEY") + + tempDir, err := os.MkdirTemp("", "freeride-test") + if err != nil { + t.Fatalf("failed to create temp dir: %v", err) + } + defer os.RemoveAll(tempDir) + + configPath := filepath.Join(tempDir, "config.json") + initialCfg := &config.Config{ + ModelList: []*config.ModelConfig{}, + } + initialCfg.Agents.Defaults.ModelName = "existing-model" + + if err := config.SaveConfig(configPath, initialCfg); err != nil { + t.Fatalf("failed to save initial config: %v", err) + } + + // Mock OpenRouter API + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(map[string]any{ + "data": []map[string]any{ + { + "id": "google/gemini-pro-1.5", + "name": "Gemini Pro 1.5", + "context_length": 128000, + "pricing": map[string]string{ + "prompt": "0", + "completion": "0", + }, + "created": 1700000000, + "supported_parameters": []string{"tools"}, + }, + }, + }) + })) + defer server.Close() + + oldTransport := http.DefaultClient.Transport + http.DefaultClient.Transport = &mockTransport{server.URL} + defer func() { http.DefaultClient.Transport = oldTransport }() + + var reloadCalled bool + reloadFunc := func() error { + reloadCalled = true + return nil + } + + tool := NewFreeRideTool(configPath, reloadFunc) + result := tool.Execute(context.Background(), map[string]any{ + "command": "auto", + }) + + if result.IsError { + t.Fatalf("Expected no error, got %s", result.ForLLM) + } + + if !reloadCalled { + t.Errorf("Expected reloadFunc to be called") + } + + // Verify config + cfg, err := config.LoadConfig(configPath) + if err != nil { + t.Fatalf("failed to load updated config: %v", err) + } + + if len(cfg.ModelList) != 1 { + t.Errorf("Expected 1 model in ModelList, got %d", len(cfg.ModelList)) + } + + if cfg.ModelList[0].ModelName != "google-gemini-pro-1.5" { + t.Errorf("Expected model name google-gemini-pro-1.5, got %s", cfg.ModelList[0].ModelName) + } + + if len(cfg.Agents.Defaults.ModelFallbacks) != 1 { + t.Errorf("Expected 1 fallback, got %d", len(cfg.Agents.Defaults.ModelFallbacks)) + } +} + +func TestFreeRideTool_SetTimeout(t *testing.T) { + tempDir, err := os.MkdirTemp("", "freeride-test") + if err != nil { + t.Fatalf("failed to create temp dir: %v", err) + } + defer os.RemoveAll(tempDir) + + configPath := filepath.Join(tempDir, "config.json") + initialCfg := &config.Config{ + ModelList: []*config.ModelConfig{ + { + ModelName: "google-gemini-pro-1.5", + Model: "google/gemini-pro-1.5", + Protocol: "openrouter", + }, + { + ModelName: "meta-llama-3-8b", + Model: "meta/llama-3-8b", + Protocol: "openrouter", + }, + { + ModelName: "gpt-4o", + Model: "openai/gpt-4o", + Protocol: "openai", + }, + }, + } + initialCfg.Agents.Defaults.ModelName = "gpt-4o" + + if err := config.SaveConfig(configPath, initialCfg); err != nil { + t.Fatalf("failed to save initial config: %v", err) + } + + var reloadCalled bool + reloadFunc := func() error { + reloadCalled = true + return nil + } + + tool := NewFreeRideTool(configPath, reloadFunc) + result := tool.Execute(context.Background(), map[string]any{ + "command": "settimeout", + "timeout": 180, + }) + + if result.IsError { + t.Fatalf("Expected no error, got %s", result.ForLLM) + } + + if !reloadCalled { + t.Errorf("Expected reloadFunc to be called") + } + + // Verify config + cfg, err := config.LoadConfig(configPath) + if err != nil { + t.Fatalf("failed to load updated config: %v", err) + } + + // Should have updated 2 openrouter models + if cfg.ModelList[0].RequestTimeout != 180 { + t.Errorf("Expected timeout 180 for google-gemini-pro-1.5, got %d", cfg.ModelList[0].RequestTimeout) + } + if cfg.ModelList[1].RequestTimeout != 180 { + t.Errorf("Expected timeout 180 for meta-llama-3-8b, got %d", cfg.ModelList[1].RequestTimeout) + } + // openai model should NOT be updated + if cfg.ModelList[2].RequestTimeout != 0 { + t.Errorf("Expected timeout 0 for gpt-4o (non-openrouter), got %d", cfg.ModelList[2].RequestTimeout) + } +} + +func TestFreeRideTool_SetTimeout_NoOpenRouterModels(t *testing.T) { + tempDir, err := os.MkdirTemp("", "freeride-test") + if err != nil { + t.Fatalf("failed to create temp dir: %v", err) + } + defer os.RemoveAll(tempDir) + + configPath := filepath.Join(tempDir, "config.json") + initialCfg := &config.Config{ + ModelList: []*config.ModelConfig{ + { + ModelName: "gpt-4o", + Model: "openai/gpt-4o", + Protocol: "openai", + }, + }, + } + + if err := config.SaveConfig(configPath, initialCfg); err != nil { + t.Fatalf("failed to save initial config: %v", err) + } + + tool := NewFreeRideTool(configPath, nil) + result := tool.Execute(context.Background(), map[string]any{ + "command": "settimeout", + "timeout": 180, + }) + + if !result.IsError { + t.Fatalf("Expected error when no OpenRouter models, got success") + } + + if !contains(result.ForLLM, "no OpenRouter models") { + t.Errorf("Expected error message about no OpenRouter models, got %s", result.ForLLM) + } +} + +func TestFreeRideTool_SetTimeout_MinimumTooLow(t *testing.T) { + tempDir, err := os.MkdirTemp("", "freeride-test") + if err != nil { + t.Fatalf("failed to create temp dir: %v", err) + } + defer os.RemoveAll(tempDir) + + configPath := filepath.Join(tempDir, "config.json") + initialCfg := &config.Config{ + ModelList: []*config.ModelConfig{ + { + ModelName: "google-gemini-pro-1.5", + Model: "google/gemini-pro-1.5", + Protocol: "openrouter", + }, + }, + } + + if err := config.SaveConfig(configPath, initialCfg); err != nil { + t.Fatalf("failed to save initial config: %v", err) + } + + tool := NewFreeRideTool(configPath, nil) + result := tool.Execute(context.Background(), map[string]any{ + "command": "settimeout", + "timeout": 20, // too low + }) + + if !result.IsError { + t.Fatalf("Expected error when timeout < 30, got success") + } + + if !contains(result.ForLLM, "at least 30") { + t.Errorf("Expected error message about minimum 30 seconds, got %s", result.ForLLM) + } +} + +type mockTransport struct { + url string +} + +func (m *mockTransport) RoundTrip(req *http.Request) (*http.Response, error) { + newReq, _ := http.NewRequest(req.Method, m.url, req.Body) + return http.DefaultTransport.RoundTrip(newReq) +} + +func contains(s, substr string) bool { + return strings.Contains(s, substr) +} diff --git a/scratch/check_paths.go b/scratch/check_paths.go new file mode 100644 index 000000000..f6eb96d75 --- /dev/null +++ b/scratch/check_paths.go @@ -0,0 +1,28 @@ +package main + +import ( + "fmt" + "os" + "path/filepath" + + "github.com/sipeed/picoclaw/pkg/agent" + "github.com/sipeed/picoclaw/pkg/config" +) + +func main() { + cfg, err := config.LoadConfig(os.ExpandEnv("$HOME/.picoclaw/config.json")) + if err != nil { + fmt.Printf("Error: %v\n", err) + return + } + registry := agent.NewAgentRegistry(cfg, nil) + defaultAgent := registry.GetDefaultAgent() + if defaultAgent == nil { + fmt.Println("No default agent") + return + } + + cooldownPath := filepath.Join(filepath.Dir(filepath.Clean(defaultAgent.Workspace)), "cooldowns.json") + fmt.Printf("Workspace: %s\n", defaultAgent.Workspace) + fmt.Printf("Cooldown Path: %s\n", cooldownPath) +} diff --git a/workspace/skills/freeride/SKILL.md b/workspace/skills/freeride/SKILL.md new file mode 100644 index 000000000..d95c292bc --- /dev/null +++ b/workspace/skills/freeride/SKILL.md @@ -0,0 +1,17 @@ +# FreeRide Skill + +FreeRide gives you unlimited free AI in PicoClaw by automatically managing OpenRouter's free models. + +## Usage + +- `/freeride auto`: Auto-configure best model + fallbacks. +- `/freeride list`: See all 30+ free models ranked. +- `/freeride status`: Check your current setup. + +## How it works + +The skill uses the `freeride` tool to fetch free models from OpenRouter, ranks them by context length, capabilities, recency, and provider trust, and then updates your PicoClaw configuration with the best models as fallbacks. + +## Setup + +Ensure you have your OpenRouter API key set in your K3s secrets or environment variables as `OPENROUTER_API_KEY`. From 68ecc96f87a30473964622e5b199226257f6c63b Mon Sep 17 00:00:00 2001 From: stevef Date: Mon, 20 Apr 2026 19:34:35 +0200 Subject: [PATCH 8/8] docs: add timeout command description to freeride skill --- cmd/picoclaw/internal/onboard/workspace/skills/freeride/SKILL.md | 1 + workspace/skills/freeride/SKILL.md | 1 + 2 files changed, 2 insertions(+) diff --git a/cmd/picoclaw/internal/onboard/workspace/skills/freeride/SKILL.md b/cmd/picoclaw/internal/onboard/workspace/skills/freeride/SKILL.md index d95c292bc..90f45f920 100644 --- a/cmd/picoclaw/internal/onboard/workspace/skills/freeride/SKILL.md +++ b/cmd/picoclaw/internal/onboard/workspace/skills/freeride/SKILL.md @@ -7,6 +7,7 @@ FreeRide gives you unlimited free AI in PicoClaw by automatically managing OpenR - `/freeride auto`: Auto-configure best model + fallbacks. - `/freeride list`: See all 30+ free models ranked. - `/freeride status`: Check your current setup. +- `/freeride timeout 120`: Set request timeout for free models (seconds). ## How it works diff --git a/workspace/skills/freeride/SKILL.md b/workspace/skills/freeride/SKILL.md index d95c292bc..90f45f920 100644 --- a/workspace/skills/freeride/SKILL.md +++ b/workspace/skills/freeride/SKILL.md @@ -7,6 +7,7 @@ FreeRide gives you unlimited free AI in PicoClaw by automatically managing OpenR - `/freeride auto`: Auto-configure best model + fallbacks. - `/freeride list`: See all 30+ free models ranked. - `/freeride status`: Check your current setup. +- `/freeride timeout 120`: Set request timeout for free models (seconds). ## How it works