@aigne/core 1.60.3 → 1.61.0-beta

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.
Files changed (45) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/lib/cjs/agents/ai-agent.d.ts +2 -1
  3. package/lib/cjs/agents/chat-model.d.ts +9 -245
  4. package/lib/cjs/agents/chat-model.js +9 -99
  5. package/lib/cjs/agents/image-agent.d.ts +3 -0
  6. package/lib/cjs/agents/image-agent.js +3 -1
  7. package/lib/cjs/agents/image-model.d.ts +157 -26
  8. package/lib/cjs/agents/image-model.js +28 -28
  9. package/lib/cjs/agents/model.d.ts +238 -0
  10. package/lib/cjs/agents/model.js +107 -0
  11. package/lib/cjs/index.d.ts +1 -0
  12. package/lib/cjs/index.js +1 -0
  13. package/lib/cjs/loader/agent-js.d.ts +1 -2
  14. package/lib/cjs/loader/agent-js.js +4 -4
  15. package/lib/cjs/loader/index.d.ts +0 -1
  16. package/lib/cjs/loader/index.js +1 -2
  17. package/lib/cjs/prompt/prompt-builder.d.ts +1 -1
  18. package/lib/cjs/prompt/prompt-builder.js +4 -4
  19. package/lib/dts/agents/ai-agent.d.ts +2 -1
  20. package/lib/dts/agents/chat-model.d.ts +9 -245
  21. package/lib/dts/agents/image-agent.d.ts +3 -0
  22. package/lib/dts/agents/image-model.d.ts +157 -26
  23. package/lib/dts/agents/model.d.ts +238 -0
  24. package/lib/dts/index.d.ts +1 -0
  25. package/lib/dts/loader/agent-js.d.ts +1 -2
  26. package/lib/dts/loader/index.d.ts +0 -1
  27. package/lib/dts/prompt/prompt-builder.d.ts +1 -1
  28. package/lib/esm/agents/ai-agent.d.ts +2 -1
  29. package/lib/esm/agents/chat-model.d.ts +9 -245
  30. package/lib/esm/agents/chat-model.js +5 -92
  31. package/lib/esm/agents/image-agent.d.ts +3 -0
  32. package/lib/esm/agents/image-agent.js +3 -1
  33. package/lib/esm/agents/image-model.d.ts +157 -26
  34. package/lib/esm/agents/image-model.js +28 -28
  35. package/lib/esm/agents/model.d.ts +238 -0
  36. package/lib/esm/agents/model.js +100 -0
  37. package/lib/esm/index.d.ts +1 -0
  38. package/lib/esm/index.js +1 -0
  39. package/lib/esm/loader/agent-js.d.ts +1 -2
  40. package/lib/esm/loader/agent-js.js +4 -4
  41. package/lib/esm/loader/index.d.ts +0 -1
  42. package/lib/esm/loader/index.js +1 -2
  43. package/lib/esm/prompt/prompt-builder.d.ts +1 -1
  44. package/lib/esm/prompt/prompt-builder.js +1 -1
  45. package/package.json +2 -2
