From ecfd36afa6c4e31f12e1daf9b8a2bdc12dda1794 Mon Sep 17 00:00:00 2001 From: Ben Brandt Date: Mon, 15 Sep 2025 18:27:43 +0200 Subject: [PATCH] Support /compact (#51) It would be nice to also support sending status via hooks when the compact action is happening (and maybe show something in the UI to show where compacting happened in the thread) but for now this at least allows it. --------- Signed-off-by: Ben Brandt --- src/acp-agent.ts | 25 +++++++++++++++++---- src/tests/acp-agent.test.ts | 44 +++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 4 deletions(-) diff --git a/src/acp-agent.ts b/src/acp-agent.ts index 86276df5..1691125c 100644 --- a/src/acp-agent.ts +++ b/src/acp-agent.ts @@ -284,6 +284,24 @@ export class ClaudeAcpAgent implements Agent { continue; } + // Slash commands like /compact can generate invalid output... doesn't match + // their own docs: https://docs.anthropic.com/en/docs/claude-code/sdk/sdk-slash-commands#%2Fcompact-compact-conversation-history + if ( + typeof message.message.content === "string" && + message.message.content.includes("") + ) { + console.log(message.message.content); + break; + } + + if ( + typeof message.message.content === "string" && + message.message.content.includes("") + ) { + console.error(message.message.content); + break; + } + if ( message.message.model === "" && message.message.content.length === 1 && @@ -361,7 +379,6 @@ async function getAvailableSlashCommands(query: Query): Promise { + .map((command) => { const input = command.argumentHint ? { hint: command.argumentHint } : null; return { name: command.name, + // @ts-expect-error type in the ts interface description: command.description || "", input, }; diff --git a/src/tests/acp-agent.test.ts b/src/tests/acp-agent.test.ts index 0a1a259e..e7a271be 100644 --- a/src/tests/acp-agent.test.ts +++ b/src/tests/acp-agent.test.ts @@ -191,6 +191,50 @@ describe.skipIf(!process.env.RUN_INTEGRATION_TESTS)("ACP subprocess integration" expect(client.takeReceivedText()).toContain("Hello GPT-5"); }, 30000); + + it("/compact works", async () => { + const { client, connection, newSessionResponse } = await setupTestSession(__dirname); + + const commands = await client.availableCommandsPromise; + + expect(commands).toContainEqual({ + description: + "Clear conversation history but keep a summary in context. Optional: /compact [instructions for summarization]", + input: { + hint: "", + }, + name: "compact", + }); + + // Error case (no previous message) + await connection.prompt({ + prompt: [{ type: "text", text: "/compact" }], + sessionId: newSessionResponse.sessionId, + }); + + expect(client.takeReceivedText()).toBe(""); + + // Send something + await connection.prompt({ + prompt: [{ type: "text", text: "Hi" }], + sessionId: newSessionResponse.sessionId, + }); + // Clear response + client.takeReceivedText(); + + // Test with instruction + await connection.prompt({ + prompt: [ + { + type: "text", + text: "/compact greeting", + }, + ], + sessionId: newSessionResponse.sessionId, + }); + + expect(client.takeReceivedText()).toContain(""); + }, 30000); }); describe("tool conversions", () => {