@inixiative/agent-session 0.1.0 → 0.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.
@@ -0,0 +1,30 @@
1
+ import type { SessionTokens } from "./harness-session";
2
+
3
+ /** Preserve the native payload as well as the counters consumers aggregate. */
4
+ export function parseClaudeUsage(value: unknown): SessionTokens | undefined {
5
+ if (!value || typeof value !== "object" || Array.isArray(value)) return undefined;
6
+ const usage = value as Record<string, unknown>;
7
+ const tokens: SessionTokens = {
8
+ input: count(usage.input_tokens) ?? 0,
9
+ output: count(usage.output_tokens) ?? 0,
10
+ providerUsage: usage,
11
+ };
12
+ const creation = usage.cache_creation as Record<string, unknown> | undefined;
13
+ const output = usage.output_tokens_details as Record<string, unknown> | undefined;
14
+ const optional = {
15
+ cacheRead: count(usage.cache_read_input_tokens),
16
+ cacheWrite: count(usage.cache_creation_input_tokens),
17
+ cacheWrite5m: count(creation?.ephemeral_5m_input_tokens),
18
+ cacheWrite1h: count(creation?.ephemeral_1h_input_tokens),
19
+ thinking: count(output?.thinking_tokens),
20
+ };
21
+ for (const [key, value] of Object.entries(optional)) {
22
+ if (value !== undefined) Object.assign(tokens, { [key]: value });
23
+ }
24
+ return tokens;
25
+ }
26
+
27
+ function count(value: unknown): number | undefined {
28
+ return typeof value === "number" && Number.isFinite(value) && value >= 0
29
+ ? value : undefined;
30
+ }