@x12i/openrouter-runtime 1.0.4 → 1.0.6
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/README.md +68 -0
- package/dist/index.cjs +103 -9
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +23 -13
- package/dist/index.d.ts +23 -13
- package/dist/index.js +100 -8
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -3,7 +3,11 @@ type ResolvedApiMode = "chat" | "responses";
|
|
|
3
3
|
type ToolMode = "disabled" | "allowed" | "required";
|
|
4
4
|
interface OpenRouterRuntime {
|
|
5
5
|
run(request: RuntimeRequest): Promise<RuntimeResponse>;
|
|
6
|
-
|
|
6
|
+
/**
|
|
7
|
+
* Streaming chat entrypoint. Separate from `run()` by design.
|
|
8
|
+
* The old `stream()` name was removed so mistaken/old clients break loudly.
|
|
9
|
+
*/
|
|
10
|
+
executeStreamingChat(request: RuntimeRequest): AsyncIterable<RuntimeStreamEvent>;
|
|
7
11
|
}
|
|
8
12
|
interface OpenRouterRuntimeOptions {
|
|
9
13
|
apiKey?: string;
|
|
@@ -367,7 +371,20 @@ type RuntimePatchApplier = (patch: RuntimePatchProposal) => Promise<unknown> | u
|
|
|
367
371
|
|
|
368
372
|
declare function createOpenRouterRuntime(options: OpenRouterRuntimeOptions): OpenRouterRuntime;
|
|
369
373
|
|
|
370
|
-
|
|
374
|
+
interface RuntimeCompileContext {
|
|
375
|
+
streaming: boolean;
|
|
376
|
+
}
|
|
377
|
+
interface OpenRouterTool {
|
|
378
|
+
type: string;
|
|
379
|
+
function?: {
|
|
380
|
+
name: string;
|
|
381
|
+
description: string;
|
|
382
|
+
parameters: Record<string, unknown>;
|
|
383
|
+
};
|
|
384
|
+
parameters?: Record<string, unknown>;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
declare function compileRuntimeRequest(request: RuntimeRequest, defaults: RuntimeDefaults, options?: OpenRouterRuntimeOptions, context?: Partial<RuntimeCompileContext>): CompiledOpenRouterRequest;
|
|
371
388
|
|
|
372
389
|
declare class RuntimeConfigError extends Error {
|
|
373
390
|
readonly code: string;
|
|
@@ -392,16 +409,9 @@ declare class OpenRouterHttpError extends Error {
|
|
|
392
409
|
constructor(status: number, code: ProviderFailureCode, message: string, body?: unknown | undefined, retryable?: boolean, providerAttempts?: ProviderAttemptError[]);
|
|
393
410
|
}
|
|
394
411
|
|
|
395
|
-
interface OpenRouterTool {
|
|
396
|
-
type: string;
|
|
397
|
-
function?: {
|
|
398
|
-
name: string;
|
|
399
|
-
description: string;
|
|
400
|
-
parameters: Record<string, unknown>;
|
|
401
|
-
};
|
|
402
|
-
parameters?: Record<string, unknown>;
|
|
403
|
-
}
|
|
404
|
-
|
|
405
412
|
declare function buildOpenRouterServerTools(policy?: RuntimeServerToolsPolicy): OpenRouterTool[];
|
|
406
413
|
|
|
407
|
-
|
|
414
|
+
declare function isRuntimeLoggingEnabled(env?: Record<string, string | undefined> | undefined): boolean;
|
|
415
|
+
declare function createConsoleLogger(): RuntimeLogger;
|
|
416
|
+
|
|
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 };
|
package/dist/index.js
CHANGED
|
@@ -1,11 +1,36 @@
|
|
|
1
|
+
// src/utils/console-logger.ts
|
|
2
|
+
var TRUTHY = /* @__PURE__ */ new Set(["1", "true", "yes", "on"]);
|
|
3
|
+
function isRuntimeLoggingEnabled(env = typeof process === "undefined" ? void 0 : process.env) {
|
|
4
|
+
const value = env?.OPENROUTER_RUNTIME_LOGS?.trim().toLowerCase();
|
|
5
|
+
return value !== void 0 && TRUTHY.has(value);
|
|
6
|
+
}
|
|
7
|
+
function createConsoleLogger() {
|
|
8
|
+
return {
|
|
9
|
+
debug(event, data) {
|
|
10
|
+
console.debug(`[openrouter-runtime] ${event}`, data ?? "");
|
|
11
|
+
},
|
|
12
|
+
info(event, data) {
|
|
13
|
+
console.info(`[openrouter-runtime] ${event}`, data ?? "");
|
|
14
|
+
},
|
|
15
|
+
warn(event, data) {
|
|
16
|
+
console.warn(`[openrouter-runtime] ${event}`, data ?? "");
|
|
17
|
+
},
|
|
18
|
+
error(event, data) {
|
|
19
|
+
console.error(`[openrouter-runtime] ${event}`, data ?? "");
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
|
|
1
24
|
// src/config/defaults.ts
|
|
2
25
|
function resolveRuntimeOptions(options) {
|
|
26
|
+
const logger = options.logger ?? (isRuntimeLoggingEnabled() ? createConsoleLogger() : void 0);
|
|
3
27
|
return {
|
|
4
28
|
...options,
|
|
5
29
|
apiKey: options.apiKey ?? readOpenRouterApiKeyFromEnv() ?? "",
|
|
6
30
|
baseUrl: options.baseUrl ?? "https://openrouter.ai/api/v1",
|
|
7
31
|
defaultHeaders: options.defaultHeaders ?? {},
|
|
8
|
-
fetch: options.fetch ?? globalThis.fetch
|
|
32
|
+
fetch: options.fetch ?? globalThis.fetch,
|
|
33
|
+
...logger ? { logger } : {}
|
|
9
34
|
};
|
|
10
35
|
}
|
|
11
36
|
function resolveDefaults(options) {
|
|
@@ -860,8 +885,9 @@ function buildSubagentTool(policy) {
|
|
|
860
885
|
}
|
|
861
886
|
|
|
862
887
|
// src/runtime/runtime.ts
|
|
863
|
-
function compileRuntimeRequest(request, defaults, options = { apiKey: "" }) {
|
|
888
|
+
function compileRuntimeRequest(request, defaults, options = { apiKey: "" }, context = {}) {
|
|
864
889
|
const warnings = [];
|
|
890
|
+
const effectiveContext = { streaming: false, ...context };
|
|
865
891
|
const requestedModel = request.model ?? options.defaultModel ?? "";
|
|
866
892
|
const normalized = normalizeModel(requestedModel);
|
|
867
893
|
warnings.push(...normalized.warnings);
|
|
@@ -882,6 +908,7 @@ function compileRuntimeRequest(request, defaults, options = { apiKey: "" }) {
|
|
|
882
908
|
const headers = buildHeaders(options);
|
|
883
909
|
const baseBody = buildBaseBody(request, normalized.model, defaults, tools, serverTools);
|
|
884
910
|
const body = apiMode === "chat" ? buildChatBody(request, baseBody) : buildResponsesBody(request, baseBody);
|
|
911
|
+
enforceStreamingPolicyForRun(body, warnings, effectiveContext);
|
|
885
912
|
return {
|
|
886
913
|
apiMode,
|
|
887
914
|
url: endpointFor(options.baseUrl ?? "https://openrouter.ai/api/v1", apiMode),
|
|
@@ -890,6 +917,41 @@ function compileRuntimeRequest(request, defaults, options = { apiKey: "" }) {
|
|
|
890
917
|
warnings
|
|
891
918
|
};
|
|
892
919
|
}
|
|
920
|
+
function enforceStreamingPolicyForRun(body, warnings, context) {
|
|
921
|
+
if (context.streaming) return;
|
|
922
|
+
if (body.stream !== void 0 && body.stream !== false) {
|
|
923
|
+
warnings.push({
|
|
924
|
+
code: "STREAMING_OVERRIDE_IGNORED_FOR_RUN",
|
|
925
|
+
message: "runtime.run() is non-streaming. Use runtime.executeStreamingChat() when streaming is implemented.",
|
|
926
|
+
details: { ignoredStreamValue: body.stream }
|
|
927
|
+
});
|
|
928
|
+
}
|
|
929
|
+
body.stream = false;
|
|
930
|
+
stripAdvisorStreaming(body, warnings);
|
|
931
|
+
}
|
|
932
|
+
function stripAdvisorStreaming(body, warnings) {
|
|
933
|
+
const tools = body.tools;
|
|
934
|
+
if (!Array.isArray(tools)) return;
|
|
935
|
+
let stripped = 0;
|
|
936
|
+
for (const tool of tools) {
|
|
937
|
+
if (!tool || typeof tool !== "object") continue;
|
|
938
|
+
const record = tool;
|
|
939
|
+
if (record.type !== "openrouter:advisor") continue;
|
|
940
|
+
const parameters = record.parameters;
|
|
941
|
+
if (!parameters || typeof parameters !== "object") continue;
|
|
942
|
+
const params = parameters;
|
|
943
|
+
if (params.stream === void 0 || params.stream === false) continue;
|
|
944
|
+
params.stream = false;
|
|
945
|
+
stripped += 1;
|
|
946
|
+
}
|
|
947
|
+
if (stripped > 0) {
|
|
948
|
+
warnings.push({
|
|
949
|
+
code: "ADVISOR_STREAMING_IGNORED_FOR_RUN",
|
|
950
|
+
message: "runtime.run() disables advisor.stream. Nested advisor streaming is not supported on the non-streaming entrypoint.",
|
|
951
|
+
details: { advisorsStripped: stripped }
|
|
952
|
+
});
|
|
953
|
+
}
|
|
954
|
+
}
|
|
893
955
|
function resolveApiMode(request, defaultMode = "auto", serverTools) {
|
|
894
956
|
const mode = request.apiMode ?? defaultMode;
|
|
895
957
|
if (mode === "chat") return "chat";
|
|
@@ -922,7 +984,7 @@ function buildChatBody(request, baseBody) {
|
|
|
922
984
|
return clean({
|
|
923
985
|
...baseBody,
|
|
924
986
|
messages,
|
|
925
|
-
|
|
987
|
+
max_completion_tokens: request.maxTokens,
|
|
926
988
|
response_format: request.responseFormat
|
|
927
989
|
});
|
|
928
990
|
}
|
|
@@ -1263,6 +1325,14 @@ function optional6(key, value) {
|
|
|
1263
1325
|
return value === void 0 ? {} : { [key]: value };
|
|
1264
1326
|
}
|
|
1265
1327
|
|
|
1328
|
+
// src/runtime/streaming.ts
|
|
1329
|
+
async function* executeStreamingChatNotImplemented() {
|
|
1330
|
+
throw new RuntimeConfigError(
|
|
1331
|
+
"STREAMING_NOT_IMPLEMENTED",
|
|
1332
|
+
"executeStreamingChat() is reserved and not implemented yet. Use runtime.run() for non-streaming execution."
|
|
1333
|
+
);
|
|
1334
|
+
}
|
|
1335
|
+
|
|
1266
1336
|
// src/runtime/create-runtime.ts
|
|
1267
1337
|
function createOpenRouterRuntime(options) {
|
|
1268
1338
|
const resolvedOptions = resolveRuntimeOptions(options);
|
|
@@ -1272,22 +1342,42 @@ function createOpenRouterRuntime(options) {
|
|
|
1272
1342
|
async run(request) {
|
|
1273
1343
|
const requestWithId = { ...request, id: request.id ?? createRuntimeId() };
|
|
1274
1344
|
try {
|
|
1275
|
-
resolvedOptions.logger?.info?.("runtime.request.started", {
|
|
1345
|
+
resolvedOptions.logger?.info?.("runtime.request.started", {
|
|
1346
|
+
requestId: requestWithId.id,
|
|
1347
|
+
streaming: false,
|
|
1348
|
+
entrypoint: "run"
|
|
1349
|
+
});
|
|
1276
1350
|
await resolvedOptions.hooks?.beforeCompile?.(requestWithId);
|
|
1277
|
-
const compiled = compileRuntimeRequest(requestWithId, defaults, resolvedOptions);
|
|
1351
|
+
const compiled = compileRuntimeRequest(requestWithId, defaults, resolvedOptions, { streaming: false });
|
|
1278
1352
|
await resolvedOptions.hooks?.afterCompile?.(compiled);
|
|
1279
1353
|
resolvedOptions.logger?.debug?.("runtime.request.compiled", {
|
|
1280
1354
|
requestId: requestWithId.id,
|
|
1281
|
-
apiMode: compiled.apiMode
|
|
1355
|
+
apiMode: compiled.apiMode,
|
|
1356
|
+
streaming: false,
|
|
1357
|
+
bodyStream: compiled.body.stream,
|
|
1358
|
+
streamingWarnings: compiled.warnings.filter((warning) => warning.code.includes("STREAMING") || warning.code.includes("ADVISOR_STREAMING")).map((warning) => warning.code)
|
|
1282
1359
|
});
|
|
1283
1360
|
await resolvedOptions.hooks?.beforeOpenRouterRequest?.(compiled);
|
|
1284
1361
|
const response = compiled.apiMode === "chat" ? await runChat(requestWithId, compiled, context) : await runResponses(requestWithId, compiled, context);
|
|
1285
1362
|
await resolvedOptions.hooks?.afterOpenRouterResponse?.(response.raw.response);
|
|
1286
|
-
resolvedOptions.logger?.info?.("runtime.response.normalized", {
|
|
1363
|
+
resolvedOptions.logger?.info?.("runtime.response.normalized", {
|
|
1364
|
+
requestId: requestWithId.id,
|
|
1365
|
+
status: response.status,
|
|
1366
|
+
streaming: false
|
|
1367
|
+
});
|
|
1287
1368
|
return response;
|
|
1288
1369
|
} catch (error) {
|
|
1289
1370
|
return errorResponse(requestWithId, error);
|
|
1290
1371
|
}
|
|
1372
|
+
},
|
|
1373
|
+
executeStreamingChat(request) {
|
|
1374
|
+
resolvedOptions.logger?.warn?.("runtime.executeStreamingChat.called", {
|
|
1375
|
+
requestId: request.id,
|
|
1376
|
+
streaming: true,
|
|
1377
|
+
entrypoint: "executeStreamingChat",
|
|
1378
|
+
implemented: false
|
|
1379
|
+
});
|
|
1380
|
+
return executeStreamingChatNotImplemented();
|
|
1291
1381
|
}
|
|
1292
1382
|
};
|
|
1293
1383
|
function errorResponse(request, error) {
|
|
@@ -1380,6 +1470,8 @@ export {
|
|
|
1380
1470
|
RuntimeConfigError,
|
|
1381
1471
|
buildOpenRouterServerTools,
|
|
1382
1472
|
compileRuntimeRequest,
|
|
1383
|
-
|
|
1473
|
+
createConsoleLogger,
|
|
1474
|
+
createOpenRouterRuntime,
|
|
1475
|
+
isRuntimeLoggingEnabled
|
|
1384
1476
|
};
|
|
1385
1477
|
//# sourceMappingURL=index.js.map
|