@aigne/core 1.6.0 → 1.8.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 +27 -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 +12 -2
- package/lib/cjs/agents/mcp-agent.js +45 -27
- 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 +14 -41
- 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/agent-js.js +1 -1
- package/lib/cjs/loader/agent-yaml.d.ts +1 -0
- package/lib/cjs/loader/agent-yaml.js +4 -0
- package/lib/cjs/loader/index.d.ts +72 -1
- package/lib/cjs/loader/index.js +13 -13
- 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 +11 -8
- 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 +12 -2
- 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/loader/agent-yaml.d.ts +1 -0
- package/lib/dts/loader/index.d.ts +72 -1
- 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 +12 -2
- package/lib/esm/agents/mcp-agent.js +46 -28
- 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 +15 -39
- 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/agent-js.js +1 -1
- package/lib/esm/loader/agent-yaml.d.ts +1 -0
- package/lib/esm/loader/agent-yaml.js +4 -0
- package/lib/esm/loader/index.d.ts +72 -1
- package/lib/esm/loader/index.js +13 -14
- 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 +11 -8
- 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 +11 -9
- 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
|
+
}
|
|
@@ -58,7 +58,7 @@ async function loadAgentFromJsFile(path) {
|
|
|
58
58
|
throw new Error(`Agent file ${path} must export a default function, but got ${typeof agent}`);
|
|
59
59
|
}
|
|
60
60
|
return (0, type_utils_js_1.tryOrThrow)(() => agentJsFileSchema.parse({
|
|
61
|
-
name: agent.name,
|
|
61
|
+
name: agent.agent_name || agent.name,
|
|
62
62
|
description: agent.description,
|
|
63
63
|
input_schema: agent.input_schema,
|
|
64
64
|
output_schema: agent.output_schema,
|
|
@@ -10,6 +10,7 @@ export declare function loadAgentFromYamlFile(path: string): Promise<{
|
|
|
10
10
|
}, {
|
|
11
11
|
[x: string]: any;
|
|
12
12
|
}> | undefined;
|
|
13
|
+
tool_choice?: "auto" | "none" | "required" | "router" | undefined;
|
|
13
14
|
output_schema?: ZodObject<Record<string, ZodType<any, z.ZodTypeDef, any>>, z.UnknownKeysParam, z.ZodTypeAny, {
|
|
14
15
|
[x: string]: any;
|
|
15
16
|
}, {
|
|
@@ -33,6 +33,10 @@ const agentFileSchema = zod_1.z.discriminatedUnion("type", [
|
|
|
33
33
|
.array(zod_1.z.string())
|
|
34
34
|
.nullish()
|
|
35
35
|
.transform((v) => v ?? undefined),
|
|
36
|
+
tool_choice: zod_1.z
|
|
37
|
+
.union([zod_1.z.literal("auto"), zod_1.z.literal("none"), zod_1.z.literal("required"), zod_1.z.literal("router")])
|
|
38
|
+
.nullish()
|
|
39
|
+
.transform((v) => v ?? undefined),
|
|
36
40
|
}),
|
|
37
41
|
zod_1.z.object({
|
|
38
42
|
type: zod_1.z.literal("mcp"),
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
1
2
|
import { type Agent } from "../agents/agent.js";
|
|
2
|
-
import type { ChatModel } from "../models/chat-model.js";
|
|
3
|
+
import type { ChatModel, ChatModelOptions } from "../models/chat-model.js";
|
|
3
4
|
export interface LoadOptions {
|
|
4
5
|
path: string;
|
|
5
6
|
}
|
|
@@ -19,6 +20,75 @@ export declare function load(options: LoadOptions): Promise<{
|
|
|
19
20
|
} | null | undefined;
|
|
20
21
|
}>;
|
|
21
22
|
export declare function loadAgent(path: string): Promise<Agent>;
|
|
23
|
+
export declare function loadModel(model?: z.infer<typeof aigneFileSchema>["chat_model"], modelOptions?: ChatModelOptions): Promise<ChatModel | undefined>;
|
|
24
|
+
declare const aigneFileSchema: z.ZodObject<{
|
|
25
|
+
name: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
26
|
+
description: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
27
|
+
chat_model: z.ZodEffects<z.ZodOptional<z.ZodNullable<z.ZodUnion<[z.ZodString, z.ZodObject<{
|
|
28
|
+
provider: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
29
|
+
name: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
30
|
+
temperature: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
|
|
31
|
+
top_p: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
|
|
32
|
+
frequent_penalty: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
|
|
33
|
+
presence_penalty: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
|
|
34
|
+
}, "strip", z.ZodTypeAny, {
|
|
35
|
+
name?: string | null | undefined;
|
|
36
|
+
temperature?: number | null | undefined;
|
|
37
|
+
provider?: string | null | undefined;
|
|
38
|
+
top_p?: number | null | undefined;
|
|
39
|
+
frequent_penalty?: number | null | undefined;
|
|
40
|
+
presence_penalty?: number | null | undefined;
|
|
41
|
+
}, {
|
|
42
|
+
name?: string | null | undefined;
|
|
43
|
+
temperature?: number | null | undefined;
|
|
44
|
+
provider?: string | null | undefined;
|
|
45
|
+
top_p?: number | null | undefined;
|
|
46
|
+
frequent_penalty?: number | null | undefined;
|
|
47
|
+
presence_penalty?: number | null | undefined;
|
|
48
|
+
}>]>>>, {
|
|
49
|
+
name?: string | null | undefined;
|
|
50
|
+
temperature?: number | null | undefined;
|
|
51
|
+
provider?: string | null | undefined;
|
|
52
|
+
top_p?: number | null | undefined;
|
|
53
|
+
frequent_penalty?: number | null | undefined;
|
|
54
|
+
presence_penalty?: number | null | undefined;
|
|
55
|
+
} | null | undefined, string | {
|
|
56
|
+
name?: string | null | undefined;
|
|
57
|
+
temperature?: number | null | undefined;
|
|
58
|
+
provider?: string | null | undefined;
|
|
59
|
+
top_p?: number | null | undefined;
|
|
60
|
+
frequent_penalty?: number | null | undefined;
|
|
61
|
+
presence_penalty?: number | null | undefined;
|
|
62
|
+
} | null | undefined>;
|
|
63
|
+
agents: z.ZodOptional<z.ZodNullable<z.ZodArray<z.ZodString, "many">>>;
|
|
64
|
+
tools: z.ZodOptional<z.ZodNullable<z.ZodArray<z.ZodString, "many">>>;
|
|
65
|
+
}, "strip", z.ZodTypeAny, {
|
|
66
|
+
description?: string | null | undefined;
|
|
67
|
+
tools?: string[] | null | undefined;
|
|
68
|
+
name?: string | null | undefined;
|
|
69
|
+
chat_model?: {
|
|
70
|
+
name?: string | null | undefined;
|
|
71
|
+
temperature?: number | null | undefined;
|
|
72
|
+
provider?: string | null | undefined;
|
|
73
|
+
top_p?: number | null | undefined;
|
|
74
|
+
frequent_penalty?: number | null | undefined;
|
|
75
|
+
presence_penalty?: number | null | undefined;
|
|
76
|
+
} | null | undefined;
|
|
77
|
+
agents?: string[] | null | undefined;
|
|
78
|
+
}, {
|
|
79
|
+
description?: string | null | undefined;
|
|
80
|
+
tools?: string[] | null | undefined;
|
|
81
|
+
name?: string | null | undefined;
|
|
82
|
+
chat_model?: string | {
|
|
83
|
+
name?: string | null | undefined;
|
|
84
|
+
temperature?: number | null | undefined;
|
|
85
|
+
provider?: string | null | undefined;
|
|
86
|
+
top_p?: number | null | undefined;
|
|
87
|
+
frequent_penalty?: number | null | undefined;
|
|
88
|
+
presence_penalty?: number | null | undefined;
|
|
89
|
+
} | null | undefined;
|
|
90
|
+
agents?: string[] | null | undefined;
|
|
91
|
+
}>;
|
|
22
92
|
export declare function loadAIGNEFile(path: string): Promise<{
|
|
23
93
|
description?: string | null | undefined;
|
|
24
94
|
tools?: string[] | null | undefined;
|
|
@@ -33,3 +103,4 @@ export declare function loadAIGNEFile(path: string): Promise<{
|
|
|
33
103
|
} | null | undefined;
|
|
34
104
|
agents?: string[] | null | undefined;
|
|
35
105
|
}>;
|
|
106
|
+
export {};
|
package/lib/cjs/loader/index.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.load = load;
|
|
4
4
|
exports.loadAgent = loadAgent;
|
|
5
|
+
exports.loadModel = loadModel;
|
|
5
6
|
exports.loadAIGNEFile = loadAIGNEFile;
|
|
6
7
|
const promises_1 = require("node:fs/promises");
|
|
7
8
|
const node_path_1 = require("node:path");
|
|
@@ -16,7 +17,6 @@ const xai_chat_model_js_1 = require("../models/xai-chat-model.js");
|
|
|
16
17
|
const type_utils_js_1 = require("../utils/type-utils.js");
|
|
17
18
|
const agent_js_js_1 = require("./agent-js.js");
|
|
18
19
|
const agent_yaml_js_1 = require("./agent-yaml.js");
|
|
19
|
-
const DEFAULT_MODEL_PROVIDER = "openai";
|
|
20
20
|
const AIGNE_FILE_NAME = ["aigne.yaml", "aigne.yml"];
|
|
21
21
|
async function load(options) {
|
|
22
22
|
const { path } = options;
|
|
@@ -54,6 +54,7 @@ async function loadAgent(path) {
|
|
|
54
54
|
outputSchema: agent.output_schema,
|
|
55
55
|
outputKey: agent.output_key,
|
|
56
56
|
tools: await Promise.all((agent.tools ?? []).map((filename) => loadAgent((0, node_path_1.join)((0, node_path_1.dirname)(path), filename)))),
|
|
57
|
+
toolChoice: agent.tool_choice,
|
|
57
58
|
});
|
|
58
59
|
}
|
|
59
60
|
if (agent.type === "mcp") {
|
|
@@ -73,21 +74,20 @@ async function loadAgent(path) {
|
|
|
73
74
|
}
|
|
74
75
|
throw new Error(`Unsupported agent file type: ${path}`);
|
|
75
76
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
return undefined;
|
|
77
|
+
const { MODEL_PROVIDER = "openai", MODEL_NAME = "gpt-4o-mini" } = process.env;
|
|
78
|
+
async function loadModel(model, modelOptions) {
|
|
79
79
|
const params = {
|
|
80
|
-
model: model
|
|
81
|
-
temperature: model
|
|
82
|
-
topP: model
|
|
83
|
-
frequencyPenalty: model
|
|
84
|
-
presencePenalty: model
|
|
80
|
+
model: model?.name ?? MODEL_NAME,
|
|
81
|
+
temperature: model?.temperature ?? undefined,
|
|
82
|
+
topP: model?.top_p ?? undefined,
|
|
83
|
+
frequencyPenalty: model?.frequent_penalty ?? undefined,
|
|
84
|
+
presencePenalty: model?.presence_penalty ?? undefined,
|
|
85
85
|
};
|
|
86
86
|
const availableModels = [openai_chat_model_js_1.OpenAIChatModel, claude_chat_model_js_1.ClaudeChatModel, xai_chat_model_js_1.XAIChatModel];
|
|
87
|
-
const M = availableModels.find((m) => m.name.toLowerCase().includes(model
|
|
87
|
+
const M = availableModels.find((m) => m.name.toLowerCase().includes(model?.provider || MODEL_PROVIDER));
|
|
88
88
|
if (!M)
|
|
89
|
-
throw new Error(`Unsupported model: ${model
|
|
90
|
-
return new M(params);
|
|
89
|
+
throw new Error(`Unsupported model: ${model?.provider} ${model?.name}`);
|
|
90
|
+
return new M({ model: params.model, modelOptions: { ...params, ...modelOptions } });
|
|
91
91
|
}
|
|
92
92
|
const aigneFileSchema = zod_1.z.object({
|
|
93
93
|
name: zod_1.z.string().nullish(),
|
|
@@ -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
|
});
|
|
@@ -36,7 +36,7 @@ class ClaudeChatModel extends chat_model_js_1.ChatModel {
|
|
|
36
36
|
}
|
|
37
37
|
_client;
|
|
38
38
|
get client() {
|
|
39
|
-
const apiKey = this.options?.apiKey || process.env.CLAUDE_API_KEY;
|
|
39
|
+
const apiKey = this.options?.apiKey || process.env.ANTHROPIC_API_KEY || process.env.CLAUDE_API_KEY;
|
|
40
40
|
if (!apiKey)
|
|
41
41
|
throw new Error("Api Key is required for ClaudeChatModel");
|
|
42
42
|
this._client ??= new sdk_1.default({ apiKey });
|
|
@@ -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> {
|