@google/genai 0.15.0 → 1.0.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/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.js CHANGED
@@ -4310,7 +4310,7 @@ const CONTENT_TYPE_HEADER = 'Content-Type';
4310
4310
  const SERVER_TIMEOUT_HEADER = 'X-Server-Timeout';
4311
4311
  const USER_AGENT_HEADER = 'User-Agent';
4312
4312
  const GOOGLE_API_CLIENT_HEADER = 'x-goog-api-client';
4313
- const SDK_VERSION = '0.15.0'; // x-release-please-version
4313
+ const SDK_VERSION = '1.0.0'; // x-release-please-version
4314
4314
  const LIBRARY_LABEL = `google-genai-sdk/${SDK_VERSION}`;
4315
4315
  const VERTEX_AI_API_DEFAULT_VERSION = 'v1beta1';
4316
4316
  const GOOGLE_AI_API_DEFAULT_VERSION = 'v1beta';
@@ -6587,7 +6587,7 @@ function weightedPromptToMldev(apiClient, fromObject) {
6587
6587
  }
6588
6588
  return toObject;
6589
6589
  }
6590
- function liveMusicSetClientContentParametersToMldev(apiClient, fromObject) {
6590
+ function liveMusicSetWeightedPromptsParametersToMldev(apiClient, fromObject) {
6591
6591
  const toObject = {};
6592
6592
  const fromWeightedPrompts = getValueByPath(fromObject, [
6593
6593
  'weightedPrompts',
@@ -10841,7 +10841,11 @@ function hasMcpToolUsage(tools) {
10841
10841
  // Sets the MCP version label in the Google API client header.
10842
10842
  function setMcpUsageHeader(headers) {
10843
10843
  var _a;
10844
- headers[GOOGLE_API_CLIENT_HEADER] = (((_a = headers[GOOGLE_API_CLIENT_HEADER]) !== null && _a !== void 0 ? _a : '') + ` ${MCP_LABEL}`).trimStart();
10844
+ const existingHeader = (_a = headers[GOOGLE_API_CLIENT_HEADER]) !== null && _a !== void 0 ? _a : '';
10845
+ if (existingHeader.includes(MCP_LABEL)) {
10846
+ return;
10847
+ }
10848
+ headers[GOOGLE_API_CLIENT_HEADER] = (existingHeader + ` ${MCP_LABEL}`).trimStart();
10845
10849
  }
10846
10850
  // Checks whether the list of tools contains any MCP clients. Will return true
10847
10851
  // if there is at least one MCP client.
@@ -10885,7 +10889,7 @@ function listAllTools(mcpClient, maxTools = 100) {
10885
10889
  * McpCallableTool can be used for model inference and invoking MCP clients with
10886
10890
  * given function call arguments.
10887
10891
  *
10888
- * @experimental Built-in MCP support is a preview feature, may change in future
10892
+ * @experimental Built-in MCP support is an experimental feature, may change in future
10889
10893
  * versions.
10890
10894
  */
10891
10895
  class McpCallableTool {
@@ -10980,7 +10984,7 @@ function isMcpClient(client) {
10980
10984
  * arguments. (often for automatic function calling).
10981
10985
  * Use the config to modify tool parameters such as behavior.
10982
10986
  *
10983
- * @experimental Built-in MCP support is a preview feature, may change in future
10987
+ * @experimental Built-in MCP support is an experimental feature, may change in future
10984
10988
  * versions.
10985
10989
  */
10986
10990
  function mcpToTool(...args) {
@@ -11014,7 +11018,13 @@ function mcpToTool(...args) {
11014
11018
  */
11015
11019
  async function handleWebSocketMessage$1(apiClient, onmessage, event) {
11016
11020
  const serverMessage = new LiveMusicServerMessage();
11017
- const data = JSON.parse(event.data);
11021
+ let data;
11022
+ if (event.data instanceof Blob) {
11023
+ data = JSON.parse(await event.data.text());
11024
+ }
11025
+ else {
11026
+ data = JSON.parse(event.data);
11027
+ }
11018
11028
  const response = liveMusicServerMessageFromMldev(apiClient, data);
11019
11029
  Object.assign(serverMessage, response);
11020
11030
  onmessage(serverMessage);
@@ -11125,13 +11135,13 @@ class LiveMusicSession {
11125
11135
 
11126
11136
  @experimental
11127
11137
  */
11128
- async setClientContent(params) {
11138
+ async setWeightedPrompts(params) {
11129
11139
  if (!params.weightedPrompts ||
11130
11140
  Object.keys(params.weightedPrompts).length === 0) {
11131
11141
  throw new Error('Weighted prompts must be set and contain at least one entry.');
11132
11142
  }
11133
- const setClientContentParameters = liveMusicSetClientContentParametersToMldev(this.apiClient, params);
11134
- const clientContent = liveMusicClientContentToMldev(this.apiClient, setClientContentParameters);
11143
+ const setWeightedPromptsParameters = liveMusicSetWeightedPromptsParametersToMldev(this.apiClient, params);
11144
+ const clientContent = liveMusicClientContentToMldev(this.apiClient, setWeightedPromptsParameters);
11135
11145
  this.conn.send(JSON.stringify({ clientContent }));
11136
11146
  }
11137
11147
  /**
@@ -11279,7 +11289,7 @@ class Live {
11279
11289
  Establishes a connection to the specified model with the given
11280
11290
  configuration and returns a Session object representing that connection.
11281
11291
 
11282
- @experimental Built-in MCP support is a preview feature, may change in
11292
+ @experimental Built-in MCP support is an experimental feature, may change in
11283
11293
  future versions.
11284
11294
 
11285
11295
  @remarks
@@ -11735,25 +11745,14 @@ class Models extends BaseModule {
11735
11745
  */
11736
11746
  this.generateContent = async (params) => {
11737
11747
  var _a, _b, _c, _d, _e;
11738
- if (params.config &&
11739
- params.config.tools &&
11740
- hasMcpToolUsage(params.config.tools)) {
11741
- if (!params.config.httpOptions) {
11742
- params.config.httpOptions = {};
11743
- }
11744
- if (!params.config.httpOptions.headers) {
11745
- params.config.httpOptions.headers = this.apiClient.getDefaultHeaders();
11746
- }
11747
- setMcpUsageHeader(params.config.httpOptions.headers);
11748
- }
11748
+ const transformedParams = await this.processParamsForMcpUsage(params);
11749
11749
  if (!hasMcpClientTools(params) || shouldDisableAfc(params.config)) {
11750
- return await this.generateContentInternal(params);
11750
+ return await this.generateContentInternal(transformedParams);
11751
11751
  }
11752
11752
  // TODO: b/418266406 - Improve the check for CallableTools and Tools.
11753
11753
  if (hasNonMcpTools(params)) {
11754
11754
  throw new Error('Automatic function calling with CallableTools and Tools is not yet supported.');
11755
11755
  }
11756
- const transformedParams = await this.transformCallableTools(params);
11757
11756
  let response;
11758
11757
  let functionResponseContent;
11759
11758
  const automaticFunctionCallingHistory = tContents(this.apiClient, transformedParams.contents);
@@ -11834,19 +11833,8 @@ class Models extends BaseModule {
11834
11833
  * ```
11835
11834
  */
11836
11835
  this.generateContentStream = async (params) => {
11837
- if (params.config &&
11838
- params.config.tools &&
11839
- hasMcpToolUsage(params.config.tools)) {
11840
- if (!params.config.httpOptions) {
11841
- params.config.httpOptions = {};
11842
- }
11843
- if (!params.config.httpOptions.headers) {
11844
- params.config.httpOptions.headers = this.apiClient.getDefaultHeaders();
11845
- }
11846
- setMcpUsageHeader(params.config.httpOptions.headers);
11847
- }
11848
11836
  if (shouldDisableAfc(params.config)) {
11849
- const transformedParams = await this.transformCallableTools(params);
11837
+ const transformedParams = await this.processParamsForMcpUsage(params);
11850
11838
  return await this.generateContentStreamInternal(transformedParams);
11851
11839
  }
11852
11840
  else {
@@ -11999,10 +11987,11 @@ class Models extends BaseModule {
11999
11987
  /**
12000
11988
  * Transforms the CallableTools in the parameters to be simply Tools, it
12001
11989
  * copies the params into a new object and replaces the tools, it does not
12002
- * modify the original params.
11990
+ * modify the original params. Also sets the MCP usage header if there are
11991
+ * MCP tools in the parameters.
12003
11992
  */
12004
- async transformCallableTools(params) {
12005
- var _a;
11993
+ async processParamsForMcpUsage(params) {
11994
+ var _a, _b, _c;
12006
11995
  const tools = (_a = params.config) === null || _a === void 0 ? void 0 : _a.tools;
12007
11996
  if (!tools) {
12008
11997
  return params;
@@ -12020,6 +12009,17 @@ class Models extends BaseModule {
12020
12009
  config: Object.assign(Object.assign({}, params.config), { tools: transformedTools }),
12021
12010
  };
12022
12011
  newParams.config.tools = transformedTools;
12012
+ if (params.config &&
12013
+ params.config.tools &&
12014
+ hasMcpToolUsage(params.config.tools)) {
12015
+ const headers = (_c = (_b = params.config.httpOptions) === null || _b === void 0 ? void 0 : _b.headers) !== null && _c !== void 0 ? _c : {};
12016
+ let newHeaders = Object.assign({}, headers);
12017
+ if (Object.keys(newHeaders).length === 0) {
12018
+ newHeaders = this.apiClient.getDefaultHeaders();
12019
+ }
12020
+ setMcpUsageHeader(newHeaders);
12021
+ newParams.config.httpOptions = Object.assign(Object.assign({}, params.config.httpOptions), { headers: newHeaders });
12022
+ }
12023
12023
  return newParams;
12024
12024
  }
12025
12025
  async initAfcToolsMap(params) {
@@ -12057,7 +12057,7 @@ class Models extends BaseModule {
12057
12057
  remoteCallCount++;
12058
12058
  wereFunctionsCalled = false;
12059
12059
  }
12060
- const transformedParams = yield __await(models.transformCallableTools(params));
12060
+ const transformedParams = yield __await(models.processParamsForMcpUsage(params));
12061
12061
  const response = yield __await(models.generateContentStreamInternal(transformedParams));
12062
12062
  const functionResponses = [];
12063
12063
  const responseContents = [];