Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

feat(cli): decode escape sequences in message send --text

Gus70d99d66fdc7
diff --git a/src/daemon/src/cli/index.test.ts b/src/daemon/src/cli/index.test.ts
index 3471dc7d5..a6d5eb285 100644
--- a/src/daemon/src/cli/index.test.ts
+++ b/src/daemon/src/cli/index.test.ts
@@ -1,5 +1,5 @@
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
-import { main, setApiForTesting } from "./index";
+import { main, setApiForTesting, decodeTextEscapes } from "./index";
import type { ServerApi } from "../server/contract";
/** Capture exactly the JSON object the CLI prints to stdout. */
@@ -892,3 +892,76 @@ describe("friend list", () => {
expect(env).toEqual({ success: { accepted: [], pendingOutgoing: [], pendingIncoming: [] } });
});
});
+
+describe("decodeTextEscapes", () => {
+ it("decodes \\n to a real newline", () => {
+ expect(decodeTextEscapes("a\\nb")).toBe("a\nb");
+ });
+
+ it("decodes \\n\\n to two newlines (the reported case)", () => {
+ expect(decodeTextEscapes("a\\n\\nb")).toBe("a\n\nb");
+ });
+
+ it("decodes \\t and \\r", () => {
+ expect(decodeTextEscapes("a\\tb\\rc")).toBe("a\tb\rc");
+ });
+
+ it("treats an escaped backslash as literal — \\\\n is NOT a newline (single-pass correctness)", () => {
+ // Input chars: backslash, backslash, n → backslash, n (literal), no LF.
+ expect(decodeTextEscapes("a\\\\nb")).toBe("a\\nb");
+ expect(decodeTextEscapes("a\\\\nb")).not.toContain("\n");
+ });
+
+ it("passes an unknown escape through unchanged (backslash kept)", () => {
+ expect(decodeTextEscapes("a\\qb")).toBe("a\\qb");
+ });
+
+ it("leaves a trailing lone backslash unchanged", () => {
+ expect(decodeTextEscapes("ab\\")).toBe("ab\\");
+ });
+
+ it("leaves plain text (incl. UTF-8/emoji) untouched", () => {
+ expect(decodeTextEscapes("总部 🎉 no escapes")).toBe("总部 🎉 no escapes");
+ });
+});
+
+describe("message send — --text escape decoding wired end-to-end", () => {
+ it("sends a real newline body for --text \"a\\n\\nb\"", async () => {
+ let sentText: string | undefined;
+ setApiForTesting(
+ stubApi({
+ send: async (input: Parameters<ServerApi["send"]>[0]) => {
+ sentText = input.content.text;
+ return { state: "sent", message: { seq: "#1", channel: "/s/c", sender: "@a", content: { text: "" }, time: "" } };
+ },
+ }),
+ );
+ // Two literal chars backslash-n twice, as a shell would pass them.
+ await main(["message", "send", "--target", "/s/general", "--text", "a\\n\\nb"]);
+ expect(sentText).toBe("a\n\nb");
+ expect(sentText!.split("\n")).toHaveLength(3);
+ });
+
+ it("does NOT decode --file content (literal backslash-n stays literal)", async () => {
+ const fs = await import("fs");
+ const os = await import("os");
+ const path = await import("path");
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), "cli-esc-"));
+ const file = path.join(dir, "body.txt");
+ // File bytes contain a literal backslash + n (not a newline).
+ fs.writeFileSync(file, "a\\nb");
+ let sentText: string | undefined;
+ setApiForTesting(
+ stubApi({
+ send: async (input: Parameters<ServerApi["send"]>[0]) => {
+ sentText = input.content.text;
+ return { state: "sent", message: { seq: "#1", channel: "/s/c", sender: "@a", content: { text: "" }, time: "" } };
+ },
+ }),
+ );
+ await main(["message", "send", "--target", "/s/general", "--file", file]);
+ fs.rmSync(dir, { recursive: true, force: true });
+ expect(sentText).toBe("a\\nb");
+ expect(sentText).not.toContain("\n");
+ });
+});
diff --git a/src/daemon/src/cli/index.ts b/src/daemon/src/cli/index.ts
index e2198154e..48d827e04 100755
--- a/src/daemon/src/cli/index.ts
+++ b/src/daemon/src/cli/index.ts
@@ -83,6 +83,21 @@ function agentId(opts: Record<string, unknown>): string {
return id;
}
+const TEXT_ESCAPE_MAP: Record<string, string> = { n: "\n", t: "\t", r: "\r", "\\": "\\" };
+
+/**
+ * Decode the standard backslash escapes an agent types into `--text`
+ * (`\n`→newline, `\t`→tab, `\r`→CR, `\\`→one backslash). Single left-to-right
+ * pass via one regex so `\\` is consumed as a unit BEFORE its following char —
+ * sequential `.replace` calls would turn `\\n` (an escaped backslash + n) into
+ * a newline, which is wrong. Unknown escapes (`\q`) and a trailing lone `\`
+ * pass through unchanged (backslash kept) — conservative, never drops data.
+ * Only applied to `--text`; `--file` content stays byte-literal.
+ */
+export function decodeTextEscapes(s: string): string {
+ return s.replace(/\\(.)/g, (m, c: string) => TEXT_ESCAPE_MAP[c] ?? m);
+}
+
/* ------------------------------------------------------------------ */
/* Commands */
/* ------------------------------------------------------------------ */
@@ -139,9 +154,13 @@ async function cmdMessageSend(opts: Record<string, unknown>): Promise<unknown> {
if (fileFlag) {
const fs = await import("fs");
if (!fs.existsSync(fileFlag)) throw new CliError(`message send: file not found: ${fileFlag}`);
+ // `--file` content is already-real bytes — never escape-decode it, or a
+ // literal `\n` in a pasted code snippet / log would get corrupted.
text = fs.readFileSync(fileFlag, "utf8").trim();
} else if (typeof textFlag === "string") {
- text = textFlag;
+ // `--text` is a shell arg where agents naturally type `\n` for a newline;
+ // decode the standard escapes so it doesn't land as a literal backslash-n.
+ text = decodeTextEscapes(textFlag);
}
// `--attachment` may repeat. Commander wires this via `.option(..., collect, [])`