@saber2pr/ai-agent 0.0.11 → 0.0.13
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/core/agent-chain.js
CHANGED
|
@@ -52,6 +52,7 @@ class McpChainAgent {
|
|
|
52
52
|
this.messages = [];
|
|
53
53
|
this.encoder = (0, js_tiktoken_1.getEncoding)("cl100k_base");
|
|
54
54
|
this.extraTools = [];
|
|
55
|
+
this.runningTokenCounter = 0; // 新增:用于追踪当前 Task 的动态消耗
|
|
55
56
|
this.extraTools = (options === null || options === void 0 ? void 0 : options.tools) || [];
|
|
56
57
|
this.maxTokens = (options === null || options === void 0 ? void 0 : options.maxTokens) || 100000;
|
|
57
58
|
this.apiConfig = options === null || options === void 0 ? void 0 : options.apiConfig;
|
|
@@ -89,8 +90,15 @@ class McpChainAgent {
|
|
|
89
90
|
const result = await handler(args);
|
|
90
91
|
const content = typeof result === "string" ? result : JSON.stringify(result);
|
|
91
92
|
// 3. 统计 Token 消耗
|
|
92
|
-
const
|
|
93
|
-
console.log(` [输出长度]: ${
|
|
93
|
+
const outputTokens = this.encoder.encode(content).length;
|
|
94
|
+
console.log(` [输出长度]: ${outputTokens} tokens`);
|
|
95
|
+
// 2. 更新动态计数器:
|
|
96
|
+
// 这里的逻辑是:当前基础 Context + 本次工具调用的输出
|
|
97
|
+
// 随着迭代增加,这个值会一直累加,直到任务结束存入 messages
|
|
98
|
+
const baseTokens = this.calculateTokens();
|
|
99
|
+
this.runningTokenCounter = baseTokens + outputTokens;
|
|
100
|
+
// 3. 打印正确且递增的状态
|
|
101
|
+
console.log(` 📊 状态: Context ${this.runningTokenCounter} / Limit ${this.maxTokens} tokens`);
|
|
94
102
|
return content;
|
|
95
103
|
};
|
|
96
104
|
}
|
package/lib/core/agent.d.ts
CHANGED
|
@@ -16,10 +16,6 @@ export default class McpAgent {
|
|
|
16
16
|
*/
|
|
17
17
|
private calculateTokens;
|
|
18
18
|
private initTools;
|
|
19
|
-
/**
|
|
20
|
-
* 核心功能:内置代码分析工具(基于 engine/targetDir,可被外部通过 createDefaultBuiltinTools 替代)
|
|
21
|
-
*/
|
|
22
|
-
private registerBuiltinTools;
|
|
23
19
|
private ensureApiConfig;
|
|
24
20
|
private loadMcpConfigs;
|
|
25
21
|
init(): Promise<void>;
|
package/lib/core/agent.js
CHANGED
|
@@ -145,14 +145,6 @@ class McpAgent {
|
|
|
145
145
|
this.allTools.push(...allTools);
|
|
146
146
|
}
|
|
147
147
|
}
|
|
148
|
-
/**
|
|
149
|
-
* 核心功能:内置代码分析工具(基于 engine/targetDir,可被外部通过 createDefaultBuiltinTools 替代)
|
|
150
|
-
*/
|
|
151
|
-
registerBuiltinTools(options) {
|
|
152
|
-
this.allTools.push(...(0, builtin_1.createDefaultBuiltinTools)({
|
|
153
|
-
options,
|
|
154
|
-
}));
|
|
155
|
-
}
|
|
156
148
|
// --- 初始化与环境准备 (API Config & MCP Servers) ---
|
|
157
149
|
async ensureApiConfig() {
|
|
158
150
|
if (this.apiConfig)
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
|
-
export declare function jsonSchemaToZod(parameters: any): z.ZodObject<
|
|
2
|
+
export declare function jsonSchemaToZod(parameters: any): z.ZodObject<any>;
|
|
@@ -2,16 +2,13 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.jsonSchemaToZod = jsonSchemaToZod;
|
|
4
4
|
const zod_1 = require("zod");
|
|
5
|
-
// 将 JSON Schema 转换为 Zod Schema 的简易转换器
|
|
6
5
|
function jsonSchemaToZod(parameters) {
|
|
7
6
|
var _a;
|
|
8
|
-
if (!parameters ||
|
|
9
|
-
return zod_1.z.object({}).
|
|
7
|
+
if (!parameters || typeof parameters !== 'object') {
|
|
8
|
+
return zod_1.z.object({}).passthrough();
|
|
10
9
|
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
for (const key in properties) {
|
|
14
|
-
const prop = properties[key];
|
|
10
|
+
// 辅助函数:递归转换单个属性
|
|
11
|
+
function convertProp(prop) {
|
|
15
12
|
let schema = zod_1.z.any();
|
|
16
13
|
if (prop.type === "string") {
|
|
17
14
|
schema = zod_1.z.string();
|
|
@@ -22,12 +19,39 @@ function jsonSchemaToZod(parameters) {
|
|
|
22
19
|
else if (prop.type === "boolean") {
|
|
23
20
|
schema = zod_1.z.boolean();
|
|
24
21
|
}
|
|
22
|
+
else if (prop.type === "array") {
|
|
23
|
+
// 递归处理数组项
|
|
24
|
+
if (prop.items) {
|
|
25
|
+
schema = zod_1.z.array(convertProp(prop.items));
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
schema = zod_1.z.array(zod_1.z.any());
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
else if (prop.type === "object") {
|
|
32
|
+
// 递归处理嵌套对象
|
|
33
|
+
const nestedObj = {};
|
|
34
|
+
if (prop.properties) {
|
|
35
|
+
for (const key in prop.properties) {
|
|
36
|
+
nestedObj[key] = convertProp(prop.properties[key]);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
schema = zod_1.z.object(nestedObj).passthrough();
|
|
40
|
+
}
|
|
25
41
|
if (prop.description) {
|
|
26
42
|
schema = schema.describe(prop.description);
|
|
27
43
|
}
|
|
28
|
-
|
|
44
|
+
return schema;
|
|
45
|
+
}
|
|
46
|
+
const obj = {};
|
|
47
|
+
const properties = parameters.properties || {};
|
|
48
|
+
for (const key in properties) {
|
|
49
|
+
const prop = properties[key];
|
|
50
|
+
let schema = convertProp(prop);
|
|
51
|
+
// 处理必填项
|
|
29
52
|
const isRequired = (_a = parameters.required) === null || _a === void 0 ? void 0 : _a.includes(key);
|
|
30
53
|
obj[key] = isRequired ? schema : schema.optional();
|
|
31
54
|
}
|
|
32
|
-
|
|
55
|
+
// 使用 passthrough 或 loose 以增加容错性
|
|
56
|
+
return zod_1.z.object(obj).passthrough();
|
|
33
57
|
}
|