@aigne/core 1.15.0 → 1.17.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 (194) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/README.md +9 -7
  3. package/README.zh.md +9 -7
  4. package/lib/cjs/agents/agent.d.ts +168 -29
  5. package/lib/cjs/agents/agent.js +152 -56
  6. package/lib/cjs/agents/ai-agent.d.ts +7 -7
  7. package/lib/cjs/agents/ai-agent.js +19 -15
  8. package/lib/{esm/models → cjs/agents}/chat-model.d.ts +29 -19
  9. package/lib/cjs/{models → agents}/chat-model.js +56 -15
  10. package/lib/cjs/agents/guide-rail-agent.d.ts +62 -0
  11. package/lib/cjs/agents/guide-rail-agent.js +14 -0
  12. package/lib/cjs/agents/mcp-agent.d.ts +3 -4
  13. package/lib/cjs/agents/mcp-agent.js +11 -11
  14. package/lib/cjs/agents/team-agent.d.ts +7 -8
  15. package/lib/cjs/agents/team-agent.js +10 -10
  16. package/lib/cjs/agents/user-agent.d.ts +4 -4
  17. package/lib/cjs/agents/user-agent.js +10 -10
  18. package/lib/cjs/aigne/aigne.d.ts +13 -11
  19. package/lib/cjs/aigne/aigne.js +7 -6
  20. package/lib/cjs/aigne/context.d.ts +19 -8
  21. package/lib/cjs/aigne/context.js +27 -7
  22. package/lib/cjs/index.d.ts +2 -1
  23. package/lib/cjs/index.js +2 -1
  24. package/lib/cjs/loader/agent-yaml.d.ts +2 -2
  25. package/lib/cjs/loader/index.d.ts +18 -11
  26. package/lib/cjs/loader/index.js +8 -27
  27. package/lib/cjs/memory/default-memory/default-memory-storage/index.d.ts +30 -0
  28. package/lib/cjs/memory/default-memory/default-memory-storage/index.js +69 -0
  29. package/lib/cjs/memory/default-memory/default-memory-storage/migrate.d.ts +7 -0
  30. package/lib/cjs/memory/default-memory/default-memory-storage/migrate.js +53 -0
  31. package/lib/cjs/memory/default-memory/default-memory-storage/migrations/20250523165801-init.d.ts +7 -0
  32. package/lib/cjs/memory/default-memory/default-memory-storage/migrations/20250523165801-init.js +33 -0
  33. package/lib/cjs/memory/default-memory/default-memory-storage/models/memory.d.ts +10 -0
  34. package/lib/cjs/memory/default-memory/default-memory-storage/models/memory.js +32 -0
  35. package/lib/cjs/memory/default-memory/index.d.ts +10 -0
  36. package/lib/cjs/memory/{default-memory.js → default-memory/index.js} +16 -25
  37. package/lib/cjs/memory/default-memory/storage.d.ts +13 -0
  38. package/lib/cjs/memory/default-memory/storage.js +6 -0
  39. package/lib/cjs/memory/memory.d.ts +3 -2
  40. package/lib/cjs/memory/memory.js +1 -1
  41. package/lib/cjs/memory/retriever.d.ts +2 -2
  42. package/lib/cjs/prompt/prompt-builder.d.ts +10 -4
  43. package/lib/cjs/prompt/prompt-builder.js +4 -4
  44. package/lib/cjs/prompt/template.d.ts +3 -3
  45. package/lib/cjs/utils/json-schema.js +1 -1
  46. package/lib/cjs/utils/logger.d.ts +33 -8
  47. package/lib/cjs/utils/logger.js +63 -5
  48. package/lib/cjs/utils/model-utils.d.ts +1 -1
  49. package/lib/cjs/utils/stream-utils.d.ts +3 -1
  50. package/lib/cjs/utils/stream-utils.js +31 -1
  51. package/lib/cjs/utils/type-utils.d.ts +7 -2
  52. package/lib/dts/agents/agent.d.ts +168 -29
  53. package/lib/dts/agents/ai-agent.d.ts +7 -7
  54. package/lib/{cjs/models → dts/agents}/chat-model.d.ts +29 -19
  55. package/lib/dts/agents/guide-rail-agent.d.ts +62 -0
  56. package/lib/dts/agents/mcp-agent.d.ts +3 -4
  57. package/lib/dts/agents/team-agent.d.ts +7 -8
  58. package/lib/dts/agents/user-agent.d.ts +4 -4
  59. package/lib/dts/aigne/aigne.d.ts +13 -11
  60. package/lib/dts/aigne/context.d.ts +19 -8
  61. package/lib/dts/index.d.ts +2 -1
  62. package/lib/dts/loader/agent-yaml.d.ts +2 -2
  63. package/lib/dts/loader/index.d.ts +18 -11
  64. package/lib/dts/memory/default-memory/default-memory-storage/index.d.ts +30 -0
  65. package/lib/dts/memory/default-memory/default-memory-storage/migrate.d.ts +7 -0
  66. package/lib/dts/memory/default-memory/default-memory-storage/migrations/20250523165801-init.d.ts +7 -0
  67. package/lib/dts/memory/default-memory/default-memory-storage/models/memory.d.ts +10 -0
  68. package/lib/dts/memory/default-memory/index.d.ts +10 -0
  69. package/lib/dts/memory/default-memory/storage.d.ts +13 -0
  70. package/lib/dts/memory/memory.d.ts +3 -2
  71. package/lib/dts/memory/retriever.d.ts +2 -2
  72. package/lib/dts/prompt/prompt-builder.d.ts +10 -4
  73. package/lib/dts/prompt/template.d.ts +3 -3
  74. package/lib/dts/utils/logger.d.ts +33 -8
  75. package/lib/dts/utils/model-utils.d.ts +1 -1
  76. package/lib/dts/utils/stream-utils.d.ts +3 -1
  77. package/lib/dts/utils/type-utils.d.ts +7 -2
  78. package/lib/esm/agents/agent.d.ts +168 -29
  79. package/lib/esm/agents/agent.js +152 -56
  80. package/lib/esm/agents/ai-agent.d.ts +7 -7
  81. package/lib/esm/agents/ai-agent.js +18 -14
  82. package/lib/{dts/models → esm/agents}/chat-model.d.ts +29 -19
  83. package/lib/esm/{models → agents}/chat-model.js +56 -15
  84. package/lib/esm/agents/guide-rail-agent.d.ts +62 -0
  85. package/lib/esm/agents/guide-rail-agent.js +11 -0
  86. package/lib/esm/agents/mcp-agent.d.ts +3 -4
  87. package/lib/esm/agents/mcp-agent.js +11 -11
  88. package/lib/esm/agents/team-agent.d.ts +7 -8
  89. package/lib/esm/agents/team-agent.js +10 -10
  90. package/lib/esm/agents/user-agent.d.ts +4 -4
  91. package/lib/esm/agents/user-agent.js +10 -10
  92. package/lib/esm/aigne/aigne.d.ts +13 -11
  93. package/lib/esm/aigne/aigne.js +7 -6
  94. package/lib/esm/aigne/context.d.ts +19 -8
  95. package/lib/esm/aigne/context.js +28 -8
  96. package/lib/esm/index.d.ts +2 -1
  97. package/lib/esm/index.js +2 -1
  98. package/lib/esm/loader/agent-yaml.d.ts +2 -2
  99. package/lib/esm/loader/index.d.ts +18 -11
  100. package/lib/esm/loader/index.js +8 -27
  101. package/lib/esm/memory/default-memory/default-memory-storage/index.d.ts +30 -0
  102. package/lib/esm/memory/default-memory/default-memory-storage/index.js +64 -0
  103. package/lib/esm/memory/default-memory/default-memory-storage/migrate.d.ts +7 -0
  104. package/lib/esm/memory/default-memory/default-memory-storage/migrate.js +16 -0
  105. package/lib/esm/memory/default-memory/default-memory-storage/migrations/20250523165801-init.d.ts +7 -0
  106. package/lib/esm/memory/default-memory/default-memory-storage/migrations/20250523165801-init.js +28 -0
  107. package/lib/esm/memory/default-memory/default-memory-storage/models/memory.d.ts +10 -0
  108. package/lib/esm/memory/default-memory/default-memory-storage/models/memory.js +29 -0
  109. package/lib/esm/memory/default-memory/index.d.ts +10 -0
  110. package/lib/esm/memory/{default-memory.js → default-memory/index.js} +15 -24
  111. package/lib/esm/memory/default-memory/storage.d.ts +13 -0
  112. package/lib/esm/memory/default-memory/storage.js +2 -0
  113. package/lib/esm/memory/memory.d.ts +3 -2
  114. package/lib/esm/memory/memory.js +2 -2
  115. package/lib/esm/memory/retriever.d.ts +2 -2
  116. package/lib/esm/prompt/prompt-builder.d.ts +10 -4
  117. package/lib/esm/prompt/prompt-builder.js +4 -4
  118. package/lib/esm/prompt/template.d.ts +3 -3
  119. package/lib/esm/utils/json-schema.js +1 -1
  120. package/lib/esm/utils/logger.d.ts +33 -8
  121. package/lib/esm/utils/logger.js +61 -4
  122. package/lib/esm/utils/model-utils.d.ts +1 -1
  123. package/lib/esm/utils/stream-utils.d.ts +3 -1
  124. package/lib/esm/utils/stream-utils.js +29 -1
  125. package/lib/esm/utils/type-utils.d.ts +7 -2
  126. package/package.json +4 -20
  127. package/lib/cjs/client/client.d.ts +0 -97
  128. package/lib/cjs/client/client.js +0 -87
  129. package/lib/cjs/client/index.d.ts +0 -1
  130. package/lib/cjs/client/index.js +0 -17
  131. package/lib/cjs/memory/default-memory.d.ts +0 -16
  132. package/lib/cjs/models/bedrock-chat-model.d.ts +0 -79
  133. package/lib/cjs/models/bedrock-chat-model.js +0 -303
  134. package/lib/cjs/models/claude-chat-model.d.ts +0 -114
  135. package/lib/cjs/models/claude-chat-model.js +0 -317
  136. package/lib/cjs/models/deepseek-chat-model.d.ts +0 -23
  137. package/lib/cjs/models/deepseek-chat-model.js +0 -35
  138. package/lib/cjs/models/gemini-chat-model.d.ts +0 -23
  139. package/lib/cjs/models/gemini-chat-model.js +0 -35
  140. package/lib/cjs/models/ollama-chat-model.d.ts +0 -22
  141. package/lib/cjs/models/ollama-chat-model.js +0 -34
  142. package/lib/cjs/models/open-router-chat-model.d.ts +0 -22
  143. package/lib/cjs/models/open-router-chat-model.js +0 -34
  144. package/lib/cjs/models/openai-chat-model.d.ts +0 -166
  145. package/lib/cjs/models/openai-chat-model.js +0 -415
  146. package/lib/cjs/models/xai-chat-model.d.ts +0 -21
  147. package/lib/cjs/models/xai-chat-model.js +0 -33
  148. package/lib/cjs/server/error.d.ts +0 -15
  149. package/lib/cjs/server/error.js +0 -22
  150. package/lib/cjs/server/index.d.ts +0 -2
  151. package/lib/cjs/server/index.js +0 -18
  152. package/lib/cjs/server/server.d.ts +0 -135
  153. package/lib/cjs/server/server.js +0 -187
  154. package/lib/dts/client/client.d.ts +0 -97
  155. package/lib/dts/client/index.d.ts +0 -1
  156. package/lib/dts/memory/default-memory.d.ts +0 -16
  157. package/lib/dts/models/bedrock-chat-model.d.ts +0 -79
  158. package/lib/dts/models/claude-chat-model.d.ts +0 -114
  159. package/lib/dts/models/deepseek-chat-model.d.ts +0 -23
  160. package/lib/dts/models/gemini-chat-model.d.ts +0 -23
  161. package/lib/dts/models/ollama-chat-model.d.ts +0 -22
  162. package/lib/dts/models/open-router-chat-model.d.ts +0 -22
  163. package/lib/dts/models/openai-chat-model.d.ts +0 -166
  164. package/lib/dts/models/xai-chat-model.d.ts +0 -21
  165. package/lib/dts/server/error.d.ts +0 -15
  166. package/lib/dts/server/index.d.ts +0 -2
  167. package/lib/dts/server/server.d.ts +0 -135
  168. package/lib/esm/client/client.d.ts +0 -97
  169. package/lib/esm/client/client.js +0 -83
  170. package/lib/esm/client/index.d.ts +0 -1
  171. package/lib/esm/client/index.js +0 -1
  172. package/lib/esm/memory/default-memory.d.ts +0 -16
  173. package/lib/esm/models/bedrock-chat-model.d.ts +0 -79
  174. package/lib/esm/models/bedrock-chat-model.js +0 -298
  175. package/lib/esm/models/claude-chat-model.d.ts +0 -114
  176. package/lib/esm/models/claude-chat-model.js +0 -310
  177. package/lib/esm/models/deepseek-chat-model.d.ts +0 -23
  178. package/lib/esm/models/deepseek-chat-model.js +0 -31
  179. package/lib/esm/models/gemini-chat-model.d.ts +0 -23
  180. package/lib/esm/models/gemini-chat-model.js +0 -31
  181. package/lib/esm/models/ollama-chat-model.d.ts +0 -22
  182. package/lib/esm/models/ollama-chat-model.js +0 -30
  183. package/lib/esm/models/open-router-chat-model.d.ts +0 -22
  184. package/lib/esm/models/open-router-chat-model.js +0 -30
  185. package/lib/esm/models/openai-chat-model.d.ts +0 -166
  186. package/lib/esm/models/openai-chat-model.js +0 -405
  187. package/lib/esm/models/xai-chat-model.d.ts +0 -21
  188. package/lib/esm/models/xai-chat-model.js +0 -29
  189. package/lib/esm/server/error.d.ts +0 -15
  190. package/lib/esm/server/error.js +0 -18
  191. package/lib/esm/server/index.d.ts +0 -2
  192. package/lib/esm/server/index.js +0 -2
  193. package/lib/esm/server/server.d.ts +0 -135
  194. package/lib/esm/server/server.js +0 -180
