@aigne/core 1.10.0 → 1.12.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 (142) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/LICENSE +93 -0
  3. package/README.md +90 -0
  4. package/README.zh.md +90 -0
  5. package/lib/cjs/agents/agent.d.ts +21 -20
  6. package/lib/cjs/agents/agent.js +29 -26
  7. package/lib/cjs/agents/ai-agent.d.ts +9 -8
  8. package/lib/cjs/agents/ai-agent.js +20 -14
  9. package/lib/cjs/agents/mcp-agent.d.ts +10 -4
  10. package/lib/cjs/agents/mcp-agent.js +12 -6
  11. package/lib/cjs/agents/memory.d.ts +1 -1
  12. package/lib/cjs/agents/team-agent.d.ts +28 -0
  13. package/lib/cjs/agents/team-agent.js +93 -0
  14. package/lib/cjs/agents/user-agent.d.ts +9 -10
  15. package/lib/cjs/agents/user-agent.js +10 -13
  16. package/lib/{esm/execution-engine/execution-engine.d.ts → cjs/aigne/aigne.d.ts} +9 -12
  17. package/lib/cjs/{execution-engine/execution-engine.js → aigne/aigne.js} +19 -19
  18. package/lib/cjs/{execution-engine → aigne}/context.d.ts +31 -32
  19. package/lib/cjs/{execution-engine → aigne}/context.js +30 -40
  20. package/lib/cjs/aigne/index.d.ts +4 -0
  21. package/lib/cjs/{execution-engine → aigne}/index.js +2 -2
  22. package/lib/cjs/{execution-engine → aigne}/usage.d.ts +1 -1
  23. package/lib/cjs/client/client.d.ts +19 -0
  24. package/lib/cjs/client/client.js +49 -0
  25. package/lib/cjs/index.d.ts +2 -1
  26. package/lib/cjs/index.js +2 -1
  27. package/lib/cjs/loader/agent-js.d.ts +2 -2
  28. package/lib/cjs/loader/agent-js.js +4 -5
  29. package/lib/cjs/loader/agent-yaml.d.ts +8 -5
  30. package/lib/cjs/loader/agent-yaml.js +21 -2
  31. package/lib/cjs/loader/index.d.ts +5 -5
  32. package/lib/cjs/loader/index.js +8 -19
  33. package/lib/cjs/models/chat-model.d.ts +1 -1
  34. package/lib/cjs/models/claude-chat-model.d.ts +3 -1
  35. package/lib/cjs/models/claude-chat-model.js +75 -60
  36. package/lib/cjs/models/openai-chat-model.d.ts +3 -3
  37. package/lib/cjs/models/openai-chat-model.js +1 -3
  38. package/lib/cjs/prompt/prompt-builder.d.ts +1 -1
  39. package/lib/cjs/prompt/prompt-builder.js +3 -3
  40. package/lib/cjs/server/error.d.ts +4 -0
  41. package/lib/cjs/server/error.js +11 -0
  42. package/lib/cjs/server/server.d.ts +54 -0
  43. package/lib/cjs/server/server.js +130 -0
  44. package/lib/cjs/utils/camelize.d.ts +13 -0
  45. package/lib/cjs/utils/camelize.js +16 -0
  46. package/lib/cjs/utils/event-stream.d.ts +11 -0
  47. package/lib/cjs/utils/event-stream.js +91 -0
  48. package/lib/cjs/utils/mcp-utils.js +4 -1
  49. package/lib/cjs/utils/stream-utils.d.ts +10 -3
  50. package/lib/cjs/utils/stream-utils.js +51 -36
  51. package/lib/cjs/utils/type-utils.d.ts +4 -2
  52. package/lib/cjs/utils/type-utils.js +10 -2
  53. package/lib/dts/agents/agent.d.ts +21 -20
  54. package/lib/dts/agents/ai-agent.d.ts +9 -8
  55. package/lib/dts/agents/mcp-agent.d.ts +10 -4
  56. package/lib/dts/agents/memory.d.ts +1 -1
  57. package/lib/dts/agents/team-agent.d.ts +28 -0
  58. package/lib/dts/agents/user-agent.d.ts +9 -10
  59. package/lib/dts/{execution-engine/execution-engine.d.ts → aigne/aigne.d.ts} +9 -12
  60. package/lib/dts/{execution-engine → aigne}/context.d.ts +31 -32
  61. package/lib/dts/aigne/index.d.ts +4 -0
  62. package/lib/dts/{execution-engine → aigne}/usage.d.ts +1 -1
  63. package/lib/dts/client/client.d.ts +19 -0
  64. package/lib/dts/index.d.ts +2 -1
  65. package/lib/dts/loader/agent-js.d.ts +2 -2
  66. package/lib/dts/loader/agent-yaml.d.ts +8 -5
  67. package/lib/dts/loader/index.d.ts +5 -5
  68. package/lib/dts/models/chat-model.d.ts +1 -1
  69. package/lib/dts/models/claude-chat-model.d.ts +3 -1
  70. package/lib/dts/models/openai-chat-model.d.ts +3 -3
  71. package/lib/dts/prompt/prompt-builder.d.ts +1 -1
  72. package/lib/dts/server/error.d.ts +4 -0
  73. package/lib/dts/server/server.d.ts +54 -0
  74. package/lib/dts/utils/camelize.d.ts +13 -0
  75. package/lib/dts/utils/event-stream.d.ts +11 -0
  76. package/lib/dts/utils/stream-utils.d.ts +10 -3
  77. package/lib/dts/utils/type-utils.d.ts +4 -2
  78. package/lib/esm/agents/agent.d.ts +21 -20
  79. package/lib/esm/agents/agent.js +29 -27
  80. package/lib/esm/agents/ai-agent.d.ts +9 -8
  81. package/lib/esm/agents/ai-agent.js +20 -14
  82. package/lib/esm/agents/mcp-agent.d.ts +10 -4
  83. package/lib/esm/agents/mcp-agent.js +12 -6
  84. package/lib/esm/agents/memory.d.ts +1 -1
  85. package/lib/esm/agents/team-agent.d.ts +28 -0
  86. package/lib/esm/agents/team-agent.js +89 -0
  87. package/lib/esm/agents/user-agent.d.ts +9 -10
  88. package/lib/esm/agents/user-agent.js +11 -14
  89. package/lib/{cjs/execution-engine/execution-engine.d.ts → esm/aigne/aigne.d.ts} +9 -12
  90. package/lib/esm/{execution-engine/execution-engine.js → aigne/aigne.js} +18 -18
  91. package/lib/esm/{execution-engine → aigne}/context.d.ts +31 -32
  92. package/lib/esm/{execution-engine → aigne}/context.js +28 -38
  93. package/lib/esm/aigne/index.d.ts +4 -0
  94. package/lib/esm/aigne/index.js +4 -0
  95. package/lib/esm/{execution-engine → aigne}/usage.d.ts +1 -1
  96. package/lib/esm/client/client.d.ts +19 -0
  97. package/lib/esm/client/client.js +45 -0
  98. package/lib/esm/index.d.ts +2 -1
  99. package/lib/esm/index.js +2 -1
  100. package/lib/esm/loader/agent-js.d.ts +2 -2
  101. package/lib/esm/loader/agent-js.js +4 -5
  102. package/lib/esm/loader/agent-yaml.d.ts +8 -5
  103. package/lib/esm/loader/agent-yaml.js +21 -2
  104. package/lib/esm/loader/index.d.ts +5 -5
  105. package/lib/esm/loader/index.js +8 -19
  106. package/lib/esm/models/chat-model.d.ts +1 -1
  107. package/lib/esm/models/claude-chat-model.d.ts +3 -1
  108. package/lib/esm/models/claude-chat-model.js +75 -60
  109. package/lib/esm/models/openai-chat-model.d.ts +3 -3
  110. package/lib/esm/models/openai-chat-model.js +1 -3
  111. package/lib/esm/prompt/prompt-builder.d.ts +1 -1
  112. package/lib/esm/prompt/prompt-builder.js +3 -3
  113. package/lib/esm/server/error.d.ts +4 -0
  114. package/lib/esm/server/error.js +7 -0
  115. package/lib/esm/server/server.d.ts +54 -0
  116. package/lib/esm/server/server.js +123 -0
  117. package/lib/esm/utils/camelize.d.ts +13 -0
  118. package/lib/esm/utils/camelize.js +10 -0
  119. package/lib/esm/utils/event-stream.d.ts +11 -0
  120. package/lib/esm/utils/event-stream.js +85 -0
  121. package/lib/esm/utils/mcp-utils.js +4 -1
  122. package/lib/esm/utils/stream-utils.d.ts +10 -3
  123. package/lib/esm/utils/stream-utils.js +49 -35
  124. package/lib/esm/utils/type-utils.d.ts +4 -2
  125. package/lib/esm/utils/type-utils.js +9 -2
  126. package/package.json +15 -5
  127. package/lib/cjs/execution-engine/index.d.ts +0 -4
  128. package/lib/cjs/execution-engine/utils.d.ts +0 -4
  129. package/lib/cjs/execution-engine/utils.js +0 -34
  130. package/lib/dts/execution-engine/index.d.ts +0 -4
  131. package/lib/dts/execution-engine/utils.d.ts +0 -4
  132. package/lib/esm/execution-engine/index.d.ts +0 -4
  133. package/lib/esm/execution-engine/index.js +0 -4
  134. package/lib/esm/execution-engine/utils.d.ts +0 -4
  135. package/lib/esm/execution-engine/utils.js +0 -30
  136. /package/lib/cjs/{execution-engine → aigne}/message-queue.d.ts +0 -0
  137. /package/lib/cjs/{execution-engine → aigne}/message-queue.js +0 -0
  138. /package/lib/cjs/{execution-engine → aigne}/usage.js +0 -0
  139. /package/lib/dts/{execution-engine → aigne}/message-queue.d.ts +0 -0
  140. /package/lib/esm/{execution-engine → aigne}/message-queue.d.ts +0 -0
  141. /package/lib/esm/{execution-engine → aigne}/message-queue.js +0 -0
  142. /package/lib/esm/{execution-engine → aigne}/usage.js +0 -0
