@aigne/core 1.57.5 → 1.58.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/CHANGELOG.md +19 -0
- package/lib/cjs/agents/ai-agent.d.ts +8 -1
- package/lib/cjs/agents/ai-agent.js +12 -2
- package/lib/cjs/agents/chat-model.d.ts +305 -4
- package/lib/cjs/agents/chat-model.js +106 -10
- package/lib/cjs/agents/image-model.d.ts +2 -2
- package/lib/cjs/aigne/context.js +2 -0
- package/lib/cjs/prompt/prompt-builder.d.ts +2 -2
- package/lib/cjs/prompt/prompt-builder.js +63 -13
- package/lib/dts/agents/ai-agent.d.ts +8 -1
- package/lib/dts/agents/chat-model.d.ts +305 -4
- package/lib/dts/agents/image-model.d.ts +2 -2
- package/lib/dts/prompt/prompt-builder.d.ts +2 -2
- package/lib/esm/agents/ai-agent.d.ts +8 -1
- package/lib/esm/agents/ai-agent.js +11 -1
- package/lib/esm/agents/chat-model.d.ts +305 -4
- package/lib/esm/agents/chat-model.js +103 -10
- package/lib/esm/agents/image-model.d.ts +2 -2
- package/lib/esm/aigne/context.js +2 -0
- package/lib/esm/prompt/prompt-builder.d.ts +2 -2
- package/lib/esm/prompt/prompt-builder.js +64 -14
- package/package.json +3 -2
|
@@ -104,6 +104,10 @@ export declare abstract class ChatModel extends Agent<ChatModelInput, ChatModelO
|
|
|
104
104
|
* @returns A promise or direct value containing the model's response
|
|
105
105
|
*/
|
|
106
106
|
abstract process(input: ChatModelInput, options: AgentInvokeOptions): PromiseOrValue<AgentProcessResult<ChatModelOutput>>;
|
|
107
|
+
transformFileOutput(input: ChatModelInput, data: FileUnionContent, options: AgentInvokeOptions): Promise<FileUnionContent>;
|
|
108
|
+
static getFileExtension(type: string): string | undefined;
|
|
109
|
+
static getMimeType(filename: string): string | undefined;
|
|
110
|
+
protected downloadFile(url: string): Promise<Response>;
|
|
107
111
|
}
|
|
108
112
|
/**
|
|
109
113
|
* Input message format for ChatModel
|
|
@@ -128,6 +132,7 @@ export interface ChatModelInput extends Message {
|
|
|
128
132
|
* Specifies the expected response format
|
|
129
133
|
*/
|
|
130
134
|
responseFormat?: ChatModelInputResponseFormat;
|
|
135
|
+
fileOutputType?: FileOutputType;
|
|
131
136
|
/**
|
|
132
137
|
* List of tools available for the model to use
|
|
133
138
|
*/
|
|
@@ -190,7 +195,7 @@ export interface ChatModelInputMessage {
|
|
|
190
195
|
*
|
|
191
196
|
* Can be a simple string, or a mixed array of text and image content
|
|
192
197
|
*/
|
|
193
|
-
export type ChatModelInputMessageContent = string |
|
|
198
|
+
export type ChatModelInputMessageContent = string | UnionContent[];
|
|
194
199
|
/**
|
|
195
200
|
* Text content type
|
|
196
201
|
*
|
|
@@ -200,15 +205,304 @@ export type TextContent = {
|
|
|
200
205
|
type: "text";
|
|
201
206
|
text: string;
|
|
202
207
|
};
|
|
208
|
+
export declare const textContentSchema: z.ZodObject<{
|
|
209
|
+
type: z.ZodLiteral<"text">;
|
|
210
|
+
text: z.ZodString;
|
|
211
|
+
}, "strip", z.ZodTypeAny, {
|
|
212
|
+
type: "text";
|
|
213
|
+
text: string;
|
|
214
|
+
}, {
|
|
215
|
+
type: "text";
|
|
216
|
+
text: string;
|
|
217
|
+
}>;
|
|
218
|
+
export interface FileContentBase {
|
|
219
|
+
filename?: string;
|
|
220
|
+
mimeType?: string;
|
|
221
|
+
}
|
|
222
|
+
export declare const fileContentBaseSchema: z.ZodObject<{
|
|
223
|
+
filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
224
|
+
mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
225
|
+
}, "strip", z.ZodTypeAny, {
|
|
226
|
+
filename?: string | undefined;
|
|
227
|
+
mimeType?: string | undefined;
|
|
228
|
+
}, {
|
|
229
|
+
filename?: string | undefined;
|
|
230
|
+
mimeType?: string | undefined;
|
|
231
|
+
}>;
|
|
203
232
|
/**
|
|
204
233
|
* Image URL content type
|
|
205
234
|
*
|
|
206
235
|
* Used for image parts of message content, referencing images via URL
|
|
207
236
|
*/
|
|
208
|
-
export
|
|
209
|
-
type: "
|
|
237
|
+
export interface UrlContent extends FileContentBase {
|
|
238
|
+
type: "url";
|
|
210
239
|
url: string;
|
|
211
|
-
}
|
|
240
|
+
}
|
|
241
|
+
export declare const urlContentSchema: z.ZodObject<{
|
|
242
|
+
filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
243
|
+
mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
244
|
+
} & {
|
|
245
|
+
type: z.ZodLiteral<"url">;
|
|
246
|
+
url: z.ZodString;
|
|
247
|
+
}, "strip", z.ZodTypeAny, {
|
|
248
|
+
type: "url";
|
|
249
|
+
url: string;
|
|
250
|
+
filename?: string | undefined;
|
|
251
|
+
mimeType?: string | undefined;
|
|
252
|
+
}, {
|
|
253
|
+
type: "url";
|
|
254
|
+
url: string;
|
|
255
|
+
filename?: string | undefined;
|
|
256
|
+
mimeType?: string | undefined;
|
|
257
|
+
}>;
|
|
258
|
+
export interface FileContent extends FileContentBase {
|
|
259
|
+
type: "file";
|
|
260
|
+
data: string;
|
|
261
|
+
}
|
|
262
|
+
export declare const fileContentSchema: z.ZodObject<{
|
|
263
|
+
filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
264
|
+
mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
265
|
+
} & {
|
|
266
|
+
type: z.ZodLiteral<"file">;
|
|
267
|
+
data: z.ZodString;
|
|
268
|
+
}, "strip", z.ZodTypeAny, {
|
|
269
|
+
type: "file";
|
|
270
|
+
data: string;
|
|
271
|
+
filename?: string | undefined;
|
|
272
|
+
mimeType?: string | undefined;
|
|
273
|
+
}, {
|
|
274
|
+
type: "file";
|
|
275
|
+
data: string;
|
|
276
|
+
filename?: string | undefined;
|
|
277
|
+
mimeType?: string | undefined;
|
|
278
|
+
}>;
|
|
279
|
+
export interface LocalContent extends FileContentBase {
|
|
280
|
+
type: "local";
|
|
281
|
+
path: string;
|
|
282
|
+
}
|
|
283
|
+
export declare const localContentSchema: z.ZodObject<{
|
|
284
|
+
filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
285
|
+
mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
286
|
+
} & {
|
|
287
|
+
type: z.ZodLiteral<"local">;
|
|
288
|
+
path: z.ZodString;
|
|
289
|
+
}, "strip", z.ZodTypeAny, {
|
|
290
|
+
path: string;
|
|
291
|
+
type: "local";
|
|
292
|
+
filename?: string | undefined;
|
|
293
|
+
mimeType?: string | undefined;
|
|
294
|
+
}, {
|
|
295
|
+
path: string;
|
|
296
|
+
type: "local";
|
|
297
|
+
filename?: string | undefined;
|
|
298
|
+
mimeType?: string | undefined;
|
|
299
|
+
}>;
|
|
300
|
+
export type FileUnionContent = LocalContent | UrlContent | FileContent;
|
|
301
|
+
export declare const fileUnionContentSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObject<{
|
|
302
|
+
filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
303
|
+
mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
304
|
+
} & {
|
|
305
|
+
type: z.ZodLiteral<"local">;
|
|
306
|
+
path: z.ZodString;
|
|
307
|
+
}, "strip", z.ZodTypeAny, {
|
|
308
|
+
path: string;
|
|
309
|
+
type: "local";
|
|
310
|
+
filename?: string | undefined;
|
|
311
|
+
mimeType?: string | undefined;
|
|
312
|
+
}, {
|
|
313
|
+
path: string;
|
|
314
|
+
type: "local";
|
|
315
|
+
filename?: string | undefined;
|
|
316
|
+
mimeType?: string | undefined;
|
|
317
|
+
}>, z.ZodObject<{
|
|
318
|
+
filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
319
|
+
mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
320
|
+
} & {
|
|
321
|
+
type: z.ZodLiteral<"url">;
|
|
322
|
+
url: z.ZodString;
|
|
323
|
+
}, "strip", z.ZodTypeAny, {
|
|
324
|
+
type: "url";
|
|
325
|
+
url: string;
|
|
326
|
+
filename?: string | undefined;
|
|
327
|
+
mimeType?: string | undefined;
|
|
328
|
+
}, {
|
|
329
|
+
type: "url";
|
|
330
|
+
url: string;
|
|
331
|
+
filename?: string | undefined;
|
|
332
|
+
mimeType?: string | undefined;
|
|
333
|
+
}>, z.ZodObject<{
|
|
334
|
+
filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
335
|
+
mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
336
|
+
} & {
|
|
337
|
+
type: z.ZodLiteral<"file">;
|
|
338
|
+
data: z.ZodString;
|
|
339
|
+
}, "strip", z.ZodTypeAny, {
|
|
340
|
+
type: "file";
|
|
341
|
+
data: string;
|
|
342
|
+
filename?: string | undefined;
|
|
343
|
+
mimeType?: string | undefined;
|
|
344
|
+
}, {
|
|
345
|
+
type: "file";
|
|
346
|
+
data: string;
|
|
347
|
+
filename?: string | undefined;
|
|
348
|
+
mimeType?: string | undefined;
|
|
349
|
+
}>]>;
|
|
350
|
+
export declare const fileUnionContentsSchema: z.ZodUnion<[z.ZodDiscriminatedUnion<"type", [z.ZodObject<{
|
|
351
|
+
filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
352
|
+
mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
353
|
+
} & {
|
|
354
|
+
type: z.ZodLiteral<"local">;
|
|
355
|
+
path: z.ZodString;
|
|
356
|
+
}, "strip", z.ZodTypeAny, {
|
|
357
|
+
path: string;
|
|
358
|
+
type: "local";
|
|
359
|
+
filename?: string | undefined;
|
|
360
|
+
mimeType?: string | undefined;
|
|
361
|
+
}, {
|
|
362
|
+
path: string;
|
|
363
|
+
type: "local";
|
|
364
|
+
filename?: string | undefined;
|
|
365
|
+
mimeType?: string | undefined;
|
|
366
|
+
}>, z.ZodObject<{
|
|
367
|
+
filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
368
|
+
mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
369
|
+
} & {
|
|
370
|
+
type: z.ZodLiteral<"url">;
|
|
371
|
+
url: z.ZodString;
|
|
372
|
+
}, "strip", z.ZodTypeAny, {
|
|
373
|
+
type: "url";
|
|
374
|
+
url: string;
|
|
375
|
+
filename?: string | undefined;
|
|
376
|
+
mimeType?: string | undefined;
|
|
377
|
+
}, {
|
|
378
|
+
type: "url";
|
|
379
|
+
url: string;
|
|
380
|
+
filename?: string | undefined;
|
|
381
|
+
mimeType?: string | undefined;
|
|
382
|
+
}>, z.ZodObject<{
|
|
383
|
+
filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
384
|
+
mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
385
|
+
} & {
|
|
386
|
+
type: z.ZodLiteral<"file">;
|
|
387
|
+
data: z.ZodString;
|
|
388
|
+
}, "strip", z.ZodTypeAny, {
|
|
389
|
+
type: "file";
|
|
390
|
+
data: string;
|
|
391
|
+
filename?: string | undefined;
|
|
392
|
+
mimeType?: string | undefined;
|
|
393
|
+
}, {
|
|
394
|
+
type: "file";
|
|
395
|
+
data: string;
|
|
396
|
+
filename?: string | undefined;
|
|
397
|
+
mimeType?: string | undefined;
|
|
398
|
+
}>]>, z.ZodArray<z.ZodDiscriminatedUnion<"type", [z.ZodObject<{
|
|
399
|
+
filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
400
|
+
mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
401
|
+
} & {
|
|
402
|
+
type: z.ZodLiteral<"local">;
|
|
403
|
+
path: z.ZodString;
|
|
404
|
+
}, "strip", z.ZodTypeAny, {
|
|
405
|
+
path: string;
|
|
406
|
+
type: "local";
|
|
407
|
+
filename?: string | undefined;
|
|
408
|
+
mimeType?: string | undefined;
|
|
409
|
+
}, {
|
|
410
|
+
path: string;
|
|
411
|
+
type: "local";
|
|
412
|
+
filename?: string | undefined;
|
|
413
|
+
mimeType?: string | undefined;
|
|
414
|
+
}>, z.ZodObject<{
|
|
415
|
+
filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
416
|
+
mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
417
|
+
} & {
|
|
418
|
+
type: z.ZodLiteral<"url">;
|
|
419
|
+
url: z.ZodString;
|
|
420
|
+
}, "strip", z.ZodTypeAny, {
|
|
421
|
+
type: "url";
|
|
422
|
+
url: string;
|
|
423
|
+
filename?: string | undefined;
|
|
424
|
+
mimeType?: string | undefined;
|
|
425
|
+
}, {
|
|
426
|
+
type: "url";
|
|
427
|
+
url: string;
|
|
428
|
+
filename?: string | undefined;
|
|
429
|
+
mimeType?: string | undefined;
|
|
430
|
+
}>, z.ZodObject<{
|
|
431
|
+
filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
432
|
+
mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
433
|
+
} & {
|
|
434
|
+
type: z.ZodLiteral<"file">;
|
|
435
|
+
data: z.ZodString;
|
|
436
|
+
}, "strip", z.ZodTypeAny, {
|
|
437
|
+
type: "file";
|
|
438
|
+
data: string;
|
|
439
|
+
filename?: string | undefined;
|
|
440
|
+
mimeType?: string | undefined;
|
|
441
|
+
}, {
|
|
442
|
+
type: "file";
|
|
443
|
+
data: string;
|
|
444
|
+
filename?: string | undefined;
|
|
445
|
+
mimeType?: string | undefined;
|
|
446
|
+
}>]>, "many">]>;
|
|
447
|
+
export type UnionContent = TextContent | FileUnionContent;
|
|
448
|
+
export declare const unionContentSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObject<{
|
|
449
|
+
type: z.ZodLiteral<"text">;
|
|
450
|
+
text: z.ZodString;
|
|
451
|
+
}, "strip", z.ZodTypeAny, {
|
|
452
|
+
type: "text";
|
|
453
|
+
text: string;
|
|
454
|
+
}, {
|
|
455
|
+
type: "text";
|
|
456
|
+
text: string;
|
|
457
|
+
}>, z.ZodObject<{
|
|
458
|
+
filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
459
|
+
mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
460
|
+
} & {
|
|
461
|
+
type: z.ZodLiteral<"local">;
|
|
462
|
+
path: z.ZodString;
|
|
463
|
+
}, "strip", z.ZodTypeAny, {
|
|
464
|
+
path: string;
|
|
465
|
+
type: "local";
|
|
466
|
+
filename?: string | undefined;
|
|
467
|
+
mimeType?: string | undefined;
|
|
468
|
+
}, {
|
|
469
|
+
path: string;
|
|
470
|
+
type: "local";
|
|
471
|
+
filename?: string | undefined;
|
|
472
|
+
mimeType?: string | undefined;
|
|
473
|
+
}>, z.ZodObject<{
|
|
474
|
+
filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
475
|
+
mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
476
|
+
} & {
|
|
477
|
+
type: z.ZodLiteral<"url">;
|
|
478
|
+
url: z.ZodString;
|
|
479
|
+
}, "strip", z.ZodTypeAny, {
|
|
480
|
+
type: "url";
|
|
481
|
+
url: string;
|
|
482
|
+
filename?: string | undefined;
|
|
483
|
+
mimeType?: string | undefined;
|
|
484
|
+
}, {
|
|
485
|
+
type: "url";
|
|
486
|
+
url: string;
|
|
487
|
+
filename?: string | undefined;
|
|
488
|
+
mimeType?: string | undefined;
|
|
489
|
+
}>, z.ZodObject<{
|
|
490
|
+
filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
491
|
+
mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
492
|
+
} & {
|
|
493
|
+
type: z.ZodLiteral<"file">;
|
|
494
|
+
data: z.ZodString;
|
|
495
|
+
}, "strip", z.ZodTypeAny, {
|
|
496
|
+
type: "file";
|
|
497
|
+
data: string;
|
|
498
|
+
filename?: string | undefined;
|
|
499
|
+
mimeType?: string | undefined;
|
|
500
|
+
}, {
|
|
501
|
+
type: "file";
|
|
502
|
+
data: string;
|
|
503
|
+
filename?: string | undefined;
|
|
504
|
+
mimeType?: string | undefined;
|
|
505
|
+
}>]>;
|
|
212
506
|
/**
|
|
213
507
|
* Model response format settings
|
|
214
508
|
*
|
|
@@ -277,6 +571,7 @@ export type ChatModelInputToolChoice = "auto" | "none" | "required" | {
|
|
|
277
571
|
description?: string;
|
|
278
572
|
};
|
|
279
573
|
};
|
|
574
|
+
export type Modality = "text" | "image" | "audio";
|
|
280
575
|
/**
|
|
281
576
|
* Model-specific configuration options
|
|
282
577
|
*
|
|
@@ -307,6 +602,7 @@ export interface ChatModelOptions {
|
|
|
307
602
|
* Whether to allow parallel tool calls
|
|
308
603
|
*/
|
|
309
604
|
parallelToolCalls?: boolean;
|
|
605
|
+
modalities?: Modality[];
|
|
310
606
|
}
|
|
311
607
|
/**
|
|
312
608
|
* Output message format for ChatModel
|
|
@@ -342,6 +638,11 @@ export interface ChatModelOutput extends Message {
|
|
|
342
638
|
* Model name or version used
|
|
343
639
|
*/
|
|
344
640
|
model?: string;
|
|
641
|
+
files?: FileUnionContent[];
|
|
642
|
+
}
|
|
643
|
+
export declare enum FileOutputType {
|
|
644
|
+
local = "local",
|
|
645
|
+
file = "file"
|
|
345
646
|
}
|
|
346
647
|
/**
|
|
347
648
|
* Tool call information in model output
|
|
@@ -1,6 +1,10 @@
|
|
|
1
|
+
import { nodejs } from "@aigne/platform-helpers/nodejs/index.js";
|
|
1
2
|
import { Ajv } from "ajv";
|
|
3
|
+
import mime from "mime";
|
|
4
|
+
import { v7 } from "uuid";
|
|
2
5
|
import { z } from "zod";
|
|
3
|
-
import {
|
|
6
|
+
import { optionalize } from "../loader/schema.js";
|
|
7
|
+
import { checkArguments, pick } from "../utils/type-utils.js";
|
|
4
8
|
import { Agent, agentOptionsSchema, } from "./agent.js";
|
|
5
9
|
const CHAT_MODEL_DEFAULT_RETRY_OPTIONS = {
|
|
6
10
|
retries: 3,
|
|
@@ -170,18 +174,100 @@ export class ChatModel extends Agent {
|
|
|
170
174
|
options.context.usage.aigneHubCredits += usage.aigneHubCredits;
|
|
171
175
|
}
|
|
172
176
|
}
|
|
177
|
+
async transformFileOutput(input, data, options) {
|
|
178
|
+
const fileOutputType = input.fileOutputType || FileOutputType.local;
|
|
179
|
+
if (fileOutputType === data.type)
|
|
180
|
+
return data;
|
|
181
|
+
const common = pick(data, "filename", "mimeType");
|
|
182
|
+
switch (fileOutputType) {
|
|
183
|
+
case FileOutputType.local: {
|
|
184
|
+
const dir = nodejs.path.join(nodejs.os.tmpdir(), options.context.id);
|
|
185
|
+
await nodejs.fs.mkdir(dir, { recursive: true });
|
|
186
|
+
const ext = ChatModel.getFileExtension(data.mimeType || data.filename || "");
|
|
187
|
+
const id = v7();
|
|
188
|
+
const filename = ext ? `${id}.${ext}` : id;
|
|
189
|
+
const path = nodejs.path.join(dir, filename);
|
|
190
|
+
if (data.type === "file") {
|
|
191
|
+
await nodejs.fs.writeFile(path, data.data, "base64");
|
|
192
|
+
}
|
|
193
|
+
else if (data.type === "url") {
|
|
194
|
+
await this.downloadFile(data.url)
|
|
195
|
+
.then((res) => res.body)
|
|
196
|
+
.then((body) => body && nodejs.fs.writeFile(path, body));
|
|
197
|
+
}
|
|
198
|
+
else {
|
|
199
|
+
throw new Error(`Unexpected file type: ${data.type}`);
|
|
200
|
+
}
|
|
201
|
+
return { ...common, type: "local", path };
|
|
202
|
+
}
|
|
203
|
+
case FileOutputType.file: {
|
|
204
|
+
let base64;
|
|
205
|
+
if (data.type === "local") {
|
|
206
|
+
base64 = await nodejs.fs.readFile(data.path, "base64");
|
|
207
|
+
}
|
|
208
|
+
else if (data.type === "url") {
|
|
209
|
+
base64 = Buffer.from(await (await this.downloadFile(data.url)).arrayBuffer()).toString("base64");
|
|
210
|
+
}
|
|
211
|
+
else {
|
|
212
|
+
throw new Error(`Unexpected file type: ${data.type}`);
|
|
213
|
+
}
|
|
214
|
+
return { ...common, type: "file", data: base64 };
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
static getFileExtension(type) {
|
|
219
|
+
return mime.getExtension(type) || undefined;
|
|
220
|
+
}
|
|
221
|
+
static getMimeType(filename) {
|
|
222
|
+
return mime.getType(filename) || undefined;
|
|
223
|
+
}
|
|
224
|
+
async downloadFile(url) {
|
|
225
|
+
const response = await fetch(url);
|
|
226
|
+
if (!response.ok) {
|
|
227
|
+
const text = await response.text().catch(() => null);
|
|
228
|
+
throw new Error(`Failed to download content from ${url}, ${response.status} ${response.statusText} ${text}`);
|
|
229
|
+
}
|
|
230
|
+
return response;
|
|
231
|
+
}
|
|
173
232
|
}
|
|
233
|
+
export const textContentSchema = z.object({
|
|
234
|
+
type: z.literal("text"),
|
|
235
|
+
text: z.string(),
|
|
236
|
+
});
|
|
237
|
+
export const fileContentBaseSchema = z.object({
|
|
238
|
+
filename: optionalize(z.string()),
|
|
239
|
+
mimeType: optionalize(z.string()),
|
|
240
|
+
});
|
|
241
|
+
export const urlContentSchema = fileContentBaseSchema.extend({
|
|
242
|
+
type: z.literal("url"),
|
|
243
|
+
url: z.string(),
|
|
244
|
+
});
|
|
245
|
+
export const fileContentSchema = fileContentBaseSchema.extend({
|
|
246
|
+
type: z.literal("file"),
|
|
247
|
+
data: z.string(),
|
|
248
|
+
});
|
|
249
|
+
export const localContentSchema = fileContentBaseSchema.extend({
|
|
250
|
+
type: z.literal("local"),
|
|
251
|
+
path: z.string(),
|
|
252
|
+
});
|
|
253
|
+
export const fileUnionContentSchema = z.discriminatedUnion("type", [
|
|
254
|
+
localContentSchema,
|
|
255
|
+
urlContentSchema,
|
|
256
|
+
fileContentSchema,
|
|
257
|
+
]);
|
|
258
|
+
export const fileUnionContentsSchema = z.union([
|
|
259
|
+
fileUnionContentSchema,
|
|
260
|
+
z.array(fileUnionContentSchema),
|
|
261
|
+
]);
|
|
262
|
+
export const unionContentSchema = z.discriminatedUnion("type", [
|
|
263
|
+
textContentSchema,
|
|
264
|
+
localContentSchema,
|
|
265
|
+
urlContentSchema,
|
|
266
|
+
fileContentSchema,
|
|
267
|
+
]);
|
|
174
268
|
const chatModelInputMessageSchema = z.object({
|
|
175
269
|
role: z.union([z.literal("system"), z.literal("user"), z.literal("agent"), z.literal("tool")]),
|
|
176
|
-
content: z
|
|
177
|
-
.union([
|
|
178
|
-
z.string(),
|
|
179
|
-
z.array(z.union([
|
|
180
|
-
z.object({ type: z.literal("text"), text: z.string() }),
|
|
181
|
-
z.object({ type: z.literal("image_url"), url: z.string() }),
|
|
182
|
-
])),
|
|
183
|
-
])
|
|
184
|
-
.optional(),
|
|
270
|
+
content: z.union([z.string(), z.array(unionContentSchema)]).optional(),
|
|
185
271
|
toolCalls: z
|
|
186
272
|
.array(z.object({
|
|
187
273
|
id: z.string(),
|
|
@@ -228,6 +314,7 @@ const chatModelOptionsSchema = z.object({
|
|
|
228
314
|
frequencyPenalty: z.number().optional(),
|
|
229
315
|
presencePenalty: z.number().optional(),
|
|
230
316
|
parallelToolCalls: z.boolean().optional().default(true),
|
|
317
|
+
modalities: z.array(z.enum(["text", "image", "audio"])).optional(),
|
|
231
318
|
});
|
|
232
319
|
const chatModelInputSchema = z.object({
|
|
233
320
|
messages: z.array(chatModelInputMessageSchema),
|
|
@@ -236,6 +323,11 @@ const chatModelInputSchema = z.object({
|
|
|
236
323
|
toolChoice: chatModelInputToolChoiceSchema.optional(),
|
|
237
324
|
modelOptions: chatModelOptionsSchema.optional(),
|
|
238
325
|
});
|
|
326
|
+
export var FileOutputType;
|
|
327
|
+
(function (FileOutputType) {
|
|
328
|
+
FileOutputType["local"] = "local";
|
|
329
|
+
FileOutputType["file"] = "file";
|
|
330
|
+
})(FileOutputType || (FileOutputType = {}));
|
|
239
331
|
const chatModelOutputToolCallSchema = z.object({
|
|
240
332
|
id: z.string(),
|
|
241
333
|
type: z.literal("function"),
|
|
@@ -255,4 +347,5 @@ const chatModelOutputSchema = z.object({
|
|
|
255
347
|
toolCalls: z.array(chatModelOutputToolCallSchema).optional(),
|
|
256
348
|
usage: chatModelOutputUsageSchema.optional(),
|
|
257
349
|
model: z.string().optional(),
|
|
350
|
+
files: z.array(fileUnionContentSchema).optional(),
|
|
258
351
|
});
|
|
@@ -32,13 +32,13 @@ export declare const imageModelInputSchema: z.ZodObject<{
|
|
|
32
32
|
}, "strip", z.ZodTypeAny, {
|
|
33
33
|
prompt: string;
|
|
34
34
|
model?: string | undefined;
|
|
35
|
-
responseFormat?: "
|
|
35
|
+
responseFormat?: "base64" | "url" | undefined;
|
|
36
36
|
image?: string | string[] | undefined;
|
|
37
37
|
n?: number | undefined;
|
|
38
38
|
}, {
|
|
39
39
|
prompt: string;
|
|
40
40
|
model?: string | undefined;
|
|
41
|
-
responseFormat?: "
|
|
41
|
+
responseFormat?: "base64" | "url" | undefined;
|
|
42
42
|
image?: string | string[] | undefined;
|
|
43
43
|
n?: number | undefined;
|
|
44
44
|
}>;
|
package/lib/esm/aigne/context.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { GetPromptResult } from "@modelcontextprotocol/sdk/types.js";
|
|
2
2
|
import { Agent, type AgentInvokeOptions, type Message } from "../agents/agent.js";
|
|
3
|
-
import type
|
|
4
|
-
import type
|
|
3
|
+
import { type AIAgent } from "../agents/ai-agent.js";
|
|
4
|
+
import { type ChatModel, type ChatModelInput } from "../agents/chat-model.js";
|
|
5
5
|
import { ChatMessagesTemplate } from "./template.js";
|
|
6
6
|
export interface PromptBuilderOptions {
|
|
7
7
|
instructions?: string | ChatMessagesTemplate;
|
|
@@ -3,8 +3,11 @@ import { stringify } from "yaml";
|
|
|
3
3
|
import { ZodObject } from "zod";
|
|
4
4
|
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
5
5
|
import { Agent } from "../agents/agent.js";
|
|
6
|
+
import { DEFAULT_FILE_OUTPUT_KEY, DEFAULT_OUTPUT_KEY } from "../agents/ai-agent.js";
|
|
7
|
+
import { fileUnionContentsSchema, } from "../agents/chat-model.js";
|
|
8
|
+
import { optionalize } from "../loader/schema.js";
|
|
6
9
|
import { outputSchemaToResponseFormatSchema } from "../utils/json-schema.js";
|
|
7
|
-
import {
|
|
10
|
+
import { checkArguments, flat, isRecord, unique } from "../utils/type-utils.js";
|
|
8
11
|
import { MEMORY_MESSAGE_TEMPLATE } from "./prompts/memory-message-template.js";
|
|
9
12
|
import { STRUCTURED_STREAM_INSTRUCTIONS } from "./prompts/structured-stream-instructions.js";
|
|
10
13
|
import { AgentMessageTemplate, ChatMessagesTemplate, PromptTemplate, SystemMessageTemplate, UserMessageTemplate, } from "./template.js";
|
|
@@ -34,11 +37,11 @@ export class PromptBuilder {
|
|
|
34
37
|
content = resource.text;
|
|
35
38
|
}
|
|
36
39
|
else if (typeof resource.blob === "string") {
|
|
37
|
-
content = [{ type: "
|
|
40
|
+
content = [{ type: "url", url: resource.blob }];
|
|
38
41
|
}
|
|
39
42
|
}
|
|
40
43
|
else if (i.content.type === "image") {
|
|
41
|
-
content = [{ type: "
|
|
44
|
+
content = [{ type: "url", url: i.content.data }];
|
|
42
45
|
}
|
|
43
46
|
if (!content)
|
|
44
47
|
throw new Error(`Unsupported content type ${i.content.type}`);
|
|
@@ -62,6 +65,7 @@ export class PromptBuilder {
|
|
|
62
65
|
responseFormat: options.agent?.structuredStreamMode
|
|
63
66
|
? undefined
|
|
64
67
|
: this.buildResponseFormat(options),
|
|
68
|
+
fileOutputType: options.agent?.fileOutputType,
|
|
65
69
|
...this.buildTools(options),
|
|
66
70
|
};
|
|
67
71
|
}
|
|
@@ -80,6 +84,10 @@ export class PromptBuilder {
|
|
|
80
84
|
const messages = (await (typeof this.instructions === "string"
|
|
81
85
|
? ChatMessagesTemplate.from([SystemMessageTemplate.from(this.instructions)])
|
|
82
86
|
: this.instructions)?.format(options.input, { workingDir: this.workingDir })) ?? [];
|
|
87
|
+
const fileInputKey = options.agent?.fileInputKey;
|
|
88
|
+
const files = flat(fileInputKey
|
|
89
|
+
? checkArguments("Check input files", optionalize(fileUnionContentsSchema), input?.[fileInputKey])
|
|
90
|
+
: null);
|
|
83
91
|
const memories = [];
|
|
84
92
|
if (options.agent && options.context) {
|
|
85
93
|
memories.push(...(await options.agent.retrieveMemories({ search: message }, { context: options.context })));
|
|
@@ -101,26 +109,68 @@ export class PromptBuilder {
|
|
|
101
109
|
},
|
|
102
110
|
})));
|
|
103
111
|
}
|
|
104
|
-
if (message) {
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
content: message
|
|
108
|
-
|
|
112
|
+
if (message || files.length) {
|
|
113
|
+
const content = [];
|
|
114
|
+
if (message)
|
|
115
|
+
content.push({ type: "text", text: message });
|
|
116
|
+
if (files.length)
|
|
117
|
+
content.push(...files);
|
|
118
|
+
messages.push({ role: "user", content });
|
|
109
119
|
}
|
|
110
120
|
return messages;
|
|
111
121
|
}
|
|
112
122
|
async convertMemoriesToMessages(memories, options) {
|
|
113
123
|
const messages = [];
|
|
114
124
|
const other = [];
|
|
125
|
+
const inputKey = options.agent?.inputKey;
|
|
126
|
+
const fileInputKey = options.agent?.fileInputKey;
|
|
127
|
+
const outputKey = options.agent?.outputKey || DEFAULT_OUTPUT_KEY;
|
|
128
|
+
const fileOutputKey = options.agent?.fileOutputKey || DEFAULT_FILE_OUTPUT_KEY;
|
|
115
129
|
const stringOrStringify = (value) => typeof value === "string" ? value : stringify(value);
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
130
|
+
const convertMemoryToMessage = async (content) => {
|
|
131
|
+
const { input, output } = content;
|
|
132
|
+
if (!input || !output)
|
|
133
|
+
return [];
|
|
134
|
+
const result = [];
|
|
135
|
+
const userMessageContent = [];
|
|
136
|
+
if (typeof input === "object") {
|
|
137
|
+
const inputMessage = inputKey ? Reflect.get(input, inputKey) : undefined;
|
|
138
|
+
const inputFiles = fileInputKey ? Reflect.get(input, fileInputKey) : undefined;
|
|
139
|
+
if (inputMessage) {
|
|
140
|
+
userMessageContent.push({ type: "text", text: stringOrStringify(inputMessage) });
|
|
120
141
|
}
|
|
121
|
-
if (
|
|
122
|
-
|
|
142
|
+
if (inputFiles) {
|
|
143
|
+
userMessageContent.push(...flat(checkArguments("Check memory input files", optionalize(fileUnionContentsSchema), inputFiles)));
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
if (!userMessageContent.length) {
|
|
147
|
+
userMessageContent.push({ type: "text", text: stringOrStringify(input) });
|
|
148
|
+
}
|
|
149
|
+
result.push({ role: "user", content: userMessageContent });
|
|
150
|
+
const agentMessageContent = [];
|
|
151
|
+
if (typeof output === "object") {
|
|
152
|
+
const outputMessage = Reflect.get(output, outputKey);
|
|
153
|
+
const outputFiles = Reflect.get(output, fileOutputKey);
|
|
154
|
+
if (outputMessage) {
|
|
155
|
+
agentMessageContent.push({ type: "text", text: stringOrStringify(outputMessage) });
|
|
123
156
|
}
|
|
157
|
+
if (outputFiles) {
|
|
158
|
+
agentMessageContent.push(...flat(checkArguments("Check memory output files", optionalize(fileUnionContentsSchema), outputFiles)));
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
if (!agentMessageContent.length) {
|
|
162
|
+
agentMessageContent.push({ type: "text", text: stringOrStringify(output) });
|
|
163
|
+
}
|
|
164
|
+
result.push({ role: "agent", content: agentMessageContent });
|
|
165
|
+
return result;
|
|
166
|
+
};
|
|
167
|
+
for (const { content } of memories) {
|
|
168
|
+
if (isRecord(content) &&
|
|
169
|
+
"input" in content &&
|
|
170
|
+
content.input &&
|
|
171
|
+
"output" in content &&
|
|
172
|
+
content.output) {
|
|
173
|
+
messages.push(...(await convertMemoryToMessage(content)));
|
|
124
174
|
}
|
|
125
175
|
else {
|
|
126
176
|
other.push(content);
|