@aigne/core 1.19.0 → 1.20.1

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 (54) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/lib/cjs/agents/agent.d.ts +11 -6
  3. package/lib/cjs/agents/agent.js +8 -10
  4. package/lib/cjs/agents/ai-agent.d.ts +18 -6
  5. package/lib/cjs/agents/ai-agent.js +14 -4
  6. package/lib/cjs/agents/guide-rail-agent.d.ts +1 -1
  7. package/lib/cjs/agents/mcp-agent.d.ts +1 -1
  8. package/lib/cjs/aigne/aigne.d.ts +10 -10
  9. package/lib/cjs/aigne/context.d.ts +8 -6
  10. package/lib/cjs/aigne/context.js +62 -42
  11. package/lib/cjs/aigne/message-queue.d.ts +1 -1
  12. package/lib/cjs/aigne/message-queue.js +2 -3
  13. package/lib/cjs/aigne/usage.d.ts +1 -0
  14. package/lib/cjs/aigne/usage.js +1 -0
  15. package/lib/cjs/loader/agent-yaml.d.ts +2 -1
  16. package/lib/cjs/loader/agent-yaml.js +4 -0
  17. package/lib/cjs/prompt/prompt-builder.d.ts +2 -16
  18. package/lib/cjs/prompt/prompt-builder.js +12 -25
  19. package/lib/cjs/utils/stream-utils.d.ts +1 -2
  20. package/lib/cjs/utils/type-utils.d.ts +1 -0
  21. package/lib/cjs/utils/type-utils.js +13 -0
  22. package/lib/dts/agents/agent.d.ts +11 -6
  23. package/lib/dts/agents/ai-agent.d.ts +18 -6
  24. package/lib/dts/agents/guide-rail-agent.d.ts +1 -1
  25. package/lib/dts/agents/mcp-agent.d.ts +1 -1
  26. package/lib/dts/aigne/aigne.d.ts +10 -10
  27. package/lib/dts/aigne/context.d.ts +8 -6
  28. package/lib/dts/aigne/message-queue.d.ts +1 -1
  29. package/lib/dts/aigne/usage.d.ts +1 -0
  30. package/lib/dts/loader/agent-yaml.d.ts +2 -1
  31. package/lib/dts/prompt/prompt-builder.d.ts +2 -16
  32. package/lib/dts/utils/stream-utils.d.ts +1 -2
  33. package/lib/dts/utils/type-utils.d.ts +1 -0
  34. package/lib/esm/agents/agent.d.ts +11 -6
  35. package/lib/esm/agents/agent.js +8 -10
  36. package/lib/esm/agents/ai-agent.d.ts +18 -6
  37. package/lib/esm/agents/ai-agent.js +15 -5
  38. package/lib/esm/agents/guide-rail-agent.d.ts +1 -1
  39. package/lib/esm/agents/mcp-agent.d.ts +1 -1
  40. package/lib/esm/aigne/aigne.d.ts +10 -10
  41. package/lib/esm/aigne/context.d.ts +8 -6
  42. package/lib/esm/aigne/context.js +63 -43
  43. package/lib/esm/aigne/message-queue.d.ts +1 -1
  44. package/lib/esm/aigne/message-queue.js +2 -3
  45. package/lib/esm/aigne/usage.d.ts +1 -0
  46. package/lib/esm/aigne/usage.js +1 -0
  47. package/lib/esm/loader/agent-yaml.d.ts +2 -1
  48. package/lib/esm/loader/agent-yaml.js +4 -0
  49. package/lib/esm/prompt/prompt-builder.d.ts +2 -16
  50. package/lib/esm/prompt/prompt-builder.js +12 -23
  51. package/lib/esm/utils/stream-utils.d.ts +1 -2
  52. package/lib/esm/utils/type-utils.d.ts +1 -0
  53. package/lib/esm/utils/type-utils.js +12 -0
  54. package/package.json +2 -2
@@ -5,6 +5,7 @@ export interface ContextUsage {
5
5
  inputTokens: number;
6
6
  outputTokens: number;
7
7
  agentCalls: number;
8
+ duration: number;
8
9
  }
