@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.
@@ -11,6 +11,7 @@ export default class McpChainAgent {
11
11
  private apiModel?;
12
12
  private targetDir;
13
13
  private verbose;
14
+ private runningTokenCounter;
14
15
  constructor(options?: AgentOptions);
15
16
  /**
16
17
  * 工具处理器包装逻辑:增加日志打印和 Token 监控
@@ -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 tokens = this.encoder.encode(content).length;
93
- console.log(` [输出长度]: ${tokens} tokens`);
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
  }
@@ -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<{}, z.core.$loose>;
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 || !parameters.properties) {
9
- return zod_1.z.object({}).loose();
7
+ if (!parameters || typeof parameters !== 'object') {
8
+ return zod_1.z.object({}).passthrough();
10
9
  }
11
- const obj = {};
12
- const properties = parameters.properties;
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
- // 默认都设为 optional 以防 Agent 报错,除非在 JSON Schema 的 required 数组中
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
- return zod_1.z.object(obj).loose();
55
+ // 使用 passthrough 或 loose 以增加容错性
56
+ return zod_1.z.object(obj).passthrough();
33
57
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@saber2pr/ai-agent",
3
- "version": "0.0.11",
3
+ "version": "0.0.13",
4
4
  "description": "AI Assistant CLI.",
5
5
  "author": "saber2pr",
6
6
  "license": "ISC",