@tstdl/base 0.92.1 → 0.92.3

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.
@@ -2,7 +2,7 @@ import '../polyfills.js';
2
2
  import { GoogleGenerativeAI } from '@google/generative-ai';
3
3
  import { type FileMetadataResponse, GoogleAIFileManager } from '@google/generative-ai/server';
4
4
  import { Resolvable, type resolveArgumentType } from '../injector/interfaces.js';
5
- import { SchemaTestable } from '../schema/index.js';
5
+ import { OneOrMany, SchemaTestable } from '../schema/index.js';
6
6
  import { Enumeration as EnumerationType, EnumerationValue } from '../types.js';
7
7
  import { LiteralUnion } from 'type-fest';
8
8
  export type FileInput = {
@@ -22,15 +22,15 @@ export declare class AiService implements Resolvable<AiServiceArgument> {
22
22
  readonly model: import("@google/generative-ai").GenerativeModel;
23
23
  readonly [resolveArgumentType]: AiServiceArgument;
24
24
  getFile(fileInput: FileInput): Promise<FileMetadataResponse>;
25
- getFiles(files: FileInput[]): Promise<FileMetadataResponse[]>;
26
- classify<T extends EnumerationType>(fileInput: FileInput, types: T): Promise<{
25
+ getFiles(files: readonly FileInput[]): Promise<FileMetadataResponse[]>;
26
+ classify<T extends EnumerationType>(fileInput: OneOrMany<FileInput>, types: T): Promise<{
27
27
  reasoning: string;
28
28
  types: {
29
29
  type: EnumerationValue<T>;
30
30
  confidence: 'high' | 'medium' | 'low';
31
31
  }[] | null;
32
32
  }>;
33
- extractData<T>(fileInput: FileInput, schema: SchemaTestable<T>): Promise<T>;
33
+ extractData<T>(fileInput: OneOrMany<FileInput>, schema: SchemaTestable<T>): Promise<T>;
34
34
  waitForFileActive(fileMetadata: FileMetadataResponse): Promise<FileMetadataResponse>;
35
35
  waitForFilesActive(...files: FileMetadataResponse[]): Promise<FileMetadataResponse[]>;
36
36
  }
@@ -68,6 +68,7 @@ import { Singleton } from '../injector/decorators.js';
68
68
  import { injectArgument } from '../injector/inject.js';
69
69
  import { convertToOpenApiSchema } from '../schema/converters/openapi-converter.js';
70
70
  import { array, enumeration, nullable, object, Schema, string } from '../schema/index.js';
71
+ import { toArray } from '../utils/array/array.js';
71
72
  import { digest } from '../utils/cryptography.js';
72
73
  import { timeout } from '../utils/timing.js';
73
74
  import { tryIgnoreAsync } from '../utils/try-ignore.js';
@@ -130,7 +131,7 @@ let AiService = class AiService {
130
131
  return Promise.all(files.map(async (file) => this.getFile(file)));
131
132
  }
132
133
  async classify(fileInput, types) {
133
- const file = await this.getFile(fileInput);
134
+ const files = await this.getFiles(toArray(fileInput));
134
135
  const resultSchema = object({
135
136
  reasoning: string({ description: 'Reasoning for classification. Use to be more confident, if unsure. Reason for every somewhat likely document type.' }),
136
137
  types: nullable(array(object({
@@ -151,7 +152,7 @@ let AiService = class AiService {
151
152
  {
152
153
  role: 'user',
153
154
  parts: [
154
- { fileData: { mimeType: file.mimeType, fileUri: file.uri } },
155
+ ...files.map((file) => ({ fileData: { mimeType: file.mimeType, fileUri: file.uri } })),
155
156
  { text: `Classify the document. Output as JSON using the following schema:\n${JSON.stringify(responseSchema, null, 2)}\n\nIf none of the provided document types are a suitable match, return null for types.` }
156
157
  ]
157
158
  }
@@ -160,7 +161,7 @@ let AiService = class AiService {
160
161
  return resultSchema.parse(JSON.parse(result.response.text()));
161
162
  }
162
163
  async extractData(fileInput, schema) {
163
- const file = await this.getFile(fileInput);
164
+ const files = await this.getFiles(toArray(fileInput));
164
165
  const responseSchema = convertToOpenApiSchema(schema);
165
166
  const result = await this.model.generateContent({
166
167
  generationConfig: {
@@ -182,7 +183,7 @@ You *MUST* output the reasoning first.`,
182
183
  {
183
184
  role: 'user',
184
185
  parts: [
185
- { fileData: { mimeType: file.mimeType, fileUri: file.uri } },
186
+ ...files.map((file) => ({ fileData: { mimeType: file.mimeType, fileUri: file.uri } })),
186
187
  { text: `Classify the document. Output as JSON using the following schema:\n${JSON.stringify(responseSchema, null, 2)}` }
187
188
  ]
188
189
  }
package/ai/index.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './ai.service.js';
package/ai/index.js ADDED
@@ -0,0 +1 @@
1
+ export * from './ai.service.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tstdl/base",
3
- "version": "0.92.1",
3
+ "version": "0.92.3",
4
4
  "author": "Patrick Hein",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -32,6 +32,7 @@
32
32
  "./tokens": "./tokens.js",
33
33
  "./types": "./types.js",
34
34
  "./web-types": "./web-types.js",
35
+ "./ai": "./ai/index.js",
35
36
  "./api": "./api/index.js",
36
37
  "./application": "./application/index.js",
37
38
  "./authentication": "./authentication/index.js",