@aigne/core 1.5.1-2 → 1.7.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 (97) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/lib/cjs/agents/agent.d.ts +5 -2
  3. package/lib/cjs/agents/agent.js +42 -24
  4. package/lib/cjs/agents/ai-agent.d.ts +8 -8
  5. package/lib/cjs/agents/ai-agent.js +5 -2
  6. package/lib/cjs/agents/mcp-agent.d.ts +11 -0
  7. package/lib/cjs/agents/mcp-agent.js +37 -23
  8. package/lib/cjs/agents/user-agent.d.ts +9 -9
  9. package/lib/cjs/agents/user-agent.js +26 -16
  10. package/lib/cjs/execution-engine/context.d.ts +84 -46
  11. package/lib/cjs/execution-engine/context.js +136 -98
  12. package/lib/cjs/execution-engine/execution-engine.d.ts +20 -47
  13. package/lib/cjs/execution-engine/execution-engine.js +20 -41
  14. package/lib/cjs/execution-engine/message-queue.d.ts +3 -3
  15. package/lib/cjs/execution-engine/message-queue.js +32 -2
  16. package/lib/cjs/execution-engine/usage.d.ts +11 -0
  17. package/lib/cjs/execution-engine/usage.js +10 -0
  18. package/lib/{esm/loader/function-agent.d.ts → cjs/loader/agent-js.d.ts} +1 -5
  19. package/lib/cjs/loader/{function-agent.js → agent-js.js} +2 -2
  20. package/lib/cjs/loader/{ai-agent.d.ts → agent-yaml.d.ts} +7 -4
  21. package/lib/cjs/loader/agent-yaml.js +58 -0
  22. package/lib/cjs/loader/index.d.ts +14 -4
  23. package/lib/cjs/loader/index.js +57 -22
  24. package/lib/cjs/models/chat-model.d.ts +3 -2
  25. package/lib/cjs/models/chat-model.js +6 -5
  26. package/lib/cjs/models/claude-chat-model.js +10 -7
  27. package/lib/cjs/models/openai-chat-model.js +5 -2
  28. package/lib/cjs/prompt/prompt-builder.d.ts +1 -1
  29. package/lib/cjs/prompt/prompt-builder.js +3 -1
  30. package/lib/cjs/utils/json-schema.js +2 -2
  31. package/lib/cjs/utils/logger.d.ts +3 -15
  32. package/lib/cjs/utils/logger.js +3 -77
  33. package/lib/cjs/utils/mcp-utils.js +1 -5
  34. package/lib/cjs/utils/model-utils.js +2 -2
  35. package/lib/cjs/utils/type-utils.d.ts +1 -0
  36. package/lib/cjs/utils/typed-event-emtter.d.ts +10 -0
  37. package/lib/cjs/utils/typed-event-emtter.js +2 -0
  38. package/lib/dts/agents/agent.d.ts +5 -2
  39. package/lib/dts/agents/ai-agent.d.ts +8 -8
  40. package/lib/dts/agents/mcp-agent.d.ts +11 -0
  41. package/lib/dts/agents/user-agent.d.ts +9 -9
  42. package/lib/dts/execution-engine/context.d.ts +84 -46
  43. package/lib/dts/execution-engine/execution-engine.d.ts +20 -47
  44. package/lib/dts/execution-engine/message-queue.d.ts +3 -3
  45. package/lib/dts/execution-engine/usage.d.ts +11 -0
  46. package/lib/dts/loader/{function-agent.d.ts → agent-js.d.ts} +1 -5
  47. package/lib/dts/loader/{ai-agent.d.ts → agent-yaml.d.ts} +7 -4
  48. package/lib/dts/loader/index.d.ts +14 -4
  49. package/lib/dts/models/chat-model.d.ts +3 -2
  50. package/lib/dts/prompt/prompt-builder.d.ts +1 -1
  51. package/lib/dts/utils/logger.d.ts +3 -15
  52. package/lib/dts/utils/type-utils.d.ts +1 -0
  53. package/lib/dts/utils/typed-event-emtter.d.ts +10 -0
  54. package/lib/esm/agents/agent.d.ts +5 -2
  55. package/lib/esm/agents/agent.js +42 -24
  56. package/lib/esm/agents/ai-agent.d.ts +8 -8
  57. package/lib/esm/agents/ai-agent.js +5 -2
  58. package/lib/esm/agents/mcp-agent.d.ts +11 -0
  59. package/lib/esm/agents/mcp-agent.js +38 -24
  60. package/lib/esm/agents/user-agent.d.ts +9 -9
  61. package/lib/esm/agents/user-agent.js +26 -16
  62. package/lib/esm/execution-engine/context.d.ts +84 -46
  63. package/lib/esm/execution-engine/context.js +135 -98
  64. package/lib/esm/execution-engine/execution-engine.d.ts +20 -47
  65. package/lib/esm/execution-engine/execution-engine.js +21 -39
  66. package/lib/esm/execution-engine/message-queue.d.ts +3 -3
  67. package/lib/esm/execution-engine/message-queue.js +33 -3
  68. package/lib/esm/execution-engine/usage.d.ts +11 -0
  69. package/lib/esm/execution-engine/usage.js +7 -0
  70. package/lib/{cjs/loader/function-agent.d.ts → esm/loader/agent-js.d.ts} +1 -5
  71. package/lib/esm/loader/{function-agent.js → agent-js.js} +2 -2
  72. package/lib/esm/loader/{ai-agent.d.ts → agent-yaml.d.ts} +7 -4
  73. package/lib/esm/loader/agent-yaml.js +55 -0
  74. package/lib/esm/loader/index.d.ts +14 -4
  75. package/lib/esm/loader/index.js +56 -21
  76. package/lib/esm/models/chat-model.d.ts +3 -2
  77. package/lib/esm/models/chat-model.js +6 -5
  78. package/lib/esm/models/claude-chat-model.js +10 -7
  79. package/lib/esm/models/openai-chat-model.js +5 -2
  80. package/lib/esm/prompt/prompt-builder.d.ts +1 -1
  81. package/lib/esm/prompt/prompt-builder.js +3 -1
  82. package/lib/esm/utils/json-schema.js +2 -2
  83. package/lib/esm/utils/logger.d.ts +3 -15
  84. package/lib/esm/utils/logger.js +3 -77
  85. package/lib/esm/utils/mcp-utils.js +1 -5
  86. package/lib/esm/utils/model-utils.js +2 -2
  87. package/lib/esm/utils/type-utils.d.ts +1 -0
  88. package/lib/esm/utils/typed-event-emtter.d.ts +10 -0
  89. package/lib/esm/utils/typed-event-emtter.js +1 -0
  90. package/package.json +10 -10
  91. package/lib/cjs/loader/ai-agent.js +0 -40
  92. package/lib/cjs/utils/run-chat-loop.d.ts +0 -11
  93. package/lib/cjs/utils/run-chat-loop.js +0 -82
  94. package/lib/dts/utils/run-chat-loop.d.ts +0 -11
  95. package/lib/esm/loader/ai-agent.js +0 -37
  96. package/lib/esm/utils/run-chat-loop.d.ts +0 -11
  97. package/lib/esm/utils/run-chat-loop.js +0 -76
