@ai-zen/agents-sdk 1.0.0-alpha.8 → 1.0.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.
Files changed (86) hide show
  1. package/CHANGELOG.md +661 -2
  2. package/README.md +22 -6
  3. package/README.zh.md +23 -7
  4. package/dist/agent-plugins/AutoMigratePlugin.d.ts +35 -9
  5. package/dist/agent-plugins/AutoMigratePlugin.js +21 -10
  6. package/dist/agent-plugins/AutoRefreshToolsPlugin.js +1 -3
  7. package/dist/agent-plugins/ContextGuardPlugin.d.ts +28 -16
  8. package/dist/agent-plugins/ContextGuardPlugin.js +23 -17
  9. package/dist/agent-plugins/UnknownToolHintPlugin.js +4 -4
  10. package/dist/config/ConfigManager.d.ts +57 -8
  11. package/dist/config/ConfigManager.js +167 -26
  12. package/dist/config/constants.d.ts +7 -0
  13. package/dist/config/constants.js +70 -43
  14. package/dist/config/factory-skills.d.ts +33 -0
  15. package/dist/config/factory-skills.js +126 -0
  16. package/dist/config/migrate.d.ts +106 -0
  17. package/dist/config/migrate.js +196 -0
  18. package/dist/config/reasoning-presets.d.ts +37 -0
  19. package/dist/config/reasoning-presets.js +64 -0
  20. package/dist/index.d.ts +13 -7
  21. package/dist/index.js +11 -3
  22. package/dist/runtime/HandoffGuardPlugin.d.ts +35 -0
  23. package/dist/runtime/HandoffGuardPlugin.js +65 -0
  24. package/dist/runtime/HandoffService.d.ts +246 -0
  25. package/dist/runtime/HandoffService.js +475 -0
  26. package/dist/runtime/SdkAgent.d.ts +7 -1
  27. package/dist/runtime/SdkAgent.js +3 -0
  28. package/dist/runtime/createAgent.d.ts +20 -1
  29. package/dist/runtime/createAgent.js +14 -9
  30. package/dist/runtime/createModel.d.ts +8 -4
  31. package/dist/runtime/createModel.js +22 -5
  32. package/dist/scope/PermissionEvaluator.js +1 -1
  33. package/dist/scope/Scope.d.ts +3 -1
  34. package/dist/scope/Scope.js +4 -2
  35. package/dist/scope/plugins/builtin/BatchEditTool.js +24 -14
  36. package/dist/scope/plugins/builtin/CopyTool.js +4 -4
  37. package/dist/scope/plugins/builtin/CwdTool.js +1 -1
  38. package/dist/scope/plugins/builtin/DownloadFileTool.d.ts +1 -0
  39. package/dist/scope/plugins/builtin/DownloadFileTool.js +58 -34
  40. package/dist/scope/plugins/builtin/EditTool.js +25 -25
  41. package/dist/scope/plugins/builtin/ExecTool.js +134 -25
  42. package/dist/scope/plugins/builtin/ExistTool.js +2 -2
  43. package/dist/scope/plugins/builtin/FindTextTool.js +15 -10
  44. package/dist/scope/plugins/builtin/GenerateImageTool.js +91 -25
  45. package/dist/scope/plugins/builtin/GlobTool.js +10 -5
  46. package/dist/scope/plugins/builtin/InspectFileTool.js +7 -7
  47. package/dist/scope/plugins/builtin/LsTool.js +5 -5
  48. package/dist/scope/plugins/builtin/MkdirTool.js +3 -3
  49. package/dist/scope/plugins/builtin/ReadTool.js +8 -8
  50. package/dist/scope/plugins/builtin/RenameTool.js +3 -3
  51. package/dist/scope/plugins/builtin/RmTool.js +3 -3
  52. package/dist/scope/plugins/builtin/SleepTool.js +6 -6
  53. package/dist/scope/plugins/builtin/SpawnTool.js +32 -49
  54. package/dist/scope/plugins/builtin/ViewImageTool.d.ts +6 -4
  55. package/dist/scope/plugins/builtin/ViewImageTool.js +50 -62
  56. package/dist/scope/plugins/builtin/WriteTool.js +3 -3
  57. package/dist/scope/plugins/builtin/searchBase.d.ts +9 -0
  58. package/dist/scope/plugins/builtin/searchBase.js +17 -0
  59. package/dist/scope/plugins/builtin/test-helpers.d.ts +3 -3
  60. package/dist/scope/plugins/builtin/test-helpers.js +3 -3
  61. package/dist/scope/plugins/builtin/textEdit.d.ts +17 -0
  62. package/dist/scope/plugins/builtin/textEdit.js +37 -0
  63. package/dist/scope/plugins/mcp/McpConnectionManager.d.ts +9 -1
  64. package/dist/scope/plugins/mcp/McpConnectionManager.js +95 -16
  65. package/dist/scope/plugins/mcp/McpScopePlugin.d.ts +1 -1
  66. package/dist/scope/plugins/mcp/McpScopePlugin.js +8 -6
  67. package/dist/scope/plugins/mcp/discover.js +1 -1
  68. package/dist/scope/plugins/mcp/mcpTools.d.ts +9 -3
  69. package/dist/scope/plugins/mcp/mcpTools.js +247 -37
  70. package/dist/scope/plugins/skills/SkillsScopePlugin.js +2 -3
  71. package/dist/scope/plugins/skills/discover.js +28 -9
  72. package/dist/scope/plugins/skills/skillTools.js +30 -20
  73. package/dist/scope/plugins/subagents/SubAgentsScopePlugin.js +1 -3
  74. package/dist/scope/plugins/subagents/subAgentTools.js +4 -3
  75. package/dist/scope/plugins/usertools/discover.d.ts +8 -3
  76. package/dist/scope/plugins/usertools/discover.js +139 -12
  77. package/dist/shared/EntityRepository.d.ts +10 -1
  78. package/dist/shared/EntityRepository.js +18 -6
  79. package/dist/shared/content-blocks.d.ts +101 -0
  80. package/dist/shared/content-blocks.js +253 -0
  81. package/dist/shared/errors.d.ts +4 -8
  82. package/dist/shared/errors.js +8 -14
  83. package/dist/types/index.d.ts +170 -8
  84. package/package.json +16 -7
  85. package/dist/runtime/TaskMigrationService.d.ts +0 -128
  86. package/dist/runtime/TaskMigrationService.js +0 -211
