@akanjs/code 0.0.0-stage → 3.3.0-rc.70
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.ko.md +31 -0
- package/README.md +30 -2
- package/agent/AkanCodeCompaction.ts +49 -0
- package/agent/AkanCodeServices.ts +122 -0
- package/agent/AkanContextFiles.ts +94 -0
- package/agent/CodeAgent.ts +598 -0
- package/agent/CodeAgentAllowed.ts +35 -0
- package/agent/CodeAgentAsks.ts +66 -0
- package/agent/CodeAgentCommandLine.ts +372 -0
- package/agent/CodeAgentEventMapper.ts +225 -0
- package/agent/CodeAgentFrames.ts +57 -0
- package/agent/CodeAgentGate.ts +258 -0
- package/agent/CodeAgentProxy.ts +83 -0
- package/agent/CodeAgentRpcHost.ts +109 -0
- package/agent/CodeAgentRpcListener.ts +195 -0
- package/agent/CodeAgentStreamPrinter.ts +86 -0
- package/agent/CodeAgentSuspended.ts +85 -0
- package/agent/CodeAgentUi.ts +47 -0
- package/agent/CodeMailbox.ts +148 -0
- package/agent/CodeSessionFork.ts +56 -0
- package/agent/CodeSessionIndex.ts +78 -0
- package/agent/SubagentPool.ts +222 -0
- package/agent/WorkspaceTrust.ts +200 -0
- package/agent/akanCodeCompaction.test.ts +36 -0
- package/agent/akanCodeModel.ts +47 -0
- package/agent/akanCodeServices.test.ts +54 -0
- package/agent/akanContextFiles.test.ts +116 -0
- package/agent/akanSystemPrompt.ts +50 -0
- package/agent/codeAgent.test.ts +337 -0
- package/agent/codeAgentCommandLine.test.ts +212 -0
- package/agent/codeAgentContract.test.ts +757 -0
- package/agent/codeAgentHosts.ts +30 -0
- package/agent/codeAgentProxy.test.ts +125 -0
- package/agent/codeAgentRpcListener.test.ts +215 -0
- package/agent/codeAgentSuspended.test.ts +47 -0
- package/agent/codeMailbox.test.ts +76 -0
- package/agent/codeSessionFork.test.ts +58 -0
- package/agent/codeSessionIndex.test.ts +77 -0
- package/agent/consoleToStderr.ts +8 -0
- package/agent/jsonLines.ts +29 -0
- package/agent/stringArg.ts +5 -0
- package/bunfig.toml +2 -0
- package/feedback/DevLogFeedback.ts +82 -0
- package/feedback/PreviewFeedback.ts +46 -0
- package/feedback/TurnFeedback.ts +98 -0
- package/feedback/VerifyFeedback.ts +39 -0
- package/host/AkanCodeRunner.ts +297 -0
- package/host/CodeTrustPrompt.ts +57 -0
- package/host/codeRunnerPrompt.test.ts +181 -0
- package/host/hostGraph.test.ts +33 -0
- package/host/index.ts +2 -0
- package/index.ts +49 -0
- package/package.json +50 -4
- package/piDependencyPins.test.ts +45 -0
- package/test/preload.ts +3 -0
- package/tools/AkanCli.ts +56 -0
- package/tools/AkanCodePlugins.ts +145 -0
- package/tools/AkanEditScope.ts +53 -0
- package/tools/AkanEnvKeys.ts +50 -0
- package/tools/AkanToolPack.ts +87 -0
- package/tools/AkanVerifier.ts +30 -0
- package/tools/AskToolPack.ts +99 -0
- package/tools/DeepSeekWeb.ts +127 -0
- package/tools/MailToolPack.ts +76 -0
- package/tools/McpClient.ts +209 -0
- package/tools/McpOAuth.ts +233 -0
- package/tools/McpOAuthLoopback.ts +64 -0
- package/tools/McpServerConfig.ts +131 -0
- package/tools/McpSignIn.ts +158 -0
- package/tools/McpTokenStore.ts +128 -0
- package/tools/McpToolPack.ts +206 -0
- package/tools/SessionToolPack.ts +91 -0
- package/tools/WebToolPack.ts +315 -0
- package/tools/akanCodePlugins.test.ts +954 -0
- package/tools/askToolPack.test.ts +116 -0
- package/tools/deepSeekWeb.test.ts +113 -0
- package/tools/mcpOAuth.test.ts +648 -0
- package/tools/mcpServerConfig.test.ts +341 -0
- package/tsconfig.json +29 -0
- package/tui/CodeTui.tsx +394 -0
- package/tui/CodeTuiAgents.ts +65 -0
- package/tui/CodeTuiApp.test.tsx +251 -0
- package/tui/CodeTuiApp.tsx +312 -0
- package/tui/CodeTuiAutoCompact.ts +33 -0
- package/tui/CodeTuiClipboard.ts +94 -0
- package/tui/CodeTuiCommandRunner.ts +242 -0
- package/tui/CodeTuiCommands.ts +59 -0
- package/tui/CodeTuiEditor.ts +168 -0
- package/tui/CodeTuiFiles.ts +45 -0
- package/tui/CodeTuiFrame.ts +133 -0
- package/tui/CodeTuiLines.ts +117 -0
- package/tui/CodeTuiMarkdown.ts +159 -0
- package/tui/CodeTuiMcp.ts +111 -0
- package/tui/CodeTuiModels.ts +82 -0
- package/tui/CodeTuiMouse.ts +39 -0
- package/tui/CodeTuiOverlay.ts +148 -0
- package/tui/CodeTuiParts.ts +168 -0
- package/tui/CodeTuiPrompt.ts +271 -0
- package/tui/CodeTuiQuestion.ts +170 -0
- package/tui/codeTranscript.test.ts +445 -0
- package/tui/codeTui.test.tsx +1104 -0
- package/tui/codeTuiAgents.test.ts +61 -0
- package/tui/codeTuiClipboard.test.ts +16 -0
- package/tui/codeTuiCommands.test.ts +38 -0
- package/tui/codeTuiEditor.test.ts +122 -0
- package/tui/codeTuiFiles.test.ts +43 -0
- package/tui/codeTuiMarkdown.test.ts +91 -0
- package/tui/codeTuiMcp.test.ts +160 -0
- package/tui/codeTuiMouse.test.ts +61 -0
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { type CodeTranscriptPart, codeAgentClip } from "akanjs/common";
|
|
2
|
+
import { type CodeTuiLine, CodeTuiLines, type CodeTuiRow, type CodeTuiSpan } from "./CodeTuiLines";
|
|
3
|
+
import { CodeTuiMarkdown } from "./CodeTuiMarkdown";
|
|
4
|
+
|
|
5
|
+
export interface CodeTuiPartsOptions {
|
|
6
|
+
/** Whether reasoning is drawn in full. Folded to one line otherwise. */
|
|
7
|
+
thinking?: boolean;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export class CodeTuiParts {
|
|
11
|
+
static readonly thinkingLines = 6;
|
|
12
|
+
/** Lines shown of a failed or refused call's output; a successful call's output is never shown. */
|
|
13
|
+
static readonly outputLines = 4;
|
|
14
|
+
|
|
15
|
+
static lines(parts: readonly CodeTranscriptPart[], width: number, options: CodeTuiPartsOptions = {}): CodeTuiLine[] {
|
|
16
|
+
const lines: CodeTuiLine[] = [];
|
|
17
|
+
parts.forEach((part, at) => {
|
|
18
|
+
const rows = CodeTuiParts.rows(part, width, options);
|
|
19
|
+
for (const line of CodeTuiLines.rows(rows, width, `${part.id}:${at}`)) lines.push(line);
|
|
20
|
+
});
|
|
21
|
+
return lines;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
static rows(part: CodeTranscriptPart, width = 80, options: CodeTuiPartsOptions = {}): CodeTuiRow[] {
|
|
25
|
+
switch (part.kind) {
|
|
26
|
+
case "user":
|
|
27
|
+
return [
|
|
28
|
+
{ prefix: { text: "› ", color: "cyan", bold: true }, spans: [{ text: part.text, color: "cyan" }] },
|
|
29
|
+
...(part.images
|
|
30
|
+
? [
|
|
31
|
+
{
|
|
32
|
+
prefix: { text: " ", dim: true },
|
|
33
|
+
spans: [{ text: `⧉ ${part.images} image${part.images > 1 ? "s" : ""}`, dim: true }],
|
|
34
|
+
},
|
|
35
|
+
]
|
|
36
|
+
: []),
|
|
37
|
+
];
|
|
38
|
+
case "assistant":
|
|
39
|
+
return CodeTuiParts.#assistant(part, width);
|
|
40
|
+
case "thinking":
|
|
41
|
+
return options.thinking ? CodeTuiParts.#thinking(part.text) : CodeTuiParts.#folded(part.text);
|
|
42
|
+
case "tool":
|
|
43
|
+
return CodeTuiParts.#tool(part);
|
|
44
|
+
case "notice":
|
|
45
|
+
return part.text.split("\n").map((line, at) => ({
|
|
46
|
+
prefix: { text: at === 0 && part.level !== "info" ? "! " : " ", ...CodeTuiParts.#noticeStyle(part.level) },
|
|
47
|
+
spans: CodeTuiMarkdown.spans(line, CodeTuiParts.#noticeStyle(part.level)),
|
|
48
|
+
}));
|
|
49
|
+
case "question":
|
|
50
|
+
return CodeTuiParts.#question(part);
|
|
51
|
+
case "approval":
|
|
52
|
+
return [
|
|
53
|
+
{ prefix: { text: "? ", color: "yellow" }, spans: [{ text: part.request.summary, color: "yellow" }] },
|
|
54
|
+
...(part.approved === undefined
|
|
55
|
+
? []
|
|
56
|
+
: [
|
|
57
|
+
{
|
|
58
|
+
prefix: { text: " " },
|
|
59
|
+
spans: [{ text: CodeTuiParts.#verdict(part.approved, part.remembered), dim: true }],
|
|
60
|
+
},
|
|
61
|
+
]),
|
|
62
|
+
];
|
|
63
|
+
case "host":
|
|
64
|
+
return [{ prefix: { text: " ", dim: true }, spans: [{ text: `${part.hostKind} ${part.text}`, dim: true }] }];
|
|
65
|
+
default:
|
|
66
|
+
return [];
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
static #assistant(part: Extract<CodeTranscriptPart, { kind: "assistant" }>, width: number): CodeTuiRow[] {
|
|
71
|
+
// Safe to render mid-stream: an unterminated fence already scans as `code`, so no block changes kind later.
|
|
72
|
+
const rows = CodeTuiMarkdown.rows(part.text, width);
|
|
73
|
+
if (!part.truncated) return rows;
|
|
74
|
+
return [
|
|
75
|
+
...rows,
|
|
76
|
+
{ spans: [{ text: "… the answer stopped at the model's output limit — ask it to continue", color: "yellow" }] },
|
|
77
|
+
];
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Folded to one marked line, not dropped: a turn opening with a long think would otherwise be a blank screen.
|
|
81
|
+
static #folded(text: string): CodeTuiRow[] {
|
|
82
|
+
const count = text.split("\n").filter((line) => line.trim()).length;
|
|
83
|
+
return [
|
|
84
|
+
{
|
|
85
|
+
prefix: { text: "✻ ", dim: true },
|
|
86
|
+
spans: [{ text: `thinking · ${count} line${count === 1 ? "" : "s"} · /thinking to show`, dim: true }],
|
|
87
|
+
},
|
|
88
|
+
];
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
static #thinking(text: string): CodeTuiRow[] {
|
|
92
|
+
const all = text.split("\n").filter((line) => line.trim());
|
|
93
|
+
const shown = all.slice(-CodeTuiParts.thinkingLines);
|
|
94
|
+
const hidden = all.length - shown.length;
|
|
95
|
+
return [
|
|
96
|
+
...(hidden
|
|
97
|
+
? [{ prefix: { text: "· ", dim: true }, spans: [{ text: `(${hidden} earlier lines)`, dim: true }] }]
|
|
98
|
+
: []),
|
|
99
|
+
...shown.map((line) => ({
|
|
100
|
+
prefix: { text: "· ", dim: true },
|
|
101
|
+
spans: [{ text: line, dim: true, italic: true }] satisfies CodeTuiSpan[],
|
|
102
|
+
})),
|
|
103
|
+
];
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
static #tool(part: Extract<CodeTranscriptPart, { kind: "tool" }>): CodeTuiRow[] {
|
|
107
|
+
const running = part.outcome === undefined;
|
|
108
|
+
const mark = running ? "◐" : part.outcome === "ok" ? "⏺" : part.outcome === "blocked" ? "⦸" : "⏺";
|
|
109
|
+
const color = running ? "cyan" : part.outcome === "ok" ? "green" : part.outcome === "blocked" ? "yellow" : "red";
|
|
110
|
+
const head: CodeTuiRow = {
|
|
111
|
+
prefix: { text: `${mark} `, color },
|
|
112
|
+
hang: " ",
|
|
113
|
+
spans: CodeTuiParts.#title(part.tool.name, part.tool.title),
|
|
114
|
+
};
|
|
115
|
+
const detail = CodeTuiParts.#detail(part);
|
|
116
|
+
if (!detail.length) return [head];
|
|
117
|
+
return [
|
|
118
|
+
head,
|
|
119
|
+
...detail.map((line, at) => ({
|
|
120
|
+
prefix: { text: at === 0 ? " ⎿ " : " ", dim: true },
|
|
121
|
+
hang: " ",
|
|
122
|
+
spans: [{ text: line, dim: true }] satisfies CodeTuiSpan[],
|
|
123
|
+
})),
|
|
124
|
+
];
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
static #title(name: string, title: string): CodeTuiSpan[] {
|
|
128
|
+
if (!title.startsWith(name)) return [{ text: title, bold: true }];
|
|
129
|
+
return [
|
|
130
|
+
{ text: name, bold: true },
|
|
131
|
+
{ text: title.slice(name.length), dim: true },
|
|
132
|
+
];
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
static #detail(part: Extract<CodeTranscriptPart, { kind: "tool" }>) {
|
|
136
|
+
if (part.outcome === undefined) return part.progress ? [part.progress] : [];
|
|
137
|
+
if (part.outcome === "ok") return [];
|
|
138
|
+
return (part.output ?? "")
|
|
139
|
+
.split("\n")
|
|
140
|
+
.filter((line) => line.trim())
|
|
141
|
+
.slice(0, CodeTuiParts.outputLines)
|
|
142
|
+
.map((line) => codeAgentClip(line.trim(), 200));
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
static #question(part: Extract<CodeTranscriptPart, { kind: "question" }>): CodeTuiRow[] {
|
|
146
|
+
return [
|
|
147
|
+
{ prefix: { text: "? ", color: "yellow" }, spans: [{ text: part.question.prompt, color: "yellow" }] },
|
|
148
|
+
...(part.question.options ?? []).map((option) => ({
|
|
149
|
+
prefix: { text: " · ", dim: true },
|
|
150
|
+
spans: [{ text: `${option.label}${option.recommended ? " (recommended)" : ""}`, dim: true }],
|
|
151
|
+
})),
|
|
152
|
+
...(part.rendered === undefined
|
|
153
|
+
? []
|
|
154
|
+
: [{ prefix: { text: " = ", color: "green" }, spans: [{ text: part.rendered, color: "green" }] }]),
|
|
155
|
+
];
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
static #verdict(approved: boolean, remembered: string[] | undefined) {
|
|
159
|
+
if (!approved) return "denied";
|
|
160
|
+
return remembered?.length ? `approved · ${remembered.join(", ")} runs unasked until this session ends` : "approved";
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
static #noticeStyle(level: "info" | "warning" | "error") {
|
|
164
|
+
if (level === "error") return { color: "red" };
|
|
165
|
+
if (level === "warning") return { color: "yellow" };
|
|
166
|
+
return { dim: true };
|
|
167
|
+
}
|
|
168
|
+
}
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
import type { CodeTranscript } from "akanjs/common";
|
|
2
|
+
import type { CodeAgent } from "../agent/CodeAgent";
|
|
3
|
+
import { type CodeTuiEditKey, type CodeTuiOption, type CodeTuiSnapshot, promptMarkWidth } from "./CodeTuiApp";
|
|
4
|
+
import { CodeTuiClipboard } from "./CodeTuiClipboard";
|
|
5
|
+
import { CodeTuiCommands } from "./CodeTuiCommands";
|
|
6
|
+
import type { CodeTuiEditor } from "./CodeTuiEditor";
|
|
7
|
+
import { CodeTuiFiles } from "./CodeTuiFiles";
|
|
8
|
+
import { type CodeTuiAskBlock, CodeTuiFrame } from "./CodeTuiFrame";
|
|
9
|
+
import type { CodeTuiOverlay } from "./CodeTuiOverlay";
|
|
10
|
+
|
|
11
|
+
interface CodeTuiMenu {
|
|
12
|
+
kind: "command" | "file";
|
|
13
|
+
from: number;
|
|
14
|
+
to: number;
|
|
15
|
+
options: CodeTuiOption[];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface CodeTuiPromptHost {
|
|
19
|
+
render: () => void;
|
|
20
|
+
snapshot: () => CodeTuiSnapshot;
|
|
21
|
+
say: (text: string) => void;
|
|
22
|
+
fail: (error: unknown) => void;
|
|
23
|
+
command: (line: string) => void;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export class CodeTuiPrompt {
|
|
27
|
+
static readonly clipboardMs = 2_000;
|
|
28
|
+
static readonly imageMark = /\[image #(\d+)] ?/g;
|
|
29
|
+
|
|
30
|
+
readonly #agent: CodeAgent;
|
|
31
|
+
readonly #transcript: CodeTranscript;
|
|
32
|
+
readonly #editor: CodeTuiEditor;
|
|
33
|
+
readonly #overlay: CodeTuiOverlay;
|
|
34
|
+
readonly #host: CodeTuiPromptHost;
|
|
35
|
+
readonly #files: CodeTuiFiles;
|
|
36
|
+
readonly #history: string[] = [];
|
|
37
|
+
#historyAt = -1;
|
|
38
|
+
#menuAt = 0;
|
|
39
|
+
#images: string[] = [];
|
|
40
|
+
#offered = false;
|
|
41
|
+
#clipboardTimer: ReturnType<typeof setInterval> | null = null;
|
|
42
|
+
|
|
43
|
+
constructor(
|
|
44
|
+
agent: CodeAgent,
|
|
45
|
+
transcript: CodeTranscript,
|
|
46
|
+
editor: CodeTuiEditor,
|
|
47
|
+
overlay: CodeTuiOverlay,
|
|
48
|
+
host: CodeTuiPromptHost,
|
|
49
|
+
) {
|
|
50
|
+
this.#agent = agent;
|
|
51
|
+
this.#transcript = transcript;
|
|
52
|
+
this.#editor = editor;
|
|
53
|
+
this.#overlay = overlay;
|
|
54
|
+
this.#host = host;
|
|
55
|
+
this.#files = new CodeTuiFiles(agent.profile.paths.root);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
get offered() {
|
|
59
|
+
return this.#offered;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
load() {
|
|
63
|
+
return this.#files.load();
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
watchClipboard() {
|
|
67
|
+
if (!this.#agent.canSeeImages) return;
|
|
68
|
+
this.#clipboardTimer = setInterval(() => {
|
|
69
|
+
const offered = CodeTuiClipboard.has();
|
|
70
|
+
if (offered === this.#offered) return;
|
|
71
|
+
this.#offered = offered;
|
|
72
|
+
this.#host.render();
|
|
73
|
+
}, CodeTuiPrompt.clipboardMs);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
unwatchClipboard() {
|
|
77
|
+
if (this.#clipboardTimer) clearInterval(this.#clipboardTimer);
|
|
78
|
+
this.#clipboardTimer = null;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
block(columns: number, frameRows: number): CodeTuiAskBlock {
|
|
82
|
+
const all = this.#menu()?.options ?? [];
|
|
83
|
+
const { rows, row, col } = this.#editor.layout(columns - promptMarkWidth - 1);
|
|
84
|
+
const offered = this.#offered ? 1 : 0;
|
|
85
|
+
const room =
|
|
86
|
+
frameRows - CodeTuiFrame.minBodyRows - CodeTuiFrame.chromeRows - 2 * CodeTuiFrame.promptRuleRows - offered;
|
|
87
|
+
const menuRows = Math.max(0, Math.min(all.length, room - 1));
|
|
88
|
+
const menuFrom = Math.max(0, Math.min(this.#menuAt - Math.floor(menuRows / 2), all.length - menuRows));
|
|
89
|
+
const menu = all.slice(menuFrom, menuFrom + menuRows);
|
|
90
|
+
const above = menu.length + CodeTuiFrame.promptRuleRows + offered;
|
|
91
|
+
const chrome = above + CodeTuiFrame.promptRuleRows;
|
|
92
|
+
const budget = Math.max(1, Math.min(CodeTuiFrame.maxInputRows, room - menu.length));
|
|
93
|
+
const first = Math.max(0, Math.min(row - budget + 1, rows.length - budget));
|
|
94
|
+
const shown = rows.slice(first, first + budget);
|
|
95
|
+
return {
|
|
96
|
+
rows: chrome + shown.length,
|
|
97
|
+
above,
|
|
98
|
+
selected: 0,
|
|
99
|
+
caret: { row: row - first, col },
|
|
100
|
+
snapshot: {
|
|
101
|
+
input: shown.map((entry) => entry.text),
|
|
102
|
+
menu,
|
|
103
|
+
menuSelected: this.#menuAt - menuFrom,
|
|
104
|
+
options: [] as CodeTuiOption[],
|
|
105
|
+
},
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
#menu(): CodeTuiMenu | null {
|
|
110
|
+
const prefix = CodeTuiCommands.prefixOf(this.#editor.text, this.#editor.caret);
|
|
111
|
+
if (prefix !== undefined)
|
|
112
|
+
return {
|
|
113
|
+
kind: "command",
|
|
114
|
+
from: 0,
|
|
115
|
+
to: this.#editor.text.length,
|
|
116
|
+
options: CodeTuiCommands.matches(prefix).map((command) => ({
|
|
117
|
+
key: command.name,
|
|
118
|
+
label: `/${command.name}${command.arg ? ` ${command.arg}` : ""}`,
|
|
119
|
+
detail: command.desc,
|
|
120
|
+
})),
|
|
121
|
+
};
|
|
122
|
+
const token = this.#editor.token();
|
|
123
|
+
if (!token.text.startsWith("@")) return null;
|
|
124
|
+
const options = this.#files.match(token.text.slice(1)).map((file) => ({ key: file, label: `@${file}` }));
|
|
125
|
+
if (!options.length && this.#files.ready) return null;
|
|
126
|
+
return { kind: "file", from: token.from, to: token.to, options };
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
type = (text: string) => {
|
|
130
|
+
const pasted = this.#agent.canSeeImages ? CodeTuiClipboard.imagePathsIn(text) : undefined;
|
|
131
|
+
if (pasted?.images.length) {
|
|
132
|
+
for (const file of pasted.images) this.#attach(file);
|
|
133
|
+
if (pasted.rest) this.#editor.insert(pasted.rest);
|
|
134
|
+
} else this.#editor.insert(text);
|
|
135
|
+
this.#menuAt = 0;
|
|
136
|
+
this.#historyAt = -1;
|
|
137
|
+
this.#host.render();
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
newline = () => {
|
|
141
|
+
this.#editor.newline();
|
|
142
|
+
this.#historyAt = -1;
|
|
143
|
+
this.#host.render();
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
edit = (action: CodeTuiEditKey) => {
|
|
147
|
+
const width = this.#host.snapshot().columns - promptMarkWidth - 1;
|
|
148
|
+
const run: { [key in CodeTuiEditKey]: () => void } = {
|
|
149
|
+
backspace: () => this.#editor.backspace(),
|
|
150
|
+
delete: () => this.#editor.deleteForward(),
|
|
151
|
+
left: () => (this.#editor.caret ? this.#editor.left() : this.#overlay.openSessions()),
|
|
152
|
+
right: () => this.#editor.right(),
|
|
153
|
+
wordLeft: () => this.#editor.wordLeft(),
|
|
154
|
+
wordRight: () => this.#editor.wordRight(),
|
|
155
|
+
home: () => this.#editor.homeOfRow(width),
|
|
156
|
+
end: () => this.#editor.endOfRow(width),
|
|
157
|
+
deleteWord: () => this.#editor.deleteWord(),
|
|
158
|
+
deleteToStart: () => this.#editor.deleteToLineStart(),
|
|
159
|
+
};
|
|
160
|
+
run[action]();
|
|
161
|
+
// A caret move is not an edit, so it leaves a recalled entry recalled and the arrows on history.
|
|
162
|
+
if (action.startsWith("delete") || action === "backspace") this.#historyAt = -1;
|
|
163
|
+
this.#host.render();
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
vertical(delta: -1 | 1) {
|
|
167
|
+
const menu = this.#menu()?.options ?? [];
|
|
168
|
+
if (menu.length) {
|
|
169
|
+
this.#menuAt = (this.#menuAt + delta + menu.length) % menu.length;
|
|
170
|
+
return this.#host.render();
|
|
171
|
+
}
|
|
172
|
+
// While a recalled entry is shown the arrows stay on history; a wrapped entry would otherwise swallow them.
|
|
173
|
+
if (this.#historyAt < 0 && this.#editor.move(delta, this.#host.snapshot().columns - promptMarkWidth - 1))
|
|
174
|
+
return this.#host.render();
|
|
175
|
+
if (delta < 0) return this.#historyPrev();
|
|
176
|
+
return this.#historyNext();
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
complete = () => {
|
|
180
|
+
const menu = this.#menu();
|
|
181
|
+
const chosen = menu?.options[this.#menuAt];
|
|
182
|
+
if (!menu || !chosen) return;
|
|
183
|
+
const command = CodeTuiCommands.all.find((entry) => entry.name === chosen.key);
|
|
184
|
+
const text = menu.kind === "command" ? `/${chosen.key}${command?.arg ? " " : ""}` : `@${chosen.key} `;
|
|
185
|
+
this.#editor.replace(menu.from, menu.to, text);
|
|
186
|
+
this.#menuAt = 0;
|
|
187
|
+
this.#host.render();
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
#historyPrev() {
|
|
191
|
+
if (!this.#history.length) return;
|
|
192
|
+
this.#historyAt = this.#historyAt < 0 ? this.#history.length - 1 : Math.max(0, this.#historyAt - 1);
|
|
193
|
+
this.#editor.set(this.#history[this.#historyAt] ?? "");
|
|
194
|
+
this.#host.render();
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
#historyNext() {
|
|
198
|
+
if (this.#historyAt < 0) return;
|
|
199
|
+
this.#historyAt += 1;
|
|
200
|
+
if (this.#historyAt >= this.#history.length) {
|
|
201
|
+
this.#historyAt = -1;
|
|
202
|
+
this.#editor.clear();
|
|
203
|
+
} else this.#editor.set(this.#history[this.#historyAt] ?? "");
|
|
204
|
+
this.#host.render();
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
submit() {
|
|
208
|
+
const menu = this.#menu();
|
|
209
|
+
if (menu?.kind === "file") return this.complete();
|
|
210
|
+
const chosen = menu?.options[this.#menuAt];
|
|
211
|
+
const raw = chosen ? `/${chosen.key}` : this.#editor.text;
|
|
212
|
+
const pending = this.#pending(raw);
|
|
213
|
+
if (!pending.text && !pending.images.length) return;
|
|
214
|
+
this.#editor.clear();
|
|
215
|
+
this.#historyAt = -1;
|
|
216
|
+
if (pending.text) this.#history.push(pending.text);
|
|
217
|
+
if (pending.text.startsWith("/")) return this.#host.command(pending.text);
|
|
218
|
+
this.send(pending.text, pending.images);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
send(text: string, images: { path: string }[] = []) {
|
|
222
|
+
this.#images = [];
|
|
223
|
+
this.#transcript.echo(text, images.length);
|
|
224
|
+
this.#host.render();
|
|
225
|
+
void this.#agent.prompt(text, images).catch((error: unknown) => this.#host.fail(error));
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// A terminal paste carries only text, so an image has to be read off the OS clipboard.
|
|
229
|
+
paste = () => {
|
|
230
|
+
if (!this.#agent.canSeeImages)
|
|
231
|
+
return this.#overlay.open("images", CodeTuiPrompt.#imageHelp(this.#transcript.info?.model));
|
|
232
|
+
void CodeTuiClipboard.image()
|
|
233
|
+
.then((file) => (file ? this.#attach(file) : this.#host.say("nothing on the clipboard we can attach")))
|
|
234
|
+
.catch((error: unknown) => this.#host.fail(error));
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
pasted = (text: string) => {
|
|
238
|
+
// A terminal sends a newline inside a paste as a carriage return; the prompt stores newlines.
|
|
239
|
+
if (text) return this.type(text.replace(/\r\n?/g, "\n"));
|
|
240
|
+
if (CodeTuiClipboard.has()) this.paste();
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
// The marker is the attachment: deleting it from the prompt before enter un-attaches the image.
|
|
244
|
+
#attach(file: string) {
|
|
245
|
+
this.#images.push(file);
|
|
246
|
+
this.#editor.insert(`[image #${this.#images.length}] `);
|
|
247
|
+
this.#host.render();
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
#pending(text: string) {
|
|
251
|
+
const marks = [...text.matchAll(CodeTuiPrompt.imageMark)].map((match) => Number(match[1]));
|
|
252
|
+
const images = marks
|
|
253
|
+
.map((mark) => this.#images[mark - 1])
|
|
254
|
+
.filter((file): file is string => !!file)
|
|
255
|
+
.map((path) => ({ path }));
|
|
256
|
+
// Only the marker's own trailing space goes; collapsing spaces would damage a pasted stack trace.
|
|
257
|
+
return { images, text: text.replace(CodeTuiPrompt.imageMark, "").trim() };
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
static #imageHelp(model: { provider: string; id: string } | undefined) {
|
|
261
|
+
const provider = model?.provider ?? "<provider>";
|
|
262
|
+
const id = model?.id ?? "<model>";
|
|
263
|
+
return [
|
|
264
|
+
`The model catalogue declares ${provider}/${id} text-only, so an image would go to a model that cannot read it.`,
|
|
265
|
+
"",
|
|
266
|
+
"If that is wrong, override the entry in ~/.akan/code/models.json:",
|
|
267
|
+
"",
|
|
268
|
+
`{"providers":{"${provider}":{"modelOverrides":{"${id}":{"input":["text","image"]}}}}}`,
|
|
269
|
+
].join("\n");
|
|
270
|
+
}
|
|
271
|
+
}
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import type { CodeAgentAnswer, CodeAgentApprovalRequest, CodeAgentQuestion, CodeTranscript } from "akanjs/common";
|
|
2
|
+
import type { CodeAgent } from "../agent/CodeAgent";
|
|
3
|
+
import type { CodeTuiOption, CodeTuiSnapshot } from "./CodeTuiApp";
|
|
4
|
+
import type { CodeTuiEditor } from "./CodeTuiEditor";
|
|
5
|
+
import { type CodeTuiAskBlock, CodeTuiFrame } from "./CodeTuiFrame";
|
|
6
|
+
|
|
7
|
+
export class CodeTuiQuestion {
|
|
8
|
+
// Host-only choice: picking it opens the prompt instead of answering, so the key never reaches the core.
|
|
9
|
+
static readonly freeTextKey = "__free_text__";
|
|
10
|
+
|
|
11
|
+
readonly #agent: CodeAgent;
|
|
12
|
+
readonly #transcript: CodeTranscript;
|
|
13
|
+
readonly #editor: CodeTuiEditor;
|
|
14
|
+
readonly #render: () => void;
|
|
15
|
+
#selected = 0;
|
|
16
|
+
#typing = false; // answering the open question in prose rather than from its list
|
|
17
|
+
#checked: string[] = [];
|
|
18
|
+
|
|
19
|
+
constructor(agent: CodeAgent, transcript: CodeTranscript, editor: CodeTuiEditor, render: () => void) {
|
|
20
|
+
this.#agent = agent;
|
|
21
|
+
this.#transcript = transcript;
|
|
22
|
+
this.#editor = editor;
|
|
23
|
+
this.#render = render;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
get typing() {
|
|
27
|
+
return this.#typing;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
get checked() {
|
|
31
|
+
return this.#checked;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
block(question: CodeAgentQuestion | undefined, frameRows: number): CodeTuiAskBlock {
|
|
35
|
+
const all = CodeTuiQuestion.optionsOf(question);
|
|
36
|
+
const chrome = 1 + 2 * CodeTuiFrame.promptRuleRows;
|
|
37
|
+
const budget = Math.max(1, frameRows - CodeTuiFrame.minBodyRows - CodeTuiFrame.chromeRows - chrome);
|
|
38
|
+
const shown = Math.min(all.length, budget);
|
|
39
|
+
const first = Math.max(0, Math.min(this.#selected - Math.floor(shown / 2), all.length - shown));
|
|
40
|
+
return {
|
|
41
|
+
rows: chrome + shown,
|
|
42
|
+
above: CodeTuiFrame.promptRuleRows,
|
|
43
|
+
selected: this.#selected - first,
|
|
44
|
+
caret: undefined,
|
|
45
|
+
snapshot: {
|
|
46
|
+
input: [] as string[],
|
|
47
|
+
menu: [] as CodeTuiOption[],
|
|
48
|
+
menuSelected: 0,
|
|
49
|
+
options: all.slice(first, first + shown),
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
static modeOf(
|
|
55
|
+
question: CodeAgentQuestion | undefined,
|
|
56
|
+
approval: CodeAgentApprovalRequest | undefined,
|
|
57
|
+
): CodeTuiSnapshot["mode"] {
|
|
58
|
+
if (approval) return "confirm";
|
|
59
|
+
if (!question) return "input";
|
|
60
|
+
if (question.kind === "confirm") return "confirm";
|
|
61
|
+
if (question.kind === "select" && question.options?.length) return "select";
|
|
62
|
+
return "input";
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
static optionsOf(question: CodeAgentQuestion | undefined): CodeTuiOption[] {
|
|
66
|
+
const options = (question?.options ?? []).map((option) => ({
|
|
67
|
+
key: option.key,
|
|
68
|
+
label: option.recommended ? `${option.label} (recommended)` : option.label,
|
|
69
|
+
...(option.detail ? { detail: option.detail } : {}),
|
|
70
|
+
}));
|
|
71
|
+
if (!options.length || !question?.freeText) return options;
|
|
72
|
+
return [
|
|
73
|
+
...options,
|
|
74
|
+
{ key: CodeTuiQuestion.freeTextKey, label: "Something else…", detail: "answer in your own words" },
|
|
75
|
+
];
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
static promptOf(question: CodeAgentQuestion | undefined, approval: CodeAgentApprovalRequest | undefined) {
|
|
79
|
+
if (approval) return `approve? ${approval.summary} ${approval.remember?.length ? "[y/n/a]" : "[y/n]"}`;
|
|
80
|
+
return question ? `? ${question.prompt}` : "";
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
hint(mode: CodeTuiSnapshot["mode"], multiSelect: boolean) {
|
|
84
|
+
if (this.#typing) return "enter answer · esc back to the choices";
|
|
85
|
+
const remember = this.#transcript.approval?.remember;
|
|
86
|
+
if (mode === "confirm" && remember?.length)
|
|
87
|
+
return `y yes · n no · esc no · a yes, and stop asking about ${remember.join(", ")} this session`;
|
|
88
|
+
if (mode === "confirm") return "y yes · n no · esc no";
|
|
89
|
+
if (mode === "select")
|
|
90
|
+
return `↑↓ choose${multiSelect ? " · space toggle" : ""} · enter answer · esc skip · pgup/pgdn scroll`;
|
|
91
|
+
return "enter send · shift+enter newline · ↑↓ move · esc interrupt · pgup/pgdn scroll · ^c quit · /help";
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
placeholder() {
|
|
95
|
+
if (this.#typing) return "your answer";
|
|
96
|
+
if (this.#transcript.question) return "type an answer, or esc to skip";
|
|
97
|
+
return this.#transcript.streaming ? "type to steer, esc to interrupt" : "ask for something, / for commands";
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
move(delta: number) {
|
|
101
|
+
const count = CodeTuiQuestion.optionsOf(this.#transcript.question).length;
|
|
102
|
+
if (!count) return;
|
|
103
|
+
this.#selected = (this.#selected + delta + count) % count;
|
|
104
|
+
this.#render();
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
toggle = () => {
|
|
108
|
+
const option = CodeTuiQuestion.optionsOf(this.#transcript.question)[this.#selected];
|
|
109
|
+
if (!option) return;
|
|
110
|
+
this.#checked = this.#checked.includes(option.key)
|
|
111
|
+
? this.#checked.filter((key) => key !== option.key)
|
|
112
|
+
: [...this.#checked, option.key];
|
|
113
|
+
this.#render();
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
answerConfirm = (value: boolean) => {
|
|
117
|
+
const approval = this.#transcript.approval;
|
|
118
|
+
if (approval) return void this.#agent.approve(approval.approvalId, value);
|
|
119
|
+
const question = this.#transcript.question;
|
|
120
|
+
if (question) void this.answer(question.questionId, { text: value ? "yes" : "no" });
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
allowForSession = () => {
|
|
124
|
+
const approval = this.#transcript.approval;
|
|
125
|
+
if (approval?.remember?.length) void this.#agent.approve(approval.approvalId, true, "session");
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
open(question: CodeAgentQuestion) {
|
|
129
|
+
this.#typing = false;
|
|
130
|
+
this.#checked = [];
|
|
131
|
+
this.#selected = Math.max(
|
|
132
|
+
0,
|
|
133
|
+
(question.options ?? []).findIndex((option) => option.recommended),
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
answerCurrent(questionId: string) {
|
|
138
|
+
const question = this.#transcript.question;
|
|
139
|
+
const shown = CodeTuiQuestion.optionsOf(question);
|
|
140
|
+
if (this.#typing || !shown.length) {
|
|
141
|
+
const text = this.#editor.text.trim();
|
|
142
|
+
this.#editor.clear();
|
|
143
|
+
this.#typing = false;
|
|
144
|
+
return void this.answer(questionId, { text });
|
|
145
|
+
}
|
|
146
|
+
if (shown[this.#selected]?.key === CodeTuiQuestion.freeTextKey) {
|
|
147
|
+
this.#typing = true;
|
|
148
|
+
this.#editor.clear();
|
|
149
|
+
return this.#render();
|
|
150
|
+
}
|
|
151
|
+
const keys = question?.multiSelect
|
|
152
|
+
? this.#checked.filter((key) => key !== CodeTuiQuestion.freeTextKey)
|
|
153
|
+
: [shown[this.#selected]?.key ?? ""];
|
|
154
|
+
this.#editor.clear();
|
|
155
|
+
void this.answer(questionId, { keys });
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
backToChoices() {
|
|
159
|
+
this.#typing = false;
|
|
160
|
+
this.#editor.clear();
|
|
161
|
+
this.#render();
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
async answer(questionId: string, answer: CodeAgentAnswer) {
|
|
165
|
+
this.#selected = 0;
|
|
166
|
+
this.#checked = [];
|
|
167
|
+
this.#render();
|
|
168
|
+
await this.#agent.answer(questionId, answer);
|
|
169
|
+
}
|
|
170
|
+
}
|