mirror of
https://github.com/agentclientprotocol/claude-agent-acp.git
synced 2026-11-23 17:51:47 +00:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7c86d4b5a4 | ||
|
|
9ca8984d55 |
4
.github/workflows/publish.yml
vendored
4
.github/workflows/publish.yml
vendored
@@ -36,7 +36,7 @@ jobs:
|
||||
# Generating a GitHub token, so that PRs and tags created by
|
||||
# the release-please-action can trigger actions workflows.
|
||||
- name: Generate GitHub token
|
||||
uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510
|
||||
uses: actions/create-github-app-token@f418cff399f7aef4c2f0ad47e25ca7a6abbdd6fc
|
||||
id: generate-token
|
||||
with:
|
||||
# GitHub App ID secret name
|
||||
@@ -292,7 +292,7 @@ jobs:
|
||||
exit 1
|
||||
|
||||
- name: Generate token scoped to the registry repo
|
||||
uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510
|
||||
uses: actions/create-github-app-token@f418cff399f7aef4c2f0ad47e25ca7a6abbdd6fc
|
||||
id: registry-token
|
||||
with:
|
||||
app-id: ${{ secrets.REGISTRY_UPDATER_APP_ID }}
|
||||
|
||||
@@ -10234,30 +10234,39 @@ describe("ACP v2 session translation", () => {
|
||||
expect(terminals.split("s", hook)).toEqual({ terminal: [], report: hook });
|
||||
});
|
||||
|
||||
it("sends piped output with each line feed as CR LF, as a PTY would", () => {
|
||||
it("adds CR only to bare line feeds in terminal chunks and snapshots", () => {
|
||||
const terminals = new V2Terminals();
|
||||
const decoded = (data: unknown) => Buffer.from(String(data), "base64").toString();
|
||||
terminals.split("s", {
|
||||
sessionUpdate: "tool_call",
|
||||
toolCallId: "toolu_bash",
|
||||
title: "Terminal",
|
||||
_meta: { terminal_info: { terminal_id: "term" } },
|
||||
});
|
||||
const [chunk] = terminals.split("s", {
|
||||
sessionUpdate: "tool_call_update",
|
||||
toolCallId: "toolu_bash",
|
||||
_meta: { terminal_output_delta: { terminal_id: "term", data: "a\nb\n" } },
|
||||
}).terminal;
|
||||
expect(chunk).toMatchObject({ sessionUpdate: "terminal_output_chunk" });
|
||||
expect(decoded((chunk as { data: string }).data)).toBe("a\r\nb\r\n");
|
||||
// The line discipline adds a CR before every LF, also one that a CR precedes.
|
||||
const [snapshot] = terminals.split("s", {
|
||||
sessionUpdate: "tool_call_update",
|
||||
toolCallId: "toolu_bash",
|
||||
_meta: { terminal_output: { terminal_id: "term", data: "a\nb\r\n" } },
|
||||
}).terminal;
|
||||
expect(snapshot).toMatchObject({ sessionUpdate: "terminal_update" });
|
||||
expect(decoded((snapshot as { output: { data: string } }).output.data)).toBe("a\r\nb\r\r\n");
|
||||
const emit = (key: "terminal_output" | "terminal_output_delta", data: string) =>
|
||||
terminals.split("s", {
|
||||
sessionUpdate: "tool_call_update",
|
||||
toolCallId: "toolu_bash",
|
||||
_meta: { [key]: { terminal_id: "term", data } },
|
||||
}).terminal[0];
|
||||
const chunk = (text: string, expected: string) =>
|
||||
expect(emit("terminal_output_delta", text)).toMatchObject({
|
||||
sessionUpdate: "terminal_output_chunk",
|
||||
data: Buffer.from(expected).toString("base64"),
|
||||
});
|
||||
const snapshot = (text: string, expected: string) =>
|
||||
expect(emit("terminal_output", text)).toMatchObject({
|
||||
sessionUpdate: "terminal_update",
|
||||
output: { data: Buffer.from(expected).toString("base64") },
|
||||
});
|
||||
|
||||
chunk("a\nb\r\n10%\r20%\r", "a\r\nb\r\n10%\r20%\r");
|
||||
chunk("", "");
|
||||
chunk("\nend\r", "\nend\r");
|
||||
// A snapshot replaces the preceding bytes, including their trailing CR.
|
||||
snapshot("\ndone\r\n", "\r\ndone\r\n");
|
||||
chunk("\n", "\r\n");
|
||||
snapshot("waiting\r", "waiting\r");
|
||||
chunk("\n", "\n");
|
||||
});
|
||||
|
||||
it("leaves an exit code that the agent does not know out of the terminal's exit", () => {
|
||||
|
||||
@@ -33,13 +33,17 @@ const TERMINAL_KEYS = [
|
||||
"terminal_exit",
|
||||
] as const;
|
||||
|
||||
interface RunningTerminal {
|
||||
terminalId: string;
|
||||
command?: string;
|
||||
cancellable?: boolean;
|
||||
endsInCR: boolean;
|
||||
}
|
||||
|
||||
/** The terminal updates of the tool call reports of one v2 connection. */
|
||||
export class V2Terminals {
|
||||
/** The terminal and the last reported command of each running command, by session and tool call. */
|
||||
private readonly running = new Map<
|
||||
string,
|
||||
{ terminalId: string; command?: string; cancellable?: boolean }
|
||||
>();
|
||||
/** The display terminal of each running command, by session and tool call. */
|
||||
private readonly running = new Map<string, RunningTerminal>();
|
||||
|
||||
/**
|
||||
* The terminal updates that a tool call report carries in the extension's
|
||||
@@ -63,7 +67,7 @@ export class V2Terminals {
|
||||
meta.terminal_info &&
|
||||
this.running.get(key)?.terminalId !== meta.terminal_info.terminal_id
|
||||
) {
|
||||
this.running.set(key, { terminalId: meta.terminal_info.terminal_id });
|
||||
this.running.set(key, { terminalId: meta.terminal_info.terminal_id, endsInCR: false });
|
||||
}
|
||||
const terminal: v2.SessionUpdate[] = [];
|
||||
const running = this.running.get(key);
|
||||
@@ -77,7 +81,7 @@ export class V2Terminals {
|
||||
sessionUpdate: "terminal_update",
|
||||
terminalId: running.terminalId,
|
||||
...(newCommand ? { command } : {}),
|
||||
...(output ? { output: { data: terminalBytes(output.data) } } : {}),
|
||||
...(output ? { output: { data: terminalBytes(output.data, running, "snapshot") } } : {}),
|
||||
...(exit
|
||||
? {
|
||||
exitStatus: {
|
||||
@@ -94,7 +98,7 @@ export class V2Terminals {
|
||||
terminal.push({
|
||||
sessionUpdate: "terminal_output_chunk",
|
||||
terminalId: running.terminalId,
|
||||
data: terminalBytes(delta.data),
|
||||
data: terminalBytes(delta.data, running, "chunk"),
|
||||
});
|
||||
}
|
||||
if (exit) this.running.delete(key);
|
||||
@@ -161,13 +165,16 @@ function commandOf(rawInput: unknown): string | undefined {
|
||||
return typeof command === "string" && command.length > 0 ? command : undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* The output as the bytes a PTY would have delivered, base64-encoded as a v2
|
||||
* terminal carries them. A v2 terminal carries the bytes a terminal emulator
|
||||
* receives, but Claude Code runs a command with its output piped, so no line
|
||||
* discipline turned each LF into CR LF (`ONLCR`). Without the CR, an emulator
|
||||
* moves down a row and keeps the column: "a\nb" draws "b" under the end of "a".
|
||||
*/
|
||||
function terminalBytes(text: string): string {
|
||||
return Buffer.from(text.replaceAll("\n", "\r\n"), "utf8").toString("base64");
|
||||
/** Insert a carriage return before bare LF so the next line starts at column zero. */
|
||||
function terminalBytes(
|
||||
text: string,
|
||||
terminal: RunningTerminal,
|
||||
kind: "snapshot" | "chunk",
|
||||
): string {
|
||||
const followsCR = kind === "chunk" && terminal.endsInCR;
|
||||
const normalized = text.replace(/(?<!\r)\n/g, (lf, offset) =>
|
||||
offset === 0 && followsCR ? lf : "\r\n",
|
||||
);
|
||||
if (kind === "snapshot" || text.length > 0) terminal.endsInCR = text.endsWith("\r");
|
||||
return Buffer.from(normalized, "utf8").toString("base64");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user