@x12i/openrouter-runtime 1.4.0 → 2.1.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.cjs +25 -65
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +17 -1
- package/dist/index.d.ts +17 -1
- package/dist/index.js +28 -65
- package/dist/index.js.map +1 -1
- package/package.json +2 -1
package/dist/index.d.cts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import * as _x12i_provider_metadata from '@x12i/provider-metadata';
|
|
2
|
+
import { MetadataPacker, MetadataTrimWarning } from '@x12i/provider-metadata';
|
|
3
|
+
|
|
1
4
|
type ApiMode = "chat" | "responses" | "auto";
|
|
2
5
|
type ResolvedApiMode = "chat" | "responses";
|
|
3
6
|
type ToolMode = "disabled" | "allowed" | "required";
|
|
@@ -24,6 +27,11 @@ interface OpenRouterRuntime {
|
|
|
24
27
|
}
|
|
25
28
|
interface OpenRouterRuntimeOptions {
|
|
26
29
|
apiKey?: string;
|
|
30
|
+
/**
|
|
31
|
+
* Trims the metadata record to the provider limit and reports dropped keys.
|
|
32
|
+
* Omit it and an oversized record throws `METADATA_RECORD_TOO_LARGE`.
|
|
33
|
+
*/
|
|
34
|
+
metadataPacker?: _x12i_provider_metadata.MetadataPacker;
|
|
27
35
|
baseUrl?: string;
|
|
28
36
|
defaultModel?: string;
|
|
29
37
|
defaultHeaders?: Record<string, string>;
|
|
@@ -509,6 +517,10 @@ interface ProviderMetadataLimits {
|
|
|
509
517
|
}
|
|
510
518
|
/** OpenRouter and OpenAI metadata: 16 pairs, 64-character keys, 512-character values. */
|
|
511
519
|
declare const OPENROUTER_METADATA_LIMITS: ProviderMetadataLimits;
|
|
520
|
+
interface PackedProviderRecord {
|
|
521
|
+
wire?: Record<string, string>;
|
|
522
|
+
warning?: MetadataTrimWarning;
|
|
523
|
+
}
|
|
512
524
|
/**
|
|
513
525
|
* One provider metadata record.
|
|
514
526
|
* Callers pass `metadata` and `agentId`. Every provider stores the same `m.0`, `m.1`, ...
|
|
@@ -516,9 +528,13 @@ declare const OPENROUTER_METADATA_LIMITS: ProviderMetadataLimits;
|
|
|
516
528
|
* `decodeProviderMetadata` restores that map from any provider log.
|
|
517
529
|
* `limits` stays on the signature for existing callers. The record uses the strictest
|
|
518
530
|
* provider limits, so the pairs do not change between providers.
|
|
531
|
+
* Without a packer, a record that does not fit throws `METADATA_RECORD_TOO_LARGE`.
|
|
532
|
+
* A packer drops the lowest-priority pairs and reports them.
|
|
519
533
|
*/
|
|
520
534
|
declare function toProviderMetadata(metadata: Record<string, unknown> | undefined, agentId: string | undefined, _limits: ProviderMetadataLimits, base: Record<string, unknown> | undefined): Record<string, string> | undefined;
|
|
535
|
+
/** Packs one record. A packer trims; otherwise an oversized record throws. */
|
|
536
|
+
declare function packProviderRecord(metadata: Record<string, unknown> | undefined, agentId: string | undefined, base: Record<string, unknown> | undefined, packer?: MetadataPacker): PackedProviderRecord;
|
|
521
537
|
/** Restores the string map from the `m.0`, `m.1`, ... pairs stored on a provider log. */
|
|
522
538
|
declare function decodeProviderMetadata(wire: unknown): Record<string, string>;
|
|
523
539
|
|
|
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 };
|
|
540
|
+
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, packProviderRecord, toProviderMetadata };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import * as _x12i_provider_metadata from '@x12i/provider-metadata';
|
|
2
|
+
import { MetadataPacker, MetadataTrimWarning } from '@x12i/provider-metadata';
|
|
3
|
+
|
|
1
4
|
type ApiMode = "chat" | "responses" | "auto";
|
|
2
5
|
type ResolvedApiMode = "chat" | "responses";
|
|
3
6
|
type ToolMode = "disabled" | "allowed" | "required";
|
|
@@ -24,6 +27,11 @@ interface OpenRouterRuntime {
|
|
|
24
27
|
}
|
|
25
28
|
interface OpenRouterRuntimeOptions {
|
|
26
29
|
apiKey?: string;
|
|
30
|
+
/**
|
|
31
|
+
* Trims the metadata record to the provider limit and reports dropped keys.
|
|
32
|
+
* Omit it and an oversized record throws `METADATA_RECORD_TOO_LARGE`.
|
|
33
|
+
*/
|
|
34
|
+
metadataPacker?: _x12i_provider_metadata.MetadataPacker;
|
|
27
35
|
baseUrl?: string;
|
|
28
36
|
defaultModel?: string;
|
|
29
37
|
defaultHeaders?: Record<string, string>;
|
|
@@ -509,6 +517,10 @@ interface ProviderMetadataLimits {
|
|
|
509
517
|
}
|
|
510
518
|
/** OpenRouter and OpenAI metadata: 16 pairs, 64-character keys, 512-character values. */
|
|
511
519
|
declare const OPENROUTER_METADATA_LIMITS: ProviderMetadataLimits;
|
|
520
|
+
interface PackedProviderRecord {
|
|
521
|
+
wire?: Record<string, string>;
|
|
522
|
+
warning?: MetadataTrimWarning;
|
|
523
|
+
}
|
|
512
524
|
/**
|
|
513
525
|
* One provider metadata record.
|
|
514
526
|
* Callers pass `metadata` and `agentId`. Every provider stores the same `m.0`, `m.1`, ...
|
|
@@ -516,9 +528,13 @@ declare const OPENROUTER_METADATA_LIMITS: ProviderMetadataLimits;
|
|
|
516
528
|
* `decodeProviderMetadata` restores that map from any provider log.
|
|
517
529
|
* `limits` stays on the signature for existing callers. The record uses the strictest
|
|
518
530
|
* provider limits, so the pairs do not change between providers.
|
|
531
|
+
* Without a packer, a record that does not fit throws `METADATA_RECORD_TOO_LARGE`.
|
|
532
|
+
* A packer drops the lowest-priority pairs and reports them.
|
|
519
533
|
*/
|
|
520
534
|
declare function toProviderMetadata(metadata: Record<string, unknown> | undefined, agentId: string | undefined, _limits: ProviderMetadataLimits, base: Record<string, unknown> | undefined): Record<string, string> | undefined;
|
|
535
|
+
/** Packs one record. A packer trims; otherwise an oversized record throws. */
|
|
536
|
+
declare function packProviderRecord(metadata: Record<string, unknown> | undefined, agentId: string | undefined, base: Record<string, unknown> | undefined, packer?: MetadataPacker): PackedProviderRecord;
|
|
521
537
|
/** Restores the string map from the `m.0`, `m.1`, ... pairs stored on a provider log. */
|
|
522
538
|
declare function decodeProviderMetadata(wire: unknown): Record<string, string>;
|
|
523
539
|
|
|
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 };
|
|
540
|
+
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, packProviderRecord, toProviderMetadata };
|
package/dist/index.js
CHANGED
|
@@ -894,77 +894,38 @@ function buildSubagentTool(policy, fallbackReasoning) {
|
|
|
894
894
|
}
|
|
895
895
|
|
|
896
896
|
// src/utils/provider-metadata.ts
|
|
897
|
+
import {
|
|
898
|
+
openMetadata,
|
|
899
|
+
packMetadata,
|
|
900
|
+
metadataTrimWarning
|
|
901
|
+
} from "@x12i/provider-metadata";
|
|
897
902
|
var OPENROUTER_METADATA_LIMITS = {
|
|
898
903
|
maxPairs: 16,
|
|
899
904
|
maxKeyLength: 64,
|
|
900
905
|
maxValueLength: 512
|
|
901
906
|
};
|
|
902
|
-
var PROVIDER_RECORD_CHUNK = 256;
|
|
903
|
-
var PROVIDER_RECORD_MAX_CHUNKS = 16;
|
|
904
907
|
function toProviderMetadata(metadata, agentId, _limits, base) {
|
|
905
|
-
return
|
|
908
|
+
return packMetadata(base, metadata, agentLayer(agentId));
|
|
906
909
|
}
|
|
907
|
-
function
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
if (typeof chunk !== "string") break;
|
|
913
|
-
parts.push(chunk);
|
|
914
|
-
}
|
|
915
|
-
if (!parts.length) return {};
|
|
916
|
-
const parsed = JSON.parse(Buffer.from(parts.join(""), "base64").toString("utf8"));
|
|
917
|
-
if (!isRecord2(parsed)) return {};
|
|
918
|
-
const record = {};
|
|
919
|
-
for (const [key, value] of Object.entries(parsed)) {
|
|
920
|
-
if (typeof value === "string") record[key] = value;
|
|
921
|
-
}
|
|
922
|
-
return record;
|
|
923
|
-
}
|
|
924
|
-
function encodeProviderRecord(map) {
|
|
925
|
-
const keys = Object.keys(map).sort();
|
|
926
|
-
if (!keys.length) return void 0;
|
|
927
|
-
const ordered = {};
|
|
928
|
-
for (const key of keys) {
|
|
929
|
-
const value = map[key];
|
|
930
|
-
if (value !== void 0) ordered[key] = value;
|
|
931
|
-
}
|
|
932
|
-
const encoded = Buffer.from(JSON.stringify(ordered), "utf8").toString("base64");
|
|
933
|
-
const chunks = Math.ceil(encoded.length / PROVIDER_RECORD_CHUNK);
|
|
934
|
-
if (chunks > PROVIDER_RECORD_MAX_CHUNKS) {
|
|
935
|
-
throw new Error(
|
|
936
|
-
`METADATA_RECORD_TOO_LARGE: provider metadata is ${encoded.length} base64 characters; the maximum is ${PROVIDER_RECORD_CHUNK * PROVIDER_RECORD_MAX_CHUNKS}.`
|
|
937
|
-
);
|
|
938
|
-
}
|
|
939
|
-
const wire = {};
|
|
940
|
-
for (let index = 0; index < chunks; index += 1) {
|
|
941
|
-
wire[`m.${index}`] = encoded.slice(index * PROVIDER_RECORD_CHUNK, (index + 1) * PROVIDER_RECORD_CHUNK);
|
|
942
|
-
}
|
|
943
|
-
return wire;
|
|
944
|
-
}
|
|
945
|
-
function collectProviderStrings(metadata, agentId, base) {
|
|
946
|
-
const map = {};
|
|
947
|
-
appendStrings(map, base);
|
|
948
|
-
appendStrings(map, metadata);
|
|
949
|
-
const trimmedAgentId = agentId?.trim();
|
|
950
|
-
if (trimmedAgentId) map.agentId = trimmedAgentId;
|
|
951
|
-
return map;
|
|
952
|
-
}
|
|
953
|
-
function appendStrings(target, metadata) {
|
|
954
|
-
if (!metadata) return;
|
|
955
|
-
for (const [key, value] of Object.entries(metadata)) {
|
|
956
|
-
const asString = asMetadataString(value);
|
|
957
|
-
if (asString !== void 0) target[key] = asString;
|
|
910
|
+
function packProviderRecord(metadata, agentId, base, packer) {
|
|
911
|
+
const layers = [base, metadata, agentLayer(agentId)];
|
|
912
|
+
if (!packer) {
|
|
913
|
+
const wire = packMetadata(...layers);
|
|
914
|
+
return wire ? { wire } : {};
|
|
958
915
|
}
|
|
916
|
+
const packed = packer.pack(...layers);
|
|
917
|
+
const warning = metadataTrimWarning(packed.dropped);
|
|
918
|
+
return {
|
|
919
|
+
...packed.wire ? { wire: packed.wire } : {},
|
|
920
|
+
...warning ? { warning } : {}
|
|
921
|
+
};
|
|
959
922
|
}
|
|
960
|
-
function
|
|
961
|
-
|
|
962
|
-
if (typeof value === "boolean") return value ? "true" : "false";
|
|
963
|
-
if (typeof value === "number" && Number.isFinite(value)) return String(value);
|
|
964
|
-
return void 0;
|
|
923
|
+
function decodeProviderMetadata(wire) {
|
|
924
|
+
return openMetadata(wire);
|
|
965
925
|
}
|
|
966
|
-
function
|
|
967
|
-
|
|
926
|
+
function agentLayer(agentId) {
|
|
927
|
+
const trimmed = agentId?.trim();
|
|
928
|
+
return trimmed ? { agentId: trimmed } : void 0;
|
|
968
929
|
}
|
|
969
930
|
|
|
970
931
|
// src/runtime/runtime.ts
|
|
@@ -996,7 +957,7 @@ function compileRuntimeRequest(request, defaults, options = { apiKey: "" }, cont
|
|
|
996
957
|
const assembled = apiMode === "chat" ? buildChatBody(request, baseBody) : buildResponsesBody(request, baseBody);
|
|
997
958
|
let body = effectiveContext.finalizeBody ? effectiveContext.finalizeBody(assembled) : assembled;
|
|
998
959
|
enforceStreamingPolicyForRun(body, warnings, effectiveContext);
|
|
999
|
-
body = attachProviderMetadata(body, request);
|
|
960
|
+
body = attachProviderMetadata(body, request, options, warnings);
|
|
1000
961
|
return {
|
|
1001
962
|
apiMode,
|
|
1002
963
|
url: endpointFor(options.baseUrl ?? "https://openrouter.ai/api/v1", apiMode),
|
|
@@ -1005,10 +966,11 @@ function compileRuntimeRequest(request, defaults, options = { apiKey: "" }, cont
|
|
|
1005
966
|
warnings
|
|
1006
967
|
};
|
|
1007
968
|
}
|
|
1008
|
-
function attachProviderMetadata(body, request) {
|
|
969
|
+
function attachProviderMetadata(body, request, options, warnings) {
|
|
1009
970
|
const fromBody = isRecord(body.metadata) ? body.metadata : void 0;
|
|
1010
|
-
const
|
|
1011
|
-
if (
|
|
971
|
+
const packed = packProviderRecord(request.metadata, request.agentId, fromBody, options.metadataPacker);
|
|
972
|
+
if (packed.warning) warnings.push(packed.warning);
|
|
973
|
+
if (packed.wire) return { ...body, metadata: packed.wire };
|
|
1012
974
|
if (body.metadata === void 0) return body;
|
|
1013
975
|
const rest = { ...body };
|
|
1014
976
|
delete rest.metadata;
|
|
@@ -1917,6 +1879,7 @@ export {
|
|
|
1917
1879
|
createOpenRouterRuntime,
|
|
1918
1880
|
decodeProviderMetadata,
|
|
1919
1881
|
isRuntimeLoggingEnabled,
|
|
1882
|
+
packProviderRecord,
|
|
1920
1883
|
toProviderMetadata
|
|
1921
1884
|
};
|
|
1922
1885
|
//# sourceMappingURL=index.js.map
|