@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
@@ -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
+ }
@@ -0,0 +1,86 @@
1
+ import { type ZodType, z } from "zod";
2
+ import { Agent, type AgentOptions, type Message } from "../agents/agent.js";
3
+ import type { Memory } from "./memory.js";
4
+ /**
5
+ * Input for memory recording operations.
6
+ *
7
+ * This interface represents the data needed to record new memories
8
+ * in the system. The content array can contain any type of data that
9
+ * should be stored as memories.
10
+ */
11
+ export interface MemoryRecorderInput extends Message {
12
+ /**
13
+ * Array of content items to record as memories.
14
+ * Each item in this array will typically be converted into a separate memory entry.
15
+ */
16
+ content: unknown[];
17
+ }
18
+ /**
19
+ * @hidden
20
+ */
21
+ export declare const memoryRecorderInputSchema: ZodType<MemoryRecorderInput>;
22
+ /**
23
+ * Output from memory recording operations.
24
+ *
25
+ * This interface represents the result of recording new memories,
26
+ * including the newly created memory objects with their IDs and timestamps.
27
+ */
28
+ export interface MemoryRecorderOutput extends Message {
29
+ /**
30
+ * Array of newly created memory objects.
31
+ * Each memory includes a unique ID, the stored content, and a creation timestamp.
32
+ */
33
+ memories: Memory[];
34
+ }
35
+ /**
36
+ * @hidden
37
+ */
38
+ export declare const memoryRecorderOutputSchema: z.ZodObject<{
39
+ memories: z.ZodArray<z.ZodObject<{
40
+ id: z.ZodString;
41
+ content: ZodType<{}, z.ZodTypeDef, {}>;
42
+ createdAt: z.ZodString;
43
+ }, "strip", z.ZodTypeAny, {
44
+ content: {};
45
+ id: string;
46
+ createdAt: string;
47
+ }, {
48
+ content: {};
49
+ id: string;
50
+ createdAt: string;
51
+ }>, "many">;
52
+ }, "strip", z.ZodTypeAny, {
53
+ memories: {
54
+ content: {};
55
+ id: string;
56
+ createdAt: string;
57
+ }[];
58
+ }, {
59
+ memories: {
60
+ content: {};
61
+ id: string;
62
+ createdAt: string;
63
+ }[];
64
+ }>;
65
+ /**
66
+ * Abstract base class for agents that record and store memories.
67
+ *
68
+ * The MemoryRecorder serves as a foundation for implementing specific memory storage
69
+ * mechanisms. Implementations of this class are responsible for:
70
+ *
71
+ * 1. Converting input content into standardized memory objects
72
+ * 2. Assigning unique IDs to new memories
73
+ * 3. Storing memories in an appropriate backend (database, file system, etc.)
74
+ * 4. Ensuring proper timestamping of memories
75
+ *
76
+ * Custom implementations should extend this class and provide concrete
77
+ * implementations of the process method to handle the actual storage logic.
78
+ */
79
+ export declare abstract class MemoryRecorder extends Agent<MemoryRecorderInput, MemoryRecorderOutput> {
80
+ /**
81
+ * Creates a new MemoryRecorder instance with predefined input and output schemas.
82
+ *
83
+ * @param options - Configuration options for the memory recorder agent
84
+ */
85
+ constructor(options: Omit<AgentOptions<MemoryRecorderInput, MemoryRecorderOutput>, "inputSchema" | "outputSchema">);
86
+ }
@@ -0,0 +1,99 @@
1
+ import { z } from "zod";
2
+ import { Agent, type AgentOptions, type Message } from "../agents/agent.js";
3
+ import type { Memory } from "./memory.js";
4
+ /**
5
+ * Input for memory retrieval operations.
6
+ *
7
+ * This interface defines the parameters that can be used to query and filter
8
+ * memories when retrieving them from storage.
9
+ */
10
+ export interface MemoryRetrieverInput extends Message {
11
+ /**
12
+ * Maximum number of memories to retrieve.
13
+ * Used for pagination or limiting result set size.
14
+ */
15
+ limit?: number;
16
+ /**
17
+ * Search term to filter memories by.
18
+ * How the search is implemented depends on the specific retriever implementation.
19
+ */
20
+ search?: string;
21
+ }
22
+ /**
23
+ * Output from memory retrieval operations.
24
+ *
25
+ * This interface represents the result of retrieving memories from storage,
26
+ * containing an array of memory objects that match the query criteria.
27
+ */
28
+ export interface MemoryRetrieverOutput extends Message {
29
+ /**
30
+ * Array of retrieved memory objects.
31
+ * Each memory includes its ID, content, and creation timestamp.
32
+ */
33
+ memories: Memory[];
34
+ }
35
+ /**
36
+ * @hidden
37
+ */
38
+ export declare const memoryRetrieverInputSchema: z.ZodObject<{
39
+ limit: z.ZodOptional<z.ZodNumber>;
40
+ search: z.ZodOptional<z.ZodString>;
41
+ }, "strip", z.ZodTypeAny, {
42
+ limit?: number | undefined;
43
+ search?: string | undefined;
44
+ }, {
45
+ limit?: number | undefined;
46
+ search?: string | undefined;
47
+ }>;
48
+ /**
49
+ * @hidden
50
+ */
51
+ export declare const memoryRetrieverOutputSchema: z.ZodObject<{
52
+ memories: z.ZodArray<z.ZodObject<{
53
+ id: z.ZodString;
54
+ content: z.ZodType<{}, z.ZodTypeDef, {}>;
55
+ createdAt: z.ZodString;
56
+ }, "strip", z.ZodTypeAny, {
57
+ content: {};
58
+ id: string;
59
+ createdAt: string;
60
+ }, {
61
+ content: {};
62
+ id: string;
63
+ createdAt: string;
64
+ }>, "many">;
65
+ }, "strip", z.ZodTypeAny, {
66
+ memories: {
67
+ content: {};
68
+ id: string;
69
+ createdAt: string;
70
+ }[];
71
+ }, {
72
+ memories: {
73
+ content: {};
74
+ id: string;
75
+ createdAt: string;
76
+ }[];
77
+ }>;
78
+ /**
79
+ * Abstract base class for agents that retrieve memories from storage.
80
+ *
81
+ * The MemoryRetriever serves as a foundation for implementing specific memory
82
+ * retrieval mechanisms. Implementations of this class are responsible for:
83
+ *
84
+ * 1. Querying a memory storage backend to find relevant memories
85
+ * 2. Filtering memories based on search criteria
86
+ * 3. Limiting the number of results returned
87
+ * 4. Potentially implementing sorting, ranking, or relevance-based retrieval
88
+ *
89
+ * Custom implementations should extend this class and provide concrete
90
+ * implementations of the process method to handle the actual retrieval logic.
91
+ */
92
+ export declare abstract class MemoryRetriever extends Agent<MemoryRetrieverInput, MemoryRetrieverOutput> {
93
+ /**
94
+ * Creates a new MemoryRetriever instance with predefined input and output schemas.
95
+ *
96
+ * @param options - Configuration options for the memory retriever agent
97
+ */
98
+ constructor(options: Omit<AgentOptions<MemoryRetrieverInput, MemoryRetrieverOutput>, "inputSchema" | "outputSchema">);
99
+ }
@@ -1,8 +1,10 @@
1
1
  import { BedrockRuntimeClient } from "@aws-sdk/client-bedrock-runtime";
