@aigne/core 1.1.0-beta.1 → 1.1.0-beta.2
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/lib/cjs/agents/agent.d.ts +23 -17
- package/lib/cjs/agents/agent.js +14 -15
- package/lib/cjs/agents/ai-agent.d.ts +2 -3
- package/lib/cjs/agents/ai-agent.js +6 -4
- package/lib/cjs/agents/mcp-agent.d.ts +2 -1
- package/lib/cjs/agents/mcp-agent.js +25 -5
- package/lib/cjs/execution-engine/index.d.ts +3 -3
- package/lib/cjs/execution-engine/index.js +3 -3
- package/lib/cjs/models/chat-openai.d.ts +4 -4
- package/lib/cjs/models/chat-openai.js +4 -1
- package/lib/cjs/models/chat.d.ts +1 -1
- package/lib/cjs/prompt/prompt-builder.d.ts +18 -9
- package/lib/cjs/prompt/prompt-builder.js +29 -15
- package/lib/cjs/utils/logger.d.ts +6 -3
- package/lib/cjs/utils/logger.js +9 -12
- package/lib/dts/agents/agent.d.ts +23 -17
- package/lib/dts/agents/ai-agent.d.ts +2 -3
- package/lib/dts/agents/mcp-agent.d.ts +2 -1
- package/lib/dts/execution-engine/index.d.ts +3 -3
- package/lib/dts/models/chat-openai.d.ts +4 -4
- package/lib/dts/models/chat.d.ts +1 -1
- package/lib/dts/prompt/prompt-builder.d.ts +18 -9
- package/lib/dts/utils/logger.d.ts +6 -3
- package/lib/esm/agents/agent.d.ts +23 -17
- package/lib/esm/agents/agent.js +14 -15
- package/lib/esm/agents/ai-agent.d.ts +2 -3
- package/lib/esm/agents/ai-agent.js +6 -4
- package/lib/esm/agents/mcp-agent.d.ts +2 -1
- package/lib/esm/agents/mcp-agent.js +25 -5
- package/lib/esm/execution-engine/index.d.ts +3 -3
- package/lib/esm/execution-engine/index.js +3 -3
- package/lib/esm/models/chat-openai.d.ts +4 -4
- package/lib/esm/models/chat-openai.js +4 -1
- package/lib/esm/models/chat.d.ts +1 -1
- package/lib/esm/prompt/prompt-builder.d.ts +18 -9
- package/lib/esm/prompt/prompt-builder.js +30 -16
- package/lib/esm/utils/logger.d.ts +6 -3
- package/lib/esm/utils/logger.js +6 -12
- package/package.json +5 -1
- package/lib/cjs/prompt/templates/instructions.d.ts +0 -1
- package/lib/cjs/prompt/templates/instructions.js +0 -10
- package/lib/dts/prompt/templates/instructions.d.ts +0 -1
- package/lib/esm/prompt/templates/instructions.d.ts +0 -1
- package/lib/esm/prompt/templates/instructions.js +0 -7
|
@@ -1,41 +1,47 @@
|
|
|
1
1
|
import EventEmitter from "node:events";
|
|
2
|
-
import { type ZodObject } from "zod";
|
|
2
|
+
import { type ZodObject, type ZodType } from "zod";
|
|
3
3
|
import type { Context } from "../execution-engine/context";
|
|
4
4
|
import { type TransferAgentOutput } from "./types";
|
|
5
5
|
export type AgentInput = Record<string, unknown>;
|
|
6
6
|
export type AgentOutput = Record<string, unknown> & Partial<TransferAgentOutput>;
|
|
7
7
|
export type SubscribeTopic = string | string[];
|
|
8
8
|
export type PublishTopic<O extends AgentOutput = AgentOutput> = string | string[] | ((output: O) => string | string[] | Promise<string | string[]>);
|
|
9
|
-
export interface AgentOptions<
|
|
9
|
+
export interface AgentOptions<I extends AgentInput = AgentInput, O extends AgentOutput = AgentOutput> {
|
|
10
10
|
subscribeTopic?: SubscribeTopic;
|
|
11
11
|
publishTopic?: PublishTopic<O>;
|
|
12
12
|
name?: string;
|
|
13
13
|
description?: string;
|
|
14
|
-
inputSchema?: ZodObject<
|
|
15
|
-
|
|
14
|
+
inputSchema?: ZodObject<{
|
|
15
|
+
[key in keyof I]: ZodType;
|
|
16
|
+
}>;
|
|
17
|
+
outputSchema?: ZodObject<{
|
|
18
|
+
[key in keyof O]: ZodType;
|
|
19
|
+
}>;
|
|
16
20
|
includeInputInOutput?: boolean;
|
|
17
21
|
tools?: (Agent | FunctionAgentFn)[];
|
|
18
|
-
skills?: (Agent | FunctionAgentFn)[];
|
|
19
22
|
}
|
|
20
23
|
export declare class Agent<I extends AgentInput = AgentInput, O extends AgentOutput = AgentOutput> extends EventEmitter {
|
|
21
24
|
static from<I extends AgentInput = AgentInput, O extends AgentOutput = AgentOutput>(options: AgentOptions<I, O>): Agent<I, O>;
|
|
22
25
|
constructor(options: AgentOptions<I, O>);
|
|
23
|
-
name: string;
|
|
24
|
-
description?: string;
|
|
25
|
-
inputSchema: ZodObject<
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
readonly
|
|
26
|
+
readonly name: string;
|
|
27
|
+
readonly description?: string;
|
|
28
|
+
readonly inputSchema: ZodObject<{
|
|
29
|
+
[key in keyof I]: ZodType;
|
|
30
|
+
}>;
|
|
31
|
+
readonly outputSchema: ZodObject<{
|
|
32
|
+
[key in keyof O]: ZodType;
|
|
33
|
+
}>;
|
|
34
|
+
readonly includeInputInOutput?: boolean;
|
|
35
|
+
readonly subscribeTopic?: SubscribeTopic;
|
|
36
|
+
readonly publishTopic?: PublishTopic<AgentOutput>;
|
|
37
|
+
readonly tools: Agent<AgentInput, AgentOutput>[] & {
|
|
32
38
|
[key: string]: Agent<AgentInput, AgentOutput>;
|
|
33
|
-
}
|
|
34
|
-
|
|
39
|
+
};
|
|
40
|
+
addTool<I extends AgentInput, O extends AgentOutput>(tool: Agent<I, O> | FunctionAgentFn<I, O>): void;
|
|
35
41
|
get isCallable(): boolean;
|
|
36
42
|
call(input: I | string, context?: Context): Promise<O>;
|
|
37
43
|
process?(input: I, context?: Context): Promise<O>;
|
|
38
|
-
|
|
44
|
+
shutdown(): Promise<void>;
|
|
39
45
|
}
|
|
40
46
|
export interface FunctionAgentOptions<I extends AgentInput = AgentInput, O extends AgentOutput = AgentOutput> extends AgentOptions<I, O> {
|
|
41
47
|
fn?: FunctionAgentFn<I, O>;
|
package/lib/cjs/agents/agent.js
CHANGED
|
@@ -17,15 +17,15 @@ class Agent extends node_events_1.default {
|
|
|
17
17
|
super();
|
|
18
18
|
this.name = options.name || this.constructor.name;
|
|
19
19
|
this.description = options.description;
|
|
20
|
-
this.inputSchema =
|
|
21
|
-
|
|
20
|
+
this.inputSchema =
|
|
21
|
+
options.inputSchema || zod_1.z.object({});
|
|
22
|
+
this.outputSchema =
|
|
23
|
+
options.outputSchema || zod_1.z.object({});
|
|
22
24
|
this.includeInputInOutput = options.includeInputInOutput;
|
|
23
25
|
this.subscribeTopic = options.subscribeTopic;
|
|
24
26
|
this.publishTopic = options.publishTopic;
|
|
25
27
|
if (options.tools?.length)
|
|
26
28
|
this.tools.push(...options.tools.map(functionToAgent));
|
|
27
|
-
if (options.skills?.length)
|
|
28
|
-
this.skills.push(...options.skills.map(functionToAgent));
|
|
29
29
|
}
|
|
30
30
|
name;
|
|
31
31
|
description;
|
|
@@ -34,14 +34,11 @@ class Agent extends node_events_1.default {
|
|
|
34
34
|
includeInputInOutput;
|
|
35
35
|
subscribeTopic;
|
|
36
36
|
publishTopic;
|
|
37
|
-
tools = []
|
|
38
|
-
|
|
39
|
-
get: (target, p, receiver) => {
|
|
40
|
-
return Reflect.get(target, p, receiver) ?? target.find((i) => i.name === p);
|
|
41
|
-
},
|
|
37
|
+
tools = new Proxy([], {
|
|
38
|
+
get: (t, p, r) => Reflect.get(t, p, r) ?? t.find((t) => t.name === p),
|
|
42
39
|
});
|
|
43
|
-
|
|
44
|
-
this.
|
|
40
|
+
addTool(tool) {
|
|
41
|
+
this.tools.push(typeof tool === "function" ? functionToAgent(tool) : tool);
|
|
45
42
|
}
|
|
46
43
|
get isCallable() {
|
|
47
44
|
return !!this.process;
|
|
@@ -51,19 +48,21 @@ class Agent extends node_events_1.default {
|
|
|
51
48
|
throw new Error("Agent must implement process method");
|
|
52
49
|
const _input = typeof input === "string" ? (0, prompt_builder_1.userInput)(input) : input;
|
|
53
50
|
const parsedInput = this.inputSchema.passthrough().parse(_input);
|
|
54
|
-
logger_1.logger.debug(`
|
|
51
|
+
logger_1.logger.debug(`Call agent ${this.name} start`, parsedInput);
|
|
55
52
|
const output = await this.process(parsedInput, context);
|
|
56
53
|
const parsedOutput = this.outputSchema.passthrough().parse(output);
|
|
57
54
|
const finalOutput = this.includeInputInOutput
|
|
58
55
|
? { ...parsedInput, ...parsedOutput }
|
|
59
56
|
: parsedOutput;
|
|
60
|
-
logger_1.logger.debug(`
|
|
57
|
+
logger_1.logger.debug(`Call agent ${this.name} successfully`, {
|
|
61
58
|
...finalOutput,
|
|
62
|
-
|
|
59
|
+
...(finalOutput[types_1.transferAgentOutputKey]
|
|
60
|
+
? { [types_1.transferAgentOutputKey]: finalOutput[types_1.transferAgentOutputKey].agent.name }
|
|
61
|
+
: {}),
|
|
63
62
|
});
|
|
64
63
|
return finalOutput;
|
|
65
64
|
}
|
|
66
|
-
async
|
|
65
|
+
async shutdown() { }
|
|
67
66
|
}
|
|
68
67
|
exports.Agent = Agent;
|
|
69
68
|
class FunctionAgent extends Agent {
|
|
@@ -4,7 +4,7 @@ import { PromptBuilder } from "../prompt/prompt-builder";
|
|
|
4
4
|
import { Agent, type AgentInput, type AgentOptions, type AgentOutput } from "./agent";
|
|
5
5
|
export interface AIAgentOptions<I extends AgentInput = AgentInput, O extends AgentOutput = AgentOutput> extends AgentOptions<I, O> {
|
|
6
6
|
model?: ChatModel;
|
|
7
|
-
instructions?: string;
|
|
7
|
+
instructions?: string | PromptBuilder;
|
|
8
8
|
outputKey?: string;
|
|
9
9
|
toolChoice?: AIAgentToolChoice;
|
|
10
10
|
}
|
|
@@ -13,8 +13,7 @@ export declare class AIAgent<I extends AgentInput = AgentInput, O extends AgentO
|
|
|
13
13
|
static from<I extends AgentInput, O extends AgentOutput>(options: AIAgentOptions<I, O>): AIAgent<I, O>;
|
|
14
14
|
constructor(options: AIAgentOptions<I, O>);
|
|
15
15
|
model?: ChatModel;
|
|
16
|
-
|
|
17
|
-
instructions?: string;
|
|
16
|
+
instructions: PromptBuilder;
|
|
18
17
|
outputKey?: string;
|
|
19
18
|
toolChoice?: AIAgentToolChoice;
|
|
20
19
|
process(input: I, context?: Context): Promise<O>;
|
|
@@ -13,12 +13,14 @@ class AIAgent extends agent_1.Agent {
|
|
|
13
13
|
constructor(options) {
|
|
14
14
|
super(options);
|
|
15
15
|
this.model = options.model;
|
|
16
|
-
this.instructions =
|
|
16
|
+
this.instructions =
|
|
17
|
+
typeof options.instructions === "string"
|
|
18
|
+
? prompt_builder_1.PromptBuilder.from(options.instructions)
|
|
19
|
+
: (options.instructions ?? new prompt_builder_1.PromptBuilder());
|
|
17
20
|
this.outputKey = options.outputKey;
|
|
18
21
|
this.toolChoice = options.toolChoice;
|
|
19
22
|
}
|
|
20
23
|
model;
|
|
21
|
-
promptBuilder = new prompt_builder_1.PromptBuilder();
|
|
22
24
|
instructions;
|
|
23
25
|
outputKey;
|
|
24
26
|
toolChoice;
|
|
@@ -27,13 +29,13 @@ class AIAgent extends agent_1.Agent {
|
|
|
27
29
|
if (!model)
|
|
28
30
|
throw new Error("model is required to run AIAgent");
|
|
29
31
|
let transferOutput;
|
|
30
|
-
const { toolAgents, ...modelInput } = await this.
|
|
32
|
+
const { toolAgents, ...modelInput } = await this.instructions.build({
|
|
31
33
|
agent: this,
|
|
32
34
|
input,
|
|
33
35
|
model,
|
|
34
36
|
context,
|
|
35
37
|
});
|
|
36
|
-
const toolsMap = new Map(toolAgents
|
|
38
|
+
const toolsMap = new Map(toolAgents?.map((i) => [i.name, i]));
|
|
37
39
|
for (;;) {
|
|
38
40
|
const { text, json, toolCalls } = await model.call(modelInput, context);
|
|
39
41
|
if (toolCalls?.length) {
|
|
@@ -14,7 +14,7 @@ export declare class MCPAgent extends Agent {
|
|
|
14
14
|
private static fromTransport;
|
|
15
15
|
constructor(options: MCPAgentOptions);
|
|
16
16
|
private client;
|
|
17
|
-
|
|
17
|
+
shutdown(): Promise<void>;
|
|
18
18
|
}
|
|
19
19
|
export interface MCPToolOptions extends AgentOptions {
|
|
20
20
|
client: Client;
|
|
@@ -22,5 +22,6 @@ export interface MCPToolOptions extends AgentOptions {
|
|
|
22
22
|
export declare class MCPTool extends Agent {
|
|
23
23
|
constructor(options: MCPToolOptions);
|
|
24
24
|
private client;
|
|
25
|
+
private get mcpServer();
|
|
25
26
|
process(input: AgentInput): Promise<AgentOutput>;
|
|
26
27
|
}
|
|
@@ -6,9 +6,11 @@ const sse_js_1 = require("@modelcontextprotocol/sdk/client/sse.js");
|
|
|
6
6
|
const stdio_js_1 = require("@modelcontextprotocol/sdk/client/stdio.js");
|
|
7
7
|
const json_schema_to_zod_1 = require("@n8n/json-schema-to-zod");
|
|
8
8
|
const zod_1 = require("zod");
|
|
9
|
+
const logger_1 = require("../utils/logger");
|
|
9
10
|
const agent_1 = require("./agent");
|
|
10
11
|
const MCP_AGENT_CLIENT_NAME = "MCPAgent";
|
|
11
12
|
const MCP_AGENT_CLIENT_VERSION = "0.0.1";
|
|
13
|
+
const debug = logger_1.logger.base.extend("mcp-agent:debug", "/");
|
|
12
14
|
function isSSEServerParameters(options) {
|
|
13
15
|
return "url" in options && typeof options.url === "string";
|
|
14
16
|
}
|
|
@@ -32,9 +34,14 @@ class MCPAgent extends agent_1.Agent {
|
|
|
32
34
|
name: MCP_AGENT_CLIENT_NAME,
|
|
33
35
|
version: MCP_AGENT_CLIENT_VERSION,
|
|
34
36
|
});
|
|
37
|
+
debug(`Connecting to MCP server with transport ${transport.constructor.name}`);
|
|
35
38
|
await client.connect(transport);
|
|
39
|
+
debug(`Connected to MCP server with transport ${transport.constructor.name}`);
|
|
40
|
+
const mcpServer = getMCPServerName(client);
|
|
41
|
+
debug(`Listing tools from ${mcpServer}`);
|
|
36
42
|
const { tools: mcpTools } = await client.listTools();
|
|
37
|
-
|
|
43
|
+
debug(`Listed tools from ${mcpServer}`, mcpTools.map((i) => i.name));
|
|
44
|
+
const tools = mcpTools.map((tool) => {
|
|
38
45
|
return new MCPTool({
|
|
39
46
|
client,
|
|
40
47
|
name: tool.name,
|
|
@@ -49,15 +56,15 @@ class MCPAgent extends agent_1.Agent {
|
|
|
49
56
|
.passthrough(),
|
|
50
57
|
});
|
|
51
58
|
});
|
|
52
|
-
return new MCPAgent({ client,
|
|
59
|
+
return new MCPAgent({ client, tools });
|
|
53
60
|
}
|
|
54
61
|
constructor(options) {
|
|
55
62
|
super(options);
|
|
56
63
|
this.client = options.client;
|
|
57
64
|
}
|
|
58
65
|
client;
|
|
59
|
-
async
|
|
60
|
-
super.
|
|
66
|
+
async shutdown() {
|
|
67
|
+
super.shutdown();
|
|
61
68
|
await this.client.close();
|
|
62
69
|
}
|
|
63
70
|
}
|
|
@@ -68,8 +75,21 @@ class MCPTool extends agent_1.Agent {
|
|
|
68
75
|
this.client = options.client;
|
|
69
76
|
}
|
|
70
77
|
client;
|
|
78
|
+
get mcpServer() {
|
|
79
|
+
return getMCPServerName(this.client);
|
|
80
|
+
}
|
|
71
81
|
async process(input) {
|
|
72
|
-
|
|
82
|
+
debug(`Start call tool ${this.name} from ${this.mcpServer} with input`, input);
|
|
83
|
+
const result = await this.client.callTool({ name: this.name, arguments: input });
|
|
84
|
+
debug(`End call tool ${this.name} from ${this.mcpServer} with result`, result);
|
|
85
|
+
return result;
|
|
73
86
|
}
|
|
74
87
|
}
|
|
75
88
|
exports.MCPTool = MCPTool;
|
|
89
|
+
function getMCPServerName(client) {
|
|
90
|
+
const info = client.getServerVersion();
|
|
91
|
+
if (!info)
|
|
92
|
+
return undefined;
|
|
93
|
+
const { name, version } = info;
|
|
94
|
+
return `${name}@${version}`;
|
|
95
|
+
}
|
|
@@ -32,13 +32,13 @@ export declare class ExecutionEngine extends EventEmitter implements Context {
|
|
|
32
32
|
private publishUserInputTopic;
|
|
33
33
|
private runChatLoop;
|
|
34
34
|
private callAgent;
|
|
35
|
-
|
|
35
|
+
shutdown(): Promise<void>;
|
|
36
36
|
}
|
|
37
37
|
export declare class UserAgent<I extends AgentInput = AgentInput, O extends AgentOutput = AgentOutput> extends Agent<I, O> {
|
|
38
|
-
options: AgentOptions & {
|
|
38
|
+
options: AgentOptions<I, O> & {
|
|
39
39
|
run: (input: I) => Promise<O>;
|
|
40
40
|
};
|
|
41
|
-
constructor(options: AgentOptions & {
|
|
41
|
+
constructor(options: AgentOptions<I, O> & {
|
|
42
42
|
run: (input: I) => Promise<O>;
|
|
43
43
|
});
|
|
44
44
|
process(input: I): Promise<O>;
|
|
@@ -167,12 +167,12 @@ class ExecutionEngine extends node_events_1.default {
|
|
|
167
167
|
output,
|
|
168
168
|
};
|
|
169
169
|
}
|
|
170
|
-
async
|
|
170
|
+
async shutdown() {
|
|
171
171
|
for (const tool of this.tools) {
|
|
172
|
-
await tool.
|
|
172
|
+
await tool.shutdown();
|
|
173
173
|
}
|
|
174
174
|
for (const agent of this.agents) {
|
|
175
|
-
await agent.
|
|
175
|
+
await agent.shutdown();
|
|
176
176
|
}
|
|
177
177
|
}
|
|
178
178
|
}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { ChatModel, type ChatModelInput, type ChatModelOutput } from "./chat";
|
|
2
2
|
export declare class ChatModelOpenAI extends ChatModel {
|
|
3
3
|
config?: {
|
|
4
|
-
apiKey
|
|
5
|
-
model
|
|
4
|
+
apiKey?: string;
|
|
5
|
+
model?: string;
|
|
6
6
|
} | undefined;
|
|
7
7
|
constructor(config?: {
|
|
8
|
-
apiKey
|
|
9
|
-
model
|
|
8
|
+
apiKey?: string;
|
|
9
|
+
model?: string;
|
|
10
10
|
} | undefined);
|
|
11
11
|
private _client?;
|
|
12
12
|
private get client();
|
|
@@ -56,6 +56,8 @@ class ChatModelOpenAI extends chat_1.ChatModel {
|
|
|
56
56
|
args: "",
|
|
57
57
|
};
|
|
58
58
|
const c = toolCalls[call.index];
|
|
59
|
+
if (!c)
|
|
60
|
+
throw new Error("Tool call not found");
|
|
59
61
|
if (call.type)
|
|
60
62
|
c.type = call.type;
|
|
61
63
|
c.function.name = c.function.name + (call.function?.name || "");
|
|
@@ -146,9 +148,10 @@ function jsonSchemaToOpenAIJsonSchema(schema) {
|
|
|
146
148
|
};
|
|
147
149
|
}
|
|
148
150
|
if (schema?.type === "array") {
|
|
151
|
+
const { items } = schema;
|
|
149
152
|
return {
|
|
150
153
|
...schema,
|
|
151
|
-
items: jsonSchemaToOpenAIJsonSchema(
|
|
154
|
+
items: jsonSchemaToOpenAIJsonSchema(items),
|
|
152
155
|
};
|
|
153
156
|
}
|
|
154
157
|
return schema;
|
package/lib/cjs/models/chat.d.ts
CHANGED
|
@@ -3,21 +3,30 @@ import type { AIAgent } from "../agents/ai-agent";
|
|
|
3
3
|
import type { Context } from "../execution-engine/context";
|
|
4
4
|
import type { ChatModel, ChatModelInput, ChatModelInputMessage } from "../models/chat";
|
|
5
5
|
export declare const USER_INPUT_MESSAGE_KEY = "$user_input_message";
|
|
6
|
-
export declare function userInput(message: string | object):
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
};
|
|
6
|
+
export declare function userInput(message: string | object): AgentInput;
|
|
7
|
+
export declare function addMessagesToInput(input: AgentInput, messages: ChatModelInputMessage[]): AgentInput;
|
|
8
|
+
export interface PromptBuilderOptions {
|
|
9
|
+
instructions?: string;
|
|
10
|
+
}
|
|
12
11
|
export interface PromptBuilderBuildOptions {
|
|
13
12
|
context?: Context;
|
|
14
|
-
agent
|
|
15
|
-
input
|
|
13
|
+
agent?: AIAgent;
|
|
14
|
+
input?: AgentInput;
|
|
16
15
|
model?: ChatModel;
|
|
17
16
|
}
|
|
18
17
|
export declare class PromptBuilder {
|
|
18
|
+
static from(instructions: string): PromptBuilder;
|
|
19
|
+
static from(instructions: {
|
|
20
|
+
path: string;
|
|
21
|
+
}): Promise<PromptBuilder>;
|
|
22
|
+
static from(instructions: string | {
|
|
23
|
+
path: string;
|
|
24
|
+
}): PromptBuilder | Promise<PromptBuilder>;
|
|
25
|
+
private static fromFile;
|
|
26
|
+
constructor(options?: PromptBuilderOptions);
|
|
27
|
+
instructions?: string;
|
|
19
28
|
build(options: PromptBuilderBuildOptions): Promise<ChatModelInput & {
|
|
20
|
-
toolAgents
|
|
29
|
+
toolAgents?: Agent[];
|
|
21
30
|
}>;
|
|
22
31
|
private buildMessages;
|
|
23
32
|
private buildResponseFormat;
|
|
@@ -6,11 +6,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.PromptBuilder = exports.USER_INPUT_MESSAGE_KEY = void 0;
|
|
7
7
|
exports.userInput = userInput;
|
|
8
8
|
exports.addMessagesToInput = addMessagesToInput;
|
|
9
|
+
const promises_1 = require("node:fs/promises");
|
|
9
10
|
const zod_1 = require("zod");
|
|
10
11
|
const zod_to_json_schema_1 = __importDefault(require("zod-to-json-schema"));
|
|
11
12
|
const agent_1 = require("../agents/agent");
|
|
12
13
|
const template_1 = require("./template");
|
|
13
|
-
const instructions_1 = require("./templates/instructions");
|
|
14
14
|
exports.USER_INPUT_MESSAGE_KEY = "$user_input_message";
|
|
15
15
|
function userInput(message) {
|
|
16
16
|
return { [exports.USER_INPUT_MESSAGE_KEY]: message };
|
|
@@ -35,6 +35,20 @@ function addMessagesToInput(input, messages) {
|
|
|
35
35
|
return { ...input, [exports.USER_INPUT_MESSAGE_KEY]: newMessages };
|
|
36
36
|
}
|
|
37
37
|
class PromptBuilder {
|
|
38
|
+
static from(instructions) {
|
|
39
|
+
if (typeof instructions === "string") {
|
|
40
|
+
return new PromptBuilder({ instructions });
|
|
41
|
+
}
|
|
42
|
+
return PromptBuilder.fromFile(instructions.path);
|
|
43
|
+
}
|
|
44
|
+
static async fromFile(path) {
|
|
45
|
+
const text = await (0, promises_1.readFile)(path, "utf-8");
|
|
46
|
+
return PromptBuilder.from(text);
|
|
47
|
+
}
|
|
48
|
+
constructor(options) {
|
|
49
|
+
this.instructions = options?.instructions;
|
|
50
|
+
}
|
|
51
|
+
instructions;
|
|
38
52
|
async build(options) {
|
|
39
53
|
return {
|
|
40
54
|
messages: this.buildMessages(options),
|
|
@@ -43,13 +57,11 @@ class PromptBuilder {
|
|
|
43
57
|
};
|
|
44
58
|
}
|
|
45
59
|
buildMessages(options) {
|
|
46
|
-
const {
|
|
60
|
+
const { input } = options;
|
|
47
61
|
const template = template_1.ChatMessagesTemplate.from([]);
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
})));
|
|
52
|
-
const userInputMessage = input[exports.USER_INPUT_MESSAGE_KEY];
|
|
62
|
+
if (this.instructions)
|
|
63
|
+
template.messages.push(template_1.SystemMessageTemplate.from(this.instructions));
|
|
64
|
+
const userInputMessage = input?.[exports.USER_INPUT_MESSAGE_KEY];
|
|
53
65
|
if (typeof userInputMessage === "string") {
|
|
54
66
|
template.messages.push(template_1.UserMessageTemplate.from(userInputMessage));
|
|
55
67
|
}
|
|
@@ -63,7 +75,9 @@ class PromptBuilder {
|
|
|
63
75
|
return template.format(input);
|
|
64
76
|
}
|
|
65
77
|
buildResponseFormat(options) {
|
|
66
|
-
const
|
|
78
|
+
const outputSchema = options.agent?.outputSchema;
|
|
79
|
+
if (!outputSchema)
|
|
80
|
+
return undefined;
|
|
67
81
|
const isJsonOutput = !isEmptyObjectType(outputSchema);
|
|
68
82
|
return isJsonOutput
|
|
69
83
|
? {
|
|
@@ -77,10 +91,10 @@ class PromptBuilder {
|
|
|
77
91
|
: undefined;
|
|
78
92
|
}
|
|
79
93
|
buildTools(options) {
|
|
80
|
-
const toolAgents = options.
|
|
81
|
-
.concat(options.
|
|
82
|
-
// TODO: support nested tools
|
|
83
|
-
.flatMap((i) => (i.isCallable ? i.
|
|
94
|
+
const toolAgents = (options.context?.tools ?? [])
|
|
95
|
+
.concat(options.agent?.tools ?? [])
|
|
96
|
+
// TODO: support nested tools?
|
|
97
|
+
.flatMap((i) => (i.isCallable ? i.tools.concat(i) : i.tools));
|
|
84
98
|
const tools = toolAgents.map((i) => ({
|
|
85
99
|
type: "function",
|
|
86
100
|
function: {
|
|
@@ -91,7 +105,7 @@ class PromptBuilder {
|
|
|
91
105
|
}));
|
|
92
106
|
let toolChoice;
|
|
93
107
|
// use manual choice if configured in the agent
|
|
94
|
-
const
|
|
108
|
+
const manualChoice = options.agent?.toolChoice;
|
|
95
109
|
if (manualChoice) {
|
|
96
110
|
if (manualChoice instanceof agent_1.Agent) {
|
|
97
111
|
toolChoice = {
|
|
@@ -114,8 +128,8 @@ class PromptBuilder {
|
|
|
114
128
|
toolChoice = tools.length ? "auto" : undefined;
|
|
115
129
|
}
|
|
116
130
|
return {
|
|
117
|
-
toolAgents,
|
|
118
|
-
tools,
|
|
131
|
+
toolAgents: toolAgents.length ? toolAgents : undefined,
|
|
132
|
+
tools: tools.length ? tools : undefined,
|
|
119
133
|
toolChoice,
|
|
120
134
|
};
|
|
121
135
|
}
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
export declare const logger:
|
|
3
|
-
|
|
1
|
+
import debug from "debug";
|
|
2
|
+
export declare const logger: {
|
|
3
|
+
base: debug.Debugger;
|
|
4
|
+
debug: debug.Debugger;
|
|
5
|
+
error: debug.Debugger;
|
|
6
|
+
};
|
package/lib/cjs/utils/logger.js
CHANGED
|
@@ -1,15 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
6
|
exports.logger = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
info: level === "debug" || level === "info" ? logger.info : () => { },
|
|
11
|
-
warn: level === "debug" || level === "info" || level === "warn" ? logger.warn : () => { },
|
|
12
|
-
error: logger.error,
|
|
13
|
-
};
|
|
14
|
-
}
|
|
15
|
-
exports.logger = createLogger(console);
|
|
7
|
+
const debug_1 = __importDefault(require("debug"));
|
|
8
|
+
exports.logger = {
|
|
9
|
+
base: (0, debug_1.default)("@aigne/core"),
|
|
10
|
+
debug: (0, debug_1.default)("@aigne/core:debug"),
|
|
11
|
+
error: (0, debug_1.default)("@aigne/core:error"),
|
|
12
|
+
};
|
|
@@ -1,41 +1,47 @@
|
|
|
1
1
|
import EventEmitter from "node:events";
|
|
2
|
-
import { type ZodObject } from "zod";
|
|
2
|
+
import { type ZodObject, type ZodType } from "zod";
|
|
3
3
|
import type { Context } from "../execution-engine/context";
|
|
4
4
|
import { type TransferAgentOutput } from "./types";
|
|
5
5
|
export type AgentInput = Record<string, unknown>;
|
|
6
6
|
export type AgentOutput = Record<string, unknown> & Partial<TransferAgentOutput>;
|
|
7
7
|
export type SubscribeTopic = string | string[];
|
|
8
8
|
export type PublishTopic<O extends AgentOutput = AgentOutput> = string | string[] | ((output: O) => string | string[] | Promise<string | string[]>);
|
|
9
|
-
export interface AgentOptions<
|
|
9
|
+
export interface AgentOptions<I extends AgentInput = AgentInput, O extends AgentOutput = AgentOutput> {
|
|
10
10
|
subscribeTopic?: SubscribeTopic;
|
|
11
11
|
publishTopic?: PublishTopic<O>;
|
|
12
12
|
name?: string;
|
|
13
13
|
description?: string;
|
|
14
|
-
inputSchema?: ZodObject<
|
|
15
|
-
|
|
14
|
+
inputSchema?: ZodObject<{
|
|
15
|
+
[key in keyof I]: ZodType;
|
|
16
|
+
}>;
|
|
17
|
+
outputSchema?: ZodObject<{
|
|
18
|
+
[key in keyof O]: ZodType;
|
|
19
|
+
}>;
|
|
16
20
|
includeInputInOutput?: boolean;
|
|
17
21
|
tools?: (Agent | FunctionAgentFn)[];
|
|
18
|
-
skills?: (Agent | FunctionAgentFn)[];
|
|
19
22
|
}
|
|
20
23
|
export declare class Agent<I extends AgentInput = AgentInput, O extends AgentOutput = AgentOutput> extends EventEmitter {
|
|
21
24
|
static from<I extends AgentInput = AgentInput, O extends AgentOutput = AgentOutput>(options: AgentOptions<I, O>): Agent<I, O>;
|
|
22
25
|
constructor(options: AgentOptions<I, O>);
|
|
23
|
-
name: string;
|
|
24
|
-
description?: string;
|
|
25
|
-
inputSchema: ZodObject<
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
readonly
|
|
26
|
+
readonly name: string;
|
|
27
|
+
readonly description?: string;
|
|
28
|
+
readonly inputSchema: ZodObject<{
|
|
29
|
+
[key in keyof I]: ZodType;
|
|
30
|
+
}>;
|
|
31
|
+
readonly outputSchema: ZodObject<{
|
|
32
|
+
[key in keyof O]: ZodType;
|
|
33
|
+
}>;
|
|
34
|
+
readonly includeInputInOutput?: boolean;
|
|
35
|
+
readonly subscribeTopic?: SubscribeTopic;
|
|
36
|
+
readonly publishTopic?: PublishTopic<AgentOutput>;
|
|
37
|
+
readonly tools: Agent<AgentInput, AgentOutput>[] & {
|
|
32
38
|
[key: string]: Agent<AgentInput, AgentOutput>;
|
|
33
|
-
}
|
|
34
|
-
|
|
39
|
+
};
|
|
40
|
+
addTool<I extends AgentInput, O extends AgentOutput>(tool: Agent<I, O> | FunctionAgentFn<I, O>): void;
|
|
35
41
|
get isCallable(): boolean;
|
|
36
42
|
call(input: I | string, context?: Context): Promise<O>;
|
|
37
43
|
process?(input: I, context?: Context): Promise<O>;
|
|
38
|
-
|
|
44
|
+
shutdown(): Promise<void>;
|
|
39
45
|
}
|
|
40
46
|
export interface FunctionAgentOptions<I extends AgentInput = AgentInput, O extends AgentOutput = AgentOutput> extends AgentOptions<I, O> {
|
|
41
47
|
fn?: FunctionAgentFn<I, O>;
|
|
@@ -4,7 +4,7 @@ import { PromptBuilder } from "../prompt/prompt-builder";
|
|
|
4
4
|
import { Agent, type AgentInput, type AgentOptions, type AgentOutput } from "./agent";
|
|
5
5
|
export interface AIAgentOptions<I extends AgentInput = AgentInput, O extends AgentOutput = AgentOutput> extends AgentOptions<I, O> {
|
|
6
6
|
model?: ChatModel;
|
|
7
|
-
instructions?: string;
|
|
7
|
+
instructions?: string | PromptBuilder;
|
|
8
8
|
outputKey?: string;
|
|
9
9
|
toolChoice?: AIAgentToolChoice;
|
|
10
10
|
}
|
|
@@ -13,8 +13,7 @@ export declare class AIAgent<I extends AgentInput = AgentInput, O extends AgentO
|
|
|
13
13
|
static from<I extends AgentInput, O extends AgentOutput>(options: AIAgentOptions<I, O>): AIAgent<I, O>;
|
|
14
14
|
constructor(options: AIAgentOptions<I, O>);
|
|
15
15
|
model?: ChatModel;
|
|
16
|
-
|
|
17
|
-
instructions?: string;
|
|
16
|
+
instructions: PromptBuilder;
|
|
18
17
|
outputKey?: string;
|
|
19
18
|
toolChoice?: AIAgentToolChoice;
|
|
20
19
|
process(input: I, context?: Context): Promise<O>;
|
|
@@ -14,7 +14,7 @@ export declare class MCPAgent extends Agent {
|
|
|
14
14
|
private static fromTransport;
|
|
15
15
|
constructor(options: MCPAgentOptions);
|
|
16
16
|
private client;
|
|
17
|
-
|
|
17
|
+
shutdown(): Promise<void>;
|
|
18
18
|
}
|
|
19
19
|
export interface MCPToolOptions extends AgentOptions {
|
|
20
20
|
client: Client;
|
|
@@ -22,5 +22,6 @@ export interface MCPToolOptions extends AgentOptions {
|
|
|
22
22
|
export declare class MCPTool extends Agent {
|
|
23
23
|
constructor(options: MCPToolOptions);
|
|
24
24
|
private client;
|
|
25
|
+
private get mcpServer();
|
|
25
26
|
process(input: AgentInput): Promise<AgentOutput>;
|
|
26
27
|
}
|
|
@@ -32,13 +32,13 @@ export declare class ExecutionEngine extends EventEmitter implements Context {
|
|
|
32
32
|
private publishUserInputTopic;
|
|
33
33
|
private runChatLoop;
|
|
34
34
|
private callAgent;
|
|
35
|
-
|
|
35
|
+
shutdown(): Promise<void>;
|
|
36
36
|
}
|
|
37
37
|
export declare class UserAgent<I extends AgentInput = AgentInput, O extends AgentOutput = AgentOutput> extends Agent<I, O> {
|
|
38
|
-
options: AgentOptions & {
|
|
38
|
+
options: AgentOptions<I, O> & {
|
|
39
39
|
run: (input: I) => Promise<O>;
|
|
40
40
|
};
|
|
41
|
-
constructor(options: AgentOptions & {
|
|
41
|
+
constructor(options: AgentOptions<I, O> & {
|
|
42
42
|
run: (input: I) => Promise<O>;
|
|
43
43
|
});
|
|
44
44
|
process(input: I): Promise<O>;
|