@aigne/core 1.51.0 → 1.53.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.
Files changed (60) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/lib/cjs/agents/agent.d.ts +2 -0
  3. package/lib/cjs/agents/agent.js +8 -1
  4. package/lib/cjs/agents/chat-model.d.ts +17 -3
  5. package/lib/cjs/agents/chat-model.js +8 -4
  6. package/lib/cjs/agents/image-agent.d.ts +21 -0
  7. package/lib/cjs/agents/image-agent.js +43 -0
  8. package/lib/cjs/agents/image-model.d.ts +115 -0
  9. package/lib/cjs/agents/image-model.js +60 -0
  10. package/lib/cjs/aigne/aigne.d.ts +10 -1
  11. package/lib/cjs/aigne/aigne.js +10 -6
  12. package/lib/cjs/aigne/context.d.ts +5 -1
  13. package/lib/cjs/aigne/context.js +6 -0
  14. package/lib/cjs/index.d.ts +2 -0
  15. package/lib/cjs/index.js +2 -0
  16. package/lib/cjs/loader/agent-yaml.d.ts +6 -1
  17. package/lib/cjs/loader/agent-yaml.js +18 -9
  18. package/lib/cjs/loader/index.d.ts +46 -44
  19. package/lib/cjs/loader/index.js +35 -12
  20. package/lib/cjs/prompt/prompt-builder.d.ts +3 -0
  21. package/lib/cjs/prompt/prompt-builder.js +8 -0
  22. package/lib/cjs/utils/camelize.d.ts +7 -1
  23. package/lib/cjs/utils/camelize.js +30 -13
  24. package/lib/cjs/utils/type-utils.d.ts +1 -1
  25. package/lib/dts/agents/agent.d.ts +2 -0
  26. package/lib/dts/agents/chat-model.d.ts +17 -3
  27. package/lib/dts/agents/image-agent.d.ts +21 -0
  28. package/lib/dts/agents/image-model.d.ts +115 -0
  29. package/lib/dts/aigne/aigne.d.ts +10 -1
  30. package/lib/dts/aigne/context.d.ts +5 -1
  31. package/lib/dts/index.d.ts +2 -0
  32. package/lib/dts/loader/agent-yaml.d.ts +6 -1
  33. package/lib/dts/loader/index.d.ts +46 -44
  34. package/lib/dts/prompt/prompt-builder.d.ts +3 -0
  35. package/lib/dts/utils/camelize.d.ts +7 -1
  36. package/lib/dts/utils/type-utils.d.ts +1 -1
  37. package/lib/esm/agents/agent.d.ts +2 -0
  38. package/lib/esm/agents/agent.js +8 -1
  39. package/lib/esm/agents/chat-model.d.ts +17 -3
  40. package/lib/esm/agents/chat-model.js +7 -3
  41. package/lib/esm/agents/image-agent.d.ts +21 -0
  42. package/lib/esm/agents/image-agent.js +36 -0
  43. package/lib/esm/agents/image-model.d.ts +115 -0
  44. package/lib/esm/agents/image-model.js +56 -0
  45. package/lib/esm/aigne/aigne.d.ts +10 -1
  46. package/lib/esm/aigne/aigne.js +10 -6
  47. package/lib/esm/aigne/context.d.ts +5 -1
  48. package/lib/esm/aigne/context.js +6 -0
  49. package/lib/esm/index.d.ts +2 -0
  50. package/lib/esm/index.js +2 -0
  51. package/lib/esm/loader/agent-yaml.d.ts +6 -1
  52. package/lib/esm/loader/agent-yaml.js +18 -9
  53. package/lib/esm/loader/index.d.ts +46 -44
  54. package/lib/esm/loader/index.js +36 -13
  55. package/lib/esm/prompt/prompt-builder.d.ts +3 -0
  56. package/lib/esm/prompt/prompt-builder.js +8 -0
  57. package/lib/esm/utils/camelize.d.ts +7 -1
  58. package/lib/esm/utils/camelize.js +29 -13
  59. package/lib/esm/utils/type-utils.d.ts +1 -1
  60. package/package.json +3 -3
@@ -1,51 +1,55 @@
1
- import type { Camelize } from "camelize-ts";
2
1
  import { z } from "zod";
3
2
  import { Agent, type AgentOptions } from "../agents/agent.js";
4
3
  import type { ChatModel } from "../agents/chat-model.js";
4
+ import type { ImageModel } from "../agents/image-model.js";
5
5
  import type { AIGNEOptions } from "../aigne/aigne.js";
6
6
  import type { MemoryAgent, MemoryAgentOptions } from "../memory/memory.js";
7
7
  import { type PromiseOrValue } from "../utils/type-utils.js";
