@google/genai 1.30.0 → 1.32.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/genai.d.ts +91 -74
- package/dist/index.cjs +1343 -1265
- package/dist/index.mjs +1343 -1266
- package/dist/index.mjs.map +1 -1
- package/dist/node/index.cjs +3159 -3081
- package/dist/node/index.mjs +3159 -3082
- package/dist/node/index.mjs.map +1 -1
- package/dist/node/node.d.ts +91 -74
- package/dist/web/index.mjs +3159 -3082
- package/dist/web/index.mjs.map +1 -1
- package/dist/web/web.d.ts +91 -74
- package/package.json +8 -7
package/dist/index.cjs
CHANGED
|
@@ -2196,6 +2196,9 @@ class GenerateVideosOperation {
|
|
|
2196
2196
|
/** Response for the list tuning jobs method. */
|
|
2197
2197
|
class ListTuningJobsResponse {
|
|
2198
2198
|
}
|
|
2199
|
+
/** Empty response for tunings.cancel method. */
|
|
2200
|
+
class CancelTuningJobResponse {
|
|
2201
|
+
}
|
|
2199
2202
|
/** Empty response for caches.delete method. */
|
|
2200
2203
|
class DeleteCachedContentResponse {
|
|
2201
2204
|
}
|
|
@@ -4082,6 +4085,12 @@ function generateContentConfigToMldev$1(apiClient, fromObject, parentObject) {
|
|
|
4082
4085
|
if (fromImageConfig != null) {
|
|
4083
4086
|
setValueByPath(toObject, ['imageConfig'], imageConfigToMldev$1(fromImageConfig));
|
|
4084
4087
|
}
|
|
4088
|
+
const fromEnableEnhancedCivicAnswers = getValueByPath(fromObject, [
|
|
4089
|
+
'enableEnhancedCivicAnswers',
|
|
4090
|
+
]);
|
|
4091
|
+
if (fromEnableEnhancedCivicAnswers != null) {
|
|
4092
|
+
setValueByPath(toObject, ['enableEnhancedCivicAnswers'], fromEnableEnhancedCivicAnswers);
|
|
4093
|
+
}
|
|
4085
4094
|
return toObject;
|
|
4086
4095
|
}
|
|
4087
4096
|
function generateContentResponseFromMldev$1(fromObject) {
|
|
@@ -4663,6 +4672,23 @@ class Batches extends BaseModule {
|
|
|
4663
4672
|
constructor(apiClient) {
|
|
4664
4673
|
super();
|
|
4665
4674
|
this.apiClient = apiClient;
|
|
4675
|
+
/**
|
|
4676
|
+
* Lists batch jobs.
|
|
4677
|
+
*
|
|
4678
|
+
* @param params - The parameters for the list request.
|
|
4679
|
+
* @return - A pager of batch jobs.
|
|
4680
|
+
*
|
|
4681
|
+
* @example
|
|
4682
|
+
* ```ts
|
|
4683
|
+
* const batchJobs = await ai.batches.list({config: {'pageSize': 2}});
|
|
4684
|
+
* for await (const batchJob of batchJobs) {
|
|
4685
|
+
* console.log(batchJob);
|
|
4686
|
+
* }
|
|
4687
|
+
* ```
|
|
4688
|
+
*/
|
|
4689
|
+
this.list = async (params = {}) => {
|
|
4690
|
+
return new Pager(exports.PagedItem.PAGED_ITEM_BATCH_JOBS, (x) => this.listInternal(x), await this.listInternal(params), params);
|
|
4691
|
+
};
|
|
4666
4692
|
/**
|
|
4667
4693
|
* Create batch job.
|
|
4668
4694
|
*
|
|
@@ -4711,23 +4737,6 @@ class Batches extends BaseModule {
|
|
|
4711
4737
|
}
|
|
4712
4738
|
return this.createEmbeddingsInternal(params);
|
|
4713
4739
|
};
|
|
4714
|
-
/**
|
|
4715
|
-
* Lists batch job configurations.
|
|
4716
|
-
*
|
|
4717
|
-
* @param params - The parameters for the list request.
|
|
4718
|
-
* @return The paginated results of the list of batch jobs.
|
|
4719
|
-
*
|
|
4720
|
-
* @example
|
|
4721
|
-
* ```ts
|
|
4722
|
-
* const batchJobs = await ai.batches.list({config: {'pageSize': 2}});
|
|
4723
|
-
* for await (const batchJob of batchJobs) {
|
|
4724
|
-
* console.log(batchJob);
|
|
4725
|
-
* }
|
|
4726
|
-
* ```
|
|
4727
|
-
*/
|
|
4728
|
-
this.list = async (params = {}) => {
|
|
4729
|
-
return new Pager(exports.PagedItem.PAGED_ITEM_BATCH_JOBS, (x) => this.listInternal(x), await this.listInternal(params), params);
|
|
4730
|
-
};
|
|
4731
4740
|
}
|
|
4732
4741
|
// Helper function to handle inlined generate content requests
|
|
4733
4742
|
createInlinedGenerateContentRequest(params) {
|
|
@@ -5830,10 +5839,10 @@ class Caches extends BaseModule {
|
|
|
5830
5839
|
super();
|
|
5831
5840
|
this.apiClient = apiClient;
|
|
5832
5841
|
/**
|
|
5833
|
-
* Lists cached
|
|
5842
|
+
* Lists cached contents.
|
|
5834
5843
|
*
|
|
5835
5844
|
* @param params - The parameters for the list request.
|
|
5836
|
-
* @return
|
|
5845
|
+
* @return - A pager of cached contents.
|
|
5837
5846
|
*
|
|
5838
5847
|
* @example
|
|
5839
5848
|
* ```ts
|
|
@@ -6526,17 +6535,17 @@ class Chat {
|
|
|
6526
6535
|
return structuredClone(history);
|
|
6527
6536
|
}
|
|
6528
6537
|
processStreamResponse(streamResponse, inputContent) {
|
|
6529
|
-
var _a, _b;
|
|
6530
6538
|
return __asyncGenerator(this, arguments, function* processStreamResponse_1() {
|
|
6531
|
-
var
|
|
6539
|
+
var _a, e_1, _b, _c;
|
|
6540
|
+
var _d, _e;
|
|
6532
6541
|
const outputContent = [];
|
|
6533
6542
|
try {
|
|
6534
|
-
for (var _f = true, streamResponse_1 = __asyncValues(streamResponse), streamResponse_1_1; streamResponse_1_1 = yield __await(streamResponse_1.next()),
|
|
6535
|
-
|
|
6543
|
+
for (var _f = true, streamResponse_1 = __asyncValues(streamResponse), streamResponse_1_1; streamResponse_1_1 = yield __await(streamResponse_1.next()), _a = streamResponse_1_1.done, !_a; _f = true) {
|
|
6544
|
+
_c = streamResponse_1_1.value;
|
|
6536
6545
|
_f = false;
|
|
6537
|
-
const chunk =
|
|
6546
|
+
const chunk = _c;
|
|
6538
6547
|
if (isValidResponse(chunk)) {
|
|
6539
|
-
const content = (
|
|
6548
|
+
const content = (_e = (_d = chunk.candidates) === null || _d === void 0 ? void 0 : _d[0]) === null || _e === void 0 ? void 0 : _e.content;
|
|
6540
6549
|
if (content !== undefined) {
|
|
6541
6550
|
outputContent.push(content);
|
|
6542
6551
|
}
|
|
@@ -6547,7 +6556,7 @@ class Chat {
|
|
|
6547
6556
|
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
6548
6557
|
finally {
|
|
6549
6558
|
try {
|
|
6550
|
-
if (!_f && !
|
|
6559
|
+
if (!_f && !_a && (_b = streamResponse_1.return)) yield __await(_b.call(streamResponse_1));
|
|
6551
6560
|
}
|
|
6552
6561
|
finally { if (e_1) throw e_1.error; }
|
|
6553
6562
|
}
|
|
@@ -6584,1150 +6593,880 @@ class Chat {
|
|
|
6584
6593
|
* Copyright 2025 Google LLC
|
|
6585
6594
|
* SPDX-License-Identifier: Apache-2.0
|
|
6586
6595
|
*/
|
|
6587
|
-
|
|
6588
|
-
|
|
6589
|
-
|
|
6590
|
-
|
|
6591
|
-
|
|
6592
|
-
|
|
6593
|
-
this.name = 'ApiError';
|
|
6594
|
-
this.status = options.status;
|
|
6595
|
-
Object.setPrototypeOf(this, ApiError.prototype);
|
|
6596
|
+
// Code generated by the Google Gen AI SDK generator DO NOT EDIT.
|
|
6597
|
+
function createFileSearchStoreConfigToMldev(fromObject, parentObject) {
|
|
6598
|
+
const toObject = {};
|
|
6599
|
+
const fromDisplayName = getValueByPath(fromObject, ['displayName']);
|
|
6600
|
+
if (parentObject !== undefined && fromDisplayName != null) {
|
|
6601
|
+
setValueByPath(parentObject, ['displayName'], fromDisplayName);
|
|
6596
6602
|
}
|
|
6603
|
+
return toObject;
|
|
6597
6604
|
}
|
|
6598
|
-
|
|
6599
|
-
|
|
6600
|
-
|
|
6601
|
-
|
|
6602
|
-
|
|
6603
|
-
*/
|
|
6604
|
-
const CONTENT_TYPE_HEADER = 'Content-Type';
|
|
6605
|
-
const SERVER_TIMEOUT_HEADER = 'X-Server-Timeout';
|
|
6606
|
-
const USER_AGENT_HEADER = 'User-Agent';
|
|
6607
|
-
const GOOGLE_API_CLIENT_HEADER = 'x-goog-api-client';
|
|
6608
|
-
const SDK_VERSION = '1.30.0'; // x-release-please-version
|
|
6609
|
-
const LIBRARY_LABEL = `google-genai-sdk/${SDK_VERSION}`;
|
|
6610
|
-
const VERTEX_AI_API_DEFAULT_VERSION = 'v1beta1';
|
|
6611
|
-
const GOOGLE_AI_API_DEFAULT_VERSION = 'v1beta';
|
|
6612
|
-
const responseLineRE = /^\s*data: (.*)(?:\n\n|\r\r|\r\n\r\n)/;
|
|
6613
|
-
/**
|
|
6614
|
-
* The ApiClient class is used to send requests to the Gemini API or Vertex AI
|
|
6615
|
-
* endpoints.
|
|
6616
|
-
*/
|
|
6617
|
-
class ApiClient {
|
|
6618
|
-
constructor(opts) {
|
|
6619
|
-
var _a, _b;
|
|
6620
|
-
this.clientOptions = Object.assign(Object.assign({}, opts), { project: opts.project, location: opts.location, apiKey: opts.apiKey, vertexai: opts.vertexai });
|
|
6621
|
-
const initHttpOptions = {};
|
|
6622
|
-
if (this.clientOptions.vertexai) {
|
|
6623
|
-
initHttpOptions.apiVersion =
|
|
6624
|
-
(_a = this.clientOptions.apiVersion) !== null && _a !== void 0 ? _a : VERTEX_AI_API_DEFAULT_VERSION;
|
|
6625
|
-
initHttpOptions.baseUrl = this.baseUrlFromProjectLocation();
|
|
6626
|
-
this.normalizeAuthParameters();
|
|
6627
|
-
}
|
|
6628
|
-
else {
|
|
6629
|
-
// Gemini API
|
|
6630
|
-
initHttpOptions.apiVersion =
|
|
6631
|
-
(_b = this.clientOptions.apiVersion) !== null && _b !== void 0 ? _b : GOOGLE_AI_API_DEFAULT_VERSION;
|
|
6632
|
-
initHttpOptions.baseUrl = `https://generativelanguage.googleapis.com/`;
|
|
6633
|
-
}
|
|
6634
|
-
initHttpOptions.headers = this.getDefaultHeaders();
|
|
6635
|
-
this.clientOptions.httpOptions = initHttpOptions;
|
|
6636
|
-
if (opts.httpOptions) {
|
|
6637
|
-
this.clientOptions.httpOptions = this.patchHttpOptions(initHttpOptions, opts.httpOptions);
|
|
6638
|
-
}
|
|
6639
|
-
}
|
|
6640
|
-
/**
|
|
6641
|
-
* Determines the base URL for Vertex AI based on project and location.
|
|
6642
|
-
* Uses the global endpoint if location is 'global' or if project/location
|
|
6643
|
-
* are not specified (implying API key usage).
|
|
6644
|
-
* @private
|
|
6645
|
-
*/
|
|
6646
|
-
baseUrlFromProjectLocation() {
|
|
6647
|
-
if (this.clientOptions.project &&
|
|
6648
|
-
this.clientOptions.location &&
|
|
6649
|
-
this.clientOptions.location !== 'global') {
|
|
6650
|
-
// Regional endpoint
|
|
6651
|
-
return `https://${this.clientOptions.location}-aiplatform.googleapis.com/`;
|
|
6652
|
-
}
|
|
6653
|
-
// Global endpoint (covers 'global' location and API key usage)
|
|
6654
|
-
return `https://aiplatform.googleapis.com/`;
|
|
6655
|
-
}
|
|
6656
|
-
/**
|
|
6657
|
-
* Normalizes authentication parameters for Vertex AI.
|
|
6658
|
-
* If project and location are provided, API key is cleared.
|
|
6659
|
-
* If project and location are not provided (implying API key usage),
|
|
6660
|
-
* project and location are cleared.
|
|
6661
|
-
* @private
|
|
6662
|
-
*/
|
|
6663
|
-
normalizeAuthParameters() {
|
|
6664
|
-
if (this.clientOptions.project && this.clientOptions.location) {
|
|
6665
|
-
// Using project/location for auth, clear potential API key
|
|
6666
|
-
this.clientOptions.apiKey = undefined;
|
|
6667
|
-
return;
|
|
6668
|
-
}
|
|
6669
|
-
// Using API key for auth (or no auth provided yet), clear project/location
|
|
6670
|
-
this.clientOptions.project = undefined;
|
|
6671
|
-
this.clientOptions.location = undefined;
|
|
6605
|
+
function createFileSearchStoreParametersToMldev(fromObject) {
|
|
6606
|
+
const toObject = {};
|
|
6607
|
+
const fromConfig = getValueByPath(fromObject, ['config']);
|
|
6608
|
+
if (fromConfig != null) {
|
|
6609
|
+
createFileSearchStoreConfigToMldev(fromConfig, toObject);
|
|
6672
6610
|
}
|
|
6673
|
-
|
|
6674
|
-
|
|
6675
|
-
|
|
6611
|
+
return toObject;
|
|
6612
|
+
}
|
|
6613
|
+
function deleteFileSearchStoreConfigToMldev(fromObject, parentObject) {
|
|
6614
|
+
const toObject = {};
|
|
6615
|
+
const fromForce = getValueByPath(fromObject, ['force']);
|
|
6616
|
+
if (parentObject !== undefined && fromForce != null) {
|
|
6617
|
+
setValueByPath(parentObject, ['_query', 'force'], fromForce);
|
|
6676
6618
|
}
|
|
6677
|
-
|
|
6678
|
-
|
|
6619
|
+
return toObject;
|
|
6620
|
+
}
|
|
6621
|
+
function deleteFileSearchStoreParametersToMldev(fromObject) {
|
|
6622
|
+
const toObject = {};
|
|
6623
|
+
const fromName = getValueByPath(fromObject, ['name']);
|
|
6624
|
+
if (fromName != null) {
|
|
6625
|
+
setValueByPath(toObject, ['_url', 'name'], fromName);
|
|
6679
6626
|
}
|
|
6680
|
-
|
|
6681
|
-
|
|
6627
|
+
const fromConfig = getValueByPath(fromObject, ['config']);
|
|
6628
|
+
if (fromConfig != null) {
|
|
6629
|
+
deleteFileSearchStoreConfigToMldev(fromConfig, toObject);
|
|
6682
6630
|
}
|
|
6683
|
-
|
|
6684
|
-
|
|
6685
|
-
|
|
6686
|
-
|
|
6687
|
-
|
|
6688
|
-
|
|
6631
|
+
return toObject;
|
|
6632
|
+
}
|
|
6633
|
+
function getFileSearchStoreParametersToMldev(fromObject) {
|
|
6634
|
+
const toObject = {};
|
|
6635
|
+
const fromName = getValueByPath(fromObject, ['name']);
|
|
6636
|
+
if (fromName != null) {
|
|
6637
|
+
setValueByPath(toObject, ['_url', 'name'], fromName);
|
|
6689
6638
|
}
|
|
6690
|
-
|
|
6691
|
-
|
|
6692
|
-
|
|
6693
|
-
|
|
6639
|
+
return toObject;
|
|
6640
|
+
}
|
|
6641
|
+
function importFileConfigToMldev(fromObject, parentObject) {
|
|
6642
|
+
const toObject = {};
|
|
6643
|
+
const fromCustomMetadata = getValueByPath(fromObject, [
|
|
6644
|
+
'customMetadata',
|
|
6645
|
+
]);
|
|
6646
|
+
if (parentObject !== undefined && fromCustomMetadata != null) {
|
|
6647
|
+
let transformedList = fromCustomMetadata;
|
|
6648
|
+
if (Array.isArray(transformedList)) {
|
|
6649
|
+
transformedList = transformedList.map((item) => {
|
|
6650
|
+
return item;
|
|
6651
|
+
});
|
|
6694
6652
|
}
|
|
6695
|
-
|
|
6653
|
+
setValueByPath(parentObject, ['customMetadata'], transformedList);
|
|
6696
6654
|
}
|
|
6697
|
-
|
|
6698
|
-
|
|
6655
|
+
const fromChunkingConfig = getValueByPath(fromObject, [
|
|
6656
|
+
'chunkingConfig',
|
|
6657
|
+
]);
|
|
6658
|
+
if (parentObject !== undefined && fromChunkingConfig != null) {
|
|
6659
|
+
setValueByPath(parentObject, ['chunkingConfig'], fromChunkingConfig);
|
|
6699
6660
|
}
|
|
6700
|
-
|
|
6701
|
-
|
|
6702
|
-
|
|
6703
|
-
|
|
6704
|
-
|
|
6705
|
-
|
|
6706
|
-
|
|
6707
|
-
}
|
|
6661
|
+
return toObject;
|
|
6662
|
+
}
|
|
6663
|
+
function importFileOperationFromMldev(fromObject) {
|
|
6664
|
+
const toObject = {};
|
|
6665
|
+
const fromName = getValueByPath(fromObject, ['name']);
|
|
6666
|
+
if (fromName != null) {
|
|
6667
|
+
setValueByPath(toObject, ['name'], fromName);
|
|
6708
6668
|
}
|
|
6709
|
-
|
|
6710
|
-
|
|
6711
|
-
|
|
6712
|
-
httpOptions.apiVersion === undefined) {
|
|
6713
|
-
throw new Error('HTTP options are not correctly set.');
|
|
6714
|
-
}
|
|
6715
|
-
const baseUrl = httpOptions.baseUrl.endsWith('/')
|
|
6716
|
-
? httpOptions.baseUrl.slice(0, -1)
|
|
6717
|
-
: httpOptions.baseUrl;
|
|
6718
|
-
const urlElement = [baseUrl];
|
|
6719
|
-
if (httpOptions.apiVersion && httpOptions.apiVersion !== '') {
|
|
6720
|
-
urlElement.push(httpOptions.apiVersion);
|
|
6721
|
-
}
|
|
6722
|
-
return urlElement.join('/');
|
|
6669
|
+
const fromMetadata = getValueByPath(fromObject, ['metadata']);
|
|
6670
|
+
if (fromMetadata != null) {
|
|
6671
|
+
setValueByPath(toObject, ['metadata'], fromMetadata);
|
|
6723
6672
|
}
|
|
6724
|
-
|
|
6725
|
-
|
|
6673
|
+
const fromDone = getValueByPath(fromObject, ['done']);
|
|
6674
|
+
if (fromDone != null) {
|
|
6675
|
+
setValueByPath(toObject, ['done'], fromDone);
|
|
6726
6676
|
}
|
|
6727
|
-
|
|
6728
|
-
|
|
6677
|
+
const fromError = getValueByPath(fromObject, ['error']);
|
|
6678
|
+
if (fromError != null) {
|
|
6679
|
+
setValueByPath(toObject, ['error'], fromError);
|
|
6729
6680
|
}
|
|
6730
|
-
|
|
6731
|
-
|
|
6732
|
-
|
|
6733
|
-
urlParts.protocol = urlParts.protocol == 'http:' ? 'ws' : 'wss';
|
|
6734
|
-
return urlParts.toString();
|
|
6681
|
+
const fromResponse = getValueByPath(fromObject, ['response']);
|
|
6682
|
+
if (fromResponse != null) {
|
|
6683
|
+
setValueByPath(toObject, ['response'], importFileResponseFromMldev(fromResponse));
|
|
6735
6684
|
}
|
|
6736
|
-
|
|
6737
|
-
|
|
6738
|
-
|
|
6739
|
-
|
|
6740
|
-
|
|
6741
|
-
|
|
6742
|
-
|
|
6685
|
+
return toObject;
|
|
6686
|
+
}
|
|
6687
|
+
function importFileParametersToMldev(fromObject) {
|
|
6688
|
+
const toObject = {};
|
|
6689
|
+
const fromFileSearchStoreName = getValueByPath(fromObject, [
|
|
6690
|
+
'fileSearchStoreName',
|
|
6691
|
+
]);
|
|
6692
|
+
if (fromFileSearchStoreName != null) {
|
|
6693
|
+
setValueByPath(toObject, ['_url', 'file_search_store_name'], fromFileSearchStoreName);
|
|
6743
6694
|
}
|
|
6744
|
-
|
|
6745
|
-
|
|
6746
|
-
|
|
6747
|
-
urlElement.push(this.getBaseResourcePath());
|
|
6748
|
-
}
|
|
6749
|
-
if (path !== '') {
|
|
6750
|
-
urlElement.push(path);
|
|
6751
|
-
}
|
|
6752
|
-
const url = new URL(`${urlElement.join('/')}`);
|
|
6753
|
-
return url;
|
|
6754
|
-
}
|
|
6755
|
-
shouldPrependVertexProjectPath(request) {
|
|
6756
|
-
if (this.clientOptions.apiKey) {
|
|
6757
|
-
return false;
|
|
6758
|
-
}
|
|
6759
|
-
if (!this.clientOptions.vertexai) {
|
|
6760
|
-
return false;
|
|
6761
|
-
}
|
|
6762
|
-
if (request.path.startsWith('projects/')) {
|
|
6763
|
-
// Assume the path already starts with
|
|
6764
|
-
// `projects/<project>/location/<location>`.
|
|
6765
|
-
return false;
|
|
6766
|
-
}
|
|
6767
|
-
if (request.httpMethod === 'GET' &&
|
|
6768
|
-
request.path.startsWith('publishers/google/models')) {
|
|
6769
|
-
// These paths are used by Vertex's models.get and models.list
|
|
6770
|
-
// calls. For base models Vertex does not accept a project/location
|
|
6771
|
-
// prefix (for tuned model the prefix is required).
|
|
6772
|
-
return false;
|
|
6773
|
-
}
|
|
6774
|
-
return true;
|
|
6695
|
+
const fromFileName = getValueByPath(fromObject, ['fileName']);
|
|
6696
|
+
if (fromFileName != null) {
|
|
6697
|
+
setValueByPath(toObject, ['fileName'], fromFileName);
|
|
6775
6698
|
}
|
|
6776
|
-
|
|
6777
|
-
|
|
6778
|
-
|
|
6779
|
-
patchedHttpOptions = this.patchHttpOptions(this.clientOptions.httpOptions, request.httpOptions);
|
|
6780
|
-
}
|
|
6781
|
-
const prependProjectLocation = this.shouldPrependVertexProjectPath(request);
|
|
6782
|
-
const url = this.constructUrl(request.path, patchedHttpOptions, prependProjectLocation);
|
|
6783
|
-
if (request.queryParams) {
|
|
6784
|
-
for (const [key, value] of Object.entries(request.queryParams)) {
|
|
6785
|
-
url.searchParams.append(key, String(value));
|
|
6786
|
-
}
|
|
6787
|
-
}
|
|
6788
|
-
let requestInit = {};
|
|
6789
|
-
if (request.httpMethod === 'GET') {
|
|
6790
|
-
if (request.body && request.body !== '{}') {
|
|
6791
|
-
throw new Error('Request body should be empty for GET request, but got non empty request body');
|
|
6792
|
-
}
|
|
6793
|
-
}
|
|
6794
|
-
else {
|
|
6795
|
-
requestInit.body = request.body;
|
|
6796
|
-
}
|
|
6797
|
-
requestInit = await this.includeExtraHttpOptionsToRequestInit(requestInit, patchedHttpOptions, url.toString(), request.abortSignal);
|
|
6798
|
-
return this.unaryApiCall(url, requestInit, request.httpMethod);
|
|
6699
|
+
const fromConfig = getValueByPath(fromObject, ['config']);
|
|
6700
|
+
if (fromConfig != null) {
|
|
6701
|
+
importFileConfigToMldev(fromConfig, toObject);
|
|
6799
6702
|
}
|
|
6800
|
-
|
|
6801
|
-
|
|
6802
|
-
|
|
6803
|
-
|
|
6804
|
-
|
|
6805
|
-
|
|
6806
|
-
|
|
6807
|
-
|
|
6808
|
-
|
|
6809
|
-
}
|
|
6810
|
-
else if (value !== undefined) {
|
|
6811
|
-
// @ts-expect-error TS2345TS7053: Element implicitly has an 'any' type
|
|
6812
|
-
// because expression of type 'string' can't be used to index type
|
|
6813
|
-
// 'HttpOptions'.
|
|
6814
|
-
patchedHttpOptions[key] = value;
|
|
6815
|
-
}
|
|
6816
|
-
}
|
|
6817
|
-
return patchedHttpOptions;
|
|
6703
|
+
return toObject;
|
|
6704
|
+
}
|
|
6705
|
+
function importFileResponseFromMldev(fromObject) {
|
|
6706
|
+
const toObject = {};
|
|
6707
|
+
const fromSdkHttpResponse = getValueByPath(fromObject, [
|
|
6708
|
+
'sdkHttpResponse',
|
|
6709
|
+
]);
|
|
6710
|
+
if (fromSdkHttpResponse != null) {
|
|
6711
|
+
setValueByPath(toObject, ['sdkHttpResponse'], fromSdkHttpResponse);
|
|
6818
6712
|
}
|
|
6819
|
-
|
|
6820
|
-
|
|
6821
|
-
|
|
6822
|
-
patchedHttpOptions = this.patchHttpOptions(this.clientOptions.httpOptions, request.httpOptions);
|
|
6823
|
-
}
|
|
6824
|
-
const prependProjectLocation = this.shouldPrependVertexProjectPath(request);
|
|
6825
|
-
const url = this.constructUrl(request.path, patchedHttpOptions, prependProjectLocation);
|
|
6826
|
-
if (!url.searchParams.has('alt') || url.searchParams.get('alt') !== 'sse') {
|
|
6827
|
-
url.searchParams.set('alt', 'sse');
|
|
6828
|
-
}
|
|
6829
|
-
let requestInit = {};
|
|
6830
|
-
requestInit.body = request.body;
|
|
6831
|
-
requestInit = await this.includeExtraHttpOptionsToRequestInit(requestInit, patchedHttpOptions, url.toString(), request.abortSignal);
|
|
6832
|
-
return this.streamApiCall(url, requestInit, request.httpMethod);
|
|
6713
|
+
const fromParent = getValueByPath(fromObject, ['parent']);
|
|
6714
|
+
if (fromParent != null) {
|
|
6715
|
+
setValueByPath(toObject, ['parent'], fromParent);
|
|
6833
6716
|
}
|
|
6834
|
-
|
|
6835
|
-
|
|
6836
|
-
|
|
6837
|
-
const signal = abortController.signal;
|
|
6838
|
-
if (httpOptions.timeout && (httpOptions === null || httpOptions === void 0 ? void 0 : httpOptions.timeout) > 0) {
|
|
6839
|
-
const timeoutHandle = setTimeout(() => abortController.abort(), httpOptions.timeout);
|
|
6840
|
-
if (timeoutHandle &&
|
|
6841
|
-
typeof timeoutHandle.unref ===
|
|
6842
|
-
'function') {
|
|
6843
|
-
// call unref to prevent nodejs process from hanging, see
|
|
6844
|
-
// https://nodejs.org/api/timers.html#timeoutunref
|
|
6845
|
-
timeoutHandle.unref();
|
|
6846
|
-
}
|
|
6847
|
-
}
|
|
6848
|
-
if (abortSignal) {
|
|
6849
|
-
abortSignal.addEventListener('abort', () => {
|
|
6850
|
-
abortController.abort();
|
|
6851
|
-
});
|
|
6852
|
-
}
|
|
6853
|
-
requestInit.signal = signal;
|
|
6854
|
-
}
|
|
6855
|
-
if (httpOptions && httpOptions.extraBody !== null) {
|
|
6856
|
-
includeExtraBodyToRequestInit(requestInit, httpOptions.extraBody);
|
|
6857
|
-
}
|
|
6858
|
-
requestInit.headers = await this.getHeadersInternal(httpOptions, url);
|
|
6859
|
-
return requestInit;
|
|
6717
|
+
const fromDocumentName = getValueByPath(fromObject, ['documentName']);
|
|
6718
|
+
if (fromDocumentName != null) {
|
|
6719
|
+
setValueByPath(toObject, ['documentName'], fromDocumentName);
|
|
6860
6720
|
}
|
|
6861
|
-
|
|
6862
|
-
|
|
6863
|
-
|
|
6864
|
-
|
|
6865
|
-
|
|
6866
|
-
|
|
6867
|
-
|
|
6868
|
-
if (e instanceof Error) {
|
|
6869
|
-
throw e;
|
|
6870
|
-
}
|
|
6871
|
-
else {
|
|
6872
|
-
throw new Error(JSON.stringify(e));
|
|
6873
|
-
}
|
|
6874
|
-
});
|
|
6721
|
+
return toObject;
|
|
6722
|
+
}
|
|
6723
|
+
function listFileSearchStoresConfigToMldev(fromObject, parentObject) {
|
|
6724
|
+
const toObject = {};
|
|
6725
|
+
const fromPageSize = getValueByPath(fromObject, ['pageSize']);
|
|
6726
|
+
if (parentObject !== undefined && fromPageSize != null) {
|
|
6727
|
+
setValueByPath(parentObject, ['_query', 'pageSize'], fromPageSize);
|
|
6875
6728
|
}
|
|
6876
|
-
|
|
6877
|
-
|
|
6878
|
-
|
|
6879
|
-
await throwErrorIfNotOK(response);
|
|
6880
|
-
return this.processStreamResponse(response);
|
|
6881
|
-
})
|
|
6882
|
-
.catch((e) => {
|
|
6883
|
-
if (e instanceof Error) {
|
|
6884
|
-
throw e;
|
|
6885
|
-
}
|
|
6886
|
-
else {
|
|
6887
|
-
throw new Error(JSON.stringify(e));
|
|
6888
|
-
}
|
|
6889
|
-
});
|
|
6729
|
+
const fromPageToken = getValueByPath(fromObject, ['pageToken']);
|
|
6730
|
+
if (parentObject !== undefined && fromPageToken != null) {
|
|
6731
|
+
setValueByPath(parentObject, ['_query', 'pageToken'], fromPageToken);
|
|
6890
6732
|
}
|
|
6891
|
-
|
|
6892
|
-
|
|
6893
|
-
|
|
6894
|
-
|
|
6895
|
-
|
|
6896
|
-
|
|
6897
|
-
|
|
6898
|
-
}
|
|
6899
|
-
try {
|
|
6900
|
-
let buffer = '';
|
|
6901
|
-
while (true) {
|
|
6902
|
-
const { done, value } = yield __await(reader.read());
|
|
6903
|
-
if (done) {
|
|
6904
|
-
if (buffer.trim().length > 0) {
|
|
6905
|
-
throw new Error('Incomplete JSON segment at the end');
|
|
6906
|
-
}
|
|
6907
|
-
break;
|
|
6908
|
-
}
|
|
6909
|
-
const chunkString = decoder.decode(value, { stream: true });
|
|
6910
|
-
// Parse and throw an error if the chunk contains an error.
|
|
6911
|
-
try {
|
|
6912
|
-
const chunkJson = JSON.parse(chunkString);
|
|
6913
|
-
if ('error' in chunkJson) {
|
|
6914
|
-
const errorJson = JSON.parse(JSON.stringify(chunkJson['error']));
|
|
6915
|
-
const status = errorJson['status'];
|
|
6916
|
-
const code = errorJson['code'];
|
|
6917
|
-
const errorMessage = `got status: ${status}. ${JSON.stringify(chunkJson)}`;
|
|
6918
|
-
if (code >= 400 && code < 600) {
|
|
6919
|
-
const apiError = new ApiError({
|
|
6920
|
-
message: errorMessage,
|
|
6921
|
-
status: code,
|
|
6922
|
-
});
|
|
6923
|
-
throw apiError;
|
|
6924
|
-
}
|
|
6925
|
-
}
|
|
6926
|
-
}
|
|
6927
|
-
catch (e) {
|
|
6928
|
-
const error = e;
|
|
6929
|
-
if (error.name === 'ApiError') {
|
|
6930
|
-
throw e;
|
|
6931
|
-
}
|
|
6932
|
-
}
|
|
6933
|
-
buffer += chunkString;
|
|
6934
|
-
let match = buffer.match(responseLineRE);
|
|
6935
|
-
while (match) {
|
|
6936
|
-
const processedChunkString = match[1];
|
|
6937
|
-
try {
|
|
6938
|
-
const partialResponse = new Response(processedChunkString, {
|
|
6939
|
-
headers: response === null || response === void 0 ? void 0 : response.headers,
|
|
6940
|
-
status: response === null || response === void 0 ? void 0 : response.status,
|
|
6941
|
-
statusText: response === null || response === void 0 ? void 0 : response.statusText,
|
|
6942
|
-
});
|
|
6943
|
-
yield yield __await(new HttpResponse(partialResponse));
|
|
6944
|
-
buffer = buffer.slice(match[0].length);
|
|
6945
|
-
match = buffer.match(responseLineRE);
|
|
6946
|
-
}
|
|
6947
|
-
catch (e) {
|
|
6948
|
-
throw new Error(`exception parsing stream chunk ${processedChunkString}. ${e}`);
|
|
6949
|
-
}
|
|
6950
|
-
}
|
|
6951
|
-
}
|
|
6952
|
-
}
|
|
6953
|
-
finally {
|
|
6954
|
-
reader.releaseLock();
|
|
6955
|
-
}
|
|
6956
|
-
});
|
|
6733
|
+
return toObject;
|
|
6734
|
+
}
|
|
6735
|
+
function listFileSearchStoresParametersToMldev(fromObject) {
|
|
6736
|
+
const toObject = {};
|
|
6737
|
+
const fromConfig = getValueByPath(fromObject, ['config']);
|
|
6738
|
+
if (fromConfig != null) {
|
|
6739
|
+
listFileSearchStoresConfigToMldev(fromConfig, toObject);
|
|
6957
6740
|
}
|
|
6958
|
-
|
|
6959
|
-
|
|
6960
|
-
|
|
6961
|
-
|
|
6741
|
+
return toObject;
|
|
6742
|
+
}
|
|
6743
|
+
function listFileSearchStoresResponseFromMldev(fromObject) {
|
|
6744
|
+
const toObject = {};
|
|
6745
|
+
const fromSdkHttpResponse = getValueByPath(fromObject, [
|
|
6746
|
+
'sdkHttpResponse',
|
|
6747
|
+
]);
|
|
6748
|
+
if (fromSdkHttpResponse != null) {
|
|
6749
|
+
setValueByPath(toObject, ['sdkHttpResponse'], fromSdkHttpResponse);
|
|
6962
6750
|
}
|
|
6963
|
-
|
|
6964
|
-
|
|
6965
|
-
|
|
6966
|
-
|
|
6967
|
-
|
|
6968
|
-
headers[CONTENT_TYPE_HEADER] = 'application/json';
|
|
6969
|
-
return headers;
|
|
6751
|
+
const fromNextPageToken = getValueByPath(fromObject, [
|
|
6752
|
+
'nextPageToken',
|
|
6753
|
+
]);
|
|
6754
|
+
if (fromNextPageToken != null) {
|
|
6755
|
+
setValueByPath(toObject, ['nextPageToken'], fromNextPageToken);
|
|
6970
6756
|
}
|
|
6971
|
-
|
|
6972
|
-
|
|
6973
|
-
|
|
6974
|
-
|
|
6975
|
-
|
|
6976
|
-
|
|
6977
|
-
|
|
6978
|
-
|
|
6979
|
-
|
|
6980
|
-
headers.append(SERVER_TIMEOUT_HEADER, String(Math.ceil(httpOptions.timeout / 1000)));
|
|
6981
|
-
}
|
|
6757
|
+
const fromFileSearchStores = getValueByPath(fromObject, [
|
|
6758
|
+
'fileSearchStores',
|
|
6759
|
+
]);
|
|
6760
|
+
if (fromFileSearchStores != null) {
|
|
6761
|
+
let transformedList = fromFileSearchStores;
|
|
6762
|
+
if (Array.isArray(transformedList)) {
|
|
6763
|
+
transformedList = transformedList.map((item) => {
|
|
6764
|
+
return item;
|
|
6765
|
+
});
|
|
6982
6766
|
}
|
|
6983
|
-
|
|
6984
|
-
return headers;
|
|
6767
|
+
setValueByPath(toObject, ['fileSearchStores'], transformedList);
|
|
6985
6768
|
}
|
|
6986
|
-
|
|
6987
|
-
|
|
6988
|
-
|
|
6989
|
-
|
|
6990
|
-
|
|
6991
|
-
|
|
6769
|
+
return toObject;
|
|
6770
|
+
}
|
|
6771
|
+
function uploadToFileSearchStoreConfigToMldev(fromObject, parentObject) {
|
|
6772
|
+
const toObject = {};
|
|
6773
|
+
const fromMimeType = getValueByPath(fromObject, ['mimeType']);
|
|
6774
|
+
if (parentObject !== undefined && fromMimeType != null) {
|
|
6775
|
+
setValueByPath(parentObject, ['mimeType'], fromMimeType);
|
|
6776
|
+
}
|
|
6777
|
+
const fromDisplayName = getValueByPath(fromObject, ['displayName']);
|
|
6778
|
+
if (parentObject !== undefined && fromDisplayName != null) {
|
|
6779
|
+
setValueByPath(parentObject, ['displayName'], fromDisplayName);
|
|
6780
|
+
}
|
|
6781
|
+
const fromCustomMetadata = getValueByPath(fromObject, [
|
|
6782
|
+
'customMetadata',
|
|
6783
|
+
]);
|
|
6784
|
+
if (parentObject !== undefined && fromCustomMetadata != null) {
|
|
6785
|
+
let transformedList = fromCustomMetadata;
|
|
6786
|
+
if (Array.isArray(transformedList)) {
|
|
6787
|
+
transformedList = transformedList.map((item) => {
|
|
6788
|
+
return item;
|
|
6789
|
+
});
|
|
6992
6790
|
}
|
|
6993
|
-
|
|
6791
|
+
setValueByPath(parentObject, ['customMetadata'], transformedList);
|
|
6994
6792
|
}
|
|
6995
|
-
|
|
6996
|
-
|
|
6997
|
-
|
|
6998
|
-
|
|
6999
|
-
|
|
7000
|
-
|
|
7001
|
-
|
|
7002
|
-
|
|
7003
|
-
|
|
7004
|
-
|
|
7005
|
-
|
|
7006
|
-
|
|
7007
|
-
|
|
7008
|
-
|
|
7009
|
-
|
|
7010
|
-
|
|
7011
|
-
|
|
7012
|
-
|
|
6793
|
+
const fromChunkingConfig = getValueByPath(fromObject, [
|
|
6794
|
+
'chunkingConfig',
|
|
6795
|
+
]);
|
|
6796
|
+
if (parentObject !== undefined && fromChunkingConfig != null) {
|
|
6797
|
+
setValueByPath(parentObject, ['chunkingConfig'], fromChunkingConfig);
|
|
6798
|
+
}
|
|
6799
|
+
return toObject;
|
|
6800
|
+
}
|
|
6801
|
+
function uploadToFileSearchStoreParametersToMldev(fromObject) {
|
|
6802
|
+
const toObject = {};
|
|
6803
|
+
const fromFileSearchStoreName = getValueByPath(fromObject, [
|
|
6804
|
+
'fileSearchStoreName',
|
|
6805
|
+
]);
|
|
6806
|
+
if (fromFileSearchStoreName != null) {
|
|
6807
|
+
setValueByPath(toObject, ['_url', 'file_search_store_name'], fromFileSearchStoreName);
|
|
6808
|
+
}
|
|
6809
|
+
const fromConfig = getValueByPath(fromObject, ['config']);
|
|
6810
|
+
if (fromConfig != null) {
|
|
6811
|
+
uploadToFileSearchStoreConfigToMldev(fromConfig, toObject);
|
|
6812
|
+
}
|
|
6813
|
+
return toObject;
|
|
6814
|
+
}
|
|
6815
|
+
function uploadToFileSearchStoreResumableResponseFromMldev(fromObject) {
|
|
6816
|
+
const toObject = {};
|
|
6817
|
+
const fromSdkHttpResponse = getValueByPath(fromObject, [
|
|
6818
|
+
'sdkHttpResponse',
|
|
6819
|
+
]);
|
|
6820
|
+
if (fromSdkHttpResponse != null) {
|
|
6821
|
+
setValueByPath(toObject, ['sdkHttpResponse'], fromSdkHttpResponse);
|
|
6822
|
+
}
|
|
6823
|
+
return toObject;
|
|
6824
|
+
}
|
|
6825
|
+
|
|
6826
|
+
/**
|
|
6827
|
+
* @license
|
|
6828
|
+
* Copyright 2025 Google LLC
|
|
6829
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
6830
|
+
*/
|
|
6831
|
+
/**
|
|
6832
|
+
* API errors raised by the GenAI API.
|
|
6833
|
+
*/
|
|
6834
|
+
class ApiError extends Error {
|
|
6835
|
+
constructor(options) {
|
|
6836
|
+
super(options.message);
|
|
6837
|
+
this.name = 'ApiError';
|
|
6838
|
+
this.status = options.status;
|
|
6839
|
+
Object.setPrototypeOf(this, ApiError.prototype);
|
|
6840
|
+
}
|
|
6841
|
+
}
|
|
6842
|
+
|
|
6843
|
+
/**
|
|
6844
|
+
* @license
|
|
6845
|
+
* Copyright 2025 Google LLC
|
|
6846
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
6847
|
+
*/
|
|
6848
|
+
const CONTENT_TYPE_HEADER = 'Content-Type';
|
|
6849
|
+
const SERVER_TIMEOUT_HEADER = 'X-Server-Timeout';
|
|
6850
|
+
const USER_AGENT_HEADER = 'User-Agent';
|
|
6851
|
+
const GOOGLE_API_CLIENT_HEADER = 'x-goog-api-client';
|
|
6852
|
+
const SDK_VERSION = '1.32.0'; // x-release-please-version
|
|
6853
|
+
const LIBRARY_LABEL = `google-genai-sdk/${SDK_VERSION}`;
|
|
6854
|
+
const VERTEX_AI_API_DEFAULT_VERSION = 'v1beta1';
|
|
6855
|
+
const GOOGLE_AI_API_DEFAULT_VERSION = 'v1beta';
|
|
6856
|
+
/**
|
|
6857
|
+
* The ApiClient class is used to send requests to the Gemini API or Vertex AI
|
|
6858
|
+
* endpoints.
|
|
6859
|
+
*/
|
|
6860
|
+
class ApiClient {
|
|
6861
|
+
constructor(opts) {
|
|
6862
|
+
var _a, _b;
|
|
6863
|
+
this.clientOptions = Object.assign(Object.assign({}, opts), { project: opts.project, location: opts.location, apiKey: opts.apiKey, vertexai: opts.vertexai });
|
|
6864
|
+
const initHttpOptions = {};
|
|
6865
|
+
if (this.clientOptions.vertexai) {
|
|
6866
|
+
initHttpOptions.apiVersion =
|
|
6867
|
+
(_a = this.clientOptions.apiVersion) !== null && _a !== void 0 ? _a : VERTEX_AI_API_DEFAULT_VERSION;
|
|
6868
|
+
initHttpOptions.baseUrl = this.baseUrlFromProjectLocation();
|
|
6869
|
+
this.normalizeAuthParameters();
|
|
7013
6870
|
}
|
|
7014
|
-
|
|
7015
|
-
|
|
6871
|
+
else {
|
|
6872
|
+
// Gemini API
|
|
6873
|
+
initHttpOptions.apiVersion =
|
|
6874
|
+
(_b = this.clientOptions.apiVersion) !== null && _b !== void 0 ? _b : GOOGLE_AI_API_DEFAULT_VERSION;
|
|
6875
|
+
initHttpOptions.baseUrl = `https://generativelanguage.googleapis.com/`;
|
|
7016
6876
|
}
|
|
7017
|
-
|
|
7018
|
-
|
|
7019
|
-
|
|
7020
|
-
|
|
7021
|
-
if (mimeType === undefined || mimeType === '') {
|
|
7022
|
-
throw new Error('Can not determine mimeType. Please provide mimeType in the config.');
|
|
6877
|
+
initHttpOptions.headers = this.getDefaultHeaders();
|
|
6878
|
+
this.clientOptions.httpOptions = initHttpOptions;
|
|
6879
|
+
if (opts.httpOptions) {
|
|
6880
|
+
this.clientOptions.httpOptions = this.patchHttpOptions(initHttpOptions, opts.httpOptions);
|
|
7023
6881
|
}
|
|
7024
|
-
fileToUpload.mimeType = mimeType;
|
|
7025
|
-
const body = {
|
|
7026
|
-
file: fileToUpload,
|
|
7027
|
-
};
|
|
7028
|
-
const fileName = this.getFileName(file);
|
|
7029
|
-
const path = formatMap('upload/v1beta/files', body['_url']);
|
|
7030
|
-
const uploadUrl = await this.fetchUploadUrl(path, fileToUpload.sizeBytes, fileToUpload.mimeType, fileName, body, config === null || config === void 0 ? void 0 : config.httpOptions);
|
|
7031
|
-
return uploader.upload(file, uploadUrl, this);
|
|
7032
6882
|
}
|
|
7033
6883
|
/**
|
|
7034
|
-
*
|
|
7035
|
-
*
|
|
7036
|
-
*
|
|
7037
|
-
* @
|
|
7038
|
-
* @param file The string path to the file to be uploaded or a Blob object.
|
|
7039
|
-
* @param config Optional parameters specified in the `UploadFileConfig`
|
|
7040
|
-
* interface. @see {@link UploadFileConfig}
|
|
7041
|
-
* @return A promise that resolves to a `File` object.
|
|
7042
|
-
* @throws An error if called on a Vertex AI client.
|
|
7043
|
-
* @throws An error if the `mimeType` is not provided and can not be inferred,
|
|
6884
|
+
* Determines the base URL for Vertex AI based on project and location.
|
|
6885
|
+
* Uses the global endpoint if location is 'global' or if project/location
|
|
6886
|
+
* are not specified (implying API key usage).
|
|
6887
|
+
* @private
|
|
7044
6888
|
*/
|
|
7045
|
-
|
|
7046
|
-
|
|
7047
|
-
|
|
7048
|
-
|
|
7049
|
-
|
|
7050
|
-
|
|
7051
|
-
if (mimeType === undefined || mimeType === '') {
|
|
7052
|
-
throw new Error('Can not determine mimeType. Please provide mimeType in the config.');
|
|
7053
|
-
}
|
|
7054
|
-
const path = `upload/v1beta/${fileSearchStoreName}:uploadToFileSearchStore`;
|
|
7055
|
-
const fileName = this.getFileName(file);
|
|
7056
|
-
const body = {};
|
|
7057
|
-
if (config === null || config === void 0 ? void 0 : config.customMetadata) {
|
|
7058
|
-
body['customMetadata'] = config.customMetadata;
|
|
7059
|
-
}
|
|
7060
|
-
if (config === null || config === void 0 ? void 0 : config.chunkingConfig) {
|
|
7061
|
-
body['chunkingConfig'] = config.chunkingConfig;
|
|
6889
|
+
baseUrlFromProjectLocation() {
|
|
6890
|
+
if (this.clientOptions.project &&
|
|
6891
|
+
this.clientOptions.location &&
|
|
6892
|
+
this.clientOptions.location !== 'global') {
|
|
6893
|
+
// Regional endpoint
|
|
6894
|
+
return `https://${this.clientOptions.location}-aiplatform.googleapis.com/`;
|
|
7062
6895
|
}
|
|
7063
|
-
|
|
7064
|
-
return
|
|
6896
|
+
// Global endpoint (covers 'global' location and API key usage)
|
|
6897
|
+
return `https://aiplatform.googleapis.com/`;
|
|
7065
6898
|
}
|
|
7066
6899
|
/**
|
|
7067
|
-
*
|
|
7068
|
-
*
|
|
7069
|
-
*
|
|
7070
|
-
*
|
|
6900
|
+
* Normalizes authentication parameters for Vertex AI.
|
|
6901
|
+
* If project and location are provided, API key is cleared.
|
|
6902
|
+
* If project and location are not provided (implying API key usage),
|
|
6903
|
+
* project and location are cleared.
|
|
6904
|
+
* @private
|
|
7071
6905
|
*/
|
|
7072
|
-
|
|
7073
|
-
|
|
7074
|
-
|
|
6906
|
+
normalizeAuthParameters() {
|
|
6907
|
+
if (this.clientOptions.project && this.clientOptions.location) {
|
|
6908
|
+
// Using project/location for auth, clear potential API key
|
|
6909
|
+
this.clientOptions.apiKey = undefined;
|
|
6910
|
+
return;
|
|
6911
|
+
}
|
|
6912
|
+
// Using API key for auth (or no auth provided yet), clear project/location
|
|
6913
|
+
this.clientOptions.project = undefined;
|
|
6914
|
+
this.clientOptions.location = undefined;
|
|
7075
6915
|
}
|
|
7076
|
-
|
|
6916
|
+
isVertexAI() {
|
|
7077
6917
|
var _a;
|
|
7078
|
-
|
|
7079
|
-
|
|
7080
|
-
|
|
7081
|
-
|
|
7082
|
-
|
|
7083
|
-
|
|
7084
|
-
|
|
7085
|
-
|
|
7086
|
-
|
|
7087
|
-
|
|
7088
|
-
|
|
7089
|
-
|
|
7090
|
-
body: JSON.stringify(body),
|
|
7091
|
-
httpMethod: 'POST',
|
|
7092
|
-
httpOptions,
|
|
7093
|
-
});
|
|
7094
|
-
if (!httpResponse || !(httpResponse === null || httpResponse === void 0 ? void 0 : httpResponse.headers)) {
|
|
7095
|
-
throw new Error('Server did not return an HttpResponse or the returned HttpResponse did not have headers.');
|
|
6918
|
+
return (_a = this.clientOptions.vertexai) !== null && _a !== void 0 ? _a : false;
|
|
6919
|
+
}
|
|
6920
|
+
getProject() {
|
|
6921
|
+
return this.clientOptions.project;
|
|
6922
|
+
}
|
|
6923
|
+
getLocation() {
|
|
6924
|
+
return this.clientOptions.location;
|
|
6925
|
+
}
|
|
6926
|
+
getApiVersion() {
|
|
6927
|
+
if (this.clientOptions.httpOptions &&
|
|
6928
|
+
this.clientOptions.httpOptions.apiVersion !== undefined) {
|
|
6929
|
+
return this.clientOptions.httpOptions.apiVersion;
|
|
7096
6930
|
}
|
|
7097
|
-
|
|
7098
|
-
|
|
7099
|
-
|
|
6931
|
+
throw new Error('API version is not set.');
|
|
6932
|
+
}
|
|
6933
|
+
getBaseUrl() {
|
|
6934
|
+
if (this.clientOptions.httpOptions &&
|
|
6935
|
+
this.clientOptions.httpOptions.baseUrl !== undefined) {
|
|
6936
|
+
return this.clientOptions.httpOptions.baseUrl;
|
|
7100
6937
|
}
|
|
7101
|
-
|
|
6938
|
+
throw new Error('Base URL is not set.');
|
|
7102
6939
|
}
|
|
7103
|
-
|
|
7104
|
-
|
|
7105
|
-
var _a;
|
|
7106
|
-
if (response === undefined) {
|
|
7107
|
-
throw new Error('response is undefined');
|
|
6940
|
+
getRequestUrl() {
|
|
6941
|
+
return this.getRequestUrlInternal(this.clientOptions.httpOptions);
|
|
7108
6942
|
}
|
|
7109
|
-
|
|
7110
|
-
|
|
7111
|
-
|
|
7112
|
-
|
|
7113
|
-
errorBody = await response.json();
|
|
6943
|
+
getHeaders() {
|
|
6944
|
+
if (this.clientOptions.httpOptions &&
|
|
6945
|
+
this.clientOptions.httpOptions.headers !== undefined) {
|
|
6946
|
+
return this.clientOptions.httpOptions.headers;
|
|
7114
6947
|
}
|
|
7115
6948
|
else {
|
|
7116
|
-
|
|
7117
|
-
error: {
|
|
7118
|
-
message: await response.text(),
|
|
7119
|
-
code: response.status,
|
|
7120
|
-
status: response.statusText,
|
|
7121
|
-
},
|
|
7122
|
-
};
|
|
7123
|
-
}
|
|
7124
|
-
const errorMessage = JSON.stringify(errorBody);
|
|
7125
|
-
if (status >= 400 && status < 600) {
|
|
7126
|
-
const apiError = new ApiError({
|
|
7127
|
-
message: errorMessage,
|
|
7128
|
-
status: status,
|
|
7129
|
-
});
|
|
7130
|
-
throw apiError;
|
|
6949
|
+
throw new Error('Headers are not set.');
|
|
7131
6950
|
}
|
|
7132
|
-
throw new Error(errorMessage);
|
|
7133
|
-
}
|
|
7134
|
-
}
|
|
7135
|
-
/**
|
|
7136
|
-
* Recursively updates the `requestInit.body` with values from an `extraBody` object.
|
|
7137
|
-
*
|
|
7138
|
-
* If `requestInit.body` is a string, it's assumed to be JSON and will be parsed.
|
|
7139
|
-
* The `extraBody` is then deeply merged into this parsed object.
|
|
7140
|
-
* If `requestInit.body` is a Blob, `extraBody` will be ignored, and a warning logged,
|
|
7141
|
-
* as merging structured data into an opaque Blob is not supported.
|
|
7142
|
-
*
|
|
7143
|
-
* The function does not enforce that updated values from `extraBody` have the
|
|
7144
|
-
* same type as existing values in `requestInit.body`. Type mismatches during
|
|
7145
|
-
* the merge will result in a warning, but the value from `extraBody` will overwrite
|
|
7146
|
-
* the original. `extraBody` users are responsible for ensuring `extraBody` has the correct structure.
|
|
7147
|
-
*
|
|
7148
|
-
* @param requestInit The RequestInit object whose body will be updated.
|
|
7149
|
-
* @param extraBody The object containing updates to be merged into `requestInit.body`.
|
|
7150
|
-
*/
|
|
7151
|
-
function includeExtraBodyToRequestInit(requestInit, extraBody) {
|
|
7152
|
-
if (!extraBody || Object.keys(extraBody).length === 0) {
|
|
7153
|
-
return;
|
|
7154
|
-
}
|
|
7155
|
-
if (requestInit.body instanceof Blob) {
|
|
7156
|
-
console.warn('includeExtraBodyToRequestInit: extraBody provided but current request body is a Blob. extraBody will be ignored as merging is not supported for Blob bodies.');
|
|
7157
|
-
return;
|
|
7158
6951
|
}
|
|
7159
|
-
|
|
7160
|
-
|
|
7161
|
-
|
|
7162
|
-
|
|
7163
|
-
|
|
7164
|
-
const parsedBody = JSON.parse(requestInit.body);
|
|
7165
|
-
if (typeof parsedBody === 'object' &&
|
|
7166
|
-
parsedBody !== null &&
|
|
7167
|
-
!Array.isArray(parsedBody)) {
|
|
7168
|
-
currentBodyObject = parsedBody;
|
|
7169
|
-
}
|
|
7170
|
-
else {
|
|
7171
|
-
console.warn('includeExtraBodyToRequestInit: Original request body is valid JSON but not a non-array object. Skip applying extraBody to the request body.');
|
|
7172
|
-
return;
|
|
7173
|
-
}
|
|
7174
|
-
/* eslint-disable-next-line @typescript-eslint/no-unused-vars */
|
|
6952
|
+
getRequestUrlInternal(httpOptions) {
|
|
6953
|
+
if (!httpOptions ||
|
|
6954
|
+
httpOptions.baseUrl === undefined ||
|
|
6955
|
+
httpOptions.apiVersion === undefined) {
|
|
6956
|
+
throw new Error('HTTP options are not correctly set.');
|
|
7175
6957
|
}
|
|
7176
|
-
|
|
7177
|
-
|
|
7178
|
-
|
|
6958
|
+
const baseUrl = httpOptions.baseUrl.endsWith('/')
|
|
6959
|
+
? httpOptions.baseUrl.slice(0, -1)
|
|
6960
|
+
: httpOptions.baseUrl;
|
|
6961
|
+
const urlElement = [baseUrl];
|
|
6962
|
+
if (httpOptions.apiVersion && httpOptions.apiVersion !== '') {
|
|
6963
|
+
urlElement.push(httpOptions.apiVersion);
|
|
7179
6964
|
}
|
|
6965
|
+
return urlElement.join('/');
|
|
7180
6966
|
}
|
|
7181
|
-
|
|
7182
|
-
|
|
7183
|
-
for (const key in source) {
|
|
7184
|
-
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
7185
|
-
const sourceValue = source[key];
|
|
7186
|
-
const targetValue = output[key];
|
|
7187
|
-
if (sourceValue &&
|
|
7188
|
-
typeof sourceValue === 'object' &&
|
|
7189
|
-
!Array.isArray(sourceValue) &&
|
|
7190
|
-
targetValue &&
|
|
7191
|
-
typeof targetValue === 'object' &&
|
|
7192
|
-
!Array.isArray(targetValue)) {
|
|
7193
|
-
output[key] = deepMerge(targetValue, sourceValue);
|
|
7194
|
-
}
|
|
7195
|
-
else {
|
|
7196
|
-
if (targetValue &&
|
|
7197
|
-
sourceValue &&
|
|
7198
|
-
typeof targetValue !== typeof sourceValue) {
|
|
7199
|
-
console.warn(`includeExtraBodyToRequestInit:deepMerge: Type mismatch for key "${key}". Original type: ${typeof targetValue}, New type: ${typeof sourceValue}. Overwriting.`);
|
|
7200
|
-
}
|
|
7201
|
-
output[key] = sourceValue;
|
|
7202
|
-
}
|
|
7203
|
-
}
|
|
7204
|
-
}
|
|
7205
|
-
return output;
|
|
6967
|
+
getBaseResourcePath() {
|
|
6968
|
+
return `projects/${this.clientOptions.project}/locations/${this.clientOptions.location}`;
|
|
7206
6969
|
}
|
|
7207
|
-
|
|
7208
|
-
|
|
7209
|
-
}
|
|
7210
|
-
|
|
7211
|
-
/**
|
|
7212
|
-
* @license
|
|
7213
|
-
* Copyright 2025 Google LLC
|
|
7214
|
-
* SPDX-License-Identifier: Apache-2.0
|
|
7215
|
-
*/
|
|
7216
|
-
function crossError() {
|
|
7217
|
-
// TODO(b/399934880): this message needs a link to a help page explaining how to enable conditional exports
|
|
7218
|
-
return new Error(`This feature requires the web or Node specific @google/genai implementation, you can fix this by either:
|
|
7219
|
-
|
|
7220
|
-
*Enabling conditional exports for your project [recommended]*
|
|
7221
|
-
|
|
7222
|
-
*Using a platform specific import* - Make sure your code imports either '@google/genai/web' or '@google/genai/node' instead of '@google/genai'.
|
|
7223
|
-
`);
|
|
7224
|
-
}
|
|
7225
|
-
|
|
7226
|
-
/**
|
|
7227
|
-
* @license
|
|
7228
|
-
* Copyright 2025 Google LLC
|
|
7229
|
-
* SPDX-License-Identifier: Apache-2.0
|
|
7230
|
-
*/
|
|
7231
|
-
class CrossDownloader {
|
|
7232
|
-
async download(_params, _apiClient) {
|
|
7233
|
-
throw crossError();
|
|
6970
|
+
getApiKey() {
|
|
6971
|
+
return this.clientOptions.apiKey;
|
|
7234
6972
|
}
|
|
7235
|
-
|
|
7236
|
-
|
|
7237
|
-
const
|
|
7238
|
-
|
|
7239
|
-
|
|
7240
|
-
|
|
7241
|
-
|
|
7242
|
-
|
|
7243
|
-
|
|
7244
|
-
if (typeof file === 'string') {
|
|
7245
|
-
throw crossError();
|
|
6973
|
+
getWebsocketBaseUrl() {
|
|
6974
|
+
const baseUrl = this.getBaseUrl();
|
|
6975
|
+
const urlParts = new URL(baseUrl);
|
|
6976
|
+
urlParts.protocol = urlParts.protocol == 'http:' ? 'ws' : 'wss';
|
|
6977
|
+
return urlParts.toString();
|
|
6978
|
+
}
|
|
6979
|
+
setBaseUrl(url) {
|
|
6980
|
+
if (this.clientOptions.httpOptions) {
|
|
6981
|
+
this.clientOptions.httpOptions.baseUrl = url;
|
|
7246
6982
|
}
|
|
7247
6983
|
else {
|
|
7248
|
-
|
|
6984
|
+
throw new Error('HTTP options are not correctly set.');
|
|
7249
6985
|
}
|
|
7250
6986
|
}
|
|
7251
|
-
|
|
7252
|
-
|
|
7253
|
-
|
|
6987
|
+
constructUrl(path, httpOptions, prependProjectLocation) {
|
|
6988
|
+
const urlElement = [this.getRequestUrlInternal(httpOptions)];
|
|
6989
|
+
if (prependProjectLocation) {
|
|
6990
|
+
urlElement.push(this.getBaseResourcePath());
|
|
7254
6991
|
}
|
|
7255
|
-
|
|
7256
|
-
|
|
6992
|
+
if (path !== '') {
|
|
6993
|
+
urlElement.push(path);
|
|
7257
6994
|
}
|
|
6995
|
+
const url = new URL(`${urlElement.join('/')}`);
|
|
6996
|
+
return url;
|
|
7258
6997
|
}
|
|
7259
|
-
|
|
7260
|
-
if (
|
|
7261
|
-
|
|
6998
|
+
shouldPrependVertexProjectPath(request) {
|
|
6999
|
+
if (this.clientOptions.apiKey) {
|
|
7000
|
+
return false;
|
|
7262
7001
|
}
|
|
7263
|
-
|
|
7264
|
-
return
|
|
7002
|
+
if (!this.clientOptions.vertexai) {
|
|
7003
|
+
return false;
|
|
7265
7004
|
}
|
|
7005
|
+
if (request.path.startsWith('projects/')) {
|
|
7006
|
+
// Assume the path already starts with
|
|
7007
|
+
// `projects/<project>/location/<location>`.
|
|
7008
|
+
return false;
|
|
7009
|
+
}
|
|
7010
|
+
if (request.httpMethod === 'GET' &&
|
|
7011
|
+
request.path.startsWith('publishers/google/models')) {
|
|
7012
|
+
// These paths are used by Vertex's models.get and models.list
|
|
7013
|
+
// calls. For base models Vertex does not accept a project/location
|
|
7014
|
+
// prefix (for tuned model the prefix is required).
|
|
7015
|
+
return false;
|
|
7016
|
+
}
|
|
7017
|
+
return true;
|
|
7266
7018
|
}
|
|
7267
|
-
|
|
7268
|
-
|
|
7269
|
-
|
|
7270
|
-
|
|
7271
|
-
const responseJson = (await (response === null || response === void 0 ? void 0 : response.json()));
|
|
7272
|
-
if (((_a = response === null || response === void 0 ? void 0 : response.headers) === null || _a === void 0 ? void 0 : _a[X_GOOG_UPLOAD_STATUS_HEADER_FIELD]) !== 'final') {
|
|
7273
|
-
throw new Error('Failed to upload file: Upload status is not finalized.');
|
|
7274
|
-
}
|
|
7275
|
-
return responseJson['file'];
|
|
7276
|
-
}
|
|
7277
|
-
async function uploadBlobToFileSearchStore(file, uploadUrl, apiClient) {
|
|
7278
|
-
var _a;
|
|
7279
|
-
const response = await uploadBlobInternal(file, uploadUrl, apiClient);
|
|
7280
|
-
const responseJson = (await (response === null || response === void 0 ? void 0 : response.json()));
|
|
7281
|
-
if (((_a = response === null || response === void 0 ? void 0 : response.headers) === null || _a === void 0 ? void 0 : _a[X_GOOG_UPLOAD_STATUS_HEADER_FIELD]) !== 'final') {
|
|
7282
|
-
throw new Error('Failed to upload file: Upload status is not finalized.');
|
|
7283
|
-
}
|
|
7284
|
-
const resp = uploadToFileSearchStoreOperationFromMldev(responseJson);
|
|
7285
|
-
const typedResp = new UploadToFileSearchStoreOperation();
|
|
7286
|
-
Object.assign(typedResp, resp);
|
|
7287
|
-
return typedResp;
|
|
7288
|
-
}
|
|
7289
|
-
async function uploadBlobInternal(file, uploadUrl, apiClient) {
|
|
7290
|
-
var _a, _b;
|
|
7291
|
-
let fileSize = 0;
|
|
7292
|
-
let offset = 0;
|
|
7293
|
-
let response = new HttpResponse(new Response());
|
|
7294
|
-
let uploadCommand = 'upload';
|
|
7295
|
-
fileSize = file.size;
|
|
7296
|
-
while (offset < fileSize) {
|
|
7297
|
-
const chunkSize = Math.min(MAX_CHUNK_SIZE, fileSize - offset);
|
|
7298
|
-
const chunk = file.slice(offset, offset + chunkSize);
|
|
7299
|
-
if (offset + chunkSize >= fileSize) {
|
|
7300
|
-
uploadCommand += ', finalize';
|
|
7019
|
+
async request(request) {
|
|
7020
|
+
let patchedHttpOptions = this.clientOptions.httpOptions;
|
|
7021
|
+
if (request.httpOptions) {
|
|
7022
|
+
patchedHttpOptions = this.patchHttpOptions(this.clientOptions.httpOptions, request.httpOptions);
|
|
7301
7023
|
}
|
|
7302
|
-
|
|
7303
|
-
|
|
7304
|
-
|
|
7305
|
-
|
|
7306
|
-
|
|
7307
|
-
body: chunk,
|
|
7308
|
-
httpMethod: 'POST',
|
|
7309
|
-
httpOptions: {
|
|
7310
|
-
apiVersion: '',
|
|
7311
|
-
baseUrl: uploadUrl,
|
|
7312
|
-
headers: {
|
|
7313
|
-
'X-Goog-Upload-Command': uploadCommand,
|
|
7314
|
-
'X-Goog-Upload-Offset': String(offset),
|
|
7315
|
-
'Content-Length': String(chunkSize),
|
|
7316
|
-
},
|
|
7317
|
-
},
|
|
7318
|
-
});
|
|
7319
|
-
if ((_a = response === null || response === void 0 ? void 0 : response.headers) === null || _a === void 0 ? void 0 : _a[X_GOOG_UPLOAD_STATUS_HEADER_FIELD]) {
|
|
7320
|
-
break;
|
|
7024
|
+
const prependProjectLocation = this.shouldPrependVertexProjectPath(request);
|
|
7025
|
+
const url = this.constructUrl(request.path, patchedHttpOptions, prependProjectLocation);
|
|
7026
|
+
if (request.queryParams) {
|
|
7027
|
+
for (const [key, value] of Object.entries(request.queryParams)) {
|
|
7028
|
+
url.searchParams.append(key, String(value));
|
|
7321
7029
|
}
|
|
7322
|
-
retryCount++;
|
|
7323
|
-
await sleep(currentDelayMs);
|
|
7324
|
-
currentDelayMs = currentDelayMs * DELAY_MULTIPLIER;
|
|
7325
7030
|
}
|
|
7326
|
-
|
|
7327
|
-
|
|
7328
|
-
|
|
7329
|
-
|
|
7330
|
-
|
|
7031
|
+
let requestInit = {};
|
|
7032
|
+
if (request.httpMethod === 'GET') {
|
|
7033
|
+
if (request.body && request.body !== '{}') {
|
|
7034
|
+
throw new Error('Request body should be empty for GET request, but got non empty request body');
|
|
7035
|
+
}
|
|
7331
7036
|
}
|
|
7332
|
-
|
|
7333
|
-
|
|
7334
|
-
if (fileSize <= offset) {
|
|
7335
|
-
throw new Error('All content has been uploaded, but the upload status is not finalized.');
|
|
7037
|
+
else {
|
|
7038
|
+
requestInit.body = request.body;
|
|
7336
7039
|
}
|
|
7040
|
+
requestInit = await this.includeExtraHttpOptionsToRequestInit(requestInit, patchedHttpOptions, url.toString(), request.abortSignal);
|
|
7041
|
+
return this.unaryApiCall(url, requestInit, request.httpMethod);
|
|
7337
7042
|
}
|
|
7338
|
-
|
|
7339
|
-
|
|
7340
|
-
|
|
7341
|
-
|
|
7342
|
-
|
|
7343
|
-
|
|
7344
|
-
|
|
7345
|
-
|
|
7346
|
-
}
|
|
7347
|
-
|
|
7348
|
-
|
|
7349
|
-
|
|
7350
|
-
|
|
7351
|
-
|
|
7352
|
-
|
|
7353
|
-
|
|
7354
|
-
|
|
7355
|
-
|
|
7356
|
-
}
|
|
7357
|
-
}
|
|
7358
|
-
|
|
7359
|
-
/**
|
|
7360
|
-
* @license
|
|
7361
|
-
* Copyright 2025 Google LLC
|
|
7362
|
-
* SPDX-License-Identifier: Apache-2.0
|
|
7363
|
-
*/
|
|
7364
|
-
// Code generated by the Google Gen AI SDK generator DO NOT EDIT.
|
|
7365
|
-
function createFileParametersToMldev(fromObject) {
|
|
7366
|
-
const toObject = {};
|
|
7367
|
-
const fromFile = getValueByPath(fromObject, ['file']);
|
|
7368
|
-
if (fromFile != null) {
|
|
7369
|
-
setValueByPath(toObject, ['file'], fromFile);
|
|
7370
|
-
}
|
|
7371
|
-
return toObject;
|
|
7372
|
-
}
|
|
7373
|
-
function createFileResponseFromMldev(fromObject) {
|
|
7374
|
-
const toObject = {};
|
|
7375
|
-
const fromSdkHttpResponse = getValueByPath(fromObject, [
|
|
7376
|
-
'sdkHttpResponse',
|
|
7377
|
-
]);
|
|
7378
|
-
if (fromSdkHttpResponse != null) {
|
|
7379
|
-
setValueByPath(toObject, ['sdkHttpResponse'], fromSdkHttpResponse);
|
|
7043
|
+
patchHttpOptions(baseHttpOptions, requestHttpOptions) {
|
|
7044
|
+
const patchedHttpOptions = JSON.parse(JSON.stringify(baseHttpOptions));
|
|
7045
|
+
for (const [key, value] of Object.entries(requestHttpOptions)) {
|
|
7046
|
+
// Records compile to objects.
|
|
7047
|
+
if (typeof value === 'object') {
|
|
7048
|
+
// @ts-expect-error TS2345TS7053: Element implicitly has an 'any' type
|
|
7049
|
+
// because expression of type 'string' can't be used to index type
|
|
7050
|
+
// 'HttpOptions'.
|
|
7051
|
+
patchedHttpOptions[key] = Object.assign(Object.assign({}, patchedHttpOptions[key]), value);
|
|
7052
|
+
}
|
|
7053
|
+
else if (value !== undefined) {
|
|
7054
|
+
// @ts-expect-error TS2345TS7053: Element implicitly has an 'any' type
|
|
7055
|
+
// because expression of type 'string' can't be used to index type
|
|
7056
|
+
// 'HttpOptions'.
|
|
7057
|
+
patchedHttpOptions[key] = value;
|
|
7058
|
+
}
|
|
7059
|
+
}
|
|
7060
|
+
return patchedHttpOptions;
|
|
7380
7061
|
}
|
|
7381
|
-
|
|
7382
|
-
|
|
7383
|
-
|
|
7384
|
-
|
|
7385
|
-
|
|
7386
|
-
|
|
7387
|
-
|
|
7062
|
+
async requestStream(request) {
|
|
7063
|
+
let patchedHttpOptions = this.clientOptions.httpOptions;
|
|
7064
|
+
if (request.httpOptions) {
|
|
7065
|
+
patchedHttpOptions = this.patchHttpOptions(this.clientOptions.httpOptions, request.httpOptions);
|
|
7066
|
+
}
|
|
7067
|
+
const prependProjectLocation = this.shouldPrependVertexProjectPath(request);
|
|
7068
|
+
const url = this.constructUrl(request.path, patchedHttpOptions, prependProjectLocation);
|
|
7069
|
+
if (!url.searchParams.has('alt') || url.searchParams.get('alt') !== 'sse') {
|
|
7070
|
+
url.searchParams.set('alt', 'sse');
|
|
7071
|
+
}
|
|
7072
|
+
let requestInit = {};
|
|
7073
|
+
requestInit.body = request.body;
|
|
7074
|
+
requestInit = await this.includeExtraHttpOptionsToRequestInit(requestInit, patchedHttpOptions, url.toString(), request.abortSignal);
|
|
7075
|
+
return this.streamApiCall(url, requestInit, request.httpMethod);
|
|
7388
7076
|
}
|
|
7389
|
-
|
|
7390
|
-
|
|
7391
|
-
|
|
7392
|
-
|
|
7393
|
-
|
|
7394
|
-
|
|
7395
|
-
|
|
7396
|
-
|
|
7397
|
-
|
|
7077
|
+
async includeExtraHttpOptionsToRequestInit(requestInit, httpOptions, url, abortSignal) {
|
|
7078
|
+
if ((httpOptions && httpOptions.timeout) || abortSignal) {
|
|
7079
|
+
const abortController = new AbortController();
|
|
7080
|
+
const signal = abortController.signal;
|
|
7081
|
+
if (httpOptions.timeout && (httpOptions === null || httpOptions === void 0 ? void 0 : httpOptions.timeout) > 0) {
|
|
7082
|
+
const timeoutHandle = setTimeout(() => abortController.abort(), httpOptions.timeout);
|
|
7083
|
+
if (timeoutHandle &&
|
|
7084
|
+
typeof timeoutHandle.unref ===
|
|
7085
|
+
'function') {
|
|
7086
|
+
// call unref to prevent nodejs process from hanging, see
|
|
7087
|
+
// https://nodejs.org/api/timers.html#timeoutunref
|
|
7088
|
+
timeoutHandle.unref();
|
|
7089
|
+
}
|
|
7090
|
+
}
|
|
7091
|
+
if (abortSignal) {
|
|
7092
|
+
abortSignal.addEventListener('abort', () => {
|
|
7093
|
+
abortController.abort();
|
|
7094
|
+
});
|
|
7095
|
+
}
|
|
7096
|
+
requestInit.signal = signal;
|
|
7097
|
+
}
|
|
7098
|
+
if (httpOptions && httpOptions.extraBody !== null) {
|
|
7099
|
+
includeExtraBodyToRequestInit(requestInit, httpOptions.extraBody);
|
|
7100
|
+
}
|
|
7101
|
+
requestInit.headers = await this.getHeadersInternal(httpOptions, url);
|
|
7102
|
+
return requestInit;
|
|
7398
7103
|
}
|
|
7399
|
-
|
|
7400
|
-
}
|
|
7401
|
-
|
|
7402
|
-
|
|
7403
|
-
|
|
7404
|
-
|
|
7405
|
-
|
|
7104
|
+
async unaryApiCall(url, requestInit, httpMethod) {
|
|
7105
|
+
return this.apiCall(url.toString(), Object.assign(Object.assign({}, requestInit), { method: httpMethod }))
|
|
7106
|
+
.then(async (response) => {
|
|
7107
|
+
await throwErrorIfNotOK(response);
|
|
7108
|
+
return new HttpResponse(response);
|
|
7109
|
+
})
|
|
7110
|
+
.catch((e) => {
|
|
7111
|
+
if (e instanceof Error) {
|
|
7112
|
+
throw e;
|
|
7113
|
+
}
|
|
7114
|
+
else {
|
|
7115
|
+
throw new Error(JSON.stringify(e));
|
|
7116
|
+
}
|
|
7117
|
+
});
|
|
7406
7118
|
}
|
|
7407
|
-
|
|
7408
|
-
}
|
|
7409
|
-
|
|
7410
|
-
|
|
7411
|
-
|
|
7412
|
-
|
|
7413
|
-
|
|
7119
|
+
async streamApiCall(url, requestInit, httpMethod) {
|
|
7120
|
+
return this.apiCall(url.toString(), Object.assign(Object.assign({}, requestInit), { method: httpMethod }))
|
|
7121
|
+
.then(async (response) => {
|
|
7122
|
+
await throwErrorIfNotOK(response);
|
|
7123
|
+
return this.processStreamResponse(response);
|
|
7124
|
+
})
|
|
7125
|
+
.catch((e) => {
|
|
7126
|
+
if (e instanceof Error) {
|
|
7127
|
+
throw e;
|
|
7128
|
+
}
|
|
7129
|
+
else {
|
|
7130
|
+
throw new Error(JSON.stringify(e));
|
|
7131
|
+
}
|
|
7132
|
+
});
|
|
7414
7133
|
}
|
|
7415
|
-
|
|
7416
|
-
|
|
7417
|
-
|
|
7134
|
+
processStreamResponse(response) {
|
|
7135
|
+
return __asyncGenerator(this, arguments, function* processStreamResponse_1() {
|
|
7136
|
+
var _a;
|
|
7137
|
+
const reader = (_a = response === null || response === void 0 ? void 0 : response.body) === null || _a === void 0 ? void 0 : _a.getReader();
|
|
7138
|
+
const decoder = new TextDecoder('utf-8');
|
|
7139
|
+
if (!reader) {
|
|
7140
|
+
throw new Error('Response body is empty');
|
|
7141
|
+
}
|
|
7142
|
+
try {
|
|
7143
|
+
let buffer = '';
|
|
7144
|
+
const dataPrefix = 'data:';
|
|
7145
|
+
const delimiters = ['\n\n', '\r\r', '\r\n\r\n'];
|
|
7146
|
+
while (true) {
|
|
7147
|
+
const { done, value } = yield __await(reader.read());
|
|
7148
|
+
if (done) {
|
|
7149
|
+
if (buffer.trim().length > 0) {
|
|
7150
|
+
throw new Error('Incomplete JSON segment at the end');
|
|
7151
|
+
}
|
|
7152
|
+
break;
|
|
7153
|
+
}
|
|
7154
|
+
const chunkString = decoder.decode(value, { stream: true });
|
|
7155
|
+
// Parse and throw an error if the chunk contains an error.
|
|
7156
|
+
try {
|
|
7157
|
+
const chunkJson = JSON.parse(chunkString);
|
|
7158
|
+
if ('error' in chunkJson) {
|
|
7159
|
+
const errorJson = JSON.parse(JSON.stringify(chunkJson['error']));
|
|
7160
|
+
const status = errorJson['status'];
|
|
7161
|
+
const code = errorJson['code'];
|
|
7162
|
+
const errorMessage = `got status: ${status}. ${JSON.stringify(chunkJson)}`;
|
|
7163
|
+
if (code >= 400 && code < 600) {
|
|
7164
|
+
const apiError = new ApiError({
|
|
7165
|
+
message: errorMessage,
|
|
7166
|
+
status: code,
|
|
7167
|
+
});
|
|
7168
|
+
throw apiError;
|
|
7169
|
+
}
|
|
7170
|
+
}
|
|
7171
|
+
}
|
|
7172
|
+
catch (e) {
|
|
7173
|
+
const error = e;
|
|
7174
|
+
if (error.name === 'ApiError') {
|
|
7175
|
+
throw e;
|
|
7176
|
+
}
|
|
7177
|
+
}
|
|
7178
|
+
buffer += chunkString;
|
|
7179
|
+
let delimiterIndex = -1;
|
|
7180
|
+
let delimiterLength = 0;
|
|
7181
|
+
while (true) {
|
|
7182
|
+
delimiterIndex = -1;
|
|
7183
|
+
delimiterLength = 0;
|
|
7184
|
+
for (const delimiter of delimiters) {
|
|
7185
|
+
const index = buffer.indexOf(delimiter);
|
|
7186
|
+
if (index !== -1 &&
|
|
7187
|
+
(delimiterIndex === -1 || index < delimiterIndex)) {
|
|
7188
|
+
delimiterIndex = index;
|
|
7189
|
+
delimiterLength = delimiter.length;
|
|
7190
|
+
}
|
|
7191
|
+
}
|
|
7192
|
+
if (delimiterIndex === -1) {
|
|
7193
|
+
break; // No complete event in buffer
|
|
7194
|
+
}
|
|
7195
|
+
const eventString = buffer.substring(0, delimiterIndex);
|
|
7196
|
+
buffer = buffer.substring(delimiterIndex + delimiterLength);
|
|
7197
|
+
const trimmedEvent = eventString.trim();
|
|
7198
|
+
if (trimmedEvent.startsWith(dataPrefix)) {
|
|
7199
|
+
const processedChunkString = trimmedEvent
|
|
7200
|
+
.substring(dataPrefix.length)
|
|
7201
|
+
.trim();
|
|
7202
|
+
try {
|
|
7203
|
+
const partialResponse = new Response(processedChunkString, {
|
|
7204
|
+
headers: response === null || response === void 0 ? void 0 : response.headers,
|
|
7205
|
+
status: response === null || response === void 0 ? void 0 : response.status,
|
|
7206
|
+
statusText: response === null || response === void 0 ? void 0 : response.statusText,
|
|
7207
|
+
});
|
|
7208
|
+
yield yield __await(new HttpResponse(partialResponse));
|
|
7209
|
+
}
|
|
7210
|
+
catch (e) {
|
|
7211
|
+
throw new Error(`exception parsing stream chunk ${processedChunkString}. ${e}`);
|
|
7212
|
+
}
|
|
7213
|
+
}
|
|
7214
|
+
}
|
|
7215
|
+
}
|
|
7216
|
+
}
|
|
7217
|
+
finally {
|
|
7218
|
+
reader.releaseLock();
|
|
7219
|
+
}
|
|
7220
|
+
});
|
|
7418
7221
|
}
|
|
7419
|
-
|
|
7420
|
-
|
|
7421
|
-
|
|
7422
|
-
|
|
7423
|
-
const fromConfig = getValueByPath(fromObject, ['config']);
|
|
7424
|
-
if (fromConfig != null) {
|
|
7425
|
-
listFilesConfigToMldev(fromConfig, toObject);
|
|
7222
|
+
async apiCall(url, requestInit) {
|
|
7223
|
+
return fetch(url, requestInit).catch((e) => {
|
|
7224
|
+
throw new Error(`exception ${e} sending request`);
|
|
7225
|
+
});
|
|
7426
7226
|
}
|
|
7427
|
-
|
|
7428
|
-
}
|
|
7429
|
-
|
|
7430
|
-
|
|
7431
|
-
|
|
7432
|
-
'
|
|
7433
|
-
|
|
7434
|
-
if (fromSdkHttpResponse != null) {
|
|
7435
|
-
setValueByPath(toObject, ['sdkHttpResponse'], fromSdkHttpResponse);
|
|
7227
|
+
getDefaultHeaders() {
|
|
7228
|
+
const headers = {};
|
|
7229
|
+
const versionHeaderValue = LIBRARY_LABEL + ' ' + this.clientOptions.userAgentExtra;
|
|
7230
|
+
headers[USER_AGENT_HEADER] = versionHeaderValue;
|
|
7231
|
+
headers[GOOGLE_API_CLIENT_HEADER] = versionHeaderValue;
|
|
7232
|
+
headers[CONTENT_TYPE_HEADER] = 'application/json';
|
|
7233
|
+
return headers;
|
|
7436
7234
|
}
|
|
7437
|
-
|
|
7438
|
-
|
|
7439
|
-
|
|
7440
|
-
|
|
7441
|
-
|
|
7235
|
+
async getHeadersInternal(httpOptions, url) {
|
|
7236
|
+
const headers = new Headers();
|
|
7237
|
+
if (httpOptions && httpOptions.headers) {
|
|
7238
|
+
for (const [key, value] of Object.entries(httpOptions.headers)) {
|
|
7239
|
+
headers.append(key, value);
|
|
7240
|
+
}
|
|
7241
|
+
// Append a timeout header if it is set, note that the timeout option is
|
|
7242
|
+
// in milliseconds but the header is in seconds.
|
|
7243
|
+
if (httpOptions.timeout && httpOptions.timeout > 0) {
|
|
7244
|
+
headers.append(SERVER_TIMEOUT_HEADER, String(Math.ceil(httpOptions.timeout / 1000)));
|
|
7245
|
+
}
|
|
7246
|
+
}
|
|
7247
|
+
await this.clientOptions.auth.addAuthHeaders(headers, url);
|
|
7248
|
+
return headers;
|
|
7442
7249
|
}
|
|
7443
|
-
|
|
7444
|
-
|
|
7445
|
-
let
|
|
7446
|
-
if (
|
|
7447
|
-
|
|
7448
|
-
|
|
7449
|
-
});
|
|
7250
|
+
getFileName(file) {
|
|
7251
|
+
var _a;
|
|
7252
|
+
let fileName = '';
|
|
7253
|
+
if (typeof file === 'string') {
|
|
7254
|
+
fileName = file.replace(/[/\\]+$/, '');
|
|
7255
|
+
fileName = (_a = fileName.split(/[/\\]/).pop()) !== null && _a !== void 0 ? _a : '';
|
|
7450
7256
|
}
|
|
7451
|
-
|
|
7257
|
+
return fileName;
|
|
7452
7258
|
}
|
|
7453
|
-
|
|
7454
|
-
|
|
7455
|
-
|
|
7456
|
-
|
|
7457
|
-
|
|
7458
|
-
|
|
7459
|
-
|
|
7460
|
-
|
|
7461
|
-
|
|
7462
|
-
|
|
7463
|
-
|
|
7464
|
-
|
|
7465
|
-
|
|
7466
|
-
|
|
7467
|
-
|
|
7468
|
-
|
|
7469
|
-
|
|
7470
|
-
|
|
7471
|
-
|
|
7472
|
-
|
|
7473
|
-
|
|
7474
|
-
|
|
7475
|
-
|
|
7476
|
-
|
|
7477
|
-
|
|
7478
|
-
|
|
7479
|
-
|
|
7480
|
-
|
|
7481
|
-
|
|
7482
|
-
|
|
7483
|
-
|
|
7259
|
+
/**
|
|
7260
|
+
* Uploads a file asynchronously using Gemini API only, this is not supported
|
|
7261
|
+
* in Vertex AI.
|
|
7262
|
+
*
|
|
7263
|
+
* @param file The string path to the file to be uploaded or a Blob object.
|
|
7264
|
+
* @param config Optional parameters specified in the `UploadFileConfig`
|
|
7265
|
+
* interface. @see {@link types.UploadFileConfig}
|
|
7266
|
+
* @return A promise that resolves to a `File` object.
|
|
7267
|
+
* @throws An error if called on a Vertex AI client.
|
|
7268
|
+
* @throws An error if the `mimeType` is not provided and can not be inferred,
|
|
7269
|
+
*/
|
|
7270
|
+
async uploadFile(file, config) {
|
|
7271
|
+
var _a;
|
|
7272
|
+
const fileToUpload = {};
|
|
7273
|
+
if (config != null) {
|
|
7274
|
+
fileToUpload.mimeType = config.mimeType;
|
|
7275
|
+
fileToUpload.name = config.name;
|
|
7276
|
+
fileToUpload.displayName = config.displayName;
|
|
7277
|
+
}
|
|
7278
|
+
if (fileToUpload.name && !fileToUpload.name.startsWith('files/')) {
|
|
7279
|
+
fileToUpload.name = `files/${fileToUpload.name}`;
|
|
7280
|
+
}
|
|
7281
|
+
const uploader = this.clientOptions.uploader;
|
|
7282
|
+
const fileStat = await uploader.stat(file);
|
|
7283
|
+
fileToUpload.sizeBytes = String(fileStat.size);
|
|
7284
|
+
const mimeType = (_a = config === null || config === void 0 ? void 0 : config.mimeType) !== null && _a !== void 0 ? _a : fileStat.type;
|
|
7285
|
+
if (mimeType === undefined || mimeType === '') {
|
|
7286
|
+
throw new Error('Can not determine mimeType. Please provide mimeType in the config.');
|
|
7287
|
+
}
|
|
7288
|
+
fileToUpload.mimeType = mimeType;
|
|
7289
|
+
const body = {
|
|
7290
|
+
file: fileToUpload,
|
|
7484
7291
|
};
|
|
7292
|
+
const fileName = this.getFileName(file);
|
|
7293
|
+
const path = formatMap('upload/v1beta/files', body['_url']);
|
|
7294
|
+
const uploadUrl = await this.fetchUploadUrl(path, fileToUpload.sizeBytes, fileToUpload.mimeType, fileName, body, config === null || config === void 0 ? void 0 : config.httpOptions);
|
|
7295
|
+
return uploader.upload(file, uploadUrl, this);
|
|
7485
7296
|
}
|
|
7486
7297
|
/**
|
|
7487
|
-
* Uploads a file asynchronously
|
|
7488
|
-
*
|
|
7489
|
-
* Supported upload sources:
|
|
7490
|
-
* - Node.js: File path (string) or Blob object.
|
|
7491
|
-
* - Browser: Blob object (e.g., File).
|
|
7492
|
-
*
|
|
7493
|
-
* @remarks
|
|
7494
|
-
* The `mimeType` can be specified in the `config` parameter. If omitted:
|
|
7495
|
-
* - For file path (string) inputs, the `mimeType` will be inferred from the
|
|
7496
|
-
* file extension.
|
|
7497
|
-
* - For Blob object inputs, the `mimeType` will be set to the Blob's `type`
|
|
7498
|
-
* property.
|
|
7499
|
-
* Somex eamples for file extension to mimeType mapping:
|
|
7500
|
-
* .txt -> text/plain
|
|
7501
|
-
* .json -> application/json
|
|
7502
|
-
* .jpg -> image/jpeg
|
|
7503
|
-
* .png -> image/png
|
|
7504
|
-
* .mp3 -> audio/mpeg
|
|
7505
|
-
* .mp4 -> video/mp4
|
|
7506
|
-
*
|
|
7507
|
-
* This section can contain multiple paragraphs and code examples.
|
|
7298
|
+
* Uploads a file to a given file search store asynchronously using Gemini API only, this is not supported
|
|
7299
|
+
* in Vertex AI.
|
|
7508
7300
|
*
|
|
7509
|
-
* @param
|
|
7510
|
-
*
|
|
7511
|
-
*
|
|
7512
|
-
*
|
|
7513
|
-
* @return A promise that resolves to a `
|
|
7301
|
+
* @param fileSearchStoreName The name of the file search store to upload the file to.
|
|
7302
|
+
* @param file The string path to the file to be uploaded or a Blob object.
|
|
7303
|
+
* @param config Optional parameters specified in the `UploadFileConfig`
|
|
7304
|
+
* interface. @see {@link UploadFileConfig}
|
|
7305
|
+
* @return A promise that resolves to a `File` object.
|
|
7514
7306
|
* @throws An error if called on a Vertex AI client.
|
|
7515
7307
|
* @throws An error if the `mimeType` is not provided and can not be inferred,
|
|
7516
|
-
* the `mimeType` can be provided in the `params.config` parameter.
|
|
7517
|
-
* @throws An error occurs if a suitable upload location cannot be established.
|
|
7518
|
-
*
|
|
7519
|
-
* @example
|
|
7520
|
-
* The following code uploads a file to Gemini API.
|
|
7521
|
-
*
|
|
7522
|
-
* ```ts
|
|
7523
|
-
* const file = await ai.files.upload({file: 'file.txt', config: {
|
|
7524
|
-
* mimeType: 'text/plain',
|
|
7525
|
-
* }});
|
|
7526
|
-
* console.log(file.name);
|
|
7527
|
-
* ```
|
|
7528
7308
|
*/
|
|
7529
|
-
async
|
|
7530
|
-
|
|
7531
|
-
|
|
7309
|
+
async uploadFileToFileSearchStore(fileSearchStoreName, file, config) {
|
|
7310
|
+
var _a;
|
|
7311
|
+
const uploader = this.clientOptions.uploader;
|
|
7312
|
+
const fileStat = await uploader.stat(file);
|
|
7313
|
+
const sizeBytes = String(fileStat.size);
|
|
7314
|
+
const mimeType = (_a = config === null || config === void 0 ? void 0 : config.mimeType) !== null && _a !== void 0 ? _a : fileStat.type;
|
|
7315
|
+
if (mimeType === undefined || mimeType === '') {
|
|
7316
|
+
throw new Error('Can not determine mimeType. Please provide mimeType in the config.');
|
|
7532
7317
|
}
|
|
7533
|
-
|
|
7534
|
-
|
|
7535
|
-
|
|
7536
|
-
|
|
7537
|
-
|
|
7318
|
+
const path = `upload/v1beta/${fileSearchStoreName}:uploadToFileSearchStore`;
|
|
7319
|
+
const fileName = this.getFileName(file);
|
|
7320
|
+
const body = {};
|
|
7321
|
+
if (config != null) {
|
|
7322
|
+
uploadToFileSearchStoreConfigToMldev(config, body);
|
|
7323
|
+
}
|
|
7324
|
+
const uploadUrl = await this.fetchUploadUrl(path, sizeBytes, mimeType, fileName, body, config === null || config === void 0 ? void 0 : config.httpOptions);
|
|
7325
|
+
return uploader.uploadToFileSearchStore(file, uploadUrl, this);
|
|
7538
7326
|
}
|
|
7539
7327
|
/**
|
|
7540
|
-
* Downloads a
|
|
7541
|
-
* the `params` object. This method only works on Node environment, to
|
|
7542
|
-
* download files in the browser, use a browser compliant method like an <a>
|
|
7543
|
-
* tag.
|
|
7544
|
-
*
|
|
7545
|
-
* @param params - The parameters for the download request.
|
|
7546
|
-
*
|
|
7547
|
-
* @example
|
|
7548
|
-
* The following code downloads an example file named "files/mehozpxf877d" as
|
|
7549
|
-
* "file.txt".
|
|
7328
|
+
* Downloads a file asynchronously to the specified path.
|
|
7550
7329
|
*
|
|
7551
|
-
*
|
|
7552
|
-
*
|
|
7553
|
-
* ```
|
|
7330
|
+
* @params params - The parameters for the download request, see {@link
|
|
7331
|
+
* types.DownloadFileParameters}
|
|
7554
7332
|
*/
|
|
7555
|
-
async
|
|
7556
|
-
|
|
7333
|
+
async downloadFile(params) {
|
|
7334
|
+
const downloader = this.clientOptions.downloader;
|
|
7335
|
+
await downloader.download(params, this);
|
|
7557
7336
|
}
|
|
7558
|
-
async
|
|
7559
|
-
var _a
|
|
7560
|
-
let
|
|
7561
|
-
|
|
7562
|
-
|
|
7563
|
-
if (this.apiClient.isVertexAI()) {
|
|
7564
|
-
throw new Error('This method is only supported by the Gemini Developer API.');
|
|
7337
|
+
async fetchUploadUrl(path, sizeBytes, mimeType, fileName, body, configHttpOptions) {
|
|
7338
|
+
var _a;
|
|
7339
|
+
let httpOptions = {};
|
|
7340
|
+
if (configHttpOptions) {
|
|
7341
|
+
httpOptions = configHttpOptions;
|
|
7565
7342
|
}
|
|
7566
7343
|
else {
|
|
7567
|
-
|
|
7568
|
-
|
|
7569
|
-
|
|
7570
|
-
|
|
7571
|
-
|
|
7572
|
-
|
|
7573
|
-
|
|
7574
|
-
|
|
7575
|
-
|
|
7576
|
-
|
|
7577
|
-
|
|
7578
|
-
|
|
7579
|
-
|
|
7580
|
-
|
|
7581
|
-
|
|
7582
|
-
|
|
7583
|
-
|
|
7584
|
-
response.sdkHttpResponse = {
|
|
7585
|
-
headers: httpResponse.headers,
|
|
7586
|
-
};
|
|
7587
|
-
return response;
|
|
7588
|
-
});
|
|
7589
|
-
});
|
|
7590
|
-
return response.then((apiResponse) => {
|
|
7591
|
-
const resp = listFilesResponseFromMldev(apiResponse);
|
|
7592
|
-
const typedResp = new ListFilesResponse();
|
|
7593
|
-
Object.assign(typedResp, resp);
|
|
7594
|
-
return typedResp;
|
|
7595
|
-
});
|
|
7344
|
+
httpOptions = {
|
|
7345
|
+
apiVersion: '', // api-version is set in the path.
|
|
7346
|
+
headers: Object.assign({ 'Content-Type': 'application/json', 'X-Goog-Upload-Protocol': 'resumable', 'X-Goog-Upload-Command': 'start', 'X-Goog-Upload-Header-Content-Length': `${sizeBytes}`, 'X-Goog-Upload-Header-Content-Type': `${mimeType}` }, (fileName ? { 'X-Goog-Upload-File-Name': fileName } : {})),
|
|
7347
|
+
};
|
|
7348
|
+
}
|
|
7349
|
+
const httpResponse = await this.request({
|
|
7350
|
+
path,
|
|
7351
|
+
body: JSON.stringify(body),
|
|
7352
|
+
httpMethod: 'POST',
|
|
7353
|
+
httpOptions,
|
|
7354
|
+
});
|
|
7355
|
+
if (!httpResponse || !(httpResponse === null || httpResponse === void 0 ? void 0 : httpResponse.headers)) {
|
|
7356
|
+
throw new Error('Server did not return an HttpResponse or the returned HttpResponse did not have headers.');
|
|
7357
|
+
}
|
|
7358
|
+
const uploadUrl = (_a = httpResponse === null || httpResponse === void 0 ? void 0 : httpResponse.headers) === null || _a === void 0 ? void 0 : _a['x-goog-upload-url'];
|
|
7359
|
+
if (uploadUrl === undefined) {
|
|
7360
|
+
throw new Error('Failed to get upload url. Server did not return the x-google-upload-url in the headers');
|
|
7596
7361
|
}
|
|
7362
|
+
return uploadUrl;
|
|
7597
7363
|
}
|
|
7598
|
-
|
|
7599
|
-
|
|
7600
|
-
|
|
7601
|
-
|
|
7602
|
-
|
|
7603
|
-
|
|
7604
|
-
|
|
7364
|
+
}
|
|
7365
|
+
async function throwErrorIfNotOK(response) {
|
|
7366
|
+
var _a;
|
|
7367
|
+
if (response === undefined) {
|
|
7368
|
+
throw new Error('response is undefined');
|
|
7369
|
+
}
|
|
7370
|
+
if (!response.ok) {
|
|
7371
|
+
const status = response.status;
|
|
7372
|
+
let errorBody;
|
|
7373
|
+
if ((_a = response.headers.get('content-type')) === null || _a === void 0 ? void 0 : _a.includes('application/json')) {
|
|
7374
|
+
errorBody = await response.json();
|
|
7605
7375
|
}
|
|
7606
7376
|
else {
|
|
7607
|
-
|
|
7608
|
-
|
|
7609
|
-
|
|
7610
|
-
|
|
7611
|
-
|
|
7612
|
-
|
|
7613
|
-
|
|
7614
|
-
|
|
7615
|
-
|
|
7616
|
-
|
|
7617
|
-
|
|
7618
|
-
|
|
7619
|
-
|
|
7620
|
-
})
|
|
7621
|
-
.then((httpResponse) => {
|
|
7622
|
-
return httpResponse.json();
|
|
7623
|
-
});
|
|
7624
|
-
return response.then((apiResponse) => {
|
|
7625
|
-
const resp = createFileResponseFromMldev(apiResponse);
|
|
7626
|
-
const typedResp = new CreateFileResponse();
|
|
7627
|
-
Object.assign(typedResp, resp);
|
|
7628
|
-
return typedResp;
|
|
7377
|
+
errorBody = {
|
|
7378
|
+
error: {
|
|
7379
|
+
message: await response.text(),
|
|
7380
|
+
code: response.status,
|
|
7381
|
+
status: response.statusText,
|
|
7382
|
+
},
|
|
7383
|
+
};
|
|
7384
|
+
}
|
|
7385
|
+
const errorMessage = JSON.stringify(errorBody);
|
|
7386
|
+
if (status >= 400 && status < 600) {
|
|
7387
|
+
const apiError = new ApiError({
|
|
7388
|
+
message: errorMessage,
|
|
7389
|
+
status: status,
|
|
7629
7390
|
});
|
|
7391
|
+
throw apiError;
|
|
7630
7392
|
}
|
|
7393
|
+
throw new Error(errorMessage);
|
|
7631
7394
|
}
|
|
7632
|
-
|
|
7633
|
-
|
|
7634
|
-
|
|
7635
|
-
|
|
7636
|
-
|
|
7637
|
-
|
|
7638
|
-
|
|
7639
|
-
|
|
7640
|
-
|
|
7641
|
-
|
|
7642
|
-
|
|
7643
|
-
|
|
7644
|
-
|
|
7645
|
-
|
|
7646
|
-
|
|
7647
|
-
|
|
7648
|
-
|
|
7649
|
-
|
|
7650
|
-
|
|
7651
|
-
|
|
7652
|
-
|
|
7653
|
-
|
|
7395
|
+
}
|
|
7396
|
+
/**
|
|
7397
|
+
* Recursively updates the `requestInit.body` with values from an `extraBody` object.
|
|
7398
|
+
*
|
|
7399
|
+
* If `requestInit.body` is a string, it's assumed to be JSON and will be parsed.
|
|
7400
|
+
* The `extraBody` is then deeply merged into this parsed object.
|
|
7401
|
+
* If `requestInit.body` is a Blob, `extraBody` will be ignored, and a warning logged,
|
|
7402
|
+
* as merging structured data into an opaque Blob is not supported.
|
|
7403
|
+
*
|
|
7404
|
+
* The function does not enforce that updated values from `extraBody` have the
|
|
7405
|
+
* same type as existing values in `requestInit.body`. Type mismatches during
|
|
7406
|
+
* the merge will result in a warning, but the value from `extraBody` will overwrite
|
|
7407
|
+
* the original. `extraBody` users are responsible for ensuring `extraBody` has the correct structure.
|
|
7408
|
+
*
|
|
7409
|
+
* @param requestInit The RequestInit object whose body will be updated.
|
|
7410
|
+
* @param extraBody The object containing updates to be merged into `requestInit.body`.
|
|
7411
|
+
*/
|
|
7412
|
+
function includeExtraBodyToRequestInit(requestInit, extraBody) {
|
|
7413
|
+
if (!extraBody || Object.keys(extraBody).length === 0) {
|
|
7414
|
+
return;
|
|
7415
|
+
}
|
|
7416
|
+
if (requestInit.body instanceof Blob) {
|
|
7417
|
+
console.warn('includeExtraBodyToRequestInit: extraBody provided but current request body is a Blob. extraBody will be ignored as merging is not supported for Blob bodies.');
|
|
7418
|
+
return;
|
|
7419
|
+
}
|
|
7420
|
+
let currentBodyObject = {};
|
|
7421
|
+
// If adding new type to HttpRequest.body, please check the code below to
|
|
7422
|
+
// see if we need to update the logic.
|
|
7423
|
+
if (typeof requestInit.body === 'string' && requestInit.body.length > 0) {
|
|
7424
|
+
try {
|
|
7425
|
+
const parsedBody = JSON.parse(requestInit.body);
|
|
7426
|
+
if (typeof parsedBody === 'object' &&
|
|
7427
|
+
parsedBody !== null &&
|
|
7428
|
+
!Array.isArray(parsedBody)) {
|
|
7429
|
+
currentBodyObject = parsedBody;
|
|
7430
|
+
}
|
|
7431
|
+
else {
|
|
7432
|
+
console.warn('includeExtraBodyToRequestInit: Original request body is valid JSON but not a non-array object. Skip applying extraBody to the request body.');
|
|
7433
|
+
return;
|
|
7434
|
+
}
|
|
7435
|
+
/* eslint-disable-next-line @typescript-eslint/no-unused-vars */
|
|
7654
7436
|
}
|
|
7655
|
-
|
|
7656
|
-
|
|
7657
|
-
|
|
7658
|
-
queryParams = body['_query'];
|
|
7659
|
-
delete body['_url'];
|
|
7660
|
-
delete body['_query'];
|
|
7661
|
-
response = this.apiClient
|
|
7662
|
-
.request({
|
|
7663
|
-
path: path,
|
|
7664
|
-
queryParams: queryParams,
|
|
7665
|
-
body: JSON.stringify(body),
|
|
7666
|
-
httpMethod: 'GET',
|
|
7667
|
-
httpOptions: (_a = params.config) === null || _a === void 0 ? void 0 : _a.httpOptions,
|
|
7668
|
-
abortSignal: (_b = params.config) === null || _b === void 0 ? void 0 : _b.abortSignal,
|
|
7669
|
-
})
|
|
7670
|
-
.then((httpResponse) => {
|
|
7671
|
-
return httpResponse.json();
|
|
7672
|
-
});
|
|
7673
|
-
return response.then((resp) => {
|
|
7674
|
-
return resp;
|
|
7675
|
-
});
|
|
7437
|
+
catch (e) {
|
|
7438
|
+
console.warn('includeExtraBodyToRequestInit: Original request body is not valid JSON. Skip applying extraBody to the request body.');
|
|
7439
|
+
return;
|
|
7676
7440
|
}
|
|
7677
7441
|
}
|
|
7678
|
-
|
|
7679
|
-
|
|
7680
|
-
|
|
7681
|
-
|
|
7682
|
-
|
|
7683
|
-
|
|
7684
|
-
|
|
7685
|
-
|
|
7686
|
-
|
|
7687
|
-
|
|
7688
|
-
|
|
7689
|
-
|
|
7690
|
-
|
|
7691
|
-
|
|
7692
|
-
|
|
7693
|
-
|
|
7694
|
-
|
|
7695
|
-
|
|
7696
|
-
|
|
7697
|
-
|
|
7698
|
-
|
|
7699
|
-
|
|
7700
|
-
|
|
7701
|
-
path = formatMap('files/{file}', body['_url']);
|
|
7702
|
-
queryParams = body['_query'];
|
|
7703
|
-
delete body['_url'];
|
|
7704
|
-
delete body['_query'];
|
|
7705
|
-
response = this.apiClient
|
|
7706
|
-
.request({
|
|
7707
|
-
path: path,
|
|
7708
|
-
queryParams: queryParams,
|
|
7709
|
-
body: JSON.stringify(body),
|
|
7710
|
-
httpMethod: 'DELETE',
|
|
7711
|
-
httpOptions: (_a = params.config) === null || _a === void 0 ? void 0 : _a.httpOptions,
|
|
7712
|
-
abortSignal: (_b = params.config) === null || _b === void 0 ? void 0 : _b.abortSignal,
|
|
7713
|
-
})
|
|
7714
|
-
.then((httpResponse) => {
|
|
7715
|
-
return httpResponse.json().then((jsonResponse) => {
|
|
7716
|
-
const response = jsonResponse;
|
|
7717
|
-
response.sdkHttpResponse = {
|
|
7718
|
-
headers: httpResponse.headers,
|
|
7719
|
-
};
|
|
7720
|
-
return response;
|
|
7721
|
-
});
|
|
7722
|
-
});
|
|
7723
|
-
return response.then((apiResponse) => {
|
|
7724
|
-
const resp = deleteFileResponseFromMldev(apiResponse);
|
|
7725
|
-
const typedResp = new DeleteFileResponse();
|
|
7726
|
-
Object.assign(typedResp, resp);
|
|
7727
|
-
return typedResp;
|
|
7728
|
-
});
|
|
7442
|
+
function deepMerge(target, source) {
|
|
7443
|
+
const output = Object.assign({}, target);
|
|
7444
|
+
for (const key in source) {
|
|
7445
|
+
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
7446
|
+
const sourceValue = source[key];
|
|
7447
|
+
const targetValue = output[key];
|
|
7448
|
+
if (sourceValue &&
|
|
7449
|
+
typeof sourceValue === 'object' &&
|
|
7450
|
+
!Array.isArray(sourceValue) &&
|
|
7451
|
+
targetValue &&
|
|
7452
|
+
typeof targetValue === 'object' &&
|
|
7453
|
+
!Array.isArray(targetValue)) {
|
|
7454
|
+
output[key] = deepMerge(targetValue, sourceValue);
|
|
7455
|
+
}
|
|
7456
|
+
else {
|
|
7457
|
+
if (targetValue &&
|
|
7458
|
+
sourceValue &&
|
|
7459
|
+
typeof targetValue !== typeof sourceValue) {
|
|
7460
|
+
console.warn(`includeExtraBodyToRequestInit:deepMerge: Type mismatch for key "${key}". Original type: ${typeof targetValue}, New type: ${typeof sourceValue}. Overwriting.`);
|
|
7461
|
+
}
|
|
7462
|
+
output[key] = sourceValue;
|
|
7463
|
+
}
|
|
7464
|
+
}
|
|
7729
7465
|
}
|
|
7466
|
+
return output;
|
|
7730
7467
|
}
|
|
7468
|
+
const mergedBody = deepMerge(currentBodyObject, extraBody);
|
|
7469
|
+
requestInit.body = JSON.stringify(mergedBody);
|
|
7731
7470
|
}
|
|
7732
7471
|
|
|
7733
7472
|
/**
|
|
@@ -7735,116 +7474,182 @@ class Files extends BaseModule {
|
|
|
7735
7474
|
* Copyright 2025 Google LLC
|
|
7736
7475
|
* SPDX-License-Identifier: Apache-2.0
|
|
7737
7476
|
*/
|
|
7738
|
-
|
|
7739
|
-
|
|
7740
|
-
|
|
7741
|
-
|
|
7742
|
-
|
|
7743
|
-
|
|
7744
|
-
|
|
7745
|
-
|
|
7477
|
+
function crossError() {
|
|
7478
|
+
// TODO(b/399934880): this message needs a link to a help page explaining how to enable conditional exports
|
|
7479
|
+
return new Error(`This feature requires the web or Node specific @google/genai implementation, you can fix this by either:
|
|
7480
|
+
|
|
7481
|
+
*Enabling conditional exports for your project [recommended]*
|
|
7482
|
+
|
|
7483
|
+
*Using a platform specific import* - Make sure your code imports either '@google/genai/web' or '@google/genai/node' instead of '@google/genai'.
|
|
7484
|
+
`);
|
|
7746
7485
|
}
|
|
7747
|
-
|
|
7748
|
-
|
|
7749
|
-
|
|
7750
|
-
|
|
7751
|
-
|
|
7486
|
+
|
|
7487
|
+
/**
|
|
7488
|
+
* @license
|
|
7489
|
+
* Copyright 2025 Google LLC
|
|
7490
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
7491
|
+
*/
|
|
7492
|
+
class CrossDownloader {
|
|
7493
|
+
async download(_params, _apiClient) {
|
|
7494
|
+
throw crossError();
|
|
7752
7495
|
}
|
|
7753
|
-
return toObject;
|
|
7754
7496
|
}
|
|
7755
|
-
|
|
7756
|
-
|
|
7757
|
-
|
|
7758
|
-
|
|
7759
|
-
|
|
7497
|
+
|
|
7498
|
+
const MAX_CHUNK_SIZE = 1024 * 1024 * 8; // bytes
|
|
7499
|
+
const MAX_RETRY_COUNT = 3;
|
|
7500
|
+
const INITIAL_RETRY_DELAY_MS = 1000;
|
|
7501
|
+
const DELAY_MULTIPLIER = 2;
|
|
7502
|
+
const X_GOOG_UPLOAD_STATUS_HEADER_FIELD = 'x-goog-upload-status';
|
|
7503
|
+
class CrossUploader {
|
|
7504
|
+
async upload(file, uploadUrl, apiClient) {
|
|
7505
|
+
if (typeof file === 'string') {
|
|
7506
|
+
throw crossError();
|
|
7507
|
+
}
|
|
7508
|
+
else {
|
|
7509
|
+
return uploadBlob(file, uploadUrl, apiClient);
|
|
7510
|
+
}
|
|
7760
7511
|
}
|
|
7761
|
-
|
|
7762
|
-
|
|
7763
|
-
|
|
7764
|
-
|
|
7765
|
-
|
|
7766
|
-
|
|
7767
|
-
|
|
7512
|
+
async uploadToFileSearchStore(file, uploadUrl, apiClient) {
|
|
7513
|
+
if (typeof file === 'string') {
|
|
7514
|
+
throw crossError();
|
|
7515
|
+
}
|
|
7516
|
+
else {
|
|
7517
|
+
return uploadBlobToFileSearchStore(file, uploadUrl, apiClient);
|
|
7518
|
+
}
|
|
7768
7519
|
}
|
|
7769
|
-
|
|
7770
|
-
|
|
7771
|
-
|
|
7520
|
+
async stat(file) {
|
|
7521
|
+
if (typeof file === 'string') {
|
|
7522
|
+
throw crossError();
|
|
7523
|
+
}
|
|
7524
|
+
else {
|
|
7525
|
+
return getBlobStat(file);
|
|
7526
|
+
}
|
|
7772
7527
|
}
|
|
7773
|
-
return toObject;
|
|
7774
7528
|
}
|
|
7775
|
-
function
|
|
7776
|
-
|
|
7777
|
-
const
|
|
7778
|
-
|
|
7779
|
-
|
|
7529
|
+
async function uploadBlob(file, uploadUrl, apiClient) {
|
|
7530
|
+
var _a;
|
|
7531
|
+
const response = await uploadBlobInternal(file, uploadUrl, apiClient);
|
|
7532
|
+
const responseJson = (await (response === null || response === void 0 ? void 0 : response.json()));
|
|
7533
|
+
if (((_a = response === null || response === void 0 ? void 0 : response.headers) === null || _a === void 0 ? void 0 : _a[X_GOOG_UPLOAD_STATUS_HEADER_FIELD]) !== 'final') {
|
|
7534
|
+
throw new Error('Failed to upload file: Upload status is not finalized.');
|
|
7780
7535
|
}
|
|
7781
|
-
return
|
|
7536
|
+
return responseJson['file'];
|
|
7782
7537
|
}
|
|
7783
|
-
function
|
|
7784
|
-
|
|
7785
|
-
const
|
|
7786
|
-
|
|
7787
|
-
])
|
|
7788
|
-
|
|
7789
|
-
|
|
7790
|
-
|
|
7791
|
-
|
|
7792
|
-
|
|
7538
|
+
async function uploadBlobToFileSearchStore(file, uploadUrl, apiClient) {
|
|
7539
|
+
var _a;
|
|
7540
|
+
const response = await uploadBlobInternal(file, uploadUrl, apiClient);
|
|
7541
|
+
const responseJson = (await (response === null || response === void 0 ? void 0 : response.json()));
|
|
7542
|
+
if (((_a = response === null || response === void 0 ? void 0 : response.headers) === null || _a === void 0 ? void 0 : _a[X_GOOG_UPLOAD_STATUS_HEADER_FIELD]) !== 'final') {
|
|
7543
|
+
throw new Error('Failed to upload file: Upload status is not finalized.');
|
|
7544
|
+
}
|
|
7545
|
+
const resp = uploadToFileSearchStoreOperationFromMldev(responseJson);
|
|
7546
|
+
const typedResp = new UploadToFileSearchStoreOperation();
|
|
7547
|
+
Object.assign(typedResp, resp);
|
|
7548
|
+
return typedResp;
|
|
7549
|
+
}
|
|
7550
|
+
async function uploadBlobInternal(file, uploadUrl, apiClient) {
|
|
7551
|
+
var _a, _b;
|
|
7552
|
+
let fileSize = 0;
|
|
7553
|
+
let offset = 0;
|
|
7554
|
+
let response = new HttpResponse(new Response());
|
|
7555
|
+
let uploadCommand = 'upload';
|
|
7556
|
+
fileSize = file.size;
|
|
7557
|
+
while (offset < fileSize) {
|
|
7558
|
+
const chunkSize = Math.min(MAX_CHUNK_SIZE, fileSize - offset);
|
|
7559
|
+
const chunk = file.slice(offset, offset + chunkSize);
|
|
7560
|
+
if (offset + chunkSize >= fileSize) {
|
|
7561
|
+
uploadCommand += ', finalize';
|
|
7562
|
+
}
|
|
7563
|
+
let retryCount = 0;
|
|
7564
|
+
let currentDelayMs = INITIAL_RETRY_DELAY_MS;
|
|
7565
|
+
while (retryCount < MAX_RETRY_COUNT) {
|
|
7566
|
+
response = await apiClient.request({
|
|
7567
|
+
path: '',
|
|
7568
|
+
body: chunk,
|
|
7569
|
+
httpMethod: 'POST',
|
|
7570
|
+
httpOptions: {
|
|
7571
|
+
apiVersion: '',
|
|
7572
|
+
baseUrl: uploadUrl,
|
|
7573
|
+
headers: {
|
|
7574
|
+
'X-Goog-Upload-Command': uploadCommand,
|
|
7575
|
+
'X-Goog-Upload-Offset': String(offset),
|
|
7576
|
+
'Content-Length': String(chunkSize),
|
|
7577
|
+
},
|
|
7578
|
+
},
|
|
7793
7579
|
});
|
|
7580
|
+
if ((_a = response === null || response === void 0 ? void 0 : response.headers) === null || _a === void 0 ? void 0 : _a[X_GOOG_UPLOAD_STATUS_HEADER_FIELD]) {
|
|
7581
|
+
break;
|
|
7582
|
+
}
|
|
7583
|
+
retryCount++;
|
|
7584
|
+
await sleep(currentDelayMs);
|
|
7585
|
+
currentDelayMs = currentDelayMs * DELAY_MULTIPLIER;
|
|
7586
|
+
}
|
|
7587
|
+
offset += chunkSize;
|
|
7588
|
+
// The `x-goog-upload-status` header field can be `active`, `final` and
|
|
7589
|
+
//`cancelled` in resposne.
|
|
7590
|
+
if (((_b = response === null || response === void 0 ? void 0 : response.headers) === null || _b === void 0 ? void 0 : _b[X_GOOG_UPLOAD_STATUS_HEADER_FIELD]) !== 'active') {
|
|
7591
|
+
break;
|
|
7592
|
+
}
|
|
7593
|
+
// TODO(b/401391430) Investigate why the upload status is not finalized
|
|
7594
|
+
// even though all content has been uploaded.
|
|
7595
|
+
if (fileSize <= offset) {
|
|
7596
|
+
throw new Error('All content has been uploaded, but the upload status is not finalized.');
|
|
7794
7597
|
}
|
|
7795
|
-
setValueByPath(parentObject, ['customMetadata'], transformedList);
|
|
7796
7598
|
}
|
|
7797
|
-
|
|
7798
|
-
|
|
7799
|
-
|
|
7800
|
-
|
|
7801
|
-
|
|
7599
|
+
return response;
|
|
7600
|
+
}
|
|
7601
|
+
async function getBlobStat(file) {
|
|
7602
|
+
const fileStat = { size: file.size, type: file.type };
|
|
7603
|
+
return fileStat;
|
|
7604
|
+
}
|
|
7605
|
+
function sleep(ms) {
|
|
7606
|
+
return new Promise((resolvePromise) => setTimeout(resolvePromise, ms));
|
|
7607
|
+
}
|
|
7608
|
+
|
|
7609
|
+
/**
|
|
7610
|
+
* @license
|
|
7611
|
+
* Copyright 2025 Google LLC
|
|
7612
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
7613
|
+
*/
|
|
7614
|
+
class CrossWebSocketFactory {
|
|
7615
|
+
create(_url, _headers, _callbacks) {
|
|
7616
|
+
throw crossError();
|
|
7802
7617
|
}
|
|
7803
|
-
return toObject;
|
|
7804
7618
|
}
|
|
7805
|
-
|
|
7619
|
+
|
|
7620
|
+
/**
|
|
7621
|
+
* @license
|
|
7622
|
+
* Copyright 2025 Google LLC
|
|
7623
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
7624
|
+
*/
|
|
7625
|
+
// Code generated by the Google Gen AI SDK generator DO NOT EDIT.
|
|
7626
|
+
function createFileParametersToMldev(fromObject) {
|
|
7806
7627
|
const toObject = {};
|
|
7807
|
-
const
|
|
7808
|
-
if (
|
|
7809
|
-
setValueByPath(toObject, ['
|
|
7810
|
-
}
|
|
7811
|
-
const fromMetadata = getValueByPath(fromObject, ['metadata']);
|
|
7812
|
-
if (fromMetadata != null) {
|
|
7813
|
-
setValueByPath(toObject, ['metadata'], fromMetadata);
|
|
7814
|
-
}
|
|
7815
|
-
const fromDone = getValueByPath(fromObject, ['done']);
|
|
7816
|
-
if (fromDone != null) {
|
|
7817
|
-
setValueByPath(toObject, ['done'], fromDone);
|
|
7818
|
-
}
|
|
7819
|
-
const fromError = getValueByPath(fromObject, ['error']);
|
|
7820
|
-
if (fromError != null) {
|
|
7821
|
-
setValueByPath(toObject, ['error'], fromError);
|
|
7822
|
-
}
|
|
7823
|
-
const fromResponse = getValueByPath(fromObject, ['response']);
|
|
7824
|
-
if (fromResponse != null) {
|
|
7825
|
-
setValueByPath(toObject, ['response'], importFileResponseFromMldev(fromResponse));
|
|
7628
|
+
const fromFile = getValueByPath(fromObject, ['file']);
|
|
7629
|
+
if (fromFile != null) {
|
|
7630
|
+
setValueByPath(toObject, ['file'], fromFile);
|
|
7826
7631
|
}
|
|
7827
7632
|
return toObject;
|
|
7828
7633
|
}
|
|
7829
|
-
function
|
|
7634
|
+
function createFileResponseFromMldev(fromObject) {
|
|
7830
7635
|
const toObject = {};
|
|
7831
|
-
const
|
|
7832
|
-
'
|
|
7636
|
+
const fromSdkHttpResponse = getValueByPath(fromObject, [
|
|
7637
|
+
'sdkHttpResponse',
|
|
7833
7638
|
]);
|
|
7834
|
-
if (
|
|
7835
|
-
setValueByPath(toObject, ['
|
|
7836
|
-
}
|
|
7837
|
-
const fromFileName = getValueByPath(fromObject, ['fileName']);
|
|
7838
|
-
if (fromFileName != null) {
|
|
7839
|
-
setValueByPath(toObject, ['fileName'], fromFileName);
|
|
7639
|
+
if (fromSdkHttpResponse != null) {
|
|
7640
|
+
setValueByPath(toObject, ['sdkHttpResponse'], fromSdkHttpResponse);
|
|
7840
7641
|
}
|
|
7841
|
-
|
|
7842
|
-
|
|
7843
|
-
|
|
7642
|
+
return toObject;
|
|
7643
|
+
}
|
|
7644
|
+
function deleteFileParametersToMldev(fromObject) {
|
|
7645
|
+
const toObject = {};
|
|
7646
|
+
const fromName = getValueByPath(fromObject, ['name']);
|
|
7647
|
+
if (fromName != null) {
|
|
7648
|
+
setValueByPath(toObject, ['_url', 'file'], tFileName(fromName));
|
|
7844
7649
|
}
|
|
7845
7650
|
return toObject;
|
|
7846
7651
|
}
|
|
7847
|
-
function
|
|
7652
|
+
function deleteFileResponseFromMldev(fromObject) {
|
|
7848
7653
|
const toObject = {};
|
|
7849
7654
|
const fromSdkHttpResponse = getValueByPath(fromObject, [
|
|
7850
7655
|
'sdkHttpResponse',
|
|
@@ -7852,17 +7657,17 @@ function importFileResponseFromMldev(fromObject) {
|
|
|
7852
7657
|
if (fromSdkHttpResponse != null) {
|
|
7853
7658
|
setValueByPath(toObject, ['sdkHttpResponse'], fromSdkHttpResponse);
|
|
7854
7659
|
}
|
|
7855
|
-
|
|
7856
|
-
|
|
7857
|
-
|
|
7858
|
-
}
|
|
7859
|
-
const
|
|
7860
|
-
if (
|
|
7861
|
-
setValueByPath(toObject, ['
|
|
7660
|
+
return toObject;
|
|
7661
|
+
}
|
|
7662
|
+
function getFileParametersToMldev(fromObject) {
|
|
7663
|
+
const toObject = {};
|
|
7664
|
+
const fromName = getValueByPath(fromObject, ['name']);
|
|
7665
|
+
if (fromName != null) {
|
|
7666
|
+
setValueByPath(toObject, ['_url', 'file'], tFileName(fromName));
|
|
7862
7667
|
}
|
|
7863
7668
|
return toObject;
|
|
7864
7669
|
}
|
|
7865
|
-
function
|
|
7670
|
+
function listFilesConfigToMldev(fromObject, parentObject) {
|
|
7866
7671
|
const toObject = {};
|
|
7867
7672
|
const fromPageSize = getValueByPath(fromObject, ['pageSize']);
|
|
7868
7673
|
if (parentObject !== undefined && fromPageSize != null) {
|
|
@@ -7874,15 +7679,15 @@ function listFileSearchStoresConfigToMldev(fromObject, parentObject) {
|
|
|
7874
7679
|
}
|
|
7875
7680
|
return toObject;
|
|
7876
7681
|
}
|
|
7877
|
-
function
|
|
7682
|
+
function listFilesParametersToMldev(fromObject) {
|
|
7878
7683
|
const toObject = {};
|
|
7879
7684
|
const fromConfig = getValueByPath(fromObject, ['config']);
|
|
7880
7685
|
if (fromConfig != null) {
|
|
7881
|
-
|
|
7686
|
+
listFilesConfigToMldev(fromConfig, toObject);
|
|
7882
7687
|
}
|
|
7883
7688
|
return toObject;
|
|
7884
7689
|
}
|
|
7885
|
-
function
|
|
7690
|
+
function listFilesResponseFromMldev(fromObject) {
|
|
7886
7691
|
const toObject = {};
|
|
7887
7692
|
const fromSdkHttpResponse = getValueByPath(fromObject, [
|
|
7888
7693
|
'sdkHttpResponse',
|
|
@@ -7896,73 +7701,291 @@ function listFileSearchStoresResponseFromMldev(fromObject) {
|
|
|
7896
7701
|
if (fromNextPageToken != null) {
|
|
7897
7702
|
setValueByPath(toObject, ['nextPageToken'], fromNextPageToken);
|
|
7898
7703
|
}
|
|
7899
|
-
const
|
|
7900
|
-
|
|
7901
|
-
|
|
7902
|
-
if (fromFileSearchStores != null) {
|
|
7903
|
-
let transformedList = fromFileSearchStores;
|
|
7704
|
+
const fromFiles = getValueByPath(fromObject, ['files']);
|
|
7705
|
+
if (fromFiles != null) {
|
|
7706
|
+
let transformedList = fromFiles;
|
|
7904
7707
|
if (Array.isArray(transformedList)) {
|
|
7905
7708
|
transformedList = transformedList.map((item) => {
|
|
7906
7709
|
return item;
|
|
7907
7710
|
});
|
|
7908
7711
|
}
|
|
7909
|
-
setValueByPath(toObject, ['
|
|
7910
|
-
}
|
|
7911
|
-
return toObject;
|
|
7912
|
-
}
|
|
7913
|
-
|
|
7914
|
-
|
|
7915
|
-
|
|
7916
|
-
|
|
7917
|
-
|
|
7918
|
-
|
|
7919
|
-
|
|
7920
|
-
|
|
7921
|
-
|
|
7712
|
+
setValueByPath(toObject, ['files'], transformedList);
|
|
7713
|
+
}
|
|
7714
|
+
return toObject;
|
|
7715
|
+
}
|
|
7716
|
+
|
|
7717
|
+
/**
|
|
7718
|
+
* @license
|
|
7719
|
+
* Copyright 2025 Google LLC
|
|
7720
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
7721
|
+
*/
|
|
7722
|
+
class Files extends BaseModule {
|
|
7723
|
+
constructor(apiClient) {
|
|
7724
|
+
super();
|
|
7725
|
+
this.apiClient = apiClient;
|
|
7726
|
+
/**
|
|
7727
|
+
* Lists files.
|
|
7728
|
+
*
|
|
7729
|
+
* @param params - The parameters for the list request.
|
|
7730
|
+
* @return - A pager of files.
|
|
7731
|
+
*
|
|
7732
|
+
* @example
|
|
7733
|
+
* ```ts
|
|
7734
|
+
* const files = await ai.files.list({config: {'pageSize': 2}});
|
|
7735
|
+
* for await (const file of files) {
|
|
7736
|
+
* console.log(file);
|
|
7737
|
+
* }
|
|
7738
|
+
* ```
|
|
7739
|
+
*/
|
|
7740
|
+
this.list = async (params = {}) => {
|
|
7741
|
+
return new Pager(exports.PagedItem.PAGED_ITEM_FILES, (x) => this.listInternal(x), await this.listInternal(params), params);
|
|
7742
|
+
};
|
|
7743
|
+
}
|
|
7744
|
+
/**
|
|
7745
|
+
* Uploads a file asynchronously to the Gemini API.
|
|
7746
|
+
* This method is not available in Vertex AI.
|
|
7747
|
+
* Supported upload sources:
|
|
7748
|
+
* - Node.js: File path (string) or Blob object.
|
|
7749
|
+
* - Browser: Blob object (e.g., File).
|
|
7750
|
+
*
|
|
7751
|
+
* @remarks
|
|
7752
|
+
* The `mimeType` can be specified in the `config` parameter. If omitted:
|
|
7753
|
+
* - For file path (string) inputs, the `mimeType` will be inferred from the
|
|
7754
|
+
* file extension.
|
|
7755
|
+
* - For Blob object inputs, the `mimeType` will be set to the Blob's `type`
|
|
7756
|
+
* property.
|
|
7757
|
+
* Somex eamples for file extension to mimeType mapping:
|
|
7758
|
+
* .txt -> text/plain
|
|
7759
|
+
* .json -> application/json
|
|
7760
|
+
* .jpg -> image/jpeg
|
|
7761
|
+
* .png -> image/png
|
|
7762
|
+
* .mp3 -> audio/mpeg
|
|
7763
|
+
* .mp4 -> video/mp4
|
|
7764
|
+
*
|
|
7765
|
+
* This section can contain multiple paragraphs and code examples.
|
|
7766
|
+
*
|
|
7767
|
+
* @param params - Optional parameters specified in the
|
|
7768
|
+
* `types.UploadFileParameters` interface.
|
|
7769
|
+
* @see {@link types.UploadFileParameters#config} for the optional
|
|
7770
|
+
* config in the parameters.
|
|
7771
|
+
* @return A promise that resolves to a `types.File` object.
|
|
7772
|
+
* @throws An error if called on a Vertex AI client.
|
|
7773
|
+
* @throws An error if the `mimeType` is not provided and can not be inferred,
|
|
7774
|
+
* the `mimeType` can be provided in the `params.config` parameter.
|
|
7775
|
+
* @throws An error occurs if a suitable upload location cannot be established.
|
|
7776
|
+
*
|
|
7777
|
+
* @example
|
|
7778
|
+
* The following code uploads a file to Gemini API.
|
|
7779
|
+
*
|
|
7780
|
+
* ```ts
|
|
7781
|
+
* const file = await ai.files.upload({file: 'file.txt', config: {
|
|
7782
|
+
* mimeType: 'text/plain',
|
|
7783
|
+
* }});
|
|
7784
|
+
* console.log(file.name);
|
|
7785
|
+
* ```
|
|
7786
|
+
*/
|
|
7787
|
+
async upload(params) {
|
|
7788
|
+
if (this.apiClient.isVertexAI()) {
|
|
7789
|
+
throw new Error('Vertex AI does not support uploading files. You can share files through a GCS bucket.');
|
|
7790
|
+
}
|
|
7791
|
+
return this.apiClient
|
|
7792
|
+
.uploadFile(params.file, params.config)
|
|
7793
|
+
.then((resp) => {
|
|
7794
|
+
return resp;
|
|
7795
|
+
});
|
|
7796
|
+
}
|
|
7797
|
+
/**
|
|
7798
|
+
* Downloads a remotely stored file asynchronously to a location specified in
|
|
7799
|
+
* the `params` object. This method only works on Node environment, to
|
|
7800
|
+
* download files in the browser, use a browser compliant method like an <a>
|
|
7801
|
+
* tag.
|
|
7802
|
+
*
|
|
7803
|
+
* @param params - The parameters for the download request.
|
|
7804
|
+
*
|
|
7805
|
+
* @example
|
|
7806
|
+
* The following code downloads an example file named "files/mehozpxf877d" as
|
|
7807
|
+
* "file.txt".
|
|
7808
|
+
*
|
|
7809
|
+
* ```ts
|
|
7810
|
+
* await ai.files.download({file: file.name, downloadPath: 'file.txt'});
|
|
7811
|
+
* ```
|
|
7812
|
+
*/
|
|
7813
|
+
async download(params) {
|
|
7814
|
+
await this.apiClient.downloadFile(params);
|
|
7815
|
+
}
|
|
7816
|
+
async listInternal(params) {
|
|
7817
|
+
var _a, _b;
|
|
7818
|
+
let response;
|
|
7819
|
+
let path = '';
|
|
7820
|
+
let queryParams = {};
|
|
7821
|
+
if (this.apiClient.isVertexAI()) {
|
|
7822
|
+
throw new Error('This method is only supported by the Gemini Developer API.');
|
|
7823
|
+
}
|
|
7824
|
+
else {
|
|
7825
|
+
const body = listFilesParametersToMldev(params);
|
|
7826
|
+
path = formatMap('files', body['_url']);
|
|
7827
|
+
queryParams = body['_query'];
|
|
7828
|
+
delete body['_url'];
|
|
7829
|
+
delete body['_query'];
|
|
7830
|
+
response = this.apiClient
|
|
7831
|
+
.request({
|
|
7832
|
+
path: path,
|
|
7833
|
+
queryParams: queryParams,
|
|
7834
|
+
body: JSON.stringify(body),
|
|
7835
|
+
httpMethod: 'GET',
|
|
7836
|
+
httpOptions: (_a = params.config) === null || _a === void 0 ? void 0 : _a.httpOptions,
|
|
7837
|
+
abortSignal: (_b = params.config) === null || _b === void 0 ? void 0 : _b.abortSignal,
|
|
7838
|
+
})
|
|
7839
|
+
.then((httpResponse) => {
|
|
7840
|
+
return httpResponse.json().then((jsonResponse) => {
|
|
7841
|
+
const response = jsonResponse;
|
|
7842
|
+
response.sdkHttpResponse = {
|
|
7843
|
+
headers: httpResponse.headers,
|
|
7844
|
+
};
|
|
7845
|
+
return response;
|
|
7846
|
+
});
|
|
7847
|
+
});
|
|
7848
|
+
return response.then((apiResponse) => {
|
|
7849
|
+
const resp = listFilesResponseFromMldev(apiResponse);
|
|
7850
|
+
const typedResp = new ListFilesResponse();
|
|
7851
|
+
Object.assign(typedResp, resp);
|
|
7852
|
+
return typedResp;
|
|
7853
|
+
});
|
|
7854
|
+
}
|
|
7855
|
+
}
|
|
7856
|
+
async createInternal(params) {
|
|
7857
|
+
var _a, _b;
|
|
7858
|
+
let response;
|
|
7859
|
+
let path = '';
|
|
7860
|
+
let queryParams = {};
|
|
7861
|
+
if (this.apiClient.isVertexAI()) {
|
|
7862
|
+
throw new Error('This method is only supported by the Gemini Developer API.');
|
|
7863
|
+
}
|
|
7864
|
+
else {
|
|
7865
|
+
const body = createFileParametersToMldev(params);
|
|
7866
|
+
path = formatMap('upload/v1beta/files', body['_url']);
|
|
7867
|
+
queryParams = body['_query'];
|
|
7868
|
+
delete body['_url'];
|
|
7869
|
+
delete body['_query'];
|
|
7870
|
+
response = this.apiClient
|
|
7871
|
+
.request({
|
|
7872
|
+
path: path,
|
|
7873
|
+
queryParams: queryParams,
|
|
7874
|
+
body: JSON.stringify(body),
|
|
7875
|
+
httpMethod: 'POST',
|
|
7876
|
+
httpOptions: (_a = params.config) === null || _a === void 0 ? void 0 : _a.httpOptions,
|
|
7877
|
+
abortSignal: (_b = params.config) === null || _b === void 0 ? void 0 : _b.abortSignal,
|
|
7878
|
+
})
|
|
7879
|
+
.then((httpResponse) => {
|
|
7880
|
+
return httpResponse.json();
|
|
7881
|
+
});
|
|
7882
|
+
return response.then((apiResponse) => {
|
|
7883
|
+
const resp = createFileResponseFromMldev(apiResponse);
|
|
7884
|
+
const typedResp = new CreateFileResponse();
|
|
7885
|
+
Object.assign(typedResp, resp);
|
|
7886
|
+
return typedResp;
|
|
7887
|
+
});
|
|
7888
|
+
}
|
|
7922
7889
|
}
|
|
7923
|
-
|
|
7924
|
-
|
|
7925
|
-
|
|
7926
|
-
|
|
7927
|
-
|
|
7928
|
-
|
|
7929
|
-
|
|
7930
|
-
|
|
7890
|
+
/**
|
|
7891
|
+
* Retrieves the file information from the service.
|
|
7892
|
+
*
|
|
7893
|
+
* @param params - The parameters for the get request
|
|
7894
|
+
* @return The Promise that resolves to the types.File object requested.
|
|
7895
|
+
*
|
|
7896
|
+
* @example
|
|
7897
|
+
* ```ts
|
|
7898
|
+
* const config: GetFileParameters = {
|
|
7899
|
+
* name: fileName,
|
|
7900
|
+
* };
|
|
7901
|
+
* file = await ai.files.get(config);
|
|
7902
|
+
* console.log(file.name);
|
|
7903
|
+
* ```
|
|
7904
|
+
*/
|
|
7905
|
+
async get(params) {
|
|
7906
|
+
var _a, _b;
|
|
7907
|
+
let response;
|
|
7908
|
+
let path = '';
|
|
7909
|
+
let queryParams = {};
|
|
7910
|
+
if (this.apiClient.isVertexAI()) {
|
|
7911
|
+
throw new Error('This method is only supported by the Gemini Developer API.');
|
|
7912
|
+
}
|
|
7913
|
+
else {
|
|
7914
|
+
const body = getFileParametersToMldev(params);
|
|
7915
|
+
path = formatMap('files/{file}', body['_url']);
|
|
7916
|
+
queryParams = body['_query'];
|
|
7917
|
+
delete body['_url'];
|
|
7918
|
+
delete body['_query'];
|
|
7919
|
+
response = this.apiClient
|
|
7920
|
+
.request({
|
|
7921
|
+
path: path,
|
|
7922
|
+
queryParams: queryParams,
|
|
7923
|
+
body: JSON.stringify(body),
|
|
7924
|
+
httpMethod: 'GET',
|
|
7925
|
+
httpOptions: (_a = params.config) === null || _a === void 0 ? void 0 : _a.httpOptions,
|
|
7926
|
+
abortSignal: (_b = params.config) === null || _b === void 0 ? void 0 : _b.abortSignal,
|
|
7927
|
+
})
|
|
7928
|
+
.then((httpResponse) => {
|
|
7929
|
+
return httpResponse.json();
|
|
7930
|
+
});
|
|
7931
|
+
return response.then((resp) => {
|
|
7932
|
+
return resp;
|
|
7931
7933
|
});
|
|
7932
7934
|
}
|
|
7933
|
-
setValueByPath(parentObject, ['customMetadata'], transformedList);
|
|
7934
|
-
}
|
|
7935
|
-
const fromChunkingConfig = getValueByPath(fromObject, [
|
|
7936
|
-
'chunkingConfig',
|
|
7937
|
-
]);
|
|
7938
|
-
if (parentObject !== undefined && fromChunkingConfig != null) {
|
|
7939
|
-
setValueByPath(parentObject, ['chunkingConfig'], fromChunkingConfig);
|
|
7940
|
-
}
|
|
7941
|
-
return toObject;
|
|
7942
|
-
}
|
|
7943
|
-
function uploadToFileSearchStoreParametersToMldev(fromObject) {
|
|
7944
|
-
const toObject = {};
|
|
7945
|
-
const fromFileSearchStoreName = getValueByPath(fromObject, [
|
|
7946
|
-
'fileSearchStoreName',
|
|
7947
|
-
]);
|
|
7948
|
-
if (fromFileSearchStoreName != null) {
|
|
7949
|
-
setValueByPath(toObject, ['_url', 'file_search_store_name'], fromFileSearchStoreName);
|
|
7950
|
-
}
|
|
7951
|
-
const fromConfig = getValueByPath(fromObject, ['config']);
|
|
7952
|
-
if (fromConfig != null) {
|
|
7953
|
-
uploadToFileSearchStoreConfigToMldev(fromConfig, toObject);
|
|
7954
7935
|
}
|
|
7955
|
-
|
|
7956
|
-
|
|
7957
|
-
|
|
7958
|
-
|
|
7959
|
-
|
|
7960
|
-
|
|
7961
|
-
|
|
7962
|
-
|
|
7963
|
-
|
|
7936
|
+
/**
|
|
7937
|
+
* Deletes a remotely stored file.
|
|
7938
|
+
*
|
|
7939
|
+
* @param params - The parameters for the delete request.
|
|
7940
|
+
* @return The DeleteFileResponse, the response for the delete method.
|
|
7941
|
+
*
|
|
7942
|
+
* @example
|
|
7943
|
+
* The following code deletes an example file named "files/mehozpxf877d".
|
|
7944
|
+
*
|
|
7945
|
+
* ```ts
|
|
7946
|
+
* await ai.files.delete({name: file.name});
|
|
7947
|
+
* ```
|
|
7948
|
+
*/
|
|
7949
|
+
async delete(params) {
|
|
7950
|
+
var _a, _b;
|
|
7951
|
+
let response;
|
|
7952
|
+
let path = '';
|
|
7953
|
+
let queryParams = {};
|
|
7954
|
+
if (this.apiClient.isVertexAI()) {
|
|
7955
|
+
throw new Error('This method is only supported by the Gemini Developer API.');
|
|
7956
|
+
}
|
|
7957
|
+
else {
|
|
7958
|
+
const body = deleteFileParametersToMldev(params);
|
|
7959
|
+
path = formatMap('files/{file}', body['_url']);
|
|
7960
|
+
queryParams = body['_query'];
|
|
7961
|
+
delete body['_url'];
|
|
7962
|
+
delete body['_query'];
|
|
7963
|
+
response = this.apiClient
|
|
7964
|
+
.request({
|
|
7965
|
+
path: path,
|
|
7966
|
+
queryParams: queryParams,
|
|
7967
|
+
body: JSON.stringify(body),
|
|
7968
|
+
httpMethod: 'DELETE',
|
|
7969
|
+
httpOptions: (_a = params.config) === null || _a === void 0 ? void 0 : _a.httpOptions,
|
|
7970
|
+
abortSignal: (_b = params.config) === null || _b === void 0 ? void 0 : _b.abortSignal,
|
|
7971
|
+
})
|
|
7972
|
+
.then((httpResponse) => {
|
|
7973
|
+
return httpResponse.json().then((jsonResponse) => {
|
|
7974
|
+
const response = jsonResponse;
|
|
7975
|
+
response.sdkHttpResponse = {
|
|
7976
|
+
headers: httpResponse.headers,
|
|
7977
|
+
};
|
|
7978
|
+
return response;
|
|
7979
|
+
});
|
|
7980
|
+
});
|
|
7981
|
+
return response.then((apiResponse) => {
|
|
7982
|
+
const resp = deleteFileResponseFromMldev(apiResponse);
|
|
7983
|
+
const typedResp = new DeleteFileResponse();
|
|
7984
|
+
Object.assign(typedResp, resp);
|
|
7985
|
+
return typedResp;
|
|
7986
|
+
});
|
|
7987
|
+
}
|
|
7964
7988
|
}
|
|
7965
|
-
return toObject;
|
|
7966
7989
|
}
|
|
7967
7990
|
|
|
7968
7991
|
/**
|
|
@@ -8067,17 +8090,14 @@ class Documents extends BaseModule {
|
|
|
8067
8090
|
*
|
|
8068
8091
|
* @example
|
|
8069
8092
|
* ```ts
|
|
8070
|
-
* const documents = await ai.documents.list({config: {'pageSize': 2}});
|
|
8093
|
+
* const documents = await ai.documents.list({parent:'rag_store_name', config: {'pageSize': 2}});
|
|
8071
8094
|
* for await (const document of documents) {
|
|
8072
8095
|
* console.log(document);
|
|
8073
8096
|
* }
|
|
8074
8097
|
* ```
|
|
8075
8098
|
*/
|
|
8076
8099
|
this.list = async (params) => {
|
|
8077
|
-
return new Pager(exports.PagedItem.PAGED_ITEM_DOCUMENTS, (x) => this.listInternal({
|
|
8078
|
-
parent: params.parent,
|
|
8079
|
-
config: x.config,
|
|
8080
|
-
}), await this.listInternal(params), params);
|
|
8100
|
+
return new Pager(exports.PagedItem.PAGED_ITEM_DOCUMENTS, (x) => this.listInternal({ parent: params.parent, config: x.config }), await this.listInternal(params), params);
|
|
8081
8101
|
};
|
|
8082
8102
|
}
|
|
8083
8103
|
/**
|
|
@@ -8145,12 +8165,6 @@ class Documents extends BaseModule {
|
|
|
8145
8165
|
});
|
|
8146
8166
|
}
|
|
8147
8167
|
}
|
|
8148
|
-
/**
|
|
8149
|
-
* Lists all Documents in a FileSearchStore.
|
|
8150
|
-
*
|
|
8151
|
-
* @param params - The parameters for listing documents.
|
|
8152
|
-
* @return ListDocumentsResponse.
|
|
8153
|
-
*/
|
|
8154
8168
|
async listInternal(params) {
|
|
8155
8169
|
var _a, _b;
|
|
8156
8170
|
let response;
|
|
@@ -8359,12 +8373,6 @@ class FileSearchStores extends BaseModule {
|
|
|
8359
8373
|
});
|
|
8360
8374
|
}
|
|
8361
8375
|
}
|
|
8362
|
-
/**
|
|
8363
|
-
* Lists all FileSearchStore owned by the user.
|
|
8364
|
-
*
|
|
8365
|
-
* @param params - The parameters for listing file search stores.
|
|
8366
|
-
* @return ListFileSearchStoresResponse.
|
|
8367
|
-
*/
|
|
8368
8376
|
async listInternal(params) {
|
|
8369
8377
|
var _a, _b;
|
|
8370
8378
|
let response;
|
|
@@ -9211,14 +9219,14 @@ function sessionResumptionConfigToMldev$1(fromObject) {
|
|
|
9211
9219
|
}
|
|
9212
9220
|
function speechConfigToVertex$1(fromObject) {
|
|
9213
9221
|
const toObject = {};
|
|
9214
|
-
const fromLanguageCode = getValueByPath(fromObject, ['languageCode']);
|
|
9215
|
-
if (fromLanguageCode != null) {
|
|
9216
|
-
setValueByPath(toObject, ['languageCode'], fromLanguageCode);
|
|
9217
|
-
}
|
|
9218
9222
|
const fromVoiceConfig = getValueByPath(fromObject, ['voiceConfig']);
|
|
9219
9223
|
if (fromVoiceConfig != null) {
|
|
9220
9224
|
setValueByPath(toObject, ['voiceConfig'], fromVoiceConfig);
|
|
9221
9225
|
}
|
|
9226
|
+
const fromLanguageCode = getValueByPath(fromObject, ['languageCode']);
|
|
9227
|
+
if (fromLanguageCode != null) {
|
|
9228
|
+
setValueByPath(toObject, ['languageCode'], fromLanguageCode);
|
|
9229
|
+
}
|
|
9222
9230
|
if (getValueByPath(fromObject, ['multiSpeakerVoiceConfig']) !== undefined) {
|
|
9223
9231
|
throw new Error('multiSpeakerVoiceConfig parameter is not supported in Vertex AI.');
|
|
9224
9232
|
}
|
|
@@ -10314,6 +10322,12 @@ function generateContentConfigToMldev(apiClient, fromObject, parentObject) {
|
|
|
10314
10322
|
if (fromImageConfig != null) {
|
|
10315
10323
|
setValueByPath(toObject, ['imageConfig'], imageConfigToMldev(fromImageConfig));
|
|
10316
10324
|
}
|
|
10325
|
+
const fromEnableEnhancedCivicAnswers = getValueByPath(fromObject, [
|
|
10326
|
+
'enableEnhancedCivicAnswers',
|
|
10327
|
+
]);
|
|
10328
|
+
if (fromEnableEnhancedCivicAnswers != null) {
|
|
10329
|
+
setValueByPath(toObject, ['enableEnhancedCivicAnswers'], fromEnableEnhancedCivicAnswers);
|
|
10330
|
+
}
|
|
10317
10331
|
return toObject;
|
|
10318
10332
|
}
|
|
10319
10333
|
function generateContentConfigToVertex(apiClient, fromObject, parentObject) {
|
|
@@ -10478,6 +10492,10 @@ function generateContentConfigToVertex(apiClient, fromObject, parentObject) {
|
|
|
10478
10492
|
if (fromImageConfig != null) {
|
|
10479
10493
|
setValueByPath(toObject, ['imageConfig'], imageConfigToVertex(fromImageConfig));
|
|
10480
10494
|
}
|
|
10495
|
+
if (getValueByPath(fromObject, ['enableEnhancedCivicAnswers']) !==
|
|
10496
|
+
undefined) {
|
|
10497
|
+
throw new Error('enableEnhancedCivicAnswers parameter is not supported in Vertex AI.');
|
|
10498
|
+
}
|
|
10481
10499
|
return toObject;
|
|
10482
10500
|
}
|
|
10483
10501
|
function generateContentParametersToMldev(apiClient, fromObject) {
|
|
@@ -12206,14 +12224,14 @@ function segmentImageSourceToVertex(fromObject, parentObject) {
|
|
|
12206
12224
|
}
|
|
12207
12225
|
function speechConfigToVertex(fromObject) {
|
|
12208
12226
|
const toObject = {};
|
|
12209
|
-
const fromLanguageCode = getValueByPath(fromObject, ['languageCode']);
|
|
12210
|
-
if (fromLanguageCode != null) {
|
|
12211
|
-
setValueByPath(toObject, ['languageCode'], fromLanguageCode);
|
|
12212
|
-
}
|
|
12213
12227
|
const fromVoiceConfig = getValueByPath(fromObject, ['voiceConfig']);
|
|
12214
12228
|
if (fromVoiceConfig != null) {
|
|
12215
12229
|
setValueByPath(toObject, ['voiceConfig'], fromVoiceConfig);
|
|
12216
12230
|
}
|
|
12231
|
+
const fromLanguageCode = getValueByPath(fromObject, ['languageCode']);
|
|
12232
|
+
if (fromLanguageCode != null) {
|
|
12233
|
+
setValueByPath(toObject, ['languageCode'], fromLanguageCode);
|
|
12234
|
+
}
|
|
12217
12235
|
if (getValueByPath(fromObject, ['multiSpeakerVoiceConfig']) !== undefined) {
|
|
12218
12236
|
throw new Error('multiSpeakerVoiceConfig parameter is not supported in Vertex AI.');
|
|
12219
12237
|
}
|
|
@@ -12691,8 +12709,8 @@ function isMcpCallableTool(object) {
|
|
|
12691
12709
|
object instanceof McpCallableTool);
|
|
12692
12710
|
}
|
|
12693
12711
|
// List all tools from the MCP client.
|
|
12694
|
-
function listAllTools(
|
|
12695
|
-
return __asyncGenerator(this, arguments, function* listAllTools_1() {
|
|
12712
|
+
function listAllTools(mcpClient_1) {
|
|
12713
|
+
return __asyncGenerator(this, arguments, function* listAllTools_1(mcpClient, maxTools = 100) {
|
|
12696
12714
|
let cursor = undefined;
|
|
12697
12715
|
let numTools = 0;
|
|
12698
12716
|
while (numTools < maxTools) {
|
|
@@ -14012,9 +14030,9 @@ class Models extends BaseModule {
|
|
|
14012
14030
|
let remoteCallCount = 0;
|
|
14013
14031
|
const afcToolsMap = await this.initAfcToolsMap(params);
|
|
14014
14032
|
return (function (models, afcTools, params) {
|
|
14015
|
-
var _a, _b;
|
|
14016
14033
|
return __asyncGenerator(this, arguments, function* () {
|
|
14017
|
-
var
|
|
14034
|
+
var _a, e_1, _b, _c;
|
|
14035
|
+
var _d, _e;
|
|
14018
14036
|
while (remoteCallCount < maxRemoteCalls) {
|
|
14019
14037
|
if (wereFunctionsCalled) {
|
|
14020
14038
|
remoteCallCount++;
|
|
@@ -14025,14 +14043,14 @@ class Models extends BaseModule {
|
|
|
14025
14043
|
const functionResponses = [];
|
|
14026
14044
|
const responseContents = [];
|
|
14027
14045
|
try {
|
|
14028
|
-
for (var _f = true, response_1 = (e_1 = void 0, __asyncValues(response)), response_1_1; response_1_1 = yield __await(response_1.next()),
|
|
14029
|
-
|
|
14046
|
+
for (var _f = true, response_1 = (e_1 = void 0, __asyncValues(response)), response_1_1; response_1_1 = yield __await(response_1.next()), _a = response_1_1.done, !_a; _f = true) {
|
|
14047
|
+
_c = response_1_1.value;
|
|
14030
14048
|
_f = false;
|
|
14031
|
-
const chunk =
|
|
14049
|
+
const chunk = _c;
|
|
14032
14050
|
yield yield __await(chunk);
|
|
14033
|
-
if (chunk.candidates && ((
|
|
14051
|
+
if (chunk.candidates && ((_d = chunk.candidates[0]) === null || _d === void 0 ? void 0 : _d.content)) {
|
|
14034
14052
|
responseContents.push(chunk.candidates[0].content);
|
|
14035
|
-
for (const part of (
|
|
14053
|
+
for (const part of (_e = chunk.candidates[0].content.parts) !== null && _e !== void 0 ? _e : []) {
|
|
14036
14054
|
if (remoteCallCount < maxRemoteCalls && part.functionCall) {
|
|
14037
14055
|
if (!part.functionCall.name) {
|
|
14038
14056
|
throw new Error('Function call name was not returned by the model.');
|
|
@@ -14054,7 +14072,7 @@ class Models extends BaseModule {
|
|
|
14054
14072
|
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
14055
14073
|
finally {
|
|
14056
14074
|
try {
|
|
14057
|
-
if (!_f && !
|
|
14075
|
+
if (!_f && !_a && (_b = response_1.return)) yield __await(_b.call(response_1));
|
|
14058
14076
|
}
|
|
14059
14077
|
finally { if (e_1) throw e_1.error; }
|
|
14060
14078
|
}
|
|
@@ -15940,6 +15958,26 @@ function cancelTuningJobParametersToVertex(fromObject, _rootObject) {
|
|
|
15940
15958
|
}
|
|
15941
15959
|
return toObject;
|
|
15942
15960
|
}
|
|
15961
|
+
function cancelTuningJobResponseFromMldev(fromObject, _rootObject) {
|
|
15962
|
+
const toObject = {};
|
|
15963
|
+
const fromSdkHttpResponse = getValueByPath(fromObject, [
|
|
15964
|
+
'sdkHttpResponse',
|
|
15965
|
+
]);
|
|
15966
|
+
if (fromSdkHttpResponse != null) {
|
|
15967
|
+
setValueByPath(toObject, ['sdkHttpResponse'], fromSdkHttpResponse);
|
|
15968
|
+
}
|
|
15969
|
+
return toObject;
|
|
15970
|
+
}
|
|
15971
|
+
function cancelTuningJobResponseFromVertex(fromObject, _rootObject) {
|
|
15972
|
+
const toObject = {};
|
|
15973
|
+
const fromSdkHttpResponse = getValueByPath(fromObject, [
|
|
15974
|
+
'sdkHttpResponse',
|
|
15975
|
+
]);
|
|
15976
|
+
if (fromSdkHttpResponse != null) {
|
|
15977
|
+
setValueByPath(toObject, ['sdkHttpResponse'], fromSdkHttpResponse);
|
|
15978
|
+
}
|
|
15979
|
+
return toObject;
|
|
15980
|
+
}
|
|
15943
15981
|
function createTuningJobConfigToMldev(fromObject, parentObject, _rootObject) {
|
|
15944
15982
|
const toObject = {};
|
|
15945
15983
|
if (getValueByPath(fromObject, ['validationDataset']) !== undefined) {
|
|
@@ -16601,28 +16639,33 @@ class Tunings extends BaseModule {
|
|
|
16601
16639
|
super();
|
|
16602
16640
|
this.apiClient = apiClient;
|
|
16603
16641
|
/**
|
|
16604
|
-
*
|
|
16642
|
+
* Lists tuning jobs.
|
|
16605
16643
|
*
|
|
16606
|
-
* @param
|
|
16607
|
-
* @return - A
|
|
16644
|
+
* @param params - The parameters for the list request.
|
|
16645
|
+
* @return - A pager of tuning jobs.
|
|
16608
16646
|
*
|
|
16609
|
-
* @
|
|
16610
|
-
*
|
|
16647
|
+
* @example
|
|
16648
|
+
* ```ts
|
|
16649
|
+
* const tuningJobs = await ai.tunings.list({config: {'pageSize': 2}});
|
|
16650
|
+
* for await (const tuningJob of tuningJobs) {
|
|
16651
|
+
* console.log(tuningJob);
|
|
16652
|
+
* }
|
|
16653
|
+
* ```
|
|
16611
16654
|
*/
|
|
16612
|
-
this.
|
|
16613
|
-
return await this.
|
|
16655
|
+
this.list = async (params = {}) => {
|
|
16656
|
+
return new Pager(exports.PagedItem.PAGED_ITEM_TUNING_JOBS, (x) => this.listInternal(x), await this.listInternal(params), params);
|
|
16614
16657
|
};
|
|
16615
16658
|
/**
|
|
16616
|
-
*
|
|
16659
|
+
* Gets a TuningJob.
|
|
16617
16660
|
*
|
|
16618
|
-
* @param
|
|
16619
|
-
* @return - A
|
|
16661
|
+
* @param name - The resource name of the tuning job.
|
|
16662
|
+
* @return - A TuningJob object.
|
|
16620
16663
|
*
|
|
16621
16664
|
* @experimental - The SDK's tuning implementation is experimental, and may
|
|
16622
16665
|
* change in future versions.
|
|
16623
16666
|
*/
|
|
16624
|
-
this.
|
|
16625
|
-
return
|
|
16667
|
+
this.get = async (params) => {
|
|
16668
|
+
return await this.getInternal(params);
|
|
16626
16669
|
};
|
|
16627
16670
|
/**
|
|
16628
16671
|
* Creates a supervised fine-tuning job.
|
|
@@ -16817,6 +16860,7 @@ class Tunings extends BaseModule {
|
|
|
16817
16860
|
*/
|
|
16818
16861
|
async cancel(params) {
|
|
16819
16862
|
var _a, _b, _c, _d;
|
|
16863
|
+
let response;
|
|
16820
16864
|
let path = '';
|
|
16821
16865
|
let queryParams = {};
|
|
16822
16866
|
if (this.apiClient.isVertexAI()) {
|
|
@@ -16825,13 +16869,29 @@ class Tunings extends BaseModule {
|
|
|
16825
16869
|
queryParams = body['_query'];
|
|
16826
16870
|
delete body['_url'];
|
|
16827
16871
|
delete body['_query'];
|
|
16828
|
-
|
|
16872
|
+
response = this.apiClient
|
|
16873
|
+
.request({
|
|
16829
16874
|
path: path,
|
|
16830
16875
|
queryParams: queryParams,
|
|
16831
16876
|
body: JSON.stringify(body),
|
|
16832
16877
|
httpMethod: 'POST',
|
|
16833
16878
|
httpOptions: (_a = params.config) === null || _a === void 0 ? void 0 : _a.httpOptions,
|
|
16834
16879
|
abortSignal: (_b = params.config) === null || _b === void 0 ? void 0 : _b.abortSignal,
|
|
16880
|
+
})
|
|
16881
|
+
.then((httpResponse) => {
|
|
16882
|
+
return httpResponse.json().then((jsonResponse) => {
|
|
16883
|
+
const response = jsonResponse;
|
|
16884
|
+
response.sdkHttpResponse = {
|
|
16885
|
+
headers: httpResponse.headers,
|
|
16886
|
+
};
|
|
16887
|
+
return response;
|
|
16888
|
+
});
|
|
16889
|
+
});
|
|
16890
|
+
return response.then((apiResponse) => {
|
|
16891
|
+
const resp = cancelTuningJobResponseFromVertex(apiResponse);
|
|
16892
|
+
const typedResp = new CancelTuningJobResponse();
|
|
16893
|
+
Object.assign(typedResp, resp);
|
|
16894
|
+
return typedResp;
|
|
16835
16895
|
});
|
|
16836
16896
|
}
|
|
16837
16897
|
else {
|
|
@@ -16840,13 +16900,29 @@ class Tunings extends BaseModule {
|
|
|
16840
16900
|
queryParams = body['_query'];
|
|
16841
16901
|
delete body['_url'];
|
|
16842
16902
|
delete body['_query'];
|
|
16843
|
-
|
|
16903
|
+
response = this.apiClient
|
|
16904
|
+
.request({
|
|
16844
16905
|
path: path,
|
|
16845
16906
|
queryParams: queryParams,
|
|
16846
16907
|
body: JSON.stringify(body),
|
|
16847
16908
|
httpMethod: 'POST',
|
|
16848
16909
|
httpOptions: (_c = params.config) === null || _c === void 0 ? void 0 : _c.httpOptions,
|
|
16849
16910
|
abortSignal: (_d = params.config) === null || _d === void 0 ? void 0 : _d.abortSignal,
|
|
16911
|
+
})
|
|
16912
|
+
.then((httpResponse) => {
|
|
16913
|
+
return httpResponse.json().then((jsonResponse) => {
|
|
16914
|
+
const response = jsonResponse;
|
|
16915
|
+
response.sdkHttpResponse = {
|
|
16916
|
+
headers: httpResponse.headers,
|
|
16917
|
+
};
|
|
16918
|
+
return response;
|
|
16919
|
+
});
|
|
16920
|
+
});
|
|
16921
|
+
return response.then((apiResponse) => {
|
|
16922
|
+
const resp = cancelTuningJobResponseFromMldev(apiResponse);
|
|
16923
|
+
const typedResp = new CancelTuningJobResponse();
|
|
16924
|
+
Object.assign(typedResp, resp);
|
|
16925
|
+
return typedResp;
|
|
16850
16926
|
});
|
|
16851
16927
|
}
|
|
16852
16928
|
}
|
|
@@ -17001,13 +17077,14 @@ class GoogleGenAI {
|
|
|
17001
17077
|
this.vertexai = (_a = options.vertexai) !== null && _a !== void 0 ? _a : false;
|
|
17002
17078
|
this.apiKey = options.apiKey;
|
|
17003
17079
|
this.apiVersion = options.apiVersion;
|
|
17080
|
+
this.httpOptions = options.httpOptions;
|
|
17004
17081
|
const auth = new WebAuth(this.apiKey);
|
|
17005
17082
|
this.apiClient = new ApiClient({
|
|
17006
17083
|
auth: auth,
|
|
17007
17084
|
apiVersion: this.apiVersion,
|
|
17008
17085
|
apiKey: this.apiKey,
|
|
17009
17086
|
vertexai: this.vertexai,
|
|
17010
|
-
httpOptions:
|
|
17087
|
+
httpOptions: this.httpOptions,
|
|
17011
17088
|
userAgentExtra: LANGUAGE_LABEL_PREFIX + 'cross',
|
|
17012
17089
|
uploader: new CrossUploader(),
|
|
17013
17090
|
downloader: new CrossDownloader(),
|
|
@@ -17028,6 +17105,7 @@ class GoogleGenAI {
|
|
|
17028
17105
|
exports.ApiError = ApiError;
|
|
17029
17106
|
exports.Batches = Batches;
|
|
17030
17107
|
exports.Caches = Caches;
|
|
17108
|
+
exports.CancelTuningJobResponse = CancelTuningJobResponse;
|
|
17031
17109
|
exports.Chat = Chat;
|
|
17032
17110
|
exports.Chats = Chats;
|
|
17033
17111
|
exports.ComputeTokensResponse = ComputeTokensResponse;
|