@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
@@ -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 {};
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.ExecutionContext = void 0;
6
+ exports.AIGNEContext = void 0;
7
7
  exports.createPublishMessage = createPublishMessage;
8
8
  const node_events_1 = __importDefault(require("node:events"));
9
9
  const uuid_1 = require("uuid");
@@ -23,14 +23,14 @@ function createPublishMessage(message, from) {
23
23
  message: (0, prompt_builder_js_1.createMessage)(message),
24
24
  };
25
25
  }
26
- class ExecutionContext {
26
+ class AIGNEContext {
27
27
  constructor(parent) {
28
- if (parent instanceof ExecutionContext) {
28
+ if (parent instanceof AIGNEContext) {
29
29
  this.parentId = parent.id;
30
30
  this.internal = parent.internal;
31
31
  }
32
32
  else {
33
- this.internal = new ExecutionContextInternal(parent);
33
+ this.internal = new AIGNEContextInternal(parent);
34
34
  }
35
35
  }
36
36
  parentId;
@@ -39,8 +39,8 @@ class ExecutionContext {
39
39
  get model() {
40
40
  return this.internal.model;
41
41
  }
42
- get tools() {
43
- return this.internal.tools;
42
+ get skills() {
43
+ return this.internal.skills;
44
44
  }
45
45
  get limits() {
46
46
  return this.internal.limits;
@@ -53,11 +53,11 @@ class ExecutionContext {
53
53
  }
54
54
  newContext({ reset } = {}) {
55
55
  if (reset)
56
- return new ExecutionContext(this.internal);
57
- return new ExecutionContext(this);
56
+ return new AIGNEContext(this.internal);
57
+ return new AIGNEContext(this);
58
58
  }
59
- call = ((agent, message, options) => {
60
- (0, type_utils_js_1.checkArguments)("ExecutionContext.call", executionContextCallArgsSchema, {
59
+ invoke = ((agent, message, options) => {
60
+ (0, type_utils_js_1.checkArguments)("AIGNEContext.invoke", aigneContextInvokeArgsSchema, {
61
61
  agent,
62
62
  message,
63
63
  options,
@@ -70,10 +70,10 @@ class ExecutionContext {
70
70
  }
71
71
  const newContext = this.newContext();
72
72
  const msg = (0, prompt_builder_js_1.createMessage)(message);
73
- return Promise.resolve(newContext.internal.call(agent, msg, newContext, options)).then(async (response) => {
73
+ return Promise.resolve(newContext.internal.invoke(agent, msg, newContext, options)).then(async (response) => {
74
74
  if (!options?.streaming) {
75
75
  const { __activeAgent__: activeAgent, ...output } = await (0, stream_utils_js_1.agentResponseStreamToObject)(response);
76
- this.onCallSuccess(activeAgent, output, newContext);
76
+ this.onInvokeSuccess(activeAgent, output, newContext);
77
77
  if (options?.returnActiveAgent) {
78
78
  return [output, activeAgent];
79
79
  }
@@ -81,7 +81,7 @@ class ExecutionContext {
81
81
  }
82
82
  const activeAgentPromise = Promise.withResolvers();
83
83
  const stream = (0, stream_utils_js_1.onAgentResponseStreamEnd)((0, stream_utils_js_1.asyncGeneratorToReadableStream)(response), async ({ __activeAgent__: activeAgent, ...output }) => {
84
- this.onCallSuccess(activeAgent, output, newContext);
84
+ this.onInvokeSuccess(activeAgent, output, newContext);
85
85
  activeAgentPromise.resolve(activeAgent);
86
86
  }, {
87
87
  processChunk(chunk) {
@@ -103,7 +103,7 @@ class ExecutionContext {
103
103
  return stream;
104
104
  });
105
105
  });
106
- async onCallSuccess(activeAgent, output, context) {
106
+ async onInvokeSuccess(activeAgent, output, context) {
107
107
  if (activeAgent instanceof agent_js_1.Agent) {
108
108
  const publishTopics = typeof activeAgent.publishTopic === "function"
109
109
  ? await activeAgent.publishTopic(output)
@@ -145,8 +145,8 @@ class ExecutionContext {
145
145
  return this;
146
146
  }
147
147
  }
148
- exports.ExecutionContext = ExecutionContext;
149
- class ExecutionContextInternal {
148
+ exports.AIGNEContext = AIGNEContext;
149
+ class AIGNEContextInternal {
150
150
  parent;
151
151
  constructor(parent) {
152
152
  this.parent = parent;
@@ -157,8 +157,8 @@ class ExecutionContextInternal {
157
157
  get model() {
158
158
  return this.parent?.model;
159
159
  }
160
- get tools() {
161
- return this.parent?.tools;
160
+ get skills() {
161
+ return this.parent?.skills;
162
162
  }
163
163
  get limits() {
164
164
  return this.parent?.limits;
@@ -179,33 +179,23 @@ class ExecutionContextInternal {
179
179
  get status() {
180
180
  return this.abortController.signal.aborted ? "timeout" : "normal";
181
181
  }
182
- call(agent, input, context, options) {
182
+ invoke(agent, input, context, options) {
183
183
  this.initTimeout();
184
- return withAbortSignal(this.abortController.signal, new Error("ExecutionContext is timeout"), () => this.callAgent(agent, input, context, options));
184
+ return withAbortSignal(this.abortController.signal, new Error("AIGNEContext is timeout"), () => this.invokeAgent(agent, input, context, options));
185
185
  }
186
- async *callAgent(agent, input, context, options) {
186
+ async *invokeAgent(agent, input, context, options) {
187
187
  let activeAgent = agent;
188
188
  let output;
189
189
  for (;;) {
190
- let result;
191
- if (typeof activeAgent === "function") {
192
- result = await activeAgent(input, context);
193
- }
194
- else {
195
- result = {};
196
- const stream = await activeAgent.call(input, context, { streaming: true });
197
- for await (const value of (0, stream_utils_js_1.readableStreamToAsyncIterator)(stream)) {
198
- if (value.delta.text) {
199
- yield { delta: { text: value.delta.text } };
200
- }
201
- if (value.delta.json) {
202
- Object.assign(result, value.delta.json);
203
- }
190
+ const result = {};
191
+ const stream = await activeAgent.invoke(input, context, { streaming: true });
192
+ for await (const value of (0, stream_utils_js_1.readableStreamToAsyncIterator)(stream)) {
193
+ if (value.delta.text) {
194
+ yield { delta: { text: value.delta.text } };
195
+ }
196
+ if (value.delta.json) {
197
+ Object.assign(result, value.delta.json);
204
198
  }
205
- }
206
- if (result instanceof agent_js_1.Agent) {
207
- activeAgent = result;
208
- continue;
209
199
  }
210
200
  if (!options?.disableTransfer) {
211
201
  const transferToAgent = (0, types_js_1.isTransferAgentOutput)(result)
@@ -250,7 +240,7 @@ async function* withAbortSignal(signal, error, fn) {
250
240
  signal.removeEventListener("abort", listener);
251
241
  }
252
242
  }
253
- const executionContextCallArgsSchema = zod_1.z.object({
243
+ const aigneContextInvokeArgsSchema = zod_1.z.object({
254
244
  agent: zod_1.z.union([zod_1.z.function(), zod_1.z.instanceof(agent_js_1.Agent)]),
255
245
  message: zod_1.z.union([zod_1.z.record(zod_1.z.unknown()), zod_1.z.string()]).optional(),
256
246
  options: zod_1.z.object({ returnActiveAgent: zod_1.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";
@@ -15,6 +15,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./context.js"), exports);
18
- __exportStar(require("./execution-engine.js"), exports);
18
+ __exportStar(require("./aigne.js"), exports);
19
19
  __exportStar(require("./message-queue.js"), exports);
20
- __exportStar(require("./utils.js"), exports);
20
+ __exportStar(require("./usage.js"), exports);
@@ -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,49 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AIGNEClient = void 0;
4
+ const event_stream_js_1 = require("../utils/event-stream.js");
5
+ const type_utils_js_1 = require("../utils/type-utils.js");
6
+ class AIGNEClient {
7
+ options;
8
+ constructor(options) {
9
+ this.options = options;
10
+ }
11
+ async invoke(agent, input, options) {
12
+ const response = await this.fetch(this.options.url, {
13
+ ...options?.fetchOptions,
14
+ method: "POST",
15
+ headers: {
16
+ "Content-Type": "application/json",
17
+ ...options?.fetchOptions?.headers,
18
+ },
19
+ body: JSON.stringify({ agent, input, options }),
20
+ });
21
+ if (!options?.streaming) {
22
+ return await response.json();
23
+ }
24
+ const stream = response.body;
25
+ if (!stream)
26
+ throw new Error("Response body is not a stream");
27
+ return stream
28
+ .pipeThrough(new TextDecoderStream())
29
+ .pipeThrough(new event_stream_js_1.EventStreamParser())
30
+ .pipeThrough(new event_stream_js_1.AgentResponseStreamParser());
31
+ }
32
+ async fetch(...args) {
33
+ const result = await globalThis.fetch(...args);
34
+ if (!result.ok) {
35
+ let message;
36
+ try {
37
+ const text = await result.text();
38
+ const json = (0, type_utils_js_1.tryOrThrow)(() => JSON.parse(text));
39
+ message = json?.error?.message || text;
40
+ }
41
+ catch {
42
+ // ignore
43
+ }
44
+ throw new Error(`Failed to fetch url ${args[0]} with status ${result.status}: ${message}`);
45
+ }
46
+ return result;
47
+ }
48
+ }
49
+ exports.AIGNEClient = AIGNEClient;
@@ -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/cjs/index.js CHANGED
@@ -18,9 +18,10 @@ __exportStar(require("./agents/agent.js"), exports);
18
18
  __exportStar(require("./agents/ai-agent.js"), exports);
19
19
  __exportStar(require("./agents/mcp-agent.js"), exports);
20
20
  __exportStar(require("./agents/memory.js"), exports);
21
+ __exportStar(require("./agents/team-agent.js"), exports);
21
22
  __exportStar(require("./agents/types.js"), exports);
22
23
  __exportStar(require("./agents/user-agent.js"), exports);
23
- __exportStar(require("./execution-engine/index.js"), exports);
24
+ __exportStar(require("./aigne/index.js"), exports);
24
25
  __exportStar(require("./models/chat-model.js"), exports);
25
26
  __exportStar(require("./prompt/prompt-builder.js"), exports);
26
27
  __exportStar(require("./prompt/template.js"), exports);
@@ -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;
@@ -36,6 +36,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
36
36
  exports.loadAgentFromJsFile = loadAgentFromJsFile;
37
37
  const json_schema_to_zod_1 = require("@aigne/json-schema-to-zod");
38
38
  const zod_1 = require("zod");
39
+ const camelize_js_1 = require("../utils/camelize.js");
39
40
  const type_utils_js_1 = require("../utils/type-utils.js");
40
41
  const schema_js_1 = require("./schema.js");
41
42
  const agentJsFileSchema = zod_1.z.object({
@@ -57,11 +58,9 @@ async function loadAgentFromJsFile(path) {
57
58
  if (typeof agent !== "function") {
58
59
  throw new Error(`Agent file ${path} must export a default function, but got ${typeof agent}`);
59
60
  }
60
- return (0, type_utils_js_1.tryOrThrow)(() => agentJsFileSchema.parse({
61
+ return (0, type_utils_js_1.tryOrThrow)(() => (0, camelize_js_1.customCamelize)(agentJsFileSchema.parse({
62
+ ...agent,
61
63
  name: agent.agent_name || agent.name,
62
- description: agent.description,
63
- input_schema: agent.input_schema,
64
- output_schema: agent.output_schema,
65
64
  fn: agent,
66
- }), (error) => new Error(`Failed to parse agent from ${path}: ${error.message}`));
65
+ }), { shallowKeys: ["input_schema", "output_schema"] }), (error) => new Error(`Failed to parse agent from ${path}: ${error.message}`));
67
66
  }
@@ -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;
@@ -5,6 +5,7 @@ const promises_1 = require("node:fs/promises");
5
5
  const json_schema_to_zod_1 = require("@aigne/json-schema-to-zod");
6
6
  const yaml_1 = require("yaml");
7
7
  const zod_1 = require("zod");
8
+ const camelize_js_1 = require("../utils/camelize.js");
8
9
  const type_utils_js_1 = require("../utils/type-utils.js");
9
10
  const schema_js_1 = require("./schema.js");
10
11
  const agentFileSchema = zod_1.z.discriminatedUnion("type", [
@@ -29,7 +30,7 @@ const agentFileSchema = zod_1.z.discriminatedUnion("type", [
29
30
  .string()
30
31
  .nullish()
31
32
  .transform((v) => v ?? undefined),
32
- tools: zod_1.z
33
+ skills: zod_1.z
33
34
  .array(zod_1.z.string())
34
35
  .nullish()
35
36
  .transform((v) => v ?? undefined),
@@ -37,6 +38,18 @@ const agentFileSchema = zod_1.z.discriminatedUnion("type", [
37
38
  .union([zod_1.z.literal("auto"), zod_1.z.literal("none"), zod_1.z.literal("required"), zod_1.z.literal("router")])
38
39
  .nullish()
39
40
  .transform((v) => v ?? undefined),
41
+ memory: zod_1.z
42
+ .union([
43
+ zod_1.z.boolean(),
44
+ zod_1.z.object({
45
+ subscribe_topic: zod_1.z
46
+ .array(zod_1.z.string())
47
+ .nullish()
48
+ .transform((v) => v ?? undefined),
49
+ }),
50
+ ])
51
+ .nullish()
52
+ .transform((v) => v || undefined),
40
53
  }),
41
54
  zod_1.z.object({
42
55
  type: zod_1.z.literal("mcp"),
@@ -57,6 +70,12 @@ const agentFileSchema = zod_1.z.discriminatedUnion("type", [
57
70
  async function loadAgentFromYamlFile(path) {
58
71
  const raw = await (0, type_utils_js_1.tryOrThrow)(() => (0, promises_1.readFile)(path, "utf8"), (error) => new Error(`Failed to load agent definition from ${path}: ${error.message}`));
59
72
  const json = await (0, type_utils_js_1.tryOrThrow)(() => (0, yaml_1.parse)(raw), (error) => new Error(`Failed to parse agent definition from ${path}: ${error.message}`));
60
- const agent = (0, type_utils_js_1.tryOrThrow)(() => agentFileSchema.parse({ ...json, type: json.type ?? "ai" }), (error) => new Error(`Failed to validate agent definition from ${path}: ${error.message}`));
73
+ const agent = (0, type_utils_js_1.tryOrThrow)(() => (0, camelize_js_1.customCamelize)(agentFileSchema.parse({
74
+ ...json,
75
+ type: json.type ?? "ai",
76
+ skills: json.skills ?? json.tools,
77
+ }), {
78
+ shallowKeys: ["input_schema", "output_schema"],
79
+ }), (error) => new Error(`Failed to validate agent definition from ${path}: ${error.message}`));
61
80
  return agent;
62
81
  }
@@ -7,7 +7,7 @@ export interface LoadOptions {
7
7
  export declare function load(options: LoadOptions): Promise<{
8
8
  model: ChatModel | undefined;
9
9
  agents: Agent<import("../agents/agent.js").Message, import("../agents/agent.js").Message>[];
10
- tools: Agent<import("../agents/agent.js").Message, import("../agents/agent.js").Message>[];
10
+ skills: Agent<import("../agents/agent.js").Message, import("../agents/agent.js").Message>[];
11
11
  description?: string | null | undefined;
12
12
  name?: string | null | undefined;
13
13
  chat_model?: {
@@ -61,11 +61,11 @@ declare const aigneFileSchema: z.ZodObject<{
61
61
  presence_penalty?: number | null | undefined;
62
62
  } | null | undefined>;
63
63
  agents: z.ZodOptional<z.ZodNullable<z.ZodArray<z.ZodString, "many">>>;
64
- tools: z.ZodOptional<z.ZodNullable<z.ZodArray<z.ZodString, "many">>>;
64
+ skills: z.ZodOptional<z.ZodNullable<z.ZodArray<z.ZodString, "many">>>;
65
65
  }, "strip", z.ZodTypeAny, {
66
66
  description?: string | null | undefined;
67
- tools?: string[] | null | undefined;
68
67
  name?: string | null | undefined;
68
+ skills?: string[] | null | undefined;
69
69
  chat_model?: {
70
70
  name?: string | null | undefined;
71
71
  temperature?: number | null | undefined;
@@ -77,8 +77,8 @@ declare const aigneFileSchema: z.ZodObject<{
77
77
  agents?: string[] | null | undefined;
78
78
  }, {
79
79
  description?: string | null | undefined;
80
- tools?: string[] | null | undefined;
81
80
  name?: string | null | undefined;
81
+ skills?: string[] | null | undefined;
82
82
  chat_model?: string | {
83
83
  name?: string | null | undefined;
84
84
  temperature?: number | null | undefined;
@@ -91,8 +91,8 @@ declare const aigneFileSchema: z.ZodObject<{
91
91
  }>;
92
92
  export declare function loadAIGNEFile(path: string): Promise<{
93
93
  description?: string | null | undefined;
94
- tools?: string[] | null | undefined;
95
94
  name?: string | null | undefined;
95
+ skills?: string[] | null | undefined;
96
96
  chat_model?: {
97
97
  name?: string | null | undefined;
98
98
  temperature?: number | null | undefined;
@@ -28,37 +28,26 @@ async function load(options) {
28
28
  const rootDir = (0, node_path_1.dirname)(aigneFilePath);
29
29
  const aigne = await loadAIGNEFile(aigneFilePath);
30
30
  const agents = await Promise.all((aigne.agents ?? []).map((filename) => loadAgent((0, node_path_1.join)(rootDir, filename))));
31
- const tools = await Promise.all((aigne.tools ?? []).map((filename) => loadAgent((0, node_path_1.join)(rootDir, filename))));
31
+ const skills = await Promise.all((aigne.skills ?? []).map((filename) => loadAgent((0, node_path_1.join)(rootDir, filename))));
32
32
  return {
33
33
  ...aigne,
34
34
  model: await loadModel(aigne.chat_model),
35
35
  agents,
36
- tools,
36
+ skills,
37
37
  };
38
38
  }
39
39
  async function loadAgent(path) {
40
40
  if ((0, node_path_1.extname)(path) === ".js") {
41
41
  const agent = await (0, agent_js_js_1.loadAgentFromJsFile)(path);
42
- return agent_js_1.FunctionAgent.from({
43
- name: agent.name,
44
- description: agent.description,
45
- inputSchema: agent.input_schema,
46
- outputSchema: agent.output_schema,
47
- fn: agent.fn,
48
- });
42
+ return agent_js_1.FunctionAgent.from(agent);
49
43
  }
50
44
  if ((0, node_path_1.extname)(path) === ".yaml" || (0, node_path_1.extname)(path) === ".yml") {
51
45
  const agent = await (0, agent_yaml_js_1.loadAgentFromYamlFile)(path);
52
46
  if (agent.type === "ai") {
53
47
  return ai_agent_js_1.AIAgent.from({
54
- name: agent.name,
55
- description: agent.description,
56
- instructions: agent.instructions,
57
- inputSchema: agent.input_schema,
58
- outputSchema: agent.output_schema,
59
- outputKey: agent.output_key,
60
- tools: await Promise.all((agent.tools ?? []).map((filename) => loadAgent((0, node_path_1.join)((0, node_path_1.dirname)(path), filename)))),
61
- toolChoice: agent.tool_choice,
48
+ ...agent,
49
+ skills: agent.skills &&
50
+ (await Promise.all(agent.skills.map((filename) => loadAgent((0, node_path_1.join)((0, node_path_1.dirname)(path), filename))))),
62
51
  });
63
52
  }
64
53
  if (agent.type === "mcp") {
@@ -123,12 +112,12 @@ const aigneFileSchema = zod_1.z.object({
123
112
  .nullish()
124
113
  .transform((v) => (typeof v === "string" ? { name: v } : v)),
125
114
  agents: zod_1.z.array(zod_1.z.string()).nullish(),
126
- tools: zod_1.z.array(zod_1.z.string()).nullish(),
115
+ skills: zod_1.z.array(zod_1.z.string()).nullish(),
127
116
  });
128
117
  async function loadAIGNEFile(path) {
129
118
  const raw = await (0, type_utils_js_1.tryOrThrow)(() => (0, promises_1.readFile)(path, "utf8"), (error) => new Error(`Failed to load aigne.yaml from ${path}: ${error.message}`));
130
119
  const json = await (0, type_utils_js_1.tryOrThrow)(() => (0, yaml_1.parse)(raw), (error) => new Error(`Failed to parse aigne.yaml from ${path}: ${error.message}`));
131
- const agent = (0, type_utils_js_1.tryOrThrow)(() => aigneFileSchema.parse(json), (error) => new Error(`Failed to validate aigne.yaml from ${path}: ${error.message}`));
120
+ const agent = (0, type_utils_js_1.tryOrThrow)(() => aigneFileSchema.parse({ ...json, skills: json.skills ?? json.tools }), (error) => new Error(`Failed to validate aigne.yaml from ${path}: ${error.message}`));
132
121
  return agent;
133
122
  }
134
123
  async function getAIGNEFilePath(path) {