@useavalon/caelence-agent 0.1.0
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/README.md +201 -0
- package/bin/harness.ts +10 -0
- package/package.json +75 -0
- package/skills/copy-editor/SKILL.md +74 -0
- package/skills/copy-editor/references/anti-slop.md +62 -0
- package/skills/copy-editor/references/ui-states.md +46 -0
- package/skills/copy-editor/scripts/lint-copy.py +78 -0
- package/skills/copy-harry-dry/SKILL.md +120 -0
- package/skills/copy-rmbc/SKILL.md +97 -0
- package/skills/copywriting/SKILL.md +73 -0
- package/skills/impeccable/SKILL.md +133 -0
- package/skills/impeccable/references/visual-slop.md +36 -0
- package/src/cli/main.test.ts +36 -0
- package/src/cli/main.ts +384 -0
- package/src/cli/media-models.ts +82 -0
- package/src/cli/models.ts +43 -0
- package/src/cli/openrouter-live.test.ts +35 -0
- package/src/cli/openrouter-live.ts +72 -0
- package/src/cli/openrouter-modality.test.ts +25 -0
- package/src/cli/openrouter-modality.ts +83 -0
- package/src/cli/picker.test.ts +53 -0
- package/src/cli/picker.ts +48 -0
- package/src/cli/plain-text.ts +15 -0
- package/src/cli/slash-dispatch.test.ts +213 -0
- package/src/cli/slash-dispatch.ts +298 -0
- package/src/cli/slash.test.ts +113 -0
- package/src/cli/slash.ts +119 -0
- package/src/cli/theme.test.ts +10 -0
- package/src/cli/theme.ts +44 -0
- package/src/cli/tui-layout.test.ts +33 -0
- package/src/cli/tui-layout.ts +33 -0
- package/src/cli/tui-screen.test.ts +33 -0
- package/src/cli/tui-screen.ts +13 -0
- package/src/cli/tui.tsx +735 -0
- package/src/config.ts +86 -0
- package/src/core/approval.test.ts +60 -0
- package/src/core/approval.ts +83 -0
- package/src/core/auto-router.test.ts +31 -0
- package/src/core/auto-router.ts +66 -0
- package/src/core/errors.ts +67 -0
- package/src/core/events.ts +85 -0
- package/src/core/hooks.test.ts +66 -0
- package/src/core/hooks.ts +94 -0
- package/src/core/ids.ts +6 -0
- package/src/core/instructions.test.ts +49 -0
- package/src/core/instructions.ts +44 -0
- package/src/core/logger.ts +45 -0
- package/src/core/mcp.ts +191 -0
- package/src/core/mode.test.ts +63 -0
- package/src/core/mode.ts +60 -0
- package/src/core/run-turn.test.ts +339 -0
- package/src/core/run-turn.ts +487 -0
- package/src/core/session.test.ts +203 -0
- package/src/core/session.ts +349 -0
- package/src/core/stdio-mcp.ts +143 -0
- package/src/core/strands.ts +262 -0
- package/src/core/usage.test.ts +69 -0
- package/src/core/usage.ts +171 -0
- package/src/desktop/bridge.test.ts +331 -0
- package/src/desktop/bridge.ts +729 -0
- package/src/desktop/open-local.test.ts +40 -0
- package/src/desktop/open-local.ts +52 -0
- package/src/desktop/preview-cache.test.ts +36 -0
- package/src/desktop/preview-cache.ts +63 -0
- package/src/desktop/secrets.test.ts +44 -0
- package/src/desktop/secrets.ts +60 -0
- package/src/evals/runner.test.ts +43 -0
- package/src/evals/runner.ts +225 -0
- package/src/index.ts +132 -0
- package/src/init.test.ts +54 -0
- package/src/init.ts +117 -0
- package/src/integrations/catalog-data.json +424 -0
- package/src/integrations/catalog.ts +81 -0
- package/src/integrations/connect.ts +231 -0
- package/src/integrations/integrations.test.ts +329 -0
- package/src/integrations/marks/airtable.svg +1 -0
- package/src/integrations/marks/algolia-mcp.svg +1 -0
- package/src/integrations/marks/canva.jpg +0 -0
- package/src/integrations/marks/cloudflare-mcp.svg +1 -0
- package/src/integrations/marks/contentful-mcp.svg +1 -0
- package/src/integrations/marks/craft.png +0 -0
- package/src/integrations/marks/dinero.png +0 -0
- package/src/integrations/marks/figma.svg +1 -0
- package/src/integrations/marks/gitlab.svg +1 -0
- package/src/integrations/marks/google-calendar.svg +1 -0
- package/src/integrations/marks/google-docs.svg +1 -0
- package/src/integrations/marks/google-drive.svg +1 -0
- package/src/integrations/marks/google-gmail.svg +1 -0
- package/src/integrations/marks/google-sheets.svg +1 -0
- package/src/integrations/marks/google-slides.svg +1 -0
- package/src/integrations/marks/granola.png +0 -0
- package/src/integrations/marks/jira.svg +1 -0
- package/src/integrations/marks/linear.svg +1 -0
- package/src/integrations/marks/microsoft-365.svg +7 -0
- package/src/integrations/marks/miro.svg +1 -0
- package/src/integrations/marks/mixpanel-mcp.svg +1 -0
- package/src/integrations/marks/monday.png +0 -0
- package/src/integrations/marks/mux-mcp.png +0 -0
- package/src/integrations/marks/neon-mcp.svg +1 -0
- package/src/integrations/marks/notion.svg +1 -0
- package/src/integrations/marks/paddle-mcp.svg +1 -0
- package/src/integrations/marks/posthog-mcp.svg +1 -0
- package/src/integrations/marks/prismic-mcp.svg +1 -0
- package/src/integrations/marks/replicate-mcp.svg +1 -0
- package/src/integrations/marks/resend-mcp.svg +1 -0
- package/src/integrations/marks/sanity-mcp.svg +1 -0
- package/src/integrations/marks/sceneplane.png +0 -0
- package/src/integrations/marks/sentry-mcp.svg +1 -0
- package/src/integrations/marks/stripe-mcp.svg +1 -0
- package/src/integrations/marks/supabase-mcp.svg +1 -0
- package/src/integrations/marks/upstash-mcp.svg +1 -0
- package/src/integrations/marks/vercel.svg +1 -0
- package/src/integrations/marks/webflow.svg +1 -0
- package/src/integrations/mcp-oauth.test.ts +39 -0
- package/src/integrations/mcp-oauth.ts +330 -0
- package/src/integrations/oauth-clients.test.ts +77 -0
- package/src/integrations/oauth-clients.ts +127 -0
- package/src/integrations/oauth.ts +145 -0
- package/src/integrations/remote-mcp.ts +307 -0
- package/src/integrations/store.ts +149 -0
- package/src/media/openrouter-generate.test.ts +80 -0
- package/src/media/openrouter-generate.ts +178 -0
- package/src/media/prefs.ts +54 -0
- package/src/observability/categories.ts +41 -0
- package/src/observability/create.test.ts +56 -0
- package/src/observability/create.ts +45 -0
- package/src/observability/experiments.ts +113 -0
- package/src/observability/fake.ts +191 -0
- package/src/observability/index.ts +57 -0
- package/src/observability/judges.ts +91 -0
- package/src/observability/langfuse.ts +275 -0
- package/src/observability/noop.ts +33 -0
- package/src/observability/observability.test.ts +113 -0
- package/src/observability/prompts.ts +98 -0
- package/src/observability/regression-items.ts +158 -0
- package/src/observability/sanitize.test.ts +26 -0
- package/src/observability/sanitize.ts +57 -0
- package/src/observability/types.ts +122 -0
- package/src/observability/version.ts +41 -0
- package/src/product.ts +2 -0
- package/src/runtime.test.ts +98 -0
- package/src/runtime.ts +438 -0
- package/src/skills/author-skill.ts +49 -0
- package/src/skills/create-skill.test.ts +108 -0
- package/src/skills/create-skill.ts +70 -0
- package/src/skills/loader.test.ts +122 -0
- package/src/skills/loader.ts +162 -0
- package/src/skills/registry.test.ts +190 -0
- package/src/skills/registry.ts +402 -0
- package/src/skills/skill-command.ts +53 -0
- package/src/skills/user-dir.ts +8 -0
- package/src/tools/git.test.ts +71 -0
- package/src/tools/git.ts +126 -0
- package/src/tools/local.ts +258 -0
- package/src/tools/paths.test.ts +24 -0
- package/src/tools/paths.ts +39 -0
- package/src/tools/read-skill.ts +43 -0
- package/src/tools/subagent.test.ts +77 -0
- package/src/tools/subagent.ts +74 -0
- package/src/tools/todo.test.ts +25 -0
- package/src/tools/todo.ts +70 -0
package/src/cli/tui.tsx
ADDED
|
@@ -0,0 +1,735 @@
|
|
|
1
|
+
import { Box, render, Text, useApp, useInput, useStdout } from "ink";
|
|
2
|
+
import TextInput from "ink-text-input";
|
|
3
|
+
import type React from "react";
|
|
4
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
5
|
+
import { resolveOpenRouter } from "../config.ts";
|
|
6
|
+
import type { ApprovalRequest } from "../core/approval.ts";
|
|
7
|
+
import type { AgentEvent } from "../core/events.ts";
|
|
8
|
+
import type { Session } from "../core/session.ts";
|
|
9
|
+
import { formatUsage, type TokenUsage } from "../core/usage.ts";
|
|
10
|
+
import { readMediaPrefs } from "../media/prefs.ts";
|
|
11
|
+
import { PRODUCT_NAME } from "../product.ts";
|
|
12
|
+
import type { HarnessRuntime } from "../runtime.ts";
|
|
13
|
+
import { formatMediaComposerLine } from "./media-models.ts";
|
|
14
|
+
import { HARNESS_MODELS } from "./models.ts";
|
|
15
|
+
import { cachedLiveTargetNames, loadLiveTargetNames } from "./openrouter-live.ts";
|
|
16
|
+
import { type PickerItem, sessionPickerItems, stepIndex } from "./picker.ts";
|
|
17
|
+
import { plainTerminalText } from "./plain-text.ts";
|
|
18
|
+
import {
|
|
19
|
+
completeSlashCommand,
|
|
20
|
+
draftAfterPicker,
|
|
21
|
+
filterSlashCommands,
|
|
22
|
+
formatSlashCommand,
|
|
23
|
+
resolveSlashSubmit,
|
|
24
|
+
} from "./slash.ts";
|
|
25
|
+
import { applyPickerChoice, dispatchSlash, type SlashPickerKind } from "./slash-dispatch.ts";
|
|
26
|
+
import type { Theme } from "./theme.ts";
|
|
27
|
+
import {
|
|
28
|
+
clipLabel,
|
|
29
|
+
sidebarWidth,
|
|
30
|
+
takeVisibleLines,
|
|
31
|
+
tuiChromeRows,
|
|
32
|
+
visibleTranscriptCount,
|
|
33
|
+
} from "./tui-layout.ts";
|
|
34
|
+
import { enterAltScreen } from "./tui-screen.ts";
|
|
35
|
+
|
|
36
|
+
type ToolStatus = "running" | "ok" | "fail";
|
|
37
|
+
|
|
38
|
+
type StreamLine =
|
|
39
|
+
| { key: string; type: "user"; text: string }
|
|
40
|
+
| { key: string; type: "assistant"; text: string }
|
|
41
|
+
| {
|
|
42
|
+
key: string;
|
|
43
|
+
type: "tool";
|
|
44
|
+
name: string;
|
|
45
|
+
callId: string;
|
|
46
|
+
status: ToolStatus;
|
|
47
|
+
preview: string;
|
|
48
|
+
error?: string;
|
|
49
|
+
}
|
|
50
|
+
| { key: string; type: "system"; text: string }
|
|
51
|
+
| { key: string; type: "error"; text: string }
|
|
52
|
+
| { key: string; type: "todos"; text: string };
|
|
53
|
+
|
|
54
|
+
type UiStatus = "idle" | "running" | "approval" | "picker";
|
|
55
|
+
|
|
56
|
+
type PickerState = {
|
|
57
|
+
title: string;
|
|
58
|
+
items: PickerItem[];
|
|
59
|
+
index: number;
|
|
60
|
+
kind: SlashPickerKind;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
function previewInput(input: Record<string, unknown>): string {
|
|
64
|
+
const cmd = typeof input.command === "string" ? input.command : undefined;
|
|
65
|
+
const path = typeof input.path === "string" ? input.path : undefined;
|
|
66
|
+
const pattern = typeof input.pattern === "string" ? input.pattern : undefined;
|
|
67
|
+
const prompt = typeof input.prompt === "string" ? input.prompt : undefined;
|
|
68
|
+
const label = typeof input.label === "string" ? input.label : undefined;
|
|
69
|
+
const raw = label ?? cmd ?? path ?? pattern ?? prompt ?? JSON.stringify(input);
|
|
70
|
+
return raw.length > 80 ? `${raw.slice(0, 79)}…` : raw;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function applyEvent(lines: StreamLine[], event: AgentEvent): StreamLine[] {
|
|
74
|
+
switch (event.kind) {
|
|
75
|
+
case "text_delta": {
|
|
76
|
+
const last = lines[lines.length - 1];
|
|
77
|
+
if (last?.type === "assistant") {
|
|
78
|
+
return [...lines.slice(0, -1), { ...last, text: last.text + event.text }];
|
|
79
|
+
}
|
|
80
|
+
return [...lines, { key: `a-${lines.length}`, type: "assistant", text: event.text }];
|
|
81
|
+
}
|
|
82
|
+
case "reasoning_delta":
|
|
83
|
+
return lines;
|
|
84
|
+
case "tool_call_start":
|
|
85
|
+
return [
|
|
86
|
+
...lines,
|
|
87
|
+
{
|
|
88
|
+
key: event.callId,
|
|
89
|
+
type: "tool",
|
|
90
|
+
name: event.toolName,
|
|
91
|
+
callId: event.callId,
|
|
92
|
+
status: "running",
|
|
93
|
+
preview: previewInput(event.input),
|
|
94
|
+
},
|
|
95
|
+
];
|
|
96
|
+
case "tool_call_end":
|
|
97
|
+
return lines.map((line) =>
|
|
98
|
+
line.type === "tool" && line.callId === event.callId
|
|
99
|
+
? {
|
|
100
|
+
...line,
|
|
101
|
+
status: event.success ? "ok" : "fail",
|
|
102
|
+
error: event.error,
|
|
103
|
+
}
|
|
104
|
+
: line,
|
|
105
|
+
);
|
|
106
|
+
case "session_meta":
|
|
107
|
+
return lines;
|
|
108
|
+
case "error":
|
|
109
|
+
return [...lines, { key: `e-${lines.length}`, type: "error", text: event.message }];
|
|
110
|
+
case "todos":
|
|
111
|
+
return [
|
|
112
|
+
...lines,
|
|
113
|
+
{
|
|
114
|
+
key: `todo-${lines.length}`,
|
|
115
|
+
type: "todos",
|
|
116
|
+
text: event.items.map((item) => `${item.status} ${item.content}`).join(" · "),
|
|
117
|
+
},
|
|
118
|
+
];
|
|
119
|
+
case "usage":
|
|
120
|
+
case "completion":
|
|
121
|
+
case "approval_request":
|
|
122
|
+
return lines;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function modelLabel(id: string): string {
|
|
127
|
+
return HARNESS_MODELS.find((model) => model.id === id)?.label ?? id;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function useTerminalSize(): { columns: number; rows: number } {
|
|
131
|
+
const { stdout } = useStdout();
|
|
132
|
+
const [size, setSize] = useState({
|
|
133
|
+
columns: stdout.columns || 80,
|
|
134
|
+
rows: stdout.rows || 24,
|
|
135
|
+
});
|
|
136
|
+
useEffect(() => {
|
|
137
|
+
const onResize = () =>
|
|
138
|
+
setSize({
|
|
139
|
+
columns: stdout.columns || 80,
|
|
140
|
+
rows: stdout.rows || 24,
|
|
141
|
+
});
|
|
142
|
+
stdout.on("resize", onResize);
|
|
143
|
+
return () => {
|
|
144
|
+
stdout.off("resize", onResize);
|
|
145
|
+
};
|
|
146
|
+
}, [stdout]);
|
|
147
|
+
return size;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function Header(props: {
|
|
151
|
+
theme: Theme;
|
|
152
|
+
name: string;
|
|
153
|
+
model: string;
|
|
154
|
+
cwd: string;
|
|
155
|
+
title: string;
|
|
156
|
+
status: UiStatus;
|
|
157
|
+
mode: string;
|
|
158
|
+
spend?: string;
|
|
159
|
+
}): React.ReactElement {
|
|
160
|
+
const mark = props.status === "running" ? "·" : props.status === "approval" ? "!" : "●";
|
|
161
|
+
const color =
|
|
162
|
+
props.status === "running"
|
|
163
|
+
? props.theme.action
|
|
164
|
+
: props.status === "approval"
|
|
165
|
+
? props.theme.warning
|
|
166
|
+
: props.theme.brand;
|
|
167
|
+
const label = modelLabel(props.model);
|
|
168
|
+
const target = cachedLiveTargetNames().get(props.model);
|
|
169
|
+
return (
|
|
170
|
+
<Box flexDirection="column">
|
|
171
|
+
<Box>
|
|
172
|
+
<Text color={color}>{mark}</Text>
|
|
173
|
+
<Text color={props.theme.ink} bold>
|
|
174
|
+
{" "}
|
|
175
|
+
{PRODUCT_NAME}
|
|
176
|
+
</Text>
|
|
177
|
+
<Text color={props.theme.muted}>
|
|
178
|
+
{" "}
|
|
179
|
+
{props.title} · {props.mode} · {label}
|
|
180
|
+
{target && target.toLowerCase() !== label.toLowerCase() ? ` · ${target}` : ""}
|
|
181
|
+
{props.spend ? ` · ${props.spend}` : ""}
|
|
182
|
+
</Text>
|
|
183
|
+
</Box>
|
|
184
|
+
<Text color={props.theme.line}>{clipLabel(props.cwd, 72)}</Text>
|
|
185
|
+
</Box>
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function Sidebar(props: {
|
|
190
|
+
theme: Theme;
|
|
191
|
+
width: number;
|
|
192
|
+
height: number;
|
|
193
|
+
sessions: PickerItem[];
|
|
194
|
+
currentId?: string;
|
|
195
|
+
}): React.ReactElement {
|
|
196
|
+
const inner = Math.max(8, props.width - 2);
|
|
197
|
+
const slots = Math.max(3, props.height - 6);
|
|
198
|
+
const items = props.sessions.slice(0, slots);
|
|
199
|
+
return (
|
|
200
|
+
<Box
|
|
201
|
+
flexDirection="column"
|
|
202
|
+
width={props.width}
|
|
203
|
+
height={props.height}
|
|
204
|
+
paddingRight={1}
|
|
205
|
+
borderStyle="single"
|
|
206
|
+
borderColor={props.theme.line}
|
|
207
|
+
>
|
|
208
|
+
<Text color={props.theme.brand} bold>
|
|
209
|
+
Caelence
|
|
210
|
+
</Text>
|
|
211
|
+
<Text color={props.theme.muted}>agent</Text>
|
|
212
|
+
<Text color={props.theme.line}> </Text>
|
|
213
|
+
<Text color={props.theme.muted}>Sessions</Text>
|
|
214
|
+
{items.length === 0 ? (
|
|
215
|
+
<Text color={props.theme.line}>No saved sessions</Text>
|
|
216
|
+
) : (
|
|
217
|
+
items.map((item) => (
|
|
218
|
+
<Text
|
|
219
|
+
key={item.id}
|
|
220
|
+
color={item.id === props.currentId ? props.theme.brand : props.theme.ink}
|
|
221
|
+
wrap="truncate"
|
|
222
|
+
>
|
|
223
|
+
{item.id === props.currentId ? "› " : " "}
|
|
224
|
+
{clipLabel(item.label, inner - 2)}
|
|
225
|
+
</Text>
|
|
226
|
+
))
|
|
227
|
+
)}
|
|
228
|
+
<Box flexGrow={1} />
|
|
229
|
+
<Text color={props.theme.line}>/resume</Text>
|
|
230
|
+
<Text color={props.theme.line}>/settings</Text>
|
|
231
|
+
</Box>
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function StreamView(props: { theme: Theme; lines: StreamLine[] }): React.ReactElement {
|
|
236
|
+
return (
|
|
237
|
+
<Box flexDirection="column">
|
|
238
|
+
{props.lines.map((line) => {
|
|
239
|
+
if (line.type === "user") {
|
|
240
|
+
return (
|
|
241
|
+
<Box key={line.key} marginBottom={1}>
|
|
242
|
+
<Text color={props.theme.brand}>› </Text>
|
|
243
|
+
<Text color={props.theme.ink}>{line.text}</Text>
|
|
244
|
+
</Box>
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
if (line.type === "assistant") {
|
|
248
|
+
return (
|
|
249
|
+
<Box key={line.key} marginBottom={1}>
|
|
250
|
+
<Text color={props.theme.ink}>{plainTerminalText(line.text)}</Text>
|
|
251
|
+
</Box>
|
|
252
|
+
);
|
|
253
|
+
}
|
|
254
|
+
if (line.type === "tool") {
|
|
255
|
+
const mark = line.status === "running" ? "…" : line.status === "ok" ? "✓" : "×";
|
|
256
|
+
const color =
|
|
257
|
+
line.status === "running"
|
|
258
|
+
? props.theme.action
|
|
259
|
+
: line.status === "ok"
|
|
260
|
+
? props.theme.done
|
|
261
|
+
: props.theme.danger;
|
|
262
|
+
return (
|
|
263
|
+
<Box key={line.key} flexDirection="column" marginBottom={1}>
|
|
264
|
+
<Text color={color}>
|
|
265
|
+
{mark} {line.name} <Text color={props.theme.muted}>{line.preview}</Text>
|
|
266
|
+
</Text>
|
|
267
|
+
{line.error ? <Text color={props.theme.danger}>{line.error}</Text> : null}
|
|
268
|
+
</Box>
|
|
269
|
+
);
|
|
270
|
+
}
|
|
271
|
+
if (line.type === "todos") {
|
|
272
|
+
return (
|
|
273
|
+
<Box key={line.key} marginBottom={1}>
|
|
274
|
+
<Text color={props.theme.muted}>todos {line.text}</Text>
|
|
275
|
+
</Box>
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
if (line.type === "error") {
|
|
279
|
+
return (
|
|
280
|
+
<Box key={line.key} marginBottom={1}>
|
|
281
|
+
<Text color={props.theme.danger}>{line.text}</Text>
|
|
282
|
+
</Box>
|
|
283
|
+
);
|
|
284
|
+
}
|
|
285
|
+
return (
|
|
286
|
+
<Box key={line.key} marginBottom={1}>
|
|
287
|
+
<Text color={props.theme.muted}>{line.text}</Text>
|
|
288
|
+
</Box>
|
|
289
|
+
);
|
|
290
|
+
})}
|
|
291
|
+
</Box>
|
|
292
|
+
);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
function PickerMenu(props: { theme: Theme; picker: PickerState }): React.ReactElement {
|
|
296
|
+
return (
|
|
297
|
+
<Box flexDirection="column" marginY={1}>
|
|
298
|
+
<Text color={props.theme.muted}>{props.picker.title}</Text>
|
|
299
|
+
{props.picker.items.map((item, index) => {
|
|
300
|
+
const active = index === props.picker.index;
|
|
301
|
+
return (
|
|
302
|
+
<Text key={item.id}>
|
|
303
|
+
<Text color={active ? props.theme.brand : props.theme.muted}>
|
|
304
|
+
{active ? "› " : " "}
|
|
305
|
+
</Text>
|
|
306
|
+
<Text color={active ? props.theme.brand : undefined} bold={active}>
|
|
307
|
+
{item.label}
|
|
308
|
+
</Text>
|
|
309
|
+
{item.hint ? <Text color={props.theme.muted}> · {item.hint}</Text> : null}
|
|
310
|
+
</Text>
|
|
311
|
+
);
|
|
312
|
+
})}
|
|
313
|
+
<Text color={props.theme.line}>tab or arrows · enter to choose · esc to cancel</Text>
|
|
314
|
+
</Box>
|
|
315
|
+
);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
function SlashMenu(props: {
|
|
319
|
+
theme: Theme;
|
|
320
|
+
items: ReturnType<typeof filterSlashCommands>;
|
|
321
|
+
selected: number;
|
|
322
|
+
}): React.ReactElement {
|
|
323
|
+
if (props.items.length === 0) {
|
|
324
|
+
return <Text color={props.theme.muted}>No matching command</Text>;
|
|
325
|
+
}
|
|
326
|
+
return (
|
|
327
|
+
<Box flexDirection="column" marginTop={1}>
|
|
328
|
+
{props.items.map((item, index) => {
|
|
329
|
+
const active = index === props.selected;
|
|
330
|
+
return (
|
|
331
|
+
<Text key={item.name}>
|
|
332
|
+
<Text color={active ? props.theme.brand : props.theme.muted}>
|
|
333
|
+
{active ? "› " : " "}
|
|
334
|
+
</Text>
|
|
335
|
+
<Text color={active ? props.theme.brand : undefined} bold={active}>
|
|
336
|
+
{formatSlashCommand(item)}
|
|
337
|
+
</Text>
|
|
338
|
+
<Text color={props.theme.muted}> · {item.hint}</Text>
|
|
339
|
+
</Text>
|
|
340
|
+
);
|
|
341
|
+
})}
|
|
342
|
+
</Box>
|
|
343
|
+
);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
function ApprovalCard(props: { theme: Theme; req: ApprovalRequest }): React.ReactElement {
|
|
347
|
+
const command =
|
|
348
|
+
typeof props.req.input.command === "string"
|
|
349
|
+
? props.req.input.command
|
|
350
|
+
: JSON.stringify(props.req.input);
|
|
351
|
+
return (
|
|
352
|
+
<Box
|
|
353
|
+
flexDirection="column"
|
|
354
|
+
marginY={1}
|
|
355
|
+
borderStyle="single"
|
|
356
|
+
borderColor={props.theme.warning}
|
|
357
|
+
paddingX={1}
|
|
358
|
+
>
|
|
359
|
+
<Text color={props.theme.warning}>Approval needed · {props.req.toolName}</Text>
|
|
360
|
+
<Text color={props.theme.ink}>{command}</Text>
|
|
361
|
+
<Text color={props.theme.muted}>y approve · n deny · a always this session</Text>
|
|
362
|
+
</Box>
|
|
363
|
+
);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
function App(props: { harness: HarnessRuntime }): React.ReactElement {
|
|
367
|
+
const { harness } = props;
|
|
368
|
+
const theme = harness.theme;
|
|
369
|
+
const { exit } = useApp();
|
|
370
|
+
const [lines, setLines] = useState<StreamLine[]>([]);
|
|
371
|
+
const [value, setValue] = useState("");
|
|
372
|
+
const [status, setStatus] = useState<UiStatus>("idle");
|
|
373
|
+
const [title, setTitle] = useState("New chat");
|
|
374
|
+
const [model, setModel] = useState(harness.modelId);
|
|
375
|
+
const [mode, setMode] = useState(harness.mode);
|
|
376
|
+
const [mediaLine, setMediaLine] = useState(() => formatMediaComposerLine(readMediaPrefs()));
|
|
377
|
+
const [spend, setSpend] = useState<TokenUsage | undefined>(harness.spend);
|
|
378
|
+
const [slashIndex, setSlashIndex] = useState(0);
|
|
379
|
+
const { columns, rows } = useTerminalSize();
|
|
380
|
+
const [sessions, setSessions] = useState<PickerItem[]>([]);
|
|
381
|
+
const [picker, setPicker] = useState<PickerState | null>(null);
|
|
382
|
+
const [pending, setPending] = useState<{
|
|
383
|
+
req: ApprovalRequest;
|
|
384
|
+
resolve: (ok: boolean) => void;
|
|
385
|
+
} | null>(null);
|
|
386
|
+
const abortRef = useRef<AbortController | null>(null);
|
|
387
|
+
|
|
388
|
+
const slashItems = useMemo(() => filterSlashCommands(value), [value]);
|
|
389
|
+
const slashOpen = status === "idle" && !picker && value.startsWith("/");
|
|
390
|
+
|
|
391
|
+
useEffect(() => {
|
|
392
|
+
setSlashIndex(0);
|
|
393
|
+
}, [value]);
|
|
394
|
+
|
|
395
|
+
useEffect(() => {
|
|
396
|
+
if (!harness.hasApiKey) return;
|
|
397
|
+
void loadLiveTargetNames(resolveOpenRouter(harness.config)).then(() => {
|
|
398
|
+
setModel((current) => current);
|
|
399
|
+
});
|
|
400
|
+
}, [harness]);
|
|
401
|
+
|
|
402
|
+
const refreshSessions = useCallback(async () => {
|
|
403
|
+
setSessions(sessionPickerItems(await harness.store.list()));
|
|
404
|
+
}, [harness]);
|
|
405
|
+
|
|
406
|
+
const refreshMedia = useCallback(() => {
|
|
407
|
+
setMediaLine(formatMediaComposerLine(readMediaPrefs()));
|
|
408
|
+
}, []);
|
|
409
|
+
|
|
410
|
+
useEffect(() => {
|
|
411
|
+
void refreshSessions();
|
|
412
|
+
}, [refreshSessions, title]);
|
|
413
|
+
|
|
414
|
+
const pushSystem = useCallback((text: string) => {
|
|
415
|
+
setLines((prev) => [
|
|
416
|
+
...prev,
|
|
417
|
+
{ key: `sys-${prev.length}-${Date.now()}`, type: "system", text },
|
|
418
|
+
]);
|
|
419
|
+
}, []);
|
|
420
|
+
|
|
421
|
+
const applySession = useCallback(
|
|
422
|
+
(session: Session) => {
|
|
423
|
+
harness.setSession(session.id);
|
|
424
|
+
setTitle(session.title);
|
|
425
|
+
if (session.model) {
|
|
426
|
+
harness.setModel(session.model);
|
|
427
|
+
setModel(session.model);
|
|
428
|
+
}
|
|
429
|
+
setLines(
|
|
430
|
+
session.messages
|
|
431
|
+
.filter((m): m is Extract<typeof m, { kind: "turn" }> => m.kind === "turn")
|
|
432
|
+
.map((m, i) =>
|
|
433
|
+
m.role === "user"
|
|
434
|
+
? { key: `u-${i}`, type: "user" as const, text: m.content }
|
|
435
|
+
: { key: `a-${i}`, type: "assistant" as const, text: m.content },
|
|
436
|
+
),
|
|
437
|
+
);
|
|
438
|
+
pushSystem(`Resumed ${session.title}`);
|
|
439
|
+
},
|
|
440
|
+
[harness, pushSystem],
|
|
441
|
+
);
|
|
442
|
+
|
|
443
|
+
const confirmPicker = useCallback(
|
|
444
|
+
(state: PickerState) => {
|
|
445
|
+
const item = state.items[state.index];
|
|
446
|
+
if (!item) return;
|
|
447
|
+
setPicker(null);
|
|
448
|
+
setStatus("idle");
|
|
449
|
+
if (state.kind === "resume") {
|
|
450
|
+
void harness.store.get(item.id).then((session) => {
|
|
451
|
+
if (!session) {
|
|
452
|
+
pushSystem(`No session ${item.label}`);
|
|
453
|
+
return;
|
|
454
|
+
}
|
|
455
|
+
applySession(session);
|
|
456
|
+
});
|
|
457
|
+
return;
|
|
458
|
+
}
|
|
459
|
+
const result = applyPickerChoice(harness, state.kind, item.id);
|
|
460
|
+
if (result.kind === "text") {
|
|
461
|
+
pushSystem(result.text);
|
|
462
|
+
}
|
|
463
|
+
setModel(harness.modelId);
|
|
464
|
+
setMode(harness.mode);
|
|
465
|
+
refreshMedia();
|
|
466
|
+
const nextDraft = draftAfterPicker(state.kind);
|
|
467
|
+
if (nextDraft) setValue(nextDraft);
|
|
468
|
+
},
|
|
469
|
+
[applySession, harness, pushSystem, refreshMedia],
|
|
470
|
+
);
|
|
471
|
+
|
|
472
|
+
useInput((input, key) => {
|
|
473
|
+
if (status === "approval" && pending) {
|
|
474
|
+
if (input === "y" || input === "Y") {
|
|
475
|
+
pending.resolve(true);
|
|
476
|
+
setPending(null);
|
|
477
|
+
setStatus("running");
|
|
478
|
+
} else if (input === "n" || input === "N") {
|
|
479
|
+
pending.resolve(false);
|
|
480
|
+
setPending(null);
|
|
481
|
+
setStatus("running");
|
|
482
|
+
} else if (input === "a" || input === "A") {
|
|
483
|
+
harness.allowRiskyAlways(pending.req.toolName);
|
|
484
|
+
pending.resolve(true);
|
|
485
|
+
setPending(null);
|
|
486
|
+
setStatus("running");
|
|
487
|
+
} else if (key.escape) {
|
|
488
|
+
pending.resolve(false);
|
|
489
|
+
setPending(null);
|
|
490
|
+
setStatus("running");
|
|
491
|
+
}
|
|
492
|
+
return;
|
|
493
|
+
}
|
|
494
|
+
if (picker) {
|
|
495
|
+
if (key.escape) {
|
|
496
|
+
const next = draftAfterPicker(picker.kind);
|
|
497
|
+
setPicker(null);
|
|
498
|
+
setStatus("idle");
|
|
499
|
+
if (next) setValue(next);
|
|
500
|
+
return;
|
|
501
|
+
}
|
|
502
|
+
if (key.upArrow || (key.tab && key.shift)) {
|
|
503
|
+
setPicker((current) =>
|
|
504
|
+
current
|
|
505
|
+
? { ...current, index: stepIndex(current.index, current.items.length, -1) }
|
|
506
|
+
: current,
|
|
507
|
+
);
|
|
508
|
+
return;
|
|
509
|
+
}
|
|
510
|
+
if (key.downArrow || key.tab) {
|
|
511
|
+
setPicker((current) =>
|
|
512
|
+
current
|
|
513
|
+
? { ...current, index: stepIndex(current.index, current.items.length, 1) }
|
|
514
|
+
: current,
|
|
515
|
+
);
|
|
516
|
+
return;
|
|
517
|
+
}
|
|
518
|
+
if (key.return) {
|
|
519
|
+
confirmPicker(picker);
|
|
520
|
+
}
|
|
521
|
+
return;
|
|
522
|
+
}
|
|
523
|
+
if (status !== "idle" || !slashOpen || slashItems.length === 0) return;
|
|
524
|
+
if (key.upArrow) {
|
|
525
|
+
setSlashIndex((index) => stepIndex(index, slashItems.length, -1));
|
|
526
|
+
return;
|
|
527
|
+
}
|
|
528
|
+
if (key.downArrow) {
|
|
529
|
+
setSlashIndex((index) => stepIndex(index, slashItems.length, 1));
|
|
530
|
+
return;
|
|
531
|
+
}
|
|
532
|
+
if (key.tab) {
|
|
533
|
+
const selected = slashItems[slashIndex] ?? slashItems[0];
|
|
534
|
+
if (selected) setValue(completeSlashCommand(selected));
|
|
535
|
+
}
|
|
536
|
+
});
|
|
537
|
+
|
|
538
|
+
const handleSlash = useCallback(
|
|
539
|
+
async (raw: string) => {
|
|
540
|
+
setStatus("running");
|
|
541
|
+
try {
|
|
542
|
+
const outcome = await dispatchSlash(harness, raw);
|
|
543
|
+
if (outcome.kind === "applied" || outcome.kind === "hold") {
|
|
544
|
+
setModel(harness.modelId);
|
|
545
|
+
setMode(harness.mode);
|
|
546
|
+
refreshMedia();
|
|
547
|
+
return;
|
|
548
|
+
}
|
|
549
|
+
if (outcome.kind === "incomplete") {
|
|
550
|
+
setValue(outcome.draft);
|
|
551
|
+
return;
|
|
552
|
+
}
|
|
553
|
+
if (outcome.kind === "text") {
|
|
554
|
+
pushSystem(outcome.text);
|
|
555
|
+
setModel(harness.modelId);
|
|
556
|
+
setMode(harness.mode);
|
|
557
|
+
return;
|
|
558
|
+
}
|
|
559
|
+
if (outcome.kind === "clear") {
|
|
560
|
+
setTitle("New chat");
|
|
561
|
+
setLines([]);
|
|
562
|
+
pushSystem("New session.");
|
|
563
|
+
void refreshSessions();
|
|
564
|
+
return;
|
|
565
|
+
}
|
|
566
|
+
if (outcome.kind === "session") {
|
|
567
|
+
applySession(outcome.session);
|
|
568
|
+
return;
|
|
569
|
+
}
|
|
570
|
+
if (outcome.kind === "exit") {
|
|
571
|
+
exit();
|
|
572
|
+
return;
|
|
573
|
+
}
|
|
574
|
+
if (outcome.kind === "media") {
|
|
575
|
+
pushSystem(outcome.caption);
|
|
576
|
+
return;
|
|
577
|
+
}
|
|
578
|
+
const prefs = readMediaPrefs();
|
|
579
|
+
const currentId =
|
|
580
|
+
outcome.picker === "model"
|
|
581
|
+
? harness.modelId
|
|
582
|
+
: outcome.picker === "mode"
|
|
583
|
+
? harness.mode
|
|
584
|
+
: outcome.picker === "image"
|
|
585
|
+
? prefs.imageModel
|
|
586
|
+
: outcome.picker === "video"
|
|
587
|
+
? prefs.videoModel
|
|
588
|
+
: outcome.picker === "transcribe"
|
|
589
|
+
? prefs.transcribeModel
|
|
590
|
+
: undefined;
|
|
591
|
+
const selected = currentId
|
|
592
|
+
? Math.max(
|
|
593
|
+
0,
|
|
594
|
+
outcome.items.findIndex((item) => item.id === currentId),
|
|
595
|
+
)
|
|
596
|
+
: 0;
|
|
597
|
+
setPicker({
|
|
598
|
+
title: outcome.title,
|
|
599
|
+
kind: outcome.picker,
|
|
600
|
+
items: outcome.items,
|
|
601
|
+
index: selected,
|
|
602
|
+
});
|
|
603
|
+
setStatus("picker");
|
|
604
|
+
} finally {
|
|
605
|
+
setStatus((current) => (current === "picker" ? current : "idle"));
|
|
606
|
+
}
|
|
607
|
+
},
|
|
608
|
+
[applySession, exit, harness, pushSystem, refreshMedia, refreshSessions],
|
|
609
|
+
);
|
|
610
|
+
|
|
611
|
+
const submit = useCallback(
|
|
612
|
+
async (text: string) => {
|
|
613
|
+
const trimmed = text.trim();
|
|
614
|
+
if (!trimmed || status !== "idle") return;
|
|
615
|
+
const slash = resolveSlashSubmit(trimmed);
|
|
616
|
+
if (slash?.action === "hold") return;
|
|
617
|
+
if (slash?.action === "complete") {
|
|
618
|
+
setValue(slash.line);
|
|
619
|
+
return;
|
|
620
|
+
}
|
|
621
|
+
setValue("");
|
|
622
|
+
if (slash?.action === "send") {
|
|
623
|
+
await handleSlash(slash.line);
|
|
624
|
+
return;
|
|
625
|
+
}
|
|
626
|
+
setLines((prev) => [...prev, { key: `u-${prev.length}`, type: "user", text: trimmed }]);
|
|
627
|
+
setStatus("running");
|
|
628
|
+
const controller = new AbortController();
|
|
629
|
+
abortRef.current = controller;
|
|
630
|
+
try {
|
|
631
|
+
await harness.runTurn(
|
|
632
|
+
trimmed,
|
|
633
|
+
(event) => {
|
|
634
|
+
if (event.kind === "session_meta") setTitle(event.title);
|
|
635
|
+
if (event.kind === "usage") setSpend(harness.spend);
|
|
636
|
+
setLines((prev) => applyEvent(prev, event));
|
|
637
|
+
},
|
|
638
|
+
{
|
|
639
|
+
signal: controller.signal,
|
|
640
|
+
approvalAsk: (req) =>
|
|
641
|
+
new Promise<boolean>((resolve) => {
|
|
642
|
+
setPending({ req, resolve });
|
|
643
|
+
setStatus("approval");
|
|
644
|
+
}),
|
|
645
|
+
},
|
|
646
|
+
);
|
|
647
|
+
} finally {
|
|
648
|
+
abortRef.current = null;
|
|
649
|
+
setPending(null);
|
|
650
|
+
setStatus("idle");
|
|
651
|
+
}
|
|
652
|
+
},
|
|
653
|
+
[handleSlash, harness, status],
|
|
654
|
+
);
|
|
655
|
+
|
|
656
|
+
const statusLabel = useMemo(() => {
|
|
657
|
+
if (status === "running") return "running";
|
|
658
|
+
if (status === "approval") return "awaiting approval";
|
|
659
|
+
if (status === "picker") return "";
|
|
660
|
+
return "";
|
|
661
|
+
}, [status]);
|
|
662
|
+
|
|
663
|
+
const rail = sidebarWidth(columns);
|
|
664
|
+
const chrome = tuiChromeRows({
|
|
665
|
+
approval: Boolean(pending),
|
|
666
|
+
pickerCount: picker?.items.length ?? 0,
|
|
667
|
+
slashCount: slashOpen ? slashItems.length : 0,
|
|
668
|
+
});
|
|
669
|
+
const shown = takeVisibleLines(lines, visibleTranscriptCount(rows, chrome));
|
|
670
|
+
const mainWidth = rail ? Math.max(40, columns - rail) : columns;
|
|
671
|
+
|
|
672
|
+
const main = (
|
|
673
|
+
<Box flexDirection="column" width={mainWidth} height={rows}>
|
|
674
|
+
<Header
|
|
675
|
+
theme={theme}
|
|
676
|
+
name={harness.config.name}
|
|
677
|
+
model={model}
|
|
678
|
+
cwd={harness.cwd}
|
|
679
|
+
title={title}
|
|
680
|
+
status={status}
|
|
681
|
+
mode={mode}
|
|
682
|
+
spend={spend ? formatUsage(spend) : undefined}
|
|
683
|
+
/>
|
|
684
|
+
<Box flexDirection="column" flexGrow={1} overflow="hidden">
|
|
685
|
+
<StreamView theme={theme} lines={shown} />
|
|
686
|
+
</Box>
|
|
687
|
+
{pending ? <ApprovalCard theme={theme} req={pending.req} /> : null}
|
|
688
|
+
{picker ? <PickerMenu theme={theme} picker={picker} /> : null}
|
|
689
|
+
{status === "idle" ? (
|
|
690
|
+
<Box flexDirection="column">
|
|
691
|
+
<Box>
|
|
692
|
+
<Text color={theme.brand}>› </Text>
|
|
693
|
+
<TextInput value={value} onChange={setValue} onSubmit={submit} />
|
|
694
|
+
</Box>
|
|
695
|
+
{slashOpen ? (
|
|
696
|
+
<SlashMenu
|
|
697
|
+
theme={theme}
|
|
698
|
+
items={slashItems}
|
|
699
|
+
selected={Math.min(slashIndex, Math.max(slashItems.length - 1, 0))}
|
|
700
|
+
/>
|
|
701
|
+
) : (
|
|
702
|
+
<Text color={theme.line}>{mediaLine}</Text>
|
|
703
|
+
)}
|
|
704
|
+
</Box>
|
|
705
|
+
) : status === "picker" ? null : (
|
|
706
|
+
<Text color={theme.muted}>{statusLabel}</Text>
|
|
707
|
+
)}
|
|
708
|
+
</Box>
|
|
709
|
+
);
|
|
710
|
+
|
|
711
|
+
if (!rail) return main;
|
|
712
|
+
return (
|
|
713
|
+
<Box width={columns} height={rows} flexDirection="row">
|
|
714
|
+
<Sidebar
|
|
715
|
+
theme={theme}
|
|
716
|
+
width={rail}
|
|
717
|
+
height={rows}
|
|
718
|
+
sessions={sessions}
|
|
719
|
+
currentId={harness.activeSessionId}
|
|
720
|
+
/>
|
|
721
|
+
{main}
|
|
722
|
+
</Box>
|
|
723
|
+
);
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
export async function startTui(harness: HarnessRuntime): Promise<void> {
|
|
727
|
+
const restore = enterAltScreen();
|
|
728
|
+
try {
|
|
729
|
+
const instance = render(<App harness={harness} />, { patchConsole: true });
|
|
730
|
+
await instance.waitUntilExit();
|
|
731
|
+
} finally {
|
|
732
|
+
restore();
|
|
733
|
+
harness.close();
|
|
734
|
+
}
|
|
735
|
+
}
|