@google/genai 0.15.0 → 1.0.1

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/README.md CHANGED
@@ -15,10 +15,6 @@ and [Vertex AI](https://cloud.google.com/vertex-ai/generative-ai/docs/learn/over
15
15
 
16
16
  The Google Gen AI SDK is designed to work with Gemini 2.0 features.
17
17
 
18
- > [!NOTE]
19
- > **SDK Preview:**
20
- > See: [Preview Launch](#preview-launch).
21
-
22
18
  > [!CAUTION]
23
19
  > **API Key Security:** Avoid exposing API keys in client-side code.
24
20
  > Use server-side implementations in production environments.
@@ -264,19 +260,6 @@ if you are specifying those, you need to explicitly provide the full
264
260
  `Content[]` structure making it explicit which Parts are 'spoken' by the model,
265
261
  or the user. The SDK will throw an exception if you try this.
266
262
 
267
- ## Preview Launch
268
-
269
- The SDK is curently in a preview launch stage, per
270
- [Google's launch stages](https://cloud.google.com/products?hl=en#section-22)
271
- this means:
272
-
273
- > At Preview, products or features are ready for testing by customers.
274
- > Preview offerings are often publicly announced, but are not necessarily
275
- > feature-complete, and no SLAs or technical support commitments are provided
276
- > for these. Unless stated otherwise by Google, Preview offerings are intended
277
- > for use in test environments only. The average Preview stage lasts about six
278
- > months.
279
-
280
263
  ## How is this different from the other Google AI SDKs
281
264
  This SDK (`@google/genai`) is Google Deepmind’s "vanilla" SDK for its generative
282
265
  AI offerings, and is where Google Deepmind adds new AI features.
package/dist/genai.d.ts CHANGED
@@ -909,6 +909,40 @@ export declare class CountTokensResponse {
909
909
  cachedContentTokenCount?: number;
910
910
  }
911
911
 
912
+ /** Optional parameters. */
913
+ export declare interface CreateAuthTokenConfig {
914
+ /** Used to override HTTP request options. */
915
+ httpOptions?: HttpOptions;
916
+ /** Abort signal which can be used to cancel the request.
917
+
918
+ NOTE: AbortSignal is a client-only operation. Using it to cancel an
919
+ operation will not cancel the request in the service. You will still
920
+ be charged usage for any applicable operations.
921
+ */
922
+ abortSignal?: AbortSignal;
923
+ /** An optional time after which, when using the resulting token,
924
+ messages in Live API sessions will be rejected. (Gemini may
925
+ preemptively close the session after this time.)
926
+
927
+ If not set then this defaults to 30 minutes in the future. If set, this
928
+ value must be less than 20 hours in the future. */
929
+ expireTime?: string;
930
+ /** The time after which new Live API sessions using the token
931
+ resulting from this request will be rejected.
932
+
933
+ If not set this defaults to 60 seconds in the future. If set, this value
934
+ must be less than 20 hours in the future. */
935
+ newSessionExpireTime?: string;
936
+ /** The number of times the token can be used. If this value is zero
937
+ then no limit is applied. Default is 1. Resuming a Live API session does
938
+ not count as a use. */
939
+ uses?: number;
940
+ /** Configuration specific to Live API connections created using this token. */
941
+ liveEphemeralParameters?: LiveEphemeralParameters;
942
+ /** Additional fields to lock in the effective LiveConnectParameters. */
943
+ lockAdditionalFields?: string[];
944
+ }
945
+
912
946
  /** Optional configuration for cached content creation. */
913
947
  export declare interface CreateCachedContentConfig {
914
948
  /** Used to override HTTP request options. */
@@ -3212,7 +3246,7 @@ export declare class Live {
3212
3246
  Establishes a connection to the specified model with the given
3213
3247
  configuration and returns a Session object representing that connection.
3214
3248
 
3215
- @experimental Built-in MCP support is a preview feature, may change in
3249
+ @experimental Built-in MCP support is an experimental feature, may change in
3216
3250
  future versions.
3217
3251
 
3218
3252
  @remarks
@@ -3407,6 +3441,15 @@ export declare class LiveClientToolResponse {
3407
3441
 
3408
3442
  /** Session config for the API connection. */
3409
3443
  export declare interface LiveConnectConfig {
3444
+ /** Used to override HTTP request options. */
3445
+ httpOptions?: HttpOptions;
3446
+ /** Abort signal which can be used to cancel the request.
3447
+
3448
+ NOTE: AbortSignal is a client-only operation. Using it to cancel an
3449
+ operation will not cancel the request in the service. You will still
3450
+ be charged usage for any applicable operations.
3451
+ */
3452
+ abortSignal?: AbortSignal;
3410
3453
  /** The generation configuration for the session. */
3411
3454
  generationConfig?: GenerationConfig;
3412
3455
  /** The requested modalities of the response. Represents the set of
@@ -3491,6 +3534,16 @@ export declare interface LiveConnectParameters {
3491
3534
  config?: LiveConnectConfig;
3492
3535
  }
3493
3536
 
3537
+ /** Config for LiveEphemeralParameters for Auth Token creation. */
3538
+ export declare interface LiveEphemeralParameters {
3539
+ /** ID of the model to configure in the ephemeral token for Live API.
3540
+ For a list of models, see `Gemini models
3541
+ <https://ai.google.dev/gemini-api/docs/models>`. */
3542
+ model?: string;
3543
+ /** Configuration specific to Live API connections created using this token. */
3544
+ config?: LiveConnectConfig;
3545
+ }
3546
+
3494
3547
  /**
3495
3548
  LiveMusic class encapsulates the configuration for live music
3496
3549
  generation via Lyria Live models.
@@ -3689,7 +3742,7 @@ export declare interface LiveMusicServerSetupComplete {
3689
3742
 
3690
3743
  @experimental
3691
3744
  */
3692
- declare class LiveMusicSession {
3745
+ export declare class LiveMusicSession {
3693
3746
  readonly conn: WebSocket_2;
3694
3747
  private readonly apiClient;
3695
3748
  constructor(conn: WebSocket_2, apiClient: ApiClient);
@@ -3704,7 +3757,7 @@ declare class LiveMusicSession {
3704
3757
 
3705
3758
  @experimental
3706
3759
  */
3707
- setClientContent(params: types.LiveMusicSetClientContentParameters): Promise<void>;
3760
+ setWeightedPrompts(params: types.LiveMusicSetWeightedPromptsParameters): Promise<void>;
3708
3761
  /**
3709
3762
  Sets a configuration to the model. Updates the session's current
3710
3763
  music generation config.
@@ -3753,18 +3806,18 @@ declare class LiveMusicSession {
3753
3806
  close(): void;
3754
3807
  }
3755
3808
 
3756
- /** Parameters for setting client content for the live API. */
3757
- export declare interface LiveMusicSetClientContentParameters {
3758
- /** A map of text prompts to weights to use for the generation request. */
3759
- weightedPrompts: WeightedPrompt[];
3760
- }
3761
-
3762
- /** Parameters for setting config for the live API. */
3809
+ /** Parameters for setting config for the live music API. */
3763
3810
  export declare interface LiveMusicSetConfigParameters {
3764
3811
  /** Configuration for music generation. */
3765
3812
  musicGenerationConfig: LiveMusicGenerationConfig;
3766
3813
  }
3767
3814
 
3815
+ /** Parameters for setting weighted prompts for the live music API. */
3816
+ export declare interface LiveMusicSetWeightedPromptsParameters {
3817
+ /** A map of text prompts to weights to use for the generation request. */
3818
+ weightedPrompts: WeightedPrompt[];
3819
+ }
3820
+
3768
3821
  /** Prompts and config used for generating this audio chunk. */
3769
3822
  export declare interface LiveMusicSourceMetadata {
3770
3823
  /** Weighted prompts for generating this audio chunk. */
@@ -4005,7 +4058,7 @@ export declare enum MaskReferenceMode {
4005
4058
  * arguments. (often for automatic function calling).
4006
4059
  * Use the config to modify tool parameters such as behavior.
4007
4060
  *
4008
- * @experimental Built-in MCP support is a preview feature, may change in future
4061
+ * @experimental Built-in MCP support is an experimental feature, may change in future
4009
4062
  * versions.
4010
4063
  */
4011
4064
  export declare function mcpToTool(...args: [...Client[], CallableToolConfig | Client]): CallableTool;
@@ -4218,9 +4271,10 @@ export declare class Models extends BaseModule {
4218
4271
  /**
4219
4272
  * Transforms the CallableTools in the parameters to be simply Tools, it
4220
4273
  * copies the params into a new object and replaces the tools, it does not
4221
- * modify the original params.
4274
+ * modify the original params. Also sets the MCP usage header if there are
4275
+ * MCP tools in the parameters.
4222
4276
  */
4223
- private transformCallableTools;
4277
+ private processParamsForMcpUsage;
4224
4278
  private initAfcToolsMap;
4225
4279
  private processAfcStream;
4226
4280
  /**
@@ -6008,7 +6062,9 @@ declare namespace types {
6008
6062
  CallableToolConfig,
6009
6063
  LiveMusicConnectParameters,
6010
6064
  LiveMusicSetConfigParameters,
6011
- LiveMusicSetClientContentParameters,
6065
+ LiveMusicSetWeightedPromptsParameters,
6066
+ LiveEphemeralParameters,
6067
+ CreateAuthTokenConfig,
6012
6068
  OperationGetParameters,
6013
6069
  BlobImageUnion,
6014
6070
  PartUnion,
package/dist/index.mjs CHANGED
@@ -4170,7 +4170,10 @@ class Chat {
4170
4170
  this.recordHistory(inputContent, modelOutput, automaticFunctionCallingHistory);
4171
4171
  return;
4172
4172
  })();
4173
- await this.sendPromise;
4173
+ await this.sendPromise.catch(() => {
4174
+ // Resets sendPromise to avoid subsequent calls failing
4175
+ this.sendPromise = Promise.resolve();
4176
+ });
4174
4177
  return responsePromise;
4175
4178
  }
4176
4179
  /**
@@ -4308,7 +4311,7 @@ const CONTENT_TYPE_HEADER = 'Content-Type';
4308
4311
  const SERVER_TIMEOUT_HEADER = 'X-Server-Timeout';
4309
4312
  const USER_AGENT_HEADER = 'User-Agent';
4310
4313
  const GOOGLE_API_CLIENT_HEADER = 'x-goog-api-client';
4311
- const SDK_VERSION = '0.15.0'; // x-release-please-version
4314
+ const SDK_VERSION = '1.0.1'; // x-release-please-version
4312
4315
  const LIBRARY_LABEL = `google-genai-sdk/${SDK_VERSION}`;
4313
4316
  const VERTEX_AI_API_DEFAULT_VERSION = 'v1beta1';
4314
4317
  const GOOGLE_AI_API_DEFAULT_VERSION = 'v1beta';
@@ -6585,7 +6588,7 @@ function weightedPromptToMldev(apiClient, fromObject) {
6585
6588
  }
6586
6589
  return toObject;
6587
6590
  }
6588
- function liveMusicSetClientContentParametersToMldev(apiClient, fromObject) {
6591
+ function liveMusicSetWeightedPromptsParametersToMldev(apiClient, fromObject) {
6589
6592
  const toObject = {};
6590
6593
  const fromWeightedPrompts = getValueByPath(fromObject, [
6591
6594
  'weightedPrompts',
@@ -10839,7 +10842,11 @@ function hasMcpToolUsage(tools) {
10839
10842
  // Sets the MCP version label in the Google API client header.
10840
10843
  function setMcpUsageHeader(headers) {
10841
10844
  var _a;
10842
- headers[GOOGLE_API_CLIENT_HEADER] = (((_a = headers[GOOGLE_API_CLIENT_HEADER]) !== null && _a !== void 0 ? _a : '') + ` ${MCP_LABEL}`).trimStart();
10845
+ const existingHeader = (_a = headers[GOOGLE_API_CLIENT_HEADER]) !== null && _a !== void 0 ? _a : '';
10846
+ if (existingHeader.includes(MCP_LABEL)) {
10847
+ return;
10848
+ }
10849
+ headers[GOOGLE_API_CLIENT_HEADER] = (existingHeader + ` ${MCP_LABEL}`).trimStart();
10843
10850
  }
10844
10851
  // Checks whether the list of tools contains any MCP clients. Will return true
10845
10852
  // if there is at least one MCP client.
@@ -10883,7 +10890,7 @@ function listAllTools(mcpClient, maxTools = 100) {
10883
10890
  * McpCallableTool can be used for model inference and invoking MCP clients with
10884
10891
  * given function call arguments.
10885
10892
  *
10886
- * @experimental Built-in MCP support is a preview feature, may change in future
10893
+ * @experimental Built-in MCP support is an experimental feature, may change in future
10887
10894
  * versions.
10888
10895
  */
10889
10896
  class McpCallableTool {
@@ -10978,7 +10985,7 @@ function isMcpClient(client) {
10978
10985
  * arguments. (often for automatic function calling).
10979
10986
  * Use the config to modify tool parameters such as behavior.
10980
10987
  *
10981
- * @experimental Built-in MCP support is a preview feature, may change in future
10988
+ * @experimental Built-in MCP support is an experimental feature, may change in future
10982
10989
  * versions.
10983
10990
  */
10984
10991
  function mcpToTool(...args) {
@@ -11012,7 +11019,13 @@ function mcpToTool(...args) {
11012
11019
  */
11013
11020
  async function handleWebSocketMessage$1(apiClient, onmessage, event) {
11014
11021
  const serverMessage = new LiveMusicServerMessage();
11015
- const data = JSON.parse(event.data);
11022
+ let data;
11023
+ if (event.data instanceof Blob) {
11024
+ data = JSON.parse(await event.data.text());
11025
+ }
11026
+ else {
11027
+ data = JSON.parse(event.data);
11028
+ }
11016
11029
  const response = liveMusicServerMessageFromMldev(apiClient, data);
11017
11030
  Object.assign(serverMessage, response);
11018
11031
  onmessage(serverMessage);
@@ -11123,13 +11136,13 @@ class LiveMusicSession {
11123
11136
 
11124
11137
  @experimental
11125
11138
  */
11126
- async setClientContent(params) {
11139
+ async setWeightedPrompts(params) {
11127
11140
  if (!params.weightedPrompts ||
11128
11141
  Object.keys(params.weightedPrompts).length === 0) {
11129
11142
  throw new Error('Weighted prompts must be set and contain at least one entry.');
11130
11143
  }
11131
- const setClientContentParameters = liveMusicSetClientContentParametersToMldev(this.apiClient, params);
11132
- const clientContent = liveMusicClientContentToMldev(this.apiClient, setClientContentParameters);
11144
+ const setWeightedPromptsParameters = liveMusicSetWeightedPromptsParametersToMldev(this.apiClient, params);
11145
+ const clientContent = liveMusicClientContentToMldev(this.apiClient, setWeightedPromptsParameters);
11133
11146
  this.conn.send(JSON.stringify({ clientContent }));
11134
11147
  }
11135
11148
  /**
@@ -11277,7 +11290,7 @@ class Live {
11277
11290
  Establishes a connection to the specified model with the given
11278
11291
  configuration and returns a Session object representing that connection.
11279
11292
 
11280
- @experimental Built-in MCP support is a preview feature, may change in
11293
+ @experimental Built-in MCP support is an experimental feature, may change in
11281
11294
  future versions.
11282
11295
 
11283
11296
  @remarks
@@ -11733,25 +11746,14 @@ class Models extends BaseModule {
11733
11746
  */
11734
11747
  this.generateContent = async (params) => {
11735
11748
  var _a, _b, _c, _d, _e;
11736
- if (params.config &&
11737
- params.config.tools &&
11738
- hasMcpToolUsage(params.config.tools)) {
11739
- if (!params.config.httpOptions) {
11740
- params.config.httpOptions = {};
11741
- }
11742
- if (!params.config.httpOptions.headers) {
11743
- params.config.httpOptions.headers = this.apiClient.getDefaultHeaders();
11744
- }
11745
- setMcpUsageHeader(params.config.httpOptions.headers);
11746
- }
11749
+ const transformedParams = await this.processParamsForMcpUsage(params);
11747
11750
  if (!hasMcpClientTools(params) || shouldDisableAfc(params.config)) {
11748
- return await this.generateContentInternal(params);
11751
+ return await this.generateContentInternal(transformedParams);
11749
11752
  }
11750
11753
  // TODO: b/418266406 - Improve the check for CallableTools and Tools.
11751
11754
  if (hasNonMcpTools(params)) {
11752
11755
  throw new Error('Automatic function calling with CallableTools and Tools is not yet supported.');
11753
11756
  }
11754
- const transformedParams = await this.transformCallableTools(params);
11755
11757
  let response;
11756
11758
  let functionResponseContent;
11757
11759
  const automaticFunctionCallingHistory = tContents(this.apiClient, transformedParams.contents);
@@ -11832,19 +11834,8 @@ class Models extends BaseModule {
11832
11834
  * ```
11833
11835
  */
11834
11836
  this.generateContentStream = async (params) => {
11835
- if (params.config &&
11836
- params.config.tools &&
11837
- hasMcpToolUsage(params.config.tools)) {
11838
- if (!params.config.httpOptions) {
11839
- params.config.httpOptions = {};
11840
- }
11841
- if (!params.config.httpOptions.headers) {
11842
- params.config.httpOptions.headers = this.apiClient.getDefaultHeaders();
11843
- }
11844
- setMcpUsageHeader(params.config.httpOptions.headers);
11845
- }
11846
11837
  if (shouldDisableAfc(params.config)) {
11847
- const transformedParams = await this.transformCallableTools(params);
11838
+ const transformedParams = await this.processParamsForMcpUsage(params);
11848
11839
  return await this.generateContentStreamInternal(transformedParams);
11849
11840
  }
11850
11841
  else {
@@ -11997,10 +11988,11 @@ class Models extends BaseModule {
11997
11988
  /**
11998
11989
  * Transforms the CallableTools in the parameters to be simply Tools, it
11999
11990
  * copies the params into a new object and replaces the tools, it does not
12000
- * modify the original params.
11991
+ * modify the original params. Also sets the MCP usage header if there are
11992
+ * MCP tools in the parameters.
12001
11993
  */
12002
- async transformCallableTools(params) {
12003
- var _a;
11994
+ async processParamsForMcpUsage(params) {
11995
+ var _a, _b, _c;
12004
11996
  const tools = (_a = params.config) === null || _a === void 0 ? void 0 : _a.tools;
12005
11997
  if (!tools) {
12006
11998
  return params;
@@ -12018,6 +12010,17 @@ class Models extends BaseModule {
12018
12010
  config: Object.assign(Object.assign({}, params.config), { tools: transformedTools }),
12019
12011
  };
12020
12012
  newParams.config.tools = transformedTools;
12013
+ if (params.config &&
12014
+ params.config.tools &&
12015
+ hasMcpToolUsage(params.config.tools)) {
12016
+ const headers = (_c = (_b = params.config.httpOptions) === null || _b === void 0 ? void 0 : _b.headers) !== null && _c !== void 0 ? _c : {};
12017
+ let newHeaders = Object.assign({}, headers);
12018
+ if (Object.keys(newHeaders).length === 0) {
12019
+ newHeaders = this.apiClient.getDefaultHeaders();
12020
+ }
12021
+ setMcpUsageHeader(newHeaders);
12022
+ newParams.config.httpOptions = Object.assign(Object.assign({}, params.config.httpOptions), { headers: newHeaders });
12023
+ }
12021
12024
  return newParams;
12022
12025
  }
12023
12026
  async initAfcToolsMap(params) {
@@ -12055,7 +12058,7 @@ class Models extends BaseModule {
12055
12058
  remoteCallCount++;
12056
12059
  wereFunctionsCalled = false;
12057
12060
  }
12058
- const transformedParams = yield __await(models.transformCallableTools(params));
12061
+ const transformedParams = yield __await(models.processParamsForMcpUsage(params));
12059
12062
  const response = yield __await(models.generateContentStreamInternal(transformedParams));
12060
12063
  const functionResponses = [];
12061
12064
  const responseContents = [];