@useavalon/caelence-agent 0.2.4 → 0.2.5-canary.1
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/scripts/run-tauri.ts +3 -2
- package/desktop/src/DeskChrome.tsx +18 -1
- package/desktop/src/app.css +32 -0
- package/package.json +1 -1
- package/src/agent-cwd.ts +51 -4
- package/src/cli/agent-cwd.test.ts +17 -0
- package/src/cli/main.ts +10 -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-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 +267 -157
- package/src/desktop/bridge.ts +5 -1
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { spawn } from "node:child_process";
|
|
2
2
|
import { existsSync } from "node:fs";
|
|
3
3
|
import { resolve } from "node:path";
|
|
4
|
+
import { resolveAgentCwd } from "../../src/agent-cwd.ts";
|
|
4
5
|
|
|
5
6
|
const desktopDir = resolve(import.meta.dir, "..");
|
|
6
7
|
const cargoHome = resolve(desktopDir, ".toolchain/cargo");
|
|
@@ -13,8 +14,8 @@ if (existsSync(resolve(cargoBin, "cargo"))) {
|
|
|
13
14
|
env.RUSTUP_HOME = rustupHome;
|
|
14
15
|
env.PATH = `${cargoBin}:${env.PATH ?? ""}`;
|
|
15
16
|
}
|
|
16
|
-
if (!env.HARNESS_CWD) {
|
|
17
|
-
env.HARNESS_CWD =
|
|
17
|
+
if (!env.HARNESS_CWD?.trim()) {
|
|
18
|
+
env.HARNESS_CWD = resolveAgentCwd({ start: process.cwd() });
|
|
18
19
|
}
|
|
19
20
|
|
|
20
21
|
function run(bin: string, args: string[]): Promise<number> {
|
|
@@ -293,7 +293,24 @@ export function DeskMain(
|
|
|
293
293
|
<header className="desk-bar">
|
|
294
294
|
<div className="desk-id">
|
|
295
295
|
<h1>{mainViewTitle(view)}</h1>
|
|
296
|
-
{view === "chat"
|
|
296
|
+
{view === "chat" ? (
|
|
297
|
+
<p className="desk-project" title={desk.state.cwd}>
|
|
298
|
+
<button
|
|
299
|
+
type="button"
|
|
300
|
+
className="desk-project-btn"
|
|
301
|
+
onClick={() => {
|
|
302
|
+
void openLocalFile(desk.state.cwd, true, desk.bridge).catch((err: unknown) => {
|
|
303
|
+
desk.noticeError(err);
|
|
304
|
+
});
|
|
305
|
+
}}
|
|
306
|
+
>
|
|
307
|
+
{homePath(desk.state.cwd)}
|
|
308
|
+
</button>
|
|
309
|
+
{desk.state.spend ? (
|
|
310
|
+
<span className="desk-project-spend">{desk.state.spend}</span>
|
|
311
|
+
) : null}
|
|
312
|
+
</p>
|
|
313
|
+
) : null}
|
|
297
314
|
</div>
|
|
298
315
|
</header>
|
|
299
316
|
<div className="desk-stage">
|
package/desktop/src/app.css
CHANGED
|
@@ -505,6 +505,38 @@ body {
|
|
|
505
505
|
white-space: nowrap;
|
|
506
506
|
}
|
|
507
507
|
|
|
508
|
+
.desk-project {
|
|
509
|
+
display: flex;
|
|
510
|
+
align-items: center;
|
|
511
|
+
gap: 8px;
|
|
512
|
+
min-width: 0;
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
.desk-project-btn {
|
|
516
|
+
margin: 0;
|
|
517
|
+
padding: 0;
|
|
518
|
+
border: 0;
|
|
519
|
+
background: transparent;
|
|
520
|
+
font: inherit;
|
|
521
|
+
color: inherit;
|
|
522
|
+
cursor: pointer;
|
|
523
|
+
overflow: hidden;
|
|
524
|
+
text-overflow: ellipsis;
|
|
525
|
+
white-space: nowrap;
|
|
526
|
+
min-width: 0;
|
|
527
|
+
flex: 1;
|
|
528
|
+
text-align: left;
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
.desk-project-btn:hover {
|
|
532
|
+
color: var(--text);
|
|
533
|
+
text-decoration: underline;
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
.desk-project-spend {
|
|
537
|
+
flex-shrink: 0;
|
|
538
|
+
}
|
|
539
|
+
|
|
508
540
|
.desk-bar .cel-chip {
|
|
509
541
|
padding: 3px 8px;
|
|
510
542
|
gap: 6px;
|
package/package.json
CHANGED
package/src/agent-cwd.ts
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
|
-
import { existsSync, mkdirSync } from "node:fs";
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
2
|
import { homedir } from "node:os";
|
|
3
3
|
import { dirname, resolve } from "node:path";
|
|
4
4
|
import { resolveConfigPath } from "./config.ts";
|
|
5
5
|
import { harnessHome } from "./desktop/secrets.ts";
|
|
6
6
|
|
|
7
|
+
export function lastProjectCwdPath(env: Record<string, string | undefined> = process.env): string {
|
|
8
|
+
return resolve(harnessHome(env), "last-project-cwd");
|
|
9
|
+
}
|
|
10
|
+
|
|
7
11
|
export function standaloneWorkspaceDir(
|
|
8
12
|
env: Record<string, string | undefined> = process.env,
|
|
9
13
|
): string {
|
|
@@ -23,6 +27,38 @@ function isProjectRoot(dir: string): boolean {
|
|
|
23
27
|
return existsSync(resolve(dir, ".git"));
|
|
24
28
|
}
|
|
25
29
|
|
|
30
|
+
function readLastProjectCwd(
|
|
31
|
+
env: Record<string, string | undefined> = process.env,
|
|
32
|
+
): string | undefined {
|
|
33
|
+
const path = lastProjectCwdPath(env);
|
|
34
|
+
if (!existsSync(path)) return undefined;
|
|
35
|
+
try {
|
|
36
|
+
const line = readFileSync(path, "utf8").trim();
|
|
37
|
+
if (!line) return undefined;
|
|
38
|
+
const dir = resolve(line);
|
|
39
|
+
if (!existsSync(dir) || !isProjectRoot(dir)) return undefined;
|
|
40
|
+
return dir;
|
|
41
|
+
} catch {
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Remember a git repo or harness.config root for the next standalone launch. */
|
|
47
|
+
export function rememberProjectCwd(
|
|
48
|
+
cwd: string,
|
|
49
|
+
env: Record<string, string | undefined> = process.env,
|
|
50
|
+
): void {
|
|
51
|
+
const dir = resolve(cwd);
|
|
52
|
+
if (dir === resolve(standaloneWorkspaceDir(env))) return;
|
|
53
|
+
if (!isProjectRoot(dir)) return;
|
|
54
|
+
try {
|
|
55
|
+
mkdirSync(harnessHome(env), { recursive: true });
|
|
56
|
+
writeFileSync(lastProjectCwdPath(env), `${dir}\n`, "utf8");
|
|
57
|
+
} catch {
|
|
58
|
+
// best effort
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
26
62
|
/** Nearest git repo or harness.config, not the home directory. */
|
|
27
63
|
export function findProjectRoot(
|
|
28
64
|
start: string,
|
|
@@ -38,7 +74,7 @@ export function findProjectRoot(
|
|
|
38
74
|
}
|
|
39
75
|
}
|
|
40
76
|
|
|
41
|
-
/** `--cwd` wins. Else a project root from `start`. Else `~/.harness/workspace`. */
|
|
77
|
+
/** `--cwd` wins. Else a project root from `start`. Else last project, then `~/.harness/workspace`. */
|
|
42
78
|
export function resolveAgentCwd(input: {
|
|
43
79
|
start: string;
|
|
44
80
|
explicit?: string;
|
|
@@ -46,6 +82,17 @@ export function resolveAgentCwd(input: {
|
|
|
46
82
|
}): string {
|
|
47
83
|
const env = input.env ?? process.env;
|
|
48
84
|
const explicit = input.explicit?.trim();
|
|
49
|
-
if (explicit)
|
|
50
|
-
|
|
85
|
+
if (explicit) {
|
|
86
|
+
const dir = resolve(explicit);
|
|
87
|
+
rememberProjectCwd(dir, env);
|
|
88
|
+
return dir;
|
|
89
|
+
}
|
|
90
|
+
const found = findProjectRoot(input.start, env);
|
|
91
|
+
if (found) {
|
|
92
|
+
rememberProjectCwd(found, env);
|
|
93
|
+
return found;
|
|
94
|
+
}
|
|
95
|
+
const last = readLastProjectCwd(env);
|
|
96
|
+
if (last) return last;
|
|
97
|
+
return ensureStandaloneWorkspace(env);
|
|
51
98
|
}
|
|
@@ -48,6 +48,23 @@ describe("resolveAgentCwd", () => {
|
|
|
48
48
|
}
|
|
49
49
|
});
|
|
50
50
|
|
|
51
|
+
test("falls back to the last project cwd before standalone workspace", async () => {
|
|
52
|
+
const last = await tempDir("agent-cwd-last-");
|
|
53
|
+
const harnessHome = await tempDir("agent-cwd-last-harness-");
|
|
54
|
+
const scratch = await tempDir("agent-cwd-last-scratch-");
|
|
55
|
+
try {
|
|
56
|
+
await mkdir(join(last, ".git"));
|
|
57
|
+
const env = { HOME: scratch, HARNESS_HOME: harnessHome };
|
|
58
|
+
const { rememberProjectCwd } = await import("../agent-cwd.ts");
|
|
59
|
+
rememberProjectCwd(last, env);
|
|
60
|
+
expect(resolveAgentCwd({ start: scratch, env })).toBe(last);
|
|
61
|
+
} finally {
|
|
62
|
+
await rm(last, { recursive: true, force: true });
|
|
63
|
+
await rm(harnessHome, { recursive: true, force: true });
|
|
64
|
+
await rm(scratch, { recursive: true, force: true });
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
|
|
51
68
|
test("home is not a project even with .git", async () => {
|
|
52
69
|
const home = await tempDir("agent-cwd-home-");
|
|
53
70
|
const harnessHome = await tempDir("agent-cwd-home-harness-");
|
package/src/cli/main.ts
CHANGED
|
@@ -114,13 +114,18 @@ function printEvent(event: AgentEvent): void {
|
|
|
114
114
|
}
|
|
115
115
|
}
|
|
116
116
|
|
|
117
|
-
function desktopToolchainEnv(
|
|
117
|
+
function desktopToolchainEnv(
|
|
118
|
+
desktopDir: string,
|
|
119
|
+
cwd: string,
|
|
120
|
+
cwdExplicit: boolean,
|
|
121
|
+
): NodeJS.ProcessEnv {
|
|
118
122
|
const cargoHome = resolve(desktopDir, ".toolchain/cargo");
|
|
119
123
|
const rustupHome = resolve(desktopDir, ".toolchain/rustup");
|
|
120
124
|
const cargoBin = resolve(cargoHome, "bin");
|
|
121
125
|
const bunDir = dirname(process.execPath);
|
|
122
126
|
const systemCargoBin = resolve(homedir(), ".cargo/bin");
|
|
123
127
|
const env: NodeJS.ProcessEnv = { ...process.env, HARNESS_CWD: cwd };
|
|
128
|
+
if (cwdExplicit) env.HARNESS_CWD_EXPLICIT = cwd;
|
|
124
129
|
const pathDirs = [bunDir];
|
|
125
130
|
if (existsSync(resolve(cargoBin, "cargo"))) {
|
|
126
131
|
env.CARGO_HOME = cargoHome;
|
|
@@ -195,7 +200,8 @@ function agentCwd(parsed: ParsedCli): string {
|
|
|
195
200
|
});
|
|
196
201
|
}
|
|
197
202
|
|
|
198
|
-
async function runDesktop(
|
|
203
|
+
async function runDesktop(parsed: ParsedCli): Promise<number> {
|
|
204
|
+
const cwd = agentCwd(parsed);
|
|
199
205
|
const desktopDir = resolve(fileURLToPath(new URL("../../desktop", import.meta.url)));
|
|
200
206
|
if (!existsSync(resolve(desktopDir, "package.json"))) {
|
|
201
207
|
process.stderr.write(
|
|
@@ -203,7 +209,7 @@ async function runDesktop(cwd: string): Promise<number> {
|
|
|
203
209
|
);
|
|
204
210
|
return 1;
|
|
205
211
|
}
|
|
206
|
-
const env = desktopToolchainEnv(desktopDir, cwd);
|
|
212
|
+
const env = desktopToolchainEnv(desktopDir, cwd, parsed.cwdExplicit);
|
|
207
213
|
if (!hasCargo(env)) {
|
|
208
214
|
process.stderr.write(
|
|
209
215
|
"Rust is required for the desktop window. Install rustup from https://rustup.rs then run caelence desktop again.\n",
|
|
@@ -430,7 +436,7 @@ const COMMANDS: Record<string, (parsed: ParsedCli) => Promise<number>> = {
|
|
|
430
436
|
printHelp();
|
|
431
437
|
return 0;
|
|
432
438
|
},
|
|
433
|
-
desktop: async (parsed) => runDesktop(
|
|
439
|
+
desktop: async (parsed) => runDesktop(parsed),
|
|
434
440
|
init: async (parsed) => runInit(parsed),
|
|
435
441
|
skill: async (parsed) => runSkill(parsed.cwd, parsed.rest),
|
|
436
442
|
eval: async (parsed) => runEval(parsed.cwd, parsed.rest),
|
|
@@ -14,7 +14,7 @@ describe("tui layout", () => {
|
|
|
14
14
|
});
|
|
15
15
|
|
|
16
16
|
test("keeps a minimum transcript viewport", () => {
|
|
17
|
-
expect(visibleTranscriptCount(12, 20)).toBe(
|
|
17
|
+
expect(visibleTranscriptCount(12, 20)).toBe(4);
|
|
18
18
|
expect(visibleTranscriptCount(40, 10)).toBe(30);
|
|
19
19
|
});
|
|
20
20
|
|
|
@@ -27,7 +27,9 @@ describe("tui layout", () => {
|
|
|
27
27
|
});
|
|
28
28
|
|
|
29
29
|
test("counts chrome for approval and pickers", () => {
|
|
30
|
-
expect(tuiChromeRows({ approval: false, pickerCount: 0, slashCount: 0 })).toBe(
|
|
31
|
-
expect(tuiChromeRows({ approval: true, pickerCount: 3, slashCount: 0 })).toBe(
|
|
30
|
+
expect(tuiChromeRows({ approval: false, pickerCount: 0, slashCount: 0 })).toBe(4);
|
|
31
|
+
expect(tuiChromeRows({ approval: true, pickerCount: 3, slashCount: 0, activity: true })).toBe(
|
|
32
|
+
15,
|
|
33
|
+
);
|
|
32
34
|
});
|
|
33
35
|
});
|
package/src/cli/tui-layout.ts
CHANGED
|
@@ -2,8 +2,17 @@ export function sidebarWidth(columns: number): number {
|
|
|
2
2
|
return columns >= 88 ? 26 : 0;
|
|
3
3
|
}
|
|
4
4
|
|
|
5
|
+
export const TUI_HEADER_ROWS = 2;
|
|
6
|
+
export const TUI_COMPOSER_ROWS = 2;
|
|
7
|
+
export const TUI_ACTIVITY_ROWS = 1;
|
|
8
|
+
|
|
9
|
+
export function visibleTranscriptRows(rows: number, chromeRows: number): number {
|
|
10
|
+
return Math.max(4, rows - Math.max(0, chromeRows));
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** @deprecated Line count; prefer visibleTranscriptRows with row-budget clipping. */
|
|
5
14
|
export function visibleTranscriptCount(rows: number, chromeRows: number): number {
|
|
6
|
-
return
|
|
15
|
+
return visibleTranscriptRows(rows, chromeRows);
|
|
7
16
|
}
|
|
8
17
|
|
|
9
18
|
export function takeVisibleLines<T>(lines: T[], count: number): T[] {
|
|
@@ -22,12 +31,12 @@ export function tuiChromeRows(input: {
|
|
|
22
31
|
approval: boolean;
|
|
23
32
|
pickerCount: number;
|
|
24
33
|
slashCount: number;
|
|
34
|
+
activity?: boolean;
|
|
25
35
|
}): number {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
);
|
|
36
|
+
let rows = TUI_HEADER_ROWS + TUI_COMPOSER_ROWS;
|
|
37
|
+
if (input.activity) rows += TUI_ACTIVITY_ROWS;
|
|
38
|
+
if (input.approval) rows += 5;
|
|
39
|
+
if (input.pickerCount > 0) rows += Math.min(input.pickerCount, 8) + 2;
|
|
40
|
+
if (input.slashCount > 0) rows += Math.min(input.slashCount, 8) + 1;
|
|
41
|
+
return rows;
|
|
33
42
|
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { turnProgressLabel, warmupHeadline } from "./tui-progress.ts";
|
|
3
|
+
|
|
4
|
+
describe("tui progress", () => {
|
|
5
|
+
test("rotates warmup headlines", () => {
|
|
6
|
+
expect(warmupHeadline(0)).toBe("Thinking");
|
|
7
|
+
expect(warmupHeadline(1)).toBe("Looking this over");
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
test("shows warmup before tools or text", () => {
|
|
11
|
+
expect(turnProgressLabel({ tools: [], assistantStarted: false, tick: 0, warmupSeed: 0 })).toBe(
|
|
12
|
+
"Thinking",
|
|
13
|
+
);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
test("shows tool activity while running", () => {
|
|
17
|
+
expect(
|
|
18
|
+
turnProgressLabel({
|
|
19
|
+
tools: [
|
|
20
|
+
{
|
|
21
|
+
callId: "1",
|
|
22
|
+
name: "read_file",
|
|
23
|
+
status: "running",
|
|
24
|
+
preview: "src/index.ts",
|
|
25
|
+
},
|
|
26
|
+
],
|
|
27
|
+
assistantStarted: false,
|
|
28
|
+
tick: 1,
|
|
29
|
+
warmupSeed: 0,
|
|
30
|
+
}),
|
|
31
|
+
).toBe("Reading src/index.ts.");
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test("shows writing once assistant text starts", () => {
|
|
35
|
+
expect(
|
|
36
|
+
turnProgressLabel({
|
|
37
|
+
tools: [],
|
|
38
|
+
assistantStarted: true,
|
|
39
|
+
tick: 2,
|
|
40
|
+
warmupSeed: 0,
|
|
41
|
+
}),
|
|
42
|
+
).toBe("Writing..");
|
|
43
|
+
});
|
|
44
|
+
});
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { clipLabel } from "./tui-layout.ts";
|
|
2
|
+
import type { TurnToolLine } from "./tui-stream.ts";
|
|
3
|
+
|
|
4
|
+
export const WARMUP_PHRASES = ["Thinking", "Looking this over", "Working it out"] as const;
|
|
5
|
+
|
|
6
|
+
const TICK_FRAMES = ["", ".", "..", "..."] as const;
|
|
7
|
+
|
|
8
|
+
export function warmupHeadline(seed: number): string {
|
|
9
|
+
return WARMUP_PHRASES[Math.abs(seed) % WARMUP_PHRASES.length] ?? "Thinking";
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const TOOL_ACTIVITY_RULES: ReadonlyArray<{
|
|
13
|
+
match: RegExp;
|
|
14
|
+
verb: string;
|
|
15
|
+
previewWidth: number;
|
|
16
|
+
}> = [
|
|
17
|
+
{ match: /read_file|read/i, verb: "Reading", previewWidth: 48 },
|
|
18
|
+
{ match: /grep|glob|search/i, verb: "Searching", previewWidth: 40 },
|
|
19
|
+
{ match: /write_file|edit_file|str_replace|apply_patch/i, verb: "Editing", previewWidth: 48 },
|
|
20
|
+
{ match: /exec|shell|bash/i, verb: "Running", previewWidth: 40 },
|
|
21
|
+
{ match: /git_/i, verb: "Git", previewWidth: 40 },
|
|
22
|
+
];
|
|
23
|
+
|
|
24
|
+
function labelWithPreview(verb: string, preview: string, width: number): string {
|
|
25
|
+
return preview ? `${verb} ${clipLabel(preview, width)}` : verb;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function toolActivityLabel(tool: TurnToolLine): string {
|
|
29
|
+
const preview = tool.preview.trim();
|
|
30
|
+
const rule = TOOL_ACTIVITY_RULES.find((entry) => entry.match.test(tool.name));
|
|
31
|
+
if (rule) return labelWithPreview(rule.verb, preview, rule.previewWidth);
|
|
32
|
+
return preview ? `${tool.name} ${clipLabel(preview, 40)}` : tool.name;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function turnProgressLabel(input: {
|
|
36
|
+
tools: readonly TurnToolLine[];
|
|
37
|
+
assistantStarted: boolean;
|
|
38
|
+
tick: number;
|
|
39
|
+
warmupSeed: number;
|
|
40
|
+
}): string {
|
|
41
|
+
if (input.assistantStarted) {
|
|
42
|
+
const frame = TICK_FRAMES[input.tick % TICK_FRAMES.length] ?? "";
|
|
43
|
+
return `Writing${frame}`;
|
|
44
|
+
}
|
|
45
|
+
const running = [...input.tools].reverse().find((tool) => tool.status === "running");
|
|
46
|
+
if (running) {
|
|
47
|
+
const frame = TICK_FRAMES[input.tick % TICK_FRAMES.length] ?? "";
|
|
48
|
+
return `${toolActivityLabel(running)}${frame}`;
|
|
49
|
+
}
|
|
50
|
+
if (input.tools.length > 0) {
|
|
51
|
+
return `${warmupHeadline(input.warmupSeed)}…`;
|
|
52
|
+
}
|
|
53
|
+
const frame = TICK_FRAMES[input.tick % TICK_FRAMES.length] ?? "";
|
|
54
|
+
return `${warmupHeadline(input.warmupSeed)}${frame}`;
|
|
55
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export type ToolStatus = "running" | "ok" | "fail";
|
|
2
|
+
|
|
3
|
+
export type StreamLine =
|
|
4
|
+
| { key: string; type: "user"; text: string }
|
|
5
|
+
| { key: string; type: "assistant"; text: string }
|
|
6
|
+
| { key: string; type: "thought"; text: string }
|
|
7
|
+
| {
|
|
8
|
+
key: string;
|
|
9
|
+
type: "tool";
|
|
10
|
+
name: string;
|
|
11
|
+
callId: string;
|
|
12
|
+
status: ToolStatus;
|
|
13
|
+
preview: string;
|
|
14
|
+
error?: string;
|
|
15
|
+
}
|
|
16
|
+
| { key: string; type: "system"; text: string }
|
|
17
|
+
| { key: string; type: "error"; text: string }
|
|
18
|
+
| { key: string; type: "todos"; text: string };
|
|
19
|
+
|
|
20
|
+
export type TurnToolLine = {
|
|
21
|
+
callId: string;
|
|
22
|
+
name: string;
|
|
23
|
+
status: ToolStatus;
|
|
24
|
+
preview: string;
|
|
25
|
+
error?: string;
|
|
26
|
+
};
|
|
@@ -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
|
+
}
|
package/src/cli/tui.tsx
CHANGED
|
@@ -33,33 +33,12 @@ import {
|
|
|
33
33
|
} from "./slash.ts";
|
|
34
34
|
import { applyPickerChoice, dispatchSlash, type SlashPickerKind } from "./slash-dispatch.ts";
|
|
35
35
|
import type { Theme } from "./theme.ts";
|
|
36
|
-
import {
|
|
37
|
-
|
|
38
|
-
sidebarWidth,
|
|
39
|
-
takeVisibleLines,
|
|
40
|
-
tuiChromeRows,
|
|
41
|
-
visibleTranscriptCount,
|
|
42
|
-
} from "./tui-layout.ts";
|
|
36
|
+
import { clipLabel, sidebarWidth, tuiChromeRows, visibleTranscriptRows } from "./tui-layout.ts";
|
|
37
|
+
import { turnProgressLabel } from "./tui-progress.ts";
|
|
43
38
|
import { enterAltScreen } from "./tui-screen.ts";
|
|
39
|
+
import type { StreamLine, ToolStatus, TurnToolLine } from "./tui-stream.ts";
|
|
44
40
|
import { TuiTextInput } from "./tui-text-input.tsx";
|
|
45
|
-
|
|
46
|
-
type ToolStatus = "running" | "ok" | "fail";
|
|
47
|
-
|
|
48
|
-
type StreamLine =
|
|
49
|
-
| { key: string; type: "user"; text: string }
|
|
50
|
-
| { key: string; type: "assistant"; text: string }
|
|
51
|
-
| {
|
|
52
|
-
key: string;
|
|
53
|
-
type: "tool";
|
|
54
|
-
name: string;
|
|
55
|
-
callId: string;
|
|
56
|
-
status: ToolStatus;
|
|
57
|
-
preview: string;
|
|
58
|
-
error?: string;
|
|
59
|
-
}
|
|
60
|
-
| { key: string; type: "system"; text: string }
|
|
61
|
-
| { key: string; type: "error"; text: string }
|
|
62
|
-
| { key: string; type: "todos"; text: string };
|
|
41
|
+
import { summarizeTurnTools, takeLinesForRowBudget } from "./tui-transcript.ts";
|
|
63
42
|
|
|
64
43
|
type UiStatus = "idle" | "running" | "approval" | "picker";
|
|
65
44
|
|
|
@@ -89,30 +68,16 @@ function applyEvent(lines: StreamLine[], event: AgentEvent): StreamLine[] {
|
|
|
89
68
|
}
|
|
90
69
|
return [...lines, { key: `a-${lines.length}`, type: "assistant", text: event.text }];
|
|
91
70
|
}
|
|
92
|
-
case "reasoning_delta":
|
|
93
|
-
|
|
71
|
+
case "reasoning_delta": {
|
|
72
|
+
const last = lines.at(-1);
|
|
73
|
+
if (last?.type === "thought") {
|
|
74
|
+
return [...lines.slice(0, -1), { ...last, text: last.text + event.text }];
|
|
75
|
+
}
|
|
76
|
+
return [...lines, { key: `th-${lines.length}`, type: "thought", text: event.text }];
|
|
77
|
+
}
|
|
94
78
|
case "tool_call_start":
|
|
95
|
-
return [
|
|
96
|
-
...lines,
|
|
97
|
-
{
|
|
98
|
-
key: event.callId,
|
|
99
|
-
type: "tool",
|
|
100
|
-
name: event.toolName,
|
|
101
|
-
callId: event.callId,
|
|
102
|
-
status: "running",
|
|
103
|
-
preview: previewInput(event.input),
|
|
104
|
-
},
|
|
105
|
-
];
|
|
106
79
|
case "tool_call_end":
|
|
107
|
-
return lines
|
|
108
|
-
line.type === "tool" && line.callId === event.callId
|
|
109
|
-
? {
|
|
110
|
-
...line,
|
|
111
|
-
status: event.success ? "ok" : "fail",
|
|
112
|
-
error: event.error,
|
|
113
|
-
}
|
|
114
|
-
: line,
|
|
115
|
-
);
|
|
80
|
+
return lines;
|
|
116
81
|
case "session_meta":
|
|
117
82
|
return lines;
|
|
118
83
|
case "error":
|
|
@@ -133,6 +98,32 @@ function applyEvent(lines: StreamLine[], event: AgentEvent): StreamLine[] {
|
|
|
133
98
|
}
|
|
134
99
|
}
|
|
135
100
|
|
|
101
|
+
function applyTurnToolEvent(tools: TurnToolLine[], event: AgentEvent): TurnToolLine[] {
|
|
102
|
+
if (event.kind === "tool_call_start") {
|
|
103
|
+
return [
|
|
104
|
+
...tools,
|
|
105
|
+
{
|
|
106
|
+
callId: event.callId,
|
|
107
|
+
name: event.toolName,
|
|
108
|
+
status: "running",
|
|
109
|
+
preview: previewInput(event.input),
|
|
110
|
+
},
|
|
111
|
+
];
|
|
112
|
+
}
|
|
113
|
+
if (event.kind === "tool_call_end") {
|
|
114
|
+
return tools.map((tool) =>
|
|
115
|
+
tool.callId === event.callId
|
|
116
|
+
? {
|
|
117
|
+
...tool,
|
|
118
|
+
status: event.success ? "ok" : "fail",
|
|
119
|
+
error: event.error,
|
|
120
|
+
}
|
|
121
|
+
: tool,
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
return tools;
|
|
125
|
+
}
|
|
126
|
+
|
|
136
127
|
function modelLabel(id: string): string {
|
|
137
128
|
return HARNESS_MODELS.find((model) => model.id === id)?.label ?? id;
|
|
138
129
|
}
|
|
@@ -158,7 +149,7 @@ function useTerminalSize(): { columns: number; rows: number } {
|
|
|
158
149
|
}
|
|
159
150
|
|
|
160
151
|
function headerStatusMark(status: UiStatus): string {
|
|
161
|
-
if (status === "running") return "
|
|
152
|
+
if (status === "running") return "…";
|
|
162
153
|
if (status === "approval") return "!";
|
|
163
154
|
return "●";
|
|
164
155
|
}
|
|
@@ -264,30 +255,55 @@ function toolStatusColor(status: ToolStatus, theme: Theme): string {
|
|
|
264
255
|
return theme.danger;
|
|
265
256
|
}
|
|
266
257
|
|
|
267
|
-
function StreamLineView(
|
|
258
|
+
function StreamLineView(
|
|
259
|
+
props: Readonly<{ theme: Theme; line: StreamLine; width: number }>,
|
|
260
|
+
): React.ReactElement {
|
|
268
261
|
const { theme, line } = props;
|
|
262
|
+
const width = Math.max(1, props.width);
|
|
269
263
|
if (line.type === "user") {
|
|
270
264
|
return (
|
|
271
|
-
<Box
|
|
272
|
-
<Text color={theme.brand}
|
|
273
|
-
|
|
265
|
+
<Box flexDirection="column" marginTop={1} marginBottom={0} width={width} overflow="hidden">
|
|
266
|
+
<Text color={theme.brand} bold>
|
|
267
|
+
You
|
|
268
|
+
</Text>
|
|
269
|
+
<Text color={theme.ink} wrap="wrap">
|
|
270
|
+
{line.text}
|
|
271
|
+
</Text>
|
|
274
272
|
</Box>
|
|
275
273
|
);
|
|
276
274
|
}
|
|
277
275
|
if (line.type === "assistant") {
|
|
278
276
|
return (
|
|
279
|
-
<Box
|
|
280
|
-
<Text color={theme.
|
|
277
|
+
<Box flexDirection="column" marginTop={1} marginBottom={0} width={width} overflow="hidden">
|
|
278
|
+
<Text color={theme.muted} bold>
|
|
279
|
+
Agent
|
|
280
|
+
</Text>
|
|
281
|
+
<Text color={theme.ink} wrap="wrap">
|
|
282
|
+
{plainTerminalText(line.text)}
|
|
283
|
+
</Text>
|
|
284
|
+
</Box>
|
|
285
|
+
);
|
|
286
|
+
}
|
|
287
|
+
if (line.type === "thought") {
|
|
288
|
+
return (
|
|
289
|
+
<Box flexDirection="column" marginTop={1} width={width} overflow="hidden">
|
|
290
|
+
<Text color={theme.muted} wrap="truncate">
|
|
291
|
+
{clipLabel(line.text.replace(/\s+/g, " ").trim(), width)}
|
|
292
|
+
</Text>
|
|
281
293
|
</Box>
|
|
282
294
|
);
|
|
283
295
|
}
|
|
284
296
|
if (line.type === "tool") {
|
|
285
297
|
return (
|
|
286
|
-
<Box flexDirection="column" marginBottom={1}>
|
|
287
|
-
<Text color={toolStatusColor(line.status, theme)}>
|
|
298
|
+
<Box flexDirection="column" marginBottom={1} width={width} overflow="hidden">
|
|
299
|
+
<Text color={toolStatusColor(line.status, theme)} wrap="truncate">
|
|
288
300
|
{toolStatusMark(line.status)} {line.name} <Text color={theme.muted}>{line.preview}</Text>
|
|
289
301
|
</Text>
|
|
290
|
-
{line.error ?
|
|
302
|
+
{line.error ? (
|
|
303
|
+
<Text color={theme.danger} wrap="truncate">
|
|
304
|
+
{line.error}
|
|
305
|
+
</Text>
|
|
306
|
+
) : null}
|
|
291
307
|
</Box>
|
|
292
308
|
);
|
|
293
309
|
}
|
|
@@ -312,18 +328,38 @@ function StreamLineView(props: Readonly<{ theme: Theme; line: StreamLine }>): Re
|
|
|
312
328
|
);
|
|
313
329
|
}
|
|
314
330
|
|
|
315
|
-
function StreamView(
|
|
331
|
+
function StreamView(
|
|
332
|
+
props: Readonly<{ theme: Theme; lines: StreamLine[]; width: number; height: number }>,
|
|
333
|
+
): React.ReactElement {
|
|
316
334
|
return (
|
|
317
|
-
<Box
|
|
335
|
+
<Box
|
|
336
|
+
flexDirection="column"
|
|
337
|
+
width={props.width}
|
|
338
|
+
height={props.height}
|
|
339
|
+
overflow="hidden"
|
|
340
|
+
flexShrink={0}
|
|
341
|
+
>
|
|
318
342
|
{props.lines.map((line) => (
|
|
319
|
-
<Box key={line.key}>
|
|
320
|
-
<StreamLineView theme={props.theme} line={line} />
|
|
343
|
+
<Box key={line.key} width={props.width} overflow="hidden" flexShrink={0}>
|
|
344
|
+
<StreamLineView theme={props.theme} line={line} width={props.width} />
|
|
321
345
|
</Box>
|
|
322
346
|
))}
|
|
323
347
|
</Box>
|
|
324
348
|
);
|
|
325
349
|
}
|
|
326
350
|
|
|
351
|
+
function TurnProgressBar(
|
|
352
|
+
props: Readonly<{ theme: Theme; label: string; width: number }>,
|
|
353
|
+
): React.ReactElement {
|
|
354
|
+
return (
|
|
355
|
+
<Box width={props.width} height={1} overflow="hidden" flexShrink={0}>
|
|
356
|
+
<Text color={props.theme.action} wrap="truncate">
|
|
357
|
+
… {props.label}
|
|
358
|
+
</Text>
|
|
359
|
+
</Box>
|
|
360
|
+
);
|
|
361
|
+
}
|
|
362
|
+
|
|
327
363
|
function pickerRowText(item: PickerItem, active: boolean): string {
|
|
328
364
|
const mark = active ? "› " : " ";
|
|
329
365
|
const hint = item.hint ? ` · ${item.hint}` : "";
|
|
@@ -453,18 +489,6 @@ function approvalDecision(
|
|
|
453
489
|
return undefined;
|
|
454
490
|
}
|
|
455
491
|
|
|
456
|
-
function slashMenuNav(
|
|
457
|
-
key: { upArrow: boolean; downArrow: boolean; tab: boolean },
|
|
458
|
-
slashOpen: boolean,
|
|
459
|
-
count: number,
|
|
460
|
-
): "up" | "down" | "tab" | undefined {
|
|
461
|
-
if (!slashOpen || count === 0) return undefined;
|
|
462
|
-
if (key.upArrow) return "up";
|
|
463
|
-
if (key.downArrow) return "down";
|
|
464
|
-
if (key.tab) return "tab";
|
|
465
|
-
return undefined;
|
|
466
|
-
}
|
|
467
|
-
|
|
468
492
|
function applyPickerKeys(
|
|
469
493
|
picker: PickerState,
|
|
470
494
|
key: {
|
|
@@ -503,53 +527,115 @@ function applyPickerKeys(
|
|
|
503
527
|
}
|
|
504
528
|
}
|
|
505
529
|
|
|
506
|
-
function
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
530
|
+
function applyTuiApprovalKey(
|
|
531
|
+
input: string,
|
|
532
|
+
key: { escape: boolean },
|
|
533
|
+
pending: { req: ApprovalRequest; resolve: (ok: boolean) => void },
|
|
534
|
+
harness: HarnessRuntime,
|
|
535
|
+
setPending: (value: null) => void,
|
|
536
|
+
setStatus: (status: UiStatus) => void,
|
|
513
537
|
): void {
|
|
514
|
-
const
|
|
515
|
-
if (
|
|
516
|
-
|
|
517
|
-
|
|
538
|
+
const decision = approvalDecision(input, key.escape, isUploadEditRequest(pending.req));
|
|
539
|
+
if (!decision) return;
|
|
540
|
+
if (decision === "always") harness.allowRiskyAlways(pending.req.toolName);
|
|
541
|
+
if (decision === "original") {
|
|
542
|
+
const original = pending.req.input.originalPath;
|
|
543
|
+
if (typeof original === "string" && original.trim()) {
|
|
544
|
+
pending.req.input.path = original;
|
|
545
|
+
}
|
|
518
546
|
}
|
|
519
|
-
|
|
520
|
-
|
|
547
|
+
pending.resolve(decision !== "no");
|
|
548
|
+
setPending(null);
|
|
549
|
+
setStatus("running");
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
function handleOpenPickerInput(
|
|
553
|
+
open: PickerState,
|
|
554
|
+
key: {
|
|
555
|
+
escape: boolean;
|
|
556
|
+
upArrow: boolean;
|
|
557
|
+
downArrow: boolean;
|
|
558
|
+
tab: boolean;
|
|
559
|
+
shift: boolean;
|
|
560
|
+
return: boolean;
|
|
561
|
+
},
|
|
562
|
+
ctx: {
|
|
563
|
+
pickerEscTimer: React.MutableRefObject<ReturnType<typeof setTimeout> | null>;
|
|
564
|
+
pickerRef: React.MutableRefObject<PickerState | null>;
|
|
565
|
+
setPicker: (
|
|
566
|
+
value: PickerState | null | ((current: PickerState | null) => PickerState | null),
|
|
567
|
+
) => void;
|
|
568
|
+
setStatus: (status: UiStatus) => void;
|
|
569
|
+
setValue: (value: string) => void;
|
|
570
|
+
confirmPicker: (state: PickerState) => void;
|
|
571
|
+
},
|
|
572
|
+
): void {
|
|
573
|
+
const nav = inkPickerNav(key);
|
|
574
|
+
if (nav === "esc") {
|
|
575
|
+
if (ctx.pickerEscTimer.current) clearTimeout(ctx.pickerEscTimer.current);
|
|
576
|
+
ctx.pickerEscTimer.current = setTimeout(() => {
|
|
577
|
+
ctx.pickerEscTimer.current = null;
|
|
578
|
+
const still = ctx.pickerRef.current;
|
|
579
|
+
if (!still) return;
|
|
580
|
+
applyPickerKeys(
|
|
581
|
+
still,
|
|
582
|
+
{
|
|
583
|
+
escape: true,
|
|
584
|
+
upArrow: false,
|
|
585
|
+
downArrow: false,
|
|
586
|
+
tab: false,
|
|
587
|
+
shift: false,
|
|
588
|
+
return: false,
|
|
589
|
+
},
|
|
590
|
+
ctx.setPicker,
|
|
591
|
+
ctx.setStatus,
|
|
592
|
+
ctx.setValue,
|
|
593
|
+
ctx.confirmPicker,
|
|
594
|
+
);
|
|
595
|
+
}, 40);
|
|
521
596
|
return;
|
|
522
597
|
}
|
|
523
|
-
if (
|
|
524
|
-
|
|
525
|
-
|
|
598
|
+
if (ctx.pickerEscTimer.current) {
|
|
599
|
+
clearTimeout(ctx.pickerEscTimer.current);
|
|
600
|
+
ctx.pickerEscTimer.current = null;
|
|
526
601
|
}
|
|
602
|
+
applyPickerKeys(open, key, ctx.setPicker, ctx.setStatus, ctx.setValue, ctx.confirmPicker);
|
|
527
603
|
}
|
|
528
604
|
|
|
529
605
|
function ComposerFooter(
|
|
530
606
|
props: Readonly<{
|
|
531
607
|
theme: Theme;
|
|
532
608
|
status: UiStatus;
|
|
533
|
-
statusLabel: string;
|
|
534
609
|
value: string;
|
|
610
|
+
width: number;
|
|
535
611
|
onChange: (value: string) => void;
|
|
536
612
|
onSubmit: (value: string) => void;
|
|
537
613
|
slashOpen: boolean;
|
|
538
614
|
slashItems: ReturnType<typeof filterSlashCommands>;
|
|
539
615
|
slashIndex: number;
|
|
540
|
-
|
|
616
|
+
onSlashUp: () => void;
|
|
617
|
+
onSlashDown: () => void;
|
|
618
|
+
onSlashTab: () => void;
|
|
619
|
+
hintLine: string;
|
|
541
620
|
pickerOpen: boolean;
|
|
542
621
|
}>,
|
|
543
622
|
): React.ReactElement | null {
|
|
544
623
|
if (props.pickerOpen || props.status === "picker") return null;
|
|
545
|
-
|
|
546
|
-
return <Text color={props.theme.muted}>{props.statusLabel}</Text>;
|
|
547
|
-
}
|
|
624
|
+
const typingEnabled = props.status !== "approval";
|
|
548
625
|
return (
|
|
549
|
-
<Box flexDirection="column">
|
|
550
|
-
<Box>
|
|
551
|
-
<
|
|
552
|
-
|
|
626
|
+
<Box flexDirection="column" width={props.width} flexShrink={0} overflow="hidden">
|
|
627
|
+
<Box width={props.width} overflow="hidden">
|
|
628
|
+
<TuiTextInput
|
|
629
|
+
value={props.value}
|
|
630
|
+
width={props.width}
|
|
631
|
+
enabled={typingEnabled}
|
|
632
|
+
slashOpen={props.slashOpen}
|
|
633
|
+
onSlashUp={props.onSlashUp}
|
|
634
|
+
onSlashDown={props.onSlashDown}
|
|
635
|
+
onSlashTab={props.onSlashTab}
|
|
636
|
+
onChange={props.onChange}
|
|
637
|
+
onSubmit={props.onSubmit}
|
|
638
|
+
/>
|
|
553
639
|
</Box>
|
|
554
640
|
{props.slashOpen ? (
|
|
555
641
|
<SlashMenu
|
|
@@ -558,7 +644,9 @@ function ComposerFooter(
|
|
|
558
644
|
selected={Math.min(props.slashIndex, Math.max(props.slashItems.length - 1, 0))}
|
|
559
645
|
/>
|
|
560
646
|
) : (
|
|
561
|
-
<Text color={props.theme.
|
|
647
|
+
<Text color={props.theme.muted} wrap="truncate">
|
|
648
|
+
{props.hintLine}
|
|
649
|
+
</Text>
|
|
562
650
|
)}
|
|
563
651
|
</Box>
|
|
564
652
|
);
|
|
@@ -569,6 +657,8 @@ function App(props: Readonly<{ harness: HarnessRuntime }>): React.ReactElement {
|
|
|
569
657
|
const theme = harness.theme;
|
|
570
658
|
const { exit } = useApp();
|
|
571
659
|
const [lines, setLines] = useState<StreamLine[]>([]);
|
|
660
|
+
const [turnTools, setTurnTools] = useState<TurnToolLine[]>([]);
|
|
661
|
+
const [progressTick, setProgressTick] = useState(0);
|
|
572
662
|
const [value, setValue] = useState("");
|
|
573
663
|
const [status, setStatus] = useState<UiStatus>("idle");
|
|
574
664
|
const [title, setTitle] = useState("New chat");
|
|
@@ -602,6 +692,14 @@ function App(props: Readonly<{ harness: HarnessRuntime }>): React.ReactElement {
|
|
|
602
692
|
setSlashIndex(0);
|
|
603
693
|
}, [value]);
|
|
604
694
|
|
|
695
|
+
useEffect(() => {
|
|
696
|
+
if (status !== "running") return;
|
|
697
|
+
const timer = setInterval(() => {
|
|
698
|
+
setProgressTick((tick) => tick + 1);
|
|
699
|
+
}, 450);
|
|
700
|
+
return () => clearInterval(timer);
|
|
701
|
+
}, [status]);
|
|
702
|
+
|
|
605
703
|
useEffect(() => {
|
|
606
704
|
if (!harness.hasApiKey) return;
|
|
607
705
|
void loadLiveTargetNames(resolveOpenRouter(harness.config)).then(() => {
|
|
@@ -692,58 +790,22 @@ function App(props: Readonly<{ harness: HarnessRuntime }>): React.ReactElement {
|
|
|
692
790
|
useInput(
|
|
693
791
|
(input, key) => {
|
|
694
792
|
if (status === "approval" && pending) {
|
|
695
|
-
|
|
696
|
-
if (!decision) return;
|
|
697
|
-
if (decision === "always") harness.allowRiskyAlways(pending.req.toolName);
|
|
698
|
-
if (
|
|
699
|
-
decision === "original" &&
|
|
700
|
-
typeof pending.req.input.originalPath === "string" &&
|
|
701
|
-
pending.req.input.originalPath.trim()
|
|
702
|
-
) {
|
|
703
|
-
pending.req.input.path = pending.req.input.originalPath;
|
|
704
|
-
}
|
|
705
|
-
pending.resolve(decision !== "no");
|
|
706
|
-
setPending(null);
|
|
707
|
-
setStatus("running");
|
|
793
|
+
applyTuiApprovalKey(input, key, pending, harness, setPending, setStatus);
|
|
708
794
|
return;
|
|
709
795
|
}
|
|
710
796
|
const open = pickerRef.current;
|
|
711
797
|
if (open) {
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
still,
|
|
721
|
-
{
|
|
722
|
-
escape: true,
|
|
723
|
-
upArrow: false,
|
|
724
|
-
downArrow: false,
|
|
725
|
-
tab: false,
|
|
726
|
-
shift: false,
|
|
727
|
-
return: false,
|
|
728
|
-
},
|
|
729
|
-
setPicker,
|
|
730
|
-
setStatus,
|
|
731
|
-
setValue,
|
|
732
|
-
confirmPicker,
|
|
733
|
-
);
|
|
734
|
-
}, 40);
|
|
735
|
-
return;
|
|
736
|
-
}
|
|
737
|
-
if (pickerEscTimer.current) {
|
|
738
|
-
clearTimeout(pickerEscTimer.current);
|
|
739
|
-
pickerEscTimer.current = null;
|
|
740
|
-
}
|
|
741
|
-
applyPickerKeys(open, key, setPicker, setStatus, setValue, confirmPicker);
|
|
742
|
-
return;
|
|
798
|
+
handleOpenPickerInput(open, key, {
|
|
799
|
+
pickerEscTimer,
|
|
800
|
+
pickerRef,
|
|
801
|
+
setPicker,
|
|
802
|
+
setStatus,
|
|
803
|
+
setValue,
|
|
804
|
+
confirmPicker,
|
|
805
|
+
});
|
|
743
806
|
}
|
|
744
|
-
trySlashMenuInput(key, slashOpen, slashItems, slashIndex, setSlashIndex, setValue);
|
|
745
807
|
},
|
|
746
|
-
{ isActive:
|
|
808
|
+
{ isActive: (status === "approval" && Boolean(pending)) || Boolean(picker) },
|
|
747
809
|
);
|
|
748
810
|
|
|
749
811
|
const handleSlash = useCallback(
|
|
@@ -824,6 +886,7 @@ function App(props: Readonly<{ harness: HarnessRuntime }>): React.ReactElement {
|
|
|
824
886
|
return;
|
|
825
887
|
}
|
|
826
888
|
setLines((prev) => [...prev, { key: `u-${prev.length}`, type: "user", text: trimmed }]);
|
|
889
|
+
setTurnTools([]);
|
|
827
890
|
setStatus("running");
|
|
828
891
|
const controller = new AbortController();
|
|
829
892
|
abortRef.current = controller;
|
|
@@ -833,6 +896,26 @@ function App(props: Readonly<{ harness: HarnessRuntime }>): React.ReactElement {
|
|
|
833
896
|
(event) => {
|
|
834
897
|
if (event.kind === "session_meta") setTitle(event.title);
|
|
835
898
|
if (event.kind === "usage") setSpend(harness.spend);
|
|
899
|
+
if (event.kind === "completion") {
|
|
900
|
+
setTurnTools((tools) => {
|
|
901
|
+
const summary = summarizeTurnTools(tools);
|
|
902
|
+
setLines((prev) => {
|
|
903
|
+
const next = applyEvent(prev, event);
|
|
904
|
+
if (!summary) return next;
|
|
905
|
+
return [
|
|
906
|
+
...next,
|
|
907
|
+
{
|
|
908
|
+
key: `toolsum-${next.length}`,
|
|
909
|
+
type: "system" as const,
|
|
910
|
+
text: summary,
|
|
911
|
+
},
|
|
912
|
+
];
|
|
913
|
+
});
|
|
914
|
+
return [];
|
|
915
|
+
});
|
|
916
|
+
return;
|
|
917
|
+
}
|
|
918
|
+
setTurnTools((prev) => applyTurnToolEvent(prev, event));
|
|
836
919
|
setLines((prev) => applyEvent(prev, event));
|
|
837
920
|
},
|
|
838
921
|
{
|
|
@@ -853,21 +936,44 @@ function App(props: Readonly<{ harness: HarnessRuntime }>): React.ReactElement {
|
|
|
853
936
|
[handleSlash, harness, slashIndex, slashItems, slashOpen, status],
|
|
854
937
|
);
|
|
855
938
|
|
|
856
|
-
const
|
|
857
|
-
if (status === "running") return "
|
|
858
|
-
if (status === "approval") return "
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
939
|
+
const composerHint = useMemo(() => {
|
|
940
|
+
if (status === "running") return "Agent is working… (you can still type your next message)";
|
|
941
|
+
if (status === "approval") return "Answer the approval prompt above (y / n / a)";
|
|
942
|
+
return mediaLine;
|
|
943
|
+
}, [mediaLine, status]);
|
|
944
|
+
|
|
945
|
+
const slashUp = useCallback(
|
|
946
|
+
() => setSlashIndex((index) => stepIndex(index, slashItems.length, -1)),
|
|
947
|
+
[slashItems.length],
|
|
948
|
+
);
|
|
949
|
+
const slashDown = useCallback(
|
|
950
|
+
() => setSlashIndex((index) => stepIndex(index, slashItems.length, 1)),
|
|
951
|
+
[slashItems.length],
|
|
952
|
+
);
|
|
953
|
+
const slashTab = useCallback(() => {
|
|
954
|
+
const selected = slashItems[slashIndex] ?? slashItems[0];
|
|
955
|
+
if (selected) setValue(completeSlashCommand(selected));
|
|
956
|
+
}, [slashIndex, slashItems]);
|
|
862
957
|
|
|
863
958
|
const rail = sidebarWidth(columns);
|
|
959
|
+
const warmupSeed = useMemo(() => lines.filter((line) => line.type === "user").length, [lines]);
|
|
960
|
+
const assistantStarted = lines.at(-1)?.type === "assistant";
|
|
961
|
+
const progressLabel = turnProgressLabel({
|
|
962
|
+
tools: turnTools,
|
|
963
|
+
assistantStarted,
|
|
964
|
+
tick: progressTick,
|
|
965
|
+
warmupSeed,
|
|
966
|
+
});
|
|
967
|
+
const showProgress = status === "running";
|
|
864
968
|
const chrome = tuiChromeRows({
|
|
865
969
|
approval: Boolean(pending),
|
|
866
970
|
pickerCount: picker?.items.length ?? 0,
|
|
867
971
|
slashCount: slashOpen ? slashItems.length : 0,
|
|
972
|
+
activity: showProgress,
|
|
868
973
|
});
|
|
869
|
-
const
|
|
974
|
+
const transcriptHeight = visibleTranscriptRows(rows, chrome);
|
|
870
975
|
const mainWidth = rail ? Math.max(40, columns - rail) : columns;
|
|
976
|
+
const shown = takeLinesForRowBudget(lines, mainWidth, transcriptHeight);
|
|
871
977
|
|
|
872
978
|
const main = (
|
|
873
979
|
<Box flexDirection="column" width={mainWidth} height={rows}>
|
|
@@ -890,22 +996,26 @@ function App(props: Readonly<{ harness: HarnessRuntime }>): React.ReactElement {
|
|
|
890
996
|
/>
|
|
891
997
|
</Box>
|
|
892
998
|
) : (
|
|
893
|
-
<
|
|
894
|
-
<StreamView theme={theme} lines={shown} />
|
|
895
|
-
</Box>
|
|
999
|
+
<StreamView theme={theme} lines={shown} width={mainWidth} height={transcriptHeight} />
|
|
896
1000
|
)}
|
|
897
1001
|
{pending ? <ApprovalCard theme={theme} req={pending.req} /> : null}
|
|
1002
|
+
{showProgress ? (
|
|
1003
|
+
<TurnProgressBar theme={theme} label={progressLabel} width={mainWidth} />
|
|
1004
|
+
) : null}
|
|
898
1005
|
<ComposerFooter
|
|
899
1006
|
theme={theme}
|
|
900
1007
|
status={status}
|
|
901
|
-
statusLabel={statusLabel}
|
|
902
1008
|
value={value}
|
|
1009
|
+
width={mainWidth}
|
|
903
1010
|
onChange={setValue}
|
|
904
1011
|
onSubmit={submit}
|
|
905
1012
|
slashOpen={slashOpen}
|
|
906
1013
|
slashItems={slashItems}
|
|
907
1014
|
slashIndex={slashIndex}
|
|
908
|
-
|
|
1015
|
+
onSlashUp={slashUp}
|
|
1016
|
+
onSlashDown={slashDown}
|
|
1017
|
+
onSlashTab={slashTab}
|
|
1018
|
+
hintLine={composerHint}
|
|
909
1019
|
pickerOpen={Boolean(picker)}
|
|
910
1020
|
/>
|
|
911
1021
|
</Box>
|
package/src/desktop/bridge.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { randomBytes } from "node:crypto";
|
|
2
2
|
import { readFile, stat } from "node:fs/promises";
|
|
3
3
|
import { isAbsolute, relative, resolve } from "node:path";
|
|
4
|
+
import { resolveAgentCwd } from "../agent-cwd.ts";
|
|
4
5
|
import { mediaModelLabel } from "../cli/media-models.ts";
|
|
5
6
|
import { HARNESS_MODELS, modelPickerItems } from "../cli/models.ts";
|
|
6
7
|
import {
|
|
@@ -1056,7 +1057,10 @@ export async function startDesktopBridge(options: StartDesktopBridgeOptions): Pr
|
|
|
1056
1057
|
}
|
|
1057
1058
|
|
|
1058
1059
|
export async function main(): Promise<void> {
|
|
1059
|
-
const cwd =
|
|
1060
|
+
const cwd = resolveAgentCwd({
|
|
1061
|
+
start: process.env.HARNESS_CWD?.trim() || process.cwd(),
|
|
1062
|
+
explicit: process.env.HARNESS_CWD_EXPLICIT?.trim(),
|
|
1063
|
+
});
|
|
1060
1064
|
const { ready } = await startDesktopBridge({ cwd });
|
|
1061
1065
|
process.stdout.write(`HARNESS_READY=${JSON.stringify(ready)}\n`);
|
|
1062
1066
|
}
|