@meridian-flow/runtime 0.1.2 → 0.2.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.
@@ -203,7 +203,7 @@ function mergeConsecutiveSameRole(messages) {
203
203
  // turns it into a single marked text block (see `markCachePrefix`).
204
204
  //
205
205
  function extractSystem(messages) {
206
- const system = textFromParts(messages.filter((m) => m.role === "system").flatMap((m) => m.content));
206
+ const system = textFromParts(messages.flatMap((m) => (m.role === "system" ? m.content : [])));
207
207
  return system.length > 0 ? system : undefined;
208
208
  }
209
209
  // ── Cache marking ─────────────────────────────────────────────────
@@ -219,13 +219,11 @@ function markCachePrefix(system, messages, cacheControl) {
219
219
  const marked = [...messages];
220
220
  let remaining = 2;
221
221
  for (let index = marked.length - 1; index >= 0 && remaining > 0; index--) {
222
- if (marked[index].role !== "user")
222
+ const message = marked[index];
223
+ if (message.role !== "user")
223
224
  continue;
224
225
  remaining--;
225
- marked[index] = {
226
- ...marked[index],
227
- content: markLastBlock(marked[index].content, cacheControl),
228
- };
226
+ marked[index] = { ...message, content: markLastBlock(message.content, cacheControl) };
229
227
  }
230
228
  return {
231
229
  system: system && [{ type: "text", text: system, cache_control: cacheControl }],
@@ -22,10 +22,6 @@
22
22
  * not user-visible, arrives just before `content_block_stop`),
23
23
  * `input_json_delta` (partial JSON for tool input — must be concatenated
24
24
  * before parsing).
25
- * - StopReason mismatch: current Anthropic docs list
26
- * `model_context_window_exceeded`, but the SDK's
27
- * `StopReason` union is `'end_turn' | 'max_tokens' | 'stop_sequence' |
28
- * 'tool_use' | 'pause_turn' | 'refusal'` — that value is not present.
29
25
  */
30
26
  import type Anthropic from "@anthropic-ai/sdk";
31
27
  import type { ContentPart, FinishReason, GenerateResult, StreamEvent } from "../../domain/index.js";
@@ -54,11 +50,6 @@ interface ReasoningBlockEntry {
54
50
  export declare function createStreamAccumulator(model: string, provider: string, longCacheWrites: boolean): StreamAccumulator;
55
51
  /**
56
52
  * Map Anthropic stop reasons to the canonical FinishReason.
57
- *
58
- * The SDK's StopReason union is:
59
- * 'end_turn' | 'max_tokens' | 'stop_sequence' | 'tool_use' | 'pause_turn' | 'refusal'
60
- * Current Anthropic docs additionally list `model_context_window_exceeded`,
61
- * but that value is not in the SDK union; accept the wire string at this boundary.
62
53
  */
63
54
  export declare function mapStopReason(reason: string | null | undefined): FinishReason;
64
55
  export declare function mapUsage(usage: Anthropic.Messages.Usage | Anthropic.Messages.MessageDeltaUsage, longCacheWrites: boolean, fallback?: Usage): Usage;
@@ -19,11 +19,6 @@ export function createStreamAccumulator(model, provider, longCacheWrites) {
19
19
  // ── Finish reason mapping ─────────────────────────────────────────
20
20
  /**
21
21
  * Map Anthropic stop reasons to the canonical FinishReason.
22
- *
23
- * The SDK's StopReason union is:
24
- * 'end_turn' | 'max_tokens' | 'stop_sequence' | 'tool_use' | 'pause_turn' | 'refusal'
25
- * Current Anthropic docs additionally list `model_context_window_exceeded`,
26
- * but that value is not in the SDK union; accept the wire string at this boundary.
27
22
  */
28
23
  export function mapStopReason(reason) {
29
24
  // Anthropic stop reasons mostly line up with the canonical FinishReason. `refusal`
@@ -103,8 +103,7 @@ export function mapUsage(usage, longCacheWrites) {
103
103
  //
104
104
  // GPT-5.6+ also bills cache writes: `cache_write_tokens` is their total, split by
105
105
  // lifetime in `cache_write_tokens_details` (`ttl_30m_tokens` + `ttl_12h_tokens`), and
106
- // `cached_tokens` excludes them. The installed SDK types `cache_write_tokens` but not
107
- // the `cache_write_tokens_details` split, so that is read through a local shape.
106
+ // `cached_tokens` excludes them. Models before GPT-5.6 omit both at runtime, whatever the SDK's types say.
108
107
  const details = usage.input_tokens_details;
109
108
  const result = {
110
109
  inputTokens: usage.input_tokens,
@@ -49,10 +49,14 @@ export declare function providerClientOptions(config: ProviderConfig, defaultBas
49
49
  maxRetries: number;
50
50
  fetch: typeof fetch;
51
51
  };
52
- /** Anthropic client options that default from the environment, set to "none". */
52
+ /**
53
+ * Anthropic client options that default from the environment, set to "none".
54
+ * `openTelemetry: false` stops the `ANTHROPIC_OPEN_TELEMETRY*` variables and the SDK's spans and trace headers.
55
+ */
53
56
  export declare const ANTHROPIC_ENV_OPTOUT: {
54
57
  authToken: null;
55
58
  webhookKey: null;
59
+ openTelemetry: false;
56
60
  };
57
61
  /** OpenAI client options that default from the environment, set to "none". */
58
62
  export declare const OPENAI_ENV_OPTOUT: {
@@ -96,8 +96,15 @@ export function providerClientOptions(config, defaultBaseUrl) {
96
96
  fetch: providerFetch,
97
97
  };
98
98
  }
99
- /** Anthropic client options that default from the environment, set to "none". */
100
- export const ANTHROPIC_ENV_OPTOUT = { authToken: null, webhookKey: null };
99
+ /**
100
+ * Anthropic client options that default from the environment, set to "none".
101
+ * `openTelemetry: false` stops the `ANTHROPIC_OPEN_TELEMETRY*` variables and the SDK's spans and trace headers.
102
+ */
103
+ export const ANTHROPIC_ENV_OPTOUT = {
104
+ authToken: null,
105
+ webhookKey: null,
106
+ openTelemetry: false,
107
+ };
101
108
  /** OpenAI client options that default from the environment, set to "none". */
102
109
  export const OPENAI_ENV_OPTOUT = {
103
110
  adminAPIKey: null,
@@ -101,7 +101,7 @@ export type ProviderModel = Omit<ModelInfo, "wire" | "imageLinks">;
101
101
  * OpenAI-Chat `delta.content`.
102
102
  * - MediaPart: an image or PDF: Anthropic `image`/`document` block, OpenAI
103
103
  * `input_image`/`input_file` or Chat `image_url`/`file`, Gemini `fileData`/`inlineData`. Allowed in a user message and
104
- * inside a tool result.
104
+ * inside a tool result, never in an assistant message.
105
105
  * - ReasoningPart: Anthropic `thinking` / OpenAI `reasoning` items.
106
106
  * Provider metadata (signature, encrypted_content) lives in providerOptions.
107
107
  * - ToolUsePart: Anthropic `tool_use` / OpenAI `function_call` items.
@@ -171,10 +171,18 @@ export type ContentPart = TextPart | MediaPart | ReasoningPart | ToolUsePart | T
171
171
  * Chat/Responses message shapes. The `tool` role carries tool_result parts
172
172
  * back to the provider after execution.
173
173
  */
174
- export interface Message {
175
- role: "system" | "user" | "assistant" | "tool";
176
- content: ContentPart[];
177
- }
174
+ export type Message = {
175
+ role: "system" | "user";
176
+ content: Array<TextPart | MediaPart>;
177
+ } | {
178
+ role: "tool";
179
+ content: ToolResultPart[];
180
+ } | {
181
+ role: "assistant";
182
+ content: AssistantContentPart[];
183
+ };
184
+ /** What a model produces. No provider takes media in an assistant turn, so the type has none. */
185
+ export type AssistantContentPart = TextPart | ReasoningPart | ToolUsePart;
178
186
  /**
179
187
  * A tool the caller executes, with a JSON Schema input signature. The request carries these as
180
188
  * the tool definitions, and the adapter translates them into the provider's tool schema format.
@@ -321,9 +329,7 @@ export interface ToolCall {
321
329
  * Provider mapping:
322
330
  * - Anthropic StopReason → FinishReason: `end_turn`→`end_turn`, `tool_use`→`tool_use`,
323
331
  * `max_tokens`→`max_tokens`, `stop_sequence`→`stop_sequence`, `refusal`→`error`.
324
- * NOTE: Anthropic docs additionally list `model_context_window_exceeded` but the
325
- * `StopReason` union in `@anthropic-ai/sdk` 0.106 does not include it; if that value arrives at runtime
326
- * the adapter maps the wire string to the canonical `context_overflow` error.
332
+ * `model_context_window_exceeded` maps to the canonical `context_overflow` error.
327
333
  * - OpenAI Responses status: `completed`→`end_turn`, `failed`/`cancelled`→`error`,
328
334
  * `incomplete` maps to `max_tokens` or `error` depending on `incomplete_details.reason`.
329
335
  * - OpenAI-Chat finish_reason: `stop`→`end_turn`, `tool_calls`→`tool_use`,
@@ -6,11 +6,11 @@
6
6
  * These are simple constructors that produce well-formed Message/ContentPart
7
7
  * values without boilerplate. Hosts and tests use them to build requests.
8
8
  */
9
- import type { ContentPart, MediaPart, Message, ProviderOptions, TextPart } from "../domain/index.js";
9
+ import type { AssistantContentPart, MediaPart, Message, ProviderOptions, TextPart } from "../domain/index.js";
10
10
  export declare function user(text: string): Message;
11
11
  export declare function system(text: string): Message;
12
12
  export declare function text(t: string, opts?: {
13
13
  providerOptions?: ProviderOptions;
14
14
  }): TextPart;
15
- export declare function assistant(parts: ContentPart[]): Message;
15
+ export declare function assistant(parts: AssistantContentPart[]): Message;
16
16
  export declare function toolResult(toolCallId: string, content: Array<TextPart | MediaPart>, isError?: boolean): Message;
@@ -8,10 +8,10 @@ import { z } from "zod";
8
8
  export declare const mockModelDefaultResponseSchema: z.ZodObject<{
9
9
  text: z.ZodOptional<z.ZodString>;
10
10
  sample: z.ZodOptional<z.ZodEnum<{
11
- scene: "scene";
12
11
  critique: "critique";
13
- outline: "outline";
14
12
  markdown: "markdown";
13
+ outline: "outline";
14
+ scene: "scene";
15
15
  }>>;
16
16
  lorem: z.ZodOptional<z.ZodObject<{
17
17
  words: z.ZodNumber;
@@ -29,27 +29,12 @@ export type MockModelDefaultResponse = z.infer<typeof mockModelDefaultResponseSc
29
29
  * is sticky, so gateway retries and fallbacks fail deterministically.
30
30
  */
31
31
  export declare const mockModelStepSchema: z.ZodObject<{
32
- toolCalls: z.ZodOptional<z.ZodArray<z.ZodObject<{
33
- name: z.ZodString;
34
- id: z.ZodOptional<z.ZodString>;
35
- args: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
36
- rawArgs: z.ZodOptional<z.ZodString>;
37
- }, z.core.$strict>>>;
38
- finishReason: z.ZodOptional<z.ZodEnum<{
39
- length: "length";
40
- content_filter: "content_filter";
41
- }>>;
42
- error: z.ZodOptional<z.ZodObject<{
43
- status: z.ZodDefault<z.ZodNumber>;
44
- message: z.ZodDefault<z.ZodString>;
45
- }, z.core.$strict>>;
46
- times: z.ZodOptional<z.ZodNumber>;
47
32
  text: z.ZodOptional<z.ZodString>;
48
33
  sample: z.ZodOptional<z.ZodEnum<{
49
- scene: "scene";
50
34
  critique: "critique";
51
- outline: "outline";
52
35
  markdown: "markdown";
36
+ outline: "outline";
37
+ scene: "scene";
53
38
  }>>;
54
39
  lorem: z.ZodOptional<z.ZodObject<{
55
40
  words: z.ZodNumber;
@@ -60,32 +45,32 @@ export declare const mockModelStepSchema: z.ZodObject<{
60
45
  delayMs: z.ZodOptional<z.ZodNumber>;
61
46
  chunkSize: z.ZodOptional<z.ZodNumber>;
62
47
  chunkDelayMs: z.ZodOptional<z.ZodNumber>;
48
+ toolCalls: z.ZodOptional<z.ZodArray<z.ZodObject<{
49
+ name: z.ZodString;
50
+ id: z.ZodOptional<z.ZodString>;
51
+ args: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
52
+ rawArgs: z.ZodOptional<z.ZodString>;
53
+ }, z.core.$strict>>>;
54
+ finishReason: z.ZodOptional<z.ZodEnum<{
55
+ content_filter: "content_filter";
56
+ length: "length";
57
+ }>>;
58
+ error: z.ZodOptional<z.ZodObject<{
59
+ status: z.ZodDefault<z.ZodNumber>;
60
+ message: z.ZodDefault<z.ZodString>;
61
+ }, z.core.$strict>>;
62
+ times: z.ZodOptional<z.ZodNumber>;
63
63
  }, z.core.$strict>;
64
64
  export type MockModelStep = z.infer<typeof mockModelStepSchema>;
65
65
  export declare const mockModelScriptSchema: z.ZodObject<{
66
66
  match: z.ZodOptional<z.ZodString>;
67
67
  steps: z.ZodArray<z.ZodObject<{
68
- toolCalls: z.ZodOptional<z.ZodArray<z.ZodObject<{
69
- name: z.ZodString;
70
- id: z.ZodOptional<z.ZodString>;
71
- args: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
72
- rawArgs: z.ZodOptional<z.ZodString>;
73
- }, z.core.$strict>>>;
74
- finishReason: z.ZodOptional<z.ZodEnum<{
75
- length: "length";
76
- content_filter: "content_filter";
77
- }>>;
78
- error: z.ZodOptional<z.ZodObject<{
79
- status: z.ZodDefault<z.ZodNumber>;
80
- message: z.ZodDefault<z.ZodString>;
81
- }, z.core.$strict>>;
82
- times: z.ZodOptional<z.ZodNumber>;
83
68
  text: z.ZodOptional<z.ZodString>;
84
69
  sample: z.ZodOptional<z.ZodEnum<{
85
- scene: "scene";
86
70
  critique: "critique";
87
- outline: "outline";
88
71
  markdown: "markdown";
72
+ outline: "outline";
73
+ scene: "scene";
89
74
  }>>;
90
75
  lorem: z.ZodOptional<z.ZodObject<{
91
76
  words: z.ZodNumber;
@@ -96,6 +81,21 @@ export declare const mockModelScriptSchema: z.ZodObject<{
96
81
  delayMs: z.ZodOptional<z.ZodNumber>;
97
82
  chunkSize: z.ZodOptional<z.ZodNumber>;
98
83
  chunkDelayMs: z.ZodOptional<z.ZodNumber>;
84
+ toolCalls: z.ZodOptional<z.ZodArray<z.ZodObject<{
85
+ name: z.ZodString;
86
+ id: z.ZodOptional<z.ZodString>;
87
+ args: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
88
+ rawArgs: z.ZodOptional<z.ZodString>;
89
+ }, z.core.$strict>>>;
90
+ finishReason: z.ZodOptional<z.ZodEnum<{
91
+ content_filter: "content_filter";
92
+ length: "length";
93
+ }>>;
94
+ error: z.ZodOptional<z.ZodObject<{
95
+ status: z.ZodDefault<z.ZodNumber>;
96
+ message: z.ZodDefault<z.ZodString>;
97
+ }, z.core.$strict>>;
98
+ times: z.ZodOptional<z.ZodNumber>;
99
99
  }, z.core.$strict>>;
100
100
  }, z.core.$strict>;
101
101
  export type MockModelScript = z.infer<typeof mockModelScriptSchema>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@meridian-flow/runtime",
3
- "version": "0.1.2",
3
+ "version": "0.2.0",
4
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
5
  "keywords": [
6
6
  "llm",
@@ -24,7 +24,7 @@
24
24
  "type": "module",
25
25
  "sideEffects": false,
26
26
  "engines": {
27
- "node": ">=20"
27
+ "node": ">=22"
28
28
  },
29
29
  "files": [
30
30
  "dist"
@@ -54,17 +54,17 @@
54
54
  ]
55
55
  },
56
56
  "dependencies": {
57
- "@anthropic-ai/sdk": "^0.106.0",
57
+ "@anthropic-ai/sdk": "^1.8.0",
58
58
  "@google/genai": "^3.3.0",
59
- "jsonrepair": "^3.14.1",
60
- "openai": "^6.45.0",
61
- "zod": "^4.4.3"
59
+ "jsonrepair": "^3.15.0",
60
+ "openai": "^7.57.0",
61
+ "zod": "^4.6.7"
62
62
  },
63
63
  "devDependencies": {
64
- "@biomejs/biome": "^2.5.1",
65
- "@types/node": "^24",
66
- "typescript": "^6.0.3",
67
- "vitest": "^4.1.9"
64
+ "@biomejs/biome": "^2.6.7",
65
+ "@types/node": "^26",
66
+ "typescript": "^7.0.2",
67
+ "vitest": "^5.1.0"
68
68
  },
69
69
  "scripts": {
70
70
  "build": "rm -rf dist && tsc -p tsconfig.build.json",