@@ -1,10 +1,11 @@
1
- import { z } from "zod";
1
+ import { type ZodType, 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 type { AgentInvokeOptions, AgentOptions, AgentProcessResult, AgentResponse, AgentResponseStream, Message } from "./agent.js";
4
4
  import { type ChatModelOutputUsage } from "./chat-model.js";
5
+ import { FileOutputType, type FileUnionContent, Model } from "./model.js";
5
6
  export interface ImageModelOptions<I extends ImageModelInput = ImageModelInput, O extends ImageModelOutput = ImageModelOutput> extends Omit<AgentOptions<I, O>, "model"> {
6
7
  }
7
- export declare abstract class ImageModel<I extends ImageModelInput = ImageModelInput, O extends ImageModelOutput = ImageModelOutput> extends Agent<I, O> {
8
+ export declare abstract class ImageModel<I extends ImageModelInput = ImageModelInput, O extends ImageModelOutput = ImageModelOutput> extends Model<I, O> {
8
9
  tag: string;
9
10
  constructor(options?: ImageModelOptions<I, O>);
10
11
  get credential(): PromiseOrValue<{
@@ -15,36 +16,115 @@ export declare abstract class ImageModel<I extends ImageModelInput = ImageModelI
15
16
  protected preprocess(input: I, options: AgentInvokeOptions): Promise<void>;
16
17
  protected postprocess(input: I, output: O, options: AgentInvokeOptions): Promise<void>;
17
18
  abstract process(input: I, options: AgentInvokeOptions): PromiseOrValue<AgentProcessResult<O>>;
18
- protected downloadFile(url: string, timeout?: number): Promise<Response>;
19
+ protected processAgentOutput(input: I, output: Exclude<AgentResponse<O>, AgentResponseStream<O>>, options: AgentInvokeOptions): Promise<O>;
19
20
  }
21
+ export type ImageModelInputImage = FileUnionContent[];
20
22
  export interface ImageModelInput extends Message {
21
23
  model?: string;
22
- image?: string | string[];
24
+ image?: ImageModelInputImage;
23
25
  prompt: string;
24
26
  n?: number;
25
- responseFormat?: "url" | "base64";
27
+ outputType?: FileOutputType;
26
28
  }
27
29
  export declare const imageModelInputSchema: z.ZodObject<{
28
30
  model: z.ZodOptional<z.ZodString>;
29
- image: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString, "many">]>>;
31
+ image: z.ZodOptional<z.ZodArray<z.ZodDiscriminatedUnion<"type", [z.ZodObject<{
32
+ filename: ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
33
+ mimeType: ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
34
+ } & {
35
+ type: z.ZodLiteral<"local">;
36
+ path: z.ZodString;
37
+ }, "strip", z.ZodTypeAny, {
38
+ path: string;
39
+ type: "local";
40
+ filename?: string | undefined;
41
+ mimeType?: string | undefined;
42
+ }, {
43
+ path: string;
44
+ type: "local";
45
+ filename?: string | undefined;
46
+ mimeType?: string | undefined;
47
+ }>, z.ZodObject<{
48
+ filename: ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
49
+ mimeType: ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
50
+ } & {
51
+ type: z.ZodLiteral<"url">;
52
+ url: z.ZodString;
53
+ }, "strip", z.ZodTypeAny, {
54
+ type: "url";
55
+ url: string;
56
+ filename?: string | undefined;
57
+ mimeType?: string | undefined;
58
+ }, {
59
+ type: "url";
60
+ url: string;
61
+ filename?: string | undefined;
62
+ mimeType?: string | undefined;
63
+ }>, z.ZodObject<{
64
+ filename: ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
65
+ mimeType: ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
66
+ } & {
67
+ type: z.ZodLiteral<"file">;
68
+ data: z.ZodString;
69
+ }, "strip", z.ZodTypeAny, {
70
+ type: "file";
71
+ data: string;
72
+ filename?: string | undefined;
73
+ mimeType?: string | undefined;
74
+ }, {
75
+ type: "file";
76
+ data: string;
77
+ filename?: string | undefined;
78
+ mimeType?: string | undefined;
79
+ }>]>, "many">>;
30
80
  prompt: z.ZodString;
31
81
  n: z.ZodOptional<z.ZodNumber>;
32
- responseFormat: z.ZodOptional<z.ZodEnum<["url", "base64"]>>;
82
+ outputType: z.ZodOptional<z.ZodNativeEnum<typeof FileOutputType>>;
33
83
  }, "strip", z.ZodTypeAny, {
34
84
  prompt: string;
35
85
  model?: string | undefined;
36
- image?: string | string[] | undefined;
37
- responseFormat?: "base64" | "url" | undefined;
86
+ image?: ({
87
+ type: "url";
88
+ url: string;
89
+ filename?: string | undefined;
90
+ mimeType?: string | undefined;
91
+ } | {
92
+ type: "file";
93
+ data: string;
94
+ filename?: string | undefined;
95
+ mimeType?: string | undefined;
96
+ } | {
97
+ path: string;
98
+ type: "local";
99
+ filename?: string | undefined;
100
+ mimeType?: string | undefined;
101
+ })[] | undefined;
38
102
  n?: number | undefined;
103
+ outputType?: FileOutputType | undefined;
39
104
  }, {
40
105
  prompt: string;
41
106
  model?: string | undefined;
42
- image?: string | string[] | undefined;
43
- responseFormat?: "base64" | "url" | undefined;
107
+ image?: ({
108
+ type: "url";
109
+ url: string;
110
+ filename?: string | undefined;
111
+ mimeType?: string | undefined;
112
+ } | {
113
+ type: "file";
114
+ data: string;
115
+ filename?: string | undefined;
116
+ mimeType?: string | undefined;
117
+ } | {
118
+ path: string;
119
+ type: "local";
120
+ filename?: string | undefined;
121
+ mimeType?: string | undefined;
122
+ })[] | undefined;
44
123
  n?: number | undefined;
124
+ outputType?: FileOutputType | undefined;
45
125
  }>;
46
126
  export interface ImageModelOutput extends Message {
47
- images: ImageModelOutputImage[];
127
+ images: FileUnionContent[];
48
128
  /**
49
129
  * Token usage statistics
50
130
  */
@@ -54,26 +134,55 @@ export interface ImageModelOutput extends Message {
54
134
  */
55
135
  model?: string;
56
136
  }
57
- export type ImageModelOutputImage = ImageModelOutputImageUrl | ImageModelOutputImageBase64;
58
- export interface ImageModelOutputImageUrl {
59
- url: string;
60
- }
61
- export interface ImageModelOutputImageBase64 {
62
- base64: string;
63
- }
64
137
  export declare const imageModelOutputSchema: z.ZodObject<{
65
- images: z.ZodArray<z.ZodUnion<[z.ZodObject<{
138
+ images: z.ZodArray<z.ZodDiscriminatedUnion<"type", [z.ZodObject<{
139
+ filename: ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
140
+ mimeType: ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
141
+ } & {
142
+ type: z.ZodLiteral<"local">;
143
+ path: z.ZodString;
144
+ }, "strip", z.ZodTypeAny, {
145
+ path: string;
146
+ type: "local";
147
+ filename?: string | undefined;
148
+ mimeType?: string | undefined;
149
+ }, {
150
+ path: string;
151
+ type: "local";
152
+ filename?: string | undefined;
153
+ mimeType?: string | undefined;
154
+ }>, z.ZodObject<{
155
+ filename: ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
156
+ mimeType: ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
157
+ } & {
158
+ type: z.ZodLiteral<"url">;
66
159
  url: z.ZodString;
67
160
  }, "strip", z.ZodTypeAny, {
161
+ type: "url";
68
162
  url: string;
163
+ filename?: string | undefined;
164
+ mimeType?: string | undefined;
69
165
  }, {
166
+ type: "url";
70
167
  url: string;
168
+ filename?: string | undefined;
169
+ mimeType?: string | undefined;
71
170
  }>, z.ZodObject<{
72
- base64: z.ZodString;
171
+ filename: ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
172
+ mimeType: ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
173
+ } & {
174
+ type: z.ZodLiteral<"file">;
175
+ data: z.ZodString;
73
176
  }, "strip", z.ZodTypeAny, {
74
- base64: string;
177
+ type: "file";
178
+ data: string;
179
+ filename?: string | undefined;
180
+ mimeType?: string | undefined;
75
181
  }, {
76
- base64: string;
182
+ type: "file";
183
+ data: string;
184
+ filename?: string | undefined;
185
+ mimeType?: string | undefined;
77
186
  }>]>, "many">;
78
187
  usage: z.ZodOptional<z.ZodObject<{
79
188
  inputTokens: z.ZodNumber;
@@ -91,9 +200,20 @@ export declare const imageModelOutputSchema: z.ZodObject<{
91
200
  model: z.ZodOptional<z.ZodString>;
92
201
  }, "strip", z.ZodTypeAny, {
93
202
  images: ({
203
+ type: "url";
94
204
  url: string;
205
+ filename?: string | undefined;
206
+ mimeType?: string | undefined;
207
+ } | {
208
+ type: "file";
209
+ data: string;
210
+ filename?: string | undefined;
211
+ mimeType?: string | undefined;
95
212
  } | {
96
- base64: string;
213
+ path: string;
214
+ type: "local";
215
+ filename?: string | undefined;
216
+ mimeType?: string | undefined;
97
217
  })[];
98
218
  model?: string | undefined;
99
219
  usage?: {
@@ -103,9 +223,20 @@ export declare const imageModelOutputSchema: z.ZodObject<{
103
223
  } | undefined;
104
224
  }, {
105
225
  images: ({
226
+ type: "url";
106
227
  url: string;
228
+ filename?: string | undefined;
229
+ mimeType?: string | undefined;
230
+ } | {
231
+ type: "file";
232
+ data: string;
233
+ filename?: string | undefined;
234
+ mimeType?: string | undefined;
107
235
  } | {
108
- base64: string;
236
+ path: string;
237
+ type: "local";
238
+ filename?: string | undefined;
239
+ mimeType?: string | undefined;
109
240
  })[];
110
241
  model?: string | undefined;
111
242
  usage?: {
@@ -1,7 +1,8 @@
1
+ import { nodejs } from "@aigne/platform-helpers/nodejs/index.js";
1
2
  import { z } from "zod";
2
- import { Agent, } from "./agent.js";
3
3
  import { chatModelOutputUsageSchema } from "./chat-model.js";
4
- export class ImageModel extends Agent {
4
+ import { FileOutputType, fileUnionContentSchema, Model } from "./model.js";
5
+ export class ImageModel extends Model {
5
6
  tag = "ImageModelAgent";
6
7
  constructor(options) {
7
8
  super({
@@ -20,6 +21,20 @@ export class ImageModel extends Agent {
20
21
  if (limits?.maxTokens && usedTokens >= limits.maxTokens) {
21
22
  throw new Error(`Exceeded max tokens ${usedTokens}/${limits.maxTokens}`);
22
23
  }
24
+ if (input.image) {
25
+ input.image = await Promise.all(input.image.map(async (item) => {
26
+ if (item.type === "local") {
27
+ return {
28
+ ...item,
29
+ type: "file",
30
+ data: await nodejs.fs.readFile(item.path, "base64"),
31
+ path: undefined,
32
+ mimeType: item.mimeType || ImageModel.getMimeType(item.filename || item.path),
33
+ };
34
+ }
35
+ return item;
36
+ }));
37
+ }
23
38
  }
24
39
  async postprocess(input, output, options) {
25
40
  super.postprocess(input, output, options);
@@ -31,41 +46,26 @@ export class ImageModel extends Agent {
31
46
  options.context.usage.aigneHubCredits += usage.aigneHubCredits;
32
47
  }
33
48
  }
34
- async downloadFile(url, timeout = 30000) {
35
- const controller = new AbortController();
36
- const timeoutId = setTimeout(() => controller.abort(), timeout);
37
- try {
38
- const response = await fetch(url, { signal: controller.signal });
39
- if (!response.ok) {
40
- const text = await response.text().catch(() => null);
41
- throw new Error(`Failed to download content from ${url}, ${response.status} ${response.statusText} ${text}`);
42
- }
43
- return response;
44
- }
45
- finally {
46
- clearTimeout(timeoutId);
49
+ async processAgentOutput(input, output, options) {
50
+ if (output.images) {
51
+ const images = z.array(fileUnionContentSchema).parse(output.images);
52
+ output = {
53
+ ...output,
54
+ images: await Promise.all(images.map((image) => this.transformFileOutput(input.outputType, image, options))),
55
+ };
47
56
  }
57
+ return super.processAgentOutput(input, output, options);
48
58
  }
49
59
  }
50
60
  export const imageModelInputSchema = z.object({
51
61
  model: z.string().optional(),
52
- image: z
53
- .union([z.string(), z.array(z.string())])
54
- .optional()
55
- .describe("Image URL or base64 string(s) used for editing"),
62
+ image: z.array(fileUnionContentSchema).optional().describe("Images used for editing"),
56
63
  prompt: z.string(),
57
64
  n: z.number().int().min(1).optional(),
58
- responseFormat: z.enum(["url", "base64"]).optional(),
65
+ outputType: z.nativeEnum(FileOutputType).optional(),
59
66
  });
60
67
  export const imageModelOutputSchema = z.object({
61
- images: z.array(z.union([
62
- z.object({
63
- url: z.string(),
64
- }),
65
- z.object({
66
- base64: z.string(),
67
- }),
68
- ])),
68
+ images: z.array(fileUnionContentSchema),
69
69
  usage: chatModelOutputUsageSchema.optional(),
70
70
  model: z.string().optional(),
71
71
  });
@@ -0,0 +1,238 @@
1
+ import { z } from "zod";
2
+ import { Agent, type AgentInvokeOptions, type Message } from "./agent.js";
3
+ export declare abstract class Model<I extends Message = any, O extends Message = any> extends Agent<I, O> {
4
+ transformFileOutput(fileOutputType: FileOutputType.file, data: FileUnionContent, options: AgentInvokeOptions): Promise<FileContent>;
5
+ transformFileOutput(fileOutputType: FileOutputType.local | undefined, data: FileUnionContent, options: AgentInvokeOptions): Promise<LocalContent>;
6
+ transformFileOutput(fileOutputType: FileOutputType | undefined, data: FileUnionContent, options: AgentInvokeOptions): Promise<FileUnionContent>;
7
+ static getFileExtension(type: string): string | undefined;
8
+ static getMimeType(filename: string): string | undefined;
9
+ downloadFile(url: string): Promise<Response>;
10
+ }
11
+ export declare enum FileOutputType {
12
+ local = "local",
13
+ file = "file"
14
+ }
15
+ export interface FileContentBase {
16
+ filename?: string;
17
+ mimeType?: string;
18
+ }
19
+ export declare const fileContentBaseSchema: z.ZodObject<{
20
+ filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
21
+ mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
22
+ }, "strip", z.ZodTypeAny, {
23
+ filename?: string | undefined;
24
+ mimeType?: string | undefined;
25
+ }, {
26
+ filename?: string | undefined;
27
+ mimeType?: string | undefined;
28
+ }>;
29
+ export interface UrlContent extends FileContentBase {
30
+ type: "url";
31
+ url: string;
32
+ }
33
+ export declare const urlContentSchema: z.ZodObject<{
34
+ filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
35
+ mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
36
+ } & {
37
+ type: z.ZodLiteral<"url">;
38
+ url: z.ZodString;
39
+ }, "strip", z.ZodTypeAny, {
40
+ type: "url";
41
+ url: string;
42
+ filename?: string | undefined;
43
+ mimeType?: string | undefined;
44
+ }, {
45
+ type: "url";
46
+ url: string;
47
+ filename?: string | undefined;
48
+ mimeType?: string | undefined;
49
+ }>;
50
+ export interface FileContent extends FileContentBase {
51
+ type: "file";
52
+ data: string;
53
+ }
54
+ export declare const fileContentSchema: z.ZodObject<{
55
+ filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
56
+ mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
57
+ } & {
58
+ type: z.ZodLiteral<"file">;
59
+ data: z.ZodString;
60
+ }, "strip", z.ZodTypeAny, {
61
+ type: "file";
62
+ data: string;
63
+ filename?: string | undefined;
64
+ mimeType?: string | undefined;
65
+ }, {
66
+ type: "file";
67
+ data: string;
68
+ filename?: string | undefined;
69
+ mimeType?: string | undefined;
70
+ }>;
71
+ export interface LocalContent extends FileContentBase {
72
+ type: "local";
73
+ path: string;
74
+ }
75
+ export declare const localContentSchema: z.ZodObject<{
76
+ filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
77
+ mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
78
+ } & {
79
+ type: z.ZodLiteral<"local">;
80
+ path: z.ZodString;
81
+ }, "strip", z.ZodTypeAny, {
82
+ path: string;
83
+ type: "local";
84
+ filename?: string | undefined;
85
+ mimeType?: string | undefined;
86
+ }, {
87
+ path: string;
88
+ type: "local";
89
+ filename?: string | undefined;
90
+ mimeType?: string | undefined;
91
+ }>;
92
+ export type FileUnionContent = LocalContent | UrlContent | FileContent;
93
+ export declare const fileUnionContentSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObject<{
94
+ filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
95
+ mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
96
+ } & {
97
+ type: z.ZodLiteral<"local">;
98
+ path: z.ZodString;
99
+ }, "strip", z.ZodTypeAny, {
100
+ path: string;
101
+ type: "local";
102
+ filename?: string | undefined;
103
+ mimeType?: string | undefined;
104
+ }, {
105
+ path: string;
106
+ type: "local";
107
+ filename?: string | undefined;
108
+ mimeType?: string | undefined;
109
+ }>, z.ZodObject<{
110
+ filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
111
+ mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
112
+ } & {
113
+ type: z.ZodLiteral<"url">;
114
+ url: z.ZodString;
115
+ }, "strip", z.ZodTypeAny, {
116
+ type: "url";
117
+ url: string;
118
+ filename?: string | undefined;
119
+ mimeType?: string | undefined;
120
+ }, {
121
+ type: "url";
122
+ url: string;
123
+ filename?: string | undefined;
124
+ mimeType?: string | undefined;
125
+ }>, z.ZodObject<{
126
+ filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
127
+ mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
128
+ } & {
129
+ type: z.ZodLiteral<"file">;
130
+ data: z.ZodString;
131
+ }, "strip", z.ZodTypeAny, {
132
+ type: "file";
133
+ data: string;
134
+ filename?: string | undefined;
135
+ mimeType?: string | undefined;
136
+ }, {
137
+ type: "file";
138
+ data: string;
139
+ filename?: string | undefined;
140
+ mimeType?: string | undefined;
141
+ }>]>;
142
+ export declare const fileUnionContentsSchema: z.ZodUnion<[z.ZodDiscriminatedUnion<"type", [z.ZodObject<{
143
+ filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
144
+ mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
145
+ } & {
146
+ type: z.ZodLiteral<"local">;
147
+ path: z.ZodString;
148
+ }, "strip", z.ZodTypeAny, {
149
+ path: string;
150
+ type: "local";
151
+ filename?: string | undefined;
152
+ mimeType?: string | undefined;
153
+ }, {
154
+ path: string;
155
+ type: "local";
156
+ filename?: string | undefined;
157
+ mimeType?: string | undefined;
158
+ }>, z.ZodObject<{
159
+ filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
160
+ mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
161
+ } & {
162
+ type: z.ZodLiteral<"url">;
163
+ url: z.ZodString;
164
+ }, "strip", z.ZodTypeAny, {
165
+ type: "url";
166
+ url: string;
167
+ filename?: string | undefined;
168
+ mimeType?: string | undefined;
169
+ }, {
170
+ type: "url";
171
+ url: string;
172
+ filename?: string | undefined;
173
+ mimeType?: string | undefined;
174
+ }>, z.ZodObject<{
175
+ filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
176
+ mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
177
+ } & {
178
+ type: z.ZodLiteral<"file">;
179
+ data: z.ZodString;
180
+ }, "strip", z.ZodTypeAny, {
181
+ type: "file";
182
+ data: string;
183
+ filename?: string | undefined;
184
+ mimeType?: string | undefined;
185
+ }, {
186
+ type: "file";
187
+ data: string;
188
+ filename?: string | undefined;
189
+ mimeType?: string | undefined;
190
+ }>]>, z.ZodArray<z.ZodDiscriminatedUnion<"type", [z.ZodObject<{
191
+ filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
192
+ mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
193
+ } & {
194
+ type: z.ZodLiteral<"local">;
195
+ path: z.ZodString;
196
+ }, "strip", z.ZodTypeAny, {
197
+ path: string;
198
+ type: "local";
199
+ filename?: string | undefined;
200
+ mimeType?: string | undefined;
201
+ }, {
202
+ path: string;
203
+ type: "local";
204
+ filename?: string | undefined;
205
+ mimeType?: string | undefined;
206
+ }>, z.ZodObject<{
207
+ filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
208
+ mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
209
+ } & {
210
+ type: z.ZodLiteral<"url">;
211
+ url: z.ZodString;
212
+ }, "strip", z.ZodTypeAny, {
213
+ type: "url";
214
+ url: string;
215
+ filename?: string | undefined;
216
+ mimeType?: string | undefined;
217
+ }, {
218
+ type: "url";
219
+ url: string;
220
+ filename?: string | undefined;
221
+ mimeType?: string | undefined;
222
+ }>, z.ZodObject<{
223
+ filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
224
+ mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
225
+ } & {
226
+ type: z.ZodLiteral<"file">;
227
+ data: z.ZodString;
228
+ }, "strip", z.ZodTypeAny, {
229
+ type: "file";
230
+ data: string;
231
+ filename?: string | undefined;
232
+ mimeType?: string | undefined;
233
+ }, {
234
+ type: "file";
235
+ data: string;
236
+ filename?: string | undefined;
237
+ mimeType?: string | undefined;
238
+ }>]>, "many">]>;
@@ -0,0 +1,100 @@
1
+ import { nodejs } from "@aigne/platform-helpers/nodejs/index.js";
2
+ import mime from "mime";
3
+ import { parseURL } from "ufo";
4
+ import { v7 } from "uuid";
5
+ import { z } from "zod";
6
+ import { optionalize } from "../loader/schema.js";
7
+ import { pick } from "../utils/type-utils.js";
8
+ import { Agent } from "./agent.js";
9
+ export class Model extends Agent {
10
+ async transformFileOutput(fileOutputType = FileOutputType.local, data, options) {
11
+ if (fileOutputType === data.type)
12
+ return data;
13
+ const common = pick(data, "filename", "mimeType");
14
+ switch (fileOutputType) {
15
+ case FileOutputType.local: {
16
+ const dir = nodejs.path.join(nodejs.os.tmpdir(), options.context.id);
17
+ await nodejs.fs.mkdir(dir, { recursive: true });
18
+ const ext = Model.getFileExtension(data.mimeType || data.filename || "");
19
+ const id = v7();
20
+ const filename = ext ? `${id}.${ext}` : id;
21
+ const path = nodejs.path.join(dir, filename);
22
+ let mimeType = data.mimeType;
23
+ if (data.type === "file") {
24
+ await nodejs.fs.writeFile(path, data.data, "base64");
25
+ mimeType ||= Model.getMimeType(data.filename || "");
26
+ }
27
+ else if (data.type === "url") {
28
+ await this.downloadFile(data.url)
29
+ .then((res) => res.body)
30
+ .then((body) => body && nodejs.fs.writeFile(path, body));
31
+ mimeType ||= Model.getMimeType(data.filename || parseURL(data.url).pathname);
32
+ }
33
+ else {
34
+ throw new Error(`Unexpected file type: ${data.type}`);
35
+ }
36
+ return { ...common, type: "local", path, mimeType };
37
+ }
38
+ case FileOutputType.file: {
39
+ let base64;
40
+ let mimeType = data.mimeType;
41
+ if (data.type === "local") {
42
+ base64 = await nodejs.fs.readFile(data.path, "base64");
43
+ mimeType ||= Model.getMimeType(data.filename || data.path);
44
+ }
45
+ else if (data.type === "url") {
46
+ base64 = Buffer.from(await (await this.downloadFile(data.url)).arrayBuffer()).toString("base64");
47
+ mimeType ||= Model.getMimeType(data.filename || parseURL(data.url).pathname);
48
+ }
49
+ else {
50
+ throw new Error(`Unexpected file type: ${data.type}`);
51
+ }
52
+ return { ...common, type: "file", data: base64, mimeType };
53
+ }
54
+ }
55
+ }
56
+ static getFileExtension(type) {
57
+ return mime.getExtension(type) || undefined;
58
+ }
59
+ static getMimeType(filename) {
60
+ return mime.getType(filename) || undefined;
61
+ }
62
+ async downloadFile(url) {
63
+ const response = await fetch(url);
64
+ if (!response.ok) {
65
+ const text = await response.text().catch(() => null);
66
+ throw new Error(`Failed to download content from ${url}, ${response.status} ${response.statusText} ${text}`);
67
+ }
68
+ return response;
69
+ }
70
+ }
71
+ export var FileOutputType;
72
+ (function (FileOutputType) {
73
+ FileOutputType["local"] = "local";
74
+ FileOutputType["file"] = "file";
75
+ })(FileOutputType || (FileOutputType = {}));
76
+ export const fileContentBaseSchema = z.object({
77
+ filename: optionalize(z.string()),
78
+ mimeType: optionalize(z.string()),
79
+ });
80
+ export const urlContentSchema = fileContentBaseSchema.extend({
81
+ type: z.literal("url"),
82
+ url: z.string(),
83
+ });
84
+ export const fileContentSchema = fileContentBaseSchema.extend({
85
+ type: z.literal("file"),
86
+ data: z.string(),
87
+ });
88
+ export const localContentSchema = fileContentBaseSchema.extend({
89
+ type: z.literal("local"),
90
+ path: z.string(),
91
+ });
92
+ export const fileUnionContentSchema = z.discriminatedUnion("type", [
93
+ localContentSchema,
94
+ urlContentSchema,
95
+ fileContentSchema,
96
+ ]);
97
+ export const fileUnionContentsSchema = z.union([
98
+ fileUnionContentSchema,
99
+ z.array(fileUnionContentSchema),
100
+ ]);
@@ -5,6 +5,7 @@ export * from "./agents/guide-rail-agent.js";
5
5
  export * from "./agents/image-agent.js";
6
6
  export * from "./agents/image-model.js";
7
7
  export * from "./agents/mcp-agent.js";
8
+ export * from "./agents/model.js";
8
9
  export * from "./agents/team-agent.js";
9
10
  export * from "./agents/transform-agent.js";
10
11
  export * from "./agents/types.js";
package/lib/esm/index.js CHANGED
@@ -5,6 +5,7 @@ export * from "./agents/guide-rail-agent.js";
5
5
  export * from "./agents/image-agent.js";
6
6
  export * from "./agents/image-model.js";
7
7
  export * from "./agents/mcp-agent.js";
8
+ export * from "./agents/model.js";
8
9
  export * from "./agents/team-agent.js";
9
10
  export * from "./agents/transform-agent.js";
10
11
  export * from "./agents/types.js";