@ai-sdk/openai-compatible 2.0.84 → 2.0.86

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
@@ -581,7 +581,6 @@ var OpenAICompatibleChatLanguageModel = class {
581
581
  });
582
582
  }
583
583
  const content = [];
584
- content.push(...convertOpenAICompatibleContent(choice.message.content));
585
584
  const reasoning = (_a = choice.message.reasoning_content) != null ? _a : choice.message.reasoning;
586
585
  if (reasoning != null && reasoning.length > 0) {
587
586
  content.push({
@@ -589,6 +588,7 @@ var OpenAICompatibleChatLanguageModel = class {
589
588
  text: reasoning
590
589
  });
591
590
  }
591
+ content.push(...convertOpenAICompatibleContent(choice.message.content));
592
592
  if (choice.message.tool_calls != null) {
593
593
  for (const toolCall of choice.message.tool_calls) {
594
594
  const thoughtSignature = (_c = (_b = toolCall.extra_content) == null ? void 0 : _b.google) == null ? void 0 : _c.thought_signature;
@@ -1578,13 +1578,19 @@ var openaiTextEmbeddingResponseSchema = z7.object({
1578
1578
  });
1579
1579
 
1580
1580
  // src/image/openai-compatible-image-model.ts
1581
+ import {
1582
+ InvalidResponseDataError as InvalidResponseDataError2
1583
+ } from "@ai-sdk/provider";
1581
1584
  import {
1582
1585
  combineHeaders as combineHeaders4,
1583
1586
  convertBase64ToUint8Array as convertBase64ToUint8Array2,
1584
1587
  convertToFormData,
1588
+ createBinaryResponseHandler,
1585
1589
  createJsonErrorResponseHandler as createJsonErrorResponseHandler4,
1586
1590
  createJsonResponseHandler as createJsonResponseHandler4,
1591
+ createStatusCodeErrorResponseHandler,
1587
1592
  downloadBlob,
1593
+ getFromApi,
1588
1594
  postFormDataToApi,
1589
1595
  postJsonToApi as postJsonToApi4
1590
1596
  } from "@ai-sdk/provider-utils";
@@ -1638,13 +1644,16 @@ var OpenAICompatibleImageModel = class {
1638
1644
  }
1639
1645
  const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
1640
1646
  const args = this.getArgs(providerOptions);
1641
- if (files != null && files.length > 0) {
1647
+ const isEditing = files != null && files.length > 0;
1648
+ const url = this.config.url({
1649
+ path: isEditing ? "/images/edits" : "/images/generations",
1650
+ modelId: this.modelId
1651
+ });
1652
+ const requestHeaders = combineHeaders4(this.config.headers(), headers);
1653
+ if (isEditing) {
1642
1654
  const { value: response2, responseHeaders: responseHeaders2 } = await postFormDataToApi({
1643
- url: this.config.url({
1644
- path: "/images/edits",
1645
- modelId: this.modelId
1646
- }),
1647
- headers: combineHeaders4(this.config.headers(), headers),
1655
+ url,
1656
+ headers: requestHeaders,
1648
1657
  formData: convertToFormData({
1649
1658
  model: this.modelId,
1650
1659
  prompt,
@@ -1664,7 +1673,11 @@ var OpenAICompatibleImageModel = class {
1664
1673
  fetch: this.config.fetch
1665
1674
  });
1666
1675
  return {
1667
- images: response2.data.map((item) => item.b64_json),
1676
+ images: await this.getImages(response2.data, {
1677
+ url,
1678
+ headers: requestHeaders,
1679
+ abortSignal
1680
+ }),
1668
1681
  warnings,
1669
1682
  response: {
1670
1683
  timestamp: currentDate,
@@ -1674,11 +1687,8 @@ var OpenAICompatibleImageModel = class {
1674
1687
  };
1675
1688
  }
1676
1689
  const { value: response, responseHeaders } = await postJsonToApi4({
1677
- url: this.config.url({
1678
- path: "/images/generations",
1679
- modelId: this.modelId
1680
- }),
1681
- headers: combineHeaders4(this.config.headers(), headers),
1690
+ url,
1691
+ headers: requestHeaders,
1682
1692
  body: {
1683
1693
  model: this.modelId,
1684
1694
  prompt,
@@ -1697,7 +1707,11 @@ var OpenAICompatibleImageModel = class {
1697
1707
  fetch: this.config.fetch
1698
1708
  });
1699
1709
  return {
1700
- images: response.data.map((item) => item.b64_json),
1710
+ images: await this.getImages(response.data, {
1711
+ url,
1712
+ headers: requestHeaders,
1713
+ abortSignal
1714
+ }),
1701
1715
  warnings,
1702
1716
  response: {
1703
1717
  timestamp: currentDate,
@@ -1706,9 +1720,46 @@ var OpenAICompatibleImageModel = class {
1706
1720
  }
1707
1721
  };
1708
1722
  }
1723
+ async getImages(data, options) {
1724
+ if (data.every((image) => image.b64_json != null)) {
1725
+ return data.map((image) => image.b64_json);
1726
+ }
1727
+ return Promise.all(
1728
+ data.map(async (image) => {
1729
+ if (image.b64_json != null) {
1730
+ return convertBase64ToUint8Array2(image.b64_json);
1731
+ }
1732
+ if (image.url == null) {
1733
+ throw new InvalidResponseDataError2({
1734
+ data: image,
1735
+ message: "Expected url in the image response"
1736
+ });
1737
+ }
1738
+ const { value } = await getFromApi({
1739
+ url: image.url,
1740
+ validateUrl: true,
1741
+ trustedOrigin: options.url,
1742
+ credentialedOrigin: options.url,
1743
+ headers: options.headers,
1744
+ abortSignal: options.abortSignal,
1745
+ fetch: this.config.fetch,
1746
+ successfulResponseHandler: createBinaryResponseHandler(),
1747
+ failedResponseHandler: createStatusCodeErrorResponseHandler()
1748
+ });
1749
+ return value;
1750
+ })
1751
+ );
1752
+ }
1709
1753
  };
1710
1754
  var openaiCompatibleImageResponseSchema = z8.object({
1711
- data: z8.array(z8.object({ b64_json: z8.string() }))
1755
+ data: z8.array(
1756
+ z8.object({
1757
+ b64_json: z8.string().nullish(),
1758
+ url: z8.string().nullish()
1759
+ }).refine((image) => image.b64_json != null || image.url != null, {
1760
+ message: "Expected b64_json or url in the image response"
1761
+ })
1762
+ )
1712
1763
  });
1713
1764
  async function fileToBlob(file) {
1714
1765
  if (file.type === "url") {
@@ -1725,7 +1776,7 @@ import {
1725
1776
  } from "@ai-sdk/provider-utils";
1726
1777
 
1727
1778
  // src/version.ts
1728
- var VERSION = true ? "2.0.84" : "0.0.0-test";
1779
+ var VERSION = true ? "2.0.86" : "0.0.0-test";
1729
1780
 
1730
1781
  // src/openai-compatible-provider.ts
1731
1782
  function createOpenAICompatible(options) {