@a-t-h-i/bot-lobby 0.6.1 → 0.6.3
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 +187 -834
- package/package.json +1 -1
- package/prompts/master.md +17 -0
- package/prompts/panel.md +3 -1
- package/prompts/planner.md +14 -0
- package/prompts/quickfix.md +3 -1
- package/prompts/scout.md +3 -0
- package/prompts/worker.md +3 -0
- package/src/classifier/answers.ts +110 -0
- package/src/classifier/classifier.ts +171 -0
- package/src/classifier/client.ts +239 -0
- package/src/classifier/effort.ts +143 -0
- package/src/classifier/files.ts +427 -0
- package/src/classifier/hosts.ts +157 -0
- package/src/classifier/instance.ts +98 -0
- package/src/classifier/limits.ts +37 -0
- package/src/classifier/seats.ts +102 -0
- package/src/classifier/tools.ts +58 -0
- package/src/classifier/triage.ts +203 -0
- package/src/execution/agent-runner.ts +5 -0
- package/src/index.ts +9 -0
- package/src/lobby/feed.ts +76 -8
- package/src/lobby/layout.ts +14 -13
- package/src/lobby/markdown.ts +61 -18
- package/src/lobby/planner.ts +305 -27
- package/src/lobby/quickfix.ts +120 -8
- package/src/lobby/runtime.ts +57 -10
- package/src/lobby/session-files.ts +162 -25
- package/src/lobby/sessions.ts +21 -7
- package/src/lobby/tabs/home.ts +219 -61
- package/src/lobby/tabs/metrics.ts +28 -1
- package/src/lobby/tabs/plan.ts +27 -4
- package/src/lobby/tabs/quickfix.ts +5 -1
- package/src/lobby/view.ts +140 -22
- package/src/master/master.ts +95 -29
- package/src/pi/commands.ts +4 -2
- package/src/pi/events.ts +18 -4
- package/src/pi/fresh-context.ts +134 -0
- package/src/pi/owner.ts +19 -10
- package/src/pi/run-summary.ts +3 -1
- package/src/pi/settings-ui.ts +138 -2
- package/src/pi/start-task.ts +7 -0
- package/src/pi/tools.ts +7 -2
- package/src/pi/ui.ts +3 -2
- package/src/pi/zen-large.ts +10 -10
- package/src/pi/zen.ts +6 -6
- package/src/schemas/configuration.ts +129 -1
- package/src/schemas/findings.ts +4 -0
- package/src/schemas/task.ts +22 -0
- package/src/state/archive.ts +12 -3
- package/src/state/backlog.ts +13 -3
- package/src/state/file-cache.ts +62 -0
- package/src/state/metrics.ts +137 -14
- package/src/state/persistence.ts +36 -2
- package/src/width.ts +102 -0
- package/src/workflow/workflow.ts +49 -1
|
@@ -55,6 +55,9 @@ export interface AgentRequest {
|
|
|
55
55
|
onStart?: (handle: AgentHandle) => void;
|
|
56
56
|
/** Called once per attempt after it ends, before any retry. */
|
|
57
57
|
onAttemptEnd?: (run: AgentRun) => void;
|
|
58
|
+
/** The classifier routed this run down from this configured profile (receipts and metrics show it). */
|
|
59
|
+
routedFrom?: string;
|
|
60
|
+
route?: string;
|
|
58
61
|
}
|
|
59
62
|
|
|
60
63
|
/** The watchdog fields of an AgentRequest, taken from the workflow config. */
|
|
@@ -115,6 +118,8 @@ function baseRun(request: AgentRequest, runId: string, startedAt: string, attemp
|
|
|
115
118
|
attempts,
|
|
116
119
|
startedAt,
|
|
117
120
|
...(request.thinking ? { thinking: request.thinking } : {}),
|
|
121
|
+
...(request.routedFrom ? { routedFrom: request.routedFrom } : {}),
|
|
122
|
+
...(request.route ? { route: request.route } : {}),
|
|
118
123
|
...(attempts > 1 ? { note: `retry ${attempts - 1} of ${(request.retries ?? 0)}`, noteKind: "warning" as const } : {}),
|
|
119
124
|
};
|
|
120
125
|
}
|
package/src/index.ts
CHANGED
|
@@ -8,8 +8,15 @@ import { isSubagentProcess } from "./pi/quiet.ts";
|
|
|
8
8
|
import { registerDeskClient } from "./desk/client-extension.ts";
|
|
9
9
|
import { registerLobbyEvents } from "./lobby/runtime.ts";
|
|
10
10
|
import { registerOwner } from "./pi/owner.ts";
|
|
11
|
+
import { registerClassifier } from "./classifier/instance.ts";
|
|
12
|
+
import { registerClassifierTools } from "./classifier/tools.ts";
|
|
13
|
+
import { registerFreshContext } from "./pi/fresh-context.ts";
|
|
11
14
|
|
|
12
15
|
export default function (pi: ExtensionAPI): void {
|
|
16
|
+
// First, so every session_start handler below finds the classifier bound to this session's keys.
|
|
17
|
+
registerClassifier(pi, CONFIG_DIR_NAME);
|
|
18
|
+
// Before the lifecycle, so a task that ended while the oracle was idle is closed before it builds the next turn's prompt.
|
|
19
|
+
registerFreshContext(pi, CONFIG_DIR_NAME);
|
|
13
20
|
registerLifecycle(pi, CONFIG_DIR_NAME);
|
|
14
21
|
// After the lifecycle, so the lobby opens over a task the widget state already knows.
|
|
15
22
|
registerLobbyEvents(pi, CONFIG_DIR_NAME);
|
|
@@ -19,4 +26,6 @@ export default function (pi: ExtensionAPI): void {
|
|
|
19
26
|
registerOrchestrateTool(pi, CONFIG_DIR_NAME);
|
|
20
27
|
// Parallel workers share files through the master's file desk.
|
|
21
28
|
if (isSubagentProcess()) registerDeskClient(pi);
|
|
29
|
+
// Subagents look files up with the classifier (the engine allows the tool only while file hints are on).
|
|
30
|
+
if (isSubagentProcess()) registerClassifierTools(pi);
|
|
22
31
|
}
|
package/src/lobby/feed.ts
CHANGED
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import type { AgentRun } from "../schemas/findings.ts";
|
|
9
9
|
import { agentName } from "../pi/run-summary.ts";
|
|
10
|
+
import { markdownBlocks } from "./markdown.ts";
|
|
10
11
|
|
|
11
12
|
export type ActivityKind = "info" | "success" | "warning" | "error";
|
|
12
13
|
|
|
@@ -43,9 +44,12 @@ export interface ChatEntry {
|
|
|
43
44
|
|
|
44
45
|
export const MAX_ACTIVITY = 400;
|
|
45
46
|
export const MAX_THOUGHTS = 40;
|
|
46
|
-
|
|
47
|
+
/** Messages the conversation keeps in memory; earlier ones are loaded from the session when scrolled to. */
|
|
48
|
+
export const MAX_CHAT = 100;
|
|
47
49
|
/** Longest thought kept; a streaming thought keeps its newest text. */
|
|
48
50
|
export const MAX_THOUGHT_TEXT = 4000;
|
|
51
|
+
/** The streaming reply kept: several panes' worth; past it, whole blocks are dropped from its start. */
|
|
52
|
+
export const MAX_REPLY_TEXT = 8000;
|
|
49
53
|
|
|
50
54
|
function bounded<T>(list: T[], max: number): T[] {
|
|
51
55
|
return list.length > max ? list.slice(-max) : list;
|
|
@@ -57,6 +61,8 @@ export class LobbyFeed {
|
|
|
57
61
|
chat: ChatEntry[] = [];
|
|
58
62
|
/** The oracle's reply while it streams; cleared when the message ends. */
|
|
59
63
|
reply = "";
|
|
64
|
+
/** Earlier messages than `chat` holds exist in the session (loaded only when scrolled back to). */
|
|
65
|
+
chatOlder = false;
|
|
60
66
|
version = 0;
|
|
61
67
|
private nextId = 1;
|
|
62
68
|
private readonly listeners = new Set<() => void>();
|
|
@@ -90,16 +96,22 @@ export class LobbyFeed {
|
|
|
90
96
|
|
|
91
97
|
/** A one-off entry that is already done (a receipt, a transition, a note). */
|
|
92
98
|
log(source: string, text: string, kind: ActivityKind = "info", at = Date.now()): void {
|
|
93
|
-
this.
|
|
99
|
+
this.pushActivity({ id: this.nextId++, at, source, text, kind, pending: false });
|
|
94
100
|
this.touch();
|
|
95
101
|
}
|
|
96
102
|
|
|
97
103
|
/** Open an in-flight step; `key` lets `end` settle exactly this one. */
|
|
98
104
|
begin(source: string, text: string, key?: string, at = Date.now()): void {
|
|
99
|
-
this.
|
|
105
|
+
this.pushActivity({ id: this.nextId++, at, source, text, kind: "info", pending: true, ...(key ? { key } : {}) });
|
|
100
106
|
this.touch();
|
|
101
107
|
}
|
|
102
108
|
|
|
109
|
+
/** Append in place (a busy agent logs many steps a second), dropping the oldest past the limit. */
|
|
110
|
+
private pushActivity(entry: ActivityEntry): void {
|
|
111
|
+
this.activity.push(entry);
|
|
112
|
+
if (this.activity.length > MAX_ACTIVITY) this.activity.splice(0, this.activity.length - MAX_ACTIVITY);
|
|
113
|
+
}
|
|
114
|
+
|
|
103
115
|
/** Settle the step opened with `key`; an error marks it. */
|
|
104
116
|
end(key: string, isError = false): void {
|
|
105
117
|
if (this.settle(undefined, key, isError ? "error" : undefined)) this.touch();
|
|
@@ -116,7 +128,7 @@ export class LobbyFeed {
|
|
|
116
128
|
const last = this.thoughts.at(-1);
|
|
117
129
|
if (last && last.live && last.source === source) {
|
|
118
130
|
const text = last.text + delta;
|
|
119
|
-
last.text = text.length > MAX_THOUGHT_TEXT ? text
|
|
131
|
+
last.text = text.length > MAX_THOUGHT_TEXT ? trimThought(text) : text;
|
|
120
132
|
} else {
|
|
121
133
|
this.thoughts = bounded([...this.thoughts, { id: this.nextId++, at, source, text: delta.slice(-MAX_THOUGHT_TEXT), live: true }], MAX_THOUGHTS);
|
|
122
134
|
}
|
|
@@ -146,7 +158,7 @@ export class LobbyFeed {
|
|
|
146
158
|
|
|
147
159
|
/** Stream the oracle's reply text as it arrives. */
|
|
148
160
|
replyDelta(delta: string): void {
|
|
149
|
-
this.reply = (this.reply + delta)
|
|
161
|
+
this.reply = trimReply(this.reply + delta);
|
|
150
162
|
this.touch();
|
|
151
163
|
}
|
|
152
164
|
|
|
@@ -160,13 +172,19 @@ export class LobbyFeed {
|
|
|
160
172
|
say(role: ChatRole, text: string, at = Date.now()): void {
|
|
161
173
|
const body = text.trim();
|
|
162
174
|
if (!body) return;
|
|
163
|
-
this.chat
|
|
175
|
+
this.chat.push({ id: this.nextId++, at, role, text: body });
|
|
176
|
+
if (this.chat.length > MAX_CHAT) {
|
|
177
|
+
this.chat.splice(0, this.chat.length - MAX_CHAT);
|
|
178
|
+
this.chatOlder = true;
|
|
179
|
+
}
|
|
164
180
|
this.touch();
|
|
165
181
|
}
|
|
166
182
|
|
|
167
|
-
/** Replace the conversation (seeding from the session when the lobby first opens). */
|
|
183
|
+
/** Replace the conversation (seeding from the session when the lobby first opens); only the newest `MAX_CHAT` are kept. */
|
|
168
184
|
seedChat(entries: ReadonlyArray<{ role: ChatRole; text: string; at?: number }>): void {
|
|
169
|
-
|
|
185
|
+
const kept = entries.filter((entry) => entry.text.trim());
|
|
186
|
+
this.chatOlder = kept.length > MAX_CHAT;
|
|
187
|
+
this.chat = kept.slice(-MAX_CHAT).map((entry) => ({ id: this.nextId++, at: entry.at ?? 0, role: entry.role, text: entry.text.trim() }));
|
|
170
188
|
this.touch();
|
|
171
189
|
}
|
|
172
190
|
|
|
@@ -202,6 +220,7 @@ export class LobbyFeed {
|
|
|
202
220
|
this.activity = [];
|
|
203
221
|
this.thoughts = [];
|
|
204
222
|
this.chat = [];
|
|
223
|
+
this.chatOlder = false;
|
|
205
224
|
this.reply = "";
|
|
206
225
|
this.runSteps.clear();
|
|
207
226
|
this.runThoughts.clear();
|
|
@@ -210,6 +229,38 @@ export class LobbyFeed {
|
|
|
210
229
|
}
|
|
211
230
|
}
|
|
212
231
|
|
|
232
|
+
/** A streaming thought past `MAX_THOUGHT_TEXT` cut to three quarters of it from a word's start, so its start then holds still a while. */
|
|
233
|
+
function trimThought(text: string): string {
|
|
234
|
+
const cut = text.slice(-Math.floor(MAX_THOUGHT_TEXT * 0.75));
|
|
235
|
+
const space = cut.search(/\s/);
|
|
236
|
+
return space >= 0 && space < 80 ? cut.slice(space + 1) : cut;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* A streaming reply past `max` characters cut down to three quarters of that
|
|
241
|
+
* (so its start then holds still for a while) by dropping whole Markdown
|
|
242
|
+
* blocks from its start, so what is left renders as it did (a cut inside a
|
|
243
|
+
* code block would turn the rest of the reply inside out). A single block
|
|
244
|
+
* longer than that keeps its end, and its opening fence if it is code.
|
|
245
|
+
*/
|
|
246
|
+
export function trimReply(text: string, max = MAX_REPLY_TEXT): string {
|
|
247
|
+
if (text.length <= max) return text;
|
|
248
|
+
const target = Math.floor(max * 0.75);
|
|
249
|
+
const blocks = markdownBlocks(text);
|
|
250
|
+
let length = text.length;
|
|
251
|
+
let first = 0;
|
|
252
|
+
// Each dropped block takes the blank line after it too.
|
|
253
|
+
while (first < blocks.length - 1 && length > target) length -= blocks[first++]!.length + 2;
|
|
254
|
+
if (length <= target) return blocks.slice(first).join("\n\n");
|
|
255
|
+
const last = blocks.at(-1) ?? text;
|
|
256
|
+
const opener = /^ {0,3}(?:`{3,}|~{3,}).*\n/.exec(last)?.[0] ?? "";
|
|
257
|
+
const rest = last.slice(opener.length);
|
|
258
|
+
const cut = rest.slice(-(target - opener.length));
|
|
259
|
+
// From a line start, so no line (or code) is shown half cut.
|
|
260
|
+
const line = cut.indexOf("\n");
|
|
261
|
+
return `${opener}${line >= 0 && line < cut.length - 1 ? cut.slice(line + 1) : cut}`;
|
|
262
|
+
}
|
|
263
|
+
|
|
213
264
|
/** The process-wide lobby feed; the Master session is the only writer that matters. */
|
|
214
265
|
export const lobbyFeed = new LobbyFeed();
|
|
215
266
|
|
|
@@ -240,11 +291,28 @@ export function chatText(role: "user" | "assistant", text: string): Array<{ role
|
|
|
240
291
|
return [{ role: "you", text: body }];
|
|
241
292
|
}
|
|
242
293
|
|
|
294
|
+
/** The session entry bot-lobby leaves where the oracle's context starts over (see pi/fresh-context.ts). */
|
|
295
|
+
export const CONTEXT_MARK = "bot-lobby-context";
|
|
296
|
+
|
|
297
|
+
/** What the conversation shows where a task's end cleared the oracle's context. */
|
|
298
|
+
export const CLEARED_NOTE = "context cleared · the next request starts fresh";
|
|
299
|
+
|
|
300
|
+
/** Whether a session entry is where a finished task cleared the oracle's context. */
|
|
301
|
+
export function isClearedMark(entry: unknown): boolean {
|
|
302
|
+
const record = entry as { type?: string; customType?: string; data?: { kind?: unknown } } | undefined;
|
|
303
|
+
return record?.type === "custom" && record.customType === CONTEXT_MARK && record.data?.kind === "end";
|
|
304
|
+
}
|
|
305
|
+
|
|
243
306
|
/** Session entries (`{ type: "message", message }`) as conversation entries, oldest first. */
|
|
244
307
|
export function chatFromEntries(entries: readonly unknown[], max = MAX_CHAT): Array<{ role: ChatRole; text: string; at?: number }> {
|
|
245
308
|
const chat: Array<{ role: ChatRole; text: string; at?: number }> = [];
|
|
246
309
|
for (const entry of entries) {
|
|
247
310
|
const record = entry as { type?: string; timestamp?: string; message?: { role?: string; content?: unknown } };
|
|
311
|
+
if (isClearedMark(entry)) {
|
|
312
|
+
const at = record.timestamp ? Date.parse(record.timestamp) : Number.NaN;
|
|
313
|
+
chat.push({ role: "note", text: CLEARED_NOTE, ...(Number.isFinite(at) ? { at } : {}) });
|
|
314
|
+
continue;
|
|
315
|
+
}
|
|
248
316
|
if (record?.type !== "message") continue;
|
|
249
317
|
const role = record.message?.role;
|
|
250
318
|
if (role !== "user" && role !== "assistant") continue;
|
package/src/lobby/layout.ts
CHANGED
|
@@ -4,7 +4,8 @@
|
|
|
4
4
|
* its widths and heights explicitly and never reads a clock or the terminal,
|
|
5
5
|
* so each tab renders deterministically in tests.
|
|
6
6
|
*/
|
|
7
|
-
import { truncateToWidth,
|
|
7
|
+
import { truncateToWidth, wrapTextWithAnsi } from "@earendil-works/pi-tui";
|
|
8
|
+
import { textWidth } from "../width.ts";
|
|
8
9
|
|
|
9
10
|
/** Colours the lobby uses; a subset of pi's theme so tests can pass a plain stub. */
|
|
10
11
|
export type LobbyColor =
|
|
@@ -29,7 +30,7 @@ export interface LobbyTheme {
|
|
|
29
30
|
italic?(text: string): string;
|
|
30
31
|
bg?(color: "selectedBg" | "searchMatchBg" | "userMessageBg", text: string): string;
|
|
31
32
|
/** Render Markdown to styled lines; plain wrapping without it (tests). */
|
|
32
|
-
markdown?(text: string, width: number): string[];
|
|
33
|
+
markdown?(text: string, width: number, keep?: boolean): string[];
|
|
33
34
|
/** Strike text through (an abandoned task's title); plain without it (tests). */
|
|
34
35
|
strike?(text: string): string;
|
|
35
36
|
}
|
|
@@ -54,7 +55,7 @@ export function strike(theme: LobbyTheme | undefined, text: string): string {
|
|
|
54
55
|
/** `left` and `right` on one line of exactly `width` columns: the left side gives way first. */
|
|
55
56
|
export function spread(left: string, right: string, width: number): string {
|
|
56
57
|
if (!right) return fit(left, width);
|
|
57
|
-
const room = width -
|
|
58
|
+
const room = width - textWidth(right) - 1;
|
|
58
59
|
if (room < 4) return fit(left, width);
|
|
59
60
|
return `${fit(left, room)} ${right}`;
|
|
60
61
|
}
|
|
@@ -62,8 +63,8 @@ export function spread(left: string, right: string, width: number): string {
|
|
|
62
63
|
/** Exactly `width` columns: truncated with an ellipsis, or padded with spaces. */
|
|
63
64
|
export function fit(text: string, width: number): string {
|
|
64
65
|
if (width <= 0) return "";
|
|
65
|
-
const cut =
|
|
66
|
-
const pad = width -
|
|
66
|
+
const cut = textWidth(text) > width ? truncateToWidth(text, width, "…") : text;
|
|
67
|
+
const pad = width - textWidth(cut);
|
|
67
68
|
return pad > 0 ? `${cut}${" ".repeat(pad)}` : cut;
|
|
68
69
|
}
|
|
69
70
|
|
|
@@ -83,7 +84,7 @@ export function wrap(text: string, width: number): string[] {
|
|
|
83
84
|
|
|
84
85
|
/** Wrap with a hanging indent: the first line carries `lead`, the rest align under it. */
|
|
85
86
|
export function wrapHanging(lead: string, text: string, width: number): string[] {
|
|
86
|
-
const indent =
|
|
87
|
+
const indent = textWidth(lead);
|
|
87
88
|
const body = wrap(text, Math.max(1, width - indent));
|
|
88
89
|
if (body.length === 0) return [lead];
|
|
89
90
|
return body.map((line, index) => `${index === 0 ? lead : " ".repeat(indent)}${line}`);
|
|
@@ -94,7 +95,7 @@ export function rule(width: number, title = "", theme?: LobbyTheme, right = "",
|
|
|
94
95
|
if (width <= 0) return "";
|
|
95
96
|
const head = title ? `── ${title} ` : "";
|
|
96
97
|
const tail = right ? ` ${right} ──` : "";
|
|
97
|
-
const fill = width -
|
|
98
|
+
const fill = width - textWidth(head) - textWidth(tail);
|
|
98
99
|
if (fill < 1) return fit(paint(theme, color, head.trimEnd() || "─".repeat(width)), width);
|
|
99
100
|
const titled = title ? `${paint(theme, color, "── ")}${bold(theme, paint(theme, "accent", title))}${paint(theme, color, " ")}` : "";
|
|
100
101
|
const ending = right ? `${paint(theme, color, " ")}${paint(theme, "muted", right)}${paint(theme, color, " ──")}` : "";
|
|
@@ -183,8 +184,8 @@ export function spinner(tick: number): string {
|
|
|
183
184
|
}
|
|
184
185
|
|
|
185
186
|
/** Markdown for plans, reports and replies: the theme's renderer, or headings bold without their `#` and the rest wrapped. */
|
|
186
|
-
export function markdownLines(text: string, width: number, theme?: LobbyTheme): string[] {
|
|
187
|
-
if (theme?.markdown) return theme.markdown(text, width);
|
|
187
|
+
export function markdownLines(text: string, width: number, theme?: LobbyTheme, keep = true): string[] {
|
|
188
|
+
if (theme?.markdown) return theme.markdown(text, width, keep);
|
|
188
189
|
return text.split("\n").flatMap((line) => {
|
|
189
190
|
const heading = /^#{1,6}\s+(.*)$/.exec(line);
|
|
190
191
|
return wrap(heading ? bold(theme, paint(theme, "mdHeading", heading[1]!)) : line, width);
|
|
@@ -193,7 +194,7 @@ export function markdownLines(text: string, width: number, theme?: LobbyTheme):
|
|
|
193
194
|
|
|
194
195
|
/** Markdown behind a hanging lead (`oracle ▸ `): the first line carries the lead, the rest align under it. */
|
|
195
196
|
export function markdownHanging(lead: string, text: string, width: number, theme?: LobbyTheme): string[] {
|
|
196
|
-
const indent =
|
|
197
|
+
const indent = textWidth(lead);
|
|
197
198
|
const body = markdownLines(text, Math.max(1, width - indent), theme);
|
|
198
199
|
if (body.length === 0) return [lead];
|
|
199
200
|
return body.map((line, index) => (index === 0 ? `${lead}${line}` : line ? `${" ".repeat(indent)}${line}` : ""));
|
|
@@ -261,11 +262,11 @@ export function box(width: number, height: number, content: readonly string[], o
|
|
|
261
262
|
const inner = width - 4;
|
|
262
263
|
const title = options.title ? ` ${options.title} ` : "";
|
|
263
264
|
const right = options.right ? ` ${options.right} ` : "";
|
|
264
|
-
let room = width - 2 -
|
|
265
|
+
let room = width - 2 - textWidth(title) - textWidth(right);
|
|
265
266
|
const shownRight = room >= 1 ? right : "";
|
|
266
|
-
room = width - 2 -
|
|
267
|
+
room = width - 2 - textWidth(title) - textWidth(shownRight);
|
|
267
268
|
const titled = room >= 1 ? title : fit(title, Math.max(0, width - 3));
|
|
268
|
-
const fillWidth = Math.max(0, width - 2 -
|
|
269
|
+
const fillWidth = Math.max(0, width - 2 - textWidth(titled) - textWidth(shownRight));
|
|
269
270
|
const paintedTitle = titled ? bold(theme, paint(theme, focused ? "accent" : "text", titled)) : "";
|
|
270
271
|
const top = `${edge("╭")}${paintedTitle}${edge("─".repeat(fillWidth))}${shownRight ? paint(theme, "dim", shownRight) : ""}${edge("╮")}`;
|
|
271
272
|
const thumb = options.scroll ? scrollThumb(options.scroll.total, height - 2, options.scroll.start) : undefined;
|
package/src/lobby/markdown.ts
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Markdown for the lobby: pi's own renderer (headings, emphasis, lists, code
|
|
3
3
|
* with syntax highlighting, quotes, tables) under the session's theme, with
|
|
4
|
-
* heading hashes dropped for a cleaner read. Renders are cached per
|
|
5
|
-
*
|
|
4
|
+
* heading hashes dropped for a cleaner read. Renders are cached per width and
|
|
5
|
+
* text, since the lobby repaints many times a second.
|
|
6
6
|
*/
|
|
7
7
|
import { getMarkdownTheme } from "@earendil-works/pi-coding-agent";
|
|
8
8
|
import { Markdown, stripTerminalSequences, type MarkdownTheme } from "@earendil-works/pi-tui";
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
const CACHE_LIMIT = 96;
|
|
10
|
+
/** Markdown rendered to lines at a width; `keep: false` for text seen once (a reply still streaming), so it does not crowd the cache. */
|
|
11
|
+
export type MarkdownRenderer = (text: string, width: number, keep?: boolean) => string[];
|
|
13
12
|
|
|
14
13
|
/** A heading line as pi renders it: optional styling, then `#`…`######` and a space. */
|
|
15
14
|
const HEADING_HASHES = /^((?:\x1b\[[0-9;]*m)*)#{1,6} ((?:\x1b\[[0-9;]*m)*)/;
|
|
@@ -21,28 +20,72 @@ export function tidyHeading(line: string, bold: (text: string) => string): strin
|
|
|
21
20
|
return bold(body.trimEnd());
|
|
22
21
|
}
|
|
23
22
|
|
|
23
|
+
/** Rendered Markdown kept per renderer: the most recently used survive. */
|
|
24
|
+
export const CACHE_LIMIT = 512;
|
|
25
|
+
|
|
24
26
|
/**
|
|
25
|
-
* A renderer
|
|
26
|
-
*
|
|
27
|
+
* A renderer for one Markdown theme. Renders are kept per width and text, the
|
|
28
|
+
* least recently used dropped first. pi's `getMarkdownTheme()` builds a new
|
|
29
|
+
* theme object on every call, so the theme is taken once, here: a theme
|
|
30
|
+
* switch makes a new renderer (the lobby keeps one per pi theme).
|
|
27
31
|
*/
|
|
28
|
-
export function createMarkdownRenderer(theme:
|
|
29
|
-
const
|
|
30
|
-
return (text, width) => {
|
|
31
|
-
const current = theme();
|
|
32
|
-
let cache = caches.get(current);
|
|
33
|
-
if (!cache) {
|
|
34
|
-
cache = new Map();
|
|
35
|
-
caches.set(current, cache);
|
|
36
|
-
}
|
|
32
|
+
export function createMarkdownRenderer(theme: MarkdownTheme = getMarkdownTheme()): MarkdownRenderer {
|
|
33
|
+
const cache = new Map<string, string[]>();
|
|
34
|
+
return (text, width, keep = true) => {
|
|
37
35
|
const key = `${width}\0${text}`;
|
|
38
36
|
const hit = cache.get(key);
|
|
39
|
-
if (hit)
|
|
40
|
-
|
|
37
|
+
if (hit) {
|
|
38
|
+
// Most recently used last, so the oldest is the one dropped.
|
|
39
|
+
cache.delete(key);
|
|
40
|
+
cache.set(key, hit);
|
|
41
|
+
return hit;
|
|
42
|
+
}
|
|
43
|
+
const lines = new Markdown(text, 0, 0, theme).render(Math.max(1, width)).map((line) => tidyHeading(line.trimEnd(), theme.bold));
|
|
41
44
|
// Drop the blank lines pi pads the render with at either end.
|
|
42
45
|
while (lines.length > 0 && !lines[0]!.trim()) lines.shift();
|
|
43
46
|
while (lines.length > 0 && !lines.at(-1)!.trim()) lines.pop();
|
|
47
|
+
if (!keep) return lines;
|
|
44
48
|
cache.set(key, lines);
|
|
45
49
|
if (cache.size > CACHE_LIMIT) cache.delete(cache.keys().next().value!);
|
|
46
50
|
return lines;
|
|
47
51
|
};
|
|
48
52
|
}
|
|
53
|
+
|
|
54
|
+
/** A fenced code block's opening or closing line. */
|
|
55
|
+
const FENCE = /^ {0,3}(`{3,}|~{3,})/;
|
|
56
|
+
/** A list item's first line: after a blank line it continues the list above, so blocks never split there. */
|
|
57
|
+
const LIST_ITEM = /^(?:[-*+]|\d{1,9}[.)])(?:\s|$)/;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Markdown split into blocks that render, one by one, into the same lines as
|
|
61
|
+
* the whole (each block's lines, a blank line between blocks, blocks that
|
|
62
|
+
* render to nothing left out). A split falls on a blank line outside code
|
|
63
|
+
* fences that is followed by an unindented line that does not start a list
|
|
64
|
+
* item — the only places where one block can never run into the next. A
|
|
65
|
+
* streaming reply can then keep its finished blocks rendered and render only
|
|
66
|
+
* the one still being written.
|
|
67
|
+
*/
|
|
68
|
+
export function markdownBlocks(text: string): string[] {
|
|
69
|
+
const lines = text.split("\n");
|
|
70
|
+
const blocks: string[] = [];
|
|
71
|
+
let fence: string | undefined;
|
|
72
|
+
let start = 0;
|
|
73
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
74
|
+
const line = lines[index]!;
|
|
75
|
+
const marker = FENCE.exec(line)?.[1];
|
|
76
|
+
if (marker) {
|
|
77
|
+
if (!fence) fence = marker;
|
|
78
|
+
else if (marker[0] === fence[0] && marker.length >= fence.length) fence = undefined;
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
if (fence || line.trim() || index === 0) continue;
|
|
82
|
+
const next = lines[index + 1];
|
|
83
|
+
if (next === undefined || !/^\S/.test(next) || LIST_ITEM.test(next)) continue;
|
|
84
|
+
const block = lines.slice(start, index).join("\n");
|
|
85
|
+
if (block.trim()) blocks.push(block);
|
|
86
|
+
start = index + 1;
|
|
87
|
+
}
|
|
88
|
+
const last = lines.slice(start).join("\n");
|
|
89
|
+
if (last.trim()) blocks.push(last);
|
|
90
|
+
return blocks;
|
|
91
|
+
}
|