|
1 | 1 | #!/usr/bin/env node |
2 | 2 | import * as NodeChildProcess from "node:child_process"; |
3 | 3 | import * as NodeFS from "node:fs"; |
| 4 | +import * as NodeStringDecoder from "node:string_decoder"; |
4 | 5 |
|
5 | 6 | // Transparent CLI observer: retain only user text blocks, never env, auth, or control frames. |
6 | 7 | const executable = process.env.T3_CLAUDE_OBSERVER_EXECUTABLE; |
@@ -11,10 +12,11 @@ if (!executable || !output?.startsWith("/tmp/")) { |
11 | 12 | const child = NodeChildProcess.spawn(executable, process.argv.slice(2), { |
12 | 13 | stdio: ["pipe", "inherit", "inherit"], |
13 | 14 | }); |
| 15 | +// One decoder across chunks, so a multibyte character split between reads stays intact. |
| 16 | +const decoder = new NodeStringDecoder.StringDecoder("utf8"); |
14 | 17 | let pending = ""; |
15 | | -process.stdin.on("data", (chunk) => { |
16 | | - child.stdin.write(chunk); |
17 | | - pending += chunk; |
| 18 | +function record(text) { |
| 19 | + pending += text; |
18 | 20 | const lines = pending.split("\n"); |
19 | 21 | pending = lines.pop(); |
20 | 22 | for (const line of lines) { |
@@ -36,8 +38,15 @@ process.stdin.on("data", (chunk) => { |
36 | 38 | { mode: 0o600 }, |
37 | 39 | ); |
38 | 40 | } |
| 41 | +} |
| 42 | +process.stdin.on("data", (chunk) => { |
| 43 | + child.stdin.write(chunk); |
| 44 | + record(decoder.write(chunk)); |
| 45 | +}); |
| 46 | +process.stdin.on("end", () => { |
| 47 | + child.stdin.end(); |
| 48 | + record(decoder.end() + "\n"); |
39 | 49 | }); |
40 | | -process.stdin.on("end", () => child.stdin.end()); |
41 | 50 | child.on("exit", (code) => process.exit(code ?? 1)); |
42 | 51 | process.on("SIGTERM", () => child.kill("SIGTERM")); |
43 | 52 | process.on("SIGINT", () => child.kill("SIGINT")); |