blun-king-cli 9.1.653 → 9.1.655
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/LIESMICH.txt +1 -1
- package/README.md +1 -1
- package/blun.mjs +1938 -420
- package/original-history-runtime.mjs +1420 -0
- package/original-history-tui.mjs +879 -0
- package/original-markdown-runtime.mjs +291 -0
- package/package.json +1 -1
- package/telegram-plugin/dist/bridge.mjs +33 -0
- package/telegram-plugin/dist/console-control.mjs +388 -0
- package/telegram-plugin/dist/mcp-server.mjs +1 -0
- package/worker-host.mjs +889 -109
|
@@ -0,0 +1,879 @@
|
|
|
1
|
+
// history-ui:helpers
|
|
2
|
+
var Input;
|
|
3
|
+
var Key;
|
|
4
|
+
var matchesKey;
|
|
5
|
+
var isKeyRelease;
|
|
6
|
+
var truncateToWidth;
|
|
7
|
+
var visibleWidth;
|
|
8
|
+
var decodeKittyPrintable;
|
|
9
|
+
var fuzzyFilter;
|
|
10
|
+
var copyTextToClipboard;
|
|
11
|
+
var toTerminalHyperlink;
|
|
12
|
+
function useHistoryUi(api) {
|
|
13
|
+
if (api.Input == null) throw new Error("Missing UI dependency: Input");
|
|
14
|
+
if (api.Key == null) throw new Error("Missing UI dependency: Key");
|
|
15
|
+
if (api.matchesKey == null) throw new Error("Missing UI dependency: matchesKey");
|
|
16
|
+
if (api.isKeyRelease == null) throw new Error("Missing UI dependency: isKeyRelease");
|
|
17
|
+
if (api.truncateToWidth == null) throw new Error("Missing UI dependency: truncateToWidth");
|
|
18
|
+
if (api.visibleWidth == null) throw new Error("Missing UI dependency: visibleWidth");
|
|
19
|
+
if (api.decodeKittyPrintable == null) throw new Error("Missing UI dependency: decodeKittyPrintable");
|
|
20
|
+
if (api.fuzzyFilter == null) throw new Error("Missing UI dependency: fuzzyFilter");
|
|
21
|
+
if (api.copyTextToClipboard == null) throw new Error("Missing UI dependency: copyTextToClipboard");
|
|
22
|
+
if (api.toTerminalHyperlink == null) throw new Error("Missing UI dependency: toTerminalHyperlink");
|
|
23
|
+
({ Input, Key, matchesKey, isKeyRelease, truncateToWidth, visibleWidth, decodeKittyPrintable, fuzzyFilter, copyTextToClipboard, toTerminalHyperlink } = api);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// apps/blun-king/src/tui/utils/printable-key.ts
|
|
27
|
+
function printableChar(data) {
|
|
28
|
+
return decodeKittyPrintable(data) ?? data;
|
|
29
|
+
}
|
|
30
|
+
function isPrintableChar(ch) {
|
|
31
|
+
if (ch.length !== 1) return false;
|
|
32
|
+
const code = ch.codePointAt(0);
|
|
33
|
+
return code >= 32 && code !== 127;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// apps/blun-king/src/tui/controllers/original-history-attachments.ts
|
|
37
|
+
import { pathToFileURL } from "node:url";
|
|
38
|
+
var OriginalHistoryAttachments = class {
|
|
39
|
+
constructor(view, entry, owner, refresh) {
|
|
40
|
+
this.view = view;
|
|
41
|
+
this.refresh = refresh;
|
|
42
|
+
this.signal = AbortSignal.any([owner, this.abort.signal]);
|
|
43
|
+
void this.load(entry);
|
|
44
|
+
}
|
|
45
|
+
view;
|
|
46
|
+
refresh;
|
|
47
|
+
abort = new AbortController();
|
|
48
|
+
signal;
|
|
49
|
+
items = [];
|
|
50
|
+
selected = 0;
|
|
51
|
+
loading = true;
|
|
52
|
+
notice;
|
|
53
|
+
file;
|
|
54
|
+
close() {
|
|
55
|
+
this.abort.abort();
|
|
56
|
+
}
|
|
57
|
+
handleInput(data) {
|
|
58
|
+
if (this.loading || this.items.length === 0) return;
|
|
59
|
+
if (matchesKey(data, Key.enter)) {
|
|
60
|
+
void this.open(this.items[this.selected]);
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
const delta = matchesKey(data, Key.up) ? -1 : matchesKey(data, Key.down) ? 1 : 0;
|
|
64
|
+
if (delta === 0) return;
|
|
65
|
+
this.selected = Math.max(0, Math.min(this.selected + delta, this.items.length - 1));
|
|
66
|
+
this.file = void 0;
|
|
67
|
+
this.notice = void 0;
|
|
68
|
+
}
|
|
69
|
+
render(height, theme) {
|
|
70
|
+
const rows = [theme.fg("textMuted", " \u2191\u2193 navigate \xB7 Enter prepare file link \xB7 Esc back")];
|
|
71
|
+
if (this.loading) rows.push(theme.fg("textMuted", " Reading stored attachments\u2026"));
|
|
72
|
+
else if (this.notice !== void 0) rows.push(theme.fg("textMuted", ` ${this.notice}`));
|
|
73
|
+
if (this.file !== void 0) {
|
|
74
|
+
const label = this.items[this.selected]?.original === true ? "Open original image" : "Open stored file";
|
|
75
|
+
rows.push(` ${toTerminalHyperlink(theme.fg("primary", label), pathToFileURL(this.file.path).href)}`);
|
|
76
|
+
}
|
|
77
|
+
const count = Math.max(0, height - rows.length);
|
|
78
|
+
const start = Math.max(0, this.selected - count + 1);
|
|
79
|
+
rows.push(...this.items.slice(start, start + count).map((item, index) => {
|
|
80
|
+
const active = start + index === this.selected;
|
|
81
|
+
const label = item.original ? "Original image" : `Stored ${item.kind}`;
|
|
82
|
+
return theme.fg(active ? "primary" : "text", `${active ? " \u203A " : " "}${String(start + index + 1)}. ${label}`);
|
|
83
|
+
}));
|
|
84
|
+
return rows.slice(0, height);
|
|
85
|
+
}
|
|
86
|
+
async load(entry) {
|
|
87
|
+
try {
|
|
88
|
+
this.items = await this.view.attachments(entry, this.signal);
|
|
89
|
+
if (this.items.length === 0) this.notice = "This record has no stored attachments.";
|
|
90
|
+
} catch {
|
|
91
|
+
this.notice = "Could not read attachments for this record.";
|
|
92
|
+
} finally {
|
|
93
|
+
this.finish();
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
async open(attachment) {
|
|
97
|
+
this.loading = true;
|
|
98
|
+
this.notice = void 0;
|
|
99
|
+
this.file = void 0;
|
|
100
|
+
this.refresh();
|
|
101
|
+
try {
|
|
102
|
+
this.file = await this.view.readAttachment(attachment, this.signal);
|
|
103
|
+
if (this.file === void 0) {
|
|
104
|
+
this.notice = attachment.original ? "The original image is unavailable." : "The stored file is unavailable.";
|
|
105
|
+
}
|
|
106
|
+
} catch {
|
|
107
|
+
this.notice = "Could not read the stored file.";
|
|
108
|
+
} finally {
|
|
109
|
+
this.finish();
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
finish() {
|
|
113
|
+
this.loading = false;
|
|
114
|
+
if (!this.signal.aborted) this.refresh();
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
// apps/blun-king/src/tui/controllers/original-history-detail.ts
|
|
119
|
+
var graphemes = new Intl.Segmenter();
|
|
120
|
+
function checkpointRows(checkpoint) {
|
|
121
|
+
if (checkpoint === void 0) return void 0;
|
|
122
|
+
const { time, tokensBefore, tokensAfter, compactedCount, droppedCount, sources } = checkpoint;
|
|
123
|
+
const rows = [
|
|
124
|
+
"Compaction checkpoint",
|
|
125
|
+
`Estimated tokens: ${tokensBefore} before / ${tokensAfter} after`,
|
|
126
|
+
`Messages in context before compaction: ${compactedCount}`
|
|
127
|
+
];
|
|
128
|
+
if (time !== void 0) rows.push(`Recorded: ${new Date(time).toISOString()}`);
|
|
129
|
+
if (droppedCount !== void 0) rows.push(`Excluded from summary input: ${droppedCount}`);
|
|
130
|
+
if (sources === void 0) rows.push("Original source references were not recorded.");
|
|
131
|
+
else {
|
|
132
|
+
rows.push(`Recorded original source references: ${sources.records}`);
|
|
133
|
+
if (sources.incompleteMessages > 0) rows.push(`Messages with incomplete source references: ${sources.incompleteMessages}`);
|
|
134
|
+
if (sources.omittedRecords > 0) rows.push(`Additional source references not recorded: ${sources.omittedRecords}`);
|
|
135
|
+
}
|
|
136
|
+
return rows.join("\n");
|
|
137
|
+
}
|
|
138
|
+
function detailRows(text, start, width, rows) {
|
|
139
|
+
const lines = [];
|
|
140
|
+
let offset = start;
|
|
141
|
+
while (offset < text.length && lines.length < rows) {
|
|
142
|
+
const end = Math.min(text.length, offset + Math.max(128, width * 12));
|
|
143
|
+
const splitCRLF = text[end - 1] === "\r" && text[end] === "\n";
|
|
144
|
+
const window = text.slice(offset, end + Number(splitCRLF));
|
|
145
|
+
let columns = 0;
|
|
146
|
+
let length = 0;
|
|
147
|
+
let newline = 0;
|
|
148
|
+
for (const { segment } of graphemes.segment(window)) {
|
|
149
|
+
if (segment === "\n" || segment === "\r\n" || segment === "\r") {
|
|
150
|
+
newline = segment.length;
|
|
151
|
+
break;
|
|
152
|
+
}
|
|
153
|
+
const cells = segment === " " ? 4 : visibleWidth(segment);
|
|
154
|
+
if (length > 0 && columns + cells > width) break;
|
|
155
|
+
length += segment.length;
|
|
156
|
+
columns += cells;
|
|
157
|
+
}
|
|
158
|
+
lines.push({ start: offset, text: window.slice(0, length).replaceAll(" ", " ") });
|
|
159
|
+
offset += length + newline;
|
|
160
|
+
}
|
|
161
|
+
return lines;
|
|
162
|
+
}
|
|
163
|
+
async function readHistoryDetail(view, entry, signal, offset = 0) {
|
|
164
|
+
const checkpoint = entry.kind === "compaction" ? await view.checkpoint(entry, signal) : void 0;
|
|
165
|
+
const sourceCount = checkpoint?.sources?.records;
|
|
166
|
+
const text = entry.kind === "compaction" ? checkpointRows(checkpoint) : entry.expandable ? await view.text(entry, signal) : entry.preview;
|
|
167
|
+
return text === void 0 || text.length === 0 ? void 0 : { text, sourceCount, offset: offset < text.length ? offset : 0, previous: [] };
|
|
168
|
+
}
|
|
169
|
+
function scrollHistoryDetail(detail, width, delta) {
|
|
170
|
+
const { text, previous, offset } = detail;
|
|
171
|
+
if (delta < 0) {
|
|
172
|
+
for (let step = 0; step > delta; step--) {
|
|
173
|
+
const saved = previous.pop();
|
|
174
|
+
if (saved !== void 0) {
|
|
175
|
+
detail.offset = saved;
|
|
176
|
+
continue;
|
|
177
|
+
}
|
|
178
|
+
const before = detail.offset;
|
|
179
|
+
if (before === 0) break;
|
|
180
|
+
const start = Math.max(0, before - Math.max(128, width * 12));
|
|
181
|
+
const lines = detailRows(text.slice(0, before), start, width, Number.MAX_SAFE_INTEGER);
|
|
182
|
+
detail.offset = lines.at(-1)?.start ?? start;
|
|
183
|
+
}
|
|
184
|
+
} else if (delta > 0) {
|
|
185
|
+
const lines = detailRows(text, offset, width, delta + 1);
|
|
186
|
+
if (lines.length > 1) {
|
|
187
|
+
previous.push(...lines.slice(0, -1).map((line) => line.start));
|
|
188
|
+
detail.offset = lines.at(-1).start;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// apps/blun-king/src/tui/controllers/original-history-panel-state.ts
|
|
194
|
+
var ORIGINAL_HISTORY_LABELS = {
|
|
195
|
+
user: "You",
|
|
196
|
+
assistant: "Assistant",
|
|
197
|
+
tool: "Tool",
|
|
198
|
+
output: "Output",
|
|
199
|
+
compaction: "Context compacted",
|
|
200
|
+
clear: "Context cleared",
|
|
201
|
+
undo: "Prompt withdrawn",
|
|
202
|
+
large: "Large record",
|
|
203
|
+
unavailable: "Record unavailable"
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
// apps/blun-king/src/tui/controllers/original-history-search.ts
|
|
207
|
+
var OriginalHistorySearch = class {
|
|
208
|
+
constructor(view, owner, refresh) {
|
|
209
|
+
this.view = view;
|
|
210
|
+
this.refresh = refresh;
|
|
211
|
+
const signal = AbortSignal.any([owner, this.abort.signal]);
|
|
212
|
+
this.signal = signal;
|
|
213
|
+
this.input.focused = true;
|
|
214
|
+
this.input.onSubmit = (value) => {
|
|
215
|
+
const query = value.trim();
|
|
216
|
+
if (query === "") return;
|
|
217
|
+
this.query = query;
|
|
218
|
+
void this.load(void 0);
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
view;
|
|
222
|
+
refresh;
|
|
223
|
+
input = new Input();
|
|
224
|
+
abort = new AbortController();
|
|
225
|
+
previous = [];
|
|
226
|
+
query;
|
|
227
|
+
result;
|
|
228
|
+
selected = 0;
|
|
229
|
+
loading = false;
|
|
230
|
+
notice;
|
|
231
|
+
signal;
|
|
232
|
+
close() {
|
|
233
|
+
this.abort.abort();
|
|
234
|
+
}
|
|
235
|
+
handleInput(data, height) {
|
|
236
|
+
if (this.query === void 0) {
|
|
237
|
+
this.input.handleInput(data);
|
|
238
|
+
return void 0;
|
|
239
|
+
}
|
|
240
|
+
if (this.loading || this.result === void 0) return void 0;
|
|
241
|
+
if (matchesKey(data, Key.enter)) return this.result.matches[this.selected];
|
|
242
|
+
const rowCount = Math.max(1, height - 2);
|
|
243
|
+
const delta = matchesKey(data, Key.up) ? -1 : matchesKey(data, Key.down) ? 1 : matchesKey(data, Key.pageUp) ? -rowCount : matchesKey(data, Key.pageDown) ? rowCount : 0;
|
|
244
|
+
const target = this.selected + delta;
|
|
245
|
+
if (target < 0 && this.selected === 0 && this.previous.length > 0) {
|
|
246
|
+
void this.load(this.previous.at(-1), "newer");
|
|
247
|
+
} else if (target >= this.result.matches.length && this.selected === this.result.matches.length - 1 && this.result.older !== void 0) {
|
|
248
|
+
void this.load(this.result.older, "older");
|
|
249
|
+
} else this.selected = Math.max(0, Math.min(target, this.result.matches.length - 1));
|
|
250
|
+
return void 0;
|
|
251
|
+
}
|
|
252
|
+
render(width, height, theme) {
|
|
253
|
+
if (this.query === void 0) {
|
|
254
|
+
const prefix = " Find: ";
|
|
255
|
+
return [theme.fg("textMuted", " Enter search \xB7 Esc back"), "", ...this.input.render(Math.max(1, width - prefix.length)).map((line) => prefix + line)];
|
|
256
|
+
}
|
|
257
|
+
const rows = [theme.fg("textMuted", " \u2191\u2193 navigate \xB7 PgUp/PgDn page \xB7 Enter open \xB7 Esc back"), ""];
|
|
258
|
+
if (height <= 2) return rows.slice(0, height);
|
|
259
|
+
const status = this.loading ? "Searching stored history\u2026" : this.notice;
|
|
260
|
+
if (status !== void 0) rows.push(theme.fg("textMuted", ` ${status}`));
|
|
261
|
+
const matches = this.result?.matches ?? [];
|
|
262
|
+
const count = Math.max(0, height - rows.length);
|
|
263
|
+
const start = Math.min(Math.max(0, this.selected - count + 1), Math.max(0, matches.length - count));
|
|
264
|
+
rows.push(...matches.slice(start, start + count).map((match, index) => {
|
|
265
|
+
const current = start + index === this.selected;
|
|
266
|
+
return theme.fg(current ? "primary" : "text", `${current ? " \u203A " : " "}${match.preview.replaceAll(/[\n\t]+/g, " ")}`);
|
|
267
|
+
}));
|
|
268
|
+
return rows;
|
|
269
|
+
}
|
|
270
|
+
async load(start, direction) {
|
|
271
|
+
const query = this.query;
|
|
272
|
+
if (query === void 0) return;
|
|
273
|
+
this.loading = true;
|
|
274
|
+
this.notice = void 0;
|
|
275
|
+
this.refresh();
|
|
276
|
+
try {
|
|
277
|
+
let cursor = start;
|
|
278
|
+
do {
|
|
279
|
+
const page = await this.view.search(query, cursor, this.signal);
|
|
280
|
+
if (this.signal.aborted) return;
|
|
281
|
+
if (page.matches.length > 0) {
|
|
282
|
+
if (direction === "older") this.previous.push(this.result?.start);
|
|
283
|
+
else if (direction === "newer") this.previous.pop();
|
|
284
|
+
this.result = { matches: page.matches, start: cursor, older: page.older };
|
|
285
|
+
this.selected = direction === "newer" ? page.matches.length - 1 : 0;
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
cursor = page.older;
|
|
289
|
+
} while (cursor !== void 0);
|
|
290
|
+
if (this.result === void 0) this.notice = "No matches in this session.";
|
|
291
|
+
else if (direction === "older") this.result = { ...this.result, older: void 0 };
|
|
292
|
+
} catch {
|
|
293
|
+
if (!this.signal.aborted) this.notice = "Could not search stored history.";
|
|
294
|
+
} finally {
|
|
295
|
+
this.loading = false;
|
|
296
|
+
if (!this.signal.aborted) this.refresh();
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
// apps/blun-king/src/tui/constant/symbols.ts
|
|
302
|
+
var SELECT_POINTER = "\u276F";
|
|
303
|
+
|
|
304
|
+
// apps/blun-king/src/tui/utils/paging.ts
|
|
305
|
+
function pageView(total, selectedIndex, pageSize) {
|
|
306
|
+
const size = Math.max(1, Math.floor(pageSize));
|
|
307
|
+
const pageCount = Math.max(1, Math.ceil(total / size));
|
|
308
|
+
const safeIndex = total <= 0 ? 0 : Math.min(Math.max(0, selectedIndex), total - 1);
|
|
309
|
+
const page = Math.min(Math.floor(safeIndex / size), pageCount - 1);
|
|
310
|
+
const start = page * size;
|
|
311
|
+
const end = Math.min(start + size, total);
|
|
312
|
+
return { page, pageCount, start, end };
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// apps/blun-king/src/tui/utils/searchable-list.ts
|
|
316
|
+
var DEFAULT_PAGE_SIZE = 8;
|
|
317
|
+
var SearchableList = class {
|
|
318
|
+
items;
|
|
319
|
+
toSearchText;
|
|
320
|
+
pageSize;
|
|
321
|
+
searchable;
|
|
322
|
+
query = "";
|
|
323
|
+
cursor;
|
|
324
|
+
constructor(opts) {
|
|
325
|
+
this.items = opts.items;
|
|
326
|
+
this.toSearchText = opts.toSearchText;
|
|
327
|
+
this.pageSize = opts.pageSize ?? DEFAULT_PAGE_SIZE;
|
|
328
|
+
this.searchable = opts.searchable ?? false;
|
|
329
|
+
this.cursor = Math.max(opts.initialIndex ?? 0, 0);
|
|
330
|
+
}
|
|
331
|
+
filtered() {
|
|
332
|
+
if (this.query.length === 0) return this.items;
|
|
333
|
+
return fuzzyFilter([...this.items], this.query, this.toSearchText);
|
|
334
|
+
}
|
|
335
|
+
/** The item under the cursor, clamped into the filtered range. */
|
|
336
|
+
selected() {
|
|
337
|
+
const items = this.filtered();
|
|
338
|
+
if (items.length === 0) return void 0;
|
|
339
|
+
return items[Math.min(this.cursor, items.length - 1)];
|
|
340
|
+
}
|
|
341
|
+
view() {
|
|
342
|
+
const items = this.filtered();
|
|
343
|
+
return {
|
|
344
|
+
items,
|
|
345
|
+
page: pageView(items.length, this.cursor, this.pageSize),
|
|
346
|
+
selectedIndex: Math.min(this.cursor, Math.max(0, items.length - 1)),
|
|
347
|
+
query: this.query
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
moveUp() {
|
|
351
|
+
this.cursor = Math.max(0, this.cursor - 1);
|
|
352
|
+
}
|
|
353
|
+
moveDown() {
|
|
354
|
+
this.cursor = Math.min(Math.max(0, this.filtered().length - 1), this.cursor + 1);
|
|
355
|
+
}
|
|
356
|
+
pageUp() {
|
|
357
|
+
this.cursor = Math.max(0, this.cursor - this.pageSize);
|
|
358
|
+
}
|
|
359
|
+
pageDown() {
|
|
360
|
+
this.cursor = Math.min(Math.max(0, this.filtered().length - 1), this.cursor + this.pageSize);
|
|
361
|
+
}
|
|
362
|
+
/** Clears the active query and resets the cursor. Returns whether a query was cleared. */
|
|
363
|
+
clearQuery() {
|
|
364
|
+
if (this.query.length === 0) return false;
|
|
365
|
+
this.query = "";
|
|
366
|
+
this.cursor = 0;
|
|
367
|
+
return true;
|
|
368
|
+
}
|
|
369
|
+
/**
|
|
370
|
+
* Handles the keys every picker shares: ↑/↓, PgUp/PgDn, and — when searchable —
|
|
371
|
+
* Backspace and printable characters. Returns true when the key was consumed.
|
|
372
|
+
* Enter, Esc, and ←/→ are intentionally left to the component.
|
|
373
|
+
*/
|
|
374
|
+
handleKey(data) {
|
|
375
|
+
if (matchesKey(data, Key.up)) {
|
|
376
|
+
this.moveUp();
|
|
377
|
+
return true;
|
|
378
|
+
}
|
|
379
|
+
if (matchesKey(data, Key.down)) {
|
|
380
|
+
this.moveDown();
|
|
381
|
+
return true;
|
|
382
|
+
}
|
|
383
|
+
if (matchesKey(data, Key.pageUp)) {
|
|
384
|
+
this.pageUp();
|
|
385
|
+
return true;
|
|
386
|
+
}
|
|
387
|
+
if (matchesKey(data, Key.pageDown)) {
|
|
388
|
+
this.pageDown();
|
|
389
|
+
return true;
|
|
390
|
+
}
|
|
391
|
+
if (!this.searchable) return false;
|
|
392
|
+
if (matchesKey(data, Key.backspace)) {
|
|
393
|
+
if (this.query.length > 0) {
|
|
394
|
+
this.query = this.query.slice(0, -1);
|
|
395
|
+
this.cursor = 0;
|
|
396
|
+
}
|
|
397
|
+
return true;
|
|
398
|
+
}
|
|
399
|
+
const ch = printableChar(data);
|
|
400
|
+
if (isPrintableChar(ch)) {
|
|
401
|
+
this.query += ch;
|
|
402
|
+
this.cursor = 0;
|
|
403
|
+
return true;
|
|
404
|
+
}
|
|
405
|
+
return false;
|
|
406
|
+
}
|
|
407
|
+
};
|
|
408
|
+
|
|
409
|
+
// apps/blun-king/src/tui/controllers/original-history-sources.ts
|
|
410
|
+
var OriginalHistorySources = class {
|
|
411
|
+
constructor(view, entry, owner, expected, refresh) {
|
|
412
|
+
this.view = view;
|
|
413
|
+
this.expected = expected;
|
|
414
|
+
this.refresh = refresh;
|
|
415
|
+
this.signal = AbortSignal.any([owner, this.abort.signal]);
|
|
416
|
+
void this.load(entry);
|
|
417
|
+
}
|
|
418
|
+
view;
|
|
419
|
+
expected;
|
|
420
|
+
refresh;
|
|
421
|
+
abort = new AbortController();
|
|
422
|
+
items = [];
|
|
423
|
+
list = new SearchableList({ items: this.items, toSearchText: (item) => item.preview });
|
|
424
|
+
loading = true;
|
|
425
|
+
failed = false;
|
|
426
|
+
visibleRows = 1;
|
|
427
|
+
opening = false;
|
|
428
|
+
notice;
|
|
429
|
+
signal;
|
|
430
|
+
close() {
|
|
431
|
+
this.abort.abort();
|
|
432
|
+
}
|
|
433
|
+
handleInput(data) {
|
|
434
|
+
if (this.opening) return void 0;
|
|
435
|
+
if (matchesKey(data, Key.enter)) return this.list.selected();
|
|
436
|
+
const up = matchesKey(data, Key.pageUp);
|
|
437
|
+
if (up || matchesKey(data, Key.pageDown)) {
|
|
438
|
+
for (let i = 0; i < this.visibleRows; i++) {
|
|
439
|
+
if (up) this.list.moveUp();
|
|
440
|
+
else this.list.moveDown();
|
|
441
|
+
}
|
|
442
|
+
} else this.list.handleKey(data);
|
|
443
|
+
return void 0;
|
|
444
|
+
}
|
|
445
|
+
async open(source) {
|
|
446
|
+
this.opening = true;
|
|
447
|
+
this.notice = void 0;
|
|
448
|
+
this.refresh();
|
|
449
|
+
try {
|
|
450
|
+
const entry = await source.open(this.signal);
|
|
451
|
+
const detail = entry === void 0 ? void 0 : await readHistoryDetail(this.view, entry, this.signal);
|
|
452
|
+
if (this.signal.aborted) return void 0;
|
|
453
|
+
if (entry !== void 0 && detail !== void 0) return { entry, detail };
|
|
454
|
+
this.notice = "The original source is unavailable.";
|
|
455
|
+
} catch {
|
|
456
|
+
if (!this.signal.aborted) this.notice = "Could not open the original source.";
|
|
457
|
+
} finally {
|
|
458
|
+
this.opening = false;
|
|
459
|
+
if (!this.signal.aborted) this.refresh();
|
|
460
|
+
}
|
|
461
|
+
return void 0;
|
|
462
|
+
}
|
|
463
|
+
render(height, theme, label) {
|
|
464
|
+
const header = " Up/Down navigate / PgUp/PgDn page / Enter open / Esc back";
|
|
465
|
+
const rows = [theme.fg("textMuted", header), ""];
|
|
466
|
+
const status = this.opening ? "Opening original source..." : this.notice ?? (this.loading ? "Finding original sources..." : this.failed ? "Could not finish reading original sources." : this.resultStatus());
|
|
467
|
+
const { items, selectedIndex } = this.list.view();
|
|
468
|
+
this.visibleRows = Math.max(1, height - 4);
|
|
469
|
+
const { start, end, page, pageCount } = pageView(items.length, selectedIndex, this.visibleRows);
|
|
470
|
+
for (let i = start; i < end; i++) {
|
|
471
|
+
const item = items[i];
|
|
472
|
+
const selected = i === selectedIndex;
|
|
473
|
+
const snippet = item.preview.replaceAll(/[\n\t]+/g, " ").trim();
|
|
474
|
+
rows.push(theme.fg(
|
|
475
|
+
selected ? "primary" : "text",
|
|
476
|
+
` ${selected ? SELECT_POINTER : " "} ${label(item)}${snippet ? ": " + snippet : ""}`
|
|
477
|
+
));
|
|
478
|
+
}
|
|
479
|
+
if (pageCount > 1) rows.push(theme.fg("textMuted", ` ${page + 1} / ${pageCount}`));
|
|
480
|
+
if (status !== void 0) rows.push(theme.fg("textMuted", " " + status));
|
|
481
|
+
return rows.slice(0, height);
|
|
482
|
+
}
|
|
483
|
+
resultStatus() {
|
|
484
|
+
const missing = Math.max(0, this.expected - this.items.length);
|
|
485
|
+
if (missing === 1) return "1 stored source unavailable.";
|
|
486
|
+
if (missing > 1) return `${missing} stored sources unavailable.`;
|
|
487
|
+
return this.items.length === 0 ? "No stored sources." : void 0;
|
|
488
|
+
}
|
|
489
|
+
async load(entry) {
|
|
490
|
+
try {
|
|
491
|
+
for await (const source of this.view.checkpointSources(entry, this.signal)) {
|
|
492
|
+
if (this.signal.aborted) return;
|
|
493
|
+
this.items.push(source);
|
|
494
|
+
this.refresh();
|
|
495
|
+
}
|
|
496
|
+
} catch {
|
|
497
|
+
this.failed = true;
|
|
498
|
+
} finally {
|
|
499
|
+
this.loading = false;
|
|
500
|
+
if (!this.signal.aborted) this.refresh();
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
};
|
|
504
|
+
|
|
505
|
+
// apps/blun-king/src/tui/controllers/original-history.ts
|
|
506
|
+
var OriginalHistoryController = class {
|
|
507
|
+
constructor(host) {
|
|
508
|
+
this.host = host;
|
|
509
|
+
}
|
|
510
|
+
host;
|
|
511
|
+
panel;
|
|
512
|
+
pendingPosition = Promise.resolve();
|
|
513
|
+
open(checkpoint) {
|
|
514
|
+
const { session, state: { ui, editor } } = this.host;
|
|
515
|
+
if (this.panel !== void 0 || session === void 0 || !editor.focused || ui.hasOverlay()) return false;
|
|
516
|
+
const panel = {
|
|
517
|
+
stop: new AbortController(),
|
|
518
|
+
overlay: void 0,
|
|
519
|
+
view: void 0,
|
|
520
|
+
page: void 0,
|
|
521
|
+
detail: void 0,
|
|
522
|
+
selected: 0,
|
|
523
|
+
loading: true,
|
|
524
|
+
notice: void 0,
|
|
525
|
+
search: void 0,
|
|
526
|
+
attachments: void 0
|
|
527
|
+
};
|
|
528
|
+
this.panel = panel;
|
|
529
|
+
panel.overlay = ui.showOverlay({
|
|
530
|
+
render: (width) => this.render(panel, width),
|
|
531
|
+
handleInput: (data) => {
|
|
532
|
+
this.handleInput(panel, data);
|
|
533
|
+
},
|
|
534
|
+
invalidate() {
|
|
535
|
+
}
|
|
536
|
+
}, { width: "100%", maxHeight: "100%", anchor: "top-left" });
|
|
537
|
+
void this.capture(panel, session, checkpoint);
|
|
538
|
+
return true;
|
|
539
|
+
}
|
|
540
|
+
close() {
|
|
541
|
+
const panel = this.panel;
|
|
542
|
+
if (panel === void 0) return this.pendingPosition;
|
|
543
|
+
this.panel = void 0;
|
|
544
|
+
panel.stop.abort();
|
|
545
|
+
panel.overlay?.hide();
|
|
546
|
+
const entry = panel.page?.entries[panel.selected];
|
|
547
|
+
const view = panel.view;
|
|
548
|
+
const offset = panel.detail?.offset;
|
|
549
|
+
if (entry !== void 0 && entry.kind !== "unavailable" && view !== void 0) {
|
|
550
|
+
this.pendingPosition = this.pendingPosition.then(() => view.savePosition(entry, offset)).catch(() => {
|
|
551
|
+
});
|
|
552
|
+
}
|
|
553
|
+
return this.pendingPosition;
|
|
554
|
+
}
|
|
555
|
+
async capture(panel, session, checkpoint) {
|
|
556
|
+
try {
|
|
557
|
+
await this.pendingPosition;
|
|
558
|
+
if (this.panel !== panel) return;
|
|
559
|
+
if (checkpoint !== void 0) {
|
|
560
|
+
const active = await this.host.harness.openModelCheckpoint?.(session, checkpoint, panel.stop.signal);
|
|
561
|
+
if (this.panel !== panel || this.host.session !== session) return;
|
|
562
|
+
if (active === void 0) panel.notice = "This checkpoint is unavailable or no longer active.";
|
|
563
|
+
else {
|
|
564
|
+
panel.view = active.view;
|
|
565
|
+
panel.page = { entries: [active.entry] };
|
|
566
|
+
await this.detail(panel, active.entry);
|
|
567
|
+
}
|
|
568
|
+
return;
|
|
569
|
+
}
|
|
570
|
+
const view = await this.host.harness.openOriginalHistory(session, panel.stop.signal);
|
|
571
|
+
if (this.panel !== panel || this.host.session !== session) return;
|
|
572
|
+
panel.view = view;
|
|
573
|
+
if (view === void 0) {
|
|
574
|
+
panel.notice = "Stored history is unavailable for this session.";
|
|
575
|
+
return;
|
|
576
|
+
}
|
|
577
|
+
const last = await view.restorePosition(panel.stop.signal);
|
|
578
|
+
if (this.panel !== panel) return;
|
|
579
|
+
if (last === void 0) {
|
|
580
|
+
await this.load(panel, "latest");
|
|
581
|
+
return;
|
|
582
|
+
}
|
|
583
|
+
panel.page = last.page;
|
|
584
|
+
const entry = last.page.entries[0];
|
|
585
|
+
if (entry !== void 0 && last.detailOffset !== void 0) await this.detail(panel, entry, last.detailOffset);
|
|
586
|
+
} catch {
|
|
587
|
+
if (!panel.stop.signal.aborted) panel.notice = "Could not open stored history.";
|
|
588
|
+
} finally {
|
|
589
|
+
this.finish(panel);
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
async load(panel, from) {
|
|
593
|
+
const session = this.host.session;
|
|
594
|
+
const previousView = panel.view;
|
|
595
|
+
if (session === void 0 || previousView === void 0) return;
|
|
596
|
+
const refreshing = from === "refresh-latest";
|
|
597
|
+
panel.loading = true;
|
|
598
|
+
panel.notice = void 0;
|
|
599
|
+
try {
|
|
600
|
+
const view = refreshing ? await this.host.harness.openOriginalHistory(session, panel.stop.signal) : previousView;
|
|
601
|
+
if (this.panel !== panel || this.host.session !== session) return;
|
|
602
|
+
if (view === void 0) {
|
|
603
|
+
panel.notice = "Could not refresh stored history.";
|
|
604
|
+
return;
|
|
605
|
+
}
|
|
606
|
+
const cursor = typeof from === "string" ? view.at(refreshing ? "latest" : from) : from;
|
|
607
|
+
const older = cursor.direction === "backward";
|
|
608
|
+
let next = cursor;
|
|
609
|
+
while (next !== void 0) {
|
|
610
|
+
const page = await view.read(next, panel.stop.signal);
|
|
611
|
+
if (this.panel !== panel || this.host.session !== session) return;
|
|
612
|
+
if (page.entries.length > 0) {
|
|
613
|
+
if (view !== previousView) {
|
|
614
|
+
panel.search?.controller.close();
|
|
615
|
+
panel.search = void 0;
|
|
616
|
+
panel.view = view;
|
|
617
|
+
}
|
|
618
|
+
panel.page = page;
|
|
619
|
+
panel.selected = older ? page.entries.length - 1 : 0;
|
|
620
|
+
return;
|
|
621
|
+
}
|
|
622
|
+
next = older ? page.older : page.newer;
|
|
623
|
+
}
|
|
624
|
+
if (refreshing || panel.page === void 0) panel.notice = "No stored messages.";
|
|
625
|
+
else panel.page = { ...panel.page, older: older ? void 0 : panel.page.older, newer: older ? panel.page.newer : void 0 };
|
|
626
|
+
} catch {
|
|
627
|
+
if (!panel.stop.signal.aborted) panel.notice = refreshing ? "Could not refresh stored history." : "Stored history changed. Reopen it to continue.";
|
|
628
|
+
} finally {
|
|
629
|
+
this.finish(panel);
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
async detail(panel, entry, offset = 0) {
|
|
633
|
+
panel.loading = true;
|
|
634
|
+
panel.notice = void 0;
|
|
635
|
+
try {
|
|
636
|
+
const detail = panel.view === void 0 ? void 0 : await readHistoryDetail(panel.view, entry, panel.stop.signal, offset);
|
|
637
|
+
if (this.panel !== panel) return;
|
|
638
|
+
if (detail !== void 0) panel.detail = detail;
|
|
639
|
+
else panel.notice = entry.kind === "compaction" ? "Stored checkpoint details are unavailable." : "No readable content is stored for this record.";
|
|
640
|
+
} catch (error) {
|
|
641
|
+
if (!panel.stop.signal.aborted) panel.notice = error instanceof Error && error.message === "ORIGINAL_TOOL_OUTPUT_UNAVAILABLE" ? "Saved full tool output is unavailable." : "Could not read this record.";
|
|
642
|
+
} finally {
|
|
643
|
+
this.finish(panel);
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
async copyDetail(panel, detail) {
|
|
647
|
+
if (panel.copying === true) return;
|
|
648
|
+
panel.copying = true;
|
|
649
|
+
let notice;
|
|
650
|
+
try {
|
|
651
|
+
await copyTextToClipboard(detail.text);
|
|
652
|
+
notice = "Record copied.";
|
|
653
|
+
} catch {
|
|
654
|
+
notice = "Could not copy this record.";
|
|
655
|
+
} finally {
|
|
656
|
+
panel.copying = false;
|
|
657
|
+
}
|
|
658
|
+
if (this.panel !== panel || panel.detail !== detail || panel.stop.signal.aborted) return;
|
|
659
|
+
panel.notice = notice;
|
|
660
|
+
this.host.state.ui.requestRender();
|
|
661
|
+
}
|
|
662
|
+
finish(panel) {
|
|
663
|
+
if (this.panel !== panel) return;
|
|
664
|
+
panel.loading = false;
|
|
665
|
+
this.host.state.ui.requestRender();
|
|
666
|
+
}
|
|
667
|
+
startSearch(panel, view) {
|
|
668
|
+
const before = panel.search ?? panel;
|
|
669
|
+
panel.search?.controller.close();
|
|
670
|
+
panel.search = {
|
|
671
|
+
controller: new OriginalHistorySearch(view, panel.stop.signal, () => {
|
|
672
|
+
this.host.state.ui.requestRender();
|
|
673
|
+
}),
|
|
674
|
+
page: before.page,
|
|
675
|
+
detail: before.detail,
|
|
676
|
+
selected: before.selected,
|
|
677
|
+
visible: true
|
|
678
|
+
};
|
|
679
|
+
}
|
|
680
|
+
async openMatch(panel, match) {
|
|
681
|
+
const { search, view } = panel;
|
|
682
|
+
if (search === void 0 || view === void 0) return;
|
|
683
|
+
search.visible = false;
|
|
684
|
+
panel.loading = true;
|
|
685
|
+
panel.notice = void 0;
|
|
686
|
+
try {
|
|
687
|
+
const anchor = await view.bookmark(match.entry, panel.stop.signal);
|
|
688
|
+
const page = await view.readAt(anchor, panel.stop.signal);
|
|
689
|
+
if (this.panel !== panel || panel.search !== search || search.visible) return;
|
|
690
|
+
const entry = page?.entries[0];
|
|
691
|
+
if (entry === void 0) {
|
|
692
|
+
panel.notice = "The matching record is unavailable.";
|
|
693
|
+
return;
|
|
694
|
+
}
|
|
695
|
+
panel.page = page;
|
|
696
|
+
panel.selected = 0;
|
|
697
|
+
panel.detail = void 0;
|
|
698
|
+
await this.detail(panel, entry, match.offset);
|
|
699
|
+
} catch {
|
|
700
|
+
if (!panel.stop.signal.aborted) panel.notice = "Could not open the matching record.";
|
|
701
|
+
} finally {
|
|
702
|
+
this.finish(panel);
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
async openSource(panel, scope, source) {
|
|
706
|
+
const opened = await scope.controller.open(source);
|
|
707
|
+
if (opened === void 0 || this.panel !== panel || panel.sources !== scope || scope.controller.signal.aborted) return;
|
|
708
|
+
panel.page = { entries: [opened.entry] };
|
|
709
|
+
panel.selected = 0;
|
|
710
|
+
panel.detail = opened.detail;
|
|
711
|
+
panel.notice = void 0;
|
|
712
|
+
scope.visible = false;
|
|
713
|
+
this.host.state.ui.requestRender();
|
|
714
|
+
}
|
|
715
|
+
handleInput(panel, data) {
|
|
716
|
+
if (this.panel !== panel || isKeyRelease(data)) return;
|
|
717
|
+
if (matchesKey(data, Key.ctrl("s"))) {
|
|
718
|
+
this.host.requestQueueSteer();
|
|
719
|
+
return;
|
|
720
|
+
}
|
|
721
|
+
if (matchesKey(data, Key.escape) || matchesKey(data, Key.ctrl("c"))) {
|
|
722
|
+
if (panel.attachments !== void 0) {
|
|
723
|
+
panel.attachments.close();
|
|
724
|
+
panel.attachments = void 0;
|
|
725
|
+
return;
|
|
726
|
+
}
|
|
727
|
+
const sources = panel.sources;
|
|
728
|
+
if (sources !== void 0) {
|
|
729
|
+
if (sources.visible) {
|
|
730
|
+
sources.controller.close();
|
|
731
|
+
panel.sources = sources.parent;
|
|
732
|
+
} else sources.visible = true;
|
|
733
|
+
panel.page = sources.page;
|
|
734
|
+
panel.selected = sources.selected;
|
|
735
|
+
panel.detail = sources.detail;
|
|
736
|
+
panel.notice = void 0;
|
|
737
|
+
return;
|
|
738
|
+
}
|
|
739
|
+
const search = panel.search;
|
|
740
|
+
if (search?.visible === true) {
|
|
741
|
+
search.controller.close();
|
|
742
|
+
panel.page = search.page;
|
|
743
|
+
panel.selected = search.selected;
|
|
744
|
+
panel.detail = search.detail;
|
|
745
|
+
panel.notice = void 0;
|
|
746
|
+
panel.search = void 0;
|
|
747
|
+
} else if (panel.loading && search !== void 0) search.visible = true;
|
|
748
|
+
else if (panel.detail !== void 0) panel.detail = void 0;
|
|
749
|
+
else if (search !== void 0) search.visible = true;
|
|
750
|
+
else void this.close();
|
|
751
|
+
return;
|
|
752
|
+
}
|
|
753
|
+
if (panel.loading) return;
|
|
754
|
+
const { ui } = this.host.state;
|
|
755
|
+
if (panel.attachments !== void 0) {
|
|
756
|
+
panel.attachments.handleInput(data);
|
|
757
|
+
return;
|
|
758
|
+
}
|
|
759
|
+
if (panel.sources === void 0 && panel.view !== void 0 && (matchesKey(data, Key.ctrl("f")) || panel.search?.visible !== true && printableChar(data) === "/")) {
|
|
760
|
+
this.startSearch(panel, panel.view);
|
|
761
|
+
return;
|
|
762
|
+
}
|
|
763
|
+
if (panel.search?.visible === true) {
|
|
764
|
+
const result = panel.search.controller.handleInput(data, ui.terminal.rows - 3);
|
|
765
|
+
if (result !== void 0) void this.openMatch(panel, result);
|
|
766
|
+
return;
|
|
767
|
+
}
|
|
768
|
+
if (printableChar(data).toLowerCase() === "q") {
|
|
769
|
+
void this.close();
|
|
770
|
+
return;
|
|
771
|
+
}
|
|
772
|
+
if (panel.sources?.visible === true) {
|
|
773
|
+
const source = panel.sources.controller.handleInput(data);
|
|
774
|
+
if (source !== void 0) void this.openSource(panel, panel.sources, source);
|
|
775
|
+
return;
|
|
776
|
+
}
|
|
777
|
+
const entry = panel.page?.entries[panel.selected];
|
|
778
|
+
if (panel.view !== void 0 && entry !== void 0 && printableChar(data)?.toLowerCase() === "a") {
|
|
779
|
+
panel.attachments = new OriginalHistoryAttachments(panel.view, entry, panel.stop.signal, () => {
|
|
780
|
+
ui.requestRender();
|
|
781
|
+
});
|
|
782
|
+
return;
|
|
783
|
+
}
|
|
784
|
+
const rows = Math.max(1, ui.terminal.rows - 5);
|
|
785
|
+
const delta = matchesKey(data, Key.up) ? -1 : matchesKey(data, Key.down) ? 1 : matchesKey(data, Key.pageUp) ? -rows : matchesKey(data, Key.pageDown) ? rows : 0;
|
|
786
|
+
if (panel.detail !== void 0) {
|
|
787
|
+
const count = panel.detail.sourceCount ?? 0;
|
|
788
|
+
if (count > 0 && panel.view !== void 0 && entry !== void 0 && printableChar(data).toLowerCase() === "s") {
|
|
789
|
+
panel.sources = {
|
|
790
|
+
controller: new OriginalHistorySources(panel.view, entry, panel.stop.signal, count, () => {
|
|
791
|
+
ui.requestRender();
|
|
792
|
+
}),
|
|
793
|
+
page: panel.page,
|
|
794
|
+
selected: panel.selected,
|
|
795
|
+
detail: panel.detail,
|
|
796
|
+
parent: panel.sources,
|
|
797
|
+
visible: true
|
|
798
|
+
};
|
|
799
|
+
panel.notice = void 0;
|
|
800
|
+
return;
|
|
801
|
+
}
|
|
802
|
+
if (printableChar(data)?.toLowerCase() === "c") {
|
|
803
|
+
void this.copyDetail(panel, panel.detail);
|
|
804
|
+
return;
|
|
805
|
+
}
|
|
806
|
+
scrollHistoryDetail(panel.detail, Math.max(1, ui.terminal.columns - 2), delta);
|
|
807
|
+
return;
|
|
808
|
+
}
|
|
809
|
+
if (panel.view === void 0) return;
|
|
810
|
+
if (matchesKey(data, Key.home) || matchesKey(data, Key.end)) {
|
|
811
|
+
void this.load(panel, matchesKey(data, Key.home) ? "oldest" : "refresh-latest");
|
|
812
|
+
return;
|
|
813
|
+
}
|
|
814
|
+
const page = panel.page;
|
|
815
|
+
if (page === void 0) return;
|
|
816
|
+
if (matchesKey(data, Key.enter)) {
|
|
817
|
+
if (entry && (entry.kind === "compaction" || entry.expandable || entry.preview.length > 0)) void this.detail(panel, entry);
|
|
818
|
+
return;
|
|
819
|
+
}
|
|
820
|
+
if (delta === 0) return;
|
|
821
|
+
const target = panel.selected + delta;
|
|
822
|
+
if (target < 0 && panel.selected === 0 && page.older !== void 0) void this.load(panel, page.older);
|
|
823
|
+
else if (target >= page.entries.length && panel.selected === page.entries.length - 1 && page.newer !== void 0) {
|
|
824
|
+
void this.load(panel, page.newer);
|
|
825
|
+
} else panel.selected = Math.max(0, Math.min(target, page.entries.length - 1));
|
|
826
|
+
}
|
|
827
|
+
render(panel, width) {
|
|
828
|
+
const { state: { ui, theme } } = this.host;
|
|
829
|
+
const height = Math.max(1, ui.terminal.rows);
|
|
830
|
+
const count = Math.max(1, height - 5);
|
|
831
|
+
const rail = theme.fg("primary", "\u2500".repeat(width));
|
|
832
|
+
const search = panel.search?.visible === true ? panel.search.controller : void 0;
|
|
833
|
+
const attachments = panel.attachments;
|
|
834
|
+
const sources = panel.sources?.visible === true ? panel.sources.controller : void 0;
|
|
835
|
+
const title = theme.boldFg("primary", attachments !== void 0 ? " Stored attachments" : sources !== void 0 ? " Original sources" : search !== void 0 ? " Search this session" : panel.detail === void 0 ? " Original history" : " Original record");
|
|
836
|
+
const hint = panel.detail === void 0 ? " \u2191\u2193 navigate \xB7 End latest \xB7 Enter open \xB7 / search \xB7 A attachments \xB7 Q close" : " \u2191\u2193 scroll \xB7 PgUp/PgDn page \xB7 C copy \xB7 A attachments \xB7 Esc back \xB7 Q close";
|
|
837
|
+
const rows = [rail, title];
|
|
838
|
+
if (attachments !== void 0) {
|
|
839
|
+
rows.push(...attachments.render(Math.max(0, height - 3), theme));
|
|
840
|
+
} else if (sources !== void 0) {
|
|
841
|
+
rows.push(...sources.render(Math.max(0, height - 3), theme, (source) => ORIGINAL_HISTORY_LABELS[source.kind]));
|
|
842
|
+
} else if (search !== void 0) {
|
|
843
|
+
rows.push(...search.render(width, Math.max(0, height - 3), theme));
|
|
844
|
+
} else if (panel.detail !== void 0) {
|
|
845
|
+
rows.push(theme.fg("textMuted", (panel.detail.sourceCount ?? 0) > 0 ? " S sources / Up/Down scroll / C copy / Esc back / Q close" : hint), "");
|
|
846
|
+
const detail = detailRows(panel.detail.text, panel.detail.offset, Math.max(1, width - 2), count);
|
|
847
|
+
rows.push(...detail.map((line) => ` ${line.text}`));
|
|
848
|
+
} else {
|
|
849
|
+
rows.push(theme.fg("textMuted", hint), "");
|
|
850
|
+
if (panel.page !== void 0) {
|
|
851
|
+
const { entries } = panel.page;
|
|
852
|
+
const start = Math.min(Math.max(0, panel.selected - count + 1), Math.max(0, entries.length - count));
|
|
853
|
+
rows.push(...entries.slice(start, start + count).map((entry, index) => {
|
|
854
|
+
const selected = start + index === panel.selected;
|
|
855
|
+
const label = ORIGINAL_HISTORY_LABELS[entry.kind];
|
|
856
|
+
const text = entry.preview.replaceAll(/[\n\t]+/g, " ").trim();
|
|
857
|
+
const mark = selected ? " \u203A " : " ";
|
|
858
|
+
return theme.fg(selected ? "primary" : "text", `${mark}${label}${text ? `: ${text}` : ""}`);
|
|
859
|
+
}));
|
|
860
|
+
}
|
|
861
|
+
}
|
|
862
|
+
const status = panel.loading ? "Loading stored history\u2026" : panel.notice;
|
|
863
|
+
if (status !== void 0) rows[Math.min(rows.length, Math.max(0, height - 2))] = theme.fg("textMuted", ` ${status}`);
|
|
864
|
+
const padding = Array.from({ length: Math.max(0, height - 1 - rows.length) }, () => "");
|
|
865
|
+
return [...rows.slice(0, height - 1), ...padding, rail].map((line) => {
|
|
866
|
+
const fitting = truncateToWidth(line, width);
|
|
867
|
+
return fitting + " ".repeat(Math.max(0, width - visibleWidth(fitting)));
|
|
868
|
+
});
|
|
869
|
+
}
|
|
870
|
+
};
|
|
871
|
+
|
|
872
|
+
// apps/blun-king/src/tui/controllers/original-history-tui-entry.ts
|
|
873
|
+
function createJournalHistoryController(host, api) {
|
|
874
|
+
useHistoryUi(api);
|
|
875
|
+
return new OriginalHistoryController(host);
|
|
876
|
+
}
|
|
877
|
+
export {
|
|
878
|
+
createJournalHistoryController
|
|
879
|
+
};
|