@meridian-flow/runtime 0.1.1 → 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.
- package/README.md +10 -8
- package/dist/gateway/adapters/anthropic/request-map.d.ts +1 -2
- package/dist/gateway/adapters/anthropic/request-map.js +4 -6
- package/dist/gateway/adapters/anthropic/stream-collect.d.ts +2 -11
- package/dist/gateway/adapters/anthropic/stream-collect.js +3 -8
- package/dist/gateway/adapters/gemini/request-map.d.ts +1 -1
- package/dist/gateway/adapters/openai/request-map.d.ts +2 -2
- package/dist/gateway/adapters/openai/request-map.js +1 -1
- package/dist/gateway/adapters/openai/stream-collect.d.ts +5 -6
- package/dist/gateway/adapters/openai/stream-collect.js +9 -11
- package/dist/gateway/adapters/openai-compatible/stream-collect.d.ts +1 -1
- package/dist/gateway/adapters/openai-compatible/stream-collect.js +2 -2
- package/dist/gateway/adapters/provider-http-error.d.ts +5 -1
- package/dist/gateway/adapters/provider-http-error.js +9 -2
- package/dist/gateway/domain/media.d.ts +3 -2
- package/dist/gateway/domain/types.d.ts +36 -32
- package/dist/gateway/domain/types.js +1 -1
- package/dist/gateway/helpers/messages.d.ts +3 -4
- package/dist/gateway/helpers/messages.js +1 -2
- package/dist/gateway/mock-script.d.ts +39 -38
- package/dist/gateway/mock-script.js +4 -3
- package/dist/gateway/ports/gateway.d.ts +6 -8
- package/dist/gateway/ports/provider-adapter.d.ts +1 -1
- package/dist/gateway/shapes/provider-error.d.ts +2 -3
- package/dist/gateway/shapes/usage.d.ts +3 -4
- package/dist/gateway/shapes/usage.js +3 -4
- package/dist/gateway/stream-events.d.ts +1 -1
- package/dist/gateway/stream-events.js +1 -1
- package/package.json +10 -10
package/README.md
CHANGED
|
@@ -22,7 +22,9 @@ The package reads no environment variable: keys, URLs and policy come in through
|
|
|
22
22
|
|
|
23
23
|
## Example
|
|
24
24
|
|
|
25
|
-
A gateway with one provider and a credential chosen per call
|
|
25
|
+
A gateway with one provider and a credential chosen per call. The model row (limits, input
|
|
26
|
+
kinds, reasoning efforts, cache lifetimes) is yours to declare; this one follows
|
|
27
|
+
[models.dev](https://models.dev) for Claude Sonnet 5.5.
|
|
26
28
|
|
|
27
29
|
```ts
|
|
28
30
|
import { createGateway, user } from "@meridian-flow/runtime/gateway";
|
|
@@ -34,14 +36,14 @@ const gateway = createGateway({
|
|
|
34
36
|
adapter: "anthropic",
|
|
35
37
|
models: [
|
|
36
38
|
{
|
|
37
|
-
id: "claude-sonnet-
|
|
39
|
+
id: "claude-sonnet-5-5",
|
|
38
40
|
provider: "anthropic",
|
|
39
|
-
displayName: "Claude Sonnet
|
|
40
|
-
contextWindow:
|
|
41
|
-
maxOutputTokens:
|
|
41
|
+
displayName: "Claude Sonnet 5.5",
|
|
42
|
+
contextWindow: 1_000_000,
|
|
43
|
+
maxOutputTokens: 128_000,
|
|
42
44
|
tokenizer: "anthropic",
|
|
43
|
-
inputModalities: ["text", "image"],
|
|
44
|
-
reasoning: { efforts: ["low", "medium", "high"
|
|
45
|
+
inputModalities: ["text", "image", "pdf"],
|
|
46
|
+
reasoning: { efforts: ["low", "medium", "high", "max"] },
|
|
45
47
|
promptCache: { kind: "explicit", shortTtlSeconds: 300, longTtlSeconds: 3600 },
|
|
46
48
|
},
|
|
47
49
|
],
|
|
@@ -52,7 +54,7 @@ const gateway = createGateway({
|
|
|
52
54
|
const apiKey = "key-for-this-tenant"; // read it wherever your product keeps keys
|
|
53
55
|
|
|
54
56
|
for await (const event of gateway.stream({
|
|
55
|
-
model: "claude-sonnet-
|
|
57
|
+
model: "claude-sonnet-5-5",
|
|
56
58
|
messages: [user("Say hello.")],
|
|
57
59
|
promptCacheRetention: "none",
|
|
58
60
|
auth: { anthropic: { apiKey } },
|
|
@@ -36,8 +36,7 @@
|
|
|
36
36
|
* landing beside a tool result. Marks go on wire user messages, whose last block
|
|
37
37
|
* always accepts `cache_control`. Marks move as the tail
|
|
38
38
|
* advances; providers hash content, not the marker, so that never invalidates
|
|
39
|
-
* a cached prefix. A 1h write costs 2x input tokens (5m: 1.25x)
|
|
40
|
-
* registry's `cacheWriteUsdPerMillionTokens` pinned rates.
|
|
39
|
+
* a cached prefix. A 1h write costs 2x input tokens (5m: 1.25x).
|
|
41
40
|
*/
|
|
42
41
|
import type Anthropic from "@anthropic-ai/sdk";
|
|
43
42
|
import type { GenerateRequest, ModelInfo, ResolvedCall } from "../../domain/index.js";
|
|
@@ -203,7 +203,7 @@ function mergeConsecutiveSameRole(messages) {
|
|
|
203
203
|
// turns it into a single marked text block (see `markCachePrefix`).
|
|
204
204
|
//
|
|
205
205
|
function extractSystem(messages) {
|
|
206
|
-
const system = textFromParts(messages.
|
|
206
|
+
const system = textFromParts(messages.flatMap((m) => (m.role === "system" ? m.content : [])));
|
|
207
207
|
return system.length > 0 ? system : undefined;
|
|
208
208
|
}
|
|
209
209
|
// ── Cache marking ─────────────────────────────────────────────────
|
|
@@ -219,13 +219,11 @@ function markCachePrefix(system, messages, cacheControl) {
|
|
|
219
219
|
const marked = [...messages];
|
|
220
220
|
let remaining = 2;
|
|
221
221
|
for (let index = marked.length - 1; index >= 0 && remaining > 0; index--) {
|
|
222
|
-
|
|
222
|
+
const message = marked[index];
|
|
223
|
+
if (message.role !== "user")
|
|
223
224
|
continue;
|
|
224
225
|
remaining--;
|
|
225
|
-
marked[index] = {
|
|
226
|
-
...marked[index],
|
|
227
|
-
content: markLastBlock(marked[index].content, cacheControl),
|
|
228
|
-
};
|
|
226
|
+
marked[index] = { ...message, content: markLastBlock(message.content, cacheControl) };
|
|
229
227
|
}
|
|
230
228
|
return {
|
|
231
229
|
system: system && [{ type: "text", text: system, cache_control: cacheControl }],
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
* when building the final result, so persisted content follows the order Claude
|
|
11
11
|
* streamed instead of grouping all reasoning, then all text, then all tools.
|
|
12
12
|
*
|
|
13
|
-
* Provider-protocol
|
|
13
|
+
* Provider-protocol facts this file relies on (checked against the `@anthropic-ai/sdk` types and docs):
|
|
14
14
|
* - Anthropic's documented stream flow is strictly serial per block:
|
|
15
15
|
* `message_start` → (content_block_start → delta(s) → content_block_stop)*
|
|
16
16
|
* → `message_delta` → `message_stop`. Each block's `index` corresponds to
|
|
@@ -22,10 +22,6 @@
|
|
|
22
22
|
* not user-visible, arrives just before `content_block_stop`),
|
|
23
23
|
* `input_json_delta` (partial JSON for tool input — must be concatenated
|
|
24
24
|
* before parsing).
|
|
25
|
-
* - StopReason mismatch: current Anthropic docs list
|
|
26
|
-
* `model_context_window_exceeded`, but the installed SDK 0.100.1's
|
|
27
|
-
* `StopReason` union is `'end_turn' | 'max_tokens' | 'stop_sequence' |
|
|
28
|
-
* 'tool_use' | 'pause_turn' | 'refusal'` — that value is not present.
|
|
29
25
|
*/
|
|
30
26
|
import type Anthropic from "@anthropic-ai/sdk";
|
|
31
27
|
import type { ContentPart, FinishReason, GenerateResult, StreamEvent } from "../../domain/index.js";
|
|
@@ -53,12 +49,7 @@ interface ReasoningBlockEntry {
|
|
|
53
49
|
}
|
|
54
50
|
export declare function createStreamAccumulator(model: string, provider: string, longCacheWrites: boolean): StreamAccumulator;
|
|
55
51
|
/**
|
|
56
|
-
* Map Anthropic stop reasons to
|
|
57
|
-
*
|
|
58
|
-
* The installed SDK 0.100.1 StopReason union is:
|
|
59
|
-
* 'end_turn' | 'max_tokens' | 'stop_sequence' | 'tool_use' | 'pause_turn' | 'refusal'
|
|
60
|
-
* Current Anthropic docs additionally list `model_context_window_exceeded`,
|
|
61
|
-
* but that value is not in the SDK union; accept the wire string at this boundary.
|
|
52
|
+
* Map Anthropic stop reasons to the canonical FinishReason.
|
|
62
53
|
*/
|
|
63
54
|
export declare function mapStopReason(reason: string | null | undefined): FinishReason;
|
|
64
55
|
export declare function mapUsage(usage: Anthropic.Messages.Usage | Anthropic.Messages.MessageDeltaUsage, longCacheWrites: boolean, fallback?: Usage): Usage;
|
|
@@ -18,15 +18,10 @@ export function createStreamAccumulator(model, provider, longCacheWrites) {
|
|
|
18
18
|
}
|
|
19
19
|
// ── Finish reason mapping ─────────────────────────────────────────
|
|
20
20
|
/**
|
|
21
|
-
* Map Anthropic stop reasons to
|
|
22
|
-
*
|
|
23
|
-
* The installed SDK 0.100.1 StopReason union is:
|
|
24
|
-
* 'end_turn' | 'max_tokens' | 'stop_sequence' | 'tool_use' | 'pause_turn' | 'refusal'
|
|
25
|
-
* Current Anthropic docs additionally list `model_context_window_exceeded`,
|
|
26
|
-
* but that value is not in the SDK union; accept the wire string at this boundary.
|
|
21
|
+
* Map Anthropic stop reasons to the canonical FinishReason.
|
|
27
22
|
*/
|
|
28
23
|
export function mapStopReason(reason) {
|
|
29
|
-
// Anthropic stop reasons mostly line up with
|
|
24
|
+
// Anthropic stop reasons mostly line up with the canonical FinishReason. `refusal`
|
|
30
25
|
// and `pause_turn` have no canonical equivalent today, so they are
|
|
31
26
|
// represented as an error finish and end_turn respectively.
|
|
32
27
|
switch (reason) {
|
|
@@ -47,7 +42,7 @@ export function mapStopReason(reason) {
|
|
|
47
42
|
}
|
|
48
43
|
// ── Usage mapping ─────────────────────────────────────────────────
|
|
49
44
|
export function mapUsage(usage, longCacheWrites, fallback) {
|
|
50
|
-
// Anthropic's input_tokens excludes cache reads and cache creation.
|
|
45
|
+
// Anthropic's input_tokens excludes cache reads and cache creation. the canonical
|
|
51
46
|
// canonical inputTokens is inclusive, so the adapter owns the additive
|
|
52
47
|
// conversion before usage reaches persistence, displays, or pricing.
|
|
53
48
|
const fallbackCacheRead = fallback?.cacheReadTokens ?? 0;
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
* - Thought signatures: the model's `thoughtSignature` on a text, thought or function-call part
|
|
14
14
|
* is kept in `providerOptions.gemini.signature` and sent back on the same part, only to the
|
|
15
15
|
* provider/model pair that issued it. Gemini 3 rejects a replayed function-call step whose
|
|
16
|
-
* first call carries no signature (history from another model,
|
|
16
|
+
* first call carries no signature (history from another model, or one with its thinking removed), so that
|
|
17
17
|
* call gets Google's documented validator-bypass value.
|
|
18
18
|
* - Prompt caching is implicit: no mark and no TTL is sent.
|
|
19
19
|
*/
|
|
@@ -18,9 +18,9 @@
|
|
|
18
18
|
* guide).
|
|
19
19
|
* - Prompt caching is automatic for prompts >=1024 tokens and needs no
|
|
20
20
|
* explicit breakpoints, so this mapper only forwards `GenerateRequest.
|
|
21
|
-
* promptCacheKey` as `prompt_cache_key` (a stable per-
|
|
21
|
+
* promptCacheKey` as `prompt_cache_key` (a stable per-conversation routing hint
|
|
22
22
|
* that improves cache-affinity — OpenAI's own docs recommend one per
|
|
23
|
-
*
|
|
23
|
+
* conversation). A long retention request adds
|
|
24
24
|
* `prompt_cache_options: { ttl: "12h" }`; the short one relies on the 30m
|
|
25
25
|
* default.
|
|
26
26
|
*/
|
|
@@ -222,7 +222,7 @@ export function toOpenAIResponsesParams(request, model, call) {
|
|
|
222
222
|
...(mapTools(request.tools) ? { tools: mapTools(request.tools) } : {}),
|
|
223
223
|
reasoning: mapReasoning(call.reasoning),
|
|
224
224
|
...(request.promptCacheKey ? { prompt_cache_key: request.promptCacheKey } : {}),
|
|
225
|
-
// The 30m default needs no parameter.
|
|
225
|
+
// The 30m default needs no parameter.
|
|
226
226
|
...(cacheTtl ? { prompt_cache_options: { ttl: cacheTtl } } : {}),
|
|
227
227
|
};
|
|
228
228
|
}
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* canonical StreamEvents and a final GenerateResult. The Responses protocol
|
|
4
4
|
* names the same logical output item by different keys in different event
|
|
5
5
|
* families, so this file owns the reconciliation from provider item IDs,
|
|
6
|
-
* call IDs, and output positions into
|
|
6
|
+
* call IDs, and output positions into the canonical ContentPart,
|
|
7
7
|
* ToolCall, Usage, and FinishReason vocabulary.
|
|
8
8
|
*
|
|
9
9
|
* Key decision: provider `output_index` is treated as the source-order spine.
|
|
@@ -11,18 +11,17 @@
|
|
|
11
11
|
* position and sorted only when building the final result, so persisted content
|
|
12
12
|
* follows the provider's streamed order instead of being grouped by part type.
|
|
13
13
|
*
|
|
14
|
-
* Provider-protocol
|
|
14
|
+
* Provider-protocol facts this file relies on (checked against the `openai` SDK types and docs):
|
|
15
15
|
* - `output_index` is the position of the output item in `response.output[]`.
|
|
16
16
|
* The final array order is model-dependent and the docs do NOT explicitly
|
|
17
|
-
* guarantee non-interleaving during streaming, so
|
|
17
|
+
* guarantee non-interleaving during streaming, so this file buffers by index
|
|
18
18
|
* and sorts at assembly time rather than trusting arrival order.
|
|
19
19
|
* - `response.function_call_arguments.delta` events carry `item_id` and
|
|
20
20
|
* `output_index` but not `call_id`. This is why
|
|
21
21
|
* argument deltas are buffered in `pendingDeltas` until the durable `call_id`
|
|
22
22
|
* arrives via `response.output_item.added` or a later event.
|
|
23
|
-
* -
|
|
24
|
-
*
|
|
25
|
-
* identity so the external Meridian contract does not change.
|
|
23
|
+
* - Reasoning text events are named `response.reasoning_text.*`; the accumulator is keyed by
|
|
24
|
+
* output item identity, not by event name.
|
|
26
25
|
*/
|
|
27
26
|
import type OpenAI from "openai";
|
|
28
27
|
import type { ContentPart, FinishReason, GenerateResult, StreamEvent } from "../../domain/index.js";
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* canonical StreamEvents and a final GenerateResult. The Responses protocol
|
|
4
4
|
* names the same logical output item by different keys in different event
|
|
5
5
|
* families, so this file owns the reconciliation from provider item IDs,
|
|
6
|
-
* call IDs, and output positions into
|
|
6
|
+
* call IDs, and output positions into the canonical ContentPart,
|
|
7
7
|
* ToolCall, Usage, and FinishReason vocabulary.
|
|
8
8
|
*
|
|
9
9
|
* Key decision: provider `output_index` is treated as the source-order spine.
|
|
@@ -11,18 +11,17 @@
|
|
|
11
11
|
* position and sorted only when building the final result, so persisted content
|
|
12
12
|
* follows the provider's streamed order instead of being grouped by part type.
|
|
13
13
|
*
|
|
14
|
-
* Provider-protocol
|
|
14
|
+
* Provider-protocol facts this file relies on (checked against the `openai` SDK types and docs):
|
|
15
15
|
* - `output_index` is the position of the output item in `response.output[]`.
|
|
16
16
|
* The final array order is model-dependent and the docs do NOT explicitly
|
|
17
|
-
* guarantee non-interleaving during streaming, so
|
|
17
|
+
* guarantee non-interleaving during streaming, so this file buffers by index
|
|
18
18
|
* and sorts at assembly time rather than trusting arrival order.
|
|
19
19
|
* - `response.function_call_arguments.delta` events carry `item_id` and
|
|
20
20
|
* `output_index` but not `call_id`. This is why
|
|
21
21
|
* argument deltas are buffered in `pendingDeltas` until the durable `call_id`
|
|
22
22
|
* arrives via `response.output_item.added` or a later event.
|
|
23
|
-
* -
|
|
24
|
-
*
|
|
25
|
-
* identity so the external Meridian contract does not change.
|
|
23
|
+
* - Reasoning text events are named `response.reasoning_text.*`; the accumulator is keyed by
|
|
24
|
+
* output item identity, not by event name.
|
|
26
25
|
*/
|
|
27
26
|
import { parseToolCallArguments } from "../../helpers/parse-tool-arguments.js";
|
|
28
27
|
import { assertValidUsage } from "../../shapes/index.js";
|
|
@@ -86,7 +85,7 @@ function rememberToolCallEntry(acc, entry) {
|
|
|
86
85
|
acc.toolCallsByOutputIndex.set(entry.outputIndex, entry);
|
|
87
86
|
}
|
|
88
87
|
function findToolCallEntry(acc, params) {
|
|
89
|
-
// Lookup order follows semantic strength: callId is
|
|
88
|
+
// Lookup order follows semantic strength: callId is the gateway's durable tool
|
|
90
89
|
// identity, itemId is the provider output identity, and output_index is the
|
|
91
90
|
// ordering fallback used by deltas before either ID has appeared.
|
|
92
91
|
return ((params.callId ? acc.toolCalls.get(params.callId) : undefined) ??
|
|
@@ -99,13 +98,12 @@ export function mapUsage(usage, longCacheWrites) {
|
|
|
99
98
|
// hits and reasoning tokens live in provider-specific detail objects
|
|
100
99
|
// (`input_tokens_details.cached_tokens`, `output_tokens_details.reasoning_tokens`).
|
|
101
100
|
// Both fields are confirmed in the OpenAI reasoning guide and the installed
|
|
102
|
-
// SDK's ResponseUsage shape.
|
|
101
|
+
// SDK's ResponseUsage shape. Optional Usage fields are set only when the
|
|
103
102
|
// provider reports a positive value.
|
|
104
103
|
//
|
|
105
104
|
// GPT-5.6+ also bills cache writes: `cache_write_tokens` is their total, split by
|
|
106
105
|
// lifetime in `cache_write_tokens_details` (`ttl_30m_tokens` + `ttl_12h_tokens`), and
|
|
107
|
-
// `cached_tokens` excludes them.
|
|
108
|
-
// they are read through a local shape.
|
|
106
|
+
// `cached_tokens` excludes them. Models before GPT-5.6 omit both at runtime, whatever the SDK's types say.
|
|
109
107
|
const details = usage.input_tokens_details;
|
|
110
108
|
const result = {
|
|
111
109
|
inputTokens: usage.input_tokens,
|
|
@@ -405,7 +403,7 @@ export function* eventsFromResponseStreamEvent(event, acc) {
|
|
|
405
403
|
break;
|
|
406
404
|
default:
|
|
407
405
|
// Other hosted-tool/provider events (web_search, code_interpreter, etc.)
|
|
408
|
-
// have no
|
|
406
|
+
// have no canonical ContentPart/StreamEvent mapping yet. Ignore
|
|
409
407
|
// rather than inventing a lossy custom contract at this boundary.
|
|
410
408
|
break;
|
|
411
409
|
}
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
* ChatCompletionChunk.Choice.Delta.ToolCall SDK shape) so interleaved argument
|
|
13
13
|
* fragments rebuild the correct ToolCall entries.
|
|
14
14
|
*
|
|
15
|
-
* Provider-protocol
|
|
15
|
+
* Provider-protocol facts this file relies on (checked against the `openai` SDK types and docs):
|
|
16
16
|
* - Chat Completions `finish_reason` on streaming chunks is `'stop' | 'length'
|
|
17
17
|
* | 'tool_calls' | 'content_filter' | 'function_call' | null` (SDK-confirmed).
|
|
18
18
|
* `null` means the stream is still in progress; only the final content chunk
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
* ChatCompletionChunk.Choice.Delta.ToolCall SDK shape) so interleaved argument
|
|
13
13
|
* fragments rebuild the correct ToolCall entries.
|
|
14
14
|
*
|
|
15
|
-
* Provider-protocol
|
|
15
|
+
* Provider-protocol facts this file relies on (checked against the `openai` SDK types and docs):
|
|
16
16
|
* - Chat Completions `finish_reason` on streaming chunks is `'stop' | 'length'
|
|
17
17
|
* | 'tool_calls' | 'content_filter' | 'function_call' | null` (SDK-confirmed).
|
|
18
18
|
* `null` means the stream is still in progress; only the final content chunk
|
|
@@ -119,7 +119,7 @@ export function* eventsFromOpenAIChunk(chunk, acc) {
|
|
|
119
119
|
// OpenAI-compatible providers deliver cumulative usage on the final chunk
|
|
120
120
|
// (or a trailing usage-only chunk) when stream_options.include_usage is
|
|
121
121
|
// set. Map prompt/completion tokens and optional reasoning tokens into
|
|
122
|
-
//
|
|
122
|
+
// canonical Usage. The SDK docs warn: interrupted streams may never deliver
|
|
123
123
|
// this chunk.
|
|
124
124
|
const reasoningTokens = chunk.usage.completion_tokens_details?.reasoning_tokens;
|
|
125
125
|
const cacheReadTokens = chunk.usage.prompt_tokens_details?.cached_tokens;
|
|
@@ -49,10 +49,14 @@ export declare function providerClientOptions(config: ProviderConfig, defaultBas
|
|
|
49
49
|
maxRetries: number;
|
|
50
50
|
fetch: typeof fetch;
|
|
51
51
|
};
|
|
52
|
-
/**
|
|
52
|
+
/**
|
|
53
|
+
* Anthropic client options that default from the environment, set to "none".
|
|
54
|
+
* `openTelemetry: false` stops the `ANTHROPIC_OPEN_TELEMETRY*` variables and the SDK's spans and trace headers.
|
|
55
|
+
*/
|
|
53
56
|
export declare const ANTHROPIC_ENV_OPTOUT: {
|
|
54
57
|
authToken: null;
|
|
55
58
|
webhookKey: null;
|
|
59
|
+
openTelemetry: false;
|
|
56
60
|
};
|
|
57
61
|
/** OpenAI client options that default from the environment, set to "none". */
|
|
58
62
|
export declare const OPENAI_ENV_OPTOUT: {
|
|
@@ -96,8 +96,15 @@ export function providerClientOptions(config, defaultBaseUrl) {
|
|
|
96
96
|
fetch: providerFetch,
|
|
97
97
|
};
|
|
98
98
|
}
|
|
99
|
-
/**
|
|
100
|
-
|
|
99
|
+
/**
|
|
100
|
+
* Anthropic client options that default from the environment, set to "none".
|
|
101
|
+
* `openTelemetry: false` stops the `ANTHROPIC_OPEN_TELEMETRY*` variables and the SDK's spans and trace headers.
|
|
102
|
+
*/
|
|
103
|
+
export const ANTHROPIC_ENV_OPTOUT = {
|
|
104
|
+
authToken: null,
|
|
105
|
+
webhookKey: null,
|
|
106
|
+
openTelemetry: false,
|
|
107
|
+
};
|
|
101
108
|
/** OpenAI client options that default from the environment, set to "none". */
|
|
102
109
|
export const OPENAI_ENV_OPTOUT = {
|
|
103
110
|
adminAPIKey: null,
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* What a model can take as media and how each wire format carries it.
|
|
3
|
-
*
|
|
2
|
+
* What a model can take as media and how each wire format carries it. The provider facts behind it
|
|
3
|
+
* are collected at
|
|
4
|
+
* https://github.com/haowjy/meridian-flow-docs/blob/main/kb/wiki/agents/files-the-model-sees.md
|
|
4
5
|
*
|
|
5
6
|
* Two levels, because they change independently: the model says which kinds it takes
|
|
6
7
|
* (`inputModalities`, copied from models.dev) and how large a file its provider fetches by link
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* The gateway's canonical types (the public API): the provider-neutral request,
|
|
3
3
|
* result, stream-event, message, tool, and capability shapes. The vocabulary
|
|
4
4
|
* every adapter maps its provider SDK to and from; depends on nothing provider-specific.
|
|
5
5
|
*
|
|
6
|
-
* Why this boundary exists (see
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* these types at the adapter boundary.
|
|
6
|
+
* Why this boundary exists (see the gateway's AGENTS.md): provider-specific types stay inside
|
|
7
|
+
* adapter directories so a host never imports `openai` or `@anthropic-ai/sdk` to use the gateway.
|
|
8
|
+
* Every adapter translates to and from these types at the adapter boundary.
|
|
10
9
|
*
|
|
11
10
|
* Key design invariants:
|
|
12
11
|
* - JSON-natural: every field must survive `JSON.parse(JSON.stringify(x))`.
|
|
@@ -17,7 +16,7 @@
|
|
|
17
16
|
* `tool_calls[].index`), used by adapters to reconstruct source order.
|
|
18
17
|
* - `GenerateRequest.promptCacheKey` is canonical prompt-cache *intent*, not
|
|
19
18
|
* provider syntax, so it stays top-level (never nested in `providerOptions`):
|
|
20
|
-
* the
|
|
19
|
+
* the caller knows a conversation's stable key but has no opinion on TTL or wire
|
|
21
20
|
* shape. Each adapter decides whether to forward it (a `prompt_cache_key`
|
|
22
21
|
* routing hint, or nothing). Cache breakpoints are not part of the request:
|
|
23
22
|
* the Anthropic adapter places them on the wire blocks itself.
|
|
@@ -28,20 +27,21 @@ export type ProviderOptions = Record<string, Record<string, unknown>>;
|
|
|
28
27
|
export type TokenizerFamily = "anthropic" | "o200k" | "gemini" | "deepseek";
|
|
29
28
|
/** What a model reads as input, in the models.dev vocabulary. */
|
|
30
29
|
export type InputModality = "text" | "image" | "pdf" | "audio" | "video";
|
|
31
|
-
/**
|
|
30
|
+
/** Common provider cache lifetimes in seconds (Anthropic's 5 minutes and 1 hour), for a host to put in a `PromptCacheDescriptor` or to compute a buffer from. */
|
|
32
31
|
export declare const DEFAULT_SUBAGENT_PROMPT_CACHE_TTL: number;
|
|
33
32
|
export declare const DEFAULT_PRIMARY_PROMPT_CACHE_TTL: number;
|
|
34
|
-
/**
|
|
33
|
+
/** A model's provider cache behavior and its best-known retention window. */
|
|
35
34
|
export interface PromptCacheDescriptor {
|
|
36
35
|
kind: "explicit" | "automatic";
|
|
37
|
-
/** Lifetime in seconds of a short-retention request
|
|
36
|
+
/** Lifetime in seconds of a short-retention request; null means the provider documents none. */
|
|
38
37
|
shortTtlSeconds: number | null;
|
|
39
|
-
/** Lifetime of a long-retention request
|
|
38
|
+
/** Lifetime of a long-retention request; equals `shortTtlSeconds` when the provider offers one lifetime. */
|
|
40
39
|
longTtlSeconds: number | null;
|
|
41
40
|
}
|
|
42
41
|
/**
|
|
43
|
-
* Which cache lifetime a request wants:
|
|
44
|
-
*
|
|
42
|
+
* Which cache lifetime a request wants: `long` for a conversation the caller keeps extending,
|
|
43
|
+
* `short` for a call re-read only within minutes, and `none` for a one-shot call nothing reads
|
|
44
|
+
* again (a summary): no cache mark is sent.
|
|
45
45
|
*/
|
|
46
46
|
export type PromptCacheRetention = "none" | "short" | "long";
|
|
47
47
|
/** The TTL a request with this retention gets from a model, for adapters and warm/cold prediction. */
|
|
@@ -50,7 +50,7 @@ export declare const REASONING_EFFORTS: readonly ["low", "medium", "high", "max"
|
|
|
50
50
|
export type ReasoningEffort = (typeof REASONING_EFFORTS)[number];
|
|
51
51
|
/**
|
|
52
52
|
* A model's reasoning controls, copied from its models.dev `reasoning_options`
|
|
53
|
-
* (`xhigh
|
|
53
|
+
* (`xhigh` is not in `REASONING_EFFORTS`, so it is omitted from `efforts`).
|
|
54
54
|
*/
|
|
55
55
|
export interface ReasoningSupport {
|
|
56
56
|
/** Effort levels the model accepts. */
|
|
@@ -101,7 +101,7 @@ export type ProviderModel = Omit<ModelInfo, "wire" | "imageLinks">;
|
|
|
101
101
|
* OpenAI-Chat `delta.content`.
|
|
102
102
|
* - MediaPart: an image or PDF: Anthropic `image`/`document` block, OpenAI
|
|
103
103
|
* `input_image`/`input_file` or Chat `image_url`/`file`, Gemini `fileData`/`inlineData`. Allowed in a user message and
|
|
104
|
-
* inside a tool result.
|
|
104
|
+
* inside a tool result, never in an assistant message.
|
|
105
105
|
* - ReasoningPart: Anthropic `thinking` / OpenAI `reasoning` items.
|
|
106
106
|
* Provider metadata (signature, encrypted_content) lives in providerOptions.
|
|
107
107
|
* - ToolUsePart: Anthropic `tool_use` / OpenAI `function_call` items.
|
|
@@ -171,14 +171,21 @@ export type ContentPart = TextPart | MediaPart | ReasoningPart | ToolUsePart | T
|
|
|
171
171
|
* Chat/Responses message shapes. The `tool` role carries tool_result parts
|
|
172
172
|
* back to the provider after execution.
|
|
173
173
|
*/
|
|
174
|
-
export
|
|
175
|
-
role: "system" | "user"
|
|
176
|
-
content:
|
|
177
|
-
}
|
|
174
|
+
export type Message = {
|
|
175
|
+
role: "system" | "user";
|
|
176
|
+
content: Array<TextPart | MediaPart>;
|
|
177
|
+
} | {
|
|
178
|
+
role: "tool";
|
|
179
|
+
content: ToolResultPart[];
|
|
180
|
+
} | {
|
|
181
|
+
role: "assistant";
|
|
182
|
+
content: AssistantContentPart[];
|
|
183
|
+
};
|
|
184
|
+
/** What a model produces. No provider takes media in an assistant turn, so the type has none. */
|
|
185
|
+
export type AssistantContentPart = TextPart | ReasoningPart | ToolUsePart;
|
|
178
186
|
/**
|
|
179
|
-
* A tool
|
|
180
|
-
*
|
|
181
|
-
* adapter translates them into the provider's tool schema format.
|
|
187
|
+
* A tool the caller executes, with a JSON Schema input signature. The request carries these as
|
|
188
|
+
* the tool definitions, and the adapter translates them into the provider's tool schema format.
|
|
182
189
|
*/
|
|
183
190
|
export interface Tool {
|
|
184
191
|
type: "function";
|
|
@@ -206,14 +213,14 @@ export interface GenerateRequest {
|
|
|
206
213
|
};
|
|
207
214
|
signal?: AbortSignal;
|
|
208
215
|
/**
|
|
209
|
-
* Stable
|
|
216
|
+
* Stable cache-routing key for one conversation (opaque, not a secret): unlike
|
|
210
217
|
* `correlation` below, adapters MAY map this into the provider request —
|
|
211
218
|
* e.g. OpenAI Responses `prompt_cache_key`, which improves cache-affinity
|
|
212
219
|
* routing for automatic caching. Anthropic has no equivalent concept and
|
|
213
220
|
* ignores it.
|
|
214
221
|
*/
|
|
215
222
|
promptCacheKey?: string;
|
|
216
|
-
/** Cache lifetime wanted;
|
|
223
|
+
/** Cache lifetime wanted; keep it the same for every call of one conversation (`none` for one-shot calls nothing re-reads) so the cached prefix never changes. */
|
|
217
224
|
promptCacheRetention: PromptCacheRetention;
|
|
218
225
|
/**
|
|
219
226
|
* Credentials for this call, keyed by provider id. The routed provider's entry replaces that
|
|
@@ -242,7 +249,7 @@ export interface GenerateRequest {
|
|
|
242
249
|
export type ErrorCode = "network_error" | "rate_limited" | "server_error" | "auth_error" | "invalid_request" | "content_filtered" | "context_overflow" | "provider_error";
|
|
243
250
|
/**
|
|
244
251
|
* Stream event — the canonical event set that every adapter emits and the
|
|
245
|
-
*
|
|
252
|
+
* caller consumes. Each event is a complete value of the gateway's own types; the
|
|
246
253
|
* generator produces them one-at-a-time so subscribers receive them as they
|
|
247
254
|
* arrive from the provider without waiting for the full response.
|
|
248
255
|
*
|
|
@@ -322,17 +329,15 @@ export interface ToolCall {
|
|
|
322
329
|
* Provider mapping:
|
|
323
330
|
* - Anthropic StopReason → FinishReason: `end_turn`→`end_turn`, `tool_use`→`tool_use`,
|
|
324
331
|
* `max_tokens`→`max_tokens`, `stop_sequence`→`stop_sequence`, `refusal`→`error`.
|
|
325
|
-
*
|
|
326
|
-
* installed SDK 0.100.1 union does not include it; if that value arrives at runtime
|
|
327
|
-
* the adapter maps the wire string to the canonical `context_overflow` error.
|
|
332
|
+
* `model_context_window_exceeded` maps to the canonical `context_overflow` error.
|
|
328
333
|
* - OpenAI Responses status: `completed`→`end_turn`, `failed`/`cancelled`→`error`,
|
|
329
334
|
* `incomplete` maps to `max_tokens` or `error` depending on `incomplete_details.reason`.
|
|
330
335
|
* - OpenAI-Chat finish_reason: `stop`→`end_turn`, `tool_calls`→`tool_use`,
|
|
331
336
|
* `length`→`max_tokens`, `content_filter`→`error`.
|
|
332
337
|
*
|
|
333
338
|
* Precedence: `tool_use` wins over any terminal status when tool calls were
|
|
334
|
-
* actually assembled (the
|
|
335
|
-
*
|
|
339
|
+
* actually assembled (the caller must execute the tools before treating the
|
|
340
|
+
* response as finished).
|
|
336
341
|
*/
|
|
337
342
|
export type FinishReason = "end_turn" | "tool_use" | "max_tokens" | "stop_sequence" | "error";
|
|
338
343
|
/**
|
|
@@ -343,8 +348,7 @@ export type FinishReason = "end_turn" | "tool_use" | "max_tokens" | "stop_sequen
|
|
|
343
348
|
* OpenAI Responses output_index), not grouped by part type. This preserves the
|
|
344
349
|
* model's intended content sequence: reasoning → text → tool_use interleaving.
|
|
345
350
|
*
|
|
346
|
-
* `toolCalls` is a flat list of every parsed ToolCall in the response
|
|
347
|
-
* orchestrator uses this to dispatch tool execution.
|
|
351
|
+
* `toolCalls` is a flat list of every parsed ToolCall in the response, for the caller to run.
|
|
348
352
|
*/
|
|
349
353
|
export interface GenerateResult {
|
|
350
354
|
content: ContentPart[];
|
|
@@ -447,7 +451,7 @@ export interface GatewayConfig {
|
|
|
447
451
|
initialDelayMs: number;
|
|
448
452
|
maxDelayMs: number;
|
|
449
453
|
};
|
|
450
|
-
/**
|
|
454
|
+
/** Warnings while the gateway is built (e.g. duplicate model IDs skipped). */
|
|
451
455
|
onWarning?: (span: TraceSpan) => void;
|
|
452
456
|
}
|
|
453
457
|
/** Minimal trace span passed to `GatewayConfig.onWarning`. */
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/** Common provider cache lifetimes in seconds (Anthropic's 5 minutes and 1 hour), for a host to put in a `PromptCacheDescriptor` or to compute a buffer from. */
|
|
2
2
|
export const DEFAULT_SUBAGENT_PROMPT_CACHE_TTL = 5 * 60;
|
|
3
3
|
export const DEFAULT_PRIMARY_PROMPT_CACHE_TTL = 60 * 60;
|
|
4
4
|
/** The TTL a request with this retention gets from a model, for adapters and warm/cold prediction. */
|
|
@@ -4,14 +4,13 @@
|
|
|
4
4
|
* domain message types.
|
|
5
5
|
*
|
|
6
6
|
* These are simple constructors that produce well-formed Message/ContentPart
|
|
7
|
-
* values without boilerplate.
|
|
8
|
-
* tests.
|
|
7
|
+
* values without boilerplate. Hosts and tests use them to build requests.
|
|
9
8
|
*/
|
|
10
|
-
import type {
|
|
9
|
+
import type { AssistantContentPart, MediaPart, Message, ProviderOptions, TextPart } from "../domain/index.js";
|
|
11
10
|
export declare function user(text: string): Message;
|
|
12
11
|
export declare function system(text: string): Message;
|
|
13
12
|
export declare function text(t: string, opts?: {
|
|
14
13
|
providerOptions?: ProviderOptions;
|
|
15
14
|
}): TextPart;
|
|
16
|
-
export declare function assistant(parts:
|
|
15
|
+
export declare function assistant(parts: AssistantContentPart[]): Message;
|
|
17
16
|
export declare function toolResult(toolCallId: string, content: Array<TextPart | MediaPart>, isError?: boolean): Message;
|
|
@@ -4,8 +4,7 @@
|
|
|
4
4
|
* domain message types.
|
|
5
5
|
*
|
|
6
6
|
* These are simple constructors that produce well-formed Message/ContentPart
|
|
7
|
-
* values without boilerplate.
|
|
8
|
-
* tests.
|
|
7
|
+
* values without boilerplate. Hosts and tests use them to build requests.
|
|
9
8
|
*/
|
|
10
9
|
export function user(text) {
|
|
11
10
|
return { role: "user", content: [{ type: "text", text }] };
|
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
2
|
+
* Wire contract for scripting the mock model: queued replies that answer the next model calls.
|
|
3
|
+
* It is its own entry so a host's tooling that writes scripts and the mock server that reads them
|
|
4
|
+
* validate against the same schema without importing the server.
|
|
4
5
|
*/
|
|
5
6
|
import { z } from "zod";
|
|
6
7
|
/** Repeated defaults cannot execute tools or consume queue-step budgets. */
|
|
7
8
|
export declare const mockModelDefaultResponseSchema: z.ZodObject<{
|
|
8
9
|
text: z.ZodOptional<z.ZodString>;
|
|
9
10
|
sample: z.ZodOptional<z.ZodEnum<{
|
|
10
|
-
scene: "scene";
|
|
11
11
|
critique: "critique";
|
|
12
|
-
outline: "outline";
|
|
13
12
|
markdown: "markdown";
|
|
13
|
+
outline: "outline";
|
|
14
|
+
scene: "scene";
|
|
14
15
|
}>>;
|
|
15
16
|
lorem: z.ZodOptional<z.ZodObject<{
|
|
16
17
|
words: z.ZodNumber;
|
|
@@ -28,27 +29,12 @@ export type MockModelDefaultResponse = z.infer<typeof mockModelDefaultResponseSc
|
|
|
28
29
|
* is sticky, so gateway retries and fallbacks fail deterministically.
|
|
29
30
|
*/
|
|
30
31
|
export declare const mockModelStepSchema: z.ZodObject<{
|
|
31
|
-
toolCalls: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
32
|
-
name: z.ZodString;
|
|
33
|
-
id: z.ZodOptional<z.ZodString>;
|
|
34
|
-
args: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
35
|
-
rawArgs: z.ZodOptional<z.ZodString>;
|
|
36
|
-
}, z.core.$strict>>>;
|
|
37
|
-
finishReason: z.ZodOptional<z.ZodEnum<{
|
|
38
|
-
length: "length";
|
|
39
|
-
content_filter: "content_filter";
|
|
40
|
-
}>>;
|
|
41
|
-
error: z.ZodOptional<z.ZodObject<{
|
|
42
|
-
status: z.ZodDefault<z.ZodNumber>;
|
|
43
|
-
message: z.ZodDefault<z.ZodString>;
|
|
44
|
-
}, z.core.$strict>>;
|
|
45
|
-
times: z.ZodOptional<z.ZodNumber>;
|
|
46
32
|
text: z.ZodOptional<z.ZodString>;
|
|
47
33
|
sample: z.ZodOptional<z.ZodEnum<{
|
|
48
|
-
scene: "scene";
|
|
49
34
|
critique: "critique";
|
|
50
|
-
outline: "outline";
|
|
51
35
|
markdown: "markdown";
|
|
36
|
+
outline: "outline";
|
|
37
|
+
scene: "scene";
|
|
52
38
|
}>>;
|
|
53
39
|
lorem: z.ZodOptional<z.ZodObject<{
|
|
54
40
|
words: z.ZodNumber;
|
|
@@ -59,32 +45,32 @@ export declare const mockModelStepSchema: z.ZodObject<{
|
|
|
59
45
|
delayMs: z.ZodOptional<z.ZodNumber>;
|
|
60
46
|
chunkSize: z.ZodOptional<z.ZodNumber>;
|
|
61
47
|
chunkDelayMs: z.ZodOptional<z.ZodNumber>;
|
|
48
|
+
toolCalls: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
49
|
+
name: z.ZodString;
|
|
50
|
+
id: z.ZodOptional<z.ZodString>;
|
|
51
|
+
args: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
52
|
+
rawArgs: z.ZodOptional<z.ZodString>;
|
|
53
|
+
}, z.core.$strict>>>;
|
|
54
|
+
finishReason: z.ZodOptional<z.ZodEnum<{
|
|
55
|
+
content_filter: "content_filter";
|
|
56
|
+
length: "length";
|
|
57
|
+
}>>;
|
|
58
|
+
error: z.ZodOptional<z.ZodObject<{
|
|
59
|
+
status: z.ZodDefault<z.ZodNumber>;
|
|
60
|
+
message: z.ZodDefault<z.ZodString>;
|
|
61
|
+
}, z.core.$strict>>;
|
|
62
|
+
times: z.ZodOptional<z.ZodNumber>;
|
|
62
63
|
}, z.core.$strict>;
|
|
63
64
|
export type MockModelStep = z.infer<typeof mockModelStepSchema>;
|
|
64
65
|
export declare const mockModelScriptSchema: z.ZodObject<{
|
|
65
66
|
match: z.ZodOptional<z.ZodString>;
|
|
66
67
|
steps: z.ZodArray<z.ZodObject<{
|
|
67
|
-
toolCalls: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
68
|
-
name: z.ZodString;
|
|
69
|
-
id: z.ZodOptional<z.ZodString>;
|
|
70
|
-
args: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
71
|
-
rawArgs: z.ZodOptional<z.ZodString>;
|
|
72
|
-
}, z.core.$strict>>>;
|
|
73
|
-
finishReason: z.ZodOptional<z.ZodEnum<{
|
|
74
|
-
length: "length";
|
|
75
|
-
content_filter: "content_filter";
|
|
76
|
-
}>>;
|
|
77
|
-
error: z.ZodOptional<z.ZodObject<{
|
|
78
|
-
status: z.ZodDefault<z.ZodNumber>;
|
|
79
|
-
message: z.ZodDefault<z.ZodString>;
|
|
80
|
-
}, z.core.$strict>>;
|
|
81
|
-
times: z.ZodOptional<z.ZodNumber>;
|
|
82
68
|
text: z.ZodOptional<z.ZodString>;
|
|
83
69
|
sample: z.ZodOptional<z.ZodEnum<{
|
|
84
|
-
scene: "scene";
|
|
85
70
|
critique: "critique";
|
|
86
|
-
outline: "outline";
|
|
87
71
|
markdown: "markdown";
|
|
72
|
+
outline: "outline";
|
|
73
|
+
scene: "scene";
|
|
88
74
|
}>>;
|
|
89
75
|
lorem: z.ZodOptional<z.ZodObject<{
|
|
90
76
|
words: z.ZodNumber;
|
|
@@ -95,6 +81,21 @@ export declare const mockModelScriptSchema: z.ZodObject<{
|
|
|
95
81
|
delayMs: z.ZodOptional<z.ZodNumber>;
|
|
96
82
|
chunkSize: z.ZodOptional<z.ZodNumber>;
|
|
97
83
|
chunkDelayMs: z.ZodOptional<z.ZodNumber>;
|
|
84
|
+
toolCalls: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
85
|
+
name: z.ZodString;
|
|
86
|
+
id: z.ZodOptional<z.ZodString>;
|
|
87
|
+
args: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
88
|
+
rawArgs: z.ZodOptional<z.ZodString>;
|
|
89
|
+
}, z.core.$strict>>>;
|
|
90
|
+
finishReason: z.ZodOptional<z.ZodEnum<{
|
|
91
|
+
content_filter: "content_filter";
|
|
92
|
+
length: "length";
|
|
93
|
+
}>>;
|
|
94
|
+
error: z.ZodOptional<z.ZodObject<{
|
|
95
|
+
status: z.ZodDefault<z.ZodNumber>;
|
|
96
|
+
message: z.ZodDefault<z.ZodString>;
|
|
97
|
+
}, z.core.$strict>>;
|
|
98
|
+
times: z.ZodOptional<z.ZodNumber>;
|
|
98
99
|
}, z.core.$strict>>;
|
|
99
100
|
}, z.core.$strict>;
|
|
100
101
|
export type MockModelScript = z.infer<typeof mockModelScriptSchema>;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
2
|
+
* Wire contract for scripting the mock model: queued replies that answer the next model calls.
|
|
3
|
+
* It is its own entry so a host's tooling that writes scripts and the mock server that reads them
|
|
4
|
+
* validate against the same schema without importing the server.
|
|
4
5
|
*/
|
|
5
6
|
import { z } from "zod";
|
|
6
7
|
const toolCallSchema = z
|
|
@@ -96,7 +97,7 @@ export const mockModelStepSchema = z
|
|
|
96
97
|
export const mockModelScriptSchema = z.strictObject({
|
|
97
98
|
/**
|
|
98
99
|
* Only model calls whose latest user message contains this text consume the
|
|
99
|
-
* script, so concurrent
|
|
100
|
+
* script, so concurrent conversations do not steal each other's steps. Omitted: any call.
|
|
100
101
|
*/
|
|
101
102
|
match: z.string().min(1).optional(),
|
|
102
103
|
steps: z.array(mockModelStepSchema).min(1).max(100),
|
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Gateway port: the provider-neutral interface (stream/listModels)
|
|
3
|
-
*
|
|
2
|
+
* Gateway port: the provider-neutral interface (stream/listModels) a host depends on instead of
|
|
3
|
+
* any concrete provider.
|
|
4
4
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* They never know which provider is behind the call.
|
|
5
|
+
* A caller runs `gateway.stream(request)` to get an AsyncIterable<StreamEvent> and never knows
|
|
6
|
+
* which provider is behind the call.
|
|
8
7
|
*
|
|
9
|
-
* Why a port: adapters are
|
|
10
|
-
*
|
|
11
|
-
* and route handlers never import concrete adapter factories.
|
|
8
|
+
* Why a port: adapters are chosen by config (see create-gateway.ts). The host builds the gateway
|
|
9
|
+
* once from GatewayConfig and never imports a concrete adapter factory.
|
|
12
10
|
*/
|
|
13
11
|
import type { GenerateRequest, ModelInfo, StreamEvent } from "../domain/index.js";
|
|
14
12
|
export interface Gateway {
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* to canonical StreamEvents.
|
|
7
7
|
*
|
|
8
8
|
* Why one port: every provider has a different SDK contract, but the
|
|
9
|
-
*
|
|
9
|
+
* gateway's edge is uniform. Adding a new provider means implementing
|
|
10
10
|
* this one interface and registering it in createAdapter.
|
|
11
11
|
*/
|
|
12
12
|
import type { GenerateRequest, ModelInfo, ResolvedCall, StreamEvent } from "../domain/index.js";
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The provider's own status and message, kept as
|
|
3
|
-
*
|
|
4
|
-
* never shows the message.
|
|
2
|
+
* The provider's own status and message, kept as debug evidence for the host's operators. Never
|
|
3
|
+
* put it in event payloads or model context, and never show the message to an end user.
|
|
5
4
|
*/
|
|
6
5
|
export type ProviderErrorResponse = {
|
|
7
6
|
/** HTTP status; null when the provider reported the failure inside the stream. */
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
* this plain DTO before any runtime or billing logic consumes it.
|
|
2
|
+
* The JSON-natural token usage contract the gateway returns, which a host can meter and price.
|
|
3
|
+
* Provider adapters normalize their native token accounting into this plain DTO, so a host
|
|
4
|
+
* never reads a provider's own counters.
|
|
6
5
|
*/
|
|
7
6
|
/**
|
|
8
7
|
* Normalized token usage across providers.
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
* this plain DTO before any runtime or billing logic consumes it.
|
|
2
|
+
* The JSON-natural token usage contract the gateway returns, which a host can meter and price.
|
|
3
|
+
* Provider adapters normalize their native token accounting into this plain DTO, so a host
|
|
4
|
+
* never reads a provider's own counters.
|
|
6
5
|
*/
|
|
7
6
|
/** Throws when a Usage value violates the canonical inclusive-total contract. */
|
|
8
7
|
export function assertValidUsage(usage) {
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
* one that has already streamed visible text or opened a tool call is not.
|
|
13
13
|
*/
|
|
14
14
|
import type { StreamEvent } from "./domain/index.js";
|
|
15
|
-
/** Content deltas
|
|
15
|
+
/** Content deltas a consumer can show to the end user. */
|
|
16
16
|
export declare function isFirstTokenEvent(event: StreamEvent): boolean;
|
|
17
17
|
/**
|
|
18
18
|
* Output the caller may have acted on: visible text, a tool call, or a terminal success. Reasoning and usage are excluded so an
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@meridian-flow/runtime",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "What an AI product needs to run models: a provider-neutral streaming model gateway (Anthropic, OpenAI, Gemini, OpenAI-compatible) with retry, deadlines and usage normalization.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"llm",
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
"type": "module",
|
|
25
25
|
"sideEffects": false,
|
|
26
26
|
"engines": {
|
|
27
|
-
"node": ">=
|
|
27
|
+
"node": ">=22"
|
|
28
28
|
},
|
|
29
29
|
"files": [
|
|
30
30
|
"dist"
|
|
@@ -54,17 +54,17 @@
|
|
|
54
54
|
]
|
|
55
55
|
},
|
|
56
56
|
"dependencies": {
|
|
57
|
-
"@anthropic-ai/sdk": "^
|
|
57
|
+
"@anthropic-ai/sdk": "^1.8.0",
|
|
58
58
|
"@google/genai": "^3.3.0",
|
|
59
|
-
"jsonrepair": "^3.
|
|
60
|
-
"openai": "^
|
|
61
|
-
"zod": "^4.
|
|
59
|
+
"jsonrepair": "^3.15.0",
|
|
60
|
+
"openai": "^7.57.0",
|
|
61
|
+
"zod": "^4.6.7"
|
|
62
62
|
},
|
|
63
63
|
"devDependencies": {
|
|
64
|
-
"@biomejs/biome": "^2.
|
|
65
|
-
"@types/node": "^
|
|
66
|
-
"typescript": "^
|
|
67
|
-
"vitest": "^
|
|
64
|
+
"@biomejs/biome": "^2.6.7",
|
|
65
|
+
"@types/node": "^26",
|
|
66
|
+
"typescript": "^7.0.2",
|
|
67
|
+
"vitest": "^5.1.0"
|
|
68
68
|
},
|
|
69
69
|
"scripts": {
|
|
70
70
|
"build": "rm -rf dist && tsc -p tsconfig.build.json",
|