@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
@@ -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 @@
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aigne/core",
3
- "version": "1.5.1-2",
3
+ "version": "1.7.0",
4
4
  "description": "AIGNE core library for building AI-powered applications",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -60,14 +60,14 @@
60
60
  },
61
61
  "dependencies": {
62
62
  "@aigne/json-schema-to-zod": "^1.3.3",
63
- "@modelcontextprotocol/sdk": "^1.8.0",
63
+ "@modelcontextprotocol/sdk": "^1.9.0",
64
64
  "@types/debug": "^4.1.12",
65
65
  "debug": "^4.4.0",
66
- "inquirer": "^12.5.0",
66
+ "inquirer": "^12.5.2",
67
67
  "mustache": "^4.2.0",
68
68
  "nanoid": "^5.1.5",
69
- "ora": "^8.2.0",
70
69
  "p-retry": "^6.2.1",
70
+ "uuid": "^11.1.0",
71
71
  "yaml": "^2.7.1",
72
72
  "zod": "^3.24.2",
73
73
  "zod-to-json-schema": "^3.24.5"
@@ -80,23 +80,23 @@
80
80
  "devDependencies": {
81
81
  "@anthropic-ai/sdk": "^0.39.0",
82
82
  "@google/generative-ai": "^0.24.0",
83
- "@types/bun": "^1.2.8",
83
+ "@types/bun": "^1.2.9",
84
84
  "@types/express": "^5.0.1",
85
85
  "@types/mustache": "^4.2.5",
86
- "@types/node": "^22.13.17",
86
+ "@types/node": "^22.14.0",
87
87
  "detect-port": "^2.1.0",
88
88
  "express": "^5.1.0",
89
89
  "npm-run-all": "^4.1.5",
90
- "openai": "^4.91.1",
90
+ "openai": "^4.93.0",
91
91
  "rimraf": "^6.0.1",
92
- "typescript": "^5.8.2"
92
+ "typescript": "^5.8.3"
93
93
  },
94
94
  "scripts": {
95
95
  "lint": "tsc --noEmit",
96
96
  "build": "tsc --build scripts/tsconfig.build.json",
97
97
  "clean": "rimraf lib test/coverage",
98
- "test": "cd test && bun test",
99
- "test:coverage": "cd test && bun test --coverage --coverage-reporter=lcov --coverage-reporter=text",
98
+ "test": "bun --cwd test test",
99
+ "test:coverage": "bun --cwd test test --coverage --coverage-reporter=lcov --coverage-reporter=text",
100
100
  "postbuild": "echo '{\"type\": \"module\"}' > lib/esm/package.json && echo '{\"type\": \"commonjs\"}' > lib/cjs/package.json"
101
101
  }
102
102
  }