@@ -3,29 +3,29 @@ import { Agent } from "../agents/agent.js";
3
3
  import { load } from "../loader/index.js";
4
4
  import { ChatModel } from "../models/chat-model.js";
5
5
  import { checkArguments, createAccessorArray } from "../utils/type-utils.js";
6
- import { ExecutionContext } from "./context.js";
6
+ import { AIGNEContext } from "./context.js";
7
7
  import { MessageQueue } from "./message-queue.js";
8
- export class ExecutionEngine {
8
+ export class AIGNE {
9
9
  static async load({ path, ...options }) {
10
- const { model, agents, tools, ...aigne } = await load({ path });
11
- return new ExecutionEngine({
10
+ const { model, agents, skills, ...aigne } = await load({ path });
11
+ return new AIGNE({
12
12
  ...options,
13
13
  model: options.model || model,
14
14
  name: options.name || aigne.name || undefined,
15
15
  description: options.description || aigne.description || undefined,
16
16
  agents: agents.concat(options.agents ?? []),
17
- tools: tools.concat(options.tools ?? []),
17
+ skills: skills.concat(options.skills ?? []),
18
18
  });
19
19
  }
20
20
  constructor(options) {
21
21
  if (options)
22
- checkArguments("ExecutionEngine", executionEngineOptionsSchema, options);
22
+ checkArguments("AIGNE", aigneOptionsSchema, options);
23
23
  this.name = options?.name;
24
24
  this.description = options?.description;
25
25
  this.model = options?.model;
26
26
  this.limits = options?.limits;
27
- if (options?.tools?.length)
28
- this.tools.push(...options.tools);
27
+ if (options?.skills?.length)
28
+ this.skills.push(...options.skills);
29
29
  if (options?.agents?.length)
30
30
  this.addAgent(...options.agents);
31
31
  this.initProcessExitHandler();
@@ -34,24 +34,24 @@ export class ExecutionEngine {
34
34
  description;
35
35
  messageQueue = new MessageQueue();
36
36
  model;
37
- tools = createAccessorArray([], (arr, name) => arr.find((i) => i.name === name));
37
+ skills = createAccessorArray([], (arr, name) => arr.find((i) => i.name === name));
38
38
  agents = createAccessorArray([], (arr, name) => arr.find((i) => i.name === name));
39
39
  limits;
40
40
  addAgent(...agents) {
41
- checkArguments("ExecutionEngine.addAgent", executionEngineAddAgentArgsSchema, agents);
41
+ checkArguments("AIGNE.addAgent", aigneAddAgentArgsSchema, agents);
42
42
  for (const agent of agents) {
43
43
  this.agents.push(agent);
44
44
  agent.attach(this);
45
45
  }
46
46
  }
47
47
  newContext() {
48
- return new ExecutionContext(this);
48
+ return new AIGNEContext(this);
49
49
  }
50
50
  publish = ((...args) => {
51
- return new ExecutionContext(this).publish(...args);
51
+ return new AIGNEContext(this).publish(...args);
52
52
  });
53
- call = ((...args) => {
54
- return new ExecutionContext(this).call(...args);
53
+ invoke = ((...args) => {
54
+ return new AIGNEContext(this).invoke(...args);
55
55
  });
56
56
  subscribe = ((...args) => {
57
57
  return this.messageQueue.subscribe(...args);
@@ -60,7 +60,7 @@ export class ExecutionEngine {
60
60
  this.messageQueue.unsubscribe(...args);
61
61
  });
62
62
  async shutdown() {
63
- for (const tool of this.tools) {
63
+ for (const tool of this.skills) {
64
64
  await tool.shutdown();
65
65
  }
66
66
  for (const agent of this.agents) {
@@ -73,9 +73,9 @@ export class ExecutionEngine {
73
73
  process.on("exit", shutdownAndExit);
74
74
  }
75
75
  }
76
- const executionEngineOptionsSchema = z.object({
76
+ const aigneOptionsSchema = z.object({
77
77
  model: z.instanceof(ChatModel).optional(),
78
- tools: z.array(z.instanceof(Agent)).optional(),
78
+ skills: z.array(z.instanceof(Agent)).optional(),
79
79
  agents: z.array(z.instanceof(Agent)).optional(),
80
80
  });
81
- const executionEngineAddAgentArgsSchema = z.array(z.instanceof(Agent));
81
+ const aigneAddAgentArgsSchema = z.array(z.instanceof(Agent));
@@ -1,12 +1,11 @@
1
1
  import EventEmitter from "node:events";
2
- import { Agent, type AgentCallOptions, type AgentProcessAsyncGenerator, type AgentResponse, type AgentResponseStream, type FunctionAgentFn, type Message } from "../agents/agent.js";
2
+ import { Agent, type AgentInvokeOptions, type AgentProcessAsyncGenerator, type AgentResponse, type AgentResponseStream, type Message } from "../agents/agent.js";
3
3
  import { UserAgent } from "../agents/user-agent.js";
4
4
  import type { ChatModel } from "../models/chat-model.js";
5
5
  import { type OmitPropertiesFromArrayFirstElement } from "../utils/type-utils.js";
6
6
  import type { Args, Listener, TypedEventEmitter } from "../utils/typed-event-emtter.js";
7
7
  import { type MessagePayload, MessageQueue, type MessageQueueListener, type Unsubscribe } from "./message-queue.js";
8
8
  import { type ContextLimits, type ContextUsage } from "./usage.js";
9
- export type Runnable<I extends Message = Message, O extends Message = Message> = Agent<I, O> | FunctionAgentFn;
10
9
  export interface AgentEvent {
11
10
  parentContextId?: string;
12
11
  contextId: string;
@@ -27,53 +26,53 @@ export interface ContextEventMap {
27
26
  export type ContextEmitEventMap = {
28
27
  [K in keyof ContextEventMap]: OmitPropertiesFromArrayFirstElement<ContextEventMap[K], "contextId" | "parentContextId" | "timestamp">;
29
28
  };
30
- export interface CallOptions extends AgentCallOptions {
29
+ export interface InvokeOptions extends AgentInvokeOptions {
31
30
  returnActiveAgent?: boolean;
32
31
  disableTransfer?: boolean;
33
32
  }
34
33
  export interface Context extends TypedEventEmitter<ContextEventMap, ContextEmitEventMap> {
35
34
  model?: ChatModel;
36
- tools?: Agent[];
35
+ skills?: Agent[];
37
36
  usage: ContextUsage;
38
37
  limits?: ContextLimits;
39
38
  status?: "normal" | "timeout";
40
39
  /**
41
- * Create a user agent to consistently call an agent
42
- * @param agent Agent to call
40
+ * Create a user agent to consistently invoke an agent
41
+ * @param agent Agent to invoke
43
42
  * @returns User agent
44
43
  */
45
- call<I extends Message, O extends Message>(agent: Runnable<I, O>): UserAgent<I, O>;
44
+ invoke<I extends Message, O extends Message>(agent: Agent<I, O>): UserAgent<I, O>;
46
45
  /**
47
- * Call an agent with a message and return the output and the active agent
48
- * @param agent Agent to call
46
+ * Invoke an agent with a message and return the output and the active agent
47
+ * @param agent Agent to invoke
49
48
  * @param message Message to pass to the agent
50
49
  * @param options.returnActiveAgent return the active agent
51
50
  * @param options.streaming return a stream of the output
52
51
  * @returns the output of the agent and the final active agent
53
52
  */
54
- call<I extends Message, O extends Message>(agent: Runnable<I, O>, message: I | string, options: CallOptions & {
53
+ invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I | string, options: InvokeOptions & {
55
54
  returnActiveAgent: true;
56
55
  streaming?: false;
57
- }): Promise<[O, Runnable]>;
58
- call<I extends Message, O extends Message>(agent: Runnable<I, O>, message: I | string, options: CallOptions & {
56
+ }): Promise<[O, Agent]>;
57
+ invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I | string, options: InvokeOptions & {
59
58
  returnActiveAgent: true;
60
59
  streaming: true;
61
- }): Promise<[AgentResponseStream<O>, Promise<Runnable>]>;
60
+ }): Promise<[AgentResponseStream<O>, Promise<Agent>]>;
62
61
  /**
63
- * Call an agent with a message
64
- * @param agent Agent to call
62
+ * Invoke an agent with a message
63
+ * @param agent Agent to invoke
65
64
  * @param message Message to pass to the agent
66
65
  * @returns the output of the agent
67
66
  */
68
- call<I extends Message, O extends Message>(agent: Runnable<I, O>, message: I | string, options?: CallOptions & {
67
+ invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I | string, options?: InvokeOptions & {
69
68
  streaming?: false;
70
69
  }): Promise<O>;
71
- call<I extends Message, O extends Message>(agent: Runnable<I, O>, message: I | string, options: CallOptions & {
70
+ invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I | string, options: InvokeOptions & {
72
71
  streaming: true;
73
72
  }): Promise<AgentResponseStream<O>>;
74
- call<I extends Message, O extends Message>(agent: Runnable<I, O>, message?: I | string, options?: CallOptions): UserAgent<I, O> | Promise<AgentResponse<O> | [AgentResponse<O>, Runnable]>;
73
+ invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message?: I | string, options?: InvokeOptions): UserAgent<I, O> | Promise<AgentResponse<O> | [AgentResponse<O>, Agent]>;
75
74
  /**
76
- * Publish a message to a topic, the engine will call the listeners of the topic
75
+ * Publish a message to a topic, the aigne will invoke the listeners of the topic
77
76
  * @param topic topic name, or an array of topic names
78
77
  * @param payload message to publish
79
78
  */
@@ -96,21 +95,21 @@ export interface Context extends TypedEventEmitter<ContextEventMap, ContextEmitE
96
95
  }): Context;
97
96
  }
98
97
  export declare function createPublishMessage(message: string | Message, from?: Agent): Omit<MessagePayload, "context">;
99
- export declare class ExecutionContext implements Context {
100
- constructor(parent?: ConstructorParameters<typeof ExecutionContextInternal>[0]);
98
+ export declare class AIGNEContext implements Context {
99
+ constructor(parent?: ConstructorParameters<typeof AIGNEContextInternal>[0]);
101
100
  parentId?: string;
102
101
  id: string;
103
- readonly internal: ExecutionContextInternal;
102
+ readonly internal: AIGNEContextInternal;
104
103
  get model(): ChatModel | undefined;
105
- get tools(): Agent<Message, Message>[] | undefined;
104
+ get skills(): Agent<Message, Message>[] | undefined;
106
105
  get limits(): ContextLimits | undefined;
107
106
  get status(): "normal" | "timeout";
108
107
  get usage(): ContextUsage;
109
108
  newContext({ reset }?: {
110
109
  reset?: boolean;
111
- }): ExecutionContext;
112
- call: Context["call"];
113
- private onCallSuccess;
110
+ }): AIGNEContext;
111
+ invoke: Context["invoke"];
112
+ private onInvokeSuccess;
114
113
  publish: Context["publish"];
115
114
  subscribe: Context["subscribe"];
116
115
  unsubscribe: Context["unsubscribe"];
@@ -119,24 +118,24 @@ export declare class ExecutionContext implements Context {
119
118
  once<K extends keyof ContextEventMap>(eventName: K, listener: Listener<K, ContextEventMap>): this;
120
119
  off<K extends keyof ContextEventMap>(eventName: K, listener: Listener<K, ContextEventMap>): this;
121
120
  }
122
- declare class ExecutionContextInternal {
121
+ declare class AIGNEContextInternal {
123
122
  private readonly parent?;
124
- constructor(parent?: (Pick<Context, "model" | "tools" | "limits"> & {
123
+ constructor(parent?: (Pick<Context, "model" | "skills" | "limits"> & {
125
124
  messageQueue?: MessageQueue;
126
125
  }) | undefined);
127
126
  readonly messageQueue: MessageQueue;
128
127
  readonly events: EventEmitter<ContextEventMap>;
129
128
  get model(): ChatModel | undefined;
130
- get tools(): Agent<Message, Message>[] | undefined;
129
+ get skills(): Agent<Message, Message>[] | undefined;
131
130
  get limits(): ContextLimits | undefined;
132
131
  usage: ContextUsage;
133
132
  private abortController;
134
133
  private timer?;
135
134
  private initTimeout;
136
135
  get status(): "normal" | "timeout";
137
- call<I extends Message, O extends Message>(agent: Runnable<I, O>, input: I, context: Context, options?: CallOptions): AgentProcessAsyncGenerator<O & {
138
- __activeAgent__: Runnable;
136
+ invoke<I extends Message, O extends Message>(agent: Agent<I, O>, input: I, context: Context, options?: InvokeOptions): AgentProcessAsyncGenerator<O & {
137
+ __activeAgent__: Agent;
139
138
  }>;
140
- private callAgent;
139
+ private invokeAgent;
141
140
  }
142
141
  export {};
@@ -16,14 +16,14 @@ export function createPublishMessage(message, from) {
16
16
  message: createMessage(message),
17
17
  };
18
18
  }
19
- export class ExecutionContext {
19
+ export class AIGNEContext {
20
20
  constructor(parent) {
21
- if (parent instanceof ExecutionContext) {
21
+ if (parent instanceof AIGNEContext) {
22
22
  this.parentId = parent.id;
23
23
  this.internal = parent.internal;
24
24
  }
25
25
  else {
26
- this.internal = new ExecutionContextInternal(parent);
26
+ this.internal = new AIGNEContextInternal(parent);
27
27
  }
28
28
  }
29
29
  parentId;
@@ -32,8 +32,8 @@ export class ExecutionContext {
32
32
  get model() {
33
33
  return this.internal.model;
34
34
  }
35
- get tools() {
36
- return this.internal.tools;
35
+ get skills() {
36
+ return this.internal.skills;
37
37
  }
38
38
  get limits() {
39
39
  return this.internal.limits;
@@ -46,11 +46,11 @@ export class ExecutionContext {
46
46
  }
47
47
  newContext({ reset } = {}) {
48
48
  if (reset)
49
- return new ExecutionContext(this.internal);
50
- return new ExecutionContext(this);
49
+ return new AIGNEContext(this.internal);
50
+ return new AIGNEContext(this);
51
51
  }
52
- call = ((agent, message, options) => {
53
- checkArguments("ExecutionContext.call", executionContextCallArgsSchema, {
52
+ invoke = ((agent, message, options) => {
53
+ checkArguments("AIGNEContext.invoke", aigneContextInvokeArgsSchema, {
54
54
  agent,
55
55
  message,
56
56
  options,
@@ -63,10 +63,10 @@ export class ExecutionContext {
63
63
  }
64
64
  const newContext = this.newContext();
65
65
  const msg = createMessage(message);
66
- return Promise.resolve(newContext.internal.call(agent, msg, newContext, options)).then(async (response) => {
66
+ return Promise.resolve(newContext.internal.invoke(agent, msg, newContext, options)).then(async (response) => {
67
67
  if (!options?.streaming) {
68
68
  const { __activeAgent__: activeAgent, ...output } = await agentResponseStreamToObject(response);
69
- this.onCallSuccess(activeAgent, output, newContext);
69
+ this.onInvokeSuccess(activeAgent, output, newContext);
70
70
  if (options?.returnActiveAgent) {
71
71
  return [output, activeAgent];
72
72
  }
@@ -74,7 +74,7 @@ export class ExecutionContext {
74
74
  }
75
75
  const activeAgentPromise = Promise.withResolvers();
76
76
  const stream = onAgentResponseStreamEnd(asyncGeneratorToReadableStream(response), async ({ __activeAgent__: activeAgent, ...output }) => {
77
- this.onCallSuccess(activeAgent, output, newContext);
77
+ this.onInvokeSuccess(activeAgent, output, newContext);
78
78
  activeAgentPromise.resolve(activeAgent);
79
79
  }, {
80
80
  processChunk(chunk) {
@@ -96,7 +96,7 @@ export class ExecutionContext {
96
96
  return stream;
97
97
  });
98
98
  });
99
- async onCallSuccess(activeAgent, output, context) {
99
+ async onInvokeSuccess(activeAgent, output, context) {
100
100
  if (activeAgent instanceof Agent) {
101
101
  const publishTopics = typeof activeAgent.publishTopic === "function"
102
102
  ? await activeAgent.publishTopic(output)
@@ -138,7 +138,7 @@ export class ExecutionContext {
138
138
  return this;
139
139
  }
140
140
  }
141
- class ExecutionContextInternal {
141
+ class AIGNEContextInternal {
142
142
  parent;
143
143
  constructor(parent) {
144
144
  this.parent = parent;
@@ -149,8 +149,8 @@ class ExecutionContextInternal {
149
149
  get model() {
150
150
  return this.parent?.model;
151
151
  }
152
- get tools() {
153
- return this.parent?.tools;
152
+ get skills() {
153
+ return this.parent?.skills;
154
154
  }
155
155
  get limits() {
156
156
  return this.parent?.limits;
@@ -171,33 +171,23 @@ class ExecutionContextInternal {
171
171
  get status() {
172
172
  return this.abortController.signal.aborted ? "timeout" : "normal";
173
173
  }
174
- call(agent, input, context, options) {
174
+ invoke(agent, input, context, options) {
175
175
  this.initTimeout();
176
- return withAbortSignal(this.abortController.signal, new Error("ExecutionContext is timeout"), () => this.callAgent(agent, input, context, options));
176
+ return withAbortSignal(this.abortController.signal, new Error("AIGNEContext is timeout"), () => this.invokeAgent(agent, input, context, options));
177
177
  }
178
- async *callAgent(agent, input, context, options) {
178
+ async *invokeAgent(agent, input, context, options) {
179
179
  let activeAgent = agent;
180
180
  let output;
181
181
  for (;;) {
182
- let result;
183
- if (typeof activeAgent === "function") {
184
- result = await activeAgent(input, context);
185
- }
186
- else {
187
- result = {};
188
- const stream = await activeAgent.call(input, context, { streaming: true });
189
- for await (const value of readableStreamToAsyncIterator(stream)) {
190
- if (value.delta.text) {
191
- yield { delta: { text: value.delta.text } };
192
- }
193
- if (value.delta.json) {
194
- Object.assign(result, value.delta.json);
195
- }
182
+ const result = {};
183
+ const stream = await activeAgent.invoke(input, context, { streaming: true });
184
+ for await (const value of readableStreamToAsyncIterator(stream)) {
185
+ if (value.delta.text) {
186
+ yield { delta: { text: value.delta.text } };
187
+ }
188
+ if (value.delta.json) {
189
+ Object.assign(result, value.delta.json);
196
190
  }
197
- }
198
- if (result instanceof Agent) {
199
- activeAgent = result;
200
- continue;
201
191
  }
202
192
  if (!options?.disableTransfer) {
203
193
  const transferToAgent = isTransferAgentOutput(result)
@@ -242,7 +232,7 @@ async function* withAbortSignal(signal, error, fn) {
242
232
  signal.removeEventListener("abort", listener);
243
233
  }
244
234
  }
245
- const executionContextCallArgsSchema = z.object({
235
+ const aigneContextInvokeArgsSchema = z.object({
246
236
  agent: z.union([z.function(), z.instanceof(Agent)]),
247
237
  message: z.union([z.record(z.unknown()), z.string()]).optional(),
248
238
  options: z.object({ returnActiveAgent: z.boolean().optional() }).optional(),
@@ -0,0 +1,4 @@
1
+ export * from "./context.js";
2
+ export * from "./aigne.js";
3
+ export * from "./message-queue.js";
4
+ export * from "./usage.js";
@@ -0,0 +1,4 @@
1
+ export * from "./context.js";
2
+ export * from "./aigne.js";
3
+ export * from "./message-queue.js";
4
+ export * from "./usage.js";
@@ -6,6 +6,6 @@ export interface ContextUsage {
6
6
  export declare function newEmptyContextUsage(): ContextUsage;
7
7
  export interface ContextLimits {
8
8
  maxTokens?: number;
9
- maxAgentCalls?: number;
9
+ maxAgentInvokes?: number;
10
10
  timeout?: number;
11
11
  }
@@ -0,0 +1,19 @@
1
+ import type { AgentInvokeOptions, AgentResponse, AgentResponseStream, Message } from "../agents/agent.js";
2
+ export interface AIGNEClientOptions {
3
+ url: string;
4
+ }
5
+ export interface AIGNEClientInvokeOptions extends AgentInvokeOptions {
6
+ fetchOptions?: Partial<RequestInit>;
7
+ }
8
+ export declare class AIGNEClient {
9
+ options: AIGNEClientOptions;
10
+ constructor(options: AIGNEClientOptions);
11
+ invoke<I extends Message, O extends Message>(agent: string, input: I, options: AIGNEClientInvokeOptions & {
12
+ streaming: true;
13
+ }): Promise<AgentResponseStream<O>>;
14
+ invoke<I extends Message, O extends Message>(agent: string, input: I, options?: AIGNEClientInvokeOptions & {
15
+ streaming?: false;
16
+ }): Promise<O>;
17
+ invoke<I extends Message, O extends Message>(agent: string, input: I, options?: AIGNEClientInvokeOptions): Promise<AgentResponse<O>>;
18
+ fetch(...args: Parameters<typeof globalThis.fetch>): Promise<Response>;
19
+ }
@@ -0,0 +1,45 @@
1
+ import { AgentResponseStreamParser, EventStreamParser } from "../utils/event-stream.js";
2
+ import { tryOrThrow } from "../utils/type-utils.js";
3
+ export class AIGNEClient {
4
+ options;
5
+ constructor(options) {
6
+ this.options = options;
7
+ }
8
+ async invoke(agent, input, options) {
9
+ const response = await this.fetch(this.options.url, {
10
+ ...options?.fetchOptions,
11
+ method: "POST",
12
+ headers: {
13
+ "Content-Type": "application/json",
14
+ ...options?.fetchOptions?.headers,
15
+ },
16
+ body: JSON.stringify({ agent, input, options }),
17
+ });
18
+ if (!options?.streaming) {
19
+ return await response.json();
20
+ }
21
+ const stream = response.body;
22
+ if (!stream)
23
+ throw new Error("Response body is not a stream");
24
+ return stream
25
+ .pipeThrough(new TextDecoderStream())
26
+ .pipeThrough(new EventStreamParser())
27
+ .pipeThrough(new AgentResponseStreamParser());
28
+ }
29
+ async fetch(...args) {
30
+ const result = await globalThis.fetch(...args);
31
+ if (!result.ok) {
32
+ let message;
33
+ try {
34
+ const text = await result.text();
35
+ const json = tryOrThrow(() => JSON.parse(text));
36
+ message = json?.error?.message || text;
37
+ }
38
+ catch {
39
+ // ignore
40
+ }
41
+ throw new Error(`Failed to fetch url ${args[0]} with status ${result.status}: ${message}`);
42
+ }
43
+ return result;
44
+ }
45
+ }
@@ -2,9 +2,10 @@ export * from "./agents/agent.js";
2
2
  export * from "./agents/ai-agent.js";
3
3
  export * from "./agents/mcp-agent.js";
4
4
  export * from "./agents/memory.js";
5
+ export * from "./agents/team-agent.js";
5
6
  export * from "./agents/types.js";
6
7
  export * from "./agents/user-agent.js";
7
- export * from "./execution-engine/index.js";
8
+ export * from "./aigne/index.js";
8
9
  export * from "./models/chat-model.js";
9
10
  export * from "./prompt/prompt-builder.js";
10
11
  export * from "./prompt/template.js";
package/lib/esm/index.js CHANGED
@@ -2,9 +2,10 @@ export * from "./agents/agent.js";
2
2
  export * from "./agents/ai-agent.js";
3
3
  export * from "./agents/mcp-agent.js";
4
4
  export * from "./agents/memory.js";
5
+ export * from "./agents/team-agent.js";
5
6
  export * from "./agents/types.js";
6
7
  export * from "./agents/user-agent.js";
7
- export * from "./execution-engine/index.js";
8
+ export * from "./aigne/index.js";
8
9
  export * from "./models/chat-model.js";
9
10
  export * from "./prompt/prompt-builder.js";
10
11
  export * from "./prompt/template.js";
@@ -4,12 +4,12 @@ export declare function loadAgentFromJsFile(path: string): Promise<{
4
4
  name: string;
5
5
  fn: (args_0: Message) => Message;
6
6
  description?: string | undefined;
7
- input_schema?: ZodObject<Record<string, ZodType<any, z.ZodTypeDef, any>>, z.UnknownKeysParam, z.ZodTypeAny, {
7
+ inputSchema?: ZodObject<Record<string, ZodType<any, z.ZodTypeDef, any>>, z.UnknownKeysParam, z.ZodTypeAny, {
8
8
  [x: string]: any;
9
9
  }, {
10
10
  [x: string]: any;
11
11
  }> | undefined;
12
- output_schema?: ZodObject<Record<string, ZodType<any, z.ZodTypeDef, any>>, z.UnknownKeysParam, z.ZodTypeAny, {
12
+ outputSchema?: ZodObject<Record<string, ZodType<any, z.ZodTypeDef, any>>, z.UnknownKeysParam, z.ZodTypeAny, {
13
13
  [x: string]: any;
14
14
  }, {
15
15
  [x: string]: any;
@@ -1,5 +1,6 @@
1
1
  import { jsonSchemaToZod } from "@aigne/json-schema-to-zod";
2
2
  import { z } from "zod";
3
+ import { customCamelize } from "../utils/camelize.js";
3
4
  import { tryOrThrow } from "../utils/type-utils.js";
4
5
  import { inputOutputSchema } from "./schema.js";
5
6
  const agentJsFileSchema = z.object({
@@ -21,11 +22,9 @@ export async function loadAgentFromJsFile(path) {
21
22
  if (typeof agent !== "function") {
22
23
  throw new Error(`Agent file ${path} must export a default function, but got ${typeof agent}`);
23
24
  }
24
- return tryOrThrow(() => agentJsFileSchema.parse({
25
+ return tryOrThrow(() => customCamelize(agentJsFileSchema.parse({
26
+ ...agent,
25
27
  name: agent.agent_name || agent.name,
26
- description: agent.description,
27
- input_schema: agent.input_schema,
28
- output_schema: agent.output_schema,
29
28
  fn: agent,
30
- }), (error) => new Error(`Failed to parse agent from ${path}: ${error.message}`));
29
+ }), { shallowKeys: ["input_schema", "output_schema"] }), (error) => new Error(`Failed to parse agent from ${path}: ${error.message}`));
31
30
  }
@@ -3,20 +3,23 @@ export declare function loadAgentFromYamlFile(path: string): Promise<{
3
3
  type: "ai";
4
4
  name: string;
5
5
  description?: string | undefined;
6
- tools?: string[] | undefined;
6
+ memory?: true | {
7
+ subscribeTopic?: string[] | undefined;
8
+ } | undefined;
9
+ skills?: string[] | undefined;
7
10
  instructions?: string | undefined;
8
- input_schema?: ZodObject<Record<string, ZodType<any, z.ZodTypeDef, any>>, z.UnknownKeysParam, z.ZodTypeAny, {
11
+ inputSchema?: ZodObject<Record<string, ZodType<any, z.ZodTypeDef, any>>, z.UnknownKeysParam, z.ZodTypeAny, {
9
12
  [x: string]: any;
10
13
  }, {
11
14
  [x: string]: any;
12
15
  }> | undefined;
13
- tool_choice?: "auto" | "none" | "required" | "router" | undefined;
14
- output_schema?: ZodObject<Record<string, ZodType<any, z.ZodTypeDef, any>>, z.UnknownKeysParam, z.ZodTypeAny, {
16
+ toolChoice?: "auto" | "none" | "required" | "router" | undefined;
17
+ outputSchema?: ZodObject<Record<string, ZodType<any, z.ZodTypeDef, any>>, z.UnknownKeysParam, z.ZodTypeAny, {
15
18
  [x: string]: any;
16
19
  }, {
17
20
  [x: string]: any;
18
21
  }> | undefined;
19
- output_key?: string | undefined;
22
+ outputKey?: string | undefined;
20
23
  } | {
21
24
  type: "mcp";
22
25
  url?: string | undefined;
@@ -2,6 +2,7 @@ import { readFile } from "node:fs/promises";
2
2
  import { jsonSchemaToZod } from "@aigne/json-schema-to-zod";
3
3
  import { parse } from "yaml";
4
4
  import { z } from "zod";
5
+ import { customCamelize } from "../utils/camelize.js";
5
6
  import { tryOrThrow } from "../utils/type-utils.js";
6
7
  import { inputOutputSchema } from "./schema.js";
7
8
  const agentFileSchema = z.discriminatedUnion("type", [
@@ -26,7 +27,7 @@ const agentFileSchema = z.discriminatedUnion("type", [
26
27
  .string()
27
28
  .nullish()
28
29
  .transform((v) => v ?? undefined),
29
- tools: z
30
+ skills: z
30
31
  .array(z.string())
31
32
  .nullish()
32
33
  .transform((v) => v ?? undefined),
@@ -34,6 +35,18 @@ const agentFileSchema = z.discriminatedUnion("type", [
34
35
  .union([z.literal("auto"), z.literal("none"), z.literal("required"), z.literal("router")])
35
36
  .nullish()
36
37
  .transform((v) => v ?? undefined),
38
+ memory: z
39
+ .union([
40
+ z.boolean(),
41
+ z.object({
42
+ subscribe_topic: z
43
+ .array(z.string())
44
+ .nullish()
45
+ .transform((v) => v ?? undefined),
46
+ }),
47
+ ])
48
+ .nullish()
49
+ .transform((v) => v || undefined),
37
50
  }),
38
51
  z.object({
39
52
  type: z.literal("mcp"),
@@ -54,6 +67,12 @@ const agentFileSchema = z.discriminatedUnion("type", [
54
67
  export async function loadAgentFromYamlFile(path) {
55
68
  const raw = await tryOrThrow(() => readFile(path, "utf8"), (error) => new Error(`Failed to load agent definition from ${path}: ${error.message}`));
56
69
  const json = await tryOrThrow(() => parse(raw), (error) => new Error(`Failed to parse agent definition from ${path}: ${error.message}`));
57
- const agent = tryOrThrow(() => agentFileSchema.parse({ ...json, type: json.type ?? "ai" }), (error) => new Error(`Failed to validate agent definition from ${path}: ${error.message}`));
70
+ const agent = tryOrThrow(() => customCamelize(agentFileSchema.parse({
71
+ ...json,
72
+ type: json.type ?? "ai",
73
+ skills: json.skills ?? json.tools,
74
+ }), {
75
+ shallowKeys: ["input_schema", "output_schema"],
76
+ }), (error) => new Error(`Failed to validate agent definition from ${path}: ${error.message}`));
58
77
  return agent;
59
78
  }