@each1024/pi-mini-mode 2.0.19 → 2.0.21
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/extensions/footer-status.ts +64 -8
- package/lib/agent-render.ts +4 -4
- package/lib/agent-state.ts +170 -34
- package/lib/agent-widgets.ts +6 -3
- package/lib/assistant-images.ts +1 -1
- package/lib/decision-dock.ts +243 -0
- package/lib/input-enhancements-types.ts +2 -0
- package/lib/input-enhancements.ts +3 -1
- package/lib/input-hover-preview.ts +6 -4
- package/lib/input-image-graphics.ts +3 -2
- package/lib/input-image-paths.ts +36 -8
- package/lib/input-inline-images.ts +12 -4
- package/lib/input-skill-completion.ts +2 -1
- package/lib/link-hover.ts +100 -7
- package/lib/minimal-markdown.ts +22 -4
- package/lib/minimal-output.ts +56 -10
- package/lib/minimal-theme.ts +2 -2
- package/lib/semantic-badges.ts +274 -0
- package/lib/settings.html +12 -0
- package/lib/settings.ts +12 -4
- package/lib/token-speed.ts +67 -20
- package/lib/transcript-adapter.ts +14 -0
- package/lib/transcript-cards.ts +7 -6
- package/lib/transcript-notice-render.ts +9 -6
- package/lib/transcript-notices.ts +72 -16
- package/lib/update-banner.ts +220 -54
- package/package.json +1 -1
|
@@ -15,6 +15,8 @@ import { addUsage, cacheWaste, EMPTY_USAGE, finiteNumber, formatSpeed, sessionUs
|
|
|
15
15
|
import { statusLine } from "../lib/footer-line.ts";
|
|
16
16
|
import { contentText, minimalTurnsFromBranch, processText, pushProcess, skillNames, type MinimalTurn } from "../lib/minimal-turns.ts";
|
|
17
17
|
import { minimalOutputComponent } from "../lib/minimal-output.ts";
|
|
18
|
+
import { createDecisionDock, extractDecisionPrompt } from "../lib/decision-dock.ts";
|
|
19
|
+
import { appendTriageGuidance, isBadgeStyle } from "../lib/semantic-badges.ts";
|
|
18
20
|
import { createThinkingStream } from "../lib/thinking-stream.ts";
|
|
19
21
|
import { PACKAGE_VERSION, isNewerVersion } from "../lib/update-banner.ts";
|
|
20
22
|
|
|
@@ -26,6 +28,8 @@ export { SPEED_REFRESH_MS } from "../lib/token-speed.ts";
|
|
|
26
28
|
export { isPlannotatorPlanningStatus, planCapsule, settingsPreviewLine, statusLine } from "../lib/footer-line.ts";
|
|
27
29
|
export { formatElapsed, formatToolElapsed, formatThinkingElapsed, minimalTurnsFromBranch, type MinimalTurn, type ThinkingClock } from "../lib/minimal-turns.ts";
|
|
28
30
|
export { minimalOutputComponent } from "../lib/minimal-output.ts";
|
|
31
|
+
export { isBadgeStyle, parseSemanticBadge, renderSemanticBadge, transformSemanticBadges, appendTriageGuidance, type BadgeStyle, type BadgeTone } from "../lib/semantic-badges.ts";
|
|
32
|
+
export { extractDecisionPrompt, createDecisionDock, type DecisionChoice, type DecisionPrompt } from "../lib/decision-dock.ts";
|
|
29
33
|
export { PACKAGE_VERSION, UPDATE_RELEASE_DATE, isNewerVersion, renderUpdateBanner } from "../lib/update-banner.ts";
|
|
30
34
|
|
|
31
35
|
/** Pi /model and other selectors own Ctrl+S; the editor exposes getText. */
|
|
@@ -87,19 +91,35 @@ export default function (pi: ExtensionAPI) {
|
|
|
87
91
|
let speedTimer: ReturnType<typeof setInterval> | undefined;
|
|
88
92
|
let settings: MiniLensSettings = { ...DEFAULT_SETTINGS };
|
|
89
93
|
let cleanupInputEnhancements: InputEnhancementsCleanup | undefined;
|
|
94
|
+
let liveCtx: ExtensionContext | undefined;
|
|
90
95
|
let messageCwd = process.cwd();
|
|
91
96
|
const messageLinks = (text: string) => settings["pi-mini-mode-input-enhancements"] ? linkMessageFiles(text, messageCwd) : text;
|
|
92
97
|
pi.registerMarkdownTransformer?.(messageLinks);
|
|
93
98
|
let saveChain: Promise<void> = Promise.resolve();
|
|
94
99
|
const configPath = settingsPath();
|
|
95
100
|
let updateBannerChecked = false;
|
|
96
|
-
let activeUpdateBanner: { sessionKey: string; version: string; collapsed: boolean } | undefined;
|
|
101
|
+
let activeUpdateBanner: { sessionKey: string; version: string; previousVersion?: string; collapsed: boolean } | undefined;
|
|
97
102
|
const getSessionKey = (ctx: ExtensionContext) =>
|
|
98
103
|
ctx.sessionManager.getSessionFile?.() ?? (ctx.sessionManager as { getSessionId?(): string | undefined }).getSessionId?.() ?? "";
|
|
99
104
|
|
|
100
105
|
const refresh = () => refreshFooter?.();
|
|
101
106
|
const refreshMinimalOutput = () => refreshMinimal?.();
|
|
107
|
+
const decisionDock = createDecisionDock({
|
|
108
|
+
language: () => uiLanguage(settings),
|
|
109
|
+
isEditorEmpty: () => (liveCtx?.ui.getEditorText?.() ?? "").trim() === "",
|
|
110
|
+
onSubmit: (replyText) => pi.sendUserMessage?.(replyText),
|
|
111
|
+
requestRender: () => refreshMinimalOutput(),
|
|
112
|
+
});
|
|
113
|
+
const syncDecisionDock = () => {
|
|
114
|
+
const lastTurn = minimalTurns.at(-1);
|
|
115
|
+
const enabled = settings["pi-mini-mode-minimal-show"] && settings["pi-mini-mode-semantic-badges"] && !nativeOutput && liveCtx?.mode === "tui";
|
|
116
|
+
const prompt = enabled && !activeMinimalTurn?.running && lastTurn && !lastTurn.running
|
|
117
|
+
? extractDecisionPrompt(lastTurn.final, minimalTurns.length - 1)
|
|
118
|
+
: undefined;
|
|
119
|
+
if (decisionDock.sync(prompt)) refreshMinimalOutput();
|
|
120
|
+
};
|
|
102
121
|
const mountMinimalOutput = (ctx: ExtensionContext) => {
|
|
122
|
+
liveCtx = ctx;
|
|
103
123
|
persistAgentStatuses = undefined;
|
|
104
124
|
if (waitingTimer) clearInterval(waitingTimer);
|
|
105
125
|
waitingTimer = undefined;
|
|
@@ -115,6 +135,15 @@ export default function (pi: ExtensionAPI) {
|
|
|
115
135
|
const nativeKey = matchesKey(data, "ctrl+alt+o");
|
|
116
136
|
const subAgentKey = matchesKey(data, "ctrl+s");
|
|
117
137
|
if (!nativeKey && focusedSelectorOwnsKeys(minimalTui)) return;
|
|
138
|
+
if (!nativeKey && restoreTranscript && decisionDock.isActive) {
|
|
139
|
+
const dockNav = matchesKey(data, "escape")
|
|
140
|
+
|| (((liveCtx?.ui.getEditorText?.() ?? "").trim() === "")
|
|
141
|
+
&& (matchesKey(data, "up") || matchesKey(data, "down") || matchesKey(data, "enter") || matchesKey(data, "return")));
|
|
142
|
+
if (dockNav) {
|
|
143
|
+
if (isKeyRelease(data) || isKeyRepeat(data)) return { consume: true };
|
|
144
|
+
return decisionDock.handleTerminalInput(data);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
118
147
|
if (!nativeKey && (!restoreTranscript || (!subAgentKey && !matchesKey(data, "ctrl+o")))) return;
|
|
119
148
|
// Input listeners run before Pi filters Kitty release/repeat events.
|
|
120
149
|
if (isKeyRelease(data) || isKeyRepeat(data)) return { consume: true };
|
|
@@ -132,6 +161,8 @@ export default function (pi: ExtensionAPI) {
|
|
|
132
161
|
if (nativeOutput || !settings["pi-mini-mode-minimal-show"] || ctx.mode !== "tui") {
|
|
133
162
|
minimalTui = undefined;
|
|
134
163
|
lastAttachMode = undefined;
|
|
164
|
+
decisionDock.sync(undefined);
|
|
165
|
+
ctx.ui.setWidget?.("pi-mini-mode-decision", undefined);
|
|
135
166
|
ctx.ui.setWidget?.("pi-mini-mode-minimal-output", undefined);
|
|
136
167
|
refreshMinimalOutput();
|
|
137
168
|
refreshMinimal = undefined;
|
|
@@ -187,6 +218,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
187
218
|
updateBanner: () => activeUpdateBanner && activeUpdateBanner.sessionKey === sessionKey
|
|
188
219
|
? {
|
|
189
220
|
version: activeUpdateBanner.version,
|
|
221
|
+
previousVersion: activeUpdateBanner.previousVersion,
|
|
190
222
|
collapsed: activeUpdateBanner.collapsed,
|
|
191
223
|
toggle: () => {
|
|
192
224
|
if (!activeUpdateBanner) return;
|
|
@@ -195,6 +227,10 @@ export default function (pi: ExtensionAPI) {
|
|
|
195
227
|
},
|
|
196
228
|
}
|
|
197
229
|
: undefined,
|
|
230
|
+
semanticBadges: () => ({
|
|
231
|
+
enabled: settings["pi-mini-mode-semantic-badges"],
|
|
232
|
+
style: settings["pi-mini-mode-badge-style"],
|
|
233
|
+
}),
|
|
198
234
|
}, () => settings["pi-mini-mode-minimal-record-limit"], () => uiLanguage(settings));
|
|
199
235
|
// Native info notifications use dim; warning/error notifications retain
|
|
200
236
|
// their position in the transcript. No plugin names or message matching.
|
|
@@ -204,12 +240,13 @@ export default function (pi: ExtensionAPI) {
|
|
|
204
240
|
const hideStartupLine = (line: string) => line.replace(/\x1b\[[0-9;]*m/g, "").includes("No models match pattern");
|
|
205
241
|
// Transcript hover wrappers must surround the compact widget handlers;
|
|
206
242
|
// unmount in reverse order so neither adapter resurrects stale handlers.
|
|
207
|
-
restoreAgentWidgets = attachAgentWidgets(tui, theme, () => subAgentsExpanded, undefined, true);
|
|
243
|
+
restoreAgentWidgets = attachAgentWidgets(tui, theme, () => subAgentsExpanded, undefined, true, () => uiLanguage(settings));
|
|
208
244
|
restoreTranscript = attachTranscript(tui, view, {
|
|
209
245
|
turnCount: () => minimalTurns.length,
|
|
210
246
|
supervisor: {
|
|
211
247
|
theme,
|
|
212
248
|
expanded: () => processExpanded,
|
|
249
|
+
language: () => uiLanguage(settings),
|
|
213
250
|
handledRunIds: () => handledRunIds,
|
|
214
251
|
onNotices: groups => {
|
|
215
252
|
let changed = false;
|
|
@@ -272,6 +309,13 @@ export default function (pi: ExtensionAPI) {
|
|
|
272
309
|
}, 100);
|
|
273
310
|
waitingTimer.unref();
|
|
274
311
|
}
|
|
312
|
+
if (restoreTranscript && settings["pi-mini-mode-semantic-badges"]) {
|
|
313
|
+
ctx.ui.setWidget?.("pi-mini-mode-decision", (_dockTui, dockTheme) => decisionDock.component(dockTheme), { placement: "aboveEditor" });
|
|
314
|
+
syncDecisionDock();
|
|
315
|
+
} else {
|
|
316
|
+
decisionDock.sync(undefined);
|
|
317
|
+
ctx.ui.setWidget?.("pi-mini-mode-decision", undefined);
|
|
318
|
+
}
|
|
275
319
|
tui.requestRender();
|
|
276
320
|
// Factory acquires the renderer only; never duplicate content in the dock.
|
|
277
321
|
return { render: () => [], invalidate() {} };
|
|
@@ -370,6 +414,8 @@ export default function (pi: ExtensionAPI) {
|
|
|
370
414
|
|| entry.some(id => typeof id !== "string" || !(FOOTER_FIELDS as readonly string[]).includes(id))) throw new TypeError("Invalid order");
|
|
371
415
|
} else if (key === "pi-mini-mode-image-preview") {
|
|
372
416
|
if (entry !== "hover" && entry !== "inline") throw new TypeError("Invalid preview mode");
|
|
417
|
+
} else if (key === "pi-mini-mode-badge-style") {
|
|
418
|
+
if (!isBadgeStyle(entry)) throw new TypeError("Invalid badge style");
|
|
373
419
|
} else if (key === "pi-mini-mode-minimal-record-limit") {
|
|
374
420
|
if (!isRecordLimit(entry)) throw new TypeError("Invalid record limit");
|
|
375
421
|
} else if (key === "pi-mini-mode-language") {
|
|
@@ -453,7 +499,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
453
499
|
settings = loaded.settings;
|
|
454
500
|
messageCwd = ctx.cwd;
|
|
455
501
|
// 输入增强只在会话中生效,会话开始时再加载;传 getter 让开关变更即时生效,重复调用是幂等的。
|
|
456
|
-
cleanupInputEnhancements = installInputEnhancements(pi, ctx, () => settings["pi-mini-mode-input-enhancements"], () => settings["pi-mini-mode-image-preview"]);
|
|
502
|
+
cleanupInputEnhancements = installInputEnhancements(pi, ctx, () => settings["pi-mini-mode-input-enhancements"], () => settings["pi-mini-mode-image-preview"], () => uiLanguage(settings));
|
|
457
503
|
thinkingStream.stop(activeMinimalTurn, "aborted");
|
|
458
504
|
thinkingStream.reset();
|
|
459
505
|
minimalTurns = minimalTurnsFromBranch(ctx.sessionManager.getBranch());
|
|
@@ -479,7 +525,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
479
525
|
await saveSettings(settings, configPath).catch(() => {});
|
|
480
526
|
}
|
|
481
527
|
if (loaded.exists && versionChanged && ctx.mode === "tui" && ctx.hasUI && minimalTurns.length === 0) {
|
|
482
|
-
activeUpdateBanner = { sessionKey: currentSessionKey, version: PACKAGE_VERSION, collapsed: false };
|
|
528
|
+
activeUpdateBanner = { sessionKey: currentSessionKey, version: PACKAGE_VERSION, previousVersion: prevVersion, collapsed: false };
|
|
483
529
|
}
|
|
484
530
|
}
|
|
485
531
|
mountMinimalOutput(ctx);
|
|
@@ -497,7 +543,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
497
543
|
refreshFooter = () => tui.requestRender();
|
|
498
544
|
speedHover?.dispose();
|
|
499
545
|
speedHover = attachSpeedHover(tui, theme, () => ({
|
|
500
|
-
metrics: settings["pi-mini-mode-speed-show"] ? speeds.metrics() : [],
|
|
546
|
+
metrics: settings["pi-mini-mode-speed-show"] ? speeds.metrics(uiLanguage(settings)) : [],
|
|
501
547
|
hit: speedHitBox,
|
|
502
548
|
}));
|
|
503
549
|
const onBranchChange = footerData?.onBranchChange?.(() => tui.requestRender());
|
|
@@ -552,6 +598,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
552
598
|
minimalTurns = minimalTurnsFromBranch(ctx.sessionManager.getBranch());
|
|
553
599
|
activeMinimalTurn = undefined;
|
|
554
600
|
pendingMinimalFinal = "";
|
|
601
|
+
decisionDock.clearAll();
|
|
555
602
|
thinkingStream.reset();
|
|
556
603
|
minimalToolOutputIndices.clear();
|
|
557
604
|
mountMinimalOutput(ctx);
|
|
@@ -565,9 +612,13 @@ export default function (pi: ExtensionAPI) {
|
|
|
565
612
|
syncMinimalBranch(event, ctx);
|
|
566
613
|
});
|
|
567
614
|
pi.on("before_agent_start", (event) => {
|
|
568
|
-
if (
|
|
569
|
-
|
|
570
|
-
|
|
615
|
+
if (activeMinimalTurn) {
|
|
616
|
+
for (const name of skillNames(event.prompt)) pushProcess(activeMinimalTurn, "skill", name);
|
|
617
|
+
refreshMinimalOutput();
|
|
618
|
+
}
|
|
619
|
+
if (settings["pi-mini-mode-minimal-show"] && settings["pi-mini-mode-semantic-badges"] && !nativeOutput && typeof event.systemPrompt === "string" && event.systemPrompt) {
|
|
620
|
+
return { systemPrompt: appendTriageGuidance(event.systemPrompt) };
|
|
621
|
+
}
|
|
571
622
|
});
|
|
572
623
|
pi.on("ui_prompt_start", (event) => {
|
|
573
624
|
activeUIPrompt = event;
|
|
@@ -581,6 +632,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
581
632
|
pi.on("thinking_level_select", refresh);
|
|
582
633
|
pi.on("message_start", (event) => {
|
|
583
634
|
if (event.message.role === "user") {
|
|
635
|
+
decisionDock.sync(undefined);
|
|
584
636
|
if (activeMinimalTurn) {
|
|
585
637
|
thinkingStream.stop(activeMinimalTurn, "aborted");
|
|
586
638
|
activeMinimalTurn.final = pendingMinimalFinal;
|
|
@@ -760,11 +812,15 @@ export default function (pi: ExtensionAPI) {
|
|
|
760
812
|
pendingMinimalFinal = "";
|
|
761
813
|
thinkingStream.reset();
|
|
762
814
|
minimalToolOutputIndices.clear();
|
|
815
|
+
syncDecisionDock();
|
|
763
816
|
refreshMinimalOutput();
|
|
764
817
|
});
|
|
765
818
|
pi.on("session_shutdown", () => {
|
|
766
819
|
thinkingStream.stop(activeMinimalTurn, "aborted");
|
|
767
820
|
thinkingStream.reset();
|
|
821
|
+
decisionDock.clearAll();
|
|
822
|
+
liveCtx?.ui.setWidget?.("pi-mini-mode-decision", undefined);
|
|
823
|
+
liveCtx = undefined;
|
|
768
824
|
restoreScrollbar?.();
|
|
769
825
|
restoreScrollbar = undefined;
|
|
770
826
|
scrollbarRoot = undefined;
|
package/lib/agent-render.ts
CHANGED
|
@@ -375,11 +375,11 @@ export function liveAgentView(statuses: Record<string, unknown>[], theme: Theme,
|
|
|
375
375
|
const item = msg && typeof msg === "object" ? msg as Record<string, unknown> : undefined;
|
|
376
376
|
const details = item?.details && typeof item.details === "object" ? item.details as Record<string, unknown> : undefined;
|
|
377
377
|
if (item?.customType === "subagent_supervisor_request" && details?.reason === "progress_update") {
|
|
378
|
-
const progress = extractNoticeBody(msg);
|
|
378
|
+
const progress = extractNoticeBody(msg, lang);
|
|
379
379
|
if (progress) latestProgress = progress;
|
|
380
380
|
continue;
|
|
381
381
|
}
|
|
382
|
-
const b = supervisorNoticeBody(msg);
|
|
382
|
+
const b = supervisorNoticeBody(msg, lang);
|
|
383
383
|
if (b && !bodies.includes(b)) bodies.push(b);
|
|
384
384
|
}
|
|
385
385
|
if (!bodies.length && latestProgress) bodies.push(latestProgress);
|
|
@@ -501,11 +501,11 @@ export function liveAgentView(statuses: Record<string, unknown>[], theme: Theme,
|
|
|
501
501
|
const item = msg && typeof msg === "object" ? msg as Record<string, unknown> : undefined;
|
|
502
502
|
const details = item?.details && typeof item.details === "object" ? item.details as Record<string, unknown> : undefined;
|
|
503
503
|
if (item?.customType === "subagent_supervisor_request" && details?.reason === "progress_update") {
|
|
504
|
-
const progress = extractNoticeBody(msg);
|
|
504
|
+
const progress = extractNoticeBody(msg, lang);
|
|
505
505
|
if (progress) latestProgress = progress;
|
|
506
506
|
continue;
|
|
507
507
|
}
|
|
508
|
-
const b = supervisorNoticeBody(msg);
|
|
508
|
+
const b = supervisorNoticeBody(msg, lang);
|
|
509
509
|
if (b && !bodies.includes(b)) bodies.push(b);
|
|
510
510
|
}
|
|
511
511
|
if (!bodies.length && latestProgress) bodies.push(latestProgress);
|
package/lib/agent-state.ts
CHANGED
|
@@ -187,14 +187,16 @@ export function retainAgentStatuses(previous: Record<string, unknown>[], current
|
|
|
187
187
|
const retained = new Map(previous.map(status => [String(status.runId), status]));
|
|
188
188
|
for (const status of new Map([...previous, ...current].map(status => [String(status.runId), status])).values()) {
|
|
189
189
|
const saved = retained.get(String(status.runId));
|
|
190
|
-
const children = agentChildren([status])
|
|
191
|
-
|
|
190
|
+
const children = agentChildren([status]);
|
|
191
|
+
const previousChildren = saved && saved !== status ? agentChildren([saved]) : saved ? children : undefined;
|
|
192
|
+
const merged = children.map(child => {
|
|
193
|
+
const previous = previousChildren?.find(item => item.displayId === child.displayId);
|
|
192
194
|
const sameAttempt = previous?.sessionFile === child.sessionFile && previous?.startedAt === child.startedAt;
|
|
193
195
|
const finalOutput = childSessionData(child.sessionFile)?.finalOutput ?? child.finalOutput ?? (sameAttempt ? previous?.finalOutput : undefined);
|
|
194
196
|
return { ...child, finalOutput, notice: child.notice ?? previous?.notice, noticeMessages: child.noticeMessages ?? previous?.noticeMessages };
|
|
195
197
|
});
|
|
196
|
-
retained.set(String(status.runId),
|
|
197
|
-
? { ...status, steps:
|
|
198
|
+
retained.set(String(status.runId), merged.length
|
|
199
|
+
? { ...status, steps: merged, workflowChildren: undefined, children: undefined } : status);
|
|
198
200
|
}
|
|
199
201
|
return [...retained.values()];
|
|
200
202
|
}
|
|
@@ -218,38 +220,173 @@ export function readAgentStatuses(sessionId: string, root = process.env.PI_SUBAG
|
|
|
218
220
|
} catch { return previous.filter(status => status.sessionId === sessionId); }
|
|
219
221
|
}
|
|
220
222
|
|
|
221
|
-
|
|
223
|
+
// Frames re-read every call output unless the source values are unchanged. Object calls
|
|
224
|
+
// keep their extracted run ids in a WeakMap (collected with the call); short string ids use
|
|
225
|
+
// a bounded LRU. Any edited source rescans that call. An append can create or destroy a
|
|
226
|
+
// UUID word boundary, so a tail increment is not reused.
|
|
227
|
+
const RUN_ID_PATTERN = /\b([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\b/gi;
|
|
228
|
+
const STRING_RUN_CACHE_LIMIT = 256;
|
|
229
|
+
const STRING_RUN_CACHE_MAX_LENGTH = 128;
|
|
230
|
+
const stringRunCache = new Map<string, string | null>();
|
|
231
|
+
type SourceSnap = { ref: unknown; text?: string };
|
|
232
|
+
type CallSnap = { id: SourceSnap; output: SourceSnap; task: SourceSnap; action: SourceSnap; callId?: string; runIds: string[] };
|
|
233
|
+
const callSnaps = new WeakMap<object, CallSnap>();
|
|
234
|
+
|
|
235
|
+
function sourceSnap(value: unknown): SourceSnap {
|
|
236
|
+
if (typeof value === "string" || value == null) return { ref: value };
|
|
237
|
+
return { ref: value, text: String(value) };
|
|
238
|
+
}
|
|
239
|
+
function sourceText(snap: SourceSnap): string {
|
|
240
|
+
if (typeof snap.ref === "string") return snap.ref;
|
|
241
|
+
if (snap.ref == null) return "";
|
|
242
|
+
return snap.text ?? "";
|
|
243
|
+
}
|
|
244
|
+
function sameSource(value: unknown, snap: SourceSnap): boolean {
|
|
245
|
+
if (typeof value === "string" || value == null) return value === snap.ref;
|
|
246
|
+
return String(value) === snap.text;
|
|
247
|
+
}
|
|
248
|
+
function collectRunIds(text: string, seen: Set<string>, into: string[]) {
|
|
249
|
+
if (text.length < 36) return;
|
|
250
|
+
RUN_ID_PATTERN.lastIndex = 0;
|
|
251
|
+
let match: RegExpExecArray | null;
|
|
252
|
+
while ((match = RUN_ID_PATTERN.exec(text))) {
|
|
253
|
+
if (seen.has(match[1])) continue;
|
|
254
|
+
seen.add(match[1]);
|
|
255
|
+
into.push(match[1]);
|
|
256
|
+
}
|
|
257
|
+
RUN_ID_PATTERN.lastIndex = 0;
|
|
258
|
+
}
|
|
259
|
+
function firstRunId(text: string): string | null {
|
|
260
|
+
if (text.length < 36) return null;
|
|
261
|
+
RUN_ID_PATTERN.lastIndex = 0;
|
|
262
|
+
const match = RUN_ID_PATTERN.exec(text);
|
|
263
|
+
RUN_ID_PATTERN.lastIndex = 0;
|
|
264
|
+
return match?.[1] ?? null;
|
|
265
|
+
}
|
|
266
|
+
function cachedStringRunId(item: string): string | null {
|
|
267
|
+
if (item.length > STRING_RUN_CACHE_MAX_LENGTH) return firstRunId(item);
|
|
268
|
+
const cached = stringRunCache.get(item);
|
|
269
|
+
if (cached !== undefined) {
|
|
270
|
+
stringRunCache.delete(item);
|
|
271
|
+
stringRunCache.set(item, cached);
|
|
272
|
+
return cached;
|
|
273
|
+
}
|
|
274
|
+
const found = firstRunId(item);
|
|
275
|
+
stringRunCache.set(item, found);
|
|
276
|
+
if (stringRunCache.size > STRING_RUN_CACHE_LIMIT) {
|
|
277
|
+
const oldest = stringRunCache.keys().next().value;
|
|
278
|
+
if (oldest !== undefined) stringRunCache.delete(oldest);
|
|
279
|
+
}
|
|
280
|
+
return found;
|
|
281
|
+
}
|
|
282
|
+
function callSnap(item: object): CallSnap {
|
|
283
|
+
const call = item as Record<string, unknown>;
|
|
284
|
+
const cached = callSnaps.get(item);
|
|
285
|
+
if (cached && sameSource(call.id, cached.id) && sameSource(call.output, cached.output)
|
|
286
|
+
&& sameSource(call.task, cached.task) && sameSource(call.action, cached.action)) return cached;
|
|
287
|
+
const output = sourceSnap(call.output);
|
|
288
|
+
const task = sourceSnap(call.task);
|
|
289
|
+
const action = sourceSnap(call.action);
|
|
290
|
+
const runIds: string[] = [];
|
|
291
|
+
const seen = new Set<string>();
|
|
292
|
+
collectRunIds(sourceText(output), seen, runIds);
|
|
293
|
+
collectRunIds(sourceText(task), seen, runIds);
|
|
294
|
+
collectRunIds(sourceText(action), seen, runIds);
|
|
295
|
+
const next: CallSnap = {
|
|
296
|
+
id: sourceSnap(call.id), output, task, action,
|
|
297
|
+
callId: typeof call.id === "string" ? call.id : undefined, runIds,
|
|
298
|
+
};
|
|
299
|
+
callSnaps.set(item, next);
|
|
300
|
+
return next;
|
|
301
|
+
}
|
|
302
|
+
type CallKeys = { callIds: Set<string>; runIds: Set<string> };
|
|
303
|
+
function collectCallKeys(items: unknown[]): CallKeys {
|
|
222
304
|
const callIds = new Set<string>();
|
|
223
305
|
const runIds = new Set<string>();
|
|
224
|
-
for (const item of
|
|
306
|
+
for (const item of items) {
|
|
225
307
|
if (typeof item === "string") {
|
|
226
308
|
callIds.add(item);
|
|
227
|
-
const
|
|
228
|
-
if (
|
|
309
|
+
const runId = cachedStringRunId(item);
|
|
310
|
+
if (runId) runIds.add(runId);
|
|
229
311
|
} else if (item && typeof item === "object") {
|
|
230
|
-
const
|
|
231
|
-
if (
|
|
232
|
-
const
|
|
233
|
-
const matches = text.matchAll(/\b([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\b/gi);
|
|
234
|
-
for (const m of matches) runIds.add(m[1]);
|
|
312
|
+
const snap = callSnap(item);
|
|
313
|
+
if (snap.callId) callIds.add(snap.callId);
|
|
314
|
+
for (const runId of snap.runIds) runIds.add(runId);
|
|
235
315
|
}
|
|
236
316
|
}
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
317
|
+
return { callIds, runIds };
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
type StatusHit = { status: Record<string, unknown>; index: number };
|
|
321
|
+
type StatusIndex = { byRun: Map<string, StatusHit[]>; byTool: Map<string, string[]>; children: Map<string, string[]> };
|
|
322
|
+
function indexStatuses(statuses: Record<string, unknown>[]): StatusIndex {
|
|
323
|
+
const byRun = new Map<string, StatusHit[]>();
|
|
324
|
+
const byTool = new Map<string, string[]>();
|
|
325
|
+
const children = new Map<string, string[]>();
|
|
326
|
+
for (let index = 0; index < statuses.length; index++) {
|
|
327
|
+
const status = statuses[index];
|
|
328
|
+
const selectKey = String(status.runId);
|
|
329
|
+
const bucket = byRun.get(selectKey);
|
|
330
|
+
if (bucket) bucket.push({ status, index });
|
|
331
|
+
else byRun.set(selectKey, [{ status, index }]);
|
|
332
|
+
const directKey = String(status.runId ?? "");
|
|
240
333
|
const toolCallId = String(status.toolCallId ?? "");
|
|
241
|
-
if (
|
|
242
|
-
|
|
334
|
+
if (toolCallId && directKey) {
|
|
335
|
+
const tools = byTool.get(toolCallId);
|
|
336
|
+
if (tools) tools.push(directKey);
|
|
337
|
+
else byTool.set(toolCallId, [directKey]);
|
|
338
|
+
}
|
|
339
|
+
const parentId = String(status.parentWorkflowRunId ?? "");
|
|
340
|
+
if (parentId) {
|
|
341
|
+
const kids = children.get(parentId);
|
|
342
|
+
if (kids) kids.push(selectKey);
|
|
343
|
+
else children.set(parentId, [selectKey]);
|
|
243
344
|
}
|
|
244
345
|
}
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
346
|
+
return { byRun, byTool, children };
|
|
347
|
+
}
|
|
348
|
+
function selectRunIds(index: StatusIndex, keys: CallKeys): Set<string> {
|
|
349
|
+
const selected = new Set<string>();
|
|
350
|
+
for (const id of keys.callIds) {
|
|
351
|
+
const runs = index.byTool.get(id);
|
|
352
|
+
if (runs) for (const runId of runs) selected.add(runId);
|
|
353
|
+
}
|
|
354
|
+
for (const runId of keys.runIds) if (index.byRun.has(runId)) selected.add(runId);
|
|
355
|
+
if (selected.size === 0) return selected;
|
|
356
|
+
const queue = [...selected];
|
|
357
|
+
for (let i = 0; i < queue.length; i++) {
|
|
358
|
+
const kids = index.children.get(queue[i]);
|
|
359
|
+
if (!kids) continue;
|
|
360
|
+
for (const runId of kids) if (!selected.has(runId)) {
|
|
361
|
+
selected.add(runId);
|
|
362
|
+
queue.push(runId);
|
|
250
363
|
}
|
|
251
364
|
}
|
|
252
|
-
return
|
|
365
|
+
return selected;
|
|
366
|
+
}
|
|
367
|
+
function takeStatuses(index: StatusIndex, selected: Set<string>, claimed?: Set<string>): Record<string, unknown>[] {
|
|
368
|
+
const hits: StatusHit[] = [];
|
|
369
|
+
for (const runId of selected) {
|
|
370
|
+
const bucket = index.byRun.get(runId);
|
|
371
|
+
if (!bucket) continue;
|
|
372
|
+
for (const hit of bucket) {
|
|
373
|
+
const key = String(hit.status.runId ?? "");
|
|
374
|
+
if (claimed && key && claimed.has(key)) continue;
|
|
375
|
+
hits.push(hit);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
if (hits.length > 1) hits.sort((a, b) => a.index - b.index);
|
|
379
|
+
if (claimed) for (const hit of hits) {
|
|
380
|
+
const key = String(hit.status.runId ?? "");
|
|
381
|
+
if (key) claimed.add(key);
|
|
382
|
+
}
|
|
383
|
+
return hits.map(hit => hit.status);
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
export function currentAgentStatuses(statuses: Record<string, unknown>[], callsOrIds: unknown[]): Record<string, unknown>[] {
|
|
387
|
+
if (statuses.length === 0 || callsOrIds.length === 0) return [];
|
|
388
|
+
const index = indexStatuses(statuses);
|
|
389
|
+
return takeStatuses(index, selectRunIds(index, collectCallKeys(callsOrIds)));
|
|
253
390
|
}
|
|
254
391
|
|
|
255
392
|
/** Associate each status only with the user turn that dispatched its tool call.
|
|
@@ -257,22 +394,21 @@ export function currentAgentStatuses(statuses: Record<string, unknown>[], callsO
|
|
|
257
394
|
*/
|
|
258
395
|
export function agentStatusesByTurn(statuses: Record<string, unknown>[], turns: Array<{ agentCalls?: unknown[] }>): Map<number, Record<string, unknown>[]> {
|
|
259
396
|
const assigned = new Map<number, Record<string, unknown>[]>();
|
|
397
|
+
if (statuses.length === 0) {
|
|
398
|
+
for (let index = 0; index < turns.length; index++) assigned.set(index, []);
|
|
399
|
+
return assigned;
|
|
400
|
+
}
|
|
401
|
+
const indexed = indexStatuses(statuses);
|
|
260
402
|
const claimed = new Set<string>();
|
|
261
|
-
for (
|
|
262
|
-
const
|
|
263
|
-
|
|
264
|
-
return !runId || !claimed.has(runId);
|
|
265
|
-
});
|
|
266
|
-
assigned.set(index, matched);
|
|
267
|
-
for (const status of matched) {
|
|
268
|
-
const runId = String(status.runId ?? "");
|
|
269
|
-
if (runId) claimed.add(runId);
|
|
270
|
-
}
|
|
403
|
+
for (let index = 0; index < turns.length; index++) {
|
|
404
|
+
const calls = turns[index]?.agentCalls ?? [];
|
|
405
|
+
assigned.set(index, calls.length === 0 ? [] : takeStatuses(indexed, selectRunIds(indexed, collectCallKeys(calls)), claimed));
|
|
271
406
|
}
|
|
272
407
|
return assigned;
|
|
273
408
|
}
|
|
274
409
|
|
|
275
410
|
export function agentChildren(statuses: Record<string, unknown>[]): Record<string, unknown>[] {
|
|
411
|
+
if (statuses.length === 0) return [];
|
|
276
412
|
const children = new Map<string, Record<string, unknown>>();
|
|
277
413
|
const visit = (value: unknown, key: string) => {
|
|
278
414
|
if (!value || typeof value !== "object") return;
|
package/lib/agent-widgets.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { diagramMarkdown, renderMinimalMarkdown } from "./minimal-markdown.ts";
|
|
2
2
|
import { plain } from "./agent-state.ts";
|
|
3
3
|
import { agentSummary, liveAgentRows, runningGlyph, type AgentDeadlines } from "./agent-render.ts";
|
|
4
|
+
import type { UiLanguage } from "./settings.ts";
|
|
4
5
|
import { getMarkdownTheme, type ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
5
6
|
import { truncateToWidth, visibleWidth, type Component } from "@earendil-works/pi-tui";
|
|
6
7
|
|
|
@@ -64,7 +65,7 @@ const isAgentFleetSummary = (lines: string[]) => {
|
|
|
64
65
|
};
|
|
65
66
|
|
|
66
67
|
/** Pi 0.85 widget containers: preserve unrelated widgets and their lifecycle. */
|
|
67
|
-
export function attachAgentWidgets(tui: unknown, theme: Theme, expanded: () => boolean, statuses?: () => Record<string, unknown>[], inline = false): () => void {
|
|
68
|
+
export function attachAgentWidgets(tui: unknown, theme: Theme, expanded: () => boolean, statuses?: () => Record<string, unknown>[], inline = false, language: () => UiLanguage = () => "zh"): () => void {
|
|
68
69
|
const host = tui as { children?: Array<Component & { children?: Component[] }> };
|
|
69
70
|
if (host.children?.length !== 7) return () => {};
|
|
70
71
|
const restores: Array<() => void> = [];
|
|
@@ -75,7 +76,8 @@ export function attachAgentWidgets(tui: unknown, theme: Theme, expanded: () => b
|
|
|
75
76
|
const descriptor = Object.getOwnPropertyDescriptor(container, "render");
|
|
76
77
|
const mouseDescriptor = Object.getOwnPropertyDescriptor(container, "handleMouse");
|
|
77
78
|
const render = (width: number) => {
|
|
78
|
-
const
|
|
79
|
+
const lang = language();
|
|
80
|
+
const live = !inline && statuses ? liveAgentRows(statuses(), theme, width, expanded(), deadlines, lang) : [];
|
|
79
81
|
let inserted = false;
|
|
80
82
|
const rows = container.children!.flatMap(child => {
|
|
81
83
|
const original = child.render(width);
|
|
@@ -96,7 +98,8 @@ export function attachAgentWidgets(tui: unknown, theme: Theme, expanded: () => b
|
|
|
96
98
|
const mouse: NonNullable<Component["handleMouse"]> = (event) => {
|
|
97
99
|
let y = 0;
|
|
98
100
|
let inserted = false;
|
|
99
|
-
const
|
|
101
|
+
const lang = language();
|
|
102
|
+
const live = !inline && statuses ? liveAgentRows(statuses(), theme, event.width, expanded(), deadlines, lang) : [];
|
|
100
103
|
|
|
101
104
|
for (const child of container.children!) {
|
|
102
105
|
const original = child.render(event.width);
|
package/lib/assistant-images.ts
CHANGED
|
@@ -73,7 +73,7 @@ export function imagePlaceholders(source: string, cwd: string): string {
|
|
|
73
73
|
const spans = assistantImageSpans(source, cwd, true);
|
|
74
74
|
for (let index = spans.length - 1; index >= 0; index--) {
|
|
75
75
|
const span = spans[index];
|
|
76
|
-
out = out.slice(0, span.start) + fileLink(`
|
|
76
|
+
out = out.slice(0, span.start) + fileLink(` #${index + 1}`, span.path, false) + out.slice(span.end);
|
|
77
77
|
}
|
|
78
78
|
if (placeholdersCache.size >= IMAGE_CACHE_MAX) {
|
|
79
79
|
const first = placeholdersCache.keys().next().value;
|