@aigne/core 1.17.0 → 1.18.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.
- package/CHANGELOG.md +33 -19
- package/lib/cjs/agents/agent.d.ts +27 -11
- package/lib/cjs/agents/agent.js +36 -12
- package/lib/cjs/agents/ai-agent.d.ts +1 -3
- package/lib/cjs/agents/ai-agent.js +2 -11
- package/lib/cjs/agents/mcp-agent.d.ts +1 -1
- package/lib/cjs/agents/mcp-agent.js +5 -11
- package/lib/cjs/agents/user-agent.d.ts +0 -1
- package/lib/cjs/agents/user-agent.js +1 -2
- package/lib/cjs/aigne/context.d.ts +2 -3
- package/lib/cjs/aigne/context.js +3 -5
- package/lib/cjs/aigne/message-queue.d.ts +6 -2
- package/lib/cjs/aigne/message-queue.js +2 -2
- package/lib/cjs/index.d.ts +1 -0
- package/lib/cjs/index.js +1 -0
- package/lib/cjs/loader/agent-js.d.ts +1 -1
- package/lib/cjs/loader/agent-yaml.d.ts +2 -1
- package/lib/cjs/loader/agent-yaml.js +3 -2
- package/lib/cjs/loader/index.d.ts +5 -1
- package/lib/cjs/loader/index.js +25 -16
- package/lib/cjs/memory/retriever.d.ts +2 -2
- package/lib/cjs/prompt/prompt-builder.d.ts +2 -6
- package/lib/cjs/prompt/prompt-builder.js +10 -6
- package/lib/cjs/prompt/template.js +1 -3
- package/lib/cjs/utils/logger.js +3 -3
- package/lib/cjs/utils/nodejs.d.ts +8 -0
- package/lib/cjs/utils/nodejs.js +24 -0
- package/lib/dts/agents/agent.d.ts +27 -11
- package/lib/dts/agents/ai-agent.d.ts +1 -3
- package/lib/dts/agents/mcp-agent.d.ts +1 -1
- package/lib/dts/agents/user-agent.d.ts +0 -1
- package/lib/dts/aigne/context.d.ts +2 -3
- package/lib/dts/aigne/message-queue.d.ts +6 -2
- package/lib/dts/index.d.ts +1 -0
- package/lib/dts/loader/agent-js.d.ts +1 -1
- package/lib/dts/loader/agent-yaml.d.ts +2 -1
- package/lib/dts/loader/index.d.ts +5 -1
- package/lib/dts/memory/retriever.d.ts +2 -2
- package/lib/dts/prompt/prompt-builder.d.ts +2 -6
- package/lib/dts/utils/nodejs.d.ts +8 -0
- package/lib/esm/agents/agent.d.ts +27 -11
- package/lib/esm/agents/agent.js +37 -13
- package/lib/esm/agents/ai-agent.d.ts +1 -3
- package/lib/esm/agents/ai-agent.js +2 -11
- package/lib/esm/agents/mcp-agent.d.ts +1 -1
- package/lib/esm/agents/mcp-agent.js +5 -11
- package/lib/esm/agents/user-agent.d.ts +0 -1
- package/lib/esm/agents/user-agent.js +0 -1
- package/lib/esm/aigne/context.d.ts +2 -3
- package/lib/esm/aigne/context.js +3 -2
- package/lib/esm/aigne/message-queue.d.ts +6 -2
- package/lib/esm/aigne/message-queue.js +2 -2
- package/lib/esm/index.d.ts +1 -0
- package/lib/esm/index.js +1 -0
- package/lib/esm/loader/agent-js.d.ts +1 -1
- package/lib/esm/loader/agent-yaml.d.ts +2 -1
- package/lib/esm/loader/agent-yaml.js +3 -2
- package/lib/esm/loader/index.d.ts +5 -1
- package/lib/esm/loader/index.js +25 -16
- package/lib/esm/memory/retriever.d.ts +2 -2
- package/lib/esm/prompt/prompt-builder.d.ts +2 -6
- package/lib/esm/prompt/prompt-builder.js +11 -7
- package/lib/esm/prompt/template.js +1 -3
- package/lib/esm/utils/logger.js +3 -3
- package/lib/esm/utils/nodejs.d.ts +8 -0
- package/lib/esm/utils/nodejs.js +21 -0
- package/package.json +4 -6
- package/lib/cjs/memory/default-memory/default-memory-storage/index.d.ts +0 -30
- package/lib/cjs/memory/default-memory/default-memory-storage/index.js +0 -69
- package/lib/cjs/memory/default-memory/default-memory-storage/migrate.d.ts +0 -7
- package/lib/cjs/memory/default-memory/default-memory-storage/migrate.js +0 -53
- package/lib/cjs/memory/default-memory/default-memory-storage/migrations/20250523165801-init.d.ts +0 -7
- package/lib/cjs/memory/default-memory/default-memory-storage/migrations/20250523165801-init.js +0 -33
- package/lib/cjs/memory/default-memory/default-memory-storage/models/memory.d.ts +0 -10
- package/lib/cjs/memory/default-memory/default-memory-storage/models/memory.js +0 -32
- package/lib/cjs/memory/default-memory/index.d.ts +0 -10
- package/lib/cjs/memory/default-memory/index.js +0 -61
- package/lib/cjs/memory/default-memory/storage.d.ts +0 -13
- package/lib/cjs/memory/default-memory/storage.js +0 -6
- package/lib/cjs/utils/fs.d.ts +0 -2
- package/lib/cjs/utils/fs.js +0 -25
- package/lib/dts/memory/default-memory/default-memory-storage/index.d.ts +0 -30
- package/lib/dts/memory/default-memory/default-memory-storage/migrate.d.ts +0 -7
- package/lib/dts/memory/default-memory/default-memory-storage/migrations/20250523165801-init.d.ts +0 -7
- package/lib/dts/memory/default-memory/default-memory-storage/models/memory.d.ts +0 -10
- package/lib/dts/memory/default-memory/index.d.ts +0 -10
- package/lib/dts/memory/default-memory/storage.d.ts +0 -13
- package/lib/dts/utils/fs.d.ts +0 -2
- package/lib/esm/memory/default-memory/default-memory-storage/index.d.ts +0 -30
- package/lib/esm/memory/default-memory/default-memory-storage/index.js +0 -64
- package/lib/esm/memory/default-memory/default-memory-storage/migrate.d.ts +0 -7
- package/lib/esm/memory/default-memory/default-memory-storage/migrate.js +0 -16
- package/lib/esm/memory/default-memory/default-memory-storage/migrations/20250523165801-init.d.ts +0 -7
- package/lib/esm/memory/default-memory/default-memory-storage/migrations/20250523165801-init.js +0 -28
- package/lib/esm/memory/default-memory/default-memory-storage/models/memory.d.ts +0 -10
- package/lib/esm/memory/default-memory/default-memory-storage/models/memory.js +0 -29
- package/lib/esm/memory/default-memory/index.d.ts +0 -10
- package/lib/esm/memory/default-memory/index.js +0 -54
- package/lib/esm/memory/default-memory/storage.d.ts +0 -13
- package/lib/esm/memory/default-memory/storage.js +0 -2
- package/lib/esm/utils/fs.d.ts +0 -2
- package/lib/esm/utils/fs.js +0 -21
package/lib/cjs/loader/index.js
CHANGED
|
@@ -4,24 +4,22 @@ exports.load = load;
|
|
|
4
4
|
exports.loadAgent = loadAgent;
|
|
5
5
|
exports.loadModel = loadModel;
|
|
6
6
|
exports.loadAIGNEFile = loadAIGNEFile;
|
|
7
|
-
const promises_1 = require("node:fs/promises");
|
|
8
|
-
const node_path_1 = require("node:path");
|
|
9
7
|
const yaml_1 = require("yaml");
|
|
10
8
|
const zod_1 = require("zod");
|
|
11
9
|
const agent_js_1 = require("../agents/agent.js");
|
|
12
10
|
const ai_agent_js_1 = require("../agents/ai-agent.js");
|
|
13
11
|
const mcp_agent_js_1 = require("../agents/mcp-agent.js");
|
|
12
|
+
const nodejs_js_1 = require("../utils/nodejs.js");
|
|
14
13
|
const type_utils_js_1 = require("../utils/type-utils.js");
|
|
15
14
|
const agent_js_js_1 = require("./agent-js.js");
|
|
16
15
|
const agent_yaml_js_1 = require("./agent-yaml.js");
|
|
17
16
|
const AIGNE_FILE_NAME = ["aigne.yaml", "aigne.yml"];
|
|
18
17
|
async function load(options) {
|
|
19
|
-
const
|
|
20
|
-
const
|
|
21
|
-
const rootDir = (0, node_path_1.dirname)(aigneFilePath);
|
|
18
|
+
const aigneFilePath = await getAIGNEFilePath(options.path);
|
|
19
|
+
const rootDir = nodejs_js_1.nodejs.path.dirname(aigneFilePath);
|
|
22
20
|
const aigne = await loadAIGNEFile(aigneFilePath);
|
|
23
|
-
const agents = await Promise.all((aigne.agents ?? []).map((filename) => loadAgent(
|
|
24
|
-
const skills = await Promise.all((aigne.skills ?? []).map((filename) => loadAgent(
|
|
21
|
+
const agents = await Promise.all((aigne.agents ?? []).map((filename) => loadAgent(nodejs_js_1.nodejs.path.join(rootDir, filename))));
|
|
22
|
+
const skills = await Promise.all((aigne.skills ?? []).map((filename) => loadAgent(nodejs_js_1.nodejs.path.join(rootDir, filename))));
|
|
25
23
|
return {
|
|
26
24
|
...aigne,
|
|
27
25
|
model: await loadModel(options.models, aigne.chat_model),
|
|
@@ -29,18 +27,21 @@ async function load(options) {
|
|
|
29
27
|
skills,
|
|
30
28
|
};
|
|
31
29
|
}
|
|
32
|
-
async function loadAgent(path) {
|
|
33
|
-
if (
|
|
30
|
+
async function loadAgent(path, options) {
|
|
31
|
+
if (nodejs_js_1.nodejs.path.extname(path) === ".js") {
|
|
34
32
|
const agent = await (0, agent_js_js_1.loadAgentFromJsFile)(path);
|
|
35
33
|
return agent_js_1.FunctionAgent.from(agent);
|
|
36
34
|
}
|
|
37
|
-
if (
|
|
35
|
+
if (nodejs_js_1.nodejs.path.extname(path) === ".yaml" || nodejs_js_1.nodejs.path.extname(path) === ".yml") {
|
|
38
36
|
const agent = await (0, agent_yaml_js_1.loadAgentFromYamlFile)(path);
|
|
39
37
|
if (agent.type === "ai") {
|
|
40
38
|
return ai_agent_js_1.AIAgent.from({
|
|
41
39
|
...agent,
|
|
40
|
+
memory: !options?.memories?.length || !agent.memory
|
|
41
|
+
? undefined
|
|
42
|
+
: await loadMemory(options.memories, typeof agent.memory === "object" ? agent.memory.provider : undefined, typeof agent.memory === "object" ? agent.memory : {}),
|
|
42
43
|
skills: agent.skills &&
|
|
43
|
-
(await Promise.all(agent.skills.map((filename) => loadAgent(
|
|
44
|
+
(await Promise.all(agent.skills.map((filename) => loadAgent(nodejs_js_1.nodejs.path.join(nodejs_js_1.nodejs.path.dirname(path), filename))))),
|
|
44
45
|
});
|
|
45
46
|
}
|
|
46
47
|
if (agent.type === "mcp") {
|
|
@@ -60,7 +61,15 @@ async function loadAgent(path) {
|
|
|
60
61
|
}
|
|
61
62
|
throw new Error(`Unsupported agent file type: ${path}`);
|
|
62
63
|
}
|
|
63
|
-
|
|
64
|
+
async function loadMemory(memories, provider, options) {
|
|
65
|
+
const M = !provider
|
|
66
|
+
? memories[0]
|
|
67
|
+
: memories.find((i) => i.name.toLowerCase().includes(provider.toLowerCase()));
|
|
68
|
+
if (!M)
|
|
69
|
+
throw new Error(`Unsupported memory: ${provider}`);
|
|
70
|
+
return new M(options);
|
|
71
|
+
}
|
|
72
|
+
const { MODEL_PROVIDER, MODEL_NAME } = nodejs_js_1.nodejs.env;
|
|
64
73
|
const DEFAULT_MODEL_PROVIDER = "openai";
|
|
65
74
|
async function loadModel(models, model, modelOptions) {
|
|
66
75
|
const params = {
|
|
@@ -98,17 +107,17 @@ const aigneFileSchema = zod_1.z.object({
|
|
|
98
107
|
skills: zod_1.z.array(zod_1.z.string()).nullish(),
|
|
99
108
|
});
|
|
100
109
|
async function loadAIGNEFile(path) {
|
|
101
|
-
const raw = await (0, type_utils_js_1.tryOrThrow)(() =>
|
|
110
|
+
const raw = await (0, type_utils_js_1.tryOrThrow)(() => nodejs_js_1.nodejs.fs.readFile(path, "utf8"), (error) => new Error(`Failed to load aigne.yaml from ${path}: ${error.message}`));
|
|
102
111
|
const json = await (0, type_utils_js_1.tryOrThrow)(() => (0, yaml_1.parse)(raw), (error) => new Error(`Failed to parse aigne.yaml from ${path}: ${error.message}`));
|
|
103
112
|
const agent = (0, type_utils_js_1.tryOrThrow)(() => aigneFileSchema.parse({ ...json, skills: json.skills ?? json.tools }), (error) => new Error(`Failed to validate aigne.yaml from ${path}: ${error.message}`));
|
|
104
113
|
return agent;
|
|
105
114
|
}
|
|
106
115
|
async function getAIGNEFilePath(path) {
|
|
107
|
-
const s = await
|
|
116
|
+
const s = await nodejs_js_1.nodejs.fs.stat(path);
|
|
108
117
|
if (s.isDirectory()) {
|
|
109
118
|
for (const file of AIGNE_FILE_NAME) {
|
|
110
|
-
const filePath =
|
|
111
|
-
if ((await
|
|
119
|
+
const filePath = nodejs_js_1.nodejs.path.join(path, file);
|
|
120
|
+
if ((await nodejs_js_1.nodejs.fs.stat(filePath)).isFile())
|
|
112
121
|
return filePath;
|
|
113
122
|
}
|
|
114
123
|
}
|
|
@@ -39,11 +39,11 @@ export declare const memoryRetrieverInputSchema: z.ZodObject<{
|
|
|
39
39
|
limit: z.ZodOptional<z.ZodNumber>;
|
|
40
40
|
search: z.ZodOptional<z.ZodString>;
|
|
41
41
|
}, "strip", z.ZodTypeAny, {
|
|
42
|
-
search?: string | undefined;
|
|
43
42
|
limit?: number | undefined;
|
|
44
|
-
}, {
|
|
45
43
|
search?: string | undefined;
|
|
44
|
+
}, {
|
|
46
45
|
limit?: number | undefined;
|
|
46
|
+
search?: string | undefined;
|
|
47
47
|
}>;
|
|
48
48
|
/**
|
|
49
49
|
* @hidden
|
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import type { GetPromptResult } from "@modelcontextprotocol/sdk/types.js";
|
|
2
|
-
import { Agent, type Message } from "../agents/agent.js";
|
|
2
|
+
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
|
-
import type { Context } from "../aigne/context.js";
|
|
6
|
-
import type { MemoryAgent } from "../memory/memory.js";
|
|
7
5
|
import { ChatMessagesTemplate } from "./template.js";
|
|
8
6
|
export declare const MESSAGE_KEY = "$message";
|
|
9
7
|
export declare function createMessage<V extends Message>(message: string, variables?: V): {
|
|
@@ -17,9 +15,7 @@ export declare function getMessage(input: Message): string | undefined;
|
|
|
17
15
|
export interface PromptBuilderOptions {
|
|
18
16
|
instructions?: string | ChatMessagesTemplate;
|
|
19
17
|
}
|
|
20
|
-
export interface PromptBuildOptions {
|
|
21
|
-
memory?: MemoryAgent | MemoryAgent[];
|
|
22
|
-
context: Context;
|
|
18
|
+
export interface PromptBuildOptions extends AgentInvokeOptions {
|
|
23
19
|
agent?: AIAgent;
|
|
24
20
|
input?: Message;
|
|
25
21
|
model?: ChatModel;
|
|
@@ -3,11 +3,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.PromptBuilder = exports.MESSAGE_KEY = void 0;
|
|
4
4
|
exports.createMessage = createMessage;
|
|
5
5
|
exports.getMessage = getMessage;
|
|
6
|
-
const promises_1 = require("node:fs/promises");
|
|
7
6
|
const yaml_1 = require("yaml");
|
|
8
7
|
const zod_1 = require("zod");
|
|
9
8
|
const agent_js_1 = require("../agents/agent.js");
|
|
10
9
|
const json_schema_js_1 = require("../utils/json-schema.js");
|
|
10
|
+
const nodejs_js_1 = require("../utils/nodejs.js");
|
|
11
11
|
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");
|
|
@@ -36,7 +36,7 @@ class PromptBuilder {
|
|
|
36
36
|
throw new Error(`Invalid instructions ${instructions}`);
|
|
37
37
|
}
|
|
38
38
|
static async fromFile(path) {
|
|
39
|
-
const text = await
|
|
39
|
+
const text = await nodejs_js_1.nodejs.fs.readFile(path, "utf-8");
|
|
40
40
|
return PromptBuilder.from(text);
|
|
41
41
|
}
|
|
42
42
|
static fromMCPPromptResult(result) {
|
|
@@ -83,11 +83,15 @@ class PromptBuilder {
|
|
|
83
83
|
const messages = (typeof this.instructions === "string"
|
|
84
84
|
? template_js_1.ChatMessagesTemplate.from([template_js_1.SystemMessageTemplate.from(this.instructions)])
|
|
85
85
|
: this.instructions)?.format(options.input) ?? [];
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
messages.push(...this.convertMemoriesToMessages(memories, options));
|
|
86
|
+
const memories = [];
|
|
87
|
+
if (options.agent) {
|
|
88
|
+
memories.push(...(await options.agent.retrieveMemories({ search: options.input }, options)));
|
|
90
89
|
}
|
|
90
|
+
if (options.memories?.length) {
|
|
91
|
+
memories.push(...options.memories);
|
|
92
|
+
}
|
|
93
|
+
if (memories.length)
|
|
94
|
+
messages.push(...this.convertMemoriesToMessages(memories, options));
|
|
91
95
|
const content = input && getMessage(input);
|
|
92
96
|
// add user input if it's not the same as the last message
|
|
93
97
|
if (content && messages.at(-1)?.content !== content) {
|
|
@@ -5,10 +5,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.ChatMessagesTemplate = exports.ToolMessageTemplate = exports.AgentMessageTemplate = exports.UserMessageTemplate = exports.SystemMessageTemplate = exports.ChatMessageTemplate = exports.PromptTemplate = void 0;
|
|
7
7
|
exports.parseChatMessages = parseChatMessages;
|
|
8
|
-
const node_util_1 = require("node:util");
|
|
9
8
|
const mustache_1 = __importDefault(require("mustache"));
|
|
10
9
|
const zod_1 = require("zod");
|
|
11
|
-
const type_utils_js_1 = require("../utils/type-utils.js");
|
|
12
10
|
class PromptTemplate {
|
|
13
11
|
template;
|
|
14
12
|
static from(template) {
|
|
@@ -92,7 +90,7 @@ class ToolMessageTemplate extends ChatMessageTemplate {
|
|
|
92
90
|
constructor(content, toolCallId, name) {
|
|
93
91
|
super("tool", typeof content === "string"
|
|
94
92
|
? content
|
|
95
|
-
:
|
|
93
|
+
: JSON.stringify(content, (_, value) => typeof value === "bigint" ? value.toString() : value), name);
|
|
96
94
|
this.toolCallId = toolCallId;
|
|
97
95
|
}
|
|
98
96
|
format(variables) {
|
package/lib/cjs/utils/logger.js
CHANGED
|
@@ -4,8 +4,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.logger = exports.Logger = exports.LogLevel = void 0;
|
|
7
|
-
const node_tty_1 = require("node:tty");
|
|
8
7
|
const debug_1 = __importDefault(require("debug"));
|
|
8
|
+
const nodejs_js_1 = require("./nodejs.js");
|
|
9
9
|
var LogLevel;
|
|
10
10
|
(function (LogLevel) {
|
|
11
11
|
LogLevel["ERROR"] = "error";
|
|
@@ -23,13 +23,13 @@ class Logger {
|
|
|
23
23
|
this.errorLogger = (0, debug_1.default)(`${options.ns}:error`);
|
|
24
24
|
for (const logger of [this.debugLogger, this.infoLogger, this.warnLogger]) {
|
|
25
25
|
// @ts-ignore
|
|
26
|
-
logger.useColors =
|
|
26
|
+
logger.useColors = nodejs_js_1.nodejs.isStdoutATTY;
|
|
27
27
|
logger.enabled = true;
|
|
28
28
|
logger.log = (...args) => this.logMessage(...args);
|
|
29
29
|
}
|
|
30
30
|
this.errorLogger.log = (...args) => this.logError(...args);
|
|
31
31
|
// @ts-ignore
|
|
32
|
-
this.errorLogger.useColors =
|
|
32
|
+
this.errorLogger.useColors = nodejs_js_1.nodejs.isStderrATTY;
|
|
33
33
|
this.errorLogger.enabled = true;
|
|
34
34
|
}
|
|
35
35
|
level;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.nodejs = void 0;
|
|
4
|
+
exports.nodejs = {
|
|
5
|
+
customInspect: isNodejsEnvironment() ? require("node:util").inspect.custom : Symbol("inspect"),
|
|
6
|
+
isStdoutATTY: isNodejsEnvironment() && process.stdout?.isTTY,
|
|
7
|
+
isStderrATTY: isNodejsEnvironment() && process.stderr?.isTTY,
|
|
8
|
+
env: isNodejsEnvironment() ? process.env : {},
|
|
9
|
+
get fs() {
|
|
10
|
+
ensureNodejsEnvironment();
|
|
11
|
+
return require("node:fs/promises");
|
|
12
|
+
},
|
|
13
|
+
get path() {
|
|
14
|
+
ensureNodejsEnvironment();
|
|
15
|
+
return require("node:path");
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
function isNodejsEnvironment() {
|
|
19
|
+
return typeof process !== "undefined";
|
|
20
|
+
}
|
|
21
|
+
function ensureNodejsEnvironment() {
|
|
22
|
+
if (!isNodejsEnvironment())
|
|
23
|
+
throw new Error("This code must run in a Node.js environment.");
|
|
24
|
+
}
|
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
import { inspect } from "node:util";
|
|
2
1
|
import { ZodObject, type ZodType } from "zod";
|
|
3
2
|
import type { Context, UserContext } from "../aigne/context.js";
|
|
4
3
|
import type { MessagePayload } from "../aigne/message-queue.js";
|
|
5
|
-
import type { MemoryAgent } from "../memory/memory.js";
|
|
4
|
+
import type { Memory, MemoryAgent } from "../memory/memory.js";
|
|
5
|
+
import type { MemoryRecorderInput } from "../memory/recorder.js";
|
|
6
|
+
import type { MemoryRetrieverInput } from "../memory/retriever.js";
|
|
7
|
+
import { nodejs } from "../utils/nodejs.js";
|
|
6
8
|
import { type Nullish, type PromiseOrValue, type XOr } from "../utils/type-utils.js";
|
|
7
9
|
import type { GuideRailAgent, GuideRailAgentOutput } from "./guide-rail-agent.js";
|
|
8
10
|
import { type TransferAgentOutput } from "./types.js";
|
|
@@ -95,6 +97,10 @@ export interface AgentOptions<I extends Message = Message, O extends Message = M
|
|
|
95
97
|
* One or more memory agents this agent can use
|
|
96
98
|
*/
|
|
97
99
|
memory?: MemoryAgent | MemoryAgent[];
|
|
100
|
+
/**
|
|
101
|
+
* Maximum number of memory items to retrieve
|
|
102
|
+
*/
|
|
103
|
+
maxRetrieveMemoryCount?: number;
|
|
98
104
|
}
|
|
99
105
|
export declare const agentOptionsSchema: ZodObject<{
|
|
100
106
|
[key in keyof AgentOptions]: ZodType<AgentOptions[key]>;
|
|
@@ -125,6 +131,8 @@ export interface AgentInvokeOptions<U extends UserContext = UserContext> {
|
|
|
125
131
|
* and returns the final JSON result
|
|
126
132
|
*/
|
|
127
133
|
streaming?: boolean;
|
|
134
|
+
userContext?: U;
|
|
135
|
+
memories?: Pick<Memory, "content">[];
|
|
128
136
|
}
|
|
129
137
|
/**
|
|
130
138
|
* Agent is the base class for all agents.
|
|
@@ -149,11 +157,24 @@ export interface AgentInvokeOptions<U extends UserContext = UserContext> {
|
|
|
149
157
|
* {@includeCode ../../test/agents/agent.test.ts#example-custom-agent}
|
|
150
158
|
*/
|
|
151
159
|
export declare abstract class Agent<I extends Message = Message, O extends Message = Message> {
|
|
160
|
+
/**
|
|
161
|
+
* Custom object inspection behavior
|
|
162
|
+
*
|
|
163
|
+
* When using Node.js's util.inspect function to inspect an agent,
|
|
164
|
+
* only the agent's name will be shown, making output more concise
|
|
165
|
+
*
|
|
166
|
+
* @returns Agent name
|
|
167
|
+
*/
|
|
168
|
+
[nodejs.customInspect]: () => string;
|
|
152
169
|
constructor(options?: AgentOptions<I, O>);
|
|
153
170
|
/**
|
|
154
171
|
* List of memories this agent can use
|
|
155
172
|
*/
|
|
156
173
|
readonly memories: MemoryAgent[];
|
|
174
|
+
/**
|
|
175
|
+
* Maximum number of memory items to retrieve
|
|
176
|
+
*/
|
|
177
|
+
maxRetrieveMemoryCount?: number;
|
|
157
178
|
/**
|
|
158
179
|
* Lifecycle hooks for agent processing.
|
|
159
180
|
*
|
|
@@ -297,6 +318,10 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
|
|
|
297
318
|
*/
|
|
298
319
|
private checkContextStatus;
|
|
299
320
|
private newDefaultContext;
|
|
321
|
+
retrieveMemories(input: Pick<MemoryRetrieverInput, "limit"> & {
|
|
322
|
+
search?: Message | string;
|
|
323
|
+
}, options: Pick<AgentInvokeOptions, "context">): Promise<Pick<Memory, "content">[]>;
|
|
324
|
+
recordMemories(input: MemoryRecorderInput, options: Pick<AgentInvokeOptions, "context">): Promise<void>;
|
|
300
325
|
/**
|
|
301
326
|
* Invoke the agent with regular (non-streaming) response
|
|
302
327
|
*
|
|
@@ -460,15 +485,6 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
|
|
|
460
485
|
* {@includeCode ../../test/agents/agent.test.ts#example-agent-shutdown-by-using}
|
|
461
486
|
*/
|
|
462
487
|
shutdown(): Promise<void>;
|
|
463
|
-
/**
|
|
464
|
-
* Custom object inspection behavior
|
|
465
|
-
*
|
|
466
|
-
* When using Node.js's util.inspect function to inspect an agent,
|
|
467
|
-
* only the agent's name will be shown, making output more concise
|
|
468
|
-
*
|
|
469
|
-
* @returns Agent name
|
|
470
|
-
*/
|
|
471
|
-
[inspect.custom](): string;
|
|
472
488
|
/**
|
|
473
489
|
* Async dispose method for shutdown the agent
|
|
474
490
|
*
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { type ZodObject, type ZodType, z } from "zod";
|
|
2
|
-
import { type DefaultMemoryOptions } from "../memory/default-memory/index.js";
|
|
3
2
|
import { PromptBuilder } from "../prompt/prompt-builder.js";
|
|
4
3
|
import { Agent, type AgentInvokeOptions, type AgentOptions, type AgentProcessAsyncGenerator, type Message } from "./agent.js";
|
|
5
4
|
import { ChatModel, type ChatModelInput } from "./chat-model.js";
|
|
@@ -13,7 +12,7 @@ import type { GuideRailAgentOutput } from "./guide-rail-agent.js";
|
|
|
13
12
|
* @template I The input message type the agent accepts
|
|
14
13
|
* @template O The output message type the agent returns
|
|
15
14
|
*/
|
|
16
|
-
export interface AIAgentOptions<I extends Message = Message, O extends Message = Message> extends
|
|
15
|
+
export interface AIAgentOptions<I extends Message = Message, O extends Message = Message> extends AgentOptions<I, O> {
|
|
17
16
|
/**
|
|
18
17
|
* The language model to use for this agent
|
|
19
18
|
*
|
|
@@ -65,7 +64,6 @@ export interface AIAgentOptions<I extends Message = Message, O extends Message =
|
|
|
65
64
|
* The template receives a {{memories}} variable containing serialized memory content.
|
|
66
65
|
*/
|
|
67
66
|
memoryPromptTemplate?: string;
|
|
68
|
-
memory?: AgentOptions<I, O>["memory"] | DefaultMemoryOptions | true;
|
|
69
67
|
}
|
|
70
68
|
/**
|
|
71
69
|
* Tool choice options for AI agents
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Client, type ClientOptions } from "@modelcontextprotocol/sdk/client/index.js";
|
|
2
2
|
import { type SSEClientTransportOptions } from "@modelcontextprotocol/sdk/client/sse.js";
|
|
3
|
-
import {
|
|
3
|
+
import type { StdioServerParameters } from "@modelcontextprotocol/sdk/client/stdio.js";
|
|
4
4
|
import { type StreamableHTTPClientTransportOptions } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
|
|
5
5
|
import type { RequestOptions } from "@modelcontextprotocol/sdk/shared/protocol.js";
|
|
6
6
|
import type { Transport } from "@modelcontextprotocol/sdk/shared/transport.js";
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { ReadableStream } from "node:stream/web";
|
|
2
1
|
import type { Context } from "../aigne/context.js";
|
|
3
2
|
import { type MessagePayload } from "../aigne/message-queue.js";
|
|
4
3
|
import { Agent, type AgentInvokeOptions, type AgentOptions, type AgentProcessResult, type FunctionAgentFn, type Message } from "./agent.js";
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { Emitter } from "strict-event-emitter";
|
|
2
2
|
import { Agent, type AgentInvokeOptions, type AgentProcessAsyncGenerator, type AgentResponse, type AgentResponseStream, type Message } from "../agents/agent.js";
|
|
3
3
|
import type { ChatModel } from "../agents/chat-model.js";
|
|
4
4
|
import { UserAgent } from "../agents/user-agent.js";
|
|
@@ -42,7 +42,6 @@ export interface InvokeOptions<U extends UserContext = UserContext> extends Part
|
|
|
42
42
|
returnActiveAgent?: boolean;
|
|
43
43
|
disableTransfer?: boolean;
|
|
44
44
|
sourceAgent?: Agent;
|
|
45
|
-
userContext?: U;
|
|
46
45
|
}
|
|
47
46
|
/**
|
|
48
47
|
* @hidden
|
|
@@ -150,7 +149,7 @@ declare class AIGNEContextShared {
|
|
|
150
149
|
messageQueue?: MessageQueue;
|
|
151
150
|
}) | undefined, overrides?: Partial<Context>);
|
|
152
151
|
readonly messageQueue: MessageQueue;
|
|
153
|
-
readonly events:
|
|
152
|
+
readonly events: Emitter<any>;
|
|
154
153
|
get model(): ChatModel | undefined;
|
|
155
154
|
get skills(): Agent<Message, Message>[] | undefined;
|
|
156
155
|
get limits(): ContextLimits | undefined;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Emitter, type EventMap } from "strict-event-emitter";
|
|
2
2
|
import type { Message } from "../agents/agent.js";
|
|
3
3
|
import type { Context } from "./context.js";
|
|
4
4
|
/**
|
|
@@ -30,11 +30,14 @@ export type MessageQueueListener = (message: MessagePayload) => void;
|
|
|
30
30
|
* @hidden
|
|
31
31
|
*/
|
|
32
32
|
export type Unsubscribe = () => void;
|
|
33
|
+
interface MessageQueueEventMap extends EventMap {
|
|
34
|
+
[key: string]: any[];
|
|
35
|
+
}
|
|
33
36
|
/**
|
|
34
37
|
* @hidden
|
|
35
38
|
*/
|
|
36
39
|
export declare class MessageQueue {
|
|
37
|
-
events:
|
|
40
|
+
events: Emitter<MessageQueueEventMap>;
|
|
38
41
|
publish(topic: string | string[], payload: MessagePayload): void;
|
|
39
42
|
error(error: Error): void;
|
|
40
43
|
subscribe(topic: string | string[], listener?: undefined): Promise<MessagePayload>;
|
|
@@ -42,3 +45,4 @@ export declare class MessageQueue {
|
|
|
42
45
|
subscribe(topic: string | string[], listener?: MessageQueueListener): Unsubscribe | Promise<MessagePayload>;
|
|
43
46
|
unsubscribe(topic: string | string[], listener: MessageQueueListener): void;
|
|
44
47
|
}
|
|
48
|
+
export {};
|
package/lib/dts/index.d.ts
CHANGED
|
@@ -7,5 +7,6 @@ export * from "./agents/team-agent.js";
|
|
|
7
7
|
export * from "./agents/types.js";
|
|
8
8
|
export * from "./agents/user-agent.js";
|
|
9
9
|
export * from "./aigne/index.js";
|
|
10
|
+
export * from "./memory/index.js";
|
|
10
11
|
export * from "./prompt/prompt-builder.js";
|
|
11
12
|
export * from "./prompt/template.js";
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { type ZodObject, type ZodType, z } from "zod";
|
|
2
2
|
import type { Message } from "../agents/agent.js";
|
|
3
3
|
export declare function loadAgentFromJsFile(path: string): Promise<{
|
|
4
|
-
name: string;
|
|
5
4
|
process: (args_0: Message) => Message;
|
|
5
|
+
name: string;
|
|
6
6
|
description?: string | undefined;
|
|
7
7
|
inputSchema?: ZodObject<Record<string, ZodType<any, z.ZodTypeDef, any>>, z.UnknownKeysParam, z.ZodTypeAny, {
|
|
8
8
|
[x: string]: any;
|
|
@@ -5,10 +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;
|
|
8
9
|
memory?: true | {
|
|
10
|
+
provider: string;
|
|
9
11
|
subscribeTopic?: string[] | undefined;
|
|
10
12
|
} | undefined;
|
|
11
|
-
instructions?: string | undefined;
|
|
12
13
|
inputSchema?: ZodObject<Record<string, ZodType<any, z.ZodTypeDef, any>>, z.UnknownKeysParam, z.ZodTypeAny, {
|
|
13
14
|
[x: string]: any;
|
|
14
15
|
}, {
|
|
@@ -2,6 +2,7 @@ import type { Camelize } from "camelize-ts";
|
|
|
2
2
|
import { z } from "zod";
|
|
3
3
|
import { type Agent } from "../agents/agent.js";
|
|
4
4
|
import type { ChatModel, ChatModelOptions } from "../agents/chat-model.js";
|
|
5
|
+
import type { MemoryAgent, MemoryAgentOptions } from "../memory/memory.js";
|
|
5
6
|
export interface LoadOptions {
|
|
6
7
|
models: {
|
|
7
8
|
new (parameters: {
|
|
@@ -9,6 +10,9 @@ export interface LoadOptions {
|
|
|
9
10
|
modelOptions?: ChatModelOptions;
|
|
10
11
|
}): ChatModel;
|
|
11
12
|
}[];
|
|
13
|
+
memories?: {
|
|
14
|
+
new (parameters?: MemoryAgentOptions): MemoryAgent;
|
|
15
|
+
}[];
|
|
12
16
|
path: string;
|
|
13
17
|
}
|
|
14
18
|
export declare function load(options: LoadOptions): Promise<{
|
|
@@ -26,7 +30,7 @@ export declare function load(options: LoadOptions): Promise<{
|
|
|
26
30
|
presence_penalty?: number | null | undefined;
|
|
27
31
|
} | null | undefined;
|
|
28
32
|
}>;
|
|
29
|
-
export declare function loadAgent(path: string): Promise<Agent>;
|
|
33
|
+
export declare function loadAgent(path: string, options?: LoadOptions): Promise<Agent>;
|
|
30
34
|
export declare function loadModel(models: LoadOptions["models"], model?: Camelize<z.infer<typeof aigneFileSchema>["chat_model"]>, modelOptions?: ChatModelOptions): Promise<ChatModel | undefined>;
|
|
31
35
|
declare const aigneFileSchema: z.ZodObject<{
|
|
32
36
|
name: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
@@ -39,11 +39,11 @@ export declare const memoryRetrieverInputSchema: z.ZodObject<{
|
|
|
39
39
|
limit: z.ZodOptional<z.ZodNumber>;
|
|
40
40
|
search: z.ZodOptional<z.ZodString>;
|
|
41
41
|
}, "strip", z.ZodTypeAny, {
|
|
42
|
-
search?: string | undefined;
|
|
43
42
|
limit?: number | undefined;
|
|
44
|
-
}, {
|
|
45
43
|
search?: string | undefined;
|
|
44
|
+
}, {
|
|
46
45
|
limit?: number | undefined;
|
|
46
|
+
search?: string | undefined;
|
|
47
47
|
}>;
|
|
48
48
|
/**
|
|
49
49
|
* @hidden
|
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import type { GetPromptResult } from "@modelcontextprotocol/sdk/types.js";
|
|
2
|
-
import { Agent, type Message } from "../agents/agent.js";
|
|
2
|
+
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
|
-
import type { Context } from "../aigne/context.js";
|
|
6
|
-
import type { MemoryAgent } from "../memory/memory.js";
|
|
7
5
|
import { ChatMessagesTemplate } from "./template.js";
|
|
8
6
|
export declare const MESSAGE_KEY = "$message";
|
|
9
7
|
export declare function createMessage<V extends Message>(message: string, variables?: V): {
|
|
@@ -17,9 +15,7 @@ export declare function getMessage(input: Message): string | undefined;
|
|
|
17
15
|
export interface PromptBuilderOptions {
|
|
18
16
|
instructions?: string | ChatMessagesTemplate;
|
|
19
17
|
}
|
|
20
|
-
export interface PromptBuildOptions {
|
|
21
|
-
memory?: MemoryAgent | MemoryAgent[];
|
|
22
|
-
context: Context;
|
|
18
|
+
export interface PromptBuildOptions extends AgentInvokeOptions {
|
|
23
19
|
agent?: AIAgent;
|
|
24
20
|
input?: Message;
|
|
25
21
|
model?: ChatModel;
|
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
import { inspect } from "node:util";
|
|
2
1
|
import { ZodObject, type ZodType } from "zod";
|
|
3
2
|
import type { Context, UserContext } from "../aigne/context.js";
|
|
4
3
|
import type { MessagePayload } from "../aigne/message-queue.js";
|
|
5
|
-
import type { MemoryAgent } from "../memory/memory.js";
|
|
4
|
+
import type { Memory, MemoryAgent } from "../memory/memory.js";
|
|
5
|
+
import type { MemoryRecorderInput } from "../memory/recorder.js";
|
|
6
|
+
import type { MemoryRetrieverInput } from "../memory/retriever.js";
|
|
7
|
+
import { nodejs } from "../utils/nodejs.js";
|
|
6
8
|
import { type Nullish, type PromiseOrValue, type XOr } from "../utils/type-utils.js";
|
|
7
9
|
import type { GuideRailAgent, GuideRailAgentOutput } from "./guide-rail-agent.js";
|
|
8
10
|
import { type TransferAgentOutput } from "./types.js";
|
|
@@ -95,6 +97,10 @@ export interface AgentOptions<I extends Message = Message, O extends Message = M
|
|
|
95
97
|
* One or more memory agents this agent can use
|
|
96
98
|
*/
|
|
97
99
|
memory?: MemoryAgent | MemoryAgent[];
|
|
100
|
+
/**
|
|
101
|
+
* Maximum number of memory items to retrieve
|
|
102
|
+
*/
|
|
103
|
+
maxRetrieveMemoryCount?: number;
|
|
98
104
|
}
|
|
99
105
|
export declare const agentOptionsSchema: ZodObject<{
|
|
100
106
|
[key in keyof AgentOptions]: ZodType<AgentOptions[key]>;
|
|
@@ -125,6 +131,8 @@ export interface AgentInvokeOptions<U extends UserContext = UserContext> {
|
|
|
125
131
|
* and returns the final JSON result
|
|
126
132
|
*/
|
|
127
133
|
streaming?: boolean;
|
|
134
|
+
userContext?: U;
|
|
135
|
+
memories?: Pick<Memory, "content">[];
|
|
128
136
|
}
|
|
129
137
|
/**
|
|
130
138
|
* Agent is the base class for all agents.
|
|
@@ -149,11 +157,24 @@ export interface AgentInvokeOptions<U extends UserContext = UserContext> {
|
|
|
149
157
|
* {@includeCode ../../test/agents/agent.test.ts#example-custom-agent}
|
|
150
158
|
*/
|
|
151
159
|
export declare abstract class Agent<I extends Message = Message, O extends Message = Message> {
|
|
160
|
+
/**
|
|
161
|
+
* Custom object inspection behavior
|
|
162
|
+
*
|
|
163
|
+
* When using Node.js's util.inspect function to inspect an agent,
|
|
164
|
+
* only the agent's name will be shown, making output more concise
|
|
165
|
+
*
|
|
166
|
+
* @returns Agent name
|
|
167
|
+
*/
|
|
168
|
+
[nodejs.customInspect]: () => string;
|
|
152
169
|
constructor(options?: AgentOptions<I, O>);
|
|
153
170
|
/**
|
|
154
171
|
* List of memories this agent can use
|
|
155
172
|
*/
|
|
156
173
|
readonly memories: MemoryAgent[];
|
|
174
|
+
/**
|
|
175
|
+
* Maximum number of memory items to retrieve
|
|
176
|
+
*/
|
|
177
|
+
maxRetrieveMemoryCount?: number;
|
|
157
178
|
/**
|
|
158
179
|
* Lifecycle hooks for agent processing.
|
|
159
180
|
*
|
|
@@ -297,6 +318,10 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
|
|
|
297
318
|
*/
|
|
298
319
|
private checkContextStatus;
|
|
299
320
|
private newDefaultContext;
|
|
321
|
+
retrieveMemories(input: Pick<MemoryRetrieverInput, "limit"> & {
|
|
322
|
+
search?: Message | string;
|
|
323
|
+
}, options: Pick<AgentInvokeOptions, "context">): Promise<Pick<Memory, "content">[]>;
|
|
324
|
+
recordMemories(input: MemoryRecorderInput, options: Pick<AgentInvokeOptions, "context">): Promise<void>;
|
|
300
325
|
/**
|
|
301
326
|
* Invoke the agent with regular (non-streaming) response
|
|
302
327
|
*
|
|
@@ -460,15 +485,6 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
|
|
|
460
485
|
* {@includeCode ../../test/agents/agent.test.ts#example-agent-shutdown-by-using}
|
|
461
486
|
*/
|
|
462
487
|
shutdown(): Promise<void>;
|
|
463
|
-
/**
|
|
464
|
-
* Custom object inspection behavior
|
|
465
|
-
*
|
|
466
|
-
* When using Node.js's util.inspect function to inspect an agent,
|
|
467
|
-
* only the agent's name will be shown, making output more concise
|
|
468
|
-
*
|
|
469
|
-
* @returns Agent name
|
|
470
|
-
*/
|
|
471
|
-
[inspect.custom](): string;
|
|
472
488
|
/**
|
|
473
489
|
* Async dispose method for shutdown the agent
|
|
474
490
|
*
|