@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,6 +1,12 @@
1
1
  import { IncomingMessage, ServerResponse } from "node:http";
2
2
  import { z } from "zod";
3
3
  import type { AIGNE } from "../aigne/aigne.js";
4
+ /**
5
+ * Schema for validating agent invocation payloads.
6
+ * Defines the expected structure for requests to invoke an agent.
7
+ *
8
+ * @hidden
9
+ */
4
10
  export declare const invokePayloadSchema: z.ZodObject<{
5
11
  agent: z.ZodString;
6
12
  input: z.ZodRecord<z.ZodString, z.ZodUnknown>;
@@ -24,31 +30,106 @@ export declare const invokePayloadSchema: z.ZodObject<{
24
30
  streaming?: boolean | null | undefined;
25
31
  } | null | undefined;
26
32
  }>;
33
+ /**
34
+ * Configuration options for the AIGNEServer.
35
+ * These options control various aspects of server behavior including
36
+ * request parsing, payload limits, and response handling.
37
+ */
27
38
  export interface AIGNEServerOptions {
28
39
  /**
29
- * Maximum body size for the request.
30
- * Only used when the request is a Node.js IncomingMessage and no `body` property is present.
40
+ * Maximum body size for incoming HTTP requests.
41
+ * This controls the upper limit of request payload size when parsing raw HTTP requests.
42
+ * Only used when working with Node.js IncomingMessage objects that don't already have
43
+ * a pre-parsed body property (e.g., when not using Express middleware).
44
+ *
31
45
  * @default "4mb"
32
46
  */
33
47
  maximumBodySize?: string;
34
48
  }
