@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,228 @@
|
|
|
1
|
+
import { wireMediaSource } from "../../domain/media.js";
|
|
2
|
+
/** The `prompt_cache_options.ttl` each supported lifetime, in seconds, is sent as; the 30m default is sent as no parameter. */
|
|
3
|
+
const TTL_WIRE = new Map([
|
|
4
|
+
[30 * 60, undefined],
|
|
5
|
+
[12 * 60 * 60, "12h"],
|
|
6
|
+
]);
|
|
7
|
+
/** Reject, at gateway creation, a model whose cache lifetimes this API cannot be sent. A null lifetime (unknown) sends nothing. */
|
|
8
|
+
export function assertCacheTtlsOnWire(model) {
|
|
9
|
+
for (const ttl of [model.promptCache.shortTtlSeconds, model.promptCache.longTtlSeconds]) {
|
|
10
|
+
if (ttl !== null && !TTL_WIRE.has(ttl)) {
|
|
11
|
+
throw new Error(`OpenAI prompt cache TTLs for ${model.id} must be 30m or 12h`);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
function mapMediaToResponseInput(part, wire) {
|
|
16
|
+
const media = wireMediaSource({ wire }, part);
|
|
17
|
+
if (media.kind === "image") {
|
|
18
|
+
return {
|
|
19
|
+
type: "input_image",
|
|
20
|
+
image_url: "url" in media ? media.url : `data:${media.mediaType};base64,${media.data}`,
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
return "url" in media
|
|
24
|
+
? { type: "input_file", file_url: media.url }
|
|
25
|
+
: {
|
|
26
|
+
type: "input_file",
|
|
27
|
+
filename: media.filename,
|
|
28
|
+
file_data: `data:${media.mediaType};base64,${media.data}`,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
function mapContentPartToResponseInput(part, wire) {
|
|
32
|
+
switch (part.type) {
|
|
33
|
+
case "text":
|
|
34
|
+
return part.text.length > 0 ? { type: "input_text", text: part.text } : null;
|
|
35
|
+
case "media":
|
|
36
|
+
return mapMediaToResponseInput(part, wire);
|
|
37
|
+
default:
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
// ── Message mapping ───────────────────────────────────────────────
|
|
42
|
+
//
|
|
43
|
+
// Canonical Message → OpenAI Responses input items.
|
|
44
|
+
// System messages are extracted into `instructions`; tool results become
|
|
45
|
+
// function_call_output items; assistant output is split into separate
|
|
46
|
+
// message/function_call/reasoning items per the Responses input model.
|
|
47
|
+
//
|
|
48
|
+
function textFromParts(parts) {
|
|
49
|
+
return parts
|
|
50
|
+
.filter((p) => p.type === "text")
|
|
51
|
+
.map((p) => p.text)
|
|
52
|
+
.join("");
|
|
53
|
+
}
|
|
54
|
+
function matchesReasoningOrigin(part, targetProviderId, targetModelId) {
|
|
55
|
+
const origin = part.providerOptions?.meridian;
|
|
56
|
+
return origin?.provider === targetProviderId && origin?.model === targetModelId;
|
|
57
|
+
}
|
|
58
|
+
function mapReasoningPartToResponseInput(part, targetProviderId, targetModelId) {
|
|
59
|
+
if (!matchesReasoningOrigin(part, targetProviderId, targetModelId))
|
|
60
|
+
return null;
|
|
61
|
+
const openai = part.providerOptions?.openai;
|
|
62
|
+
const itemId = openai?.itemId;
|
|
63
|
+
const encrypted = openai?.encrypted;
|
|
64
|
+
if (typeof itemId !== "string" || typeof encrypted !== "string")
|
|
65
|
+
return null;
|
|
66
|
+
return {
|
|
67
|
+
type: "reasoning",
|
|
68
|
+
id: itemId,
|
|
69
|
+
encrypted_content: encrypted,
|
|
70
|
+
summary: [],
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
function mapMessage(message, wire) {
|
|
74
|
+
// System messages are handled via `instructions` param
|
|
75
|
+
if (message.role === "system")
|
|
76
|
+
return null;
|
|
77
|
+
// Tool result → function_call_output
|
|
78
|
+
if (message.role === "tool") {
|
|
79
|
+
const result = message.content.find((p) => p.type === "tool_result");
|
|
80
|
+
if (result) {
|
|
81
|
+
const hasMedia = result.content.some((p) => p.type === "media");
|
|
82
|
+
return {
|
|
83
|
+
type: "function_call_output",
|
|
84
|
+
call_id: result.toolCallId,
|
|
85
|
+
output: hasMedia
|
|
86
|
+
? result.content
|
|
87
|
+
.map((p) => mapContentPartToResponseInput(p, wire))
|
|
88
|
+
.filter((p) => p !== null)
|
|
89
|
+
: textFromParts(result.content),
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
// Assistant messages with tool calls → function_call items
|
|
95
|
+
if (message.role === "assistant") {
|
|
96
|
+
const toolUses = message.content.filter((p) => p.type === "tool_use");
|
|
97
|
+
if (toolUses.length > 0) {
|
|
98
|
+
// Return an array would be better but the SDK expects single items
|
|
99
|
+
// We'll handle this by flattening in the caller
|
|
100
|
+
// For now, return the message + tool calls will be handled separately
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
const role = message.role;
|
|
104
|
+
// Simple text-only
|
|
105
|
+
const hasOnlyText = message.content.every((p) => p.type === "text");
|
|
106
|
+
if (hasOnlyText) {
|
|
107
|
+
const content = textFromParts(message.content);
|
|
108
|
+
if (content.length === 0)
|
|
109
|
+
return null;
|
|
110
|
+
return {
|
|
111
|
+
role,
|
|
112
|
+
content,
|
|
113
|
+
type: "message",
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
// Mixed content
|
|
117
|
+
const contentParts = message.content
|
|
118
|
+
.map((part) => mapContentPartToResponseInput(part, wire))
|
|
119
|
+
.filter((p) => p !== null);
|
|
120
|
+
if (contentParts.length === 0)
|
|
121
|
+
return null;
|
|
122
|
+
return {
|
|
123
|
+
role,
|
|
124
|
+
content: contentParts,
|
|
125
|
+
type: "message",
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
function flattenAssistantToolCalls(messages, { provider: targetProviderId, id: targetModelId, wire, }) {
|
|
129
|
+
const items = [];
|
|
130
|
+
for (const message of messages) {
|
|
131
|
+
if (message.role === "assistant") {
|
|
132
|
+
const toolUses = message.content.filter((p) => p.type === "tool_use");
|
|
133
|
+
for (const reasoning of message.content.filter((p) => p.type === "reasoning")) {
|
|
134
|
+
const mapped = mapReasoningPartToResponseInput(reasoning, targetProviderId, targetModelId);
|
|
135
|
+
if (mapped)
|
|
136
|
+
items.push(mapped);
|
|
137
|
+
}
|
|
138
|
+
// Emit message content first (text parts)
|
|
139
|
+
const textContent = message.content.filter((p) => p.type === "text");
|
|
140
|
+
if (textContent.length > 0 || toolUses.length === 0) {
|
|
141
|
+
const mapped = mapMessage({
|
|
142
|
+
...message,
|
|
143
|
+
content: textContent.length > 0 ? textContent : message.content,
|
|
144
|
+
}, wire);
|
|
145
|
+
if (mapped)
|
|
146
|
+
items.push(mapped);
|
|
147
|
+
}
|
|
148
|
+
// Emit tool calls as separate function_call items
|
|
149
|
+
for (const tu of toolUses) {
|
|
150
|
+
if (tu.type === "tool_use") {
|
|
151
|
+
items.push({
|
|
152
|
+
type: "function_call",
|
|
153
|
+
call_id: tu.toolCallId,
|
|
154
|
+
name: tu.toolName,
|
|
155
|
+
arguments: JSON.stringify(tu.input),
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
const mapped = mapMessage(message, wire);
|
|
162
|
+
if (mapped)
|
|
163
|
+
items.push(mapped);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return items;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Extract system prompt from messages for the `instructions` param.
|
|
170
|
+
* The Responses API accepts instructions as a top-level string rather than
|
|
171
|
+
* as a system-message role in the `input` array.
|
|
172
|
+
*/
|
|
173
|
+
function extractInstructions(messages) {
|
|
174
|
+
const systemMessages = messages.filter((m) => m.role === "system");
|
|
175
|
+
if (systemMessages.length === 0)
|
|
176
|
+
return undefined;
|
|
177
|
+
return systemMessages.map((m) => textFromParts(m.content)).join("\n");
|
|
178
|
+
}
|
|
179
|
+
function mapTools(tools) {
|
|
180
|
+
if (!tools?.length)
|
|
181
|
+
return undefined;
|
|
182
|
+
return tools.map((tool) => ({
|
|
183
|
+
type: "function",
|
|
184
|
+
name: tool.name,
|
|
185
|
+
description: tool.description,
|
|
186
|
+
parameters: tool.inputSchema,
|
|
187
|
+
strict: false,
|
|
188
|
+
}));
|
|
189
|
+
}
|
|
190
|
+
// ── Reasoning config ──────────────────────────────────────────────
|
|
191
|
+
//
|
|
192
|
+
// Canonical reasoning → OpenAI Responses reasoning config.
|
|
193
|
+
// `default` sends only summary=auto (auto-summarization of reasoning) and leaves
|
|
194
|
+
// the effort to the model; `off` is the `none` effort, which the models that
|
|
195
|
+
// can disable reasoning accept.
|
|
196
|
+
//
|
|
197
|
+
function mapReasoning(reasoning) {
|
|
198
|
+
if (reasoning === "off")
|
|
199
|
+
return { effort: "none" };
|
|
200
|
+
if (reasoning === "default")
|
|
201
|
+
return { summary: "auto" };
|
|
202
|
+
return { effort: reasoning.effort, summary: "auto" };
|
|
203
|
+
}
|
|
204
|
+
// ── Public: build Responses API params ────────────────────────────
|
|
205
|
+
//
|
|
206
|
+
// Assembles the full ResponseCreateParamsStreaming from a canonical
|
|
207
|
+
// GenerateRequest. Always sets stream:true; always includes
|
|
208
|
+
// "reasoning.encrypted_content" in `include` so reasoning items survive
|
|
209
|
+
// round-trips in stateless mode (OpenAI reasoning guide).
|
|
210
|
+
//
|
|
211
|
+
export function toOpenAIResponsesParams(request, model, call) {
|
|
212
|
+
const { id: modelId } = model;
|
|
213
|
+
const instructions = extractInstructions(request.messages);
|
|
214
|
+
const input = flattenAssistantToolCalls(request.messages, model);
|
|
215
|
+
const cacheTtl = call.cacheTtlSeconds === null ? undefined : TTL_WIRE.get(call.cacheTtlSeconds);
|
|
216
|
+
return {
|
|
217
|
+
model: modelId,
|
|
218
|
+
input,
|
|
219
|
+
stream: true,
|
|
220
|
+
include: ["reasoning.encrypted_content"],
|
|
221
|
+
...(instructions ? { instructions } : {}),
|
|
222
|
+
...(mapTools(request.tools) ? { tools: mapTools(request.tools) } : {}),
|
|
223
|
+
reasoning: mapReasoning(call.reasoning),
|
|
224
|
+
...(request.promptCacheKey ? { prompt_cache_key: request.promptCacheKey } : {}),
|
|
225
|
+
// The 30m default needs no parameter. The SDK (6.45.0) predates `prompt_cache_options`.
|
|
226
|
+
...(cacheTtl ? { prompt_cache_options: { ttl: cacheTtl } } : {}),
|
|
227
|
+
};
|
|
228
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenAI Responses stream accumulator: folds OpenAI streaming events into
|
|
3
|
+
* canonical StreamEvents and a final GenerateResult. The Responses protocol
|
|
4
|
+
* names the same logical output item by different keys in different event
|
|
5
|
+
* families, so this file owns the reconciliation from provider item IDs,
|
|
6
|
+
* call IDs, and output positions into Meridian's canonical ContentPart,
|
|
7
|
+
* ToolCall, Usage, and FinishReason vocabulary.
|
|
8
|
+
*
|
|
9
|
+
* Key decision: provider `output_index` is treated as the source-order spine.
|
|
10
|
+
* Text, reasoning, and tool-use items are accumulated under that stable output
|
|
11
|
+
* position and sorted only when building the final result, so persisted content
|
|
12
|
+
* follows the provider's streamed order instead of being grouped by part type.
|
|
13
|
+
*
|
|
14
|
+
* Provider-protocol grounding (verified against OpenAI SDK 4.104.0 + docs):
|
|
15
|
+
* - `output_index` is the position of the output item in `response.output[]`.
|
|
16
|
+
* The final array order is model-dependent and the docs do NOT explicitly
|
|
17
|
+
* guarantee non-interleaving during streaming, so Meridian buffers by index
|
|
18
|
+
* and sorts at assembly time rather than trusting arrival order.
|
|
19
|
+
* - `response.function_call_arguments.delta` events carry `item_id` and
|
|
20
|
+
* `output_index` but not `call_id`. This is why
|
|
21
|
+
* argument deltas are buffered in `pendingDeltas` until the durable `call_id`
|
|
22
|
+
* arrives via `response.output_item.added` or a later event.
|
|
23
|
+
* - SDK v6 renamed the streaming event literals from `response.reasoning.*`
|
|
24
|
+
* to `response.reasoning_text.*`; keep the accumulator keyed by output item
|
|
25
|
+
* identity so the external Meridian contract does not change.
|
|
26
|
+
*/
|
|
27
|
+
import type OpenAI from "openai";
|
|
28
|
+
import type { ContentPart, FinishReason, GenerateResult, StreamEvent } from "../../domain/index.js";
|
|
29
|
+
import { type Usage } from "../../shapes/index.js";
|
|
30
|
+
interface StreamAccumulator {
|
|
31
|
+
longCacheWrites: boolean;
|
|
32
|
+
contentParts: ContentPart[];
|
|
33
|
+
textPartsByOutputIndex: Map<number, string>;
|
|
34
|
+
reasoningItems: Map<string, ReasoningItemEntry>;
|
|
35
|
+
reasoningItemsByOutputIndex: Map<number, ReasoningItemEntry>;
|
|
36
|
+
toolCalls: Map<string, ToolCallEntry>;
|
|
37
|
+
toolCallsByItemId: Map<string, ToolCallEntry>;
|
|
38
|
+
toolCallsByOutputIndex: Map<number, ToolCallEntry>;
|
|
39
|
+
usage: Usage;
|
|
40
|
+
finishReason: FinishReason;
|
|
41
|
+
model: string;
|
|
42
|
+
provider: string;
|
|
43
|
+
}
|
|
44
|
+
interface ReasoningItemEntry {
|
|
45
|
+
itemId?: string;
|
|
46
|
+
outputIndex: number;
|
|
47
|
+
text: string;
|
|
48
|
+
encrypted?: string;
|
|
49
|
+
}
|
|
50
|
+
interface ToolCallEntry {
|
|
51
|
+
itemId?: string;
|
|
52
|
+
outputIndex: number;
|
|
53
|
+
callId?: string;
|
|
54
|
+
name: string;
|
|
55
|
+
arguments: string;
|
|
56
|
+
pendingDeltas: string[];
|
|
57
|
+
}
|
|
58
|
+
export declare function createStreamAccumulator(model: string, provider: string, longCacheWrites: boolean): StreamAccumulator;
|
|
59
|
+
export declare function mapUsage(usage: OpenAI.Responses.ResponseUsage, longCacheWrites: boolean): Usage;
|
|
60
|
+
export declare function mapResponseStatus(status: OpenAI.Responses.ResponseStatus | undefined, incompleteReason: string | undefined, hasToolCalls: boolean): FinishReason;
|
|
61
|
+
export declare function eventsFromResponseStreamEvent(event: OpenAI.Responses.ResponseStreamEvent, acc: StreamAccumulator): Generator<StreamEvent>;
|
|
62
|
+
export declare function buildGenerateResult(acc: StreamAccumulator): GenerateResult;
|
|
63
|
+
export {};
|