@saber2pr/ai-agent 0.0.12 → 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.d.ts +1 -0
- package/lib/core/agent-chain.js +10 -2
- package/lib/core/agent.d.ts +0 -4
- package/lib/core/agent.js +0 -8
- package/package.json +1 -1
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)
|