@tstdl/base 0.92.36 → 0.92.37

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.
@@ -1,5 +1,5 @@
1
1
  import '../polyfills.js';
2
- import { afterResolve, Resolvable, type resolveArgumentType } from '../injector/interfaces.js';
2
+ import { Resolvable, type resolveArgumentType } from '../injector/interfaces.js';
3
3
  import { AiServiceOptions } from './ai.service.js';
4
4
  import { FileContentPart, FileInput } from './types.js';
5
5
  export type AiFileServiceOptions = Pick<AiServiceOptions, 'apiKey' | 'vertex'>;
@@ -13,7 +13,6 @@ type File = {
13
13
  export declare class AiFileService implements Resolvable<AiFileServiceArgument> {
14
14
  #private;
15
15
  readonly [resolveArgumentType]: AiFileServiceArgument;
16
- [afterResolve](): Promise<void>;
17
16
  processFile(fileInput: FileInput): Promise<FileContentPart>;
18
17
  processFiles(fileInputs: FileInput[]): Promise<FileContentPart[]>;
19
18
  getFileById(id: string): File | undefined;
@@ -21,6 +20,7 @@ export declare class AiFileService implements Resolvable<AiFileServiceArgument>
21
20
  private getFile;
22
21
  private getFiles;
23
22
  private uploadFile;
23
+ private getBucket;
24
24
  private waitForFileActive;
25
25
  private waitForFilesActive;
26
26
  }
@@ -66,7 +66,6 @@ import { AsyncEnumerable } from '../enumerable/async-enumerable.js';
66
66
  import { DetailsError } from '../errors/details.error.js';
67
67
  import { Singleton } from '../injector/decorators.js';
68
68
  import { inject, injectArgument } from '../injector/inject.js';
69
- import { afterResolve } from '../injector/interfaces.js';
70
69
  import { Logger } from '../logger/logger.js';
71
70
  import { createArray } from '../utils/array/array.js';
72
71
  import { formatBytes } from '../utils/format.js';
@@ -77,28 +76,11 @@ import { millisecondsPerSecond } from '../utils/units.js';
77
76
  let AiFileService = class AiFileService {
78
77
  #options = injectArgument(this);
79
78
  #fileManager = isUndefined(this.#options.vertex) ? new GoogleAIFileManager(this.#options.apiKey) : undefined;
80
- #storage = isDefined(this.#options.vertex) ? new Storage({ apiKey: this.#options.apiKey }) : undefined;
79
+ #storage = isDefined(this.#options.vertex) ? new Storage({ projectId: this.#options.vertex.project, apiKey: this.#options.apiKey }) : undefined;
81
80
  #fileMap = new Map();
82
81
  #fileUriMap = new Map();
83
82
  #logger = inject(Logger, 'AiFileService');
84
83
  #bucket;
85
- async [afterResolve]() {
86
- if (isDefined(this.#options.vertex)) {
87
- const bucketName = assertDefinedPass(this.#options.vertex.bucket, 'Bucket not specified');
88
- const [exists] = await this.#storage.bucket(bucketName).exists();
89
- if (!exists) {
90
- const [bucket] = await this.#storage.createBucket(bucketName, {
91
- lifecycle: {
92
- rule: [{
93
- action: { type: 'Delete' },
94
- condition: { age: 1 }
95
- }]
96
- }
97
- });
98
- this.#bucket = bucket;
99
- }
100
- }
101
- }
102
84
  async processFile(fileInput) {
103
85
  const file = await this.getFile(fileInput);
104
86
  this.#fileMap.set(file.id, file);
@@ -146,8 +128,9 @@ let AiFileService = class AiFileService {
146
128
  }
147
129
  const fileSize = isBlob(fileInput) ? fileInput.size : (await stat(path)).size;
148
130
  this.#logger.verbose(`Uploading file "${id}" (${formatBytes(fileSize)})...`);
149
- if (isDefined(this.#bucket)) {
150
- const [file] = await this.#bucket.upload(path, { destination: id, contentType: mimeType });
131
+ if (isDefined(this.#storage)) {
132
+ const bucket = await this.getBucket();
133
+ const [file] = await bucket.upload(path, { destination: id, contentType: mimeType });
151
134
  return {
152
135
  id,
153
136
  name: id,
@@ -173,6 +156,29 @@ let AiFileService = class AiFileService {
173
156
  await result_1;
174
157
  }
175
158
  }
159
+ async getBucket() {
160
+ if (isUndefined(this.#options.vertex)) {
161
+ throw new Error('Not using Vertex');
162
+ }
163
+ if (isDefined(this.#bucket)) {
164
+ return this.#bucket;
165
+ }
166
+ const bucketName = assertDefinedPass(this.#options.vertex.bucket, 'Bucket not specified');
167
+ const [exists] = await this.#storage.bucket(bucketName).exists();
168
+ if (!exists) {
169
+ const [bucket] = await this.#storage.createBucket(bucketName, {
170
+ location: this.#options.vertex.location,
171
+ lifecycle: {
172
+ rule: [{
173
+ action: { type: 'Delete' },
174
+ condition: { age: 1 }
175
+ }]
176
+ }
177
+ });
178
+ this.#bucket = bucket;
179
+ }
180
+ return this.#bucket;
181
+ }
176
182
  async waitForFileActive(file) {
177
183
  if (isUndefined(this.#fileManager)) {
178
184
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tstdl/base",
3
- "version": "0.92.36",
3
+ "version": "0.92.37",
4
4
  "author": "Patrick Hein",
5
5
  "publishConfig": {
6
6
  "access": "public"