@laoyuehanni/dsh-token-usage 0.4.9 → 0.4.11

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.
Files changed (175) hide show
  1. package/README.md +12 -5
  2. package/README.zh.md +12 -5
  3. package/lib/aux-request.d.ts +10 -5
  4. package/lib/aux-request.d.ts.map +1 -1
  5. package/lib/aux-request.js +13 -9
  6. package/lib/aux-request.js.map +1 -1
  7. package/lib/client/DashboardCards.d.ts +62 -0
  8. package/lib/client/DashboardCards.d.ts.map +1 -0
  9. package/lib/client/DashboardCards.js +25 -0
  10. package/lib/client/DashboardCards.js.map +1 -0
  11. package/lib/client/OverheadRows.d.ts +30 -0
  12. package/lib/client/OverheadRows.d.ts.map +1 -0
  13. package/lib/client/OverheadRows.js +57 -0
  14. package/lib/client/OverheadRows.js.map +1 -0
  15. package/lib/client/OverviewBoard.d.ts +54 -0
  16. package/lib/client/OverviewBoard.d.ts.map +1 -0
  17. package/lib/client/OverviewBoard.js +35 -0
  18. package/lib/client/OverviewBoard.js.map +1 -0
  19. package/lib/client/PanellistMoreEntry.d.ts +29 -0
  20. package/lib/client/PanellistMoreEntry.d.ts.map +1 -0
  21. package/lib/client/PanellistMoreEntry.js +12 -0
  22. package/lib/client/PanellistMoreEntry.js.map +1 -0
  23. package/lib/client/PanellistPinEntry.d.ts +21 -0
  24. package/lib/client/PanellistPinEntry.d.ts.map +1 -0
  25. package/lib/client/PanellistPinEntry.js +24 -0
  26. package/lib/client/PanellistPinEntry.js.map +1 -0
  27. package/lib/client/QuotaButton.d.ts +1 -0
  28. package/lib/client/QuotaButton.d.ts.map +1 -1
  29. package/lib/client/QuotaButton.js +69 -17
  30. package/lib/client/QuotaButton.js.map +1 -1
  31. package/lib/client/RequestPool.d.ts +41 -0
  32. package/lib/client/RequestPool.d.ts.map +1 -0
  33. package/lib/client/RequestPool.js +176 -0
  34. package/lib/client/RequestPool.js.map +1 -0
  35. package/lib/client/StatCard.d.ts +58 -5
  36. package/lib/client/StatCard.d.ts.map +1 -1
  37. package/lib/client/StatCard.js +27 -8
  38. package/lib/client/StatCard.js.map +1 -1
  39. package/lib/client/TokenActivity.d.ts +21 -0
  40. package/lib/client/TokenActivity.d.ts.map +1 -0
  41. package/lib/client/TokenActivity.js +71 -0
  42. package/lib/client/TokenActivity.js.map +1 -0
  43. package/lib/client/TokenUsageCard.d.ts.map +1 -1
  44. package/lib/client/TokenUsageCard.js +1 -1
  45. package/lib/client/TokenUsageCard.js.map +1 -1
  46. package/lib/client/TokenUsageSection.d.ts +13 -34
  47. package/lib/client/TokenUsageSection.d.ts.map +1 -1
  48. package/lib/client/TokenUsageSection.js +81 -93
  49. package/lib/client/TokenUsageSection.js.map +1 -1
  50. package/lib/client/TrendChart.d.ts +2 -1
  51. package/lib/client/TrendChart.d.ts.map +1 -1
  52. package/lib/client/TrendChart.js +31 -10
  53. package/lib/client/TrendChart.js.map +1 -1
  54. package/lib/client/UsageChart.d.ts +20 -0
  55. package/lib/client/UsageChart.d.ts.map +1 -0
  56. package/lib/client/UsageChart.js +26 -0
  57. package/lib/client/UsageChart.js.map +1 -0
  58. package/lib/client/UsagePanelPage.d.ts +37 -0
  59. package/lib/client/UsagePanelPage.d.ts.map +1 -0
  60. package/lib/client/UsagePanelPage.js +16 -0
  61. package/lib/client/UsagePanelPage.js.map +1 -0
  62. package/lib/client/UsageRows.d.ts +102 -0
  63. package/lib/client/UsageRows.d.ts.map +1 -0
  64. package/lib/client/UsageRows.js +261 -0
  65. package/lib/client/UsageRows.js.map +1 -0
  66. package/lib/client/UsageView.d.ts.map +1 -1
  67. package/lib/client/UsageView.js +79 -5
  68. package/lib/client/UsageView.js.map +1 -1
  69. package/lib/client/activity-grid.d.ts +42 -0
  70. package/lib/client/activity-grid.d.ts.map +1 -0
  71. package/lib/client/activity-grid.js +86 -0
  72. package/lib/client/activity-grid.js.map +1 -0
  73. package/lib/client/async-resource.d.ts +8 -0
  74. package/lib/client/async-resource.d.ts.map +1 -1
  75. package/lib/client/async-resource.js +22 -4
  76. package/lib/client/async-resource.js.map +1 -1
  77. package/lib/client/card-form.d.ts +15 -0
  78. package/lib/client/card-form.d.ts.map +1 -1
  79. package/lib/client/card-form.js +27 -5
  80. package/lib/client/card-form.js.map +1 -1
  81. package/lib/client/cost-split.d.ts +26 -0
  82. package/lib/client/cost-split.d.ts.map +1 -0
  83. package/lib/client/cost-split.js +37 -0
  84. package/lib/client/cost-split.js.map +1 -0
  85. package/lib/client/format.d.ts +31 -1
  86. package/lib/client/format.d.ts.map +1 -1
  87. package/lib/client/format.js +48 -0
  88. package/lib/client/format.js.map +1 -1
  89. package/lib/client/index.d.ts.map +1 -1
  90. package/lib/client/index.js +102 -0
  91. package/lib/client/index.js.map +1 -1
  92. package/lib/client/locales.d.ts +100 -1
  93. package/lib/client/locales.d.ts.map +1 -1
  94. package/lib/client/locales.js +200 -2
  95. package/lib/client/locales.js.map +1 -1
  96. package/lib/client/project-rows.d.ts +4 -0
  97. package/lib/client/project-rows.d.ts.map +1 -0
  98. package/lib/client/project-rows.js +3 -0
  99. package/lib/client/project-rows.js.map +1 -0
  100. package/lib/client/quota-format.d.ts +6 -4
  101. package/lib/client/quota-format.d.ts.map +1 -1
  102. package/lib/client/quota-format.js +7 -5
  103. package/lib/client/quota-format.js.map +1 -1
  104. package/lib/client.js +3108 -1521
  105. package/lib/client.js.map +1 -1
  106. package/lib/index.d.ts +6 -0
  107. package/lib/index.d.ts.map +1 -1
  108. package/lib/index.js +60 -10
  109. package/lib/index.js.map +1 -1
  110. package/lib/project-rows.d.ts +22 -0
  111. package/lib/project-rows.d.ts.map +1 -0
  112. package/lib/project-rows.js +82 -0
  113. package/lib/project-rows.js.map +1 -0
  114. package/lib/quota/adapters/kimi.d.ts +8 -6
  115. package/lib/quota/adapters/kimi.d.ts.map +1 -1
  116. package/lib/quota/adapters/kimi.js +64 -18
  117. package/lib/quota/adapters/kimi.js.map +1 -1
  118. package/lib/quota/adapters/magpie.d.ts +84 -0
  119. package/lib/quota/adapters/magpie.d.ts.map +1 -0
  120. package/lib/quota/adapters/magpie.js +290 -0
  121. package/lib/quota/adapters/magpie.js.map +1 -0
  122. package/lib/quota/provider-tracking.d.ts +15 -4
  123. package/lib/quota/provider-tracking.d.ts.map +1 -1
  124. package/lib/quota/provider-tracking.js +18 -7
  125. package/lib/quota/provider-tracking.js.map +1 -1
  126. package/lib/quota/quota-route.d.ts +3 -3
  127. package/lib/quota/quota-route.d.ts.map +1 -1
  128. package/lib/quota/quota-route.js +3 -3
  129. package/lib/quota/quota-route.js.map +1 -1
  130. package/lib/quota/quota-service.d.ts +14 -4
  131. package/lib/quota/quota-service.d.ts.map +1 -1
  132. package/lib/quota/quota-service.js +45 -15
  133. package/lib/quota/quota-service.js.map +1 -1
  134. package/lib/quota/registry.d.ts.map +1 -1
  135. package/lib/quota/registry.js +6 -0
  136. package/lib/quota/registry.js.map +1 -1
  137. package/lib/quota/types.d.ts +9 -0
  138. package/lib/quota/types.d.ts.map +1 -1
  139. package/lib/quota/types.js.map +1 -1
  140. package/lib/request-details.d.ts +106 -0
  141. package/lib/request-details.d.ts.map +1 -0
  142. package/lib/request-details.js +227 -0
  143. package/lib/request-details.js.map +1 -0
  144. package/lib/request-observe.d.ts +181 -0
  145. package/lib/request-observe.d.ts.map +1 -0
  146. package/lib/request-observe.js +495 -0
  147. package/lib/request-observe.js.map +1 -0
  148. package/lib/rollup.d.ts +18 -0
  149. package/lib/rollup.d.ts.map +1 -1
  150. package/lib/rollup.js +12 -0
  151. package/lib/rollup.js.map +1 -1
  152. package/lib/session-request-details.d.ts +50 -0
  153. package/lib/session-request-details.d.ts.map +1 -0
  154. package/lib/session-request-details.js +325 -0
  155. package/lib/session-request-details.js.map +1 -0
  156. package/lib/stats-route.d.ts +9 -1
  157. package/lib/stats-route.d.ts.map +1 -1
  158. package/lib/stats-route.js +34 -3
  159. package/lib/stats-route.js.map +1 -1
  160. package/lib/stats.d.ts +7 -0
  161. package/lib/stats.d.ts.map +1 -1
  162. package/lib/stats.js +87 -9
  163. package/lib/stats.js.map +1 -1
  164. package/lib/sync.d.ts.map +1 -1
  165. package/lib/sync.js +1 -0
  166. package/lib/sync.js.map +1 -1
  167. package/lib/usage-record.d.ts +35 -5
  168. package/lib/usage-record.d.ts.map +1 -1
  169. package/lib/usage-record.js +33 -0
  170. package/lib/usage-record.js.map +1 -1
  171. package/lib/wire.d.ts +182 -9
  172. package/lib/wire.d.ts.map +1 -1
  173. package/lib/wire.js +8 -0
  174. package/lib/wire.js.map +1 -1
  175. package/package.json +30 -26
