@tstdl/base 0.92.35 → 0.92.36
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/ai/ai-file.service.d.ts +2 -1
- package/ai/ai-file.service.js +57 -33
- package/ai/ai.service.d.ts +1 -0
- package/package.json +2 -1
package/ai/ai-file.service.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import '../polyfills.js';
|
|
2
|
-
import { Resolvable, type resolveArgumentType } from '../injector/interfaces.js';
|
|
2
|
+
import { afterResolve, 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,6 +13,7 @@ type File = {
|
|
|
13
13
|
export declare class AiFileService implements Resolvable<AiFileServiceArgument> {
|
|
14
14
|
#private;
|
|
15
15
|
readonly [resolveArgumentType]: AiFileServiceArgument;
|
|
16
|
+
[afterResolve](): Promise<void>;
|
|
16
17
|
processFile(fileInput: FileInput): Promise<FileContentPart>;
|
|
17
18
|
processFiles(fileInputs: FileInput[]): Promise<FileContentPart[]>;
|
|
18
19
|
getFileById(id: string): File | undefined;
|
package/ai/ai-file.service.js
CHANGED
|
@@ -60,24 +60,45 @@ import '../polyfills.js';
|
|
|
60
60
|
import { stat, unlink, writeFile } from 'node:fs/promises';
|
|
61
61
|
import { tmpdir } from 'node:os';
|
|
62
62
|
import { join } from 'node:path';
|
|
63
|
+
import { Storage } from '@google-cloud/storage';
|
|
63
64
|
import { FileState, GoogleAIFileManager } from '@google/generative-ai/server';
|
|
64
65
|
import { AsyncEnumerable } from '../enumerable/async-enumerable.js';
|
|
65
66
|
import { DetailsError } from '../errors/details.error.js';
|
|
66
67
|
import { Singleton } from '../injector/decorators.js';
|
|
67
68
|
import { inject, injectArgument } from '../injector/inject.js';
|
|
69
|
+
import { afterResolve } from '../injector/interfaces.js';
|
|
68
70
|
import { Logger } from '../logger/logger.js';
|
|
69
71
|
import { createArray } from '../utils/array/array.js';
|
|
70
72
|
import { formatBytes } from '../utils/format.js';
|
|
71
73
|
import { timeout } from '../utils/timing.js';
|
|
72
74
|
import { tryIgnoreAsync } from '../utils/try-ignore.js';
|
|
73
|
-
import { isBlob } from '../utils/type-guards.js';
|
|
75
|
+
import { assertDefinedPass, isBlob, isDefined, isUndefined } from '../utils/type-guards.js';
|
|
74
76
|
import { millisecondsPerSecond } from '../utils/units.js';
|
|
75
77
|
let AiFileService = class AiFileService {
|
|
76
78
|
#options = injectArgument(this);
|
|
77
|
-
#fileManager = new GoogleAIFileManager(this.#options.apiKey);
|
|
79
|
+
#fileManager = isUndefined(this.#options.vertex) ? new GoogleAIFileManager(this.#options.apiKey) : undefined;
|
|
80
|
+
#storage = isDefined(this.#options.vertex) ? new Storage({ apiKey: this.#options.apiKey }) : undefined;
|
|
78
81
|
#fileMap = new Map();
|
|
79
82
|
#fileUriMap = new Map();
|
|
80
83
|
#logger = inject(Logger, 'AiFileService');
|
|
84
|
+
#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
|
+
}
|
|
81
102
|
async processFile(fileInput) {
|
|
82
103
|
const file = await this.getFile(fileInput);
|
|
83
104
|
this.#fileMap.set(file.id, file);
|
|
@@ -100,27 +121,17 @@ let AiFileService = class AiFileService {
|
|
|
100
121
|
}
|
|
101
122
|
async getFile(fileInput) {
|
|
102
123
|
const id = crypto.randomUUID();
|
|
103
|
-
const
|
|
124
|
+
const file = await this.uploadFile(fileInput, id);
|
|
104
125
|
this.#logger.verbose(`Processing file "${id}"...`);
|
|
105
|
-
|
|
106
|
-
return
|
|
107
|
-
id,
|
|
108
|
-
name: response.name,
|
|
109
|
-
uri: response.uri,
|
|
110
|
-
mimeType: response.mimeType
|
|
111
|
-
};
|
|
126
|
+
await this.waitForFileActive(file);
|
|
127
|
+
return file;
|
|
112
128
|
}
|
|
113
|
-
async getFiles(
|
|
114
|
-
const ids = createArray(
|
|
115
|
-
const
|
|
116
|
-
this.#logger.verbose(`Processing ${
|
|
117
|
-
|
|
118
|
-
return
|
|
119
|
-
id: ids[index],
|
|
120
|
-
name: response.name,
|
|
121
|
-
uri: response.uri,
|
|
122
|
-
mimeType: response.mimeType
|
|
123
|
-
}));
|
|
129
|
+
async getFiles(fileInputs) {
|
|
130
|
+
const ids = createArray(fileInputs.length, () => crypto.randomUUID());
|
|
131
|
+
const files = await AsyncEnumerable.from(fileInputs).parallelMap(5, true, async (file, index) => this.uploadFile(file, ids[index])).toArray();
|
|
132
|
+
this.#logger.verbose(`Processing ${fileInputs.length} files...`);
|
|
133
|
+
await this.waitForFilesActive(files);
|
|
134
|
+
return files;
|
|
124
135
|
}
|
|
125
136
|
async uploadFile(fileInput, id) {
|
|
126
137
|
const env_1 = { stack: [], error: void 0, hasError: false };
|
|
@@ -135,8 +146,22 @@ let AiFileService = class AiFileService {
|
|
|
135
146
|
}
|
|
136
147
|
const fileSize = isBlob(fileInput) ? fileInput.size : (await stat(path)).size;
|
|
137
148
|
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 });
|
|
151
|
+
return {
|
|
152
|
+
id,
|
|
153
|
+
name: id,
|
|
154
|
+
uri: file.cloudStorageURI.toString(),
|
|
155
|
+
mimeType
|
|
156
|
+
};
|
|
157
|
+
}
|
|
138
158
|
const response = await this.#fileManager.uploadFile(path, { mimeType });
|
|
139
|
-
return
|
|
159
|
+
return {
|
|
160
|
+
id,
|
|
161
|
+
name: response.file.name,
|
|
162
|
+
uri: response.file.uri,
|
|
163
|
+
mimeType: response.file.mimeType
|
|
164
|
+
};
|
|
140
165
|
}
|
|
141
166
|
catch (e_1) {
|
|
142
167
|
env_1.error = e_1;
|
|
@@ -148,24 +173,23 @@ let AiFileService = class AiFileService {
|
|
|
148
173
|
await result_1;
|
|
149
174
|
}
|
|
150
175
|
}
|
|
151
|
-
async waitForFileActive(
|
|
152
|
-
|
|
153
|
-
|
|
176
|
+
async waitForFileActive(file) {
|
|
177
|
+
if (isUndefined(this.#fileManager)) {
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
let state = await this.#fileManager.getFile(file.name);
|
|
181
|
+
while (state.state == FileState.PROCESSING) {
|
|
154
182
|
await timeout(millisecondsPerSecond);
|
|
155
|
-
|
|
183
|
+
state = await this.#fileManager.getFile(file.name);
|
|
156
184
|
}
|
|
157
|
-
if (
|
|
158
|
-
throw new DetailsError(
|
|
185
|
+
if (state.state == FileState.FAILED) {
|
|
186
|
+
throw new DetailsError(state.error?.message ?? `Failed to process file ${state.name}`, state.error?.details);
|
|
159
187
|
}
|
|
160
|
-
return file;
|
|
161
188
|
}
|
|
162
189
|
async waitForFilesActive(files) {
|
|
163
|
-
const responses = [];
|
|
164
190
|
for (const file of files) {
|
|
165
|
-
|
|
166
|
-
responses.push(respones);
|
|
191
|
+
await this.waitForFileActive(file);
|
|
167
192
|
}
|
|
168
|
-
return responses;
|
|
169
193
|
}
|
|
170
194
|
};
|
|
171
195
|
AiFileService = __decorate([
|
package/ai/ai.service.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tstdl/base",
|
|
3
|
-
"version": "0.92.
|
|
3
|
+
"version": "0.92.36",
|
|
4
4
|
"author": "Patrick Hein",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -118,6 +118,7 @@
|
|
|
118
118
|
"./utils/string": "./utils/string/index.js"
|
|
119
119
|
},
|
|
120
120
|
"dependencies": {
|
|
121
|
+
"@google-cloud/storage": "^7.15.0",
|
|
121
122
|
"@google-cloud/vertexai": "^1.9.2",
|
|
122
123
|
"disposablestack": "1.1",
|
|
123
124
|
"luxon": "^3.5",
|