@aigne/core 1.13.0 → 1.14.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 (195) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/README.md +13 -26
  3. package/README.zh.md +24 -37
  4. package/lib/cjs/agents/agent.d.ts +522 -15
  5. package/lib/cjs/agents/agent.js +357 -36
  6. package/lib/cjs/agents/ai-agent.d.ts +210 -52
  7. package/lib/cjs/agents/ai-agent.js +182 -24
  8. package/lib/cjs/agents/mcp-agent.d.ts +112 -0
  9. package/lib/cjs/agents/mcp-agent.js +79 -1
  10. package/lib/cjs/agents/team-agent.d.ts +99 -0
  11. package/lib/cjs/agents/team-agent.js +94 -0
  12. package/lib/cjs/agents/user-agent.d.ts +6 -4
  13. package/lib/cjs/agents/user-agent.js +16 -5
  14. package/lib/cjs/aigne/aigne.d.ts +263 -16
  15. package/lib/cjs/aigne/aigne.js +130 -20
  16. package/lib/cjs/aigne/context.d.ts +24 -8
  17. package/lib/cjs/aigne/context.js +8 -22
  18. package/lib/cjs/aigne/message-queue.d.ts +26 -4
  19. package/lib/cjs/aigne/message-queue.js +42 -7
  20. package/lib/cjs/aigne/usage.d.ts +9 -0
  21. package/lib/cjs/aigne/usage.js +3 -0
  22. package/lib/cjs/client/client.d.ts +81 -3
  23. package/lib/cjs/client/client.js +38 -0
  24. package/lib/cjs/client/index.d.ts +1 -0
  25. package/lib/cjs/client/index.js +17 -0
  26. package/lib/cjs/index.d.ts +0 -1
  27. package/lib/cjs/index.js +0 -1
  28. package/lib/cjs/loader/agent-js.d.ts +1 -1
  29. package/lib/cjs/loader/agent-js.js +2 -2
  30. package/lib/cjs/loader/agent-yaml.d.ts +3 -2
  31. package/lib/cjs/loader/agent-yaml.js +2 -1
  32. package/lib/cjs/loader/index.d.ts +4 -4
  33. package/lib/cjs/memory/default-memory.d.ts +16 -0
  34. package/lib/cjs/memory/default-memory.js +70 -0
  35. package/lib/cjs/memory/index.d.ts +3 -0
  36. package/lib/cjs/memory/index.js +19 -0
  37. package/lib/cjs/memory/memory.d.ts +89 -0
  38. package/lib/cjs/memory/memory.js +132 -0
  39. package/lib/cjs/memory/recorder.d.ts +86 -0
  40. package/lib/cjs/memory/recorder.js +50 -0
  41. package/lib/cjs/memory/retriever.d.ts +99 -0
  42. package/lib/cjs/memory/retriever.js +51 -0
  43. package/lib/cjs/models/bedrock-chat-model.d.ts +12 -3
  44. package/lib/cjs/models/bedrock-chat-model.js +54 -24
  45. package/lib/cjs/models/chat-model.d.ts +278 -1
  46. package/lib/cjs/models/chat-model.js +54 -0
  47. package/lib/cjs/models/claude-chat-model.d.ts +49 -3
  48. package/lib/cjs/models/claude-chat-model.js +34 -2
  49. package/lib/cjs/models/deepseek-chat-model.d.ts +16 -0
  50. package/lib/cjs/models/deepseek-chat-model.js +16 -0
  51. package/lib/cjs/models/gemini-chat-model.d.ts +14 -0
  52. package/lib/cjs/models/gemini-chat-model.js +14 -0
  53. package/lib/cjs/models/ollama-chat-model.d.ts +16 -0
  54. package/lib/cjs/models/ollama-chat-model.js +16 -0
  55. package/lib/cjs/models/open-router-chat-model.d.ts +16 -0
  56. package/lib/cjs/models/open-router-chat-model.js +16 -0
  57. package/lib/cjs/models/openai-chat-model.d.ts +67 -3
  58. package/lib/cjs/models/openai-chat-model.js +47 -2
  59. package/lib/cjs/models/xai-chat-model.d.ts +16 -0
  60. package/lib/cjs/models/xai-chat-model.js +16 -0
  61. package/lib/cjs/prompt/prompt-builder.d.ts +4 -4
  62. package/lib/cjs/prompt/prompt-builder.js +19 -18
  63. package/lib/cjs/prompt/prompts/memory-message-template.d.ts +1 -0
  64. package/lib/cjs/prompt/prompts/memory-message-template.js +10 -0
  65. package/lib/cjs/prompt/template.js +5 -1
  66. package/lib/cjs/server/error.d.ts +11 -0
  67. package/lib/cjs/server/error.js +11 -0
  68. package/lib/cjs/server/index.d.ts +2 -0
  69. package/lib/cjs/server/index.js +18 -0
  70. package/lib/cjs/server/server.d.ts +89 -8
  71. package/lib/cjs/server/server.js +58 -0
  72. package/lib/cjs/utils/fs.d.ts +2 -0
  73. package/lib/cjs/utils/fs.js +25 -0
  74. package/lib/cjs/utils/prompts.d.ts +1 -0
  75. package/lib/cjs/utils/prompts.js +11 -2
  76. package/lib/cjs/utils/type-utils.d.ts +1 -0
  77. package/lib/cjs/utils/type-utils.js +14 -0
  78. package/lib/dts/agents/agent.d.ts +522 -15
  79. package/lib/dts/agents/ai-agent.d.ts +210 -52
  80. package/lib/dts/agents/mcp-agent.d.ts +112 -0
  81. package/lib/dts/agents/team-agent.d.ts +99 -0
  82. package/lib/dts/agents/user-agent.d.ts +6 -4
  83. package/lib/dts/aigne/aigne.d.ts +263 -16
  84. package/lib/dts/aigne/context.d.ts +24 -8
  85. package/lib/dts/aigne/message-queue.d.ts +26 -4
  86. package/lib/dts/aigne/usage.d.ts +9 -0
  87. package/lib/dts/client/client.d.ts +81 -3
  88. package/lib/dts/client/index.d.ts +1 -0
  89. package/lib/dts/index.d.ts +0 -1
  90. package/lib/dts/loader/agent-js.d.ts +1 -1
  91. package/lib/dts/loader/agent-yaml.d.ts +3 -2
  92. package/lib/dts/loader/index.d.ts +4 -4
  93. package/lib/dts/memory/default-memory.d.ts +16 -0
  94. package/lib/dts/memory/index.d.ts +3 -0
  95. package/lib/dts/memory/memory.d.ts +89 -0
  96. package/lib/dts/memory/recorder.d.ts +86 -0
  97. package/lib/dts/memory/retriever.d.ts +99 -0
  98. package/lib/dts/models/bedrock-chat-model.d.ts +12 -3
  99. package/lib/dts/models/chat-model.d.ts +278 -1
  100. package/lib/dts/models/claude-chat-model.d.ts +49 -3
  101. package/lib/dts/models/deepseek-chat-model.d.ts +16 -0
  102. package/lib/dts/models/gemini-chat-model.d.ts +14 -0
  103. package/lib/dts/models/ollama-chat-model.d.ts +16 -0
  104. package/lib/dts/models/open-router-chat-model.d.ts +16 -0
  105. package/lib/dts/models/openai-chat-model.d.ts +67 -3
  106. package/lib/dts/models/xai-chat-model.d.ts +16 -0
  107. package/lib/dts/prompt/prompt-builder.d.ts +4 -4
  108. package/lib/dts/prompt/prompts/memory-message-template.d.ts +1 -0
  109. package/lib/dts/server/error.d.ts +11 -0
  110. package/lib/dts/server/index.d.ts +2 -0
  111. package/lib/dts/server/server.d.ts +89 -8
  112. package/lib/dts/utils/fs.d.ts +2 -0
  113. package/lib/dts/utils/prompts.d.ts +1 -0
  114. package/lib/dts/utils/type-utils.d.ts +1 -0
  115. package/lib/esm/agents/agent.d.ts +522 -15
  116. package/lib/esm/agents/agent.js +351 -35
  117. package/lib/esm/agents/ai-agent.d.ts +210 -52
  118. package/lib/esm/agents/ai-agent.js +183 -25
  119. package/lib/esm/agents/mcp-agent.d.ts +112 -0
  120. package/lib/esm/agents/mcp-agent.js +79 -1
  121. package/lib/esm/agents/team-agent.d.ts +99 -0
  122. package/lib/esm/agents/team-agent.js +94 -0
  123. package/lib/esm/agents/user-agent.d.ts +6 -4
  124. package/lib/esm/agents/user-agent.js +17 -6
  125. package/lib/esm/aigne/aigne.d.ts +263 -16
  126. package/lib/esm/aigne/aigne.js +132 -22
  127. package/lib/esm/aigne/context.d.ts +24 -8
  128. package/lib/esm/aigne/context.js +9 -22
  129. package/lib/esm/aigne/message-queue.d.ts +26 -4
  130. package/lib/esm/aigne/message-queue.js +42 -8
  131. package/lib/esm/aigne/usage.d.ts +9 -0
  132. package/lib/esm/aigne/usage.js +3 -0
  133. package/lib/esm/client/client.d.ts +81 -3
  134. package/lib/esm/client/client.js +38 -0
  135. package/lib/esm/client/index.d.ts +1 -0
  136. package/lib/esm/client/index.js +1 -0
  137. package/lib/esm/index.d.ts +0 -1
  138. package/lib/esm/index.js +0 -1
  139. package/lib/esm/loader/agent-js.d.ts +1 -1
  140. package/lib/esm/loader/agent-js.js +2 -2
  141. package/lib/esm/loader/agent-yaml.d.ts +3 -2
  142. package/lib/esm/loader/agent-yaml.js +2 -1
  143. package/lib/esm/loader/index.d.ts +4 -4
  144. package/lib/esm/memory/default-memory.d.ts +16 -0
  145. package/lib/esm/memory/default-memory.js +63 -0
  146. package/lib/esm/memory/index.d.ts +3 -0
  147. package/lib/esm/memory/index.js +3 -0
  148. package/lib/esm/memory/memory.d.ts +89 -0
  149. package/lib/esm/memory/memory.js +127 -0
  150. package/lib/esm/memory/recorder.d.ts +86 -0
  151. package/lib/esm/memory/recorder.js +46 -0
  152. package/lib/esm/memory/retriever.d.ts +99 -0
  153. package/lib/esm/memory/retriever.js +47 -0
  154. package/lib/esm/models/bedrock-chat-model.d.ts +12 -3
  155. package/lib/esm/models/bedrock-chat-model.js +56 -26
  156. package/lib/esm/models/chat-model.d.ts +278 -1
  157. package/lib/esm/models/chat-model.js +54 -0
  158. package/lib/esm/models/claude-chat-model.d.ts +49 -3
  159. package/lib/esm/models/claude-chat-model.js +35 -3
  160. package/lib/esm/models/deepseek-chat-model.d.ts +16 -0
  161. package/lib/esm/models/deepseek-chat-model.js +16 -0
  162. package/lib/esm/models/gemini-chat-model.d.ts +14 -0
  163. package/lib/esm/models/gemini-chat-model.js +14 -0
  164. package/lib/esm/models/ollama-chat-model.d.ts +16 -0
  165. package/lib/esm/models/ollama-chat-model.js +16 -0
  166. package/lib/esm/models/open-router-chat-model.d.ts +16 -0
  167. package/lib/esm/models/open-router-chat-model.js +16 -0
  168. package/lib/esm/models/openai-chat-model.d.ts +67 -3
  169. package/lib/esm/models/openai-chat-model.js +47 -2
  170. package/lib/esm/models/xai-chat-model.d.ts +16 -0
  171. package/lib/esm/models/xai-chat-model.js +16 -0
  172. package/lib/esm/prompt/prompt-builder.d.ts +4 -4
  173. package/lib/esm/prompt/prompt-builder.js +20 -19
  174. package/lib/esm/prompt/prompts/memory-message-template.d.ts +1 -0
  175. package/lib/esm/prompt/prompts/memory-message-template.js +7 -0
  176. package/lib/esm/prompt/template.js +5 -1
  177. package/lib/esm/server/error.d.ts +11 -0
  178. package/lib/esm/server/error.js +11 -0
  179. package/lib/esm/server/index.d.ts +2 -0
  180. package/lib/esm/server/index.js +2 -0
  181. package/lib/esm/server/server.d.ts +89 -8
  182. package/lib/esm/server/server.js +58 -0
  183. package/lib/esm/utils/fs.d.ts +2 -0
  184. package/lib/esm/utils/fs.js +21 -0
  185. package/lib/esm/utils/prompts.d.ts +1 -0
  186. package/lib/esm/utils/prompts.js +10 -2
  187. package/lib/esm/utils/type-utils.d.ts +1 -0
  188. package/lib/esm/utils/type-utils.js +13 -0
  189. package/package.json +14 -11
  190. package/lib/cjs/agents/memory.d.ts +0 -26
  191. package/lib/cjs/agents/memory.js +0 -45
  192. package/lib/dts/agents/memory.d.ts +0 -26
  193. package/lib/esm/agents/memory.d.ts +0 -26
  194. package/lib/esm/agents/memory.js +0 -41
  195. /package/{LICENSE → LICENSE.md} +0 -0