@@ -1,34 +1,25 @@
1
1
  import equal from "fast-deep-equal";
2
- import { MemoryAgent, newMemoryId } from "./memory.js";
3
- import { MemoryRecorder } from "./recorder.js";
4
- import { MemoryRetriever, } from "./retriever.js";
5
- export const DEFAULT_MAX_MEMORY_COUNT = 10;
2
+ import { MemoryAgent } from "../memory.js";
3
+ import { MemoryRecorder, } from "../recorder.js";
4
+ import { MemoryRetriever, } from "../retriever.js";
5
+ import { DefaultMemoryStorage, } from "./default-memory-storage/index.js";
6
+ import { MemoryStorage } from "./storage.js";
6
7
  export class DefaultMemory extends MemoryAgent {
7
8
  constructor(options = {}) {
8
9
  super({
9
10
  ...options,
10
11
  autoUpdate: options.autoUpdate ?? true,
11
12
  });
13
+ this.storage =
14
+ options.storage instanceof MemoryStorage
15
+ ? options.storage
16
+ : new DefaultMemoryStorage(options.storage);
12
17
  if (!this.recorder)
13
18
  this.recorder = new DefaultMemoryRecorder(this);
14
19
  if (!this.retriever)
15
20
  this.retriever = new DefaultMemoryRetriever(this);
16
21
  }
17
- storage = [];
18
- async search(options = {}) {
19
- const { limit = DEFAULT_MAX_MEMORY_COUNT } = options;
20
- const result = limit < 0 ? this.storage.slice(limit) : this.storage.slice(0, limit);
21
- return { result };
22
- }
23
- async create(memory) {
24
- const m = {
25
- ...memory,
26
- id: newMemoryId(),
27
- createdAt: new Date().toISOString(),
28
- };
29
- this.storage.push(m);
30
- return { result: m };
31
- }
22
+ storage;
32
23
  }