@@ -2,6 +2,7 @@ import * as fsp from "fs/promises";
2
2
  import * as path from "path";
3
3
  import { SdkCallbackTool } from "./SdkCallbackTool.js";
4
4
  import { DEFAULT_MAX_TOOL_OUTPUT, guardOutput, headPreview } from "./outputGuard.js";
5
+ import { checkBaseDirectory } from "./searchBase.js";
5
6
  /** 默认总匹配数上限 */
6
7
  const DEFAULT_MAX_MATCHES = 200;
7
8
  /** 默认文件大小上限(字节,与 ReadTool 的 300KB 口径一致) */
@@ -11,7 +12,7 @@ const DEFAULT_MAX_LINE_LENGTH = 200;
11
12
  export class FindTextTool extends SdkCallbackTool {
12
13
  function = {
13
14
  name: "find_text",
14
- description: "查找文本出现的位置,支持普通文本或正则匹配,返回文件名及具体行号、行内容。为避免结果撑爆上下文,默认限制总匹配数、单文件大小与行内容长度,超限会截断并标记 truncated。",
15
+ description: "Find where text occurs, supporting plain text or regex matching; returns file names with line numbers and line content. To avoid blowing up the context, the total match count, per-file size and line content length are limited by default, and results beyond the limits are truncated and marked truncated.",
15
16
  parameters: {
16
17
  type: "object",
17
18
  properties: {
@@ -25,30 +26,30 @@ export class FindTextTool extends SdkCallbackTool {
25
26
  },
26
27
  text: {
27
28
  type: "string",
28
- description: "要查找的文本(与 regex 二选一)",
29
+ description: "Text to search for (mutually exclusive with regex)",
29
30
  },
30
31
  regex: {
31
32
  type: "string",
32
- description: "正则表达式(与 text 二选一,例如 \\bconst\\s+\\w+)",
33
+ description: "Regular expression (mutually exclusive with text, e.g. \\bconst\\s+\\w+)",
33
34
  },
34
35
  exclude: {
35
36
  type: "array",
36
- description: "要排除的 glob 模式数组",
37
+ description: 'Array of glob patterns to exclude. When omitted, defaults to ["**/node_modules/**"]; pass an explicit list to override.',
37
38
  items: {
38
39
  type: "string",
39
40
  },
40
41
  },
41
42
  maxMatches: {
42
43
  type: "number",
43
- description: "总匹配数上限,默认 200",
44
+ description: "Maximum total matches (default 200)",
44
45
  },
45
46
  maxFileSize: {
46
47
  type: "number",
47
- description: "单文件大小上限(字节),超过则跳过该文件,默认 307200(300KB)",
48
+ description: "Maximum file size in bytes; larger files are skipped (default 307200, i.e. 300KB)",
48
49
  },
49
50
  maxLineLength: {
50
51
  type: "number",
51
- description: "行内容与匹配子串的截断长度,默认 200",
52
+ description: "Truncation length for line content and matched substrings (default 200)",
52
53
  },
53
54
  },
54
55
  required: ["path", "pattern"],
@@ -61,11 +62,15 @@ export class FindTextTool extends SdkCallbackTool {
61
62
  const signal = ctx?.signal;
62
63
  try {
63
64
  const cwd = input.path ? this.resolve(input.path) : this.env.cwd;
65
+ // 基目录不存在/不是目录时明确报错:静默返回 [] 会被模型当成「没找到匹配」
66
+ const baseError = await checkBaseDirectory(cwd);
67
+ if (baseError)
68
+ return baseError;
64
69
  const result = [];
65
70
  const text = input.text;
66
71
  const regexStr = input.regex;
67
72
  if (!text && !regexStr) {
68
- return "请提供 text 或 regex 参数";
73
+ return "Provide either the text or the regex parameter";
69
74
  }
70
75
  const regex = regexStr ? new RegExp(regexStr) : null;
71
76
  const maxMatches = input.maxMatches ?? DEFAULT_MAX_MATCHES;
@@ -153,8 +158,8 @@ export class FindTextTool extends SdkCallbackTool {
153
158
  const preview = headPreview(json);
154
159
  return JSON.stringify({
155
160
  outputTooLarge: true,
156
- warning: `输出过大:结果共 ${json.length} 字符,超过上限 ${limit} 字符。` +
157
- `完整结果已落盘(result.json),可用 read 分批读取。`,
161
+ warning: `Output too large: the results total ${json.length} characters, exceeding the limit of ${limit} characters. ` +
162
+ `The full result has been written to disk (result.json) and can be read in chunks with read.`,
158
163
  files: ctx.files,
159
164
  chars: json.length,
160
165
  ...preview,
@@ -1,5 +1,20 @@
1
1
  import OpenAI from "openai";
2
+ import * as fsp from "fs/promises";
3
+ import * as os from "os";
4
+ import * as path from "path";
5
+ import { randomBytes } from "crypto";
2
6
  import { SdkCallbackTool } from "./SdkCallbackTool.js";
7
+ /** 默认图片生成实现:走 OpenAI SDK 的 `images.generate`(任何 OpenAI 兼容端点均可) */
8
+ const defaultGenerateImage = async ({ endpoint, model, prompt, size, quality, signal, }) => {
9
+ const client = new OpenAI({ apiKey: endpoint.apiKey, baseURL: endpoint.baseUrl });
10
+ // 厂商特有字段(size / quality)用 any 透传
11
+ const result = await client.images.generate({ model, prompt, size, quality }, { signal });
12
+ return {
13
+ created: result.created,
14
+ // 有的端点只返回 b64_json,两类字段都原样带出去,由调用方决定怎么落地
15
+ data: (result.data ?? []),
16
+ };
17
+ };
3
18
  /**
4
19
  * 根据文字描述生成图片。**统一返回字符串**(JSON,含图片 URL 列表):
5
20
  * 生成结果如何处理(查看/下载/转发)由模型与上层自由决定——
@@ -15,25 +30,25 @@ export class GenerateImageTool extends SdkCallbackTool {
15
30
  }
16
31
  function = {
17
32
  name: "generate_image",
18
- description: "根据文字描述生成图片,返回图片 URL 列表(JSON)。如需查看图片内容,请用 view_image 工具(传入图片 URL);如需保存到本地,用 download_file 下载。",
33
+ description: "Generate an image from a text description and return a list of image URLs (JSON). To inspect the image content, use the view_image tool (pass the image URL); to save it locally, use download_file.",
19
34
  parameters: {
20
35
  type: "object",
21
36
  properties: {
22
37
  prompt: {
23
38
  type: "string",
24
- description: "所需图像的文本描述,应详细描述画面内容、风格、构图等",
39
+ description: "Text description of the desired image; describe the subject, style, composition and other details",
25
40
  },
26
41
  model: {
27
42
  type: "string",
28
- description: "图片模型 ID,不指定则使用配置中的默认图片模型;若指定的模型不存在,本工具会返回当前可用的模型列表。",
43
+ description: "Image model ID. Defaults to the configured default image model; if the given model does not exist, this tool returns the list of currently available models.",
29
44
  },
30
45
  size: {
31
46
  type: "string",
32
- description: '图片尺寸。不指定则使用模型的默认尺寸。cogview系列: 1024x1024, 768x1344, 864x1152, 1344x768, 1152x864, 1440x720, 720x1440。glm-image: 1280x1280, 1568x1056, 1056x1568, 1472x1088, 1088x1472, 1728x960, 960x1728。',
47
+ description: "Image size. Defaults to the model default size. cogview series: 1024x1024, 768x1344, 864x1152, 1344x768, 1152x864, 1440x720, 720x1440. glm-image: 1280x1280, 1568x1056, 1056x1568, 1472x1088, 1088x1472, 1728x960, 960x1728.",
33
48
  },
34
49
  quality: {
35
50
  type: "string",
36
- description: "图像质量。hd: 精细(约20秒), standard: 快速(约5-10秒)",
51
+ description: "Image quality. hd: fine (about 20 seconds), standard: fast (about 5-10 seconds)",
37
52
  enum: ["hd", "standard"],
38
53
  },
39
54
  },
@@ -49,7 +64,7 @@ export class GenerateImageTool extends SdkCallbackTool {
49
64
  try {
50
65
  const prompt = input.prompt;
51
66
  if (!prompt || !prompt.trim()) {
52
- return JSON.stringify({ success: false, error: "prompt 不能为空" });
67
+ return JSON.stringify({ success: false, error: "prompt must not be empty" });
53
68
  }
54
69
  const config = this.env.config;
55
70
  const imageModels = config.imageModels;
@@ -57,7 +72,7 @@ export class GenerateImageTool extends SdkCallbackTool {
57
72
  if (!imageModels || imageModels.length === 0) {
58
73
  return JSON.stringify({
59
74
  success: false,
60
- error: "未配置图片生成模型。请在配置中添加 imageModels。",
75
+ error: "No image generation model configured. Add imageModels to the configuration.",
61
76
  });
62
77
  }
63
78
  const modelId = input.model || defaultImageModel;
@@ -67,47 +82,68 @@ export class GenerateImageTool extends SdkCallbackTool {
67
82
  if (!imageModel) {
68
83
  return JSON.stringify({
69
84
  success: false,
70
- error: `图片模型 "${modelId}" 不存在。可用的图片模型: ${imageModels.map((m) => m.id).join(", ")}`,
85
+ error: `Image model "${modelId}" does not exist. Available image models: ${imageModels.map((m) => m.id).join(", ")}`,
71
86
  });
72
87
  }
73
88
  const endpoint = config.endpoints.find((e) => e.id === imageModel.endpointId);
74
89
  if (!endpoint) {
75
90
  return JSON.stringify({
76
91
  success: false,
77
- error: `图片模型 "${imageModel.name}" 对应的端点 "${imageModel.endpointId}" 未配置`,
92
+ error: `Endpoint "${imageModel.endpointId}" for image model "${imageModel.name}" is not configured`,
78
93
  });
79
94
  }
80
95
  if (!endpoint.apiKey) {
81
96
  return JSON.stringify({
82
97
  success: false,
83
- error: `端点 "${endpoint.name}" 的 API Key 未设置`,
98
+ error: `API Key for endpoint "${endpoint.name}" is not set`,
84
99
  });
85
100
  }
86
- const client = new OpenAI({
87
- apiKey: endpoint.apiKey,
88
- baseURL: endpoint.baseUrl,
89
- });
90
101
  const modelName = input.model || imageModel.modelName;
91
- // 通过官方 SDK 生成图片(厂商特有字段如 size/quality 用 any 透传)
92
- const result = await client.images.generate({
102
+ // 出网动作走注入的 deps(缺省时用 OpenAI SDK),测试无需 mock openai 模块
103
+ const generateImage = this.env.deps?.generateImage ?? defaultGenerateImage;
104
+ const result = await generateImage({
105
+ endpoint,
93
106
  model: modelName,
94
107
  prompt: prompt.trim(),
95
108
  size: input.size || imageModel.defaultSize || undefined,
96
109
  quality: input.quality || imageModel.defaultQuality || undefined,
97
- }, { signal });
98
- const urls = (result.data ?? [])
99
- .map((img) => img.url)
100
- .filter((url) => !!url);
110
+ signal,
111
+ });
112
+ // 逐个收集:有 url 的直接给链接;只有 b64_json 的端点则落盘后给本地路径。
113
+ // 只看 url 会把「只返回 b64_json 的模型」变成 success: true + images: [](假成功)。
114
+ const images = [];
115
+ for (const [index, item] of (result.data ?? []).entries()) {
116
+ if (item.url) {
117
+ images.push({ index, url: item.url });
118
+ continue;
119
+ }
120
+ if (item.b64_json) {
121
+ images.push({ index, filePath: await saveBase64Image(item.b64_json) });
122
+ }
123
+ }
124
+ if (images.length === 0) {
125
+ return JSON.stringify({
126
+ success: false,
127
+ model: imageModel.name,
128
+ modelId: imageModel.id,
129
+ created: result.created,
130
+ images: [],
131
+ error: "The endpoint returned no image: neither `url` nor `b64_json` was present in the response",
132
+ });
133
+ }
101
134
  // 统一返回字符串(JSON):生成结果如何处理(查看/下载/转发)由模型与上层自由决定,
102
- // 不替模型做「看或不看」的假设。需要让模型看图 → 模型可主动调用 view_image(URL);
135
+ // 不替模型做「看或不看」的假设。需要让模型看图 → 模型可主动调用 view_image;
103
136
  // 需要保存 → download_file 下载。
137
+ const hasUrl = images.some((img) => img.url);
104
138
  return JSON.stringify({
105
139
  success: true,
106
140
  model: imageModel.name,
107
141
  modelId: imageModel.id,
108
142
  created: result.created,
109
- images: urls.map((url, i) => ({ index: i, url })),
110
- note: "图片临时链接有效期为30天。如需查看图片内容,可用 view_image 工具(传入图片 URL);如需保存,可用 download_file 下载。",
143
+ images,
144
+ note: hasUrl
145
+ ? "Image links are temporary and expire after 30 days. To inspect the image content, use the view_image tool (pass the image URL); to save it, use download_file."
146
+ : "The endpoint returned base64 images; they were written to local files, so use view_image with the filePath to inspect them.",
111
147
  }, null, 2);
112
148
  }
113
149
  catch (error) {
@@ -116,13 +152,43 @@ export class GenerateImageTool extends SdkCallbackTool {
116
152
  return JSON.stringify({
117
153
  success: false,
118
154
  aborted: true,
119
- error: "图片生成被中断(aborted)",
155
+ error: "Image generation aborted",
120
156
  });
121
157
  }
122
158
  return JSON.stringify({
123
159
  success: false,
124
- error: error instanceof Error ? error.message : "图片生成失败",
160
+ error: error instanceof Error ? error.message : "Image generation failed",
125
161
  });
126
162
  }
127
163
  }
128
164
  }
165
+ /** 依据 base64 头部字节判断图片扩展名(判断不了就当 png) */
166
+ function imageExtension(buffer) {
167
+ if (buffer.length >= 8 && buffer.subarray(0, 8).equals(Buffer.from("89504e470d0a1a0a", "hex"))) {
168
+ return "png";
169
+ }
170
+ if (buffer.length >= 3 && buffer[0] === 0xff && buffer[1] === 0xd8 && buffer[2] === 0xff) {
171
+ return "jpg";
172
+ }
173
+ if (buffer.length >= 6 && buffer.subarray(0, 3).toString("latin1") === "GIF")
174
+ return "gif";
175
+ if (buffer.length >= 12 &&
176
+ buffer.subarray(0, 4).toString("latin1") === "RIFF" &&
177
+ buffer.subarray(8, 12).toString("latin1") === "WEBP") {
178
+ return "webp";
179
+ }
180
+ return "png";
181
+ }
182
+ /**
183
+ * 把 `b64_json` 写成本地文件,返回绝对路径。
184
+ *
185
+ * 落盘到系统临时目录下的独立子目录(与工具输出落盘同一习惯),不污染工作目录。
186
+ */
187
+ async function saveBase64Image(b64) {
188
+ const buffer = Buffer.from(b64, "base64");
189
+ const dir = path.join(os.tmpdir(), "ai-zen", "generated-images");
190
+ await fsp.mkdir(dir, { recursive: true });
191
+ const filePath = path.join(dir, `image-${Date.now()}-${randomBytes(4).toString("hex")}.${imageExtension(buffer)}`);
192
+ await fsp.writeFile(filePath, buffer);
193
+ return filePath;
194
+ }
@@ -1,10 +1,11 @@
1
1
  import * as fsp from "fs/promises";
2
2
  import { SdkCallbackTool } from "./SdkCallbackTool.js";
3
3
  import { DEFAULT_MAX_TOOL_OUTPUT, guardOutput, headPreview } from "./outputGuard.js";
4
+ import { checkBaseDirectory } from "./searchBase.js";
4
5
  export class GlobTool extends SdkCallbackTool {
5
6
  function = {
6
7
  name: "glob",
7
- description: "使用 glob 模式递归扫描和查找文件。这是进行文件系统搜索的首选工具,功能远优于简单的 'ls' 列表命令。当你需要查找特定类型的文件、遍历目录树或需要排除特定文件时,请优先使用此函数。",
8
+ description: "Recursively scan for and find files using a glob pattern. This is the preferred tool for file system searches, far better than a plain 'ls' listing. Prefer this tool when you need to find files of a specific type, walk a directory tree, or exclude specific files.",
8
9
  parameters: {
9
10
  type: "object",
10
11
  properties: {
@@ -18,7 +19,7 @@ export class GlobTool extends SdkCallbackTool {
18
19
  },
19
20
  exclude: {
20
21
  type: "array",
21
- description: "glob pattern to exclude",
22
+ description: 'Glob patterns to exclude. When omitted, defaults to ["**/node_modules/**"]; pass an explicit list to override.',
22
23
  items: {
23
24
  type: "string",
24
25
  },
@@ -35,6 +36,10 @@ export class GlobTool extends SdkCallbackTool {
35
36
  const signal = ctx?.signal;
36
37
  try {
37
38
  const cwd = input.path ? this.resolve(input.path) : this.env.cwd;
39
+ // 基目录不存在/不是目录时明确报错:静默返回 [] 会被模型当成「文件不存在」
40
+ const baseError = await checkBaseDirectory(cwd);
41
+ if (baseError)
42
+ return baseError;
38
43
  const result = [];
39
44
  for await (const file of fsp.glob(input.pattern, {
40
45
  exclude: input.exclude || ["**/node_modules/**"],
@@ -68,9 +73,9 @@ export class GlobTool extends SdkCallbackTool {
68
73
  const preview = headPreview(json);
69
74
  return JSON.stringify({
70
75
  outputTooLarge: true,
71
- warning: `输出过大:匹配结果共 ${json.length} 字符,超过上限 ${limit} 字符。` +
72
- `完整结果已落盘(result.json),可用 read 分批读取;` +
73
- `也可用更精确的 pattern 或 exclude 缩小范围后重新查找。`,
76
+ warning: `Output too large: the matches total ${json.length} characters, exceeding the limit of ${limit} characters. ` +
77
+ `The full result has been written to disk (result.json) and can be read in chunks with read; ` +
78
+ `alternatively, narrow the scope with a more precise pattern or exclude and search again.`,
74
79
  files: ctx.files,
75
80
  chars: json.length,
76
81
  ...preview,
@@ -12,17 +12,17 @@ import { DEFAULT_MAX_TOOL_OUTPUT } from "./outputGuard.js";
12
12
  export class InspectFileTool extends SdkCallbackTool {
13
13
  function = {
14
14
  name: "inspect_file",
15
- description: "勘察文件的结构概况:总行数、总字符数、最长行的列数及其行索引、平均列数、行尾类型(LF/CRLF)。可选返回每行的列数映射。适用于在 read 之前先了解文件规模,再决定用 range 读取哪一段。仅做流式扫描,不受 read 的 300KB 读取阈值限制。",
15
+ description: "Inspect a file's structural overview: total lines, total characters, the longest line's column count and its line index, average column count, and line ending type (LF/CRLF). Optionally returns a per-line column count map. Useful for sizing up a file before reading it, so you can decide which range to read. Streaming scan only, not subject to the 300KB read threshold of read.",
16
16
  parameters: {
17
17
  type: "object",
18
18
  properties: {
19
19
  path: {
20
20
  type: "string",
21
- description: "文件路径",
21
+ description: "File path",
22
22
  },
23
23
  withColCountMap: {
24
24
  type: "boolean",
25
- description: "是否返回每行列数映射(对象:行索引 → 列数,行索引从 0 开始)。默认 false。行数很多时该映射可能过大,此时将只返回概况字段。",
25
+ description: "Whether to return a per-line column count map (object: line index → column count, line index is 0-based). Default false. When there are many lines the map may be too large; in that case only the overview fields are returned.",
26
26
  default: false,
27
27
  },
28
28
  },
@@ -38,7 +38,7 @@ export class InspectFileTool extends SdkCallbackTool {
38
38
  const filePath = this.resolve(input.path);
39
39
  const stats = await fsp.stat(filePath);
40
40
  if (!stats.isFile()) {
41
- return `"${filePath}" 不是文件`;
41
+ return `"${filePath}" is not a file`;
42
42
  }
43
43
  const withColCountMap = input.withColCountMap ?? false;
44
44
  const result = await scan(filePath, withColCountMap);
@@ -61,9 +61,9 @@ export class InspectFileTool extends SdkCallbackTool {
61
61
  return JSON.stringify({
62
62
  ...summary,
63
63
  colCountMapOmitted: true,
64
- warning: `行长度明细过大:概况与 colCountMap 合计 ${payload.length} 字符,` +
65
- `超过上限 ${limit} 字符,未落盘返回。文件规模超出可处理范围,` +
66
- `建议改用 find_text 定位后再按 range 分段读取。`,
64
+ warning: `Line length details too large: overview plus colCountMap total ${payload.length} characters, ` +
65
+ `exceeding the limit of ${limit} characters; not written to disk. The file is beyond the supported size, ` +
66
+ `so use find_text to locate the target and then read it in chunks by range.`,
67
67
  });
68
68
  }
69
69
  return payload;
@@ -4,13 +4,13 @@ import { DEFAULT_MAX_TOOL_OUTPUT, guardOutput, headPreview } from "./outputGuard
4
4
  export class LsTool extends SdkCallbackTool {
5
5
  function = {
6
6
  name: "ls",
7
- description: "列出目录",
7
+ description: "List a directory",
8
8
  parameters: {
9
9
  type: "object",
10
10
  properties: {
11
11
  path: {
12
12
  type: "string",
13
- description: "目录路径",
13
+ description: "Directory path",
14
14
  },
15
15
  },
16
16
  required: ["path"],
@@ -35,9 +35,9 @@ export class LsTool extends SdkCallbackTool {
35
35
  const preview = headPreview(json);
36
36
  return JSON.stringify({
37
37
  outputTooLarge: true,
38
- warning: `输出过大:目录条目共 ${entries.length} 项、${json.length} 字符,超过上限 ${limit} 字符。` +
39
- `完整列表已落盘(result.json),可用 read 分批读取;` +
40
- `也可改用 glob 按模式筛选。`,
38
+ warning: `Output too large: ${entries.length} directory entries, ${json.length} characters in total, exceeding the limit of ${limit} characters. ` +
39
+ `The full list has been written to disk (result.json) and can be read in chunks with read; ` +
40
+ `alternatively, use glob to filter by pattern.`,
41
41
  files: ctx.files,
42
42
  chars: json.length,
43
43
  ...preview,
@@ -3,17 +3,17 @@ import { SdkCallbackTool } from "./SdkCallbackTool.js";
3
3
  export class MkdirTool extends SdkCallbackTool {
4
4
  function = {
5
5
  name: "mkdir",
6
- description: "创建目录",
6
+ description: "Create a directory",
7
7
  parameters: {
8
8
  type: "object",
9
9
  properties: {
10
10
  path: {
11
11
  type: "string",
12
- description: "目录路径",
12
+ description: "Directory path",
13
13
  },
14
14
  recursive: {
15
15
  type: "boolean",
16
- description: "是否递归创建子目录",
16
+ description: "Whether to create parent directories recursively",
17
17
  },
18
18
  },
19
19
  required: ["path"],
@@ -4,18 +4,18 @@ import { DEFAULT_MAX_TOOL_OUTPUT, guardOutput } from "./outputGuard.js";
4
4
  export class ReadTool extends SdkCallbackTool {
5
5
  function = {
6
6
  name: "read",
7
- description: "读取文件",
7
+ description: "Read a file",
8
8
  parameters: {
9
9
  type: "object",
10
10
  properties: {
11
11
  path: {
12
12
  type: "string",
13
- description: "文件路径",
13
+ description: "File path",
14
14
  },
15
15
  range: {
16
16
  type: "array",
17
- description: "读取范围 [起始行, 起始列, 结束行, 结束列],行列均从 0 开始,-1 表示末位;" +
18
- "例如 [0, 0, 100, -1] 表示从第 0 行第 0 列读到第 100 行最后一列。省略则读取整个文件。",
17
+ description: "Read range [startLine, startCol, endLine, endCol]; rows and columns are 0-based, -1 means the last position. " +
18
+ "For example, [0, 0, 100, -1] reads from line 0, column 0 through the last column of line 100; omit it to read the whole file.",
19
19
  items: {
20
20
  type: "number",
21
21
  },
@@ -34,7 +34,7 @@ export class ReadTool extends SdkCallbackTool {
34
34
  const stats = await fsp.stat(filePath);
35
35
  // 读取阈值:文件过大时拒绝读取(与输出阈值 maxToolOutput 无关)
36
36
  if (stats.size > 300 * 1024) {
37
- throw new Error(`文件过大,无法读取,当前文件大小 ${stats.size} 字节`);
37
+ throw new Error(`File too large to read: ${stats.size} bytes`);
38
38
  }
39
39
  const content = await fsp.readFile(filePath, "utf-8");
40
40
  const output = input.range ? sliceByRange(content, input.range) : content;
@@ -44,9 +44,9 @@ export class ReadTool extends SdkCallbackTool {
44
44
  tool: "read",
45
45
  content: output,
46
46
  isOverLimit: (text) => text.length > limit,
47
- buildWarning: () => `读取内容过大:本次输出 ${output.length} 字符,超过上限 ${limit} 字符,未落盘。` +
48
- `文件共 ${countLines(content)} 行、${content.length} 字符,` +
49
- `请用 range 参数分范围读取(形如 [起始行, 起始列, 结束行, 结束列],行列从 0 开始,-1 表示末位)。`,
47
+ buildWarning: () => `Output too large: this read returned ${output.length} characters, exceeding the limit of ${limit} characters; not written to disk. ` +
48
+ `The file has ${countLines(content)} lines and ${content.length} characters. ` +
49
+ `Use the range parameter to read it in chunks (format [startLine, startCol, endLine, endCol], 0-based, -1 means the last position).`,
50
50
  });
51
51
  }
52
52
  catch (error) {
@@ -3,17 +3,17 @@ import { SdkCallbackTool } from "./SdkCallbackTool.js";
3
3
  export class RenameTool extends SdkCallbackTool {
4
4
  function = {
5
5
  name: "rename",
6
- description: "重命名或移动文件/目录(相当于 mv 命令)",
6
+ description: "Rename or move a file/directory (equivalent to the mv command)",
7
7
  parameters: {
8
8
  type: "object",
9
9
  properties: {
10
10
  oldPath: {
11
11
  type: "string",
12
- description: "原路径",
12
+ description: "Original path",
13
13
  },
14
14
  newPath: {
15
15
  type: "string",
16
- description: "新路径",
16
+ description: "New path",
17
17
  },
18
18
  },
19
19
  required: ["oldPath", "newPath"],
@@ -3,17 +3,17 @@ import { SdkCallbackTool } from "./SdkCallbackTool.js";
3
3
  export class RmTool extends SdkCallbackTool {
4
4
  function = {
5
5
  name: "rm",
6
- description: "删除文件或目录",
6
+ description: "Delete a file or directory",
7
7
  parameters: {
8
8
  type: "object",
9
9
  properties: {
10
10
  path: {
11
11
  type: "string",
12
- description: "文件或目录路径",
12
+ description: "File or directory path",
13
13
  },
14
14
  recursive: {
15
15
  type: "boolean",
16
- description: "是否递归删除子目录",
16
+ description: "Whether to delete subdirectories recursively",
17
17
  },
18
18
  },
19
19
  required: ["path"],
@@ -2,13 +2,13 @@ import { SdkCallbackTool } from "./SdkCallbackTool.js";
2
2
  export class SleepTool extends SdkCallbackTool {
3
3
  function = {
4
4
  name: "sleep",
5
- description: "等待指定的时间(毫秒)。用于需要延迟执行的场景,例如等待其他操作完成、操作间隔控制等。注意:过长的等待时间会影响响应速度,建议不超过 30000 毫秒(30 秒)。",
5
+ description: "Wait for the specified time in milliseconds. Use it when a delay is needed, for example to wait for another operation to finish or to space out actions. Note: long waits slow down responses; keep it at or below 30000 milliseconds (30 seconds).",
6
6
  parameters: {
7
7
  type: "object",
8
8
  properties: {
9
9
  ms: {
10
10
  type: "number",
11
- description: "等待的毫秒数(1 秒 = 1000 毫秒)。建议不超过 30000(30 秒)。",
11
+ description: "Milliseconds to wait (1 second = 1000 milliseconds). Values above 30000 (30 seconds) are not recommended.",
12
12
  },
13
13
  },
14
14
  required: ["ms"],
@@ -23,14 +23,14 @@ export class SleepTool extends SdkCallbackTool {
23
23
  if (typeof ms !== "number" || !Number.isFinite(ms) || ms < 0) {
24
24
  return JSON.stringify({
25
25
  success: false,
26
- error: "ms 必须是正整数",
26
+ error: "ms must be a positive integer",
27
27
  });
28
28
  }
29
29
  if (ms > 300_000) {
30
30
  // 超过 5 分钟给出警告
31
31
  return JSON.stringify({
32
32
  success: false,
33
- error: `等待时间 ${ms}ms 过长,建议不超过 300000ms(5 分钟)。如需长时间等待请分段执行。`,
33
+ error: `Wait time ${ms}ms is too long, keep it at or below 300000ms (5 minutes). For longer waits, split them into shorter steps`,
34
34
  });
35
35
  }
36
36
  // 若 signal 已处于 aborted,直接返回中断结果,避免不必要的等待
@@ -53,14 +53,14 @@ export class SleepTool extends SdkCallbackTool {
53
53
  return JSON.stringify({
54
54
  success: false,
55
55
  aborted: true,
56
- error: "等待被中断(aborted)",
56
+ error: "Wait aborted",
57
57
  });
58
58
  }
59
59
  return JSON.stringify({
60
60
  success: true,
61
61
  waitedMs: ms,
62
62
  waitedSeconds: (ms / 1000).toFixed(1),
63
- message: `已等待 ${ms}ms(${(ms / 1000).toFixed(1)} 秒)`,
63
+ message: `Waited ${ms}ms (${(ms / 1000).toFixed(1)} seconds)`,
64
64
  });
65
65
  }
66
66
  }