|
1 | 1 | import { sliceUtf16Safe, truncateUtf16Safe } from "@openclaw/normalization-core/utf16-slice"; |
2 | 2 | import { sanitizeForLog, stripAnsi } from "../../packages/terminal-core/src/ansi.js"; |
| 3 | +import { sanitizeTerminalText } from "../../packages/terminal-core/src/safe-text.js"; |
3 | 4 | import type { SpawnResult } from "./exec-result.js"; |
4 | 5 | import type { runCommandBuffered } from "./exec.js"; |
5 | 6 |
|
6 | | -function formatCommandOutputs(outputs: (string | Buffer)[], maxChars: number, shared = false) { |
7 | | - // CR redraws replace the current progress frame; keep its final visible content. |
8 | | - const normalized = outputs.map((output) => |
9 | | - stripAnsi(output.toString()) |
10 | | - .replace(/\r\n/g, "\n") |
11 | | - .split("\n") |
12 | | - .map((line) => line.replace(/\r+$/, "").split("\r").at(-1) ?? "") |
13 | | - .join("\n") |
14 | | - .trim(), |
15 | | - ); |
16 | | - // Extra streams reserve their newline and omission marker inside the existing bound. |
17 | | - const count = shared ? Math.max(1, normalized.filter(Boolean).length) : 1; |
18 | | - const limit = shared ? Math.floor((maxChars - 3 * (count - 1)) / count) : maxChars; |
19 | | - return normalized.map((output) => { |
| 7 | +function normalizeCommandOutput(output: string | Buffer): string { |
| 8 | + const visible = stripAnsi(output.toString()) |
| 9 | + .replace(/\r\n/g, "\n") |
| 10 | + .split("\n") |
| 11 | + .map((line) => line.replace(/\r+$/, "").split("\r").at(-1) ?? "") |
| 12 | + .join("\n") |
| 13 | + .trim(); |
| 14 | + return visible |
| 15 | + .split("\n") |
| 16 | + .map((line) => sanitizeTerminalText(line)) |
| 17 | + .join("\n"); |
| 18 | +} |
| 19 | + |
| 20 | +function renderCommandOutputTail(output: string, maxChars: number, markerChars = 0): string { |
| 21 | + const tail = output.split("\n").slice(-12).join("\n"); |
| 22 | + if (tail.length === output.length && tail.length <= maxChars) { |
| 23 | + return tail; |
| 24 | + } |
| 25 | + const contentLimit = Math.max(0, Math.floor(maxChars)) - markerChars; |
| 26 | + return contentLimit < 0 |
| 27 | + ? "" |
| 28 | + : `…\n${contentLimit > 0 ? sliceUtf16Safe(tail, -contentLimit) : ""}`; |
| 29 | +} |
| 30 | + |
| 31 | +function formatCommandErrorDetail(stderr: string | Buffer, stdout: string | Buffer): string { |
| 32 | + const streams = [normalizeCommandOutput(stderr), normalizeCommandOutput(stdout)]; |
| 33 | + const visible = streams.filter(Boolean); |
| 34 | + if (visible.length < 2) { |
| 35 | + return visible[0] ? renderCommandOutputTail(visible[0], 2_000) : ""; |
| 36 | + } |
| 37 | + |
| 38 | + const bodyBudget = 2_000 - "stderr: \nstdout: ".length; |
| 39 | + const demands = streams.map((output) => { |
20 | 40 | const tail = output.split("\n").slice(-12).join("\n"); |
21 | | - const omitted = tail.length < output.length || tail.length > limit; |
22 | | - return `${omitted ? "…\n" : ""}${sliceUtf16Safe(tail, -limit)}`; |
| 41 | + return tail.length + (tail.length < output.length ? 2 : 0); |
23 | 42 | }); |
| 43 | + let allocations = [...demands]; |
| 44 | + if (demands[0]! + demands[1]! > bodyBudget) { |
| 45 | + allocations = demands.map((demand) => Math.min(demand, Math.floor(bodyBudget / 2))); |
| 46 | + const remaining = bodyBudget - allocations[0]! - allocations[1]!; |
| 47 | + const stderrUnmet = demands[0]! - allocations[0]!; |
| 48 | + const stdoutUnmet = demands[1]! - allocations[1]!; |
| 49 | + const index = stderrUnmet >= stdoutUnmet ? 0 : 1; |
| 50 | + allocations[index] = allocations[index]! + remaining; |
| 51 | + } |
| 52 | + return `stderr: ${renderCommandOutputTail( |
| 53 | + streams[0]!, |
| 54 | + Math.max(0, Math.floor(allocations[0]!)), |
| 55 | + 2, |
| 56 | + )}\nstdout: ${renderCommandOutputTail(streams[1]!, Math.max(0, Math.floor(allocations[1]!)), 2)}`; |
24 | 57 | } |
25 | 58 |
|
26 | 59 | export function formatCommandOutput(output: string | Buffer, maxChars = 800): string { |
27 | | - return formatCommandOutputs([output], maxChars).join(""); |
| 60 | + return renderCommandOutputTail(normalizeCommandOutput(output), maxChars); |
28 | 61 | } |
29 | 62 |
|
30 | | -// Pass an operation label, never argv that may contain credentials. |
| 63 | +/** Use an operation label, never argv that may contain credentials. */ |
31 | 64 | export function formatCommandResult(command: string, result: SpawnResult): string { |
32 | 65 | const label = truncateUtf16Safe(sanitizeForLog(command.replace(/[\r\n]+/g, " ")), 256); |
33 | 66 | const termination = result.outputLimitExceeded ? "output-limit" : result.termination; |
34 | 67 | const signal = result.signal ? `, signal=${result.signal}` : ""; |
35 | 68 | const killed = result.killed ? ", killed=true" : ""; |
36 | 69 | const status = result.code === 0 ? "exited" : "failed"; |
37 | | - return [ |
| 70 | + const lines = [ |
38 | 71 | `${label} ${status} (code=${result.code}, termination=${termination}${signal}${killed})`, |
39 | | - ...formatCommandOutputs([result.stderr, result.stdout], 800).flatMap((output, index) => |
40 | | - output ? [`${index === 0 ? "stderr" : "stdout"}: ${output}`] : [], |
41 | | - ), |
42 | | - ].join("\n"); |
| 72 | + ]; |
| 73 | + for (const stream of ["stderr", "stdout"] as const) { |
| 74 | + const output = formatCommandOutput(result[stream]); |
| 75 | + if (output) { |
| 76 | + lines.push(`${stream}: ${output}`); |
| 77 | + } |
| 78 | + } |
| 79 | + return lines.join("\n"); |
43 | 80 | } |
44 | 81 |
|
45 | 82 | export function createCommandError( |
46 | 83 | command: string, |
47 | 84 | result: SpawnResult | Awaited<ReturnType<typeof runCommandBuffered>>, |
48 | 85 | options: { timeoutMs: number }, |
49 | 86 | ): Error { |
50 | | - const detail = formatCommandOutputs([result.stderr, result.stdout], 2000, true) |
51 | | - .filter(Boolean) |
52 | | - .join("\n"); |
| 87 | + const detail = formatCommandErrorDetail(result.stderr, result.stdout); |
53 | 88 | const reasons: string[] = []; |
54 | 89 | if (result.termination === "timeout") { |
55 | 90 | reasons.push(`timed out after ${options.timeoutMs / 1000} seconds`); |
|
0 commit comments