@narumitw/pi-btw 0.61.2 → 0.63.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +21 -7
- package/dist/index.ts +1486 -726
- package/dist/index.ts.map +4 -4
- package/docs/core-alignment.md +277 -0
- package/docs/main-context-runtime.md +45 -0
- package/docs/native-appearance.md +857 -0
- package/docs/workflows.md +80 -16
- package/package.json +4 -4
- package/src/btw.ts +185 -116
- package/src/conversation-chrome.ts +93 -0
- package/src/fullscreen-renderer.ts +329 -0
- package/src/fullscreen-ui.ts +109 -315
- package/src/import-main-updates.ts +84 -0
- package/src/keybindings.ts +125 -22
- package/src/main-context-snapshot.ts +125 -0
- package/src/menu.ts +43 -27
- package/src/session-lifetime.ts +66 -0
- package/src/side-thread.ts +66 -17
- package/src/transcript-markdown.ts +5 -5
- package/src/transcript-pager.ts +412 -169
- package/src/workspace-layout.ts +96 -36
package/src/btw.ts
CHANGED
|
@@ -12,8 +12,10 @@ import {
|
|
|
12
12
|
getAnsweredTurns,
|
|
13
13
|
summarizeBringToMain,
|
|
14
14
|
} from "./bring-to-main.js";
|
|
15
|
-
import {
|
|
15
|
+
import type { BtwDisplayModel } from "./conversation-chrome.js";
|
|
16
16
|
import { type RunBtwFullscreen, runBtwFullscreen } from "./fullscreen-ui.js";
|
|
17
|
+
import { importMainUpdates } from "./import-main-updates.js";
|
|
18
|
+
import { buildMainContextSnapshot, captureMainContextSource } from "./main-context-snapshot.js";
|
|
17
19
|
import { registerBtwMainThreadUpdates } from "./main-thread-updates.js";
|
|
18
20
|
import { pickMainEntry } from "./main-tree-picker.js";
|
|
19
21
|
import {
|
|
@@ -23,6 +25,7 @@ import {
|
|
|
23
25
|
showBtwCommandMenu,
|
|
24
26
|
showBtwCustomPreservingEditor,
|
|
25
27
|
} from "./menu.js";
|
|
28
|
+
import { type BtwSessionLifetime, registerBtwSessionLifetimes } from "./session-lifetime.js";
|
|
26
29
|
import {
|
|
27
30
|
type BtwSettings,
|
|
28
31
|
effectiveBtwLayout,
|
|
@@ -41,6 +44,7 @@ import {
|
|
|
41
44
|
createSideThread,
|
|
42
45
|
type SideQuestionAuth,
|
|
43
46
|
type SideThread,
|
|
47
|
+
sideTranscript,
|
|
44
48
|
} from "./side-thread.js";
|
|
45
49
|
import { sanitizeSingleLine } from "./text.js";
|
|
46
50
|
import { type BtwMarkdownTransformers, prepareBtwTranscriptMarkdown } from "./transcript-markdown.js";
|
|
@@ -178,6 +182,7 @@ function notifySafely(
|
|
|
178
182
|
message: string,
|
|
179
183
|
level: Parameters<ExtensionCommandContext["ui"]["notify"]>[1],
|
|
180
184
|
): void {
|
|
185
|
+
if (ctx.signal?.aborted) return;
|
|
181
186
|
try {
|
|
182
187
|
ctx.ui.notify(sanitizeSingleLine(message), level);
|
|
183
188
|
} catch {
|
|
@@ -209,123 +214,150 @@ export default function btw(pi: ExtensionAPI, dependencies: BtwExtensionDependen
|
|
|
209
214
|
const runThread = dependencies.runThread ?? runBtwThread;
|
|
210
215
|
const runFullscreen = dependencies.runFullscreen ?? runBtwFullscreen;
|
|
211
216
|
const subscribeMainThreadUpdates = registerBtwMainThreadUpdates(pi);
|
|
212
|
-
|
|
213
|
-
const
|
|
217
|
+
const getLifetime = registerBtwSessionLifetimes(pi);
|
|
218
|
+
const sessionThreads = new WeakMap<BtwSessionLifetime, Map<string, BtwThreadState>>();
|
|
214
219
|
let nextThreadNumber = 1;
|
|
215
|
-
const listResumeThreads = (): BtwResumeThreadSummary[] =>
|
|
220
|
+
const listResumeThreads = (resumableThreads: Map<string, BtwThreadState>): BtwResumeThreadSummary[] =>
|
|
216
221
|
[...resumableThreads.values()]
|
|
217
222
|
.reverse()
|
|
218
|
-
.filter((state) => state.thread.turns.length > 0 && state.title)
|
|
223
|
+
.filter((state) => (state.thread.turns.length > 0 && state.title) || state.thread.updates.length > 0)
|
|
219
224
|
.sort((first, second) => second.updatedAt - first.updatedAt || second.createdAt - first.createdAt)
|
|
220
225
|
.map((state) => ({
|
|
221
226
|
id: state.id,
|
|
222
|
-
title: state.title ?? "
|
|
227
|
+
title: state.title ?? "Main context updates",
|
|
223
228
|
questionCount: state.thread.turns.length,
|
|
224
229
|
}));
|
|
225
230
|
pi.registerCommand("btw", {
|
|
226
231
|
description: "Ask a quick side question without adding it to the main conversation",
|
|
227
|
-
handler:
|
|
228
|
-
const
|
|
229
|
-
if (
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
232
|
+
handler: (args, ctx) => {
|
|
233
|
+
const lifetime = getLifetime(ctx);
|
|
234
|
+
if (lifetime.signal.aborted) return Promise.resolve();
|
|
235
|
+
const commandCtx = Object.create(ctx) as ExtensionCommandContext;
|
|
236
|
+
Object.defineProperty(commandCtx, "signal", { value: lifetime.signal, configurable: true });
|
|
237
|
+
return lifetime.track(
|
|
238
|
+
handleCommand(args, commandCtx, lifetime).catch((error: unknown) => {
|
|
239
|
+
if (!lifetime.signal.aborted) throw error;
|
|
240
|
+
}),
|
|
241
|
+
);
|
|
242
|
+
},
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
async function handleCommand(args: string, ctx: ExtensionCommandContext, lifetime: BtwSessionLifetime) {
|
|
246
|
+
let resumableThreads = sessionThreads.get(lifetime);
|
|
247
|
+
if (!resumableThreads) {
|
|
248
|
+
resumableThreads = new Map();
|
|
249
|
+
sessionThreads.set(lifetime, resumableThreads);
|
|
250
|
+
}
|
|
251
|
+
const question = args.trim();
|
|
252
|
+
if (ctx.mode !== "tui") {
|
|
253
|
+
ctx.ui.notify("/btw requires interactive TUI mode", "error");
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
233
256
|
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
}
|
|
255
|
-
selectedConversationContext = buildConversationContext(branch);
|
|
256
|
-
menuResult = "start";
|
|
257
|
-
break;
|
|
258
|
-
} catch {
|
|
259
|
-
return;
|
|
257
|
+
let menuResult: BtwCommandMenuResult = "start";
|
|
258
|
+
let selectedSource: ReturnType<typeof captureMainContextSource> | undefined;
|
|
259
|
+
if (!question) {
|
|
260
|
+
while (true) {
|
|
261
|
+
menuResult = await showCommandMenu(pi, ctx, listResumeThreads(resumableThreads));
|
|
262
|
+
if (lifetime.signal.aborted || menuResult === "closed") return;
|
|
263
|
+
if (menuResult !== "tree") break;
|
|
264
|
+
|
|
265
|
+
const treeResult = await pickEntry(pi, ctx);
|
|
266
|
+
if (lifetime.signal.aborted || treeResult.kind === "closed") return;
|
|
267
|
+
if (treeResult.kind === "back") continue;
|
|
268
|
+
try {
|
|
269
|
+
if (!ctx.sessionManager.getEntry(treeResult.entryId)) {
|
|
270
|
+
notifySafely(ctx, "The selected main-thread entry is no longer available", "warning");
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
const branch = ctx.sessionManager.getBranch(treeResult.entryId);
|
|
274
|
+
if (branch.at(-1)?.id !== treeResult.entryId) {
|
|
275
|
+
notifySafely(ctx, "The selected main-thread branch is no longer available", "warning");
|
|
276
|
+
continue;
|
|
260
277
|
}
|
|
278
|
+
selectedSource = captureMainContextSource(ctx.sessionManager, treeResult.entryId, branch);
|
|
279
|
+
menuResult = "start";
|
|
280
|
+
break;
|
|
281
|
+
} catch {
|
|
282
|
+
return;
|
|
261
283
|
}
|
|
262
284
|
}
|
|
285
|
+
}
|
|
263
286
|
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
287
|
+
const settings = await loadSettings(ctx);
|
|
288
|
+
if (lifetime.signal.aborted) return;
|
|
289
|
+
const sameAsMainThinkingLevel = settings.thinkingLevel === undefined;
|
|
290
|
+
const resolution = await resolveModel(settings, ctx);
|
|
291
|
+
if (lifetime.signal.aborted) return;
|
|
292
|
+
if (resolution.kind === "cancelled") {
|
|
293
|
+
notifySafely(ctx, "Cancelled", "info");
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
if (resolution.kind === "unavailable") {
|
|
297
|
+
notifySafely(ctx, "No available model for /btw", "error");
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
275
300
|
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
+
let state = typeof menuResult === "object" ? resumableThreads.get(menuResult.threadId) : undefined;
|
|
302
|
+
if (typeof menuResult === "object" && !state) {
|
|
303
|
+
notifySafely(ctx, "The selected /btw side thread is no longer available", "warning");
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
const startingTurnCount = state?.thread.turns.length ?? 0;
|
|
307
|
+
|
|
308
|
+
try {
|
|
309
|
+
await runFullscreen(
|
|
310
|
+
ctx,
|
|
311
|
+
(fullscreenCtx) => {
|
|
312
|
+
if (lifetime.signal.aborted) return Promise.resolve({ kind: "closed" as const });
|
|
313
|
+
if (!state) {
|
|
314
|
+
const createdAt = Date.now();
|
|
315
|
+
const source = selectedSource ?? captureMainContextSource(fullscreenCtx.sessionManager);
|
|
316
|
+
state = {
|
|
317
|
+
id: `btw-${nextThreadNumber}`,
|
|
318
|
+
thread: createSideThread(source.conversationContext, source.anchor),
|
|
319
|
+
thinkingLevel: settings.thinkingLevel ?? pi.getThinkingLevel(),
|
|
320
|
+
createdAt,
|
|
321
|
+
updatedAt: createdAt,
|
|
322
|
+
};
|
|
323
|
+
nextThreadNumber += 1;
|
|
324
|
+
}
|
|
325
|
+
return lifetime.track(
|
|
326
|
+
runThread({
|
|
301
327
|
initialQuestion: question || undefined,
|
|
302
328
|
selected: resolution.selected,
|
|
303
329
|
thinkingLevel: state.thinkingLevel,
|
|
304
330
|
rememberThinkingLevelChanges: !sameAsMainThinkingLevel && effectiveRememberThinkingLevelChanges(settings),
|
|
305
331
|
state,
|
|
306
332
|
ctx: fullscreenCtx,
|
|
307
|
-
})
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
333
|
+
}),
|
|
334
|
+
);
|
|
335
|
+
},
|
|
336
|
+
{
|
|
337
|
+
signal: lifetime.signal,
|
|
338
|
+
canRestoreParent: lifetime.canRestoreParent,
|
|
339
|
+
copyOnSelect: effectiveFullscreenCopyOnSelect(settings),
|
|
340
|
+
layout: effectiveBtwLayout(settings),
|
|
341
|
+
sidePaneRatio: effectiveBtwSidePaneRatio(settings),
|
|
342
|
+
persistSidePaneRatio: (ratio, signal) =>
|
|
343
|
+
updateBtwSettings({ sidePaneRatio: ratio }, { signal }).then(() => undefined),
|
|
344
|
+
subscribeMainThreadUpdates: (listener) => subscribeMainThreadUpdates(ctx.sessionManager, listener),
|
|
345
|
+
...(settings.keybindings ? { keybindings: settings.keybindings } : {}),
|
|
346
|
+
},
|
|
347
|
+
);
|
|
348
|
+
} finally {
|
|
349
|
+
if (
|
|
350
|
+
!lifetime.signal.aborted &&
|
|
351
|
+
state &&
|
|
352
|
+
((state.title && state.thread.turns.length > 0) || state.thread.updates.length > 0)
|
|
353
|
+
) {
|
|
354
|
+
if (state.thread.turns.length > startingTurnCount) {
|
|
355
|
+
resumableThreads.delete(state.id);
|
|
325
356
|
}
|
|
357
|
+
resumableThreads.set(state.id, state);
|
|
326
358
|
}
|
|
327
|
-
}
|
|
328
|
-
}
|
|
359
|
+
}
|
|
360
|
+
}
|
|
329
361
|
}
|
|
330
362
|
|
|
331
363
|
async function showCommandMenuForBtw(
|
|
@@ -373,6 +405,7 @@ interface RunBtwThreadDependencies {
|
|
|
373
405
|
deliverBringToMain?: typeof loadBringToMainDraft;
|
|
374
406
|
persistThinkingLevel?: (level: BtwThinkingLevel) => Promise<unknown>;
|
|
375
407
|
now?: () => number;
|
|
408
|
+
importUpdates?: typeof importMainUpdates;
|
|
376
409
|
}
|
|
377
410
|
|
|
378
411
|
export type BtwThreadResult = { kind: "closed" };
|
|
@@ -418,6 +451,7 @@ export async function runBtwThread({
|
|
|
418
451
|
ctx,
|
|
419
452
|
dependencies = {},
|
|
420
453
|
}: RunBtwThreadOptions): Promise<BtwThreadResult> {
|
|
454
|
+
if (ctx.signal?.aborted) return { kind: "closed" };
|
|
421
455
|
const ask = dependencies.ask ?? askThreadQuestion;
|
|
422
456
|
const interact = dependencies.interact ?? showThreadComposer;
|
|
423
457
|
const chooseBringToMainAction = dependencies.chooseBringToMain ?? chooseBringToMain;
|
|
@@ -426,7 +460,8 @@ export async function runBtwThread({
|
|
|
426
460
|
dependencies.persistThinkingLevel ??
|
|
427
461
|
((level: BtwThinkingLevel) => updateBtwSettings({ thinkingLevel: level }, { settingsPath }));
|
|
428
462
|
const now = dependencies.now ?? Date.now;
|
|
429
|
-
const
|
|
463
|
+
const source = state ? undefined : captureMainContextSource(ctx.sessionManager);
|
|
464
|
+
const thread = state?.thread ?? createSideThread(source?.conversationContext ?? "", source?.anchor);
|
|
430
465
|
const thinkingLevels = getSupportedThinkingLevels(selected.model);
|
|
431
466
|
const pendingWrites = new Set<Promise<void>>();
|
|
432
467
|
const steeringQuestions: string[] = [];
|
|
@@ -438,7 +473,7 @@ export async function runBtwThread({
|
|
|
438
473
|
level: activeThinkingLevel,
|
|
439
474
|
levels: thinkingLevels,
|
|
440
475
|
onChange: (level) => {
|
|
441
|
-
if (!thinkingLevels.includes(level)) return;
|
|
476
|
+
if (ctx.signal?.aborted || !thinkingLevels.includes(level)) return;
|
|
442
477
|
activeThinkingLevel = level;
|
|
443
478
|
if (state) state.thinkingLevel = level;
|
|
444
479
|
if (!rememberThinkingLevelChanges) return;
|
|
@@ -460,18 +495,29 @@ export async function runBtwThread({
|
|
|
460
495
|
|
|
461
496
|
try {
|
|
462
497
|
while (true) {
|
|
498
|
+
if (ctx.signal?.aborted) return { kind: "closed" };
|
|
463
499
|
if (!pendingQuestion) {
|
|
464
|
-
const action = await interact(thread, thread.turns.length > 0, ctx, composerDraft, createThinkingControl()
|
|
465
|
-
|
|
500
|
+
const action = await interact(thread, thread.turns.length > 0, ctx, composerDraft, createThinkingControl(), {
|
|
501
|
+
id: selected.model.id,
|
|
502
|
+
provider: selected.model.provider,
|
|
503
|
+
});
|
|
504
|
+
if (ctx.signal?.aborted || action.kind === "close") return { kind: "closed" };
|
|
505
|
+
if (action.kind === "importMainUpdates") {
|
|
506
|
+
composerDraft = action.questionDraft;
|
|
507
|
+
const result = await (dependencies.importUpdates ?? importMainUpdates)(thread, ctx);
|
|
508
|
+
if (ctx.signal?.aborted || result === "close") return { kind: "closed" };
|
|
509
|
+
continue;
|
|
510
|
+
}
|
|
466
511
|
if (action.kind === "bringToMain") {
|
|
467
512
|
const choice = await chooseBringToMainAction(thread, ctx);
|
|
513
|
+
if (ctx.signal?.aborted) return { kind: "closed" };
|
|
468
514
|
if (choice.kind === "closed") return choice;
|
|
469
515
|
if (choice.kind === "back") {
|
|
470
516
|
composerDraft = action.questionDraft;
|
|
471
517
|
continue;
|
|
472
518
|
}
|
|
473
519
|
const delivery = await deliverBringToMainDraft(choice.draft, ctx, choice.summary);
|
|
474
|
-
if (delivery === "loaded" || delivery === "closed") return { kind: "closed" };
|
|
520
|
+
if (ctx.signal?.aborted || delivery === "loaded" || delivery === "closed") return { kind: "closed" };
|
|
475
521
|
composerDraft = action.questionDraft;
|
|
476
522
|
continue;
|
|
477
523
|
}
|
|
@@ -481,9 +527,12 @@ export async function runBtwThread({
|
|
|
481
527
|
|
|
482
528
|
const result = await ask(thread, pendingQuestion, selected, activeThinkingLevel, ctx, {
|
|
483
529
|
questions: steeringQuestions,
|
|
484
|
-
submit: (question) =>
|
|
530
|
+
submit: (question) => {
|
|
531
|
+
if (!ctx.signal?.aborted) steeringQuestions.push(question);
|
|
532
|
+
},
|
|
485
533
|
thinking: createThinkingControl(),
|
|
486
534
|
});
|
|
535
|
+
if (ctx.signal?.aborted) return { kind: "closed" };
|
|
487
536
|
if (result.kind === "aborted") {
|
|
488
537
|
notifySafely(ctx, "Cancelled", "info");
|
|
489
538
|
return { kind: "closed" };
|
|
@@ -517,6 +566,7 @@ export async function chooseBringToMain(
|
|
|
517
566
|
ctx: ExtensionCommandContext,
|
|
518
567
|
dependencies: ChooseBringToMainDependencies = {},
|
|
519
568
|
): Promise<BtwBringToMainChoice> {
|
|
569
|
+
if (ctx.signal?.aborted) return { kind: "closed" };
|
|
520
570
|
const answered = getAnsweredTurns(thread.turns);
|
|
521
571
|
if (answered.length === 0) return { kind: "back" };
|
|
522
572
|
const showMenu = dependencies.showMenu ?? showBtwMenu;
|
|
@@ -543,7 +593,7 @@ export async function chooseBringToMain(
|
|
|
543
593
|
[latestOption, fromOption, exactOption, entireOption, cancelOption],
|
|
544
594
|
selectedScope,
|
|
545
595
|
);
|
|
546
|
-
if (scopeResult.kind === "close") return { kind: "closed" };
|
|
596
|
+
if (ctx.signal?.aborted || scopeResult.kind === "close") return { kind: "closed" };
|
|
547
597
|
if (scopeResult.kind === "back" || scopeResult.value === cancelOption) return { kind: "back" };
|
|
548
598
|
const scope = scopeResult.value;
|
|
549
599
|
selectedScope = scope;
|
|
@@ -551,7 +601,7 @@ export async function chooseBringToMain(
|
|
|
551
601
|
if (scope === entireOption) {
|
|
552
602
|
const choice = makeChoice(entireSegments);
|
|
553
603
|
const preview = await showPreview(ctx, choice.draft, choice.summary);
|
|
554
|
-
if (preview.kind === "close") return { kind: "closed" };
|
|
604
|
+
if (ctx.signal?.aborted || preview.kind === "close") return { kind: "closed" };
|
|
555
605
|
if (preview.kind === "back") continue;
|
|
556
606
|
return choice;
|
|
557
607
|
}
|
|
@@ -562,14 +612,14 @@ export async function chooseBringToMain(
|
|
|
562
612
|
let selectedQuestion: string | undefined;
|
|
563
613
|
while (true) {
|
|
564
614
|
const questionResult = await showMenu(ctx, "Start from which question?", questions, selectedQuestion);
|
|
565
|
-
if (questionResult.kind === "close") return { kind: "closed" };
|
|
615
|
+
if (ctx.signal?.aborted || questionResult.kind === "close") return { kind: "closed" };
|
|
566
616
|
if (questionResult.kind === "back") break;
|
|
567
617
|
const answeredTurnIndex = questions.indexOf(questionResult.value);
|
|
568
618
|
if (answeredTurnIndex < 0) continue;
|
|
569
619
|
selectedQuestion = questionResult.value;
|
|
570
620
|
const choice = makeChoice(buildQuickBringToMainSegments(thread.turns, { kind: "from", answeredTurnIndex }));
|
|
571
621
|
const preview = await showPreview(ctx, choice.draft, choice.summary);
|
|
572
|
-
if (preview.kind === "close") return { kind: "closed" };
|
|
622
|
+
if (ctx.signal?.aborted || preview.kind === "close") return { kind: "closed" };
|
|
573
623
|
if (preview.kind === "back") continue;
|
|
574
624
|
return choice;
|
|
575
625
|
}
|
|
@@ -598,11 +648,11 @@ export async function chooseBringToMain(
|
|
|
598
648
|
return selector;
|
|
599
649
|
},
|
|
600
650
|
);
|
|
601
|
-
if (!selectedRange) return { kind: "closed" };
|
|
651
|
+
if (ctx.signal?.aborted || !selectedRange) return { kind: "closed" };
|
|
602
652
|
if (selectedRange.kind === "closed") return selectedRange;
|
|
603
653
|
if (selectedRange.kind === "back") break;
|
|
604
654
|
const preview = await showPreview(ctx, selectedRange.draft, selectedRange.summary);
|
|
605
|
-
if (preview.kind === "close") return { kind: "closed" };
|
|
655
|
+
if (ctx.signal?.aborted || preview.kind === "close") return { kind: "closed" };
|
|
606
656
|
if (preview.kind === "back") {
|
|
607
657
|
selectionState = selectedRange.selectionState;
|
|
608
658
|
continue;
|
|
@@ -650,8 +700,9 @@ async function showBringToMainPreview(
|
|
|
650
700
|
},
|
|
651
701
|
});
|
|
652
702
|
const result = await runBtwMenuPreservingEditor(ctx, (menuContext) =>
|
|
653
|
-
runMenu(menuContext, menu, { getState: () => undefined }),
|
|
703
|
+
runMenu(menuContext, menu, { getState: () => undefined, signal: ctx.signal }),
|
|
654
704
|
);
|
|
705
|
+
if (ctx.signal?.aborted) return { kind: "close" };
|
|
655
706
|
if (confirmed && result.kind === "closed" && result.reason === "close") {
|
|
656
707
|
return { kind: "bring" };
|
|
657
708
|
}
|
|
@@ -690,8 +741,9 @@ async function showBtwMenu(
|
|
|
690
741
|
},
|
|
691
742
|
});
|
|
692
743
|
const result = await runBtwMenuPreservingEditor(ctx, (menuContext) =>
|
|
693
|
-
runMenu(menuContext, menu, { getState: () => undefined }),
|
|
744
|
+
runMenu(menuContext, menu, { getState: () => undefined, signal: ctx.signal }),
|
|
694
745
|
);
|
|
746
|
+
if (ctx.signal?.aborted) return { kind: "close" };
|
|
695
747
|
return selectedValue !== undefined && result.kind === "closed" && result.reason === "close"
|
|
696
748
|
? { kind: "select", value: selectedValue }
|
|
697
749
|
: terminalBtwMenuAction(result);
|
|
@@ -708,6 +760,7 @@ export async function loadBringToMainDraft(
|
|
|
708
760
|
ctx: ExtensionCommandContext,
|
|
709
761
|
summary: BtwBringToMainSummary,
|
|
710
762
|
): Promise<BtwBringToMainDelivery> {
|
|
763
|
+
if (ctx.signal?.aborted) return "closed";
|
|
711
764
|
const describeContent = () =>
|
|
712
765
|
`${summary.messages} ${summary.messages === 1 ? "message" : "messages"} (~${summary.tokens} ${summary.tokens === 1 ? "token" : "tokens"})`;
|
|
713
766
|
const existing = ctx.ui.getEditorText();
|
|
@@ -726,7 +779,7 @@ export async function loadBringToMainDraft(
|
|
|
726
779
|
replaceOption,
|
|
727
780
|
cancelOption,
|
|
728
781
|
]);
|
|
729
|
-
if (action.kind === "close") return "closed";
|
|
782
|
+
if (ctx.signal?.aborted || action.kind === "close") return "closed";
|
|
730
783
|
if (action.kind === "back" || action.value === cancelOption) return "back";
|
|
731
784
|
if (action.value === appendOption) {
|
|
732
785
|
ctx.ui.setEditorText(`${ctx.ui.getEditorText()}\n\n${draft}`);
|
|
@@ -744,7 +797,7 @@ export async function loadBringToMainDraft(
|
|
|
744
797
|
"Back Keep current editor text",
|
|
745
798
|
"⚠ Replace current draft Cannot be undone",
|
|
746
799
|
]);
|
|
747
|
-
if (confirmed.kind === "close") return "closed";
|
|
800
|
+
if (ctx.signal?.aborted || confirmed.kind === "close") return "closed";
|
|
748
801
|
if (confirmed.kind === "back" || confirmed.value === "Back Keep current editor text") continue;
|
|
749
802
|
if (confirmed.value !== "⚠ Replace current draft Cannot be undone") continue;
|
|
750
803
|
if (ctx.ui.getEditorText() !== current) {
|
|
@@ -770,10 +823,11 @@ async function prepareCurrentTranscriptMarkdown(
|
|
|
770
823
|
ctx: ExtensionCommandContext,
|
|
771
824
|
): Promise<BtwMarkdownTransformers | undefined> {
|
|
772
825
|
while (true) {
|
|
826
|
+
if (ctx.signal?.aborted) return undefined;
|
|
773
827
|
const turnCount = thread.turns.length;
|
|
774
828
|
const createMarkdownTransformers = ctx.signal
|
|
775
|
-
? await prepareBtwTranscriptMarkdown(thread
|
|
776
|
-
: await prepareBtwTranscriptMarkdown(thread
|
|
829
|
+
? await prepareBtwTranscriptMarkdown(sideTranscript(thread), pendingQuestion, ctx.signal)
|
|
830
|
+
: await prepareBtwTranscriptMarkdown(sideTranscript(thread), pendingQuestion);
|
|
777
831
|
if (!createMarkdownTransformers || ctx.signal?.aborted) return undefined;
|
|
778
832
|
if (thread.turns.length === turnCount) return createMarkdownTransformers;
|
|
779
833
|
}
|
|
@@ -788,13 +842,13 @@ async function askThreadQuestion(
|
|
|
788
842
|
steering: BtwThreadSteeringControl,
|
|
789
843
|
) {
|
|
790
844
|
const createMarkdownTransformers = await prepareCurrentTranscriptMarkdown(thread, question, ctx);
|
|
791
|
-
if (!createMarkdownTransformers) return { kind: "aborted" as const };
|
|
845
|
+
if (!createMarkdownTransformers || ctx.signal?.aborted) return { kind: "aborted" as const };
|
|
792
846
|
return ctx.ui.custom<Awaited<ReturnType<typeof completeSideThreadTurn>>>((tui, theme, keybindings, done) => {
|
|
793
847
|
let settled = false;
|
|
794
848
|
const view = new BtwAnsweringView(
|
|
795
849
|
tui,
|
|
796
850
|
theme,
|
|
797
|
-
thread
|
|
851
|
+
sideTranscript(thread),
|
|
798
852
|
question,
|
|
799
853
|
() => {
|
|
800
854
|
if (settled) return;
|
|
@@ -803,6 +857,7 @@ async function askThreadQuestion(
|
|
|
803
857
|
},
|
|
804
858
|
thinkingLevel,
|
|
805
859
|
{
|
|
860
|
+
model: { id: selected.model.id, provider: selected.model.provider },
|
|
806
861
|
markdownTransformers: createMarkdownTransformers(theme),
|
|
807
862
|
steering: {
|
|
808
863
|
questions: steering.questions,
|
|
@@ -835,14 +890,28 @@ async function showThreadComposer(
|
|
|
835
890
|
ctx: ExtensionCommandContext,
|
|
836
891
|
initialQuestion: string | undefined,
|
|
837
892
|
thinking: BtwThreadThinkingControl,
|
|
893
|
+
model?: BtwDisplayModel,
|
|
838
894
|
): Promise<TranscriptPagerAction> {
|
|
839
895
|
const createMarkdownTransformers = await prepareCurrentTranscriptMarkdown(thread, initialQuestion, ctx);
|
|
840
|
-
if (!createMarkdownTransformers) return { kind: "close" };
|
|
896
|
+
if (!createMarkdownTransformers || ctx.signal?.aborted) return { kind: "close" };
|
|
897
|
+
let statusLeaf: string | null | undefined;
|
|
898
|
+
let hasUpdates = false;
|
|
899
|
+
const mainUpdates = () => {
|
|
900
|
+
if (ctx.signal?.aborted || !thread.source) return false;
|
|
901
|
+
const leaf = ctx.sessionManager.getLeafId();
|
|
902
|
+
if (leaf !== statusLeaf) {
|
|
903
|
+
statusLeaf = leaf;
|
|
904
|
+
hasUpdates = buildMainContextSnapshot(ctx.sessionManager, thread.source).kind === "ready";
|
|
905
|
+
}
|
|
906
|
+
return hasUpdates;
|
|
907
|
+
};
|
|
841
908
|
return ctx.ui.custom<TranscriptPagerAction>(
|
|
842
909
|
(tui, theme, keybindings, done) =>
|
|
843
|
-
new BtwTranscriptPager(tui, theme, thread
|
|
910
|
+
new BtwTranscriptPager(tui, theme, sideTranscript(thread), done, {
|
|
911
|
+
mainUpdates,
|
|
844
912
|
startAtBottom,
|
|
845
913
|
initialQuestion,
|
|
914
|
+
model,
|
|
846
915
|
markdownTransformers: createMarkdownTransformers(theme),
|
|
847
916
|
thinking: { ...thinking, keybindings },
|
|
848
917
|
}),
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import type { Theme } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { Loader, truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
|
|
3
|
+
import type { BtwThinkingLevel } from "./side-thread.js";
|
|
4
|
+
import { sanitizeSingleLine } from "./text.js";
|
|
5
|
+
|
|
6
|
+
/** Display-only identity, not a model/session runtime or a source of request options. */
|
|
7
|
+
export interface BtwDisplayModel {
|
|
8
|
+
id: string;
|
|
9
|
+
provider: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface BtwWorkspaceFocus {
|
|
13
|
+
pane: "side" | "main";
|
|
14
|
+
shortcutLabel: string | undefined;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// Pi's status classes are not root exports. Use the public Loader extension
|
|
18
|
+
// points to supply the same border text without importing app-owned handlers.
|
|
19
|
+
export class BtwWorkingIndicator extends Loader {
|
|
20
|
+
renderInBorder(width: number): string {
|
|
21
|
+
const line = super.render(width + 2)[1] ?? "";
|
|
22
|
+
return truncateToWidth(line.startsWith(" ") ? line.slice(1).trimEnd() : line.trimEnd(), width, "");
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
renderSpinnerInBorder(width: number): string {
|
|
26
|
+
return truncateToWidth(this.getRenderedIndicator(), width, "");
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Native CustomEditor's border geometry, through public Editor/Loader hooks. */
|
|
31
|
+
export function renderWorkingBorder(
|
|
32
|
+
width: number,
|
|
33
|
+
hiddenLineCount: number,
|
|
34
|
+
borderColor: (text: string) => string,
|
|
35
|
+
status: BtwWorkingIndicator,
|
|
36
|
+
): string | undefined {
|
|
37
|
+
if (width <= 0) return "";
|
|
38
|
+
let label = status.renderInBorder(Math.max(1, width - 5));
|
|
39
|
+
let labelWidth = visibleWidth(label);
|
|
40
|
+
if (labelWidth === 0) return undefined;
|
|
41
|
+
const overflow = hiddenLineCount > 0 ? ` ↑ ${hiddenLineCount} more ` : undefined;
|
|
42
|
+
const overflowWidth = overflow ? visibleWidth(overflow) : 0;
|
|
43
|
+
const overflowStart = Math.floor((width - overflowWidth) / 2);
|
|
44
|
+
const fitsOverflow = () =>
|
|
45
|
+
overflow !== undefined && overflowWidth + 2 <= width && overflowStart - (3 + labelWidth + 1) >= 1;
|
|
46
|
+
if (overflow && !fitsOverflow()) {
|
|
47
|
+
label = status.renderSpinnerInBorder(width);
|
|
48
|
+
labelWidth = visibleWidth(label);
|
|
49
|
+
}
|
|
50
|
+
if (fitsOverflow()) {
|
|
51
|
+
const leftWidth = 3 + labelWidth + 1;
|
|
52
|
+
return (
|
|
53
|
+
borderColor("── ") +
|
|
54
|
+
label +
|
|
55
|
+
borderColor(
|
|
56
|
+
` ${"─".repeat(overflowStart - leftWidth)}${overflow}${"─".repeat(width - overflowStart - overflowWidth)}`,
|
|
57
|
+
)
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
if (width >= labelWidth + 5) {
|
|
61
|
+
return borderColor("── ") + label + borderColor(` ${"─".repeat(width - labelWidth - 4)}`);
|
|
62
|
+
}
|
|
63
|
+
label = status.renderSpinnerInBorder(width);
|
|
64
|
+
labelWidth = visibleWidth(label);
|
|
65
|
+
const prefix = Math.min(3, Math.max(0, width - labelWidth));
|
|
66
|
+
return borderColor("─".repeat(prefix)) + label + borderColor("─".repeat(Math.max(0, width - prefix - labelWidth)));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function renderConversationFooter(
|
|
70
|
+
width: number,
|
|
71
|
+
theme: Theme,
|
|
72
|
+
options: {
|
|
73
|
+
model?: BtwDisplayModel;
|
|
74
|
+
thinkingLevel?: BtwThinkingLevel;
|
|
75
|
+
focus?: BtwWorkspaceFocus;
|
|
76
|
+
hint: string;
|
|
77
|
+
warning?: boolean;
|
|
78
|
+
},
|
|
79
|
+
): string[] {
|
|
80
|
+
if (width < 3) return [];
|
|
81
|
+
const focus = options.focus ? `${options.focus.pane === "side" ? "BTW" : "Main"} focus` : undefined;
|
|
82
|
+
const left = focus && visibleWidth(`btw · ${focus}`) <= width ? `btw · ${focus}` : "btw";
|
|
83
|
+
const model = options.model ? sanitizeSingleLine(`${options.model.provider}/${options.model.id}`) : "";
|
|
84
|
+
const thinking = options.thinkingLevel ? `thinking ${options.thinkingLevel}` : "";
|
|
85
|
+
const right = [[model, thinking].filter(Boolean).join(" • "), thinking, model].find(
|
|
86
|
+
(value) => value && visibleWidth(left) + 2 + visibleWidth(value) <= width,
|
|
87
|
+
);
|
|
88
|
+
const identity = right ? `${left}${" ".repeat(width - visibleWidth(left) - visibleWidth(right))}${right}` : left;
|
|
89
|
+
return [
|
|
90
|
+
theme.fg("dim", identity),
|
|
91
|
+
...(options.hint ? [theme.fg(options.warning ? "warning" : "muted", options.hint)] : []),
|
|
92
|
+
];
|
|
93
|
+
}
|