@@ -1,22 +1,44 @@
1
1
  import { EventEmitter } from "node:events";
2
2
  import type { Message } from "../agents/agent.js";
3
3
  import type { Context } from "./context.js";
4
+ /**
5
+ * @hidden
6
+ */
4
7
  export declare const UserInputTopic = "UserInputTopic";
8
+ /**
9
+ * @hidden
10
+ */
5
11
  export declare const UserOutputTopic = "UserOutputTopic";
12
+ /**
13
+ * @hidden
14
+ */
6
15
  export interface MessagePayload {
7
16
  role: "user" | "agent";
8
17
  source?: string;
9
18
  message: Message;
10
19
  context: Context;
11
20
  }
21
+ /**
22
+ * @hidden
23
+ */
24
+ export declare function toMessagePayload(payload: Omit<MessagePayload, "context"> | string | Message, options?: Partial<Pick<MessagePayload, "role" | "source">>): Omit<MessagePayload, "context">;
25
+ /**
26
+ * @hidden
27
+ */
12
28
  export type MessageQueueListener = (message: MessagePayload) => void;
29
+ /**
30
+ * @hidden
31
+ */
13
32
  export type Unsubscribe = () => void;
33
+ /**
34
+ * @hidden
35
+ */
14
36
  export declare class MessageQueue {
15
37
  events: EventEmitter<[never]>;
16
38
  publish(topic: string | string[], payload: MessagePayload): void;
17
39
  error(error: Error): void;
18
- subscribe(topic: string, listener?: undefined): Promise<MessagePayload>;
19
- subscribe(topic: string, listener: MessageQueueListener): Unsubscribe;
20
- subscribe(topic: string, listener?: MessageQueueListener): Unsubscribe | Promise<MessagePayload>;
21
- unsubscribe(topic: string, listener: MessageQueueListener): void;
40
+ subscribe(topic: string | string[], listener?: undefined): Promise<MessagePayload>;
41
+ subscribe(topic: string | string[], listener: MessageQueueListener): Unsubscribe;
42
+ subscribe(topic: string | string[], listener?: MessageQueueListener): Unsubscribe | Promise<MessagePayload>;
43
+ unsubscribe(topic: string | string[], listener: MessageQueueListener): void;
22
44
  }
