@x12i/openrouter-runtime 1.0.5 → 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 +46 -22
- package/dist/index.cjs +86 -13
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +9 -2
- package/dist/index.d.ts +9 -2
- package/dist/index.js +83 -12
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
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) {
|
|
@@ -894,13 +919,38 @@ function compileRuntimeRequest(request, defaults, options = { apiKey: "" }, cont
|
|
|
894
919
|
}
|
|
895
920
|
function enforceStreamingPolicyForRun(body, warnings, context) {
|
|
896
921
|
if (context.streaming) return;
|
|
897
|
-
if (body.stream
|
|
922
|
+
if (body.stream !== void 0 && body.stream !== false) {
|
|
898
923
|
warnings.push({
|
|
899
924
|
code: "STREAMING_OVERRIDE_IGNORED_FOR_RUN",
|
|
900
|
-
message: "runtime.run() is non-streaming. Use runtime.
|
|
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 }
|
|
901
952
|
});
|
|
902
953
|
}
|
|
903
|
-
delete body.stream;
|
|
904
954
|
}
|
|
905
955
|
function resolveApiMode(request, defaultMode = "auto", serverTools) {
|
|
906
956
|
const mode = request.apiMode ?? defaultMode;
|
|
@@ -934,7 +984,7 @@ function buildChatBody(request, baseBody) {
|
|
|
934
984
|
return clean({
|
|
935
985
|
...baseBody,
|
|
936
986
|
messages,
|
|
937
|
-
|
|
987
|
+
max_completion_tokens: request.maxTokens,
|
|
938
988
|
response_format: request.responseFormat
|
|
939
989
|
});
|
|
940
990
|
}
|
|
@@ -1276,10 +1326,10 @@ function optional6(key, value) {
|
|
|
1276
1326
|
}
|
|
1277
1327
|
|
|
1278
1328
|
// src/runtime/streaming.ts
|
|
1279
|
-
async function*
|
|
1329
|
+
async function* executeStreamingChatNotImplemented() {
|
|
1280
1330
|
throw new RuntimeConfigError(
|
|
1281
1331
|
"STREAMING_NOT_IMPLEMENTED",
|
|
1282
|
-
"
|
|
1332
|
+
"executeStreamingChat() is reserved and not implemented yet. Use runtime.run() for non-streaming execution."
|
|
1283
1333
|
);
|
|
1284
1334
|
}
|
|
1285
1335
|
|
|
@@ -1292,24 +1342,43 @@ function createOpenRouterRuntime(options) {
|
|
|
1292
1342
|
async run(request) {
|
|
1293
1343
|
const requestWithId = { ...request, id: request.id ?? createRuntimeId() };
|
|
1294
1344
|
try {
|
|
1295
|
-
resolvedOptions.logger?.info?.("runtime.request.started", {
|
|
1345
|
+
resolvedOptions.logger?.info?.("runtime.request.started", {
|
|
1346
|
+
requestId: requestWithId.id,
|
|
1347
|
+
streaming: false,
|
|
1348
|
+
entrypoint: "run"
|
|
1349
|
+
});
|
|
1296
1350
|
await resolvedOptions.hooks?.beforeCompile?.(requestWithId);
|
|
1297
1351
|
const compiled = compileRuntimeRequest(requestWithId, defaults, resolvedOptions, { streaming: false });
|
|
1298
1352
|
await resolvedOptions.hooks?.afterCompile?.(compiled);
|
|
1299
1353
|
resolvedOptions.logger?.debug?.("runtime.request.compiled", {
|
|
1300
1354
|
requestId: requestWithId.id,
|
|
1301
|
-
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)
|
|
1302
1359
|
});
|
|
1303
1360
|
await resolvedOptions.hooks?.beforeOpenRouterRequest?.(compiled);
|
|
1304
1361
|
const response = compiled.apiMode === "chat" ? await runChat(requestWithId, compiled, context) : await runResponses(requestWithId, compiled, context);
|
|
1305
1362
|
await resolvedOptions.hooks?.afterOpenRouterResponse?.(response.raw.response);
|
|
1306
|
-
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
|
+
});
|
|
1307
1368
|
return response;
|
|
1308
1369
|
} catch (error) {
|
|
1309
1370
|
return errorResponse(requestWithId, error);
|
|
1310
1371
|
}
|
|
1311
1372
|
},
|
|
1312
|
-
|
|
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();
|
|
1381
|
+
}
|
|
1313
1382
|
};
|
|
1314
1383
|
function errorResponse(request, error) {
|
|
1315
1384
|
const runtimeError = normalizeError(error);
|
|
@@ -1401,6 +1470,8 @@ export {
|
|
|
1401
1470
|
RuntimeConfigError,
|
|
1402
1471
|
buildOpenRouterServerTools,
|
|
1403
1472
|
compileRuntimeRequest,
|
|
1404
|
-
|
|
1473
|
+
createConsoleLogger,
|
|
1474
|
+
createOpenRouterRuntime,
|
|
1475
|
+
isRuntimeLoggingEnabled
|
|
1405
1476
|
};
|
|
1406
1477
|
//# sourceMappingURL=index.js.map
|