@x12i/openrouter-runtime 2.0.0 → 2.2.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 +32 -65
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +18 -1
- package/dist/index.d.ts +18 -1
- package/dist/index.js +36 -65
- package/dist/index.js.map +1 -1
- package/package.json +2 -1
package/dist/index.d.cts
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
import * as _x12i_provider_metadata from '@x12i/provider-metadata';
|
|
2
|
+
import { MetadataPacker, MetadataTrimWarning } from '@x12i/provider-metadata';
|
|
3
|
+
export { METADATA_NORMALIZATION_MAP, NormalizedMetadataKey, canonicalMetadataKey, normalizeMetadata } from '@x12i/provider-metadata';
|
|
4
|
+
|
|
1
5
|
type ApiMode = "chat" | "responses" | "auto";
|
|
2
6
|
type ResolvedApiMode = "chat" | "responses";
|
|
3
7
|
type ToolMode = "disabled" | "allowed" | "required";
|
|
@@ -24,6 +28,11 @@ interface OpenRouterRuntime {
|
|
|
24
28
|
}
|
|
25
29
|
interface OpenRouterRuntimeOptions {
|
|
26
30
|
apiKey?: string;
|
|
31
|
+
/**
|
|
32
|
+
* Trims the metadata record to the provider limit and reports dropped keys.
|
|
33
|
+
* Omit it and an oversized record throws `METADATA_RECORD_TOO_LARGE`.
|
|
34
|
+
*/
|
|
35
|
+
metadataPacker?: _x12i_provider_metadata.MetadataPacker;
|
|
27
36
|
baseUrl?: string;
|
|
28
37
|
defaultModel?: string;
|
|
29
38
|
defaultHeaders?: Record<string, string>;
|
|
@@ -509,6 +518,10 @@ interface ProviderMetadataLimits {
|
|
|
509
518
|
}
|
|
510
519
|
/** OpenRouter and OpenAI metadata: 16 pairs, 64-character keys, 512-character values. */
|
|
511
520
|
declare const OPENROUTER_METADATA_LIMITS: ProviderMetadataLimits;
|
|
521
|
+
interface PackedProviderRecord {
|
|
522
|
+
wire?: Record<string, string>;
|
|
523
|
+
warning?: MetadataTrimWarning;
|
|
524
|
+
}
|
|
512
525
|
/**
|
|
513
526
|
* One provider metadata record.
|
|
514
527
|
* Callers pass `metadata` and `agentId`. Every provider stores the same `m.0`, `m.1`, ...
|
|
@@ -516,9 +529,13 @@ declare const OPENROUTER_METADATA_LIMITS: ProviderMetadataLimits;
|
|
|
516
529
|
* `decodeProviderMetadata` restores that map from any provider log.
|
|
517
530
|
* `limits` stays on the signature for existing callers. The record uses the strictest
|
|
518
531
|
* provider limits, so the pairs do not change between providers.
|
|
532
|
+
* Without a packer, a record that does not fit throws `METADATA_RECORD_TOO_LARGE`.
|
|
533
|
+
* A packer drops the lowest-priority pairs and reports them.
|
|
519
534
|
*/
|
|
520
535
|
declare function toProviderMetadata(metadata: Record<string, unknown> | undefined, agentId: string | undefined, _limits: ProviderMetadataLimits, base: Record<string, unknown> | undefined): Record<string, string> | undefined;
|
|
536
|
+
/** Packs one record. A packer trims; otherwise an oversized record throws. */
|
|
537
|
+
declare function packProviderRecord(metadata: Record<string, unknown> | undefined, agentId: string | undefined, base: Record<string, unknown> | undefined, packer?: MetadataPacker): PackedProviderRecord;
|
|
521
538
|
/** Restores the string map from the `m.0`, `m.1`, ... pairs stored on a provider log. */
|
|
522
539
|
declare function decodeProviderMetadata(wire: unknown): Record<string, string>;
|
|
523
540
|
|
|
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 };
|
|
541
|
+
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,7 @@
|
|
|
1
|
+
import * as _x12i_provider_metadata from '@x12i/provider-metadata';
|
|
2
|
+
import { MetadataPacker, MetadataTrimWarning } from '@x12i/provider-metadata';
|
|
3
|
+
export { METADATA_NORMALIZATION_MAP, NormalizedMetadataKey, canonicalMetadataKey, normalizeMetadata } from '@x12i/provider-metadata';
|
|
4
|
+
|
|
1
5
|
type ApiMode = "chat" | "responses" | "auto";
|
|
2
6
|
type ResolvedApiMode = "chat" | "responses";
|
|
3
7
|
type ToolMode = "disabled" | "allowed" | "required";
|
|
@@ -24,6 +28,11 @@ interface OpenRouterRuntime {
|
|
|
24
28
|
}
|
|
25
29
|
interface OpenRouterRuntimeOptions {
|
|
26
30
|
apiKey?: string;
|
|
31
|
+
/**
|
|
32
|
+
* Trims the metadata record to the provider limit and reports dropped keys.
|
|
33
|
+
* Omit it and an oversized record throws `METADATA_RECORD_TOO_LARGE`.
|
|
34
|
+
*/
|
|
35
|
+
metadataPacker?: _x12i_provider_metadata.MetadataPacker;
|
|
27
36
|
baseUrl?: string;
|
|
28
37
|
defaultModel?: string;
|
|
29
38
|
defaultHeaders?: Record<string, string>;
|
|
@@ -509,6 +518,10 @@ interface ProviderMetadataLimits {
|
|
|
509
518
|
}
|
|
510
519
|
/** OpenRouter and OpenAI metadata: 16 pairs, 64-character keys, 512-character values. */
|
|
511
520
|
declare const OPENROUTER_METADATA_LIMITS: ProviderMetadataLimits;
|
|
521
|
+
interface PackedProviderRecord {
|
|
522
|
+
wire?: Record<string, string>;
|
|
523
|
+
warning?: MetadataTrimWarning;
|
|
524
|
+
}
|
|
512
525
|
/**
|
|
513
526
|
* One provider metadata record.
|
|
514
527
|
* Callers pass `metadata` and `agentId`. Every provider stores the same `m.0`, `m.1`, ...
|
|
@@ -516,9 +529,13 @@ declare const OPENROUTER_METADATA_LIMITS: ProviderMetadataLimits;
|
|
|
516
529
|
* `decodeProviderMetadata` restores that map from any provider log.
|
|
517
530
|
* `limits` stays on the signature for existing callers. The record uses the strictest
|
|
518
531
|
* provider limits, so the pairs do not change between providers.
|
|
532
|
+
* Without a packer, a record that does not fit throws `METADATA_RECORD_TOO_LARGE`.
|
|
533
|
+
* A packer drops the lowest-priority pairs and reports them.
|
|
519
534
|
*/
|
|
520
535
|
declare function toProviderMetadata(metadata: Record<string, unknown> | undefined, agentId: string | undefined, _limits: ProviderMetadataLimits, base: Record<string, unknown> | undefined): Record<string, string> | undefined;
|
|
536
|
+
/** Packs one record. A packer trims; otherwise an oversized record throws. */
|
|
537
|
+
declare function packProviderRecord(metadata: Record<string, unknown> | undefined, agentId: string | undefined, base: Record<string, unknown> | undefined, packer?: MetadataPacker): PackedProviderRecord;
|
|
521
538
|
/** Restores the string map from the `m.0`, `m.1`, ... pairs stored on a provider log. */
|
|
522
539
|
declare function decodeProviderMetadata(wire: unknown): Record<string, string>;
|
|
523
540
|
|
|
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 };
|
|
541
|
+
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,43 @@ 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";
|
|
902
|
+
import {
|
|
903
|
+
METADATA_NORMALIZATION_MAP,
|
|
904
|
+
canonicalMetadataKey,
|
|
905
|
+
normalizeMetadata
|
|
906
|
+
} from "@x12i/provider-metadata";
|
|
897
907
|
var OPENROUTER_METADATA_LIMITS = {
|
|
898
908
|
maxPairs: 16,
|
|
899
909
|
maxKeyLength: 64,
|
|
900
910
|
maxValueLength: 512
|
|
901
911
|
};
|
|
902
|
-
var PROVIDER_RECORD_CHUNK = 256;
|
|
903
|
-
var PROVIDER_RECORD_MAX_CHUNKS = 16;
|
|
904
912
|
function toProviderMetadata(metadata, agentId, _limits, base) {
|
|
905
|
-
return
|
|
913
|
+
return packMetadata(base, metadata, agentLayer(agentId));
|
|
906
914
|
}
|
|
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;
|
|
915
|
+
function packProviderRecord(metadata, agentId, base, packer) {
|
|
916
|
+
const layers = [base, metadata, agentLayer(agentId)];
|
|
917
|
+
if (!packer) {
|
|
918
|
+
const wire = packMetadata(...layers);
|
|
919
|
+
return wire ? { wire } : {};
|
|
958
920
|
}
|
|
921
|
+
const packed = packer.pack(...layers);
|
|
922
|
+
const warning = metadataTrimWarning(packed.dropped);
|
|
923
|
+
return {
|
|
924
|
+
...packed.wire ? { wire: packed.wire } : {},
|
|
925
|
+
...warning ? { warning } : {}
|
|
926
|
+
};
|
|
959
927
|
}
|
|
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;
|
|
928
|
+
function decodeProviderMetadata(wire) {
|
|
929
|
+
return openMetadata(wire);
|
|
965
930
|
}
|
|
966
|
-
function
|
|
967
|
-
|
|
931
|
+
function agentLayer(agentId) {
|
|
932
|
+
const trimmed = agentId?.trim();
|
|
933
|
+
return trimmed ? { agentId: trimmed } : void 0;
|
|
968
934
|
}
|
|
969
935
|
|
|
970
936
|
// src/runtime/runtime.ts
|
|
@@ -996,7 +962,7 @@ function compileRuntimeRequest(request, defaults, options = { apiKey: "" }, cont
|
|
|
996
962
|
const assembled = apiMode === "chat" ? buildChatBody(request, baseBody) : buildResponsesBody(request, baseBody);
|
|
997
963
|
let body = effectiveContext.finalizeBody ? effectiveContext.finalizeBody(assembled) : assembled;
|
|
998
964
|
enforceStreamingPolicyForRun(body, warnings, effectiveContext);
|
|
999
|
-
body = attachProviderMetadata(body, request);
|
|
965
|
+
body = attachProviderMetadata(body, request, options, warnings);
|
|
1000
966
|
return {
|
|
1001
967
|
apiMode,
|
|
1002
968
|
url: endpointFor(options.baseUrl ?? "https://openrouter.ai/api/v1", apiMode),
|
|
@@ -1005,10 +971,11 @@ function compileRuntimeRequest(request, defaults, options = { apiKey: "" }, cont
|
|
|
1005
971
|
warnings
|
|
1006
972
|
};
|
|
1007
973
|
}
|
|
1008
|
-
function attachProviderMetadata(body, request) {
|
|
974
|
+
function attachProviderMetadata(body, request, options, warnings) {
|
|
1009
975
|
const fromBody = isRecord(body.metadata) ? body.metadata : void 0;
|
|
1010
|
-
const
|
|
1011
|
-
if (
|
|
976
|
+
const packed = packProviderRecord(request.metadata, request.agentId, fromBody, options.metadataPacker);
|
|
977
|
+
if (packed.warning) warnings.push(packed.warning);
|
|
978
|
+
if (packed.wire) return { ...body, metadata: packed.wire };
|
|
1012
979
|
if (body.metadata === void 0) return body;
|
|
1013
980
|
const rest = { ...body };
|
|
1014
981
|
delete rest.metadata;
|
|
@@ -1908,15 +1875,19 @@ function operatorHintFor(code) {
|
|
|
1908
1875
|
}
|
|
1909
1876
|
}
|
|
1910
1877
|
export {
|
|
1878
|
+
METADATA_NORMALIZATION_MAP,
|
|
1911
1879
|
OPENROUTER_METADATA_LIMITS,
|
|
1912
1880
|
OpenRouterHttpError,
|
|
1913
1881
|
RuntimeConfigError,
|
|
1914
1882
|
buildOpenRouterServerTools,
|
|
1883
|
+
canonicalMetadataKey,
|
|
1915
1884
|
compileRuntimeRequest,
|
|
1916
1885
|
createConsoleLogger,
|
|
1917
1886
|
createOpenRouterRuntime,
|
|
1918
1887
|
decodeProviderMetadata,
|
|
1919
1888
|
isRuntimeLoggingEnabled,
|
|
1889
|
+
normalizeMetadata,
|
|
1890
|
+
packProviderRecord,
|
|
1920
1891
|
toProviderMetadata
|
|
1921
1892
|
};
|
|
1922
1893
|
//# sourceMappingURL=index.js.map
|