dsh-last-response-stats 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,23 +1,27 @@
1
1
  # Last Response Stats
2
2
 
3
- A small DeepSeek Harness bundle that shows the average output speed and
4
- prompt-cache hit of the most recent Chat response — for example
5
- `42 tok/s · 87% cached` — directly under that response in the conversation
6
- flow.
3
+ A small DeepSeek Harness bundle that shows the average output speed,
4
+ prompt-cache hit, and time to first token of the most recent Chat response —
5
+ for example `42 tok/s · 87% cache hit · 0.4s TTFT` — directly under that
6
+ response in the conversation flow.
7
7
 
8
8
  ## What it computes
9
9
 
10
10
  ```text
11
11
  tok/s = provider output tokens / (message settlement - first token)
12
12
  cache hit = cache-read tokens / (uncached input + cache-read + cache-write)
13
+ TTFT = first token - step start
14
+ total = message settlement - step start
13
15
  ```
14
16
 
15
- The first-token timestamp comes from the durable `assistant/message` stream,
16
- so the reading survives a page reload. Only content-bearing assistant messages
17
- count as a response; reasoning-only and tool-call-only messages are skipped.
18
- Each part is hidden when its inputs are missing: no output-token usage or
19
- first token means no speed, and no cache-read/cache-write report means no
20
- cache hit.
17
+ The first-token timestamp comes from the durable `assistant/message` stream
18
+ and the step start from its preceding `step/start`, so the readings survive a
19
+ page reload. Only content-bearing assistant messages count as a response;
20
+ reasoning-only and tool-call-only messages are skipped. Each part is hidden
21
+ when its inputs are missing: no output-token usage or first token means no
22
+ speed, and no cache-read/cache-write report means no cache hit. Hovering the
23
+ reading shows the output and prompt token counts plus the total response
24
+ time.
21
25
 
22
26
  ## Where it appears
23
27
 
