@aigne/core 1.57.5 → 1.58.1
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/agent.d.ts +1 -1
- 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 +307 -5
- package/lib/cjs/agents/chat-model.js +116 -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/agent.d.ts +1 -1
- package/lib/dts/agents/ai-agent.d.ts +8 -1
- package/lib/dts/agents/chat-model.d.ts +307 -5
- package/lib/dts/agents/image-model.d.ts +2 -2
- package/lib/dts/prompt/prompt-builder.d.ts +2 -2
- package/lib/esm/agents/agent.d.ts +1 -1
- 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 +307 -5
- package/lib/esm/agents/chat-model.js +113 -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
|
@@ -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
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import { type PromiseOrValue } from "../utils/type-utils.js";
|
|
3
|
-
import { Agent, type AgentInvokeOptions, type AgentOptions, type AgentProcessResult, type Message } from "./agent.js";
|
|
3
|
+
import { Agent, type AgentInvokeOptions, type AgentOptions, type AgentProcessResult, type AgentResponse, type AgentResponseStream, type Message } from "./agent.js";
|
|
4
4
|
export declare class StructuredOutputError extends Error {
|
|
5
5
|
}
|
|
6
6
|
/**
|
|
@@ -104,6 +104,11 @@ 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
|
+
protected processAgentOutput(input: ChatModelInput, output: Exclude<AgentResponse<ChatModelOutput>, AgentResponseStream<ChatModelOutput>>, options: AgentInvokeOptions): Promise<ChatModelOutput>;
|
|
108
|
+
transformFileOutput(input: ChatModelInput, data: FileUnionContent, options: AgentInvokeOptions): Promise<FileUnionContent>;
|
|
109
|
+
static getFileExtension(type: string): string | undefined;
|
|
110
|
+
static getMimeType(filename: string): string | undefined;
|
|
111
|
+
protected downloadFile(url: string): Promise<Response>;
|
|
107
112
|
}
|
|
108
113
|
/**
|
|
109
114
|
* Input message format for ChatModel
|
|
@@ -128,6 +133,7 @@ export interface ChatModelInput extends Message {
|
|
|
128
133
|
* Specifies the expected response format
|
|
129
134
|
*/
|
|
130
135
|
responseFormat?: ChatModelInputResponseFormat;
|
|
136
|
+
fileOutputType?: FileOutputType;
|
|
131
137
|
/**
|
|
132
138
|
* List of tools available for the model to use
|
|
133
139
|
*/
|
|
@@ -190,7 +196,7 @@ export interface ChatModelInputMessage {
|
|
|
190
196
|
*
|
|
191
197
|
* Can be a simple string, or a mixed array of text and image content
|
|
192
198
|
*/
|
|
193
|
-
export type ChatModelInputMessageContent = string |
|
|
199
|
+
export type ChatModelInputMessageContent = string | UnionContent[];
|
|
194
200
|
/**
|
|
195
201
|
* Text content type
|
|
196
202
|
*
|
|
@@ -200,15 +206,304 @@ export type TextContent = {
|
|
|
200
206
|
type: "text";
|
|
201
207
|
text: string;
|
|
202
208
|
};
|
|
209
|
+
export declare const textContentSchema: z.ZodObject<{
|
|
210
|
+
type: z.ZodLiteral<"text">;
|
|
211
|
+
text: z.ZodString;
|
|
212
|
+
}, "strip", z.ZodTypeAny, {
|
|
213
|
+
type: "text";
|
|
214
|
+
text: string;
|
|
215
|
+
}, {
|
|
216
|
+
type: "text";
|
|
217
|
+
text: string;
|
|
218
|
+
}>;
|
|
219
|
+
export interface FileContentBase {
|
|
220
|
+
filename?: string;
|
|
221
|
+
mimeType?: string;
|
|
222
|
+
}
|
|
223
|
+
export declare const fileContentBaseSchema: z.ZodObject<{
|
|
224
|
+
filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
225
|
+
mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
226
|
+
}, "strip", z.ZodTypeAny, {
|
|
227
|
+
filename?: string | undefined;
|
|
228
|
+
mimeType?: string | undefined;
|
|
229
|
+
}, {
|
|
230
|
+
filename?: string | undefined;
|
|
231
|
+
mimeType?: string | undefined;
|
|
232
|
+
}>;
|
|
203
233
|
/**
|
|
204
234
|
* Image URL content type
|
|
205
235
|
*
|
|
206
236
|
* Used for image parts of message content, referencing images via URL
|
|
207
237
|
*/
|
|
208
|
-
export
|
|
209
|
-
type: "
|
|
238
|
+
export interface UrlContent extends FileContentBase {
|
|
239
|
+
type: "url";
|
|
210
240
|
url: string;
|
|
211
|
-
}
|
|
241
|
+
}
|
|
242
|
+
export declare const urlContentSchema: z.ZodObject<{
|
|
243
|
+
filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
244
|
+
mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
245
|
+
} & {
|
|
246
|
+
type: z.ZodLiteral<"url">;
|
|
247
|
+
url: z.ZodString;
|
|
248
|
+
}, "strip", z.ZodTypeAny, {
|
|
249
|
+
type: "url";
|
|
250
|
+
url: string;
|
|
251
|
+
filename?: string | undefined;
|
|
252
|
+
mimeType?: string | undefined;
|
|
253
|
+
}, {
|
|
254
|
+
type: "url";
|
|
255
|
+
url: string;
|
|
256
|
+
filename?: string | undefined;
|
|
257
|
+
mimeType?: string | undefined;
|
|
258
|
+
}>;
|
|
259
|
+
export interface FileContent extends FileContentBase {
|
|
260
|
+
type: "file";
|
|
261
|
+
data: string;
|
|
262
|
+
}
|
|
263
|
+
export declare const fileContentSchema: z.ZodObject<{
|
|
264
|
+
filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
265
|
+
mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
266
|
+
} & {
|
|
267
|
+
type: z.ZodLiteral<"file">;
|
|
268
|
+
data: z.ZodString;
|
|
269
|
+
}, "strip", z.ZodTypeAny, {
|
|
270
|
+
type: "file";
|
|
271
|
+
data: string;
|
|
272
|
+
filename?: string | undefined;
|
|
273
|
+
mimeType?: string | undefined;
|
|
274
|
+
}, {
|
|
275
|
+
type: "file";
|
|
276
|
+
data: string;
|
|
277
|
+
filename?: string | undefined;
|
|
278
|
+
mimeType?: string | undefined;
|
|
279
|
+
}>;
|
|
280
|
+
export interface LocalContent extends FileContentBase {
|
|
281
|
+
type: "local";
|
|
282
|
+
path: string;
|
|
283
|
+
}
|
|
284
|
+
export declare const localContentSchema: z.ZodObject<{
|
|
285
|
+
filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
286
|
+
mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
287
|
+
} & {
|
|
288
|
+
type: z.ZodLiteral<"local">;
|
|
289
|
+
path: z.ZodString;
|
|
290
|
+
}, "strip", z.ZodTypeAny, {
|
|
291
|
+
path: string;
|
|
292
|
+
type: "local";
|
|
293
|
+
filename?: string | undefined;
|
|
294
|
+
mimeType?: string | undefined;
|
|
295
|
+
}, {
|
|
296
|
+
path: string;
|
|
297
|
+
type: "local";
|
|
298
|
+
filename?: string | undefined;
|
|
299
|
+
mimeType?: string | undefined;
|
|
300
|
+
}>;
|
|
301
|
+
export type FileUnionContent = LocalContent | UrlContent | FileContent;
|
|
302
|
+
export declare const fileUnionContentSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObject<{
|
|
303
|
+
filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
304
|
+
mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
305
|
+
} & {
|
|
306
|
+
type: z.ZodLiteral<"local">;
|
|
307
|
+
path: z.ZodString;
|
|
308
|
+
}, "strip", z.ZodTypeAny, {
|
|
309
|
+
path: string;
|
|
310
|
+
type: "local";
|
|
311
|
+
filename?: string | undefined;
|
|
312
|
+
mimeType?: string | undefined;
|
|
313
|
+
}, {
|
|
314
|
+
path: string;
|
|
315
|
+
type: "local";
|
|
316
|
+
filename?: string | undefined;
|
|
317
|
+
mimeType?: string | undefined;
|
|
318
|
+
}>, z.ZodObject<{
|
|
319
|
+
filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
320
|
+
mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
321
|
+
} & {
|
|
322
|
+
type: z.ZodLiteral<"url">;
|
|
323
|
+
url: z.ZodString;
|
|
324
|
+
}, "strip", z.ZodTypeAny, {
|
|
325
|
+
type: "url";
|
|
326
|
+
url: string;
|
|
327
|
+
filename?: string | undefined;
|
|
328
|
+
mimeType?: string | undefined;
|
|
329
|
+
}, {
|
|
330
|
+
type: "url";
|
|
331
|
+
url: string;
|
|
332
|
+
filename?: string | undefined;
|
|
333
|
+
mimeType?: string | undefined;
|
|
334
|
+
}>, z.ZodObject<{
|
|
335
|
+
filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
336
|
+
mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
337
|
+
} & {
|
|
338
|
+
type: z.ZodLiteral<"file">;
|
|
339
|
+
data: z.ZodString;
|
|
340
|
+
}, "strip", z.ZodTypeAny, {
|
|
341
|
+
type: "file";
|
|
342
|
+
data: string;
|
|
343
|
+
filename?: string | undefined;
|
|
344
|
+
mimeType?: string | undefined;
|
|
345
|
+
}, {
|
|
346
|
+
type: "file";
|
|
347
|
+
data: string;
|
|
348
|
+
filename?: string | undefined;
|
|
349
|
+
mimeType?: string | undefined;
|
|
350
|
+
}>]>;
|
|
351
|
+
export declare const fileUnionContentsSchema: z.ZodUnion<[z.ZodDiscriminatedUnion<"type", [z.ZodObject<{
|
|
352
|
+
filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
353
|
+
mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
354
|
+
} & {
|
|
355
|
+
type: z.ZodLiteral<"local">;
|
|
356
|
+
path: z.ZodString;
|
|
357
|
+
}, "strip", z.ZodTypeAny, {
|
|
358
|
+
path: string;
|
|
359
|
+
type: "local";
|
|
360
|
+
filename?: string | undefined;
|
|
361
|
+
mimeType?: string | undefined;
|
|
362
|
+
}, {
|
|
363
|
+
path: string;
|
|
364
|
+
type: "local";
|
|
365
|
+
filename?: string | undefined;
|
|
366
|
+
mimeType?: string | undefined;
|
|
367
|
+
}>, z.ZodObject<{
|
|
368
|
+
filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
369
|
+
mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
370
|
+
} & {
|
|
371
|
+
type: z.ZodLiteral<"url">;
|
|
372
|
+
url: z.ZodString;
|
|
373
|
+
}, "strip", z.ZodTypeAny, {
|
|
374
|
+
type: "url";
|
|
375
|
+
url: string;
|
|
376
|
+
filename?: string | undefined;
|
|
377
|
+
mimeType?: string | undefined;
|
|
378
|
+
}, {
|
|
379
|
+
type: "url";
|
|
380
|
+
url: string;
|
|
381
|
+
filename?: string | undefined;
|
|
382
|
+
mimeType?: string | undefined;
|
|
383
|
+
}>, z.ZodObject<{
|
|
384
|
+
filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
385
|
+
mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
386
|
+
} & {
|
|
387
|
+
type: z.ZodLiteral<"file">;
|
|
388
|
+
data: z.ZodString;
|
|
389
|
+
}, "strip", z.ZodTypeAny, {
|
|
390
|
+
type: "file";
|
|
391
|
+
data: string;
|
|
392
|
+
filename?: string | undefined;
|
|
393
|
+
mimeType?: string | undefined;
|
|
394
|
+
}, {
|
|
395
|
+
type: "file";
|
|
396
|
+
data: string;
|
|
397
|
+
filename?: string | undefined;
|
|
398
|
+
mimeType?: string | undefined;
|
|
399
|
+
}>]>, z.ZodArray<z.ZodDiscriminatedUnion<"type", [z.ZodObject<{
|
|
400
|
+
filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
401
|
+
mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
402
|
+
} & {
|
|
403
|
+
type: z.ZodLiteral<"local">;
|
|
404
|
+
path: z.ZodString;
|
|
405
|
+
}, "strip", z.ZodTypeAny, {
|
|
406
|
+
path: string;
|
|
407
|
+
type: "local";
|
|
408
|
+
filename?: string | undefined;
|
|
409
|
+
mimeType?: string | undefined;
|
|
410
|
+
}, {
|
|
411
|
+
path: string;
|
|
412
|
+
type: "local";
|
|
413
|
+
filename?: string | undefined;
|
|
414
|
+
mimeType?: string | undefined;
|
|
415
|
+
}>, z.ZodObject<{
|
|
416
|
+
filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
417
|
+
mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
418
|
+
} & {
|
|
419
|
+
type: z.ZodLiteral<"url">;
|
|
420
|
+
url: z.ZodString;
|
|
421
|
+
}, "strip", z.ZodTypeAny, {
|
|
422
|
+
type: "url";
|
|
423
|
+
url: string;
|
|
424
|
+
filename?: string | undefined;
|
|
425
|
+
mimeType?: string | undefined;
|
|
426
|
+
}, {
|
|
427
|
+
type: "url";
|
|
428
|
+
url: string;
|
|
429
|
+
filename?: string | undefined;
|
|
430
|
+
mimeType?: string | undefined;
|
|
431
|
+
}>, z.ZodObject<{
|
|
432
|
+
filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
433
|
+
mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
434
|
+
} & {
|
|
435
|
+
type: z.ZodLiteral<"file">;
|
|
436
|
+
data: z.ZodString;
|
|
437
|
+
}, "strip", z.ZodTypeAny, {
|
|
438
|
+
type: "file";
|
|
439
|
+
data: string;
|
|
440
|
+
filename?: string | undefined;
|
|
441
|
+
mimeType?: string | undefined;
|
|
442
|
+
}, {
|
|
443
|
+
type: "file";
|
|
444
|
+
data: string;
|
|
445
|
+
filename?: string | undefined;
|
|
446
|
+
mimeType?: string | undefined;
|
|
447
|
+
}>]>, "many">]>;
|
|
448
|
+
export type UnionContent = TextContent | FileUnionContent;
|
|
449
|
+
export declare const unionContentSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObject<{
|
|
450
|
+
type: z.ZodLiteral<"text">;
|
|
451
|
+
text: z.ZodString;
|
|
452
|
+
}, "strip", z.ZodTypeAny, {
|
|
453
|
+
type: "text";
|
|
454
|
+
text: string;
|
|
455
|
+
}, {
|
|
456
|
+
type: "text";
|
|
457
|
+
text: string;
|
|
458
|
+
}>, z.ZodObject<{
|
|
459
|
+
filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
460
|
+
mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
461
|
+
} & {
|
|
462
|
+
type: z.ZodLiteral<"local">;
|
|
463
|
+
path: z.ZodString;
|
|
464
|
+
}, "strip", z.ZodTypeAny, {
|
|
465
|
+
path: string;
|
|
466
|
+
type: "local";
|
|
467
|
+
filename?: string | undefined;
|
|
468
|
+
mimeType?: string | undefined;
|
|
469
|
+
}, {
|
|
470
|
+
path: string;
|
|
471
|
+
type: "local";
|
|
472
|
+
filename?: string | undefined;
|
|
473
|
+
mimeType?: string | undefined;
|
|
474
|
+
}>, z.ZodObject<{
|
|
475
|
+
filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
476
|
+
mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
477
|
+
} & {
|
|
478
|
+
type: z.ZodLiteral<"url">;
|
|
479
|
+
url: z.ZodString;
|
|
480
|
+
}, "strip", z.ZodTypeAny, {
|
|
481
|
+
type: "url";
|
|
482
|
+
url: string;
|
|
483
|
+
filename?: string | undefined;
|
|
484
|
+
mimeType?: string | undefined;
|
|
485
|
+
}, {
|
|
486
|
+
type: "url";
|
|
487
|
+
url: string;
|
|
488
|
+
filename?: string | undefined;
|
|
489
|
+
mimeType?: string | undefined;
|
|
490
|
+
}>, z.ZodObject<{
|
|
491
|
+
filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
492
|
+
mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
493
|
+
} & {
|
|
494
|
+
type: z.ZodLiteral<"file">;
|
|
495
|
+
data: z.ZodString;
|
|
496
|
+
}, "strip", z.ZodTypeAny, {
|
|
497
|
+
type: "file";
|
|
498
|
+
data: string;
|
|
499
|
+
filename?: string | undefined;
|
|
500
|
+
mimeType?: string | undefined;
|
|
501
|
+
}, {
|
|
502
|
+
type: "file";
|
|
503
|
+
data: string;
|
|
504
|
+
filename?: string | undefined;
|
|
505
|
+
mimeType?: string | undefined;
|
|
506
|
+
}>]>;
|
|
212
507
|
/**
|
|
213
508
|
* Model response format settings
|
|
214
509
|
*
|
|
@@ -277,6 +572,7 @@ export type ChatModelInputToolChoice = "auto" | "none" | "required" | {
|
|
|
277
572
|
description?: string;
|
|
278
573
|
};
|
|
279
574
|
};
|
|
575
|
+
export type Modality = "text" | "image" | "audio";
|
|
280
576
|
/**
|
|
281
577
|
* Model-specific configuration options
|
|
282
578
|
*
|
|
@@ -307,6 +603,7 @@ export interface ChatModelOptions {
|
|
|
307
603
|
* Whether to allow parallel tool calls
|
|
308
604
|
*/
|
|
309
605
|
parallelToolCalls?: boolean;
|
|
606
|
+
modalities?: Modality[];
|
|
310
607
|
}
|
|
311
608
|
/**
|
|
312
609
|
* Output message format for ChatModel
|
|
@@ -342,6 +639,11 @@ export interface ChatModelOutput extends Message {
|
|
|
342
639
|
* Model name or version used
|
|
343
640
|
*/
|
|
344
641
|
model?: string;
|
|
642
|
+
files?: FileUnionContent[];
|
|
643
|
+
}
|
|
644
|
+
export declare enum FileOutputType {
|
|
645
|
+
local = "local",
|
|
646
|
+
file = "file"
|
|
345
647
|
}
|
|
346
648
|
/**
|
|
347
649
|
* 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,110 @@ export class ChatModel extends Agent {
|
|
|
170
174
|
options.context.usage.aigneHubCredits += usage.aigneHubCredits;
|
|
171
175
|
}
|
|
172
176
|
}
|
|
177
|
+
async processAgentOutput(input, output, options) {
|
|
178
|
+
if (output.files) {
|
|
179
|
+
const files = z.array(fileUnionContentSchema).parse(output.files);
|
|
180
|
+
output = {
|
|
181
|
+
...output,
|
|
182
|
+
files: await Promise.all(files.map((file) => this.transformFileOutput(input, file, options))),
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
return super.processAgentOutput(input, output, options);
|
|
186
|
+
}
|
|
187
|
+
async transformFileOutput(input, data, options) {
|
|
188
|
+
const fileOutputType = input.fileOutputType || FileOutputType.local;
|
|
189
|
+
if (fileOutputType === data.type)
|
|
190
|
+
return data;
|
|
191
|
+
const common = pick(data, "filename", "mimeType");
|
|
192
|
+
switch (fileOutputType) {
|
|
193
|
+
case FileOutputType.local: {
|
|
194
|
+
const dir = nodejs.path.join(nodejs.os.tmpdir(), options.context.id);
|
|
195
|
+
await nodejs.fs.mkdir(dir, { recursive: true });
|
|
196
|
+
const ext = ChatModel.getFileExtension(data.mimeType || data.filename || "");
|
|
197
|
+
const id = v7();
|
|
198
|
+
const filename = ext ? `${id}.${ext}` : id;
|
|
199
|
+
const path = nodejs.path.join(dir, filename);
|
|
200
|
+
if (data.type === "file") {
|
|
201
|
+
await nodejs.fs.writeFile(path, data.data, "base64");
|
|
202
|
+
}
|
|
203
|
+
else if (data.type === "url") {
|
|
204
|
+
await this.downloadFile(data.url)
|
|
205
|
+
.then((res) => res.body)
|
|
206
|
+
.then((body) => body && nodejs.fs.writeFile(path, body));
|
|
207
|
+
}
|
|
208
|
+
else {
|
|
209
|
+
throw new Error(`Unexpected file type: ${data.type}`);
|
|
210
|
+
}
|
|
211
|
+
return { ...common, type: "local", path };
|
|
212
|
+
}
|
|
213
|
+
case FileOutputType.file: {
|
|
214
|
+
let base64;
|
|
215
|
+
if (data.type === "local") {
|
|
216
|
+
base64 = await nodejs.fs.readFile(data.path, "base64");
|
|
217
|
+
}
|
|
218
|
+
else if (data.type === "url") {
|
|
219
|
+
base64 = Buffer.from(await (await this.downloadFile(data.url)).arrayBuffer()).toString("base64");
|
|
220
|
+
}
|
|
221
|
+
else {
|
|
222
|
+
throw new Error(`Unexpected file type: ${data.type}`);
|
|
223
|
+
}
|
|
224
|
+
return { ...common, type: "file", data: base64 };
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
static getFileExtension(type) {
|
|
229
|
+
return mime.getExtension(type) || undefined;
|
|
230
|
+
}
|
|
231
|
+
static getMimeType(filename) {
|
|
232
|
+
return mime.getType(filename) || undefined;
|
|
233
|
+
}
|
|
234
|
+
async downloadFile(url) {
|
|
235
|
+
const response = await fetch(url);
|
|
236
|
+
if (!response.ok) {
|
|
237
|
+
const text = await response.text().catch(() => null);
|
|
238
|
+
throw new Error(`Failed to download content from ${url}, ${response.status} ${response.statusText} ${text}`);
|
|
239
|
+
}
|
|
240
|
+
return response;
|
|
241
|
+
}
|
|
173
242
|
}
|
|
243
|
+
export const textContentSchema = z.object({
|
|
244
|
+
type: z.literal("text"),
|
|
245
|
+
text: z.string(),
|
|
246
|
+
});
|
|
247
|
+
export const fileContentBaseSchema = z.object({
|
|
248
|
+
filename: optionalize(z.string()),
|
|
249
|
+
mimeType: optionalize(z.string()),
|
|
250
|
+
});
|
|
251
|
+
export const urlContentSchema = fileContentBaseSchema.extend({
|
|
252
|
+
type: z.literal("url"),
|
|
253
|
+
url: z.string(),
|
|
254
|
+
});
|
|
255
|
+
export const fileContentSchema = fileContentBaseSchema.extend({
|
|
256
|
+
type: z.literal("file"),
|
|
257
|
+
data: z.string(),
|
|
258
|
+
});
|
|
259
|
+
export const localContentSchema = fileContentBaseSchema.extend({
|
|
260
|
+
type: z.literal("local"),
|
|
261
|
+
path: z.string(),
|
|
262
|
+
});
|
|
263
|
+
export const fileUnionContentSchema = z.discriminatedUnion("type", [
|
|
264
|
+
localContentSchema,
|
|
265
|
+
urlContentSchema,
|
|
266
|
+
fileContentSchema,
|
|
267
|
+
]);
|
|
268
|
+
export const fileUnionContentsSchema = z.union([
|
|
269
|
+
fileUnionContentSchema,
|
|
270
|
+
z.array(fileUnionContentSchema),
|
|
271
|
+
]);
|
|
272
|
+
export const unionContentSchema = z.discriminatedUnion("type", [
|
|
273
|
+
textContentSchema,
|
|
274
|
+
localContentSchema,
|
|
275
|
+
urlContentSchema,
|
|
276
|
+
fileContentSchema,
|
|
277
|
+
]);
|
|
174
278
|
const chatModelInputMessageSchema = z.object({
|
|
175
279
|
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(),
|
|
280
|
+
content: z.union([z.string(), z.array(unionContentSchema)]).optional(),
|
|
185
281
|
toolCalls: z
|
|
186
282
|
.array(z.object({
|
|
187
283
|
id: z.string(),
|
|
@@ -228,6 +324,7 @@ const chatModelOptionsSchema = z.object({
|
|
|
228
324
|
frequencyPenalty: z.number().optional(),
|
|
229
325
|
presencePenalty: z.number().optional(),
|
|
230
326
|
parallelToolCalls: z.boolean().optional().default(true),
|
|
327
|
+
modalities: z.array(z.enum(["text", "image", "audio"])).optional(),
|
|
231
328
|
});
|
|
232
329
|
const chatModelInputSchema = z.object({
|
|
233
330
|
messages: z.array(chatModelInputMessageSchema),
|
|
@@ -236,6 +333,11 @@ const chatModelInputSchema = z.object({
|
|
|
236
333
|
toolChoice: chatModelInputToolChoiceSchema.optional(),
|
|
237
334
|
modelOptions: chatModelOptionsSchema.optional(),
|
|
238
335
|
});
|
|
336
|
+
export var FileOutputType;
|
|
337
|
+
(function (FileOutputType) {
|
|
338
|
+
FileOutputType["local"] = "local";
|
|
339
|
+
FileOutputType["file"] = "file";
|
|
340
|
+
})(FileOutputType || (FileOutputType = {}));
|
|
239
341
|
const chatModelOutputToolCallSchema = z.object({
|
|
240
342
|
id: z.string(),
|
|
241
343
|
type: z.literal("function"),
|
|
@@ -255,4 +357,5 @@ const chatModelOutputSchema = z.object({
|
|
|
255
357
|
toolCalls: z.array(chatModelOutputToolCallSchema).optional(),
|
|
256
358
|
usage: chatModelOutputUsageSchema.optional(),
|
|
257
359
|
model: z.string().optional(),
|
|
360
|
+
files: z.array(fileUnionContentSchema).optional(),
|
|
258
361
|
});
|
|
@@ -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;
|