8
8
  export interface LoadOptions {
9
- loadModel: (model?: Camelize<z.infer<typeof aigneFileSchema>["model"]>) => PromiseOrValue<ChatModel | undefined>;
10
9
  memories?: {
11
10
  new (parameters?: MemoryAgentOptions): MemoryAgent;
12
11
  }[];
13
- path: string;
14
- model?: ChatModel;
12
+ model?: ChatModel | ((model?: z.infer<typeof aigneFileSchema>["chatModel"]) => PromiseOrValue<ChatModel | undefined>);
13
+ imageModel?: ImageModel | ((model?: z.infer<typeof aigneFileSchema>["imageModel"]) => PromiseOrValue<ImageModel | undefined>);
15
14
  }
16
- export declare function load(options: LoadOptions): Promise<AIGNEOptions>;
15
+ export declare function load(path: string, options?: LoadOptions): Promise<AIGNEOptions>;
17
16
  export declare function loadAgent(path: string, options?: LoadOptions, agentOptions?: AgentOptions): Promise<Agent>;
18
17
  declare const aigneFileSchema: z.ZodObject<{
19
18
  name: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
20
19
  description: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
21
- model: z.ZodEffects<z.ZodType<string | {
22
- name?: string | null | undefined;
23
- temperature?: number | null | undefined;
24
- topP?: number | null | undefined;
25
- frequencyPenalty?: number | null | undefined;
26
- presencePenalty?: number | null | undefined;
27
- provider?: string | null | undefined;
20
+ chatModel: z.ZodEffects<z.ZodEffects<z.ZodType<string | {
21
+ model?: string | undefined;
22
+ temperature?: number | undefined;
23
+ topP?: number | undefined;
24
+ frequencyPenalty?: number | undefined;
25
+ presencePenalty?: number | undefined;
28
26
  } | undefined, z.ZodTypeDef, string | {
29
- name?: string | null | undefined;
30
- temperature?: number | null | undefined;
31
- topP?: number | null | undefined;
32
- frequencyPenalty?: number | null | undefined;
33
- presencePenalty?: number | null | undefined;
34
- provider?: string | null | undefined;
27
+ model?: string | undefined;
28
+ temperature?: number | undefined;
29
+ topP?: number | undefined;
30
+ frequencyPenalty?: number | undefined;
31
+ presencePenalty?: number | undefined;
32
+ } | undefined>, string | {
33
+ model?: string | undefined;
34
+ temperature?: number | undefined;
35
+ topP?: number | undefined;
36
+ frequencyPenalty?: number | undefined;
37
+ presencePenalty?: number | undefined;
38
+ } | undefined, unknown>, {
39
+ model?: string | undefined;
40
+ temperature?: number | undefined;
41
+ topP?: number | undefined;
42
+ frequencyPenalty?: number | undefined;
43
+ presencePenalty?: number | undefined;
44
+ } | undefined, unknown>;
45
+ imageModel: z.ZodEffects<z.ZodType<string | {
46
+ model?: string | undefined;
47
+ } | undefined, z.ZodTypeDef, string | {
48
+ model?: string | undefined;
35
49
  } | undefined>, {
36
- name?: string | null | undefined;
37
- temperature?: number | null | undefined;
38
- topP?: number | null | undefined;
39
- frequencyPenalty?: number | null | undefined;
40
- presencePenalty?: number | null | undefined;
41
- provider?: string | null | undefined;
50
+ model?: string | undefined;
42
51
  } | undefined, string | {
43
- name?: string | null | undefined;
44
- temperature?: number | null | undefined;
45
- topP?: number | null | undefined;
46
- frequencyPenalty?: number | null | undefined;
47
- presencePenalty?: number | null | undefined;
48
- provider?: string | null | undefined;
52
+ model?: string | undefined;
49
53
  } | undefined>;
50
54
  agents: z.ZodType<string[] | undefined, z.ZodTypeDef, string[] | undefined>;
51
55
  skills: z.ZodType<string[] | undefined, z.ZodTypeDef, string[] | undefined>;
@@ -63,15 +67,17 @@ declare const aigneFileSchema: z.ZodObject<{
63
67
  name?: string | undefined;
64
68
  description?: string | undefined;
65
69
  skills?: string[] | undefined;
66
- model?: {
67
- name?: string | null | undefined;
68
- temperature?: number | null | undefined;
69
- topP?: number | null | undefined;
70
- frequencyPenalty?: number | null | undefined;
71
- presencePenalty?: number | null | undefined;
72
- provider?: string | null | undefined;
70
+ imageModel?: {
71
+ model?: string | undefined;
73
72
  } | undefined;
74
73
  agents?: string[] | undefined;
74
+ chatModel?: {
75
+ model?: string | undefined;
76
+ temperature?: number | undefined;
77
+ topP?: number | undefined;
78
+ frequencyPenalty?: number | undefined;
79
+ presencePenalty?: number | undefined;
80
+ } | undefined;
75
81
  mcpServer?: {
76
82
  agents?: string[] | undefined;
77
83
  } | undefined;
@@ -82,15 +88,11 @@ declare const aigneFileSchema: z.ZodObject<{
82
88
  name?: string | undefined;
83
89
  description?: string | undefined;
84
90
  skills?: string[] | undefined;
85
- model?: string | {
86
- name?: string | null | undefined;
87
- temperature?: number | null | undefined;
88
- topP?: number | null | undefined;
89
- frequencyPenalty?: number | null | undefined;
90
- presencePenalty?: number | null | undefined;
91
- provider?: string | null | undefined;
91
+ imageModel?: string | {
92
+ model?: string | undefined;
92
93
  } | undefined;
93
94
  agents?: string[] | undefined;
95
+ chatModel?: unknown;
94
96
  mcpServer?: {
95
97
  agents?: string[] | undefined;
96
98
  } | undefined;
@@ -27,6 +27,9 @@ export declare class PromptBuilder {
27
27
  build(options: PromptBuildOptions): Promise<ChatModelInput & {
28
28
  toolAgents?: Agent[];
29
29
  }>;
30
+ buildImagePrompt(options: Pick<PromptBuildOptions, "input">): Promise<{
31
+ prompt: string;
32
+ }>;
30
33
  private buildMessages;
31
34
  private convertMemoriesToMessages;
32
35
  private buildResponseFormat;
@@ -1 +1,7 @@
1
- export declare function camelize<T>(obj: T, shallow?: boolean): any;
1
+ export type CamelCase<S extends string> = S extends `${infer P1}_${infer P2}${infer P3}` ? `${P1}${Uppercase<P2>}${CamelCase<P3>}` : S;
2
+ export type CamelizeObject<T, S = false> = {
3
+ [K in keyof T as Uncapitalize<CamelCase<string & K>>]: T[K] extends Date ? T[K] : T[K] extends RegExp ? T[K] : T[K] extends Array<infer U> ? U extends object | undefined ? Array<CamelizeObject<U>> : T[K] : T[K] extends object | undefined ? S extends true ? T[K] : CamelizeObject<T[K]> : T[K];
4
+ };
5
+ export type Camelize<T, S = false> = T extends Array<infer U> ? Array<CamelizeObject<U, S>> : CamelizeObject<T, S>;
6
+ export declare function camelize<T, S extends boolean = false>(obj: T, shallow?: S): T extends string ? string : Camelize<T, S>;
7
+ export declare function snakelize<T, S extends boolean = false>(obj: T, shallow?: S): T extends string ? string : any;
@@ -18,7 +18,7 @@ export declare function unique<T>(arr: T[], key?: (item: T) => unknown): T[];
18
18
  export declare function pick<T extends object, K extends keyof T>(obj: T, ...keys: (K | K[])[]): Pick<T, K>;
19
19
  export declare function omit<T extends object, K extends keyof T>(obj: T, ...keys: (K | K[])[]): Omit<T, K>;
20
20
  export declare function omitDeep<T, K>(obj: T, ...keys: (K | K[])[]): unknown;
21
- export declare function omitBy<T extends Record<string, unknown>, K extends keyof T>(obj: T, predicate: (value: T[K], key: K) => boolean): Partial<T>;
21
+ export declare function omitBy<T extends object, K extends keyof T>(obj: T, predicate: (value: T[K], key: K) => boolean): Partial<T>;
22
22
  export declare function flat<T>(...value: (T | T[])[]): NonNullable<T>[];
23
23
  export declare function createAccessorArray<T>(array: T[], accessor: (array: T[], name: string) => T | undefined): T[] & {
24
24
  [key: string]: T;
@@ -116,6 +116,7 @@ export interface AgentOptions<I extends Message = Message, O extends Message = M
116
116
  * One or more memory agents this agent can use
117
117
  */
118
118
  memory?: MemoryAgent | MemoryAgent[];
119
+ asyncMemoryRecord?: boolean;
119
120
  /**
120
121
  * Maximum number of memory items to retrieve
121
122
  */
@@ -199,6 +200,7 @@ export declare abstract class Agent<I extends Message = any, O extends Message =
199
200
  * List of memories this agent can use
200
201
  */
201
202
  readonly memories: MemoryAgent[];
203
+ asyncMemoryRecord?: boolean;
202
204
  tag?: string;
203
205
  /**
204
206
  * Maximum number of memory items to retrieve
@@ -30,6 +30,7 @@ export const agentOptionsSchema = z.object({
30
30
  skills: z.array(z.union([z.custom(), z.custom()])).optional(),
31
31
  disableEvents: z.boolean().optional(),
32
32
  memory: z.union([z.custom(), z.array(z.custom())]).optional(),
33
+ asyncMemoryRecord: z.boolean().optional(),
33
34
  maxRetrieveMemoryCount: z.number().optional(),
34
35
  hooks: z.union([z.array(hooksSchema), hooksSchema]).optional(),
35
36
  guideRails: z.array(z.custom()).optional(),
@@ -83,6 +84,7 @@ export class Agent {
83
84
  else if (options.memory) {
84
85
  this.memories.push(options.memory);
85
86
  }
87
+ this.asyncMemoryRecord = options.asyncMemoryRecord;
86
88
  this.maxRetrieveMemoryCount = options.maxRetrieveMemoryCount;
87
89
  this.hooks = flat(options.hooks);
88
90
  this.guideRails = options.guideRails;
@@ -91,6 +93,7 @@ export class Agent {
91
93
  * List of memories this agent can use
92
94
  */
93
95
  memories = [];
96
+ asyncMemoryRecord;
94
97
  tag;
95
98
  /**
96
99
  * Maximum number of memory items to retrieve
@@ -552,7 +555,11 @@ export class Agent {
552
555
  async postprocess(input, output, options) {
553
556
  this.checkContextStatus(options);
554
557
  this.publishToTopics(output, options);
555
- await this.recordMemories({ content: [{ input, output: replaceTransferAgentToName(output), source: this.name }] }, options);
558
+ const memory = this.recordMemories({ content: [{ input, output: replaceTransferAgentToName(output), source: this.name }] }, options).catch((error) => {
559
+ logger.error(`Agent ${this.name} failed to record memories:`, error);
560
+ });
561
+ if (!this.asyncMemoryRecord)
562
+ await memory;
556
563
  }
557
564
  async publishToTopics(output, options) {
558
565
  const publishTopics = typeof this.publishTopic === "function" ? await this.publishTopic(output) : this.publishTopic;
@@ -1,5 +1,6 @@
1
+ import { z } from "zod";
1
2
  import type { PromiseOrValue } from "../utils/type-utils.js";
2
- import { Agent, type AgentInvokeOptions, type AgentProcessResult, type Message } from "./agent.js";
3
+ import { Agent, type AgentInvokeOptions, type AgentOptions, type AgentProcessResult, type Message } from "./agent.js";
3
4
  /**
4
5
  * ChatModel is an abstract base class for interacting with Large Language Models (LLMs).
5
6
  *
@@ -25,8 +26,8 @@ import { Agent, type AgentInvokeOptions, type AgentProcessResult, type Message }
25
26
  */
26
27
  export declare abstract class ChatModel extends Agent<ChatModelInput, ChatModelOutput> {
27
28
  tag: string;
28
- constructor();
29
- abstract getCredential(): Promise<{
29
+ constructor(options?: Omit<AgentOptions<ChatModelInput, ChatModelOutput>, "inputSchema" | "outputSchema">);
30
+ get credential(): PromiseOrValue<{
30
31
  url?: string;
31
32
  apiKey?: string;
32
33
  model?: string;
@@ -391,3 +392,16 @@ export interface ChatModelOutputUsage {
391
392
  */
392
393
  aigneHubCredits?: number;
393
394
  }
395
+ export declare const chatModelOutputUsageSchema: z.ZodObject<{
396
+ inputTokens: z.ZodNumber;
397
+ outputTokens: z.ZodNumber;
398
+ aigneHubCredits: z.ZodOptional<z.ZodNumber>;
399
+ }, "strip", z.ZodTypeAny, {
400
+ inputTokens: number;
401
+ outputTokens: number;
402
+ aigneHubCredits?: number | undefined;
403
+ }, {
404
+ inputTokens: number;
405
+ outputTokens: number;
406
+ aigneHubCredits?: number | undefined;
407
+ }>;
@@ -1,5 +1,5 @@
1
1
  import { z } from "zod";
2
- import { Agent } from "./agent.js";
2
+ import { Agent, } from "./agent.js";
3
3
  /**
4
4
  * ChatModel is an abstract base class for interacting with Large Language Models (LLMs).
5
5
  *
@@ -25,12 +25,16 @@ import { Agent } from "./agent.js";
25
25
  */
26
26
  export class ChatModel extends Agent {
27
27
  tag = "ChatModelAgent";
28
- constructor() {
28
+ constructor(options) {
29
29
  super({
30
+ ...options,
30
31
  inputSchema: chatModelInputSchema,
31
32
  outputSchema: chatModelOutputSchema,
32
33
  });
33
34
  }
35
+ get credential() {
36
+ return {};
37
+ }
34
38
  /**
35
39
  * Indicates whether the model supports parallel tool calls
36
40
  *
@@ -213,7 +217,7 @@ const chatModelOutputToolCallSchema = z.object({
213
217
  arguments: z.record(z.string(), z.unknown()),
214
218
  }),
215
219
  });
216
- const chatModelOutputUsageSchema = z.object({
220
+ export const chatModelOutputUsageSchema = z.object({
217
221
  inputTokens: z.number(),
218
222
  outputTokens: z.number(),
219
223
  aigneHubCredits: z.number().optional(),
@@ -0,0 +1,21 @@
1
+ import { type ZodObject, type ZodType } from "zod";
2
+ import { PromptBuilder } from "../prompt/prompt-builder.js";
3
+ import { Agent, type AgentInvokeOptions, type AgentOptions, type Message } from "./agent.js";
4
+ import { type ImageModel, type ImageModelOutput } from "./image-model.js";
5
+ export interface ImageAgentOptions<I extends Message = any, O extends ImageModelOutput = any> extends Omit<AgentOptions<I, O>, "outputSchema"> {
6
+ model?: ImageModel;
7
+ instructions: string | PromptBuilder;
8
+ modelOptions?: Record<string, any>;
9
+ }
10
+ export declare const imageAgentOptionsSchema: ZodObject<{
11
+ [key in keyof ImageAgentOptions]: ZodType<ImageAgentOptions[key]>;
12
+ }>;
13
+ export declare class ImageAgent<I extends Message = any, O extends ImageModelOutput = any> extends Agent<I, O> {
14
+ tag: string;
15
+ static from<I extends Message = any, O extends ImageModelOutput = any>(options: ImageAgentOptions<I, O>): ImageAgent<I, O>;
16
+ constructor(options: ImageAgentOptions<I, O>);
17
+ model?: ImageModel;
18
+ instructions: PromptBuilder;
19
+ modelOptions?: Record<string, any>;
20
+ process(input: I, options: AgentInvokeOptions): Promise<O>;
21
+ }
@@ -0,0 +1,36 @@
1
+ import z from "zod";
2
+ import { PromptBuilder } from "../prompt/prompt-builder.js";
3
+ import { checkArguments } from "../utils/type-utils.js";
4
+ import { Agent, agentOptionsSchema, } from "./agent.js";
5
+ import { imageModelOutputSchema } from "./image-model.js";
6
+ export const imageAgentOptionsSchema = agentOptionsSchema.extend({
7
+ model: z.custom().optional(),
8
+ instructions: z.union([z.string(), z.custom()]),
9
+ modelOptions: z.record(z.any()).optional(),
10
+ });
11
+ export class ImageAgent extends Agent {
12
+ tag = "ImageAgent";
13
+ static from(options) {
14
+ return new ImageAgent(options);
15
+ }
16
+ constructor(options) {
17
+ super({ ...options, outputSchema: imageModelOutputSchema });
18
+ checkArguments("ImageAgent", imageAgentOptionsSchema, options);
19
+ this.model = options.model;
20
+ this.instructions =
21
+ typeof options.instructions === "string"
22
+ ? PromptBuilder.from(options.instructions)
23
+ : options.instructions;
24
+ this.modelOptions = options.modelOptions;
25
+ }
26
+ model;
27
+ instructions;
28
+ modelOptions;
29
+ async process(input, options) {
30
+ const model = this.model ?? options.context.imageModel;
31
+ if (!model)
32
+ throw new Error("image model is required to run ImageAgent");
33
+ const { prompt } = await this.instructions.buildImagePrompt({ input });
34
+ return (await options.context.invoke(model, { ...input, ...this.modelOptions, prompt }, { ...options, streaming: false }));
35
+ }
36
+ }
@@ -0,0 +1,115 @@
1
+ import { z } from "zod";
2
+ import type { PromiseOrValue } from "../utils/type-utils.js";
3
+ import { Agent, type AgentInvokeOptions, type AgentOptions, type AgentProcessResult, type Message } from "./agent.js";
4
+ import { type ChatModelOutputUsage } from "./chat-model.js";
5
+ export interface ImageModelOptions<I extends ImageModelInput = ImageModelInput, O extends ImageModelOutput = ImageModelOutput> extends AgentOptions<I, O> {
6
+ }
7
+ export declare abstract class ImageModel<I extends ImageModelInput = ImageModelInput, O extends ImageModelOutput = ImageModelOutput> extends Agent<I, O> {
8
+ tag: string;
9
+ constructor(options?: ImageModelOptions<I, O>);
10
+ get credential(): PromiseOrValue<{
11
+ url?: string;
12
+ apiKey?: string;
13
+ model?: string;
14
+ }>;
15
+ protected preprocess(input: I, options: AgentInvokeOptions): Promise<void>;
16
+ protected postprocess(input: I, output: O, options: AgentInvokeOptions): Promise<void>;
17
+ abstract process(input: I, options: AgentInvokeOptions): PromiseOrValue<AgentProcessResult<O>>;
18
+ }
19
+ export interface ImageModelInput extends Message {
20
+ model?: string;
21
+ image?: string | string[];
22
+ prompt: string;
23
+ n?: number;
24
+ responseFormat?: "url" | "base64";
25
+ }
26
+ export declare const imageModelInputSchema: z.ZodObject<{
27
+ model: z.ZodOptional<z.ZodString>;
28
+ image: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString, "many">]>>;
29
+ prompt: z.ZodString;
30
+ n: z.ZodOptional<z.ZodNumber>;
31
+ responseFormat: z.ZodOptional<z.ZodEnum<["url", "base64"]>>;
32
+ }, "strip", z.ZodTypeAny, {
33
+ prompt: string;
34
+ model?: string | undefined;
35
+ responseFormat?: "url" | "base64" | undefined;
36
+ image?: string | string[] | undefined;
37
+ n?: number | undefined;
38
+ }, {
39
+ prompt: string;
40
+ model?: string | undefined;
41
+ responseFormat?: "url" | "base64" | undefined;
42
+ image?: string | string[] | undefined;
43
+ n?: number | undefined;
44
+ }>;
45
+ export interface ImageModelOutput extends Message {
46
+ images: ImageModelOutputImage[];
47
+ /**
48
+ * Token usage statistics
49
+ */
50
+ usage?: ChatModelOutputUsage;
51
+ /**
52
+ * Model name or version used
53
+ */
54
+ model?: string;
55
+ }
56
+ export type ImageModelOutputImage = ImageModelOutputImageUrl | ImageModelOutputImageBase64;
57
+ export interface ImageModelOutputImageUrl {
58
+ url: string;
59
+ }
60
+ export interface ImageModelOutputImageBase64 {
61
+ base64: string;
62
+ }
63
+ export declare const imageModelOutputSchema: z.ZodObject<{
64
+ images: z.ZodArray<z.ZodUnion<[z.ZodObject<{
65
+ url: z.ZodString;
66
+ }, "strip", z.ZodTypeAny, {
67
+ url: string;
68
+ }, {
69
+ url: string;
70
+ }>, z.ZodObject<{
71
+ base64: z.ZodString;
72
+ }, "strip", z.ZodTypeAny, {
73
+ base64: string;
74
+ }, {
75
+ base64: string;
76
+ }>]>, "many">;
77
+ usage: z.ZodOptional<z.ZodObject<{
78
+ inputTokens: z.ZodNumber;
79
+ outputTokens: z.ZodNumber;
80
+ aigneHubCredits: z.ZodOptional<z.ZodNumber>;
81
+ }, "strip", z.ZodTypeAny, {
82
+ inputTokens: number;
83
+ outputTokens: number;
84
+ aigneHubCredits?: number | undefined;
85
+ }, {
86
+ inputTokens: number;
87
+ outputTokens: number;
88
+ aigneHubCredits?: number | undefined;
89
+ }>>;
90
+ model: z.ZodOptional<z.ZodString>;
91
+ }, "strip", z.ZodTypeAny, {
92
+ images: ({
93
+ url: string;
94
+ } | {
95
+ base64: string;
96
+ })[];
97
+ model?: string | undefined;
98
+ usage?: {
99
+ inputTokens: number;
100
+ outputTokens: number;
101
+ aigneHubCredits?: number | undefined;
102
+ } | undefined;
103
+ }, {
104
+ images: ({
105
+ url: string;
106
+ } | {
107
+ base64: string;
108
+ })[];
109
+ model?: string | undefined;
110
+ usage?: {
111
+ inputTokens: number;
112
+ outputTokens: number;
113
+ aigneHubCredits?: number | undefined;
114
+ } | undefined;
115
+ }>;
@@ -0,0 +1,56 @@
1
+ import { z } from "zod";
2
+ import { Agent, } from "./agent.js";
3
+ import { chatModelOutputUsageSchema } from "./chat-model.js";
4
+ export class ImageModel extends Agent {
5
+ tag = "ImageModelAgent";
6
+ constructor(options) {
7
+ super({
8
+ inputSchema: imageModelInputSchema,
9
+ outputSchema: imageModelOutputSchema,
10
+ ...options,
11
+ });
12
+ }
13
+ get credential() {
14
+ return {};
15
+ }
16
+ async preprocess(input, options) {
17
+ super.preprocess(input, options);
18
+ const { limits, usage } = options.context;
19
+ const usedTokens = usage.outputTokens + usage.inputTokens;
20
+ if (limits?.maxTokens && usedTokens >= limits.maxTokens) {
21
+ throw new Error(`Exceeded max tokens ${usedTokens}/${limits.maxTokens}`);
22
+ }
23
+ }
24
+ async postprocess(input, output, options) {
25
+ super.postprocess(input, output, options);
26
+ const { usage } = output;
27
+ if (usage) {
28
+ options.context.usage.outputTokens += usage.outputTokens;
29
+ options.context.usage.inputTokens += usage.inputTokens;
30
+ if (usage.aigneHubCredits)
31
+ options.context.usage.aigneHubCredits += usage.aigneHubCredits;
32
+ }
33
+ }
34
+ }
35
+ export const imageModelInputSchema = z.object({
36
+ model: z.string().optional(),
37
+ image: z
38
+ .union([z.string(), z.array(z.string())])
39
+ .optional()
40
+ .describe("Image URL or base64 string(s) used for editing"),
41
+ prompt: z.string(),
42
+ n: z.number().int().min(1).optional(),
43
+ responseFormat: z.enum(["url", "base64"]).optional(),
44
+ });
45
+ export const imageModelOutputSchema = z.object({
46
+ images: z.array(z.union([
47
+ z.object({
48
+ url: z.string(),
49
+ }),
50
+ z.object({
51
+ base64: z.string(),
52
+ }),
53
+ ])),
54
+ usage: chatModelOutputUsageSchema.optional(),
55
+ model: z.string().optional(),
56
+ });
@@ -1,6 +1,7 @@
1
1
  import { AIGNEObserver } from "@aigne/observability-api";
2
2
  import type { Agent, AgentResponse, AgentResponseStream, Message } from "../agents/agent.js";
3
3
  import type { ChatModel } from "../agents/chat-model.js";
4
+ import type { ImageModel } from "../agents/image-model.js";
4
5
  import type { UserAgent } from "../agents/user-agent.js";
5
6
  import { type LoadOptions } from "../loader/index.js";
6
7
  import { AIGNEContext, type Context, type InvokeOptions, type UserContext } from "./context.js";
@@ -27,6 +28,10 @@ export interface AIGNEOptions {
27
28
  * Global model to use for all agents not specifying a model.
28
29
  */
29
30
  model?: ChatModel;
31
+ /**
32
+ * Optional image model to use for image processing tasks.
33
+ */
34
+ imageModel?: ImageModel;
30
35
  /**
31
36
  * Skills to use for the AIGNE instance.
32
37
  */
@@ -71,7 +76,7 @@ export declare class AIGNE<U extends UserContext = UserContext> {
71
76
  * @param options - Options to override the loaded configuration.
72
77
  * @returns A fully initialized AIGNE instance with configured agents and skills.
73
78
  */
74
- static load(path: string, options: AIGNEOptions & Omit<LoadOptions, "path">): Promise<AIGNE>;
79
+ static load(path: string, options?: Omit<AIGNEOptions, keyof LoadOptions> & LoadOptions): Promise<AIGNE>;
75
80
  /**
76
81
  * Creates a new AIGNE instance with the specified options.
77
82
  *
@@ -94,6 +99,10 @@ export declare class AIGNE<U extends UserContext = UserContext> {
94
99
  * Global model to use for all agents that don't specify their own model.
95
100
  */
96
101
  model?: ChatModel;
102
+ /**
103
+ * Optional image model to use for image processing tasks.
104
+ */
105
+ imageModel?: ImageModel;
97
106
  /**
98
107
  * Usage limits applied to this AIGNE instance's execution.
99
108
  */
@@ -25,15 +25,13 @@ export class AIGNE {
25
25
  * @param options - Options to override the loaded configuration.
26
26
  * @returns A fully initialized AIGNE instance with configured agents and skills.
27
27
  */
28
- static async load(path, options) {
29
- const { model, agents = [], skills = [], ...aigne } = await load({ ...options, path });
28
+ static async load(path, options = {}) {
29
+ const { agents = [], skills = [], model, imageModel, ...aigne } = await load(path, options);
30
30
  return new AIGNE({
31
31
  ...aigne,
32
32
  ...options,
33
- rootDir: aigne.rootDir,
34
- model: options?.model || model,
35
- name: options?.name || aigne.name || undefined,
36
- description: options?.description || aigne.description || undefined,
33
+ model,
34
+ imageModel,
37
35
  agents: agents.concat(options?.agents ?? []),
38
36
  skills: skills.concat(options?.skills ?? []),
39
37
  });
@@ -50,6 +48,7 @@ export class AIGNE {
50
48
  this.name = options?.name;
51
49
  this.description = options?.description;
52
50
  this.model = options?.model;
51
+ this.imageModel = options?.imageModel;
53
52
  this.limits = options?.limits;
54
53
  this.observer =
55
54
  process.env.AIGNE_OBSERVABILITY_DISABLED === "true"
@@ -82,6 +81,10 @@ export class AIGNE {
82
81
  * Global model to use for all agents that don't specify their own model.
83
82
  */
84
83
  model;
84
+ /**
85
+ * Optional image model to use for image processing tasks.
86
+ */
87
+ imageModel;
85
88
  /**
86
89
  * Usage limits applied to this AIGNE instance's execution.
87
90
  */
@@ -219,6 +222,7 @@ export class AIGNE {
219
222
  }
220
223
  const aigneOptionsSchema = z.object({
221
224
  model: z.custom().optional(),
225
+ imageModel: z.custom().optional(),
222
226
  skills: z.array(z.custom()).optional(),
223
227
  agents: z.array(z.custom()).optional(),
224
228
  observer: z.custom().optional(),
@@ -3,6 +3,7 @@ import type { Span } from "@opentelemetry/api";
3
3
  import { Emitter } from "strict-event-emitter";
4
4
  import { type Agent, type AgentHooks, type AgentInvokeOptions, type AgentProcessAsyncGenerator, type AgentResponse, type AgentResponseStream, type Message } from "../agents/agent.js";
5
5
  import type { ChatModel } from "../agents/chat-model.js";
6
+ import type { ImageModel } from "../agents/image-model.js";
6
7
  import { UserAgent } from "../agents/user-agent.js";
7
8
  import type { Memory } from "../memory/memory.js";
8
9
  import { type OmitPropertiesFromArrayFirstElement } from "../utils/type-utils.js";
@@ -73,6 +74,7 @@ export interface Context<U extends UserContext = UserContext> extends TypedEvent
73
74
  parentId?: string;
74
75
  rootId: string;
75
76
  model?: ChatModel;
77
+ imageModel?: ImageModel;
76
78
  skills?: Agent[];
77
79
  agents: Agent[];
78
80
  observer?: AIGNEObserver;
@@ -155,6 +157,7 @@ export declare class AIGNEContext implements Context {
155
157
  readonly internal: AIGNEContextShared;
156
158
  get messageQueue(): MessageQueue;
157
159
  get model(): ChatModel | undefined;
160
+ get imageModel(): ImageModel<import("../agents/image-model.js").ImageModelInput, import("../agents/image-model.js").ImageModelOutput> | undefined;
158
161
  get skills(): Agent<any, any>[] | undefined;
159
162
  get agents(): Agent<any, any>[];
160
163
  get observer(): AIGNEObserver | undefined;
@@ -187,13 +190,14 @@ export declare class AIGNEContext implements Context {
187
190
  declare class AIGNEContextShared {
188
191
  private readonly parent?;
189
192
  spans: Span[];
190
- constructor(parent?: (Pick<Context, "model" | "agents" | "skills" | "limits" | "observer"> & {
193
+ constructor(parent?: (Pick<Context, "model" | "imageModel" | "agents" | "skills" | "limits" | "observer"> & {
191
194
  messageQueue?: MessageQueue;
192
195
  events?: Emitter<any>;
193
196
  }) | undefined);
194
197
  readonly messageQueue: MessageQueue;
195
198
  readonly events: Emitter<any>;
196
199
  get model(): ChatModel | undefined;
200
+ get imageModel(): ImageModel<import("../agents/image-model.js").ImageModelInput, import("../agents/image-model.js").ImageModelOutput> | undefined;
197
201
  get skills(): Agent<any, any>[] | undefined;
198
202
  get agents(): Agent<any, any>[];
199
203
  get observer(): AIGNEObserver | undefined;
@@ -55,6 +55,9 @@ export class AIGNEContext {
55
55
  get model() {
56
56
  return this.internal.model;
57
57
  }
58
+ get imageModel() {
59
+ return this.internal.imageModel;
60
+ }
58
61
  get skills() {
59
62
  return this.internal.skills;
60
63
  }
@@ -299,6 +302,9 @@ class AIGNEContextShared {
299
302
  get model() {
300
303
  return this.parent?.model;
301
304
  }
305
+ get imageModel() {
306
+ return this.parent?.imageModel;
307
+ }
302
308
  get skills() {
303
309
  return this.parent?.skills;
304
310
  }