@aigne/core 1.1.0-beta.6 → 1.3.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 (105) hide show
  1. package/CHANGELOG.md +77 -0
  2. package/lib/cjs/agents/agent.d.ts +37 -33
  3. package/lib/cjs/agents/agent.js +68 -28
  4. package/lib/cjs/agents/ai-agent.d.ts +8 -12
  5. package/lib/cjs/agents/ai-agent.js +17 -35
  6. package/lib/cjs/agents/mcp-agent.d.ts +25 -9
  7. package/lib/cjs/agents/mcp-agent.js +61 -14
  8. package/lib/cjs/agents/memory.d.ts +26 -0
  9. package/lib/cjs/agents/memory.js +38 -0
  10. package/lib/cjs/agents/types.d.ts +4 -3
  11. package/lib/cjs/agents/types.js +11 -1
  12. package/lib/cjs/agents/user-agent.d.ts +24 -0
  13. package/lib/cjs/agents/user-agent.js +62 -0
  14. package/lib/cjs/execution-engine/context.d.ts +35 -5
  15. package/lib/cjs/execution-engine/execution-engine.d.ts +64 -0
  16. package/lib/cjs/execution-engine/execution-engine.js +136 -0
  17. package/lib/cjs/execution-engine/index.d.ts +4 -46
  18. package/lib/cjs/execution-engine/index.js +17 -193
  19. package/lib/cjs/execution-engine/message-queue.d.ts +17 -2
  20. package/lib/cjs/execution-engine/message-queue.js +37 -1
  21. package/lib/cjs/execution-engine/utils.d.ts +4 -0
  22. package/lib/cjs/execution-engine/utils.js +32 -0
  23. package/lib/cjs/index.d.ts +14 -12
  24. package/lib/cjs/index.js +14 -12
  25. package/lib/{dts/models/chat.d.ts → cjs/models/chat-model.d.ts} +15 -12
  26. package/lib/cjs/models/{chat.js → chat-model.js} +5 -5
  27. package/lib/cjs/models/claude-chat-model.d.ts +17 -0
  28. package/lib/cjs/models/claude-chat-model.js +199 -0
  29. package/lib/cjs/models/{chat-openai.d.ts → openai-chat-model.d.ts} +2 -2
  30. package/lib/cjs/models/{chat-openai.js → openai-chat-model.js} +8 -8
  31. package/lib/cjs/prompt/prompt-builder.d.ts +14 -14
  32. package/lib/cjs/prompt/prompt-builder.js +62 -67
  33. package/lib/cjs/prompt/template.d.ts +9 -21
  34. package/lib/cjs/prompt/template.js +3 -5
  35. package/lib/cjs/utils/json-schema.d.ts +3 -0
  36. package/lib/cjs/utils/json-schema.js +23 -0
  37. package/lib/cjs/utils/mcp-utils.d.ts +3 -2
  38. package/lib/cjs/utils/mcp-utils.js +29 -17
  39. package/lib/cjs/utils/run-chat-loop.d.ts +5 -4
  40. package/lib/cjs/utils/run-chat-loop.js +25 -4
  41. package/lib/cjs/utils/type-utils.d.ts +3 -0
  42. package/lib/cjs/utils/type-utils.js +8 -8
  43. package/lib/dts/agents/agent.d.ts +37 -33
  44. package/lib/dts/agents/ai-agent.d.ts +8 -12
  45. package/lib/dts/agents/mcp-agent.d.ts +25 -9
  46. package/lib/dts/agents/memory.d.ts +26 -0
  47. package/lib/dts/agents/types.d.ts +4 -3
  48. package/lib/dts/agents/user-agent.d.ts +24 -0
  49. package/lib/dts/execution-engine/context.d.ts +35 -5
  50. package/lib/dts/execution-engine/execution-engine.d.ts +64 -0
  51. package/lib/dts/execution-engine/index.d.ts +4 -46
  52. package/lib/dts/execution-engine/message-queue.d.ts +17 -2
  53. package/lib/dts/execution-engine/utils.d.ts +4 -0
  54. package/lib/dts/index.d.ts +14 -12
  55. package/lib/{esm/models/chat.d.ts → dts/models/chat-model.d.ts} +15 -12
  56. package/lib/dts/models/claude-chat-model.d.ts +17 -0
  57. package/lib/dts/models/{chat-openai.d.ts → openai-chat-model.d.ts} +2 -2
  58. package/lib/dts/prompt/prompt-builder.d.ts +14 -14
  59. package/lib/dts/prompt/template.d.ts +9 -21
  60. package/lib/dts/utils/json-schema.d.ts +3 -0
  61. package/lib/dts/utils/mcp-utils.d.ts +3 -2
  62. package/lib/dts/utils/run-chat-loop.d.ts +5 -4
  63. package/lib/dts/utils/type-utils.d.ts +3 -0
  64. package/lib/esm/agents/agent.d.ts +37 -33
  65. package/lib/esm/agents/agent.js +67 -24
  66. package/lib/esm/agents/ai-agent.d.ts +8 -12
  67. package/lib/esm/agents/ai-agent.js +13 -31
  68. package/lib/esm/agents/mcp-agent.d.ts +25 -9
  69. package/lib/esm/agents/mcp-agent.js +54 -8
  70. package/lib/esm/agents/memory.d.ts +26 -0
  71. package/lib/esm/agents/memory.js +34 -0
  72. package/lib/esm/agents/types.d.ts +4 -3
  73. package/lib/esm/agents/types.js +10 -1
  74. package/lib/esm/agents/user-agent.d.ts +24 -0
  75. package/lib/esm/agents/user-agent.js +58 -0
  76. package/lib/esm/execution-engine/context.d.ts +35 -5
  77. package/lib/esm/execution-engine/execution-engine.d.ts +64 -0
  78. package/lib/esm/execution-engine/execution-engine.js +129 -0
  79. package/lib/esm/execution-engine/index.d.ts +4 -46
  80. package/lib/esm/execution-engine/index.js +4 -188
  81. package/lib/esm/execution-engine/message-queue.d.ts +17 -2
  82. package/lib/esm/execution-engine/message-queue.js +37 -1
  83. package/lib/esm/execution-engine/utils.d.ts +4 -0
  84. package/lib/esm/execution-engine/utils.js +28 -0
  85. package/lib/esm/index.d.ts +14 -12
  86. package/lib/esm/index.js +14 -12
  87. package/lib/{cjs/models/chat.d.ts → esm/models/chat-model.d.ts} +15 -12
  88. package/lib/esm/models/{chat.js → chat-model.js} +4 -4
  89. package/lib/esm/models/claude-chat-model.d.ts +17 -0
  90. package/lib/esm/models/claude-chat-model.js +192 -0
  91. package/lib/esm/models/{chat-openai.d.ts → openai-chat-model.d.ts} +2 -2
  92. package/lib/esm/models/{chat-openai.js → openai-chat-model.js} +5 -5
  93. package/lib/esm/prompt/prompt-builder.d.ts +14 -14
  94. package/lib/esm/prompt/prompt-builder.js +56 -58
  95. package/lib/esm/prompt/template.d.ts +9 -21
  96. package/lib/esm/prompt/template.js +3 -5
  97. package/lib/esm/utils/json-schema.d.ts +3 -0
  98. package/lib/esm/utils/json-schema.js +20 -0
  99. package/lib/esm/utils/mcp-utils.d.ts +3 -2
  100. package/lib/esm/utils/mcp-utils.js +26 -15
  101. package/lib/esm/utils/run-chat-loop.d.ts +5 -4
  102. package/lib/esm/utils/run-chat-loop.js +25 -4
  103. package/lib/esm/utils/type-utils.d.ts +3 -0
  104. package/lib/esm/utils/type-utils.js +4 -2
  105. package/package.json +31 -30
