Verbose: send tool result metadata only

This commit is contained in:
Peter Steinberger
2025-12-03 09:40:05 +00:00
parent 394c751d7d
commit 318166f8b0
8 changed files with 108 additions and 31 deletions

View File

@@ -67,6 +67,15 @@ describe("agent buildArgs + parseOutput helpers", () => {
expect((parsed.meta?.usage as { output?: number })?.output).toBe(5);
});
it("piSpec carries tool names when present", () => {
const stdout =
'{"type":"message_end","message":{"role":"tool_result","name":"bash","content":[{"type":"text","text":"ls output"}]}}';
const parsed = piSpec.parseOutput(stdout);
const tool = parsed.toolResults?.[0] as { text?: string; toolName?: string };
expect(tool?.text).toBe("ls output");
expect(tool?.toolName).toBe("bash");
});
it("codexSpec parses agent_message and aggregates usage", () => {
const stdout = [
'{"type":"item.completed","item":{"type":"agent_message","text":"hi there"}}',

View File

@@ -1,6 +1,11 @@
import path from "node:path";
import type { AgentMeta, AgentParseResult, AgentSpec } from "./types.js";
import type {
AgentMeta,
AgentParseResult,
AgentSpec,
AgentToolResult,
} from "./types.js";
type PiAssistantMessage = {
role?: string;
@@ -9,15 +14,37 @@ type PiAssistantMessage = {
model?: string;
provider?: string;
stopReason?: string;
name?: string;
toolName?: string;
tool_call_id?: string;
toolCallId?: string;
};
function inferToolName(msg: PiAssistantMessage): string | undefined {
const candidates = [
msg.toolName,
msg.name,
msg.toolCallId,
msg.tool_call_id,
]
.map((c) => (typeof c === "string" ? c.trim() : ""))
.filter(Boolean);
if (candidates.length) return candidates[0];
if (msg.role && msg.role.includes(":")) {
const suffix = msg.role.split(":").slice(1).join(":").trim();
if (suffix) return suffix;
}
return undefined;
}
function parsePiJson(raw: string): AgentParseResult {
const lines = raw.split(/\n+/).filter((l) => l.trim().startsWith("{"));
// Collect only completed assistant messages (skip streaming updates/toolcalls).
const texts: string[] = [];
const toolResults: string[] = [];
const toolResults: AgentToolResult[] = [];
let lastAssistant: PiAssistantMessage | undefined;
let lastPushed: string | undefined;
@@ -59,7 +86,9 @@ function parsePiJson(raw: string): AgentParseResult {
.map((c) => c.text)
.join("\n")
.trim();
if (toolText) toolResults.push(toolText);
if (toolText) {
toolResults.push({ text: toolText, toolName: inferToolName(msg) });
}
}
} catch {
// ignore malformed lines

View File

@@ -15,11 +15,16 @@ export type AgentMeta = {
extra?: Record<string, unknown>;
};
export type AgentToolResult = {
text: string;
toolName?: string;
};
export type AgentParseResult = {
// Plural to support agents that emit multiple assistant turns per prompt.
texts?: string[];
mediaUrls?: string[];
toolResults?: string[];
toolResults?: Array<string | AgentToolResult>;
meta?: AgentMeta;
};