@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.
Files changed (89) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +67 -2
  3. package/dist/gateway/adapters/anthropic/adapter.d.ts +9 -0
  4. package/dist/gateway/adapters/anthropic/adapter.js +52 -0
  5. package/dist/gateway/adapters/anthropic/request-map.d.ts +46 -0
  6. package/dist/gateway/adapters/anthropic/request-map.js +285 -0
  7. package/dist/gateway/adapters/anthropic/stream-collect.d.ts +67 -0
  8. package/dist/gateway/adapters/anthropic/stream-collect.js +295 -0
  9. package/dist/gateway/adapters/anthropic/tool-schema.d.ts +8 -0
  10. package/dist/gateway/adapters/anthropic/tool-schema.js +68 -0
  11. package/dist/gateway/adapters/credentials.d.ts +25 -0
  12. package/dist/gateway/adapters/credentials.js +118 -0
  13. package/dist/gateway/adapters/gemini/adapter.d.ts +10 -0
  14. package/dist/gateway/adapters/gemini/adapter.js +90 -0
  15. package/dist/gateway/adapters/gemini/request-map.d.ts +29 -0
  16. package/dist/gateway/adapters/gemini/request-map.js +178 -0
  17. package/dist/gateway/adapters/gemini/stream-collect.d.ts +29 -0
  18. package/dist/gateway/adapters/gemini/stream-collect.js +116 -0
  19. package/dist/gateway/adapters/mock/response.d.ts +11 -0
  20. package/dist/gateway/adapters/mock/response.js +160 -0
  21. package/dist/gateway/adapters/mock/samples.d.ts +8 -0
  22. package/dist/gateway/adapters/mock/samples.js +133 -0
  23. package/dist/gateway/adapters/mock/script-queue.d.ts +10 -0
  24. package/dist/gateway/adapters/mock/script-queue.js +60 -0
  25. package/dist/gateway/adapters/mock/server.d.ts +16 -0
  26. package/dist/gateway/adapters/mock/server.js +342 -0
  27. package/dist/gateway/adapters/mock/write-directive.d.ts +15 -0
  28. package/dist/gateway/adapters/mock/write-directive.js +21 -0
  29. package/dist/gateway/adapters/openai/adapter.d.ts +3 -0
  30. package/dist/gateway/adapters/openai/adapter.js +35 -0
  31. package/dist/gateway/adapters/openai/request-map.d.ts +31 -0
  32. package/dist/gateway/adapters/openai/request-map.js +228 -0
  33. package/dist/gateway/adapters/openai/stream-collect.d.ts +63 -0
  34. package/dist/gateway/adapters/openai/stream-collect.js +490 -0
  35. package/dist/gateway/adapters/openai-compatible/adapter.d.ts +3 -0
  36. package/dist/gateway/adapters/openai-compatible/adapter.js +35 -0
  37. package/dist/gateway/adapters/openai-compatible/request-map.d.ts +31 -0
  38. package/dist/gateway/adapters/openai-compatible/request-map.js +180 -0
  39. package/dist/gateway/adapters/openai-compatible/stream-collect.d.ts +84 -0
  40. package/dist/gateway/adapters/openai-compatible/stream-collect.js +215 -0
  41. package/dist/gateway/adapters/provider-http-error.d.ts +65 -0
  42. package/dist/gateway/adapters/provider-http-error.js +188 -0
  43. package/dist/gateway/adapters/provider-stream.d.ts +23 -0
  44. package/dist/gateway/adapters/provider-stream.js +58 -0
  45. package/dist/gateway/attempt-stream.d.ts +27 -0
  46. package/dist/gateway/attempt-stream.js +329 -0
  47. package/dist/gateway/create-gateway.d.ts +25 -0
  48. package/dist/gateway/create-gateway.js +88 -0
  49. package/dist/gateway/deadline.d.ts +56 -0
  50. package/dist/gateway/deadline.js +120 -0
  51. package/dist/gateway/domain/index.d.ts +10 -0
  52. package/dist/gateway/domain/index.js +10 -0
  53. package/dist/gateway/domain/media.d.ts +62 -0
  54. package/dist/gateway/domain/media.js +126 -0
  55. package/dist/gateway/domain/metering.d.ts +7 -0
  56. package/dist/gateway/domain/metering.js +6 -0
  57. package/dist/gateway/domain/resolve-call.d.ts +24 -0
  58. package/dist/gateway/domain/resolve-call.js +39 -0
  59. package/dist/gateway/domain/types.d.ts +457 -0
  60. package/dist/gateway/domain/types.js +10 -0
  61. package/dist/gateway/helpers/messages.d.ts +17 -0
  62. package/dist/gateway/helpers/messages.js +29 -0
  63. package/dist/gateway/helpers/parse-tool-arguments.d.ts +9 -0
  64. package/dist/gateway/helpers/parse-tool-arguments.js +38 -0
  65. package/dist/gateway/helpers/tool-call-pairing.d.ts +13 -0
  66. package/dist/gateway/helpers/tool-call-pairing.js +25 -0
  67. package/dist/gateway/index.d.ts +12 -0
  68. package/dist/gateway/index.js +11 -0
  69. package/dist/gateway/mock-script.d.ts +129 -0
  70. package/dist/gateway/mock-script.js +129 -0
  71. package/dist/gateway/mock.d.ts +11 -0
  72. package/dist/gateway/mock.js +22 -0
  73. package/dist/gateway/ports/gateway.d.ts +18 -0
  74. package/dist/gateway/ports/gateway.js +1 -0
  75. package/dist/gateway/ports/provider-adapter.d.ts +21 -0
  76. package/dist/gateway/ports/provider-adapter.js +1 -0
  77. package/dist/gateway/routing.d.ts +28 -0
  78. package/dist/gateway/routing.js +48 -0
  79. package/dist/gateway/shapes/index.d.ts +7 -0
  80. package/dist/gateway/shapes/index.js +1 -0
  81. package/dist/gateway/shapes/json.d.ts +4 -0
  82. package/dist/gateway/shapes/json.js +1 -0
  83. package/dist/gateway/shapes/provider-error.d.ts +10 -0
  84. package/dist/gateway/shapes/provider-error.js +1 -0
  85. package/dist/gateway/shapes/usage.d.ts +41 -0
  86. package/dist/gateway/shapes/usage.js +29 -0
  87. package/dist/gateway/stream-events.d.ts +21 -0
  88. package/dist/gateway/stream-events.js +26 -0
  89. package/package.json +62 -3
