Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions agent-chat/adapters/acp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ function handleAgentMessage(sess: SessionCtx, st: AcpState, def: ProviderDef, ms
kind: "tool-start",
toolId: u.toolCallId,
name: u.title ?? u.kind ?? "tool",
detail: truncate(JSON.stringify(u.rawInput ?? {})),
detail: acpToolCallDetail(u.locations, u.rawInput),
});
break;
case "tool_call_update":
Expand Down Expand Up @@ -573,10 +573,34 @@ function normalizeCommands(commands: any): CommandEntry[] {
})).filter((c) => c.name);
}

function contentText(content: unknown): string {
export function acpToolCallDetail(locations: unknown, rawInput: unknown): string {
const paths = Array.isArray(locations)
? locations
.map((location: any) => typeof location?.path === "string" ? location.path : "")
.filter(Boolean)
: [];
return truncate(paths.length ? paths.join(", ") : JSON.stringify(rawInput ?? {}));
}

function lineCount(text: string): number {
return text ? text.split(/\r\n|\r|\n/).length : 0;
}

function diffText(content: any): string {
const oldText = typeof content.oldText === "string" ? content.oldText : "";
const newText = typeof content.newText === "string" ? content.newText : "";
const path = typeof content.path === "string" && content.path ? content.path : "unknown path";
return `diff ${path} (+${lineCount(newText)}/-${lineCount(oldText)})${newText ? `\n${newText}` : ""}`;
}

export function contentText(content: unknown): string {
if (!Array.isArray(content)) return "";
return content
.map((c: any) => c?.content?.text ?? c?.text ?? "")
.map((c: any) => {
if (c?.type === "diff") return diffText(c);
if (c?.type === "terminal") return `terminal ${String(c.terminalId ?? "")}`.trim();
return c?.content?.text ?? c?.text ?? "";
})
.join("");
}

Expand Down
56 changes: 56 additions & 0 deletions agent-chat/test/acp-tool-detail.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { expect, test } from "bun:test";
import { makeAcpAdapter } from "../adapters/acp";
import type { AgentEvent, ProviderDef, SessionCtx, SessionStatus } from "../types";

test("shows ACP tool locations and every tool output variant", async () => {
const def: ProviderDef = {
id: "fake-acp-tool-detail",
label: "Fake ACP Tool Detail",
adapter: "acp",
cmd: ["bun", `${import.meta.dir}/fake-acp-tool-detail.ts`],
};
const adapter = makeAcpAdapter(def);
const events: AgentEvent[] = [];
const sess: SessionCtx = {
id: "fake-tool-detail-session",
provider: def.id,
cwd: `${import.meta.dir}/../scratch`,
title: "fake ACP tool detail",
autoApprove: true,
startOptions: {},
status: "idle",
events,
internal: {},
emit(evt: AgentEvent) {
events.push(evt);
},
setStatus(status: SessionStatus) {
this.status = status;
},
};

try {
await adapter.send(sess, "show tool details");

const starts = events.filter((event) => event.kind === "tool-start");
expect(starts).toEqual([
{ kind: "tool-start", toolId: "located", name: "Edit files", detail: "src/one.ts, src/two.ts" },
{ kind: "tool-start", toolId: "raw-input", name: "Inspect files", detail: '{"command":"cat README.md"}' },
]);

const ends = events.filter((event) => event.kind === "tool-end");
expect(ends.find((event) => event.toolId === "new-file")?.detail).toContain("src/new.ts");
expect(ends.find((event) => event.toolId === "new-file")?.detail).toContain("+2/-0");
expect(ends.find((event) => event.toolId === "new-file")?.detail).toContain("one two");

expect(ends.find((event) => event.toolId === "changed-file")?.detail).toContain("src/changed.ts");
expect(ends.find((event) => event.toolId === "changed-file")?.detail).toContain("+2/-1");

const mixed = ends.find((event) => event.toolId === "mixed")?.detail ?? "";
expect(mixed).toContain("text output");
expect(mixed).toContain("src/mixed.ts");
expect(mixed).toContain("term-42");
} finally {
adapter.dispose(sess);
}
});
55 changes: 55 additions & 0 deletions agent-chat/test/fake-acp-tool-detail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { createInterface } from "node:readline";

const send = (msg: unknown) => {
process.stdout.write(`${JSON.stringify(msg)}\n`);
};

for await (const line of createInterface({ input: process.stdin })) {
if (!line.trim()) continue;
const msg = JSON.parse(line);
if (msg.method === "initialize") {
send({ jsonrpc: "2.0", id: msg.id, result: { protocolVersion: 1 } });
} else if (msg.method === "session/new") {
send({ jsonrpc: "2.0", id: msg.id, result: { sessionId: "fake-tool-detail" } });
} else if (msg.method === "session/prompt") {
const notify = (update: unknown) => send({ jsonrpc: "2.0", method: "session/update", params: { update } });
notify({
sessionUpdate: "tool_call",
toolCallId: "located",
title: "Edit files",
rawInput: { command: "edit" },
locations: [{ path: "src/one.ts", line: 4 }, { path: "src/two.ts" }],
});
notify({
sessionUpdate: "tool_call",
toolCallId: "raw-input",
title: "Inspect files",
rawInput: { command: "cat README.md" },
});
notify({
sessionUpdate: "tool_call_update",
toolCallId: "new-file",
status: "completed",
content: [{ type: "diff", path: "src/new.ts", oldText: null, newText: "one\ntwo" }],
});
notify({
sessionUpdate: "tool_call_update",
toolCallId: "changed-file",
status: "completed",
content: [{ type: "diff", path: "src/changed.ts", oldText: "old", newText: "new\nlines" }],
});
notify({
sessionUpdate: "tool_call_update",
toolCallId: "mixed",
status: "completed",
content: [
{ type: "content", content: { type: "text", text: "text output" } },
{ type: "diff", path: "src/mixed.ts", oldText: "old", newText: "new" },
{ type: "terminal", terminalId: "term-42" },
],
});
send({ jsonrpc: "2.0", id: msg.id, result: { stopReason: "end_turn" } });
} else if (msg.id != null) {
send({ jsonrpc: "2.0", id: msg.id, result: {} });
}
}
Loading