@x12i/openrouter-runtime 1.0.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/LICENSE +21 -0
- package/README.md +63 -0
- package/dist/index.cjs +1243 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +396 -0
- package/dist/index.d.ts +396 -0
- package/dist/index.js +1212 -0
- package/dist/index.js.map +1 -0
- package/package.json +41 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
type ApiMode = "chat" | "responses" | "auto";
|
|
2
|
+
type ResolvedApiMode = "chat" | "responses";
|
|
3
|
+
type ToolMode = "disabled" | "allowed" | "required";
|
|
4
|
+
interface OpenRouterRuntime {
|
|
5
|
+
run(request: RuntimeRequest): Promise<RuntimeResponse>;
|
|
6
|
+
stream?(request: RuntimeRequest): AsyncIterable<RuntimeStreamEvent>;
|
|
7
|
+
}
|
|
8
|
+
interface OpenRouterRuntimeOptions {
|
|
9
|
+
apiKey: string;
|
|
10
|
+
baseUrl?: string;
|
|
11
|
+
defaultModel?: string;
|
|
12
|
+
defaultHeaders?: Record<string, string>;
|
|
13
|
+
appAttribution?: {
|
|
14
|
+
siteUrl?: string;
|
|
15
|
+
appName?: string;
|
|
16
|
+
};
|
|
17
|
+
defaults?: RuntimeDefaults;
|
|
18
|
+
tools?: RuntimeToolRegistry;
|
|
19
|
+
fetch?: typeof globalThis.fetch;
|
|
20
|
+
logger?: RuntimeLogger;
|
|
21
|
+
hooks?: RuntimeHooks;
|
|
22
|
+
patchApplier?: RuntimePatchApplier;
|
|
23
|
+
}
|
|
24
|
+
interface RuntimeDefaults {
|
|
25
|
+
apiMode?: ApiMode;
|
|
26
|
+
timeoutMs?: number;
|
|
27
|
+
maxRetries?: number;
|
|
28
|
+
retry?: {
|
|
29
|
+
enabled?: boolean;
|
|
30
|
+
maxAttempts?: number;
|
|
31
|
+
baseDelayMs?: number;
|
|
32
|
+
maxDelayMs?: number;
|
|
33
|
+
};
|
|
34
|
+
serverTools?: RuntimeServerToolsPolicy;
|
|
35
|
+
temperature?: number;
|
|
36
|
+
maxTokens?: number;
|
|
37
|
+
requireCitationsWhenSearchUsed?: boolean;
|
|
38
|
+
onPolicyViolation?: "throw" | "return_error";
|
|
39
|
+
}
|
|
40
|
+
interface RuntimeRequest {
|
|
41
|
+
id?: string;
|
|
42
|
+
model?: string;
|
|
43
|
+
apiMode?: ApiMode;
|
|
44
|
+
messages?: RuntimeMessage[];
|
|
45
|
+
input?: string | RuntimeInputItem[];
|
|
46
|
+
instructions?: string;
|
|
47
|
+
system?: string;
|
|
48
|
+
prompt?: string;
|
|
49
|
+
temperature?: number;
|
|
50
|
+
maxTokens?: number;
|
|
51
|
+
reasoning?: RuntimeReasoningConfig;
|
|
52
|
+
responseFormat?: RuntimeResponseFormat;
|
|
53
|
+
serverTools?: RuntimeServerToolsPolicy;
|
|
54
|
+
functionTools?: RuntimeFunctionToolDefinition[];
|
|
55
|
+
toolChoice?: RuntimeToolChoice;
|
|
56
|
+
execution?: RuntimeExecutionPolicy;
|
|
57
|
+
metadata?: Record<string, unknown>;
|
|
58
|
+
rawOpenRouterOverrides?: Record<string, unknown>;
|
|
59
|
+
}
|
|
60
|
+
type RuntimeMessageRole = "system" | "user" | "assistant" | "tool";
|
|
61
|
+
interface RuntimeMessage {
|
|
62
|
+
role: RuntimeMessageRole;
|
|
63
|
+
content: string | RuntimeContentPart[];
|
|
64
|
+
name?: string;
|
|
65
|
+
toolCallId?: string;
|
|
66
|
+
}
|
|
67
|
+
type RuntimeContentPart = RuntimeTextPart | RuntimeImagePart | RuntimeFilePart;
|
|
68
|
+
interface RuntimeTextPart {
|
|
69
|
+
type: "text";
|
|
70
|
+
text: string;
|
|
71
|
+
}
|
|
72
|
+
interface RuntimeImagePart {
|
|
73
|
+
type: "image_url";
|
|
74
|
+
imageUrl: string;
|
|
75
|
+
}
|
|
76
|
+
interface RuntimeFilePart {
|
|
77
|
+
type: "file";
|
|
78
|
+
fileName?: string;
|
|
79
|
+
mimeType?: string;
|
|
80
|
+
data?: string;
|
|
81
|
+
url?: string;
|
|
82
|
+
}
|
|
83
|
+
type RuntimeInputItem = Record<string, unknown> | string;
|
|
84
|
+
type RuntimeReasoningConfig = Record<string, unknown>;
|
|
85
|
+
type RuntimeResponseFormat = Record<string, unknown>;
|
|
86
|
+
interface RuntimeResponse {
|
|
87
|
+
id: string;
|
|
88
|
+
status: "completed" | "failed" | "requires_action" | "policy_violation";
|
|
89
|
+
apiMode: ResolvedApiMode;
|
|
90
|
+
model: string;
|
|
91
|
+
text: string;
|
|
92
|
+
messages?: RuntimeMessage[];
|
|
93
|
+
citations: RuntimeCitation[];
|
|
94
|
+
images: RuntimeGeneratedImage[];
|
|
95
|
+
patches: RuntimePatchProposal[];
|
|
96
|
+
toolUsage: RuntimeToolUsage;
|
|
97
|
+
usage?: RuntimeUsage;
|
|
98
|
+
finishReason?: string;
|
|
99
|
+
warnings: RuntimeWarning[];
|
|
100
|
+
errors: RuntimeError[];
|
|
101
|
+
raw: {
|
|
102
|
+
request: unknown;
|
|
103
|
+
response: unknown;
|
|
104
|
+
};
|
|
105
|
+
metadata?: Record<string, unknown>;
|
|
106
|
+
}
|
|
107
|
+
interface RuntimeServerToolsPolicy {
|
|
108
|
+
webSearch?: WebSearchPolicy;
|
|
109
|
+
webFetch?: WebFetchPolicy;
|
|
110
|
+
datetime?: DatetimePolicy;
|
|
111
|
+
imageGeneration?: ImageGenerationPolicy;
|
|
112
|
+
applyPatch?: ApplyPatchPolicy;
|
|
113
|
+
fusion?: FusionPolicy;
|
|
114
|
+
advisor?: AdvisorPolicy | AdvisorPolicy[];
|
|
115
|
+
subagent?: SubagentPolicy;
|
|
116
|
+
}
|
|
117
|
+
type WebSearchEngine = "auto" | "native" | "exa" | "firecrawl" | "parallel" | "perplexity";
|
|
118
|
+
type SearchContextSize = "low" | "medium" | "high";
|
|
119
|
+
interface WebSearchPolicy {
|
|
120
|
+
mode: ToolMode;
|
|
121
|
+
engine?: WebSearchEngine;
|
|
122
|
+
maxResults?: number;
|
|
123
|
+
maxTotalResults?: number;
|
|
124
|
+
searchContextSize?: SearchContextSize;
|
|
125
|
+
allowedDomains?: string[];
|
|
126
|
+
excludedDomains?: string[];
|
|
127
|
+
userLocation?: {
|
|
128
|
+
type: "approximate";
|
|
129
|
+
city?: string;
|
|
130
|
+
region?: string;
|
|
131
|
+
country?: string;
|
|
132
|
+
timezone?: string;
|
|
133
|
+
};
|
|
134
|
+
requireCitations?: boolean;
|
|
135
|
+
onFailure?: "fail" | "warn" | "continue";
|
|
136
|
+
}
|
|
137
|
+
type WebFetchEngine = "auto" | "native" | "exa" | "openrouter" | "firecrawl" | "parallel";
|
|
138
|
+
interface WebFetchPolicy {
|
|
139
|
+
mode: ToolMode;
|
|
140
|
+
engine?: WebFetchEngine;
|
|
141
|
+
maxUses?: number;
|
|
142
|
+
maxContentTokens?: number;
|
|
143
|
+
allowedDomains?: string[];
|
|
144
|
+
blockedDomains?: string[];
|
|
145
|
+
onFailure?: "fail" | "warn" | "continue";
|
|
146
|
+
}
|
|
147
|
+
interface DatetimePolicy {
|
|
148
|
+
mode: ToolMode;
|
|
149
|
+
timezone?: string;
|
|
150
|
+
}
|
|
151
|
+
interface ImageGenerationPolicy {
|
|
152
|
+
mode: ToolMode;
|
|
153
|
+
model?: string;
|
|
154
|
+
quality?: "low" | "medium" | "high" | string;
|
|
155
|
+
size?: string;
|
|
156
|
+
aspectRatio?: string;
|
|
157
|
+
background?: "transparent" | "opaque" | string;
|
|
158
|
+
outputFormat?: "png" | "jpeg" | "webp" | string;
|
|
159
|
+
outputCompression?: number;
|
|
160
|
+
moderation?: "auto" | "low" | string;
|
|
161
|
+
}
|
|
162
|
+
interface ApplyPatchPolicy {
|
|
163
|
+
mode: ToolMode;
|
|
164
|
+
behavior?: "return_only" | "apply_with_callback";
|
|
165
|
+
workspaceRoot?: string;
|
|
166
|
+
allowDelete?: boolean;
|
|
167
|
+
allowCreate?: boolean;
|
|
168
|
+
allowUpdate?: boolean;
|
|
169
|
+
}
|
|
170
|
+
interface FusionPolicy {
|
|
171
|
+
mode: ToolMode;
|
|
172
|
+
analysisModels?: string[];
|
|
173
|
+
judgeModel?: string;
|
|
174
|
+
maxToolCalls?: number;
|
|
175
|
+
maxCompletionTokens?: number;
|
|
176
|
+
reasoning?: RuntimeReasoningConfig;
|
|
177
|
+
temperature?: number;
|
|
178
|
+
}
|
|
179
|
+
interface AdvisorPolicy {
|
|
180
|
+
mode: ToolMode;
|
|
181
|
+
name?: string;
|
|
182
|
+
model?: string;
|
|
183
|
+
instructions?: string;
|
|
184
|
+
tools?: NestedOpenRouterServerToolPolicy[];
|
|
185
|
+
forwardTranscript?: boolean;
|
|
186
|
+
stream?: boolean;
|
|
187
|
+
maxToolCalls?: number;
|
|
188
|
+
maxCompletionTokens?: number;
|
|
189
|
+
reasoning?: RuntimeReasoningConfig;
|
|
190
|
+
temperature?: number;
|
|
191
|
+
}
|
|
192
|
+
interface SubagentPolicy {
|
|
193
|
+
mode: ToolMode;
|
|
194
|
+
model?: string;
|
|
195
|
+
instructions?: string;
|
|
196
|
+
tools?: NestedOpenRouterServerToolPolicy[];
|
|
197
|
+
maxToolCalls?: number;
|
|
198
|
+
maxCompletionTokens?: number;
|
|
199
|
+
reasoning?: RuntimeReasoningConfig;
|
|
200
|
+
temperature?: number;
|
|
201
|
+
}
|
|
202
|
+
type NestedOpenRouterServerToolPolicy = {
|
|
203
|
+
type: "openrouter:web_search";
|
|
204
|
+
parameters?: Record<string, unknown>;
|
|
205
|
+
} | {
|
|
206
|
+
type: "openrouter:web_fetch";
|
|
207
|
+
parameters?: Record<string, unknown>;
|
|
208
|
+
} | {
|
|
209
|
+
type: "openrouter:datetime";
|
|
210
|
+
parameters?: Record<string, unknown>;
|
|
211
|
+
} | {
|
|
212
|
+
type: "openrouter:image_generation";
|
|
213
|
+
parameters?: Record<string, unknown>;
|
|
214
|
+
};
|
|
215
|
+
interface RuntimeFunctionToolDefinition {
|
|
216
|
+
name: string;
|
|
217
|
+
description: string;
|
|
218
|
+
parameters: Record<string, unknown>;
|
|
219
|
+
executor?: RuntimeToolExecutor;
|
|
220
|
+
}
|
|
221
|
+
type RuntimeToolExecutor = (args: unknown, context: RuntimeToolExecutionContext) => Promise<unknown> | unknown;
|
|
222
|
+
interface RuntimeToolExecutionContext {
|
|
223
|
+
requestId: string;
|
|
224
|
+
toolName: string;
|
|
225
|
+
callId: string;
|
|
226
|
+
metadata?: Record<string, unknown>;
|
|
227
|
+
}
|
|
228
|
+
interface RuntimeToolRegistry {
|
|
229
|
+
[toolName: string]: RuntimeToolExecutor;
|
|
230
|
+
}
|
|
231
|
+
interface RuntimeExecutionPolicy {
|
|
232
|
+
maxToolIterations?: number;
|
|
233
|
+
maxServerToolCalls?: number;
|
|
234
|
+
maxFunctionToolCalls?: number;
|
|
235
|
+
timeoutMs?: number;
|
|
236
|
+
}
|
|
237
|
+
type RuntimeToolChoice = "auto" | "none" | "required" | {
|
|
238
|
+
type: "function";
|
|
239
|
+
functionName: string;
|
|
240
|
+
} | {
|
|
241
|
+
type: "server_tool";
|
|
242
|
+
serverTool: "web_search" | "web_fetch" | "datetime" | "image_generation" | "apply_patch" | "fusion" | "advisor" | "subagent";
|
|
243
|
+
};
|
|
244
|
+
interface RuntimeCitation {
|
|
245
|
+
type: "url";
|
|
246
|
+
url: string;
|
|
247
|
+
title?: string;
|
|
248
|
+
excerpt?: string;
|
|
249
|
+
startIndex?: number;
|
|
250
|
+
endIndex?: number;
|
|
251
|
+
sourceProvider: "openrouter";
|
|
252
|
+
raw?: unknown;
|
|
253
|
+
}
|
|
254
|
+
interface RuntimeGeneratedImage {
|
|
255
|
+
imageUrl: string;
|
|
256
|
+
status: "ok" | "error";
|
|
257
|
+
error?: string;
|
|
258
|
+
raw?: unknown;
|
|
259
|
+
}
|
|
260
|
+
interface RuntimeUsage {
|
|
261
|
+
inputTokens?: number;
|
|
262
|
+
outputTokens?: number;
|
|
263
|
+
totalTokens?: number;
|
|
264
|
+
reasoningTokens?: number;
|
|
265
|
+
costUsd?: number;
|
|
266
|
+
raw?: unknown;
|
|
267
|
+
}
|
|
268
|
+
interface RuntimeToolUsage {
|
|
269
|
+
serverTools: Record<ServerToolKey, ServerToolUsage>;
|
|
270
|
+
functionTools: RuntimeFunctionToolUsage[];
|
|
271
|
+
}
|
|
272
|
+
type ServerToolKey = "webSearch" | "webFetch" | "datetime" | "imageGeneration" | "applyPatch" | "fusion" | "advisor" | "subagent";
|
|
273
|
+
interface ServerToolUsage {
|
|
274
|
+
requested: boolean;
|
|
275
|
+
required: boolean;
|
|
276
|
+
used: boolean;
|
|
277
|
+
callCount?: number;
|
|
278
|
+
policyViolation?: string;
|
|
279
|
+
}
|
|
280
|
+
interface RuntimeFunctionToolUsage {
|
|
281
|
+
name: string;
|
|
282
|
+
callId: string;
|
|
283
|
+
status: "completed" | "failed";
|
|
284
|
+
args?: unknown;
|
|
285
|
+
result?: unknown;
|
|
286
|
+
error?: string;
|
|
287
|
+
durationMs?: number;
|
|
288
|
+
}
|
|
289
|
+
interface RuntimePatchProposal {
|
|
290
|
+
callId: string;
|
|
291
|
+
status: "completed" | "failed";
|
|
292
|
+
operation: RuntimeCreateFilePatch | RuntimeUpdateFilePatch | RuntimeDeleteFilePatch;
|
|
293
|
+
raw: unknown;
|
|
294
|
+
}
|
|
295
|
+
interface RuntimeCreateFilePatch {
|
|
296
|
+
type: "create_file";
|
|
297
|
+
path: string;
|
|
298
|
+
diff: string;
|
|
299
|
+
}
|
|
300
|
+
interface RuntimeUpdateFilePatch {
|
|
301
|
+
type: "update_file";
|
|
302
|
+
path: string;
|
|
303
|
+
diff: string;
|
|
304
|
+
}
|
|
305
|
+
interface RuntimeDeleteFilePatch {
|
|
306
|
+
type: "delete_file";
|
|
307
|
+
path: string;
|
|
308
|
+
}
|
|
309
|
+
interface RuntimeError {
|
|
310
|
+
code: string;
|
|
311
|
+
message: string;
|
|
312
|
+
source: "runtime" | "validation" | "openrouter" | "tool_executor" | "policy";
|
|
313
|
+
retryable?: boolean;
|
|
314
|
+
details?: unknown;
|
|
315
|
+
}
|
|
316
|
+
interface RuntimeWarning {
|
|
317
|
+
code: string;
|
|
318
|
+
message: string;
|
|
319
|
+
details?: unknown;
|
|
320
|
+
}
|
|
321
|
+
interface RuntimeLogger {
|
|
322
|
+
debug?(event: string, data?: unknown): void;
|
|
323
|
+
info?(event: string, data?: unknown): void;
|
|
324
|
+
warn?(event: string, data?: unknown): void;
|
|
325
|
+
error?(event: string, data?: unknown): void;
|
|
326
|
+
}
|
|
327
|
+
interface RuntimeHooks {
|
|
328
|
+
beforeCompile?(request: RuntimeRequest): Promise<void> | void;
|
|
329
|
+
afterCompile?(compiled: CompiledOpenRouterRequest): Promise<void> | void;
|
|
330
|
+
beforeOpenRouterRequest?(compiled: CompiledOpenRouterRequest): Promise<void> | void;
|
|
331
|
+
afterOpenRouterResponse?(response: unknown): Promise<void> | void;
|
|
332
|
+
beforeFunctionToolExecution?(call: RuntimeFunctionToolCall): Promise<void> | void;
|
|
333
|
+
afterFunctionToolExecution?(result: RuntimeFunctionToolResult): Promise<void> | void;
|
|
334
|
+
onPolicyViolation?(violation: RuntimePolicyViolation): Promise<void> | void;
|
|
335
|
+
}
|
|
336
|
+
interface CompiledOpenRouterRequest {
|
|
337
|
+
apiMode: ResolvedApiMode;
|
|
338
|
+
url: string;
|
|
339
|
+
headers: Record<string, string>;
|
|
340
|
+
body: Record<string, unknown>;
|
|
341
|
+
warnings: RuntimeWarning[];
|
|
342
|
+
}
|
|
343
|
+
interface RuntimeFunctionToolCall {
|
|
344
|
+
name: string;
|
|
345
|
+
callId: string;
|
|
346
|
+
args: unknown;
|
|
347
|
+
}
|
|
348
|
+
interface RuntimeFunctionToolResult {
|
|
349
|
+
name: string;
|
|
350
|
+
callId: string;
|
|
351
|
+
status: "completed" | "failed";
|
|
352
|
+
result?: unknown;
|
|
353
|
+
error?: string;
|
|
354
|
+
durationMs?: number;
|
|
355
|
+
}
|
|
356
|
+
interface RuntimePolicyViolation {
|
|
357
|
+
code: string;
|
|
358
|
+
message: string;
|
|
359
|
+
details?: unknown;
|
|
360
|
+
}
|
|
361
|
+
interface RuntimeStreamEvent {
|
|
362
|
+
type: string;
|
|
363
|
+
data?: unknown;
|
|
364
|
+
}
|
|
365
|
+
type RuntimePatchApplier = (patch: RuntimePatchProposal) => Promise<unknown> | unknown;
|
|
366
|
+
|
|
367
|
+
declare function createOpenRouterRuntime(options: OpenRouterRuntimeOptions): OpenRouterRuntime;
|
|
368
|
+
|
|
369
|
+
declare function compileRuntimeRequest(request: RuntimeRequest, defaults: RuntimeDefaults, options?: OpenRouterRuntimeOptions): CompiledOpenRouterRequest;
|
|
370
|
+
|
|
371
|
+
declare class RuntimeConfigError extends Error {
|
|
372
|
+
readonly code: string;
|
|
373
|
+
readonly details?: unknown | undefined;
|
|
374
|
+
constructor(code: string, message: string, details?: unknown | undefined);
|
|
375
|
+
}
|
|
376
|
+
declare class OpenRouterHttpError extends Error {
|
|
377
|
+
readonly status: number;
|
|
378
|
+
readonly code: string;
|
|
379
|
+
readonly body?: unknown | undefined;
|
|
380
|
+
readonly retryable: boolean;
|
|
381
|
+
constructor(status: number, code: string, message: string, body?: unknown | undefined, retryable?: boolean);
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
interface OpenRouterTool {
|
|
385
|
+
type: string;
|
|
386
|
+
function?: {
|
|
387
|
+
name: string;
|
|
388
|
+
description: string;
|
|
389
|
+
parameters: Record<string, unknown>;
|
|
390
|
+
};
|
|
391
|
+
parameters?: Record<string, unknown>;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
declare function buildOpenRouterServerTools(policy?: RuntimeServerToolsPolicy): OpenRouterTool[];
|
|
395
|
+
|
|
396
|
+
export { type AdvisorPolicy, type ApplyPatchPolicy, type CompiledOpenRouterRequest, type DatetimePolicy, type FusionPolicy, type ImageGenerationPolicy, OpenRouterHttpError, type OpenRouterRuntime, type OpenRouterRuntimeOptions, type RuntimeCitation, RuntimeConfigError, type RuntimeDefaults, type RuntimeFunctionToolDefinition, type RuntimeGeneratedImage, type RuntimeMessage, type RuntimePatchProposal, type RuntimeRequest, type RuntimeResponse, type RuntimeServerToolsPolicy, type RuntimeToolExecutor, type RuntimeToolUsage, type RuntimeUsage, type SubagentPolicy, type WebFetchPolicy, type WebSearchPolicy, buildOpenRouterServerTools, compileRuntimeRequest, createOpenRouterRuntime };
|