pi-sakura-cyberdeck 1.5.2 → 1.5.4
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/CHANGELOG.md +124 -0
- package/README.md +36 -18
- package/README.zh-CN.md +7 -5
- package/extensions/changelog/index.ts +186 -42
- package/extensions/claude-shimmer/index.ts +336 -140
- package/extensions/header/index.ts +84 -41
- package/extensions/matrix/index.ts +221 -69
- package/extensions/shared/color.ts +90 -36
- package/extensions/shared/config-file.ts +324 -0
- package/extensions/shared/tui-context.ts +78 -0
- package/extensions/zentui/config.ts +87 -128
- package/extensions/zentui/extension-status.ts +158 -19
- package/extensions/zentui/footer-format.ts +279 -85
- package/extensions/zentui/footer-segments.ts +630 -0
- package/extensions/zentui/footer.ts +668 -525
- package/extensions/zentui/format.ts +43 -19
- package/extensions/zentui/git.ts +362 -167
- package/extensions/zentui/gradient.ts +100 -50
- package/extensions/zentui/index.ts +172 -59
- package/extensions/zentui/live-context.ts +2 -1
- package/extensions/zentui/package-version.ts +102 -14
- package/extensions/zentui/project-refresh.ts +42 -20
- package/extensions/zentui/project-state.ts +2 -2
- package/extensions/zentui/runtime.ts +140 -51
- package/extensions/zentui/selector-border.ts +96 -16
- package/extensions/zentui/session-lifecycle.ts +3 -1
- package/extensions/zentui/settings-command.ts +404 -246
- package/extensions/zentui/shell-call.ts +140 -26
- package/extensions/zentui/style.ts +109 -40
- package/extensions/zentui/thinking-trail.ts +234 -9
- package/extensions/zentui/tool-execution.ts +144 -64
- package/extensions/zentui/ui.ts +698 -145
- package/extensions/zentui/user-message.ts +24 -12
- package/package.json +2 -1
|
@@ -13,7 +13,10 @@
|
|
|
13
13
|
*/
|
|
14
14
|
|
|
15
15
|
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
16
|
-
import {
|
|
16
|
+
import { getColorMode, paintFg, rgbTo256, syncColorMode, type RGB } from "../shared/color";
|
|
17
|
+
import { isInteractiveTui, mixRgb } from "../shared/tui-context";
|
|
18
|
+
|
|
19
|
+
export { isInteractiveTui, mixRgb as blend } from "../shared/tui-context";
|
|
17
20
|
|
|
18
21
|
type SpinnerMode = "requesting" | "thinking" | "responding" | "tool-input" | "tool-use";
|
|
19
22
|
export type RunOutcome = "completed" | "aborted" | "error";
|
|
@@ -69,6 +72,29 @@ const THINKING_GLOW_DELAY_MS = 1_800;
|
|
|
69
72
|
const THINKING_GLOW_PERIOD_MS = 1_600;
|
|
70
73
|
const TOOL_PULSE_PERIOD_MS = 1_400;
|
|
71
74
|
|
|
75
|
+
const NATIVE_DATE = globalThis.Date;
|
|
76
|
+
const NATIVE_DATE_NOW = Date.now;
|
|
77
|
+
const HAS_NATIVE_DATE_NOW =
|
|
78
|
+
typeof NATIVE_DATE_NOW === "function" &&
|
|
79
|
+
Function.prototype.toString.call(NATIVE_DATE_NOW).includes("[native code]");
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Monotonic elapsed clock (immune to NTP/system-clock jumps in production).
|
|
83
|
+
* Falls back to `Date.now()` only when a test harness replaces global `Date`
|
|
84
|
+
* (e.g., `node:test` `mock.timers.enable({ apis: ["Date"] })`).
|
|
85
|
+
*/
|
|
86
|
+
function nowMs(): number {
|
|
87
|
+
if (
|
|
88
|
+
typeof performance?.now === "function" &&
|
|
89
|
+
HAS_NATIVE_DATE_NOW &&
|
|
90
|
+
globalThis.Date === NATIVE_DATE &&
|
|
91
|
+
Date.now === NATIVE_DATE_NOW
|
|
92
|
+
) {
|
|
93
|
+
return performance.now();
|
|
94
|
+
}
|
|
95
|
+
return Date.now();
|
|
96
|
+
}
|
|
97
|
+
|
|
72
98
|
// ─── Pure helpers (exported for tests) ────────────────────────────
|
|
73
99
|
|
|
74
100
|
export function pickVerb(random: () => number = Math.random): string {
|
|
@@ -76,14 +102,14 @@ export function pickVerb(random: () => number = Math.random): string {
|
|
|
76
102
|
}
|
|
77
103
|
|
|
78
104
|
export function formatDuration(ms: number): string {
|
|
79
|
-
const s = Math.max(0, Math.floor(ms / 1000));
|
|
105
|
+
const s = Number.isFinite(ms) ? Math.max(0, Math.floor(ms / 1000)) : 0;
|
|
80
106
|
const m = Math.floor(s / 60);
|
|
81
107
|
return m > 0 ? `${m}m ${s % 60}s` : `${s}s`;
|
|
82
108
|
}
|
|
83
109
|
|
|
84
110
|
/** Fixed-width clock: mm:ss, or hh:mm:ss past one hour. */
|
|
85
111
|
export function formatDigital(ms: number): string {
|
|
86
|
-
const total = Math.max(0, Math.floor(ms / 1000));
|
|
112
|
+
const total = Number.isFinite(ms) ? Math.max(0, Math.floor(ms / 1000)) : 0;
|
|
87
113
|
const h = Math.floor(total / 3600);
|
|
88
114
|
const mm = String(Math.floor((total % 3600) / 60)).padStart(2, "0");
|
|
89
115
|
const ss = String(total % 60).padStart(2, "0");
|
|
@@ -94,7 +120,7 @@ const SMALL_NUMBER = new Intl.NumberFormat("en-US");
|
|
|
94
120
|
const COMPACT_NUMBER = new Intl.NumberFormat("en-US", { notation: "compact", maximumFractionDigits: 1 });
|
|
95
121
|
|
|
96
122
|
export function formatTokenCount(n: number): string {
|
|
97
|
-
const value = Math.max(0, Math.round(n));
|
|
123
|
+
const value = Number.isFinite(n) ? Math.max(0, Math.round(n)) : 0;
|
|
98
124
|
const number = value < 1_000 ? SMALL_NUMBER.format(value) : COMPACT_NUMBER.format(value).replace("K", "k");
|
|
99
125
|
return `${number} ${value === 1 ? "token" : "tokens"}`;
|
|
100
126
|
}
|
|
@@ -123,6 +149,10 @@ export type AssistantTokenMessage = {
|
|
|
123
149
|
stopReason?: string;
|
|
124
150
|
};
|
|
125
151
|
|
|
152
|
+
function isPositiveFinite(value: unknown): boolean {
|
|
153
|
+
return typeof value === "number" && Number.isFinite(value) && value > 0;
|
|
154
|
+
}
|
|
155
|
+
|
|
126
156
|
/** Provider-reported output tokens, or null when the provider has not reported any. */
|
|
127
157
|
export function reportedOutputTokens(message: AssistantTokenMessage | undefined, final = false): number | null {
|
|
128
158
|
const usage = message?.usage;
|
|
@@ -131,8 +161,12 @@ export function reportedOutputTokens(message: AssistantTokenMessage | undefined,
|
|
|
131
161
|
if (output > 0) return Math.round(output);
|
|
132
162
|
// Streaming messages start with zero-filled usage. At message_end, non-zero
|
|
133
163
|
// input/cache/total proves the provider reported usage, so output=0 is real.
|
|
134
|
-
const hasFinalUsage =
|
|
135
|
-
|
|
164
|
+
const hasFinalUsage =
|
|
165
|
+
final &&
|
|
166
|
+
(isPositiveFinite(usage?.input) ||
|
|
167
|
+
isPositiveFinite(usage?.cacheRead) ||
|
|
168
|
+
isPositiveFinite(usage?.cacheWrite) ||
|
|
169
|
+
isPositiveFinite(usage?.totalTokens));
|
|
136
170
|
return hasFinalUsage ? 0 : null;
|
|
137
171
|
}
|
|
138
172
|
|
|
@@ -142,11 +176,27 @@ const EMOJI_CHAR = /\p{Extended_Pictographic}/u;
|
|
|
142
176
|
const TEXT_SYMBOLS = new Set(["©", "®", "™", "‼", "⁉", "ℹ", "↔", "↕", "↖", "↗", "↘", "↙"]);
|
|
143
177
|
|
|
144
178
|
/** Quarter-token units make estimates additive across streamed deltas. */
|
|
145
|
-
|
|
179
|
+
function estimateTextTokenUnits(text: string): number {
|
|
146
180
|
let units = 0;
|
|
147
|
-
for (
|
|
148
|
-
|
|
149
|
-
|
|
181
|
+
for (let i = 0; i < text.length; i++) {
|
|
182
|
+
const code = text.charCodeAt(i);
|
|
183
|
+
if (code <= 0x7f) {
|
|
184
|
+
units += 1; // ≈ 4 ASCII chars/token
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
let char: string;
|
|
188
|
+
if (code >= 0xd800 && code <= 0xdbff && i + 1 < text.length) {
|
|
189
|
+
const next = text.charCodeAt(i + 1);
|
|
190
|
+
if (next >= 0xdc00 && next <= 0xdfff) {
|
|
191
|
+
char = text.slice(i, i + 2);
|
|
192
|
+
i += 1;
|
|
193
|
+
} else {
|
|
194
|
+
char = text[i]!;
|
|
195
|
+
}
|
|
196
|
+
} else {
|
|
197
|
+
char = text[i]!;
|
|
198
|
+
}
|
|
199
|
+
if (CJK_CHAR.test(char)) units += 4; // ≈ 1 token
|
|
150
200
|
else if (EMOJI_CHAR.test(char) && !TEXT_SYMBOLS.has(char)) units += 8; // ≈ 2 tokens
|
|
151
201
|
else units += 2;
|
|
152
202
|
}
|
|
@@ -167,7 +217,8 @@ function toolCallText(name: string | undefined, args: unknown): string {
|
|
|
167
217
|
}
|
|
168
218
|
}
|
|
169
219
|
|
|
170
|
-
function estimateBlockTokenUnits(block: ContentBlock): number {
|
|
220
|
+
function estimateBlockTokenUnits(block: ContentBlock | null | undefined): number {
|
|
221
|
+
if (!block || typeof block !== "object") return 0;
|
|
171
222
|
if (block.type === "text" && typeof block.text === "string") return estimateTextTokenUnits(block.text);
|
|
172
223
|
if (block.type === "thinking" && typeof block.thinking === "string") return estimateTextTokenUnits(block.thinking);
|
|
173
224
|
if (block.type === "toolCall") return estimateTextTokenUnits(toolCallText(block.name, block.arguments));
|
|
@@ -175,7 +226,12 @@ function estimateBlockTokenUnits(block: ContentBlock): number {
|
|
|
175
226
|
}
|
|
176
227
|
|
|
177
228
|
export function estimateOutputTokens(message: AssistantTokenMessage | undefined): number {
|
|
178
|
-
const
|
|
229
|
+
const content = message?.content;
|
|
230
|
+
if (!Array.isArray(content)) return 0;
|
|
231
|
+
let units = 0;
|
|
232
|
+
for (let i = 0; i < content.length; i++) {
|
|
233
|
+
units += estimateBlockTokenUnits(content[i]);
|
|
234
|
+
}
|
|
179
235
|
return Math.ceil(units / 4);
|
|
180
236
|
}
|
|
181
237
|
|
|
@@ -202,9 +258,9 @@ export function liveTokenReading(reported: number | null, estimate: number): Tok
|
|
|
202
258
|
*/
|
|
203
259
|
export function finalTokenReading(message: AssistantTokenMessage | undefined): TokenReading {
|
|
204
260
|
const reported = reportedOutputTokens(message, true);
|
|
205
|
-
const estimate = estimateOutputTokens(message);
|
|
206
261
|
const interrupted = message?.stopReason === "aborted" || message?.stopReason === "error";
|
|
207
262
|
if (!interrupted && reported !== null) return { tokens: reported, estimated: false };
|
|
263
|
+
const estimate = estimateOutputTokens(message);
|
|
208
264
|
return liveTokenReading(reported, estimate);
|
|
209
265
|
}
|
|
210
266
|
|
|
@@ -237,21 +293,6 @@ export function tweenTokens(displayed: number, target: number): number {
|
|
|
237
293
|
return displayed + Math.sign(gap) * Math.min(distance, step);
|
|
238
294
|
}
|
|
239
295
|
|
|
240
|
-
export function blend(a: RGB, b: RGB, t: number): RGB {
|
|
241
|
-
const k = Math.max(0, Math.min(1, t));
|
|
242
|
-
return [
|
|
243
|
-
Math.round(a[0] + (b[0] - a[0]) * k),
|
|
244
|
-
Math.round(a[1] + (b[1] - a[1]) * k),
|
|
245
|
-
Math.round(a[2] + (b[2] - a[2]) * k),
|
|
246
|
-
];
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
function sampleStops(pos: number): RGB {
|
|
250
|
-
const scaled = Math.max(0, Math.min(1, pos)) * (SWEEP_STOPS.length - 1);
|
|
251
|
-
const i = Math.min(SWEEP_STOPS.length - 2, Math.floor(scaled));
|
|
252
|
-
return blend(SWEEP_STOPS[i]!, SWEEP_STOPS[i + 1]!, scaled - i);
|
|
253
|
-
}
|
|
254
|
-
|
|
255
296
|
/**
|
|
256
297
|
* Highlight center for a sweep frame. It starts SHIMMER_BAND cells outside the
|
|
257
298
|
* text and leaves SHIMMER_BAND cells past the other end, so it enters and exits smoothly.
|
|
@@ -262,27 +303,74 @@ export function sweepPosition(length: number, frame: number, reverse: boolean):
|
|
|
262
303
|
return reverse ? length - 1 + SHIMMER_BAND - step : step - SHIMMER_BAND;
|
|
263
304
|
}
|
|
264
305
|
|
|
306
|
+
const SWEEP_RGB: [number, number, number] = [0, 0, 0];
|
|
307
|
+
|
|
308
|
+
function clampByte(value: number): number {
|
|
309
|
+
return Number.isFinite(value) ? Math.max(0, Math.min(255, Math.round(value))) : 0;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
function lerpByte(from: number, to: number, amount: number): number {
|
|
313
|
+
return amount === 0 ? from : clampByte(from + (to - from) * amount);
|
|
314
|
+
}
|
|
315
|
+
|
|
265
316
|
/**
|
|
266
317
|
* Macaron sweep with a soft white bloom. `tint` pulls every char toward `tintColor`
|
|
267
318
|
* (stall → coral, tool → mint) while keeping the sweep alive.
|
|
268
319
|
*/
|
|
269
320
|
export function colorSweep(text: string, frame: number, reverse: boolean, tint = 0, tintColor: RGB = CORAL): string {
|
|
321
|
+
const colorMode = getColorMode();
|
|
322
|
+
if (colorMode === "none" || text.length === 0) return text;
|
|
270
323
|
const chars = [...text];
|
|
271
|
-
const
|
|
272
|
-
const
|
|
324
|
+
const len = chars.length;
|
|
325
|
+
const pos = sweepPosition(len, frame, reverse);
|
|
326
|
+
const safeTint = Number.isFinite(tint) ? Math.max(0, Math.min(1, tint)) : 0;
|
|
327
|
+
const hlAmount = safeTint * 0.6;
|
|
328
|
+
const tr = tintColor[0];
|
|
329
|
+
const tg = tintColor[1];
|
|
330
|
+
const tb = tintColor[2];
|
|
331
|
+
const trClamped = clampByte(tr);
|
|
332
|
+
const tgClamped = clampByte(tg);
|
|
333
|
+
const tbClamped = clampByte(tb);
|
|
334
|
+
const hlR = lerpByte(HIGHLIGHT[0], tr, hlAmount);
|
|
335
|
+
const hlG = lerpByte(HIGHLIGHT[1], tg, hlAmount);
|
|
336
|
+
const hlB = lerpByte(HIGHLIGHT[2], tb, hlAmount);
|
|
337
|
+
const stopSpan = SWEEP_STOPS.length - 1;
|
|
338
|
+
const isTruecolor = colorMode === "truecolor";
|
|
339
|
+
let last256 = -1;
|
|
273
340
|
let out = "";
|
|
274
|
-
for (let i = 0; i <
|
|
275
|
-
const
|
|
276
|
-
const
|
|
277
|
-
|
|
341
|
+
for (let i = 0; i < len; i++) {
|
|
342
|
+
const scaled = len <= 1 ? 0 : (i / (len - 1)) * stopSpan;
|
|
343
|
+
const idx = Math.min(stopSpan - 1, Math.floor(scaled));
|
|
344
|
+
const frac = scaled - idx;
|
|
345
|
+
const s0 = SWEEP_STOPS[idx]!;
|
|
346
|
+
const s1 = SWEEP_STOPS[idx + 1]!;
|
|
347
|
+
const stopR = lerpByte(s0[0], s1[0], frac);
|
|
348
|
+
const stopG = lerpByte(s0[1], s1[1], frac);
|
|
349
|
+
const stopB = lerpByte(s0[2], s1[2], frac);
|
|
350
|
+
const baseR = safeTint === 1 ? trClamped : lerpByte(stopR, tr, safeTint);
|
|
351
|
+
const baseG = safeTint === 1 ? tgClamped : lerpByte(stopG, tg, safeTint);
|
|
352
|
+
const baseB = safeTint === 1 ? tbClamped : lerpByte(stopB, tb, safeTint);
|
|
353
|
+
const dist = Math.abs(i - pos);
|
|
354
|
+
const t = dist < SHIMMER_BAND ? 1 - dist / SHIMMER_BAND : 0;
|
|
355
|
+
const bloom = t * t * 0.92;
|
|
356
|
+
const r = lerpByte(baseR, hlR, bloom);
|
|
357
|
+
const g = lerpByte(baseG, hlG, bloom);
|
|
358
|
+
const b = lerpByte(baseB, hlB, bloom);
|
|
359
|
+
if (isTruecolor) {
|
|
360
|
+
out += `\x1b[38;2;${r};${g};${b}m${chars[i]}`;
|
|
361
|
+
} else {
|
|
362
|
+
SWEEP_RGB[0] = r;
|
|
363
|
+
SWEEP_RGB[1] = g;
|
|
364
|
+
SWEEP_RGB[2] = b;
|
|
365
|
+
const code = rgbTo256(SWEEP_RGB);
|
|
366
|
+
if (code !== last256) {
|
|
367
|
+
out += `\x1b[38;5;${code}m`;
|
|
368
|
+
last256 = code;
|
|
369
|
+
}
|
|
370
|
+
out += chars[i];
|
|
371
|
+
}
|
|
278
372
|
}
|
|
279
|
-
return
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
/** Animations and notices only belong in the interactive terminal UI. */
|
|
283
|
-
export function isInteractiveTui(ctx: Pick<ExtensionContext, "mode" | "hasUI"> | undefined): boolean {
|
|
284
|
-
if (!ctx) return false;
|
|
285
|
-
return typeof ctx.mode === "string" ? ctx.mode === "tui" : ctx.hasUI === true;
|
|
373
|
+
return `${out}\x1b[39m`;
|
|
286
374
|
}
|
|
287
375
|
|
|
288
376
|
const EFFORT_TAGS: Record<string, { tag: string; color: RGB }> = {
|
|
@@ -294,6 +382,14 @@ const EFFORT_TAGS: Record<string, { tag: string; color: RGB }> = {
|
|
|
294
382
|
max: { tag: "MAX", color: CORAL },
|
|
295
383
|
};
|
|
296
384
|
|
|
385
|
+
type AssistantUpdateEventLike = {
|
|
386
|
+
type?: string;
|
|
387
|
+
contentIndex?: number;
|
|
388
|
+
delta?: unknown;
|
|
389
|
+
content?: unknown;
|
|
390
|
+
toolCall?: { name?: string; arguments?: unknown } | null;
|
|
391
|
+
};
|
|
392
|
+
|
|
297
393
|
// ─── Extension ────────────────────────────────────────────────────
|
|
298
394
|
|
|
299
395
|
export default function claudeShimmer(pi: ExtensionAPI) {
|
|
@@ -304,21 +400,26 @@ export default function claudeShimmer(pi: ExtensionAPI) {
|
|
|
304
400
|
let runStart = 0;
|
|
305
401
|
let verb = "";
|
|
306
402
|
let runOutcome: RunOutcome = "completed";
|
|
403
|
+
let completedAssistantMessage = false;
|
|
307
404
|
let completedTokens = 0;
|
|
308
405
|
let completedEstimated = false;
|
|
309
406
|
|
|
310
407
|
// Current assistant message.
|
|
311
408
|
let mode: SpinnerMode = "requesting";
|
|
312
409
|
let thinkingStart = 0;
|
|
313
|
-
let lastStreamAt =
|
|
410
|
+
let lastStreamAt: number | null = null;
|
|
314
411
|
let activeToolCount = 0;
|
|
315
412
|
let toolStart = 0;
|
|
316
413
|
let reportedTokens: number | null = null;
|
|
317
414
|
const blockUnits = new Map<number, number>();
|
|
318
415
|
let estimateUnits = 0;
|
|
319
416
|
|
|
320
|
-
// Animation.
|
|
417
|
+
// Animation & dialog pause state.
|
|
321
418
|
let timer: ReturnType<typeof setInterval> | null = null;
|
|
419
|
+
let uiTouched = false;
|
|
420
|
+
let promptActive = false;
|
|
421
|
+
let promptPausedAt = 0;
|
|
422
|
+
let clockPausedByPrompt = false;
|
|
322
423
|
let tick = 0;
|
|
323
424
|
let displayedTokens = 0;
|
|
324
425
|
|
|
@@ -329,7 +430,7 @@ export default function claudeShimmer(pi: ExtensionAPI) {
|
|
|
329
430
|
return { tokens: completedTokens + live.tokens, estimated: completedEstimated || live.estimated };
|
|
330
431
|
}
|
|
331
432
|
|
|
332
|
-
function effortTag(): string | undefined {
|
|
433
|
+
function effortTag(now: number): string | undefined {
|
|
333
434
|
let level = "";
|
|
334
435
|
try {
|
|
335
436
|
level = String(pi.getThinkingLevel() || "").toLowerCase();
|
|
@@ -338,39 +439,41 @@ export default function claudeShimmer(pi: ExtensionAPI) {
|
|
|
338
439
|
const tag = info?.tag ?? (mode === "thinking" ? "THINK" : "");
|
|
339
440
|
if (!tag) return undefined;
|
|
340
441
|
const base = info?.color ?? LAVENDER;
|
|
341
|
-
const thinkElapsed =
|
|
442
|
+
const thinkElapsed = Math.max(0, now - thinkingStart);
|
|
342
443
|
if (mode === "thinking" && thinkElapsed > THINKING_GLOW_DELAY_MS) {
|
|
343
444
|
const phase = ((thinkElapsed - THINKING_GLOW_DELAY_MS) / THINKING_GLOW_PERIOD_MS) * Math.PI * 2;
|
|
344
|
-
return paintFg(
|
|
445
|
+
return paintFg(mixRgb(base, HIGHLIGHT, (Math.sin(phase) + 1) / 2), tag);
|
|
345
446
|
}
|
|
346
447
|
return paintFg(base, tag);
|
|
347
448
|
}
|
|
348
449
|
|
|
349
450
|
function stallAmount(now: number): number {
|
|
350
|
-
if (mode === "tool-use" || mode === "tool-input" || activeToolCount > 0 || lastStreamAt ===
|
|
351
|
-
|
|
451
|
+
if (promptActive || mode === "tool-use" || mode === "tool-input" || activeToolCount > 0 || lastStreamAt === null) {
|
|
452
|
+
return 0;
|
|
453
|
+
}
|
|
454
|
+
return Math.max(0, Math.min(1, (Math.max(0, now - lastStreamAt) - STALL_TIMEOUT_MS) / STALL_FADE_MS));
|
|
352
455
|
}
|
|
353
456
|
|
|
354
457
|
function buildMessage(): string {
|
|
355
|
-
const now =
|
|
458
|
+
const now = nowMs();
|
|
356
459
|
const glyph = paintFg(SAKURA, SPINNER_FRAMES[tick % SPINNER_FRAMES.length]!);
|
|
357
460
|
const text = `${verb}${animatedDots(tick)}`;
|
|
358
461
|
const reverse = mode !== "requesting";
|
|
359
462
|
let verbText: string;
|
|
360
463
|
if (mode === "tool-use") {
|
|
361
|
-
const pulse = (Math.sin(((now - toolStart) / TOOL_PULSE_PERIOD_MS) * Math.PI * 2) + 1) / 2;
|
|
464
|
+
const pulse = (Math.sin((Math.max(0, now - toolStart) / TOOL_PULSE_PERIOD_MS) * Math.PI * 2) + 1) / 2;
|
|
362
465
|
verbText = colorSweep(text, tick, reverse, 0.25 + pulse * 0.5, MINT);
|
|
363
466
|
} else {
|
|
364
467
|
verbText = colorSweep(text, tick, reverse, stallAmount(now), CORAL);
|
|
365
468
|
}
|
|
366
469
|
|
|
367
470
|
const parts: string[] = [];
|
|
368
|
-
const effort = effortTag();
|
|
471
|
+
const effort = effortTag(now);
|
|
369
472
|
if (effort) parts.push(effort);
|
|
370
473
|
const target = tokenTarget();
|
|
371
474
|
const arrow = mode === "requesting" ? "↑" : "↓";
|
|
372
475
|
parts.push(paintFg(SKY, `${arrow} ${target.estimated ? "~" : ""}${formatTokenCount(displayedTokens)}`));
|
|
373
|
-
parts.push(paintFg(MUTED, formatDigital(now - runStart)));
|
|
476
|
+
parts.push(paintFg(MUTED, formatDigital(Math.max(0, now - runStart))));
|
|
374
477
|
const hud = `${paintFg(MUTED, "( ")}${parts.join(paintFg(MUTED, " · "))}${paintFg(MUTED, " )")}`;
|
|
375
478
|
return `${glyph} ${verbText} ${hud}`;
|
|
376
479
|
}
|
|
@@ -380,6 +483,7 @@ export default function claudeShimmer(pi: ExtensionAPI) {
|
|
|
380
483
|
if (!ctx || !timer) return;
|
|
381
484
|
try {
|
|
382
485
|
ctx.ui.setWorkingMessage(buildMessage());
|
|
486
|
+
uiTouched = true;
|
|
383
487
|
} catch {
|
|
384
488
|
// UI disposed mid-tick (session replaced/shutdown): stop quietly.
|
|
385
489
|
stopClock();
|
|
@@ -387,10 +491,15 @@ export default function claudeShimmer(pi: ExtensionAPI) {
|
|
|
387
491
|
}
|
|
388
492
|
|
|
389
493
|
function startClock() {
|
|
390
|
-
if (
|
|
494
|
+
if (promptActive) {
|
|
495
|
+
clockPausedByPrompt = true;
|
|
496
|
+
return;
|
|
497
|
+
}
|
|
498
|
+
if (timer || !isInteractiveTui(ctx_)) return;
|
|
391
499
|
try {
|
|
392
500
|
// Glyph lives inside the message so our clock is the only one; hide Pi's.
|
|
393
501
|
ctx_!.ui.setWorkingIndicator({ frames: [], intervalMs: TICK_MS });
|
|
502
|
+
uiTouched = true;
|
|
394
503
|
} catch {
|
|
395
504
|
return;
|
|
396
505
|
}
|
|
@@ -409,6 +518,7 @@ export default function claudeShimmer(pi: ExtensionAPI) {
|
|
|
409
518
|
}
|
|
410
519
|
|
|
411
520
|
function restoreUi() {
|
|
521
|
+
uiTouched = false;
|
|
412
522
|
const ctx = ctx_;
|
|
413
523
|
if (!ctx || !isInteractiveTui(ctx)) return;
|
|
414
524
|
try {
|
|
@@ -422,7 +532,20 @@ export default function claudeShimmer(pi: ExtensionAPI) {
|
|
|
422
532
|
reportedTokens = null;
|
|
423
533
|
blockUnits.clear();
|
|
424
534
|
estimateUnits = 0;
|
|
425
|
-
lastStreamAt =
|
|
535
|
+
lastStreamAt = null;
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
function resetSessionState() {
|
|
539
|
+
stopClock();
|
|
540
|
+
if (runActive || uiTouched) restoreUi();
|
|
541
|
+
runActive = false;
|
|
542
|
+
uiTouched = false;
|
|
543
|
+
promptActive = false;
|
|
544
|
+
promptPausedAt = 0;
|
|
545
|
+
clockPausedByPrompt = false;
|
|
546
|
+
activeToolCount = 0;
|
|
547
|
+
completedAssistantMessage = false;
|
|
548
|
+
resetMessage();
|
|
426
549
|
}
|
|
427
550
|
|
|
428
551
|
function setBlockUnits(index: number, units: number) {
|
|
@@ -437,14 +560,89 @@ export default function claudeShimmer(pi: ExtensionAPI) {
|
|
|
437
560
|
updateDisplay();
|
|
438
561
|
}
|
|
439
562
|
|
|
563
|
+
function applyMessageEstimate(
|
|
564
|
+
evt: AssistantUpdateEventLike,
|
|
565
|
+
message: AssistantTokenMessage | undefined,
|
|
566
|
+
hasIndex: boolean,
|
|
567
|
+
): void {
|
|
568
|
+
switch (evt.type) {
|
|
569
|
+
case "start":
|
|
570
|
+
blockUnits.clear();
|
|
571
|
+
estimateUnits = 0;
|
|
572
|
+
break;
|
|
573
|
+
case "text_start":
|
|
574
|
+
case "thinking_start":
|
|
575
|
+
if (hasIndex) setBlockUnits(evt.contentIndex!, 0);
|
|
576
|
+
break;
|
|
577
|
+
case "text_delta":
|
|
578
|
+
case "thinking_delta":
|
|
579
|
+
case "toolcall_delta":
|
|
580
|
+
if (hasIndex && typeof evt.delta === "string" && evt.delta.length > 0) {
|
|
581
|
+
setBlockUnits(evt.contentIndex!, (blockUnits.get(evt.contentIndex!) ?? 0) + estimateTextTokenUnits(evt.delta));
|
|
582
|
+
}
|
|
583
|
+
if (typeof evt.delta === "string") lastStreamAt = nowMs();
|
|
584
|
+
break;
|
|
585
|
+
case "text_end":
|
|
586
|
+
case "thinking_end":
|
|
587
|
+
// Deltas accumulate exact additive quarter-token units; only scan evt.content when no non-empty deltas ran.
|
|
588
|
+
if (
|
|
589
|
+
hasIndex &&
|
|
590
|
+
(blockUnits.get(evt.contentIndex!) ?? 0) === 0 &&
|
|
591
|
+
typeof evt.content === "string" &&
|
|
592
|
+
evt.content.length > 0
|
|
593
|
+
) {
|
|
594
|
+
setBlockUnits(evt.contentIndex!, estimateTextTokenUnits(evt.content));
|
|
595
|
+
}
|
|
596
|
+
break;
|
|
597
|
+
case "toolcall_start":
|
|
598
|
+
if (hasIndex) {
|
|
599
|
+
const block = Array.isArray(message?.content) ? message.content[evt.contentIndex!] : undefined;
|
|
600
|
+
setBlockUnits(evt.contentIndex!, block ? estimateBlockTokenUnits(block) : 0);
|
|
601
|
+
}
|
|
602
|
+
break;
|
|
603
|
+
case "toolcall_end":
|
|
604
|
+
if (hasIndex && evt.toolCall && typeof evt.toolCall === "object") {
|
|
605
|
+
setBlockUnits(evt.contentIndex!, estimateTextTokenUnits(toolCallText(evt.toolCall.name, evt.toolCall.arguments)));
|
|
606
|
+
}
|
|
607
|
+
break;
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
function applySpinnerMode(eventType: string): void {
|
|
612
|
+
switch (eventType) {
|
|
613
|
+
case "thinking_start":
|
|
614
|
+
thinkingStart = nowMs();
|
|
615
|
+
setMode("thinking");
|
|
616
|
+
break;
|
|
617
|
+
case "thinking_delta":
|
|
618
|
+
if (mode !== "thinking") {
|
|
619
|
+
thinkingStart = nowMs();
|
|
620
|
+
setMode("thinking");
|
|
621
|
+
}
|
|
622
|
+
break;
|
|
623
|
+
case "text_start":
|
|
624
|
+
case "text_delta":
|
|
625
|
+
lastStreamAt = nowMs();
|
|
626
|
+
setMode("responding");
|
|
627
|
+
break;
|
|
628
|
+
case "toolcall_start":
|
|
629
|
+
setMode("tool-input");
|
|
630
|
+
break;
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
|
|
440
634
|
function finishRun() {
|
|
441
635
|
stopClock();
|
|
636
|
+
clockPausedByPrompt = false;
|
|
442
637
|
if (!runActive) return;
|
|
443
|
-
const elapsed =
|
|
638
|
+
const elapsed = Math.max(0, nowMs() - runStart);
|
|
444
639
|
const outcome = runOutcome;
|
|
640
|
+
const hadCompletion = completedAssistantMessage;
|
|
445
641
|
runActive = false;
|
|
642
|
+
completedAssistantMessage = false;
|
|
643
|
+
activeToolCount = 0;
|
|
446
644
|
restoreUi();
|
|
447
|
-
const notice = completionNotice(outcome, elapsed);
|
|
645
|
+
const notice = hadCompletion ? completionNotice(outcome, elapsed) : undefined;
|
|
448
646
|
if (notice && ctx_ && isInteractiveTui(ctx_)) {
|
|
449
647
|
try {
|
|
450
648
|
ctx_.ui.notify(`${paintFg(SAKURA, notice.slice(0, 1))}${paintFg(MUTED, notice.slice(1))}`, "info");
|
|
@@ -454,112 +652,76 @@ export default function claudeShimmer(pi: ExtensionAPI) {
|
|
|
454
652
|
|
|
455
653
|
// ── Events ──────────────────────────────────────────────────
|
|
456
654
|
|
|
457
|
-
pi.on("session_start",
|
|
458
|
-
|
|
459
|
-
|
|
655
|
+
pi.on("session_start", (_event, ctx) => {
|
|
656
|
+
resetSessionState();
|
|
657
|
+
ctx_ = ctx ?? null;
|
|
658
|
+
if (isInteractiveTui(ctx)) syncColorMode(ctx.ui?.theme);
|
|
460
659
|
});
|
|
461
660
|
|
|
462
|
-
pi.on("agent_start",
|
|
463
|
-
ctx_ = ctx;
|
|
661
|
+
pi.on("agent_start", (_event, ctx) => {
|
|
662
|
+
ctx_ = ctx ?? null;
|
|
464
663
|
if (!isInteractiveTui(ctx)) return;
|
|
465
|
-
syncColorMode(ctx.ui
|
|
664
|
+
syncColorMode(ctx.ui?.theme);
|
|
466
665
|
if (!runActive) {
|
|
467
666
|
// New run: everything below stays stable across tool rounds and retries.
|
|
468
667
|
runActive = true;
|
|
469
|
-
runStart =
|
|
668
|
+
runStart = nowMs();
|
|
470
669
|
verb = pickVerb();
|
|
471
670
|
runOutcome = "completed";
|
|
671
|
+
completedAssistantMessage = false;
|
|
472
672
|
completedTokens = 0;
|
|
473
673
|
completedEstimated = false;
|
|
474
674
|
displayedTokens = 0;
|
|
475
675
|
tick = 0;
|
|
676
|
+
} else {
|
|
677
|
+
// Retry after error/continuation: require a completed assistant message in this attempt.
|
|
678
|
+
completedAssistantMessage = false;
|
|
476
679
|
}
|
|
477
680
|
resetMessage();
|
|
478
681
|
activeToolCount = 0;
|
|
479
682
|
startClock();
|
|
480
683
|
});
|
|
481
684
|
|
|
482
|
-
pi.on("turn_start",
|
|
483
|
-
ctx_ = ctx;
|
|
685
|
+
pi.on("turn_start", (_event, ctx) => {
|
|
686
|
+
ctx_ = ctx ?? null;
|
|
484
687
|
if (!runActive) return;
|
|
688
|
+
activeToolCount = 0;
|
|
485
689
|
resetMessage();
|
|
486
690
|
startClock();
|
|
487
691
|
});
|
|
488
692
|
|
|
489
|
-
pi.on("message_start",
|
|
490
|
-
if (!runActive || event
|
|
491
|
-
ctx_ = ctx;
|
|
693
|
+
pi.on("message_start", (event, ctx) => {
|
|
694
|
+
if (!runActive || event?.message?.role !== "assistant") return;
|
|
695
|
+
ctx_ = ctx ?? null;
|
|
492
696
|
resetMessage();
|
|
493
697
|
});
|
|
494
698
|
|
|
495
|
-
pi.on("message_update",
|
|
699
|
+
pi.on("message_update", (event, ctx) => {
|
|
496
700
|
if (!runActive) return;
|
|
497
|
-
ctx_ = ctx;
|
|
498
|
-
const
|
|
499
|
-
const message = event.message as AssistantTokenMessage;
|
|
701
|
+
ctx_ = ctx ?? null;
|
|
702
|
+
const message = event?.message as AssistantTokenMessage | undefined;
|
|
500
703
|
reportedTokens = reportedOutputTokens(message) ?? reportedTokens;
|
|
704
|
+
const evt = event?.assistantMessageEvent as AssistantUpdateEventLike | null | undefined;
|
|
705
|
+
if (!evt || typeof evt.type !== "string") return;
|
|
706
|
+
const hasIndex =
|
|
707
|
+
typeof evt.contentIndex === "number" && Number.isInteger(evt.contentIndex) && evt.contentIndex >= 0;
|
|
501
708
|
|
|
502
709
|
// Incremental estimate keyed by contentIndex (streams may interleave blocks).
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
blockUnits.clear();
|
|
506
|
-
estimateUnits = 0;
|
|
507
|
-
break;
|
|
508
|
-
case "text_start":
|
|
509
|
-
case "thinking_start":
|
|
510
|
-
setBlockUnits(evt.contentIndex, 0);
|
|
511
|
-
break;
|
|
512
|
-
case "text_delta":
|
|
513
|
-
case "thinking_delta":
|
|
514
|
-
case "toolcall_delta":
|
|
515
|
-
setBlockUnits(evt.contentIndex, (blockUnits.get(evt.contentIndex) ?? 0) + estimateTextTokenUnits(evt.delta));
|
|
516
|
-
lastStreamAt = Date.now();
|
|
517
|
-
break;
|
|
518
|
-
case "text_end":
|
|
519
|
-
case "thinking_end":
|
|
520
|
-
setBlockUnits(evt.contentIndex, estimateTextTokenUnits(evt.content));
|
|
521
|
-
break;
|
|
522
|
-
case "toolcall_start": {
|
|
523
|
-
const block = message.content?.[evt.contentIndex];
|
|
524
|
-
setBlockUnits(evt.contentIndex, block ? estimateBlockTokenUnits(block) : 0);
|
|
525
|
-
break;
|
|
526
|
-
}
|
|
527
|
-
case "toolcall_end":
|
|
528
|
-
setBlockUnits(evt.contentIndex, estimateTextTokenUnits(toolCallText(evt.toolCall.name, evt.toolCall.arguments)));
|
|
529
|
-
break;
|
|
530
|
-
}
|
|
531
|
-
|
|
532
|
-
switch (evt.type) {
|
|
533
|
-
case "thinking_start":
|
|
534
|
-
thinkingStart = Date.now();
|
|
535
|
-
setMode("thinking");
|
|
536
|
-
break;
|
|
537
|
-
case "thinking_delta":
|
|
538
|
-
if (mode !== "thinking") {
|
|
539
|
-
thinkingStart = Date.now();
|
|
540
|
-
setMode("thinking");
|
|
541
|
-
}
|
|
542
|
-
break;
|
|
543
|
-
case "text_start":
|
|
544
|
-
case "text_delta":
|
|
545
|
-
lastStreamAt = Date.now();
|
|
546
|
-
setMode("responding");
|
|
547
|
-
break;
|
|
548
|
-
case "toolcall_start":
|
|
549
|
-
setMode("tool-input");
|
|
550
|
-
break;
|
|
551
|
-
}
|
|
710
|
+
applyMessageEstimate(evt, message, hasIndex);
|
|
711
|
+
applySpinnerMode(evt.type);
|
|
552
712
|
});
|
|
553
713
|
|
|
554
|
-
pi.on("message_end",
|
|
555
|
-
if (!runActive || event
|
|
556
|
-
ctx_ = ctx;
|
|
714
|
+
pi.on("message_end", (event, ctx) => {
|
|
715
|
+
if (!runActive || event?.message?.role !== "assistant") return;
|
|
716
|
+
ctx_ = ctx ?? null;
|
|
557
717
|
const message = event.message as AssistantTokenMessage;
|
|
558
718
|
const final = finalTokenReading(message);
|
|
559
719
|
// Exactly once per finalized assistant message; totals accumulate across tool turns.
|
|
560
720
|
completedTokens += final.tokens;
|
|
561
721
|
completedEstimated ||= final.estimated;
|
|
562
|
-
|
|
722
|
+
const outcome = outcomeFromStopReason(message.stopReason);
|
|
723
|
+
runOutcome = outcome;
|
|
724
|
+
if (outcome === "completed") completedAssistantMessage = true;
|
|
563
725
|
resetMessage();
|
|
564
726
|
mode = "responding";
|
|
565
727
|
// Snap so a correction (either direction) lands before tools run.
|
|
@@ -567,43 +729,77 @@ export default function claudeShimmer(pi: ExtensionAPI) {
|
|
|
567
729
|
updateDisplay();
|
|
568
730
|
});
|
|
569
731
|
|
|
570
|
-
pi.on("tool_execution_start",
|
|
732
|
+
pi.on("tool_execution_start", (_event, ctx) => {
|
|
571
733
|
if (!runActive) return;
|
|
572
|
-
ctx_ = ctx;
|
|
573
|
-
if (activeToolCount++ === 0) toolStart =
|
|
734
|
+
ctx_ = ctx ?? null;
|
|
735
|
+
if (activeToolCount++ === 0) toolStart = nowMs();
|
|
574
736
|
setMode("tool-use");
|
|
575
737
|
});
|
|
576
738
|
|
|
577
|
-
pi.on("tool_execution_end",
|
|
739
|
+
pi.on("tool_execution_end", (_event, ctx) => {
|
|
578
740
|
if (!runActive) return;
|
|
579
|
-
ctx_ = ctx;
|
|
741
|
+
ctx_ = ctx ?? null;
|
|
580
742
|
activeToolCount = Math.max(0, activeToolCount - 1);
|
|
581
743
|
if (activeToolCount === 0) setMode("requesting");
|
|
582
744
|
});
|
|
583
745
|
|
|
746
|
+
pi.on("ui_prompt_start", () => {
|
|
747
|
+
if (promptActive) return;
|
|
748
|
+
promptActive = true;
|
|
749
|
+
promptPausedAt = nowMs();
|
|
750
|
+
clockPausedByPrompt = timer !== null;
|
|
751
|
+
stopClock();
|
|
752
|
+
});
|
|
753
|
+
|
|
754
|
+
pi.on("ui_prompt_end", () => {
|
|
755
|
+
if (!promptActive) return;
|
|
756
|
+
const now = nowMs();
|
|
757
|
+
const pausedMs = Math.max(0, now - promptPausedAt);
|
|
758
|
+
const shouldResume = clockPausedByPrompt;
|
|
759
|
+
promptActive = false;
|
|
760
|
+
promptPausedAt = 0;
|
|
761
|
+
clockPausedByPrompt = false;
|
|
762
|
+
if (lastStreamAt !== null) {
|
|
763
|
+
lastStreamAt = Math.min(now, lastStreamAt + pausedMs);
|
|
764
|
+
}
|
|
765
|
+
if (runActive && shouldResume) startClock();
|
|
766
|
+
});
|
|
767
|
+
|
|
584
768
|
// Pi removes its working loader at agent_end; a retry/continuation restarts us
|
|
585
769
|
// via agent_start. The run (verb, clock, totals) only ends at agent_settled.
|
|
586
|
-
pi.on("agent_end",
|
|
770
|
+
pi.on("agent_end", (event, ctx) => {
|
|
587
771
|
stopClock();
|
|
772
|
+
clockPausedByPrompt = false;
|
|
588
773
|
if (!runActive) return;
|
|
589
|
-
const
|
|
590
|
-
|
|
774
|
+
const messages = Array.isArray(event?.messages) ? event.messages : [];
|
|
775
|
+
let last: AssistantTokenMessage | undefined;
|
|
776
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
777
|
+
const m = messages[i] as (AssistantTokenMessage & { role?: unknown }) | null | undefined;
|
|
778
|
+
if (m?.role === "assistant") {
|
|
779
|
+
last = m;
|
|
780
|
+
break;
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
if (last) {
|
|
784
|
+
const outcome = outcomeFromStopReason(last.stopReason);
|
|
785
|
+
runOutcome = outcome;
|
|
786
|
+
if (outcome === "completed") completedAssistantMessage = true;
|
|
787
|
+
}
|
|
591
788
|
try {
|
|
592
|
-
if (ctx
|
|
789
|
+
if (ctx?.signal?.aborted) runOutcome = "aborted";
|
|
593
790
|
} catch {}
|
|
594
791
|
});
|
|
595
792
|
|
|
596
|
-
pi.on("agent_settled",
|
|
597
|
-
ctx_ = ctx;
|
|
793
|
+
pi.on("agent_settled", (event, ctx) => {
|
|
794
|
+
ctx_ = ctx ?? null;
|
|
598
795
|
// Esc after agent_end (auto-compaction, agent_before_settle work) only shows up here:
|
|
599
796
|
// Pi 1.5.2 reports it as aborted. Older Pi sends no field, so check strictly.
|
|
600
|
-
if (runActive && event
|
|
797
|
+
if (runActive && event?.aborted === true) runOutcome = "aborted";
|
|
601
798
|
finishRun();
|
|
602
799
|
});
|
|
603
800
|
|
|
604
|
-
pi.on("session_shutdown",
|
|
605
|
-
|
|
606
|
-
runActive = false;
|
|
801
|
+
pi.on("session_shutdown", () => {
|
|
802
|
+
resetSessionState();
|
|
607
803
|
ctx_ = null;
|
|
608
804
|
});
|
|
609
805
|
}
|