49
+ /**
50
+ * AIGNEServer provides HTTP API access to AIGNE capabilities.
51
+ * It handles requests to invoke agents, manages response streaming,
52
+ * and supports multiple HTTP server frameworks including Node.js http,
53
+ * Express, and Fetch API compatible environments.
54
+ *
55
+ * @example
56
+ * Here's a simple example of how to use AIGNEServer with express:
57
+ * {@includeCode ../../test/server/server.test.ts#example-aigne-server-express}
58
+ *
59
+ * @example
60
+ * Here's an example of how to use AIGNEServer with Hono:
61
+ * {@includeCode ../../test/server/server.test.ts#example-aigne-server-hono}
62
+ */
35
63
  export declare class AIGNEServer {
36
64
  engine: AIGNE;
37
65
  options?: AIGNEServerOptions | undefined;
66
+ /**
67
+ * Creates a new AIGNEServer instance.
68
+ *
69
+ * @param engine - The AIGNE engine instance that will process agent invocations
70
+ * @param options - Configuration options for the server
71
+ */
38
72
  constructor(engine: AIGNE, options?: AIGNEServerOptions | undefined);
39
73
  /**
40
- * Invoke the agent with the given input.
41
- * @param request - The request object, which can be a parsed JSON object, a Fetch API Request object, or a Node.js IncomingMessage object.
42
- * @returns The web standard response, you can return it directly in supported frameworks like hono.
74
+ * Invokes an agent with the provided input and returns a standard web Response.
75
+ * This method serves as the primary API endpoint for agent invocation.
76
+ *
77
+ * The request can be provided in various formats to support different integration scenarios:
78
+ * - As a pre-parsed JavaScript object
79
+ * - As a Fetch API Request instance (for modern web frameworks)
80
+ * - As a Node.js IncomingMessage (for Express, Fastify, etc.)
81
+ *
82
+ * @param request - The agent invocation request in any supported format
83
+ * @returns A web standard Response object that can be returned directly in frameworks
84
+ * like Hono, Next.js, or any Fetch API compatible environment
85
+ *
86
+ * @example
87
+ * Here's a simple example of how to use AIGNEServer with Hono:
88
+ * {@includeCode ../../test/server/server.test.ts#example-aigne-server-hono}
43
89
  */
44
90
  invoke(request: Record<string, unknown> | Request | IncomingMessage): Promise<Response>;
45
91
  /**
46
- * Invoke the agent with the given input, and write the SSE response to the Node.js ServerResponse.
47
- * @param request - The request object, which can be a parsed JSON object, a Fetch API Request object, or a Node.js IncomingMessage object.
48
- * @param response - The Node.js ServerResponse object to write the SSE response to.
92
+ * Invokes an agent with the provided input and streams the response to a Node.js ServerResponse.
93
+ * This overload is specifically designed for Node.js HTTP server environments.
94
+ *
95
+ * The method handles both regular JSON responses and streaming Server-Sent Events (SSE)
96
+ * responses based on the options specified in the request.
97
+ *
98
+ * @param request - The agent invocation request in any supported format
99
+ * @param response - The Node.js ServerResponse object to write the response to
100
+ *
101
+ * @example
102
+ * Here's a simple example of how to use AIGNEServer with express:
103
+ * {@includeCode ../../test/server/server.test.ts#example-aigne-server-express}
49
104
  */
50
105
  invoke(request: Record<string, unknown> | Request | IncomingMessage, response: ServerResponse): Promise<void>;
106
+ /**
107
+ * Internal method that handles the core logic of processing an agent invocation request.
108
+ * Validates the request payload, finds the requested agent, and processes the invocation
109
+ * with either streaming or non-streaming response handling.
110
+ *
111
+ * @param request - The parsed or raw request to process
112
+ * @returns A standard Response object with the invocation result
113
+ * @private
114
+ */
51
115
  _invoke(request: Record<string, unknown> | Request | IncomingMessage): Promise<Response>;
116
+ /**
117
+ * Prepares and normalizes the input from various request types.
118
+ * Handles different request formats (Node.js IncomingMessage, Fetch API Request,
119
+ * or already parsed object) and extracts the JSON payload.
120
+ *
121
+ * @param request - The request object in any supported format
122
+ * @returns The normalized payload as a JavaScript object
123
+ * @private
124
+ */
52
125
  _prepareInput(request: Record<string, unknown> | Request | IncomingMessage): Promise<Record<string, unknown>>;
126
+ /**
127
+ * Writes a web standard Response object to a Node.js ServerResponse.
128
+ * Handles streaming responses and error conditions appropriately.
129
+ *
130
+ * @param response - The web standard Response object to write
131
+ * @param res - The Node.js ServerResponse to write to
132
+ * @private
133
+ */
53
134
  _writeResponse(response: Response, res: ServerResponse): Promise<void>;
54
135
  }
@@ -6,7 +6,19 @@ import { AgentResponseStreamSSE } from "../utils/event-stream.js";
6
6
  import { readableStreamToAsyncIterator } from "../utils/stream-utils.js";
7
7
  import { checkArguments, isRecord, tryOrThrow } from "../utils/type-utils.js";
8
8
  import { ServerError } from "./error.js";
9
+ /**
10
+ * Default maximum allowed size for request bodies when parsing raw HTTP requests.
11
+ * This limits the amount of data that can be uploaded to protect against denial of service attacks.
12
+ * Can be overridden via AIGNEServerOptions.
13
+ * @internal
14
+ */
9
15
  const DEFAULT_MAXIMUM_BODY_SIZE = "4mb";
16
+ /**
17
+ * Schema for validating agent invocation payloads.
18
+ * Defines the expected structure for requests to invoke an agent.
19
+ *
20
+ * @hidden
21
+ */
10
22
  export const invokePayloadSchema = z.object({
11
23
  agent: z.string(),
12
24
  input: z.record(z.string(), z.unknown()),
@@ -16,9 +28,29 @@ export const invokePayloadSchema = z.object({
16
28
  })
17
29
  .nullish(),
18
30
  });