33
24
  class DefaultMemoryRetriever extends MemoryRetriever {
34
25
  memory;
@@ -36,8 +27,8 @@ class DefaultMemoryRetriever extends MemoryRetriever {
36
27
  super({});
37
28
  this.memory = memory;
38
29
  }
39
- async process(input) {
40
- const { result } = await this.memory.search(input);
30
+ async process(input, options) {
31
+ const { result } = await this.memory.storage.search(input, options);
41
32
  return { memories: result };
42
33
  }
43
34
  }
@@ -47,12 +38,12 @@ class DefaultMemoryRecorder extends MemoryRecorder {
47
38
  super({});
48
39
  this.memory = memory;
49
40
  }
50
- async process(input) {
41
+ async process(input, options) {
51
42
  const newMemories = [];
52
43
  for (const content of input.content) {
53
- const { result: [last], } = await this.memory.search({ limit: -1 });
44
+ const { result: [last], } = await this.memory.storage.search({ limit: 1 }, options);
54
45
  if (!equal(last?.content, content)) {
55
- const { result } = await this.memory.create({ content });
46
+ const { result } = await this.memory.storage.create({ content }, options);
56
47
  newMemories.push(result);
57
48
  }
58
49
  }
@@ -0,0 +1,13 @@
1
+ import type { AgentInvokeOptions } from "../../agents/agent.js";
2
+ import type { Memory } from "../memory.js";
3
+ export declare abstract class MemoryStorage {
4
+ abstract create(memory: Pick<Memory, "content">, options: AgentInvokeOptions): Promise<{
5
+ result: Memory;
6
+ }>;
7
+ abstract search(query: {
8
+ search?: string;
9
+ limit?: number;
10
+ }, options: AgentInvokeOptions): Promise<{
11
+ result: Memory[];
12
+ }>;
13
+ }
@@ -0,0 +1,2 @@
1
+ export class MemoryStorage {
2
+ }
@@ -1,10 +1,11 @@
1
- import { Agent, type AgentOptions, type Message } from "../agents/agent.js";
1
+ import { Agent, type AgentInvokeOptions, type AgentOptions, type Message } from "../agents/agent.js";
2
2
  import type { Context } from "../aigne/context.js";
3
3
  import type { MessagePayload } from "../aigne/message-queue.js";
4
4
  import type { MemoryRecorder, MemoryRecorderInput, MemoryRecorderOutput } from "./recorder.js";
5
5
  import type { MemoryRetriever, MemoryRetrieverInput, MemoryRetrieverOutput } from "./retriever.js";
6
6
  export interface Memory {
7
7
  id: string;
8
+ sessionId?: string | null;
8
9
  content: unknown;
9
10
  createdAt: string;
10
11
  }
@@ -62,7 +63,7 @@ export declare class MemoryAgent extends Agent {
62
63
  * MemoryAgent doesn't directly process messages like other agents, so this method
63
64
  * throws an error when called. Use the specialized retrieve() and record() methods instead.
64
65
  */
65
- process(_input: Message, _context: Context): Promise<Message>;
66
+ process(_input: Message, _options: AgentInvokeOptions): Promise<Message>;
66
67
  /**
67
68
  * Retrieves memories based on the provided input criteria.
68
69
  *
@@ -1,6 +1,6 @@
1
1
  import { v7 } from "uuid";
2
2
  import { z } from "zod";
3
- import { Agent } from "../agents/agent.js";
3
+ import { Agent, } from "../agents/agent.js";
4
4
  import { checkArguments, remove } from "../utils/type-utils.js";
5
5
  export const newMemoryId = () => v7();
6
6
  /**
@@ -81,7 +81,7 @@ export class MemoryAgent extends Agent {
81
81
  * MemoryAgent doesn't directly process messages like other agents, so this method
82
82
  * throws an error when called. Use the specialized retrieve() and record() methods instead.
83
83
  */
84
- async process(_input, _context) {
84
+ async process(_input, _options) {
85
85
  throw new Error("Method not implemented.");
86
86
  }
87
87
  /**
@@ -39,11 +39,11 @@ export declare const memoryRetrieverInputSchema: z.ZodObject<{
39
39
  limit: z.ZodOptional<z.ZodNumber>;
40
40
  search: z.ZodOptional<z.ZodString>;
41
41
  }, "strip", z.ZodTypeAny, {
42
- limit?: number | undefined;
43
42
  search?: string | undefined;
44
- }, {
45
43
  limit?: number | undefined;
44
+ }, {
46
45
  search?: string | undefined;
46
+ limit?: number | undefined;
47
47
  }>;
48
48
  /**
49
49
  * @hidden
@@ -1,17 +1,23 @@
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 { ChatModel, ChatModelInput } from "../agents/chat-model.js";
4
5
  import type { Context } from "../aigne/context.js";
5
6
  import type { MemoryAgent } from "../memory/memory.js";
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 function createMessage<I extends Message>(message: string | I): I;
9
+ export declare function createMessage<V extends Message>(message: string, variables?: V): {
10
+ [MESSAGE_KEY]: string;
11
+ } & typeof variables;
12
+ export declare function createMessage<I extends Message, V extends Message>(message: I, variables?: V): I & typeof variables;
13
+ export declare function createMessage<I extends Message, V extends Message>(message: string | I, variables?: V): ({
14
+ [MESSAGE_KEY]: string;
15
+ } | I) & typeof variables;
10
16
  export declare function getMessage(input: Message): string | undefined;
11
17
  export interface PromptBuilderOptions {
12
18
  instructions?: string | ChatMessagesTemplate;
13
19
  }
14
- export interface PromptBuilderBuildOptions {
20
+ export interface PromptBuildOptions {
15
21
  memory?: MemoryAgent | MemoryAgent[];
16
22
  context: Context;
17
23
  agent?: AIAgent;
@@ -32,7 +38,7 @@ export declare class PromptBuilder {
32
38
  private static fromMCPPromptResult;
33
39
  constructor(options?: PromptBuilderOptions);
34
40
  instructions?: string | ChatMessagesTemplate;
35
- build(options: PromptBuilderBuildOptions): Promise<ChatModelInput & {
41
+ build(options: PromptBuildOptions): Promise<ChatModelInput & {
36
42
  toolAgents?: Agent[];
37
43
  }>;
38
44
  private buildMessages;
@@ -7,10 +7,10 @@ import { isNil, orArrayToArray, unique } from "../utils/type-utils.js";
7
7
  import { MEMORY_MESSAGE_TEMPLATE } from "./prompts/memory-message-template.js";
8
8
  import { AgentMessageTemplate, ChatMessagesTemplate, PromptTemplate, SystemMessageTemplate, UserMessageTemplate, } from "./template.js";
9
9
  export const MESSAGE_KEY = "$message";
10
- export function createMessage(message) {
11
- return typeof message === "string"
12
- ? { [MESSAGE_KEY]: message }
13
- : { ...message };
10
+ export function createMessage(message, variables) {
11
+ return (typeof message === "string"
12
+ ? { [MESSAGE_KEY]: message, ...variables }
13
+ : { ...message, ...variables });
14
14
  }
15
15
  export function getMessage(input) {
16
16
  const userInputMessage = input[MESSAGE_KEY];
@@ -1,4 +1,4 @@
1
- import type { ChatModelInputMessage, ChatModelInputMessageContent, ChatModelOutputToolCall } from "../models/chat-model.js";
1
+ import type { ChatModelInputMessage, ChatModelInputMessageContent, ChatModelOutputToolCall } from "../agents/chat-model.js";
2
2
  export declare class PromptTemplate {
3
3
  template: string;
4
4
  static from(template: string): PromptTemplate;
@@ -24,7 +24,7 @@ export declare class AgentMessageTemplate extends ChatMessageTemplate {
24
24
  constructor(content?: ChatModelInputMessage["content"], toolCalls?: ChatModelOutputToolCall[] | undefined, name?: string);
25
25
  format(variables?: Record<string, unknown>): {
26
26
  toolCalls: ChatModelOutputToolCall[] | undefined;
27
- role: import("../models/chat-model.js").Role;
27
+ role: import("../agents/chat-model.js").Role;
28
28
  content?: ChatModelInputMessageContent;
29
29
  toolCallId?: string;
30
30
  name?: string;
@@ -36,7 +36,7 @@ export declare class ToolMessageTemplate extends ChatMessageTemplate {
36
36
  constructor(content: object | string, toolCallId: string, name?: string);
37
37
  format(variables?: Record<string, unknown>): {
38
38
  toolCallId: string;
39
- role: import("../models/chat-model.js").Role;
39
+ role: import("../agents/chat-model.js").Role;
40
40
  content?: ChatModelInputMessageContent;
41
41
  toolCalls?: {
42
42
  id: string;
@@ -23,7 +23,7 @@ export function parseJSON(json) {
23
23
  return JSON.parse(json);
24
24
  }
25
25
  catch (error) {
26
- logger.core("Failed to parse JSON", { json, error });
26
+ logger.error("Failed to parse JSON", { json, error });
27
27
  throw error;
28
28
  }
29
29
  }
@@ -1,8 +1,33 @@
1
- import debug from "debug";
2
- export declare const logger: debug.Debug & {
3
- debug: debug.Debug;
4
- default: debug.Debug;
5
- } & {
6
- core: debug.Debugger;
7
- mcp: debug.Debugger;
8
- };
1
+ export declare enum LogLevel {
2
+ ERROR = "error",
3
+ WARN = "warn",
4
+ INFO = "info",
5
+ DEBUG = "debug"
6
+ }
7
+ export declare class Logger {
8
+ constructor(options: {
9
+ level: LogLevel;
10
+ ns: string;
11
+ });
12
+ level: LogLevel;
13
+ private debugLogger;
14
+ private infoLogger;
15
+ private warnLogger;
16
+ private errorLogger;
17
+ logMessage: {
18
+ (...data: any[]): void;
19
+ (...data: any[]): void;
20
+ (message?: any, ...optionalParams: any[]): void;
21
+ };
22
+ logError: {
23
+ (...data: any[]): void;
24
+ (...data: any[]): void;
25
+ (message?: any, ...optionalParams: any[]): void;
26
+ };
27
+ debug(message: string, ...args: unknown[]): void;
28
+ info(message: string, ...args: unknown[]): void;
29
+ warn(message: string, ...args: unknown[]): void;
30
+ error(message: string, ...args: unknown[]): void;
31
+ enabled(level: LogLevel): boolean;
32
+ }
33
+ export declare const logger: Logger;
@@ -1,6 +1,63 @@
1
+ import { isatty } from "node:tty";
1
2
  import debug from "debug";
2
- const base = debug("aigne");
3
- export const logger = Object.assign(debug, {
4
- core: base.extend("core"),
5
- mcp: base.extend("mcp"),
3
+ export var LogLevel;
4
+ (function (LogLevel) {
5
+ LogLevel["ERROR"] = "error";
6
+ LogLevel["WARN"] = "warn";
7
+ LogLevel["INFO"] = "info";
8
+ LogLevel["DEBUG"] = "debug";
9
+ })(LogLevel || (LogLevel = {}));
10
+ const levels = Object.values(LogLevel);
11
+ export class Logger {
12
+ constructor(options) {
13
+ this.level = options.level;
14
+ this.debugLogger = debug(`${options.ns}:debug`);
15
+ this.infoLogger = debug(`${options.ns}:info`);
16
+ this.warnLogger = debug(`${options.ns}:warn`);
17
+ this.errorLogger = debug(`${options.ns}:error`);
18
+ for (const logger of [this.debugLogger, this.infoLogger, this.warnLogger]) {
19
+ // @ts-ignore
20
+ logger.useColors = isatty(process.stdout.fd);
21
+ logger.enabled = true;
22
+ logger.log = (...args) => this.logMessage(...args);
23
+ }
24
+ this.errorLogger.log = (...args) => this.logError(...args);
25
+ // @ts-ignore
26
+ this.errorLogger.useColors = isatty(process.stderr.fd);
27
+ this.errorLogger.enabled = true;
28
+ }
29
+ level;
30
+ debugLogger;
31
+ infoLogger;
32
+ warnLogger;
33
+ errorLogger;
34
+ logMessage = console.log;
35
+ logError = console.error;
36
+ debug(message, ...args) {
37
+ if (this.enabled(LogLevel.DEBUG)) {
38
+ this.debugLogger(message, ...args);
39
+ }
40
+ }
41
+ info(message, ...args) {
42
+ if (this.enabled(LogLevel.INFO)) {
43
+ this.infoLogger(message, ...args);
44
+ }
45
+ }
46
+ warn(message, ...args) {
47
+ if (this.enabled(LogLevel.WARN)) {
48
+ this.warnLogger(message, ...args);
49
+ }
50
+ }
51
+ error(message, ...args) {
52
+ if (this.enabled(LogLevel.ERROR)) {
53
+ this.errorLogger(message, ...args);
54
+ }
55
+ }
56
+ enabled(level) {
57
+ return levels.indexOf(this.level) >= levels.indexOf(level);
58
+ }
59
+ }
60
+ export const logger = new Logger({
61
+ ns: "aigne:core",
62
+ level: LogLevel.INFO,
6
63
  });
@@ -1,3 +1,3 @@
1
- import type { ChatModelOutputUsage } from "../models/chat-model.js";
1
+ import type { ChatModelOutputUsage } from "../agents/chat-model.js";
2
2
  import type { Nullish } from "./type-utils.js";
3
3
  export declare function mergeUsage(...usages: Nullish<ChatModelOutputUsage>[]): ChatModelOutputUsage;
@@ -6,7 +6,7 @@ export declare function mergeAgentResponseChunk<T extends Message>(output: T, ch
6
6
  export declare function agentResponseStreamToObject<T extends Message>(stream: AgentResponseStream<T> | AgentProcessAsyncGenerator<T>): Promise<T>;
7
7
  export declare function asyncGeneratorToReadableStream<T extends Message>(generator: AgentProcessAsyncGenerator<T>): AgentResponseStream<T>;
8
8
  export declare function onAgentResponseStreamEnd<T extends Message>(stream: AgentResponseStream<T>, callback: (result: T) => PromiseOrValue<Partial<T> | void>, options?: {
9
- errorCallback?: (error: Error) => Error;
9
+ errorCallback?: (error: Error) => PromiseOrValue<Error>;
10
10
  processChunk?: (chunk: AgentResponseChunk<T>) => AgentResponseChunk<T>;
11
11
  }): ReadableStream<any>;
12
12
  export declare function isAsyncGenerator<T extends AsyncGenerator>(value: AsyncGenerator | unknown): value is T;
@@ -19,3 +19,5 @@ export declare function readableStreamToArray<T>(stream: ReadableStream<T>, opti
19
19
  catchError?: false;
20
20
  }): Promise<T[]>;
21
21
  export declare function stringToAgentResponseStream(str: string, key?: "text" | typeof MESSAGE_KEY | string): AgentResponseStream<Message>;
22
+ export declare function toReadableStream(stream: NodeJS.ReadStream): ReadableStream<Uint8Array<ArrayBufferLike>>;
23
+ export declare function readAllString(stream: NodeJS.ReadStream | ReadableStream): Promise<string>;
@@ -98,7 +98,7 @@ export function onAgentResponseStreamEnd(stream, callback, options) {
98
98
  }
99
99
  }
100
100
  catch (error) {
101
- controller.error(options?.errorCallback?.(error) ?? error);
101
+ controller.error((await options?.errorCallback?.(error)) ?? error);
102
102
  }
103
103
  },
104
104
  });
@@ -151,3 +151,31 @@ export function stringToAgentResponseStream(str, key = "text") {
151
151
  const segments = segmenter.segment(str);
152
152
  return arrayToReadableStream(Array.from(segments).map((segment) => ({ delta: { text: { [key]: segment.segment } } })));
153
153
  }
154
+ export function toReadableStream(stream) {
155
+ return new ReadableStream({
156
+ start(controller) {
157
+ const onData = (chunk) => {
158
+ controller.enqueue(new Uint8Array(chunk));
159
+ };
160
+ const onEnd = () => {
161
+ cleanup();
162
+ controller.close();
163
+ };
164
+ const onError = (err) => {
165
+ cleanup();
166
+ controller.error(err);
167
+ };
168
+ function cleanup() {
169
+ stream.off("data", onData);
170
+ stream.off("end", onEnd);
171
+ stream.off("error", onError);
172
+ }
173
+ stream.on("data", onData);
174
+ stream.on("end", onEnd);
175
+ stream.on("error", onError);
176
+ },
177
+ });
178
+ }
179
+ export async function readAllString(stream) {
180
+ return (await readableStreamToArray((stream instanceof ReadableStream ? stream : toReadableStream(stream)).pipeThrough(new TextDecoderStream()))).join("");
181
+ }
@@ -1,7 +1,12 @@
1
- import { type ZodType } from "zod";
1
+ import { type ZodType, z } from "zod";
2
2
  export type PromiseOrValue<T> = T | Promise<T>;
3
3
  export type Nullish<T> = T | null | undefined | void;
4
4
  export type OmitPropertiesFromArrayFirstElement<T extends unknown[], K extends string | number | symbol> = T extends [infer U, ...infer Rest] ? [Omit<U, K>, ...Rest] : never;
5
+ export type XOr<T, K extends keyof T, O extends keyof T> = (Omit<T, O> & {
6
+ [key in O]?: undefined;
7
+ }) | (Omit<T, K> & {
8
+ [key in K]?: undefined;
9
+ });
5
10
  export declare function isNil(value: unknown): value is null | undefined;
6
11
  export declare function isRecord<T>(value: unknown): value is Record<string, T>;
7
12
  export declare function isEmpty(obj: unknown): boolean;
@@ -15,6 +20,6 @@ export declare function orArrayToArray<T>(value?: T | T[]): T[];
15
20
  export declare function createAccessorArray<T>(array: T[], accessor: (array: T[], name: string) => T | undefined): T[] & {
16
21
  [key: string]: T;
17
22
  };
18
- export declare function checkArguments<T>(prefix: string, schema: ZodType<T>, args: T | unknown): T;
23
+ export declare function checkArguments<T extends ZodType>(prefix: string, schema: T, args: unknown): z.infer<T>;
19
24
  export declare function tryOrThrow<P extends PromiseOrValue<unknown>>(fn: () => P, error: string | Error | ((error: Error) => Error)): P;
20
25
  export declare function tryOrThrow<P extends PromiseOrValue<unknown>>(fn: () => P, error?: Nullish<string | Error | ((error: Error) => Nullish<Error>)>): P | undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aigne/core",
3
- "version": "1.15.0",
3
+ "version": "1.17.0",
4
4
  "description": "AIGNE core library for building AI-powered applications",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -50,9 +50,6 @@
50
50
  "loader/*": [
51
51
  "./lib/dts/loader/*"
52
52
  ],
53
- "models/*": [
54
- "./lib/dts/models/*"
55
- ],
56
53
  "memory/*": [
57
54
  "./lib/dts/memory/*"
58
55
  ],
@@ -61,12 +58,6 @@
61
58
  ],
62
59
  "utils/*": [
63
60
  "./lib/dts/utils/*"
64
- ],
65
- "client/*": [
66
- "./lib/dts/client/*"
67
- ],
68
- "server/*": [
69
- "./lib/dts/server/*"
70
61
  ]
71
62
  }
72
63
  },
@@ -85,21 +76,16 @@
85
76
  "nanoid": "^5.1.5",
86
77
  "p-retry": "^6.2.1",
87
78
  "raw-body": "^3.0.0",
79
+ "sequelize": "^6.37.7",
80
+ "sqlite3": "^5.1.7",
88
81
  "ufo": "^1.6.1",
82
+ "umzug": "^3.8.2",
89
83
  "uuid": "^11.1.0",
90
84
  "yaml": "^2.7.1",
91
85
  "zod": "^3.24.4",
92
86
  "zod-to-json-schema": "^3.24.5"
93
87
  },
94
- "peerDependencies": {
95
- "@anthropic-ai/sdk": "^0.39.0",
96
- "@aws-sdk/client-bedrock-runtime": "^3.796.0",
97
- "@google/generative-ai": "^0.24.0",
98
- "openai": "^4.87.3"
99
- },
100
88
  "devDependencies": {
101
- "@anthropic-ai/sdk": "^0.41.0",
102
- "@google/generative-ai": "^0.24.1",
103
89
  "@types/bun": "^1.2.12",
104
90
  "@types/compression": "^1.7.5",
105
91
  "@types/content-type": "^1.1.8",
@@ -109,9 +95,7 @@
109
95
  "compression": "^1.8.0",
110
96
  "detect-port": "^2.1.0",
111
97
  "express": "^5.1.0",
112
- "hono": "^4.7.8",
113
98
  "npm-run-all": "^4.1.5",
114
- "openai": "^4.97.0",
115
99
  "rimraf": "^6.0.1",
116
100
  "typescript": "^5.8.3"
117
101
  },
@@ -1,97 +0,0 @@
1
- /**
2
- * Client module used to interact with the AIGNE framework.
3
- */
4
- import type { AgentInvokeOptions, AgentResponse, AgentResponseStream, Message } from "../agents/agent.js";
5
- /**
6
- * Configuration options for the AIGNEClient.
7
- */
8
- export interface AIGNEClientOptions {
9
- /**
10
- * The URL of the AIGNE server to connect to.
11
- * This should point to the base endpoint where the AIGNEServer is hosted.
12
- */
13
- url: string;
14
- }
15
- /**
16
- * Options for invoking an agent through the AIGNEClient.
17
- * Extends the standard AgentInvokeOptions with client-specific options.
18
- */
19
- export interface AIGNEClientInvokeOptions extends AgentInvokeOptions {
20
- /**
21
- * Additional fetch API options to customize the HTTP request.
22
- * These options will be merged with the default options used by the client.
23
- */
24
- fetchOptions?: Partial<RequestInit>;
25
- }
26
- /**
27
- * Client for interacting with a remote AIGNE server.
28
- * AIGNEClient provides a client-side interface that matches the AIGNE API,
29
- * allowing applications to invoke agents and receive responses from a remote AIGNE instance.
30
- *
31
- * @example
32
- * Here's a simple example of how to use AIGNEClient:
33
- * {@includeCode ../../test/client/client.test.ts#example-aigne-client-simple}
34
- *
35
- * @example
36
- * Here's an example of how to use AIGNEClient with streaming response:
37
- * {@includeCode ../../test/client/client.test.ts#example-aigne-client-streaming}
38
- */
39
- export declare class AIGNEClient {
40
- options: AIGNEClientOptions;
41
- /**
42
- * Creates a new AIGNEClient instance.
43
- *
44
- * @param options - Configuration options for connecting to the AIGNE server
45
- */
46
- constructor(options: AIGNEClientOptions);
47
- /**
48
- * Invokes an agent in non-streaming mode and returns the complete response.
49
- *
50
- * @param agent - Name of the agent to invoke
51
- * @param input - Input message for the agent
52
- * @param options - Options with streaming mode explicitly set to false or omitted
53
- * @returns The complete agent response
54
- *
55
- * @example
56
- * Here's a simple example of how to use AIGNEClient:
57
- * {@includeCode ../../test/client/client.test.ts#example-aigne-client-simple}
58
- */
59
- invoke<I extends Message, O extends Message>(agent: string, input: I, options?: AIGNEClientInvokeOptions & {
60
- streaming?: false;
61
- }): Promise<O>;
62
- /**
63
- * Invokes an agent with streaming mode enabled and returns a stream of response chunks.
64
- *
65
- * @param agent - Name of the agent to invoke
66
- * @param input - Input message for the agent
67
- * @param options - Options with streaming mode explicitly set to true
68
- * @returns A stream of agent response chunks
69
- *
70
- * @example
71
- * Here's an example of how to use AIGNEClient with streaming response:
72
- * {@includeCode ../../test/client/client.test.ts#example-aigne-client-streaming}
73
- */
74
- invoke<I extends Message, O extends Message>(agent: string, input: I, options: AIGNEClientInvokeOptions & {
75
- streaming: true;
76
- }): Promise<AgentResponseStream<O>>;
77
- /**
78
- * Invokes an agent with the given input and options.
79
- *
80
- * @param agent - Name of the agent to invoke
81
- * @param input - Input message for the agent
82
- * @param options - Options for the invocation
83
- * @returns Either a complete response or a response stream depending on the streaming option
84
- */
85
- invoke<I extends Message, O extends Message>(agent: string, input: I, options?: AIGNEClientInvokeOptions): Promise<AgentResponse<O>>;
86
- /**
87
- * Enhanced fetch method that handles error responses from the AIGNE server.
88
- * This method wraps the standard fetch API to provide better error handling and reporting.
89
- *
90
- * @param args - Standard fetch API arguments (url and options)
91
- * @returns A Response object if the request was successful
92
- * @throws Error with detailed information if the request failed
93
- *
94
- * @private
95
- */
96
- fetch(...args: Parameters<typeof globalThis.fetch>): Promise<Response>;
97
- }