@@ -1,8 +1,40 @@
1
1
  import { EventEmitter } from "node:events";
2
2
  import { z } from "zod";
3
- import { checkArguments, orArrayToArray } from "../utils/type-utils.js";
3
+ import { createMessage } from "../prompt/prompt-builder.js";
4
+ import { checkArguments, isNil, orArrayToArray } from "../utils/type-utils.js";
5
+ /**
6
+ * @hidden
7
+ */
4
8
  export const UserInputTopic = "UserInputTopic";
9
+ /**
10
+ * @hidden
11
+ */
5
12
  export const UserOutputTopic = "UserOutputTopic";
13
+ function isMessagePayload(payload) {
14
+ return (!isNil(payload) &&
15
+ typeof payload === "object" &&
16
+ "role" in payload &&
17
+ typeof payload.role === "string" &&
18
+ ["user", "agent"].includes(payload.role) &&
19
+ "message" in payload &&
20
+ !isNil(payload.message));
21
+ }
22
+ /**
23
+ * @hidden
24
+ */
25
+ export function toMessagePayload(payload, options) {
26
+ if (isMessagePayload(payload)) {
27
+ return { ...payload, message: createMessage(payload.message), ...options };
28
+ }
29
+ return {
30
+ role: options?.role || "user",
31
+ source: options?.source,
32
+ message: createMessage(payload),
33
+ };
34
+ }
35
+ /**
36
+ * @hidden
37
+ */
6
38
  export class MessageQueue {
7
39
  events = new EventEmitter();
8
40
  publish(topic, payload) {
@@ -41,23 +73,25 @@ export class MessageQueue {
41
73
  topic,
42
74
  listener,
43
75
  });
44
- this.events.off(topic, listener);
76
+ for (const t of orArrayToArray(topic)) {
77
+ this.events.off(t, listener);
78
+ }
45
79
  }
