@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 CHANGED
@@ -20,15 +20,19 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/index.ts
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
+ METADATA_NORMALIZATION_MAP: () => import_provider_metadata2.METADATA_NORMALIZATION_MAP,
23
24
  OPENROUTER_METADATA_LIMITS: () => OPENROUTER_METADATA_LIMITS,
24
25
  OpenRouterHttpError: () => OpenRouterHttpError,
25
26
  RuntimeConfigError: () => RuntimeConfigError,
26
27
  buildOpenRouterServerTools: () => buildOpenRouterServerTools,
28
+ canonicalMetadataKey: () => import_provider_metadata2.canonicalMetadataKey,
27
29
  compileRuntimeRequest: () => compileRuntimeRequest,
28
30
  createConsoleLogger: () => createConsoleLogger,
29
31
  createOpenRouterRuntime: () => createOpenRouterRuntime,
30
32
  decodeProviderMetadata: () => decodeProviderMetadata,
31
33
  isRuntimeLoggingEnabled: () => isRuntimeLoggingEnabled,
34
+ normalizeMetadata: () => import_provider_metadata2.normalizeMetadata,
35
+ packProviderRecord: () => packProviderRecord,
32
36
  toProviderMetadata: () => toProviderMetadata
33
37
  });
34
38
  module.exports = __toCommonJS(index_exports);
@@ -929,77 +933,35 @@ function buildSubagentTool(policy, fallbackReasoning) {
929
933
  }
930
934
 
931
935
  // src/utils/provider-metadata.ts
936
+ var import_provider_metadata = require("@x12i/provider-metadata");
937
+ var import_provider_metadata2 = require("@x12i/provider-metadata");
932
938
  var OPENROUTER_METADATA_LIMITS = {
933
939
  maxPairs: 16,
934
940
  maxKeyLength: 64,
935
941
  maxValueLength: 512
936
942
  };
937
- var PROVIDER_RECORD_CHUNK = 256;
938
- var PROVIDER_RECORD_MAX_CHUNKS = 16;
939
943
  function toProviderMetadata(metadata, agentId, _limits, base) {
940
- return encodeProviderRecord(collectProviderStrings(metadata, agentId, base));
944
+ return (0, import_provider_metadata.packMetadata)(base, metadata, agentLayer(agentId));
941
945
  }