@@ -1,40 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.loadAgentFromYamlFile = loadAgentFromYamlFile;
4
- const promises_1 = require("node:fs/promises");
5
- const json_schema_to_zod_1 = require("@aigne/json-schema-to-zod");
6
- const yaml_1 = require("yaml");
7
- const zod_1 = require("zod");
8
- const type_utils_js_1 = require("../utils/type-utils.js");
9
- const schema_js_1 = require("./schema.js");
10
- const agentFileSchema = zod_1.z.object({
11
- name: zod_1.z.string(),
12
- description: zod_1.z
13
- .string()
14
- .nullish()
15
- .transform((v) => v ?? undefined),
16
- instructions: zod_1.z
17
- .string()
18
- .nullish()
19
- .transform((v) => v ?? undefined),
20
- input_schema: schema_js_1.inputOutputSchema
21
- .nullish()
22
- .transform((v) => (v ? (0, json_schema_to_zod_1.jsonSchemaToZod)(v) : undefined)),
23
- output_schema: schema_js_1.inputOutputSchema
24
- .nullish()
25
- .transform((v) => (v ? (0, json_schema_to_zod_1.jsonSchemaToZod)(v) : undefined)),
26
- output_key: zod_1.z
27
- .string()
28
- .nullish()
29
- .transform((v) => v ?? undefined),
30
- tools: zod_1.z
31
- .array(zod_1.z.string())
32
- .nullish()
33
- .transform((v) => v ?? undefined),
34
- });
35
- async function loadAgentFromYamlFile(path, { readFile: _readFile = promises_1.readFile } = {}) {
36
- const raw = await (0, type_utils_js_1.tryOrThrow)(() => _readFile(path, "utf8"), (error) => new Error(`Failed to load agent definition from ${path}: ${error.message}`));
37
- const json = await (0, type_utils_js_1.tryOrThrow)(() => (0, yaml_1.parse)(raw), (error) => new Error(`Failed to parse agent definition from ${path}: ${error.message}`));
38
- const agent = (0, type_utils_js_1.tryOrThrow)(() => agentFileSchema.parse(json), (error) => new Error(`Failed to validate agent definition from ${path}: ${error.message}`));
39
- return agent;
40
- }
@@ -1,11 +0,0 @@
1
- import type { Message } from "../agents/agent.js";
2
- import type { UserAgent } from "../agents/user-agent.js";
3
- export interface ChatLoopOptions {
4
- log?: typeof console.log;
5
- initialCall?: Message | string;
6
- welcome?: string;
7
- defaultQuestion?: string;
8
- onResponse?: (response: Message) => void;
9
- inputKey?: string;
10
- }
11
- export declare function runChatLoopInTerminal(userAgent: UserAgent, { log, ...options }?: ChatLoopOptions): Promise<void>;
@@ -1,82 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.runChatLoopInTerminal = runChatLoopInTerminal;
7
- const inquirer_1 = __importDefault(require("inquirer"));
8
- const ora_1 = __importDefault(require("ora"));
9
- const logger_js_1 = require("./logger.js");
10
- async function runChatLoopInTerminal(userAgent, { log = console.log.bind(console), ...options } = {}) {
11
- logger_js_1.logger.setSpinner((0, ora_1.default)());
12
- let isLoopExited = false;
13
- let prompt;
14
- if (options?.welcome)
15
- log(options.welcome);
16
- if (options?.initialCall) {
17
- await callAgent(userAgent, options.initialCall, { ...options, log });
18
- }
19
- (async () => {
20
- for await (const output of userAgent.stream) {
21
- if (isLoopExited)
22
- return;
23
- if (options?.onResponse)
24
- options.onResponse(output.message);
25
- else
26
- log(output);
27
- prompt?.ui.close();
28
- }
29
- })();
30
- for (let i = 0;; i++) {
31
- prompt = inquirer_1.default.prompt([
32
- {
33
- type: "input",
34
- name: "question",
35
- message: "💬",
36
- default: i === 0 ? options?.defaultQuestion : undefined,
37
- },
38
- ]);
39
- let question;
40
- try {
41
- question = (await prompt).question;
42
- }
43
- catch {
44
- // ignore abort error from inquirer
45
- }
46
- if (!question?.trim())
47
- continue;
48
- const cmd = COMMANDS[question.trim()];
49
- if (cmd) {
50
- const result = cmd();
51
- if (result.message)
52
- log(result.message);
53
- if (result?.exit)
54
- break;
55
- continue;
56
- }
57
- await callAgent(userAgent, question, { ...options, log });
58
- }
59
- isLoopExited = true;
60
- }
61
- async function callAgent(agent, input, options) {
62
- try {
63
- const response = await logger_js_1.logger.spinner(agent.call(options.inputKey && typeof input === "string" ? { [options.inputKey]: input } : input), "🤖");
64
- if (options?.onResponse)
65
- options.onResponse(response);
66
- else
67
- options.log(response);
68
- }
69
- catch (error) {
70
- options.log(`ERROR: ${error.message}`);
71
- }
72
- }
73
- const COMMANDS = {
74
- "/exit": () => ({ exit: true }),
75
- "/help": () => ({
76
- message: `\
77
- Commands:
78
- /exit - exit the chat loop
79
- /help - show this help message
80
- `,
81
- }),
82
- };
@@ -1,11 +0,0 @@
1
- import type { Message } from "../agents/agent.js";
2
- import type { UserAgent } from "../agents/user-agent.js";
3
- export interface ChatLoopOptions {
4
- log?: typeof console.log;
5
- initialCall?: Message | string;
6
- welcome?: string;
7
- defaultQuestion?: string;
8
- onResponse?: (response: Message) => void;
9
- inputKey?: string;
10
- }
11
- export declare function runChatLoopInTerminal(userAgent: UserAgent, { log, ...options }?: ChatLoopOptions): Promise<void>;
@@ -1,37 +0,0 @@
1
- import { readFile } from "node:fs/promises";
2
- import { jsonSchemaToZod } from "@aigne/json-schema-to-zod";
3
- import { parse } from "yaml";
4
- import { z } from "zod";
5
- import { tryOrThrow } from "../utils/type-utils.js";
6
- import { inputOutputSchema } from "./schema.js";
7
- const agentFileSchema = z.object({
8
- name: z.string(),
9
- description: z
10
- .string()
11
- .nullish()
12
- .transform((v) => v ?? undefined),
13
- instructions: z
14
- .string()
15
- .nullish()
16
- .transform((v) => v ?? undefined),
17
- input_schema: inputOutputSchema
18
- .nullish()
19
- .transform((v) => (v ? jsonSchemaToZod(v) : undefined)),
20
- output_schema: inputOutputSchema
21
- .nullish()
22
- .transform((v) => (v ? jsonSchemaToZod(v) : undefined)),
23
- output_key: z
24
- .string()
25
- .nullish()
26
- .transform((v) => v ?? undefined),
27
- tools: z
28
- .array(z.string())
29
- .nullish()
30
- .transform((v) => v ?? undefined),
31
- });
32
- export async function loadAgentFromYamlFile(path, { readFile: _readFile = readFile } = {}) {
33
- const raw = await tryOrThrow(() => _readFile(path, "utf8"), (error) => new Error(`Failed to load agent definition from ${path}: ${error.message}`));
34
- const json = await tryOrThrow(() => parse(raw), (error) => new Error(`Failed to parse agent definition from ${path}: ${error.message}`));
35
- const agent = tryOrThrow(() => agentFileSchema.parse(json), (error) => new Error(`Failed to validate agent definition from ${path}: ${error.message}`));
36
- return agent;
37
- }
@@ -1,11 +0,0 @@
1
- import type { Message } from "../agents/agent.js";
2
- import type { UserAgent } from "../agents/user-agent.js";
3
- export interface ChatLoopOptions {
4
- log?: typeof console.log;
5
- initialCall?: Message | string;
6
- welcome?: string;
7
- defaultQuestion?: string;
8
- onResponse?: (response: Message) => void;
9
- inputKey?: string;
10
- }
11
- export declare function runChatLoopInTerminal(userAgent: UserAgent, { log, ...options }?: ChatLoopOptions): Promise<void>;
@@ -1,76 +0,0 @@
1
- import inquirer from "inquirer";
2
- import ora from "ora";
3
- import { logger } from "./logger.js";
4
- export async function runChatLoopInTerminal(userAgent, { log = console.log.bind(console), ...options } = {}) {
5
- logger.setSpinner(ora());
6
- let isLoopExited = false;
7
- let prompt;
8
- if (options?.welcome)
9
- log(options.welcome);
10
- if (options?.initialCall) {
11
- await callAgent(userAgent, options.initialCall, { ...options, log });
12
- }
13
- (async () => {
14
- for await (const output of userAgent.stream) {
15
- if (isLoopExited)
16
- return;
17
- if (options?.onResponse)
18
- options.onResponse(output.message);
19
- else
20
- log(output);
21
- prompt?.ui.close();
22
- }
23
- })();
24
- for (let i = 0;; i++) {
25
- prompt = inquirer.prompt([
26
- {
27
- type: "input",
28
- name: "question",
29
- message: "💬",
30
- default: i === 0 ? options?.defaultQuestion : undefined,
31
- },
32
- ]);
33
- let question;
34
- try {
35
- question = (await prompt).question;
36
- }
37
- catch {
38
- // ignore abort error from inquirer
39
- }
40
- if (!question?.trim())
41
- continue;
42
- const cmd = COMMANDS[question.trim()];
43
- if (cmd) {
44
- const result = cmd();
45
- if (result.message)
46
- log(result.message);
47
- if (result?.exit)
48
- break;
49
- continue;
50
- }
51
- await callAgent(userAgent, question, { ...options, log });
52
- }
53
- isLoopExited = true;
54
- }
55
- async function callAgent(agent, input, options) {
56
- try {
57
- const response = await logger.spinner(agent.call(options.inputKey && typeof input === "string" ? { [options.inputKey]: input } : input), "🤖");
58
- if (options?.onResponse)
59
- options.onResponse(response);
60
- else
61
- options.log(response);
62
- }
63
- catch (error) {
64
- options.log(`ERROR: ${error.message}`);
65
- }
66
- }
67
- const COMMANDS = {
68
- "/exit": () => ({ exit: true }),
69
- "/help": () => ({
70
- message: `\
71
- Commands:
72
- /exit - exit the chat loop
73
- /help - show this help message
74
- `,
75
- }),
76
- };