@@ -0,0 +1,490 @@
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 { parseToolCallArguments } from "../../helpers/parse-tool-arguments.js";
28
+ import { assertValidUsage } from "../../shapes/index.js";
29
+ function emptyUsage() {
30
+ return { inputTokens: 0, outputTokens: 0 };
31
+ }
32
+ export function createStreamAccumulator(model, provider, longCacheWrites) {
33
+ return {
34
+ longCacheWrites,
35
+ contentParts: [],
36
+ textPartsByOutputIndex: new Map(),
37
+ reasoningItems: new Map(),
38
+ reasoningItemsByOutputIndex: new Map(),
39
+ toolCalls: new Map(),
40
+ toolCallsByItemId: new Map(),
41
+ toolCallsByOutputIndex: new Map(),
42
+ usage: emptyUsage(),
43
+ finishReason: "end_turn",
44
+ model,
45
+ provider,
46
+ };
47
+ }
48
+ function rememberReasoningItemEntry(acc, entry) {
49
+ // Re-register on every update because an entry can start life keyed only by
50
+ // outputIndex and later gain itemId/encrypted content from a different event.
51
+ if (entry.itemId)
52
+ acc.reasoningItems.set(entry.itemId, entry);
53
+ acc.reasoningItemsByOutputIndex.set(entry.outputIndex, entry);
54
+ }
55
+ function findReasoningItemEntry(acc, params) {
56
+ // Prefer the provider item ID when present, but fall back to output_index so
57
+ // early deltas and final snapshots still update the same logical reasoning part.
58
+ return ((params.itemId ? acc.reasoningItems.get(params.itemId) : undefined) ??
59
+ acc.reasoningItemsByOutputIndex.get(params.outputIndex));
60
+ }
61
+ function rememberReasoningOutputItem(acc, item, outputIndex) {
62
+ // `response.output_item.*` and the completed response output can carry
63
+ // metadata (notably encrypted_content) without carrying the full accumulated
64
+ // text. Merge metadata into any entry created earlier by reasoning deltas.
65
+ const entry = findReasoningItemEntry(acc, {
66
+ itemId: item.id,
67
+ outputIndex,
68
+ }) ?? {
69
+ outputIndex,
70
+ text: "",
71
+ };
72
+ entry.itemId = item.id;
73
+ entry.outputIndex = outputIndex;
74
+ if (item.encrypted_content)
75
+ entry.encrypted = item.encrypted_content;
76
+ rememberReasoningItemEntry(acc, entry);
77
+ }
78
+ function rememberToolCallEntry(acc, entry) {
79
+ // One ToolCallEntry intentionally lives in three indexes. Responses stream
80
+ // events are not consistent about whether they name a function call by callId,
81
+ // output item ID, or output position.
82
+ if (entry.callId)
83
+ acc.toolCalls.set(entry.callId, entry);
84
+ if (entry.itemId)
85
+ acc.toolCallsByItemId.set(entry.itemId, entry);
86
+ acc.toolCallsByOutputIndex.set(entry.outputIndex, entry);
87
+ }
88
+ function findToolCallEntry(acc, params) {
89
+ // Lookup order follows semantic strength: callId is Meridian's durable tool
90
+ // identity, itemId is the provider output identity, and output_index is the
91
+ // ordering fallback used by deltas before either ID has appeared.
92
+ return ((params.callId ? acc.toolCalls.get(params.callId) : undefined) ??
93
+ (params.itemId ? acc.toolCallsByItemId.get(params.itemId) : undefined) ??
94
+ acc.toolCallsByOutputIndex.get(params.outputIndex));
95
+ }
96
+ // ── Usage mapping ─────────────────────────────────────────────────
97
+ export function mapUsage(usage, longCacheWrites) {
98
+ // Responses reports canonical input/output token totals directly, but cache
99
+ // hits and reasoning tokens live in provider-specific detail objects
100
+ // (`input_tokens_details.cached_tokens`, `output_tokens_details.reasoning_tokens`).
101
+ // Both fields are confirmed in the OpenAI reasoning guide and the installed
102
+ // SDK's ResponseUsage shape. Meridian only sets optional Usage fields when the
103
+ // provider reports a positive value.
104
+ //
105
+ // GPT-5.6+ also bills cache writes: `cache_write_tokens` is their total, split by
106
+ // lifetime in `cache_write_tokens_details` (`ttl_30m_tokens` + `ttl_12h_tokens`), and
107
+ // `cached_tokens` excludes them. The installed SDK (6.45.0) predates these fields, so
108
+ // they are read through a local shape.
109
+ const details = usage.input_tokens_details;
110
+ const result = {
111
+ inputTokens: usage.input_tokens,
112
+ outputTokens: usage.output_tokens,
113
+ };
114
+ if (usage.input_tokens_details?.cached_tokens > 0) {
115
+ result.cacheReadTokens = usage.input_tokens_details.cached_tokens;
116
+ }
117
+ if (details?.cache_write_tokens && details.cache_write_tokens > 0) {
118
+ result.cacheWriteTokens = details.cache_write_tokens;
119
+ const long = details.cache_write_tokens_details?.ttl_12h_tokens ??
120
+ (longCacheWrites ? details.cache_write_tokens : 0);
121
+ if (long > 0)
122
+ result.cacheWriteLongTokens = long;
123
+ }
124
+ if (usage.output_tokens_details?.reasoning_tokens > 0) {
125
+ result.reasoningTokens = usage.output_tokens_details.reasoning_tokens;
126
+ }
127
+ assertValidUsage(result);
128
+ return result;
129
+ }
130
+ // ── Status → FinishReason mapping ─────────────────────────────────
131
+ export function mapResponseStatus(status, incompleteReason, hasToolCalls) {
132
+ // SDK ResponseStatus is 'completed' | 'failed' | 'in_progress' | 'cancelled'
133
+ // | 'queued' | 'incomplete'. Only 'incomplete' and 'failed' need mapping;
134
+ // other terminal statuses default to end_turn below.
135
+ if (status === "incomplete") {
136
+ // Response.IncompleteDetails.reason is 'max_output_tokens' | 'content_filter'
137
+ // (SDK-confirmed). The canonical FinishReason has no content_filter variant,
138
+ // so filtered content → "error"; exhausted tokens → "max_tokens". Unknown
139
+ // incomplete reasons default to max_tokens: the response was cut short
140
+ // rather than naturally ended.
141
+ if (incompleteReason === "max_output_tokens")
142
+ return "max_tokens";
143
+ if (incompleteReason === "content_filter")
144
+ return "error";
145
+ return "max_tokens";
146
+ }
147
+ if (status === "failed")
148
+ return "error";
149
+ if (status === "cancelled")
150
+ return "error";
151
+ if (hasToolCalls)
152
+ return "tool_use";
153
+ return "end_turn";
154
+ }
155
+ // ── Stream event processing ───────────────────────────────────────
156
+ export function* eventsFromResponseStreamEvent(event, acc) {
157
+ // Each yielded StreamEvent is the low-latency canonical view for clients, while
158
+ // the accumulator keeps the lossless state needed for the final GenerateResult.
159
+ switch (event.type) {
160
+ case "response.output_text.delta": {
161
+ // `output_index` identifies the whole output item's position in
162
+ // `response.output[]`, not the text content fragment within an item.
163
+ // Accumulating by index preserves separate text items across a multi-item
164
+ // response and supplies the canonical `partIndex` for deltas. Because the
165
+ // docs do not guarantee non-interleaving of output items during streaming,
166
+ // we buffer per-index and sort only at assembly time.
167
+ acc.textPartsByOutputIndex.set(event.output_index, (acc.textPartsByOutputIndex.get(event.output_index) ?? "") + event.delta);
168
+ yield {
169
+ type: "text.delta",
170
+ text: event.delta,
171
+ partIndex: event.output_index,
172
+ };
173
+ break;
174
+ }
175
+ case "response.reasoning_text.delta": {
176
+ // OpenAI SDK v6 exposes reasoning text as its own stream family. Keep the
177
+ // object fallback because older recorded/mock events used `{ text }` deltas
178
+ // and the normalization is harmless at this provider boundary.
179
+ const text = typeof event.delta === "string"
180
+ ? event.delta
181
+ : typeof event.delta === "object" && event.delta !== null && "text" in event.delta
182
+ ? String(event.delta.text)
183
+ : "";
184
+ if (text) {
185
+ // Reasoning deltas carry both `item_id` and `output_index` when the
186
+ // provider knows them. The helper lets this delta merge with metadata
187
+ // learned from output-item events in either order.
188
+ const entry = findReasoningItemEntry(acc, {
189
+ itemId: event.item_id,
190
+ outputIndex: event.output_index,
191
+ }) ?? {
192
+ itemId: event.item_id,
193
+ outputIndex: event.output_index,
194
+ text: "",
195
+ };
196
+ entry.itemId = event.item_id;
197
+ entry.outputIndex = event.output_index;
198
+ entry.text += text;
199
+ rememberReasoningItemEntry(acc, entry);
200
+ yield {
201
+ type: "reasoning.delta",
202
+ text,
203
+ partIndex: event.output_index,
204
+ };
205
+ }
206
+ break;
207
+ }
208
+ case "response.function_call_arguments.delta": {
209
+ // Function-call argument deltas stream as raw JSON text.
210
+ // ResponseFunctionCallArgumentsDeltaEvent (SDK-confirmed) carries
211
+ // `item_id` and `output_index` but NOT `call_id`, so arguments can arrive
212
+ // before `response.output_item.added` has supplied call_id/name.
213
+ // We accumulate immediately but only emit canonical deltas once a durable
214
+ // call_id exists, because downstream tool_result messages must reference
215
+ // the provider call_id exactly.
216
+ let entry = findToolCallEntry(acc, {
217
+ itemId: event.item_id,
218
+ outputIndex: event.output_index,
219
+ });
220
+ if (!entry) {
221
+ entry = {
222
+ itemId: event.item_id,
223
+ outputIndex: event.output_index,
224
+ name: "",
225
+ arguments: "",
226
+ pendingDeltas: [],
227
+ };
228
+ rememberToolCallEntry(acc, entry);
229
+ }
230
+ entry.arguments += event.delta;
231
+ if (entry.callId) {
232
+ // Once callId is known, every subsequent raw JSON fragment can be
233
+ // forwarded as a canonical tool_call.delta tied to the stable tool ID.
234
+ yield {
235
+ type: "tool_call.delta",
236
+ id: entry.callId,
237
+ name: entry.name,
238
+ argumentsDelta: event.delta,
239
+ partIndex: event.output_index,
240
+ };
241
+ }
242
+ else {
243
+ // Without callId, emitting would create an unusable delta: downstream
244
+ // tool-result messages must reference the provider call_id exactly.
245
+ entry.pendingDeltas.push(event.delta);
246
+ }
247
+ break;
248
+ }
249
+ case "response.output_item.added": {
250
+ const item = event.item;
251
+ if (item.type === "reasoning") {
252
+ // Reasoning item metadata can precede or follow reasoning.delta text.
253
+ rememberReasoningOutputItem(acc, item, event.output_index);
254
+ }
255
+ if (item.type === "function_call") {
256
+ // This event is where Responses usually reveals the call_id and tool
257
+ // name. It may be racing with argument deltas, so merge with any entry
258
+ // created earlier by output_index/item_id instead of replacing it.
259
+ const entry = findToolCallEntry(acc, {
260
+ callId: item.call_id,
261
+ itemId: item.id,
262
+ outputIndex: event.output_index,
263
+ }) ?? {
264
+ outputIndex: event.output_index,
265
+ name: "",
266
+ arguments: "",
267
+ pendingDeltas: [],
268
+ };
269
+ entry.itemId = item.id;
270
+ entry.outputIndex = event.output_index;
271
+ entry.callId = item.call_id;
272
+ entry.name = item.name;
273
+ if (entry.arguments === "" && item.arguments) {
274
+ entry.arguments = item.arguments;
275
+ }
276
+ rememberToolCallEntry(acc, entry);
277
+ // Flush buffered JSON fragments in original arrival order now that the
278
+ // canonical delta can carry the provider call_id.
279
+ for (const argumentsDelta of entry.pendingDeltas.splice(0)) {
280
+ yield {
281
+ type: "tool_call.delta",
282
+ id: item.call_id,
283
+ name: item.name,
284
+ argumentsDelta,
285
+ partIndex: event.output_index,
286
+ };
287
+ }
288
+ }
289
+ break;
290
+ }
291
+ case "response.output_item.done": {
292
+ const item = event.item;
293
+ if (item.type === "reasoning") {
294
+ // Done events can be the last chance to observe reasoning metadata.
295
+ rememberReasoningOutputItem(acc, item, event.output_index);
296
+ }
297
+ if (item.type === "function_call") {
298
+ // The done event can contain the final full arguments string. Prefer it
299
+ // over reconstructed deltas when present, while keeping the same entry
300
+ // and indexes for ordering.
301
+ const entry = findToolCallEntry(acc, {
302
+ callId: item.call_id,
303
+ itemId: item.id,
304
+ outputIndex: event.output_index,
305
+ });
306
+ if (entry) {
307
+ entry.itemId = item.id;
308
+ entry.outputIndex = event.output_index;
309
+ entry.callId = item.call_id;
310
+ entry.name = item.name;
311
+ if (item.arguments) {
312
+ entry.arguments = item.arguments;
313
+ }
314
+ rememberToolCallEntry(acc, entry);
315
+ }
316
+ }
317
+ break;
318
+ }
319
+ case "response.function_call_arguments.done": {
320
+ // The provider sends a final full JSON argument string. Store it so
321
+ // GenerateResult parses the authoritative final payload, even if deltas
322
+ // were missing, duplicated, or chunked oddly.
323
+ const entry = findToolCallEntry(acc, {
324
+ itemId: event.item_id,
325
+ outputIndex: event.output_index,
326
+ });
327
+ if (entry && event.arguments) {
328
+ entry.arguments = event.arguments;
329
+ }
330
+ break;
331
+ }
332
+ case "response.failed":
333
+ case "response.incomplete":
334
+ case "response.completed": {
335
+ const response = event.response;
336
+ // The completed response carries the provider's terminal status and final
337
+ // usage. Tool-use precedence is handled inside mapResponseStatus.
338
+ acc.finishReason = mapResponseStatus(response.status, response.incomplete_details?.reason, acc.toolCalls.size > 0);
339
+ // Usage appears only on terminal events for Responses streams
340
+ // (response.completed). Emit it as a canonical usage event now so
341
+ // subscribers see token counts before the adapter later emits the final
342
+ // end event with the assembled GenerateResult.
343
+ if (response.usage) {
344
+ acc.usage = mapUsage(response.usage, acc.longCacheWrites);
345
+ yield { type: "usage", usage: acc.usage };
346
+ }
347
+ if (response.error) {
348
+ acc.finishReason = "error";
349
+ yield {
350
+ type: "error",
351
+ code: response.error.code === "context_length_exceeded"
352
+ ? "context_overflow"
353
+ : "provider_error",
354
+ message: response.error.message,
355
+ retryable: false,
356
+ providerError: { status: null, message: response.error.message },
357
+ result: buildGenerateResult(acc),
358
+ };
359
+ }
360
+ response.output.forEach((item, outputIndex) => {
361
+ if (item.type === "reasoning") {
362
+ // The terminal output array is indexed in source order and can include
363
+ // encrypted reasoning content that did not appear on streaming deltas.
364
+ rememberReasoningOutputItem(acc, item, outputIndex);
365
+ }
366
+ });
367
+ break;
368
+ }
369
+ case "response.refusal.delta": {
370
+ // Refusal text is not a normal output_text item and has no output_index in
371
+ // the SDK event (ResponseRefusalDeltaEvent carries item_id/output_index
372
+ // but no content_index for per-part positioning). Preserve it as text in
373
+ // the unindexed fallback so callers still see the provider's refusal
374
+ // explanation, appended after indexed content.
375
+ const refusalEvent = event;
376
+ if (refusalEvent.delta) {
377
+ acc.contentParts.push({ type: "text", text: refusalEvent.delta });
378
+ }
379
+ break;
380
+ }
381
+ case "response.reasoning_text.done": {
382
+ // Reasoning.done supplies the final full reasoning text for an item. Store
383
+ // it authoritatively, preserving any item metadata already reconciled.
384
+ const entry = findReasoningItemEntry(acc, {
385
+ itemId: event.item_id,
386
+ outputIndex: event.output_index,
387
+ }) ?? {
388
+ itemId: event.item_id,
389
+ outputIndex: event.output_index,
390
+ text: "",
391
+ };
392
+ entry.itemId = event.item_id;
393
+ entry.outputIndex = event.output_index;
394
+ entry.text = event.text;
395
+ rememberReasoningItemEntry(acc, entry);
396
+ break;
397
+ }
398
+ // Lifecycle/content-part boundary events are useful for the provider stream
399
+ // but do not add canonical content beyond deltas/items already handled.
400
+ case "response.created":
401
+ case "response.in_progress":
402
+ case "response.content_part.added":
403
+ case "response.content_part.done":
404
+ case "response.output_text.done":
405
+ break;
406
+ default:
407
+ // Other hosted-tool/provider events (web_search, code_interpreter, etc.)
408
+ // have no Meridian-canonical ContentPart/StreamEvent mapping yet. Ignore
409
+ // rather than inventing a lossy custom contract at this boundary.
410
+ break;
411
+ }
412
+ }
413
+ // ── Build final result ────────────────────────────────────────────
414
+ export function buildGenerateResult(acc) {
415
+ // Build the final assistant content in source order. Each accumulated
416
+ // reasoning/text/tool_use part is tagged with provider `output_index` (the
417
+ // position in `response.output[]`), then sorted once here so persisted block
418
+ // order matches the provider stream rather than being grouped by part type
419
+ // (all reasoning, then all text, then all tools) as a naive Map iteration
420
+ // would produce.
421
+ const indexedParts = [];
422
+ for (const [index, reasoning] of acc.reasoningItemsByOutputIndex.entries()) {
423
+ // Keep provider metadata with reasoning so future OpenAI requests can replay
424
+ // encrypted reasoning items only to the same provider/model pair.
425
+ if (!reasoning.text && !reasoning.itemId && !reasoning.encrypted)
426
+ continue;
427
+ indexedParts.push({
428
+ index,
429
+ part: {
430
+ type: "reasoning",
431
+ text: reasoning.text,
432
+ providerOptions: {
433
+ openai: {
434
+ ...(reasoning.itemId ? { itemId: reasoning.itemId } : {}),
435
+ ...(reasoning.encrypted ? { encrypted: reasoning.encrypted } : {}),
436
+ },
437
+ meridian: { provider: acc.provider, model: acc.model },
438
+ },
439
+ },
440
+ });
441
+ }
442
+ for (const [index, text] of acc.textPartsByOutputIndex.entries()) {
443
+ // Text is intentionally stored by output_index instead of one global buffer:
444
+ // a text item between two reasoning/tool items must stay in that position,
445
+ // and distinct text output items must not be merged across rounds.
446
+ if (!text)
447
+ continue;
448
+ indexedParts.push({ index, part: { type: "text", text } });
449
+ }
450
+ const toolCalls = [];
451
+ for (const entry of acc.toolCalls.values()) {
452
+ // Only entries with the provider call_id can participate in the canonical
453
+ // tool-call contract; tool_result messages must reference this exact ID.
454
+ if (!entry.callId)
455
+ continue;
456
+ const parsed = parseToolCallArguments(entry.arguments);
457
+ const input = parsed.ok ? parsed.arguments : {};
458
+ const parseError = parsed.ok ? undefined : { raw: parsed.raw, message: parsed.message };
459
+ indexedParts.push({
460
+ index: entry.outputIndex,
461
+ part: {
462
+ type: "tool_use",
463
+ toolCallId: entry.callId,
464
+ toolName: entry.name,
465
+ input,
466
+ ...(parseError ? { inputParseError: parseError } : {}),
467
+ },
468
+ });
469
+ toolCalls.push({
470
+ id: entry.callId,
471
+ name: entry.name,
472
+ arguments: input,
473
+ ...(parseError ? { argumentsParseError: parseError } : {}),
474
+ });
475
+ }
476
+ const content = indexedParts
477
+ .sort((a, b) => a.index - b.index)
478
+ .map(({ part }) => part)
479
+ // Anything without a provider position, such as refusal text, is appended as
480
+ // an explicit fallback after all source-indexed output.
481
+ .concat(acc.contentParts);
482
+ return {
483
+ content,
484
+ toolCalls,
485
+ finishReason: acc.finishReason,
486
+ usage: acc.usage,
487
+ model: acc.model,
488
+ provider: acc.provider,
489
+ };
490
+ }
@@ -0,0 +1,3 @@
1
+ import type { ProviderConfig } from "../../domain/index.js";
2
+ import type { ProviderAdapter } from "../../ports/provider-adapter.js";
3
+ export declare function createOpenAICompatibleAdapter(config: ProviderConfig): ProviderAdapter;
@@ -0,0 +1,35 @@
1
+ /**
2
+ * OpenAI-compatible (Chat Completions) provider adapter: implements the
3
+ * provider-adapter port over any OpenAI-Chat-Completions-shaped endpoint
4
+ * (gemini, mock, self-hosted). Owns the chat-completions streaming wiring. The
5
+ * shared stream envelope is `../provider-stream.ts`.
6
+ *
7
+ * Its accumulator takes no `longCacheWrites`: this wire reports no cache writes.
8
+ */
9
+ import OpenAI from "openai";
10
+ import { bearerClientOptions, clientForRequest } from "../credentials.js";
11
+ import { OPENAI_ENV_OPTOUT, providerClientOptions, providerFetch } from "../provider-http-error.js";
12
+ import { streamProvider } from "../provider-stream.js";
13
+ import { toOpenAIChatCompletionParams } from "./request-map.js";
14
+ import { buildGenerateResult, createStreamAccumulator, eventsFromOpenAIChunk, } from "./stream-collect.js";
15
+ export function createOpenAICompatibleAdapter(config) {
16
+ const own = new OpenAI({
17
+ ...providerClientOptions(config, "https://api.openai.com/v1"),
18
+ ...OPENAI_ENV_OPTOUT,
19
+ ...bearerClientOptions(config.auth, providerFetch),
20
+ });
21
+ const clientFor = clientForRequest(config, own, (credential) => own.withOptions(bearerClientOptions(credential, providerFetch)));
22
+ const providerId = config.id;
23
+ return {
24
+ stream(request, model, call) {
25
+ const acc = createStreamAccumulator(model.id, providerId);
26
+ return streamProvider({
27
+ start: { type: "start", model: model.id, provider: providerId },
28
+ signal: request.signal,
29
+ open: () => clientFor(request).chat.completions.create({ ...toOpenAIChatCompletionParams(request, model, call), stream: true }, { signal: request.signal }),
30
+ events: (chunk) => eventsFromOpenAIChunk(chunk, acc),
31
+ result: () => buildGenerateResult(acc),
32
+ });
33
+ },
34
+ };
35
+ }
@@ -0,0 +1,31 @@
1
+ /**
2
+ * OpenAI-compatible request mapper: converts canonical GenerateRequests into
3
+ * Chat Completions request bodies (messages, tools, response_format). Owns the
4
+ * canonical→chat-completions translation.
5
+ *
6
+ * Key decisions:
7
+ * - Tool-result messages (role="tool") use the Chat Completions `tool` role
8
+ * with `tool_call_id` referencing the original call.
9
+ * - Assistant messages with tool_use parts emit both a text `content` field
10
+ * (possibly null) and a `tool_calls` array, matching the Chat Completions
11
+ * assistant-message shape.
12
+ * - Media parts trigger array-format content (text + image_url/file blocks);
13
+ * text-only messages use the simpler string content format.
14
+ * - A tool result's media go in one user message right after the run of tool
15
+ * messages (the `tool` role takes text only), each preceded by a line naming its
16
+ * tool call, not stringified into the result.
17
+ * - `stream_options: { include_usage: true }` is always set so the final
18
+ * chunk carries cumulative token usage (per OpenAI streaming docs).
19
+ * - Reasoning: a resolved effort goes out as `reasoning_effort`; `default` and
20
+ * `off` send nothing (the model's default applies).
21
+ * - No prompt-cache marks are sent: the models on this adapter cache automatically.
22
+ */
23
+ import type OpenAI from "openai";
24
+ import type { GenerateRequest, ModelInfo, ResolvedCall } from "../../domain/index.js";
25
+ /**
26
+ * Assemble the full ChatCompletionCreateParams from a canonical
27
+ * GenerateRequest. Always sets stream:true and stream_options with
28
+ * include_usage:true (per OpenAI streaming docs) so the final chunk
29
+ * carries cumulative token counts.
30
+ */
31
+ export declare function toOpenAIChatCompletionParams(request: GenerateRequest, model: Pick<ModelInfo, "id" | "wire" | "imageLinks">, call: ResolvedCall): OpenAI.Chat.Completions.ChatCompletionCreateParams;