@animalabs/membrane 0.5.42 → 0.5.44

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 (57) hide show
  1. package/dist/formatters/completions.d.ts +7 -0
  2. package/dist/formatters/completions.d.ts.map +1 -1
  3. package/dist/formatters/completions.js +9 -0
  4. package/dist/formatters/completions.js.map +1 -1
  5. package/dist/formatters/index.d.ts +1 -0
  6. package/dist/formatters/index.js +1 -0
  7. package/dist/formatters/index.js.map +1 -1
  8. package/dist/formatters/types.d.ts +4 -0
  9. package/dist/membrane.d.ts +3 -0
  10. package/dist/membrane.d.ts.map +1 -1
  11. package/dist/membrane.js +75 -12
  12. package/dist/membrane.js.map +1 -1
  13. package/dist/providers/anthropic.d.ts.map +1 -1
  14. package/dist/providers/anthropic.js +9 -1
  15. package/dist/providers/anthropic.js.map +1 -1
  16. package/dist/providers/bedrock.js +4 -13
  17. package/dist/providers/bedrock.js.map +1 -1
  18. package/dist/providers/gemini.js +2 -11
  19. package/dist/providers/gemini.js.map +1 -1
  20. package/dist/providers/openai-compatible.js +15 -25
  21. package/dist/providers/openai-compatible.js.map +1 -1
  22. package/dist/providers/openai-completions.js +24 -48
  23. package/dist/providers/openai-completions.js.map +1 -1
  24. package/dist/providers/openai-responses.js +1 -6
  25. package/dist/providers/openai-responses.js.map +1 -1
  26. package/dist/providers/openai.js +15 -25
  27. package/dist/providers/openai.js.map +1 -1
  28. package/dist/providers/openrouter.js +16 -35
  29. package/dist/providers/openrouter.js.map +1 -1
  30. package/dist/providers/utils.d.ts +0 -38
  31. package/dist/providers/utils.d.ts.map +1 -1
  32. package/dist/providers/utils.js +0 -86
  33. package/dist/providers/utils.js.map +1 -1
  34. package/dist/registry/default-pricing.d.ts +3 -0
  35. package/dist/registry/default-pricing.d.ts.map +1 -0
  36. package/dist/registry/default-pricing.js +75 -0
  37. package/dist/registry/default-pricing.js.map +1 -0
  38. package/dist/types/request.d.ts +0 -8
  39. package/dist/types/request.d.ts.map +1 -1
  40. package/dist/types/yielding-stream.d.ts +2 -2
  41. package/dist/types/yielding-stream.d.ts.map +1 -1
  42. package/dist/utils/cost.d.ts +10 -0
  43. package/dist/utils/cost.d.ts.map +1 -0
  44. package/dist/utils/cost.js +19 -0
  45. package/dist/utils/cost.js.map +1 -0
  46. package/dist/utils/index.d.ts +2 -0
  47. package/dist/utils/index.d.ts.map +1 -1
  48. package/dist/utils/index.js +1 -0
  49. package/dist/utils/index.js.map +1 -1
  50. package/package.json +1 -1
  51. package/src/formatters/completions.ts +19 -0
  52. package/src/membrane.ts +83 -18
  53. package/src/providers/anthropic.ts +13 -1
  54. package/src/registry/default-pricing.ts +77 -0
  55. package/src/types/yielding-stream.ts +2 -2
  56. package/src/utils/cost.ts +29 -0
  57. package/src/utils/index.ts +3 -0
@@ -178,7 +178,19 @@ export class AnthropicAdapter implements ProviderAdapter {
178
178
  const delta = event.delta as { stop_reason?: string; stop_sequence?: string };
179
179
  stopReason = delta.stop_reason ?? 'end_turn';
180
180
  stopSequence = delta.stop_sequence ?? undefined;
181
- outputTokens = (event.usage as { output_tokens: number }).output_tokens ?? 0;
181
+ const deltaUsage = event.usage as unknown as {
182
+ output_tokens: number;
183
+ cache_creation_input_tokens?: number | null;
184
+ cache_read_input_tokens?: number | null;
185
+ };
186
+ outputTokens = deltaUsage.output_tokens ?? 0;
187
+ // message_delta carries cumulative cache metrics — use as authoritative
188
+ if (deltaUsage.cache_creation_input_tokens != null) {
189
+ cacheCreationTokens = deltaUsage.cache_creation_input_tokens;
190
+ }
191
+ if (deltaUsage.cache_read_input_tokens != null) {
192
+ cacheReadTokens = deltaUsage.cache_read_input_tokens;
193
+ }
182
194
  break;
183
195
  }