9
10
  /**
10
11
  * @hidden
@@ -9,5 +9,6 @@ function newEmptyContextUsage() {
9
9
  inputTokens: 0,
10
10
  outputTokens: 0,
11
11
  agentCalls: 0,
12
+ duration: 0,
12
13
  };
13
14
  }
@@ -5,11 +5,11 @@ export declare function loadAgentFromYamlFile(path: string): Promise<{
5
5
  name: string;
6
6
  description?: string | undefined;
7
7
  skills?: string[] | undefined;
8
- instructions?: string | undefined;
9
8
  memory?: true | {
10
9
  provider: string;
11
10
  subscribeTopic?: string[] | undefined;
12
11
  } | undefined;
12
+ instructions?: string | undefined;
13
13
  inputSchema?: ZodObject<Record<string, ZodType<any, z.ZodTypeDef, any>>, z.UnknownKeysParam, z.ZodTypeAny, {
14
14
  [x: string]: any;
15
15
  }, {
@@ -20,6 +20,7 @@ export declare function loadAgentFromYamlFile(path: string): Promise<{
20
20
  }, {
21
21
  [x: string]: any;
22
22
  }> | undefined;
23
+ inputKey?: string | undefined;
23
24
  outputKey?: string | undefined;
24
25
  toolChoice?: AIAgentToolChoice | undefined;
25
26
  } | {
@@ -21,6 +21,10 @@ const agentFileSchema = zod_1.z.discriminatedUnion("type", [
21
21
  .string()
22
22
  .nullish()
23
23
  .transform((v) => v ?? undefined),
24
+ input_key: zod_1.z
25
+ .string()
26
+ .nullish()
27
+ .transform((v) => v ?? undefined),
24
28
  input_schema: schema_js_1.inputOutputSchema
25
29
  .nullish()
26
30
  .transform((v) => (v ? (0, json_schema_to_zod_1.jsonSchemaToZod)(v) : undefined)),
@@ -3,33 +3,19 @@ import { Agent, type AgentInvokeOptions, type Message } from "../agents/agent.js
3
3
  import type { AIAgent } from "../agents/ai-agent.js";
4
4
  import type { ChatModel, ChatModelInput } from "../agents/chat-model.js";
5
5
  import { ChatMessagesTemplate } from "./template.js";
6
- export declare const MESSAGE_KEY = "$message";
7
- export declare function createMessage<V extends Message>(message: string, variables?: V): {
8
- [MESSAGE_KEY]: string;
9
- } & typeof variables;
10
- export declare function createMessage<I extends Message, V extends Message>(message: I, variables?: V): I & typeof variables;
11
- export declare function createMessage<I extends Message, V extends Message>(message: string | I, variables?: V): ({
12
- [MESSAGE_KEY]: string;
13
- } | I) & typeof variables;
14
- export declare function getMessage(input: Message): string | undefined;
15
6
  export interface PromptBuilderOptions {
16
7
  instructions?: string | ChatMessagesTemplate;
17
8
  }
18
9
  export interface PromptBuildOptions extends Pick<AgentInvokeOptions, "context"> {
19
- agent?: AIAgent;
10
+ agent?: AIAgent<any, any, any>;
20
11
  input?: Message;
21
12
  model?: ChatModel;
22
13
  outputSchema?: Agent["outputSchema"];
23
14
  }
24
15
  export declare class PromptBuilder {
25
- static from(instructions: string): PromptBuilder;
26
- static from(instructions: GetPromptResult): PromptBuilder;
27
- static from(instructions: {
28
- path: string;
29
- }): Promise<PromptBuilder>;
30
16
  static from(instructions: string | {
31
17
  path: string;
32
- } | GetPromptResult): PromptBuilder | Promise<PromptBuilder>;
18
+ } | GetPromptResult): PromptBuilder;
33
19
  private static fromFile;
34
20
  private static fromMCPPromptResult;
35
21
  constructor(options?: PromptBuilderOptions);
@@ -1,8 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PromptBuilder = exports.MESSAGE_KEY = void 0;
4
- exports.createMessage = createMessage;
5
- exports.getMessage = getMessage;
3
+ exports.PromptBuilder = void 0;
6
4
  const index_js_1 = require("@aigne/platform-helpers/nodejs/index.js");
7
5
  const yaml_1 = require("yaml");
8
6
  const zod_1 = require("zod");
@@ -11,20 +9,6 @@ const json_schema_js_1 = require("../utils/json-schema.js");
11
9
  const type_utils_js_1 = require("../utils/type-utils.js");
12
10
  const memory_message_template_js_1 = require("./prompts/memory-message-template.js");
13
11
  const template_js_1 = require("./template.js");
14
- exports.MESSAGE_KEY = "$message";
15
- function createMessage(message, variables) {
16
- return (typeof message === "string"
17
- ? { [exports.MESSAGE_KEY]: message, ...variables }
18
- : { ...message, ...variables });
19
- }
20
- function getMessage(input) {
21
- const userInputMessage = input[exports.MESSAGE_KEY];
22
- if (typeof userInputMessage === "string")
23
- return userInputMessage;
24
- if (!(0, type_utils_js_1.isNil)(userInputMessage))
25
- return JSON.stringify(userInputMessage);
26
- return undefined;
27
- }
28
12
  class PromptBuilder {
29
13
  static from(instructions) {
30
14
  if (typeof instructions === "string")
@@ -35,8 +19,8 @@ class PromptBuilder {
35
19
  return PromptBuilder.fromFile(instructions.path);
36
20
  throw new Error(`Invalid instructions ${instructions}`);
37
21
  }
38
- static async fromFile(path) {
39
- const text = await index_js_1.nodejs.fs.readFile(path, "utf-8");
22
+ static fromFile(path) {
23
+ const text = index_js_1.nodejs.fsSync.readFileSync(path, "utf-8");
40
24
  return PromptBuilder.from(text);
41
25
  }
42
26
  static fromMCPPromptResult(result) {
@@ -80,22 +64,25 @@ class PromptBuilder {
80
64
  }
81
65
  async buildMessages(options) {
82
66
  const { input } = options;
67
+ const inputKey = options.agent?.inputKey;
68
+ const message = inputKey && typeof input?.[inputKey] === "string" ? input[inputKey] : undefined;
83
69
  const messages = (typeof this.instructions === "string"
84
70
  ? template_js_1.ChatMessagesTemplate.from([template_js_1.SystemMessageTemplate.from(this.instructions)])
85
71
  : this.instructions)?.format(options.input) ?? [];
86
72
  const memories = [];
87
- if (options.agent) {
88
- memories.push(...(await options.agent.retrieveMemories({ search: options.input }, options)));
73
+ if (options.agent?.inputKey) {
74
+ memories.push(...(await options.agent.retrieveMemories({ search: message }, options)));
89
75
  }
90
76
  if (options.context.memories?.length) {
91
77
  memories.push(...options.context.memories);
92
78
  }
93
79
  if (memories.length)
94
80
  messages.push(...this.convertMemoriesToMessages(memories, options));
95
- const content = input && getMessage(input);
96
- // add user input if it's not the same as the last message
97
- if (content && messages.at(-1)?.content !== content) {
98
- messages.push({ role: "user", content });
81
+ if (message) {
82
+ messages.push({
83
+ role: "user",
84
+ content: message,
85
+ });
99
86
  }
100
87
  return messages;
101
88
  }
@@ -1,5 +1,4 @@
1
1
  import { type AgentProcessAsyncGenerator, type AgentResponseChunk, type AgentResponseStream, type Message } from "../agents/agent.js";
2
- import type { MESSAGE_KEY } from "../prompt/prompt-builder.js";
3
2
  import { type PromiseOrValue } from "./type-utils.js";
4
3
  import "./stream-polyfill.js";
5
4
  export declare function objectToAgentResponseStream<T extends Message>(json: T): AgentResponseStream<T>;
@@ -20,7 +19,7 @@ export declare function readableStreamToArray<T>(stream: ReadableStream<T>, opti
20
19
  export declare function readableStreamToArray<T>(stream: ReadableStream<T>, options?: {
21
20
  catchError?: boolean;
22
21
  }): Promise<T[]>;
23
- export declare function stringToAgentResponseStream(str: string, key?: "text" | typeof MESSAGE_KEY | string): AgentResponseStream<Message>;
22
+ export declare function stringToAgentResponseStream(str: string, key?: "text" | string): AgentResponseStream<Message>;
24
23
  export declare function toReadableStream(stream: NodeJS.ReadStream): ReadableStream<Uint8Array<ArrayBufferLike>>;
25
24
  export declare function readAllString(stream: NodeJS.ReadStream | ReadableStream): Promise<string>;
26
25
  export declare function mergeReadableStreams<T1, T2>(s1: ReadableStream<T1>, s2: ReadableStream<T2>): ReadableStream<T1 | T2>;
@@ -16,6 +16,7 @@ export declare function duplicates<T>(arr: T[], key?: (item: T) => unknown): T[]
16
16
  export declare function remove<T>(arr: T[], remove: T[] | ((item: T) => boolean)): T[];
17
17
  export declare function unique<T>(arr: T[], key?: (item: T) => unknown): T[];
18
18
  export declare function omit<T extends Record<string, unknown>, K extends keyof T>(obj: T, ...keys: (K | K[])[]): Omit<T, K>;
19
+ export declare function omitDeep<T, K>(obj: T, ...keys: (K | K[])[]): unknown;
19
20
  export declare function omitBy<T extends Record<string, unknown>, K extends keyof T>(obj: T, predicate: (value: T[K], key: K) => boolean): Partial<T>;
20
21
  export declare function orArrayToArray<T>(value?: T | T[]): T[];
21
22
  export declare function createAccessorArray<T>(array: T[], accessor: (array: T[], name: string) => T | undefined): T[] & {
@@ -9,6 +9,7 @@ exports.duplicates = duplicates;
9
9
  exports.remove = remove;
10
10
  exports.unique = unique;
11
11
  exports.omit = omit;
12
+ exports.omitDeep = omitDeep;
12
13
  exports.omitBy = omitBy;
13
14
  exports.orArrayToArray = orArrayToArray;
14
15
  exports.createAccessorArray = createAccessorArray;
@@ -78,6 +79,18 @@ function omit(obj, ...keys) {
78
79
  const flattenedKeys = new Set(keys.flat());
79
80
  return Object.fromEntries(Object.entries(obj).filter(([key]) => !flattenedKeys.has(key)));
80
81
  }
82
+ function omitDeep(obj, ...keys) {
83
+ if (Array.isArray(obj)) {
84
+ return obj.map((item) => omitDeep(item, ...keys));
85
+ }
86
+ if (isRecord(obj)) {
87
+ const flattenedKeys = new Set(keys.flat());
88
+ return Object.fromEntries(Object.entries(obj)
89
+ .filter(([key]) => !flattenedKeys.has(key))
90
+ .map(([key, value]) => [key, omitDeep(value, ...keys)]));
91
+ }
92
+ return obj;
93
+ }
81
94
  function omitBy(obj, predicate) {
82
95
  return Object.fromEntries(Object.entries(obj).filter(([key, value]) => {
83
96
  const k = key;
@@ -2,6 +2,7 @@ import { nodejs } from "@aigne/platform-helpers/nodejs/index.js";
2
2
  import { ZodObject, type ZodType } from "zod";
3
3
  import type { AgentEvent, Context, UserContext } from "../aigne/context.js";
4
4
  import type { MessagePayload } from "../aigne/message-queue.js";
5
+ import type { ContextUsage } from "../aigne/usage.js";
5
6
  import type { Memory, MemoryAgent } from "../memory/memory.js";
6
7
  import type { MemoryRecorderInput } from "../memory/recorder.js";
7
8
  import type { MemoryRetrieverInput } from "../memory/retriever.js";
@@ -12,7 +13,11 @@ export * from "./types.js";
12
13
  /**
13
14
  * Basic message type that can contain any key-value pairs
14
15
  */
