@openrouter/ai-sdk-provider 1.4.0 → 1.4.1

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.mjs CHANGED
@@ -1608,7 +1608,16 @@ var OpenRouterChatLanguageModel = class {
1608
1608
  presence_penalty: presencePenalty,
1609
1609
  seed,
1610
1610
  stop: stopSequences,
1611
- response_format: responseFormat,
1611
+ response_format: (responseFormat == null ? void 0 : responseFormat.type) === "json" ? responseFormat.schema != null ? {
1612
+ type: "json_schema",
1613
+ json_schema: __spreadValues({
1614
+ schema: responseFormat.schema,
1615
+ strict: true,
1616
+ name: (_a15 = responseFormat.name) != null ? _a15 : "response"
1617
+ }, responseFormat.description && {
1618
+ description: responseFormat.description
1619
+ })
1620
+ } : { type: "json_object" } : void 0,
1612
1621
  top_k: topK,
1613
1622
  // messages:
1614
1623
  messages: convertToOpenRouterChatMessages(prompt),
@@ -1624,20 +1633,6 @@ var OpenRouterChatLanguageModel = class {
1624
1633
  // Debug settings:
1625
1634
  debug: this.settings.debug
1626
1635
  }, this.config.extraBody), this.settings.extraBody);
1627
- if ((responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null) {
1628
- return __spreadProps(__spreadValues({}, baseArgs), {
1629
- response_format: {
1630
- type: "json_schema",
1631
- json_schema: __spreadValues({
1632
- schema: responseFormat.schema,
1633
- strict: true,
1634
- name: (_a15 = responseFormat.name) != null ? _a15 : "response"
1635
- }, responseFormat.description && {
1636
- description: responseFormat.description
1637
- })
1638
- }
1639
- });
1640
- }
1641
1636
  if (tools && tools.length > 0) {
1642
1637
  const mappedTools = tools.filter(
1643
1638
  (tool) => tool.type === "function"
@@ -2640,6 +2635,78 @@ var OpenRouterCompletionLanguageModel = class {
2640
2635
  }
2641
2636
  };
2642
2637
 
2638
+ // src/embedding/schemas.ts
2639
+ import { z as z9 } from "zod/v4";
2640
+ var openrouterEmbeddingUsageSchema = z9.object({
2641
+ prompt_tokens: z9.number(),
2642
+ total_tokens: z9.number(),
2643
+ cost: z9.number().optional()
2644
+ });
2645
+ var openrouterEmbeddingDataSchema = z9.object({
2646
+ object: z9.literal("embedding"),
2647
+ embedding: z9.array(z9.number()),
2648
+ index: z9.number().optional()
2649
+ });
2650
+ var OpenRouterEmbeddingResponseSchema = z9.object({
2651
+ id: z9.string().optional(),
2652
+ object: z9.literal("list"),
2653
+ data: z9.array(openrouterEmbeddingDataSchema),
2654
+ model: z9.string(),
2655
+ usage: openrouterEmbeddingUsageSchema.optional()
2656
+ });
2657
+
2658
+ // src/embedding/index.ts
2659
+ var OpenRouterEmbeddingModel = class {
2660
+ constructor(modelId, settings, config) {
2661
+ this.specificationVersion = "v2";
2662
+ this.provider = "openrouter";
2663
+ this.maxEmbeddingsPerCall = void 0;
2664
+ this.supportsParallelCalls = true;
2665
+ this.modelId = modelId;
2666
+ this.settings = settings;
2667
+ this.config = config;
2668
+ }
2669
+ async doEmbed(options) {
2670
+ var _a15;
2671
+ const { values, abortSignal, headers } = options;
2672
+ const args = __spreadValues(__spreadValues({
2673
+ model: this.modelId,
2674
+ input: values,
2675
+ user: this.settings.user,
2676
+ provider: this.settings.provider
2677
+ }, this.config.extraBody), this.settings.extraBody);
2678
+ const { value: responseValue, responseHeaders } = await postJsonToApi({
2679
+ url: this.config.url({
2680
+ path: "/embeddings",
2681
+ modelId: this.modelId
2682
+ }),
2683
+ headers: combineHeaders(this.config.headers(), headers),
2684
+ body: args,
2685
+ failedResponseHandler: openrouterFailedResponseHandler,
2686
+ successfulResponseHandler: createJsonResponseHandler(
2687
+ OpenRouterEmbeddingResponseSchema
2688
+ ),
2689
+ abortSignal,
2690
+ fetch: this.config.fetch
2691
+ });
2692
+ return {
2693
+ embeddings: responseValue.data.map((item) => item.embedding),
2694
+ usage: responseValue.usage ? { tokens: responseValue.usage.prompt_tokens } : void 0,
2695
+ providerMetadata: ((_a15 = responseValue.usage) == null ? void 0 : _a15.cost) ? {
2696
+ openrouter: {
2697
+ usage: {
2698
+ cost: responseValue.usage.cost
2699
+ }
2700
+ }
2701
+ } : void 0,
2702
+ response: {
2703
+ headers: responseHeaders,
2704
+ body: responseValue
2705
+ }
2706
+ };
2707
+ }
2708
+ };
2709
+
2643
2710
  // src/facade.ts
2644
2711
  var OpenRouter = class {
2645
2712
  /**
@@ -2650,17 +2717,20 @@ var OpenRouter = class {
2650
2717
  this.baseURL = (_b = withoutTrailingSlash((_a15 = options.baseURL) != null ? _a15 : options.baseUrl)) != null ? _b : "https://openrouter.ai/api/v1";
2651
2718
  this.apiKey = options.apiKey;
2652
2719
  this.headers = options.headers;
2720
+ this.api_keys = options.api_keys;
2653
2721
  }
2654
2722
  get baseConfig() {
2655
2723
  return {
2656
2724
  baseURL: this.baseURL,
2657
- headers: () => __spreadValues({
2725
+ headers: () => __spreadValues(__spreadValues({
2658
2726
  Authorization: `Bearer ${loadApiKey({
2659
2727
  apiKey: this.apiKey,
2660
2728
  environmentVariableName: "OPENROUTER_API_KEY",
2661
2729
  description: "OpenRouter"
2662
2730
  })}`
2663
- }, this.headers)
2731
+ }, this.headers), this.api_keys && Object.keys(this.api_keys).length > 0 && {
2732
+ "X-Provider-API-Keys": JSON.stringify(this.api_keys)
2733
+ })
2664
2734
  };
2665
2735
  }
2666
2736
  chat(modelId, settings = {}) {
@@ -2679,6 +2749,19 @@ var OpenRouter = class {
2679
2749
  url: ({ path }) => `${this.baseURL}${path}`
2680
2750
  }));
2681
2751
  }
2752
+ textEmbeddingModel(modelId, settings = {}) {
2753
+ return new OpenRouterEmbeddingModel(modelId, settings, __spreadProps(__spreadValues({
2754
+ provider: "openrouter.embedding"
2755
+ }, this.baseConfig), {
2756
+ url: ({ path }) => `${this.baseURL}${path}`
2757
+ }));
2758
+ }
2759
+ /**
2760
+ * @deprecated Use textEmbeddingModel instead
2761
+ */
2762
+ embedding(modelId, settings = {}) {
2763
+ return this.textEmbeddingModel(modelId, settings);
2764
+ }
2682
2765
  };
2683
2766
 
2684
2767
  // src/utils/remove-undefined.ts
@@ -2701,7 +2784,7 @@ function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
2701
2784
  }
2702
2785
 
2703
2786
  // src/version.ts
2704
- var VERSION = false ? "0.0.0-test" : "1.4.0";
2787
+ var VERSION = false ? "0.0.0-test" : "1.4.1";
2705
2788
 
2706
2789
  // src/provider.ts
2707
2790
  function createOpenRouter(options = {}) {
@@ -2709,13 +2792,15 @@ function createOpenRouter(options = {}) {
2709
2792
  const baseURL = (_b = withoutTrailingSlash((_a15 = options.baseURL) != null ? _a15 : options.baseUrl)) != null ? _b : "https://openrouter.ai/api/v1";
2710
2793
  const compatibility = (_c = options.compatibility) != null ? _c : "compatible";
2711
2794
  const getHeaders = () => withUserAgentSuffix(
2712
- __spreadValues({
2795
+ __spreadValues(__spreadValues({
2713
2796
  Authorization: `Bearer ${loadApiKey({
2714
2797
  apiKey: options.apiKey,
2715
2798
  environmentVariableName: "OPENROUTER_API_KEY",
2716
2799
  description: "OpenRouter"
2717
2800
  })}`
2718
- }, options.headers),
2801
+ }, options.headers), options.api_keys && Object.keys(options.api_keys).length > 0 && {
2802
+ "X-Provider-API-Keys": JSON.stringify(options.api_keys)
2803
+ }),
2719
2804
  `ai-sdk/openrouter/${VERSION}`
2720
2805
  );
2721
2806
  const createChatModel = (modelId, settings = {}) => new OpenRouterChatLanguageModel(modelId, settings, {
@@ -2734,6 +2819,13 @@ function createOpenRouter(options = {}) {
2734
2819
  fetch: options.fetch,
2735
2820
  extraBody: options.extraBody
2736
2821
  });
2822
+ const createEmbeddingModel = (modelId, settings = {}) => new OpenRouterEmbeddingModel(modelId, settings, {
2823
+ provider: "openrouter.embedding",
2824
+ url: ({ path }) => `${baseURL}${path}`,
2825
+ headers: getHeaders,
2826
+ fetch: options.fetch,
2827
+ extraBody: options.extraBody
2828
+ });
2737
2829
  const createLanguageModel = (modelId, settings) => {
2738
2830
  if (new.target) {
2739
2831
  throw new Error(
@@ -2752,6 +2844,8 @@ function createOpenRouter(options = {}) {
2752
2844
  provider.languageModel = createLanguageModel;
2753
2845
  provider.chat = createChatModel;
2754
2846
  provider.completion = createCompletionModel;
2847
+ provider.textEmbeddingModel = createEmbeddingModel;
2848
+ provider.embedding = createEmbeddingModel;
2755
2849
  return provider;
2756
2850
  }
2757
2851
  var openrouter = createOpenRouter({