@meridian-flow/runtime 0.0.0-stage → 0.1.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/LICENSE +202 -0
- package/README.md +67 -2
- package/dist/gateway/adapters/anthropic/adapter.d.ts +9 -0
- package/dist/gateway/adapters/anthropic/adapter.js +52 -0
- package/dist/gateway/adapters/anthropic/request-map.d.ts +46 -0
- package/dist/gateway/adapters/anthropic/request-map.js +285 -0
- package/dist/gateway/adapters/anthropic/stream-collect.d.ts +67 -0
- package/dist/gateway/adapters/anthropic/stream-collect.js +295 -0
- package/dist/gateway/adapters/anthropic/tool-schema.d.ts +8 -0
- package/dist/gateway/adapters/anthropic/tool-schema.js +68 -0
- package/dist/gateway/adapters/credentials.d.ts +25 -0
- package/dist/gateway/adapters/credentials.js +118 -0
- package/dist/gateway/adapters/gemini/adapter.d.ts +10 -0
- package/dist/gateway/adapters/gemini/adapter.js +90 -0
- package/dist/gateway/adapters/gemini/request-map.d.ts +29 -0
- package/dist/gateway/adapters/gemini/request-map.js +178 -0
- package/dist/gateway/adapters/gemini/stream-collect.d.ts +29 -0
- package/dist/gateway/adapters/gemini/stream-collect.js +116 -0
- package/dist/gateway/adapters/mock/response.d.ts +11 -0
- package/dist/gateway/adapters/mock/response.js +160 -0
- package/dist/gateway/adapters/mock/samples.d.ts +8 -0
- package/dist/gateway/adapters/mock/samples.js +133 -0
- package/dist/gateway/adapters/mock/script-queue.d.ts +10 -0
- package/dist/gateway/adapters/mock/script-queue.js +60 -0
- package/dist/gateway/adapters/mock/server.d.ts +16 -0
- package/dist/gateway/adapters/mock/server.js +342 -0
- package/dist/gateway/adapters/mock/write-directive.d.ts +15 -0
- package/dist/gateway/adapters/mock/write-directive.js +21 -0
- package/dist/gateway/adapters/openai/adapter.d.ts +3 -0
- package/dist/gateway/adapters/openai/adapter.js +35 -0
- package/dist/gateway/adapters/openai/request-map.d.ts +31 -0
- package/dist/gateway/adapters/openai/request-map.js +228 -0
- package/dist/gateway/adapters/openai/stream-collect.d.ts +63 -0
- package/dist/gateway/adapters/openai/stream-collect.js +490 -0
- package/dist/gateway/adapters/openai-compatible/adapter.d.ts +3 -0
- package/dist/gateway/adapters/openai-compatible/adapter.js +35 -0
- package/dist/gateway/adapters/openai-compatible/request-map.d.ts +31 -0
- package/dist/gateway/adapters/openai-compatible/request-map.js +180 -0
- package/dist/gateway/adapters/openai-compatible/stream-collect.d.ts +84 -0
- package/dist/gateway/adapters/openai-compatible/stream-collect.js +215 -0
- package/dist/gateway/adapters/provider-http-error.d.ts +65 -0
- package/dist/gateway/adapters/provider-http-error.js +188 -0
- package/dist/gateway/adapters/provider-stream.d.ts +23 -0
- package/dist/gateway/adapters/provider-stream.js +58 -0
- package/dist/gateway/attempt-stream.d.ts +27 -0
- package/dist/gateway/attempt-stream.js +329 -0
- package/dist/gateway/create-gateway.d.ts +25 -0
- package/dist/gateway/create-gateway.js +88 -0
- package/dist/gateway/deadline.d.ts +56 -0
- package/dist/gateway/deadline.js +120 -0
- package/dist/gateway/domain/index.d.ts +10 -0
- package/dist/gateway/domain/index.js +10 -0
- package/dist/gateway/domain/media.d.ts +62 -0
- package/dist/gateway/domain/media.js +126 -0
- package/dist/gateway/domain/metering.d.ts +7 -0
- package/dist/gateway/domain/metering.js +6 -0
- package/dist/gateway/domain/resolve-call.d.ts +24 -0
- package/dist/gateway/domain/resolve-call.js +39 -0
- package/dist/gateway/domain/types.d.ts +457 -0
- package/dist/gateway/domain/types.js +10 -0
- package/dist/gateway/helpers/messages.d.ts +17 -0
- package/dist/gateway/helpers/messages.js +29 -0
- package/dist/gateway/helpers/parse-tool-arguments.d.ts +9 -0
- package/dist/gateway/helpers/parse-tool-arguments.js +38 -0
- package/dist/gateway/helpers/tool-call-pairing.d.ts +13 -0
- package/dist/gateway/helpers/tool-call-pairing.js +25 -0
- package/dist/gateway/index.d.ts +12 -0
- package/dist/gateway/index.js +11 -0
- package/dist/gateway/mock-script.d.ts +129 -0
- package/dist/gateway/mock-script.js +129 -0
- package/dist/gateway/mock.d.ts +11 -0
- package/dist/gateway/mock.js +22 -0
- package/dist/gateway/ports/gateway.d.ts +18 -0
- package/dist/gateway/ports/gateway.js +1 -0
- package/dist/gateway/ports/provider-adapter.d.ts +21 -0
- package/dist/gateway/ports/provider-adapter.js +1 -0
- package/dist/gateway/routing.d.ts +28 -0
- package/dist/gateway/routing.js +48 -0
- package/dist/gateway/shapes/index.d.ts +7 -0
- package/dist/gateway/shapes/index.js +1 -0
- package/dist/gateway/shapes/json.d.ts +4 -0
- package/dist/gateway/shapes/json.js +1 -0
- package/dist/gateway/shapes/provider-error.d.ts +10 -0
- package/dist/gateway/shapes/provider-error.js +1 -0
- package/dist/gateway/shapes/usage.d.ts +41 -0
- package/dist/gateway/shapes/usage.js +29 -0
- package/dist/gateway/stream-events.d.ts +21 -0
- package/dist/gateway/stream-events.js +26 -0
- package/package.json +62 -3
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import { wireMediaSource } from "../../domain/media.js";
|
|
2
|
+
function textFromParts(parts) {
|
|
3
|
+
return parts
|
|
4
|
+
.filter((p) => p.type === "text")
|
|
5
|
+
.map((p) => p.text)
|
|
6
|
+
.join("");
|
|
7
|
+
}
|
|
8
|
+
/** Images go by link (where the host fetches them) or as a data URL; a PDF is bytes only (`file` with a filename), as Chat Completions has no file URL. */
|
|
9
|
+
function mapMedia(part, target) {
|
|
10
|
+
const media = wireMediaSource(target, part);
|
|
11
|
+
if (media.kind === "image") {
|
|
12
|
+
const url = "url" in media ? media.url : `data:${media.mediaType};base64,${media.data}`;
|
|
13
|
+
return { type: "image_url", image_url: { url } };
|
|
14
|
+
}
|
|
15
|
+
if ("url" in media)
|
|
16
|
+
throw new Error("Chat Completions has no file URL for a PDF");
|
|
17
|
+
return {
|
|
18
|
+
type: "file",
|
|
19
|
+
file: { filename: media.filename, file_data: `data:${media.mediaType};base64,${media.data}` },
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Map canonical ContentPart[] to Chat Completions content.
|
|
24
|
+
* Text-only messages use the string shorthand; messages with media use the
|
|
25
|
+
* array format. Non-text/non-media parts are silently dropped because Chat
|
|
26
|
+
* Completions has no native reasoning_part or tool_use_part in user/assistant
|
|
27
|
+
* message content.
|
|
28
|
+
*/
|
|
29
|
+
function mapContentParts(parts, target) {
|
|
30
|
+
if (!parts.some((p) => p.type === "media"))
|
|
31
|
+
return textFromParts(parts);
|
|
32
|
+
const mapped = [];
|
|
33
|
+
for (const part of parts) {
|
|
34
|
+
if (part.type === "text" && part.text.length > 0)
|
|
35
|
+
mapped.push({ type: "text", text: part.text });
|
|
36
|
+
else if (part.type === "media")
|
|
37
|
+
mapped.push(mapMedia(part, target));
|
|
38
|
+
}
|
|
39
|
+
return mapped;
|
|
40
|
+
}
|
|
41
|
+
function mapSystemMessage(message) {
|
|
42
|
+
const text = textFromParts(message.content.filter((p) => p.type === "text" && p.text.length > 0));
|
|
43
|
+
return text ? { role: "system", content: text } : null;
|
|
44
|
+
}
|
|
45
|
+
function hasContent(content) {
|
|
46
|
+
if (content === null)
|
|
47
|
+
return false;
|
|
48
|
+
if (typeof content === "string")
|
|
49
|
+
return content.length > 0;
|
|
50
|
+
return content.length > 0;
|
|
51
|
+
}
|
|
52
|
+
function mapToolMessage(message) {
|
|
53
|
+
const result = message.content.find((p) => p.type === "tool_result");
|
|
54
|
+
if (!result)
|
|
55
|
+
return null;
|
|
56
|
+
const text = textFromParts(result.content);
|
|
57
|
+
const hasMedia = result.content.some((p) => p.type === "media");
|
|
58
|
+
return {
|
|
59
|
+
role: "tool",
|
|
60
|
+
tool_call_id: result.toolCallId,
|
|
61
|
+
content: text.length > 0 || !hasMedia ? text : "(attachments follow)",
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
/** The user message that carries a run of tool messages' media, or null when they had none. */
|
|
65
|
+
function toolMediaMessage(run, target) {
|
|
66
|
+
const content = [];
|
|
67
|
+
for (const message of run) {
|
|
68
|
+
const result = message.content.find((p) => p.type === "tool_result");
|
|
69
|
+
const media = result?.content.filter((p) => p.type === "media") ?? [];
|
|
70
|
+
if (!result || media.length === 0)
|
|
71
|
+
continue;
|
|
72
|
+
content.push({
|
|
73
|
+
type: "text",
|
|
74
|
+
text: `Attachments from the result of tool call ${result.toolCallId}:`,
|
|
75
|
+
});
|
|
76
|
+
for (const part of media)
|
|
77
|
+
content.push(mapMedia(part, target));
|
|
78
|
+
}
|
|
79
|
+
return content.length > 0 ? { role: "user", content } : null;
|
|
80
|
+
}
|
|
81
|
+
/** Map a message list; a run of tool messages is followed by one user message with their media. */
|
|
82
|
+
function mapMessages(messages, target) {
|
|
83
|
+
const mapped = [];
|
|
84
|
+
for (let i = 0; i < messages.length; i++) {
|
|
85
|
+
if (messages[i].role !== "tool") {
|
|
86
|
+
const next = mapMessage(messages[i], target);
|
|
87
|
+
if (next)
|
|
88
|
+
mapped.push(next);
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
const run = [];
|
|
92
|
+
while (i < messages.length && messages[i].role === "tool")
|
|
93
|
+
run.push(messages[i++]);
|
|
94
|
+
i--;
|
|
95
|
+
for (const message of run) {
|
|
96
|
+
const next = mapToolMessage(message);
|
|
97
|
+
if (next)
|
|
98
|
+
mapped.push(next);
|
|
99
|
+
}
|
|
100
|
+
const media = toolMediaMessage(run, target);
|
|
101
|
+
if (media)
|
|
102
|
+
mapped.push(media);
|
|
103
|
+
}
|
|
104
|
+
return mapped;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Map a canonical Message to a Chat Completions message param.
|
|
108
|
+
*
|
|
109
|
+
* Role mapping: tool → Chat Completions `tool` role with tool_call_id;
|
|
110
|
+
* assistant with tool_use parts → assistant with tool_calls array;
|
|
111
|
+
* other roles pass through directly. Messages with empty content are
|
|
112
|
+
* filtered out (null return).
|
|
113
|
+
*/
|
|
114
|
+
function mapMessage(message, target) {
|
|
115
|
+
if (message.role === "system")
|
|
116
|
+
return mapSystemMessage(message);
|
|
117
|
+
if (message.role === "tool")
|
|
118
|
+
return mapToolMessage(message);
|
|
119
|
+
if (message.role === "assistant") {
|
|
120
|
+
const toolUses = message.content.filter((p) => p.type === "tool_use");
|
|
121
|
+
if (toolUses.length > 0) {
|
|
122
|
+
return {
|
|
123
|
+
role: "assistant",
|
|
124
|
+
content: textFromParts(message.content) || null,
|
|
125
|
+
tool_calls: toolUses.map((tu) => ({
|
|
126
|
+
id: tu.toolCallId,
|
|
127
|
+
type: "function",
|
|
128
|
+
function: {
|
|
129
|
+
name: tu.toolName,
|
|
130
|
+
arguments: JSON.stringify(tu.input),
|
|
131
|
+
},
|
|
132
|
+
})),
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
const content = mapContentParts(message.content, target);
|
|
137
|
+
if (!hasContent(content))
|
|
138
|
+
return null;
|
|
139
|
+
return {
|
|
140
|
+
role: message.role,
|
|
141
|
+
content,
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Map canonical Tool[] to Chat Completions tools.
|
|
146
|
+
* Each function tool becomes a `function` tool with name,
|
|
147
|
+
* description, and parameters (inputSchema).
|
|
148
|
+
*/
|
|
149
|
+
function mapTools(tools) {
|
|
150
|
+
if (!tools?.length)
|
|
151
|
+
return undefined;
|
|
152
|
+
return tools.map((tool) => ({
|
|
153
|
+
type: "function",
|
|
154
|
+
function: {
|
|
155
|
+
name: tool.name,
|
|
156
|
+
description: tool.description,
|
|
157
|
+
parameters: tool.inputSchema,
|
|
158
|
+
},
|
|
159
|
+
}));
|
|
160
|
+
}
|
|
161
|
+
/** The SDK type predates `max`; models whose row allows it accept it. */
|
|
162
|
+
function mapReasoningEffort(reasoning) {
|
|
163
|
+
return typeof reasoning === "object" ? reasoning.effort : undefined;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Assemble the full ChatCompletionCreateParams from a canonical
|
|
167
|
+
* GenerateRequest. Always sets stream:true and stream_options with
|
|
168
|
+
* include_usage:true (per OpenAI streaming docs) so the final chunk
|
|
169
|
+
* carries cumulative token counts.
|
|
170
|
+
*/
|
|
171
|
+
export function toOpenAIChatCompletionParams(request, model, call) {
|
|
172
|
+
return {
|
|
173
|
+
model: model.id,
|
|
174
|
+
messages: mapMessages(request.messages, model),
|
|
175
|
+
tools: mapTools(request.tools),
|
|
176
|
+
reasoning_effort: mapReasoningEffort(call.reasoning),
|
|
177
|
+
stream: true,
|
|
178
|
+
stream_options: { include_usage: true },
|
|
179
|
+
};
|
|
180
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenAI-compatible stream accumulator: folds Chat Completions streaming chunks
|
|
3
|
+
* into canonical StreamEvents and a final GenerateResult. This adapter targets
|
|
4
|
+
* providers that expose OpenAI's Chat Completions shape rather than the newer
|
|
5
|
+
* Responses item stream, so ordering information is less expressive: text is a
|
|
6
|
+
* choice-level delta stream, while tool calls are keyed by `delta.tool_calls[n].index`.
|
|
7
|
+
*
|
|
8
|
+
* Key decision: preserve the Chat Completions semantics directly. Text is kept
|
|
9
|
+
* in a single assistant text buffer because the protocol does not expose stable
|
|
10
|
+
* content-block positions for separate text parts; tool calls are accumulated by
|
|
11
|
+
* their `tool_calls[].index` (the required fragment-grouping key per the
|
|
12
|
+
* ChatCompletionChunk.Choice.Delta.ToolCall SDK shape) so interleaved argument
|
|
13
|
+
* fragments rebuild the correct ToolCall entries.
|
|
14
|
+
*
|
|
15
|
+
* Provider-protocol grounding (verified against OpenAI SDK 4.104.0):
|
|
16
|
+
* - Chat Completions `finish_reason` on streaming chunks is `'stop' | 'length'
|
|
17
|
+
* | 'tool_calls' | 'content_filter' | 'function_call' | null` (SDK-confirmed).
|
|
18
|
+
* `null` means the stream is still in progress; only the final content chunk
|
|
19
|
+
* carries a non-null finish_reason.
|
|
20
|
+
* - `usage` on streamed chat chunks is only present when
|
|
21
|
+
* `stream_options: { include_usage: true }` is set. It is null on earlier
|
|
22
|
+
* chunks; the final chunk may be empty apart from usage. Interrupted or
|
|
23
|
+
* cancelled streams may never deliver the final usage chunk (docs warning).
|
|
24
|
+
* - `tool_calls[].index` is a required field on Delta.ToolCall (SDK-confirmed)
|
|
25
|
+
* and is the stable identity for one tool call within the streamed assistant
|
|
26
|
+
* message. ID, name, and arguments for the same index can be split across
|
|
27
|
+
* multiple chunks.
|
|
28
|
+
* - There is NO separate reasoning channel in the OpenAI Chat Completions
|
|
29
|
+
* protocol. Any `reasoning_content` handling (e.g., DeepSeek) is
|
|
30
|
+
* provider-specific and outside the OpenAI SDK surface — it is accessed via
|
|
31
|
+
* untyped property access on the delta object.
|
|
32
|
+
*/
|
|
33
|
+
import type { ContentPart, FinishReason, GenerateResult, StreamEvent } from "../../domain/index.js";
|
|
34
|
+
import { type Usage } from "../../shapes/index.js";
|
|
35
|
+
interface StreamAccumulator {
|
|
36
|
+
contentParts: ContentPart[];
|
|
37
|
+
textBuffer: string;
|
|
38
|
+
reasoningBuffer: string;
|
|
39
|
+
toolCalls: Map<number, {
|
|
40
|
+
id?: string;
|
|
41
|
+
name: string;
|
|
42
|
+
arguments: string;
|
|
43
|
+
pendingDeltas: string[];
|
|
44
|
+
}>;
|
|
45
|
+
usage: Usage;
|
|
46
|
+
finishReason: FinishReason;
|
|
47
|
+
model: string;
|
|
48
|
+
provider: string;
|
|
49
|
+
}
|
|
50
|
+
export declare function createStreamAccumulator(model: string, provider: string): StreamAccumulator;
|
|
51
|
+
export declare function flushTextBuffer(acc: StreamAccumulator): void;
|
|
52
|
+
export declare function buildGenerateResult(acc: StreamAccumulator): GenerateResult;
|
|
53
|
+
export declare function applyUsage(acc: StreamAccumulator, usage: Usage): void;
|
|
54
|
+
export declare function mapFinishReason(reason: string | null | undefined, hasToolCalls: boolean): FinishReason;
|
|
55
|
+
export declare function eventsFromOpenAIChunk(chunk: {
|
|
56
|
+
id?: string;
|
|
57
|
+
choices?: Array<{
|
|
58
|
+
index?: number;
|
|
59
|
+
delta?: {
|
|
60
|
+
content?: string | null;
|
|
61
|
+
tool_calls?: Array<{
|
|
62
|
+
index?: number;
|
|
63
|
+
id?: string;
|
|
64
|
+
function?: {
|
|
65
|
+
name?: string;
|
|
66
|
+
arguments?: string;
|
|
67
|
+
};
|
|
68
|
+
}>;
|
|
69
|
+
};
|
|
70
|
+
finish_reason?: string | null;
|
|
71
|
+
}>;
|
|
72
|
+
usage?: {
|
|
73
|
+
prompt_tokens?: number;
|
|
74
|
+
completion_tokens?: number;
|
|
75
|
+
total_tokens?: number;
|
|
76
|
+
completion_tokens_details?: {
|
|
77
|
+
reasoning_tokens?: number;
|
|
78
|
+
};
|
|
79
|
+
prompt_tokens_details?: {
|
|
80
|
+
cached_tokens?: number;
|
|
81
|
+
};
|
|
82
|
+
} | null;
|
|
83
|
+
}, acc: StreamAccumulator): Generator<StreamEvent>;
|
|
84
|
+
export {};
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenAI-compatible stream accumulator: folds Chat Completions streaming chunks
|
|
3
|
+
* into canonical StreamEvents and a final GenerateResult. This adapter targets
|
|
4
|
+
* providers that expose OpenAI's Chat Completions shape rather than the newer
|
|
5
|
+
* Responses item stream, so ordering information is less expressive: text is a
|
|
6
|
+
* choice-level delta stream, while tool calls are keyed by `delta.tool_calls[n].index`.
|
|
7
|
+
*
|
|
8
|
+
* Key decision: preserve the Chat Completions semantics directly. Text is kept
|
|
9
|
+
* in a single assistant text buffer because the protocol does not expose stable
|
|
10
|
+
* content-block positions for separate text parts; tool calls are accumulated by
|
|
11
|
+
* their `tool_calls[].index` (the required fragment-grouping key per the
|
|
12
|
+
* ChatCompletionChunk.Choice.Delta.ToolCall SDK shape) so interleaved argument
|
|
13
|
+
* fragments rebuild the correct ToolCall entries.
|
|
14
|
+
*
|
|
15
|
+
* Provider-protocol grounding (verified against OpenAI SDK 4.104.0):
|
|
16
|
+
* - Chat Completions `finish_reason` on streaming chunks is `'stop' | 'length'
|
|
17
|
+
* | 'tool_calls' | 'content_filter' | 'function_call' | null` (SDK-confirmed).
|
|
18
|
+
* `null` means the stream is still in progress; only the final content chunk
|
|
19
|
+
* carries a non-null finish_reason.
|
|
20
|
+
* - `usage` on streamed chat chunks is only present when
|
|
21
|
+
* `stream_options: { include_usage: true }` is set. It is null on earlier
|
|
22
|
+
* chunks; the final chunk may be empty apart from usage. Interrupted or
|
|
23
|
+
* cancelled streams may never deliver the final usage chunk (docs warning).
|
|
24
|
+
* - `tool_calls[].index` is a required field on Delta.ToolCall (SDK-confirmed)
|
|
25
|
+
* and is the stable identity for one tool call within the streamed assistant
|
|
26
|
+
* message. ID, name, and arguments for the same index can be split across
|
|
27
|
+
* multiple chunks.
|
|
28
|
+
* - There is NO separate reasoning channel in the OpenAI Chat Completions
|
|
29
|
+
* protocol. Any `reasoning_content` handling (e.g., DeepSeek) is
|
|
30
|
+
* provider-specific and outside the OpenAI SDK surface — it is accessed via
|
|
31
|
+
* untyped property access on the delta object.
|
|
32
|
+
*/
|
|
33
|
+
import { parseToolCallArguments } from "../../helpers/parse-tool-arguments.js";
|
|
34
|
+
import { assertValidUsage } from "../../shapes/index.js";
|
|
35
|
+
function emptyUsage() {
|
|
36
|
+
return { inputTokens: 0, outputTokens: 0 };
|
|
37
|
+
}
|
|
38
|
+
export function createStreamAccumulator(model, provider) {
|
|
39
|
+
return {
|
|
40
|
+
contentParts: [],
|
|
41
|
+
textBuffer: "",
|
|
42
|
+
reasoningBuffer: "",
|
|
43
|
+
toolCalls: new Map(),
|
|
44
|
+
usage: emptyUsage(),
|
|
45
|
+
finishReason: "end_turn",
|
|
46
|
+
model,
|
|
47
|
+
provider,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
export function flushTextBuffer(acc) {
|
|
51
|
+
// The provider may split one assistant message over many content deltas. Delay
|
|
52
|
+
// creating the canonical TextPart until the end so callers do not persist one
|
|
53
|
+
// block per token/chunk.
|
|
54
|
+
if (acc.textBuffer) {
|
|
55
|
+
acc.contentParts.push({ type: "text", text: acc.textBuffer });
|
|
56
|
+
acc.textBuffer = "";
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
export function buildGenerateResult(acc) {
|
|
60
|
+
// Chat Completions has no final response object with reconstructed content, so
|
|
61
|
+
// the GenerateResult is built entirely from accumulated deltas.
|
|
62
|
+
if (acc.reasoningBuffer) {
|
|
63
|
+
acc.contentParts.push({ type: "reasoning", text: acc.reasoningBuffer });
|
|
64
|
+
acc.reasoningBuffer = "";
|
|
65
|
+
}
|
|
66
|
+
flushTextBuffer(acc);
|
|
67
|
+
const toolCalls = [];
|
|
68
|
+
for (const [index, entry] of acc.toolCalls.entries()) {
|
|
69
|
+
// Some compatible providers omit tool_call.id in early or all chunks. The
|
|
70
|
+
// fallback is deterministic within the response so the canonical contract
|
|
71
|
+
// still has an ID for tool_result references.
|
|
72
|
+
const id = entry.id ?? `call_${index}`;
|
|
73
|
+
const parsed = parseToolCallArguments(entry.arguments);
|
|
74
|
+
const input = parsed.ok ? parsed.arguments : {};
|
|
75
|
+
const parseError = parsed.ok ? undefined : { raw: parsed.raw, message: parsed.message };
|
|
76
|
+
acc.contentParts.push({
|
|
77
|
+
type: "tool_use",
|
|
78
|
+
toolCallId: id,
|
|
79
|
+
toolName: entry.name,
|
|
80
|
+
input,
|
|
81
|
+
...(parseError ? { inputParseError: parseError } : {}),
|
|
82
|
+
});
|
|
83
|
+
toolCalls.push({
|
|
84
|
+
id,
|
|
85
|
+
name: entry.name,
|
|
86
|
+
arguments: input,
|
|
87
|
+
...(parseError ? { argumentsParseError: parseError } : {}),
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
return {
|
|
91
|
+
content: [...acc.contentParts],
|
|
92
|
+
toolCalls,
|
|
93
|
+
finishReason: acc.finishReason,
|
|
94
|
+
usage: acc.usage,
|
|
95
|
+
model: acc.model,
|
|
96
|
+
provider: acc.provider,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
export function applyUsage(acc, usage) {
|
|
100
|
+
// Usage is delivered out-of-band on chunks only when stream_options includes
|
|
101
|
+
// `include_usage` (SDK-confirmed: ChatCompletionStreamOptions.include_usage).
|
|
102
|
+
// The SDK docs warn that interrupted/cancelled streams may never deliver the
|
|
103
|
+
// final usage chunk. Replace the accumulator with the provider's latest totals.
|
|
104
|
+
acc.usage = usage;
|
|
105
|
+
}
|
|
106
|
+
export function mapFinishReason(reason, hasToolCalls) {
|
|
107
|
+
// Partial or filtered tool calls are not executable, even when assembled.
|
|
108
|
+
if (reason === "length")
|
|
109
|
+
return "max_tokens";
|
|
110
|
+
if (reason === "content_filter")
|
|
111
|
+
return "error";
|
|
112
|
+
// Compatible providers may omit tool_calls on otherwise successful output.
|
|
113
|
+
if (hasToolCalls || reason === "tool_calls")
|
|
114
|
+
return "tool_use";
|
|
115
|
+
return "end_turn";
|
|
116
|
+
}
|
|
117
|
+
export function* eventsFromOpenAIChunk(chunk, acc) {
|
|
118
|
+
if (chunk.usage) {
|
|
119
|
+
// OpenAI-compatible providers deliver cumulative usage on the final chunk
|
|
120
|
+
// (or a trailing usage-only chunk) when stream_options.include_usage is
|
|
121
|
+
// set. Map prompt/completion tokens and optional reasoning tokens into
|
|
122
|
+
// Meridian Usage. The SDK docs warn: interrupted streams may never deliver
|
|
123
|
+
// this chunk.
|
|
124
|
+
const reasoningTokens = chunk.usage.completion_tokens_details?.reasoning_tokens;
|
|
125
|
+
const cacheReadTokens = chunk.usage.prompt_tokens_details?.cached_tokens;
|
|
126
|
+
const usage = {
|
|
127
|
+
inputTokens: chunk.usage.prompt_tokens ?? 0,
|
|
128
|
+
outputTokens: chunk.usage.completion_tokens ?? 0,
|
|
129
|
+
...(reasoningTokens ? { reasoningTokens } : {}),
|
|
130
|
+
...(cacheReadTokens ? { cacheReadTokens } : {}),
|
|
131
|
+
};
|
|
132
|
+
assertValidUsage(usage);
|
|
133
|
+
applyUsage(acc, usage);
|
|
134
|
+
yield { type: "usage", usage };
|
|
135
|
+
}
|
|
136
|
+
// This adapter consumes only the first streamed choice. The gateway API models
|
|
137
|
+
// one assistant continuation per request, and request mapping never asks for
|
|
138
|
+
// multiple choices. Usage-only terminal chunks intentionally continue through
|
|
139
|
+
// the provider-neutral usage path above before this choice-specific exit.
|
|
140
|
+
const choice = chunk.choices?.[0];
|
|
141
|
+
if (!choice)
|
|
142
|
+
return;
|
|
143
|
+
// Compatible providers carry visible thinking in this extension. Preserve it
|
|
144
|
+
// for the transcript, without inventing a provider signature or replay payload.
|
|
145
|
+
const reasoningContent = choice.delta?.reasoning_content;
|
|
146
|
+
if (typeof reasoningContent === "string" && reasoningContent) {
|
|
147
|
+
acc.reasoningBuffer += reasoningContent;
|
|
148
|
+
yield { type: "reasoning.delta", text: reasoningContent };
|
|
149
|
+
}
|
|
150
|
+
if (choice.delta?.content) {
|
|
151
|
+
// Choice-level text deltas have no per-part index. Accumulate into the
|
|
152
|
+
// response text buffer and emit canonical text.delta without partIndex.
|
|
153
|
+
acc.textBuffer += choice.delta.content;
|
|
154
|
+
yield { type: "text.delta", text: choice.delta.content };
|
|
155
|
+
}
|
|
156
|
+
if (choice.delta?.tool_calls) {
|
|
157
|
+
for (const tc of choice.delta.tool_calls) {
|
|
158
|
+
// `index` is the required stable identity for one tool call within the
|
|
159
|
+
// streamed assistant message (Delta.ToolCall.index, SDK-confirmed).
|
|
160
|
+
// ID/name/arguments can be split across chunks for the same index; the
|
|
161
|
+
// provider may omit `id` in early chunks and supply it later.
|
|
162
|
+
const index = tc.index ?? 0;
|
|
163
|
+
let entry = acc.toolCalls.get(index);
|
|
164
|
+
if (!entry) {
|
|
165
|
+
entry = {
|
|
166
|
+
id: tc.id,
|
|
167
|
+
name: tc.function?.name ?? "",
|
|
168
|
+
arguments: "",
|
|
169
|
+
pendingDeltas: [],
|
|
170
|
+
};
|
|
171
|
+
acc.toolCalls.set(index, entry);
|
|
172
|
+
}
|
|
173
|
+
if (tc.function?.name)
|
|
174
|
+
entry.name = tc.function.name;
|
|
175
|
+
if (tc.id) {
|
|
176
|
+
// Once the provider supplies the durable tool-call ID, buffered argument
|
|
177
|
+
// fragments can be emitted as canonical tool_call.delta events tied to
|
|
178
|
+
// that ID. Some compatible providers omit ID in early or all chunks;
|
|
179
|
+
// buildGenerateResult falls back to a deterministic `call_<index>` ID.
|
|
180
|
+
entry.id = tc.id;
|
|
181
|
+
for (const argumentsDelta of entry.pendingDeltas.splice(0)) {
|
|
182
|
+
yield {
|
|
183
|
+
type: "tool_call.delta",
|
|
184
|
+
id: entry.id,
|
|
185
|
+
name: entry.name,
|
|
186
|
+
argumentsDelta,
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
if (tc.function?.arguments) {
|
|
191
|
+
// Arguments are streamed as raw JSON fragments. As with Responses, do
|
|
192
|
+
// not emit a canonical delta before an ID is known because downstream
|
|
193
|
+
// tool results need a stable tool_call_id reference.
|
|
194
|
+
entry.arguments += tc.function.arguments;
|
|
195
|
+
if (entry.id) {
|
|
196
|
+
yield {
|
|
197
|
+
type: "tool_call.delta",
|
|
198
|
+
id: entry.id,
|
|
199
|
+
name: entry.name,
|
|
200
|
+
argumentsDelta: tc.function.arguments,
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
else {
|
|
204
|
+
// Keep early fragments in arrival order until the ID arrives.
|
|
205
|
+
entry.pendingDeltas.push(tc.function.arguments);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
if (choice.finish_reason) {
|
|
211
|
+
// Finish reason can arrive on the last choice delta, after all content/tool
|
|
212
|
+
// chunks. Combine it with observed tool calls for canonical finishReason.
|
|
213
|
+
acc.finishReason = mapFinishReason(choice.finish_reason, acc.toolCalls.size > 0);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared provider SDK setup and error mapping: the client options every adapter uses, one
|
|
3
|
+
* HTTP-status retry policy, the provider's retry headers, and the provider's own status and
|
|
4
|
+
* message as debug evidence.
|
|
5
|
+
*
|
|
6
|
+
* Retry policy: network errors, status-less SDK failures, 408, 429 and 5xx retry. Every
|
|
7
|
+
* other 4xx is the provider refusing this request (402 out of balance, 404 unknown
|
|
8
|
+
* model, 413/422 rejected payload); sending it again gets the same answer.
|
|
9
|
+
*/
|
|
10
|
+
import type { ErrorCode, ProviderConfig } from "../domain/index.js";
|
|
11
|
+
import type { ProviderErrorResponse } from "../shapes/index.js";
|
|
12
|
+
type MappedProviderError = {
|
|
13
|
+
code: ErrorCode;
|
|
14
|
+
message: string;
|
|
15
|
+
retryable: boolean;
|
|
16
|
+
retryAfterMs?: number;
|
|
17
|
+
providerError?: ProviderErrorResponse;
|
|
18
|
+
};
|
|
19
|
+
/** Both matched against the lowercased 400 message. */
|
|
20
|
+
export type ProviderErrorPatterns = {
|
|
21
|
+
contextOverflow: RegExp;
|
|
22
|
+
contentFiltered: RegExp;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* A provider's error can echo a media link it failed to fetch, and a signed link's query string
|
|
26
|
+
* is a credential, so error text never keeps one.
|
|
27
|
+
*/
|
|
28
|
+
export declare function stripUrlQueries(text: string): string;
|
|
29
|
+
/**
|
|
30
|
+
* `fetch` for provider SDK clients: records each failed response's exact body text, so
|
|
31
|
+
* the provider's message survives where the SDK's parse drops it (OpenAI's reads a body
|
|
32
|
+
* without `error` as "402 status code (no body)").
|
|
33
|
+
*/
|
|
34
|
+
export declare const providerFetch: typeof fetch;
|
|
35
|
+
/**
|
|
36
|
+
* Constructor options every adapter's SDK client shares. `maxRetries: 0` because the
|
|
37
|
+
* gateway owns retries (`streamWithRetry`); SDK retries would multiply them.
|
|
38
|
+
*
|
|
39
|
+
* Both SDKs fill every option left `undefined` from process environment variables
|
|
40
|
+
* (`*_BASE_URL`, `*_AUTH_TOKEN`, `*_ORG_ID`, `*_LOG`, ...), which would redirect requests or add
|
|
41
|
+
* credentials the config never named. So each option is passed explicitly: the adapter
|
|
42
|
+
* adds the SDK-specific ones with `null` (see `ANTHROPIC_ENV_OPTOUT`, `OPENAI_ENV_OPTOUT`).
|
|
43
|
+
* The SDKs' `*_CUSTOM_HEADERS` has no opt-out option and still applies.
|
|
44
|
+
*/
|
|
45
|
+
export declare function providerClientOptions(config: ProviderConfig, defaultBaseUrl: string): {
|
|
46
|
+
baseURL: string;
|
|
47
|
+
defaultHeaders: Record<string, string> | undefined;
|
|
48
|
+
logLevel: "warn";
|
|
49
|
+
maxRetries: number;
|
|
50
|
+
fetch: typeof fetch;
|
|
51
|
+
};
|
|
52
|
+
/** Anthropic client options that default from the environment, set to "none". */
|
|
53
|
+
export declare const ANTHROPIC_ENV_OPTOUT: {
|
|
54
|
+
authToken: null;
|
|
55
|
+
webhookKey: null;
|
|
56
|
+
};
|
|
57
|
+
/** OpenAI client options that default from the environment, set to "none". */
|
|
58
|
+
export declare const OPENAI_ENV_OPTOUT: {
|
|
59
|
+
adminAPIKey: null;
|
|
60
|
+
organization: null;
|
|
61
|
+
project: null;
|
|
62
|
+
webhookSecret: null;
|
|
63
|
+
};
|
|
64
|
+
export declare function mapProviderHttpError(err: unknown, overrides?: Partial<ProviderErrorPatterns>): MappedProviderError;
|
|
65
|
+
export {};
|