@aigne/core 1.16.0 → 1.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (96) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/lib/cjs/agents/agent.d.ts +42 -26
  3. package/lib/cjs/agents/agent.js +59 -55
  4. package/lib/cjs/agents/ai-agent.d.ts +4 -5
  5. package/lib/cjs/agents/ai-agent.js +11 -11
  6. package/lib/cjs/agents/chat-model.d.ts +7 -8
  7. package/lib/cjs/agents/chat-model.js +9 -9
  8. package/lib/cjs/agents/mcp-agent.d.ts +3 -4
  9. package/lib/cjs/agents/mcp-agent.js +2 -2
  10. package/lib/cjs/agents/team-agent.d.ts +7 -8
  11. package/lib/cjs/agents/team-agent.js +10 -10
  12. package/lib/cjs/agents/user-agent.d.ts +4 -4
  13. package/lib/cjs/agents/user-agent.js +10 -10
  14. package/lib/cjs/aigne/aigne.d.ts +10 -9
  15. package/lib/cjs/aigne/aigne.js +5 -4
  16. package/lib/cjs/aigne/context.d.ts +17 -7
  17. package/lib/cjs/aigne/context.js +20 -7
  18. package/lib/cjs/index.d.ts +2 -1
  19. package/lib/cjs/index.js +2 -1
  20. package/lib/cjs/loader/agent-yaml.d.ts +1 -1
  21. package/lib/cjs/memory/default-memory/default-memory-storage/index.d.ts +30 -0
  22. package/lib/cjs/memory/default-memory/default-memory-storage/index.js +69 -0
  23. package/lib/cjs/memory/default-memory/default-memory-storage/migrate.d.ts +7 -0
  24. package/lib/cjs/memory/default-memory/default-memory-storage/migrate.js +53 -0
  25. package/lib/cjs/memory/default-memory/default-memory-storage/migrations/20250523165801-init.d.ts +7 -0
  26. package/lib/cjs/memory/default-memory/default-memory-storage/migrations/20250523165801-init.js +33 -0
  27. package/lib/cjs/memory/default-memory/default-memory-storage/models/memory.d.ts +10 -0
  28. package/lib/cjs/memory/default-memory/default-memory-storage/models/memory.js +32 -0
  29. package/lib/cjs/memory/default-memory/index.d.ts +10 -0
  30. package/lib/cjs/memory/{default-memory.js → default-memory/index.js} +16 -25
  31. package/lib/cjs/memory/default-memory/storage.d.ts +13 -0
  32. package/lib/cjs/memory/default-memory/storage.js +6 -0
  33. package/lib/cjs/memory/memory.d.ts +3 -2
  34. package/lib/cjs/memory/memory.js +1 -1
  35. package/lib/cjs/prompt/prompt-builder.d.ts +7 -1
  36. package/lib/cjs/prompt/prompt-builder.js +4 -4
  37. package/lib/cjs/utils/type-utils.d.ts +2 -2
  38. package/lib/dts/agents/agent.d.ts +42 -26
  39. package/lib/dts/agents/ai-agent.d.ts +4 -5
  40. package/lib/dts/agents/chat-model.d.ts +7 -8
  41. package/lib/dts/agents/mcp-agent.d.ts +3 -4
  42. package/lib/dts/agents/team-agent.d.ts +7 -8
  43. package/lib/dts/agents/user-agent.d.ts +4 -4
  44. package/lib/dts/aigne/aigne.d.ts +10 -9
  45. package/lib/dts/aigne/context.d.ts +17 -7
  46. package/lib/dts/index.d.ts +2 -1
  47. package/lib/dts/loader/agent-yaml.d.ts +1 -1
  48. package/lib/dts/memory/default-memory/default-memory-storage/index.d.ts +30 -0
  49. package/lib/dts/memory/default-memory/default-memory-storage/migrate.d.ts +7 -0
  50. package/lib/dts/memory/default-memory/default-memory-storage/migrations/20250523165801-init.d.ts +7 -0
  51. package/lib/dts/memory/default-memory/default-memory-storage/models/memory.d.ts +10 -0
  52. package/lib/dts/memory/default-memory/index.d.ts +10 -0
  53. package/lib/dts/memory/default-memory/storage.d.ts +13 -0
  54. package/lib/dts/memory/memory.d.ts +3 -2
  55. package/lib/dts/prompt/prompt-builder.d.ts +7 -1
  56. package/lib/dts/utils/type-utils.d.ts +2 -2
  57. package/lib/esm/agents/agent.d.ts +42 -26
  58. package/lib/esm/agents/agent.js +59 -55
  59. package/lib/esm/agents/ai-agent.d.ts +4 -5
  60. package/lib/esm/agents/ai-agent.js +10 -10
  61. package/lib/esm/agents/chat-model.d.ts +7 -8
  62. package/lib/esm/agents/chat-model.js +9 -9
  63. package/lib/esm/agents/mcp-agent.d.ts +3 -4
  64. package/lib/esm/agents/mcp-agent.js +2 -2
  65. package/lib/esm/agents/team-agent.d.ts +7 -8
  66. package/lib/esm/agents/team-agent.js +10 -10
  67. package/lib/esm/agents/user-agent.d.ts +4 -4
  68. package/lib/esm/agents/user-agent.js +10 -10
  69. package/lib/esm/aigne/aigne.d.ts +10 -9
  70. package/lib/esm/aigne/aigne.js +5 -4
  71. package/lib/esm/aigne/context.d.ts +17 -7
  72. package/lib/esm/aigne/context.js +21 -8
  73. package/lib/esm/index.d.ts +2 -1
  74. package/lib/esm/index.js +2 -1
  75. package/lib/esm/loader/agent-yaml.d.ts +1 -1
  76. package/lib/esm/memory/default-memory/default-memory-storage/index.d.ts +30 -0
  77. package/lib/esm/memory/default-memory/default-memory-storage/index.js +64 -0
  78. package/lib/esm/memory/default-memory/default-memory-storage/migrate.d.ts +7 -0
  79. package/lib/esm/memory/default-memory/default-memory-storage/migrate.js +16 -0
  80. package/lib/esm/memory/default-memory/default-memory-storage/migrations/20250523165801-init.d.ts +7 -0
  81. package/lib/esm/memory/default-memory/default-memory-storage/migrations/20250523165801-init.js +28 -0
  82. package/lib/esm/memory/default-memory/default-memory-storage/models/memory.d.ts +10 -0
  83. package/lib/esm/memory/default-memory/default-memory-storage/models/memory.js +29 -0
  84. package/lib/esm/memory/default-memory/index.d.ts +10 -0
  85. package/lib/esm/memory/{default-memory.js → default-memory/index.js} +15 -24
  86. package/lib/esm/memory/default-memory/storage.d.ts +13 -0
  87. package/lib/esm/memory/default-memory/storage.js +2 -0
  88. package/lib/esm/memory/memory.d.ts +3 -2
  89. package/lib/esm/memory/memory.js +2 -2
  90. package/lib/esm/prompt/prompt-builder.d.ts +7 -1
  91. package/lib/esm/prompt/prompt-builder.js +4 -4
  92. package/lib/esm/utils/type-utils.d.ts +2 -2
  93. package/package.json +4 -1
  94. package/lib/cjs/memory/default-memory.d.ts +0 -16
  95. package/lib/dts/memory/default-memory.d.ts +0 -16
  96. package/lib/esm/memory/default-memory.d.ts +0 -16
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DefaultMemoryStorage = void 0;
4
+ exports.initSequelize = initSequelize;
5
+ require("sqlite3");
6
+ const sequelize_1 = require("sequelize");
7
+ const logger_js_1 = require("../../../utils/logger.js");
8
+ const storage_js_1 = require("../storage.js");
9
+ const migrate_js_1 = require("./migrate.js");
10
+ const memory_js_1 = require("./models/memory.js");
11
+ const DEFAULT_MAX_MEMORY_COUNT = 10;
12
+ class DefaultMemoryStorage extends storage_js_1.MemoryStorage {
13
+ options;
14
+ memories = {};
15
+ constructor(options) {
16
+ super();
17
+ this.options = options;
18
+ }
19
+ _models;
20
+ get models() {
21
+ this._models ??= (async () => {
22
+ const sequelize = initSequelize(this.options?.path);
23
+ await (0, migrate_js_1.migrate)(sequelize);
24
+ return {
25
+ Memory: (0, memory_js_1.initMemoryModel)(sequelize),
26
+ };
27
+ })();
28
+ return this._models;
29
+ }
30
+ convertMemory(m) {
31
+ return {
32
+ id: m.id,
33
+ sessionId: m.sessionId,
34
+ content: m.content,
35
+ createdAt: m.createdAt.toISOString(),
36
+ };
37
+ }
38
+ async search(query, { context }) {
39
+ const { limit = DEFAULT_MAX_MEMORY_COUNT } = query;
40
+ const sessionId = (await this.options?.getSessionId?.(context)) ?? null;
41
+ const { Memory } = await this.models;
42
+ const memories = await Memory.findAll({
43
+ where: { sessionId },
44
+ order: [["id", "DESC"]],
45
+ limit,
46
+ });
47
+ return {
48
+ result: memories.reverse().map(this.convertMemory),
49
+ };
50
+ }
51
+ async create(memory, { context }) {
52
+ const sessionId = (await this.options?.getSessionId?.(context)) ?? null;
53
+ const { Memory } = await this.models;
54
+ const m = await Memory.create({
55
+ ...memory,
56
+ sessionId,
57
+ });
58
+ return { result: this.convertMemory(m) };
59
+ }
60
+ }
61
+ exports.DefaultMemoryStorage = DefaultMemoryStorage;
62
+ function initSequelize(path) {
63
+ const sequelize = new sequelize_1.Sequelize({
64
+ logging: (sql) => logger_js_1.logger.debug(sql),
65
+ storage: path,
66
+ dialect: "sqlite",
67
+ });
68
+ return sequelize;
69
+ }
@@ -0,0 +1,7 @@
1
+ import type { Sequelize } from "sequelize";
2
+ import { type MigrateDownOptions, type MigrateUpOptions } from "umzug";
3
+ export declare const migrate: (sequelize: Sequelize, { type, upOptions, downOptions, }?: {
4
+ type?: "up" | "down";
5
+ upOptions?: MigrateUpOptions;
6
+ downOptions?: MigrateDownOptions;
7
+ }) => Promise<void>;
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.migrate = void 0;
37
+ const umzug_1 = require("umzug");
38
+ const init = __importStar(require("./migrations/20250523165801-init.js"));
39
+ const migrate = async (sequelize, { type = "up", upOptions, downOptions, } = {}) => {
40
+ const umzug = new umzug_1.Umzug({
41
+ migrations: [{ ...init, name: "20241224202701-init" }],
42
+ context: sequelize.getQueryInterface(),
43
+ storage: new umzug_1.SequelizeStorage({ sequelize }),
44
+ logger: console,
45
+ });
46
+ if (type === "down") {
47
+ await umzug.down(downOptions);
48
+ }
49
+ else {
50
+ await umzug.up(upOptions);
51
+ }
52
+ };
53
+ exports.migrate = migrate;
@@ -0,0 +1,7 @@
1
+ import { type QueryInterface } from "sequelize";
2
+ export declare const up: ({ context: queryInterface }: {
3
+ context: QueryInterface;
4
+ }) => Promise<void>;
5
+ export declare const down: ({ context: queryInterface }: {
6
+ context: QueryInterface;
7
+ }) => Promise<void>;
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.down = exports.up = void 0;
4
+ const sequelize_1 = require("sequelize");
5
+ const up = async ({ context: queryInterface }) => {
6
+ await queryInterface.createTable("Memories", {
7
+ id: {
8
+ type: sequelize_1.DataTypes.STRING,
9
+ primaryKey: true,
10
+ allowNull: false,
11
+ },
12
+ createdAt: {
13
+ type: sequelize_1.DataTypes.DATE,
14
+ allowNull: false,
15
+ },
16
+ updatedAt: {
17
+ type: sequelize_1.DataTypes.DATE,
18
+ allowNull: false,
19
+ },
20
+ sessionId: {
21
+ type: sequelize_1.DataTypes.STRING,
22
+ },
23
+ content: {
24
+ type: sequelize_1.DataTypes.JSON,
25
+ allowNull: false,
26
+ },
27
+ });
28
+ };
29
+ exports.up = up;
30
+ const down = async ({ context: queryInterface }) => {
31
+ await queryInterface.dropTable("Memories");
32
+ };
33
+ exports.down = down;
@@ -0,0 +1,10 @@
1
+ import { type CreationOptional, type InferAttributes, type InferCreationAttributes, Model } from "sequelize";
2
+ import type { ModelStatic, Sequelize } from "sequelize";
3
+ export interface Memory extends Model<InferAttributes<Memory>, InferCreationAttributes<Memory>> {
4
+ id: CreationOptional<string>;
5
+ createdAt: CreationOptional<Date>;
6
+ updatedAt: CreationOptional<Date>;
7
+ sessionId?: string | null;
8
+ content: unknown;
9
+ }
10
+ export declare function initMemoryModel(sequelize: Sequelize): ModelStatic<Memory>;
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.initMemoryModel = initMemoryModel;
4
+ const sequelize_1 = require("sequelize");
5
+ const uuid_1 = require("uuid");
6
+ const nextId = () => (0, uuid_1.v7)();
7
+ function initMemoryModel(sequelize) {
8
+ return class Memory extends sequelize_1.Model {
9
+ }.init({
10
+ id: {
11
+ type: sequelize_1.DataTypes.STRING,
12
+ primaryKey: true,
13
+ allowNull: false,
14
+ defaultValue: nextId,
15
+ },
16
+ createdAt: {
17
+ type: sequelize_1.DataTypes.DATE,
18
+ allowNull: false,
19
+ },
20
+ updatedAt: {
21
+ type: sequelize_1.DataTypes.DATE,
22
+ allowNull: false,
23
+ },
24
+ sessionId: {
25
+ type: sequelize_1.DataTypes.STRING,
26
+ },
27
+ content: {
28
+ type: sequelize_1.DataTypes.JSON,
29
+ allowNull: false,
30
+ },
31
+ }, { sequelize });
32
+ }
@@ -0,0 +1,10 @@
1
+ import { MemoryAgent, type MemoryAgentOptions } from "../memory.js";
2
+ import { type DefaultMemoryStorageOptions } from "./default-memory-storage/index.js";
3
+ import { MemoryStorage } from "./storage.js";
4
+ export interface DefaultMemoryOptions extends Partial<MemoryAgentOptions> {
5
+ storage?: MemoryStorage | DefaultMemoryStorageOptions;
6
+ }
7
+ export declare class DefaultMemory extends MemoryAgent {
8
+ constructor(options?: DefaultMemoryOptions);
9
+ storage: MemoryStorage;
10
+ }
@@ -3,38 +3,29 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.DefaultMemory = exports.DEFAULT_MAX_MEMORY_COUNT = void 0;
6
+ exports.DefaultMemory = void 0;
7
7
  const fast_deep_equal_1 = __importDefault(require("fast-deep-equal"));
