@meridian-flow/runtime 0.0.0-stage → 0.1.1
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 +76 -3
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Purpose: Wire contract for scripting the dev-only mock model: queued replies that answer the next model calls.
|
|
3
|
+
* Why independent: the dev CLI writes scripts and the server's mock gateway consumes them; both validate the same schema.
|
|
4
|
+
*/
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
/** Repeated defaults cannot execute tools or consume queue-step budgets. */
|
|
7
|
+
export declare const mockModelDefaultResponseSchema: z.ZodObject<{
|
|
8
|
+
text: z.ZodOptional<z.ZodString>;
|
|
9
|
+
sample: z.ZodOptional<z.ZodEnum<{
|
|
10
|
+
scene: "scene";
|
|
11
|
+
critique: "critique";
|
|
12
|
+
outline: "outline";
|
|
13
|
+
markdown: "markdown";
|
|
14
|
+
}>>;
|
|
15
|
+
lorem: z.ZodOptional<z.ZodObject<{
|
|
16
|
+
words: z.ZodNumber;
|
|
17
|
+
wordsPerParagraph: z.ZodOptional<z.ZodNumber>;
|
|
18
|
+
}, z.core.$strict>>;
|
|
19
|
+
reasoning: z.ZodOptional<z.ZodString>;
|
|
20
|
+
repeat: z.ZodOptional<z.ZodNumber>;
|
|
21
|
+
delayMs: z.ZodOptional<z.ZodNumber>;
|
|
22
|
+
chunkSize: z.ZodOptional<z.ZodNumber>;
|
|
23
|
+
chunkDelayMs: z.ZodOptional<z.ZodNumber>;
|
|
24
|
+
}, z.core.$strict>;
|
|
25
|
+
export type MockModelDefaultResponse = z.infer<typeof mockModelDefaultResponseSchema>;
|
|
26
|
+
/**
|
|
27
|
+
* One step answers `times` model calls (default 1). An error without `times`
|
|
28
|
+
* is sticky, so gateway retries and fallbacks fail deterministically.
|
|
29
|
+
*/
|
|
30
|
+
export declare const mockModelStepSchema: z.ZodObject<{
|
|
31
|
+
toolCalls: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
32
|
+
name: z.ZodString;
|
|
33
|
+
id: z.ZodOptional<z.ZodString>;
|
|
34
|
+
args: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
35
|
+
rawArgs: z.ZodOptional<z.ZodString>;
|
|
36
|
+
}, z.core.$strict>>>;
|
|
37
|
+
finishReason: z.ZodOptional<z.ZodEnum<{
|
|
38
|
+
length: "length";
|
|
39
|
+
content_filter: "content_filter";
|
|
40
|
+
}>>;
|
|
41
|
+
error: z.ZodOptional<z.ZodObject<{
|
|
42
|
+
status: z.ZodDefault<z.ZodNumber>;
|
|
43
|
+
message: z.ZodDefault<z.ZodString>;
|
|
44
|
+
}, z.core.$strict>>;
|
|
45
|
+
times: z.ZodOptional<z.ZodNumber>;
|
|
46
|
+
text: z.ZodOptional<z.ZodString>;
|
|
47
|
+
sample: z.ZodOptional<z.ZodEnum<{
|
|
48
|
+
scene: "scene";
|
|
49
|
+
critique: "critique";
|
|
50
|
+
outline: "outline";
|
|
51
|
+
markdown: "markdown";
|
|
52
|
+
}>>;
|
|
53
|
+
lorem: z.ZodOptional<z.ZodObject<{
|
|
54
|
+
words: z.ZodNumber;
|
|
55
|
+
wordsPerParagraph: z.ZodOptional<z.ZodNumber>;
|
|
56
|
+
}, z.core.$strict>>;
|
|
57
|
+
reasoning: z.ZodOptional<z.ZodString>;
|
|
58
|
+
repeat: z.ZodOptional<z.ZodNumber>;
|
|
59
|
+
delayMs: z.ZodOptional<z.ZodNumber>;
|
|
60
|
+
chunkSize: z.ZodOptional<z.ZodNumber>;
|
|
61
|
+
chunkDelayMs: z.ZodOptional<z.ZodNumber>;
|
|
62
|
+
}, z.core.$strict>;
|
|
63
|
+
export type MockModelStep = z.infer<typeof mockModelStepSchema>;
|
|
64
|
+
export declare const mockModelScriptSchema: z.ZodObject<{
|
|
65
|
+
match: z.ZodOptional<z.ZodString>;
|
|
66
|
+
steps: z.ZodArray<z.ZodObject<{
|
|
67
|
+
toolCalls: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
68
|
+
name: z.ZodString;
|
|
69
|
+
id: z.ZodOptional<z.ZodString>;
|
|
70
|
+
args: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
71
|
+
rawArgs: z.ZodOptional<z.ZodString>;
|
|
72
|
+
}, z.core.$strict>>>;
|
|
73
|
+
finishReason: z.ZodOptional<z.ZodEnum<{
|
|
74
|
+
length: "length";
|
|
75
|
+
content_filter: "content_filter";
|
|
76
|
+
}>>;
|
|
77
|
+
error: z.ZodOptional<z.ZodObject<{
|
|
78
|
+
status: z.ZodDefault<z.ZodNumber>;
|
|
79
|
+
message: z.ZodDefault<z.ZodString>;
|
|
80
|
+
}, z.core.$strict>>;
|
|
81
|
+
times: z.ZodOptional<z.ZodNumber>;
|
|
82
|
+
text: z.ZodOptional<z.ZodString>;
|
|
83
|
+
sample: z.ZodOptional<z.ZodEnum<{
|
|
84
|
+
scene: "scene";
|
|
85
|
+
critique: "critique";
|
|
86
|
+
outline: "outline";
|
|
87
|
+
markdown: "markdown";
|
|
88
|
+
}>>;
|
|
89
|
+
lorem: z.ZodOptional<z.ZodObject<{
|
|
90
|
+
words: z.ZodNumber;
|
|
91
|
+
wordsPerParagraph: z.ZodOptional<z.ZodNumber>;
|
|
92
|
+
}, z.core.$strict>>;
|
|
93
|
+
reasoning: z.ZodOptional<z.ZodString>;
|
|
94
|
+
repeat: z.ZodOptional<z.ZodNumber>;
|
|
95
|
+
delayMs: z.ZodOptional<z.ZodNumber>;
|
|
96
|
+
chunkSize: z.ZodOptional<z.ZodNumber>;
|
|
97
|
+
chunkDelayMs: z.ZodOptional<z.ZodNumber>;
|
|
98
|
+
}, z.core.$strict>>;
|
|
99
|
+
}, z.core.$strict>;
|
|
100
|
+
export type MockModelScript = z.infer<typeof mockModelScriptSchema>;
|
|
101
|
+
/** Accepts a bare step array as shorthand for `{ steps }`. */
|
|
102
|
+
export declare function parseMockModelScript(raw: unknown): {
|
|
103
|
+
ok: true;
|
|
104
|
+
value: MockModelScript;
|
|
105
|
+
} | {
|
|
106
|
+
ok: false;
|
|
107
|
+
error: string;
|
|
108
|
+
};
|
|
109
|
+
/** `remaining` counts steps left; `sticky` means the head step never runs out. */
|
|
110
|
+
export declare const mockModelScriptStateSchema: z.ZodObject<{
|
|
111
|
+
scripts: z.ZodArray<z.ZodObject<{
|
|
112
|
+
id: z.ZodString;
|
|
113
|
+
match: z.ZodNullable<z.ZodString>;
|
|
114
|
+
remaining: z.ZodNumber;
|
|
115
|
+
sticky: z.ZodBoolean;
|
|
116
|
+
}, z.core.$strip>>;
|
|
117
|
+
}, z.core.$strip>;
|
|
118
|
+
export type MockModelScriptState = z.infer<typeof mockModelScriptStateSchema>;
|
|
119
|
+
/** POST response: the queued script's id (for targeted removal) plus the queue. */
|
|
120
|
+
export declare const mockModelScriptEnqueuedSchema: z.ZodObject<{
|
|
121
|
+
scripts: z.ZodArray<z.ZodObject<{
|
|
122
|
+
id: z.ZodString;
|
|
123
|
+
match: z.ZodNullable<z.ZodString>;
|
|
124
|
+
remaining: z.ZodNumber;
|
|
125
|
+
sticky: z.ZodBoolean;
|
|
126
|
+
}, z.core.$strip>>;
|
|
127
|
+
id: z.ZodString;
|
|
128
|
+
}, z.core.$strip>;
|
|
129
|
+
export type MockModelScriptEnqueued = z.infer<typeof mockModelScriptEnqueuedSchema>;
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Purpose: Wire contract for scripting the dev-only mock model: queued replies that answer the next model calls.
|
|
3
|
+
* Why independent: the dev CLI writes scripts and the server's mock gateway consumes them; both validate the same schema.
|
|
4
|
+
*/
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
const toolCallSchema = z
|
|
7
|
+
.strictObject({
|
|
8
|
+
name: z.string().min(1),
|
|
9
|
+
/** The call's id as the provider names it; default `call_mock_<request>_<index>`. */
|
|
10
|
+
id: z.string().min(1).optional(),
|
|
11
|
+
args: z.record(z.string(), z.unknown()).optional(),
|
|
12
|
+
/** The arguments string sent as is, for malformed JSON; replaces `args`. */
|
|
13
|
+
rawArgs: z.string().max(100_000).optional(),
|
|
14
|
+
})
|
|
15
|
+
.refine((call) => call.args === undefined || call.rawArgs === undefined, {
|
|
16
|
+
message: "a tool call takes args or rawArgs, not both",
|
|
17
|
+
});
|
|
18
|
+
const responseFields = {
|
|
19
|
+
/** Choose one text source: literal text, an authored sample, or generated lorem. */
|
|
20
|
+
text: z.string().max(1_000_000).optional(),
|
|
21
|
+
sample: z.enum(["scene", "critique", "outline", "markdown"]).optional(),
|
|
22
|
+
lorem: z
|
|
23
|
+
.strictObject({
|
|
24
|
+
words: z.number().int().min(1).max(100_000),
|
|
25
|
+
wordsPerParagraph: z.number().int().min(1).max(1_000).optional(),
|
|
26
|
+
})
|
|
27
|
+
.optional(),
|
|
28
|
+
/** Thinking before text/tools; an empty string suppresses a sample's thinking. */
|
|
29
|
+
reasoning: z.string().max(1_000_000).optional(),
|
|
30
|
+
/** Repeat literal/sample text, separated by blank lines. Use lorem.words for lorem size. */
|
|
31
|
+
repeat: z.number().int().min(1).max(100).optional(),
|
|
32
|
+
/** Delay before headers and the first output. */
|
|
33
|
+
delayMs: z.number().int().min(0).max(120_000).optional(),
|
|
34
|
+
/** Unicode characters per text, reasoning, or tool-argument chunk (default 24). */
|
|
35
|
+
chunkSize: z.number().int().min(1).max(65_536).optional(),
|
|
36
|
+
/** Milliseconds between chunks; 0 streams as fast as the connection allows. */
|
|
37
|
+
chunkDelayMs: z.number().int().min(0).max(10_000).optional(),
|
|
38
|
+
};
|
|
39
|
+
function validTextSource(response) {
|
|
40
|
+
return ([response.text, response.sample, response.lorem].filter((value) => value !== undefined)
|
|
41
|
+
.length <= 1 &&
|
|
42
|
+
!(response.lorem && response.repeat !== undefined) &&
|
|
43
|
+
(response.text?.length ?? (response.sample ? 10_000 : 0)) * (response.repeat ?? 1) +
|
|
44
|
+
(response.reasoning?.length ?? 0) +
|
|
45
|
+
2 * ((response.repeat ?? 1) - 1) <=
|
|
46
|
+
2_000_000 &&
|
|
47
|
+
(response.repeat === undefined || response.text !== undefined || response.sample !== undefined));
|
|
48
|
+
}
|
|
49
|
+
const responseShape = z.strictObject(responseFields);
|
|
50
|
+
const sourceMessage = "choose at most one of text, sample, lorem; repeat needs text/sample; expanded text and reasoning must fit 2,000,000 characters";
|
|
51
|
+
/** Repeated defaults cannot execute tools or consume queue-step budgets. */
|
|
52
|
+
export const mockModelDefaultResponseSchema = responseShape
|
|
53
|
+
.refine(validTextSource, { message: sourceMessage })
|
|
54
|
+
.refine((response) => response.text !== undefined ||
|
|
55
|
+
response.sample ||
|
|
56
|
+
response.lorem ||
|
|
57
|
+
response.reasoning !== undefined, { message: "a default response needs text, sample, lorem, or reasoning" });
|
|
58
|
+
/**
|
|
59
|
+
* One step answers `times` model calls (default 1). An error without `times`
|
|
60
|
+
* is sticky, so gateway retries and fallbacks fail deterministically.
|
|
61
|
+
*/
|
|
62
|
+
export const mockModelStepSchema = z
|
|
63
|
+
.strictObject({
|
|
64
|
+
...responseFields,
|
|
65
|
+
/** Executed by the real runtime after reasoning/text. End a tool script with a text step. */
|
|
66
|
+
toolCalls: z.array(toolCallSchema).min(1).max(100).optional(),
|
|
67
|
+
/** Truncation/filtering instead of normal completion; partial tools must not execute. */
|
|
68
|
+
finishReason: z.enum(["length", "content_filter"]).optional(),
|
|
69
|
+
error: z
|
|
70
|
+
.strictObject({
|
|
71
|
+
status: z.number().int().min(400).max(599).default(500),
|
|
72
|
+
message: z.string().default("mock provider error"),
|
|
73
|
+
})
|
|
74
|
+
.optional(),
|
|
75
|
+
times: z.number().int().min(1).max(1_000).optional(),
|
|
76
|
+
})
|
|
77
|
+
.refine(validTextSource, { message: sourceMessage })
|
|
78
|
+
.refine((step) => step.text !== undefined ||
|
|
79
|
+
step.sample ||
|
|
80
|
+
step.lorem ||
|
|
81
|
+
step.reasoning !== undefined ||
|
|
82
|
+
step.toolCalls ||
|
|
83
|
+
step.error, { message: "a step needs text, sample, lorem, reasoning, toolCalls, or error" })
|
|
84
|
+
.refine((step) => !step.error ||
|
|
85
|
+
[
|
|
86
|
+
step.text,
|
|
87
|
+
step.sample,
|
|
88
|
+
step.lorem,
|
|
89
|
+
step.reasoning,
|
|
90
|
+
step.toolCalls,
|
|
91
|
+
step.finishReason,
|
|
92
|
+
step.repeat,
|
|
93
|
+
step.chunkSize,
|
|
94
|
+
step.chunkDelayMs,
|
|
95
|
+
].every((value) => value === undefined), { message: "error cannot be combined with response content or streaming controls" });
|
|
96
|
+
export const mockModelScriptSchema = z.strictObject({
|
|
97
|
+
/**
|
|
98
|
+
* Only model calls whose latest user message contains this text consume the
|
|
99
|
+
* script, so concurrent threads do not steal each other's steps. Omitted: any call.
|
|
100
|
+
*/
|
|
101
|
+
match: z.string().min(1).optional(),
|
|
102
|
+
steps: z.array(mockModelStepSchema).min(1).max(100),
|
|
103
|
+
});
|
|
104
|
+
/** Accepts a bare step array as shorthand for `{ steps }`. */
|
|
105
|
+
export function parseMockModelScript(raw) {
|
|
106
|
+
const candidate = Array.isArray(raw) ? { steps: raw } : raw;
|
|
107
|
+
const parsed = mockModelScriptSchema.safeParse(candidate);
|
|
108
|
+
if (parsed.success)
|
|
109
|
+
return { ok: true, value: parsed.data };
|
|
110
|
+
return {
|
|
111
|
+
ok: false,
|
|
112
|
+
error: parsed.error.issues
|
|
113
|
+
.map((issue) => `${issue.path.join(".") || "script"}: ${issue.message}`)
|
|
114
|
+
.join("; "),
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
/** `remaining` counts steps left; `sticky` means the head step never runs out. */
|
|
118
|
+
export const mockModelScriptStateSchema = z.object({
|
|
119
|
+
scripts: z.array(z.object({
|
|
120
|
+
id: z.string().min(1),
|
|
121
|
+
match: z.string().nullable(),
|
|
122
|
+
remaining: z.number().int().nonnegative(),
|
|
123
|
+
sticky: z.boolean(),
|
|
124
|
+
})),
|
|
125
|
+
});
|
|
126
|
+
/** POST response: the queued script's id (for targeted removal) plus the queue. */
|
|
127
|
+
export const mockModelScriptEnqueuedSchema = mockModelScriptStateSchema.extend({
|
|
128
|
+
id: z.string().min(1),
|
|
129
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The dev mock provider: an in-process OpenAI-compatible HTTP server with scripted
|
|
3
|
+
* and sample replies. Its own entry (`@meridian-flow/runtime/gateway/mock`) because it needs
|
|
4
|
+
* `node:http`, which the main entry stays free of.
|
|
5
|
+
*/
|
|
6
|
+
import type { ProviderConfig } from "./domain/index.js";
|
|
7
|
+
export type { MockScriptQueue } from "./adapters/mock/script-queue.js";
|
|
8
|
+
export { createMockScriptQueue } from "./adapters/mock/script-queue.js";
|
|
9
|
+
export type { MockOpenAIServer } from "./adapters/mock/server.js";
|
|
10
|
+
export { createMockOpenAICompatibleServer } from "./adapters/mock/server.js";
|
|
11
|
+
export declare function mockProviderConfig(baseUrl: string): ProviderConfig;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export { createMockScriptQueue } from "./adapters/mock/script-queue.js";
|
|
2
|
+
export { createMockOpenAICompatibleServer } from "./adapters/mock/server.js";
|
|
3
|
+
const MOCK_MODEL = {
|
|
4
|
+
id: "mock-llm-v1",
|
|
5
|
+
provider: "mock",
|
|
6
|
+
// The mock has no tokenizer; use the conservative estimation family explicitly.
|
|
7
|
+
tokenizer: "anthropic",
|
|
8
|
+
displayName: "Mock LLM",
|
|
9
|
+
contextWindow: 128_000,
|
|
10
|
+
maxOutputTokens: 4096,
|
|
11
|
+
promptCache: { kind: "automatic", shortTtlSeconds: null, longTtlSeconds: null },
|
|
12
|
+
reasoning: { efforts: ["low", "medium", "high", "max"] },
|
|
13
|
+
inputModalities: ["text", "image"],
|
|
14
|
+
};
|
|
15
|
+
export function mockProviderConfig(baseUrl) {
|
|
16
|
+
return {
|
|
17
|
+
id: "mock",
|
|
18
|
+
adapter: "openai-compatible",
|
|
19
|
+
baseUrl,
|
|
20
|
+
models: [MOCK_MODEL],
|
|
21
|
+
};
|
|
22
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gateway port: the provider-neutral interface (stream/listModels)
|
|
3
|
+
* the rest of the runtime depends on instead of any concrete provider.
|
|
4
|
+
*
|
|
5
|
+
* This is the top-level port that the orchestrator and turn runner consume.
|
|
6
|
+
* They call `gateway.stream(request)` to get an AsyncIterable<StreamEvent>.
|
|
7
|
+
* They never know which provider is behind the call.
|
|
8
|
+
*
|
|
9
|
+
* Why a port: adapters are DI/config-driven (see create-gateway.ts). The
|
|
10
|
+
* runtime composition root wires the right adapter based on GatewayConfig,
|
|
11
|
+
* and route handlers never import concrete adapter factories.
|
|
12
|
+
*/
|
|
13
|
+
import type { GenerateRequest, ModelInfo, StreamEvent } from "../domain/index.js";
|
|
14
|
+
export interface Gateway {
|
|
15
|
+
stream(request: GenerateRequest): AsyncIterable<StreamEvent>;
|
|
16
|
+
listModels(): ModelInfo[];
|
|
17
|
+
getDefaultModel(): string | undefined;
|
|
18
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provider-adapter port: the one streaming primitive each provider adapter
|
|
3
|
+
* (anthropic/openai/openai-compatible) implements.
|
|
4
|
+
*
|
|
5
|
+
* The gateway drives this through the retry machinery. Each adapter maps one provider's SDK events
|
|
6
|
+
* to canonical StreamEvents.
|
|
7
|
+
*
|
|
8
|
+
* Why one port: every provider has a different SDK contract, but the
|
|
9
|
+
* Meridian-gateway edge is uniform. Adding a new provider means implementing
|
|
10
|
+
* this one interface and registering it in createAdapter.
|
|
11
|
+
*/
|
|
12
|
+
import type { GenerateRequest, ModelInfo, ResolvedCall, StreamEvent } from "../domain/index.js";
|
|
13
|
+
/**
|
|
14
|
+
* Internal — adapters implement one streaming primitive. `call` carries the
|
|
15
|
+
* per-call decisions the gateway core made for the routed model (cache lifetime,
|
|
16
|
+
* long cache writes, reasoning); the adapter maps them to wire fields.
|
|
17
|
+
*/
|
|
18
|
+
export interface ProviderAdapter {
|
|
19
|
+
/** After the request's signal aborts, `end` carries whatever accumulated, possibly nothing; the caller owns what the stop means. */
|
|
20
|
+
stream(request: GenerateRequest, model: ModelInfo, call: ResolvedCall): AsyncIterable<StreamEvent>;
|
|
21
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provider routing: resolves a GenerateRequest to its provider adapter and
|
|
3
|
+
* model. Owns request->provider selection; depends inward on domain types and
|
|
4
|
+
* the provider-adapter port.
|
|
5
|
+
*/
|
|
6
|
+
import type { GenerateRequest, ModelInfo, ProviderConfig, TraceSpan } from "./domain/index.js";
|
|
7
|
+
import type { ProviderAdapter } from "./ports/provider-adapter.js";
|
|
8
|
+
/** The fully resolved output of resolveRoute() — everything needed to stream. */
|
|
9
|
+
export interface ResolvedRoute {
|
|
10
|
+
adapter: ProviderAdapter;
|
|
11
|
+
model: ModelInfo;
|
|
12
|
+
}
|
|
13
|
+
/** Model ID -> route, built once at gateway construction from the providers list. */
|
|
14
|
+
export type ProviderRegistry = Map<string, ResolvedRoute>;
|
|
15
|
+
/**
|
|
16
|
+
* Index every provider's models by ID. The first provider to declare a model
|
|
17
|
+
* keeps it; later duplicates are skipped with a warning.
|
|
18
|
+
*/
|
|
19
|
+
export declare function buildProviderRegistry(providers: Array<{
|
|
20
|
+
config: ProviderConfig;
|
|
21
|
+
adapter: ProviderAdapter;
|
|
22
|
+
}>, onWarning?: (span: TraceSpan) => void): ProviderRegistry;
|
|
23
|
+
/**
|
|
24
|
+
* Resolve the adapter and model for a request: `request.model` if present, else
|
|
25
|
+
* `defaultModel`. Throws on a missing or unknown model; the gateway.stream()
|
|
26
|
+
* caller surfaces it as a non-retryable invalid_request error.
|
|
27
|
+
*/
|
|
28
|
+
export declare function resolveRoute(registry: ProviderRegistry, request: GenerateRequest, defaultModel?: string): ResolvedRoute;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Index every provider's models by ID. The first provider to declare a model
|
|
3
|
+
* keeps it; later duplicates are skipped with a warning.
|
|
4
|
+
*/
|
|
5
|
+
export function buildProviderRegistry(providers, onWarning) {
|
|
6
|
+
const registry = new Map();
|
|
7
|
+
for (const { config, adapter } of providers) {
|
|
8
|
+
for (const model of config.models) {
|
|
9
|
+
const existing = registry.get(model.id);
|
|
10
|
+
if (existing) {
|
|
11
|
+
onWarning?.({
|
|
12
|
+
name: "gateway.model_collision_skipped",
|
|
13
|
+
attributes: {
|
|
14
|
+
modelId: model.id,
|
|
15
|
+
keptProviderId: existing.model.provider,
|
|
16
|
+
skippedProviderId: config.id,
|
|
17
|
+
},
|
|
18
|
+
});
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
21
|
+
registry.set(model.id, {
|
|
22
|
+
adapter,
|
|
23
|
+
model: {
|
|
24
|
+
...model,
|
|
25
|
+
wire: config.adapter,
|
|
26
|
+
...(config.imageLinks ? { imageLinks: true } : {}),
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return registry;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Resolve the adapter and model for a request: `request.model` if present, else
|
|
35
|
+
* `defaultModel`. Throws on a missing or unknown model; the gateway.stream()
|
|
36
|
+
* caller surfaces it as a non-retryable invalid_request error.
|
|
37
|
+
*/
|
|
38
|
+
export function resolveRoute(registry, request, defaultModel) {
|
|
39
|
+
const modelId = request.model ?? defaultModel;
|
|
40
|
+
if (!modelId) {
|
|
41
|
+
throw new Error("No model specified and no defaultModel configured");
|
|
42
|
+
}
|
|
43
|
+
const route = registry.get(modelId);
|
|
44
|
+
if (!route) {
|
|
45
|
+
throw new Error(`Unknown model: ${modelId}`);
|
|
46
|
+
}
|
|
47
|
+
return route;
|
|
48
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The gateway's plain data shapes. Browser-safe: no Node built-ins and no SDKs,
|
|
3
|
+
* so `@meridian-flow/runtime/gateway/shapes` can be imported from a client bundle.
|
|
4
|
+
*/
|
|
5
|
+
export type { JsonValue } from "./json.js";
|
|
6
|
+
export type { ProviderErrorResponse } from "./provider-error.js";
|
|
7
|
+
export { assertValidUsage, type Usage } from "./usage.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { assertValidUsage } from "./usage.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The provider's own status and message, kept as owner-scoped debug evidence for
|
|
3
|
+
* `./mf thread view`. Never enters event payloads or model context, and the app
|
|
4
|
+
* never shows the message.
|
|
5
|
+
*/
|
|
6
|
+
export type ProviderErrorResponse = {
|
|
7
|
+
/** HTTP status; null when the provider reported the failure inside the stream. */
|
|
8
|
+
status: number | null;
|
|
9
|
+
message: string;
|
|
10
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Purpose: Defines the JSON-natural token usage contract shared by the model
|
|
3
|
+
* gateway, runtime metering, and billing price conversion.
|
|
4
|
+
* Key decision: provider adapters normalize their native token accounting into
|
|
5
|
+
* this plain DTO before any runtime or billing logic consumes it.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Normalized token usage across providers.
|
|
9
|
+
*
|
|
10
|
+
* `inputTokens` is the inclusive input total. `cacheReadTokens` and
|
|
11
|
+
* `cacheWriteTokens` are disjoint subsets of that total, so their sum must not
|
|
12
|
+
* exceed `inputTokens`. This shape keeps persisted totals and token displays
|
|
13
|
+
* comparable across providers and lets provider-agnostic pricing derive the
|
|
14
|
+
* uncached remainder. Adapters for providers such as Anthropic, which reports
|
|
15
|
+
* uncached input and cache counters as separate additive categories, must sum
|
|
16
|
+
* those categories at the gateway boundary.
|
|
17
|
+
*
|
|
18
|
+
* Provider mapping:
|
|
19
|
+
* - Anthropic: inclusive input is `input_tokens` +
|
|
20
|
+
* `cache_read_input_tokens` + `cache_creation_input_tokens`; output and
|
|
21
|
+
* reasoning come from `output_tokens` and
|
|
22
|
+
* `output_tokens_details.thinking_tokens`.
|
|
23
|
+
* - OpenAI Responses: `usage.input_tokens` / `usage.output_tokens`, cache from
|
|
24
|
+
* `input_tokens_details.cached_tokens`, reasoning from
|
|
25
|
+
* `output_tokens_details.reasoning_tokens`.
|
|
26
|
+
* - OpenAI-Chat-Compatible: `usage.prompt_tokens` / `usage.completion_tokens`,
|
|
27
|
+
* reasoning from `completion_tokens_details.reasoning_tokens`.
|
|
28
|
+
*
|
|
29
|
+
* Optional fields: only set when the provider reports a positive (>0) value.
|
|
30
|
+
*/
|
|
31
|
+
export interface Usage {
|
|
32
|
+
inputTokens: number;
|
|
33
|
+
outputTokens: number;
|
|
34
|
+
reasoningTokens?: number;
|
|
35
|
+
cacheReadTokens?: number;
|
|
36
|
+
cacheWriteTokens?: number;
|
|
37
|
+
/** Subset of `cacheWriteTokens` written with the model's long TTL (Anthropic 1h, OpenAI 12h); the rest use the short TTL. Set only by providers that bill writes by TTL. */
|
|
38
|
+
cacheWriteLongTokens?: number;
|
|
39
|
+
}
|
|
40
|
+
/** Throws when a Usage value violates the canonical inclusive-total contract. */
|
|
41
|
+
export declare function assertValidUsage(usage: Usage): void;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Purpose: Defines the JSON-natural token usage contract shared by the model
|
|
3
|
+
* gateway, runtime metering, and billing price conversion.
|
|
4
|
+
* Key decision: provider adapters normalize their native token accounting into
|
|
5
|
+
* this plain DTO before any runtime or billing logic consumes it.
|
|
6
|
+
*/
|
|
7
|
+
/** Throws when a Usage value violates the canonical inclusive-total contract. */
|
|
8
|
+
export function assertValidUsage(usage) {
|
|
9
|
+
const counts = [
|
|
10
|
+
["inputTokens", usage.inputTokens],
|
|
11
|
+
["outputTokens", usage.outputTokens],
|
|
12
|
+
["reasoningTokens", usage.reasoningTokens],
|
|
13
|
+
["cacheReadTokens", usage.cacheReadTokens],
|
|
14
|
+
["cacheWriteTokens", usage.cacheWriteTokens],
|
|
15
|
+
["cacheWriteLongTokens", usage.cacheWriteLongTokens],
|
|
16
|
+
];
|
|
17
|
+
for (const [field, count] of counts) {
|
|
18
|
+
if (count !== undefined && (!Number.isInteger(count) || count < 0)) {
|
|
19
|
+
throw new Error(`Usage.${field} must be a non-negative integer; got ${count}`);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
const cacheTokens = (usage.cacheReadTokens ?? 0) + (usage.cacheWriteTokens ?? 0);
|
|
23
|
+
if ((usage.cacheWriteLongTokens ?? 0) > (usage.cacheWriteTokens ?? 0)) {
|
|
24
|
+
throw new Error("Usage invariant violated: cacheWriteLongTokens must not exceed cacheWriteTokens");
|
|
25
|
+
}
|
|
26
|
+
if (cacheTokens > usage.inputTokens) {
|
|
27
|
+
throw new Error("Usage invariant violated: cacheReadTokens + cacheWriteTokens must not exceed inputTokens");
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stream-event classification shared by the retry gate and per-attempt timing.
|
|
3
|
+
*
|
|
4
|
+
* Two questions look similar but must stay separate:
|
|
5
|
+
* - Did it emit the first model output token? (`isFirstTokenEvent`) — drives
|
|
6
|
+
* provider-arrival TTFT and generation-duration timing.
|
|
7
|
+
* - Did it emit output the caller may have already acted on?
|
|
8
|
+
* (`isCommittedOutputEvent`) — drives the retry gate.
|
|
9
|
+
*
|
|
10
|
+
* Reasoning deltas and usage are process output, not committed output: a
|
|
11
|
+
* provider that reasons and then stalls before answering is retryable, while
|
|
12
|
+
* one that has already streamed visible text or opened a tool call is not.
|
|
13
|
+
*/
|
|
14
|
+
import type { StreamEvent } from "./domain/index.js";
|
|
15
|
+
/** Content deltas the writer can receive. */
|
|
16
|
+
export declare function isFirstTokenEvent(event: StreamEvent): boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Output the caller may have acted on: visible text, a tool call, or a terminal success. Reasoning and usage are excluded so an
|
|
19
|
+
* attempt aborted while still thinking remains retryable.
|
|
20
|
+
*/
|
|
21
|
+
export declare function isCommittedOutputEvent(event: StreamEvent): boolean;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/** Content deltas the writer can receive. */
|
|
2
|
+
export function isFirstTokenEvent(event) {
|
|
3
|
+
switch (event.type) {
|
|
4
|
+
case "text.delta":
|
|
5
|
+
case "reasoning.delta":
|
|
6
|
+
return event.text.length > 0;
|
|
7
|
+
case "tool_call.delta":
|
|
8
|
+
return event.argumentsDelta.length > 0;
|
|
9
|
+
default:
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Output the caller may have acted on: visible text, a tool call, or a terminal success. Reasoning and usage are excluded so an
|
|
15
|
+
* attempt aborted while still thinking remains retryable.
|
|
16
|
+
*/
|
|
17
|
+
export function isCommittedOutputEvent(event) {
|
|
18
|
+
switch (event.type) {
|
|
19
|
+
case "text.delta":
|
|
20
|
+
case "tool_call.delta":
|
|
21
|
+
case "end":
|
|
22
|
+
return true;
|
|
23
|
+
default:
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,79 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@meridian-flow/runtime",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "What an AI product needs to run models: a provider-neutral streaming model gateway (Anthropic, OpenAI, Gemini, OpenAI-compatible) with retry, deadlines and usage normalization.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"llm",
|
|
7
|
+
"ai",
|
|
8
|
+
"model-gateway",
|
|
9
|
+
"anthropic",
|
|
10
|
+
"openai",
|
|
11
|
+
"gemini",
|
|
12
|
+
"streaming",
|
|
13
|
+
"workload-identity-federation"
|
|
14
|
+
],
|
|
15
|
+
"license": "Apache-2.0",
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/haowjy/meridian-runtime.git"
|
|
19
|
+
},
|
|
20
|
+
"homepage": "https://github.com/haowjy/meridian-runtime#readme",
|
|
21
|
+
"bugs": {
|
|
22
|
+
"url": "https://github.com/haowjy/meridian-runtime/issues"
|
|
23
|
+
},
|
|
24
|
+
"type": "module",
|
|
25
|
+
"sideEffects": false,
|
|
26
|
+
"engines": {
|
|
27
|
+
"node": ">=20"
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"dist"
|
|
31
|
+
],
|
|
32
|
+
"exports": {
|
|
33
|
+
"./gateway": {
|
|
34
|
+
"types": "./dist/gateway/index.d.ts",
|
|
35
|
+
"default": "./dist/gateway/index.js"
|
|
36
|
+
},
|
|
37
|
+
"./gateway/shapes": {
|
|
38
|
+
"types": "./dist/gateway/shapes/index.d.ts",
|
|
39
|
+
"default": "./dist/gateway/shapes/index.js"
|
|
40
|
+
},
|
|
41
|
+
"./gateway/mock": {
|
|
42
|
+
"types": "./dist/gateway/mock.d.ts",
|
|
43
|
+
"default": "./dist/gateway/mock.js"
|
|
44
|
+
},
|
|
45
|
+
"./gateway/mock-script": {
|
|
46
|
+
"types": "./dist/gateway/mock-script.d.ts",
|
|
47
|
+
"default": "./dist/gateway/mock-script.js"
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
"meridianRuntime": {
|
|
51
|
+
"nodeFreeEntries": [
|
|
52
|
+
"./gateway",
|
|
53
|
+
"./gateway/shapes"
|
|
54
|
+
]
|
|
55
|
+
},
|
|
56
|
+
"dependencies": {
|
|
57
|
+
"@anthropic-ai/sdk": "^0.106.0",
|
|
58
|
+
"@google/genai": "^3.3.0",
|
|
59
|
+
"jsonrepair": "^3.14.1",
|
|
60
|
+
"openai": "^6.45.0",
|
|
61
|
+
"zod": "^4.4.3"
|
|
62
|
+
},
|
|
63
|
+
"devDependencies": {
|
|
64
|
+
"@biomejs/biome": "^2.5.1",
|
|
65
|
+
"@types/node": "^24",
|
|
66
|
+
"typescript": "^6.0.3",
|
|
67
|
+
"vitest": "^4.1.9"
|
|
68
|
+
},
|
|
69
|
+
"scripts": {
|
|
70
|
+
"build": "rm -rf dist && tsc -p tsconfig.build.json",
|
|
71
|
+
"dev": "tsc -p tsconfig.build.json --watch --preserveWatchOutput",
|
|
72
|
+
"lint": "biome check .",
|
|
73
|
+
"format:write": "biome check --write .",
|
|
74
|
+
"typecheck": "tsc -p tsconfig.json",
|
|
75
|
+
"test": "vitest run",
|
|
76
|
+
"check:package": "node tools/check-package.mjs",
|
|
77
|
+
"check": "pnpm lint && pnpm typecheck && pnpm test && pnpm check:package"
|
|
78
|
+
}
|
|
6
79
|
}
|