@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,90 @@
1
+ /**
2
+ * Gemini provider adapter: the provider-adapter port over Google's native `generateContent`
3
+ * API through `@google/genai`. The shared stream envelope is `../provider-stream.ts`.
4
+ *
5
+ * Every option the SDK would read from the environment (key, Vertex mode, project, location,
6
+ * base URL, API version) is passed explicitly, and it sends one request because `retryOptions`
7
+ * is never set. If the machine sets both of the SDK's own key variables it prints a console
8
+ * warning; the explicit key still wins.
9
+ */
10
+ import { ApiError, GoogleGenAI } from "@google/genai";
11
+ import { bearerClientOptions, clientForRequest } from "../credentials.js";
12
+ import { streamProvider } from "../provider-stream.js";
13
+ import { toGeminiParams } from "./request-map.js";
14
+ import { buildGenerateResult, createStreamAccumulator, eventsFromGeminiChunk, } from "./stream-collect.js";
15
+ const DEFAULT_BASE_URL = "https://generativelanguage.googleapis.com";
16
+ /** `RetryInfo.retryDelay` is a duration string such as "34s" or "0.5s". */
17
+ function retryDelayMs(details) {
18
+ if (!Array.isArray(details))
19
+ return undefined;
20
+ for (const detail of details) {
21
+ const delay = detail?.retryDelay;
22
+ const seconds = typeof delay === "string" ? Number.parseFloat(delay) : Number.NaN;
23
+ if (Number.isFinite(seconds))
24
+ return Math.round(seconds * 1000);
25
+ }
26
+ return undefined;
27
+ }
28
+ /** The SDK wraps a non-JSON-typed error body as `{error: {message: <the body text>}}`, so the body can be JSON inside JSON. */
29
+ function googleErrorOf(text) {
30
+ const error = JSON.parse(text).error;
31
+ return typeof error?.message === "string" && error.message.startsWith("{")
32
+ ? googleErrorOf(error.message)
33
+ : error;
34
+ }
35
+ /**
36
+ * The SDK's `ApiError` holds the response body as a JSON string and drops the headers. This
37
+ * gives `mapProviderHttpError` the shape the other SDKs' errors have: the status, the body's
38
+ * message, the parsed body, and the retry hint as a header. Google answers a bad key with 400
39
+ * (`API_KEY_INVALID`), which is an auth error.
40
+ */
41
+ export function geminiErrorShape(err) {
42
+ if (!(err instanceof ApiError))
43
+ return err;
44
+ try {
45
+ const error = googleErrorOf(err.message);
46
+ const delay = retryDelayMs(error?.details);
47
+ const badKey = Array.isArray(error?.details) &&
48
+ error.details.some((d) => d?.reason === "API_KEY_INVALID");
49
+ return {
50
+ status: badKey ? 401 : err.status,
51
+ message: error?.message ?? err.message,
52
+ error,
53
+ headers: delay === undefined ? {} : { "retry-after-ms": String(delay) },
54
+ };
55
+ }
56
+ catch {
57
+ return err;
58
+ }
59
+ }
60
+ function newClient(config, credential) {
61
+ const { apiKey, fetch } = bearerClientOptions(credential, globalThis.fetch);
62
+ return new GoogleGenAI({
63
+ apiKey,
64
+ vertexai: false,
65
+ apiVersion: "v1beta",
66
+ httpOptions: {
67
+ baseUrl: config.baseUrl ?? DEFAULT_BASE_URL,
68
+ headers: config.headers,
69
+ ...(credential && "accessToken" in credential ? { fetch } : {}),
70
+ },
71
+ });
72
+ }
73
+ export function createGeminiAdapter(config) {
74
+ const own = newClient(config, config.auth);
75
+ const clientFor = clientForRequest(config, own, (credential) => newClient(config, credential));
76
+ const providerId = config.id;
77
+ return {
78
+ stream(request, model, call) {
79
+ const acc = createStreamAccumulator(model, providerId);
80
+ return streamProvider({
81
+ start: { type: "start", model: model.id, provider: providerId },
82
+ signal: request.signal,
83
+ open: () => clientFor(request).models.generateContentStream(toGeminiParams(request, model, call)),
84
+ normalizeError: geminiErrorShape,
85
+ events: (chunk) => eventsFromGeminiChunk(chunk, acc),
86
+ result: () => buildGenerateResult(acc),
87
+ });
88
+ },
89
+ };
90
+ }
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Gemini request mapper: converts canonical GenerateRequests into native `generateContent`
3
+ * parameters (contents, systemInstruction, tools, thinking).
4
+ *
5
+ * Key decisions:
6
+ * - Roles are `user` and `model`; a tool result is a `functionResponse` part in a user turn, so
7
+ * adjacent messages of one role merge into one turn (parallel results must share a turn).
8
+ * `functionResponse` names its function, which the canonical result lacks: the name comes from
9
+ * the earlier `tool_use` with the same call id. Gemini 3 also gets the id on both sides.
10
+ * - Media: a link is `fileData` (`https` only, a mapper never downloads), bytes are
11
+ * `inlineData`. A tool result's media nest in `functionResponse.parts`, Gemini 3 and later
12
+ * only; for an older or unknown model that is an error here and `placeMedia` answers none.
13
+ * - Thought signatures: the model's `thoughtSignature` on a text, thought or function-call part
14
+ * is kept in `providerOptions.gemini.signature` and sent back on the same part, only to the
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, a compacted turn), so that
17
+ * call gets Google's documented validator-bypass value.
18
+ * - Prompt caching is implicit: no mark and no TTL is sent.
19
+ */
20
+ import type { Content, GenerateContentConfig } from "@google/genai";
21
+ import type { GenerateRequest, ModelInfo, ResolvedCall } from "../../domain/index.js";
22
+ /** Reject, at gateway creation, a model with a cache lifetime: implicit caching documents none to send. */
23
+ export declare function assertCacheTtlsOnWire(model: Pick<ModelInfo, "id" | "promptCache">): void;
24
+ export interface GeminiParams {
25
+ model: string;
26
+ contents: Content[];
27
+ config: GenerateContentConfig;
28
+ }
29
+ export declare function toGeminiParams(request: GenerateRequest, model: Pick<ModelInfo, "id" | "provider" | "wire">, call: ResolvedCall): GeminiParams;
@@ -0,0 +1,178 @@
1
+ import { geminiMajorVersion, wireMediaSource } from "../../domain/media.js";
2
+ import { ToolCallPairing } from "../../helpers/tool-call-pairing.js";
3
+ /** Google's documented value that skips signature validation on a replayed function call. */
4
+ const SKIP_SIGNATURE_VALIDATION = "skip_thought_signature_validator";
5
+ /** Reject, at gateway creation, a model with a cache lifetime: implicit caching documents none to send. */
6
+ export function assertCacheTtlsOnWire(model) {
7
+ if (model.promptCache.shortTtlSeconds !== null || model.promptCache.longTtlSeconds !== null) {
8
+ throw new Error(`Gemini implicit caching has no lifetime to send; ${model.id} must declare none`);
9
+ }
10
+ }
11
+ function mediaPart(part, target) {
12
+ const media = wireMediaSource(target, part);
13
+ return "url" in media
14
+ ? { fileData: { fileUri: media.url, mimeType: part.mediaType } }
15
+ : { inlineData: { data: media.data, mimeType: media.mediaType } };
16
+ }
17
+ function functionResponsePart(part, target) {
18
+ const { fileData, inlineData } = mediaPart(part, target);
19
+ return fileData
20
+ ? { fileData: { fileUri: fileData.fileUri, mimeType: fileData.mimeType } }
21
+ : { inlineData: { data: inlineData?.data, mimeType: inlineData?.mimeType } };
22
+ }
23
+ /**
24
+ * Gemini 3 returns an id on each function call and pairs a result by it, so parallel calls to
25
+ * one tool stay distinct; an older model has none and pairs by name and order.
26
+ */
27
+ function callId(toolCallId, target) {
28
+ return (geminiMajorVersion(target.id) ?? 0) >= 3 ? { id: toolCallId } : {};
29
+ }
30
+ function textOf(parts) {
31
+ return parts
32
+ .filter((p) => p.type === "text")
33
+ .map((p) => p.text)
34
+ .join("");
35
+ }
36
+ function signatureFor(part, target) {
37
+ const origin = part.providerOptions?.meridian;
38
+ if (origin?.provider !== target.provider || origin?.model !== target.id)
39
+ return undefined;
40
+ const signature = part.providerOptions?.gemini?.signature;
41
+ return typeof signature === "string" ? signature : undefined;
42
+ }
43
+ function userParts(message, target) {
44
+ const parts = [];
45
+ for (const part of message.content) {
46
+ if (part.type === "text" && part.text.length > 0)
47
+ parts.push({ text: part.text });
48
+ else if (part.type === "media")
49
+ parts.push(mediaPart(part, target));
50
+ }
51
+ return parts;
52
+ }
53
+ function modelParts(message, target) {
54
+ const parts = [];
55
+ let firstCall = true;
56
+ for (const part of message.content) {
57
+ if (part.type !== "reasoning" && part.type !== "text" && part.type !== "tool_use")
58
+ continue;
59
+ const thoughtSignature = signatureFor(part, target);
60
+ if (part.type === "reasoning") {
61
+ // A summary without its signature carries nothing the API needs back.
62
+ if (thoughtSignature)
63
+ parts.push({ text: part.text, thought: true, thoughtSignature });
64
+ }
65
+ else if (part.type === "text") {
66
+ if (part.text.length > 0 || thoughtSignature) {
67
+ parts.push({ text: part.text, ...(thoughtSignature ? { thoughtSignature } : {}) });
68
+ }
69
+ }
70
+ else if (part.type === "tool_use") {
71
+ const signature = thoughtSignature ??
72
+ (firstCall && (geminiMajorVersion(target.id) ?? 0) >= 3
73
+ ? SKIP_SIGNATURE_VALIDATION
74
+ : undefined);
75
+ parts.push({
76
+ functionCall: { ...callId(part.toolCallId, target), name: part.toolName, args: part.input },
77
+ ...(signature ? { thoughtSignature: signature } : {}),
78
+ });
79
+ firstCall = false;
80
+ }
81
+ }
82
+ return parts;
83
+ }
84
+ function toolResultParts(message, calls, target) {
85
+ const parts = [];
86
+ for (const result of message.content) {
87
+ if (result.type !== "tool_result")
88
+ continue;
89
+ const name = calls.answer(result.toolCallId);
90
+ if (name === undefined) {
91
+ throw new Error(`Tool result ${result.toolCallId} has no matching tool call to name`);
92
+ }
93
+ const media = result.content.filter((p) => p.type === "media");
94
+ if (media.length > 0 && (geminiMajorVersion(target.id) ?? 0) < 3) {
95
+ throw new Error(`${target.id} takes no media inside a function response (Gemini 3 and later)`);
96
+ }
97
+ const text = textOf(result.content);
98
+ const functionResponse = {
99
+ ...callId(result.toolCallId, target),
100
+ name,
101
+ response: result.isError ? { error: text } : { output: text },
102
+ ...(media.length > 0 ? { parts: media.map((p) => functionResponsePart(p, target)) } : {}),
103
+ };
104
+ parts.push({ functionResponse });
105
+ }
106
+ return parts;
107
+ }
108
+ function mapContents(messages, target) {
109
+ const calls = new ToolCallPairing();
110
+ const contents = [];
111
+ for (const message of messages) {
112
+ if (message.role === "system")
113
+ continue;
114
+ for (const part of message.content) {
115
+ if (part.type === "tool_use")
116
+ calls.call(part.toolCallId, part.toolName);
117
+ }
118
+ const role = message.role === "assistant" ? "model" : "user";
119
+ const parts = message.role === "tool"
120
+ ? toolResultParts(message, calls, target)
121
+ : message.role === "assistant"
122
+ ? modelParts(message, target)
123
+ : userParts(message, target);
124
+ if (parts.length === 0)
125
+ continue;
126
+ const last = contents.at(-1);
127
+ if (last?.role === role)
128
+ last.parts?.push(...parts);
129
+ else
130
+ contents.push({ role, parts });
131
+ }
132
+ return contents;
133
+ }
134
+ function mapTools(tools) {
135
+ if (!tools?.length)
136
+ return undefined;
137
+ return [
138
+ {
139
+ functionDeclarations: tools.map((tool) => ({
140
+ name: tool.name,
141
+ description: tool.description,
142
+ parametersJsonSchema: tool.inputSchema,
143
+ })),
144
+ },
145
+ ];
146
+ }
147
+ /**
148
+ * `default` and an effort ask for thought summaries so reasoning streams; `off` sends a zero
149
+ * budget. `max` has no Gemini level above `high`, and no registered Gemini model lists it.
150
+ */
151
+ function mapThinking(reasoning) {
152
+ if (reasoning === "off")
153
+ return { thinkingBudget: 0 };
154
+ if (reasoning === "default")
155
+ return { includeThoughts: true };
156
+ const level = { low: "LOW", medium: "MEDIUM", high: "HIGH", max: "HIGH" }[reasoning.effort];
157
+ return { includeThoughts: true, thinkingLevel: level };
158
+ }
159
+ export function toGeminiParams(request, model, call) {
160
+ const system = request.messages
161
+ .filter((m) => m.role === "system")
162
+ .map((m) => textOf(m.content))
163
+ .filter((text) => text.length > 0)
164
+ .join("\n");
165
+ return {
166
+ model: model.id,
167
+ contents: mapContents(request.messages, model),
168
+ config: {
169
+ ...(system ? { systemInstruction: system } : {}),
170
+ tools: mapTools(request.tools),
171
+ thinkingConfig: mapThinking(call.reasoning),
172
+ // The gateway owns the loop and the request count: no SDK tool loop, no re-sent requests.
173
+ automaticFunctionCalling: { disable: true },
174
+ automaticContinuation: false,
175
+ abortSignal: request.signal,
176
+ },
177
+ };
178
+ }
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Gemini stream accumulator: folds `streamGenerateContent` chunks into canonical StreamEvents
3
+ * and a final GenerateResult.
4
+ *
5
+ * - Parts arrive in order: thought summaries (`thought: true`), text, and complete function
6
+ * calls (Gemini never streams call arguments in pieces). Content keeps that order; text and
7
+ * thought runs merge into one part until the kind changes.
8
+ * - A `thoughtSignature` arrives on a text, thought or function-call part, sometimes on an empty
9
+ * trailing text part. It is kept in `providerOptions.gemini.signature` beside the issuing
10
+ * provider/model, and the request mapper sends it back to that pair only.
11
+ * - Usage is cumulative on every chunk. Gemini counts thought tokens beside the answer's, and
12
+ * tool-use prompt tokens beside the prompt's; `Usage` is inclusive, so both are folded in. A file
13
+ * fetched by link appears only in `totalTokenCount`, so the input is read from that total.
14
+ * - A function call may carry no id (before Gemini 3); the position-based `call_<n>` stands in,
15
+ * and the mapper sends an id back only to Gemini 3 and later, which returns real ones.
16
+ */
17
+ import type { GenerateContentResponse } from "@google/genai";
18
+ import type { ContentPart, FinishReason, GenerateResult, ModelInfo, StreamEvent } from "../../domain/index.js";
19
+ import { type Usage } from "../../shapes/index.js";
20
+ export interface StreamAccumulator {
21
+ content: ContentPart[];
22
+ usage: Usage;
23
+ finishReason: FinishReason;
24
+ model: string;
25
+ provider: string;
26
+ }
27
+ export declare function createStreamAccumulator(model: Pick<ModelInfo, "id">, provider: string): StreamAccumulator;
28
+ export declare function eventsFromGeminiChunk(chunk: GenerateContentResponse, acc: StreamAccumulator): Generator<StreamEvent>;
29
+ export declare function buildGenerateResult(acc: StreamAccumulator): GenerateResult;
@@ -0,0 +1,116 @@
1
+ import { assertValidUsage } from "../../shapes/index.js";
2
+ export function createStreamAccumulator(model, provider) {
3
+ return {
4
+ content: [],
5
+ usage: { inputTokens: 0, outputTokens: 0 },
6
+ finishReason: "end_turn",
7
+ model: model.id,
8
+ provider,
9
+ };
10
+ }
11
+ /** Gemini's finish reasons that mean the output was cut or refused; everything else is a normal stop. */
12
+ function mapFinishReason(reason) {
13
+ if (reason === undefined || reason === "STOP" || reason === "FINISH_REASON_UNSPECIFIED") {
14
+ return undefined;
15
+ }
16
+ return reason === "MAX_TOKENS" || reason === "CONTINUATION" ? "max_tokens" : "error";
17
+ }
18
+ function usageOf(metadata) {
19
+ const thoughts = metadata.thoughtsTokenCount ?? 0;
20
+ const cached = metadata.cachedContentTokenCount ?? 0;
21
+ const outputTokens = (metadata.candidatesTokenCount ?? 0) + thoughts;
22
+ // A file fetched by link is counted in `totalTokenCount` only: `promptTokenCount` is absent.
23
+ const inputTokens = Math.max((metadata.promptTokenCount ?? 0) + (metadata.toolUsePromptTokenCount ?? 0), (metadata.totalTokenCount ?? 0) - outputTokens);
24
+ const usage = {
25
+ inputTokens,
26
+ outputTokens,
27
+ ...(thoughts ? { reasoningTokens: thoughts } : {}),
28
+ ...(cached ? { cacheReadTokens: cached } : {}),
29
+ };
30
+ assertValidUsage(usage);
31
+ return usage;
32
+ }
33
+ function sameUsage(a, b) {
34
+ return (a.inputTokens === b.inputTokens &&
35
+ a.outputTokens === b.outputTokens &&
36
+ a.reasoningTokens === b.reasoningTokens &&
37
+ a.cacheReadTokens === b.cacheReadTokens);
38
+ }
39
+ function withSignature(signature, acc) {
40
+ return {
41
+ gemini: { signature },
42
+ meridian: { provider: acc.provider, model: acc.model },
43
+ };
44
+ }
45
+ function* eventsFromPart(part, acc) {
46
+ const signature = part.thoughtSignature;
47
+ if (part.functionCall) {
48
+ const { name = "", args = {}, id } = part.functionCall;
49
+ const callId = id ?? `call_${acc.content.filter((p) => p.type === "tool_use").length}`;
50
+ acc.content.push({
51
+ type: "tool_use",
52
+ toolCallId: callId,
53
+ toolName: name,
54
+ input: args,
55
+ ...(signature ? { providerOptions: withSignature(signature, acc) } : {}),
56
+ });
57
+ yield { type: "tool_call.delta", id: callId, name, argumentsDelta: JSON.stringify(args) };
58
+ return;
59
+ }
60
+ if (part.text === undefined)
61
+ return;
62
+ const kind = part.thought ? "reasoning" : "text";
63
+ if (part.text.length === 0 && !signature)
64
+ return;
65
+ const last = acc.content.at(-1);
66
+ let target;
67
+ if (last?.type === kind)
68
+ target = last;
69
+ else {
70
+ target = { type: kind, text: "" };
71
+ acc.content.push(target);
72
+ }
73
+ target.text += part.text;
74
+ if (signature)
75
+ target.providerOptions = withSignature(signature, acc);
76
+ if (part.text.length > 0) {
77
+ yield kind === "reasoning"
78
+ ? { type: "reasoning.delta", text: part.text }
79
+ : { type: "text.delta", text: part.text };
80
+ }
81
+ }
82
+ export function* eventsFromGeminiChunk(chunk, acc) {
83
+ if (chunk.usageMetadata) {
84
+ const usage = usageOf(chunk.usageMetadata);
85
+ if (!sameUsage(usage, acc.usage)) {
86
+ acc.usage = usage;
87
+ yield { type: "usage", usage };
88
+ }
89
+ }
90
+ // A blocked prompt comes with no candidate at all.
91
+ if (chunk.promptFeedback?.blockReason)
92
+ acc.finishReason = "error";
93
+ const candidate = chunk.candidates?.[0];
94
+ if (!candidate)
95
+ return;
96
+ for (const part of candidate.content?.parts ?? [])
97
+ yield* eventsFromPart(part, acc);
98
+ const reason = mapFinishReason(candidate.finishReason);
99
+ if (reason)
100
+ acc.finishReason = reason;
101
+ }
102
+ export function buildGenerateResult(acc) {
103
+ const toolCalls = acc.content.flatMap((part) => part.type === "tool_use"
104
+ ? [{ id: part.toolCallId, name: part.toolName, arguments: part.input }]
105
+ : []);
106
+ // A refusal or a cut-off call is not executable, even when a call was assembled.
107
+ const finishReason = acc.finishReason === "end_turn" && toolCalls.length > 0 ? "tool_use" : acc.finishReason;
108
+ return {
109
+ content: [...acc.content],
110
+ toolCalls,
111
+ finishReason,
112
+ usage: acc.usage,
113
+ model: acc.model,
114
+ provider: acc.provider,
115
+ };
116
+ }
@@ -0,0 +1,11 @@
1
+ /** Normal mock responses: resolve content once, then serialize the same response as JSON or paced SSE. */
2
+ import type { ServerResponse } from "node:http";
3
+ import type { MockModelStep } from "../../mock-script.js";
4
+ export declare function writeMockResponse(res: ServerResponse, input: {
5
+ id: string;
6
+ model: string;
7
+ stream: boolean;
8
+ step: MockModelStep;
9
+ requestCount: number;
10
+ promptTokens: number;
11
+ }): void;
@@ -0,0 +1,160 @@
1
+ import { MOCK_SAMPLES } from "./samples.js";
2
+ const LOREM_WORDS = "Lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod tempor incididunt ut labore et dolore magna aliqua Ut enim ad minim veniam quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat".split(" ");
3
+ function resolveResponse(step, requestCount, promptTokens) {
4
+ const sample = step.sample ? MOCK_SAMPLES[step.sample] : undefined;
5
+ let text = step.text ?? sample?.text ?? "";
6
+ if (step.lorem) {
7
+ const paragraphSize = step.lorem.wordsPerParagraph ?? 80;
8
+ const words = [];
9
+ for (let i = 0; i < step.lorem.words; i++) {
10
+ const separator = i === 0 ? "" : i % paragraphSize === 0 ? ".\n\n" : " ";
11
+ words.push(separator + LOREM_WORDS[i % LOREM_WORDS.length]);
12
+ }
13
+ text = `${words.join("")}.`;
14
+ }
15
+ else if (step.repeat !== undefined) {
16
+ text = Array.from({ length: step.repeat }, () => text).join("\n\n");
17
+ }
18
+ const reasoning = step.reasoning ?? sample?.reasoning ?? "";
19
+ const toolCalls = (step.toolCalls ?? []).map((call, index) => ({
20
+ id: call.id ?? `call_mock_${requestCount}_${index}`,
21
+ type: "function",
22
+ function: { name: call.name, arguments: call.rawArgs ?? JSON.stringify(call.args ?? {}) },
23
+ }));
24
+ const reasoningTokens = Math.ceil(Buffer.byteLength(reasoning, "utf8") / 3);
25
+ const outputTokens = Math.max(1, reasoningTokens +
26
+ Math.ceil(Buffer.byteLength(text + toolCalls.map((call) => call.function.name + call.function.arguments).join(""), "utf8") / 3));
27
+ return {
28
+ text,
29
+ reasoning,
30
+ toolCalls,
31
+ finishReason: step.finishReason ?? (toolCalls.length ? "tool_calls" : "stop"),
32
+ usage: {
33
+ prompt_tokens: promptTokens,
34
+ completion_tokens: outputTokens,
35
+ total_tokens: promptTokens + outputTokens,
36
+ ...(reasoningTokens
37
+ ? { completion_tokens_details: { reasoning_tokens: reasoningTokens } }
38
+ : {}),
39
+ },
40
+ };
41
+ }
42
+ /** Code points keep surrogate pairs intact, including when chunkSize is one. */
43
+ function* fragments(text, size) {
44
+ let fragment = "";
45
+ let count = 0;
46
+ for (const character of text) {
47
+ fragment += character;
48
+ if (++count === size) {
49
+ yield fragment;
50
+ fragment = "";
51
+ count = 0;
52
+ }
53
+ }
54
+ if (fragment)
55
+ yield fragment;
56
+ }
57
+ function* responseChunks(id, model, response, chunkSize) {
58
+ const chunk = (delta, finishReason = null, usage) => `data: ${JSON.stringify({
59
+ id,
60
+ object: "chat.completion.chunk",
61
+ model,
62
+ choices: [{ index: 0, delta, finish_reason: finishReason }],
63
+ ...(usage ? { usage } : {}),
64
+ })}\n\n`;
65
+ for (const text of fragments(response.reasoning, chunkSize)) {
66
+ yield chunk({ reasoning_content: text });
67
+ }
68
+ for (const text of fragments(response.text, chunkSize))
69
+ yield chunk({ content: text });
70
+ for (const [index, call] of response.toolCalls.entries()) {
71
+ yield chunk({
72
+ tool_calls: [{ index, ...call, function: { name: call.function.name, arguments: "" } }],
73
+ });
74
+ for (const args of fragments(call.function.arguments, chunkSize)) {
75
+ yield chunk({ tool_calls: [{ index, function: { arguments: args } }] });
76
+ }
77
+ }
78
+ yield chunk({}, response.finishReason, response.usage);
79
+ }
80
+ export function writeMockResponse(res, input) {
81
+ const { id, model, step } = input;
82
+ let timer;
83
+ let chunks;
84
+ const cleanup = () => {
85
+ clearTimeout(timer);
86
+ res.off("drain", scheduleNext);
87
+ res.off("close", cleanup);
88
+ chunks?.return(undefined);
89
+ };
90
+ res.once("close", cleanup);
91
+ function scheduleNext() {
92
+ if (res.destroyed)
93
+ return;
94
+ if (step.chunkDelayMs)
95
+ timer = setTimeout(pump, step.chunkDelayMs).unref();
96
+ else
97
+ pump();
98
+ }
99
+ function pump() {
100
+ while (!res.destroyed) {
101
+ const next = chunks.next();
102
+ if (next.done) {
103
+ res.end("data: [DONE]\n\n");
104
+ return;
105
+ }
106
+ if (!res.write(next.value)) {
107
+ // Generate only as the consumer drains, including unpaced large responses.
108
+ res.once("drain", scheduleNext);
109
+ return;
110
+ }
111
+ if (step.chunkDelayMs) {
112
+ scheduleNext();
113
+ return;
114
+ }
115
+ }
116
+ }
117
+ const begin = () => {
118
+ if (res.destroyed)
119
+ return;
120
+ if (step.error) {
121
+ res.writeHead(step.error.status, { "Content-Type": "application/json" });
122
+ res.end(JSON.stringify({ error: { message: step.error.message, type: "mock_script_error" } }));
123
+ return;
124
+ }
125
+ const response = resolveResponse(step, input.requestCount, input.promptTokens);
126
+ if (!input.stream) {
127
+ res.writeHead(200, { "Content-Type": "application/json" });
128
+ res.end(JSON.stringify({
129
+ id,
130
+ object: "chat.completion",
131
+ model,
132
+ choices: [
133
+ {
134
+ index: 0,
135
+ message: {
136
+ role: "assistant",
137
+ content: response.text || null,
138
+ ...(response.reasoning ? { reasoning_content: response.reasoning } : {}),
139
+ ...(response.toolCalls.length ? { tool_calls: response.toolCalls } : {}),
140
+ },
141
+ finish_reason: response.finishReason,
142
+ },
143
+ ],
144
+ usage: response.usage,
145
+ }));
146
+ return;
147
+ }
148
+ res.writeHead(200, {
149
+ "Content-Type": "text/event-stream",
150
+ "Cache-Control": "no-cache",
151
+ Connection: "keep-alive",
152
+ });
153
+ chunks = responseChunks(id, model, response, step.chunkSize ?? 24);
154
+ pump();
155
+ };
156
+ if (step.delayMs)
157
+ timer = setTimeout(begin, step.delayMs).unref();
158
+ else
159
+ begin();
160
+ }
@@ -0,0 +1,8 @@
1
+ /** Authored fiction-workflow samples, not recordings or responses to the current prompt. */
2
+ import type { MockModelDefaultResponse } from "../../mock-script.js";
3
+ type SampleName = NonNullable<MockModelDefaultResponse["sample"]>;
4
+ export declare const MOCK_SAMPLES: Record<SampleName, {
5
+ reasoning: string;
6
+ text: string;
7
+ }>;
8
+ export {};