@xmoon76/dsh-pi-tui 0.5.1 → 0.5.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +130 -17
- package/README.md +76 -16
- package/cordis.patch.yml +17 -49
- package/dist/builtins.mjs +3 -2
- package/dist/context-summary-CxUpNkA9.mjs +2951 -0
- package/dist/extension/advanced.d.mts +2 -2
- package/dist/extension/unstable.d.mts +1 -1
- package/dist/{extensions-v3Rf-qJA.d.mts → extensions-DzpBiiYb.d.mts} +1 -1
- package/dist/{extensions-1WYBUQ8I.mjs → extensions-a-rxanjw.mjs} +3514 -2892
- package/dist/extensions.d.mts +2 -2
- package/dist/extensions.mjs +1 -1
- package/dist/index.d.mts +13 -11
- package/dist/index.mjs +20158 -7768
- package/dist/performance-host-BJT6scGF.mjs +266 -0
- package/dist/performance-host.d.mts +36 -0
- package/dist/performance-host.mjs +2 -0
- package/dist/performance-view-jvvd8Jn7.mjs +476 -0
- package/dist/{public-types-BEjj7FEb.d.mts → public-types-gZi3iFNh.d.mts} +1 -1
- package/dist/rolldown-runtime-8H4AJuhK.mjs +14 -0
- package/dist/{runtime-CcQyhIvI.mjs → runtime-BtPH-UUb.mjs} +625 -261
- package/dist/session-BZLxhrPQ.mjs +5697 -0
- package/dist/{startup-CmTYVTGC.mjs → startup-D5Xu8diR.mjs} +22 -7
- package/dist/startup.d.mts +1 -2
- package/dist/startup.mjs +1 -1
- package/dist/tasks-D_z5Yjjr.mjs +267 -0
- package/dist/{terminal-progress-source-BfiTKDT-.mjs → terminal-progress-source-BlmksrSD.mjs} +116 -265
- package/generated/dsh-presets/cordis.patch.yml +8 -26
- package/generated/dsh-presets/minimal.patch.yml +17 -0
- package/generated/dsh-presets/ptc.patch.yml +8 -26
- package/generated/dsh-presets/standard.patch.yml +8 -26
- package/package.json +166 -146
|
@@ -0,0 +1,476 @@
|
|
|
1
|
+
import { expandAssistantStream } from "@deepseek-ai/dsh-llm/assistant-stream";
|
|
2
|
+
//#region src/domain/transcript/usage.ts
|
|
3
|
+
/**
|
|
4
|
+
* Shared per-step token usage accounting, used by BOTH the session stats
|
|
5
|
+
* fold (footer) and the Focus per-turn token projection. One accounting
|
|
6
|
+
* implementation, so the two surfaces can never drift:
|
|
7
|
+
*
|
|
8
|
+
* - usage is counted ONCE per attempt. An assistant/message or
|
|
9
|
+
* assistant/attempt settlement replaces that attempt's provisional stream
|
|
10
|
+
* usage; `llm/retry-started` closes that replacement slot so the next
|
|
11
|
+
* attempt on the same (turn, step) adds to the total;
|
|
12
|
+
* - a usage fact without a step boundary (replay edge) counts immediately;
|
|
13
|
+
* - the per-turn DISPLAY total is committed completed steps PLUS the open
|
|
14
|
+
* steps' current usage (provisional or authoritative) — provisional
|
|
15
|
+
* values are never committed early, so an authoritative replacement
|
|
16
|
+
* cannot double-count.
|
|
17
|
+
*
|
|
18
|
+
* TS8-D moved this accounting authority out of the legacy root
|
|
19
|
+
* `src/token-usage.ts`. The compact token-count FORMATTER is TUI presentation
|
|
20
|
+
* and lives in `src/tui/token-format.ts`.
|
|
21
|
+
* @module @xmoon76/dsh-pi-tui/domain/transcript/usage
|
|
22
|
+
*/
|
|
23
|
+
/** The billed total: uncached input + cache read + cache write + output. */
|
|
24
|
+
function totalTokens(usage) {
|
|
25
|
+
return usage.inputTokens + usage.outputTokens + usage.cacheReadTokens + usage.cacheWriteTokens;
|
|
26
|
+
}
|
|
27
|
+
/** A zeroed totals record (the accumulation base). */
|
|
28
|
+
function emptyTotals() {
|
|
29
|
+
return {
|
|
30
|
+
inputTokens: 0,
|
|
31
|
+
outputTokens: 0,
|
|
32
|
+
cacheReadTokens: 0,
|
|
33
|
+
cacheWriteTokens: 0
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
/** Add one usage record into a totals accumulator. */
|
|
37
|
+
function addTotals(target, usage) {
|
|
38
|
+
target.inputTokens += usage.inputTokens;
|
|
39
|
+
target.outputTokens += usage.outputTokens;
|
|
40
|
+
target.cacheReadTokens += usage.cacheReadTokens ?? 0;
|
|
41
|
+
target.cacheWriteTokens += usage.cacheWriteTokens ?? 0;
|
|
42
|
+
}
|
|
43
|
+
/** Key identifying one step's model output (turn + step). */
|
|
44
|
+
function stepKey(turn, step) {
|
|
45
|
+
return `${turn}/${step}`;
|
|
46
|
+
}
|
|
47
|
+
/** The turn number encoded in a step key. */
|
|
48
|
+
function turnOfKey(key) {
|
|
49
|
+
const slash = key.indexOf("/");
|
|
50
|
+
return slash === -1 ? -1 : Number(key.slice(0, slash));
|
|
51
|
+
}
|
|
52
|
+
/** The canonical first-token predicate shared by live and durable folds. */
|
|
53
|
+
function isAssistantTokenDelta(chunk) {
|
|
54
|
+
switch (chunk.type) {
|
|
55
|
+
case "text-delta":
|
|
56
|
+
case "reasoning-delta": return chunk.text !== void 0 && chunk.text !== "";
|
|
57
|
+
case "tool-call-delta": return (chunk.argumentsDelta ?? "") !== "" || chunk.name !== void 0;
|
|
58
|
+
default: return false;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
/** Return the first and last token timestamps embedded in a compact
|
|
62
|
+
* assistant stream, in one pass. Undefined when the stream is missing or
|
|
63
|
+
* carries no token-bearing delta. Older compatible event records may omit
|
|
64
|
+
* the optional stream. */
|
|
65
|
+
function tokenTimeRangeFromAssistantStream(stream) {
|
|
66
|
+
if (stream === void 0) return void 0;
|
|
67
|
+
let first;
|
|
68
|
+
let last;
|
|
69
|
+
for (const member of expandAssistantStream(stream)) if (isAssistantTokenDelta(member.chunk)) {
|
|
70
|
+
if (first === void 0) first = member.time;
|
|
71
|
+
last = member.time;
|
|
72
|
+
}
|
|
73
|
+
return first === void 0 || last === void 0 ? void 0 : {
|
|
74
|
+
first,
|
|
75
|
+
last
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
/** Return the first token timestamp embedded in a compact assistant stream. */
|
|
79
|
+
function firstTokenTimeFromAssistantStream(stream) {
|
|
80
|
+
return tokenTimeRangeFromAssistantStream(stream)?.first;
|
|
81
|
+
}
|
|
82
|
+
/** Return the latest provider usage embedded in a compact assistant stream. */
|
|
83
|
+
function usageFromAssistantStream(stream) {
|
|
84
|
+
if (stream === void 0) return void 0;
|
|
85
|
+
let sample;
|
|
86
|
+
for (const member of expandAssistantStream(stream)) if (member.chunk.type === "usage") sample = member.chunk.usage;
|
|
87
|
+
return sample;
|
|
88
|
+
}
|
|
89
|
+
/** Resolve the usage committed by one durable assistant settlement. Message
|
|
90
|
+
* records prefer their top-level usage; when it is absent, their embedded
|
|
91
|
+
* stream's final usage is authoritative. Attempt records always use the
|
|
92
|
+
* embedded stream because they have no top-level settlement usage. */
|
|
93
|
+
function usageFromAssistantSettlement(kind, topLevelUsage, stream) {
|
|
94
|
+
return kind === "message" && topLevelUsage !== void 0 ? topLevelUsage : usageFromAssistantStream(stream);
|
|
95
|
+
}
|
|
96
|
+
var StepUsageAccumulator = class {
|
|
97
|
+
/** Open steps: their current usage (undefined = no usage fact yet) and
|
|
98
|
+
* whether an authoritative message settled it. */
|
|
99
|
+
perStep = /* @__PURE__ */ new Map();
|
|
100
|
+
/** Facts WITHOUT an open step per (turn, step) — orphan facts (the step
|
|
101
|
+
* never opened) and closed steps' committed values alike. Each fact
|
|
102
|
+
* carries its provenance: a PROVISIONAL chunk never replaces an
|
|
103
|
+
* AUTHORITATIVE value (the message is the step's final usage; a later
|
|
104
|
+
* chunk is stale), while a message replaces anything and a chunk
|
|
105
|
+
* replaces a provisional value (the latest fact wins — review
|
|
106
|
+
* findings). */
|
|
107
|
+
settledByStep = /* @__PURE__ */ new Map();
|
|
108
|
+
/** Committed per-turn totals (completed steps + orphan facts). */
|
|
109
|
+
turnTotals = /* @__PURE__ */ new Map();
|
|
110
|
+
/** Open steps' current usage per turn (the running display share). */
|
|
111
|
+
turnPending = /* @__PURE__ */ new Map();
|
|
112
|
+
/** The session-wide total (every committed fact), kept O(1). */
|
|
113
|
+
session = emptyTotals();
|
|
114
|
+
/** The highest turn seen so far: when a NEW turn appears, the settled
|
|
115
|
+
* records of all OLDER turns are dropped — their steps are closed and a
|
|
116
|
+
* well-formed log never revisits them, so the map stays bounded by the
|
|
117
|
+
* current turn's steps (review finding). A fact for an older turn's
|
|
118
|
+
* step beyond that (corrupt log) commits as a new fact. */
|
|
119
|
+
currentTurn = -1;
|
|
120
|
+
/** Track the turn boundary: when the turn advances, drop the settled
|
|
121
|
+
* records of older turns, FINALIZE their still-open steps (commit the
|
|
122
|
+
* pending usage — the facts were real) and drop their pending totals,
|
|
123
|
+
* so no open-step state leaks across turn boundaries (review finding).
|
|
124
|
+
* The current turn is remembered so a LATE fact for an older turn
|
|
125
|
+
* (corrupt log) is ignored — never re-counted. */
|
|
126
|
+
noteTurn(turn) {
|
|
127
|
+
if (turn <= this.currentTurn) return;
|
|
128
|
+
if (this.currentTurn >= 0) {
|
|
129
|
+
for (const key of this.settledByStep.keys()) if (turnOfKey(key) < turn) this.settledByStep.delete(key);
|
|
130
|
+
for (const [key, entry] of this.perStep) if (turnOfKey(key) < turn) {
|
|
131
|
+
if (entry.usage !== void 0) {
|
|
132
|
+
addTotals(this.turnTotalFor(turnOfKey(key)), entry.usage);
|
|
133
|
+
addTotals(this.session, entry.usage);
|
|
134
|
+
}
|
|
135
|
+
this.perStep.delete(key);
|
|
136
|
+
}
|
|
137
|
+
for (const pendingTurn of this.turnPending.keys()) if (pendingTurn < turn) this.turnPending.delete(pendingTurn);
|
|
138
|
+
}
|
|
139
|
+
this.currentTurn = turn;
|
|
140
|
+
}
|
|
141
|
+
/** Whether a fact for this turn is stale: the turn already advanced past
|
|
142
|
+
* it (a well-formed log is monotonic; a late older-turn fact is a
|
|
143
|
+
* corrupt-log artifact and is ignored entirely). */
|
|
144
|
+
staleTurn(turn) {
|
|
145
|
+
return turn < this.currentTurn;
|
|
146
|
+
}
|
|
147
|
+
/** Open one step's accounting. A step that opens AFTER a settled fact of
|
|
148
|
+
* the same (turn, step) (out-of-order replay) reconciles the fact: the
|
|
149
|
+
* premature commit is reversed and the fact becomes the step's pending
|
|
150
|
+
* usage — with its provenance preserved — to be committed once at
|
|
151
|
+
* step/end, never twice (review findings). */
|
|
152
|
+
onStepStart(turn, step) {
|
|
153
|
+
this.noteTurn(turn);
|
|
154
|
+
if (this.staleTurn(turn)) return;
|
|
155
|
+
const key = stepKey(turn, step);
|
|
156
|
+
if (this.perStep.has(key)) return;
|
|
157
|
+
const settled = this.settledByStep.get(key);
|
|
158
|
+
if (settled !== void 0) {
|
|
159
|
+
this.settledByStep.delete(key);
|
|
160
|
+
this.subtractTotals(this.turnTotalFor(turn), settled.usage);
|
|
161
|
+
this.subtractTotals(this.session, settled.usage);
|
|
162
|
+
this.perStep.set(key, {
|
|
163
|
+
usage: settled.usage,
|
|
164
|
+
authoritative: settled.authoritative
|
|
165
|
+
});
|
|
166
|
+
this.addPending(turn, settled.usage);
|
|
167
|
+
} else this.perStep.set(key, {});
|
|
168
|
+
}
|
|
169
|
+
/** Record a streaming usage chunk: the LATEST chunk of an open step
|
|
170
|
+
* replaces the previous one (the assembler value is cumulative — the
|
|
171
|
+
* running display must show the latest provisional usage, plan §13.2),
|
|
172
|
+
* but a provisional chunk NEVER replaces an authoritative value. A
|
|
173
|
+
* chunk without an open step (replay edge) is a settled fact and
|
|
174
|
+
* commits to the turn's totals immediately. */
|
|
175
|
+
onUsageChunk(turn, step, usage) {
|
|
176
|
+
this.noteTurn(turn);
|
|
177
|
+
if (this.staleTurn(turn)) return;
|
|
178
|
+
const entry = this.perStep.get(stepKey(turn, step));
|
|
179
|
+
if (entry !== void 0) {
|
|
180
|
+
if (entry.authoritative === true) return;
|
|
181
|
+
if (entry.usage !== void 0) this.subtractPending(turn, entry.usage);
|
|
182
|
+
entry.usage = usage;
|
|
183
|
+
this.addPending(turn, usage);
|
|
184
|
+
} else this.commitSettled(turn, step, {
|
|
185
|
+
usage,
|
|
186
|
+
authoritative: false
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
/** The authoritative usage of a settled step replaces the provisional
|
|
190
|
+
* one (never adds to it) — on the open-step path AND the settled path.
|
|
191
|
+
* A message without an open step (replay edge) commits to the turn's
|
|
192
|
+
* totals immediately. */
|
|
193
|
+
onAssistantMessage(turn, step, usage) {
|
|
194
|
+
this.noteTurn(turn);
|
|
195
|
+
if (this.staleTurn(turn)) return;
|
|
196
|
+
const entry = this.perStep.get(stepKey(turn, step));
|
|
197
|
+
if (entry !== void 0) {
|
|
198
|
+
if (usage !== void 0) {
|
|
199
|
+
if (entry.usage !== void 0) this.subtractPending(turn, entry.usage);
|
|
200
|
+
entry.usage = usage;
|
|
201
|
+
entry.authoritative = true;
|
|
202
|
+
this.addPending(turn, usage);
|
|
203
|
+
}
|
|
204
|
+
} else if (usage !== void 0) this.commitSettled(turn, step, {
|
|
205
|
+
usage,
|
|
206
|
+
authoritative: true
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
/** Settle a failed attempt from its durable embedded stream. The attempt
|
|
210
|
+
* has no surface message, but its provider usage is still billed. Any live
|
|
211
|
+
* provisional value is replaced by the stream's last usage sample. */
|
|
212
|
+
onAssistantAttempt(turn, step, usage) {
|
|
213
|
+
this.noteTurn(turn);
|
|
214
|
+
if (this.staleTurn(turn)) return;
|
|
215
|
+
const key = stepKey(turn, step);
|
|
216
|
+
const entry = this.perStep.get(key);
|
|
217
|
+
if (entry !== void 0) {
|
|
218
|
+
if (usage === void 0 && entry.authoritative === true) return;
|
|
219
|
+
if (entry.usage !== void 0) this.subtractPending(turn, entry.usage);
|
|
220
|
+
this.perStep.delete(key);
|
|
221
|
+
}
|
|
222
|
+
if (usage !== void 0) this.commitSettled(turn, step, {
|
|
223
|
+
usage,
|
|
224
|
+
authoritative: true
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
/** Close the current replacement slot before a retry opens the same
|
|
228
|
+
* (turn, step) again. The settled attempt remains in the totals; removing
|
|
229
|
+
* only the replacement record makes the retried attempt add rather than
|
|
230
|
+
* replace it. */
|
|
231
|
+
onRetryStarted(turn, step) {
|
|
232
|
+
this.noteTurn(turn);
|
|
233
|
+
if (this.staleTurn(turn)) return;
|
|
234
|
+
const key = stepKey(turn, step);
|
|
235
|
+
const entry = this.perStep.get(key);
|
|
236
|
+
if (entry !== void 0) {
|
|
237
|
+
if (entry.usage !== void 0) this.subtractPending(turn, entry.usage);
|
|
238
|
+
this.perStep.delete(key);
|
|
239
|
+
}
|
|
240
|
+
this.settledByStep.delete(key);
|
|
241
|
+
this.perStep.set(key, {});
|
|
242
|
+
}
|
|
243
|
+
/** Advance the turn boundary: finalize the prior turn's still-open
|
|
244
|
+
* steps and make its delayed facts stale. Called by the folds at
|
|
245
|
+
* turn/start — a delayed usage event for the prior turn must not
|
|
246
|
+
* replace its pending usage after the next turn started (review
|
|
247
|
+
* finding). */
|
|
248
|
+
onTurnStart(turn) {
|
|
249
|
+
this.noteTurn(turn);
|
|
250
|
+
}
|
|
251
|
+
/** Finalize a turn: commit its still-open steps' pending usage (the
|
|
252
|
+
* facts were real) and drop the pending totals, so the Focus per-turn
|
|
253
|
+
* total and the session total agree even when turn/end arrives with
|
|
254
|
+
* open steps (review finding). Idempotent — a second call finds no open
|
|
255
|
+
* steps. */
|
|
256
|
+
onTurnEnd(turn) {
|
|
257
|
+
for (const [key, entry] of this.perStep) if (turnOfKey(key) === turn && entry.usage !== void 0) {
|
|
258
|
+
addTotals(this.turnTotalFor(turn), entry.usage);
|
|
259
|
+
addTotals(this.session, entry.usage);
|
|
260
|
+
this.perStep.delete(key);
|
|
261
|
+
}
|
|
262
|
+
this.turnPending.delete(turn);
|
|
263
|
+
}
|
|
264
|
+
/** Commit one step's usage (once) and drop its open state. The step's
|
|
265
|
+
* final fact stays in {@link settledByStep} (with its provenance) so a
|
|
266
|
+
* late fact replaces or is ignored correctly; a step that never opened
|
|
267
|
+
* keeps its settled fact unchanged. */
|
|
268
|
+
onStepEnd(turn, step) {
|
|
269
|
+
this.noteTurn(turn);
|
|
270
|
+
if (this.staleTurn(turn)) return;
|
|
271
|
+
const key = stepKey(turn, step);
|
|
272
|
+
const entry = this.perStep.get(key);
|
|
273
|
+
if (entry !== void 0) {
|
|
274
|
+
if (entry.usage !== void 0) {
|
|
275
|
+
this.subtractPending(turn, entry.usage);
|
|
276
|
+
addTotals(this.turnTotalFor(turn), entry.usage);
|
|
277
|
+
addTotals(this.session, entry.usage);
|
|
278
|
+
this.settledByStep.set(key, {
|
|
279
|
+
usage: entry.usage,
|
|
280
|
+
authoritative: entry.authoritative === true
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
this.perStep.delete(key);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
/** Discard an OPEN attempt's provisional accounting WITHOUT committing it
|
|
287
|
+
* when the live stream is abandoned and has no durable settlement. A
|
|
288
|
+
* durable `assistant/attempt` must use {@link onAssistantAttempt} instead,
|
|
289
|
+
* because its embedded stream carries real provider usage. An authoritative
|
|
290
|
+
* value (a durable `assistant/message` already settled the step) and a
|
|
291
|
+
* committed fact are never discarded — the durable log owns them. */
|
|
292
|
+
discardStep(turn, step) {
|
|
293
|
+
if (this.staleTurn(turn)) return;
|
|
294
|
+
const key = stepKey(turn, step);
|
|
295
|
+
const entry = this.perStep.get(key);
|
|
296
|
+
if (entry === void 0 || entry.authoritative === true) return;
|
|
297
|
+
if (entry.usage !== void 0) this.subtractPending(turn, entry.usage);
|
|
298
|
+
this.perStep.delete(key);
|
|
299
|
+
}
|
|
300
|
+
/** The committed per-turn totals (completed steps + orphan facts);
|
|
301
|
+
* undefined when the turn has no committed usage fact. */
|
|
302
|
+
turnUsage(turn) {
|
|
303
|
+
return this.turnTotals.get(turn);
|
|
304
|
+
}
|
|
305
|
+
/** The per-turn DISPLAY totals: committed steps plus the open steps'
|
|
306
|
+
* current usage (provisional or authoritative) — never double-counted.
|
|
307
|
+
* Undefined when the turn has no usage fact at all (the header then
|
|
308
|
+
* hides the token segment instead of showing `0 tok`). */
|
|
309
|
+
turnUsageWithPending(turn) {
|
|
310
|
+
const committed = this.turnTotals.get(turn);
|
|
311
|
+
const pending = this.turnPending.get(turn);
|
|
312
|
+
if (committed === void 0 && pending === void 0) return void 0;
|
|
313
|
+
const totals = emptyTotals();
|
|
314
|
+
if (committed !== void 0) addTotals(totals, committed);
|
|
315
|
+
if (pending !== void 0) addTotals(totals, pending);
|
|
316
|
+
return totals;
|
|
317
|
+
}
|
|
318
|
+
/** The session-wide totals (every committed fact — the sum of the
|
|
319
|
+
* per-turn committed totals by construction, so the footer and the
|
|
320
|
+
* Focus per-turn projection can never drift). */
|
|
321
|
+
sessionTotals() {
|
|
322
|
+
return { ...this.session };
|
|
323
|
+
}
|
|
324
|
+
turnTotalFor(turn) {
|
|
325
|
+
let totals = this.turnTotals.get(turn);
|
|
326
|
+
if (totals === void 0) {
|
|
327
|
+
totals = emptyTotals();
|
|
328
|
+
this.turnTotals.set(turn, totals);
|
|
329
|
+
}
|
|
330
|
+
return totals;
|
|
331
|
+
}
|
|
332
|
+
addPending(turn, usage) {
|
|
333
|
+
let pending = this.turnPending.get(turn);
|
|
334
|
+
if (pending === void 0) {
|
|
335
|
+
pending = emptyTotals();
|
|
336
|
+
this.turnPending.set(turn, pending);
|
|
337
|
+
}
|
|
338
|
+
addTotals(pending, usage);
|
|
339
|
+
}
|
|
340
|
+
subtractPending(turn, usage) {
|
|
341
|
+
const pending = this.turnPending.get(turn);
|
|
342
|
+
if (pending === void 0) return;
|
|
343
|
+
pending.inputTokens -= usage.inputTokens;
|
|
344
|
+
pending.outputTokens -= usage.outputTokens;
|
|
345
|
+
pending.cacheReadTokens -= usage.cacheReadTokens ?? 0;
|
|
346
|
+
pending.cacheWriteTokens -= usage.cacheWriteTokens ?? 0;
|
|
347
|
+
if (pending.inputTokens === 0 && pending.outputTokens === 0 && pending.cacheReadTokens === 0 && pending.cacheWriteTokens === 0) this.turnPending.delete(turn);
|
|
348
|
+
}
|
|
349
|
+
/** A usage fact without an open step (replay edge) is a settled fact:
|
|
350
|
+
* commit it to the turn's totals immediately, so
|
|
351
|
+
* sum(per-turn committed) == session total stays strict. The latest
|
|
352
|
+
* fact REPLACES the previous one of the same (turn, step) — the same
|
|
353
|
+
* replace-never-add rule as the open-step path — EXCEPT a provisional
|
|
354
|
+
* chunk never replaces an authoritative value (the message is the
|
|
355
|
+
* step's final usage; the chunk is stale and is ignored entirely, so
|
|
356
|
+
* the step is never double-counted). */
|
|
357
|
+
commitSettled(turn, step, fact) {
|
|
358
|
+
const key = stepKey(turn, step);
|
|
359
|
+
const existing = this.settledByStep.get(key);
|
|
360
|
+
if (existing !== void 0) {
|
|
361
|
+
if (existing.authoritative && !fact.authoritative) return;
|
|
362
|
+
this.subtractTotals(this.turnTotalFor(turn), existing.usage);
|
|
363
|
+
this.subtractTotals(this.session, existing.usage);
|
|
364
|
+
}
|
|
365
|
+
this.settledByStep.set(key, fact);
|
|
366
|
+
addTotals(this.turnTotalFor(turn), fact.usage);
|
|
367
|
+
addTotals(this.session, fact.usage);
|
|
368
|
+
}
|
|
369
|
+
/** Subtract one usage record from a totals accumulator. */
|
|
370
|
+
subtractTotals(target, usage) {
|
|
371
|
+
target.inputTokens -= usage.inputTokens;
|
|
372
|
+
target.outputTokens -= usage.outputTokens;
|
|
373
|
+
target.cacheReadTokens -= usage.cacheReadTokens ?? 0;
|
|
374
|
+
target.cacheWriteTokens -= usage.cacheWriteTokens ?? 0;
|
|
375
|
+
}
|
|
376
|
+
};
|
|
377
|
+
//#endregion
|
|
378
|
+
//#region src/domain/status/performance-view.ts
|
|
379
|
+
/**
|
|
380
|
+
* The `piTuiPerformance` wire view and its display derivation.
|
|
381
|
+
*
|
|
382
|
+
* ONE formula, two scopes (plan §1.1), for one eligible completed model step
|
|
383
|
+
* `s`:
|
|
384
|
+
*
|
|
385
|
+
* ```text
|
|
386
|
+
* modelMs(s) = assistant/message.time - step/start.time
|
|
387
|
+
* output(s) = assistant/message usage.outputTokens
|
|
388
|
+
* TPS = (1000 * Σoutput(s)) / ΣmodelMs(s)
|
|
389
|
+
* ```
|
|
390
|
+
*
|
|
391
|
+
* `modelMs` covers the whole model request — first-token wait, invisible
|
|
392
|
+
* reasoning, generation, and same-step retries — but never tool execution
|
|
393
|
+
* outside the step. `reasoningTokens` are already part of DSH's
|
|
394
|
+
* `outputTokens`, so they are never added twice.
|
|
395
|
+
*
|
|
396
|
+
* `recent` is the last {@link RECENT_PERFORMANCE_SAMPLE_LIMIT} eligible
|
|
397
|
+
* samples of the CURRENT `(provider, model)` route generation (an A→B→A route
|
|
398
|
+
* change starts a clean window); `all` is every eligible sample of the Session.
|
|
399
|
+
* TTFB is the arithmetic mean of the recent observable first-token latencies
|
|
400
|
+
* (its own sample list, independent of `outputTokens > 0`).
|
|
401
|
+
*
|
|
402
|
+
* The module is transport/UI neutral and owns ONLY the view shape, the
|
|
403
|
+
* weighted rate, and the display precision policy: it never selects a route,
|
|
404
|
+
* folds an event, reads a Session, or holds a sample ring. The Host reducer
|
|
405
|
+
* (`src/app/host/performance-projection.ts`) owns every sample; the Direct /
|
|
406
|
+
* Remote carriers, `/status`, and the footer all read this one contract.
|
|
407
|
+
*
|
|
408
|
+
* @module @xmoon76/dsh-pi-tui/domain/status/performance-view
|
|
409
|
+
*/
|
|
410
|
+
/** The projection key registered on the official session-projection seam. */
|
|
411
|
+
const PI_TUI_PERFORMANCE_KEY = "piTuiPerformance";
|
|
412
|
+
/** The empty view (fresh objects: state is never aliased). */
|
|
413
|
+
function emptyPerformanceView() {
|
|
414
|
+
return {
|
|
415
|
+
recent: {
|
|
416
|
+
outputTokens: 0,
|
|
417
|
+
modelMs: 0,
|
|
418
|
+
samples: 0,
|
|
419
|
+
firstTokenMs: 0,
|
|
420
|
+
firstTokenSamples: 0
|
|
421
|
+
},
|
|
422
|
+
all: {
|
|
423
|
+
outputTokens: 0,
|
|
424
|
+
modelMs: 0,
|
|
425
|
+
samples: 0
|
|
426
|
+
}
|
|
427
|
+
};
|
|
428
|
+
}
|
|
429
|
+
/**
|
|
430
|
+
* The weighted average model-request output rate of one scope. `undefined`
|
|
431
|
+
* when the paired totals cannot answer (no positive output or no positive
|
|
432
|
+
* wall time) — never a zero stand-in for unknown.
|
|
433
|
+
* @param outputTokens - Σ eligible output tokens.
|
|
434
|
+
* @param modelMs - Σ eligible model request wall time, ms.
|
|
435
|
+
* @returns tok/s at full precision, or `undefined`.
|
|
436
|
+
*/
|
|
437
|
+
function rateFromTotals(outputTokens, modelMs) {
|
|
438
|
+
if (!Number.isFinite(outputTokens) || outputTokens <= 0) return void 0;
|
|
439
|
+
if (!Number.isFinite(modelMs) || modelMs <= 0) return void 0;
|
|
440
|
+
return 1e3 * outputTokens / modelMs;
|
|
441
|
+
}
|
|
442
|
+
/**
|
|
443
|
+
* The ONE display precision policy (plan §1.1): below 100 tok/s one decimal
|
|
444
|
+
* place, at or above it an integer. The projection itself keeps raw sums, so
|
|
445
|
+
* the same policy applies to every surface that renders a rate.
|
|
446
|
+
* @param rate - a rate at full precision.
|
|
447
|
+
* @returns the display number (a `number`, so trailing `.0` never appears).
|
|
448
|
+
*/
|
|
449
|
+
function displayRate(rate) {
|
|
450
|
+
return rate < 100 ? Number(rate.toFixed(1)) : Math.round(rate);
|
|
451
|
+
}
|
|
452
|
+
/**
|
|
453
|
+
* Derive the display facts of one Session's `piTuiPerformance` view. This is
|
|
454
|
+
* pure arithmetic over the Host's raw counters: no route, event, or Session
|
|
455
|
+
* selection happens here, and no value is inferred from local history.
|
|
456
|
+
*
|
|
457
|
+
* A rate requires BOTH a paired total and at least one eligible sample
|
|
458
|
+
* (plan §3.2): a zero-sample scope is unknown, so stray counters in such a
|
|
459
|
+
* scope never become a rate. The TTFB average carries its own sample gate.
|
|
460
|
+
* @param projection - the Session's own Host projection, or `undefined` when
|
|
461
|
+
* the Host cannot serve that key.
|
|
462
|
+
* @returns the present display facts (an empty object when nothing answers).
|
|
463
|
+
*/
|
|
464
|
+
function derivePerformance(projection) {
|
|
465
|
+
if (projection === void 0) return {};
|
|
466
|
+
const tokensPerSec = projection.recent.samples > 0 ? rateFromTotals(projection.recent.outputTokens, projection.recent.modelMs) : void 0;
|
|
467
|
+
const sessionTokensPerSec = projection.all.samples > 0 ? rateFromTotals(projection.all.outputTokens, projection.all.modelMs) : void 0;
|
|
468
|
+
const firstTokenMs = projection.recent.firstTokenSamples > 0 ? projection.recent.firstTokenMs / projection.recent.firstTokenSamples : void 0;
|
|
469
|
+
return {
|
|
470
|
+
...firstTokenMs === void 0 ? {} : { firstTokenMs },
|
|
471
|
+
...tokensPerSec === void 0 ? {} : { tokensPerSec: displayRate(tokensPerSec) },
|
|
472
|
+
...sessionTokensPerSec === void 0 ? {} : { sessionTokensPerSec: displayRate(sessionTokensPerSec) }
|
|
473
|
+
};
|
|
474
|
+
}
|
|
475
|
+
//#endregion
|
|
476
|
+
export { firstTokenTimeFromAssistantStream as a, usageFromAssistantSettlement as c, StepUsageAccumulator as i, derivePerformance as n, isAssistantTokenDelta as o, emptyPerformanceView as r, totalTokens as s, PI_TUI_PERFORMANCE_KEY as t };
|
|
@@ -822,7 +822,7 @@ type EditorInputEvent =
|
|
|
822
822
|
} /** One plain printable character run (ordinary typing). */ | {
|
|
823
823
|
readonly kind: 'text';
|
|
824
824
|
readonly text: string;
|
|
825
|
-
} /** A paste burst (
|
|
825
|
+
} /** A bracket-paste burst (`CSI 200~` … `CSI 201~`). */ | {
|
|
826
826
|
readonly kind: 'paste';
|
|
827
827
|
readonly text: string;
|
|
828
828
|
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
//#region \0rolldown/runtime.js
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __exportAll = (all, no_symbols) => {
|
|
5
|
+
let target = {};
|
|
6
|
+
for (var name in all) __defProp(target, name, {
|
|
7
|
+
get: all[name],
|
|
8
|
+
enumerable: true
|
|
9
|
+
});
|
|
10
|
+
if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
11
|
+
return target;
|
|
12
|
+
};
|
|
13
|
+
//#endregion
|
|
14
|
+
export { __exportAll as t };
|