package/client.js CHANGED
@@ -9,17 +9,27 @@ window.__ModuleLoader__.load({
9
9
  label: '{value} tok/s',
10
10
  title: 'Average generation speed for the last response',
11
11
  detail: '{tokens} output tokens over {seconds} s',
12
- cacheLabel: '{percent}% cached',
12
+ cacheLabel: '{percent}% cache hit',
13
13
  cacheTitle: 'Prompt cache hit for the last response',
14
14
  cacheDetail: '{cached} of {prompt} prompt tokens served from cache',
15
+ ttftLabel: 'TTFT {value}s',
16
+ ttftTitle: 'Time to first token',
17
+ ttftDetail: '{value}s from request start to first token',
18
+ totalTitle: 'Total response time',
19
+ totalDetail: '{value}s from request start to completion',
15
20
  };
16
21
  const ZH = {
17
22
  label: '{value} tok/s',
18
23
  title: '最近一次回复的平均生成速度',
19
24
  detail: '最近一次回复:{tokens} 个输出 token,用时 {seconds} 秒',
20
- cacheLabel: '{percent}% 已缓存',
25
+ cacheLabel: '{percent}% 缓存命中',
21
26
  cacheTitle: '最近一次回复的提示词缓存命中',
22
27
  cacheDetail: '提示词缓存命中 {cached}/{prompt} 个 token',
28
+ ttftLabel: 'TTFT {value}s',
29
+ ttftTitle: '首 token 延迟',
30
+ ttftDetail: '从请求开始到首 token:{value} 秒',
31
+ totalTitle: '总响应时间',
32
+ totalDetail: '从请求开始到完成:{value} 秒',
23
33
  };
24
34
 
25
35
  const CSS = [
@@ -131,14 +141,19 @@ window.__ModuleLoader__.load({
131
141
  const tokensPerSecond = hasSpeed ? outputTokens / (decodeMs / 1000) : null;
132
142
  const hasFiniteSpeed =
133
143
  tokensPerSecond !== null && Number.isFinite(tokensPerSecond) && tokensPerSecond > 0;
144
+ const ttftMs = stats.ttftMs;
145
+ const totalMs = stats.totalMs;
146
+ const hasTtft = typeof ttftMs === 'number' && ttftMs >= 0;
147
+ const hasTotal = typeof totalMs === 'number' && totalMs >= 0;
134
148
  const cache = cacheHitReading(stats);
135
- if (!hasFiniteSpeed && cache === null) return null;
149
+ if (!hasFiniteSpeed && cache === null && !hasTtft) return null;
136
150
 
137
151
  const speedLabel = hasFiniteSpeed
138
152
  ? t('label', { value: formatSpeed(tokensPerSecond) })
139
153
  : null;
140
154
  const cacheLabel = cache !== null ? t('cacheLabel', { percent: cache.percent }) : null;
141
- const label = [speedLabel, cacheLabel].filter(Boolean).join(' · ');
155
+ const ttftLabel = hasTtft ? t('ttftLabel', { value: formatSeconds(ttftMs) }) : null;
156
+ const label = [speedLabel, cacheLabel, ttftLabel].filter(Boolean).join(' · ');
142
157
 
143
158
  const titleParts = [];
144
159
  if (speedLabel !== null) {
@@ -157,6 +172,16 @@ window.__ModuleLoader__.load({
157
172
  })}`,
158
173
  );
159
174
  }
175
+ if (hasTtft) {
176
+ titleParts.push(
177
+ `${t('ttftTitle')} · ${t('ttftDetail', { value: formatSeconds(ttftMs) })}`,
178
+ );
179
+ }
180
+ if (hasTotal) {
181
+ titleParts.push(
182
+ `${t('totalTitle')} · ${t('totalDetail', { value: formatSeconds(totalMs) })}`,
183
+ );
184
+ }
160
185
 
161
186
  const data = {};
162
187
  if (speedLabel !== null) {
@@ -168,6 +193,8 @@ window.__ModuleLoader__.load({
168
193
  data['data-cache-read-tokens'] = cache.cachedTokens;
169
194
  data['data-prompt-tokens'] = cache.promptTokens;
170
195
  }
196
+ if (hasTtft) data['data-ttft-ms'] = ttftMs;
197
+ if (hasTotal) data['data-total-ms'] = totalMs;
171
198
 
172
199
  return h(
173
200
  'span',
package/index.js CHANGED
@@ -3,8 +3,8 @@
3
3
  *
4
4
  * `lastResponseStats` is a per-Session projection over the durable log: the
5
5
  * most recent content-bearing assistant message, its provider token usage
6
- * (output and prompt-cache buckets), and its decode window (first token ->
7
- * message settlement). The Client reads the whole value through
6
+ * (output and prompt-cache buckets), and its first-token, decode, and total
7
+ * latency. The Client reads the whole value through
8
8
  * `useProjection('lastResponseStats')`; no Client-side event folding is
9
9
  * involved.
10
10
  */
@@ -23,6 +23,14 @@ const EMPTY_STATE = Object.freeze({
23
23
  cacheWriteTokens: null,
24
24
  /** completedTime - firstTokenTime in milliseconds, or null when undecidable. */
25
25
  decodeMs: null,
26
+ /** firstTokenTime - stepStartTime in milliseconds, or null without a step start. */
27
+ ttftMs: null,
28
+ /** completedTime - stepStartTime in milliseconds, or null without a step start. */
29
+ totalMs: null,
30
+ /** Start boundary of the step currently being folded. */
31
+ stepStartTime: null,
32
+ stepStartTurn: null,
33
+ stepStartStep: null,
26
34
  /** First token delta timestamp, or null when the stream records none. */
27
35
  firstTokenTime: null,
28
36
  /** assistant/message settlement timestamp, or null before the first response. */
@@ -50,7 +58,7 @@ function isNullableNumber(value) {
50
58
  * on these schemas; the structural checks keep a malformed persisted-cache
51
59
  * row from seeding a fold.
52
60
  * @param {unknown} value
53
- * @returns {{ seq: number, turn: number | null, step: number | null, outputTokens: number | null, inputTokens: number | null, cacheReadTokens: number | null, cacheWriteTokens: number | null, decodeMs: number | null, firstTokenTime: number | null, completedTime: number | null }}
61
+ * @returns {{ seq: number, turn: number | null, step: number | null, outputTokens: number | null, inputTokens: number | null, cacheReadTokens: number | null, cacheWriteTokens: number | null, decodeMs: number | null, ttftMs: number | null, totalMs: number | null, stepStartTime: number | null, stepStartTurn: number | null, stepStartStep: number | null, firstTokenTime: number | null, completedTime: number | null }}
54
62
  */
55
63
  function parseState(value) {
56
64
  if (typeof value !== 'object' || value === null || Array.isArray(value)) {
@@ -65,6 +73,11 @@ function parseState(value) {
65
73
  cacheReadTokens,
66
74
  cacheWriteTokens,
67
75
  decodeMs,
76
+ ttftMs,
77
+ totalMs,
78
+ stepStartTime,
79
+ stepStartTurn,
80
+ stepStartStep,
68
81
  firstTokenTime,
69
82
  completedTime,
70
83
  } = value;
@@ -92,6 +105,21 @@ function parseState(value) {
92
105
  if (!isNullableCount(decodeMs)) {
93
106
  throw new TypeError('lastResponseStats decodeMs must be a non-negative integer or null');
94
107
  }
108
+ if (!isNullableCount(ttftMs)) {
109
+ throw new TypeError('lastResponseStats ttftMs must be a non-negative integer or null');
110
+ }
111
+ if (!isNullableCount(totalMs)) {
112
+ throw new TypeError('lastResponseStats totalMs must be a non-negative integer or null');
113
+ }
114
+ if (!isNullableNumber(stepStartTime)) {
115
+ throw new TypeError('lastResponseStats stepStartTime must be a finite number or null');
116
+ }
117
+ if (!(stepStartTurn === null || (Number.isSafeInteger(stepStartTurn) && stepStartTurn >= 0))) {
118
+ throw new TypeError('lastResponseStats stepStartTurn must be a non-negative integer or null');
119
+ }
120
+ if (!(stepStartStep === null || (Number.isSafeInteger(stepStartStep) && stepStartStep >= 0))) {
121
+ throw new TypeError('lastResponseStats stepStartStep must be a non-negative integer or null');
122
+ }
95
123
  if (!isNullableNumber(firstTokenTime)) {
96
124
  throw new TypeError('lastResponseStats firstTokenTime must be a finite number or null');
97
125
  }
@@ -107,6 +135,11 @@ function parseState(value) {
107
135
  cacheReadTokens,
108
136
  cacheWriteTokens,
109
137
  decodeMs,
138
+ ttftMs,
139
+ totalMs,
140
+ stepStartTime,
141
+ stepStartTurn,
142
+ stepStartStep,
110
143
  firstTokenTime,
111
144
  completedTime,
112
145
  };
@@ -234,6 +267,20 @@ function usageOf(event) {
234
267
  * @returns {typeof EMPTY_STATE}
235
268
  */
236
269
  function fold(state, event) {
270
+ if (event.type === 'step/start') {
271
+ if (!isFiniteNumber(event.time)) return state;
272
+ const turn = event.data.turn;
273
+ const step = event.data.step;
274
+ if (!Number.isSafeInteger(turn) || turn < 0 || !Number.isSafeInteger(step) || step < 0) {
275
+ return state;
276
+ }
277
+ return {
278
+ ...state,
279
+ stepStartTime: event.time,
280
+ stepStartTurn: turn,
281
+ stepStartStep: step,
282
+ };
283
+ }
237
284
  if (event.type !== 'assistant/message' || event.surfaceOp !== 'append') return state;
238
285
  if (event.seq <= state.seq) return state;
239
286
  const message = event.data.message;
@@ -252,7 +299,20 @@ function fold(state, event) {
252
299
  firstTokenTime !== null && completedTime > firstTokenTime
253
300
  ? completedTime - firstTokenTime
254
301
  : null;
302
+ const hasStepStart =
303
+ state.stepStartTime !== null &&
304
+ state.stepStartTurn === event.data.turn &&
305
+ state.stepStartStep === event.data.step;
306
+ const ttftMs =
307
+ hasStepStart && firstTokenTime !== null && firstTokenTime >= state.stepStartTime
308
+ ? firstTokenTime - state.stepStartTime
309
+ : null;
310
+ const totalMs =
311
+ hasStepStart && completedTime >= state.stepStartTime
312
+ ? completedTime - state.stepStartTime
313
+ : null;
255
314
  return {
315
+ ...state,
256
316
  seq: event.seq,
257
317
  turn: event.data.turn,
258
318
  step: event.data.step,
@@ -261,6 +321,8 @@ function fold(state, event) {
261
321
  cacheReadTokens,
262
322
  cacheWriteTokens,
263
323
  decodeMs,
324
+ ttftMs,
325
+ totalMs,
264
326
  firstTokenTime,
265
327
  completedTime,
266
328
  };
@@ -276,7 +338,7 @@ export function apply(ctx) {
276
338
  ctx.sessionProjections.register({
277
339
  key: 'lastResponseStats',
278
340
  stateSchema,
279
- stateVersion: 2,
341
+ stateVersion: 3,
280
342
  init: () => EMPTY_STATE,
281
343
  apply: fold,
282
344
  wire: {
package/locale/en.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "meta": {
3
3
  "title": "Last Response Stats",
4
- "description": "Adds average output tok/s and prompt-cache hit under the most recent Chat response."
4
+ "description": "Adds output tok/s, prompt-cache hit, and TTFT under the most recent Chat response."
5
5
  }
6
6
  }
package/locale/zh.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "meta": {
3
3
  "title": "最近回复统计",
4
- "description": "在聊天中最近一次回复下方显示平均输出 tok/s 和提示词缓存命中率。"
4
+ "description": "在聊天中最近一次回复下方显示输出 tok/s、提示词缓存命中率和首 token 延迟。"
5
5
  }
6
6
  }
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "dsh-last-response-stats",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "type": "module",
5
- "description": "Shows average output tok/s and prompt-cache hit for the most recent Chat response.",
5
+ "description": "Shows output speed, prompt-cache hit, and time to first token for the most recent Chat response.",
6
6
  "author": "Borivoje Latinovic",
7
7
  "license": "MIT",
8
8
  "keywords": [
@@ -10,7 +10,8 @@
10
10
  "deepseek-harness",
11
11
  "dsh-plugin",
12
12
  "tokens-per-second",
13
- "prompt-cache"
13
+ "prompt-cache",
14
+ "ttft"
14
15
  ],
15
16
  "engines": {
16
17
  "dsh": ">=0.2.1-rc.2 <0.3.0"