@x12i/openrouter-runtime 1.0.6 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -1,13 +1,26 @@
1
1
  type ApiMode = "chat" | "responses" | "auto";
2
2
  type ResolvedApiMode = "chat" | "responses";
3
3
  type ToolMode = "disabled" | "allowed" | "required";
4
+ interface RuntimeCallOptions {
5
+ /** Merged onto the compiled JSON body after known fields are assembled. */
6
+ finalizeBody?: (body: Record<string, unknown>) => Record<string, unknown>;
7
+ /**
8
+ * Reads reasoning text from one chat delta.
9
+ * Returned text is emitted as `stream.reasoning.delta` and is not copied into answer text.
10
+ */
11
+ extractStreamReasoning?: (delta: unknown) => string | undefined;
12
+ /** When true, fusion/advisor/subagent tools do not inherit `request.reasoning`. */
13
+ suppressNestedReasoningFallback?: boolean;
14
+ }
4
15
  interface OpenRouterRuntime {
5
- run(request: RuntimeRequest): Promise<RuntimeResponse>;
16
+ compile(request: RuntimeRequest, call?: RuntimeCallOptions): CompiledOpenRouterRequest;
17
+ run(request: RuntimeRequest, call?: RuntimeCallOptions): Promise<RuntimeResponse>;
6
18
  /**
7
- * Streaming chat entrypoint. Separate from `run()` by design.
19
+ * Explicit streaming chat API. Separate from `run()` by design — never an argument, never the default.
20
+ * Always sends OpenRouter Chat Completions with `stream: true` and yields typed SSE events.
8
21
  * The old `stream()` name was removed so mistaken/old clients break loudly.
9
22
  */
10
- executeStreamingChat(request: RuntimeRequest): AsyncIterable<RuntimeStreamEvent>;
23
+ executeStreamingChat(request: RuntimeRequest, call?: RuntimeCallOptions): AsyncIterable<RuntimeStreamEvent>;
11
24
  }
