@animalabs/membrane 0.5.41 → 0.5.43

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.
@@ -10,7 +10,7 @@
10
10
 
11
11
  import type { ContentBlock } from './content.js';
12
12
  import type { ToolCall, ToolResult, ToolContext } from './tools.js';
13
- import type { BasicUsage, NormalizedResponse, StopReason } from './response.js';
13
+ import type { DetailedUsage, NormalizedResponse, StopReason } from './response.js';
14
14
  import type { ChunkMeta, BlockEvent } from './streaming.js';
15
15
 
16
16
  // ============================================================================
@@ -49,7 +49,7 @@ export interface ToolCallsEvent {
49
49
  */
50
50
  export interface UsageEvent {
51
51
  type: 'usage';
52
- usage: BasicUsage;
52
+ usage: DetailedUsage;
53
53
  }
54
54
 
55
55
  /**
@@ -0,0 +1,29 @@
1
+ import type { CostBreakdown } from '../types/response.js';
2
+ import type { ModelPricing } from '../types/provider.js';
3
+
4
+ export interface CostableUsage {
5
+ inputTokens: number;
6
+ outputTokens: number;
7
+ cacheCreationTokens?: number;
8
+ cacheReadTokens?: number;
9
+ }
10
+
11
+ export function calculateCost(usage: CostableUsage, pricing: ModelPricing): CostBreakdown {
12
+ const input = usage.inputTokens * pricing.inputPerMillion / 1_000_000;
13
+ const output = usage.outputTokens * pricing.outputPerMillion / 1_000_000;
14
+ const cacheWrite = pricing.cacheWritePerMillion != null
15
+ ? (usage.cacheCreationTokens ?? 0) * pricing.cacheWritePerMillion / 1_000_000
16
+ : undefined;
17
+ const cacheRead = pricing.cacheReadPerMillion != null
18
+ ? (usage.cacheReadTokens ?? 0) * pricing.cacheReadPerMillion / 1_000_000
19
+ : undefined;
20
+
21
+ return {
22
+ input,
23
+ output,
24
+ cacheWrite,
25
+ cacheRead,
26
+ total: input + output + (cacheWrite ?? 0) + (cacheRead ?? 0),
27
+ currency: pricing.currency,
28
+ };
29
+ }
@@ -13,3 +13,6 @@ export {
13
13
  unescapeXml,
14
14
  type ToolDefinitionForPrompt,
15
15
  } from './tool-parser.js';
16
+
17
+ export { calculateCost } from './cost.js';
18
+ export type { CostableUsage } from './cost.js';