mohdel 0.104.1 → 0.104.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.
@@ -22,10 +22,16 @@ import { catalogKey } from '#core/model-id.js'
22
22
  * @param {string} output
23
23
  * @param {number} inputTokens
24
24
  * @param {number} outputTokens
25
+ * @param {{cacheWriteInputTokens?: number, cacheReadInputTokens?: number}} [extra]
26
+ * Optional cache token counts captured before cancellation. Threaded through
27
+ * so the cancellation-cost calculation prices any cache writes/reads that
28
+ * already happened before the abort.
25
29
  * @returns {import('#core/events.js').DoneEvent}
26
30
  */
27
- export function cancelledDone (start, first, envelope, output, inputTokens, outputTokens) {
31
+ export function cancelledDone (start, first, envelope, output, inputTokens, outputTokens, extra = {}) {
28
32
  const end = String(process.hrtime.bigint())
33
+ const cacheWriteInputTokens = extra.cacheWriteInputTokens || 0
34
+ const cacheReadInputTokens = extra.cacheReadInputTokens || 0
29
35
  return {
30
36
  type: 'done',
31
37
  result: {
@@ -34,9 +40,11 @@ export function cancelledDone (start, first, envelope, output, inputTokens, outp
34
40
  inputTokens,
35
41
  outputTokens,
36
42
  thinkingTokens: 0,
43
+ ...(cacheWriteInputTokens > 0 && { cacheWriteInputTokens }),
44
+ ...(cacheReadInputTokens > 0 && { cacheReadInputTokens }),
37
45
  cost: costFor(
38
46
  catalogKey(envelope.model),
39
- { inputTokens, outputTokens, thinkingTokens: 0 }
47
+ { inputTokens, outputTokens, thinkingTokens: 0, cacheWriteInputTokens, cacheReadInputTokens }
40
48
  ),
41
49
  timestamps: { start, first: first ?? end, end },
42
50
  warning: WARNING_CANCELLED
@@ -129,7 +129,7 @@ export async function * anthropic (envelope, deps = {}) {
129
129
 
130
130
  for await (const event of stream) {
131
131
  if (signal?.aborted) {
132
- yield cancelledDone(start, first, envelope, currentOutput(), inputTokens, outputTokens)
132
+ yield cancelledDone(start, first, envelope, currentOutput(), inputTokens, outputTokens, { cacheWriteInputTokens: cacheWriteTokens, cacheReadInputTokens: cacheReadTokens })
133
133
  return
134
134
  }
135
135
  switch (event.type) {
@@ -197,7 +197,7 @@ export async function * anthropic (envelope, deps = {}) {
197
197
  }
198
198
  } catch (e) {
199
199
  if (signal?.aborted) {
200
- yield cancelledDone(start, first, envelope, currentOutput(), inputTokens, outputTokens)
200
+ yield cancelledDone(start, first, envelope, currentOutput(), inputTokens, outputTokens, { cacheWriteInputTokens: cacheWriteTokens, cacheReadInputTokens: cacheReadTokens })
201
201
  return
202
202
  }
203
203
  log?.warn({ err: e }, '[mohdel:anthropic] stream failed')
@@ -206,7 +206,7 @@ export async function * anthropic (envelope, deps = {}) {
206
206
  }
207
207
 
208
208
  if (signal?.aborted) {
209
- yield cancelledDone(start, first, envelope, currentOutput(), inputTokens, outputTokens)
209
+ yield cancelledDone(start, first, envelope, currentOutput(), inputTokens, outputTokens, { cacheWriteInputTokens: cacheWriteTokens, cacheReadInputTokens: cacheReadTokens })
210
210
  return
211
211
  }
212
212
 
package/js/session/run.js CHANGED
@@ -357,6 +357,8 @@ function finalizeSpanOk (span, result, sawDelta = false, maxInterFrameMs = 0) {
357
357
  'mohdel.stream': !!sawDelta,
358
358
  'mohdel.max_inter_frame_ms': maxInterFrameMs
359
359
  }
360
+ if (result?.cacheWriteInputTokens) attrs['mohdel.cache_write_input_tokens'] = result.cacheWriteInputTokens
361
+ if (result?.cacheReadInputTokens) attrs['mohdel.cache_read_input_tokens'] = result.cacheReadInputTokens
360
362
  if (result?.cost != null) attrs['mohdel.cost'] = result.cost
361
363
  if (result?.warning) attrs['mohdel.warning'] = result.warning
362
364
  if (result?.timestamps?.start && result?.timestamps?.first) {
@@ -376,16 +378,19 @@ function finalizeSpanOk (span, result, sawDelta = false, maxInterFrameMs = 0) {
376
378
  * @param {number} startedAt
377
379
  */
378
380
  function summarizeDone (result, startedAt) {
379
- return {
381
+ const summary = {
380
382
  status: result?.status,
381
383
  in: result?.inputTokens || 0,
382
384
  out: result?.outputTokens || 0,
383
- think: result?.thinkingTokens || 0,
384
- cost: result?.cost,
385
- warning: result?.warning,
386
- totalMs: Date.now() - startedAt,
387
- maxInterFrameMs: result?.maxInterFrameMs
385
+ think: result?.thinkingTokens || 0
388
386
  }
387
+ if (result?.cacheWriteInputTokens) summary.cacheW = result.cacheWriteInputTokens
388
+ if (result?.cacheReadInputTokens) summary.cacheR = result.cacheReadInputTokens
389
+ summary.cost = result?.cost
390
+ if (result?.warning) summary.warning = result.warning
391
+ summary.totalMs = Date.now() - startedAt
392
+ if (result?.maxInterFrameMs != null) summary.maxInterFrameMs = result.maxInterFrameMs
393
+ return summary
389
394
  }
390
395
 
391
396
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mohdel",
3
- "version": "0.104.1",
3
+ "version": "0.104.3",
4
4
  "license": "MIT",
5
5
  "author": {
6
6
  "name": "Christophe Le Bars",
@@ -87,7 +87,7 @@
87
87
  "@opentelemetry/exporter-trace-otlp-grpc": "^0.217.0",
88
88
  "@opentelemetry/sdk-node": "^0.217.0",
89
89
  "chalk": "^5.4.0",
90
- "mohdel-thin-gate-linux-x64-gnu": "0.104.1"
90
+ "mohdel-thin-gate-linux-x64-gnu": "0.104.3"
91
91
  },
92
92
  "dependencies": {
93
93
  "@anthropic-ai/sdk": "^0.95.1",