@aigne/core 1.6.0 → 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.
- package/CHANGELOG.md +14 -0
- package/lib/cjs/agents/agent.d.ts +5 -2
- package/lib/cjs/agents/agent.js +42 -24
- package/lib/cjs/agents/ai-agent.d.ts +8 -8
- package/lib/cjs/agents/ai-agent.js +5 -2
- package/lib/cjs/agents/mcp-agent.d.ts +11 -0
- package/lib/cjs/agents/mcp-agent.js +37 -23
- package/lib/cjs/agents/user-agent.d.ts +9 -9
- package/lib/cjs/agents/user-agent.js +26 -16
- package/lib/cjs/execution-engine/context.d.ts +84 -46
- package/lib/cjs/execution-engine/context.js +136 -98
- package/lib/cjs/execution-engine/execution-engine.d.ts +16 -47
- package/lib/cjs/execution-engine/execution-engine.js +13 -40
- package/lib/cjs/execution-engine/message-queue.d.ts +3 -3
- package/lib/cjs/execution-engine/message-queue.js +32 -2
- package/lib/cjs/execution-engine/usage.d.ts +11 -0
- package/lib/cjs/execution-engine/usage.js +10 -0
- package/lib/cjs/loader/index.js +1 -1
- package/lib/cjs/models/chat-model.d.ts +3 -2
- package/lib/cjs/models/chat-model.js +6 -5
- package/lib/cjs/models/claude-chat-model.js +10 -7
- package/lib/cjs/models/openai-chat-model.js +5 -2
- package/lib/cjs/prompt/prompt-builder.d.ts +1 -1
- package/lib/cjs/prompt/prompt-builder.js +3 -1
- package/lib/cjs/utils/json-schema.js +2 -2
- package/lib/cjs/utils/logger.d.ts +3 -15
- package/lib/cjs/utils/logger.js +3 -77
- package/lib/cjs/utils/mcp-utils.js +1 -5
- package/lib/cjs/utils/model-utils.js +2 -2
- package/lib/cjs/utils/type-utils.d.ts +1 -0
- package/lib/cjs/utils/typed-event-emtter.d.ts +10 -0
- package/lib/cjs/utils/typed-event-emtter.js +2 -0
- package/lib/dts/agents/agent.d.ts +5 -2
- package/lib/dts/agents/ai-agent.d.ts +8 -8
- package/lib/dts/agents/mcp-agent.d.ts +11 -0
- package/lib/dts/agents/user-agent.d.ts +9 -9
- package/lib/dts/execution-engine/context.d.ts +84 -46
- package/lib/dts/execution-engine/execution-engine.d.ts +16 -47
- package/lib/dts/execution-engine/message-queue.d.ts +3 -3
- package/lib/dts/execution-engine/usage.d.ts +11 -0
- package/lib/dts/models/chat-model.d.ts +3 -2
- package/lib/dts/prompt/prompt-builder.d.ts +1 -1
- package/lib/dts/utils/logger.d.ts +3 -15
- package/lib/dts/utils/type-utils.d.ts +1 -0
- package/lib/dts/utils/typed-event-emtter.d.ts +10 -0
- package/lib/esm/agents/agent.d.ts +5 -2
- package/lib/esm/agents/agent.js +42 -24
- package/lib/esm/agents/ai-agent.d.ts +8 -8
- package/lib/esm/agents/ai-agent.js +5 -2
- package/lib/esm/agents/mcp-agent.d.ts +11 -0
- package/lib/esm/agents/mcp-agent.js +38 -24
- package/lib/esm/agents/user-agent.d.ts +9 -9
- package/lib/esm/agents/user-agent.js +26 -16
- package/lib/esm/execution-engine/context.d.ts +84 -46
- package/lib/esm/execution-engine/context.js +135 -98
- package/lib/esm/execution-engine/execution-engine.d.ts +16 -47
- package/lib/esm/execution-engine/execution-engine.js +14 -38
- package/lib/esm/execution-engine/message-queue.d.ts +3 -3
- package/lib/esm/execution-engine/message-queue.js +33 -3
- package/lib/esm/execution-engine/usage.d.ts +11 -0
- package/lib/esm/execution-engine/usage.js +7 -0
- package/lib/esm/loader/index.js +2 -2
- package/lib/esm/models/chat-model.d.ts +3 -2
- package/lib/esm/models/chat-model.js +6 -5
- package/lib/esm/models/claude-chat-model.js +10 -7
- package/lib/esm/models/openai-chat-model.js +5 -2
- package/lib/esm/prompt/prompt-builder.d.ts +1 -1
- package/lib/esm/prompt/prompt-builder.js +3 -1
- package/lib/esm/utils/json-schema.js +2 -2
- package/lib/esm/utils/logger.d.ts +3 -15
- package/lib/esm/utils/logger.js +3 -77
- package/lib/esm/utils/mcp-utils.js +1 -5
- package/lib/esm/utils/model-utils.js +2 -2
- package/lib/esm/utils/type-utils.d.ts +1 -0
- package/lib/esm/utils/typed-event-emtter.d.ts +10 -0
- package/lib/esm/utils/typed-event-emtter.js +1 -0
- package/package.json +7 -8
- package/lib/cjs/utils/run-chat-loop.d.ts +0 -11
- package/lib/cjs/utils/run-chat-loop.js +0 -82
- package/lib/dts/utils/run-chat-loop.d.ts +0 -11
- package/lib/esm/utils/run-chat-loop.d.ts +0 -11
- package/lib/esm/utils/run-chat-loop.js +0 -76
|
@@ -2,20 +2,29 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.MessageQueue = exports.UserOutputTopic = exports.UserInputTopic = void 0;
|
|
4
4
|
const node_events_1 = require("node:events");
|
|
5
|
+
const zod_1 = require("zod");
|
|
5
6
|
const type_utils_js_1 = require("../utils/type-utils.js");
|
|
6
7
|
exports.UserInputTopic = "UserInputTopic";
|
|
7
8
|
exports.UserOutputTopic = "UserOutputTopic";
|
|
8
9
|
class MessageQueue {
|
|
9
10
|
events = new node_events_1.EventEmitter();
|
|
10
|
-
publish(topic,
|
|
11
|
+
publish(topic, payload) {
|
|
12
|
+
(0, type_utils_js_1.checkArguments)("MessageQueue.publish", publishArgsSchema, {
|
|
13
|
+
topic,
|
|
14
|
+
payload,
|
|
15
|
+
});
|
|
11
16
|
for (const t of (0, type_utils_js_1.orArrayToArray)(topic)) {
|
|
12
|
-
this.events.emit(t,
|
|
17
|
+
this.events.emit(t, payload);
|
|
13
18
|
}
|
|
14
19
|
}
|
|
15
20
|
error(error) {
|
|
16
21
|
this.events.emit("error", error);
|
|
17
22
|
}
|
|
18
23
|
subscribe(topic, listener) {
|
|
24
|
+
(0, type_utils_js_1.checkArguments)("MessageQueue.subscribe", subscribeArgsSchema, {
|
|
25
|
+
topic,
|
|
26
|
+
listener,
|
|
27
|
+
});
|
|
19
28
|
if (!listener) {
|
|
20
29
|
return new Promise((resolve, reject) => {
|
|
21
30
|
const unsubscribe1 = once(this.events, topic, (message) => {
|
|
@@ -31,6 +40,10 @@ class MessageQueue {
|
|
|
31
40
|
return on(this.events, topic, listener);
|
|
32
41
|
}
|
|
33
42
|
unsubscribe(topic, listener) {
|
|
43
|
+
(0, type_utils_js_1.checkArguments)("MessageQueue.unsubscribe", unsubscribeArgsSchema, {
|
|
44
|
+
topic,
|
|
45
|
+
listener,
|
|
46
|
+
});
|
|
34
47
|
this.events.off(topic, listener);
|
|
35
48
|
}
|
|
36
49
|
}
|
|
@@ -43,3 +56,20 @@ function once(events, event, listener) {
|
|
|
43
56
|
events.once(event, listener);
|
|
44
57
|
return () => events.off(event, listener);
|
|
45
58
|
}
|
|
59
|
+
const subscribeArgsSchema = zod_1.z.object({
|
|
60
|
+
topic: zod_1.z.string(),
|
|
61
|
+
listener: zod_1.z.function(zod_1.z.tuple([zod_1.z.any()]), zod_1.z.any()).optional(),
|
|
62
|
+
});
|
|
63
|
+
const unsubscribeArgsSchema = zod_1.z.object({
|
|
64
|
+
topic: zod_1.z.string(),
|
|
65
|
+
listener: zod_1.z.function(zod_1.z.tuple([zod_1.z.any()]), zod_1.z.any()),
|
|
66
|
+
});
|
|
67
|
+
const publishArgsSchema = zod_1.z.object({
|
|
68
|
+
topic: zod_1.z.union([zod_1.z.string(), zod_1.z.array(zod_1.z.string())]),
|
|
69
|
+
payload: zod_1.z.object({
|
|
70
|
+
role: zod_1.z.union([zod_1.z.literal("user"), zod_1.z.literal("agent")]),
|
|
71
|
+
source: zod_1.z.string().optional(),
|
|
72
|
+
message: zod_1.z.union([zod_1.z.string(), zod_1.z.record(zod_1.z.unknown())]),
|
|
73
|
+
context: zod_1.z.any(),
|
|
74
|
+
}),
|
|
75
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export interface ContextUsage {
|
|
2
|
+
inputTokens: number;
|
|
3
|
+
outputTokens: number;
|
|
4
|
+
agentCalls: number;
|
|
5
|
+
}
|
|
6
|
+
export declare function newEmptyContextUsage(): ContextUsage;
|
|
7
|
+
export interface ContextLimits {
|
|
8
|
+
maxTokens?: number;
|
|
9
|
+
maxAgentCalls?: number;
|
|
10
|
+
timeout?: number;
|
|
11
|
+
}
|
package/lib/cjs/loader/index.js
CHANGED
|
@@ -120,7 +120,7 @@ async function getAIGNEFilePath(path) {
|
|
|
120
120
|
if (s.isDirectory()) {
|
|
121
121
|
for (const file of AIGNE_FILE_NAME) {
|
|
122
122
|
const filePath = (0, node_path_1.join)(path, file);
|
|
123
|
-
if (await (0, promises_1.
|
|
123
|
+
if ((await (0, promises_1.stat)(filePath)).isFile())
|
|
124
124
|
return filePath;
|
|
125
125
|
}
|
|
126
126
|
}
|
|
@@ -75,6 +75,7 @@ export interface ChatModelOutput extends Message {
|
|
|
75
75
|
json?: object;
|
|
76
76
|
toolCalls?: ChatModelOutputToolCall[];
|
|
77
77
|
usage?: ChatModelOutputUsage;
|
|
78
|
+
model?: string;
|
|
78
79
|
}
|
|
79
80
|
export interface ChatModelOutputToolCall {
|
|
80
81
|
id: string;
|
|
@@ -85,6 +86,6 @@ export interface ChatModelOutputToolCall {
|
|
|
85
86
|
};
|
|
86
87
|
}
|
|
87
88
|
export interface ChatModelOutputUsage {
|
|
88
|
-
|
|
89
|
-
|
|
89
|
+
inputTokens: number;
|
|
90
|
+
outputTokens: number;
|
|
90
91
|
}
|
|
@@ -13,7 +13,7 @@ class ChatModel extends agent_js_1.Agent {
|
|
|
13
13
|
preprocess(input, context) {
|
|
14
14
|
super.preprocess(input, context);
|
|
15
15
|
const { limits, usage } = context;
|
|
16
|
-
const usedTokens = usage.
|
|
16
|
+
const usedTokens = usage.outputTokens + usage.inputTokens;
|
|
17
17
|
if (limits?.maxTokens && usedTokens >= limits.maxTokens) {
|
|
18
18
|
throw new Error(`Exceeded max tokens ${usedTokens}/${limits.maxTokens}`);
|
|
19
19
|
}
|
|
@@ -22,8 +22,8 @@ class ChatModel extends agent_js_1.Agent {
|
|
|
22
22
|
super.postprocess(input, output, context);
|
|
23
23
|
const { usage } = output;
|
|
24
24
|
if (usage) {
|
|
25
|
-
context.usage.
|
|
26
|
-
context.usage.
|
|
25
|
+
context.usage.outputTokens += usage.outputTokens;
|
|
26
|
+
context.usage.inputTokens += usage.inputTokens;
|
|
27
27
|
}
|
|
28
28
|
}
|
|
29
29
|
}
|
|
@@ -102,12 +102,13 @@ const chatModelOutputToolCallSchema = zod_1.z.object({
|
|
|
102
102
|
}),
|
|
103
103
|
});
|
|
104
104
|
const chatModelOutputUsageSchema = zod_1.z.object({
|
|
105
|
-
|
|
106
|
-
|
|
105
|
+
inputTokens: zod_1.z.number(),
|
|
106
|
+
outputTokens: zod_1.z.number(),
|
|
107
107
|
});
|
|
108
108
|
const chatModelOutputSchema = zod_1.z.object({
|
|
109
109
|
text: zod_1.z.string().optional(),
|
|
110
110
|
json: zod_1.z.record(zod_1.z.unknown()).optional(),
|
|
111
111
|
toolCalls: zod_1.z.array(chatModelOutputToolCallSchema).optional(),
|
|
112
112
|
usage: chatModelOutputUsageSchema.optional(),
|
|
113
|
+
model: zod_1.z.string().optional(),
|
|
113
114
|
});
|
|
@@ -81,16 +81,18 @@ class ClaudeChatModel extends chat_model_js_1.ChatModel {
|
|
|
81
81
|
let text = "";
|
|
82
82
|
const toolCalls = [];
|
|
83
83
|
let usage;
|
|
84
|
+
let model;
|
|
84
85
|
for await (const chunk of stream) {
|
|
85
86
|
if (chunk.type === "message_start") {
|
|
87
|
+
model ??= chunk.message.model;
|
|
86
88
|
const { input_tokens, output_tokens } = chunk.message.usage;
|
|
87
89
|
usage = {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
+
inputTokens: input_tokens,
|
|
91
|
+
outputTokens: output_tokens,
|
|
90
92
|
};
|
|
91
93
|
}
|
|
92
94
|
if (chunk.type === "message_delta" && usage) {
|
|
93
|
-
usage.
|
|
95
|
+
usage.outputTokens = chunk.usage.output_tokens;
|
|
94
96
|
}
|
|
95
97
|
logs.push(JSON.stringify(chunk));
|
|
96
98
|
// handle streaming text
|
|
@@ -115,7 +117,7 @@ class ClaudeChatModel extends chat_model_js_1.ChatModel {
|
|
|
115
117
|
call.args += chunk.delta.partial_json;
|
|
116
118
|
}
|
|
117
119
|
}
|
|
118
|
-
const result = { usage, text };
|
|
120
|
+
const result = { usage, model, text };
|
|
119
121
|
if (toolCalls.length) {
|
|
120
122
|
result.toolCalls = toolCalls
|
|
121
123
|
.map(({ args, ...c }) => ({
|
|
@@ -131,7 +133,7 @@ class ClaudeChatModel extends chat_model_js_1.ChatModel {
|
|
|
131
133
|
return result;
|
|
132
134
|
}
|
|
133
135
|
catch (error) {
|
|
134
|
-
logger_js_1.logger.
|
|
136
|
+
logger_js_1.logger.core("Failed to process Claude stream", { error, logs });
|
|
135
137
|
throw error;
|
|
136
138
|
}
|
|
137
139
|
}
|
|
@@ -160,9 +162,10 @@ class ClaudeChatModel extends chat_model_js_1.ChatModel {
|
|
|
160
162
|
throw new Error("Json tool not found");
|
|
161
163
|
return {
|
|
162
164
|
json: jsonTool.input,
|
|
165
|
+
model: result.model,
|
|
163
166
|
usage: {
|
|
164
|
-
|
|
165
|
-
|
|
167
|
+
inputTokens: result.usage.input_tokens,
|
|
168
|
+
outputTokens: result.usage.output_tokens,
|
|
166
169
|
},
|
|
167
170
|
};
|
|
168
171
|
}
|
|
@@ -78,8 +78,10 @@ class OpenAIChatModel extends chat_model_js_1.ChatModel {
|
|
|
78
78
|
let text = "";
|
|
79
79
|
const toolCalls = [];
|
|
80
80
|
let usage;
|
|
81
|
+
let model;
|
|
81
82
|
for await (const chunk of res) {
|
|
82
83
|
const choice = chunk.choices?.[0];
|
|
84
|
+
model ??= chunk.model;
|
|
83
85
|
if (choice?.delta.tool_calls?.length) {
|
|
84
86
|
for (const call of choice.delta.tool_calls) {
|
|
85
87
|
toolCalls[call.index] ??= {
|
|
@@ -101,13 +103,14 @@ class OpenAIChatModel extends chat_model_js_1.ChatModel {
|
|
|
101
103
|
text += choice.delta.content;
|
|
102
104
|
if (chunk.usage) {
|
|
103
105
|
usage = {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
+
inputTokens: chunk.usage.prompt_tokens,
|
|
107
|
+
outputTokens: chunk.usage.completion_tokens,
|
|
106
108
|
};
|
|
107
109
|
}
|
|
108
110
|
}
|
|
109
111
|
const result = {
|
|
110
112
|
usage,
|
|
113
|
+
model,
|
|
111
114
|
};
|
|
112
115
|
if (input.responseFormat?.type === "json_schema" && text) {
|
|
113
116
|
result.json = (0, json_schema_js_1.parseJSON)(text);
|
|
@@ -7,7 +7,7 @@ import type { ChatModel, ChatModelInput } from "../models/chat-model.js";
|
|
|
7
7
|
import { ChatMessagesTemplate } from "./template.js";
|
|
8
8
|
export declare const MESSAGE_KEY = "$message";
|
|
9
9
|
export declare const DEFAULT_MAX_HISTORY_MESSAGES = 10;
|
|
10
|
-
export declare function createMessage(message: string |
|
|
10
|
+
export declare function createMessage<I extends Message>(message: string | I): I;
|
|
11
11
|
export declare function getMessage(input: Message): string | undefined;
|
|
12
12
|
export interface PromptBuilderOptions {
|
|
13
13
|
instructions?: string | ChatMessagesTemplate;
|
|
@@ -12,7 +12,9 @@ const template_js_1 = require("./template.js");
|
|
|
12
12
|
exports.MESSAGE_KEY = "$message";
|
|
13
13
|
exports.DEFAULT_MAX_HISTORY_MESSAGES = 10;
|
|
14
14
|
function createMessage(message) {
|
|
15
|
-
return
|
|
15
|
+
return typeof message === "string"
|
|
16
|
+
? { [exports.MESSAGE_KEY]: message }
|
|
17
|
+
: { ...message };
|
|
16
18
|
}
|
|
17
19
|
function getMessage(input) {
|
|
18
20
|
const userInputMessage = input[exports.MESSAGE_KEY];
|
|
@@ -28,8 +28,8 @@ function parseJSON(json) {
|
|
|
28
28
|
return JSON.parse(json);
|
|
29
29
|
}
|
|
30
30
|
catch (error) {
|
|
31
|
-
logger_js_1.logger.
|
|
32
|
-
throw
|
|
31
|
+
logger_js_1.logger.core("Failed to parse JSON", { json, error });
|
|
32
|
+
throw error;
|
|
33
33
|
}
|
|
34
34
|
}
|
|
35
35
|
/**
|
|
@@ -1,20 +1,8 @@
|
|
|
1
|
-
import debug
|
|
2
|
-
import type { Ora } from "ora";
|
|
3
|
-
interface DebugWithSpinner extends Debugger {
|
|
4
|
-
spinner<T>(promise: Promise<T>, message?: string, callback?: (result: T) => void, options?: {
|
|
5
|
-
disabled?: boolean;
|
|
6
|
-
}): Promise<T>;
|
|
7
|
-
extend: (namespace: string) => DebugWithSpinner;
|
|
8
|
-
}
|
|
9
|
-
declare function spinner<T>(promise: Promise<T>, message?: string, callback?: (result: T) => void): Promise<T>;
|
|
1
|
+
import debug from "debug";
|
|
10
2
|
export declare const logger: debug.Debug & {
|
|
11
3
|
debug: debug.Debug;
|
|
12
4
|
default: debug.Debug;
|
|
13
5
|
} & {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
debug: DebugWithSpinner;
|
|
17
|
-
spinner: typeof spinner;
|
|
18
|
-
setSpinner: (spinner: Ora) => void;
|
|
6
|
+
core: debug.Debugger;
|
|
7
|
+
mcp: debug.Debugger;
|
|
19
8
|
};
|
|
20
|
-
export {};
|
package/lib/cjs/utils/logger.js
CHANGED
|
@@ -5,82 +5,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.logger = void 0;
|
|
7
7
|
const debug_1 = __importDefault(require("debug"));
|
|
8
|
-
|
|
9
|
-
const { isSpinning } = globalSpinner ?? {};
|
|
10
|
-
if (isSpinning)
|
|
11
|
-
globalSpinner?.stop();
|
|
12
|
-
console.log(...args);
|
|
13
|
-
if (isSpinning)
|
|
14
|
-
globalSpinner?.start();
|
|
15
|
-
};
|
|
16
|
-
function createDebugger(namespace) {
|
|
17
|
-
const i = (0, debug_1.default)(namespace);
|
|
18
|
-
function overrideExtend(debug) {
|
|
19
|
-
const originalExtend = debug.extend;
|
|
20
|
-
debug.extend = (namespace) => {
|
|
21
|
-
const extended = originalExtend.call(debug, namespace);
|
|
22
|
-
overrideExtend(extended);
|
|
23
|
-
extended.spinner = async (promise, message, callback, options) => {
|
|
24
|
-
if (!extended.enabled || options?.disabled)
|
|
25
|
-
return promise;
|
|
26
|
-
return spinner(promise, message, callback);
|
|
27
|
-
};
|
|
28
|
-
return extended;
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
overrideExtend(i);
|
|
32
|
-
return i;
|
|
33
|
-
}
|
|
34
|
-
let globalSpinner;
|
|
35
|
-
const globalSpinnerTasks = [];
|
|
36
|
-
async function spinner(promise, message, callback) {
|
|
37
|
-
const task = { promise, message, callback };
|
|
38
|
-
globalSpinnerTasks.push(task);
|
|
39
|
-
globalSpinner?.start(message || " ");
|
|
40
|
-
await promise
|
|
41
|
-
.then((result) => {
|
|
42
|
-
task.result = result;
|
|
43
|
-
task.status = "succeed";
|
|
44
|
-
})
|
|
45
|
-
.catch(() => {
|
|
46
|
-
task.status = "fail";
|
|
47
|
-
});
|
|
48
|
-
// Once the promise resolves or rejects, it updates the spinner status and processes
|
|
49
|
-
// all completed tasks in a Last-In-First-Out (LIFO) order.
|
|
50
|
-
for (;;) {
|
|
51
|
-
const task = globalSpinnerTasks.at(-1);
|
|
52
|
-
if (!task)
|
|
53
|
-
break;
|
|
54
|
-
// Recover spinner state for last running task
|
|
55
|
-
if (!task.status) {
|
|
56
|
-
globalSpinner?.start(task.message || " ");
|
|
57
|
-
break;
|
|
58
|
-
}
|
|
59
|
-
globalSpinnerTasks.pop();
|
|
60
|
-
if (task.message) {
|
|
61
|
-
if (task.status === "fail")
|
|
62
|
-
globalSpinner?.fail(task.message);
|
|
63
|
-
else
|
|
64
|
-
globalSpinner?.succeed(task.message);
|
|
65
|
-
}
|
|
66
|
-
else {
|
|
67
|
-
globalSpinner?.stop();
|
|
68
|
-
}
|
|
69
|
-
// NOTE: This is a workaround to make sure the spinner stops spinning before the next tick
|
|
70
|
-
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
71
|
-
if (task.status === "succeed")
|
|
72
|
-
task.callback?.(task.result);
|
|
73
|
-
}
|
|
74
|
-
return promise;
|
|
75
|
-
}
|
|
76
|
-
const base = createDebugger("aigne");
|
|
8
|
+
const base = (0, debug_1.default)("aigne");
|
|
77
9
|
exports.logger = Object.assign(debug_1.default, {
|
|
78
|
-
|
|
79
|
-
base,
|
|
80
|
-
debug: base.extend("core"),
|
|
81
|
-
spinner,
|
|
82
|
-
setSpinner: (spinner) => {
|
|
83
|
-
globalSpinner = spinner;
|
|
84
|
-
exports.logger.globalSpinner = spinner;
|
|
85
|
-
},
|
|
10
|
+
core: base.extend("core"),
|
|
11
|
+
mcp: base.extend("mcp"),
|
|
86
12
|
});
|
|
@@ -33,11 +33,7 @@ function promptFromMCPPrompt(prompt, options) {
|
|
|
33
33
|
}
|
|
34
34
|
function resourceFromMCPResource(resource, options) {
|
|
35
35
|
const [uri, variables] = isResourceTemplate(resource)
|
|
36
|
-
? [
|
|
37
|
-
resource.uriTemplate,
|
|
38
|
-
// TODO: use template.variableNames when it's available https://github.com/modelcontextprotocol/typescript-sdk/pull/188
|
|
39
|
-
new uriTemplate_js_1.UriTemplate(resource.uriTemplate).parts.flatMap((i) => (typeof i === "object" ? i.names : [])),
|
|
40
|
-
]
|
|
36
|
+
? [resource.uriTemplate, new uriTemplate_js_1.UriTemplate(resource.uriTemplate).variableNames]
|
|
41
37
|
: [resource.uri, []];
|
|
42
38
|
return new mcp_agent_js_1.MCPResource({
|
|
43
39
|
...options,
|
|
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.mergeUsage = mergeUsage;
|
|
4
4
|
function mergeUsage(...usages) {
|
|
5
5
|
return {
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
inputTokens: usages.reduce((acc, usage) => (usage ? acc + usage.inputTokens : acc), 0),
|
|
7
|
+
outputTokens: usages.reduce((acc, usage) => (usage ? acc + usage.outputTokens : acc), 0),
|
|
8
8
|
};
|
|
9
9
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { type ZodType } from "zod";
|
|
2
2
|
export type PromiseOrValue<T> = T | Promise<T>;
|
|
3
3
|
export type Nullish<T> = T | null | undefined;
|
|
4
|
+
export type OmitPropertiesFromArrayFirstElement<T extends unknown[], K extends string | number | symbol> = T extends [infer U, ...infer Rest] ? [Omit<U, K>, ...Rest] : never;
|
|
4
5
|
export declare function isNil(value: unknown): value is null | undefined;
|
|
5
6
|
export declare function isEmpty(obj: unknown): boolean;
|
|
6
7
|
export declare function isNonNullable<T>(value: T): value is NonNullable<T>;
|
|
@@ -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 {};
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { inspect } from "node:util";
|
|
1
2
|
import { type ZodType } from "zod";
|
|
2
3
|
import type { Context } from "../execution-engine/context.js";
|
|
3
4
|
import { type Nullish, type PromiseOrValue } from "../utils/type-utils.js";
|
|
@@ -15,7 +16,7 @@ export interface AgentOptions<I extends Message = Message, O extends Message = M
|
|
|
15
16
|
outputSchema?: AgentInputOutputSchema<O>;
|
|
16
17
|
includeInputInOutput?: boolean;
|
|
17
18
|
tools?: (Agent | FunctionAgentFn)[];
|
|
18
|
-
|
|
19
|
+
disableEvents?: boolean;
|
|
19
20
|
memory?: AgentMemory | AgentMemoryOptions | true;
|
|
20
21
|
}
|
|
21
22
|
export declare abstract class Agent<I extends Message = Message, O extends Message = Message> {
|
|
@@ -37,7 +38,7 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
|
|
|
37
38
|
readonly tools: Agent<Message, Message>[] & {
|
|
38
39
|
[key: string]: Agent<Message, Message>;
|
|
39
40
|
};
|
|
40
|
-
private
|
|
41
|
+
private disableEvents?;
|
|
41
42
|
/**
|
|
42
43
|
* Attach agent to context:
|
|
43
44
|
* - subscribe to topic and call process method when message received
|
|
@@ -50,10 +51,12 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
|
|
|
50
51
|
private checkContextStatus;
|
|
51
52
|
private newDefaultContext;
|
|
52
53
|
call(input: I | string, context?: Context): Promise<O>;
|
|
54
|
+
protected checkUsageAgentCalls(context: Context): void;
|
|
53
55
|
protected preprocess(_: I, context: Context): void;
|
|
54
56
|
protected postprocess(input: I, output: O, context: Context): void;
|
|
55
57
|
abstract process(input: I, context: Context): Promise<O | TransferAgentOutput>;
|
|
56
58
|
shutdown(): Promise<void>;
|
|
59
|
+
[inspect.custom](): string;
|
|
57
60
|
}
|
|
58
61
|
export type AgentInputOutputSchema<I extends Message = Message> = ZodType<I> | ((agent: Agent) => ZodType<I>);
|
|
59
62
|
export interface FunctionAgentOptions<I extends Message = Message, O extends Message = Message> extends AgentOptions<I, O> {
|
|
@@ -28,6 +28,10 @@ export declare const aiAgentOptionsSchema: z.ZodObject<{
|
|
|
28
28
|
memory: z.ZodOptional<z.ZodUnion<[z.ZodBoolean, z.ZodAny, z.ZodAny]>>;
|
|
29
29
|
}, "strip", z.ZodTypeAny, {
|
|
30
30
|
description?: string | undefined;
|
|
31
|
+
memory?: any;
|
|
32
|
+
includeInputInOutput?: boolean | undefined;
|
|
33
|
+
subscribeTopic?: string | string[] | undefined;
|
|
34
|
+
publishTopic?: string | string[] | ((...args: unknown[]) => unknown) | undefined;
|
|
31
35
|
tools?: (Agent<Message, Message> | ((...args: unknown[]) => unknown))[] | undefined;
|
|
32
36
|
toolChoice?: Agent<Message, Message> | "auto" | "none" | "required" | "router" | undefined;
|
|
33
37
|
name?: string | undefined;
|
|
@@ -36,13 +40,13 @@ export declare const aiAgentOptionsSchema: z.ZodObject<{
|
|
|
36
40
|
outputKey?: string | undefined;
|
|
37
41
|
enableHistory?: boolean | undefined;
|
|
38
42
|
maxHistoryMessages?: number | undefined;
|
|
39
|
-
includeInputInOutput?: boolean | undefined;
|
|
40
|
-
subscribeTopic?: string | string[] | undefined;
|
|
41
|
-
publishTopic?: string | string[] | ((...args: unknown[]) => unknown) | undefined;
|
|
42
43
|
disableLogging?: boolean | undefined;
|
|
43
|
-
memory?: any;
|
|
44
44
|
}, {
|
|
45
45
|
description?: string | undefined;
|
|
46
|
+
memory?: any;
|
|
47
|
+
includeInputInOutput?: boolean | undefined;
|
|
48
|
+
subscribeTopic?: string | string[] | undefined;
|
|
49
|
+
publishTopic?: string | string[] | ((...args: unknown[]) => unknown) | undefined;
|
|
46
50
|
tools?: (Agent<Message, Message> | ((...args: unknown[]) => unknown))[] | undefined;
|
|
47
51
|
toolChoice?: Agent<Message, Message> | "auto" | "none" | "required" | "router" | undefined;
|
|
48
52
|
name?: string | undefined;
|
|
@@ -51,11 +55,7 @@ export declare const aiAgentOptionsSchema: z.ZodObject<{
|
|
|
51
55
|
outputKey?: string | undefined;
|
|
52
56
|
enableHistory?: boolean | undefined;
|
|
53
57
|
maxHistoryMessages?: number | undefined;
|
|
54
|
-
includeInputInOutput?: boolean | undefined;
|
|
55
|
-
subscribeTopic?: string | string[] | undefined;
|
|
56
|
-
publishTopic?: string | string[] | ((...args: unknown[]) => unknown) | undefined;
|
|
57
58
|
disableLogging?: boolean | undefined;
|
|
58
|
-
memory?: any;
|
|
59
59
|
}>;
|
|
60
60
|
export declare class AIAgent<I extends Message = Message, O extends Message = Message> extends Agent<I, O> {
|
|
61
61
|
static from<I extends Message, O extends Message>(options: AIAgentOptions<I, O>): AIAgent<I, O>;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Client, type ClientOptions } from "@modelcontextprotocol/sdk/client/index.js";
|
|
2
|
+
import { type SSEClientTransportOptions } from "@modelcontextprotocol/sdk/client/sse.js";
|
|
2
3
|
import { type StdioServerParameters } from "@modelcontextprotocol/sdk/client/stdio.js";
|
|
3
4
|
import type { RequestOptions } from "@modelcontextprotocol/sdk/shared/protocol.js";
|
|
4
5
|
import type { Transport } from "@modelcontextprotocol/sdk/shared/transport.js";
|
|
@@ -15,6 +16,15 @@ export interface MCPAgentOptions extends AgentOptions {
|
|
|
15
16
|
export type MCPServerOptions = SSEServerParameters | StdioServerParameters;
|
|
16
17
|
export type SSEServerParameters = {
|
|
17
18
|
url: string;
|
|
19
|
+
/**
|
|
20
|
+
* Additional options to pass to the SSEClientTransport.
|
|
21
|
+
*/
|
|
22
|
+
opts?: SSEClientTransportOptions;
|
|
23
|
+
/**
|
|
24
|
+
* The timeout for requests to the server, in milliseconds.
|
|
25
|
+
* @default 10000
|
|
26
|
+
*/
|
|
27
|
+
timeout?: number;
|
|
18
28
|
/**
|
|
19
29
|
* Whether to automatically reconnect to the server if the connection is lost.
|
|
20
30
|
* @default 10 set to 0 to disable automatic reconnection
|
|
@@ -44,6 +54,7 @@ export declare class MCPAgent extends Agent {
|
|
|
44
54
|
}
|
|
45
55
|
export interface ClientWithReconnectOptions {
|
|
46
56
|
transportCreator?: () => PromiseOrValue<Transport>;
|
|
57
|
+
timeout?: number;
|
|
47
58
|
maxReconnects?: number;
|
|
48
59
|
shouldReconnect?: (error: Error) => boolean;
|
|
49
60
|
}
|
|
@@ -1,26 +1,26 @@
|
|
|
1
1
|
import { ReadableStream } from "node:stream/web";
|
|
2
|
-
import type
|
|
3
|
-
import type { MessagePayload
|
|
2
|
+
import { type Context, type Runnable } from "../execution-engine/context.js";
|
|
3
|
+
import type { MessagePayload } from "../execution-engine/message-queue.js";
|
|
4
4
|
import { type PromiseOrValue } from "../utils/type-utils.js";
|
|
5
5
|
import { Agent, type AgentOptions, type Message } from "./agent.js";
|
|
6
6
|
export interface UserAgentOptions<I extends Message = Message, O extends Message = Message> extends AgentOptions<I, O> {
|
|
7
7
|
context: Context;
|
|
8
8
|
process?: (input: I, context: Context) => PromiseOrValue<O>;
|
|
9
|
+
activeAgent?: Runnable;
|
|
9
10
|
}
|
|
10
11
|
export declare class UserAgent<I extends Message = Message, O extends Message = Message> extends Agent<I, O> {
|
|
11
12
|
static from<I extends Message, O extends Message>(options: UserAgentOptions<I, O>): UserAgent<I, O>;
|
|
12
13
|
constructor(options: UserAgentOptions<I, O>);
|
|
13
|
-
|
|
14
|
-
private get ctx();
|
|
14
|
+
context: Context;
|
|
15
15
|
private _process?;
|
|
16
|
+
private activeAgent?;
|
|
16
17
|
call(input: string | I, context?: Context): Promise<O>;
|
|
17
18
|
process(input: I, context: Context): Promise<O>;
|
|
18
|
-
publish
|
|
19
|
-
subscribe
|
|
20
|
-
|
|
21
|
-
subscribe(topic: string, listener?: MessageQueueListener): Unsubscribe | Promise<MessagePayload>;
|
|
22
|
-
unsubscribe(topic: string, listener: MessageQueueListener): void;
|
|
19
|
+
publish: Context["publish"];
|
|
20
|
+
subscribe: Context["subscribe"];
|
|
21
|
+
unsubscribe: Context["unsubscribe"];
|
|
23
22
|
get stream(): ReadableStream<MessagePayload & {
|
|
24
23
|
topic: string;
|
|
25
24
|
}>;
|
|
25
|
+
protected checkUsageAgentCalls(_context: Context): void;
|
|
26
26
|
}
|