8
- const memory_js_1 = require("./memory.js");
9
- const recorder_js_1 = require("./recorder.js");
10
- const retriever_js_1 = require("./retriever.js");
11
- exports.DEFAULT_MAX_MEMORY_COUNT = 10;
8
+ const memory_js_1 = require("../memory.js");
9
+ const recorder_js_1 = require("../recorder.js");
10
+ const retriever_js_1 = require("../retriever.js");
11
+ const index_js_1 = require("./default-memory-storage/index.js");
12
+ const storage_js_1 = require("./storage.js");
12
13
  class DefaultMemory extends memory_js_1.MemoryAgent {
13
14
  constructor(options = {}) {
14
15
  super({
15
16
  ...options,
16
17
  autoUpdate: options.autoUpdate ?? true,
17
18
  });
19
+ this.storage =
20
+ options.storage instanceof storage_js_1.MemoryStorage
21
+ ? options.storage
22
+ : new index_js_1.DefaultMemoryStorage(options.storage);
18
23
  if (!this.recorder)
19
24
  this.recorder = new DefaultMemoryRecorder(this);
20
25
  if (!this.retriever)
21
26
  this.retriever = new DefaultMemoryRetriever(this);
22
27
  }
23
- storage = [];
24
- async search(options = {}) {
25
- const { limit = exports.DEFAULT_MAX_MEMORY_COUNT } = options;
26
- const result = limit < 0 ? this.storage.slice(limit) : this.storage.slice(0, limit);
27
- return { result };
28
- }
29
- async create(memory) {
30
- const m = {
31
- ...memory,
32
- id: (0, memory_js_1.newMemoryId)(),
33
- createdAt: new Date().toISOString(),
34
- };
35
- this.storage.push(m);
36
- return { result: m };
37
- }
28
+ storage;
38
29
  }