@@ -2,26 +2,23 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.toolFromMCPTool = toolFromMCPTool;
4
4
  exports.promptFromMCPPrompt = promptFromMCPPrompt;
5
- const json_schema_to_zod_1 = require("@n8n/json-schema-to-zod");
5
+ exports.resourceFromMCPResource = resourceFromMCPResource;
6
+ const json_schema_to_zod_1 = require("@aigne/json-schema-to-zod");
7
+ const uriTemplate_js_1 = require("@modelcontextprotocol/sdk/shared/uriTemplate.js");
8
+ const types_js_1 = require("@modelcontextprotocol/sdk/types.js");
6
9
  const zod_1 = require("zod");
7
- const mcp_agent_1 = require("../agents/mcp-agent");
10
+ const mcp_agent_js_1 = require("../agents/mcp-agent.js");
8
11
  function toolFromMCPTool(client, tool) {
9
- return new mcp_agent_1.MCPTool({
12
+ return new mcp_agent_js_1.MCPTool({
10
13
  client,
11
14
  name: tool.name,
12
15
  description: tool.description,
13
16
  inputSchema: (0, json_schema_to_zod_1.jsonSchemaToZod)(tool.inputSchema),
14
- outputSchema: zod_1.z
15
- .object({
16
- _meta: zod_1.z.record(zod_1.z.unknown()).optional(),
17
- content: zod_1.z.array(zod_1.z.record(zod_1.z.unknown())),
18
- isError: zod_1.z.boolean().optional(),
19
- })
20
- .passthrough(),
17
+ outputSchema: types_js_1.CallToolResultSchema,
21
18
  });
22
19
  }
23
20
  function promptFromMCPPrompt(client, prompt) {
24
- return new mcp_agent_1.MCPPrompt({
21
+ return new mcp_agent_js_1.MCPPrompt({
25
22
  client,
26
23
  name: prompt.name,
27
24
  description: prompt.description,
@@ -31,11 +28,26 @@ function promptFromMCPPrompt(client, prompt) {
31
28
  Object.fromEntries(prompt.arguments.map((i) => [i.name, { type: "string", description: i.description }])),
32
29
  required: prompt.arguments?.filter((i) => i.required).map((i) => i.name),
33
30
  }),
34
- outputSchema: zod_1.z
35
- .object({
36
- description: zod_1.z.string().optional(),
37
- messages: zod_1.z.array(zod_1.z.record(zod_1.z.unknown())),
38
- })
39
- .passthrough(),
31
+ outputSchema: types_js_1.GetPromptResultSchema,
40
32
  });
41
33
  }
34
+ function resourceFromMCPResource(client, resource) {
35
+ const [uri, variables] = isResourceTemplate(resource)
36
+ ? [
37
+ resource.uriTemplate,
38
+ // TODO: use template.variableNames when it's available https://github.com/modelcontextprotocol/typescript-sdk/pull/188
39
+ new uriTemplate_js_1.UriTemplate(resource.uriTemplate).parts.flatMap((i) => (typeof i === "object" ? i.names : [])),
40
+ ]
41
+ : [resource.uri, []];
42
+ return new mcp_agent_js_1.MCPResource({
43
+ client,
44
+ name: resource.name,
45
+ uri,
46
+ description: resource.description,
47
+ inputSchema: zod_1.z.object(Object.fromEntries(variables.map((i) => [i, zod_1.z.string()]))),
48
+ outputSchema: types_js_1.ReadResourceResultSchema,
49
+ });
50
+ }
51
+ function isResourceTemplate(resource) {
52
+ return typeof resource.uriTemplate === "string";
53
+ }
@@ -1,10 +1,11 @@
1
- import type { AgentInput, AgentOutput } from "../agents/agent";
2
- import type { UserAgent } from "../execution-engine";
1
+ import type { Message } from "../agents/agent.js";
2
+ import type { UserAgent } from "../agents/user-agent.js";
3
3
  export interface ChatLoopOptions {
4
4
  log?: typeof console.log;
5
- initialCall?: AgentInput | string;
5
+ initialCall?: Message | string;
6
6
  welcome?: string;
7
7
  defaultQuestion?: string;
8
- onResponse?: (response: AgentOutput) => void;
8
+ onResponse?: (response: Message) => void;
9
+ inputKey?: string;
9
10
  }
10
11
  export declare function runChatLoopInTerminal(userAgent: UserAgent, { log, ...options }?: ChatLoopOptions): Promise<void>;
@@ -5,15 +5,28 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.runChatLoopInTerminal = runChatLoopInTerminal;
7
7
  const inquirer_1 = __importDefault(require("inquirer"));
8
- const logger_1 = require("./logger");
8
+ const logger_js_1 = require("./logger.js");
9
9
  async function runChatLoopInTerminal(userAgent, { log = console.log.bind(console), ...options } = {}) {
10
+ let isLoopExited = false;
11
+ let prompt;
10
12
  if (options?.welcome)
11
13
  log(options.welcome);
12
14
  if (options?.initialCall) {
13
15
  await callAgent(userAgent, options.initialCall, { ...options, log });
14
16
  }
17
+ (async () => {
18
+ for await (const output of userAgent.stream) {
19
+ if (isLoopExited)
20
+ return;
21
+ if (options?.onResponse)
22
+ options.onResponse(output);
23
+ else
24
+ log(output);
25
+ prompt?.ui.close();
26
+ }
27
+ })();
15
28
  for (let i = 0;; i++) {
16
- const { question } = await inquirer_1.default.prompt([
29
+ prompt = inquirer_1.default.prompt([
17
30
  {
18
31
  type: "input",
19
32
  name: "question",
@@ -21,7 +34,14 @@ async function runChatLoopInTerminal(userAgent, { log = console.log.bind(console
21
34
  default: i === 0 ? options?.defaultQuestion : undefined,
22
35
  },
23
36
  ]);
24
- if (!question.trim())
37
+ let question;
38
+ try {
39
+ question = (await prompt).question;
40
+ }
41
+ catch {
42
+ // ignore abort error from inquirer
43
+ }
44
+ if (!question?.trim())
25
45
  continue;
26
46
  const cmd = COMMANDS[question.trim()];
27
47
  if (cmd) {
@@ -34,9 +54,10 @@ async function runChatLoopInTerminal(userAgent, { log = console.log.bind(console
34
54
  }
35
55
  await callAgent(userAgent, question, { ...options, log });
36
56
  }
57
+ isLoopExited = true;
37
58
  }
38
59
  async function callAgent(agent, input, options) {
39
- const response = await logger_1.logger.spinner(agent.call(input), "🤖");
60
+ const response = await logger_js_1.logger.spinner(agent.call(options.inputKey && typeof input === "string" ? { [options.inputKey]: input } : input), "🤖");
40
61
  if (options?.onResponse)
41
62
  options.onResponse(response);
42
63
  else
@@ -1,4 +1,7 @@
1
+ export type PromiseOrValue<T> = T | Promise<T>;
2
+ export type Nullish<T> = T | null | undefined;
1
3
  export declare function isNonNullable<T>(value: T): value is NonNullable<T>;
4
+ export declare function isNotEmpty<T>(arr: T[]): arr is [T, ...T[]];
2
5
  export declare function orArrayToArray<T>(value?: T | T[]): T[];
3
6
  export declare function get(obj: unknown, path: string | string[], type?: undefined): unknown | undefined;
4
7
  export declare function get(obj: unknown, path: string | string[], type: "string"): string | undefined;
@@ -1,24 +1,24 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.isNonNullable = isNonNullable;
4
+ exports.isNotEmpty = isNotEmpty;
7
5
  exports.orArrayToArray = orArrayToArray;
8
6
  exports.get = get;
9
7
  exports.createAccessorArray = createAccessorArray;
10
- const lodash_1 = require("lodash");
11
- const isNil_1 = __importDefault(require("lodash/isNil"));
8
+ const lodash_es_1 = require("lodash-es");
12
9
  function isNonNullable(value) {
13
- return !(0, isNil_1.default)(value);
10
+ return !(0, lodash_es_1.isNil)(value);
11
+ }
12
+ function isNotEmpty(arr) {
13
+ return arr.length > 0;
14
14
  }
15
15
  function orArrayToArray(value) {
16
- if ((0, isNil_1.default)(value))
16
+ if ((0, lodash_es_1.isNil)(value))
17
17
  return [];
18
18
  return Array.isArray(value) ? value : [value];
19
19
  }
20
20
  function get(obj, path, type) {
21
- const v = (0, lodash_1.get)(obj, path);
21
+ const v = (0, lodash_es_1.get)(obj, path);
22
22
  if (type === "string" && typeof v === "string")
23
23
  return v;
24
24
  if (type === "number" && typeof v === "number")
@@ -1,57 +1,61 @@
1
- import EventEmitter from "node:events";
2
- import { type ZodObject, type ZodType } from "zod";
3
- import type { Context } from "../execution-engine/context";
4
- import { type TransferAgentOutput } from "./types";
5
- export type AgentInput = Record<string, unknown>;
6
- export type AgentOutput = Record<string, unknown> & Partial<TransferAgentOutput>;
1
+ import { type ZodType } from "zod";
2
+ import type { Context } from "../execution-engine/context.js";
3
+ import { type Nullish, type PromiseOrValue } from "../utils/type-utils.js";
4
+ import { AgentMemory, type AgentMemoryOptions } from "./memory.js";
5
+ import { type TransferAgentOutput } from "./types.js";
6
+ export type Message = Record<string, unknown>;
7
7
  export type SubscribeTopic = string | string[];
8
- export type PublishTopic<O extends AgentOutput = AgentOutput> = string | string[] | ((output: O) => string | string[] | Promise<string | string[]>);
9
- export interface AgentOptions<I extends AgentInput = AgentInput, O extends AgentOutput = AgentOutput> {
8
+ export type PublishTopic<O extends Message> = string | string[] | ((output: O) => PromiseOrValue<Nullish<string | string[]>>);
9
+ export interface AgentOptions<I extends Message = Message, O extends Message = Message> {
10
10
  subscribeTopic?: SubscribeTopic;
11
11
  publishTopic?: PublishTopic<O>;
12
12
  name?: string;
13
13
  description?: string;
14
- inputSchema?: ZodObject<{
15
- [key in keyof I]: ZodType;
16
- }>;
17
- outputSchema?: ZodObject<{
18
- [key in keyof O]: ZodType;
19
- }>;
14
+ inputSchema?: ZodType<I>;
15
+ outputSchema?: ZodType<O>;
20
16
  includeInputInOutput?: boolean;
21
17
  tools?: (Agent | FunctionAgentFn)[];
22
18
  disableLogging?: boolean;
19
+ memory?: AgentMemory | AgentMemoryOptions | true;
23
20
  }
24
- export declare class Agent<I extends AgentInput = AgentInput, O extends AgentOutput = AgentOutput> extends EventEmitter {
25
- static from<I extends AgentInput = AgentInput, O extends AgentOutput = AgentOutput>(options: AgentOptions<I, O>): Agent<I, O>;
26
- constructor(options: AgentOptions<I, O>);
21
+ export declare abstract class Agent<I extends Message = Message, O extends Message = Message> {
22
+ constructor({ inputSchema, outputSchema, ...options }: AgentOptions<I, O>);
23
+ readonly memory?: AgentMemory;
27
24
  readonly name: string;
25
+ /**
26
+ * Default topic this agent will subscribe to
27
+ */
28
+ get topic(): string;
28
29
  readonly description?: string;
29
- readonly inputSchema: ZodObject<{
30
- [key in keyof I]: ZodType;
31
- }>;
32
- readonly outputSchema: ZodObject<{
33
- [key in keyof O]: ZodType;
34
- }>;
30
+ readonly inputSchema: ZodType<I>;
31
+ readonly outputSchema: ZodType<O>;
35
32
  readonly includeInputInOutput?: boolean;
36
33
  readonly subscribeTopic?: SubscribeTopic;
37
- readonly publishTopic?: PublishTopic<AgentOutput>;
38
- readonly tools: Agent<AgentInput, AgentOutput>[] & {
39
- [key: string]: Agent<AgentInput, AgentOutput>;
34
+ readonly publishTopic?: PublishTopic<Message>;
35
+ readonly tools: Agent<Message, Message>[] & {
36
+ [key: string]: Agent<Message, Message>;
40
37
  };
41
38
  private disableLogging?;
42
- addTool<I extends AgentInput, O extends AgentOutput>(tool: Agent<I, O> | FunctionAgentFn<I, O>): void;
39
+ /**
40
+ * Attach agent to context:
41
+ * - subscribe to topic and call process method when message received
42
+ * - subscribe to memory topic if memory is enabled
43
+ * @param context Context to attach
44
+ */
45
+ attach(context: Context): void;
46
+ addTool<I extends Message, O extends Message>(tool: Agent<I, O> | FunctionAgentFn<I, O>): void;
43
47
  get isCallable(): boolean;
44
48
  call(input: I | string, context?: Context): Promise<O>;
45
- process?(input: I, context?: Context): Promise<O>;
49
+ abstract process(input: I, context?: Context): Promise<O | TransferAgentOutput>;
46
50
  shutdown(): Promise<void>;
47
51
  }
48
- export interface FunctionAgentOptions<I extends AgentInput = AgentInput, O extends AgentOutput = AgentOutput> extends AgentOptions<I, O> {
52
+ export interface FunctionAgentOptions<I extends Message = Message, O extends Message = Message> extends AgentOptions<I, O> {
49
53
  fn?: FunctionAgentFn<I, O>;
50
54
  }
51
- export declare class FunctionAgent<I extends AgentInput = AgentInput, O extends AgentOutput = AgentOutput> extends Agent<I, O> {
52
- static from<I extends AgentInput, O extends AgentOutput>(options: FunctionAgentOptions<I, O> | FunctionAgentFn<I, O>): FunctionAgent<I, O>;
55
+ export declare class FunctionAgent<I extends Message = Message, O extends Message = Message> extends Agent<I, O> {
56
+ static from<I extends Message, O extends Message>(options: FunctionAgentOptions<I, O> | FunctionAgentFn<I, O>): FunctionAgent<I, O>;
53
57
  constructor(options: FunctionAgentOptions<I, O>);
54
58
  fn: FunctionAgentFn<I, O>;
55
- process(input: I, context?: Context): Promise<O>;
59
+ process(input: I, context?: Context): Promise<TransferAgentOutput | O>;
56
60
  }
57
- export type FunctionAgentFn<I extends AgentInput = AgentInput, O extends AgentOutput = AgentOutput> = (input: I, context?: Context) => O | Promise<O> | Agent | Promise<Agent>;
61
+ export type FunctionAgentFn<I extends Message = Message, O extends Message = Message> = (input: I, context?: Context) => O | Promise<O> | Agent | Promise<Agent>;
@@ -1,24 +1,20 @@
1
- import type { Context } from "../execution-engine/context";
2
- import type { ChatModel } from "../models/chat";
3
- import { PromptBuilder } from "../prompt/prompt-builder";
4
- import { Agent, type AgentInput, type AgentOptions, type AgentOutput } from "./agent";
5
- export interface AIAgentOptions<I extends AgentInput = AgentInput, O extends AgentOutput = AgentOutput> extends AgentOptions<I, O> {
1
+ import type { Context } from "../execution-engine/context.js";
2
+ import type { ChatModel } from "../models/chat-model.js";
3
+ import { PromptBuilder } from "../prompt/prompt-builder.js";
4
+ import { Agent, type AgentOptions, type Message } from "./agent.js";
5
+ export interface AIAgentOptions<I extends Message = Message, O extends Message = Message> extends AgentOptions<I, O> {
6
6
  model?: ChatModel;
7
7
  instructions?: string | PromptBuilder;
8
8
  outputKey?: string;
9
9
  toolChoice?: AIAgentToolChoice;
10
- enableHistory?: boolean;
11
- maxHistoryMessages?: number;
12
10
  }
13
11
  export type AIAgentToolChoice = "auto" | "none" | "required" | "router" | Agent;
14
- export declare class AIAgent<I extends AgentInput = AgentInput, O extends AgentOutput = AgentOutput> extends Agent<I, O> {
15
- static from<I extends AgentInput, O extends AgentOutput>(options: AIAgentOptions<I, O>): AIAgent<I, O>;
12
+ export declare class AIAgent<I extends Message = Message, O extends Message = Message> extends Agent<I, O> {
13
+ static from<I extends Message, O extends Message>(options: AIAgentOptions<I, O>): AIAgent<I, O>;
16
14
  constructor(options: AIAgentOptions<I, O>);
17
15
  model?: ChatModel;
18
16
  instructions: PromptBuilder;
19
17
  outputKey?: string;
20
18
  toolChoice?: AIAgentToolChoice;
21
- enableHistory?: boolean;
22
- maxHistoryMessages: number;
23
- process(input: I, context?: Context): Promise<O>;
19
+ process(input: I, context?: Context): Promise<import("./types.js").TransferAgentOutput | O>;
24
20
  }
@@ -1,10 +1,12 @@
1
1
  import { Client } from "@modelcontextprotocol/sdk/client/index.js";
2
2
  import { type StdioServerParameters } from "@modelcontextprotocol/sdk/client/stdio.js";
3
- import type { CallToolResult, GetPromptResult } from "@modelcontextprotocol/sdk/types";
4
- import { Agent, type AgentInput, type AgentOptions, type AgentOutput } from "./agent";
3
+ import type { CallToolResult, GetPromptResult, ReadResourceResult } from "@modelcontextprotocol/sdk/types.js";
4
+ import type { Context } from "../execution-engine/context.js";
5
+ import { Agent, type AgentOptions, type Message } from "./agent.js";
5
6
  export interface MCPAgentOptions extends AgentOptions {
6
7
  client: Client;
7
8
  prompts?: MCPPrompt[];
9
+ resources?: MCPResource[];
8
10
  }
9
11
  export type MCPServerOptions = SSEServerParameters | StdioServerParameters;
10
12
  export type SSEServerParameters = {
@@ -19,21 +21,35 @@ export declare class MCPAgent extends Agent {
19
21
  readonly prompts: MCPPrompt[] & {
20
22
  [key: string]: MCPPrompt;
21
23
  };
24
+ readonly resources: MCPResource[] & {
25
+ [key: string]: MCPResource;
26
+ };
27
+ get isCallable(): boolean;
28
+ process(_input: Message, _context?: Context): Promise<Message>;
22
29
  shutdown(): Promise<void>;
23
30
  }
24
- export interface MCPToolBaseOptions<I extends AgentInput, O extends AgentOutput> extends AgentOptions<I, O> {
31
+ export interface MCPToolBaseOptions<I extends Message, O extends Message> extends AgentOptions<I, O> {
25
32
  client: Client;
26
33
  }
27
- export declare abstract class MCPBase<I extends AgentInput, O extends AgentOutput> extends Agent<I, O> {
34
+ export declare abstract class MCPBase<I extends Message, O extends Message> extends Agent<I, O> {
28
35
  constructor(options: MCPToolBaseOptions<I, O>);
29
36
  protected client: Client;
30
37
  protected get mcpServer(): string | undefined;
31
38
  }
32
- export declare class MCPTool extends MCPBase<AgentInput, CallToolResult> {
33
- process(input: AgentInput): Promise<CallToolResult>;
39
+ export declare class MCPTool extends MCPBase<Message, CallToolResult> {
40
+ process(input: Message): Promise<CallToolResult>;
34
41
  }
35
- export declare class MCPPrompt extends MCPBase<{
42
+ export interface MCPPromptInput extends Message {
36
43
  [key: string]: string;
37
- }, GetPromptResult> {
38
- process(input: AgentInput): Promise<GetPromptResult>;
44
+ }
45
+ export declare class MCPPrompt extends MCPBase<MCPPromptInput, GetPromptResult> {
46
+ process(input: MCPPromptInput): Promise<GetPromptResult>;
47
+ }
48
+ export interface MCPResourceOptions extends MCPToolBaseOptions<MCPPromptInput, ReadResourceResult> {
49
+ uri: string;
50
+ }
51
+ export declare class MCPResource extends MCPBase<MCPPromptInput, ReadResourceResult> {
52
+ constructor(options: MCPResourceOptions);
53
+ uri: string;
54
+ process(input: MCPPromptInput): Promise<ReadResourceResult>;
39
55
  }
@@ -0,0 +1,26 @@
1
+ import type { Context } from "../execution-engine/context.js";
2
+ import type { Message } from "./agent.js";
3
+ export interface AgentMemoryOptions {
4
+ /**
5
+ * Enable memory, default is true
6
+ */
7
+ enabled?: boolean;
8
+ subscribeTopic?: string | string[];
9
+ maxMemoriesInChat?: number;
10
+ }
11
+ export interface Memory {
12
+ role: "user" | "agent";
13
+ content: Message;
14
+ source?: string;
15
+ }
16
+ export declare class AgentMemory {
17
+ constructor(options: AgentMemoryOptions);
18
+ enabled?: boolean;
19
+ subscribeTopic?: string | string[];
20
+ maxMemoriesInChat?: number;
21
+ memories: Memory[];
22
+ private subscriptions;
23
+ addMemory(memory: Memory): void;
24
+ attach(context: Context): void;
25
+ detach(): void;
26
+ }
@@ -1,9 +1,10 @@
1
- import type { Agent, AgentOutput } from "./agent";
1
+ import type { Agent, Message } from "./agent.js";
2
2
  export declare const transferAgentOutputKey = "$transferAgentTo";
3
- export interface TransferAgentOutput extends Record<string, unknown> {
3
+ export interface TransferAgentOutput extends Message {
4
4
  [transferAgentOutputKey]: {
5
5
  agent: Agent;
6
6
  };
7
7
  }
8
8
  export declare function transferToAgentOutput(agent: Agent): TransferAgentOutput;
9
- export declare function isTransferAgentOutput(output: AgentOutput): output is TransferAgentOutput;
9
+ export declare function isTransferAgentOutput(output: Message): output is TransferAgentOutput;
10
+ export declare function replaceTransferAgentToName(output: Message): Message;
@@ -0,0 +1,24 @@
1
+ import type { Context } from "../execution-engine/context.js";
2
+ import type { MessagePayload, MessageQueueListener, Unsubscribe } from "../execution-engine/message-queue.js";
3
+ import { type PromiseOrValue } from "../utils/type-utils.js";
4
+ import { Agent, type AgentOptions, type Message } from "./agent.js";
5
+ export interface UserAgentOptions<I extends Message = Message, O extends Message = Message> extends AgentOptions<I, O> {
6
+ context?: Context;
7
+ process?: (input: I, context: Context) => PromiseOrValue<O>;
8
+ }
9
+ export declare class UserAgent<I extends Message = Message, O extends Message = Message> extends Agent<I, O> {
10
+ static from<I extends Message, O extends Message>(options: UserAgentOptions<I, O>): UserAgent<I, O>;
11
+ constructor(options: UserAgentOptions<I, O>);
12
+ private context?;
13
+ private get ctx();
14
+ private _process?;
15
+ process(input: I, context?: Context): Promise<O>;
16
+ publish(topic: string | string[], message: Message | string): void;
17
+ subscribe(topic: string, listener?: undefined): Promise<MessagePayload>;
18
+ subscribe(topic: string, listener: MessageQueueListener): Unsubscribe;
19
+ subscribe(topic: string, listener?: MessageQueueListener): Unsubscribe | Promise<MessagePayload>;
20
+ unsubscribe(topic: string, listener: MessageQueueListener): void;
21
+ get stream(): ReadableStream<MessagePayload & {
22
+ topic: string;
23
+ }>;
24
+ }
@@ -1,10 +1,40 @@
1
1
  import type EventEmitter from "node:events";
2
- import type { Agent, AgentInput, AgentOutput } from "../agents/agent";
3
- import type { ChatModel } from "../models/chat";
2
+ import type { Agent, FunctionAgentFn, Message } from "../agents/agent.js";
3
+ import type { ChatModel } from "../models/chat-model.js";
4
+ import type { MessagePayload, MessageQueueListener, Unsubscribe } from "./message-queue.js";
5
+ export type Runnable<I extends Message = Message, O extends Message = Message> = Agent<I, O> | FunctionAgentFn;
4
6
  export interface Context extends EventEmitter {
5
7
  model?: ChatModel;
6
8
  tools?: Agent[];
7
- publish(topic: string, message: unknown): void;
8
- subscribe(topic: string, listener: (message: AgentInput) => void): void;
9
- unsubscribe(topic: string, listener: (message: AgentOutput) => void): void;
9
+ /**
10
+ * Call an agent with a message
11
+ * @param agent Agent to call
12
+ * @param message Message to pass to the agent
13
+ * @returns the output of the agent
14
+ */
15
+ call<I extends Message, O extends Message>(agent: Runnable<I, O>, message: I | string): Promise<O>;
16
+ /**
17
+ * Call an agent with a message and return the output and the active agent
18
+ * @param agent Agent to call
19
+ * @param message Message to pass to the agent
20
+ * @param options.returnActiveAgent return the active agent
21
+ * @returns the output of the agent and the final active agent
22
+ */
23
+ call<I extends Message, O extends Message>(agent: Runnable<I, O>, message: I | string, options: {
24
+ returnActiveAgent?: true;
25
+ }): Promise<[O, Runnable]>;
26
+ call<I extends Message, O extends Message>(agent: Runnable<I, O>, message: I | string, options?: {
27
+ returnActiveAgent?: boolean;
28
+ }): Promise<O | [O, Runnable]>;
29
+ /**
30
+ * Publish a message to a topic, the engine will call the listeners of the topic
31
+ * @param topic topic name, or an array of topic names
32
+ * @param message message to publish
33
+ * @param from the agent who publish the message, if not provided, it will be treated as a user message
34
+ */
35
+ publish(topic: string | string[], message: Message | string, from?: Agent): void;
36
+ subscribe(topic: string, listener?: undefined): Promise<MessagePayload>;
37
+ subscribe(topic: string, listener: MessageQueueListener): Unsubscribe;
38
+ subscribe(topic: string, listener?: MessageQueueListener): Unsubscribe | Promise<MessagePayload>;
39
+ unsubscribe(topic: string, listener: MessageQueueListener): void;
10
40
  }
@@ -0,0 +1,64 @@
1
+ import EventEmitter from "node:events";
2
+ import { Agent, type Message } from "../agents/agent.js";
3
+ import { UserAgent } from "../agents/user-agent.js";
4
+ import type { ChatModel } from "../models/chat-model.js";
5
+ import type { Context, Runnable } from "./context.js";
6
+ import { type MessagePayload, MessageQueue, type MessageQueueListener, type Unsubscribe } from "./message-queue.js";
7
+ export interface ExecutionEngineOptions {
8
+ model?: ChatModel;
9
+ tools?: Agent[];
10
+ agents?: Agent[];
11
+ }
12
+ export declare class ExecutionEngine extends EventEmitter implements Context {
13
+ constructor(options?: ExecutionEngineOptions);
14
+ readonly messageQueue: MessageQueue;
15
+ model?: ChatModel;
16
+ tools: Agent[];
17
+ private agents;
18
+ addAgent(...agents: Agent[]): void;
19
+ private createMessageFromInput;
20
+ /**
21
+ * Publish a message to a topic, the engine will call the listeners of the topic
22
+ * @param topic topic name, or an array of topic names
23
+ * @param message message to publish
24
+ * @param from the agent who publish the message, if not provided, it will be treated as a user message
25
+ */
26
+ publish(topic: string | string[], message: Message | string, from?: Agent): void;
27
+ /**
28
+ * Create a user agent to consistently call an agent
29
+ * @param agent Agent to call
30
+ * @returns User agent
31
+ */
32
+ call<I extends Message, O extends Message>(agent: Runnable<I, O>): UserAgent<I, O>;
33
+ /**
34
+ * Call an agent with a message
35
+ * @param agent Agent to call
36
+ * @param message Message to pass to the agent
37
+ * @returns the output of the agent
38
+ */
39
+ call<I extends Message, O extends Message>(agent: Runnable<I, O>, message: I | string): Promise<O>;
40
+ /**
41
+ * Call an agent with a message and return the output and the active agent
42
+ * @param agent Agent to call
43
+ * @param message Message to pass to the agent
44
+ * @param options.returnActiveAgent return the active agent
45
+ * @returns the output of the agent and the final active agent
46
+ */
47
+ call<I extends Message, O extends Message>(agent: Runnable<I, O>, message: I | string, options: {
48
+ returnActiveAgent?: true;
49
+ }): Promise<[O, Runnable]>;
50
+ /**
51
+ * Call an agent with a message and return the output and the active agent
52
+ * @param agent Agent to call
53
+ * @param message Message to pass to the agent
54
+ * @returns the output of the agent and the final active agent
55
+ */
56
+ private _call;
57
+ subscribe(topic: string, listener?: undefined): Promise<MessagePayload>;
58
+ subscribe(topic: string, listener: MessageQueueListener): Unsubscribe;
59
+ subscribe(topic: string, listener?: MessageQueueListener): Unsubscribe | Promise<MessagePayload>;
60
+ unsubscribe(topic: string, listener: MessageQueueListener): void;
61
+ private callAgent;
62
+ shutdown(): Promise<void>;
63
+ private initProcessExitHandler;
64
+ }
@@ -1,46 +1,4 @@
1
- import EventEmitter from "node:events";
2
- import { Agent, type AgentInput, type AgentOptions, type AgentOutput } from "../agents/agent";
3
- import type { ChatModel } from "../models/chat";
4
- import type { Context } from "./context";
5
- export interface ExecutionEngineOptions {
6
- model?: ChatModel;
7
- tools?: Agent[];
8
- agents?: Agent[];
9
- }
10
- export interface ExecutionEngineRunOptions {
11
- concurrency?: boolean;
12
- }
13
- export declare class ExecutionEngine extends EventEmitter implements Context {
14
- constructor(options?: ExecutionEngineOptions);
15
- private messageQueue;
16
- model?: ChatModel;
17
- tools: Agent[];
18
- private agents;
19
- private agentListeners;
20
- addAgent(...agents: Agent[]): void;
21
- private attachAgentSubscriptions;
22
- publish(topic: string, message: unknown): void;
23
- subscribe(topic: string, listener: (message: AgentOutput) => void): void;
24
- unsubscribe(topic: string, listener: (message: AgentOutput) => void): void;
25
- run(agent: Agent): Promise<UserAgent>;
26
- run(input: AgentInput | string): Promise<AgentOutput>;
27
- run(input: AgentInput | string, ...agents: Agent[]): Promise<AgentOutput>;
28
- run(input: AgentInput | string, options: ExecutionEngineRunOptions, ...agents: Agent[]): Promise<AgentOutput>;
29
- private splitOptionsAndAgents;
30
- private runSequential;
31
- private runParallel;
32
- private publishUserInputTopic;
33
- private runChatLoop;
34
- private callAgent;
35
- shutdown(): Promise<void>;
36
- private initProcessExitHandler;
37
- }
38
- export declare class UserAgent<I extends AgentInput = AgentInput, O extends AgentOutput = AgentOutput> extends Agent<I, O> {
39
- options: AgentOptions<I, O> & {
40
- run: (input: I) => Promise<O>;
41
- };
42
- constructor(options: AgentOptions<I, O> & {
43
- run: (input: I) => Promise<O>;
44
- });
45
- process(input: I): Promise<O>;
46
- }
1
+ export * from "./context.js";
2
+ export * from "./execution-engine.js";
3
+ export * from "./message-queue.js";
4
+ export * from "./utils.js";
@@ -1,5 +1,20 @@
1
- import { EventEmitter } from "node:events";
1
+ import type { Message } from "../agents/agent.js";
2
2
  export declare const UserInputTopic = "UserInputTopic";
3
3
  export declare const UserOutputTopic = "UserOutputTopic";
4
- export declare class MessageQueue extends EventEmitter {
4
+ export interface MessagePayload {
5
+ role: "user" | "agent";
6
+ source?: string;
7
+ message: Message;
8
+ }
9
+ export type MessageQueueListener = (message: MessagePayload) => void;
10
+ export type MessageRequest = MessagePayload;
11
+ export type Unsubscribe = () => void;
12
+ export declare class MessageQueue {
13
+ private events;
14
+ publish(topic: string | string[], message: MessageRequest): void;
15
+ error(error: Error): void;
16
+ subscribe(topic: string, listener?: undefined): Promise<MessagePayload>;
17
+ subscribe(topic: string, listener: MessageQueueListener): Unsubscribe;
18
+ subscribe(topic: string, listener?: MessageQueueListener): Unsubscribe | Promise<MessagePayload>;
19
+ unsubscribe(topic: string, listener: MessageQueueListener): void;
5
20
  }
@@ -0,0 +1,4 @@
1
+ import type { FunctionAgentFn } from "../agents/agent.js";
2
+ import type { Runnable } from "./context.js";
3
+ export declare function sequential(..._agents: [Runnable, ...Runnable[]]): FunctionAgentFn;
4
+ export declare function parallel(..._agents: [Runnable, ...Runnable[]]): FunctionAgentFn;