@@ -16,12 +16,16 @@
16
16
  * @module token-usage/quota/quota-service
17
17
  */
18
18
  import { resolveQuotaAdapter } from "./registry.js";
19
+ import { magpieAdapter, MagpieDetector } from "./adapters/magpie.js";
19
20
  import { QuotaQueryError } from "./types.js";
20
21
  import { withCatalogBaseUrl } from "./credentials.js";
21
22
  /** Default cache lifetime of one provider's ok result. */
22
23
  const DEFAULT_CACHE_TTL_MS = 45_000;
23
24
  /** Per-query deadline: quota endpoints are small; anything slower is dead. */
24
25
  const QUERY_TIMEOUT_MS = 10_000;
26
+ /** The gateway probe's own deadline — a hello that takes longer than this
27
+ * is a dead route, not a magpie. */
28
+ const PROBE_TIMEOUT_MS = 5_000;
25
29
  /** Map any thrown adapter failure onto the wire's error vocabulary. */
26
30
  function toQuotaFailure(provider, providerName, adapter, error, intervalSec, now) {
27
31
  const normalized = error instanceof QuotaQueryError
@@ -54,8 +58,10 @@ export class QuotaService {
54
58
  #deps;
55
59
  #cache = new Map();
56
60
  #inflight = new Map();
61
+ #magpieDetector;
57
62
  constructor(deps) {
58
63
  this.#deps = deps;
64
+ this.#magpieDetector = deps.magpieDetector ?? new MagpieDetector(deps.now ?? Date.now);
59
65
  }
60
66
  /** The poll cadence every payload carries. */
61
67
  get intervalSec() {
@@ -70,20 +76,39 @@ export class QuotaService {
70
76
  * @param providerHint - the provider the session's model chip currently
71
77
  * selects (the host-reported NEXT selection), when the browser could read
72
78
  * one. It wins over the request tracker: the chip is the user's live
73
- * intent, and the next `request/context` event makes the tracker agree.
79
+ * intent.
80
+ * @param modelHint - that chip selection's model, paired with the live
81
+ * provider. Without a provider hint, only the tracker's model is trusted.
74
82
  */
75
- async snapshot(sessionId, providerHint) {
76
- const provider = providerHint !== undefined && providerHint !== ''
77
- ? providerHint
78
- : this.#deps.resolveProvider(sessionId);
83
+ async snapshot(sessionId, providerHint, modelHint) {
84
+ const sighting = this.#deps.resolveSighting(sessionId);
85
+ const hintLive = providerHint !== undefined && providerHint !== '';
86
+ const provider = hintLive ? providerHint : sighting?.provider;
79
87
  if (provider === undefined) {
80
88
  return { status: 'no-provider', intervalSec: this.intervalSec };
81
89
  }
90
+ const model = hintLive && modelHint !== undefined && modelHint !== ''
91
+ ? modelHint
92
+ : sighting !== undefined && sighting.provider === provider ? sighting.model : undefined;
82
93
  const credentials = withCatalogBaseUrl(provider, await this.#deps.resolveCredentials(provider));
83
- const adapter = resolveQuotaAdapter({
94
+ let adapter = resolveQuotaAdapter({
84
95
  provider,
85
96
  ...(credentials.baseUrl !== undefined ? { baseUrl: credentials.baseUrl } : {}),
86
97
  });
98
+ if (adapter === undefined && credentials.baseUrl !== undefined) {
99
+ // A route no table knows, pointing at a base URL: ask the magpie
100
+ // hello — a user-declared gateway profile is exactly the shape the
101
+ // host table cannot enumerate. The detector caches both verdicts,
102
+ // so this stays off the hot path.
103
+ const hit = await this.#magpieDetector.detect({
104
+ baseUrl: credentials.baseUrl,
105
+ ...(credentials.apiKey !== undefined ? { apiKey: credentials.apiKey } : {}),
106
+ fetchFn: this.#deps.fetchFn ?? globalThis.fetch,
107
+ signal: AbortSignal.timeout(PROBE_TIMEOUT_MS),
108
+ });
109
+ if (hit)
110
+ adapter = magpieAdapter;
111
+ }
87
112
  if (adapter === undefined) {
88
113
  return {
89
114
  status: 'unsupported',
@@ -107,29 +132,34 @@ export class QuotaService {
107
132
  },
108
133
  };
109
134
  }
110
- const cached = this.#cached(provider);
135
+ // The cache key includes the model: a gateway adapter's windows are a
136
+ // function of both (the model scopes the cards), so two sessions on
137
+ // the same route but different models keep their own snapshots.
138
+ const cacheKey = `${provider}\u0000${model ?? ''}`;
139
+ const cached = this.#cached(cacheKey);
111
140
  if (cached !== undefined)
112
141
  return cached;
113
- const running = this.#inflight.get(provider);
142
+ const running = this.#inflight.get(cacheKey);
114
143
  if (running !== undefined)
115
144
  return running;
116
- const query = this.#query(provider, providerName, credentials, adapter);
117
- this.#inflight.set(provider, query);
145
+ const query = this.#query(provider, model, providerName, credentials, adapter);
146
+ this.#inflight.set(cacheKey, query);
118
147
  try {
119
148
  return await query;
120
149
  }
121
150
  finally {
122
- this.#inflight.delete(provider);
151
+ this.#inflight.delete(cacheKey);
123
152
  }
124
153
  }
125
154
  /** The fresh-or-cached ok result, or a live error, of one provider query. */
126
- async #query(provider, providerName, credentials, adapter) {
155
+ async #query(provider, model, providerName, credentials, adapter) {
127
156
  try {
128
157
  const result = await adapter.query({
129
158
  provider,
130
159
  ...(credentials.baseUrl !== undefined ? { baseUrl: credentials.baseUrl } : {}),
131
160
  apiKey: credentials.apiKey ?? '',
132
- signal: AbortSignal.timeout(QUERY_TIMEOUT_MS),
161
+ ...(model !== undefined && model !== '' ? { model } : {}),
162
+ signal: AbortSignal.timeout(adapter.queryTimeoutMs ?? QUERY_TIMEOUT_MS),
133
163
  ...(this.#deps.fetchFn !== undefined ? { fetchFn: this.#deps.fetchFn } : { fetchFn: globalThis.fetch }),
134
164
  ...(this.#deps.readAccountBalance !== undefined ? { readAccountBalance: this.#deps.readAccountBalance } : {}),
135
165
  });
@@ -143,12 +173,12 @@ export class QuotaService {
143
173
  ...(result.planTier !== undefined ? { planTier: result.planTier } : {}),
144
174
  windows: result.windows,
145
175
  };
146
- this.#cache.set(provider, { at: this.#now(), payload });
176
+ this.#cache.set(`${provider}\u0000${model ?? ''}`, { at: this.#now(), payload });
147
177
  return payload;
148
178
  }
149
179
  catch (error) {
150
180
  // Errors do not cache: the panel's retry must actually re-query.
151
- this.#cache.delete(provider);
181
+ this.#cache.delete(`${provider}\u0000${model ?? ''}`);
152
182
  return toQuotaFailure(provider, providerName, adapter, error, this.intervalSec, this.#now());
153
183
  }
154
184
  }
@@ -1 +1 @@
1
- {"version":3,"file":"quota-service.js","sourceRoot":"","sources":["../../src/quota/quota-service.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAGH,OAAO,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAA;AAEnD,OAAO,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAC5C,OAAO,EAAE,kBAAkB,EAAiC,MAAM,kBAAkB,CAAA;AAGpF,0DAA0D;AAC1D,MAAM,oBAAoB,GAAG,MAAM,CAAA;AACnC,8EAA8E;AAC9E,MAAM,gBAAgB,GAAG,MAAM,CAAA;AA0B/B,uEAAuE;AACvE,SAAS,cAAc,CACrB,QAAgB,EAChB,YAAgC,EAChC,OAAqB,EACrB,KAAc,EACd,WAAmB,EACnB,GAAW;IAEX,MAAM,UAAU,GAAG,KAAK,YAAY,eAAe;QACjD,CAAC,CAAC,EAAE,IAAI,EAAE,KAAK,CAAC,IAAI,EAAE,OAAO,EAAE,KAAK,CAAC,OAAO,EAAE;QAC9C,CAAC,CAAC,EAAE,IAAI,EAAE,OAAgB,EAAE,OAAO,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAA;IAC/F,OAAO;QACL,MAAM,EAAE,OAAO;QACf,QAAQ;QACR,GAAG,CAAC,YAAY,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,YAAY,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QACvD,SAAS,EAAE,OAAO,CAAC,EAAE;QACrB,SAAS,EAAE,GAAG;QACd,WAAW;QACX,KAAK,EAAE,UAAU;KAClB,CAAA;AACH,CAAC;AAED;;;;;;GAMG;AACH,SAAS,a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AAI,KAAK,KAAK,SAAS;YAAE,OAAO,SAAS,CAAA;QACzC,MAAM,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,UAAU,IAAI,oBAAoB,CAAA;QACzD,IAAI,IAAI,CAAC,IAAI,EAAE,GAAG,KAAK,CAAC,EAAE,IAAI,GAAG,EAAE,CAAC;YAClC,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAA;YAC5B,OAAO,SAAS,CAAA;QAClB,CAAC;QACD,OAAO,KAAK,CAAC,OAAO,CAAA;IACtB,CAAC;IAED,yBAAyB;IACzB,IAAI;QACF,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,EAAE,EAAE,IAAI,IAAI,CAAC,GAAG,EAAE,CAAA;IACzC,CAAC;CACF"}
@@ -1 +1 @@
1
- {"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../../src/quota/registry.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAUH,OAAO,KAAK,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAE/D,mDAAmD;AACnD,eAAO,MAAM,cAAc,EAAE,SAAS,YAAY,EAWjD,CAAA;AAED;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,eAAe,GAAG,YAAY,GAAG,SAAS,CAEpF"}
1
+ {"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../../src/quota/registry.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAWH,OAAO,KAAK,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAE/D,mDAAmD;AACnD,eAAO,MAAM,cAAc,EAAE,SAAS,YAAY,EAgBjD,CAAA;AAED;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,eAAe,GAAG,YAAY,GAAG,SAAS,CAEpF"}
@@ -14,6 +14,7 @@ import { commandCodeAdapter } from "./adapters/command-code.js";
14
14
  import { deepseekAccountAdapter } from "./adapters/deepseek-account.js";
15
15
  import { deepseekBalanceAdapter } from "./adapters/deepseek-balance.js";
16
16
  import { kimiAdapter } from "./adapters/kimi.js";
17
+ import { magpieAdapter } from "./adapters/magpie.js";
17
18
  import { minimaxAdapter } from "./adapters/minimax.js";
18
19
  import { opencodeGoAdapter } from "./adapters/opencode-go.js";
19
20
  import { openrouterAdapter } from "./adapters/openrouter.js";
@@ -30,6 +31,11 @@ export const QUOTA_ADAPTERS = [
30
31
  deepseekAccountAdapter,
31
32
  deepseekBalanceAdapter,
32
33
  openrouterAdapter,
34
+ // Last: the magpie adapter claims only its route key here — a gateway
35
+ // route is a user-declared name at an arbitrary host, so the service
36
+ // resolves those through the MagpieDetector probe when this table (and
37
+ // every host-based match above) finds nothing.
38
+ magpieAdapter,
33
39
  ];
34
40
  /**
35
41
  * The adapter handling one provider route, or undefined when none does
@@ -1 +1 @@
1
- {"version":3,"file":"registry.js","sourceRoot":"","sources":["../../src/quota/registry.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EAAE,kBAAkB,EAAE,MAAM,4BAA4B,CAAA;AAC/D,OAAO,EAAE,sBAAsB,EAAE,MAAM,gCAAgC,CAAA;AACvE,OAAO,EAAE,sBAAsB,EAAE,MAAM,gCAAgC,CAAA;AACvE,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAA;AAChD,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAA;AACtD,OAAO,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAA;AAC7D,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAA;AAC5D,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAA;AAGlD,mDAAmD;AACnD,MAAM,CAAC,MAAM,cAAc,GAA4B;IACrD,YAAY;IACZ,WAAW;IACX,cAAc;IACd,iBAAiB;IACjB,kBAAkB;IAClB,oEAAoE;IACpE,yDAAyD;IACzD,sBAAsB;IACtB,sBAA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48,10 @@ export interface QuotaQueryContext {
48
48
  /** The provider's API key, resolved through the credentials seam.
49
49
  * Empty when the adapter does not use an API key. */
50
50
  apiKey: string;
51
+ /** The model id the asking session's request used (or the model chip
52
+ * selected), when one is known — adapters that aggregate a whole
53
+ * gateway's upstreams (magpie) filter their cards by it. */
54
+ model?: string;
51
55
  /** Deadline signal; the service stamps one per query. */
52
56
  signal?: AbortSignal;
53
57
  /** Transport to use — global fetch in production, a stub in tests. */
@@ -88,6 +92,11 @@ export interface QuotaAdapter {
88
92
  * kimi adapter matches on host or the catalog route `kimi-coding`).
89
93
  */
90
94
  matches(input: QuotaMatchInput): boolean;
95
+ /** Per-query deadline override, ms. Absent reads the service default —
96
+ * an adapter whose gateway relays to upstreams with a longer deadline
97
+ * of their own (magpie's 12s fan-out) raises it so the client does not
98
+ * cut the relay off first. */
99
+ queryTimeoutMs?: number;
91
100
  /** Query the provider's quota endpoint and normalize the windows.
92
101
  * Transport failures throw {@link QuotaQueryError} with the normalized
93
102
  * kind; anything else is treated as a parse failure by the service. */
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/quota/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AAE7D,qEAAqE;AACrE,MAAM,WAAW,eAAe;IAC9B;;sDAEkD;IAClD,QAAQ,EAAE,MAAM,CAAA;IAChB;;0CAEsC;IACtC,OAAO,CAAC,EAAE,MAAM,CAAA;CACjB;AAED;iEACiE;AACjE,MAAM,WAAW,kBAAkB;IACjC,QAAQ,EAAE,MAAM,CAAA;IAChB,OAAO,EAAE,MAAM,CAAA;CAChB;AAED,4EAA4E;AAC5E,MAAM,WAAW,mBAAmB;IAClC,MAAM,EAAE,OAAO,GAAG,QAAQ,CAAA;IAC1B,KAAK,CAAC,EAAE,SAAS,kBAAkB,EAAE,CAAA;IACrC,YAAY,CAAC,EA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kEAAkE;IAClE,EAAE,EAAE,MAAM,CAAA;IACV;wEACoE;IACpE,KAAK,EAAE,MAAM,CAAA;IACb;;;;wEAIoE;IACpE,MAAM,CAAC,EAAE,SAAS,MAAM,EAAE,CAAA;IAC1B;;oDAEgD;IAChD,UAAU,CAAC,EAAE,OAAO,CAAA;IACpB;;;;;;;;OAQG;IACH,OAAO,CAAC,KAAK,EAAE,eAAe,GAAG,OAAO,CAAA;IACxC;;;kCAG8B;IAC9B,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB;;2EAEuE;IACvE,KAAK,CAAC,GAAG,EAAE,iBAAiB,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAAA;CACzD;AAED,2DAA2D;AAC3D,qBAAa,eAAgB,SAAQ,KAAK;IAC5B,QAAQ,CAAC,IAAI,EAAE,cAAc;gBAApB,IAAI,EAAE,cAAc,EAAE,OAAO,EAAE,MAAM;CAI3D"}
@@ -1 +1 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/quota/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AA2FH,2DAA2D;AAC3D,MAAM,OAAO,eAAgB,SAAQ,KAAK;IACnB;IAArB,YAAqB,IAAoB,EAAE,OAAe;QACxD,KAAK,CAAC,OAAO,CAAC,CAAA;QADK,SAAI,GAAJ,IAAI,CAAgB;QAEvC,IAAI,CAAC,IAAI,GAAG,iBAAiB,CAAA;IAC/B,CAAC;CACF"}
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/quota/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAmGH,2DAA2D;AAC3D,MAAM,OAAO,eAAgB,SAAQ,KAAK;IACnB;IAArB,YAAqB,IAAoB,EAAE,OAAe;QACxD,KAAK,CAAC,OAAO,CAAC,CAAA;QADK,SAAI,GAAJ,IAAI,CAAgB;QAEvC,IAAI,CAAC,IAAI,GAAG,iBAAiB,CAAA;IAC/B,CAAC;CACF"}
@@ -0,0 +1,106 @@
1
+ /**
2
+ * Pure rules of the conversation usage tab's live request pool (最新请求 /
3
+ * 实时请求): the settled per-request speed gate, the per-request cache
4
+ * assessment (the 50%/80% request-area policy with every exclusion reason),
5
+ * and the route-sequence walk that marks a session's first request and
6
+ * model-switch firsts. No I/O and no runtime imports beyond types, so the
7
+ * stats route, its tests, and any future consumer share one implementation.
8
+ *
9
+ * The assessment NEVER touches the global hit-rate color scale
10
+ * (client/format.ts bandOf) — that one keeps summarizing whole sessions and
11
+ * models; this module judges one request at a time.
12
+ *
13
+ * @module token-usage/request-details
14
+ */
15
+ import type { RequestCacheExclusion, RequestDetailCache, RequestDetailState } from './wire.ts';
16
+ /** Inputs of one request's cache assessment: the merged attempt's trusted
17
+ * facts, plus the route-sequence exclusion the caller walked beforehand. */
18
+ export interface CacheAssessInput {
19
+ state: RequestDetailState;
20
+ kind: 'request' | 'compaction' | 'failure' | 'aux';
21
+ /** Provider route key, used only for the documented minimum-input rule. */
22
+ provider?: string;
23
+ /** Uncached input count reported by the provider. */
24
+ inputTokens?: number;
25
+ outputTokens?: number;
26
+ cacheReadTokens?: number;
27
+ /** `cold-start` / `model-switch` when the route sequence walk excluded
28
+ * this request; other exclusions derive from the fields above. */
29
+ sequenceExclusion?: 'cold-start' | 'model-switch';
30
+ }
31
+ /**
32
+ * Assess one request's cache hit for the request-area policy. The rules,
33
+ * in evaluation order:
34
+ *
35
+ * 1. **State**: waiting/streaming rows are `in-flight`; failed / cancelled /
36
+ * interrupted rows are `not-success`; compaction rows are excluded by
37
+ * their kind. None of them ever carry the loss label.
38
+ * 2. **Data**: a missing/invalid input side, or a denominator that is zero /
39
+ * non-finite, renders the rate absent — never 0%.
40
+ * 3. **Reporting**: the cache-read field must have been explicitly reported
41
+ * (a 0 included); a missing field is `cache-unreported`, never 0%.
42
+ * 4. **Sequence**: the session's first request (`cold-start`) and the first
43
+ * request after a model change (`model-switch`) are excluded by the
44
+ * caller's walk — regardless of their rate.
45
+ * 5. **Input qualification**: on the documented DeepSeek routes only, a
46
+ * cacheable input side below one 64-token block is `insufficient-input`.
47
+ * 6. **Policy bands**: all routes with valid reported data use rate < 50% → `loss` (the 「缓存过低」 label),
48
+ * 50% ≤ rate < 80% → `low` (no label), ≥ 80% → `normal`.
49
+ * Numeric colours follow the shared summary-card scale independently.
50
+ * Boundaries use the raw value: exactly 80% is `normal`, exactly 50% is
51
+ * `low` (no label).
52
+ *
53
+ * A `loss` band is a low-hit heuristic hint, never proof of a server-side
54
+ * cache failure — the client copy says so.
55
+ */
56
+ export declare function assessRequestCache(input: CacheAssessInput): RequestDetailCache;
57
+ /**
58
+ * One entry of a session's request sequence, in start order: the session it
59
+ * belongs to, the model name the route/display folds under (the effective
60
+ * model — response model when the adapter reported one), and the provider
61
+ * route key when the row carries one.
62
+ */
63
+ export interface RouteSequenceEntry {
64
+ sessionId: string;
65
+ model: string;
66
+ kind?: 'request' | 'compaction' | 'failure' | 'aux';
67
+ /** Provider route key; absent on legacy rows (recorded before the field
68
+ * existed) and on failure rows whose session never named a route. */
69
+ provider?: string;
70
+ }
71
+ /**
72
+ * Walk a session's request sequence in start order and mark the exclusion
73
+ * each entry's cache label would carry: the session's very first request is
74
+ * a `cold-start`, and any entry whose immediate predecessor in the same
75
+ * session named a different route is a `model-switch`. The route identity is
76
+ * provider + model when BOTH sides carry a provider (the cache lineage is
77
+ * per-route: switching `deepseek-official` → `deepseek-account` at the same
78
+ * model name starts a new cache context); when either side lacks the
79
+ * provider key (legacy rows, unattributed failures) the walk falls back to
80
+ * the model-only comparison those rows have always had — an absent key never
81
+ * widens the exclusion. Switching BACK to an earlier route is still a switch
82
+ * first — conservative. Entries outside this vocabulary (unknown future
83
+ * reasons) yield undefined. The walk always covers the FULL sequence the
84
+ * caller filtered to scope — never just the display window — so a first
85
+ * request that fell out of the 20-item pool is still correctly marked, and
86
+ * the marks do not move as the window slides.
87
+ *
88
+ * Predecessors are by adjacent conversation request regardless of kind: a
89
+ * compaction summarize call is a real provider request of the same session
90
+ * and takes part in the cache lineage. Auxiliary work is a separate prompt
91
+ * lineage and is skipped.
92
+ */
93
+ export declare function routeExclusionsOf(entries: readonly RouteSequenceEntry[]): ReadonlyMap<object, 'cold-start' | 'model-switch'>;
94
+ /**
95
+ * The settled single-request decode speed: output tokens over the generation
96
+ * span (latency − first token), in tokens/second. The gate mirrors the
97
+ * global performance accumulators: the generation span must be a real
98
+ * streaming sample (≥ {@link PERF_STREAM_MIN_GEN_MS}) and the output must be
99
+ * positive. A whole-body (non-streaming) response has no first-token figure
100
+ * and stays undefined — its tiny arrival interval would fabricate enormous
101
+ * tok/s. Never returns Infinity/NaN.
102
+ */
103
+ export declare function settledSpeedOf(outputTokens: number | undefined, latencyMs: number | undefined, firstTokenLatencyMs: number | undefined): number | undefined;
104
+ /** All exclusion reasons, for locale-key coverage tests. */
105
+ export declare const CACHE_EXCLUSION_REASONS: readonly RequestCacheExclusion[];
106
+ //# sourceMappingURL=request-details.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"request-details.d.ts","sourceRoot":"","sources":["../src/request-details.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAGH,OAAO,KAAK,EAAE,qBAAqB,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,MAAM,WAAW,CAAA;AAc9F;4EAC4E;AAC5E,MAAM,WAAW,gBAAgB;IAC/B,KAAK,EAAE,kBAAkB,CAAA;IACzB,IAAI,EAAE,SAAS,GAAG,YAAY,GAAG,SAAS,GAAG,KAAK,CAAA;IAClD,2EAA2E;IAC3E,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,qDAAqD;IACrD,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,eAAe,CAAC,EAAE,MAAM,CAAA;IACxB;sEACkE;IAClE,iBAAiB,CAAC,EAAE,YAAY,GAAG,cAAc,CAAA;CAClD;AAgBD;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,gBAAgB,GAAG,kBAAkB,CA0E9E;AAED;;;;;GAKG;AACH,MAAM,WAAW,kBAAkB;IACjC,SAAS,EAAE,MAAM,CAAA;IACjB,KAAK,EAAE,MAAM,CAAA;IACb,IAAI,CAAC,EAAE,SAAS,GAAG,YAAY,GAAG,SAAS,GAAG,KAAK,CAAA;IACnD;yEACqE;IACrE,QAAQ,CAAC,EAAE,MAAM,CAAA;CAClB;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,iBAAiB,CAC/B,OAAO,EAAE,SAAS,kBAAkB,EAAE,GACrC,WAAW,CAAC,MAAM,EAAE,YAAY,GAAG,cAAc,CAAC,CA8BpD;AAED;;;;;;;;GAQG;AACH,wBAAgB,cAAc,CAC5B,YAAY,EAAE,MAAM,GAAG,SAAS,EAChC,SAAS,EAAE,MAAM,GAAG,SAAS,EAC7B,mBAAmB,EAAE,MAAM,GAAG,SAAS,GACtC,MAAM,GAAG,SAAS,CAQpB;AAED,4DAA4D;AAC5D,eAAO,MAAM,uBAAuB,EAAE,SAAS,qBAAqB,EAUnE,CAAA"}
@@ -0,0 +1,227 @@
1
+ /**
2
+ * Pure rules of the conversation usage tab's live request pool (最新请求 /
3
+ * 实时请求): the settled per-request speed gate, the per-request cache
4
+ * assessment (the 50%/80% request-area policy with every exclusion reason),
5
+ * and the route-sequence walk that marks a session's first request and
6
+ * model-switch firsts. No I/O and no runtime imports beyond types, so the
7
+ * stats route, its tests, and any future consumer share one implementation.
8
+ *
9
+ * The assessment NEVER touches the global hit-rate color scale
10
+ * (client/format.ts bandOf) — that one keeps summarizing whole sessions and
11
+ * models; this module judges one request at a time.
12
+ *
13
+ * @module token-usage/request-details
14
+ */
15
+ import { PERF_STREAM_MIN_GEN_MS } from "./stats.js";
16
+ /**
17
+ * DeepSeek context caching stores the prompt in 64-token blocks (provider
18
+ * documentation). A request on these routes whose whole
19
+ * cacheable input side is below one block cannot hit the cache, so its 0%
20
+ * is explained by the input itself — an exclusion with evidence, not a
21
+ * blanket minimum-token rule for every provider.
22
+ */
23
+ const DEEPSEEK_CACHE_BLOCK_TOKENS = 64;
24
+ /** Only these routes have a documented minimum; this is not an alert allowlist. */
25
+ const DEEPSEEK_BLOCK_PROVIDERS = new Set(['deepseek-official', 'deepseek-account']);
26
+ /**
27
+ * Whether the token figures are individually trustworthy counts: finite and
28
+ * non-negative. (There is no separate total-input figure to contradict —
29
+ * uncached input + cache reads IS the served input side, so no cross-field
30
+ * contradiction dimension exists beyond per-field validity.)
31
+ */
32
+ function countsAreValid(input) {
33
+ const { inputTokens, outputTokens, cacheReadTokens } = input;
34
+ if (inputTokens === undefined || !Number.isFinite(inputTokens) || inputTokens < 0)
35
+ return false;
36
+ if (outputTokens !== undefined && (!Number.isFinite(outputTokens) || outputTokens < 0))
37
+ return false;
38
+ if (cacheReadTokens !== undefined && (!Number.isFinite(cacheReadTokens) || cacheReadTokens < 0))
39
+ return false;
40
+ return true;
41
+ }
42
+ /**
43
+ * Assess one request's cache hit for the request-area policy. The rules,
44
+ * in evaluation order:
45
+ *
46
+ * 1. **State**: waiting/streaming rows are `in-flight`; failed / cancelled /
47
+ * interrupted rows are `not-success`; compaction rows are excluded by
48
+ * their kind. None of them ever carry the loss label.
49
+ * 2. **Data**: a missing/invalid input side, or a denominator that is zero /
50
+ * non-finite, renders the rate absent — never 0%.
51
+ * 3. **Reporting**: the cache-read field must have been explicitly reported
52
+ * (a 0 included); a missing field is `cache-unreported`, never 0%.
53
+ * 4. **Sequence**: the session's first request (`cold-start`) and the first
54
+ * request after a model change (`model-switch`) are excluded by the
55
+ * caller's walk — regardless of their rate.
56
+ * 5. **Input qualification**: on the documented DeepSeek routes only, a
57
+ * cacheable input side below one 64-token block is `insufficient-input`.
58
+ * 6. **Policy bands**: all routes with valid reported data use rate < 50% → `loss` (the 「缓存过低」 label),
59
+ * 50% ≤ rate < 80% → `low` (no label), ≥ 80% → `normal`.
60
+ * Numeric colours follow the shared summary-card scale independently.
61
+ * Boundaries use the raw value: exactly 80% is `normal`, exactly 50% is
62
+ * `low` (no label).
63
+ *
64
+ * A `loss` band is a low-hit heuristic hint, never proof of a server-side
65
+ * cache failure — the client copy says so.
66
+ */
67
+ export function assessRequestCache(input) {
68
+ const { state, kind } = input;
69
+ const assessment = { eligibility: 'unknown', band: 'unknown' };
70
+ // 1. State exclusions (before any rate is computed — an in-flight row has
71
+ // no trustworthy usage yet anyway).
72
+ if (state === 'waiting' || state === 'streaming') {
73
+ assessment.band = 'excluded';
74
+ assessment.reason = 'in-flight';
75
+ return assessment;
76
+ }
77
+ if (state !== 'completed') {
78
+ assessment.band = 'excluded';
79
+ assessment.reason = 'not-success';
80
+ return assessment;
81
+ }
82
+ if (kind === 'compaction') {
83
+ assessment.band = 'excluded';
84
+ assessment.reason = 'compaction';
85
+ return assessment;
86
+ }
87
+ if (kind === 'aux') {
88
+ assessment.band = 'excluded';
89
+ assessment.reason = 'auxiliary';
90
+ return assessment;
91
+ }
92
+ // 2. Data validity.
93
+ if (!countsAreValid(input)) {
94
+ assessment.band = 'unknown';
95
+ assessment.reason = 'invalid-data';
96
+ return assessment;
97
+ }
98
+ const served = input.inputTokens + (input.cacheReadTokens ?? 0);
99
+ if (!Number.isFinite(served) || served <= 0) {
100
+ assessment.band = 'unknown';
101
+ assessment.reason = 'invalid-data';
102
+ return assessment;
103
+ }
104
+ // 3. Explicit reporting. The field's PRESENCE is the evidence: the
105
+ // DeepSeek wire copies each field verbatim, so a reported 0 survives as
106
+ // cacheReadTokens: 0 (an assessable 0%), and a missing field means the
107
+ // provider did not report — never "0% hit". On the pi-ai routes a
108
+ // reported 0 was folded away at the adapter, which lands here the same
109
+ // way: unknown, never 0%.
110
+ if (input.cacheReadTokens === undefined) {
111
+ assessment.band = 'unknown';
112
+ assessment.reason = 'cache-unreported';
113
+ return assessment;
114
+ }
115
+ const rate = (input.cacheReadTokens ?? 0) / served;
116
+ assessment.rate = rate;
117
+ assessment.eligibility = 'eligible';
118
+ // 4. Route-sequence exclusions (the caller's walk).
119
+ if (input.sequenceExclusion !== undefined) {
120
+ assessment.band = 'excluded';
121
+ assessment.reason = input.sequenceExclusion;
122
+ return assessment;
123
+ }
124
+ // 5. Input qualification: below one cache block the 0% is explained by
125
+ // the input itself, not by a cache failure.
126
+ if (input.provider !== undefined && DEEPSEEK_BLOCK_PROVIDERS.has(input.provider)
127
+ && rate === 0 && served < DEEPSEEK_CACHE_BLOCK_TOKENS) {
128
+ assessment.band = 'excluded';
129
+ assessment.reason = 'insufficient-input';
130
+ return assessment;
131
+ }
132
+ // 6. Policy bands on the raw value, regardless of provider identity.
133
+ assessment.band = rate < 0.5 ? 'loss' : rate < 0.8 ? 'low' : 'normal';
134
+ return assessment;
135
+ }
136
+ /**
137
+ * Walk a session's request sequence in start order and mark the exclusion
138
+ * each entry's cache label would carry: the session's very first request is
139
+ * a `cold-start`, and any entry whose immediate predecessor in the same
140
+ * session named a different route is a `model-switch`. The route identity is
141
+ * provider + model when BOTH sides carry a provider (the cache lineage is
142
+ * per-route: switching `deepseek-official` → `deepseek-account` at the same
143
+ * model name starts a new cache context); when either side lacks the
144
+ * provider key (legacy rows, unattributed failures) the walk falls back to
145
+ * the model-only comparison those rows have always had — an absent key never
146
+ * widens the exclusion. Switching BACK to an earlier route is still a switch
147
+ * first — conservative. Entries outside this vocabulary (unknown future
148
+ * reasons) yield undefined. The walk always covers the FULL sequence the
149
+ * caller filtered to scope — never just the display window — so a first
150
+ * request that fell out of the 20-item pool is still correctly marked, and
151
+ * the marks do not move as the window slides.
152
+ *
153
+ * Predecessors are by adjacent conversation request regardless of kind: a
154
+ * compaction summarize call is a real provider request of the same session
155
+ * and takes part in the cache lineage. Auxiliary work is a separate prompt
156
+ * lineage and is skipped.
157
+ */
158
+ export function routeExclusionsOf(entries) {
159
+ const marks = new Map();
160
+ const lastRoute = new Map();
161
+ for (const entry of entries) {
162
+ // Auxiliary work uses a separate prompt lineage. It must not become the
163
+ // predecessor that makes the next conversation request look like a model
164
+ // switch (title generation commonly uses a different model).
165
+ if (entry.kind === 'aux')
166
+ continue;
167
+ // An unattributed entry (model '' — a failure row whose session never
168
+ // named a route) takes no mark and leaves no predecessor: it would
169
+ // otherwise forge a model switch around itself.
170
+ if (entry.model === '')
171
+ continue;
172
+ const previous = lastRoute.get(entry.sessionId);
173
+ if (previous === undefined) {
174
+ marks.set(entry, 'cold-start');
175
+ }
176
+ else {
177
+ // Route identity: provider + model when both sides carry a provider;
178
+ // model-only when either side predates the field (conservative — the
179
+ // legacy comparison never gains exclusions it cannot evidence).
180
+ const switched = previous.provider !== undefined && entry.provider !== undefined
181
+ ? previous.provider !== entry.provider || previous.model !== entry.model
182
+ : previous.model !== entry.model;
183
+ if (switched)
184
+ marks.set(entry, 'model-switch');
185
+ }
186
+ lastRoute.set(entry.sessionId, {
187
+ model: entry.model,
188
+ ...(entry.provider !== undefined ? { provider: entry.provider } : {}),
189
+ });
190
+ }
191
+ return marks;
192
+ }
193
+ /**
194
+ * The settled single-request decode speed: output tokens over the generation
195
+ * span (latency − first token), in tokens/second. The gate mirrors the
196
+ * global performance accumulators: the generation span must be a real
197
+ * streaming sample (≥ {@link PERF_STREAM_MIN_GEN_MS}) and the output must be
198
+ * positive. A whole-body (non-streaming) response has no first-token figure
199
+ * and stays undefined — its tiny arrival interval would fabricate enormous
200
+ * tok/s. Never returns Infinity/NaN.
201
+ */
202
+ export function settledSpeedOf(outputTokens, latencyMs, firstTokenLatencyMs) {
203
+ if (outputTokens === undefined || latencyMs === undefined || firstTokenLatencyMs === undefined)
204
+ return undefined;
205
+ if (!Number.isFinite(outputTokens) || !Number.isFinite(latencyMs) || !Number.isFinite(firstTokenLatencyMs))
206
+ return undefined;
207
+ if (outputTokens <= 0 || latencyMs < 0 || firstTokenLatencyMs < 0)
208
+ return undefined;
209
+ const genMs = latencyMs - firstTokenLatencyMs;
210
+ if (genMs < PERF_STREAM_MIN_GEN_MS)
211
+ return undefined;
212
+ const speed = outputTokens / (genMs / 1_000);
213
+ return Number.isFinite(speed) ? speed : undefined;
214
+ }
215
+ /** All exclusion reasons, for locale-key coverage tests. */
216
+ export const CACHE_EXCLUSION_REASONS = [
217
+ 'cold-start',
218
+ 'model-switch',
219
+ 'in-flight',
220
+ 'not-success',
221
+ 'compaction',
222
+ 'auxiliary',
223
+ 'cache-unreported',
224
+ 'insufficient-input',
225
+ 'invalid-data',
226
+ ];
227
+ //# sourceMappingURL=request-details.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"request-details.js","sourceRoot":"","sources":["../src/request-details.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAA;AAGnD;;;;;;GAMG;AACH,MAAM,2BAA2B,GAAG,EAAE,CAAA;AAEtC,mFAAmF;AACnF,MAAM,wBAAwB,GAAwB,IAAI,GAAG,CAAC,CAAC,mBAAmB,EAAE,kBAAkB,CAAC,CAAC,CAAA;AAkBxG;;;;;GAKG;AACH,SAAS,cAAc,CAAC,KAAuB;IAC7C,MAAM,EAAE,WAAW,EAAE,YAAY,EAAE,eAAe,EAAE,GAAG,KAAK,CAAA;IAC5D,IAAI,WAAW,KAAK,SAAS,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,WAAW,CAAC,IAAI,WAAW,GAAG,CAAC;QAAE,OAAO,KAAK,CAAA;IAC/F,IAAI,YAAY,KAAK,SAAS,IAAI,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,YAAY,CAAC,IAAI,YAAY,GAAG,CAAC,CAAC;QAAE,OAAO,KAAK,CAAA;IACpG,IAAI,eAAe,KAAK,SAAS,IAAI,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,eAAe,CAAC,IAAI,eAAe,GAAG,CAAC,CAAC;QAAE,OAAO,KAAK,CAAA;IAC7G,OAAO,IAAI,CAAA;AACb,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAA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