15
- export type Message = Record<string, unknown>;
16
+ export interface Message extends Record<string, unknown> {
17
+ $meta?: {
18
+ usage: ContextUsage;
19
+ };
20
+ }
16
21
  /**
17
22
  * Topics the agent subscribes to, can be a single topic string or an array of topic strings
18
23
  */
@@ -328,7 +333,7 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
328
333
  * Regular mode waits for the agent to complete processing and return the final result,
329
334
  * suitable for scenarios where a complete result is needed at once.
330
335
  *
331
- * @param input Input message to the agent, can be a string or structured object
336
+ * @param input Input message to the agent
332
337
  * @param options Invocation options, must set streaming to false or leave unset
333
338
  * @returns Final JSON response
334
339
  *
@@ -336,7 +341,7 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
336
341
  * Here's an example of invoking an agent with regular mode:
337
342
  * {@includeCode ../../test/agents/agent.test.ts#example-invoke}
338
343
  */
339
- invoke(input: I | string, options?: Partial<AgentInvokeOptions> & {
344
+ invoke(input: I, options?: Partial<AgentInvokeOptions> & {
340
345
  streaming?: false;
341
346
  }): Promise<O>;
342
347
  /**
@@ -346,7 +351,7 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
346
351
  * suitable for scenarios requiring real-time progress updates, such as
347
352
  * chat bot typing effects.
348
353
  *
349
- * @param input Input message to the agent, can be a string or structured object
354
+ * @param input Input message to the agent
350
355
  * @param options Invocation options, must set streaming to true for this overload
351
356
  * @returns Streaming response object
352
357
  *
@@ -354,7 +359,7 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
354
359
  * Here's an example of invoking an agent with streaming response:
355
360
  * {@includeCode ../../test/agents/agent.test.ts#example-invoke-streaming}
356
361
  */
357
- invoke(input: I | string, options: Partial<AgentInvokeOptions> & {
362
+ invoke(input: I, options: Partial<AgentInvokeOptions> & {
358
363
  streaming: true;
359
364
  }): Promise<AgentResponseStream<O>>;
360
365
  /**
@@ -366,7 +371,7 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
366
371
  * @param options Invocation options
367
372
  * @returns Agent response (streaming or regular)
368
373
  */
369
- invoke(input: I | string, options?: Partial<AgentInvokeOptions>): Promise<AgentResponse<O>>;
374
+ invoke(input: I, options?: Partial<AgentInvokeOptions>): Promise<AgentResponse<O>>;
370
375
  protected invokeSkill<I extends Message, O extends Message>(skill: Agent<I, O>, input: I, options: AgentInvokeOptions): Promise<O>;
371
376
  /**
372
377
  * Process agent output
@@ -12,7 +12,7 @@ import type { GuideRailAgentOutput } from "./guide-rail-agent.js";
12
12
  * @template I The input message type the agent accepts
13
13
  * @template O The output message type the agent returns
14
14
  */
15
- export interface AIAgentOptions<I extends Message = Message, O extends Message = Message> extends AgentOptions<I, O> {
15
+ export interface AIAgentOptions<InputKey extends string = string, I extends Message & InputMessage<InputKey> = Message & InputMessage<InputKey>, O extends Message = Message> extends AgentOptions<Omit<I, InputKey> & Partial<InputMessage<InputKey>>, O> {
16
16
  /**
17
17
  * The language model to use for this agent
18
18
  *
@@ -26,10 +26,14 @@ export interface AIAgentOptions<I extends Message = Message, O extends Message =
26
26
  * more complex prompt templates
27
27
  */
28
28
  instructions?: string | PromptBuilder;
29
+ /**
30
+ * Pick a message from input to use as the user's message
31
+ */
32
+ inputKey?: InputKey;
29
33
  /**
30
34
  * Custom key to use for text output in the response
31
35
  *
32
- * Defaults to $message if not specified
36
+ * Defaults to `message` if not specified
33
37
  */
34
38
  outputKey?: string;
35
39
  /**
@@ -106,6 +110,9 @@ export declare const aiAgentToolChoiceSchema: z.ZodUnion<[z.ZodNativeEnum<typeof
106
110
  export declare const aiAgentOptionsSchema: ZodObject<{
107
111
  [key in keyof AIAgentOptions]: ZodType<AIAgentOptions[key]>;
108
112
  }>;
113
+ type InputMessage<K> = K extends string ? {
114
+ [key in K]: string;
115
+ } : Message;
109
116
  /**
110
117
  * AI-powered agent that leverages language models
111
118
  *
@@ -126,7 +133,7 @@ export declare const aiAgentOptionsSchema: ZodObject<{
126
133
  * Basic AIAgent creation:
127
134
  * {@includeCode ../../test/agents/ai-agent.test.ts#example-ai-agent-basic}
128
135
  */
129
- export declare class AIAgent<I extends Message = Message, O extends Message = Message> extends Agent<I, O> {
136
+ export declare class AIAgent<InputKey extends string = string, I extends Message & InputMessage<InputKey> = Message & InputMessage<InputKey>, O extends Message = Message> extends Agent<I, O> {
130
137
  /**
131
138
  * Create an AIAgent with the specified options
132
139
  *
@@ -139,13 +146,13 @@ export declare class AIAgent<I extends Message = Message, O extends Message = Me
139
146
  * AI agent with custom instructions:
140
147
  * {@includeCode ../../test/agents/ai-agent.test.ts#example-ai-agent-instructions}
141
148
  */
142
- static from<I extends Message, O extends Message>(options: AIAgentOptions<I, O>): AIAgent<I, O>;
149
+ static from<InputKey extends string, I extends Message & InputMessage<InputKey>, O extends Message>(options: AIAgentOptions<InputKey, I, O>): AIAgent<InputKey, I, O>;
143
150
  /**
144
151
  * Create an AIAgent instance
145
152
  *
146
153
  * @param options Configuration options for the AI agent
147
154
  */
148
- constructor(options: AIAgentOptions<I, O>);
155
+ constructor(options: AIAgentOptions<InputKey, I, O>);
149
156
  /**
150
157
  * The language model used by this agent
151
158
  *
@@ -163,6 +170,10 @@ export declare class AIAgent<I extends Message = Message, O extends Message = Me
163
170
  * {@includeCode ../../test/agents/ai-agent.test.ts#example-ai-agent-prompt-builder}
164
171
  */
165
172
  instructions: PromptBuilder;
173
+ /**
174
+ * Pick a message from input to use as the user's message
175
+ */
176
+ inputKey?: InputKey;
166
177
  /**
167
178
  * Custom key to use for text output in the response
168
179
  *
@@ -170,7 +181,7 @@ export declare class AIAgent<I extends Message = Message, O extends Message = Me
170
181
  * Setting a custom output key:
171
182
  * {@includeCode ../../test/agents/ai-agent.test.ts#example-ai-agent-custom-output-key}
172
183
  */
173
- outputKey?: string;
184
+ outputKey: string;
174
185
  /**
175
186
  * Controls how the agent uses tools during execution
176
187
  *
@@ -224,3 +235,4 @@ export declare class AIAgent<I extends Message = Message, O extends Message = Me
224
235
  */
225
236
  _processRouter(input: I, model: ChatModel, modelInput: ChatModelInput, options: AgentInvokeOptions, toolsMap: Map<string, Agent>): AgentProcessAsyncGenerator<O>;
226
237
  }
238
+ export {};
@@ -59,4 +59,4 @@ export interface GuideRailAgentOutput extends Message {
59
59
  * - Monitor and audit agent behavior
60
60
  */
61
61
  export type GuideRailAgent = Agent<GuideRailAgentInput, GuideRailAgentOutput>;
62
- export declare const guideRailAgentOptions: AgentOptions<GuideRailAgentInput, GuideRailAgentOutput>;
62
+ export declare const guideRailAgentOptions: AgentOptions<any, GuideRailAgentOutput>;
@@ -192,7 +192,7 @@ export declare abstract class MCPBase<I extends Message, O extends Message> exte
192
192
  export declare class MCPTool extends MCPBase<Message, CallToolResult> {
193
193
  process(input: Message): Promise<CallToolResult>;
194
194
  }
195
- export interface MCPPromptInput extends Message {
195
+ export interface MCPPromptInput extends Record<string, unknown> {
196
196
  [key: string]: string;
197
197
  }
198
198
  export declare class MCPPrompt extends MCPBase<MCPPromptInput, GetPromptResult> {
@@ -129,12 +129,12 @@ export declare class AIGNE<U extends UserContext = UserContext> {
129
129
  * This overload is useful when you need to track which agent was ultimately responsible for generating the response.
130
130
  *
131
131
  * @param agent - Target agent to invoke
132
- * @param message - Input message to send to the agent (can be a string or a structured message object)
132
+ * @param message - Input message to send to the agent
133
133
  * @param options.returnActiveAgent - Must be true to return the final active agent
134
134
  * @param options.streaming - Must be false to return a response stream
135
135
  * @returns A promise resolving to a tuple containing the agent's response and the final active agent
136
136
  */
137
- invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I | string, options: InvokeOptions<U> & {
137
+ invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I, options: InvokeOptions<U> & {
138
138
  returnActiveAgent: true;
139
139
  streaming?: false;
140
140
  }): Promise<[O, Agent]>;
@@ -143,12 +143,12 @@ export declare class AIGNE<U extends UserContext = UserContext> {
143
143
  * This overload is useful when you need streaming responses while also tracking which agent provided them.
144
144
  *
145
145
  * @param agent - Target agent to invoke
146
- * @param message - Input message to send to the agent (can be a string or a structured message object)
146
+ * @param message - Input message to send to the agent
147
147
  * @param options.returnActiveAgent - Must be true to return the final active agent
148
148
  * @param options.streaming - Must be true to return a response stream
149
149
  * @returns A promise resolving to a tuple containing the agent's response stream and a promise for the final agent
150
150
  */
151
- invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I | string, options: InvokeOptions<U> & {
151
+ invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I, options: InvokeOptions<U> & {
152
152
  returnActiveAgent: true;
153
153
  streaming: true;
154
154
  }): Promise<[AgentResponseStream<O>, Promise<Agent>]>;
@@ -157,7 +157,7 @@ export declare class AIGNE<U extends UserContext = UserContext> {
157
157
  * This is the standard way to invoke an agent when you only need the response.
158
158
  *
159
159
  * @param agent - Target agent to invoke
160
- * @param message - Input message to send to the agent (can be a string or a structured message object)
160
+ * @param message - Input message to send to the agent
161
161
  * @param options - Optional configuration parameters for the invocation
162
162
  * @returns A promise resolving to the agent's complete response
163
163
  *
@@ -165,7 +165,7 @@ export declare class AIGNE<U extends UserContext = UserContext> {
165
165
  * Here's a simple example of how to invoke an agent:
166
166
  * {@includeCode ../../test/aigne/aigne.test.ts#example-simple}
167
167
  */
168
- invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I | string, options?: InvokeOptions<U> & {
168
+ invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I, options?: InvokeOptions<U> & {
169
169
  returnActiveAgent?: false;
170
170
  streaming?: false;
171
171
  }): Promise<O>;
@@ -174,7 +174,7 @@ export declare class AIGNE<U extends UserContext = UserContext> {
174
174
  * This allows processing the response incrementally as it's being generated.
175
175
  *
176
176
  * @param agent - Target agent to invoke
177
- * @param message - Input message to send to the agent (can be a string or a structured message object)
177
+ * @param message - Input message to send to the agent
178
178
  * @param options - Configuration with streaming enabled to receive incremental response chunks
179
179
  * @returns A promise resolving to a stream of the agent's response that can be consumed incrementally
180
180
  *
@@ -182,7 +182,7 @@ export declare class AIGNE<U extends UserContext = UserContext> {
182
182
  * Here's an example of how to invoke an agent with streaming response:
183
183
  * {@includeCode ../../test/aigne/aigne.test.ts#example-streaming}
184
184
  */
185
- invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I | string, options: InvokeOptions<U> & {
185
+ invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I, options: InvokeOptions<U> & {
186
186
  returnActiveAgent?: false;
187
187
  streaming: true;
188
188
  }): Promise<AgentResponseStream<O>>;
@@ -196,7 +196,7 @@ export declare class AIGNE<U extends UserContext = UserContext> {
196
196
  * @returns Either a UserAgent (when no message provided) or a promise resolving to the agent's response
197
197
  * with optional active agent information based on the provided options
198
198
  */
199
- invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message?: I | string, options?: InvokeOptions<U>): UserAgent<I, O> | Promise<AgentResponse<O> | [AgentResponse<O>, Agent]>;
199
+ invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message?: I, options?: InvokeOptions<U>): UserAgent<I, O> | Promise<AgentResponse<O> | [AgentResponse<O>, Agent]>;
200
200
  /**
201
201
  * Publishes a message to the message queue for inter-agent communication.
202
202
  * This method broadcasts a message to all subscribers of the specified topic(s).
@@ -210,7 +210,7 @@ export declare class AIGNE<U extends UserContext = UserContext> {
210
210
  * Here's an example of how to publish a message:
211
211
  * {@includeCode ../../test/aigne/aigne.test.ts#example-publish-message}
212
212
  */
213
- publish(topic: string | string[], payload: Omit<MessagePayload, "context"> | Message | string, options?: InvokeOptions<U>): void;
213
+ publish(topic: string | string[], payload: Omit<MessagePayload, "context"> | Message, options?: InvokeOptions<U>): void;
214
214
  /**
215
215
  * Subscribes to receive the next message on a specific topic.
216
216
  * This overload returns a Promise that resolves with the next message published to the topic.
@@ -42,6 +42,7 @@ export type ContextEmitEventMap = {
42
42
  export interface InvokeOptions<U extends UserContext = UserContext> extends Partial<Omit<AgentInvokeOptions<U>, "context">> {
43
43
  returnActiveAgent?: boolean;
44
44
  returnProgressChunks?: boolean;
45
+ returnMetadata?: boolean;
45
46
  disableTransfer?: boolean;
46
47
  sourceAgent?: Agent;
47
48
  }
@@ -77,11 +78,11 @@ export interface Context<U extends UserContext = UserContext> extends TypedEvent
77
78
  * @param options.streaming return a stream of the output
78
79
  * @returns the output of the agent and the final active agent
79
80
  */
80
- invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I | string, options: InvokeOptions & {
81
+ invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I, options: InvokeOptions & {
81
82
  returnActiveAgent: true;
82
83
  streaming?: false;
83
84
  }): Promise<[O, Agent]>;
84
- invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I | string, options: InvokeOptions & {
85
+ invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I, options: InvokeOptions & {
85
86
  returnActiveAgent: true;
86
87
  streaming: true;
87
88
  }): Promise<[AgentResponseStream<O>, Promise<Agent>]>;
@@ -91,19 +92,19 @@ export interface Context<U extends UserContext = UserContext> extends TypedEvent
91
92
  * @param message Message to pass to the agent
92
93
  * @returns the output of the agent
93
94
  */
94
- invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I | string, options?: InvokeOptions & {
95
+ invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I, options?: InvokeOptions & {
95
96
  streaming?: false;
96
97
  }): Promise<O>;
97
- invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I | string, options: InvokeOptions & {
98
+ invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I, options: InvokeOptions & {
98
99
  streaming: true;
99
100
  }): Promise<AgentResponseStream<O>>;
100
- invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message?: I | string, options?: InvokeOptions): UserAgent<I, O> | Promise<AgentResponse<O> | [AgentResponse<O>, Agent]>;
101
+ invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message?: I, options?: InvokeOptions): UserAgent<I, O> | Promise<AgentResponse<O> | [AgentResponse<O>, Agent]>;
101
102
  /**
102
103
  * Publish a message to a topic, the aigne will invoke the listeners of the topic
103
104
  * @param topic topic name, or an array of topic names
104
105
  * @param payload message to publish
105
106
  */
106
- publish(topic: string | string[], payload: Omit<MessagePayload, "context"> | Message | string, options?: InvokeOptions): void;
107
+ publish(topic: string | string[], payload: Omit<MessagePayload, "context"> | Message, options?: InvokeOptions): void;
107
108
  subscribe(topic: string | string[], listener?: undefined): Promise<MessagePayload>;
108
109
  subscribe(topic: string | string[], listener: MessageQueueListener): Unsubscribe;
109
110
  subscribe(topic: string | string[], listener?: MessageQueueListener): Unsubscribe | Promise<MessagePayload>;
@@ -142,6 +143,7 @@ export declare class AIGNEContext implements Context {
142
143
  reset?: boolean;
143
144
  }): AIGNEContext;
144
145
  invoke: Context["invoke"];
146
+ private onInvocationResult;
145
147
  publish: Context["publish"];
146
148
  subscribe: Context["subscribe"];
147
149
  unsubscribe: Context["unsubscribe"];
@@ -21,7 +21,7 @@ export interface MessagePayload {
21
21
  /**
22
22
  * @hidden
23
23
  */
24
- export declare function toMessagePayload(payload: Omit<MessagePayload, "context"> | string | Message, options?: Partial<Pick<MessagePayload, "role" | "source">>): Omit<MessagePayload, "context">;
24
+ export declare function toMessagePayload(payload: Omit<MessagePayload, "context"> | Message, options?: Partial<Pick<MessagePayload, "role" | "source">>): Omit<MessagePayload, "context">;
25
25
  /**
26
26
  * @hidden
27
27
  */
@@ -5,6 +5,7 @@ export interface ContextUsage {
5
5
  inputTokens: number;
6
6
  outputTokens: number;
7
7
  agentCalls: number;
8
+ duration: number;
8
9
  }
9
10
  /**
10
11
  * @hidden
@@ -5,11 +5,11 @@ export declare function loadAgentFromYamlFile(path: string): Promise<{
5
5
  name: string;
6
6
  description?: string | undefined;
7
7
  skills?: string[] | undefined;
8
- instructions?: string | undefined;
9
8
  memory?: true | {
10
9
  provider: string;
11
10
  subscribeTopic?: string[] | undefined;
12
11
  } | undefined;
12
+ instructions?: string | undefined;
13
13
  inputSchema?: ZodObject<Record<string, ZodType<any, z.ZodTypeDef, any>>, z.UnknownKeysParam, z.ZodTypeAny, {
14
14
  [x: string]: any;
15
15
  }, {
@@ -20,6 +20,7 @@ export declare function loadAgentFromYamlFile(path: string): Promise<{
20
20
  }, {
21
21
  [x: string]: any;
22
22
  }> | undefined;
23
+ inputKey?: string | undefined;
23
24
  outputKey?: string | undefined;
24
25
  toolChoice?: AIAgentToolChoice | undefined;
25
26
  } | {
@@ -3,33 +3,19 @@ import { Agent, type AgentInvokeOptions, type Message } from "../agents/agent.js
3
3
  import type { AIAgent } from "../agents/ai-agent.js";
4
4
  import type { ChatModel, ChatModelInput } from "../agents/chat-model.js";
5
5
  import { ChatMessagesTemplate } from "./template.js";
6
- export declare const MESSAGE_KEY = "$message";
7
- export declare function createMessage<V extends Message>(message: string, variables?: V): {
8
- [MESSAGE_KEY]: string;
9
- } & typeof variables;
10
- export declare function createMessage<I extends Message, V extends Message>(message: I, variables?: V): I & typeof variables;
11
- export declare function createMessage<I extends Message, V extends Message>(message: string | I, variables?: V): ({
12
- [MESSAGE_KEY]: string;
13
- } | I) & typeof variables;
14
- export declare function getMessage(input: Message): string | undefined;
15
6
  export interface PromptBuilderOptions {
16
7
  instructions?: string | ChatMessagesTemplate;
17
8
  }
18
9
  export interface PromptBuildOptions extends Pick<AgentInvokeOptions, "context"> {
19
- agent?: AIAgent;
10
+ agent?: AIAgent<any, any, any>;
20
11
  input?: Message;
21
12
  model?: ChatModel;
22
13
  outputSchema?: Agent["outputSchema"];
23
14
  }
24
15
  export declare class PromptBuilder {
25
- static from(instructions: string): PromptBuilder;
26
- static from(instructions: GetPromptResult): PromptBuilder;
27
- static from(instructions: {
28
- path: string;
29
- }): Promise<PromptBuilder>;
30
16
  static from(instructions: string | {
31
17
  path: string;
32
- } | GetPromptResult): PromptBuilder | Promise<PromptBuilder>;
18
+ } | GetPromptResult): PromptBuilder;
33
19
  private static fromFile;
34
20
  private static fromMCPPromptResult;
35
21
  constructor(options?: PromptBuilderOptions);
@@ -1,5 +1,4 @@
1
1
  import { type AgentProcessAsyncGenerator, type AgentResponseChunk, type AgentResponseStream, type Message } from "../agents/agent.js";
2
- import type { MESSAGE_KEY } from "../prompt/prompt-builder.js";
3
2
  import { type PromiseOrValue } from "./type-utils.js";
4
3
  import "./stream-polyfill.js";
5
4
  export declare function objectToAgentResponseStream<T extends Message>(json: T): AgentResponseStream<T>;
@@ -20,7 +19,7 @@ export declare function readableStreamToArray<T>(stream: ReadableStream<T>, opti
20
19
  export declare function readableStreamToArray<T>(stream: ReadableStream<T>, options?: {
21
20
  catchError?: boolean;
22
21
  }): Promise<T[]>;
23
- export declare function stringToAgentResponseStream(str: string, key?: "text" | typeof MESSAGE_KEY | string): AgentResponseStream<Message>;
22
+ export declare function stringToAgentResponseStream(str: string, key?: "text" | string): AgentResponseStream<Message>;
24
23
  export declare function toReadableStream(stream: NodeJS.ReadStream): ReadableStream<Uint8Array<ArrayBufferLike>>;
25
24
  export declare function readAllString(stream: NodeJS.ReadStream | ReadableStream): Promise<string>;
26
25
  export declare function mergeReadableStreams<T1, T2>(s1: ReadableStream<T1>, s2: ReadableStream<T2>): ReadableStream<T1 | T2>;
@@ -16,6 +16,7 @@ export declare function duplicates<T>(arr: T[], key?: (item: T) => unknown): T[]
16
16
  export declare function remove<T>(arr: T[], remove: T[] | ((item: T) => boolean)): T[];
17
17
  export declare function unique<T>(arr: T[], key?: (item: T) => unknown): T[];
18
18
  export declare function omit<T extends Record<string, unknown>, K extends keyof T>(obj: T, ...keys: (K | K[])[]): Omit<T, K>;
19
+ export declare function omitDeep<T, K>(obj: T, ...keys: (K | K[])[]): unknown;
19
20
  export declare function omitBy<T extends Record<string, unknown>, K extends keyof T>(obj: T, predicate: (value: T[K], key: K) => boolean): Partial<T>;
20
21
  export declare function orArrayToArray<T>(value?: T | T[]): T[];
21
22
  export declare function createAccessorArray<T>(array: T[], accessor: (array: T[], name: string) => T | undefined): T[] & {