31
+ /**
32
+ * AIGNEServer provides HTTP API access to AIGNE capabilities.
33
+ * It handles requests to invoke agents, manages response streaming,
34
+ * and supports multiple HTTP server frameworks including Node.js http,
35
+ * Express, and Fetch API compatible environments.
36
+ *
37
+ * @example
38
+ * Here's a simple example of how to use AIGNEServer with express:
39
+ * {@includeCode ../../test/server/server.test.ts#example-aigne-server-express}
40
+ *
41
+ * @example
42
+ * Here's an example of how to use AIGNEServer with Hono:
43
+ * {@includeCode ../../test/server/server.test.ts#example-aigne-server-hono}
44
+ */
19
45
  export class AIGNEServer {
20
46
  engine;
21
47
  options;
48
+ /**
49
+ * Creates a new AIGNEServer instance.
50
+ *
51
+ * @param engine - The AIGNE engine instance that will process agent invocations
52
+ * @param options - Configuration options for the server
53
+ */
22
54
  constructor(engine, options) {
23
55
  this.engine = engine;
24
56
  this.options = options;
@@ -31,6 +63,15 @@ export class AIGNEServer {
31
63
  }
32
64
  return result;
33
65
  }
66
+ /**
67
+ * Internal method that handles the core logic of processing an agent invocation request.
68
+ * Validates the request payload, finds the requested agent, and processes the invocation
69
+ * with either streaming or non-streaming response handling.
70
+ *
71
+ * @param request - The parsed or raw request to process
72
+ * @returns A standard Response object with the invocation result
73
+ * @private
74
+ */
34
75
  async _invoke(request) {
35
76
  const { engine } = this;
36
77
  try {
@@ -61,6 +102,15 @@ export class AIGNEServer {
61
102
  });
62
103
  }
63
104
  }
