mini-coder 0.5.12 → 0.5.14

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.
@@ -126,23 +126,61 @@ function isToolCallContentBlock(
126
126
  );
127
127
  }
128
128
 
129
- function isAssistantUsage(value: unknown): value is AssistantMessage["usage"] {
129
+ /**
130
+ * Parse and validate an assistant usage payload from unknown input.
131
+ *
132
+ * @param value - Unknown value to validate.
133
+ * @returns The parsed assistant usage, or `null` when invalid.
134
+ */
135
+ export function readAssistantUsage(
136
+ value: unknown,
137
+ ): AssistantMessage["usage"] | null {
130
138
  const usageRecord = toRecord(value);
131
139
  const costRecord = toRecord(usageRecord?.cost);
132
- return (
133
- usageRecord !== null &&
134
- costRecord !== null &&
135
- readFiniteNumber(usageRecord, "input") !== null &&
136
- readFiniteNumber(usageRecord, "output") !== null &&
137
- readFiniteNumber(usageRecord, "cacheRead") !== null &&
138
- readFiniteNumber(usageRecord, "cacheWrite") !== null &&
139
- readFiniteNumber(usageRecord, "totalTokens") !== null &&
140
- readFiniteNumber(costRecord, "input") !== null &&
141
- readFiniteNumber(costRecord, "output") !== null &&
142
- readFiniteNumber(costRecord, "cacheRead") !== null &&
143
- readFiniteNumber(costRecord, "cacheWrite") !== null &&
144
- readFiniteNumber(costRecord, "total") !== null
145
- );
140
+ if (!usageRecord || !costRecord) {
141
+ return null;
142
+ }
143
+
144
+ const input = readFiniteNumber(usageRecord, "input");
145
+ const output = readFiniteNumber(usageRecord, "output");
146
+ const cacheRead = readFiniteNumber(usageRecord, "cacheRead");
147
+ const cacheWrite = readFiniteNumber(usageRecord, "cacheWrite");
148
+ const totalTokens = readFiniteNumber(usageRecord, "totalTokens");
149
+ const costInput = readFiniteNumber(costRecord, "input");
150
+ const costOutput = readFiniteNumber(costRecord, "output");
151
+ const costCacheRead = readFiniteNumber(costRecord, "cacheRead");
152
+ const costCacheWrite = readFiniteNumber(costRecord, "cacheWrite");
153
+ const costTotal = readFiniteNumber(costRecord, "total");
154
+
155
+ if (
156
+ input === null ||
157
+ output === null ||
158
+ cacheRead === null ||
159
+ cacheWrite === null ||
160
+ totalTokens === null ||
161
+ costInput === null ||
162
+ costOutput === null ||
163
+ costCacheRead === null ||
164
+ costCacheWrite === null ||
165
+ costTotal === null
166
+ ) {
167
+ return null;
168
+ }
169
+
170
+ return {
171
+ input,
172
+ output,
173
+ cacheRead,
174
+ cacheWrite,
175
+ totalTokens,
176
+ cost: {
177
+ input: costInput,
178
+ output: costOutput,
179
+ cacheRead: costCacheRead,
180
+ cacheWrite: costCacheWrite,
181
+ total: costTotal,
182
+ },
183
+ };
146
184
  }
147
185
 
148
186
  function isStopReason(value: unknown): value is AssistantMessage["stopReason"] {
@@ -206,9 +244,9 @@ function parseAssistantMessageRecord(value: unknown): AssistantMessage | null {
206
244
  api,
207
245
  provider,
208
246
  model,
209
- usage: isAssistantUsage(record.usage)
210
- ? record.usage
211
- : structuredClone(EMPTY_ASSISTANT_USAGE),
247
+ usage:
248
+ readAssistantUsage(record.usage) ??
249
+ structuredClone(EMPTY_ASSISTANT_USAGE),
212
250
  stopReason: record.stopReason,
213
251
  ...(errorMessage !== null ? { errorMessage } : {}),
214
252
  timestamp,
@@ -343,51 +381,5 @@ export function getAssistantUsage(
343
381
  return null;
344
382
  }
345
383
 
346
- const messageRecord = toRecord(message);
347
- const usageRecord = toRecord(messageRecord?.usage);
348
- const costRecord = toRecord(usageRecord?.cost);
349
- if (!usageRecord || !costRecord) {
350
- return null;
351
- }
352
-
353
- const input = readFiniteNumber(usageRecord, "input");
354
- const output = readFiniteNumber(usageRecord, "output");
355
- const cacheRead = readFiniteNumber(usageRecord, "cacheRead");
356
- const cacheWrite = readFiniteNumber(usageRecord, "cacheWrite");
357
- const totalTokens = readFiniteNumber(usageRecord, "totalTokens");
358
- const costInput = readFiniteNumber(costRecord, "input");
359
- const costOutput = readFiniteNumber(costRecord, "output");
360
- const costCacheRead = readFiniteNumber(costRecord, "cacheRead");
361
- const costCacheWrite = readFiniteNumber(costRecord, "cacheWrite");
362
- const costTotal = readFiniteNumber(costRecord, "total");
363
-
364
- if (
365
- input === null ||
366
- output === null ||
367
- cacheRead === null ||
368
- cacheWrite === null ||
369
- totalTokens === null ||
370
- costInput === null ||
371
- costOutput === null ||
372
- costCacheRead === null ||
373
- costCacheWrite === null ||
374
- costTotal === null
375
- ) {
376
- return null;
377
- }
378
-
379
- return {
380
- input,
381
- output,
382
- cacheRead,
383
- cacheWrite,
384
- totalTokens,
385
- cost: {
386
- input: costInput,
387
- output: costOutput,
388
- cacheRead: costCacheRead,
389
- cacheWrite: costCacheWrite,
390
- total: costTotal,
391
- },
392
- };
384
+ return readAssistantUsage(toRecord(message)?.usage);
393
385
  }