@@ -33,11 +33,7 @@ function promptFromMCPPrompt(prompt, options) {
33
33
  }
34
34
  function resourceFromMCPResource(resource, options) {
35
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
- ]
36
+ ? [resource.uriTemplate, new uriTemplate_js_1.UriTemplate(resource.uriTemplate).variableNames]
41
37
  : [resource.uri, []];
42
38
  return new mcp_agent_js_1.MCPResource({
43
39
  ...options,
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.mergeUsage = mergeUsage;
4
4
  function mergeUsage(...usages) {
5
5
  return {
6
- promptTokens: usages.reduce((acc, usage) => (usage ? acc + usage.promptTokens : acc), 0),
7
- completionTokens: usages.reduce((acc, usage) => (usage ? acc + usage.completionTokens : acc), 0),
6
+ inputTokens: usages.reduce((acc, usage) => (usage ? acc + usage.inputTokens : acc), 0),
7
+ outputTokens: usages.reduce((acc, usage) => (usage ? acc + usage.outputTokens : acc), 0),
8
8
  };
9
9
  }
@@ -1,6 +1,7 @@
1
1
  import { type ZodType } from "zod";
2
2
  export type PromiseOrValue<T> = T | Promise<T>;
3
3
  export type Nullish<T> = T | null | undefined;
4
+ export type OmitPropertiesFromArrayFirstElement<T extends unknown[], K extends string | number | symbol> = T extends [infer U, ...infer Rest] ? [Omit<U, K>, ...Rest] : never;
4
5
  export declare function isNil(value: unknown): value is null | undefined;
5
6
  export declare function isEmpty(obj: unknown): boolean;
6
7
  export declare function isNonNullable<T>(value: T): value is NonNullable<T>;
@@ -0,0 +1,10 @@
1
+ type EventMap<T> = Record<keyof T, unknown[]>;
2
+ export type Args<K, T> = K extends keyof T ? T[K] : never;
3
+ export type Listener<K, T> = K extends keyof T ? T[K] extends unknown[] ? (...args: T[K]) => void : never : never;
4
+ export interface TypedEventEmitter<T extends EventMap<T>, E extends EventMap<E> = T> {
5
+ emit<K extends keyof E>(eventName: K, ...args: Args<K, E>): boolean;
6
+ on<K extends keyof T>(eventName: K, listener: Listener<K, T>): this;
7
+ once<K extends keyof T>(eventName: K, listener: Listener<K, T>): this;
8
+ off<K extends keyof T>(eventName: K, listener: Listener<K, T>): this;
9
+ }
10
+ export {};
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,3 +1,4 @@
1
+ import { inspect } from "node:util";
1
2
  import { type ZodType } from "zod";
2
3
  import type { Context } from "../execution-engine/context.js";
3
4
  import { type Nullish, type PromiseOrValue } from "../utils/type-utils.js";
@@ -15,7 +16,7 @@ export interface AgentOptions<I extends Message = Message, O extends Message = M
15
16
  outputSchema?: AgentInputOutputSchema<O>;
16
17
  includeInputInOutput?: boolean;
17
18
  tools?: (Agent | FunctionAgentFn)[];
18
- disableLogging?: boolean;
19
+ disableEvents?: boolean;
19
20
  memory?: AgentMemory | AgentMemoryOptions | true;
20
21
  }
21
22
  export declare abstract class Agent<I extends Message = Message, O extends Message = Message> {
@@ -37,7 +38,7 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
37
38
  readonly tools: Agent<Message, Message>[] & {
38
39
  [key: string]: Agent<Message, Message>;
39
40
  };
40
- private disableLogging?;
41
+ private disableEvents?;
41
42
  /**
42
43
  * Attach agent to context:
43
44
  * - subscribe to topic and call process method when message received
@@ -50,10 +51,12 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
50
51
  private checkContextStatus;
51
52
  private newDefaultContext;
52
53
  call(input: I | string, context?: Context): Promise<O>;
54
+ protected checkUsageAgentCalls(context: Context): void;
53
55
  protected preprocess(_: I, context: Context): void;
54
56
  protected postprocess(input: I, output: O, context: Context): void;
55
57
  abstract process(input: I, context: Context): Promise<O | TransferAgentOutput>;
56
58
  shutdown(): Promise<void>;
59
+ [inspect.custom](): string;
57
60
  }
58
61
  export type AgentInputOutputSchema<I extends Message = Message> = ZodType<I> | ((agent: Agent) => ZodType<I>);
59
62
  export interface FunctionAgentOptions<I extends Message = Message, O extends Message = Message> extends AgentOptions<I, O> {
@@ -28,6 +28,10 @@ export declare const aiAgentOptionsSchema: z.ZodObject<{
28
28
  memory: z.ZodOptional<z.ZodUnion<[z.ZodBoolean, z.ZodAny, z.ZodAny]>>;
29
29
  }, "strip", z.ZodTypeAny, {
30
30
  description?: string | undefined;
31
+ memory?: any;
32
+ includeInputInOutput?: boolean | undefined;
33
+ subscribeTopic?: string | string[] | undefined;
34
+ publishTopic?: string | string[] | ((...args: unknown[]) => unknown) | undefined;
31
35
  tools?: (Agent<Message, Message> | ((...args: unknown[]) => unknown))[] | undefined;
32
36
  toolChoice?: Agent<Message, Message> | "auto" | "none" | "required" | "router" | undefined;
33
37
  name?: string | undefined;
@@ -36,13 +40,13 @@ export declare const aiAgentOptionsSchema: z.ZodObject<{
36
40
  outputKey?: string | undefined;
37
41
  enableHistory?: boolean | undefined;
38
42
  maxHistoryMessages?: number | undefined;
39
- includeInputInOutput?: boolean | undefined;
40
- subscribeTopic?: string | string[] | undefined;
41
- publishTopic?: string | string[] | ((...args: unknown[]) => unknown) | undefined;
42
43
  disableLogging?: boolean | undefined;
43
- memory?: any;
44
44
  }, {
45
45
  description?: string | undefined;
46
+ memory?: any;
47
+ includeInputInOutput?: boolean | undefined;
48
+ subscribeTopic?: string | string[] | undefined;
49
+ publishTopic?: string | string[] | ((...args: unknown[]) => unknown) | undefined;
46
50
  tools?: (Agent<Message, Message> | ((...args: unknown[]) => unknown))[] | undefined;
47
51
  toolChoice?: Agent<Message, Message> | "auto" | "none" | "required" | "router" | undefined;
48
52
  name?: string | undefined;
@@ -51,11 +55,7 @@ export declare const aiAgentOptionsSchema: z.ZodObject<{
51
55
  outputKey?: string | undefined;
52
56
  enableHistory?: boolean | undefined;
53
57
  maxHistoryMessages?: number | undefined;
54
- includeInputInOutput?: boolean | undefined;
55
- subscribeTopic?: string | string[] | undefined;
56
- publishTopic?: string | string[] | ((...args: unknown[]) => unknown) | undefined;
57
58
  disableLogging?: boolean | undefined;
58
- memory?: any;
59
59
  }>;
60
60
  export declare class AIAgent<I extends Message = Message, O extends Message = Message> extends Agent<I, O> {
61
61
  static from<I extends Message, O extends Message>(options: AIAgentOptions<I, O>): AIAgent<I, O>;
@@ -1,4 +1,5 @@
1
1
  import { Client, type ClientOptions } from "@modelcontextprotocol/sdk/client/index.js";
2
+ import { type SSEClientTransportOptions } from "@modelcontextprotocol/sdk/client/sse.js";
2
3
  import { type StdioServerParameters } from "@modelcontextprotocol/sdk/client/stdio.js";
3
4
  import type { RequestOptions } from "@modelcontextprotocol/sdk/shared/protocol.js";
4
5
  import type { Transport } from "@modelcontextprotocol/sdk/shared/transport.js";
@@ -15,6 +16,15 @@ export interface MCPAgentOptions extends AgentOptions {
15
16
  export type MCPServerOptions = SSEServerParameters | StdioServerParameters;
16
17
  export type SSEServerParameters = {
17
18
  url: string;
19
+ /**
20
+ * Additional options to pass to the SSEClientTransport.
21
+ */
22
+ opts?: SSEClientTransportOptions;
23
+ /**
24
+ * The timeout for requests to the server, in milliseconds.
25
+ * @default 10000
26
+ */
27
+ timeout?: number;
18
28
  /**
19
29
  * Whether to automatically reconnect to the server if the connection is lost.
20
30
  * @default 10 set to 0 to disable automatic reconnection
@@ -44,6 +54,7 @@ export declare class MCPAgent extends Agent {
44
54
  }
45
55
  export interface ClientWithReconnectOptions {
46
56
  transportCreator?: () => PromiseOrValue<Transport>;
57
+ timeout?: number;
47
58
  maxReconnects?: number;
48
59
  shouldReconnect?: (error: Error) => boolean;
49
60
  }
@@ -1,26 +1,26 @@
1
1
  import { ReadableStream } from "node:stream/web";
2
- import type { Context } from "../execution-engine/context.js";
3
- import type { MessagePayload, MessageQueueListener, Unsubscribe } from "../execution-engine/message-queue.js";
2
+ import { type Context, type Runnable } from "../execution-engine/context.js";
3
+ import type { MessagePayload } from "../execution-engine/message-queue.js";
4
4
  import { type PromiseOrValue } from "../utils/type-utils.js";
5
5
  import { Agent, type AgentOptions, type Message } from "./agent.js";
6
6
  export interface UserAgentOptions<I extends Message = Message, O extends Message = Message> extends AgentOptions<I, O> {
7
7
  context: Context;
8
8
  process?: (input: I, context: Context) => PromiseOrValue<O>;
9
+ activeAgent?: Runnable;
9
10
  }
10
11
  export declare class UserAgent<I extends Message = Message, O extends Message = Message> extends Agent<I, O> {
11
12
  static from<I extends Message, O extends Message>(options: UserAgentOptions<I, O>): UserAgent<I, O>;
12
13
  constructor(options: UserAgentOptions<I, O>);
13
- readonly context: Context;
14
- private get ctx();
14
+ context: Context;
15
15
  private _process?;
16
+ private activeAgent?;
16
17
  call(input: string | I, context?: Context): Promise<O>;
17
18
  process(input: I, context: Context): Promise<O>;
18
- publish(topic: string | string[], message: Message | string): void;
19
- subscribe(topic: string, listener?: undefined): Promise<MessagePayload>;
20
- subscribe(topic: string, listener: MessageQueueListener): Unsubscribe;
21
- subscribe(topic: string, listener?: MessageQueueListener): Unsubscribe | Promise<MessagePayload>;
22
- unsubscribe(topic: string, listener: MessageQueueListener): void;
19
+ publish: Context["publish"];
20
+ subscribe: Context["subscribe"];
21
+ unsubscribe: Context["unsubscribe"];
23
22
  get stream(): ReadableStream<MessagePayload & {
24
23
  topic: string;
25
24
  }>;
25
+ protected checkUsageAgentCalls(_context: Context): void;
26
26
  }
@@ -2,19 +2,36 @@ import EventEmitter from "node:events";
2
2
  import { Agent, type FunctionAgentFn, type Message } from "../agents/agent.js";
3
3
  import { UserAgent } from "../agents/user-agent.js";
4
4
  import type { ChatModel } from "../models/chat-model.js";
5
+ import { type OmitPropertiesFromArrayFirstElement } from "../utils/type-utils.js";
6
+ import type { Args, Listener, TypedEventEmitter } from "../utils/typed-event-emtter.js";
5
7
  import { type MessagePayload, MessageQueue, type MessageQueueListener, type Unsubscribe } from "./message-queue.js";
8
+ import { type ContextLimits, type ContextUsage } from "./usage.js";
6
9
  export type Runnable<I extends Message = Message, O extends Message = Message> = Agent<I, O> | FunctionAgentFn;
7
- export interface ContextUsage {
8
- promptTokens: number;
9
- completionTokens: number;
10
- agentCalls: number;
10
+ export interface AgentEvent {
11
+ parentContextId?: string;
12
+ contextId: string;
13
+ timestamp: number;
14
+ agent: Agent;
11
15
  }
12
- export interface ContextLimits {
13
- maxTokens?: number;
14
- maxAgentCalls?: number;
15
- timeout?: number;
16
+ export interface ContextEventMap {
17
+ agentStarted: [AgentEvent & {
18
+ input: Message;
19
+ }];
20
+ agentSucceed: [AgentEvent & {
21
+ output: Message;
22
+ }];
23
+ agentFailed: [AgentEvent & {
24
+ error: Error;
25
+ }];
16
26
  }
17
- export interface Context extends EventEmitter {
27
+ export type ContextEmitEventMap = {
28
+ [K in keyof ContextEventMap]: OmitPropertiesFromArrayFirstElement<ContextEventMap[K], "contextId" | "parentContextId" | "timestamp">;
29
+ };
30
+ export interface CallOptions {
31
+ returnActiveAgent?: boolean;
32
+ disableTransfer?: boolean;
33
+ }
34
+ export interface Context extends TypedEventEmitter<ContextEventMap, ContextEmitEventMap> {
18
35
  model?: ChatModel;
19
36
  tools?: Agent[];
20
37
  usage: ContextUsage;
@@ -26,13 +43,6 @@ export interface Context extends EventEmitter {
26
43
  * @returns User agent
27
44
  */
28
45
  call<I extends Message, O extends Message>(agent: Runnable<I, O>): UserAgent<I, O>;
29
- /**
30
- * Call an agent with a message
31
- * @param agent Agent to call
32
- * @param message Message to pass to the agent
33
- * @returns the output of the agent
34
- */
35
- call<I extends Message, O extends Message>(agent: Runnable<I, O>, message: I | string): Promise<O>;
36
46
  /**
37
47
  * Call an agent with a message and return the output and the active agent
38
48
  * @param agent Agent to call
@@ -40,54 +50,82 @@ export interface Context extends EventEmitter {
40
50
  * @param options.returnActiveAgent return the active agent
41
51
  * @returns the output of the agent and the final active agent
42
52
  */
43
- call<I extends Message, O extends Message>(agent: Runnable<I, O>, message: I | string, options: {
53
+ call<I extends Message, O extends Message>(agent: Runnable<I, O>, message: I | string, options: CallOptions & {
44
54
  returnActiveAgent: true;
45
55
  }): Promise<[O, Runnable]>;
46
- call<I extends Message, O extends Message>(agent: Runnable<I, O>, message: I | string, options?: {
47
- returnActiveAgent?: boolean;
48
- }): UserAgent<I, O> | Promise<O | [O, Runnable]>;
56
+ /**
57
+ * Call an agent with a message
58
+ * @param agent Agent to call
59
+ * @param message Message to pass to the agent
60
+ * @returns the output of the agent
61
+ */
62
+ call<I extends Message, O extends Message>(agent: Runnable<I, O>, message: I | string, options?: CallOptions): Promise<O>;
63
+ call<I extends Message, O extends Message>(agent: Runnable<I, O>, message?: I | string, options?: CallOptions): UserAgent<I, O> | Promise<O | [O, Runnable]>;
49
64
  /**
50
65
  * Publish a message to a topic, the engine will call the listeners of the topic
51
66
  * @param topic topic name, or an array of topic names
52
- * @param message message to publish
53
- * @param from the agent who publish the message, if not provided, it will be treated as a user message
67
+ * @param payload message to publish
54
68
  */
55
- publish(topic: string | string[], message: Message | string, from?: Agent): void;
69
+ publish(topic: string | string[], payload: Omit<MessagePayload, "context">): void;
56
70
  subscribe(topic: string, listener?: undefined): Promise<MessagePayload>;
57
71
  subscribe(topic: string, listener: MessageQueueListener): Unsubscribe;
58
72
  subscribe(topic: string, listener?: MessageQueueListener): Unsubscribe | Promise<MessagePayload>;
73
+ subscribe(topic: string, listener?: MessageQueueListener): Unsubscribe | Promise<MessagePayload>;
59
74
  unsubscribe(topic: string, listener: MessageQueueListener): void;
75
+ /**
76
+ * Create a child context with the same configuration as the parent context.
77
+ * If `reset` is true, the child context will have a new state (such as: usage).
78
+ *
79
+ * @param options
80
+ * @param options.reset create a new context with initial state (such as: usage)
81
+ * @returns new context
82
+ */
83
+ newContext(options?: {
84
+ reset?: boolean;
85
+ }): Context;
86
+ }
87
+ export declare function createPublishMessage(message: string | Message, from?: Agent): Omit<MessagePayload, "context">;
88
+ export declare class ExecutionContext implements Context {
89
+ constructor(parent?: ConstructorParameters<typeof ExecutionContextInternal>[0]);
90
+ parentId?: string;
91
+ id: string;
92
+ readonly internal: ExecutionContextInternal;
93
+ get model(): ChatModel | undefined;
94
+ get tools(): Agent<Message, Message>[] | undefined;
95
+ get limits(): ContextLimits | undefined;
96
+ get status(): "normal" | "timeout";
97
+ get usage(): ContextUsage;
98
+ newContext({ reset }?: {
99
+ reset?: boolean;
100
+ }): ExecutionContext;
101
+ call: Context["call"];
102
+ publish: Context["publish"];
103
+ subscribe: Context["subscribe"];
104
+ unsubscribe: Context["unsubscribe"];
105
+ emit<K extends keyof ContextEmitEventMap>(eventName: K, ...args: Args<K, ContextEmitEventMap>): boolean;
106
+ on<K extends keyof ContextEventMap>(eventName: K, listener: Listener<K, ContextEventMap>): this;
107
+ once<K extends keyof ContextEventMap>(eventName: K, listener: Listener<K, ContextEventMap>): this;
108
+ off<K extends keyof ContextEventMap>(eventName: K, listener: Listener<K, ContextEventMap>): this;
60
109
  }
61
- export declare class ExecutionContext extends EventEmitter implements Context {
62
- private readonly engine?;
63
- constructor(engine?: {
64
- model?: ChatModel;
65
- tools?: Agent[];
66
- limits?: ContextLimits;
110
+ declare class ExecutionContextInternal {
111
+ private readonly parent?;
112
+ constructor(parent?: (Pick<Context, "model" | "tools" | "limits"> & {
67
113
  messageQueue?: MessageQueue;
68
- emit?: EventEmitter["emit"];
69
- } | undefined);
70
- private messageQueue;
114
+ }) | undefined);
115
+ readonly messageQueue: MessageQueue;
116
+ readonly events: EventEmitter<ContextEventMap>;
71
117
  get model(): ChatModel | undefined;
72
118
  get tools(): Agent<Message, Message>[] | undefined;
119
+ get limits(): ContextLimits | undefined;
73
120
  usage: ContextUsage;
74
- limits?: ContextLimits | undefined;
75
121
  private abortController;
76
122
  private timer?;
77
123
  private initTimeout;
78
124
  get status(): "normal" | "timeout";
79
- call<I extends Message, O extends Message>(agent: Runnable<I, O>): UserAgent<I, O>;
80
- call<I extends Message, O extends Message>(agent: Runnable<I, O>, message: I | string): Promise<O>;
81
- call<I extends Message, O extends Message>(agent: Runnable<I, O>, message: I | string, options: {
82
- returnActiveAgent: true;
83
- }): Promise<[O, Runnable]>;
84
- call<I extends Message, O extends Message>(agent: Runnable<I, O>, message?: I | string, options?: {
85
- returnActiveAgent?: boolean;
86
- }): UserAgent<I, O> | Promise<O | [O, Runnable]>;
87
- publish(topic: string | string[], message: Message | string, from?: Agent): void;
88
- subscribe(topic: string, listener?: undefined): Promise<MessagePayload>;
89
- subscribe(topic: string, listener: MessageQueueListener): Unsubscribe;
90
- unsubscribe(topic: string, listener: MessageQueueListener): void;
91
- emit(eventName: string | symbol, ...args: unknown[]): boolean;
125
+ call<I extends Message, O extends Message>(agent: Runnable<I, O>, input: I, context: Context, options?: CallOptions): Promise<{
126
+ agent: Runnable;
127
+ output: O;
128
+ }>;
92
129
  private callAgent;
93
130
  }
131
+ export {};
@@ -1,68 +1,41 @@
1
- import EventEmitter from "node:events";
2
- import { Agent, type Message } from "../agents/agent.js";
3
- import type { UserAgent } from "../agents/user-agent.js";
1
+ import { Agent } from "../agents/agent.js";
4
2
  import { ChatModel } from "../models/chat-model.js";
5
- import { type ContextLimits, ExecutionContext, type Runnable } from "./context.js";
6
- import { type MessagePayload, MessageQueue, type MessageQueueListener, type Unsubscribe } from "./message-queue.js";
3
+ import { type Context, ExecutionContext } from "./context.js";
4
+ import { MessageQueue } from "./message-queue.js";
5
+ import type { ContextLimits } from "./usage.js";
7
6
  export interface ExecutionEngineOptions {
7
+ name?: string;
8
+ description?: string;
8
9
  model?: ChatModel;
9
10
  tools?: Agent[];
10
11
  agents?: Agent[];
11
12
  limits?: ContextLimits;
12
13
  }
13
- export declare class ExecutionEngine extends EventEmitter {
14
+ export interface ExecutionEngineRunOptions {
15
+ returnActiveAgent?: boolean;
16
+ }
17
+ export declare class ExecutionEngine {
14
18
  static load({ path, ...options }: {
15
19
  path: string;
16
20
  } & ExecutionEngineOptions): Promise<ExecutionEngine>;
17
21
  constructor(options?: ExecutionEngineOptions);
22
+ name?: string;
23
+ description?: string;
18
24
  readonly messageQueue: MessageQueue;
19
25
  model?: ChatModel;
20
- readonly tools: Agent<Message, Message>[] & {
21
- [key: string]: Agent<Message, Message>;
26
+ readonly tools: Agent<import("../agents/agent.js").Message, import("../agents/agent.js").Message>[] & {
27
+ [key: string]: Agent<import("../agents/agent.js").Message, import("../agents/agent.js").Message>;
22
28
  };
23
- readonly agents: Agent<Message, Message>[] & {
24
- [key: string]: Agent<Message, Message>;
29
+ readonly agents: Agent<import("../agents/agent.js").Message, import("../agents/agent.js").Message>[] & {
30
+ [key: string]: Agent<import("../agents/agent.js").Message, import("../agents/agent.js").Message>;
25
31
  };
26
32
  limits?: ContextLimits;
27
33
  addAgent(...agents: Agent[]): void;
28
34
  newContext(): ExecutionContext;
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
- /**
37
- * Create a user agent to consistently call an agent
38
- * @param agent Agent to call
39
- * @returns User agent
40
- */
41
- call<I extends Message, O extends Message>(agent: Runnable<I, O>): UserAgent<I, O>;
42
- /**
43
- * Call an agent with a message
44
- * @param agent Agent to call
45
- * @param message Message to pass to the agent
46
- * @returns the output of the agent
47
- */
48
- call<I extends Message, O extends Message>(agent: Runnable<I, O>, message: I | string): Promise<O>;
49
- /**
50
- * Call an agent with a message and return the output and the active agent
51
- * @param agent Agent to call
52
- * @param message Message to pass to the agent
53
- * @param options.returnActiveAgent return the active agent
54
- * @returns the output of the agent and the final active agent
55
- */
56
- call<I extends Message, O extends Message>(agent: Runnable<I, O>, message: I | string, options: {
57
- returnActiveAgent: true;
58
- }): Promise<[O, Runnable]>;
59
- call<I extends Message, O extends Message>(agent: Runnable<I, O>, message?: I | string, options?: {
60
- returnActiveAgent?: boolean;
61
- }): UserAgent<I, O> | Promise<O | [O, Runnable]>;
62
- subscribe(topic: string, listener?: undefined): Promise<MessagePayload>;
63
- subscribe(topic: string, listener: MessageQueueListener): Unsubscribe;
64
- subscribe(topic: string, listener?: MessageQueueListener): Unsubscribe | Promise<MessagePayload>;
65
- unsubscribe(topic: string, listener: MessageQueueListener): void;
35
+ publish: Context["publish"];
36
+ call: Context["call"];
37
+ subscribe: Context["subscribe"];
38
+ unsubscribe: Context["unsubscribe"];
66
39
  shutdown(): Promise<void>;
67
40
  private initProcessExitHandler;
68
41
  }
@@ -1,3 +1,4 @@
1
+ import { EventEmitter } from "node:events";
1
2
  import type { Message } from "../agents/agent.js";
2
3
  import type { Context } from "./context.js";
3
4
  export declare const UserInputTopic = "UserInputTopic";
@@ -9,11 +10,10 @@ export interface MessagePayload {
9
10
  context: Context;
10
11
  }
11
12
  export type MessageQueueListener = (message: MessagePayload) => void;
12
- export type MessageRequest = MessagePayload;
13
13
  export type Unsubscribe = () => void;
14
14
  export declare class MessageQueue {
15
- private events;
16
- publish(topic: string | string[], message: MessageRequest): void;
15
+ events: EventEmitter<[never]>;
16
+ publish(topic: string | string[], payload: MessagePayload): void;
17
17
  error(error: Error): void;
18
18
  subscribe(topic: string, listener?: undefined): Promise<MessagePayload>;
19
19
  subscribe(topic: string, listener: MessageQueueListener): Unsubscribe;
@@ -0,0 +1,11 @@
1
+ export interface ContextUsage {
2
+ inputTokens: number;
3
+ outputTokens: number;
4
+ agentCalls: number;
5
+ }
6
+ export declare function newEmptyContextUsage(): ContextUsage;
7
+ export interface ContextLimits {
8
+ maxTokens?: number;
9
+ maxAgentCalls?: number;
10
+ timeout?: number;
11
+ }
@@ -1,10 +1,6 @@
1
1
  import { type ZodObject, type ZodType, z } from "zod";
2
2
  import type { Message } from "../agents/agent.js";
3
- export declare function loadAgentFromJsFile(path: string, { import: _import }?: {
4
- import?: (path: string) => Promise<{
5
- default: unknown;
6
- }>;
7
- }): Promise<{
3
+ export declare function loadAgentFromJsFile(path: string): Promise<{
8
4
  name: string;
9
5
  fn: (args_0: Message) => Message;
10
6
  description?: string | undefined;
@@ -1,8 +1,6 @@
1
- import { readFile } from "node:fs/promises";
2
1
  import { type ZodObject, type ZodType, z } from "zod";
3
- export declare function loadAgentFromYamlFile(path: string, { readFile: _readFile }?: {
4
- readFile?: typeof readFile;
5
- }): Promise<{
2
+ export declare function loadAgentFromYamlFile(path: string): Promise<{
3
+ type: "ai";
6
4
  name: string;
7
5
  description?: string | undefined;
8
6
  tools?: string[] | undefined;
@@ -18,4 +16,9 @@ export declare function loadAgentFromYamlFile(path: string, { readFile: _readFil
18
16
  [x: string]: any;
19
17
  }> | undefined;
20
18
  output_key?: string | undefined;
19
+ } | {
20
+ type: "mcp";
21
+ url?: string | undefined;
22
+ command?: string | undefined;
23
+ args?: string[] | undefined;
21
24
  }>;
@@ -1,4 +1,3 @@
1
- import { readFile } from "node:fs/promises";
2
1
  import { type Agent } from "../agents/agent.js";
3
2
  import type { ChatModel } from "../models/chat-model.js";
4
3
  export interface LoadOptions {
@@ -8,15 +7,26 @@ export declare function load(options: LoadOptions): Promise<{
8
7
  model: ChatModel | undefined;
9
8
  agents: Agent<import("../agents/agent.js").Message, import("../agents/agent.js").Message>[];
10
9
  tools: Agent<import("../agents/agent.js").Message, import("../agents/agent.js").Message>[];
10
+ description?: string | null | undefined;
11
+ name?: string | null | undefined;
12
+ chat_model?: {
13
+ name?: string | null | undefined;
14
+ temperature?: number | null | undefined;
15
+ provider?: string | null | undefined;
16
+ top_p?: number | null | undefined;
17
+ frequent_penalty?: number | null | undefined;
18
+ presence_penalty?: number | null | undefined;
19
+ } | null | undefined;
11
20
  }>;
12
21
  export declare function loadAgent(path: string): Promise<Agent>;
13
- export declare function loadAIGNEFile(path: string, { readFile: _readFile }?: {
14
- readFile?: typeof readFile;
15
- }): Promise<{
22
+ export declare function loadAIGNEFile(path: string): Promise<{
23
+ description?: string | null | undefined;
16
24
  tools?: string[] | null | undefined;
25
+ name?: string | null | undefined;
17
26
  chat_model?: {
18
27
  name?: string | null | undefined;
19
28
  temperature?: number | null | undefined;
29
+ provider?: string | null | undefined;
20
30
  top_p?: number | null | undefined;
21
31
  frequent_penalty?: number | null | undefined;
22
32
  presence_penalty?: number | null | undefined;
@@ -75,6 +75,7 @@ export interface ChatModelOutput extends Message {
75
75
  json?: object;
76
76
  toolCalls?: ChatModelOutputToolCall[];
77
77
  usage?: ChatModelOutputUsage;
78
+ model?: string;
78
79
  }
79
80
  export interface ChatModelOutputToolCall {
80
81
  id: string;
@@ -85,6 +86,6 @@ export interface ChatModelOutputToolCall {
85
86
  };
86
87
  }
87
88
  export interface ChatModelOutputUsage {
88
- promptTokens: number;
89
- completionTokens: number;
89
+ inputTokens: number;
90
+ outputTokens: number;
90
91
  }
@@ -7,7 +7,7 @@ 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
9
  export declare const DEFAULT_MAX_HISTORY_MESSAGES = 10;
10
- export declare function createMessage(message: string | object): Message;
10
+ export declare function createMessage<I extends Message>(message: string | I): I;
11
11
  export declare function getMessage(input: Message): string | undefined;
12
12
  export interface PromptBuilderOptions {
13
13
  instructions?: string | ChatMessagesTemplate;
@@ -1,20 +1,8 @@
1
- import debug, { type Debugger } from "debug";
2
- import type { Ora } from "ora";
3
- interface DebugWithSpinner extends Debugger {
4
- spinner<T>(promise: Promise<T>, message?: string, callback?: (result: T) => void, options?: {
5
- disabled?: boolean;
6
- }): Promise<T>;
7
- extend: (namespace: string) => DebugWithSpinner;
8
- }
9
- declare function spinner<T>(promise: Promise<T>, message?: string, callback?: (result: T) => void): Promise<T>;
1
+ import debug from "debug";
10
2
  export declare const logger: debug.Debug & {
11
3
  debug: debug.Debug;
12
4
  default: debug.Debug;
13
5
  } & {
14
- globalSpinner: Ora | undefined;
15
- base: DebugWithSpinner;
16
- debug: DebugWithSpinner;
17
- spinner: typeof spinner;
18
- setSpinner: (spinner: Ora) => void;
6
+ core: debug.Debugger;
7
+ mcp: debug.Debugger;
19
8
  };
20
- export {};
@@ -1,6 +1,7 @@
1
1
  import { type ZodType } from "zod";
2
2
  export type PromiseOrValue<T> = T | Promise<T>;
3
3
  export type Nullish<T> = T | null | undefined;
4
+ export type OmitPropertiesFromArrayFirstElement<T extends unknown[], K extends string | number | symbol> = T extends [infer U, ...infer Rest] ? [Omit<U, K>, ...Rest] : never;
4
5
  export declare function isNil(value: unknown): value is null | undefined;
5
6
  export declare function isEmpty(obj: unknown): boolean;
6
7
  export declare function isNonNullable<T>(value: T): value is NonNullable<T>;
@@ -0,0 +1,10 @@
1
+ type EventMap<T> = Record<keyof T, unknown[]>;
2
+ export type Args<K, T> = K extends keyof T ? T[K] : never;
3
+ export type Listener<K, T> = K extends keyof T ? T[K] extends unknown[] ? (...args: T[K]) => void : never : never;
4
+ export interface TypedEventEmitter<T extends EventMap<T>, E extends EventMap<E> = T> {
5
+ emit<K extends keyof E>(eventName: K, ...args: Args<K, E>): boolean;
6
+ on<K extends keyof T>(eventName: K, listener: Listener<K, T>): this;
7
+ once<K extends keyof T>(eventName: K, listener: Listener<K, T>): this;
8
+ off<K extends keyof T>(eventName: K, listener: Listener<K, T>): this;
9
+ }
10
+ export {};