From e1b8c301636fc800d1fdb40afaf18e220be5331d Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sat, 20 Dec 2025 13:52:04 +0000 Subject: [PATCH] feat(web): toggle tool summaries mid-run --- docs/thinking.md | 2 +- src/agents/pi-embedded.ts | 7 +- src/auto-reply/reply.directive.test.ts | 131 ++++++++++++++++++++++++- src/auto-reply/reply.ts | 15 +++ 4 files changed, 150 insertions(+), 5 deletions(-) diff --git a/docs/thinking.md b/docs/thinking.md index c95fb765f..d58af351c 100644 --- a/docs/thinking.md +++ b/docs/thinking.md @@ -32,7 +32,7 @@ read_when: - Levels: `on|full` or `off` (default). - Directive-only message toggles session verbose and replies `Verbose logging enabled.` / `Verbose logging disabled.`; invalid levels return a hint without changing state. - Inline directive affects only that message; session/global defaults apply otherwise. -- When verbose is on, agents that emit structured tool results (Pi, other JSON agents) send each tool result back as its own metadata-only message, prefixed with `[🛠️ ]` when available (path/command); the tool output itself is not forwarded. These tool summaries are sent as soon as each tool finishes (separate bubbles), not as streaming deltas. +- When verbose is on, agents that emit structured tool results (Pi, other JSON agents) send each tool result back as its own metadata-only message, prefixed with `[🛠️ ]` when available (path/command); the tool output itself is not forwarded. These tool summaries are sent as soon as each tool finishes (separate bubbles), not as streaming deltas. If you toggle `/verbose on|off` while a run is in-flight, subsequent tool bubbles honor the new setting. ## Heartbeats - Heartbeat probe body is `HEARTBEAT`. Inline directives in a heartbeat message apply as usual (but avoid changing session defaults from heartbeats). diff --git a/src/agents/pi-embedded.ts b/src/agents/pi-embedded.ts index 5e54cdc8e..40b92f8cf 100644 --- a/src/agents/pi-embedded.ts +++ b/src/agents/pi-embedded.ts @@ -217,6 +217,7 @@ export async function runEmbeddedPiAgent(params: { timeoutMs: number; runId: string; abortSignal?: AbortSignal; + shouldEmitToolResult?: () => boolean; onPartialReply?: (payload: { text?: string; mediaUrls?: string[]; @@ -419,7 +420,11 @@ export async function runEmbeddedPiAgent(params: { isError, }, }); - if (params.verboseLevel === "on" && params.onToolResult) { + const emitToolResult = + typeof params.shouldEmitToolResult === "function" + ? params.shouldEmitToolResult() + : params.verboseLevel === "on"; + if (emitToolResult && params.onToolResult) { const agg = formatToolAggregate( toolName, meta ? [meta] : undefined, diff --git a/src/auto-reply/reply.directive.test.ts b/src/auto-reply/reply.directive.test.ts index ab1f186e4..c01294df9 100644 --- a/src/auto-reply/reply.directive.test.ts +++ b/src/auto-reply/reply.directive.test.ts @@ -2,13 +2,18 @@ import fs from "node:fs/promises"; import os from "node:os"; import path from "node:path"; -import { afterEach, describe, expect, it, vi } from "vitest"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; vi.mock("../agents/pi-embedded.js", () => ({ runEmbeddedPiAgent: vi.fn(), })); import { runEmbeddedPiAgent } from "../agents/pi-embedded.js"; +import { + loadSessionStore, + resolveSessionKey, + saveSessionStore, +} from "../config/sessions.js"; import { extractThinkDirective, extractVerboseDirective, @@ -28,6 +33,10 @@ async function withTempHome(fn: (home: string) => Promise): Promise { } describe("directive parsing", () => { + beforeEach(() => { + vi.mocked(runEmbeddedPiAgent).mockReset(); + }); + afterEach(() => { vi.restoreAllMocks(); }); @@ -91,8 +100,10 @@ describe("directive parsing", () => { }, ); - const text = Array.isArray(res) ? res[0]?.text : res?.text; - expect(text).toBe("done"); + const texts = (Array.isArray(res) ? res : [res]) + .map((entry) => entry?.text) + .filter(Boolean); + expect(texts).toContain("done"); expect(runEmbeddedPiAgent).toHaveBeenCalledOnce(); }); }); @@ -118,4 +129,118 @@ describe("directive parsing", () => { expect(runEmbeddedPiAgent).not.toHaveBeenCalled(); }); }); + + it("updates tool verbose during an in-flight run (toggle on)", async () => { + await withTempHome(async (home) => { + const storePath = path.join(home, "sessions.json"); + const ctx = { Body: "please do the thing", From: "+1004", To: "+2000" }; + const sessionKey = resolveSessionKey( + "per-sender", + { From: ctx.From, To: ctx.To, Body: ctx.Body }, + "main", + ); + + vi.mocked(runEmbeddedPiAgent).mockImplementation(async (params) => { + const shouldEmit = params.shouldEmitToolResult; + expect(shouldEmit?.()).toBe(false); + const store = loadSessionStore(storePath); + const entry = store[sessionKey] ?? { + sessionId: "s", + updatedAt: Date.now(), + }; + store[sessionKey] = { + ...entry, + verboseLevel: "on", + updatedAt: Date.now(), + }; + await saveSessionStore(storePath, store); + expect(shouldEmit?.()).toBe(true); + return { + payloads: [{ text: "done" }], + meta: { + durationMs: 5, + agentMeta: { sessionId: "s", provider: "p", model: "m" }, + }, + }; + }); + + const res = await getReplyFromConfig( + ctx, + {}, + { + inbound: { + allowFrom: ["*"], + workspace: path.join(home, "clawd"), + agent: { provider: "anthropic", model: "claude-opus-4-5" }, + session: { store: storePath }, + }, + }, + ); + + const texts = (Array.isArray(res) ? res : [res]) + .map((entry) => entry?.text) + .filter(Boolean); + expect(texts).toContain("done"); + expect(runEmbeddedPiAgent).toHaveBeenCalledOnce(); + }); + }); + + it("updates tool verbose during an in-flight run (toggle off)", async () => { + await withTempHome(async (home) => { + const storePath = path.join(home, "sessions.json"); + const ctx = { + Body: "please do the thing /verbose on", + From: "+1004", + To: "+2000", + }; + const sessionKey = resolveSessionKey( + "per-sender", + { From: ctx.From, To: ctx.To, Body: ctx.Body }, + "main", + ); + + vi.mocked(runEmbeddedPiAgent).mockImplementation(async (params) => { + const shouldEmit = params.shouldEmitToolResult; + expect(shouldEmit?.()).toBe(true); + const store = loadSessionStore(storePath); + const entry = store[sessionKey] ?? { + sessionId: "s", + updatedAt: Date.now(), + }; + store[sessionKey] = { + ...entry, + verboseLevel: "off", + updatedAt: Date.now(), + }; + await saveSessionStore(storePath, store); + expect(shouldEmit?.()).toBe(false); + return { + payloads: [{ text: "done" }], + meta: { + durationMs: 5, + agentMeta: { sessionId: "s", provider: "p", model: "m" }, + }, + }; + }); + + const res = await getReplyFromConfig( + ctx, + {}, + { + inbound: { + allowFrom: ["*"], + workspace: path.join(home, "clawd"), + agent: { provider: "anthropic", model: "claude-opus-4-5" }, + session: { store: storePath }, + }, + }, + ); + + const texts = (Array.isArray(res) ? res : [res]) + .map((entry) => entry?.text) + .filter(Boolean); + expect(texts).toContain("done"); + expect(runEmbeddedPiAgent).toHaveBeenCalledOnce(); + }); + }); }); diff --git a/src/auto-reply/reply.ts b/src/auto-reply/reply.ts index 56214c6ab..3733eecd8 100644 --- a/src/auto-reply/reply.ts +++ b/src/auto-reply/reply.ts @@ -330,6 +330,20 @@ export async function getReplyFromConfig( inlineVerbose ?? (sessionEntry?.verboseLevel as VerboseLevel | undefined) ?? (agentCfg?.verboseDefault as VerboseLevel | undefined); + const shouldEmitToolResult = () => { + if (!sessionKey || !storePath) { + return resolvedVerboseLevel === "on"; + } + try { + const store = loadSessionStore(storePath); + const entry = store[sessionKey]; + const current = normalizeVerboseLevel(entry?.verboseLevel); + if (current) return current === "on"; + } catch { + // ignore store read failures + } + return resolvedVerboseLevel === "on"; + }; const combinedDirectiveOnly = hasThinkDirective && @@ -760,6 +774,7 @@ export async function getReplyFromConfig( mediaUrls: payload.mediaUrls, }) : undefined, + shouldEmitToolResult, onToolResult: opts?.onToolResult ? (payload) => opts.onToolResult?.({