@openrouter/ai-sdk-provider 0.3.0 → 0.4.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.d.ts CHANGED
@@ -1,8 +1,37 @@
1
1
  import { LanguageModelV1 } from '@ai-sdk/provider';
2
2
  export { LanguageModelV1 } from '@ai-sdk/provider';
3
3
 
4
+ type OpenRouterLanguageModel = LanguageModelV1;
5
+ type OpenRouterProviderOptions = {
6
+ models?: string[];
7
+ /**
8
+ * https://openrouter.ai/docs/use-cases/reasoning-tokens
9
+ * One of `max_tokens` or `effort` is required.
10
+ * If `exclude` is true, reasoning will be removed from the response. Default is false.
11
+ */
12
+ reasoning?: {
13
+ exclude?: boolean;
14
+ } & ({
15
+ max_tokens: number;
16
+ } | {
17
+ effort: 'high' | 'medium' | 'low';
18
+ });
19
+ /**
20
+ * A unique identifier representing your end-user, which can
21
+ * help OpenRouter to monitor and detect abuse.
22
+ */
23
+ user?: string;
24
+ };
25
+ type OpenRouterSharedSettings = OpenRouterProviderOptions & {
26
+ /**
27
+ * @deprecated use `reasoning` instead
28
+ */
29
+ includeReasoning?: boolean;
30
+ extraBody?: Record<string, any>;
31
+ };
32
+
4
33
  type OpenRouterChatModelId = string;
5
- interface OpenRouterChatSettings {
34
+ type OpenRouterChatSettings = {
6
35
  /**
7
36
  Modify the likelihood of specified tokens appearing in the completion.
8
37
 
@@ -39,52 +68,10 @@ interface OpenRouterChatSettings {
39
68
  monitor and detect abuse. Learn more.
40
69
  */
41
70
  user?: string;
42
- /**
43
- * List of model IDs to try in order if the primary model fails, e.g. ["anthropic/claude-2","gryphe/mythomax-l2-13b"]
44
- */
45
- models?: string[];
46
- /**
47
- * @deprecated use `reasoning` instead
48
- */
49
- includeReasoning?: boolean;
50
- reasoning?: {
51
- exclude?: boolean;
52
- max_tokens?: number;
53
- effort?: 'high' | 'medium' | 'low';
54
- };
55
- extraBody?: Record<string, any>;
56
- }
57
-
58
- type OpenRouterChatConfig = {
59
- provider: string;
60
- compatibility: "strict" | "compatible";
61
- headers: () => Record<string, string | undefined>;
62
- url: (options: {
63
- modelId: string;
64
- path: string;
65
- }) => string;
66
- fetch?: typeof fetch;
67
- extraBody?: Record<string, unknown>;
68
- };
69
- declare class OpenRouterChatLanguageModel implements LanguageModelV1 {
70
- readonly specificationVersion = "v1";
71
- readonly defaultObjectGenerationMode = "tool";
72
- readonly modelId: OpenRouterChatModelId;
73
- readonly settings: OpenRouterChatSettings;
74
- private readonly config;
75
- constructor(modelId: OpenRouterChatModelId, settings: OpenRouterChatSettings, config: OpenRouterChatConfig);
76
- get provider(): string;
77
- private getArgs;
78
- doGenerate(options: Parameters<LanguageModelV1["doGenerate"]>[0]): Promise<Awaited<ReturnType<LanguageModelV1["doGenerate"]>>>;
79
- doStream(options: Parameters<LanguageModelV1["doStream"]>[0]): Promise<Awaited<ReturnType<LanguageModelV1["doStream"]>>>;
80
- }
71
+ } & OpenRouterSharedSettings;
81
72
 
