@ai-sdk/google 3.0.25 → 3.0.27

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
@@ -7,7 +7,7 @@ import {
7
7
  } from "@ai-sdk/provider-utils";
8
8
 
9
9
  // src/version.ts
10
- var VERSION = true ? "3.0.25" : "0.0.0-test";
10
+ var VERSION = true ? "3.0.27" : "0.0.0-test";
11
11
 
12
12
  // src/google-generative-ai-embedding-model.ts
13
13
  import {
@@ -425,8 +425,8 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
425
425
  contents.push({
426
426
  role: "model",
427
427
  parts: content.map((part) => {
428
- var _a2;
429
- const providerOpts = (_a2 = part.providerOptions) == null ? void 0 : _a2[providerOptionsName];
428
+ var _a2, _b2, _c2;
429
+ const providerOpts = (_c2 = (_a2 = part.providerOptions) == null ? void 0 : _a2[providerOptionsName]) != null ? _c2 : providerOptionsName !== "google" ? (_b2 = part.providerOptions) == null ? void 0 : _b2.google : void 0;
430
430
  const thoughtSignature = (providerOpts == null ? void 0 : providerOpts.thoughtSignature) != null ? String(providerOpts.thoughtSignature) : void 0;
431
431
  switch (part.type) {
432
432
  case "text": {
@@ -1806,7 +1806,9 @@ var googleTools = {
1806
1806
  // src/google-generative-ai-image-model.ts
1807
1807
  import {
1808
1808
  combineHeaders as combineHeaders3,
1809
+ convertToBase64 as convertToBase642,
1809
1810
  createJsonResponseHandler as createJsonResponseHandler3,
1811
+ generateId as defaultGenerateId,
1810
1812
  lazySchema as lazySchema11,
1811
1813
  parseProviderOptions as parseProviderOptions3,
1812
1814
  postJsonToApi as postJsonToApi3,
@@ -1822,13 +1824,24 @@ var GoogleGenerativeAIImageModel = class {
1822
1824
  this.specificationVersion = "v3";
1823
1825
  }
1824
1826
  get maxImagesPerCall() {
1825
- var _a;
1826
- return (_a = this.settings.maxImagesPerCall) != null ? _a : 4;
1827
+ if (this.settings.maxImagesPerCall != null) {
1828
+ return this.settings.maxImagesPerCall;
1829
+ }
1830
+ if (isGeminiModel(this.modelId)) {
1831
+ return 10;
1832
+ }
1833
+ return 4;
1827
1834
  }
1828
1835
  get provider() {
1829
1836
  return this.config.provider;
1830
1837
  }
1831
1838
  async doGenerate(options) {
1839
+ if (isGeminiModel(this.modelId)) {
1840
+ return this.doGenerateGemini(options);
1841
+ }
1842
+ return this.doGenerateImagen(options);
1843
+ }
1844
+ async doGenerateImagen(options) {
1832
1845
  var _a, _b, _c;
1833
1846
  const {
1834
1847
  prompt,
@@ -1845,7 +1858,7 @@ var GoogleGenerativeAIImageModel = class {
1845
1858
  const warnings = [];
1846
1859
  if (files != null && files.length > 0) {
1847
1860
  throw new Error(
1848
- "Google Generative AI does not support image editing. Use Google Vertex AI (@ai-sdk/google-vertex) for image editing capabilities."
1861
+ "Google Generative AI does not support image editing with Imagen models. Use Google Vertex AI (@ai-sdk/google-vertex) for image editing capabilities."
1849
1862
  );
1850
1863
  }
1851
1864
  if (mask != null) {
@@ -1901,10 +1914,10 @@ var GoogleGenerativeAIImageModel = class {
1901
1914
  images: response.predictions.map(
1902
1915
  (p) => p.bytesBase64Encoded
1903
1916
  ),
1904
- warnings: warnings != null ? warnings : [],
1917
+ warnings,
1905
1918
  providerMetadata: {
1906
1919
  google: {
1907
- images: response.predictions.map((prediction) => ({
1920
+ images: response.predictions.map(() => ({
1908
1921
  // Add any prediction-specific metadata here
1909
1922
  }))
1910
1923
  }
@@ -1916,7 +1929,113 @@ var GoogleGenerativeAIImageModel = class {
1916
1929
  }
1917
1930
  };
1918
1931
  }
1932
+ async doGenerateGemini(options) {
1933
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i;
1934
+ const {
1935
+ prompt,
1936
+ n,
1937
+ size,
1938
+ aspectRatio,
1939
+ seed,
1940
+ providerOptions,
1941
+ headers,
1942
+ abortSignal,
1943
+ files,
1944
+ mask
1945
+ } = options;
1946
+ const warnings = [];
1947
+ if (mask != null) {
1948
+ throw new Error(
1949
+ "Gemini image models do not support mask-based image editing."
1950
+ );
1951
+ }
1952
+ if (n != null && n > 1) {
1953
+ throw new Error(
1954
+ "Gemini image models do not support generating a set number of images per call. Use n=1 or omit the n parameter."
1955
+ );
1956
+ }
1957
+ if (size != null) {
1958
+ warnings.push({
1959
+ type: "unsupported",
1960
+ feature: "size",
1961
+ details: "This model does not support the `size` option. Use `aspectRatio` instead."
1962
+ });
1963
+ }
1964
+ const userContent = [];
1965
+ if (prompt != null) {
1966
+ userContent.push({ type: "text", text: prompt });
1967
+ }
1968
+ if (files != null && files.length > 0) {
1969
+ for (const file of files) {
1970
+ if (file.type === "url") {
1971
+ userContent.push({
1972
+ type: "file",
1973
+ data: new URL(file.url),
1974
+ mediaType: "image/*"
1975
+ });
1976
+ } else {
1977
+ userContent.push({
1978
+ type: "file",
1979
+ data: typeof file.data === "string" ? file.data : new Uint8Array(file.data),
1980
+ mediaType: file.mediaType
1981
+ });
1982
+ }
1983
+ }
1984
+ }
1985
+ const languageModelPrompt = [
1986
+ { role: "user", content: userContent }
1987
+ ];
1988
+ const languageModel = new GoogleGenerativeAILanguageModel(this.modelId, {
1989
+ provider: this.config.provider,
1990
+ baseURL: this.config.baseURL,
1991
+ headers: (_a = this.config.headers) != null ? _a : {},
1992
+ fetch: this.config.fetch,
1993
+ generateId: (_b = this.config.generateId) != null ? _b : defaultGenerateId
1994
+ });
1995
+ const result = await languageModel.doGenerate({
1996
+ prompt: languageModelPrompt,
1997
+ seed,
1998
+ providerOptions: {
1999
+ google: {
2000
+ responseModalities: ["IMAGE"],
2001
+ imageConfig: aspectRatio ? { aspectRatio } : void 0,
2002
+ ...(_c = providerOptions == null ? void 0 : providerOptions.google) != null ? _c : {}
2003
+ }
2004
+ },
2005
+ headers,
2006
+ abortSignal
2007
+ });
2008
+ const currentDate = (_f = (_e = (_d = this.config._internal) == null ? void 0 : _d.currentDate) == null ? void 0 : _e.call(_d)) != null ? _f : /* @__PURE__ */ new Date();
2009
+ const images = [];
2010
+ for (const part of result.content) {
2011
+ if (part.type === "file" && part.mediaType.startsWith("image/")) {
2012
+ images.push(convertToBase642(part.data));
2013
+ }
2014
+ }
2015
+ return {
2016
+ images,
2017
+ warnings,
2018
+ providerMetadata: {
2019
+ google: {
2020
+ images: images.map(() => ({}))
2021
+ }
2022
+ },
2023
+ response: {
2024
+ timestamp: currentDate,
2025
+ modelId: this.modelId,
2026
+ headers: (_g = result.response) == null ? void 0 : _g.headers
2027
+ },
2028
+ usage: result.usage ? {
2029
+ inputTokens: result.usage.inputTokens.total,
2030
+ outputTokens: result.usage.outputTokens.total,
2031
+ totalTokens: ((_h = result.usage.inputTokens.total) != null ? _h : 0) + ((_i = result.usage.outputTokens.total) != null ? _i : 0)
2032
+ } : void 0
2033
+ };
2034
+ }
1919
2035
  };
2036
+ function isGeminiModel(modelId) {
2037
+ return modelId.startsWith("gemini-");
2038
+ }
1920
2039
  var googleImageResponseSchema = lazySchema11(
1921
2040
  () => zodSchema11(
1922
2041
  z13.object({