@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.
- package/dist/formatters/completions.d.ts +7 -0
- package/dist/formatters/completions.d.ts.map +1 -1
- package/dist/formatters/completions.js +9 -0
- package/dist/formatters/completions.js.map +1 -1
- package/dist/formatters/index.d.ts +1 -0
- package/dist/formatters/index.js +1 -0
- package/dist/formatters/index.js.map +1 -1
- package/dist/formatters/types.d.ts +4 -0
- package/dist/membrane.d.ts +3 -0
- package/dist/membrane.d.ts.map +1 -1
- package/dist/membrane.js +75 -12
- package/dist/membrane.js.map +1 -1
- package/dist/providers/anthropic.d.ts.map +1 -1
- package/dist/providers/anthropic.js +9 -1
- package/dist/providers/anthropic.js.map +1 -1
- package/dist/providers/bedrock.js +4 -13
- package/dist/providers/bedrock.js.map +1 -1
- package/dist/providers/gemini.js +2 -11
- package/dist/providers/gemini.js.map +1 -1
- package/dist/providers/openai-compatible.js +15 -25
- package/dist/providers/openai-compatible.js.map +1 -1
- package/dist/providers/openai-completions.js +24 -48
- package/dist/providers/openai-completions.js.map +1 -1
- package/dist/providers/openai-responses.js +1 -6
- package/dist/providers/openai-responses.js.map +1 -1
- package/dist/providers/openai.js +15 -25
- package/dist/providers/openai.js.map +1 -1
- package/dist/providers/openrouter.js +16 -35
- package/dist/providers/openrouter.js.map +1 -1
- package/dist/providers/utils.d.ts +0 -38
- package/dist/providers/utils.d.ts.map +1 -1
- package/dist/providers/utils.js +0 -86
- package/dist/providers/utils.js.map +1 -1
- package/dist/registry/default-pricing.d.ts +3 -0
- package/dist/registry/default-pricing.d.ts.map +1 -0
- package/dist/registry/default-pricing.js +75 -0
- package/dist/registry/default-pricing.js.map +1 -0
- package/dist/types/request.d.ts +0 -8
- package/dist/types/request.d.ts.map +1 -1
- package/dist/types/yielding-stream.d.ts +2 -2
- package/dist/types/yielding-stream.d.ts.map +1 -1
- package/dist/utils/cost.d.ts +10 -0
- package/dist/utils/cost.d.ts.map +1 -0
- package/dist/utils/cost.js +19 -0
- package/dist/utils/cost.js.map +1 -0
- package/dist/utils/index.d.ts +2 -0
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +1 -0
- package/dist/utils/index.js.map +1 -1
- package/package.json +1 -1
- package/src/formatters/completions.ts +19 -0
- package/src/membrane.ts +83 -18
- package/src/providers/anthropic.ts +13 -1
- package/src/registry/default-pricing.ts +77 -0
- package/src/types/yielding-stream.ts +2 -2
- package/src/utils/cost.ts +29 -0
- 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
|
-
|
|
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 {
|
|
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:
|
|
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
|
+
}
|