184
196
  }
@@ -0,0 +1,77 @@
1
+ import type { ModelPricing } from '../types/provider.js';
2
+
3
+ /**
4
+ * Built-in pricing table for known models.
5
+ * Prices in USD per million tokens. Last updated: 2025-07.
6
+ *
7
+ * Used as fallback when no ModelRegistry is configured.
8
+ * Registry pricing (if available) takes precedence.
9
+ */
10
+ const PRICING_TABLE: Array<{ prefix: string; pricing: ModelPricing }> = [
11
+ // Anthropic — Claude 4.6
12
+ {
13
+ prefix: 'claude-opus-4-6',
14
+ pricing: { inputPerMillion: 15, outputPerMillion: 75, cacheWritePerMillion: 18.75, cacheReadPerMillion: 1.50, currency: 'USD' },
15
+ },
16
+ {
17
+ prefix: 'claude-sonnet-4-6',
18
+ pricing: { inputPerMillion: 3, outputPerMillion: 15, cacheWritePerMillion: 3.75, cacheReadPerMillion: 0.30, currency: 'USD' },
19
+ },
20
+ // Anthropic — Claude 4.5
21
+ {
22
+ prefix: 'claude-haiku-4-5',
23
+ pricing: { inputPerMillion: 0.80, outputPerMillion: 4, cacheWritePerMillion: 1.00, cacheReadPerMillion: 0.08, currency: 'USD' },
24
+ },
25
+ // Anthropic — Claude 4
26
+ {
27
+ prefix: 'claude-opus-4',
28
+ pricing: { inputPerMillion: 15, outputPerMillion: 75, cacheWritePerMillion: 18.75, cacheReadPerMillion: 1.50, currency: 'USD' },
29
+ },
30
+ {
31
+ prefix: 'claude-sonnet-4',
32
+ pricing: { inputPerMillion: 3, outputPerMillion: 15, cacheWritePerMillion: 3.75, cacheReadPerMillion: 0.30, currency: 'USD' },
33
+ },
34
+ // Anthropic — Claude 3.5
35
+ {
36
+ prefix: 'claude-3-5-sonnet',
37
+ pricing: { inputPerMillion: 3, outputPerMillion: 15, cacheWritePerMillion: 3.75, cacheReadPerMillion: 0.30, currency: 'USD' },
38
+ },
39
+ {
40
+ prefix: 'claude-3-5-haiku',
41
+ pricing: { inputPerMillion: 0.80, outputPerMillion: 4, cacheWritePerMillion: 1.00, cacheReadPerMillion: 0.08, currency: 'USD' },
42
+ },
43
+ // OpenAI — GPT-4o
44
+ {
45
+ prefix: 'gpt-4o-2024',
46
+ pricing: { inputPerMillion: 2.50, outputPerMillion: 10, cacheReadPerMillion: 1.25, currency: 'USD' },
47
+ },
48
+ {
49
+ prefix: 'gpt-4o',
50
+ pricing: { inputPerMillion: 2.50, outputPerMillion: 10, cacheReadPerMillion: 1.25, currency: 'USD' },
51
+ },
52
+ {
53
+ prefix: 'gpt-4o-mini',
54
+ pricing: { inputPerMillion: 0.15, outputPerMillion: 0.60, cacheReadPerMillion: 0.075, currency: 'USD' },
55
+ },
56
+ // Google — Gemini 2.5
57
+ {
58
+ prefix: 'gemini-2.5-pro',
59
+ pricing: { inputPerMillion: 1.25, outputPerMillion: 10, currency: 'USD' },
60
+ },
61
+ {
62
+ prefix: 'gemini-2.5-flash',
63
+ pricing: { inputPerMillion: 0.15, outputPerMillion: 0.60, currency: 'USD' },
64
+ },
65
+ ];
66
+
67
+ export function getDefaultPricing(modelId: string): ModelPricing | undefined {
68
+ let best: ModelPricing | undefined;
69
+ let bestLen = 0;
70
+ for (const entry of PRICING_TABLE) {
71
+ if (modelId.startsWith(entry.prefix) && entry.prefix.length > bestLen) {
72
+ best = entry.pricing;
73
+ bestLen = entry.prefix.length;
74
+ }
75
+ }
76
+ return best;
77
+ }
@@ -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';