39
30
  exports.DefaultMemory = DefaultMemory;
40
31
  class DefaultMemoryRetriever extends retriever_js_1.MemoryRetriever {
@@ -43,8 +34,8 @@ class DefaultMemoryRetriever extends retriever_js_1.MemoryRetriever {
43
34
  super({});
44
35
  this.memory = memory;
45
36
  }
46
- async process(input) {
47
- const { result } = await this.memory.search(input);
37
+ async process(input, options) {
38
+ const { result } = await this.memory.storage.search(input, options);
48
39
  return { memories: result };
49
40
  }
50
41
  }
@@ -54,12 +45,12 @@ class DefaultMemoryRecorder extends recorder_js_1.MemoryRecorder {
54
45
  super({});
55
46
  this.memory = memory;
56
47
  }
57
- async process(input) {
48
+ async process(input, options) {
58
49
  const newMemories = [];
59
50
  for (const content of input.content) {
60
- const { result: [last], } = await this.memory.search({ limit: -1 });
51
+ const { result: [last], } = await this.memory.storage.search({ limit: 1 }, options);
61
52
  if (!(0, fast_deep_equal_1.default)(last?.content, content)) {
62
- const { result } = await this.memory.create({ content });
53
+ const { result } = await this.memory.storage.create({ content }, options);
63
54
  newMemories.push(result);
64
55
  }
65
56
  }
@@ -0,0 +1,13 @@
1
+ import type { AgentInvokeOptions } from "../../agents/agent.js";
2
+ import type { Memory } from "../memory.js";
3
+ export declare abstract class MemoryStorage {
4
+ abstract create(memory: Pick<Memory, "content">, options: AgentInvokeOptions): Promise<{
5
+ result: Memory;
6
+ }>;
7
+ abstract search(query: {
8
+ search?: string;
9
+ limit?: number;
10
+ }, options: AgentInvokeOptions): Promise<{
11
+ result: Memory[];
12
+ }>;
13
+ }
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MemoryStorage = void 0;
4
+ class MemoryStorage {
5
+ }
6
+ exports.MemoryStorage = MemoryStorage;
@@ -1,10 +1,11 @@
1
- import { Agent, type AgentOptions, type Message } from "../agents/agent.js";
1
+ import { Agent, type AgentInvokeOptions, type AgentOptions, type Message } from "../agents/agent.js";
2
2
  import type { Context } from "../aigne/context.js";
3
3
  import type { MessagePayload } from "../aigne/message-queue.js";
4
4
  import type { MemoryRecorder, MemoryRecorderInput, MemoryRecorderOutput } from "./recorder.js";
5
5
  import type { MemoryRetriever, MemoryRetrieverInput, MemoryRetrieverOutput } from "./retriever.js";
6
6
  export interface Memory {
7
7
  id: string;
8
+ sessionId?: string | null;
8
9
  content: unknown;
9
10
  createdAt: string;
10
11
  }
@@ -62,7 +63,7 @@ export declare class MemoryAgent extends Agent {
62
63
  * MemoryAgent doesn't directly process messages like other agents, so this method
63
64
  * throws an error when called. Use the specialized retrieve() and record() methods instead.
64
65
  */
65
- process(_input: Message, _context: Context): Promise<Message>;
66
+ process(_input: Message, _options: AgentInvokeOptions): Promise<Message>;
66
67
  /**
67
68
  * Retrieves memories based on the provided input criteria.
68
69
  *
@@ -85,7 +85,7 @@ class MemoryAgent extends agent_js_1.Agent {
85
85
  * MemoryAgent doesn't directly process messages like other agents, so this method
86
86
  * throws an error when called. Use the specialized retrieve() and record() methods instead.
87
87
  */
88
- async process(_input, _context) {
88
+ async process(_input, _options) {
89
89
  throw new Error("Method not implemented.");
90
90
  }
91
91
  /**
@@ -6,7 +6,13 @@ import type { Context } from "../aigne/context.js";
6
6
  import type { MemoryAgent } from "../memory/memory.js";
7
7
  import { ChatMessagesTemplate } from "./template.js";
8
8
  export declare const MESSAGE_KEY = "$message";
9
- export declare function createMessage<I extends Message>(message: string | I): I;
9
+ export declare function createMessage<V extends Message>(message: string, variables?: V): {
10
+ [MESSAGE_KEY]: string;
11
+ } & typeof variables;
12
+ export declare function createMessage<I extends Message, V extends Message>(message: I, variables?: V): I & typeof variables;
13
+ export declare function createMessage<I extends Message, V extends Message>(message: string | I, variables?: V): ({
14
+ [MESSAGE_KEY]: string;
15
+ } | I) & typeof variables;
10
16
  export declare function getMessage(input: Message): string | undefined;
11
17
  export interface PromptBuilderOptions {
12
18
  instructions?: string | ChatMessagesTemplate;
@@ -12,10 +12,10 @@ const type_utils_js_1 = require("../utils/type-utils.js");
12
12
  const memory_message_template_js_1 = require("./prompts/memory-message-template.js");
13
13
  const template_js_1 = require("./template.js");
14
14
  exports.MESSAGE_KEY = "$message";
15
- function createMessage(message) {
16
- return typeof message === "string"
17
- ? { [exports.MESSAGE_KEY]: message }
18
- : { ...message };
15
+ function createMessage(message, variables) {
16
+ return (typeof message === "string"
17
+ ? { [exports.MESSAGE_KEY]: message, ...variables }
18
+ : { ...message, ...variables });
19
19
  }
20
20
  function getMessage(input) {
21
21
  const userInputMessage = input[exports.MESSAGE_KEY];
@@ -1,4 +1,4 @@
1
- import { type ZodType } from "zod";
1
+ import { type ZodType, z } from "zod";
2
2
  export type PromiseOrValue<T> = T | Promise<T>;
3
3
  export type Nullish<T> = T | null | undefined | void;
4
4
  export type OmitPropertiesFromArrayFirstElement<T extends unknown[], K extends string | number | symbol> = T extends [infer U, ...infer Rest] ? [Omit<U, K>, ...Rest] : never;
@@ -20,6 +20,6 @@ export declare function orArrayToArray<T>(value?: T | T[]): T[];
20
20
  export declare function createAccessorArray<T>(array: T[], accessor: (array: T[], name: string) => T | undefined): T[] & {
21
21
  [key: string]: T;
22
22
  };
23
- export declare function checkArguments<T>(prefix: string, schema: ZodType<T>, args: T | unknown): T;
23
+ export declare function checkArguments<T extends ZodType>(prefix: string, schema: T, args: unknown): z.infer<T>;
24
24
  export declare function tryOrThrow<P extends PromiseOrValue<unknown>>(fn: () => P, error: string | Error | ((error: Error) => Error)): P;
25
25
  export declare function tryOrThrow<P extends PromiseOrValue<unknown>>(fn: () => P, error?: Nullish<string | Error | ((error: Error) => Nullish<Error>)>): P | undefined;
@@ -1,6 +1,6 @@
1
1
  import { inspect } from "node:util";
2
2
  import { ZodObject, type ZodType } from "zod";
3
- import type { Context } from "../aigne/context.js";
3
+ import type { Context, UserContext } from "../aigne/context.js";
4
4
  import type { MessagePayload } from "../aigne/message-queue.js";
5
5
  import type { MemoryAgent } from "../memory/memory.js";
6
6
  import { type Nullish, type PromiseOrValue, type XOr } from "../utils/type-utils.js";
@@ -99,7 +99,21 @@ export interface AgentOptions<I extends Message = Message, O extends Message = M
99
99
  export declare const agentOptionsSchema: ZodObject<{
100
100
  [key in keyof AgentOptions]: ZodType<AgentOptions[key]>;
101
101
  }>;
102
- export interface AgentInvokeOptions {
102
+ export interface AgentInvokeOptions<U extends UserContext = UserContext> {
103
+ /**
104
+ * The execution context for the agent
105
+ *
106
+ * The context provides the runtime environment for agent execution, including:
107
+ * - Event emission and subscription management
108
+ * - Inter-agent communication and message passing
109
+ * - Resource usage tracking and limits enforcement
110
+ * - Timeout and status management
111
+ * - Memory and state management across agent invocations
112
+ *
113
+ * Each agent invocation requires a context to coordinate with the broader
114
+ * agent system and maintain proper isolation and resource control.
115
+ */
116
+ context: Context<U>;
103
117
  /**
104
118
  * Whether to enable streaming response
105
119
  *
@@ -278,7 +292,7 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
278
292
  /**
279
293
  * Check context status to ensure it hasn't timed out
280
294
  *
281
- * @param context The context to check
295
+ * @param options Invocation options containing context
282
296
  * @throws Error if the context has timed out
283
297
  */
284
298
  private checkContextStatus;
@@ -290,7 +304,6 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
290
304
  * suitable for scenarios where a complete result is needed at once.
291
305
  *
292
306
  * @param input Input message to the agent, can be a string or structured object
293
- * @param context Execution context, providing environment and resource access
294
307
  * @param options Invocation options, must set streaming to false or leave unset
295
308
  * @returns Final JSON response
296
309
  *
@@ -298,7 +311,7 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
298
311
  * Here's an example of invoking an agent with regular mode:
299
312
  * {@includeCode ../../test/agents/agent.test.ts#example-invoke}
300
313
  */
301
- invoke(input: I | string, context?: Context, options?: AgentInvokeOptions & {
314
+ invoke(input: I | string, options?: Partial<AgentInvokeOptions> & {
302
315
  streaming?: false;
303
316
  }): Promise<O>;
304
317
  /**
@@ -309,7 +322,6 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
309
322
  * chat bot typing effects.
310
323
  *
311
324
  * @param input Input message to the agent, can be a string or structured object
312
- * @param context Execution context, providing environment and resource access
313
325
  * @param options Invocation options, must set streaming to true for this overload
314
326
  * @returns Streaming response object
315
327
  *
@@ -317,7 +329,7 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
317
329
  * Here's an example of invoking an agent with streaming response:
318
330
  * {@includeCode ../../test/agents/agent.test.ts#example-invoke-streaming}
319
331
  */
320
- invoke(input: I | string, context: Context | undefined, options: {
332
+ invoke(input: I | string, options: Partial<AgentInvokeOptions> & {
321
333
  streaming: true;
322
334
  }): Promise<AgentResponseStream<O>>;
323
335
  /**
@@ -326,12 +338,11 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
326
338
  * Returns either streaming or regular response based on the streaming parameter in options
327
339
  *
328
340
  * @param input Input message to the agent
329
- * @param context Execution context
330
341
  * @param options Invocation options
331
342
  * @returns Agent response (streaming or regular)
332
343
  */
333
- invoke(input: I | string, context?: Context, options?: AgentInvokeOptions): Promise<AgentResponse<O>>;
334
- protected invokeSkill<I extends Message, O extends Message>(skill: Agent<I, O>, input: I, context: Context): Promise<O>;
344
+ invoke(input: I | string, options?: Partial<AgentInvokeOptions>): Promise<AgentResponse<O>>;
345
+ protected invokeSkill<I extends Message, O extends Message>(skill: Agent<I, O>, input: I, options: AgentInvokeOptions): Promise<O>;
335
346
  /**
336
347
  * Process agent output
337
348
  *
@@ -339,7 +350,7 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
339
350
  *
340
351
  * @param input Original input message
341
352
  * @param output Raw output produced by the agent
342
- * @param context Execution context
353
+ * @param options Invocation options
343
354
  * @returns Final processed output
344
355
  */
345
356
  private processAgentOutput;
@@ -349,7 +360,7 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
349
360
  * Logs error information, triggers failure events, and re-throws the error
350
361
  *
351
362
  * @param error Caught error
352
- * @param context Execution context
363
+ * @param options Invocation options
353
364
  */
354
365
  private processAgentError;
355
366
  /**
@@ -358,10 +369,10 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
358
369
  * If the context has a maximum invocation limit set, checks if the limit
359
370
  * has been exceeded and increments the invocation counter
360
371
  *
361
- * @param context Execution context
372
+ * @param options Invocation options containing context and limits
362
373
  * @throws Error if maximum invocation limit is exceeded
363
374
  */
364
- protected checkAgentInvokesUsage(context: Context): void;
375
+ protected checkAgentInvokesUsage(options: AgentInvokeOptions): void;
365
376
  /**
366
377
  * Pre-processing operations before handling input
367
378
  *
@@ -370,9 +381,9 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
370
381
  * - Verifying invocation limits
371
382
  *
372
383
  * @param _ Input message (unused)
373
- * @param context Execution context
384
+ * @param options Options for agent invocation
374
385
  */
375
- protected preprocess(_: I, context: Context): PromiseOrValue<void>;
386
+ protected preprocess(_: I, options: AgentInvokeOptions): Promise<void>;
376
387
  private checkResponseByGuideRails;
377
388
  private runGuideRails;
378
389
  /**
@@ -400,10 +411,10 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
400
411
  *
401
412
  * @param input Input message
402
413
  * @param output Output message
403
- * @param context Execution context
414
+ * @param options Options for agent invocation
404
415
  */
405
- protected postprocess(input: I, output: O, context: Context): PromiseOrValue<void>;
406
- protected publishToTopics(output: Message, context: Context): Promise<void>;
416
+ protected postprocess(input: I, output: O, options: AgentInvokeOptions): Promise<void>;
417
+ protected publishToTopics(output: Message, options: AgentInvokeOptions): Promise<void>;
407
418
  /**
408
419
  * Core processing method of the agent, must be implemented in subclasses
409
420
  *
@@ -415,7 +426,7 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
415
426
  * - Another agent instance (transfer agent)
416
427
  *
417
428
  * @param input Input message
418
- * @param context Execution context
429
+ * @param options Options for agent invocation
419
430
  * @returns Processing result
420
431
  *
421
432
  * @example
@@ -434,7 +445,7 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
434
445
  * Example of transfer to another agent:
435
446
  * {@includeCode ../../test/agents/agent.test.ts#example-process-transfer}
436
447
  */
437
- abstract process(input: I, context: Context): PromiseOrValue<AgentProcessResult<O>>;
448
+ abstract process(input: I, options: AgentInvokeOptions): PromiseOrValue<AgentProcessResult<O>>;
438
449
  /**
439
450
  * Shut down the agent and clean up resources
440
451
  *
@@ -484,6 +495,7 @@ export interface AgentHooks<I extends Message = Message, O extends Message = Mes
484
495
  * @param event Object containing the input message
485
496
  */
486
497
  onStart?: (event: {
498
+ context: Context;
487
499
  input: I;
488
500
  }) => PromiseOrValue<void>;
489
501
  /**
@@ -496,6 +508,7 @@ export interface AgentHooks<I extends Message = Message, O extends Message = Mes
496
508
  * @param event Object containing the input message and either output or error
497
509
  */
498
510
  onEnd?: (event: XOr<{
511
+ context: Context;
499
512
  input: I;
500
513
  output: O;
501
514
  error: Error;
@@ -509,6 +522,7 @@ export interface AgentHooks<I extends Message = Message, O extends Message = Mes
509
522
  * @param event Object containing the skill being used and input message
510
523
  */
511
524
  onSkillStart?: (event: {
525
+ context: Context;
512
526
  skill: Agent;
513
527
  input: I;
514
528
  }) => PromiseOrValue<void>;
@@ -522,6 +536,7 @@ export interface AgentHooks<I extends Message = Message, O extends Message = Mes
522
536
  * @param event Object containing the skill used, input message, and either output or error
523
537
  */
524
538
  onSkillEnd?: (event: XOr<{
539
+ context: Context;
525
540
  skill: Agent;
526
541
  input: I;
527
542
  output: O;
@@ -537,6 +552,7 @@ export interface AgentHooks<I extends Message = Message, O extends Message = Mes
537
552
  * @param event Object containing the source agent, target agent, and input message
538
553
  */
539
554
  onHandoff?: (event: {
555
+ context: Context;
540
556
  source: Agent;
541
557
  target: Agent;
542
558
  input: I;
@@ -587,7 +603,7 @@ export interface AgentResponseDelta<T> {
587
603
  }> | Partial<{
588
604
  [key: string]: string;
589
605
  }>;
590
- json?: Partial<T | TransferAgentOutput>;
606
+ json?: Partial<T> | TransferAgentOutput;
591
607
  };
592
608
  }
593
609
  /**
@@ -619,7 +635,7 @@ export declare function jsonDelta<T extends Message>(jsonDelta: NonNullable<Agen
619
635
  *
620
636
  * @template O Agent output message type
621
637
  */
622
- export type AgentProcessAsyncGenerator<O extends Message> = AsyncGenerator<AgentResponseChunk<O>, Partial<O | TransferAgentOutput> | undefined | void>;
638
+ export type AgentProcessAsyncGenerator<O extends Message> = AsyncGenerator<AgentResponseChunk<O>, Partial<O> | TransferAgentOutput | undefined | void>;
623
639
  /**
624
640
  * Result type for agent processing method, can be:
625
641
  * - Direct or streaming response
@@ -709,10 +725,10 @@ export declare class FunctionAgent<I extends Message = Message, O extends Messag
709
725
  * Process input implementation, calls the configured processing function
710
726
  *
711
727
  * @param input Input message
712
- * @param context Execution context
728
+ * @param options Invocation options
713
729
  * @returns Processing result
714
730
  */
715
- process(input: I, context: Context): PromiseOrValue<AgentProcessResult<O>>;
731
+ process(input: I, options: AgentInvokeOptions): PromiseOrValue<AgentProcessResult<O>>;
716
732
  }
717
733
  /**
718
734
  * Function type for function agents
@@ -725,4 +741,4 @@ export declare class FunctionAgent<I extends Message = Message, O extends Messag
725
741
  * @param context Execution context
726
742
  * @returns Processing result, can be synchronous or asynchronous
727
743
  */
728
- export type FunctionAgentFn<I extends Message = any, O extends Message = any> = (input: I, context: Context) => PromiseOrValue<AgentProcessResult<O>>;
744
+ export type FunctionAgentFn<I extends Message = any, O extends Message = any> = (input: I, options: AgentInvokeOptions) => PromiseOrValue<AgentProcessResult<O>>;