82
- type OpenRouterCompletionModelId = string & {};
83
- interface OpenRouterCompletionSettings {
84
- /**
85
- Echo back the prompt in addition to the completion.
86
- */
87
- echo?: boolean;
73
+ type OpenRouterCompletionModelId = string;
74
+ type OpenRouterCompletionSettings = {
88
75
  /**
89
76
  Modify the likelihood of specified tokens appearing in the completion.
90
77
 
@@ -116,30 +103,35 @@ interface OpenRouterCompletionSettings {
116
103
  The suffix that comes after a completion of inserted text.
117
104
  */
118
105
  suffix?: string;
119
- /**
120
- A unique identifier representing your end-user, which can help OpenRouter to
121
- monitor and detect abuse. Learn more.
122
- */
123
- user?: string;
124
- /**
125
- * List of model IDs to try in order if the primary model fails, e.g. ["openai/gpt-4","anthropic/claude-2"]
126
- */
127
- models?: string[];
128
- /**
129
- * @deprecated use `reasoning` instead
130
- */
131
- includeReasoning?: boolean;
132
- reasoning?: {
133
- exclude?: boolean;
134
- max_tokens?: number;
135
- effort?: 'high' | 'medium' | 'low';
136
- };
137
- extraBody?: Record<string, any>;
106
+ } & OpenRouterSharedSettings;
107
+
108
+ type OpenRouterChatConfig = {
109
+ provider: string;
110
+ compatibility: 'strict' | 'compatible';
111
+ headers: () => Record<string, string | undefined>;
112
+ url: (options: {
113
+ modelId: string;
114
+ path: string;
115
+ }) => string;
116
+ fetch?: typeof fetch;
117
+ extraBody?: Record<string, unknown>;
118
+ };
119
+ declare class OpenRouterChatLanguageModel implements LanguageModelV1 {
120
+ readonly specificationVersion = "v1";
121
+ readonly defaultObjectGenerationMode = "tool";
122
+ readonly modelId: OpenRouterChatModelId;
123
+ readonly settings: OpenRouterChatSettings;
124
+ private readonly config;
125
+ constructor(modelId: OpenRouterChatModelId, settings: OpenRouterChatSettings, config: OpenRouterChatConfig);
126
+ get provider(): string;
127
+ private getArgs;
128
+ doGenerate(options: Parameters<LanguageModelV1['doGenerate']>[0]): Promise<Awaited<ReturnType<LanguageModelV1['doGenerate']>>>;
129
+ doStream(options: Parameters<LanguageModelV1['doStream']>[0]): Promise<Awaited<ReturnType<LanguageModelV1['doStream']>>>;
138
130
  }
139
131
 
140
132
  type OpenRouterCompletionConfig = {
141
133
  provider: string;
142
- compatibility: "strict" | "compatible";
134
+ compatibility: 'strict' | 'compatible';
143
135
  headers: () => Record<string, string | undefined>;
144
136
  url: (options: {
145
137
  modelId: string;
@@ -157,8 +149,8 @@ declare class OpenRouterCompletionLanguageModel implements LanguageModelV1 {
157
149
  constructor(modelId: OpenRouterCompletionModelId, settings: OpenRouterCompletionSettings, config: OpenRouterCompletionConfig);
158
150
  get provider(): string;
159
151
  private getArgs;
160
- doGenerate(options: Parameters<LanguageModelV1["doGenerate"]>[0]): Promise<Awaited<ReturnType<LanguageModelV1["doGenerate"]>>>;
161
- doStream(options: Parameters<LanguageModelV1["doStream"]>[0]): Promise<Awaited<ReturnType<LanguageModelV1["doStream"]>>>;
152
+ doGenerate(options: Parameters<LanguageModelV1['doGenerate']>[0]): Promise<Awaited<ReturnType<LanguageModelV1['doGenerate']>>>;
153
+ doStream(options: Parameters<LanguageModelV1['doStream']>[0]): Promise<Awaited<ReturnType<LanguageModelV1['doStream']>>>;
162
154
  }
163
155
 
164
156
  interface OpenRouterProvider {
@@ -197,7 +189,7 @@ interface OpenRouterProviderSettings {
197
189
  and `compatible` when using 3rd party providers. In `compatible` mode, newer
198
190
  information such as streamOptions are not being sent. Defaults to 'compatible'.
199
191
  */
200
- compatibility?: "strict" | "compatible";
192
+ compatibility?: 'strict' | 'compatible';
201
193
  /**
202
194
  Custom fetch implementation. You can use it as a middleware to intercept requests,
203
195
  or to provide a custom fetch implementation for e.g. testing.
@@ -244,6 +236,4 @@ declare class OpenRouter {
244
236
  completion(modelId: OpenRouterCompletionModelId, settings?: OpenRouterCompletionSettings): OpenRouterCompletionLanguageModel;
245
237
  }
246
238
 
247
- type OpenRouterLanguageModel = LanguageModelV1;
248
-
249
- export { OpenRouter, type OpenRouterCompletionSettings, type OpenRouterLanguageModel, type OpenRouterProvider, type OpenRouterProviderSettings, createOpenRouter, openrouter };
239
+ export { OpenRouter, type OpenRouterCompletionSettings, type OpenRouterLanguageModel, type OpenRouterProvider, type OpenRouterProviderOptions, type OpenRouterProviderSettings, type OpenRouterSharedSettings, createOpenRouter, openrouter };
package/dist/index.js CHANGED
@@ -135,6 +135,10 @@ function convertToOpenRouterChatMessages(prompt) {
135
135
  });
136
136
  break;
137
137
  }
138
+ // TODO: Handle reasoning and redacted-reasoning
139
+ case "reasoning":
140
+ case "redacted-reasoning":
141
+ break;
138
142
  default: {
139
143
  const _exhaustiveCheck = part;
140
144
  throw new Error(`Unsupported part: ${_exhaustiveCheck}`);
@@ -198,8 +202,8 @@ function mapOpenRouterFinishReason(finishReason) {
198
202
  }
199
203
 
200
204
  // src/openrouter-error.ts
201
- var import_zod = require("zod");
202
205
  var import_provider_utils2 = require("@ai-sdk/provider-utils");
206
+ var import_zod = require("zod");
203
207
  var openAIErrorDataSchema = import_zod.z.object({
204
208
  error: import_zod.z.object({
205
209
  message: import_zod.z.string(),
@@ -239,10 +243,13 @@ var OpenRouterChatLanguageModel = class {
239
243
  seed,
240
244
  stopSequences,
241
245
  responseFormat,
242
- topK
246
+ topK,
247
+ providerMetadata
243
248
  }) {
249
+ var _a;
244
250
  const type = mode.type;
245
- const baseArgs = __spreadValues(__spreadValues({
251
+ const extraCallingBody = (_a = providerMetadata == null ? void 0 : providerMetadata["openrouter"]) != null ? _a : {};
252
+ const baseArgs = __spreadValues(__spreadValues(__spreadValues({
246
253
  // model id:
247
254
  model: this.modelId,
248
255
  models: this.settings.models,
@@ -267,7 +274,7 @@ var OpenRouterChatLanguageModel = class {
267
274
  // OpenRouter specific settings:
268
275
  include_reasoning: this.settings.includeReasoning,
269
276
  reasoning: this.settings.reasoning
270
- }, this.config.extraBody), this.settings.extraBody);
277
+ }, this.config.extraBody), this.settings.extraBody), extraCallingBody);
271
278
  switch (type) {
272
279
  case "regular": {
273
280
  return __spreadValues(__spreadValues({}, baseArgs), prepareToolsAndToolChoice(mode));
@@ -572,7 +579,7 @@ var openrouterChatChunkSchema = import_zod2.z.union([
572
579
  delta: import_zod2.z.object({
573
580
  role: import_zod2.z.enum(["assistant"]).optional(),
574
581
  content: import_zod2.z.string().nullish(),
575
- reasoning: import_zod2.z.string().nullish(),
582
+ reasoning: import_zod2.z.string().nullish().optional(),
576
583
  tool_calls: import_zod2.z.array(
577
584
  import_zod2.z.object({
578
585
  index: import_zod2.z.number(),
@@ -711,7 +718,9 @@ function convertToOpenRouterCompletionPrompt({
711
718
  }
712
719
  default: {
713
720
  const _exhaustiveCheck = part;
714
- throw new Error(`Unsupported content type: ${_exhaustiveCheck}`);
721
+ throw new Error(
722
+ `Unsupported content type: ${_exhaustiveCheck}`
723
+ );
715
724
  }
716
725
  }
717
726
  }).join("");
@@ -732,9 +741,21 @@ ${userMessage}
732
741
  functionality: "tool-call messages"
733
742
  });
734
743
  }
744
+ case "reasoning": {
745
+ throw new import_provider2.UnsupportedFunctionalityError({
746
+ functionality: "reasoning messages"
747
+ });
748
+ }
749
+ case "redacted-reasoning": {
750
+ throw new import_provider2.UnsupportedFunctionalityError({
751
+ functionality: "redacted reasoning messages"
752
+ });
753
+ }
735
754
  default: {
736
755
  const _exhaustiveCheck = part;
737
- throw new Error(`Unsupported content type: ${_exhaustiveCheck}`);
756
+ throw new Error(
757
+ `Unsupported content type: ${_exhaustiveCheck}`
758
+ );
738
759
  }
739
760
  }
740
761
  }).join("");
@@ -803,17 +824,21 @@ var OpenRouterCompletionLanguageModel = class {
803
824
  seed,
804
825
  responseFormat,
805
826
  topK,
806
- stopSequences
827
+ stopSequences,
828
+ providerMetadata
807
829
  }) {
808
- var _a;
830
+ var _a, _b;
809
831
  const type = mode.type;
810
- const { prompt: completionPrompt } = convertToOpenRouterCompletionPrompt({ prompt, inputFormat });
811
- const baseArgs = __spreadValues(__spreadValues({
832
+ const extraCallingBody = (_a = providerMetadata == null ? void 0 : providerMetadata["openrouter"]) != null ? _a : {};
833
+ const { prompt: completionPrompt } = convertToOpenRouterCompletionPrompt({
834
+ prompt,
835
+ inputFormat
836
+ });
837
+ const baseArgs = __spreadValues(__spreadValues(__spreadValues({
812
838
  // model id:
813
839
  model: this.modelId,
814
840
  models: this.settings.models,
815
841
  // model specific settings:
816
- echo: this.settings.echo,
817
842
  logit_bias: this.settings.logitBias,
818
843
  logprobs: typeof this.settings.logprobs === "number" ? this.settings.logprobs : typeof this.settings.logprobs === "boolean" ? this.settings.logprobs ? 0 : void 0 : void 0,
819
844
  suffix: this.settings.suffix,
@@ -833,10 +858,10 @@ var OpenRouterCompletionLanguageModel = class {
833
858
  // OpenRouter specific settings:
834
859
  include_reasoning: this.settings.includeReasoning,
835
860
  reasoning: this.settings.reasoning
836
- }, this.config.extraBody), this.settings.extraBody);
861
+ }, this.config.extraBody), this.settings.extraBody), extraCallingBody);
837
862
  switch (type) {
838
863
  case "regular": {
839
- if ((_a = mode.tools) == null ? void 0 : _a.length) {
864
+ if ((_b = mode.tools) == null ? void 0 : _b.length) {
840
865
  throw new import_provider3.UnsupportedFunctionalityError({
841
866
  functionality: "tools"
842
867
  });
@@ -988,7 +1013,7 @@ var openAICompletionResponseSchema = import_zod3.z.object({
988
1013
  choices: import_zod3.z.array(
989
1014
  import_zod3.z.object({
990
1015
  text: import_zod3.z.string(),
991
- reasoning: import_zod3.z.string().nullable(),
1016
+ reasoning: import_zod3.z.string().nullish().optional(),
992
1017
  finish_reason: import_zod3.z.string(),
993
1018
  logprobs: import_zod3.z.object({
994
1019
  tokens: import_zod3.z.array(import_zod3.z.string()),