@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
@@ -12,6 +12,9 @@ const stream_utils_js_1 = require("../utils/stream-utils.js");
12
12
  const type_utils_js_1 = require("../utils/type-utils.js");
13
13
  const chat_model_js_1 = require("./chat-model.js");
14
14
  const CHAT_MODEL_CLAUDE_DEFAULT_MODEL = "claude-3-7-sonnet-latest";
15
+ /**
16
+ * @hidden
17
+ */
15
18
  exports.claudeChatModelOptionsSchema = zod_1.z.object({
16
19
  apiKey: zod_1.z.string().optional(),
17
20
  model: zod_1.z.string().optional(),
@@ -26,6 +29,24 @@ exports.claudeChatModelOptionsSchema = zod_1.z.object({
26
29
  })
27
30
  .optional(),
28
31
  });
32
+ /**
33
+ * Implementation of the ChatModel interface for Anthropic's Claude API
34
+ *
35
+ * This model provides access to Claude's capabilities including:
36
+ * - Text generation
37
+ * - Tool use
38
+ * - JSON structured output
39
+ *
40
+ * Default model: 'claude-3-7-sonnet-latest'
41
+ *
42
+ * @example
43
+ * Here's how to create and use a Claude chat model:
44
+ * {@includeCode ../../test/models/claude-chat-model.test.ts#example-claude-chat-model}
45
+ *
46
+ * @example
47
+ * Here's an example with streaming response:
48
+ * {@includeCode ../../test/models/claude-chat-model.test.ts#example-claude-chat-model-streaming-async-generator}
49
+ */
29
50
  class ClaudeChatModel extends chat_model_js_1.ChatModel {
30
51
  options;
31
52
  constructor(options) {
@@ -34,6 +55,9 @@ class ClaudeChatModel extends chat_model_js_1.ChatModel {
34
55
  super();
35
56
  this.options = options;
36
57
  }
58
+ /**
59
+ * @hidden
60
+ */
37
61
  _client;
38
62
  get client() {
39
63
  const apiKey = this.options?.apiKey || process.env.ANTHROPIC_API_KEY || process.env.CLAUDE_API_KEY;
@@ -45,7 +69,15 @@ class ClaudeChatModel extends chat_model_js_1.ChatModel {
45
69
  get modelOptions() {
46
70
  return this.options?.modelOptions;
47
71
  }
48
- async process(input, _context, options) {
72
+ /**
73
+ * Process the input using Claude's chat model
74
+ * @param input - The input to process
75
+ * @returns The processed output from the model
76
+ */
77
+ process(input) {
78
+ return this._process(input);
79
+ }
80
+ async _process(input) {
49
81
  const model = this.options?.model || CHAT_MODEL_CLAUDE_DEFAULT_MODEL;
50
82
  const disableParallelToolUse = input.modelOptions?.parallelToolCalls === false ||
51
83
  this.modelOptions?.parallelToolCalls === false;
@@ -62,7 +94,7 @@ class ClaudeChatModel extends chat_model_js_1.ChatModel {
62
94
  ...body,
63
95
  stream: true,
64
96
  });
65
- if (options?.streaming && input.responseFormat?.type !== "json_schema") {
97
+ if (input.responseFormat?.type !== "json_schema") {
66
98
  return this.extractResultFromClaudeStream(stream, true);
67
99
  }
68
100
  const result = await this.extractResultFromClaudeStream(stream);
@@ -1,4 +1,20 @@
1
1
  import { OpenAIChatModel, type OpenAIChatModelOptions } from "./openai-chat-model.js";
2
+ /**
3
+ * Implementation of the ChatModel interface for DeepSeek's API
4
+ *
5
+ * This model uses OpenAI-compatible API format to interact with DeepSeek's models,
6
+ * but with specific configuration and capabilities for DeepSeek.
7
+ *
8
+ * Default model: 'deepseek-chat'
9
+ *
10
+ * @example
11
+ * Here's how to create and use a DeepSeek chat model:
12
+ * {@includeCode ../../test/models/deepseek-chat-model.test.ts#example-deepseek-chat-model}
13
+ *
14
+ * @example
15
+ * Here's an example with streaming response:
16
+ * {@includeCode ../../test/models/deepseek-chat-model.test.ts#example-deepseek-chat-model-streaming}
17
+ */
2
18
  export declare class DeepSeekChatModel extends OpenAIChatModel {
3
19
  constructor(options?: OpenAIChatModelOptions);
4
20
  protected apiKeyEnvName: string;
@@ -4,6 +4,22 @@ exports.DeepSeekChatModel = void 0;
4
4
  const openai_chat_model_js_1 = require("./openai-chat-model.js");
5
5
  const DEEPSEEK_DEFAULT_CHAT_MODEL = "deepseek-chat";
6
6
  const DEEPSEEK_BASE_URL = "https://api.deepseek.com";
7
+ /**
8
+ * Implementation of the ChatModel interface for DeepSeek's API
9
+ *
10
+ * This model uses OpenAI-compatible API format to interact with DeepSeek's models,
11
+ * but with specific configuration and capabilities for DeepSeek.
12
+ *
13
+ * Default model: 'deepseek-chat'
14
+ *
15
+ * @example
16
+ * Here's how to create and use a DeepSeek chat model:
17
+ * {@includeCode ../../test/models/deepseek-chat-model.test.ts#example-deepseek-chat-model}
18
+ *
19
+ * @example
20
+ * Here's an example with streaming response:
21
+ * {@includeCode ../../test/models/deepseek-chat-model.test.ts#example-deepseek-chat-model-streaming}
22
+ */
7
23
  class DeepSeekChatModel extends openai_chat_model_js_1.OpenAIChatModel {
8
24
  constructor(options) {
9
25
  super({
@@ -1,4 +1,18 @@
1
1
  import { OpenAIChatModel, type OpenAIChatModelOptions } from "./openai-chat-model.js";
2
+ /**
3
+ * Implementation of the ChatModel interface for Google's Gemini API
4
+ *
5
+ * This model uses OpenAI-compatible API format to interact with Google's Gemini models,
6
+ * providing access to models like Gemini 1.5 and Gemini 2.0.
7
+ *
8
+ * @example
9
+ * Here's how to create and use a Gemini chat model:
10
+ * {@includeCode ../../test/models/gemini-chat-model.test.ts#example-gemini-chat-model}
11
+ *
12
+ * @example
13
+ * Here's an example with streaming response:
14
+ * {@includeCode ../../test/models/gemini-chat-model.test.ts#example-gemini-chat-model-streaming}
15
+ */
2
16
  export declare class GeminiChatModel extends OpenAIChatModel {
3
17
  constructor(options?: OpenAIChatModelOptions);
4
18
  protected apiKeyEnvName: string;
@@ -4,6 +4,20 @@ exports.GeminiChatModel = void 0;
4
4
  const openai_chat_model_js_1 = require("./openai-chat-model.js");
5
5
  const GEMINI_BASE_URL = "https://generativelanguage.googleapis.com/v1beta/openai";
6
6
  const GEMINI_DEFAULT_CHAT_MODEL = "gemini-2.0-flash";
7
+ /**
8
+ * Implementation of the ChatModel interface for Google's Gemini API
9
+ *
10
+ * This model uses OpenAI-compatible API format to interact with Google's Gemini models,
11
+ * providing access to models like Gemini 1.5 and Gemini 2.0.
12
+ *
13
+ * @example
14
+ * Here's how to create and use a Gemini chat model:
15
+ * {@includeCode ../../test/models/gemini-chat-model.test.ts#example-gemini-chat-model}
16
+ *
17
+ * @example
18
+ * Here's an example with streaming response:
19
+ * {@includeCode ../../test/models/gemini-chat-model.test.ts#example-gemini-chat-model-streaming}
20
+ */
7
21
  class GeminiChatModel extends openai_chat_model_js_1.OpenAIChatModel {
8
22
  constructor(options) {
9
23
  super({
@@ -1,4 +1,20 @@
1
1
  import { OpenAIChatModel, type OpenAIChatModelOptions } from "./openai-chat-model.js";
2
+ /**
3
+ * Implementation of the ChatModel interface for Ollama
4
+ *
5
+ * This model allows you to run open-source LLMs locally using Ollama,
6
+ * with an OpenAI-compatible API interface.
7
+ *
8
+ * Default model: 'llama3.2'
9
+ *
10
+ * @example
11
+ * Here's how to create and use an Ollama chat model:
12
+ * {@includeCode ../../test/models/ollama-chat-model.test.ts#example-ollama-chat-model}
13
+ *
14
+ * @example
15
+ * Here's an example with streaming response:
16
+ * {@includeCode ../../test/models/ollama-chat-model.test.ts#example-ollama-chat-model-streaming}
17
+ */
2
18
  export declare class OllamaChatModel extends OpenAIChatModel {
3
19
  constructor(options?: OpenAIChatModelOptions);
4
20
  protected apiKeyEnvName: string;
@@ -4,6 +4,22 @@ exports.OllamaChatModel = void 0;
4
4
  const openai_chat_model_js_1 = require("./openai-chat-model.js");
5
5
  const OLLAMA_DEFAULT_BASE_URL = "http://localhost:11434/v1";
6
6
  const OLLAMA_DEFAULT_CHAT_MODEL = "llama3.2";
7
+ /**
8
+ * Implementation of the ChatModel interface for Ollama
9
+ *
10
+ * This model allows you to run open-source LLMs locally using Ollama,
11
+ * with an OpenAI-compatible API interface.
12
+ *
13
+ * Default model: 'llama3.2'
14
+ *
15
+ * @example
16
+ * Here's how to create and use an Ollama chat model:
17
+ * {@includeCode ../../test/models/ollama-chat-model.test.ts#example-ollama-chat-model}
18
+ *
19
+ * @example
20
+ * Here's an example with streaming response:
21
+ * {@includeCode ../../test/models/ollama-chat-model.test.ts#example-ollama-chat-model-streaming}
22
+ */
7
23
  class OllamaChatModel extends openai_chat_model_js_1.OpenAIChatModel {
8
24
  constructor(options) {
9
25
  super({
@@ -1,4 +1,20 @@
1
1
  import { OpenAIChatModel, type OpenAIChatModelOptions } from "./openai-chat-model.js";
2
+ /**
3
+ * Implementation of the ChatModel interface for OpenRouter service
4
+ *
5
+ * OpenRouter provides access to a variety of large language models through a unified API.
6
+ * This implementation uses the OpenAI-compatible interface to connect to OpenRouter's service.
7
+ *
8
+ * Default model: 'openai/gpt-4o'
9
+ *
10
+ * @example
11
+ * Here's how to create and use an OpenRouter chat model:
12
+ * {@includeCode ../../test/models/open-router-chat-model.test.ts#example-openrouter-chat-model}
13
+ *
14
+ * @example
15
+ * Here's an example with streaming response:
16
+ * {@includeCode ../../test/models/open-router-chat-model.test.ts#example-openrouter-chat-model-streaming}
17
+ */
2
18
  export declare class OpenRouterChatModel extends OpenAIChatModel {
3
19
  constructor(options?: OpenAIChatModelOptions);
4
20
  protected apiKeyEnvName: string;
@@ -4,6 +4,22 @@ exports.OpenRouterChatModel = void 0;
4
4
  const openai_chat_model_js_1 = require("./openai-chat-model.js");
5
5
  const OPEN_ROUTER_DEFAULT_CHAT_MODEL = "openai/gpt-4o";
6
6
  const OPEN_ROUTER_BASE_URL = "https://openrouter.ai/api/v1";
7
+ /**
8
+ * Implementation of the ChatModel interface for OpenRouter service
9
+ *
10
+ * OpenRouter provides access to a variety of large language models through a unified API.
11
+ * This implementation uses the OpenAI-compatible interface to connect to OpenRouter's service.
12
+ *
13
+ * Default model: 'openai/gpt-4o'
14
+ *
15
+ * @example
16
+ * Here's how to create and use an OpenRouter chat model:
17
+ * {@includeCode ../../test/models/open-router-chat-model.test.ts#example-openrouter-chat-model}
18
+ *
19
+ * @example
20
+ * Here's an example with streaming response:
21
+ * {@includeCode ../../test/models/open-router-chat-model.test.ts#example-openrouter-chat-model-streaming}
22
+ */
7
23
  class OpenRouterChatModel extends openai_chat_model_js_1.OpenAIChatModel {
8
24
  constructor(options) {
9
25
  super({
@@ -1,8 +1,8 @@
1
1
  import OpenAI from "openai";
2
2
  import type { ChatCompletionMessageParam, ChatCompletionTool } from "openai/resources";
3
3
  import { z } from "zod";
4
- import type { AgentInvokeOptions, AgentResponse } from "../agents/agent.js";
5
- import type { Context } from "../aigne/context.js";
4
+ import type { AgentProcessResult } from "../agents/agent.js";
5
+ import { type PromiseOrValue } from "../utils/type-utils.js";
6
6
  import { ChatModel, type ChatModelInput, type ChatModelInputMessage, type ChatModelInputTool, type ChatModelOptions, type ChatModelOutput, type Role } from "./chat-model.js";
7
7
  export interface OpenAIChatModelCapabilities {
8
8
  supportsNativeStructuredOutputs: boolean;
@@ -13,12 +13,36 @@ export interface OpenAIChatModelCapabilities {
13
13
  supportsToolStreaming: boolean;
14
14
  supportsTemperature: boolean;
15
15
  }
16
+ /**
17
+ * Configuration options for OpenAI Chat Model
18
+ */
16
19
  export interface OpenAIChatModelOptions {
20
+ /**
21
+ * API key for OpenAI API
22
+ *
23
+ * If not provided, will look for OPENAI_API_KEY in environment variables
24
+ */
17
25
  apiKey?: string;
26
+ /**
27
+ * Base URL for OpenAI API
28
+ *
29
+ * Useful for proxies or alternate endpoints
30
+ */
18
31
  baseURL?: string;
32
+ /**
33
+ * OpenAI model to use
34
+ *
35
+ * Defaults to 'gpt-4o-mini'
36
+ */
19
37
  model?: string;
38
+ /**
39
+ * Additional model options to control behavior
40
+ */
20
41
  modelOptions?: ChatModelOptions;
21
42
  }
43
+ /**
44
+ * @hidden
45
+ */
22
46
  export declare const openAIChatModelOptionsSchema: z.ZodObject<{
23
47
  apiKey: z.ZodOptional<z.ZodString>;
24
48
  baseURL: z.ZodOptional<z.ZodString>;
@@ -70,9 +94,31 @@ export declare const openAIChatModelOptionsSchema: z.ZodObject<{
70
94
  apiKey?: string | undefined;
71
95
  baseURL?: string | undefined;
72
96
  }>;
97
+ /**
98
+ * Implementation of the ChatModel interface for OpenAI's API
99
+ *
100
+ * This model provides access to OpenAI's capabilities including:
101
+ * - Text generation
102
+ * - Tool use with parallel tool calls
103
+ * - JSON structured output
104
+ * - Image understanding
105
+ *
106
+ * Default model: 'gpt-4o-mini'
107
+ *
108
+ * @example
109
+ * Here's how to create and use an OpenAI chat model:
110
+ * {@includeCode ../../test/models/openai-chat-model.test.ts#example-openai-chat-model}
111
+ *
112
+ * @example
113
+ * Here's an example with streaming response:
114
+ * {@includeCode ../../test/models/openai-chat-model.test.ts#example-openai-chat-model-streaming}
115
+ */
73
116
  export declare class OpenAIChatModel extends ChatModel {
74
117
  options?: OpenAIChatModelOptions | undefined;
75
118
  constructor(options?: OpenAIChatModelOptions | undefined);
119
+ /**
120
+ * @hidden
121
+ */
76
122
  protected _client?: OpenAI;
77
123
  protected apiKeyEnvName: string;
78
124
  protected apiKeyDefault: string | undefined;
@@ -85,18 +131,36 @@ export declare class OpenAIChatModel extends ChatModel {
85
131
  protected supportsTemperature: boolean;
86
132
  get client(): OpenAI;
87
133
  get modelOptions(): ChatModelOptions | undefined;
88
- process(input: ChatModelInput, _context: Context, options?: AgentInvokeOptions): Promise<AgentResponse<ChatModelOutput>>;
134
+ /**
135
+ * Process the input and generate a response
136
+ * @param input The input to process
137
+ * @returns The generated response
138
+ */
139
+ process(input: ChatModelInput): PromiseOrValue<AgentProcessResult<ChatModelOutput>>;
140
+ private _process;
89
141
  private getParallelToolCalls;
90
142
  private getRunMessages;
91
143
  private getRunResponseFormat;
92
144
  private requestStructuredOutput;
93
145
  private extractResultFromStream;
94
146
  }
147
+ /**
148
+ * @hidden
149
+ */
95
150
  export declare const ROLE_MAP: {
96
151
  [key in Role]: ChatCompletionMessageParam["role"];
97
152
  };
153
+ /**
154
+ * @hidden
155
+ */
98
156
  export declare function contentsFromInputMessages(messages: ChatModelInputMessage[]): Promise<ChatCompletionMessageParam[]>;
157
+ /**
158
+ * @hidden
159
+ */
99
160
  export declare function toolsFromInputTools(tools?: ChatModelInputTool[], options?: {
100
161
  addTypeToEmptyParameters?: boolean;
101
162
  }): ChatCompletionTool[] | undefined;
163
+ /**
164
+ * @hidden
165
+ */
102
166
  export declare function jsonSchemaToOpenAIJsonSchema(schema: Record<string, unknown>): Record<string, unknown>;
@@ -21,6 +21,9 @@ const OPENAI_CHAT_MODEL_CAPABILITIES = {
21
21
  "o4-mini": { supportsParallelToolCalls: false, supportsTemperature: false },
22
22
  "o3-mini": { supportsParallelToolCalls: false, supportsTemperature: false },
23
23
  };
24
+ /**
25
+ * @hidden
26
+ */
24
27
  exports.openAIChatModelOptionsSchema = zod_1.z.object({
25
28
  apiKey: zod_1.z.string().optional(),
26
29
  baseURL: zod_1.z.string().optional(),
@@ -36,6 +39,25 @@ exports.openAIChatModelOptionsSchema = zod_1.z.object({
36
39
  })
37
40
  .optional(),
38
41
  });
42
+ /**
43
+ * Implementation of the ChatModel interface for OpenAI's API
44
+ *
45
+ * This model provides access to OpenAI's capabilities including:
46
+ * - Text generation
47
+ * - Tool use with parallel tool calls
48
+ * - JSON structured output
49
+ * - Image understanding
50
+ *
51
+ * Default model: 'gpt-4o-mini'
52
+ *
53
+ * @example
54
+ * Here's how to create and use an OpenAI chat model:
55
+ * {@includeCode ../../test/models/openai-chat-model.test.ts#example-openai-chat-model}
56
+ *
57
+ * @example
58
+ * Here's an example with streaming response:
59
+ * {@includeCode ../../test/models/openai-chat-model.test.ts#example-openai-chat-model-streaming}
60
+ */
39
61
  class OpenAIChatModel extends chat_model_js_1.ChatModel {
40
62
  options;
41
63
  constructor(options) {
@@ -46,6 +68,9 @@ class OpenAIChatModel extends chat_model_js_1.ChatModel {
46
68
  const preset = options?.model ? OPENAI_CHAT_MODEL_CAPABILITIES[options.model] : undefined;
47
69
  Object.assign(this, preset);
48
70
  }
71
+ /**
72
+ * @hidden
73
+ */
49
74
  _client;
50
75
  apiKeyEnvName = "OPENAI_API_KEY";
51
76
  apiKeyDefault;
@@ -69,7 +94,15 @@ class OpenAIChatModel extends chat_model_js_1.ChatModel {
69
94
  get modelOptions() {
70
95
  return this.options?.modelOptions;
71
96
  }
72
- async process(input, _context, options) {
97
+ /**
98
+ * Process the input and generate a response
99
+ * @param input The input to process
100
+ * @returns The generated response
101
+ */
102
+ process(input) {
103
+ return this._process(input);
104
+ }
105
+ async _process(input) {
73
106
  const messages = await this.getRunMessages(input);
74
107
  const body = {
75
108
  model: this.options?.model || CHAT_MODEL_OPENAI_DEFAULT_MODEL,
@@ -95,7 +128,7 @@ class OpenAIChatModel extends chat_model_js_1.ChatModel {
95
128
  parallel_tool_calls: this.getParallelToolCalls(input),
96
129
  response_format: responseFormat,
97
130
  });
98
- if (options?.streaming && input.responseFormat?.type !== "json_schema") {
131
+ if (input.responseFormat?.type !== "json_schema") {
99
132
  return await this.extractResultFromStream(stream, false, true);
100
133
  }
101
134
  const result = await this.extractResultFromStream(stream, jsonMode);
@@ -264,12 +297,18 @@ class OpenAIChatModel extends chat_model_js_1.ChatModel {
264
297
  }
265
298
  }
266
299
  exports.OpenAIChatModel = OpenAIChatModel;
300
+ /**
301
+ * @hidden
302
+ */
267
303
  exports.ROLE_MAP = {
268
304
  system: "system",
269
305
  user: "user",
270
306
  agent: "assistant",
271
307
  tool: "tool",
272
308
  };
309
+ /**
310
+ * @hidden
311
+ */
273
312
  async function contentsFromInputMessages(messages) {
274
313
  return messages.map((i) => ({
275
314
  role: exports.ROLE_MAP[i.role],
@@ -299,6 +338,9 @@ async function contentsFromInputMessages(messages) {
299
338
  name: i.name,
300
339
  }));
301
340
  }
341
+ /**
342
+ * @hidden
343
+ */
302
344
  function toolsFromInputTools(tools, options) {
303
345
  return tools?.length
304
346
  ? tools.map((i) => {
@@ -317,6 +359,9 @@ function toolsFromInputTools(tools, options) {
317
359
  })
318
360
  : undefined;
319
361
  }
362
+ /**
363
+ * @hidden
364
+ */
320
365
  function jsonSchemaToOpenAIJsonSchema(schema) {
321
366
  if (schema?.type === "object") {
322
367
  const { required, properties } = schema;
@@ -1,4 +1,20 @@
1
1
  import { OpenAIChatModel, type OpenAIChatModelOptions } from "./openai-chat-model.js";
2
+ /**
3
+ * Implementation of the ChatModel interface for X.AI's API (Grok)
4
+ *
5
+ * This model uses OpenAI-compatible API format to interact with X.AI models,
6
+ * providing access to models like Grok.
7
+ *
8
+ * Default model: 'grok-2-latest'
9
+ *
10
+ * @example
11
+ * Here's how to create and use an X.AI chat model:
12
+ * {@includeCode ../../test/models/xai-chat-model.test.ts#example-xai-chat-model}
13
+ *
14
+ * @example
15
+ * Here's an example with streaming response:
16
+ * {@includeCode ../../test/models/xai-chat-model.test.ts#example-xai-chat-model-streaming}
17
+ */
2
18
  export declare class XAIChatModel extends OpenAIChatModel {
3
19
  constructor(options?: OpenAIChatModelOptions);
4
20
  protected apiKeyEnvName: string;
@@ -4,6 +4,22 @@ exports.XAIChatModel = void 0;
4
4
  const openai_chat_model_js_1 = require("./openai-chat-model.js");
5
5
  const XAI_DEFAULT_CHAT_MODEL = "grok-2-latest";
6
6
  const XAI_BASE_URL = "https://api.x.ai/v1";
7
+ /**
8
+ * Implementation of the ChatModel interface for X.AI's API (Grok)
9
+ *
10
+ * This model uses OpenAI-compatible API format to interact with X.AI models,
11
+ * providing access to models like Grok.
12
+ *
13
+ * Default model: 'grok-2-latest'
14
+ *
15
+ * @example
16
+ * Here's how to create and use an X.AI chat model:
17
+ * {@includeCode ../../test/models/xai-chat-model.test.ts#example-xai-chat-model}
18
+ *
19
+ * @example
20
+ * Here's an example with streaming response:
21
+ * {@includeCode ../../test/models/xai-chat-model.test.ts#example-xai-chat-model-streaming}
22
+ */
7
23
  class XAIChatModel extends openai_chat_model_js_1.OpenAIChatModel {
8
24
  constructor(options) {
9
25
  super({
@@ -1,20 +1,19 @@
1
1
  import type { GetPromptResult } from "@modelcontextprotocol/sdk/types.js";
2
2
  import { Agent, type Message } from "../agents/agent.js";
3
3
  import type { AIAgent } from "../agents/ai-agent.js";
4
- import type { AgentMemory } from "../agents/memory.js";
5
4
  import type { Context } from "../aigne/context.js";
5
+ import type { MemoryAgent } from "../memory/memory.js";
6
6
  import type { ChatModel, ChatModelInput } from "../models/chat-model.js";
7
7
  import { ChatMessagesTemplate } from "./template.js";
8
8
  export declare const MESSAGE_KEY = "$message";
9
- export declare const DEFAULT_MAX_HISTORY_MESSAGES = 10;
10
9
  export declare function createMessage<I extends Message>(message: string | I): I;
11
10
  export declare function getMessage(input: Message): string | undefined;
12
11
  export interface PromptBuilderOptions {
13
12
  instructions?: string | ChatMessagesTemplate;
14
13
  }
15
14
  export interface PromptBuilderBuildOptions {
16
- memory?: AgentMemory;
17
- context?: Context;
15
+ memory?: MemoryAgent | MemoryAgent[];
16
+ context: Context;
18
17
  agent?: AIAgent;
19
18
  input?: Message;
20
19
  model?: ChatModel;
@@ -37,6 +36,7 @@ export declare class PromptBuilder {
37
36
  toolAgents?: Agent[];
38
37
  }>;
39
38
  private buildMessages;
39
+ private convertMemoriesToMessages;
40
40
  private buildResponseFormat;
41
41
  private buildTools;
42
42
  }
@@ -1,16 +1,17 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PromptBuilder = exports.DEFAULT_MAX_HISTORY_MESSAGES = exports.MESSAGE_KEY = void 0;
3
+ exports.PromptBuilder = exports.MESSAGE_KEY = void 0;
4
4
  exports.createMessage = createMessage;
5
5
  exports.getMessage = getMessage;
6
6
  const promises_1 = require("node:fs/promises");
7
+ const yaml_1 = require("yaml");
7
8
  const zod_1 = require("zod");
8
9
  const agent_js_1 = require("../agents/agent.js");
9
10
  const json_schema_js_1 = require("../utils/json-schema.js");
10
11
  const type_utils_js_1 = require("../utils/type-utils.js");
12
+ const memory_message_template_js_1 = require("./prompts/memory-message-template.js");
11
13
  const template_js_1 = require("./template.js");
12
14
  exports.MESSAGE_KEY = "$message";
13
- exports.DEFAULT_MAX_HISTORY_MESSAGES = 10;
14
15
  function createMessage(message) {
15
16
  return typeof message === "string"
16
17
  ? { [exports.MESSAGE_KEY]: message }
@@ -72,26 +73,20 @@ class PromptBuilder {
72
73
  instructions;
73
74
  async build(options) {
74
75
  return {
75
- messages: this.buildMessages(options),
76
+ messages: await this.buildMessages(options),
76
77
  responseFormat: this.buildResponseFormat(options),
77
78
  ...this.buildTools(options),
78
79
  };
79
80
  }
80
- buildMessages(options) {
81
+ async buildMessages(options) {
81
82
  const { input } = options;
82
83
  const messages = (typeof this.instructions === "string"
83
84
  ? template_js_1.ChatMessagesTemplate.from([template_js_1.SystemMessageTemplate.from(this.instructions)])
84
85
  : this.instructions)?.format(options.input) ?? [];
85
- const memory = options.memory ?? options.agent?.memory;
86
- if (memory?.enabled) {
87
- const k = memory.maxMemoriesInChat ?? exports.DEFAULT_MAX_HISTORY_MESSAGES;
88
- const histories = memory.memories.slice(-k);
89
- if (histories?.length)
90
- messages.push(...histories.map((i) => ({
91
- role: i.role,
92
- content: convertMessageToContent(i.content),
93
- name: i.source,
94
- })));
86
+ for (const memory of (0, type_utils_js_1.orArrayToArray)(options.memory ?? options.agent?.memories)) {
87
+ const memories = (await memory.retrieve({ search: input && getMessage(input) }, options.context))?.memories;
88
+ if (memories?.length)
89
+ messages.push(...this.convertMemoriesToMessages(memories, options));
95
90
  }
96
91
  const content = input && getMessage(input);
97
92
  // add user input if it's not the same as the last message
@@ -100,6 +95,15 @@ class PromptBuilder {
100
95
  }
101
96
  return messages;
102
97
  }
98
+ convertMemoriesToMessages(memories, options) {
99
+ const str = (0, yaml_1.stringify)(memories.map((i) => i.content));
100
+ return [
101
+ {
102
+ role: "system",
103
+ content: template_js_1.PromptTemplate.from(options.agent?.memoryPromptTemplate || memory_message_template_js_1.MEMORY_MESSAGE_TEMPLATE).format({ memories: str }),
104
+ },
105
+ ];
106
+ }
103
107
  buildResponseFormat(options) {
104
108
  const outputSchema = options.outputSchema || options.agent?.outputSchema;
105
109
  if (!outputSchema)
@@ -119,6 +123,7 @@ class PromptBuilder {
119
123
  buildTools(options) {
120
124
  const toolAgents = (0, type_utils_js_1.unique)((options.context?.skills ?? [])
121
125
  .concat(options.agent?.skills ?? [])
126
+ .concat(options.agent?.memoryAgentsAsTools ? options.agent.memories : [])
122
127
  // TODO: support nested tools?
123
128
  .flatMap((i) => (i.isInvokable ? i.skills.concat(i) : i.skills)), (i) => i.name);
124
129
  const tools = toolAgents.map((i) => ({
@@ -175,7 +180,3 @@ function isFromPath(value) {
175
180
  function isEmptyObjectType(schema) {
176
181
  return schema instanceof zod_1.ZodObject && Object.keys(schema.shape).length === 0;
177
182
  }
178
- function convertMessageToContent(i) {
179
- const str = i[exports.MESSAGE_KEY];
180
- return !(0, type_utils_js_1.isNil)(str) ? (typeof str === "string" ? str : JSON.stringify(str)) : JSON.stringify(i);
181
- }
@@ -0,0 +1 @@
1
+ export declare const MEMORY_MESSAGE_TEMPLATE = "Here are some useful memories to consider:\n\n<memories>\n{{memories}}\n</memories>\n";
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MEMORY_MESSAGE_TEMPLATE = void 0;
4
+ exports.MEMORY_MESSAGE_TEMPLATE = `\
5
+ Here are some useful memories to consider:
6
+
7
+ <memories>
8
+ {{memories}}
9
+ </memories>
10
+ `;
@@ -5,8 +5,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.ChatMessagesTemplate = exports.ToolMessageTemplate = exports.AgentMessageTemplate = exports.UserMessageTemplate = exports.SystemMessageTemplate = exports.ChatMessageTemplate = exports.PromptTemplate = void 0;
7
7
  exports.parseChatMessages = parseChatMessages;
8
+ const node_util_1 = require("node:util");
8
9
  const mustache_1 = __importDefault(require("mustache"));
9
10
  const zod_1 = require("zod");
11
+ const type_utils_js_1 = require("../utils/type-utils.js");
10
12
  class PromptTemplate {
11
13
  template;
12
14
  static from(template) {
@@ -88,7 +90,9 @@ class ToolMessageTemplate extends ChatMessageTemplate {
88
90
  return new ToolMessageTemplate(content, toolCallId, name);
89
91
  }
90
92
  constructor(content, toolCallId, name) {
91
- super("tool", typeof content === "string" ? content : JSON.stringify(content), name);
93
+ super("tool", typeof content === "string"
94
+ ? content
95
+ : (0, type_utils_js_1.tryOrThrow)(() => JSON.stringify(content), `Failed to stringify tool content. toolCallId: ${toolCallId}, content: ${(0, node_util_1.inspect)(content)}`), name);
92
96
  this.toolCallId = toolCallId;
93
97
  }
94
98
  format(variables) {