@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,117 @@
|
|
|
1
|
+
import stringWidth from "string-width";
|
|
2
|
+
|
|
3
|
+
export interface CodeTuiStyle {
|
|
4
|
+
color?: string;
|
|
5
|
+
dim?: boolean;
|
|
6
|
+
bold?: boolean;
|
|
7
|
+
italic?: boolean;
|
|
8
|
+
strikethrough?: boolean;
|
|
9
|
+
underline?: boolean;
|
|
10
|
+
inverse?: boolean;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export type CodeTuiSpan = CodeTuiStyle & { text: string };
|
|
14
|
+
|
|
15
|
+
/** Exactly one terminal row. */
|
|
16
|
+
export interface CodeTuiLine {
|
|
17
|
+
key: string;
|
|
18
|
+
spans: CodeTuiSpan[];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** A row before wrapping: `prefix` marks its first line and `hang` indents the rest. */
|
|
22
|
+
export interface CodeTuiRow {
|
|
23
|
+
spans: CodeTuiSpan[];
|
|
24
|
+
prefix?: CodeTuiSpan;
|
|
25
|
+
/** Defaults to blanks as wide as `prefix`. */
|
|
26
|
+
hang?: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Wraps here rather than in Ink so the scroll arithmetic knows every row; widths use Ink's own `string-width`. */
|
|
30
|
+
export class CodeTuiLines {
|
|
31
|
+
static width(text: string) {
|
|
32
|
+
return stringWidth(text);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
static rows(rows: CodeTuiRow[], width: number, keyBase: string): CodeTuiLine[] {
|
|
36
|
+
const lines: CodeTuiLine[] = [];
|
|
37
|
+
rows.forEach((row, idx) => {
|
|
38
|
+
for (const line of CodeTuiLines.#row(row, Math.max(8, width), `${keyBase}:${idx}`)) lines.push(line);
|
|
39
|
+
});
|
|
40
|
+
return lines;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
static #row(row: CodeTuiRow, width: number, key: string): CodeTuiLine[] {
|
|
44
|
+
const prefix = row.prefix;
|
|
45
|
+
const head = prefix ? CodeTuiLines.width(prefix.text) : 0;
|
|
46
|
+
const hang = row.hang ?? " ".repeat(head);
|
|
47
|
+
const body = width - Math.max(head, CodeTuiLines.width(hang));
|
|
48
|
+
const wrapped = CodeTuiLines.#wrap(row.spans, Math.max(4, body));
|
|
49
|
+
if (!wrapped.length) wrapped.push([]);
|
|
50
|
+
return wrapped.map((spans, at) => ({
|
|
51
|
+
key: `${key}:${at}`,
|
|
52
|
+
spans: [...(at === 0 ? (prefix ? [prefix] : []) : hang ? [{ text: hang } satisfies CodeTuiSpan] : []), ...spans],
|
|
53
|
+
}));
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
static #wrap(spans: CodeTuiSpan[], width: number): CodeTuiSpan[][] {
|
|
57
|
+
const lines: CodeTuiSpan[][] = [];
|
|
58
|
+
let line: CodeTuiSpan[] = [];
|
|
59
|
+
let used = 0;
|
|
60
|
+
// Leading whitespace is dropped only after an overflow break; after a newline it is the writer's indentation.
|
|
61
|
+
let soft = false;
|
|
62
|
+
const push = (fromWrap: boolean) => {
|
|
63
|
+
lines.push(line);
|
|
64
|
+
line = [];
|
|
65
|
+
used = 0;
|
|
66
|
+
soft = fromWrap;
|
|
67
|
+
};
|
|
68
|
+
for (const span of spans) {
|
|
69
|
+
const { text, ...style } = span;
|
|
70
|
+
for (const piece of text.split("\n").flatMap((part, at) => (at ? [null, part] : [part]))) {
|
|
71
|
+
if (piece === null) {
|
|
72
|
+
push(false);
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
for (const word of CodeTuiLines.#words(piece)) {
|
|
76
|
+
const size = CodeTuiLines.width(word);
|
|
77
|
+
if (!word.trim() && !used && soft) continue;
|
|
78
|
+
if (used && used + size > width) push(true);
|
|
79
|
+
if (size > width) {
|
|
80
|
+
for (const chunk of CodeTuiLines.chunks(word, width)) {
|
|
81
|
+
if (used) push(true);
|
|
82
|
+
line.push({ ...style, text: chunk });
|
|
83
|
+
used = CodeTuiLines.width(chunk);
|
|
84
|
+
if (used >= width) push(true);
|
|
85
|
+
}
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
line.push({ ...style, text: word });
|
|
89
|
+
used += size;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
if (line.length) lines.push(line);
|
|
94
|
+
return lines;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
static #words(text: string) {
|
|
98
|
+
return text.match(/\S+\s*|\s+/g) ?? [];
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
static chunks(word: string, width: number) {
|
|
102
|
+
const chunks: string[] = [];
|
|
103
|
+
let chunk = "";
|
|
104
|
+
for (const char of word) {
|
|
105
|
+
if (CodeTuiLines.width(chunk + char) > width) {
|
|
106
|
+
chunks.push(chunk);
|
|
107
|
+
chunk = char;
|
|
108
|
+
} else chunk += char;
|
|
109
|
+
}
|
|
110
|
+
if (chunk) chunks.push(chunk);
|
|
111
|
+
return chunks;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
static text(line: CodeTuiLine) {
|
|
115
|
+
return line.spans.map((span) => span.text).join("");
|
|
116
|
+
}
|
|
117
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { type MarkdownBlock, MarkdownBlocks, type MarkdownSpan, MarkdownSpans, type TableBlock } from "akanjs/common";
|
|
2
|
+
import { CodeTuiLines, type CodeTuiRow, type CodeTuiSpan, type CodeTuiStyle } from "./CodeTuiLines";
|
|
3
|
+
|
|
4
|
+
/** Terminal ink for the shared `akanjs/common` markdown scanner; parsing decisions belong there, not here. */
|
|
5
|
+
export class CodeTuiMarkdown {
|
|
6
|
+
static readonly bullet = "•";
|
|
7
|
+
static readonly minColumn = 6;
|
|
8
|
+
|
|
9
|
+
static rows(source: string, width = 80): CodeTuiRow[] {
|
|
10
|
+
const rows: CodeTuiRow[] = [];
|
|
11
|
+
const blocks = MarkdownBlocks.of(source);
|
|
12
|
+
blocks.forEach((block, at) => {
|
|
13
|
+
if (at) rows.push({ spans: [] });
|
|
14
|
+
for (const row of CodeTuiMarkdown.#block(block, width)) rows.push(row);
|
|
15
|
+
});
|
|
16
|
+
return rows;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
static #block(block: MarkdownBlock, width: number): CodeTuiRow[] {
|
|
20
|
+
switch (block.kind) {
|
|
21
|
+
case "heading":
|
|
22
|
+
return [
|
|
23
|
+
{
|
|
24
|
+
spans: CodeTuiMarkdown.spans(block.text, { bold: true, ...(block.level <= 2 ? { color: "cyan" } : {}) }),
|
|
25
|
+
},
|
|
26
|
+
];
|
|
27
|
+
case "code":
|
|
28
|
+
return CodeTuiMarkdown.#code(block.lang, block.text);
|
|
29
|
+
case "quote":
|
|
30
|
+
return block.text.split("\n").map((line) => ({
|
|
31
|
+
prefix: { text: "▌ ", dim: true },
|
|
32
|
+
spans: CodeTuiMarkdown.spans(line, { dim: true }),
|
|
33
|
+
}));
|
|
34
|
+
case "rule":
|
|
35
|
+
return [{ spans: [{ text: "─".repeat(24), dim: true }] }];
|
|
36
|
+
case "list":
|
|
37
|
+
return block.items.map((item) => {
|
|
38
|
+
const indent = " ".repeat(item.depth);
|
|
39
|
+
const marker = item.ordered ? `${item.num}. ` : `${CodeTuiMarkdown.bullet} `;
|
|
40
|
+
return {
|
|
41
|
+
prefix: { text: `${indent}${marker}`, dim: true },
|
|
42
|
+
spans: CodeTuiMarkdown.spans(item.text),
|
|
43
|
+
};
|
|
44
|
+
});
|
|
45
|
+
case "table":
|
|
46
|
+
return CodeTuiMarkdown.#table(block, width);
|
|
47
|
+
default:
|
|
48
|
+
return [{ spans: CodeTuiMarkdown.spans(block.text) }];
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
static #code(lang: string | undefined, text: string): CodeTuiRow[] {
|
|
53
|
+
const label = lang?.trim();
|
|
54
|
+
const bar: CodeTuiSpan = { text: "│ ", dim: true };
|
|
55
|
+
return [
|
|
56
|
+
...(label ? [{ spans: [{ text: label, dim: true, italic: true } satisfies CodeTuiSpan] }] : []),
|
|
57
|
+
...text.split("\n").map((line) => ({ prefix: bar, hang: "│ ", spans: [{ text: line, color: "green" }] })),
|
|
58
|
+
];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Cell markers are stripped, not styled: styled runs would need re-splitting at every column edge.
|
|
62
|
+
static #table(block: TableBlock, width: number): CodeTuiRow[] {
|
|
63
|
+
const columns = block.head.length;
|
|
64
|
+
const grid = [block.head, ...block.rows].map((row) =>
|
|
65
|
+
Array.from({ length: columns }, (_, col) => MarkdownSpans.plain(row[col] ?? "")),
|
|
66
|
+
);
|
|
67
|
+
const widths = CodeTuiMarkdown.#columns(grid, columns, width);
|
|
68
|
+
const rule: CodeTuiRow = { spans: [{ text: widths.map((size) => "─".repeat(size)).join(" "), dim: true }] };
|
|
69
|
+
const [head, ...body] = grid;
|
|
70
|
+
return [
|
|
71
|
+
...CodeTuiMarkdown.#gridRow(head ?? [], widths, block.aligns, { bold: true }),
|
|
72
|
+
rule,
|
|
73
|
+
...body.flatMap((row) => CodeTuiMarkdown.#gridRow(row, widths, block.aligns, {})),
|
|
74
|
+
];
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// A column's floor is its longest word, not a proportional share: a path split mid-name cannot be searched.
|
|
78
|
+
static #columns(grid: string[][], columns: number, width: number) {
|
|
79
|
+
const natural = Array.from({ length: columns }, (_, col) =>
|
|
80
|
+
Math.max(1, ...grid.map((row) => CodeTuiLines.width(row[col] ?? ""))),
|
|
81
|
+
);
|
|
82
|
+
const gaps = 2 * Math.max(0, columns - 1);
|
|
83
|
+
const room = Math.max(columns * CodeTuiMarkdown.minColumn, width - gaps);
|
|
84
|
+
const total = natural.reduce((sum, size) => sum + size, 0);
|
|
85
|
+
if (total <= width - gaps) return natural;
|
|
86
|
+
const floors = natural.map((size, col) =>
|
|
87
|
+
Math.min(size, Math.max(CodeTuiMarkdown.minColumn, CodeTuiMarkdown.#longestWord(grid, col))),
|
|
88
|
+
);
|
|
89
|
+
const held = floors.reduce((sum, size) => sum + size, 0);
|
|
90
|
+
if (held > room) return natural.map((size) => Math.max(1, Math.floor((size * room) / total)));
|
|
91
|
+
const want = natural.map((size, col) => size - (floors[col] ?? 0));
|
|
92
|
+
const wanted = want.reduce((sum, size) => sum + size, 0) || 1;
|
|
93
|
+
const slack = room - held;
|
|
94
|
+
return floors.map((size, col) => size + Math.floor((slack * (want[col] ?? 0)) / wanted));
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
static #longestWord(grid: string[][], col: number) {
|
|
98
|
+
return Math.max(1, ...grid.flatMap((row) => (row[col] ?? "").split(/\s+/).map((word) => CodeTuiLines.width(word))));
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
static #gridRow(cells: string[], widths: number[], aligns: TableBlock["aligns"], style: CodeTuiStyle) {
|
|
102
|
+
const wrapped = widths.map((size, col) => CodeTuiMarkdown.#cell(cells[col] ?? "", size));
|
|
103
|
+
const height = Math.max(1, ...wrapped.map((lines) => lines.length));
|
|
104
|
+
return Array.from({ length: height }, (_, line) => ({
|
|
105
|
+
spans: widths.flatMap((size, col): CodeTuiSpan[] => {
|
|
106
|
+
const text = wrapped[col]?.[line] ?? "";
|
|
107
|
+
const fill = " ".repeat(Math.max(0, size - CodeTuiLines.width(text)));
|
|
108
|
+
const align = aligns[col];
|
|
109
|
+
const lead = align === "right" ? fill : align === "center" ? " ".repeat(Math.floor(fill.length / 2)) : "";
|
|
110
|
+
const tail = fill.slice(lead.length);
|
|
111
|
+
return [{ text: lead }, { ...style, text }, { text: `${tail}${col === widths.length - 1 ? "" : " "}` }];
|
|
112
|
+
}),
|
|
113
|
+
}));
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
static #cell(text: string, width: number) {
|
|
117
|
+
const lines: string[] = [];
|
|
118
|
+
let line = "";
|
|
119
|
+
for (const word of text.split(/(\s+)/)) {
|
|
120
|
+
if (!word) continue;
|
|
121
|
+
if (line && CodeTuiLines.width(line + word) > width) {
|
|
122
|
+
lines.push(line.trimEnd());
|
|
123
|
+
line = word.trim() ? word : "";
|
|
124
|
+
continue;
|
|
125
|
+
}
|
|
126
|
+
line += word;
|
|
127
|
+
}
|
|
128
|
+
if (line.trim() || !lines.length) lines.push(line.trimEnd());
|
|
129
|
+
return lines.flatMap((entry) => (CodeTuiLines.width(entry) <= width ? [entry] : CodeTuiLines.chunks(entry, width)));
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** Inline markers as terminal styling, with `base` applied under whatever the markers add. */
|
|
133
|
+
static spans(text: string, base: CodeTuiStyle = {}): CodeTuiSpan[] {
|
|
134
|
+
return MarkdownSpans.of(text).flatMap((span): CodeTuiSpan[] => {
|
|
135
|
+
// A terminal cannot hide a url behind a clickable label, so both are shown.
|
|
136
|
+
if (span.kind === "link")
|
|
137
|
+
return [
|
|
138
|
+
{ ...base, underline: true, text: span.text },
|
|
139
|
+
{ ...base, dim: true, text: ` (${span.href})` },
|
|
140
|
+
];
|
|
141
|
+
return [{ ...base, ...CodeTuiMarkdown.#span(span), text: span.text }];
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
static #span(span: MarkdownSpan): CodeTuiStyle {
|
|
146
|
+
switch (span.kind) {
|
|
147
|
+
case "code":
|
|
148
|
+
return { color: "yellow" };
|
|
149
|
+
case "strong":
|
|
150
|
+
return { bold: true };
|
|
151
|
+
case "em":
|
|
152
|
+
return { italic: true };
|
|
153
|
+
case "del":
|
|
154
|
+
return { strikethrough: true };
|
|
155
|
+
default:
|
|
156
|
+
return {};
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import type { CodeAgentMcpServerRef, CodeAgentMcpStatus } from "akanjs/common";
|
|
2
|
+
import type { McpDeclaredServer, McpServerScope } from "../tools/McpServerConfig";
|
|
3
|
+
|
|
4
|
+
export interface CodeTuiMcpView {
|
|
5
|
+
status: CodeAgentMcpStatus[]; // the servers the session connected to when created, each with its sign-in state now
|
|
6
|
+
declared: McpDeclaredServer[]; // what the files say now
|
|
7
|
+
files: { scope: McpServerScope; file: string }[];
|
|
8
|
+
problem?: string;
|
|
9
|
+
workspaceApproved?: boolean;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
interface McpRow {
|
|
13
|
+
ref?: CodeAgentMcpServerRef;
|
|
14
|
+
disabled?: boolean;
|
|
15
|
+
scope?: McpServerScope;
|
|
16
|
+
live?: CodeAgentMcpStatus;
|
|
17
|
+
unapproved?: boolean;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export class CodeTuiMcp {
|
|
21
|
+
/** Written into a tool name as `mcp__<name>__<tool>`, so two names that sanitize alike shadow each other. */
|
|
22
|
+
static readonly namePattern = /^[a-zA-Z0-9_-]+$/;
|
|
23
|
+
|
|
24
|
+
static readonly usage = [
|
|
25
|
+
"/mcp add <name> <command> [args…] declare a server this session starts",
|
|
26
|
+
"/mcp add <name> <https://…> declare one it reaches over http",
|
|
27
|
+
"/mcp add --local <name> … declare it for this repo only, not every repo",
|
|
28
|
+
"/mcp login <name> sign in through the browser (OAuth)",
|
|
29
|
+
"/mcp logout <name> forget the token, leaving the server declared",
|
|
30
|
+
"/mcp remove <name> undeclare it, from this repo's file first",
|
|
31
|
+
"/mcp remove --local <name> undeclare it from this repo's file only",
|
|
32
|
+
"/mcp reload reopen this session so the file takes effect",
|
|
33
|
+
];
|
|
34
|
+
|
|
35
|
+
static #where(view: CodeTuiMcpView) {
|
|
36
|
+
return view.files.map(({ scope, file }) => `${scope.padEnd(11)}${file}`);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
static list(view: CodeTuiMcpView) {
|
|
40
|
+
const lines = view.problem ? [view.problem, ""] : [];
|
|
41
|
+
const rows = CodeTuiMcp.#rows(view);
|
|
42
|
+
if (!rows.length)
|
|
43
|
+
return [
|
|
44
|
+
...lines,
|
|
45
|
+
"No MCP server is declared.",
|
|
46
|
+
"",
|
|
47
|
+
"Declare one here, or paste an editor's `mcpServers` block into either file:",
|
|
48
|
+
...CodeTuiMcp.#where(view),
|
|
49
|
+
"",
|
|
50
|
+
...CodeTuiMcp.usage,
|
|
51
|
+
].join("\n");
|
|
52
|
+
const width = (at: number) => Math.max(...rows.map((row) => (row[at] ?? "").length)) + 2;
|
|
53
|
+
const [name, scope, transport, target] = [width(0), width(1), width(2), width(3)];
|
|
54
|
+
return [
|
|
55
|
+
...lines,
|
|
56
|
+
`${rows.length} server${rows.length === 1 ? "" : "s"}`,
|
|
57
|
+
"",
|
|
58
|
+
...rows.map(
|
|
59
|
+
([a, b, c, d, e]) =>
|
|
60
|
+
`${(a ?? "").padEnd(name)}${(b ?? "").padEnd(scope)}${(c ?? "").padEnd(transport)}${(d ?? "").padEnd(target)}${e ?? ""}`,
|
|
61
|
+
),
|
|
62
|
+
"",
|
|
63
|
+
...CodeTuiMcp.#where(view),
|
|
64
|
+
"",
|
|
65
|
+
...CodeTuiMcp.usage,
|
|
66
|
+
].join("\n");
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
static #rows(view: CodeTuiMcpView) {
|
|
70
|
+
const byName = new Map<string, McpRow>();
|
|
71
|
+
for (const entry of view.declared)
|
|
72
|
+
byName.set(entry.ref.name, {
|
|
73
|
+
ref: entry.ref,
|
|
74
|
+
disabled: entry.disabled,
|
|
75
|
+
scope: entry.scope,
|
|
76
|
+
unapproved: entry.scope === "workspace" && view.workspaceApproved === false,
|
|
77
|
+
});
|
|
78
|
+
for (const live of view.status) byName.set(live.name, { ...byName.get(live.name), live });
|
|
79
|
+
return [...byName]
|
|
80
|
+
.sort(([a], [b]) => a.localeCompare(b))
|
|
81
|
+
.map(([name, entry]) => {
|
|
82
|
+
const transport = entry.live?.transport ?? entry.ref?.transport ?? "";
|
|
83
|
+
const target = entry.live?.target ?? CodeTuiMcp.targetOf(entry.ref);
|
|
84
|
+
return [name, entry.scope ?? "", transport, target, CodeTuiMcp.#stateOf(entry)];
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
static #stateOf(entry: McpRow) {
|
|
89
|
+
if (entry.disabled) return "disabled in the file";
|
|
90
|
+
if (entry.unapproved && !entry.live) return "not approved · restart akan code to approve it";
|
|
91
|
+
if (!entry.live) return "declared · /mcp reload to connect";
|
|
92
|
+
// Before the error branch: a 401 is a server answering correctly, not a broken one.
|
|
93
|
+
if (entry.live.auth === "required") return `sign-in needed · /mcp login ${entry.live.name}`;
|
|
94
|
+
if (entry.live.error) return `unreachable — ${entry.live.error}`;
|
|
95
|
+
const tools = `${entry.live.tools.length} tool${entry.live.tools.length === 1 ? "" : "s"}`;
|
|
96
|
+
if (!entry.ref) return `${tools} · removed from the file`;
|
|
97
|
+
return entry.live.auth === "authorized" ? `${tools} · signed in` : tools;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
static targetOf(ref: CodeAgentMcpServerRef | undefined) {
|
|
101
|
+
if (!ref) return "";
|
|
102
|
+
return ref.url ?? [ref.command, ...(ref.args ?? [])].filter(Boolean).join(" ");
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
static tools(status: CodeAgentMcpStatus) {
|
|
106
|
+
if (status.error) return `${status.name} is unreachable — ${status.error}`;
|
|
107
|
+
if (status.auth === "required") return `${status.name} needs signing in — /mcp login ${status.name}`;
|
|
108
|
+
if (!status.tools.length) return `${status.name} connected and published no tools.`;
|
|
109
|
+
return [`${status.name} · ${status.target}`, "", status.tools.join("\n")].join("\n");
|
|
110
|
+
}
|
|
111
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import type { CodeAgentProviderInfo } from "akanjs/common";
|
|
2
|
+
|
|
3
|
+
export class CodeTuiModels {
|
|
4
|
+
/** `DEEPSEEK_API_KEY` names the provider `deepseek`; the engine's ids are lower-kebab, so the map is exact. */
|
|
5
|
+
static envKeyOf(provider: string) {
|
|
6
|
+
return `${provider.toUpperCase().replace(/-/g, "_")}_API_KEY`;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
static readonly usage = [
|
|
10
|
+
"/model <provider>/<id> switch to it, e.g. /model deepseek/deepseek-v4-pro",
|
|
11
|
+
"/model providers every provider, and which ones have a key",
|
|
12
|
+
"/model <provider> what one provider offers",
|
|
13
|
+
];
|
|
14
|
+
|
|
15
|
+
static list(catalogue: CodeAgentProviderInfo[]) {
|
|
16
|
+
const ready = catalogue.filter((provider) => provider.authorized);
|
|
17
|
+
const locked = catalogue.length - ready.length;
|
|
18
|
+
if (!ready.length)
|
|
19
|
+
return [
|
|
20
|
+
"No provider has a key, so there is no model to switch to.",
|
|
21
|
+
"",
|
|
22
|
+
`Set <PROVIDER>_API_KEY in the workspace \`.env\` or your environment — ${CodeTuiModels.envKeyOf("deepseek")}, ${CodeTuiModels.envKeyOf("anthropic")}.`,
|
|
23
|
+
"",
|
|
24
|
+
...CodeTuiModels.usage,
|
|
25
|
+
].join("\n");
|
|
26
|
+
const rows = ready.flatMap((provider) => CodeTuiModels.#modelRows(provider));
|
|
27
|
+
return [
|
|
28
|
+
`${rows.length} model${rows.length === 1 ? "" : "s"} ready · ${ready.map((provider) => provider.id).join(", ")}`,
|
|
29
|
+
"",
|
|
30
|
+
...CodeTuiModels.#table(rows),
|
|
31
|
+
"",
|
|
32
|
+
`${locked} more provider${locked === 1 ? "" : "s"} need a key — /model providers`,
|
|
33
|
+
"",
|
|
34
|
+
...CodeTuiModels.usage,
|
|
35
|
+
].join("\n");
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
static providers(catalogue: CodeAgentProviderInfo[]) {
|
|
39
|
+
const rows = catalogue.map((provider) => [
|
|
40
|
+
provider.id,
|
|
41
|
+
provider.authorized ? "ready" : "no key",
|
|
42
|
+
`${provider.models.length} model${provider.models.length === 1 ? "" : "s"}`,
|
|
43
|
+
provider.name,
|
|
44
|
+
]);
|
|
45
|
+
return [
|
|
46
|
+
`${catalogue.length} providers`,
|
|
47
|
+
"",
|
|
48
|
+
...CodeTuiModels.#table(rows),
|
|
49
|
+
"",
|
|
50
|
+
"A provider is reached by setting <PROVIDER>_API_KEY — in the workspace `.env` or your environment,",
|
|
51
|
+
"both of which are read at startup. /model <provider> lists what one offers.",
|
|
52
|
+
"",
|
|
53
|
+
...CodeTuiModels.usage,
|
|
54
|
+
].join("\n");
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
static ofProvider(catalogue: CodeAgentProviderInfo[], id: string) {
|
|
58
|
+
const provider = catalogue.find((entry) => entry.id === id);
|
|
59
|
+
if (!provider) return undefined;
|
|
60
|
+
const rows = CodeTuiModels.#modelRows(provider);
|
|
61
|
+
const head = provider.authorized
|
|
62
|
+
? `${provider.name} · ${rows.length} models · ready`
|
|
63
|
+
: `${provider.name} · ${rows.length} models · set ${CodeTuiModels.envKeyOf(provider.id)} to use it`;
|
|
64
|
+
return [head, "", ...CodeTuiModels.#table(rows)].join("\n");
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
static #modelRows(provider: CodeAgentProviderInfo) {
|
|
68
|
+
return provider.models.map((model) => [
|
|
69
|
+
`${model.current ? "❯ " : " "}${provider.id}/${model.id}`,
|
|
70
|
+
model.name,
|
|
71
|
+
model.contextWindow ? `${Math.round(model.contextWindow / 1000)}k ctx` : "",
|
|
72
|
+
]);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
static #table(rows: string[][]) {
|
|
76
|
+
const width = (at: number) => Math.max(0, ...rows.map((row) => (row[at] ?? "").length)) + 2;
|
|
77
|
+
const columns = [width(0), width(1)];
|
|
78
|
+
return rows.map((row) =>
|
|
79
|
+
`${(row[0] ?? "").padEnd(columns[0] ?? 0)}${(row[1] ?? "").padEnd(columns[1] ?? 0)}${row[2] ?? ""}`.trimEnd(),
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
export interface CodeTuiWheel {
|
|
2
|
+
rows: number; // 0 for a non-wheel report, which is swallowed rather than typed
|
|
3
|
+
rest: string; // what followed the report in the same chunk: ordinary typing
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
// Mode 1000 is the narrowest that carries the wheel; 1006 asks for SGR decimal coordinates, and a terminal without
|
|
7
|
+
// it keeps the legacy form, so both are decoded. While tracking is on, a drag selects text only with shift held.
|
|
8
|
+
export class CodeTuiMouse {
|
|
9
|
+
static readonly on = "[?1000h[?1006h";
|
|
10
|
+
static readonly off = "[?1006l[?1000l";
|
|
11
|
+
static readonly step = 3; // rows per notch, as in less and vim
|
|
12
|
+
|
|
13
|
+
#pending = 0; // coordinate bytes of a legacy report still to come, as their own chunk
|
|
14
|
+
|
|
15
|
+
// Ink strips the ESC and hands a CSI over whole (`[<64;12;7M`); a legacy `[M` report's three coordinate bytes
|
|
16
|
+
// arrive as the next chunk. undefined means the input belongs to the keyboard.
|
|
17
|
+
read(input: string): CodeTuiWheel | undefined {
|
|
18
|
+
if (this.#pending) {
|
|
19
|
+
const button = (input.codePointAt(0) ?? 32) - 32;
|
|
20
|
+
const rest = input.slice(this.#pending);
|
|
21
|
+
this.#pending = 0;
|
|
22
|
+
return { rows: CodeTuiMouse.rowsOf(button), rest };
|
|
23
|
+
}
|
|
24
|
+
const sgr = /^\[<(\d+);\d+;\d+[Mm]$/.exec(input);
|
|
25
|
+
if (sgr) return { rows: CodeTuiMouse.rowsOf(Number(sgr[1])), rest: "" };
|
|
26
|
+
if (input !== "[M") return undefined;
|
|
27
|
+
this.#pending = 3;
|
|
28
|
+
return { rows: 0, rest: "" };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Bit 64 is the wheel and the low two bits its direction; the horizontal wheel (66/67) is ignored.
|
|
32
|
+
static rowsOf(button: number) {
|
|
33
|
+
if (!(button & 64)) return 0;
|
|
34
|
+
const direction = button & 3;
|
|
35
|
+
if (direction === 0) return -CodeTuiMouse.step;
|
|
36
|
+
if (direction === 1) return CodeTuiMouse.step;
|
|
37
|
+
return 0;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import type { CodeTranscript } from "akanjs/common";
|
|
2
|
+
import type { CodeAgent } from "../agent/CodeAgent";
|
|
3
|
+
import type { CodeSessionEntry } from "../agent/CodeSessionIndex";
|
|
4
|
+
import { type CodeTuiLine, CodeTuiLines, type CodeTuiRow, type CodeTuiSpan } from "./CodeTuiLines";
|
|
5
|
+
import { CodeTuiMarkdown } from "./CodeTuiMarkdown";
|
|
6
|
+
|
|
7
|
+
interface OverlayText {
|
|
8
|
+
title: string;
|
|
9
|
+
text: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
interface CodeTuiOverlayHost {
|
|
13
|
+
render: () => void;
|
|
14
|
+
say: (text: string) => void;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export class CodeTuiOverlay {
|
|
18
|
+
readonly #agent: CodeAgent;
|
|
19
|
+
readonly #transcript: CodeTranscript;
|
|
20
|
+
readonly #host: CodeTuiOverlayHost;
|
|
21
|
+
#overlay: OverlayText | null = null;
|
|
22
|
+
#overlayAt = 0;
|
|
23
|
+
#picker: { entries: CodeSessionEntry[]; at: number } | null = null;
|
|
24
|
+
|
|
25
|
+
constructor(agent: CodeAgent, transcript: CodeTranscript, host: CodeTuiOverlayHost) {
|
|
26
|
+
this.#agent = agent;
|
|
27
|
+
this.#transcript = transcript;
|
|
28
|
+
this.#host = host;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
get shown() {
|
|
32
|
+
return this.#overlay !== null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
get picker() {
|
|
36
|
+
return this.#picker;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
block(width: number, height: number) {
|
|
40
|
+
if (this.#picker) return this.#pickerBlock(width, height);
|
|
41
|
+
const overlay = this.#overlay;
|
|
42
|
+
if (!overlay) return null;
|
|
43
|
+
const room = Math.max(1, height - 3); // a border pair and the title row
|
|
44
|
+
const body = Math.max(8, width - 4);
|
|
45
|
+
const all = CodeTuiOverlay.#overlayRows(overlay.text, body);
|
|
46
|
+
const first = Math.max(0, Math.min(this.#overlayAt, all.length - room));
|
|
47
|
+
return {
|
|
48
|
+
title: overlay.title,
|
|
49
|
+
lines: all.slice(first, first + room),
|
|
50
|
+
above: first,
|
|
51
|
+
below: Math.max(0, all.length - first - room),
|
|
52
|
+
selectable: false,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
#pickerBlock(width: number, height: number) {
|
|
57
|
+
const picker = this.#picker;
|
|
58
|
+
if (!picker) return null;
|
|
59
|
+
const room = Math.max(1, height - 3);
|
|
60
|
+
const body = Math.max(8, width - 4);
|
|
61
|
+
const first = Math.max(0, Math.min(picker.at - Math.floor(room / 2), picker.entries.length - room));
|
|
62
|
+
const rows = picker.entries
|
|
63
|
+
.slice(first, first + room)
|
|
64
|
+
.map((entry, at) => CodeTuiOverlay.#sessionRow(entry, first + at === picker.at));
|
|
65
|
+
return {
|
|
66
|
+
title: `sessions · ${picker.entries.length}`,
|
|
67
|
+
// One row per session: a wrapped entry would push the list past the box.
|
|
68
|
+
lines: rows.flatMap((row, at) => CodeTuiLines.rows([row], body, `sessions:${first + at}`).slice(0, 1)),
|
|
69
|
+
above: first,
|
|
70
|
+
below: Math.max(0, picker.entries.length - first - room),
|
|
71
|
+
selectable: true,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
static #sessionRow(entry: CodeSessionEntry, selected: boolean): CodeTuiRow {
|
|
76
|
+
const label = entry.name ?? entry.id.slice(0, 8);
|
|
77
|
+
return {
|
|
78
|
+
prefix: { text: selected ? "❯ " : " ", color: "cyan" },
|
|
79
|
+
spans: [
|
|
80
|
+
{ text: label, bold: selected },
|
|
81
|
+
{
|
|
82
|
+
text: ` ${CodeTuiOverlay.#ago(entry.updatedAt)} · ${entry.turns} turn${entry.turns === 1 ? "" : "s"}`,
|
|
83
|
+
dim: true,
|
|
84
|
+
},
|
|
85
|
+
...(entry.opening ? ([{ text: ` ${entry.opening}`, dim: true }] satisfies CodeTuiSpan[]) : []),
|
|
86
|
+
],
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
static #ago(at: number) {
|
|
91
|
+
const minutes = Math.max(0, Math.round((Date.now() - at) / 60_000));
|
|
92
|
+
if (minutes < 60) return `${minutes}m ago`;
|
|
93
|
+
if (minutes < 60 * 24) return `${Math.round(minutes / 60)}h ago`;
|
|
94
|
+
return `${Math.round(minutes / (60 * 24))}d ago`;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Line by line: the markdown scanner would fold a column of commands into one paragraph.
|
|
98
|
+
static #overlayRows(text: string, width: number): CodeTuiLine[] {
|
|
99
|
+
return text.split("\n").flatMap((line, at) =>
|
|
100
|
+
// A row with no spans renders nothing, so a blank line keeps a space.
|
|
101
|
+
CodeTuiLines.rows(
|
|
102
|
+
line.trim() ? CodeTuiMarkdown.rows(line, width) : [{ spans: [{ text: " " }] }],
|
|
103
|
+
width,
|
|
104
|
+
`overlay:${at}`,
|
|
105
|
+
),
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
openSessions = () => {
|
|
110
|
+
const entries = this.#agent.sessions();
|
|
111
|
+
if (!entries.length) return this.#host.say("no stored sessions yet");
|
|
112
|
+
const current = this.#transcript.info?.sessionId;
|
|
113
|
+
this.#picker = {
|
|
114
|
+
entries,
|
|
115
|
+
at: Math.max(
|
|
116
|
+
0,
|
|
117
|
+
entries.findIndex((entry) => entry.id === current),
|
|
118
|
+
),
|
|
119
|
+
};
|
|
120
|
+
this.#host.render();
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
move(delta: number) {
|
|
124
|
+
const picker = this.#picker;
|
|
125
|
+
if (!picker) return;
|
|
126
|
+
picker.at = Math.max(0, Math.min(picker.entries.length - 1, picker.at + delta));
|
|
127
|
+
this.#host.render();
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
scroll(delta: number) {
|
|
131
|
+
this.#overlayAt = Math.max(0, this.#overlayAt + Math.trunc(delta));
|
|
132
|
+
this.#host.render();
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// Over the transcript, not in it: a persisted transcript would reload every `/help` answer next session.
|
|
136
|
+
open(title: string, text: string) {
|
|
137
|
+
this.#overlay = { title, text };
|
|
138
|
+
this.#overlayAt = 0;
|
|
139
|
+
this.#host.render();
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
dismiss() {
|
|
143
|
+
this.#overlay = null;
|
|
144
|
+
this.#overlayAt = 0;
|
|
145
|
+
this.#picker = null;
|
|
146
|
+
this.#host.render();
|
|
147
|
+
}
|
|
148
|
+
}
|