105
+ /**
106
+ * Prepares and normalizes the input from various request types.
107
+ * Handles different request formats (Node.js IncomingMessage, Fetch API Request,
108
+ * or already parsed object) and extracts the JSON payload.
109
+ *
110
+ * @param request - The request object in any supported format
111
+ * @returns The normalized payload as a JavaScript object
112
+ * @private
113
+ */
64
114
  async _prepareInput(request) {
65
115
  const contentTypeError = new ServerError(415, "Unsupported Media Type: Content-Type must be application/json");
66
116
  if (request instanceof IncomingMessage) {
@@ -92,6 +142,14 @@ export class AIGNEServer {
92
142
  throw contentTypeError;
93
143
  return request;
94
144
  }
145
+ /**
146
+ * Writes a web standard Response object to a Node.js ServerResponse.
147
+ * Handles streaming responses and error conditions appropriately.
148
+ *
149
+ * @param response - The web standard Response object to write
150
+ * @param res - The Node.js ServerResponse to write to
151
+ * @private
152
+ */
95
153
  async _writeResponse(response, res) {
96
154
  try {
97
155
  res.writeHead(response.status, response.headers.toJSON());
@@ -0,0 +1,2 @@
1
+ export declare function exists(path: string): Promise<boolean>;
2
+ export declare function expandHome(filepath: string): string;
@@ -0,0 +1,21 @@
1
+ import { stat } from "node:fs/promises";
2
+ import { homedir } from "node:os";
3
+ import { join } from "node:path";
4
+ export async function exists(path) {
5
+ try {
6
+ await stat(path);
7
+ return true;
8
+ }
9
+ catch (e) {
10
+ if (e.code === "ENOENT") {
11
+ return false;
12
+ }
13
+ throw e;
14
+ }
15
+ }
16
+ export function expandHome(filepath) {
17
+ if (filepath.startsWith("~/") || filepath === "~") {
18
+ return join(homedir(), filepath.slice(1));
19
+ }
20
+ return filepath;
21
+ }
@@ -1 +1,2 @@
1
1
  export declare function getJsonOutputPrompt(schema: Record<string, unknown> | string): string;
2
+ export declare function getJsonToolInputPrompt(schema: Record<string, unknown> | string): string;
@@ -1,10 +1,18 @@
1
- export function getJsonOutputPrompt(schema) {
2
- let prompt = "Output must be a JSON object containing the following fields only.";
1
+ function buildJsonPrompt(schema, firstLine) {
2
+ let prompt = firstLine;
3
3
  if (typeof schema === "string") {
4
4
  prompt += `\n<json_fields>\n${schema}\n</json_fields>`;
5
5
  }
6
6
  else {
7
7
  prompt += `\n<json_fields>\n${JSON.stringify(schema)}\n</json_fields>`;
8
8
  }
9
+ prompt +=
10
+ "\nDo not include any explanations, comments, or Markdown formatting (such as triple backticks). Return only the raw JSON.";
9
11
  return prompt;
10
12
  }
13
+ export function getJsonOutputPrompt(schema) {
14
+ return buildJsonPrompt(schema, "Output must be a JSON object containing the following fields only.");
15
+ }
16
+ export function getJsonToolInputPrompt(schema) {
17
+ return buildJsonPrompt(schema, "Tool input must be a JSON object containing the following fields only.");
18
+ }
@@ -8,6 +8,7 @@ export declare function isEmpty(obj: unknown): boolean;
8
8
  export declare function isNonNullable<T>(value: T): value is NonNullable<T>;
9
9
  export declare function isNotEmpty<T>(arr: T[]): arr is [T, ...T[]];
10
10
  export declare function duplicates<T>(arr: T[], key?: (item: T) => unknown): T[];
11
+ export declare function remove<T>(arr: T[], remove: T[] | ((item: T) => boolean)): T[];
11
12
  export declare function unique<T>(arr: T[], key?: (item: T) => unknown): T[];
12
13
  export declare function omitBy<T extends Record<string, unknown>, K extends keyof T>(obj: T, predicate: (value: T[K], key: K) => boolean): Partial<T>;
13
14
  export declare function orArrayToArray<T>(value?: T | T[]): T[];
@@ -34,6 +34,19 @@ export function duplicates(arr, key = (item) => item) {
34
34
  }
35
35
  return Array.from(duplicates);
36
36
  }
37
+ export function remove(arr, remove) {
38
+ const removed = [];
39
+ for (let i = 0; i < arr.length; i++) {
40
+ // biome-ignore lint/style/noNonNullAssertion: <explanation>
41
+ const item = arr[i];
42
+ if ((Array.isArray(remove) && remove.includes(item)) ||
43
+ (typeof remove === "function" && remove(item))) {
44
+ removed.push(...arr.splice(i, 1));
45
+ i--;
46
+ }
47
+ }
48
+ return removed;
49
+ }
37
50
  export function unique(arr, key = (item) => item) {
38
51
  const seen = new Set();
39
52
  return arr.filter((item) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aigne/core",
3
- "version": "1.13.0",
3
+ "version": "1.14.0",
4
4
  "description": "AIGNE core library for building AI-powered applications",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -53,6 +53,9 @@
53
53
  "models/*": [
54
54
  "./lib/dts/models/*"
55
55
  ],
56
+ "memory/*": [
57
+ "./lib/dts/memory/*"
58
+ ],
56
59
  "prompt/*": [
57
60
  "./lib/dts/prompt/*"
58
61
  ],
@@ -69,7 +72,7 @@
69
72
  },
70
73
  "dependencies": {
71
74
  "@aigne/json-schema-to-zod": "^1.3.3",
72
- "@modelcontextprotocol/sdk": "^1.10.2",
75
+ "@modelcontextprotocol/sdk": "^1.11.0",
73
76
  "@types/debug": "^4.1.12",
74
77
  "camelize-ts": "^3.0.0",
75
78
  "content-type": "^1.0.5",
@@ -77,7 +80,7 @@
77
80
  "eventsource-parser": "^3.0.1",
78
81
  "fast-deep-equal": "^3.1.3",
79
82
  "immer": "^10.1.1",
80
- "inquirer": "^12.5.2",
83
+ "inquirer": "^12.6.0",
81
84
  "mustache": "^4.2.0",
82
85
  "nanoid": "^5.1.5",
83
86
  "p-retry": "^6.2.1",
@@ -85,7 +88,7 @@
85
88
  "ufo": "^1.6.1",
86
89
  "uuid": "^11.1.0",
87
90
  "yaml": "^2.7.1",
88
- "zod": "^3.24.2",
91
+ "zod": "^3.24.4",
89
92
  "zod-to-json-schema": "^3.24.5"
90
93
  },
91
94
  "peerDependencies": {
@@ -95,20 +98,20 @@
95
98
  "openai": "^4.87.3"
96
99
  },
97
100
  "devDependencies": {
98
- "@anthropic-ai/sdk": "^0.39.0",
99
- "@google/generative-ai": "^0.24.0",
100
- "@types/bun": "^1.2.9",
101
+ "@anthropic-ai/sdk": "^0.41.0",
102
+ "@google/generative-ai": "^0.24.1",
103
+ "@types/bun": "^1.2.12",
101
104
  "@types/compression": "^1.7.5",
102
105
  "@types/content-type": "^1.1.8",
103
106
  "@types/express": "^5.0.1",
104
- "@types/mustache": "^4.2.5",
105
- "@types/node": "^22.14.1",
107
+ "@types/mustache": "^4.2.6",
108
+ "@types/node": "^22.15.15",
106
109
  "compression": "^1.8.0",
107
110
  "detect-port": "^2.1.0",
108
111
  "express": "^5.1.0",
109
- "hono": "^4.7.7",
112
+ "hono": "^4.7.8",
110
113
  "npm-run-all": "^4.1.5",
111
- "openai": "^4.94.0",
114
+ "openai": "^4.97.0",
112
115
  "rimraf": "^6.0.1",
113
116
  "typescript": "^5.8.3"
114
117
  },
@@ -1,26 +0,0 @@
1
- import type { Context } from "../aigne/context.js";
2
- import type { Message } from "./agent.js";
3
- export interface AgentMemoryOptions {
4
- /**
5
- * Enable memory, default is true
6
- */
7
- enabled?: boolean;
8
- subscribeTopic?: string | string[];
9
- maxMemoriesInChat?: number;
10
- }
11
- export interface Memory {
12
- role: "user" | "agent";
13
- content: Message;
14
- source?: string;
15
- }
16
- export declare class AgentMemory {
17
- constructor(options: AgentMemoryOptions);
18
- enabled?: boolean;
19
- subscribeTopic?: string | string[];
20
- maxMemoriesInChat?: number;
21
- memories: Memory[];
22
- private subscriptions;
23
- addMemory(memory: Memory): void;
24
- attach(context: Pick<Context, "subscribe">): void;
25
- detach(): void;
26
- }
@@ -1,45 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AgentMemory = void 0;
4
- const zod_1 = require("zod");
5
- const type_utils_js_1 = require("../utils/type-utils.js");
6
- class AgentMemory {
7
- constructor(options) {
8
- (0, type_utils_js_1.checkArguments)("AgentMemory", agentMemoryOptionsSchema, options);
9
- this.enabled = options.enabled ?? true;
10
- this.subscribeTopic = options.subscribeTopic;
11
- this.maxMemoriesInChat = options.maxMemoriesInChat;
12
- }
13
- enabled;
14
- subscribeTopic;
15
- maxMemoriesInChat;
16
- memories = [];
17
- subscriptions = [];
18
- addMemory(memory) {
19
- if (this.memories.at(-1)?.content === memory.content)
20
- return;
21
- // TODO: save all memories into database instead of in-memory,
22
- // and give every memory a unique id to avoid duplication
23
- this.memories.push(memory);
24
- }
25
- attach(context) {
26
- for (const topic of (0, type_utils_js_1.orArrayToArray)(this.subscribeTopic)) {
27
- const sub = context.subscribe(topic, ({ role, message, source }) => {
28
- this.addMemory({ role, source, content: message });
29
- });
30
- this.subscriptions.push(sub);
31
- }
32
- }
33
- detach() {
34
- for (const sub of this.subscriptions) {
35
- sub();
36
- }
37
- this.subscriptions = [];
38
- }
39
- }
40
- exports.AgentMemory = AgentMemory;
41
- const agentMemoryOptionsSchema = zod_1.z.object({
42
- enabled: zod_1.z.boolean().optional(),
43
- subscribeTopic: zod_1.z.union([zod_1.z.string(), zod_1.z.array(zod_1.z.string())]).optional(),
44
- maxMemoriesInChat: zod_1.z.number().optional(),
45
- });
@@ -1,26 +0,0 @@
1
- import type { Context } from "../aigne/context.js";
2
- import type { Message } from "./agent.js";
3
- export interface AgentMemoryOptions {
4
- /**
5
- * Enable memory, default is true
6
- */
7
- enabled?: boolean;
8
- subscribeTopic?: string | string[];
9
- maxMemoriesInChat?: number;
10
- }
11
- export interface Memory {
12
- role: "user" | "agent";
13
- content: Message;
14
- source?: string;
15
- }
16
- export declare class AgentMemory {
17
- constructor(options: AgentMemoryOptions);
18
- enabled?: boolean;
19
- subscribeTopic?: string | string[];
20
- maxMemoriesInChat?: number;
21
- memories: Memory[];
22
- private subscriptions;
23
- addMemory(memory: Memory): void;
24
- attach(context: Pick<Context, "subscribe">): void;
25
- detach(): void;
26
- }
@@ -1,26 +0,0 @@
1
- import type { Context } from "../aigne/context.js";
2
- import type { Message } from "./agent.js";
3
- export interface AgentMemoryOptions {
4
- /**
5
- * Enable memory, default is true
6
- */
7
- enabled?: boolean;
8
- subscribeTopic?: string | string[];
9
- maxMemoriesInChat?: number;
10
- }
11
- export interface Memory {
12
- role: "user" | "agent";
13
- content: Message;
14
- source?: string;
15
- }
16
- export declare class AgentMemory {
17
- constructor(options: AgentMemoryOptions);
18
- enabled?: boolean;
19
- subscribeTopic?: string | string[];
20
- maxMemoriesInChat?: number;
21
- memories: Memory[];
22
- private subscriptions;
23
- addMemory(memory: Memory): void;
24
- attach(context: Pick<Context, "subscribe">): void;
25
- detach(): void;
26
- }
@@ -1,41 +0,0 @@
1
- import { z } from "zod";
2
- import { checkArguments, orArrayToArray } from "../utils/type-utils.js";
3
- export class AgentMemory {
4
- constructor(options) {
5
- checkArguments("AgentMemory", agentMemoryOptionsSchema, options);
6
- this.enabled = options.enabled ?? true;
7
- this.subscribeTopic = options.subscribeTopic;
8
- this.maxMemoriesInChat = options.maxMemoriesInChat;
9
- }
10
- enabled;
11
- subscribeTopic;
12
- maxMemoriesInChat;
13
- memories = [];
14
- subscriptions = [];
15
- addMemory(memory) {
16
- if (this.memories.at(-1)?.content === memory.content)
17
- return;
18
- // TODO: save all memories into database instead of in-memory,
19
- // and give every memory a unique id to avoid duplication
20
- this.memories.push(memory);
21
- }
22
- attach(context) {
23
- for (const topic of orArrayToArray(this.subscribeTopic)) {
24
- const sub = context.subscribe(topic, ({ role, message, source }) => {
25
- this.addMemory({ role, source, content: message });
26
- });
27
- this.subscriptions.push(sub);
28
- }
29
- }
30
- detach() {
31
- for (const sub of this.subscriptions) {
32
- sub();
33
- }
34
- this.subscriptions = [];
35
- }
36
- }
37
- const agentMemoryOptionsSchema = z.object({
38
- enabled: z.boolean().optional(),
39
- subscribeTopic: z.union([z.string(), z.array(z.string())]).optional(),
40
- maxMemoriesInChat: z.number().optional(),
41
- });
File without changes