@llmgateway/ai-sdk-provider 3.1.1 → 3.3.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.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { LanguageModelV2, LanguageModelV2CallOptions, LanguageModelV2Content, LanguageModelV2FinishReason, LanguageModelV2Usage, LanguageModelV2CallWarning, LanguageModelV2ResponseMetadata, SharedV2Headers, LanguageModelV2StreamPart } from '@ai-sdk/provider';
1
+ import { LanguageModelV2, LanguageModelV2CallOptions, LanguageModelV2Content, LanguageModelV2FinishReason, LanguageModelV2Usage, LanguageModelV2CallWarning, LanguageModelV2ResponseMetadata, SharedV2Headers, LanguageModelV2StreamPart, ImageModelV3, ImageModelV3CallOptions, SharedV3Warning } from '@ai-sdk/provider';
2
2
  export { LanguageModelV2, LanguageModelV2Prompt } from '@ai-sdk/provider';
3
3
  import { models, Provider } from '@llmgateway/models';
4
4
 
@@ -132,6 +132,9 @@ type LLMGatewayCompletionSettings = {
132
132
  suffix?: string;
133
133
  } & LLMGatewaySharedSettings;
134
134
 
135
+ type LLMGatewayImageModelId = string;
136
+ type LLMGatewayImageSettings = {};
137
+
135
138
  type LLMGatewayChatConfig = {
136
139
  provider: string;
137
140
  compatibility: 'strict' | 'compatible';
@@ -209,6 +212,34 @@ declare class LLMGatewayCompletionLanguageModel implements LanguageModelV2 {
209
212
  doStream(options: LanguageModelV2CallOptions): Promise<Awaited<ReturnType<LanguageModelV2['doStream']>>>;
210
213
  }
211
214
 
215
+ type LLMGatewayImageConfig = {
216
+ provider: string;
217
+ headers: () => Record<string, string | undefined>;
218
+ url: (options: {
219
+ modelId: string;
220
+ path: string;
221
+ }) => string;
222
+ fetch?: typeof fetch;
223
+ };
224
+ declare class LLMGatewayImageModel implements ImageModelV3 {
225
+ readonly specificationVersion: "v3";
226
+ readonly provider: string;
227
+ readonly modelId: LLMGatewayImageModelId;
228
+ readonly maxImagesPerCall: number | undefined;
229
+ readonly settings: LLMGatewayImageSettings;
230
+ private readonly config;
231
+ constructor(modelId: LLMGatewayImageModelId, settings: LLMGatewayImageSettings, config: LLMGatewayImageConfig);
232
+ doGenerate(options: ImageModelV3CallOptions): Promise<{
233
+ images: Array<string>;
234
+ warnings: Array<SharedV3Warning>;
235
+ response: {
236
+ timestamp: Date;
237
+ modelId: string;
238
+ headers: Record<string, string> | undefined;
239
+ };
240
+ }>;
241
+ }
242
+
212
243
  interface LLMGatewayProvider extends LanguageModelV2 {
213
244
  (modelId: LLMGatewayChatModelId, settings?: LLMGatewayCompletionSettings): LLMGatewayCompletionLanguageModel;
214
245
  (modelId: LLMGatewayChatModelId, settings?: LLMGatewayChatSettings): LLMGatewayChatLanguageModel;
@@ -222,6 +253,14 @@ interface LLMGatewayProvider extends LanguageModelV2 {
222
253
  Creates an LLMGateway completion model for text generation.
223
254
  */
224
255
  completion(modelId: LLMGatewayChatModelId, settings?: LLMGatewayCompletionSettings): LLMGatewayCompletionLanguageModel;
256
+ /**
257
+ Creates an LLMGateway image model for image generation.
258
+ */
259
+ image(modelId: LLMGatewayImageModelId, settings?: LLMGatewayImageSettings): LLMGatewayImageModel;
260
+ /**
261
+ Creates an LLMGateway image model for image generation.
262
+ */
263
+ imageModel(modelId: LLMGatewayImageModelId, settings?: LLMGatewayImageSettings): ImageModelV3;
225
264
  }
226
265
  interface LLMGatewayProviderSettings {
227
266
  /**
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { LanguageModelV2, LanguageModelV2CallOptions, LanguageModelV2Content, LanguageModelV2FinishReason, LanguageModelV2Usage, LanguageModelV2CallWarning, LanguageModelV2ResponseMetadata, SharedV2Headers, LanguageModelV2StreamPart } from '@ai-sdk/provider';
1
+ import { LanguageModelV2, LanguageModelV2CallOptions, LanguageModelV2Content, LanguageModelV2FinishReason, LanguageModelV2Usage, LanguageModelV2CallWarning, LanguageModelV2ResponseMetadata, SharedV2Headers, LanguageModelV2StreamPart, ImageModelV3, ImageModelV3CallOptions, SharedV3Warning } from '@ai-sdk/provider';
2
2
  export { LanguageModelV2, LanguageModelV2Prompt } from '@ai-sdk/provider';
3
3
  import { models, Provider } from '@llmgateway/models';
4
4
 
@@ -132,6 +132,9 @@ type LLMGatewayCompletionSettings = {
132
132
  suffix?: string;
133
133
  } & LLMGatewaySharedSettings;
134
134
 
135
+ type LLMGatewayImageModelId = string;
136
+ type LLMGatewayImageSettings = {};
137
+
135
138
  type LLMGatewayChatConfig = {
136
139
  provider: string;
137
140
  compatibility: 'strict' | 'compatible';
@@ -209,6 +212,34 @@ declare class LLMGatewayCompletionLanguageModel implements LanguageModelV2 {
209
212
  doStream(options: LanguageModelV2CallOptions): Promise<Awaited<ReturnType<LanguageModelV2['doStream']>>>;
210
213
  }
211
214
 
215
+ type LLMGatewayImageConfig = {
216
+ provider: string;
217
+ headers: () => Record<string, string | undefined>;
218
+ url: (options: {
219
+ modelId: string;
220
+ path: string;
221
+ }) => string;
222
+ fetch?: typeof fetch;
223
+ };
224
+ declare class LLMGatewayImageModel implements ImageModelV3 {
225
+ readonly specificationVersion: "v3";
226
+ readonly provider: string;
227
+ readonly modelId: LLMGatewayImageModelId;
228
+ readonly maxImagesPerCall: number | undefined;
229
+ readonly settings: LLMGatewayImageSettings;
230
+ private readonly config;
231
+ constructor(modelId: LLMGatewayImageModelId, settings: LLMGatewayImageSettings, config: LLMGatewayImageConfig);
232
+ doGenerate(options: ImageModelV3CallOptions): Promise<{
233
+ images: Array<string>;
234
+ warnings: Array<SharedV3Warning>;
235
+ response: {
236
+ timestamp: Date;
237
+ modelId: string;
238
+ headers: Record<string, string> | undefined;
239
+ };
240
+ }>;
241
+ }
242
+
212
243
  interface LLMGatewayProvider extends LanguageModelV2 {
213
244
  (modelId: LLMGatewayChatModelId, settings?: LLMGatewayCompletionSettings): LLMGatewayCompletionLanguageModel;
214
245
  (modelId: LLMGatewayChatModelId, settings?: LLMGatewayChatSettings): LLMGatewayChatLanguageModel;
@@ -222,6 +253,14 @@ interface LLMGatewayProvider extends LanguageModelV2 {
222
253
  Creates an LLMGateway completion model for text generation.
223
254
  */
224
255
  completion(modelId: LLMGatewayChatModelId, settings?: LLMGatewayCompletionSettings): LLMGatewayCompletionLanguageModel;
256
+ /**
257
+ Creates an LLMGateway image model for image generation.
258
+ */
259
+ image(modelId: LLMGatewayImageModelId, settings?: LLMGatewayImageSettings): LLMGatewayImageModel;
260
+ /**
261
+ Creates an LLMGateway image model for image generation.
262
+ */
263
+ imageModel(modelId: LLMGatewayImageModelId, settings?: LLMGatewayImageSettings): ImageModelV3;
225
264
  }
226
265
  interface LLMGatewayProviderSettings {
227
266
  /**
package/dist/index.js CHANGED
@@ -2548,7 +2548,10 @@ var LLMGatewayChatCompletionBaseResponseSchema = import_v45.z.object({
2548
2548
  reasoning_tokens: import_v45.z.number()
2549
2549
  }).nullish(),
2550
2550
  total_tokens: import_v45.z.number(),
2551
- cost: import_v45.z.number().optional(),
2551
+ cost: import_v45.z.union([
2552
+ import_v45.z.number(),
2553
+ import_v45.z.object({ total_cost: import_v45.z.number() }).passthrough()
2554
+ ]).optional(),
2552
2555
  cost_details: import_v45.z.object({
2553
2556
  upstream_inference_cost: import_v45.z.number().nullish()
2554
2557
  }).nullish()
@@ -2743,7 +2746,7 @@ var LLMGatewayChatLanguageModel = class {
2743
2746
  return baseArgs;
2744
2747
  }
2745
2748
  async doGenerate(options) {
2746
- var _a16, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x;
2749
+ var _a16, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z;
2747
2750
  const providerOptions = options.providerOptions || {};
2748
2751
  const llmgatewayOptions = providerOptions.llmgateway || {};
2749
2752
  const args = __spreadValues(__spreadValues({}, this.getArgs(options)), llmgatewayOptions);
@@ -2879,15 +2882,15 @@ var LLMGatewayChatLanguageModel = class {
2879
2882
  promptTokens: (_l = usageInfo.inputTokens) != null ? _l : 0,
2880
2883
  completionTokens: (_m = usageInfo.outputTokens) != null ? _m : 0,
2881
2884
  totalTokens: (_n = usageInfo.totalTokens) != null ? _n : 0,
2882
- cost: (_o = response.usage) == null ? void 0 : _o.cost,
2885
+ cost: typeof ((_o = response.usage) == null ? void 0 : _o.cost) === "number" ? response.usage.cost : (_q = (_p = response.usage) == null ? void 0 : _p.cost) == null ? void 0 : _q.total_cost,
2883
2886
  promptTokensDetails: {
2884
- cachedTokens: (_r = (_q = (_p = response.usage) == null ? void 0 : _p.prompt_tokens_details) == null ? void 0 : _q.cached_tokens) != null ? _r : 0
2887
+ cachedTokens: (_t = (_s = (_r = response.usage) == null ? void 0 : _r.prompt_tokens_details) == null ? void 0 : _s.cached_tokens) != null ? _t : 0
2885
2888
  },
2886
2889
  completionTokensDetails: {
2887
- reasoningTokens: (_u = (_t = (_s = response.usage) == null ? void 0 : _s.completion_tokens_details) == null ? void 0 : _t.reasoning_tokens) != null ? _u : 0
2890
+ reasoningTokens: (_w = (_v = (_u = response.usage) == null ? void 0 : _u.completion_tokens_details) == null ? void 0 : _v.reasoning_tokens) != null ? _w : 0
2888
2891
  },
2889
2892
  costDetails: {
2890
- upstreamInferenceCost: (_x = (_w = (_v = response.usage) == null ? void 0 : _v.cost_details) == null ? void 0 : _w.upstream_inference_cost) != null ? _x : 0
2893
+ upstreamInferenceCost: (_z = (_y = (_x = response.usage) == null ? void 0 : _x.cost_details) == null ? void 0 : _y.upstream_inference_cost) != null ? _z : 0
2891
2894
  }
2892
2895
  }
2893
2896
  }
@@ -2941,7 +2944,7 @@ var LLMGatewayChatLanguageModel = class {
2941
2944
  stream: response.pipeThrough(
2942
2945
  new TransformStream({
2943
2946
  transform(chunk, controller) {
2944
- var _a16, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p;
2947
+ var _a16, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q;
2945
2948
  if (!chunk.success) {
2946
2949
  finishReason = "error";
2947
2950
  controller.enqueue({ type: "error", error: chunk.error });
@@ -2986,7 +2989,7 @@ var LLMGatewayChatLanguageModel = class {
2986
2989
  reasoningTokens
2987
2990
  };
2988
2991
  }
2989
- llmgatewayUsage.cost = value.usage.cost;
2992
+ llmgatewayUsage.cost = typeof value.usage.cost === "number" ? value.usage.cost : (_c = value.usage.cost) == null ? void 0 : _c.total_cost;
2990
2993
  llmgatewayUsage.totalTokens = value.usage.total_tokens;
2991
2994
  }
2992
2995
  const choice = value.choices[0];
@@ -3041,7 +3044,7 @@ var LLMGatewayChatLanguageModel = class {
3041
3044
  }
3042
3045
  } else if (delta.reasoningText != null || "reasoning" in delta && typeof delta.reasoning === "string") {
3043
3046
  emitReasoningChunk(
3044
- (_d = (_c = delta.reasoningText) != null ? _c : "reasoning" in delta && typeof delta.reasoning === "string" ? delta.reasoning : void 0) != null ? _d : ""
3047
+ (_e = (_d = delta.reasoningText) != null ? _d : "reasoning" in delta && typeof delta.reasoning === "string" ? delta.reasoning : void 0) != null ? _e : ""
3045
3048
  );
3046
3049
  }
3047
3050
  if (delta.content != null) {
@@ -3061,7 +3064,7 @@ var LLMGatewayChatLanguageModel = class {
3061
3064
  }
3062
3065
  if (delta.tool_calls != null) {
3063
3066
  for (const toolCallDelta of delta.tool_calls) {
3064
- const index = (_e = toolCallDelta.index) != null ? _e : toolCalls.length - 1;
3067
+ const index = (_f = toolCallDelta.index) != null ? _f : toolCalls.length - 1;
3065
3068
  if (toolCalls[index] == null) {
3066
3069
  if (toolCallDelta.type !== "function") {
3067
3070
  throw new InvalidResponseDataError({
@@ -3075,7 +3078,7 @@ var LLMGatewayChatLanguageModel = class {
3075
3078
  message: `Expected 'id' to be a string.`
3076
3079
  });
3077
3080
  }
3078
- if (((_f = toolCallDelta.function) == null ? void 0 : _f.name) == null) {
3081
+ if (((_g = toolCallDelta.function) == null ? void 0 : _g.name) == null) {
3079
3082
  throw new InvalidResponseDataError({
3080
3083
  data: toolCallDelta,
3081
3084
  message: `Expected 'function.name' to be a string.`
@@ -3086,7 +3089,7 @@ var LLMGatewayChatLanguageModel = class {
3086
3089
  type: "function",
3087
3090
  function: {
3088
3091
  name: toolCallDelta.function.name,
3089
- arguments: (_g = toolCallDelta.function.arguments) != null ? _g : ""
3092
+ arguments: (_h = toolCallDelta.function.arguments) != null ? _h : ""
3090
3093
  },
3091
3094
  inputStarted: false,
3092
3095
  sent: false
@@ -3095,7 +3098,7 @@ var LLMGatewayChatLanguageModel = class {
3095
3098
  if (toolCall2 == null) {
3096
3099
  throw new Error("Tool call is missing");
3097
3100
  }
3098
- if (((_h = toolCall2.function) == null ? void 0 : _h.name) != null && ((_i = toolCall2.function) == null ? void 0 : _i.arguments) != null && isParsableJson(toolCall2.function.arguments)) {
3101
+ if (((_i = toolCall2.function) == null ? void 0 : _i.name) != null && ((_j = toolCall2.function) == null ? void 0 : _j.arguments) != null && isParsableJson(toolCall2.function.arguments)) {
3099
3102
  toolCall2.inputStarted = true;
3100
3103
  controller.enqueue({
3101
3104
  type: "tool-input-start",
@@ -3133,18 +3136,18 @@ var LLMGatewayChatLanguageModel = class {
3133
3136
  toolName: toolCall.function.name
3134
3137
  });
3135
3138
  }
3136
- if (((_j = toolCallDelta.function) == null ? void 0 : _j.arguments) != null) {
3137
- toolCall.function.arguments += (_l = (_k = toolCallDelta.function) == null ? void 0 : _k.arguments) != null ? _l : "";
3139
+ if (((_k = toolCallDelta.function) == null ? void 0 : _k.arguments) != null) {
3140
+ toolCall.function.arguments += (_m = (_l = toolCallDelta.function) == null ? void 0 : _l.arguments) != null ? _m : "";
3138
3141
  }
3139
3142
  controller.enqueue({
3140
3143
  type: "tool-input-delta",
3141
3144
  id: toolCall.id,
3142
- delta: (_m = toolCallDelta.function.arguments) != null ? _m : ""
3145
+ delta: (_n = toolCallDelta.function.arguments) != null ? _n : ""
3143
3146
  });
3144
- if (((_n = toolCall.function) == null ? void 0 : _n.name) != null && ((_o = toolCall.function) == null ? void 0 : _o.arguments) != null && isParsableJson(toolCall.function.arguments)) {
3147
+ if (((_o = toolCall.function) == null ? void 0 : _o.name) != null && ((_p = toolCall.function) == null ? void 0 : _p.arguments) != null && isParsableJson(toolCall.function.arguments)) {
3145
3148
  controller.enqueue({
3146
3149
  type: "tool-call",
3147
- toolCallId: (_p = toolCall.id) != null ? _p : generateId(),
3150
+ toolCallId: (_q = toolCall.id) != null ? _q : generateId(),
3148
3151
  toolName: toolCall.function.name,
3149
3152
  input: toolCall.function.arguments
3150
3153
  });
@@ -3343,7 +3346,10 @@ var LLMGatewayCompletionChunkSchema = import_v46.z.union([
3343
3346
  reasoning_tokens: import_v46.z.number()
3344
3347
  }).nullish(),
3345
3348
  total_tokens: import_v46.z.number(),
3346
- cost: import_v46.z.number().optional()
3349
+ cost: import_v46.z.union([
3350
+ import_v46.z.number(),
3351
+ import_v46.z.object({ total_cost: import_v46.z.number() }).passthrough()
3352
+ ]).optional()
3347
3353
  }).nullish()
3348
3354
  }),
3349
3355
  LLMGatewayErrorResponseSchema
@@ -3503,7 +3509,7 @@ var LLMGatewayCompletionLanguageModel = class {
3503
3509
  stream: response.pipeThrough(
3504
3510
  new TransformStream({
3505
3511
  transform(chunk, controller) {
3506
- var _a16, _b16;
3512
+ var _a16, _b16, _c;
3507
3513
  if (!chunk.success) {
3508
3514
  finishReason = "error";
3509
3515
  controller.enqueue({ type: "error", error: chunk.error });
@@ -3535,7 +3541,7 @@ var LLMGatewayCompletionLanguageModel = class {
3535
3541
  reasoningTokens
3536
3542
  };
3537
3543
  }
3538
- llmgatewayUsage.cost = value.usage.cost;
3544
+ llmgatewayUsage.cost = typeof value.usage.cost === "number" ? value.usage.cost : (_c = value.usage.cost) == null ? void 0 : _c.total_cost;
3539
3545
  llmgatewayUsage.totalTokens = value.usage.total_tokens;
3540
3546
  }
3541
3547
  const choice = value.choices[0];
@@ -3612,6 +3618,82 @@ var LLMGateway = class {
3612
3618
  }
3613
3619
  };
3614
3620
 
3621
+ // src/image/index.ts
3622
+ var import_v47 = require("zod/v4");
3623
+ var LLMGatewayImageResponseSchema = import_v47.z.object({
3624
+ data: import_v47.z.array(
3625
+ import_v47.z.object({
3626
+ b64_json: import_v47.z.string().optional(),
3627
+ url: import_v47.z.string().optional(),
3628
+ revised_prompt: import_v47.z.string().optional()
3629
+ })
3630
+ )
3631
+ });
3632
+ var LLMGatewayImageModel = class {
3633
+ constructor(modelId, settings, config) {
3634
+ this.specificationVersion = "v3";
3635
+ this.maxImagesPerCall = void 0;
3636
+ this.modelId = modelId;
3637
+ this.settings = settings;
3638
+ this.config = config;
3639
+ this.provider = config.provider;
3640
+ }
3641
+ async doGenerate(options) {
3642
+ const warnings = [];
3643
+ if (options.aspectRatio != null) {
3644
+ warnings.push({
3645
+ type: "unsupported",
3646
+ feature: "aspectRatio"
3647
+ });
3648
+ }
3649
+ if (options.seed != null) {
3650
+ warnings.push({
3651
+ type: "unsupported",
3652
+ feature: "seed"
3653
+ });
3654
+ }
3655
+ const body = {
3656
+ model: this.modelId,
3657
+ prompt: options.prompt,
3658
+ n: options.n,
3659
+ response_format: "b64_json"
3660
+ };
3661
+ if (options.size != null) {
3662
+ body.size = options.size;
3663
+ }
3664
+ const { value: response, responseHeaders } = await postJsonToApi({
3665
+ url: this.config.url({
3666
+ path: "/images/generations",
3667
+ modelId: this.modelId
3668
+ }),
3669
+ headers: combineHeaders(this.config.headers(), options.headers),
3670
+ body,
3671
+ failedResponseHandler: llmgatewayFailedResponseHandler,
3672
+ successfulResponseHandler: createJsonResponseHandler(
3673
+ LLMGatewayImageResponseSchema
3674
+ ),
3675
+ abortSignal: options.abortSignal,
3676
+ fetch: this.config.fetch
3677
+ });
3678
+ return {
3679
+ images: response.data.map((item) => {
3680
+ if (item.b64_json) {
3681
+ return item.b64_json;
3682
+ }
3683
+ throw new Error(
3684
+ "Expected b64_json in response but got url. Set response_format to b64_json."
3685
+ );
3686
+ }),
3687
+ warnings,
3688
+ response: {
3689
+ timestamp: /* @__PURE__ */ new Date(),
3690
+ modelId: this.modelId,
3691
+ headers: responseHeaders
3692
+ }
3693
+ };
3694
+ }
3695
+ };
3696
+
3615
3697
  // src/provider.ts
3616
3698
  function createLLMGateway(options = {}) {
3617
3699
  var _a16, _b16, _c, _d;
@@ -3640,6 +3722,12 @@ function createLLMGateway(options = {}) {
3640
3722
  fetch: options.fetch,
3641
3723
  extraBody: options.extraBody
3642
3724
  });
3725
+ const createImageModel = (modelId, settings = {}) => new LLMGatewayImageModel(modelId, settings, {
3726
+ provider: "llmgateway.image",
3727
+ url: ({ path }) => `${baseURL}${path}`,
3728
+ headers: getHeaders,
3729
+ fetch: options.fetch
3730
+ });
3643
3731
  const createLanguageModel = (modelId, settings) => {
3644
3732
  if (new.target) {
3645
3733
  throw new Error(
@@ -3658,6 +3746,8 @@ function createLLMGateway(options = {}) {
3658
3746
  provider.languageModel = createLanguageModel;
3659
3747
  provider.chat = createChatModel;
3660
3748
  provider.completion = createCompletionModel;
3749
+ provider.image = createImageModel;
3750
+ provider.imageModel = createImageModel;
3661
3751
  return provider;
3662
3752
  }
3663
3753
  var llmgateway = createLLMGateway({