942
- function decodeProviderMetadata(wire) {
943
- if (!isRecord2(wire)) return {};
944
- const parts = [];
945
- for (let index = 0; ; index += 1) {
946
- const chunk = wire[`m.${index}`];
947
- if (typeof chunk !== "string") break;
948
- parts.push(chunk);
949
- }
950
- if (!parts.length) return {};
951
- const parsed = JSON.parse(Buffer.from(parts.join(""), "base64").toString("utf8"));
952
- if (!isRecord2(parsed)) return {};
953
- const record = {};
954
- for (const [key, value] of Object.entries(parsed)) {
955
- if (typeof value === "string") record[key] = value;
956
- }
957
- return record;
958
- }
959
- function encodeProviderRecord(map) {
960
- const keys = Object.keys(map).sort();
961
- if (!keys.length) return void 0;
962
- const ordered = {};
963
- for (const key of keys) {
964
- const value = map[key];
965
- if (value !== void 0) ordered[key] = value;
966
- }
967
- const encoded = Buffer.from(JSON.stringify(ordered), "utf8").toString("base64");
968
- const chunks = Math.ceil(encoded.length / PROVIDER_RECORD_CHUNK);
969
- if (chunks > PROVIDER_RECORD_MAX_CHUNKS) {
970
- throw new Error(
971
- `METADATA_RECORD_TOO_LARGE: provider metadata is ${encoded.length} base64 characters; the maximum is ${PROVIDER_RECORD_CHUNK * PROVIDER_RECORD_MAX_CHUNKS}.`
972
- );
973
- }
974
- const wire = {};
975
- for (let index = 0; index < chunks; index += 1) {
976
- wire[`m.${index}`] = encoded.slice(index * PROVIDER_RECORD_CHUNK, (index + 1) * PROVIDER_RECORD_CHUNK);
977
- }
978
- return wire;
979
- }
980
- function collectProviderStrings(metadata, agentId, base) {
981
- const map = {};
982
- appendStrings(map, base);
983
- appendStrings(map, metadata);
984
- const trimmedAgentId = agentId?.trim();
985
- if (trimmedAgentId) map.agentId = trimmedAgentId;
986
- return map;
987
- }
988
- function appendStrings(target, metadata) {
989
- if (!metadata) return;
990
- for (const [key, value] of Object.entries(metadata)) {
991
- const asString = asMetadataString(value);
992
- if (asString !== void 0) target[key] = asString;
946
+ function packProviderRecord(metadata, agentId, base, packer) {
947
+ const layers = [base, metadata, agentLayer(agentId)];
948
+ if (!packer) {
949
+ const wire = (0, import_provider_metadata.packMetadata)(...layers);
950
+ return wire ? { wire } : {};
993
951
  }
952
+ const packed = packer.pack(...layers);
953
+ const warning = (0, import_provider_metadata.metadataTrimWarning)(packed.dropped);
954
+ return {
955
+ ...packed.wire ? { wire: packed.wire } : {},
956
+ ...warning ? { warning } : {}
957
+ };
994
958
  }
995
- function asMetadataString(value) {
996
- if (typeof value === "string") return value;
997
- if (typeof value === "boolean") return value ? "true" : "false";
998
- if (typeof value === "number" && Number.isFinite(value)) return String(value);
999
- return void 0;
959
+ function decodeProviderMetadata(wire) {
960
+ return (0, import_provider_metadata.openMetadata)(wire);
1000
961
  }
1001
- function isRecord2(value) {
1002
- return typeof value === "object" && value !== null && !Array.isArray(value);
962
+ function agentLayer(agentId) {
963
+ const trimmed = agentId?.trim();
964
+ return trimmed ? { agentId: trimmed } : void 0;
1003
965
  }
1004
966
 
1005
967
  // src/runtime/runtime.ts
@@ -1031,7 +993,7 @@ function compileRuntimeRequest(request, defaults, options = { apiKey: "" }, cont
1031
993
  const assembled = apiMode === "chat" ? buildChatBody(request, baseBody) : buildResponsesBody(request, baseBody);
1032
994
  let body = effectiveContext.finalizeBody ? effectiveContext.finalizeBody(assembled) : assembled;
1033
995
  enforceStreamingPolicyForRun(body, warnings, effectiveContext);
1034
- body = attachProviderMetadata(body, request);
996
+ body = attachProviderMetadata(body, request, options, warnings);
1035
997
  return {
1036
998
  apiMode,
1037
999
  url: endpointFor(options.baseUrl ?? "https://openrouter.ai/api/v1", apiMode),
@@ -1040,10 +1002,11 @@ function compileRuntimeRequest(request, defaults, options = { apiKey: "" }, cont
1040
1002
  warnings
1041
1003
  };
1042
1004
  }
1043
- function attachProviderMetadata(body, request) {
1005
+ function attachProviderMetadata(body, request, options, warnings) {
1044
1006
  const fromBody = isRecord(body.metadata) ? body.metadata : void 0;
1045
- const metadata = toProviderMetadata(request.metadata, request.agentId, OPENROUTER_METADATA_LIMITS, fromBody);
1046
- if (metadata) return { ...body, metadata };
1007
+ const packed = packProviderRecord(request.metadata, request.agentId, fromBody, options.metadataPacker);
1008
+ if (packed.warning) warnings.push(packed.warning);
1009
+ if (packed.wire) return { ...body, metadata: packed.wire };
1047
1010
  if (body.metadata === void 0) return body;
1048
1011
  const rest = { ...body };
1049
1012
  delete rest.metadata;
@@ -1944,15 +1907,19 @@ function operatorHintFor(code) {
1944
1907
  }
1945
1908
  // Annotate the CommonJS export names for ESM import in node:
1946
1909
  0 && (module.exports = {
1910
+ METADATA_NORMALIZATION_MAP,
1947
1911
  OPENROUTER_METADATA_LIMITS,
1948
1912
  OpenRouterHttpError,
1949
1913
  RuntimeConfigError,
1950
1914
  buildOpenRouterServerTools,
1915
+ canonicalMetadataKey,
1951
1916
  compileRuntimeRequest,
1952
1917
  createConsoleLogger,
1953
1918
  createOpenRouterRuntime,
1954
1919
  decodeProviderMetadata,
1955
1920
  isRuntimeLoggingEnabled,
1921
+ normalizeMetadata,
1922
+ packProviderRecord,
1956
1923
  toProviderMetadata
1957
1924
  });
1958
1925
  //# sourceMappingURL=index.cjs.map