@aigne/core 1.57.4 → 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 +26 -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 +137 -12
- 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 +104 -12
- 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
|
@@ -7,6 +7,7 @@ import { checkArguments, isEmpty } from "../utils/type-utils.js";
|
|
|
7
7
|
import { Agent, agentOptionsSchema, isAgentResponseDelta, } from "./agent.js";
|
|
8
8
|
import { isTransferAgentOutput } from "./types.js";
|
|
9
9
|
export const DEFAULT_OUTPUT_KEY = "message";
|
|
10
|
+
export const DEFAULT_FILE_OUTPUT_KEY = "files";
|
|
10
11
|
/**
|
|
11
12
|
* Tool choice options for AI agents
|
|
12
13
|
*
|
|
@@ -108,7 +109,10 @@ export class AIAgent extends Agent {
|
|
|
108
109
|
? PromptBuilder.from(options.instructions)
|
|
109
110
|
: (options.instructions ?? new PromptBuilder());
|
|
110
111
|
this.inputKey = options.inputKey;
|
|
112
|
+
this.fileInputKey = options.fileInputKey;
|
|
111
113
|
this.outputKey = options.outputKey || DEFAULT_OUTPUT_KEY;
|
|
114
|
+
this.fileOutputKey = options.fileOutputKey || DEFAULT_FILE_OUTPUT_KEY;
|
|
115
|
+
this.fileOutputType = options.fileOutputType;
|
|
112
116
|
this.toolChoice = options.toolChoice;
|
|
113
117
|
this.memoryAgentsAsTools = options.memoryAgentsAsTools;
|
|
114
118
|
this.memoryPromptTemplate = options.memoryPromptTemplate;
|
|
@@ -148,6 +152,7 @@ export class AIAgent extends Agent {
|
|
|
148
152
|
* Pick a message from input to use as the user's message
|
|
149
153
|
*/
|
|
150
154
|
inputKey;
|
|
155
|
+
fileInputKey;
|
|
151
156
|
/**
|
|
152
157
|
* Custom key to use for text output in the response
|
|
153
158
|
*
|
|
@@ -156,6 +161,8 @@ export class AIAgent extends Agent {
|
|
|
156
161
|
* {@includeCode ../../test/agents/ai-agent.test.ts#example-ai-agent-custom-output-key}
|
|
157
162
|
*/
|
|
158
163
|
outputKey;
|
|
164
|
+
fileOutputKey;
|
|
165
|
+
fileOutputType;
|
|
159
166
|
/**
|
|
160
167
|
* Controls how the agent uses tools during execution
|
|
161
168
|
*
|
|
@@ -266,7 +273,7 @@ export class AIAgent extends Agent {
|
|
|
266
273
|
}
|
|
267
274
|
}
|
|
268
275
|
}
|
|
269
|
-
const { toolCalls, json, text } = modelOutput;
|
|
276
|
+
const { toolCalls, json, text, files } = modelOutput;
|
|
270
277
|
if (toolCalls?.length) {
|
|
271
278
|
const executedToolCalls = [];
|
|
272
279
|
// Execute tools
|
|
@@ -305,6 +312,9 @@ export class AIAgent extends Agent {
|
|
|
305
312
|
if (text) {
|
|
306
313
|
Object.assign(result, { [outputKey]: text });
|
|
307
314
|
}
|
|
315
|
+
if (files) {
|
|
316
|
+
Object.assign(result, { [this.fileOutputKey]: files });
|
|
317
|
+
}
|
|
308
318
|
if (!isEmpty(result)) {
|
|
309
319
|
yield { delta: { json: result } };
|
|
310
320
|
}
|
|
@@ -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,11 +1,14 @@
|
|
|
1
|
+
import { nodejs } from "@aigne/platform-helpers/nodejs/index.js";
|
|
1
2
|
import { Ajv } from "ajv";
|
|
2
|
-
import
|
|
3
|
+
import mime from "mime";
|
|
4
|
+
import { v7 } from "uuid";
|
|
3
5
|
import { z } from "zod";
|
|
4
|
-
import {
|
|
6
|
+
import { optionalize } from "../loader/schema.js";
|
|
7
|
+
import { checkArguments, pick } from "../utils/type-utils.js";
|
|
5
8
|
import { Agent, agentOptionsSchema, } from "./agent.js";
|
|
6
9
|
const CHAT_MODEL_DEFAULT_RETRY_OPTIONS = {
|
|
7
10
|
retries: 3,
|
|
8
|
-
shouldRetry: (error) => error instanceof StructuredOutputError ||
|
|
11
|
+
shouldRetry: async (error) => error instanceof StructuredOutputError || (await import("is-network-error")).default(error),
|
|
9
12
|
};
|
|
10
13
|
export class StructuredOutputError extends Error {
|
|
11
14
|
}
|
|
@@ -171,18 +174,100 @@ export class ChatModel extends Agent {
|
|
|
171
174
|
options.context.usage.aigneHubCredits += usage.aigneHubCredits;
|
|
172
175
|
}
|
|
173
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
|
+
}
|
|
174
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
|
+
]);
|
|
175
268
|
const chatModelInputMessageSchema = z.object({
|
|
176
269
|
role: z.union([z.literal("system"), z.literal("user"), z.literal("agent"), z.literal("tool")]),
|
|
177
|
-
content: z
|
|
178
|
-
.union([
|
|
179
|
-
z.string(),
|
|
180
|
-
z.array(z.union([
|
|
181
|
-
z.object({ type: z.literal("text"), text: z.string() }),
|
|
182
|
-
z.object({ type: z.literal("image_url"), url: z.string() }),
|
|
183
|
-
])),
|
|
184
|
-
])
|
|
185
|
-
.optional(),
|
|
270
|
+
content: z.union([z.string(), z.array(unionContentSchema)]).optional(),
|
|
186
271
|
toolCalls: z
|
|
187
272
|
.array(z.object({
|
|
188
273
|
id: z.string(),
|
|
@@ -229,6 +314,7 @@ const chatModelOptionsSchema = z.object({
|
|
|
229
314
|
frequencyPenalty: z.number().optional(),
|
|
230
315
|
presencePenalty: z.number().optional(),
|
|
231
316
|
parallelToolCalls: z.boolean().optional().default(true),
|
|
317
|
+
modalities: z.array(z.enum(["text", "image", "audio"])).optional(),
|
|
232
318
|
});
|
|
233
319
|
const chatModelInputSchema = z.object({
|
|
234
320
|
messages: z.array(chatModelInputMessageSchema),
|
|
@@ -237,6 +323,11 @@ const chatModelInputSchema = z.object({
|
|
|
237
323
|
toolChoice: chatModelInputToolChoiceSchema.optional(),
|
|
238
324
|
modelOptions: chatModelOptionsSchema.optional(),
|
|
239
325
|
});
|
|
326
|
+
export var FileOutputType;
|
|
327
|
+
(function (FileOutputType) {
|
|
328
|
+
FileOutputType["local"] = "local";
|
|
329
|
+
FileOutputType["file"] = "file";
|
|
330
|
+
})(FileOutputType || (FileOutputType = {}));
|
|
240
331
|
const chatModelOutputToolCallSchema = z.object({
|
|
241
332
|
id: z.string(),
|
|
242
333
|
type: z.literal("function"),
|
|
@@ -256,4 +347,5 @@ const chatModelOutputSchema = z.object({
|
|
|
256
347
|
toolCalls: z.array(chatModelOutputToolCallSchema).optional(),
|
|
257
348
|
usage: chatModelOutputUsageSchema.optional(),
|
|
258
349
|
model: z.string().optional(),
|
|
350
|
+
files: z.array(fileUnionContentSchema).optional(),
|
|
259
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;
|