char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Aleksander Grygierui: Improve Chat Messages rendering performance (#28460)0afb805b1

char/aliceai-t5-moe
1.7 KiB52 linesraw
1/**
2 * Parsing helpers for `exec_shell_command` tool output.
3 *
4 * The server appends one final line to the response - an exit-code summary
5 * shaped as `[exit code: N]` (and optionally followed by `[exit due to timed
6 * out]`) - so the renderer can color that final line based on success/failure
7 * without parsing the entire output stream.
8 */
9
10export interface ExecShellExitStatus {
11	code: number;
12	timedOut: boolean;
13	/** Length-prefix slice for matching against the rendered lines list. */
14	rawText: string;
15}
16
17// Anchor to the absolute end so intermediate "[exit code: N]" string content
18// (e.g. a shell echo) doesn't false-positive. The marker is at most ~50 chars
19// with the timed-out suffix, so matching a tail slice keeps the cost constant
20// for megabyte exec outputs instead of scanning the whole blob.
21const EXIT_CODE_TAIL_REGEX = /\[exit code: (-?\d+)\](?: \[exit due to timed out\])?\s*$/;
22const EXIT_CODE_TAIL_SCAN = 128;
23
24export function parseExecShellCommandExitStatus(
25	toolResultString: string | undefined
26): ExecShellExitStatus | undefined {
27	if (!toolResultString) return undefined;
28
29	const match = toolResultString.slice(-EXIT_CODE_TAIL_SCAN).match(EXIT_CODE_TAIL_REGEX);
30
31	if (!match) return undefined;
32
33	return {
34		code: Number.parseInt(match[1], 10),
35		rawText: match[0],
36		timedOut: match[0].includes('exit due to timed out')
37	};
38}
39
40/**
41 * Returns true when the supplied rendered line equals (trimmed) the
42 * trailing exit-code text. Used by the renderer to drop the duplicated
43 * representation (since the trailing line is replaced by a status badge).
44 */
45export function isExitCodeSummaryLine(
46	lineText: string,
47	status: ExecShellExitStatus | undefined
48): boolean {
49	if (!status) return false;
50
51	return lineText.trim() === status.rawText.trim();
52}