2
2
  import { z } from "zod";
3
- import type { AgentInvokeOptions, AgentResponse } from "../agents/agent.js";
4
- import type { Context } from "../aigne/context.js";
3
+ import type { AgentResponse } from "../agents/agent.js";
5
4
  import { ChatModel, type ChatModelInput, type ChatModelOptions, type ChatModelOutput } from "./chat-model.js";
5
+ /**
6
+ * @hidden
7
+ */
6
8
  export declare function extractLastJsonObject(text: string): string | null;
7
9
  export interface BedrockChatModelOptions {
8
10
  accessKeyId?: string;
@@ -11,6 +13,9 @@ export interface BedrockChatModelOptions {
11
13
  model?: string;
12
14
  modelOptions?: ChatModelOptions;
13
15
  }
16
+ /**
17
+ * @hidden
18
+ */
14
19
  export declare const bedrockChatModelOptionsSchema: z.ZodObject<{
15
20
  region: z.ZodOptional<z.ZodString>;
16
21
  model: z.ZodOptional<z.ZodString>;
@@ -62,9 +67,13 @@ export declare const bedrockChatModelOptionsSchema: z.ZodObject<{
62
67
  export declare class BedrockChatModel extends ChatModel {
63
68
  options?: BedrockChatModelOptions | undefined;
64
69
  constructor(options?: BedrockChatModelOptions | undefined);
70
+ /**
71
+ * @hidden
72
+ */
65
73
  protected _client?: BedrockRuntimeClient;
66
74
  get client(): BedrockRuntimeClient;
67
75
  get modelOptions(): ChatModelOptions | undefined;
68
- process(input: ChatModelInput, _context: Context, options?: AgentInvokeOptions): Promise<AgentResponse<ChatModelOutput>>;
76
+ process(input: ChatModelInput): Promise<AgentResponse<ChatModelOutput>>;
69
77
  private extractResultFromStream;
78
+ private requestStructuredOutput;
70
79
  }
@@ -1,26 +1,154 @@
1
- import { Agent, type Message } from "../agents/agent.js";
1
+ import { Agent, type AgentProcessResult, type Message } from "../agents/agent.js";
2
2
  import type { Context } from "../aigne/context.js";
3
+ import type { PromiseOrValue } from "../utils/type-utils.js";
4
+ /**
5
+ * ChatModel is an abstract base class for interacting with Large Language Models (LLMs).
6
+ *
7
+ * This class extends the Agent class and provides a common interface for handling model inputs,
8
+ * outputs, and capabilities. Specific model implementations (like OpenAI, Anthropic, etc.)
9
+ * should inherit from this class and implement their specific functionalities.
10
+ *
11
+ * @example
12
+ * Here's how to implement a custom ChatModel:
13
+ * {@includeCode ../../test/models/chat-model.test.ts#example-chat-model}
14
+ *
15
+ * @example
16
+ * Here's an example showing streaming response with readable stream:
17
+ * {@includeCode ../../test/models/chat-model.test.ts#example-chat-model-streaming}
18
+ *
19
+ * @example
20
+ * Here's an example showing streaming response with async generator:
21
+ * {@includeCode ../../test/models/chat-model.test.ts#example-chat-model-streaming-async-generator}
22
+ *
23
+ * @example
24
+ * Here's an example with tool calls:
25
+ * {@includeCode ../../test/models/chat-model.test.ts#example-chat-model-tools}
26
+ */
3
27
  export declare abstract class ChatModel extends Agent<ChatModelInput, ChatModelOutput> {
4
28
  constructor();
29
+ /**
30
+ * Indicates whether the model supports parallel tool calls
31
+ *
32
+ * Defaults to true, subclasses can override this property based on
33
+ * specific model capabilities
34
+ */
5
35
  protected supportsParallelToolCalls: boolean;
36
+ /**
37
+ * Gets the model's supported capabilities
38
+ *
39
+ * Currently returns capabilities including: whether parallel tool calls are supported
40
+ *
41
+ * @returns An object containing model capabilities
42
+ */
6
43
  getModelCapabilities(): {
7
44
  supportsParallelToolCalls: boolean;
8
45
  };
9
46
  private validateToolNames;
47
+ /**
48
+ * Performs preprocessing operations before handling input
49
+ *
50
+ * Primarily checks if token usage exceeds limits, throwing an exception if limits are exceeded
51
+ *
52
+ * @param input Input message
53
+ * @param context Execution context
54
+ * @throws Error if token usage exceeds maximum limit
55
+ */
10
56
  protected preprocess(input: ChatModelInput, context: Context): void;
57
+ /**
58
+ * Performs postprocessing operations after handling output
59
+ *
60
+ * Primarily updates token usage statistics in the context
61
+ *
62
+ * @param input Input message
63
+ * @param output Output message
64
+ * @param context Execution context
65
+ */
11
66
  protected postprocess(input: ChatModelInput, output: ChatModelOutput, context: Context): void;
67
+ /**
68
+ * Processes input messages and generates model responses
69
+ *
70
+ * This is the core method that must be implemented by all ChatModel subclasses.
71
+ * It handles the communication with the underlying language model,
72
+ * processes the input messages, and generates appropriate responses.
73
+ *
74
+ * Implementations should handle:
75
+ * - Conversion of input format to model-specific format
76
+ * - Sending requests to the language model
77
+ * - Processing model responses
78
+ * - Handling streaming responses if supported
79
+ * - Proper error handling and retries
80
+ * - Token counting and usage tracking
81
+ * - Tool call processing if applicable
82
+ *
83
+ * @param input - The standardized input containing messages and model options
84
+ * @param context - The execution context with settings and state
85
+ * @returns A promise or direct value containing the model's response
86
+ */
87
+ abstract process(input: ChatModelInput, context: Context): PromiseOrValue<AgentProcessResult<ChatModelOutput>>;
12
88
  }
89
+ /**
90
+ * Input message format for ChatModel
91
+ *
92
+ * Contains an array of messages to send to the model, response format settings,
93
+ * tool definitions, and model-specific options
94
+ *
95
+ * @example
96
+ * Here's a basic ChatModel input example:
97
+ * {@includeCode ../../test/models/chat-model.test.ts#example-chat-model}
98
+ *
99
+ * @example
100
+ * Here's an example with tool calling:
101
+ * {@includeCode ../../test/models/chat-model.test.ts#example-chat-model-tools}
102
+ */
13
103
  export interface ChatModelInput extends Message {
104
+ /**
105
+ * Array of messages to send to the model
106
+ */
14
107
  messages: ChatModelInputMessage[];
108
+ /**
109
+ * Specifies the expected response format
110
+ */
15
111
  responseFormat?: ChatModelInputResponseFormat;
112
+ /**
113
+ * List of tools available for the model to use
114
+ */
16
115
  tools?: ChatModelInputTool[];
116
+ /**
117
+ * Specifies the tool selection strategy
118
+ */
17
119
  toolChoice?: ChatModelInputToolChoice;
120
+ /**
121
+ * Model-specific configuration options
122
+ */
18
123
  modelOptions?: ChatModelOptions;
19
124
  }
125
+ /**
126
+ * Message role types
127
+ *
128
+ * - system: System instructions
129
+ * - user: User messages
130
+ * - agent: Agent/assistant messages
131
+ * - tool: Tool call responses
132
+ */
20
133
  export type Role = "system" | "user" | "agent" | "tool";
134
+ /**
135
+ * Structure of input messages
136
+ *
137
+ * Defines the format of each message sent to the model, including
138
+ * role, content, and tool call related information
139
+ */
21
140
  export interface ChatModelInputMessage {
141
+ /**
142
+ * Role of the message (system, user, agent, or tool)
143
+ */
22
144
  role: Role;
145
+ /**
146
+ * Message content, can be text or multimodal content array
147
+ */
23
148
  content?: ChatModelInputMessageContent;
149
+ /**
150
+ * Tool call details when the agent wants to execute tool calls
151
+ */
24
152
  toolCalls?: {
25
153
  id: string;
26
154
  type: "function";
@@ -29,18 +157,44 @@ export interface ChatModelInputMessage {
29
157
  arguments: Message;
30
158
  };
31
159
  }[];
160
+ /**
161
+ * For tool response messages, specifies the corresponding tool call ID
162
+ */
32
163
  toolCallId?: string;
164
+ /**
165
+ * Name of the message sender (for multi-agent scenarios)
166
+ */
33
167
  name?: string;
34
168
  }
169
+ /**
170
+ * Type of input message content
171
+ *
172
+ * Can be a simple string, or a mixed array of text and image content
173
+ */
35
174
  export type ChatModelInputMessageContent = string | (TextContent | ImageUrlContent)[];
175
+ /**
176
+ * Text content type
177
+ *
178
+ * Used for text parts of message content
179
+ */
36
180
  export type TextContent = {
37
181
  type: "text";
38
182
  text: string;
39
183
  };
184
+ /**
185
+ * Image URL content type
186
+ *
187
+ * Used for image parts of message content, referencing images via URL
188
+ */
40
189
  export type ImageUrlContent = {
41
190
  type: "image_url";
42
191
  url: string;
43
192
  };
193
+ /**
194
+ * Model response format settings
195
+ *
196
+ * Can be specified as plain text format or according to a JSON Schema
197
+ */
44
198
  export type ChatModelInputResponseFormat = {
45
199
  type: "text";
46
200
  } | {
@@ -52,14 +206,51 @@ export type ChatModelInputResponseFormat = {
52
206
  strict?: boolean;
53
207
  };
54
208
  };
209
+ /**
210
+ * Tool definition provided to the model
211
+ *
212
+ * Defines a function tool, including name, description and parameter structure
213
+ *
214
+ * @example
215
+ * Here's an example showing how to use tools:
216
+ * {@includeCode ../../test/models/chat-model.test.ts#example-chat-model-tools}
217
+ */
55
218
  export interface ChatModelInputTool {
219
+ /**
220
+ * Tool type, currently only "function" is supported
221
+ */
56
222
  type: "function";
223
+ /**
224
+ * Function tool definition
225
+ */
57
226
  function: {
227
+ /**
228
+ * Function name
229
+ */
58
230
  name: string;
231
+ /**
232
+ * Function description
233
+ */
59
234
  description?: string;
235
+ /**
236
+ * Function parameter structure definition
237
+ */
60
238
  parameters: object;
61
239
  };
62
240
  }
241
+ /**
242
+ * Tool selection strategy
243
+ *
244
+ * Determines how the model selects and uses tools:
245
+ * - "auto": Automatically decides whether to use tools
246
+ * - "none": Does not use any tools
247
+ * - "required": Must use tools
248
+ * - object: Specifies a particular tool function
249
+ *
250
+ * @example
251
+ * Here's an example showing how to use tools:
252
+ * {@includeCode ../../test/models/chat-model.test.ts#example-chat-model-tools}
253
+ */
63
254
  export type ChatModelInputToolChoice = "auto" | "none" | "required" | {
64
255
  type: "function";
65
256
  function: {
@@ -67,30 +258,116 @@ export type ChatModelInputToolChoice = "auto" | "none" | "required" | {
67
258
  description?: string;
68
259
  };
69
260
  };
261
+ /**
262
+ * Model-specific configuration options
263
+ *
264
+ * Contains various parameters for controlling model behavior, such as model name, temperature, etc.
265
+ */
70
266
  export interface ChatModelOptions {
267
+ /**
268
+ * Model name or version
269
+ */
71
270
  model?: string;
271
+ /**
272
+ * Temperature parameter, controls randomness (0-1)
273
+ */
72
274
  temperature?: number;
275
+ /**
276
+ * Top-p parameter, controls vocabulary diversity
277
+ */
73
278
  topP?: number;
279
+ /**
280
+ * Frequency penalty parameter, reduces repetition
281
+ */
74
282
  frequencyPenalty?: number;
283
+ /**
284
+ * Presence penalty parameter, encourages diversity
285
+ */
75
286
  presencePenalty?: number;
287
+ /**
288
+ * Whether to allow parallel tool calls
289
+ */
76
290
  parallelToolCalls?: boolean;
77
291
  }
292
+ /**
293
+ * Output message format for ChatModel
294
+ *
295
+ * Contains model response content, which can be text, JSON data, tool calls, and usage statistics
296
+ *
297
+ * @example
298
+ * Here's a basic output example:
299
+ * {@includeCode ../../test/models/chat-model.test.ts#example-chat-model}
300
+ *
301
+ * @example
302
+ * Here's an example with tool calls:
303
+ * {@includeCode ../../test/models/chat-model.test.ts#example-chat-model-tools}
304
+ */
78
305
  export interface ChatModelOutput extends Message {
306
+ /**
307
+ * Text format response content
308
+ */
79
309
  text?: string;
310
+ /**
311
+ * JSON format response content
312
+ */
80
313
  json?: object;
314
+ /**
315
+ * List of tools the model requested to call
316
+ */
81
317
  toolCalls?: ChatModelOutputToolCall[];
318
+ /**
319
+ * Token usage statistics
320
+ */
82
321
  usage?: ChatModelOutputUsage;
322
+ /**
323
+ * Model name or version used
324
+ */
83
325
  model?: string;
84
326
  }
327
+ /**
328
+ * Tool call information in model output
329
+ *
330
+ * Describes tool calls requested by the model, including tool ID and call parameters
331
+ *
332
+ * @example
333
+ * Here's an example with tool calls:
334
+ * {@includeCode ../../test/models/chat-model.test.ts#example-chat-model-tools}
335
+ */
85
336
  export interface ChatModelOutputToolCall {
337
+ /**
338
+ * Unique ID of the tool call
339
+ */
86
340
  id: string;
341
+ /**
342
+ * Tool type, currently only "function" is supported
343
+ */
87
344
  type: "function";
345
+ /**
346
+ * Function call details
347
+ */
88
348
  function: {
349
+ /**
350
+ * Name of the function being called
351
+ */
89
352
  name: string;
353
+ /**
354
+ * Arguments for the function call
355
+ */
90
356
  arguments: Message;
91
357
  };
92
358
  }
359
+ /**
360
+ * Model usage statistics
361
+ *
362
+ * Records the number of input and output tokens for tracking model usage
363
+ */
93
364
  export interface ChatModelOutputUsage {
365
+ /**
366
+ * Number of input tokens
367
+ */
94
368
  inputTokens: number;
369
+ /**
370
+ * Number of output tokens
371
+ */
95
372
  outputTokens: number;
96
373
  }