@useavalon/caelence-agent 0.2.5-canary.0 → 0.2.5-canary.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/desktop/README.md +2 -2
- package/desktop/scripts/run-tauri.ts +3 -2
- package/desktop/src/DeskChrome.tsx +18 -1
- package/desktop/src/app.css +32 -0
- package/package.json +2 -3
- package/src/agent-cwd.ts +51 -4
- package/src/cli/agent-cwd.test.ts +17 -0
- package/src/cli/main.ts +15 -9
- package/src/cli/theme.test.ts +40 -5
- package/src/cli/theme.ts +65 -4
- package/src/cli/tui-layout.test.ts +5 -3
- package/src/cli/tui-layout.ts +17 -8
- package/src/cli/tui-progress.test.ts +44 -0
- package/src/cli/tui-progress.ts +55 -0
- package/src/cli/tui-stream.ts +26 -0
- package/src/cli/tui-terminal-probe.test.ts +19 -0
- package/src/cli/tui-terminal-probe.ts +113 -0
- package/src/cli/tui-text-input-display.ts +6 -0
- package/src/cli/tui-text-input.test.ts +13 -1
- package/src/cli/tui-text-input.ts +14 -10
- package/src/cli/tui-text-input.tsx +45 -16
- package/src/cli/tui-transcript.test.ts +33 -0
- package/src/cli/tui-transcript.ts +59 -0
- package/src/cli/tui.tsx +279 -170
- package/src/desktop/bridge.ts +5 -1
- package/src/product.ts +1 -1
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { readLightTerminalOverride, terminalPrefersLightBackground } from "./theme.ts";
|
|
2
|
+
|
|
3
|
+
function channel(value: string): number {
|
|
4
|
+
const trimmed = value.trim();
|
|
5
|
+
if (!trimmed) return 0;
|
|
6
|
+
if (trimmed.includes("/")) {
|
|
7
|
+
const max = 16 ** trimmed.length - 1;
|
|
8
|
+
const n = Number.parseInt(trimmed, 16);
|
|
9
|
+
return Number.isNaN(n) ? 0 : n / max;
|
|
10
|
+
}
|
|
11
|
+
if (trimmed.length <= 2) {
|
|
12
|
+
const n = Number.parseInt(trimmed, 16);
|
|
13
|
+
return Number.isNaN(n) ? 0 : n / 255;
|
|
14
|
+
}
|
|
15
|
+
const n = Number.parseInt(trimmed, 16);
|
|
16
|
+
const max = 16 ** trimmed.length - 1;
|
|
17
|
+
return Number.isNaN(n) ? 0 : n / max;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Relative luminance 0–1 from an OSC 11 background report. */
|
|
21
|
+
export function luminanceFromOsc11(payload: string): number | undefined {
|
|
22
|
+
const text = payload.trim();
|
|
23
|
+
const rgbParts = /^rgb:([^/]+)\/([^/]+)\/([^/]+)/i.exec(text);
|
|
24
|
+
if (rgbParts) {
|
|
25
|
+
const r = channel(rgbParts[1] ?? "0");
|
|
26
|
+
const g = channel(rgbParts[2] ?? "0");
|
|
27
|
+
const b = channel(rgbParts[3] ?? "0");
|
|
28
|
+
return 0.2126 * r + 0.7152 * g + 0.0722 * b;
|
|
29
|
+
}
|
|
30
|
+
const hex = /^#?([0-9a-f]{6})$/i.exec(text);
|
|
31
|
+
if (hex?.[1]) {
|
|
32
|
+
const value = hex[1];
|
|
33
|
+
const r = channel(value.slice(0, 2));
|
|
34
|
+
const g = channel(value.slice(2, 4));
|
|
35
|
+
const b = channel(value.slice(4, 6));
|
|
36
|
+
return 0.2126 * r + 0.7152 * g + 0.0722 * b;
|
|
37
|
+
}
|
|
38
|
+
return undefined;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function parseOsc11FromBuffer(buffer: string): number | undefined {
|
|
42
|
+
const start = buffer.indexOf("\x1b]11;");
|
|
43
|
+
if (start < 0) return undefined;
|
|
44
|
+
const end = buffer.indexOf("\x07", start);
|
|
45
|
+
if (end < 0) return undefined;
|
|
46
|
+
const body = buffer.slice(start + 5, end);
|
|
47
|
+
return luminanceFromOsc11(body);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** OSC 11 background luminance query on the **current** screen (call before alt-screen). */
|
|
51
|
+
export function queryOsc11BackgroundLight(
|
|
52
|
+
stdin: NodeJS.ReadStream = process.stdin,
|
|
53
|
+
stdout: NodeJS.WriteStream = process.stdout,
|
|
54
|
+
timeoutMs = 250,
|
|
55
|
+
): Promise<boolean | undefined> {
|
|
56
|
+
if (!stdin.isTTY || !stdout.isTTY) return Promise.resolve(undefined);
|
|
57
|
+
|
|
58
|
+
return new Promise((resolve) => {
|
|
59
|
+
let buffer = "";
|
|
60
|
+
let raw = false;
|
|
61
|
+
const timeout = setTimeout(() => finish(undefined), timeoutMs);
|
|
62
|
+
|
|
63
|
+
const finish = (light: boolean | undefined) => {
|
|
64
|
+
clearTimeout(timeout);
|
|
65
|
+
stdin.off("data", onData);
|
|
66
|
+
if (raw && stdin.isTTY && stdin.setRawMode) stdin.setRawMode(false);
|
|
67
|
+
resolve(light);
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const onData = (chunk: Buffer | string) => {
|
|
71
|
+
buffer += typeof chunk === "string" ? chunk : chunk.toString("utf8");
|
|
72
|
+
const lum = parseOsc11FromBuffer(buffer);
|
|
73
|
+
if (lum === undefined) return;
|
|
74
|
+
finish(lum > 0.55);
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
if (stdin.isTTY && stdin.setRawMode) {
|
|
78
|
+
stdin.setRawMode(true);
|
|
79
|
+
raw = true;
|
|
80
|
+
}
|
|
81
|
+
stdin.on("data", onData);
|
|
82
|
+
stdout.write("\x1b]11;?\x07");
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Best-effort light/dark detection for TUI contrast.
|
|
88
|
+
* Run on the main screen before switching to the alt buffer.
|
|
89
|
+
*/
|
|
90
|
+
export async function detectLightTerminalBackground(
|
|
91
|
+
env: Record<string, string | undefined> = process.env,
|
|
92
|
+
): Promise<boolean> {
|
|
93
|
+
const explicit = readLightTerminalOverride(env);
|
|
94
|
+
if (explicit !== undefined) return explicit;
|
|
95
|
+
if (terminalPrefersLightBackground(env)) return true;
|
|
96
|
+
|
|
97
|
+
const osc = await queryOsc11BackgroundLight();
|
|
98
|
+
if (osc !== undefined) return osc;
|
|
99
|
+
|
|
100
|
+
// Cursor / IDE terminals often omit COLORFGBG and ignore OSC on the alt screen.
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Env for `resolveTheme` after TUI detection (respects explicit overrides). */
|
|
105
|
+
export async function resolveTuiThemeEnv(
|
|
106
|
+
env: Record<string, string | undefined> = process.env,
|
|
107
|
+
): Promise<Record<string, string | undefined>> {
|
|
108
|
+
const out = { ...env };
|
|
109
|
+
if (readLightTerminalOverride(out) !== undefined) return out;
|
|
110
|
+
const light = await detectLightTerminalBackground(out);
|
|
111
|
+
out.CAELENCE_LIGHT_TERMINAL = light ? "1" : "0";
|
|
112
|
+
return out;
|
|
113
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** Tail slice so long input stays on one line (ASCII prompt, no caret glyph). */
|
|
2
|
+
export function composerVisibleValue(value: string, width: number, promptLength = 2): string {
|
|
3
|
+
const max = Math.max(1, width - promptLength);
|
|
4
|
+
if (value.length <= max) return value;
|
|
5
|
+
return value.slice(value.length - max);
|
|
6
|
+
}
|
|
@@ -8,7 +8,10 @@ describe("tui text input", () => {
|
|
|
8
8
|
value: "ab",
|
|
9
9
|
cursor: 2,
|
|
10
10
|
});
|
|
11
|
-
expect(applyTextInputKey("abc",
|
|
11
|
+
expect(applyTextInputKey("abc", 3, "", { delete: true })).toEqual({
|
|
12
|
+
value: "ab",
|
|
13
|
+
cursor: 2,
|
|
14
|
+
});
|
|
12
15
|
expect(applyTextInputKey("abc", 3, "", { leftArrow: true })).toEqual({
|
|
13
16
|
value: "abc",
|
|
14
17
|
cursor: 2,
|
|
@@ -20,4 +23,13 @@ describe("tui text input", () => {
|
|
|
20
23
|
});
|
|
21
24
|
expect(applyTextInputKey("hi", 2, "", { upArrow: true })).toBeUndefined();
|
|
22
25
|
});
|
|
26
|
+
|
|
27
|
+
test("treats terminal delete bytes as backspace", () => {
|
|
28
|
+
expect(applyTextInputKey("abc", 3, "\x7f", {})).toEqual({ value: "ab", cursor: 2 });
|
|
29
|
+
expect(applyTextInputKey("abc", 2, "\b", {})).toEqual({ value: "ac", cursor: 1 });
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test("flattens pasted newlines to spaces", () => {
|
|
33
|
+
expect(applyTextInputKey("a", 1, "b\nc", {})).toEqual({ value: "ab c", cursor: 4 });
|
|
34
|
+
});
|
|
23
35
|
});
|
|
@@ -12,6 +12,11 @@ export interface TextInputKey {
|
|
|
12
12
|
meta?: boolean;
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
+
function normalizePaste(input: string): string {
|
|
16
|
+
if (!input.includes("\n") && !input.includes("\r")) return input;
|
|
17
|
+
return input.replace(/\r\n|\r|\n/g, " ");
|
|
18
|
+
}
|
|
19
|
+
|
|
15
20
|
export function applyTextInputKey(
|
|
16
21
|
value: string,
|
|
17
22
|
cursor: number,
|
|
@@ -19,22 +24,21 @@ export function applyTextInputKey(
|
|
|
19
24
|
key: TextInputKey,
|
|
20
25
|
): { value: string; cursor: number; submit?: boolean } | undefined {
|
|
21
26
|
if (key.return) return { value, cursor, submit: true };
|
|
27
|
+
// Ink maps Mac backspace (\\x7f) to key.delete; treat both as delete-left.
|
|
28
|
+
if (key.backspace || key.delete || input === "\x7f" || input === "\b") {
|
|
29
|
+
if (cursor === 0) return { value, cursor };
|
|
30
|
+
return { value: value.slice(0, cursor - 1) + value.slice(cursor), cursor: cursor - 1 };
|
|
31
|
+
}
|
|
22
32
|
if (key.escape || key.tab || key.upArrow || key.downArrow || key.ctrl || key.meta) {
|
|
23
33
|
return undefined;
|
|
24
34
|
}
|
|
25
35
|
if (key.leftArrow) return { value, cursor: Math.max(0, cursor - 1) };
|
|
26
36
|
if (key.rightArrow) return { value, cursor: Math.min(value.length, cursor + 1) };
|
|
27
|
-
if (key.backspace) {
|
|
28
|
-
if (cursor === 0) return { value, cursor };
|
|
29
|
-
return { value: value.slice(0, cursor - 1) + value.slice(cursor), cursor: cursor - 1 };
|
|
30
|
-
}
|
|
31
|
-
if (key.delete) {
|
|
32
|
-
if (cursor >= value.length) return { value, cursor };
|
|
33
|
-
return { value: value.slice(0, cursor) + value.slice(cursor + 1), cursor };
|
|
34
|
-
}
|
|
35
37
|
if (!input) return undefined;
|
|
38
|
+
const text = normalizePaste(input);
|
|
39
|
+
if (!text) return undefined;
|
|
36
40
|
return {
|
|
37
|
-
value: value.slice(0, cursor) +
|
|
38
|
-
cursor: cursor +
|
|
41
|
+
value: value.slice(0, cursor) + text + value.slice(cursor),
|
|
42
|
+
cursor: cursor + text.length,
|
|
39
43
|
};
|
|
40
44
|
}
|
|
@@ -2,37 +2,66 @@ import { Text, useInput } from "ink";
|
|
|
2
2
|
import type React from "react";
|
|
3
3
|
import { useEffect, useState } from "react";
|
|
4
4
|
import { applyTextInputKey } from "./tui-text-input.ts";
|
|
5
|
+
import { composerVisibleValue } from "./tui-text-input-display.ts";
|
|
6
|
+
|
|
7
|
+
const PROMPT = "> ";
|
|
5
8
|
|
|
6
9
|
export function TuiTextInput(
|
|
7
10
|
props: Readonly<{
|
|
8
11
|
value: string;
|
|
12
|
+
width: number;
|
|
13
|
+
enabled?: boolean;
|
|
14
|
+
slashOpen?: boolean;
|
|
15
|
+
onSlashUp?: () => void;
|
|
16
|
+
onSlashDown?: () => void;
|
|
17
|
+
onSlashTab?: () => void;
|
|
9
18
|
onChange: (value: string) => void;
|
|
10
19
|
onSubmit: (value: string) => void;
|
|
11
20
|
}>,
|
|
12
21
|
): React.ReactElement {
|
|
22
|
+
const enabled = props.enabled !== false;
|
|
23
|
+
const [draft, setDraft] = useState(props.value);
|
|
13
24
|
const [cursor, setCursor] = useState(props.value.length);
|
|
14
25
|
|
|
15
26
|
useEffect(() => {
|
|
16
|
-
|
|
27
|
+
setDraft(props.value);
|
|
28
|
+
setCursor(props.value.length);
|
|
17
29
|
}, [props.value]);
|
|
18
30
|
|
|
19
|
-
useInput(
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
31
|
+
useInput(
|
|
32
|
+
(input, key) => {
|
|
33
|
+
if (props.slashOpen) {
|
|
34
|
+
if (key.upArrow) {
|
|
35
|
+
props.onSlashUp?.();
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
if (key.downArrow) {
|
|
39
|
+
props.onSlashDown?.();
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
if (key.tab) {
|
|
43
|
+
props.onSlashTab?.();
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
const next = applyTextInputKey(draft, cursor, input, key);
|
|
48
|
+
if (!next) return;
|
|
49
|
+
if (next.submit) {
|
|
50
|
+
props.onSubmit(next.value);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
setDraft(next.value);
|
|
54
|
+
setCursor(next.cursor);
|
|
55
|
+
if (next.value !== props.value) props.onChange(next.value);
|
|
56
|
+
},
|
|
57
|
+
{ isActive: enabled },
|
|
58
|
+
);
|
|
29
59
|
|
|
30
|
-
const
|
|
60
|
+
const visible = composerVisibleValue(draft, props.width, PROMPT.length);
|
|
31
61
|
return (
|
|
32
|
-
<Text>
|
|
33
|
-
{
|
|
34
|
-
|
|
35
|
-
{atEnd ? null : props.value.slice(cursor + 1)}
|
|
62
|
+
<Text wrap="truncate">
|
|
63
|
+
{PROMPT}
|
|
64
|
+
{visible}
|
|
36
65
|
</Text>
|
|
37
66
|
);
|
|
38
67
|
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import type { StreamLine } from "./tui-stream.ts";
|
|
3
|
+
import {
|
|
4
|
+
estimateStreamLineRows,
|
|
5
|
+
summarizeTurnTools,
|
|
6
|
+
takeLinesForRowBudget,
|
|
7
|
+
} from "./tui-transcript.ts";
|
|
8
|
+
|
|
9
|
+
describe("tui transcript layout", () => {
|
|
10
|
+
test("summarizes tool names for a completed turn", () => {
|
|
11
|
+
expect(
|
|
12
|
+
summarizeTurnTools([
|
|
13
|
+
{ name: "read_file", status: "ok" },
|
|
14
|
+
{ name: "read_file", status: "ok" },
|
|
15
|
+
{ name: "exec", status: "fail" },
|
|
16
|
+
]),
|
|
17
|
+
).toBe("Tools: read_file×2, exec");
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
test("keeps newest lines within a wrapped row budget", () => {
|
|
21
|
+
const lines: StreamLine[] = [
|
|
22
|
+
{ key: "1", type: "user", text: "first question with a longer prompt" },
|
|
23
|
+
{ key: "2", type: "assistant", text: "first answer" },
|
|
24
|
+
{ key: "3", type: "user", text: "second question" },
|
|
25
|
+
{ key: "4", type: "assistant", text: "second answer that should stay visible" },
|
|
26
|
+
];
|
|
27
|
+
const width = 24;
|
|
28
|
+
const budget =
|
|
29
|
+
estimateStreamLineRows(lines[3]!, width) + estimateStreamLineRows(lines[2]!, width);
|
|
30
|
+
const shown = takeLinesForRowBudget(lines, width, budget);
|
|
31
|
+
expect(shown.map((line) => line.key)).toEqual(["3", "4"]);
|
|
32
|
+
});
|
|
33
|
+
});
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { StreamLine } from "./tui-stream.ts";
|
|
2
|
+
|
|
3
|
+
export function estimateWrappedRows(text: string, width: number): number {
|
|
4
|
+
const cols = Math.max(1, width);
|
|
5
|
+
const parts = text.split("\n");
|
|
6
|
+
let rows = 0;
|
|
7
|
+
for (const part of parts) {
|
|
8
|
+
const len = part.length;
|
|
9
|
+
rows += len === 0 ? 1 : Math.ceil(len / cols);
|
|
10
|
+
}
|
|
11
|
+
return Math.max(1, rows);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function estimateStreamLineRows(line: StreamLine, width: number): number {
|
|
15
|
+
const labelRows = 1;
|
|
16
|
+
switch (line.type) {
|
|
17
|
+
case "user":
|
|
18
|
+
case "assistant":
|
|
19
|
+
case "thought":
|
|
20
|
+
return labelRows + estimateWrappedRows(line.text, width);
|
|
21
|
+
case "tool":
|
|
22
|
+
return line.error ? 2 : 1;
|
|
23
|
+
case "system":
|
|
24
|
+
case "error":
|
|
25
|
+
case "todos":
|
|
26
|
+
return estimateWrappedRows(line.text, width);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Keep the newest transcript lines that fit the row budget (wrapped height). */
|
|
31
|
+
export function takeLinesForRowBudget(
|
|
32
|
+
lines: readonly StreamLine[],
|
|
33
|
+
width: number,
|
|
34
|
+
maxRows: number,
|
|
35
|
+
): StreamLine[] {
|
|
36
|
+
if (maxRows <= 0 || lines.length === 0) return [];
|
|
37
|
+
const picked: StreamLine[] = [];
|
|
38
|
+
let used = 0;
|
|
39
|
+
for (let i = lines.length - 1; i >= 0; i--) {
|
|
40
|
+
const line = lines[i]!;
|
|
41
|
+
const need = estimateStreamLineRows(line, width);
|
|
42
|
+
if (used + need > maxRows && picked.length > 0) break;
|
|
43
|
+
picked.unshift(line);
|
|
44
|
+
used += need;
|
|
45
|
+
}
|
|
46
|
+
return picked;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function summarizeTurnTools(
|
|
50
|
+
tools: ReadonlyArray<{ name: string; status: "running" | "ok" | "fail" }>,
|
|
51
|
+
): string {
|
|
52
|
+
if (tools.length === 0) return "";
|
|
53
|
+
const counts = new Map<string, number>();
|
|
54
|
+
for (const tool of tools) {
|
|
55
|
+
counts.set(tool.name, (counts.get(tool.name) ?? 0) + 1);
|
|
56
|
+
}
|
|
57
|
+
const parts = [...counts.entries()].map(([name, n]) => (n > 1 ? `${name}×${n}` : name));
|
|
58
|
+
return `Tools: ${parts.join(", ")}`;
|
|
59
|
+
}
|