@google/genai 1.3.0 → 1.5.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.mjs CHANGED
@@ -1,4 +1,5 @@
1
1
  import { z } from 'zod';
2
+ import { CallToolResultSchema } from '@modelcontextprotocol/sdk/types.js';
2
3
 
3
4
  /**
4
5
  * @license
@@ -1923,16 +1924,16 @@ function flattenTypeArrayToAnyOf(typeList, resultingSchema) {
1923
1924
  }
1924
1925
  const listWithoutNull = typeList.filter((type) => type !== 'null');
1925
1926
  if (listWithoutNull.length === 1) {
1926
- resultingSchema['type'] = Object.keys(Type).includes(listWithoutNull[0].toUpperCase())
1927
- ? Type[listWithoutNull[0].toUpperCase()]
1927
+ resultingSchema['type'] = Object.values(Type).includes(listWithoutNull[0].toUpperCase())
1928
+ ? listWithoutNull[0].toUpperCase()
1928
1929
  : Type.TYPE_UNSPECIFIED;
1929
1930
  }
1930
1931
  else {
1931
1932
  resultingSchema['anyOf'] = [];
1932
1933
  for (const i of listWithoutNull) {
1933
1934
  resultingSchema['anyOf'].push({
1934
- 'type': Object.keys(Type).includes(i.toUpperCase())
1935
- ? Type[i.toUpperCase()]
1935
+ 'type': Object.values(Type).includes(i.toUpperCase())
1936
+ ? i.toUpperCase()
1936
1937
  : Type.TYPE_UNSPECIFIED,
1937
1938
  });
1938
1939
  }
@@ -2016,7 +2017,7 @@ function processJsonSchema(_jsonSchema) {
2016
2017
  // beginning of this function.
2017
2018
  continue;
2018
2019
  }
2019
- genAISchema['type'] = Object.keys(Type).includes(fieldValue.toUpperCase())
2020
+ genAISchema['type'] = Object.values(Type).includes(fieldValue.toUpperCase())
2020
2021
  ? fieldValue.toUpperCase()
2021
2022
  : Type.TYPE_UNSPECIFIED;
2022
2023
  }
@@ -2375,7 +2376,7 @@ function filterToJsonSchema(schema) {
2375
2376
  }
2376
2377
  else if (fieldName === 'type') {
2377
2378
  const typeValue = fieldValue.toUpperCase();
2378
- filteredSchema[fieldName] = Object.keys(Type).includes(typeValue)
2379
+ filteredSchema[fieldName] = Object.values(Type).includes(typeValue)
2379
2380
  ? typeValue
2380
2381
  : Type.TYPE_UNSPECIFIED;
2381
2382
  }
@@ -3052,6 +3053,10 @@ function googleMapsToVertex$2(apiClient, fromObject) {
3052
3053
  }
3053
3054
  return toObject;
3054
3055
  }
3056
+ function urlContextToVertex$2() {
3057
+ const toObject = {};
3058
+ return toObject;
3059
+ }
3055
3060
  function toolToVertex$2(apiClient, fromObject) {
3056
3061
  const toObject = {};
3057
3062
  const fromFunctionDeclarations = getValueByPath(fromObject, [
@@ -3090,8 +3095,9 @@ function toolToVertex$2(apiClient, fromObject) {
3090
3095
  if (fromGoogleMaps != null) {
3091
3096
  setValueByPath(toObject, ['googleMaps'], googleMapsToVertex$2(apiClient, fromGoogleMaps));
3092
3097
  }
3093
- if (getValueByPath(fromObject, ['urlContext']) !== undefined) {
3094
- throw new Error('urlContext parameter is not supported in Vertex AI.');
3098
+ const fromUrlContext = getValueByPath(fromObject, ['urlContext']);
3099
+ if (fromUrlContext != null) {
3100
+ setValueByPath(toObject, ['urlContext'], urlContextToVertex$2());
3095
3101
  }
3096
3102
  const fromCodeExecution = getValueByPath(fromObject, [
3097
3103
  'codeExecution',
@@ -3601,7 +3607,7 @@ class Caches extends BaseModule {
3601
3607
  * @example
3602
3608
  * ```ts
3603
3609
  * const cachedContents = await ai.caches.list({config: {'pageSize': 2}});
3604
- * for (const cachedContent of cachedContents) {
3610
+ * for await (const cachedContent of cachedContents) {
3605
3611
  * console.log(cachedContent);
3606
3612
  * }
3607
3613
  * ```
@@ -4346,7 +4352,7 @@ const CONTENT_TYPE_HEADER = 'Content-Type';
4346
4352
  const SERVER_TIMEOUT_HEADER = 'X-Server-Timeout';
4347
4353
  const USER_AGENT_HEADER = 'User-Agent';
4348
4354
  const GOOGLE_API_CLIENT_HEADER = 'x-goog-api-client';
4349
- const SDK_VERSION = '1.3.0'; // x-release-please-version
4355
+ const SDK_VERSION = '1.5.0'; // x-release-please-version
4350
4356
  const LIBRARY_LABEL = `google-genai-sdk/${SDK_VERSION}`;
4351
4357
  const VERTEX_AI_API_DEFAULT_VERSION = 'v1beta1';
4352
4358
  const GOOGLE_AI_API_DEFAULT_VERSION = 'v1beta';
@@ -6040,6 +6046,10 @@ function urlContextToMldev$2() {
6040
6046
  const toObject = {};
6041
6047
  return toObject;
6042
6048
  }
6049
+ function urlContextToVertex$1() {
6050
+ const toObject = {};
6051
+ return toObject;
6052
+ }
6043
6053
  function toolToMldev$2(apiClient, fromObject) {
6044
6054
  const toObject = {};
6045
6055
  const fromFunctionDeclarations = getValueByPath(fromObject, [
@@ -6123,8 +6133,9 @@ function toolToVertex$1(apiClient, fromObject) {
6123
6133
  if (fromGoogleMaps != null) {
6124
6134
  setValueByPath(toObject, ['googleMaps'], googleMapsToVertex$1(apiClient, fromGoogleMaps));
6125
6135
  }
6126
- if (getValueByPath(fromObject, ['urlContext']) !== undefined) {
6127
- throw new Error('urlContext parameter is not supported in Vertex AI.');
6136
+ const fromUrlContext = getValueByPath(fromObject, ['urlContext']);
6137
+ if (fromUrlContext != null) {
6138
+ setValueByPath(toObject, ['urlContext'], urlContextToVertex$1());
6128
6139
  }
6129
6140
  const fromCodeExecution = getValueByPath(fromObject, [
6130
6141
  'codeExecution',
@@ -8720,12 +8731,18 @@ function generateVideosConfigToMldev(apiClient, fromObject, parentObject) {
8720
8731
  if (parentObject !== undefined && fromNegativePrompt != null) {
8721
8732
  setValueByPath(parentObject, ['parameters', 'negativePrompt'], fromNegativePrompt);
8722
8733
  }
8723
- if (getValueByPath(fromObject, ['enhancePrompt']) !== undefined) {
8724
- throw new Error('enhancePrompt parameter is not supported in Gemini API.');
8734
+ const fromEnhancePrompt = getValueByPath(fromObject, [
8735
+ 'enhancePrompt',
8736
+ ]);
8737
+ if (parentObject !== undefined && fromEnhancePrompt != null) {
8738
+ setValueByPath(parentObject, ['parameters', 'enhancePrompt'], fromEnhancePrompt);
8725
8739
  }
8726
8740
  if (getValueByPath(fromObject, ['generateAudio']) !== undefined) {
8727
8741
  throw new Error('generateAudio parameter is not supported in Gemini API.');
8728
8742
  }
8743
+ if (getValueByPath(fromObject, ['lastFrame']) !== undefined) {
8744
+ throw new Error('lastFrame parameter is not supported in Gemini API.');
8745
+ }
8729
8746
  return toObject;
8730
8747
  }
8731
8748
  function generateVideosParametersToMldev(apiClient, fromObject) {
@@ -8742,6 +8759,9 @@ function generateVideosParametersToMldev(apiClient, fromObject) {
8742
8759
  if (fromImage != null) {
8743
8760
  setValueByPath(toObject, ['instances[0]', 'image'], imageToMldev(apiClient, fromImage));
8744
8761
  }
8762
+ if (getValueByPath(fromObject, ['video']) !== undefined) {
8763
+ throw new Error('video parameter is not supported in Gemini API.');
8764
+ }
8745
8765
  const fromConfig = getValueByPath(fromObject, ['config']);
8746
8766
  if (fromConfig != null) {
8747
8767
  setValueByPath(toObject, ['config'], generateVideosConfigToMldev(apiClient, fromConfig, toObject));
@@ -9113,6 +9133,10 @@ function googleMapsToVertex(apiClient, fromObject) {
9113
9133
  }
9114
9134
  return toObject;
9115
9135
  }
9136
+ function urlContextToVertex() {
9137
+ const toObject = {};
9138
+ return toObject;
9139
+ }
9116
9140
  function toolToVertex(apiClient, fromObject) {
9117
9141
  const toObject = {};
9118
9142
  const fromFunctionDeclarations = getValueByPath(fromObject, [
@@ -9151,8 +9175,9 @@ function toolToVertex(apiClient, fromObject) {
9151
9175
  if (fromGoogleMaps != null) {
9152
9176
  setValueByPath(toObject, ['googleMaps'], googleMapsToVertex(apiClient, fromGoogleMaps));
9153
9177
  }
9154
- if (getValueByPath(fromObject, ['urlContext']) !== undefined) {
9155
- throw new Error('urlContext parameter is not supported in Vertex AI.');
9178
+ const fromUrlContext = getValueByPath(fromObject, ['urlContext']);
9179
+ if (fromUrlContext != null) {
9180
+ setValueByPath(toObject, ['urlContext'], urlContextToVertex());
9156
9181
  }
9157
9182
  const fromCodeExecution = getValueByPath(fromObject, [
9158
9183
  'codeExecution',
@@ -10015,6 +10040,22 @@ function computeTokensParametersToVertex(apiClient, fromObject) {
10015
10040
  }
10016
10041
  return toObject;
10017
10042
  }
10043
+ function videoToVertex(apiClient, fromObject) {
10044
+ const toObject = {};
10045
+ const fromUri = getValueByPath(fromObject, ['uri']);
10046
+ if (fromUri != null) {
10047
+ setValueByPath(toObject, ['gcsUri'], fromUri);
10048
+ }
10049
+ const fromVideoBytes = getValueByPath(fromObject, ['videoBytes']);
10050
+ if (fromVideoBytes != null) {
10051
+ setValueByPath(toObject, ['bytesBase64Encoded'], tBytes(apiClient, fromVideoBytes));
10052
+ }
10053
+ const fromMimeType = getValueByPath(fromObject, ['mimeType']);
10054
+ if (fromMimeType != null) {
10055
+ setValueByPath(toObject, ['mimeType'], fromMimeType);
10056
+ }
10057
+ return toObject;
10058
+ }
10018
10059
  function generateVideosConfigToVertex(apiClient, fromObject, parentObject) {
10019
10060
  const toObject = {};
10020
10061
  const fromNumberOfVideos = getValueByPath(fromObject, [
@@ -10077,6 +10118,10 @@ function generateVideosConfigToVertex(apiClient, fromObject, parentObject) {
10077
10118
  if (parentObject !== undefined && fromGenerateAudio != null) {
10078
10119
  setValueByPath(parentObject, ['parameters', 'generateAudio'], fromGenerateAudio);
10079
10120
  }
10121
+ const fromLastFrame = getValueByPath(fromObject, ['lastFrame']);
10122
+ if (parentObject !== undefined && fromLastFrame != null) {
10123
+ setValueByPath(parentObject, ['instances[0]', 'lastFrame'], imageToVertex(apiClient, fromLastFrame));
10124
+ }
10080
10125
  return toObject;
10081
10126
  }
10082
10127
  function generateVideosParametersToVertex(apiClient, fromObject) {
@@ -10093,6 +10138,10 @@ function generateVideosParametersToVertex(apiClient, fromObject) {
10093
10138
  if (fromImage != null) {
10094
10139
  setValueByPath(toObject, ['instances[0]', 'image'], imageToVertex(apiClient, fromImage));
10095
10140
  }
10141
+ const fromVideo = getValueByPath(fromObject, ['video']);
10142
+ if (fromVideo != null) {
10143
+ setValueByPath(toObject, ['instances[0]', 'video'], videoToVertex(apiClient, fromVideo));
10144
+ }
10096
10145
  const fromConfig = getValueByPath(fromObject, ['config']);
10097
10146
  if (fromConfig != null) {
10098
10147
  setValueByPath(toObject, ['config'], generateVideosConfigToVertex(apiClient, fromConfig, toObject));
@@ -10743,6 +10792,34 @@ function citationMetadataFromVertex(apiClient, fromObject) {
10743
10792
  }
10744
10793
  return toObject;
10745
10794
  }
10795
+ function urlMetadataFromVertex(apiClient, fromObject) {
10796
+ const toObject = {};
10797
+ const fromRetrievedUrl = getValueByPath(fromObject, ['retrievedUrl']);
10798
+ if (fromRetrievedUrl != null) {
10799
+ setValueByPath(toObject, ['retrievedUrl'], fromRetrievedUrl);
10800
+ }
10801
+ const fromUrlRetrievalStatus = getValueByPath(fromObject, [
10802
+ 'urlRetrievalStatus',
10803
+ ]);
10804
+ if (fromUrlRetrievalStatus != null) {
10805
+ setValueByPath(toObject, ['urlRetrievalStatus'], fromUrlRetrievalStatus);
10806
+ }
10807
+ return toObject;
10808
+ }
10809
+ function urlContextMetadataFromVertex(apiClient, fromObject) {
10810
+ const toObject = {};
10811
+ const fromUrlMetadata = getValueByPath(fromObject, ['urlMetadata']);
10812
+ if (fromUrlMetadata != null) {
10813
+ let transformedList = fromUrlMetadata;
10814
+ if (Array.isArray(transformedList)) {
10815
+ transformedList = transformedList.map((item) => {
10816
+ return urlMetadataFromVertex(apiClient, item);
10817
+ });
10818
+ }
10819
+ setValueByPath(toObject, ['urlMetadata'], transformedList);
10820
+ }
10821
+ return toObject;
10822
+ }
10746
10823
  function candidateFromVertex(apiClient, fromObject) {
10747
10824
  const toObject = {};
10748
10825
  const fromContent = getValueByPath(fromObject, ['content']);
@@ -10765,6 +10842,12 @@ function candidateFromVertex(apiClient, fromObject) {
10765
10842
  if (fromFinishReason != null) {
10766
10843
  setValueByPath(toObject, ['finishReason'], fromFinishReason);
10767
10844
  }
10845
+ const fromUrlContextMetadata = getValueByPath(fromObject, [
10846
+ 'urlContextMetadata',
10847
+ ]);
10848
+ if (fromUrlContextMetadata != null) {
10849
+ setValueByPath(toObject, ['urlContextMetadata'], urlContextMetadataFromVertex(apiClient, fromUrlContextMetadata));
10850
+ }
10768
10851
  const fromAvgLogprobs = getValueByPath(fromObject, ['avgLogprobs']);
10769
10852
  if (fromAvgLogprobs != null) {
10770
10853
  setValueByPath(toObject, ['avgLogprobs'], fromAvgLogprobs);
@@ -11352,9 +11435,18 @@ class McpCallableTool {
11352
11435
  for (const functionCall of functionCalls) {
11353
11436
  if (functionCall.name in this.functionNameToMcpClient) {
11354
11437
  const mcpClient = this.functionNameToMcpClient[functionCall.name];
11438
+ let requestOptions = undefined;
11439
+ // TODO: b/424238654 - Add support for finer grained timeout control.
11440
+ if (this.config.timeout) {
11441
+ requestOptions = {
11442
+ timeout: this.config.timeout,
11443
+ };
11444
+ }
11355
11445
  const callToolResponse = await mcpClient.callTool({
11356
11446
  name: functionCall.name,
11357
11447
  arguments: functionCall.args,
11448
+ CallToolResultSchema,
11449
+ requestOptions,
11358
11450
  });
11359
11451
  functionCallResponseParts.push({
11360
11452
  functionResponse: {