coremind-tools 0.2.0-beta.2 → 0.3.0-rc.1

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.
@@ -0,0 +1,92 @@
1
+ import { readFile, realpath } from "node:fs/promises";
2
+ import path from "node:path";
3
+ import { createTwoFilesPatch } from "diff";
4
+ export class DiffLimitError extends Error {
5
+ code;
6
+ constructor(code, message) {
7
+ super(message);
8
+ this.code = code;
9
+ this.name = "DiffLimitError";
10
+ }
11
+ }
12
+ /** 为 UTF-8 文本生成标准 unified diff,并对输入、复杂度和输出分别限流。 */
13
+ export function createUnifiedDiff(before, after, options = {}) {
14
+ const maxInputBytes = options.maxInputBytes ?? 10 * 1024 * 1024;
15
+ const maxOutputBytes = options.maxOutputBytes ?? 2 * 1024 * 1024;
16
+ const inputBytes = Buffer.byteLength(before, "utf8") + Buffer.byteLength(after, "utf8");
17
+ if (inputBytes > maxInputBytes) {
18
+ throw new DiffLimitError("diff_input_limit", `diff 输入 ${inputBytes} 字节,超过 ${maxInputBytes} 字节上限`);
19
+ }
20
+ const patch = createTwoFilesPatch(safeHeader(options.oldPath ?? "before"), safeHeader(options.newPath ?? "after"), before, after, undefined, undefined, {
21
+ context: options.context ?? 3,
22
+ maxEditLength: options.maxEditLength ?? 50_000,
23
+ });
24
+ if (patch === undefined) {
25
+ throw new DiffLimitError("diff_complexity_limit", "diff 编辑距离超过计算上限");
26
+ }
27
+ const outputBytes = Buffer.byteLength(patch, "utf8");
28
+ if (outputBytes > maxOutputBytes) {
29
+ throw new DiffLimitError("diff_output_limit", `diff 输出 ${outputBytes} 字节,超过 ${maxOutputBytes} 字节上限`);
30
+ }
31
+ return patch;
32
+ }
33
+ /** 对两个工作区普通文件使用同一 diff 实现;路径解析包含目录链接逃逸检查。 */
34
+ export async function diffFiles(options) {
35
+ const beforeFile = await safeWorkspaceFile(options.cwd, options.beforePath);
36
+ const afterFile = await safeWorkspaceFile(options.cwd, options.afterPath);
37
+ const [before, after] = await Promise.all([readFile(beforeFile), readFile(afterFile)]);
38
+ const maxInputBytes = options.maxInputBytes ?? 10 * 1024 * 1024;
39
+ if (before.byteLength + after.byteLength > maxInputBytes) {
40
+ throw new DiffLimitError("diff_input_limit", `文件总大小超过 ${maxInputBytes} 字节上限`);
41
+ }
42
+ return createUnifiedDiff(before.toString("utf8"), after.toString("utf8"), {
43
+ ...options,
44
+ oldPath: options.oldPath ?? options.beforePath,
45
+ newPath: options.newPath ?? options.afterPath,
46
+ maxInputBytes,
47
+ });
48
+ }
49
+ function safeHeader(value) {
50
+ return value.replace(/[\r\n\t]/g, "_");
51
+ }
52
+ async function safeWorkspaceFile(cwd, input) {
53
+ if (input.length === 0 || input.includes("\0")) {
54
+ throw new DiffLimitError("diff_path_outside_workspace", "diff 文件路径非法");
55
+ }
56
+ const lexicalRoot = path.resolve(cwd);
57
+ const lexicalTarget = path.resolve(lexicalRoot, input);
58
+ if (isOutside(lexicalRoot, lexicalTarget)) {
59
+ throw new DiffLimitError("diff_path_outside_workspace", `diff 文件超出工作区:${input}`);
60
+ }
61
+ const [canonicalRoot, canonicalTarget] = await Promise.all([
62
+ canonicalize(lexicalRoot),
63
+ canonicalize(lexicalTarget),
64
+ ]);
65
+ if (isOutside(canonicalRoot, canonicalTarget)) {
66
+ throw new DiffLimitError("diff_path_outside_workspace", `diff 文件超出工作区:${input}`);
67
+ }
68
+ return canonicalTarget;
69
+ }
70
+ function isOutside(root, target) {
71
+ const relative = path.relative(root, target);
72
+ return relative === ".." || relative.startsWith(`..${path.sep}`) || path.isAbsolute(relative);
73
+ }
74
+ async function canonicalize(input) {
75
+ let current = path.resolve(input);
76
+ const missing = [];
77
+ while (true) {
78
+ try {
79
+ return path.join(await realpath(current), ...missing.reverse());
80
+ }
81
+ catch (error) {
82
+ if (error.code !== "ENOENT")
83
+ return current;
84
+ const parent = path.dirname(current);
85
+ if (parent === current)
86
+ return path.resolve(input);
87
+ missing.push(path.basename(current));
88
+ current = parent;
89
+ }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I,CAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;IAC7C,OAAO,QAAQ,KAAK,IAAI,IAAI,QAAQ,CAAC,UAAU,CAAC,KAAK,IAAI,CAAC,GAAG,EAAE,CAAC,IAAI,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC;AAChG,CAAC;AAED,KAAK,UAAU,YAAY,CAAC,KAAa;IACvC,IAAI,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;IAClC,MAAM,OAAO,GAAa,EAAE,CAAC;IAC7B,OAAO,IAAI,EAAE,CAAC;QACZ,IAAI,CAAC;YACH,OAAO,IAAI,CAAC,IAAI,CAAC,MAAM,QAAQ,CAAC,OAAO,CAAC,EAAE,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC,CAAC;QAClE,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAK,KAA+B,CAAC,IAAI,KAAK,QAAQ;gBAAE,OAAO,OAAO,CAAC;YACvE,MAAM,MAAM,GAAG,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;YACrC,IAAI,MAAM,KAAK,OAAO;gBAAE,OAAO,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;YACnD,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC;YACrC,OAAO,GAAG,MAAM,CAAC;QACnB,CAAC;IACH,CAAC;AACH,CAAC"}
package/package.json CHANGED
@@ -1,11 +1,17 @@
1
1
  {
2
2
  "name": "coremind-tools",
3
- "version": "0.2.0-beta.2",
3
+ "version": "0.3.0-rc.1",
4
4
  "description": "CoreMind 工具层:内置工具注册表与自定义脚本工具加载",
5
5
  "license": "MIT",
6
- "repository": { "type": "git", "url": "git+https://github.com/Eclipseic1848/CoreMind.git", "directory": "packages/coremind-tools" },
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/Eclipseic1848/CoreMind.git",
9
+ "directory": "packages/coremind-tools"
10
+ },
7
11
  "homepage": "https://github.com/Eclipseic1848/CoreMind#readme",
8
- "bugs": { "url": "https://github.com/Eclipseic1848/CoreMind/issues" },
12
+ "bugs": {
13
+ "url": "https://github.com/Eclipseic1848/CoreMind/issues"
14
+ },
9
15
  "type": "module",
10
16
  "main": "./dist/index.js",
11
17
  "types": "./dist/index.d.ts",
@@ -29,8 +35,11 @@
29
35
  },
30
36
  "dependencies": {
31
37
  "@anthropic-ai/sandbox-runtime": "0.0.71",
32
- "@earendil-works/pi-agent-core": "0.83.0",
33
- "@earendil-works/pi-coding-agent": "^0.84.1",
34
- "coremind-config": "0.2.0-beta.2"
38
+ "@earendil-works/pi-agent-core": "0.84.1",
39
+ "@earendil-works/pi-ai": "0.84.1",
40
+ "@earendil-works/pi-coding-agent": "0.84.1",
41
+ "coremind-config": "0.3.0-rc.1",
42
+ "diff": "9.0.0",
43
+ "execa": "10.0.1"
35
44
  }
36
45
  }