46
80
  }
47
81
  function on(events, event, listener) {
48
- events.on(event, listener);
49
- return () => events.off(event, listener);
82
+ orArrayToArray(event).forEach((e) => events.on(e, listener));
83
+ return () => orArrayToArray(event).forEach((e) => events.off(e, listener));
50
84
  }
51
85
  function once(events, event, listener) {
52
- events.once(event, listener);
53
- return () => events.off(event, listener);
86
+ orArrayToArray(event).forEach((e) => events.once(e, listener));
87
+ return () => orArrayToArray(event).forEach((e) => events.off(e, listener));
54
88
  }
55
89
  const subscribeArgsSchema = z.object({
56
- topic: z.string(),
90
+ topic: z.union([z.string(), z.array(z.string())]),
57
91
  listener: z.function(z.tuple([z.any()]), z.any()).optional(),
58
92
  });
59
93
  const unsubscribeArgsSchema = z.object({
60
- topic: z.string(),
94
+ topic: z.union([z.string(), z.array(z.string())]),
61
95
  listener: z.function(z.tuple([z.any()]), z.any()),
62
96
  });
63
97
  const publishArgsSchema = z.object({
@@ -1,9 +1,18 @@
1
+ /**
2
+ * @hidden
3
+ */
1
4
  export interface ContextUsage {
2
5
  inputTokens: number;
3
6
  outputTokens: number;
4
7
  agentCalls: number;
5
8
  }
9
+ /**
10
+ * @hidden
11
+ */
6
12
  export declare function newEmptyContextUsage(): ContextUsage;
13
+ /**
14
+ * @hidden
15
+ */
7
16
  export interface ContextLimits {
8
17
  maxTokens?: number;
9
18
  maxAgentInvokes?: number;
@@ -1,3 +1,6 @@
1
+ /**
2
+ * @hidden
3
+ */
1
4
  export function newEmptyContextUsage() {
2
5
  return {
3
6
  inputTokens: 0,
@@ -1,19 +1,97 @@
1
+ /**
2
+ * Client module used to interact with the AIGNE framework.
3
+ */
1
4
  import type { AgentInvokeOptions, AgentResponse, AgentResponseStream, Message } from "../agents/agent.js";
5
+ /**
6
+ * Configuration options for the AIGNEClient.
7
+ */
2
8
  export interface AIGNEClientOptions {
9
+ /**
10
+ * The URL of the AIGNE server to connect to.
11
+ * This should point to the base endpoint where the AIGNEServer is hosted.
12
+ */
3
13
  url: string;
4
14
  }
15
+ /**
16
+ * Options for invoking an agent through the AIGNEClient.
17
+ * Extends the standard AgentInvokeOptions with client-specific options.
18
+ */
5
19
  export interface AIGNEClientInvokeOptions extends AgentInvokeOptions {
20
+ /**
21
+ * Additional fetch API options to customize the HTTP request.
22
+ * These options will be merged with the default options used by the client.
23
+ */
6
24
  fetchOptions?: Partial<RequestInit>;
7
25
  }
26
+ /**
27
+ * Client for interacting with a remote AIGNE server.
28
+ * AIGNEClient provides a client-side interface that matches the AIGNE API,
29
+ * allowing applications to invoke agents and receive responses from a remote AIGNE instance.
30
+ *
31
+ * @example
32
+ * Here's a simple example of how to use AIGNEClient:
33
+ * {@includeCode ../../test/client/client.test.ts#example-aigne-client-simple}
34
+ *
35
+ * @example
36
+ * Here's an example of how to use AIGNEClient with streaming response:
37
+ * {@includeCode ../../test/client/client.test.ts#example-aigne-client-streaming}
38
+ */
8
39
  export declare class AIGNEClient {
9
40
  options: AIGNEClientOptions;
41
+ /**
42
+ * Creates a new AIGNEClient instance.
43
+ *
44
+ * @param options - Configuration options for connecting to the AIGNE server
45
+ */
10
46
  constructor(options: AIGNEClientOptions);
11
- invoke<I extends Message, O extends Message>(agent: string, input: I, options: AIGNEClientInvokeOptions & {
12
- streaming: true;
13
- }): Promise<AgentResponseStream<O>>;
47
+ /**
48
+ * Invokes an agent in non-streaming mode and returns the complete response.
49
+ *
50
+ * @param agent - Name of the agent to invoke
51
+ * @param input - Input message for the agent
52
+ * @param options - Options with streaming mode explicitly set to false or omitted
53
+ * @returns The complete agent response
54
+ *
55
+ * @example
56
+ * Here's a simple example of how to use AIGNEClient:
57
+ * {@includeCode ../../test/client/client.test.ts#example-aigne-client-simple}
58
+ */
14
59
  invoke<I extends Message, O extends Message>(agent: string, input: I, options?: AIGNEClientInvokeOptions & {
15
60
  streaming?: false;
16
61
  }): Promise<O>;
62
+ /**
63
+ * Invokes an agent with streaming mode enabled and returns a stream of response chunks.
64
+ *
65
+ * @param agent - Name of the agent to invoke
66
+ * @param input - Input message for the agent
67
+ * @param options - Options with streaming mode explicitly set to true
68
+ * @returns A stream of agent response chunks
69
+ *
70
+ * @example
71
+ * Here's an example of how to use AIGNEClient with streaming response:
72
+ * {@includeCode ../../test/client/client.test.ts#example-aigne-client-streaming}
73
+ */
74
+ invoke<I extends Message, O extends Message>(agent: string, input: I, options: AIGNEClientInvokeOptions & {
75
+ streaming: true;
76
+ }): Promise<AgentResponseStream<O>>;
77
+ /**
78
+ * Invokes an agent with the given input and options.
79
+ *
80
+ * @param agent - Name of the agent to invoke
81
+ * @param input - Input message for the agent
82
+ * @param options - Options for the invocation
83
+ * @returns Either a complete response or a response stream depending on the streaming option
84
+ */
17
85
  invoke<I extends Message, O extends Message>(agent: string, input: I, options?: AIGNEClientInvokeOptions): Promise<AgentResponse<O>>;
86
+ /**
87
+ * Enhanced fetch method that handles error responses from the AIGNE server.
88
+ * This method wraps the standard fetch API to provide better error handling and reporting.
89
+ *
90
+ * @param args - Standard fetch API arguments (url and options)
91
+ * @returns A Response object if the request was successful
92
+ * @throws Error with detailed information if the request failed
93
+ *
94
+ * @private
95
+ */
18
96
  fetch(...args: Parameters<typeof globalThis.fetch>): Promise<Response>;
19
97
  }
@@ -1,11 +1,33 @@
1
+ /**
2
+ * Client module used to interact with the AIGNE framework.
3
+ */
1
4
  import { AgentResponseStreamParser, EventStreamParser } from "../utils/event-stream.js";
2
5
  import { tryOrThrow } from "../utils/type-utils.js";
6
+ /**
7
+ * Client for interacting with a remote AIGNE server.
8
+ * AIGNEClient provides a client-side interface that matches the AIGNE API,
9
+ * allowing applications to invoke agents and receive responses from a remote AIGNE instance.
10
+ *
11
+ * @example
12
+ * Here's a simple example of how to use AIGNEClient:
13
+ * {@includeCode ../../test/client/client.test.ts#example-aigne-client-simple}
14
+ *
15
+ * @example
16
+ * Here's an example of how to use AIGNEClient with streaming response:
17
+ * {@includeCode ../../test/client/client.test.ts#example-aigne-client-streaming}
18
+ */
3
19
  export class AIGNEClient {
4
20
  options;
21
+ /**
22
+ * Creates a new AIGNEClient instance.
23
+ *
24
+ * @param options - Configuration options for connecting to the AIGNE server
25
+ */
5
26
  constructor(options) {
6
27
  this.options = options;
7
28
  }
8
29
  async invoke(agent, input, options) {
30
+ // Send the agent invocation request to the AIGNE server
9
31
  const response = await this.fetch(this.options.url, {
10
32
  ...options?.fetchOptions,
11
33
  method: "POST",
@@ -15,17 +37,33 @@ export class AIGNEClient {
15
37
  },
16
38
  body: JSON.stringify({ agent, input, options }),
17
39
  });
40
+ // For non-streaming responses, simply parse the JSON response and return it
18
41
  if (!options?.streaming) {
19
42
  return await response.json();
20
43
  }
44
+ // For streaming responses, set up the streaming pipeline
21
45
  const stream = response.body;
22
46
  if (!stream)
23
47
  throw new Error("Response body is not a stream");
48
+ // Process the stream through a series of transforms:
49
+ // 1. Convert bytes to text
50
+ // 2. Parse SSE format into structured events
51
+ // 3. Convert events into a standardized agent response stream
24
52
  return stream
25
53
  .pipeThrough(new TextDecoderStream())
26
54
  .pipeThrough(new EventStreamParser())
27
55
  .pipeThrough(new AgentResponseStreamParser());
28
56
  }
57
+ /**
58
+ * Enhanced fetch method that handles error responses from the AIGNE server.
59
+ * This method wraps the standard fetch API to provide better error handling and reporting.
60
+ *
61
+ * @param args - Standard fetch API arguments (url and options)
62
+ * @returns A Response object if the request was successful
63
+ * @throws Error with detailed information if the request failed
64
+ *
65
+ * @private
66
+ */
29
67
  async fetch(...args) {
30
68
  const result = await globalThis.fetch(...args);
31
69
  if (!result.ok) {
@@ -0,0 +1 @@
1
+ export * from "./client.js";
@@ -0,0 +1 @@
1
+ export * from "./client.js";
@@ -1,7 +1,6 @@
1
1
  export * from "./agents/agent.js";
2
2
  export * from "./agents/ai-agent.js";
3
3
  export * from "./agents/mcp-agent.js";
4
- export * from "./agents/memory.js";
5
4
  export * from "./agents/team-agent.js";
6
5
  export * from "./agents/types.js";
7
6
  export * from "./agents/user-agent.js";
package/lib/esm/index.js CHANGED
@@ -1,7 +1,6 @@
1
1
  export * from "./agents/agent.js";
2
2
  export * from "./agents/ai-agent.js";
3
3
  export * from "./agents/mcp-agent.js";
4
- export * from "./agents/memory.js";
5
4
  export * from "./agents/team-agent.js";
6
5
  export * from "./agents/types.js";
7
6
  export * from "./agents/user-agent.js";
@@ -2,7 +2,7 @@ import { type ZodObject, type ZodType, z } from "zod";
2
2
  import type { Message } from "../agents/agent.js";
3
3
  export declare function loadAgentFromJsFile(path: string): Promise<{
4
4
  name: string;
5
- fn: (args_0: Message) => Message;
5
+ process: (args_0: Message) => Message;
6
6
  description?: string | undefined;
7
7
  inputSchema?: ZodObject<Record<string, ZodType<any, z.ZodTypeDef, any>>, z.UnknownKeysParam, z.ZodTypeAny, {
8
8
  [x: string]: any;
@@ -15,7 +15,7 @@ const agentJsFileSchema = z.object({
15
15
  output_schema: inputOutputSchema
16
16
  .nullish()
17
17
  .transform((v) => (v ? jsonSchemaToZod(v) : undefined)),
18
- fn: z.function(),
18
+ process: z.function(),
19
19
  });
20
20
  export async function loadAgentFromJsFile(path) {
21
21
  const { default: agent } = await tryOrThrow(() => import(path), (error) => new Error(`Failed to load agent definition from ${path}: ${error.message}`));
@@ -25,6 +25,6 @@ export async function loadAgentFromJsFile(path) {
25
25
  return tryOrThrow(() => customCamelize(agentJsFileSchema.parse({
26
26
  ...agent,
27
27
  name: agent.agent_name || agent.name,
28
- fn: agent,
28
+ process: agent,
29
29
  }), { shallowKeys: ["input_schema", "output_schema"] }), (error) => new Error(`Failed to parse agent from ${path}: ${error.message}`));
30
30
  }
@@ -1,19 +1,20 @@
1
1
  import { type ZodObject, type ZodType, z } from "zod";
2
+ import { AIAgentToolChoice } from "../agents/ai-agent.js";
2
3
  export declare function loadAgentFromYamlFile(path: string): Promise<{
3
4
  type: "ai";
4
5
  name: string;
5
6
  description?: string | undefined;
7
+ skills?: string[] | undefined;
6
8
  memory?: true | {
7
9
  subscribeTopic?: string[] | undefined;
8
10
  } | undefined;
9
- skills?: string[] | undefined;
10
11
  instructions?: string | undefined;
11
12
  inputSchema?: ZodObject<Record<string, ZodType<any, z.ZodTypeDef, any>>, z.UnknownKeysParam, z.ZodTypeAny, {
12
13
  [x: string]: any;
13
14
  }, {
14
15
  [x: string]: any;
15
16
  }> | undefined;
16
- toolChoice?: "auto" | "none" | "required" | "router" | undefined;
17
+ toolChoice?: AIAgentToolChoice | undefined;
17
18
  outputSchema?: ZodObject<Record<string, ZodType<any, z.ZodTypeDef, any>>, z.UnknownKeysParam, z.ZodTypeAny, {
18
19
  [x: string]: any;
19
20
  }, {
@@ -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 { AIAgentToolChoice } from "../agents/ai-agent.js";
5
6
  import { customCamelize } from "../utils/camelize.js";
6
7
  import { tryOrThrow } from "../utils/type-utils.js";
7
8
  import { inputOutputSchema } from "./schema.js";
@@ -32,7 +33,7 @@ const agentFileSchema = z.discriminatedUnion("type", [
32
33
  .nullish()
33
34
  .transform((v) => v ?? undefined),
34
35
  tool_choice: z
35
- .union([z.literal("auto"), z.literal("none"), z.literal("required"), z.literal("router")])
36
+ .nativeEnum(AIAgentToolChoice)
36
37
  .nullish()
37
38
  .transform((v) => v ?? undefined),
38
39
  memory: z
@@ -8,8 +8,8 @@ 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
10
  skills: Agent<import("../agents/agent.js").Message, import("../agents/agent.js").Message>[];
11
- description?: string | null | undefined;
12
11
  name?: string | null | undefined;
12
+ description?: string | null | undefined;
13
13
  chat_model?: {
14
14
  name?: string | null | undefined;
15
15
  temperature?: number | null | undefined;
@@ -63,8 +63,8 @@ declare const aigneFileSchema: z.ZodObject<{
63
63
  agents: z.ZodOptional<z.ZodNullable<z.ZodArray<z.ZodString, "many">>>;
64
64
  skills: z.ZodOptional<z.ZodNullable<z.ZodArray<z.ZodString, "many">>>;
65
65
  }, "strip", z.ZodTypeAny, {
66
- description?: string | null | undefined;
67
66
  name?: string | null | undefined;
67
+ description?: string | null | undefined;
68
68
  skills?: string[] | null | undefined;
69
69
  chat_model?: {
70
70
  name?: string | null | undefined;
@@ -76,8 +76,8 @@ declare const aigneFileSchema: z.ZodObject<{
76
76
  } | null | undefined;
77
77
  agents?: string[] | null | undefined;
78
78
  }, {
79
- description?: string | null | undefined;
80
79
  name?: string | null | undefined;
80
+ description?: string | null | undefined;
81
81
  skills?: string[] | null | undefined;
82
82
  chat_model?: string | {
83
83
  name?: string | null | undefined;
@@ -90,8 +90,8 @@ declare const aigneFileSchema: z.ZodObject<{
90
90
  agents?: string[] | null | undefined;
91
91
  }>;
92
92
  export declare function loadAIGNEFile(path: string): Promise<{
93
- description?: string | null | undefined;
94
93
  name?: string | null | undefined;
94
+ description?: string | null | undefined;
95
95
  skills?: string[] | null | undefined;
96
96
  chat_model?: {
97
97
  name?: string | null | undefined;
@@ -0,0 +1,16 @@
1
+ import { type Memory, MemoryAgent, type MemoryAgentOptions } from "./memory.js";
2
+ export declare const DEFAULT_MAX_MEMORY_COUNT = 10;
3
+ export interface DefaultMemoryOptions extends Partial<MemoryAgentOptions> {
4
+ }
5
+ export declare class DefaultMemory extends MemoryAgent {
6
+ constructor(options?: DefaultMemoryOptions);
7
+ storage: Memory[];
8
+ search(options?: {
9
+ limit?: number;
10
+ }): Promise<{
11
+ result: Memory[];
12
+ }>;
13
+ create(memory: Pick<Memory, "content">): Promise<{
14
+ result: Memory;
15
+ }>;
16
+ }
@@ -0,0 +1,63 @@
1
+ import equal from "fast-deep-equal";
2
+ import { MemoryAgent, newMemoryId } from "./memory.js";
3
+ import { MemoryRecorder } from "./recorder.js";
4
+ import { MemoryRetriever, } from "./retriever.js";
5
+ export const DEFAULT_MAX_MEMORY_COUNT = 10;
6
+ export class DefaultMemory extends MemoryAgent {
7
+ constructor(options = {}) {
8
+ super({
9
+ ...options,
10
+ autoUpdate: options.autoUpdate ?? true,
11
+ });
12
+ if (!this.recorder)
13
+ this.recorder = new DefaultMemoryRecorder(this);
14
+ if (!this.retriever)
15
+ this.retriever = new DefaultMemoryRetriever(this);
16
+ }
17
+ storage = [];
18
+ async search(options = {}) {
19
+ const { limit = DEFAULT_MAX_MEMORY_COUNT } = options;
20
+ const result = limit < 0 ? this.storage.slice(limit) : this.storage.slice(0, limit);
21
+ return { result };
22
+ }
23
+ async create(memory) {
24
+ const m = {
25
+ ...memory,
26
+ id: newMemoryId(),
27
+ createdAt: new Date().toISOString(),
28
+ };
29
+ this.storage.push(m);
30
+ return { result: m };
31
+ }
32
+ }
33
+ class DefaultMemoryRetriever extends MemoryRetriever {
34
+ memory;
35
+ constructor(memory) {
36
+ super({});
37
+ this.memory = memory;
38
+ }
39
+ async process(input) {
40
+ const { result } = await this.memory.search(input);
41
+ return { memories: result };
42
+ }
43
+ }
44
+ class DefaultMemoryRecorder extends MemoryRecorder {
45
+ memory;
46
+ constructor(memory) {
47
+ super({});
48
+ this.memory = memory;
49
+ }
50
+ async process(input) {
51
+ const newMemories = [];
52
+ for (const content of input.content) {
53
+ const { result: [last], } = await this.memory.search({ limit: -1 });
54
+ if (!equal(last?.content, content)) {
55
+ const { result } = await this.memory.create({ content });
56
+ newMemories.push(result);
57
+ }
58
+ }
59
+ return {
60
+ memories: newMemories,
61
+ };
62
+ }
63
+ }
@@ -0,0 +1,3 @@
1
+ export * from "./memory.js";
2
+ export * from "./recorder.js";
3
+ export * from "./retriever.js";
@@ -0,0 +1,3 @@
1
+ export * from "./memory.js";
2
+ export * from "./recorder.js";
3
+ export * from "./retriever.js";
@@ -0,0 +1,89 @@
1
+ import { Agent, type AgentOptions, type Message } from "../agents/agent.js";
2
+ import type { Context } from "../aigne/context.js";
3
+ import type { MessagePayload } from "../aigne/message-queue.js";
4
+ import type { MemoryRecorder, MemoryRecorderInput, MemoryRecorderOutput } from "./recorder.js";
5
+ import type { MemoryRetriever, MemoryRetrieverInput, MemoryRetrieverOutput } from "./retriever.js";
6
+ export interface Memory {
7
+ id: string;
8
+ content: unknown;
9
+ createdAt: string;
10
+ }
11
+ export declare const newMemoryId: () => string;
12
+ export interface MemoryAgentOptions extends Partial<Pick<MemoryAgent, "recorder" | "retriever" | "autoUpdate">>, Pick<AgentOptions, "subscribeTopic" | "skills"> {
13
+ }
14
+ /**
15
+ * A specialized agent responsible for managing, storing, and retrieving memories within the agent system.
16
+ *
17
+ * MemoryAgent serves as a bridge between application logic and memory storage/retrieval mechanisms.
18
+ * It delegates the actual memory operations to specialized recorder and retriever agents that
19
+ * are attached as skills. This agent doesn't directly process messages like other agents but
20
+ * instead provides memory management capabilities to the system.
21
+ */
22
+ export declare class MemoryAgent extends Agent {
23
+ /**
24
+ * Creates a new MemoryAgent instance.
25
+ */
26
+ constructor(options: MemoryAgentOptions);
27
+ private _retriever?;
28
+ /**
29
+ * Agent used for retrieving memories from storage.
30
+ *
31
+ * This retriever is automatically added to the agent's skills when set.
32
+ * Setting a new retriever will remove any previously set retriever from skills.
33
+ */
34
+ get retriever(): MemoryRetriever | undefined;
35
+ set retriever(value: MemoryRetriever | undefined);
36
+ private _recorder?;
37
+ /**
38
+ * Agent used for recording and storing new memories.
39
+ *
40
+ * This recorder is automatically added to the agent's skills when set.
41
+ * Setting a new recorder will remove any previously set recorder from skills.
42
+ */
43
+ get recorder(): MemoryRecorder | undefined;
44
+ set recorder(value: MemoryRecorder | undefined);
45
+ /**
46
+ * Controls whether to automatically update the memory when agent call completes.
47
+ *
48
+ * When true, the agent will automatically record any relevant information
49
+ * after completing operations, creating a history of interactions.
50
+ */
51
+ autoUpdate?: boolean;
52
+ /**
53
+ * Indicates whether this agent can be directly called.
54
+ *
55
+ * MemoryAgent is designed to be used as a supporting component rather than
56
+ * being directly invoked for processing, so this returns false.
57
+ */
58
+ get isCallable(): boolean;
59
+ /**
60
+ * The standard message processing method required by the Agent interface.
61
+ *
62
+ * MemoryAgent doesn't directly process messages like other agents, so this method
63
+ * throws an error when called. Use the specialized retrieve() and record() methods instead.
64
+ */
65
+ process(_input: Message, _context: Context): Promise<Message>;
66
+ /**
67
+ * Retrieves memories based on the provided input criteria.
68
+ *
69
+ * Delegates the actual retrieval operation to the configured retriever agent.
70
+ *
71
+ * @param input - The retrieval parameters (can include search terms, limits, etc.)
72
+ * @param context - The execution context
73
+ * @returns A promise resolving to the retrieved memories
74
+ * @throws Error - If no retriever has been initialized
75
+ */
76
+ retrieve(input: MemoryRetrieverInput, context: Context): Promise<MemoryRetrieverOutput>;
77
+ /**
78
+ * Records new memories based on the provided input content.
79
+ *
80
+ * Delegates the actual recording operation to the configured recorder agent.
81
+ *
82
+ * @param input - The content to be recorded as memories
83
+ * @param context - The execution context
84
+ * @returns A promise resolving to the recorded memories
85
+ * @throws Error - If no recorder has been initialized
86
+ */
87
+ record(input: MemoryRecorderInput, context: Context): Promise<MemoryRecorderOutput>;
88
+ onMessage({ role, source, message, context }: MessagePayload): Promise<void>;
89
+ }