12
25
  interface OpenRouterRuntimeOptions {
13
26
  apiKey?: string;
@@ -58,6 +71,16 @@ interface RuntimeRequest {
58
71
  functionTools?: RuntimeFunctionToolDefinition[];
59
72
  toolChoice?: RuntimeToolChoice;
60
73
  execution?: RuntimeExecutionPolicy;
74
+ /**
75
+ * Optional agent id for this call.
76
+ * OpenRouter sends it as the activity-log App name (`X-Title` and `X-OpenRouter-Title`).
77
+ * It is also stored in the provider metadata record as `agentId`.
78
+ */
79
+ agentId?: string;
80
+ /**
81
+ * Optional metadata. String, number, and boolean values are stored in the one provider
82
+ * metadata record. Objects and arrays stay on the normalized response.
83
+ */
61
84
  metadata?: Record<string, unknown>;
62
85
  rawOpenRouterOverrides?: Record<string, unknown>;
63
86
  }
@@ -178,6 +201,7 @@ interface FusionPolicy {
178
201
  maxToolCalls?: number;
179
202
  maxCompletionTokens?: number;
180
203
  reasoning?: RuntimeReasoningConfig;
204
+ reasoningEffort?: "minimal" | "low" | "medium" | "high" | "max";
181
205
  temperature?: number;
182
206
  }
183
207
  interface AdvisorPolicy {
@@ -191,6 +215,7 @@ interface AdvisorPolicy {
191
215
  maxToolCalls?: number;
192
216
  maxCompletionTokens?: number;
193
217
  reasoning?: RuntimeReasoningConfig;
218
+ reasoningEffort?: "minimal" | "low" | "medium" | "high" | "max";
194
219
  temperature?: number;
195
220
  }
196
221
  interface SubagentPolicy {
@@ -201,6 +226,7 @@ interface SubagentPolicy {
201
226
  maxToolCalls?: number;
202
227
  maxCompletionTokens?: number;
203
228
  reasoning?: RuntimeReasoningConfig;
229
+ reasoningEffort?: "minimal" | "low" | "medium" | "high" | "max";
204
230
  temperature?: number;
205
231
  }
206
232
  type NestedOpenRouterServerToolPolicy = {
@@ -364,15 +390,75 @@ interface RuntimePolicyViolation {
364
390
  details?: unknown;
365
391
  }
366
392
  interface RuntimeStreamEvent {
367
- type: string;
393
+ type: RuntimeStreamEventType;
394
+ requestId: string;
368
395
  data?: unknown;
369
396
  }
397
+ type RuntimeStreamEventType = "stream.start" | "stream.text.delta" | "stream.reasoning.delta" | "stream.tool_call.delta" | "stream.usage" | "stream.warning" | "stream.error" | "stream.done";
398
+ interface RuntimeStreamStartEvent extends RuntimeStreamEvent {
399
+ type: "stream.start";
400
+ data: {
401
+ model: string;
402
+ apiMode: "chat";
403
+ entrypoint: "executeStreamingChat";
404
+ };
405
+ }
406
+ interface RuntimeStreamTextDeltaEvent extends RuntimeStreamEvent {
407
+ type: "stream.text.delta";
408
+ data: {
409
+ text: string;
410
+ };
411
+ }
412
+ interface RuntimeStreamReasoningDeltaEvent extends RuntimeStreamEvent {
413
+ type: "stream.reasoning.delta";
414
+ data: {
415
+ text: string;
416
+ };
417
+ }
418
+ interface RuntimeStreamToolCallDeltaEvent extends RuntimeStreamEvent {
419
+ type: "stream.tool_call.delta";
420
+ data: {
421
+ index: number;
422
+ id?: string;
423
+ name?: string;
424
+ argumentsDelta?: string;
425
+ };
426
+ }
427
+ interface RuntimeStreamUsageEvent extends RuntimeStreamEvent {
428
+ type: "stream.usage";
429
+ data: {
430
+ usage: RuntimeUsage;
431
+ };
432
+ }
433
+ interface RuntimeStreamWarningEvent extends RuntimeStreamEvent {
434
+ type: "stream.warning";
435
+ data: {
436
+ warning: RuntimeWarning;
437
+ };
438
+ }
439
+ interface RuntimeStreamErrorEvent extends RuntimeStreamEvent {
440
+ type: "stream.error";
441
+ data: {
442
+ error: RuntimeError;
443
+ };
444
+ }
445
+ interface RuntimeStreamDoneEvent extends RuntimeStreamEvent {
446
+ type: "stream.done";
447
+ data: {
448
+ text: string;
449
+ finishReason?: string;
450
+ model: string;
451
+ };
452
+ }
370
453
  type RuntimePatchApplier = (patch: RuntimePatchProposal) => Promise<unknown> | unknown;
371
454
 
372
455
  declare function createOpenRouterRuntime(options: OpenRouterRuntimeOptions): OpenRouterRuntime;
373
456
 
374
457
  interface RuntimeCompileContext {
375
458
  streaming: boolean;
459
+ finalizeBody?: (body: Record<string, unknown>) => Record<string, unknown>;
460
+ /** When set, nested tools do not inherit the parent request reasoning object. */
461
+ suppressNestedReasoningFallback?: boolean;
376
462
  }
377
463
  interface OpenRouterTool {
378
464
  type: string;
@@ -409,9 +495,30 @@ declare class OpenRouterHttpError extends Error {
409
495
  constructor(status: number, code: ProviderFailureCode, message: string, body?: unknown | undefined, retryable?: boolean, providerAttempts?: ProviderAttemptError[]);
410
496
  }
411
497
 
412
- declare function buildOpenRouterServerTools(policy?: RuntimeServerToolsPolicy): OpenRouterTool[];
498
+ declare function buildOpenRouterServerTools(policy?: RuntimeServerToolsPolicy, fallbackReasoning?: RuntimeReasoningConfig): OpenRouterTool[];
413
499
 
414
500
  declare function isRuntimeLoggingEnabled(env?: Record<string, string | undefined> | undefined): boolean;
415
501
  declare function createConsoleLogger(): RuntimeLogger;
416
502
 
417
- export { type AdvisorPolicy, type ApplyPatchPolicy, type CompiledOpenRouterRequest, type DatetimePolicy, type FusionPolicy, type ImageGenerationPolicy, OpenRouterHttpError, type OpenRouterRuntime, type OpenRouterRuntimeOptions, type RuntimeCitation, RuntimeConfigError, type RuntimeDefaults, type RuntimeError, type RuntimeFunctionToolDefinition, type RuntimeGeneratedImage, type RuntimeLogger, type RuntimeMessage, type RuntimePatchProposal, type RuntimePolicyViolation, type RuntimeRequest, type RuntimeResponse, type RuntimeServerToolsPolicy, type RuntimeStreamEvent, type RuntimeToolExecutor, type RuntimeToolUsage, type RuntimeUsage, type RuntimeWarning, type ServerToolKey, type ServerToolUsage, type SubagentPolicy, type WebFetchPolicy, type WebSearchPolicy, buildOpenRouterServerTools, compileRuntimeRequest, createConsoleLogger, createOpenRouterRuntime, isRuntimeLoggingEnabled };
503
+ interface ProviderMetadataLimits {
504
+ maxPairs: number;
505
+ maxKeyLength: number;
506
+ maxValueLength: number;
507
+ keyPattern?: RegExp;
508
+ valuePattern?: RegExp;
509
+ }
510
+ /** OpenRouter and OpenAI metadata: 16 pairs, 64-character keys, 512-character values. */
511
+ declare const OPENROUTER_METADATA_LIMITS: ProviderMetadataLimits;
512
+ /**
513
+ * One provider metadata record.
514
+ * Callers pass `metadata` and `agentId`. Every provider stores the same `m.0`, `m.1`, ...
515
+ * pairs: base64 of the UTF-8 JSON string map, 256 characters per pair, at most 16 pairs.
516
+ * `decodeProviderMetadata` restores that map from any provider log.
517
+ * `limits` stays on the signature for existing callers. The record uses the strictest
518
+ * provider limits, so the pairs do not change between providers.
519
+ */
520
+ declare function toProviderMetadata(metadata: Record<string, unknown> | undefined, agentId: string | undefined, _limits: ProviderMetadataLimits, base: Record<string, unknown> | undefined): Record<string, string> | undefined;
521
+ /** Restores the string map from the `m.0`, `m.1`, ... pairs stored on a provider log. */
522
+ declare function decodeProviderMetadata(wire: unknown): Record<string, string>;
523
+
524
+ export { type AdvisorPolicy, type ApplyPatchPolicy, type CompiledOpenRouterRequest, type DatetimePolicy, type FusionPolicy, type ImageGenerationPolicy, OPENROUTER_METADATA_LIMITS, OpenRouterHttpError, type OpenRouterRuntime, type OpenRouterRuntimeOptions, type ProviderMetadataLimits, type RuntimeCallOptions, type RuntimeCitation, RuntimeConfigError, type RuntimeDefaults, type RuntimeError, type RuntimeFunctionToolDefinition, type RuntimeGeneratedImage, type RuntimeLogger, type RuntimeMessage, type RuntimePatchProposal, type RuntimePolicyViolation, type RuntimeRequest, type RuntimeResponse, type RuntimeServerToolsPolicy, type RuntimeStreamDoneEvent, type RuntimeStreamErrorEvent, type RuntimeStreamEvent, type RuntimeStreamEventType, type RuntimeStreamReasoningDeltaEvent, type RuntimeStreamStartEvent, type RuntimeStreamTextDeltaEvent, type RuntimeStreamToolCallDeltaEvent, type RuntimeStreamUsageEvent, type RuntimeStreamWarningEvent, type RuntimeToolExecutor, type RuntimeToolUsage, type RuntimeUsage, type RuntimeWarning, type ServerToolKey, type ServerToolUsage, type SubagentPolicy, type WebFetchPolicy, type WebSearchPolicy, buildOpenRouterServerTools, compileRuntimeRequest, createConsoleLogger, createOpenRouterRuntime, decodeProviderMetadata, isRuntimeLoggingEnabled, toProviderMetadata };
package/dist/index.d.ts CHANGED
@@ -1,13 +1,26 @@
1
1
  type ApiMode = "chat" | "responses" | "auto";
2
2
  type ResolvedApiMode = "chat" | "responses";
3
3
  type ToolMode = "disabled" | "allowed" | "required";
4
+ interface RuntimeCallOptions {
5
+ /** Merged onto the compiled JSON body after known fields are assembled. */
6
+ finalizeBody?: (body: Record<string, unknown>) => Record<string, unknown>;
7
+ /**
8
+ * Reads reasoning text from one chat delta.
9
+ * Returned text is emitted as `stream.reasoning.delta` and is not copied into answer text.
10
+ */
11
+ extractStreamReasoning?: (delta: unknown) => string | undefined;
12
+ /** When true, fusion/advisor/subagent tools do not inherit `request.reasoning`. */
13
+ suppressNestedReasoningFallback?: boolean;
14
+ }
4
15
  interface OpenRouterRuntime {
5
- run(request: RuntimeRequest): Promise<RuntimeResponse>;
16
+ compile(request: RuntimeRequest, call?: RuntimeCallOptions): CompiledOpenRouterRequest;
17
+ run(request: RuntimeRequest, call?: RuntimeCallOptions): Promise<RuntimeResponse>;
6
18
  /**
7
- * Streaming chat entrypoint. Separate from `run()` by design.
19
+ * Explicit streaming chat API. Separate from `run()` by design — never an argument, never the default.
20
+ * Always sends OpenRouter Chat Completions with `stream: true` and yields typed SSE events.
8
21
  * The old `stream()` name was removed so mistaken/old clients break loudly.
9
22
  */
10
- executeStreamingChat(request: RuntimeRequest): AsyncIterable<RuntimeStreamEvent>;
23
+ executeStreamingChat(request: RuntimeRequest, call?: RuntimeCallOptions): AsyncIterable<RuntimeStreamEvent>;
11
24
  }
12
25
  interface OpenRouterRuntimeOptions {
13
26
  apiKey?: string;
@@ -58,6 +71,16 @@ interface RuntimeRequest {
58
71
  functionTools?: RuntimeFunctionToolDefinition[];
59
72
  toolChoice?: RuntimeToolChoice;
60
73
  execution?: RuntimeExecutionPolicy;
74
+ /**
75
+ * Optional agent id for this call.
76
+ * OpenRouter sends it as the activity-log App name (`X-Title` and `X-OpenRouter-Title`).
77
+ * It is also stored in the provider metadata record as `agentId`.
78
+ */
79
+ agentId?: string;
80
+ /**
81
+ * Optional metadata. String, number, and boolean values are stored in the one provider
82
+ * metadata record. Objects and arrays stay on the normalized response.
83
+ */
61
84
  metadata?: Record<string, unknown>;
62
85
  rawOpenRouterOverrides?: Record<string, unknown>;
63
86
  }
@@ -178,6 +201,7 @@ interface FusionPolicy {
178
201
  maxToolCalls?: number;
179
202
  maxCompletionTokens?: number;
180
203
  reasoning?: RuntimeReasoningConfig;
204
+ reasoningEffort?: "minimal" | "low" | "medium" | "high" | "max";
181
205
  temperature?: number;
182
206
  }
183
207
  interface AdvisorPolicy {
@@ -191,6 +215,7 @@ interface AdvisorPolicy {
191
215
  maxToolCalls?: number;
192
216
  maxCompletionTokens?: number;
193
217
  reasoning?: RuntimeReasoningConfig;
218
+ reasoningEffort?: "minimal" | "low" | "medium" | "high" | "max";
194
219
  temperature?: number;
195
220
  }
196
221
  interface SubagentPolicy {
@@ -201,6 +226,7 @@ interface SubagentPolicy {
201
226
  maxToolCalls?: number;
202
227
  maxCompletionTokens?: number;
203
228
  reasoning?: RuntimeReasoningConfig;
229
+ reasoningEffort?: "minimal" | "low" | "medium" | "high" | "max";
204
230
  temperature?: number;
205
231
  }
206
232
  type NestedOpenRouterServerToolPolicy = {
@@ -364,15 +390,75 @@ interface RuntimePolicyViolation {
364
390
  details?: unknown;
365
391
  }
366
392
  interface RuntimeStreamEvent {
367
- type: string;
393
+ type: RuntimeStreamEventType;
394
+ requestId: string;
368
395
  data?: unknown;
369
396
  }
397
+ type RuntimeStreamEventType = "stream.start" | "stream.text.delta" | "stream.reasoning.delta" | "stream.tool_call.delta" | "stream.usage" | "stream.warning" | "stream.error" | "stream.done";
398
+ interface RuntimeStreamStartEvent extends RuntimeStreamEvent {
399
+ type: "stream.start";
400
+ data: {
401
+ model: string;
402
+ apiMode: "chat";
403
+ entrypoint: "executeStreamingChat";
404
+ };
405
+ }
406
+ interface RuntimeStreamTextDeltaEvent extends RuntimeStreamEvent {
407
+ type: "stream.text.delta";
408
+ data: {
409
+ text: string;
410
+ };
411
+ }
412
+ interface RuntimeStreamReasoningDeltaEvent extends RuntimeStreamEvent {
413
+ type: "stream.reasoning.delta";
414
+ data: {
415
+ text: string;
416
+ };
417
+ }
418
+ interface RuntimeStreamToolCallDeltaEvent extends RuntimeStreamEvent {
419
+ type: "stream.tool_call.delta";
420
+ data: {
421
+ index: number;
422
+ id?: string;
423
+ name?: string;
424
+ argumentsDelta?: string;
425
+ };
426
+ }
427
+ interface RuntimeStreamUsageEvent extends RuntimeStreamEvent {
428
+ type: "stream.usage";
429
+ data: {
430
+ usage: RuntimeUsage;
431
+ };
432
+ }
433
+ interface RuntimeStreamWarningEvent extends RuntimeStreamEvent {
434
+ type: "stream.warning";
435
+ data: {
436
+ warning: RuntimeWarning;
437
+ };
438
+ }
439
+ interface RuntimeStreamErrorEvent extends RuntimeStreamEvent {
440
+ type: "stream.error";
441
+ data: {
442
+ error: RuntimeError;
443
+ };
444
+ }
445
+ interface RuntimeStreamDoneEvent extends RuntimeStreamEvent {
446
+ type: "stream.done";
447
+ data: {
448
+ text: string;
449
+ finishReason?: string;
450
+ model: string;
451
+ };
452
+ }
370
453
  type RuntimePatchApplier = (patch: RuntimePatchProposal) => Promise<unknown> | unknown;
371
454
 
372
455
  declare function createOpenRouterRuntime(options: OpenRouterRuntimeOptions): OpenRouterRuntime;
373
456
 
374
457
  interface RuntimeCompileContext {
375
458
  streaming: boolean;
459
+ finalizeBody?: (body: Record<string, unknown>) => Record<string, unknown>;
460
+ /** When set, nested tools do not inherit the parent request reasoning object. */
461
+ suppressNestedReasoningFallback?: boolean;
376
462
  }
377
463
  interface OpenRouterTool {
378
464
  type: string;
@@ -409,9 +495,30 @@ declare class OpenRouterHttpError extends Error {
409
495
  constructor(status: number, code: ProviderFailureCode, message: string, body?: unknown | undefined, retryable?: boolean, providerAttempts?: ProviderAttemptError[]);
410
496
  }
411
497
 
412
- declare function buildOpenRouterServerTools(policy?: RuntimeServerToolsPolicy): OpenRouterTool[];
498
+ declare function buildOpenRouterServerTools(policy?: RuntimeServerToolsPolicy, fallbackReasoning?: RuntimeReasoningConfig): OpenRouterTool[];
413
499
 
414
500
  declare function isRuntimeLoggingEnabled(env?: Record<string, string | undefined> | undefined): boolean;
415
501
  declare function createConsoleLogger(): RuntimeLogger;
416
502
 
417
- export { type AdvisorPolicy, type ApplyPatchPolicy, type CompiledOpenRouterRequest, type DatetimePolicy, type FusionPolicy, type ImageGenerationPolicy, OpenRouterHttpError, type OpenRouterRuntime, type OpenRouterRuntimeOptions, type RuntimeCitation, RuntimeConfigError, type RuntimeDefaults, type RuntimeError, type RuntimeFunctionToolDefinition, type RuntimeGeneratedImage, type RuntimeLogger, type RuntimeMessage, type RuntimePatchProposal, type RuntimePolicyViolation, type RuntimeRequest, type RuntimeResponse, type RuntimeServerToolsPolicy, type RuntimeStreamEvent, type RuntimeToolExecutor, type RuntimeToolUsage, type RuntimeUsage, type RuntimeWarning, type ServerToolKey, type ServerToolUsage, type SubagentPolicy, type WebFetchPolicy, type WebSearchPolicy, buildOpenRouterServerTools, compileRuntimeRequest, createConsoleLogger, createOpenRouterRuntime, isRuntimeLoggingEnabled };
503
+ interface ProviderMetadataLimits {
504
+ maxPairs: number;
505
+ maxKeyLength: number;
506
+ maxValueLength: number;
507
+ keyPattern?: RegExp;
508
+ valuePattern?: RegExp;
509
+ }
510
+ /** OpenRouter and OpenAI metadata: 16 pairs, 64-character keys, 512-character values. */
511
+ declare const OPENROUTER_METADATA_LIMITS: ProviderMetadataLimits;
512
+ /**
513
+ * One provider metadata record.
514
+ * Callers pass `metadata` and `agentId`. Every provider stores the same `m.0`, `m.1`, ...
515
+ * pairs: base64 of the UTF-8 JSON string map, 256 characters per pair, at most 16 pairs.
516
+ * `decodeProviderMetadata` restores that map from any provider log.
517
+ * `limits` stays on the signature for existing callers. The record uses the strictest
518
+ * provider limits, so the pairs do not change between providers.
519
+ */
520
+ declare function toProviderMetadata(metadata: Record<string, unknown> | undefined, agentId: string | undefined, _limits: ProviderMetadataLimits, base: Record<string, unknown> | undefined): Record<string, string> | undefined;
521
+ /** Restores the string map from the `m.0`, `m.1`, ... pairs stored on a provider log. */
522
+ declare function decodeProviderMetadata(wire: unknown): Record<string, string>;
523
+
524
+ export { type AdvisorPolicy, type ApplyPatchPolicy, type CompiledOpenRouterRequest, type DatetimePolicy, type FusionPolicy, type ImageGenerationPolicy, OPENROUTER_METADATA_LIMITS, OpenRouterHttpError, type OpenRouterRuntime, type OpenRouterRuntimeOptions, type ProviderMetadataLimits, type RuntimeCallOptions, type RuntimeCitation, RuntimeConfigError, type RuntimeDefaults, type RuntimeError, type RuntimeFunctionToolDefinition, type RuntimeGeneratedImage, type RuntimeLogger, type RuntimeMessage, type RuntimePatchProposal, type RuntimePolicyViolation, type RuntimeRequest, type RuntimeResponse, type RuntimeServerToolsPolicy, type RuntimeStreamDoneEvent, type RuntimeStreamErrorEvent, type RuntimeStreamEvent, type RuntimeStreamEventType, type RuntimeStreamReasoningDeltaEvent, type RuntimeStreamStartEvent, type RuntimeStreamTextDeltaEvent, type RuntimeStreamToolCallDeltaEvent, type RuntimeStreamUsageEvent, type RuntimeStreamWarningEvent, type RuntimeToolExecutor, type RuntimeToolUsage, type RuntimeUsage, type RuntimeWarning, type ServerToolKey, type ServerToolUsage, type SubagentPolicy, type WebFetchPolicy, type WebSearchPolicy, buildOpenRouterServerTools, compileRuntimeRequest, createConsoleLogger, createOpenRouterRuntime, decodeProviderMetadata, isRuntimeLoggingEnabled, toProviderMetadata };