@wrongstack/tools 0.310.1 → 0.313.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.
- package/dist/_shell-pick.d.ts +5 -10
- package/dist/audit.js +10 -4055
- package/dist/bash.js +9 -2332
- package/dist/batch-tool-use.js +2 -187
- package/dist/browser/index.js +25 -1339
- package/dist/builtin.js +69 -33985
- package/dist/chunk-2CTB3UK5.js +197 -0
- package/dist/chunk-2ME7ZWDD.js +1272 -0
- package/dist/chunk-2TFQDEPU.js +113 -0
- package/dist/chunk-2ZCJNKFE.js +315 -0
- package/dist/chunk-3PVYQFRQ.js +472 -0
- package/dist/chunk-3QMQBO5Z.js +89 -0
- package/dist/chunk-42AKKX7O.js +385 -0
- package/dist/chunk-42SKEETB.js +113 -0
- package/dist/chunk-4BHUSMNL.js +104 -0
- package/dist/chunk-4I4LWFOO.js +431 -0
- package/dist/chunk-553IQUMO.js +478 -0
- package/dist/chunk-55KF77OK.js +1944 -0
- package/dist/chunk-6HLZK37X.js +359 -0
- package/dist/chunk-6RNIFLG4.js +305 -0
- package/dist/chunk-A6BRWIXN.js +317 -0
- package/dist/chunk-AHHDWNXC.js +200 -0
- package/dist/chunk-B2CIOKWP.js +899 -0
- package/dist/chunk-BTSFTGIL.js +7 -0
- package/dist/chunk-D33S4RQG.js +222 -0
- package/dist/chunk-DIJKHSUO.js +134 -0
- package/dist/chunk-DPL47QGS.js +3515 -0
- package/dist/chunk-EMRGN4KM.js +170 -0
- package/dist/chunk-FGOABE4Z.js +955 -0
- package/dist/chunk-FW65TP4G.js +520 -0
- package/dist/chunk-GOIZNNOA.js +221 -0
- package/dist/chunk-H5MJUAA6.js +295 -0
- package/dist/chunk-H5OHH6F4.js +223 -0
- package/dist/chunk-HC5RM4WY.js +199 -0
- package/dist/chunk-HEQFMLZ7.js +190 -0
- package/dist/chunk-HFXSNKCV.js +196 -0
- package/dist/chunk-HXSBQ2SE.js +1723 -0
- package/dist/chunk-ILESNGNM.js +163 -0
- package/dist/chunk-INF5CGAO.js +137 -0
- package/dist/chunk-IWDFZ6XR.js +337 -0
- package/dist/chunk-IXMNTNW5.js +238 -0
- package/dist/chunk-J4SRJKBW.js +428 -0
- package/dist/chunk-J5SKD6LN.js +235 -0
- package/dist/chunk-JRJJX3L5.js +424 -0
- package/dist/chunk-KXNDHMUB.js +366 -0
- package/dist/chunk-LIJQMYYX.js +59 -0
- package/dist/chunk-MAN26PZ5.js +260 -0
- package/dist/chunk-MKRQMLZA.js +474 -0
- package/dist/chunk-NBDZMMG2.js +169 -0
- package/dist/chunk-NHA6BEIH.js +101 -0
- package/dist/chunk-OA6YJIWL.js +4412 -0
- package/dist/chunk-OZC255IC.js +2830 -0
- package/dist/chunk-PUR6WU2B.js +154 -0
- package/dist/chunk-Q5JPPCZC.js +12 -0
- package/dist/chunk-QBV725YG.js +128 -0
- package/dist/chunk-QHVW4AAM.js +892 -0
- package/dist/chunk-QNJ7AQ4B.js +1000 -0
- package/dist/chunk-RGISBZBD.js +231 -0
- package/dist/chunk-RJZQWEMS.js +84 -0
- package/dist/chunk-RP2ZA47V.js +15 -0
- package/dist/chunk-RXISBFR5.js +315 -0
- package/dist/chunk-S5CCLZL3.js +99 -0
- package/dist/chunk-TVTCSBJF.js +122 -0
- package/dist/chunk-U4D5S3VQ.js +21 -0
- package/dist/chunk-U4VAATKP.js +193 -0
- package/dist/chunk-UKU2ZX4C.js +203 -0
- package/dist/chunk-UNSKG6DC.js +159 -0
- package/dist/chunk-URFKEIHM.js +1091 -0
- package/dist/chunk-UTV7YAW7.js +178 -0
- package/dist/chunk-V43EQ6VR.js +68 -0
- package/dist/chunk-VB3MJGCV.js +610 -0
- package/dist/chunk-WHIIIREL.js +223 -0
- package/dist/chunk-XMIFYKUT.js +1371 -0
- package/dist/chunk-YF2RSV34.js +54 -0
- package/dist/chunk-Z6DAKQ3U.js +1 -0
- package/dist/chunk-ZIGAZFWE.js +522 -0
- package/dist/circuit-breaker.js +3 -191
- package/dist/codebase-index/index.js +84 -13688
- package/dist/codebase-index/parser-worker-script.js +8 -3995
- package/dist/codebase-index/project-server.js +60 -8548
- package/dist/codebase-index/worker.js +20 -8416
- package/dist/design.js +2 -299
- package/dist/diff.js +4 -256
- package/dist/document.js +4 -222
- package/dist/e2e.js +5 -476
- package/dist/edit.js +14 -10200
- package/dist/exec.js +16 -3131
- package/dist/fetch.js +8 -398
- package/dist/format.js +10 -3750
- package/dist/generic-parser-VUXDIAOZ.js +13 -0
- package/dist/git.js +4 -380
- package/dist/glob.js +6 -280
- package/dist/go-parser-6CA77WZ4.js +469 -0
- package/dist/grep.js +7 -604
- package/dist/index.js +318 -35423
- package/dist/install.js +10 -4091
- package/dist/json-parser-7YMDJSBJ.js +213 -0
- package/dist/json.js +4 -665
- package/dist/kanban.js +3 -2096
- package/dist/languages/index.js +23 -4664
- package/dist/legacy-bridge-7UKZT46G.js +18 -0
- package/dist/lint.js +10 -3706
- package/dist/logs.js +4 -287
- package/dist/memory.js +6 -215
- package/dist/mode.js +3 -99
- package/dist/outdated.js +4 -5170
- package/dist/pack.js +65 -33933
- package/dist/parser-dispatch-VPOFDO2U.js +14 -0
- package/dist/patch.js +13 -9935
- package/dist/plan.js +5 -3286
- package/dist/process-registry.js +8 -651
- package/dist/ps-slash.js +12 -1439
- package/dist/py-parser-GPM5VYRG.js +16 -0
- package/dist/read.js +13 -9816
- package/dist/replace.js +15 -10014
- package/dist/rs-parser-WVQ4KYCX.js +81 -0
- package/dist/scaffold.js +4 -253
- package/dist/search.js +5 -585
- package/dist/session-kanban.js +30 -972
- package/dist/skill.js +2 -215
- package/dist/task.js +5 -3410
- package/dist/test.js +10 -3773
- package/dist/todo.js +4 -2891
- package/dist/tool-help.js +3 -123
- package/dist/tool-search.js +3 -79
- package/dist/tool-tier.js +68 -34020
- package/dist/tool-use.js +2 -93
- package/dist/tree-sitter-parser-YCILHGZD.js +17 -0
- package/dist/tree.js +4 -288
- package/dist/ts-parser-YH4ZMWMH.js +232 -0
- package/dist/typecheck.js +10 -3753
- package/dist/win32.js +6 -60
- package/dist/write.js +14 -9882
- package/dist/yaml-parser-GFAOKV53.js +157 -0
- package/package.json +5 -5
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
// src/skill.ts
|
|
2
|
+
import * as fs from "node:fs/promises";
|
|
3
|
+
import * as path from "node:path";
|
|
4
|
+
import {
|
|
5
|
+
missingRequiredRuntimeTools,
|
|
6
|
+
missingRuntimeCapabilities,
|
|
7
|
+
runtimeToolReferencesFromText
|
|
8
|
+
} from "@wrongstack/core/agent-catalog";
|
|
9
|
+
import { SKILL_LIMITS, stripFrontmatter } from "@wrongstack/core/skills";
|
|
10
|
+
import { ToolValidationError } from "@wrongstack/core/types";
|
|
11
|
+
var MAX_BODY_CHARS = SKILL_LIMITS.MAX_SKILL_BODY_CHARS;
|
|
12
|
+
var MAX_RESOURCE_CHARS = SKILL_LIMITS.MAX_RESOURCE_CHARS;
|
|
13
|
+
var MAX_LISTED_RESOURCES = SKILL_LIMITS.MAX_LISTED_RESOURCES;
|
|
14
|
+
var SKIP_DIRS = /* @__PURE__ */ new Set(["node_modules", ".git", "dist", "build"]);
|
|
15
|
+
function makeSkillTool(skillLoader) {
|
|
16
|
+
return {
|
|
17
|
+
name: "skill",
|
|
18
|
+
category: "Skills",
|
|
19
|
+
description: "Load a skill's instructions or a bundled resource on demand (agentskills.io progressive disclosure). With only `name`: returns the SKILL.md body + a list of bundled resource files. With `name` + `resource` (a relative path like `references/REF.md` or `scripts/extract.py`): returns that file content. Prefer this over the read tool for skill files \u2014 it reaches foreign skills outside the project root.",
|
|
20
|
+
usageHint: 'Load a skill body or one of its bundled resources (progressive disclosure).\n\nWHEN TO USE:\n- A task matches a skill trigger \u2192 load the body: skill({ name })\n- You need a reference/template/script bundled with the skill \u2192 skill({ name, resource: "references/REF.md" })\n\nThe body call lists every bundled resource; load the ones you need. Run scripts via bash using the returned abs path.',
|
|
21
|
+
permission: "auto",
|
|
22
|
+
mutating: false,
|
|
23
|
+
capabilities: ["fs.read"],
|
|
24
|
+
icon: "document",
|
|
25
|
+
timeoutMs: 5e3,
|
|
26
|
+
inputSchema: {
|
|
27
|
+
type: "object",
|
|
28
|
+
properties: {
|
|
29
|
+
name: {
|
|
30
|
+
type: "string",
|
|
31
|
+
description: "Exact skill name (as shown in the available-skills list)."
|
|
32
|
+
},
|
|
33
|
+
resource: {
|
|
34
|
+
type: "string",
|
|
35
|
+
description: "Optional relative path of a bundled resource to load (e.g. references/REF.md, scripts/extract.py, assets/template.html). Omit to list resources."
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
required: ["name"]
|
|
39
|
+
},
|
|
40
|
+
async execute(input, ctx) {
|
|
41
|
+
const name = input?.name?.trim();
|
|
42
|
+
if (!name) {
|
|
43
|
+
throw new ToolValidationError({ message: "skill: name is required", field: "name" });
|
|
44
|
+
}
|
|
45
|
+
const manifest = await skillLoader.find(name);
|
|
46
|
+
if (!manifest) {
|
|
47
|
+
throw new ToolValidationError({
|
|
48
|
+
message: `skill "${name}" not found \u2014 use /skill to list available skills`,
|
|
49
|
+
field: "name"
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
const availableToolNames = (ctx?.catalogTools ?? ctx?.tools ?? []).map((tool) => tool.name);
|
|
53
|
+
const missingCapabilities = missingRuntimeCapabilities(
|
|
54
|
+
manifest.requiredCapabilities,
|
|
55
|
+
availableToolNames
|
|
56
|
+
);
|
|
57
|
+
const missingTools = missingRequiredRuntimeTools(manifest.requiredTools, availableToolNames);
|
|
58
|
+
if (missingCapabilities.length > 0 || missingTools.length > 0) {
|
|
59
|
+
throw new ToolValidationError({
|
|
60
|
+
message: `skill "${name}" is unavailable in this runtime; ` + [
|
|
61
|
+
missingCapabilities.length > 0 ? `missing capabilities: ${missingCapabilities.join(", ")}` : "",
|
|
62
|
+
missingTools.length > 0 ? `missing tools: ${missingTools.join(", ")}` : ""
|
|
63
|
+
].filter(Boolean).join("; "),
|
|
64
|
+
field: "name"
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
const dir = path.dirname(manifest.path);
|
|
68
|
+
let loadedResource;
|
|
69
|
+
if (input.resource?.trim()) {
|
|
70
|
+
loadedResource = await loadResource(dir, input.resource.trim());
|
|
71
|
+
}
|
|
72
|
+
const raw = await skillLoader.readBody(name);
|
|
73
|
+
const missingBodyTools = missingRequiredRuntimeTools(
|
|
74
|
+
runtimeToolReferencesFromText(raw),
|
|
75
|
+
availableToolNames
|
|
76
|
+
);
|
|
77
|
+
const warning = missingBodyTools.length > 0 ? `Warning: skill "${name}" references tools not registered in this runtime: ${missingBodyTools.join(", ")}. Steps that call them may be unavailable.` : void 0;
|
|
78
|
+
const body = stripFrontmatter(raw).trim().slice(0, MAX_BODY_CHARS);
|
|
79
|
+
const resources = loadedResource ? [] : await listResources(dir);
|
|
80
|
+
try {
|
|
81
|
+
await ctx?.session?.append({
|
|
82
|
+
type: "skill_activated",
|
|
83
|
+
ts: (/* @__PURE__ */ new Date()).toISOString(),
|
|
84
|
+
skillName: manifest.name
|
|
85
|
+
});
|
|
86
|
+
} catch {
|
|
87
|
+
}
|
|
88
|
+
return {
|
|
89
|
+
name: manifest.name,
|
|
90
|
+
description: manifest.description,
|
|
91
|
+
body,
|
|
92
|
+
resources,
|
|
93
|
+
dir,
|
|
94
|
+
loadedResource,
|
|
95
|
+
warning
|
|
96
|
+
};
|
|
97
|
+
},
|
|
98
|
+
serialize(output) {
|
|
99
|
+
const warningLine = output.warning ? `
|
|
100
|
+
|
|
101
|
+
${output.warning}` : "";
|
|
102
|
+
if (output.loadedResource) {
|
|
103
|
+
const lr = output.loadedResource;
|
|
104
|
+
const note = lr.truncated ? ` (truncated to ${lr.content.length} chars of ${lr.bytes} B)` : "";
|
|
105
|
+
return `# Resource: ${output.name}/${lr.rel}
|
|
106
|
+
(abs path: ${lr.absPath})${note}
|
|
107
|
+
|
|
108
|
+
${lr.content}${warningLine}`;
|
|
109
|
+
}
|
|
110
|
+
const head = `# Skill: ${output.name}
|
|
111
|
+
${output.description}
|
|
112
|
+
|
|
113
|
+
${output.body}`;
|
|
114
|
+
if (output.resources.length === 0) return `${head}${warningLine}`;
|
|
115
|
+
const listing = output.resources.map((r) => `- ${r.path} (${r.bytes} B)`).join("\n");
|
|
116
|
+
return `${head}
|
|
117
|
+
|
|
118
|
+
## Bundled resources (load on demand)
|
|
119
|
+
Load any with: \`skill({ name: "${output.name}", resource: "<path>" })\`. Run scripts via bash using their abs path under ${output.dir}.
|
|
120
|
+
${listing}${warningLine}`;
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
async function loadResource(skillDir, rel) {
|
|
125
|
+
const norm = rel.replace(/\\/g, "/");
|
|
126
|
+
if (path.isAbsolute(rel) || norm.split("/").some((seg) => seg === "..")) {
|
|
127
|
+
throw new ToolValidationError({
|
|
128
|
+
message: `skill: invalid resource path "${rel}"`,
|
|
129
|
+
field: "resource"
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
const absPath = path.resolve(skillDir, rel);
|
|
133
|
+
const root = path.resolve(skillDir);
|
|
134
|
+
if (absPath !== root && !absPath.startsWith(root + path.sep)) {
|
|
135
|
+
throw new ToolValidationError({
|
|
136
|
+
message: `skill: resource "${rel}" escapes the skill directory`,
|
|
137
|
+
field: "resource"
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
let realPath;
|
|
141
|
+
let realRoot;
|
|
142
|
+
try {
|
|
143
|
+
realRoot = await fs.realpath(root);
|
|
144
|
+
realPath = await fs.realpath(absPath);
|
|
145
|
+
} catch {
|
|
146
|
+
throw new ToolValidationError({
|
|
147
|
+
message: `skill: resource "${rel}" not readable`,
|
|
148
|
+
field: "resource"
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
if (realPath !== realRoot && !realPath.startsWith(realRoot + path.sep)) {
|
|
152
|
+
throw new ToolValidationError({
|
|
153
|
+
message: `skill: resource "${rel}" resolves outside the skill directory`,
|
|
154
|
+
field: "resource"
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
let buf;
|
|
158
|
+
try {
|
|
159
|
+
buf = await fs.readFile(realPath);
|
|
160
|
+
} catch {
|
|
161
|
+
throw new ToolValidationError({
|
|
162
|
+
message: `skill: resource "${rel}" not readable`,
|
|
163
|
+
field: "resource"
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
const raw = buf.toString("utf8");
|
|
167
|
+
const truncated = raw.length > MAX_RESOURCE_CHARS;
|
|
168
|
+
return {
|
|
169
|
+
rel: norm,
|
|
170
|
+
// The canonical path — the one actually opened, and the one a follow-up
|
|
171
|
+
// `bash` invocation should use.
|
|
172
|
+
absPath: realPath,
|
|
173
|
+
content: truncated ? raw.slice(0, MAX_RESOURCE_CHARS) : raw,
|
|
174
|
+
bytes: buf.length,
|
|
175
|
+
truncated
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
async function listResources(skillDir) {
|
|
179
|
+
const out = [];
|
|
180
|
+
await walk(skillDir, skillDir, out);
|
|
181
|
+
out.sort((a, b) => a.path.localeCompare(b.path));
|
|
182
|
+
return out.slice(0, MAX_LISTED_RESOURCES);
|
|
183
|
+
}
|
|
184
|
+
async function walk(root, dir, out) {
|
|
185
|
+
if (out.length >= MAX_LISTED_RESOURCES) return;
|
|
186
|
+
let entries;
|
|
187
|
+
try {
|
|
188
|
+
entries = await fs.readdir(dir, { withFileTypes: true });
|
|
189
|
+
} catch {
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
for (const e of entries) {
|
|
193
|
+
if (out.length >= MAX_LISTED_RESOURCES) return;
|
|
194
|
+
const fullPath = path.join(dir, e.name);
|
|
195
|
+
let isDir = e.isDirectory();
|
|
196
|
+
if (e.isSymbolicLink()) {
|
|
197
|
+
try {
|
|
198
|
+
isDir = (await fs.stat(fullPath)).isDirectory();
|
|
199
|
+
} catch {
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
if (isDir) {
|
|
204
|
+
if (SKIP_DIRS.has(e.name) || e.name.startsWith(".")) continue;
|
|
205
|
+
await walk(root, fullPath, out);
|
|
206
|
+
} else if (e.isFile()) {
|
|
207
|
+
if (e.name === "SKILL.md" || e.name === "SKILL.save.md") continue;
|
|
208
|
+
try {
|
|
209
|
+
const stat2 = await fs.stat(fullPath);
|
|
210
|
+
const rel = path.relative(root, fullPath).split(path.sep).join("/");
|
|
211
|
+
out.push({ path: rel, bytes: stat2.size });
|
|
212
|
+
} catch {
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
export {
|
|
219
|
+
makeSkillTool
|
|
220
|
+
};
|
|
221
|
+
//# sourceMappingURL=chunk-GOIZNNOA.js.map
|
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getIndexState,
|
|
3
|
+
searchCodebaseIndex
|
|
4
|
+
} from "./chunk-2ME7ZWDD.js";
|
|
5
|
+
import {
|
|
6
|
+
codebaseIndexDirOverride
|
|
7
|
+
} from "./chunk-OA6YJIWL.js";
|
|
8
|
+
import {
|
|
9
|
+
isBinaryBuffer,
|
|
10
|
+
safeResolveReal,
|
|
11
|
+
sha256hex
|
|
12
|
+
} from "./chunk-D33S4RQG.js";
|
|
13
|
+
|
|
14
|
+
// src/read.ts
|
|
15
|
+
import * as fs from "node:fs/promises";
|
|
16
|
+
import { FsError, ToolValidationError } from "@wrongstack/core/types";
|
|
17
|
+
import { toErrorMessage } from "@wrongstack/core/utils";
|
|
18
|
+
var ADVANCED_MODE_META_KEY = "tools.read.advancedMode";
|
|
19
|
+
var MAX_BYTES = 5 * 1024 * 1024;
|
|
20
|
+
var readTool = {
|
|
21
|
+
name: "read",
|
|
22
|
+
category: "Filesystem",
|
|
23
|
+
description: "Read the contents of a file with line numbers. This is the primary way to inspect source code, configuration, or any text file before making changes. Lines are returned 1-indexed in the form `N\u2192content` (line number, then a `\u2192` separator, then the raw line). The `N\u2192` prefix is display-only \u2014 always strip it before reusing the text, e.g. never include it in `edit.old_string`. When advanced mode is on or `includeSymbols` is set, the result also includes a `symbols` field listing codebase-index symbol names, kinds, and line numbers for the file (not file content).",
|
|
24
|
+
usageHint: "FOUNDATIONAL TOOL \u2014 call this before almost any edit operation.\n\nBest practices:\n- Always read a file before using `edit`, `replace`, or `write` on it (the system often requires it for safety).\n- Use `offset` + `limit` for very large files instead of reading everything at once.\n- Default limit is generous (2000 lines) but can be increased.\n- The output format is designed to be directly usable as context for `edit` operations.\n- Set `includeSymbols: true` to also receive the codebase-index symbol listing for the file.\n- Enable advanced mode (`ctx.meta['tools.read.advancedMode'] = true`) to auto-inject symbols on every read.",
|
|
25
|
+
selection: {
|
|
26
|
+
doNotUseWhen: "you need to search many files for matching content.",
|
|
27
|
+
useInstead: ["grep"]
|
|
28
|
+
},
|
|
29
|
+
permission: "auto",
|
|
30
|
+
mutating: false,
|
|
31
|
+
capabilities: ["fs.read"],
|
|
32
|
+
icon: "file",
|
|
33
|
+
maxOutputBytes: 262144,
|
|
34
|
+
timeoutMs: 5e3,
|
|
35
|
+
inputSchema: {
|
|
36
|
+
type: "object",
|
|
37
|
+
properties: {
|
|
38
|
+
path: {
|
|
39
|
+
type: "string",
|
|
40
|
+
description: "Path to the file (relative to project root or absolute within project)."
|
|
41
|
+
},
|
|
42
|
+
offset: {
|
|
43
|
+
type: "integer",
|
|
44
|
+
minimum: 1,
|
|
45
|
+
description: "1-based starting line number. Use together with `limit` for large files."
|
|
46
|
+
},
|
|
47
|
+
limit: {
|
|
48
|
+
type: "integer",
|
|
49
|
+
minimum: 0,
|
|
50
|
+
description: "Maximum number of lines to return (default 2000). Values above 5000 are clamped to 5000 \u2014 page with `offset` for more."
|
|
51
|
+
},
|
|
52
|
+
mode: {
|
|
53
|
+
type: "string",
|
|
54
|
+
enum: ["content", "summary"],
|
|
55
|
+
description: "Return full line-numbered content (default) or a compact file summary with imports/exports/symbols."
|
|
56
|
+
},
|
|
57
|
+
includeSymbols: {
|
|
58
|
+
type: "boolean",
|
|
59
|
+
description: "When true, include the codebase-index symbol list for this file as a structured `symbols` field in the result. Overrides the advanced-mode meta flag per-call."
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
required: ["path"]
|
|
63
|
+
},
|
|
64
|
+
async execute(input, ctx, execOpts) {
|
|
65
|
+
if (!input?.path) {
|
|
66
|
+
throw new ToolValidationError({
|
|
67
|
+
message: "read: path is required",
|
|
68
|
+
field: "path"
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
const absPath = await safeResolveReal(input.path, ctx);
|
|
72
|
+
const shouldIncludeSymbols = input.includeSymbols === true || input.includeSymbols !== false && ctx.meta[ADVANCED_MODE_META_KEY] === true;
|
|
73
|
+
let stat2;
|
|
74
|
+
try {
|
|
75
|
+
stat2 = await fs.stat(absPath);
|
|
76
|
+
} catch (err) {
|
|
77
|
+
const code = err.code;
|
|
78
|
+
if (code === "ENOENT") {
|
|
79
|
+
throw new FsError({
|
|
80
|
+
message: `read: file not found "${input.path}"`,
|
|
81
|
+
code: "FS_READ_FAILED",
|
|
82
|
+
path: absPath,
|
|
83
|
+
context: { errno: "ENOENT" }
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
throw new FsError({
|
|
87
|
+
message: `read: failed to stat "${input.path}": ${toErrorMessage(err)}`,
|
|
88
|
+
code: "FS_READ_FAILED",
|
|
89
|
+
path: absPath,
|
|
90
|
+
context: { errno: code },
|
|
91
|
+
cause: err
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
if (!stat2.isFile()) {
|
|
95
|
+
throw new FsError({
|
|
96
|
+
message: `read: "${input.path}" is not a regular file`,
|
|
97
|
+
code: "FS_READ_FAILED",
|
|
98
|
+
path: absPath,
|
|
99
|
+
context: { reason: "not-a-regular-file" }
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
if (stat2.size > MAX_BYTES) {
|
|
103
|
+
throw new FsError({
|
|
104
|
+
message: `read: file too large (${stat2.size} bytes, limit ${MAX_BYTES})`,
|
|
105
|
+
code: "FS_READ_FAILED",
|
|
106
|
+
path: absPath,
|
|
107
|
+
context: { size: stat2.size, limit: MAX_BYTES, reason: "too-large" }
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
const offset = Math.max(1, input.offset ?? 1);
|
|
111
|
+
const limit = Math.max(0, Math.min(input.limit ?? 2e3, 5e3));
|
|
112
|
+
const prior = getReadRangeRecord(ctx, absPath);
|
|
113
|
+
const requestedEnd = prior ? Math.min(offset + limit - 1, prior.totalLines) : offset + limit - 1;
|
|
114
|
+
if (input.mode !== "summary" && limit > 0 && prior && coversRange(prior, stat2.mtimeMs, offset, requestedEnd)) {
|
|
115
|
+
ctx.recordRead(absPath, stat2.mtimeMs);
|
|
116
|
+
const symResult2 = shouldIncludeSymbols ? await fetchSymbolsForFile(absPath, ctx, execOpts?.signal) : void 0;
|
|
117
|
+
return {
|
|
118
|
+
text: `[unchanged since previous read: "${input.path}" mtime=${Math.round(stat2.mtimeMs)}; requested lines ${offset}-${requestedEnd} were already shown. Use offset/limit for a new range if needed.]`,
|
|
119
|
+
total_lines: prior.totalLines,
|
|
120
|
+
encoding: "utf8",
|
|
121
|
+
truncated: requestedEnd < prior.totalLines,
|
|
122
|
+
cached: true,
|
|
123
|
+
note: mergeSymbolNote("Repeated read suppressed to save tokens.", symResult2?.note),
|
|
124
|
+
...symResult2?.symbols ? { symbols: symResult2.symbols } : {}
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
const buf = await fs.readFile(absPath);
|
|
128
|
+
if (isBinaryBuffer(buf)) {
|
|
129
|
+
throw new FsError({
|
|
130
|
+
message: `read: "${input.path}" appears to be binary`,
|
|
131
|
+
code: "FS_READ_FAILED",
|
|
132
|
+
path: absPath,
|
|
133
|
+
context: { reason: "binary" }
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
const text = buf.toString("utf8");
|
|
137
|
+
const contentHash = sha256hex(text);
|
|
138
|
+
const allLines = text.split(/\r\n|\r|\n/);
|
|
139
|
+
const total = allLines.length;
|
|
140
|
+
if (input.mode === "summary") {
|
|
141
|
+
ctx.recordRead(absPath, stat2.mtimeMs, "user", contentHash);
|
|
142
|
+
rememberReadRange(ctx, absPath, stat2.mtimeMs, total, 1, Math.min(total, 200));
|
|
143
|
+
const symResult2 = shouldIncludeSymbols ? await fetchSymbolsForFile(absPath, ctx, execOpts?.signal) : void 0;
|
|
144
|
+
return {
|
|
145
|
+
text: summarizeFile(input.path, stat2.size, allLines),
|
|
146
|
+
total_lines: total,
|
|
147
|
+
encoding: "utf8",
|
|
148
|
+
truncated: total > 200,
|
|
149
|
+
note: mergeSymbolNote(
|
|
150
|
+
"Summary mode returned compact structure instead of full file content.",
|
|
151
|
+
symResult2?.note
|
|
152
|
+
),
|
|
153
|
+
...symResult2?.symbols ? { symbols: symResult2.symbols } : {}
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
if (limit === 0) {
|
|
157
|
+
ctx.recordRead(absPath, stat2.mtimeMs, "user", contentHash);
|
|
158
|
+
rememberReadRange(ctx, absPath, stat2.mtimeMs, total, 1, 0);
|
|
159
|
+
const symResult2 = shouldIncludeSymbols ? await fetchSymbolsForFile(absPath, ctx, execOpts?.signal) : void 0;
|
|
160
|
+
return {
|
|
161
|
+
text: "",
|
|
162
|
+
total_lines: total,
|
|
163
|
+
encoding: "utf8",
|
|
164
|
+
truncated: total > 0,
|
|
165
|
+
...symResult2?.symbols ? { symbols: symResult2.symbols } : {},
|
|
166
|
+
...symResult2?.note ? { note: symResult2.note } : {}
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
if (offset > total) {
|
|
170
|
+
ctx.recordRead(absPath, stat2.mtimeMs, "user", contentHash);
|
|
171
|
+
rememberReadRange(ctx, absPath, stat2.mtimeMs, total, total + 1, total + 1);
|
|
172
|
+
const symResult2 = shouldIncludeSymbols ? await fetchSymbolsForFile(absPath, ctx, execOpts?.signal) : void 0;
|
|
173
|
+
return {
|
|
174
|
+
text: `[offset ${offset} is past end of file "${input.path}" \u2014 file has ${total} line(s). Do not retry this offset.]`,
|
|
175
|
+
total_lines: total,
|
|
176
|
+
encoding: "utf8",
|
|
177
|
+
truncated: false,
|
|
178
|
+
...symResult2?.symbols ? { symbols: symResult2.symbols } : {},
|
|
179
|
+
...symResult2?.note ? { note: symResult2.note } : {}
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
const slice = allLines.slice(offset - 1, offset - 1 + limit);
|
|
183
|
+
const truncated = offset - 1 + slice.length < total;
|
|
184
|
+
const width = String(offset + slice.length - 1).length;
|
|
185
|
+
const numbered = slice.map((line, i) => `${String(offset + i).padStart(width, " ")}\u2192${line}`).join("\n");
|
|
186
|
+
ctx.recordRead(absPath, stat2.mtimeMs, "user", contentHash);
|
|
187
|
+
rememberReadRange(ctx, absPath, stat2.mtimeMs, total, offset, offset + slice.length - 1);
|
|
188
|
+
const symResult = shouldIncludeSymbols ? await fetchSymbolsForFile(absPath, ctx, execOpts?.signal) : void 0;
|
|
189
|
+
return {
|
|
190
|
+
text: numbered,
|
|
191
|
+
total_lines: total,
|
|
192
|
+
encoding: "utf8",
|
|
193
|
+
truncated,
|
|
194
|
+
...symResult?.symbols ? { symbols: symResult.symbols } : {},
|
|
195
|
+
...symResult?.note ? { note: symResult.note } : {}
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
async function fetchSymbolsForFile(absPath, ctx, signal) {
|
|
200
|
+
try {
|
|
201
|
+
const state = getIndexState();
|
|
202
|
+
if (!state.ready) return {};
|
|
203
|
+
const { results, total } = await searchCodebaseIndex(
|
|
204
|
+
{
|
|
205
|
+
projectRoot: ctx.projectRoot,
|
|
206
|
+
indexDir: codebaseIndexDirOverride(ctx),
|
|
207
|
+
query: "",
|
|
208
|
+
file: absPath,
|
|
209
|
+
limit: 500
|
|
210
|
+
},
|
|
211
|
+
{ signal }
|
|
212
|
+
);
|
|
213
|
+
if (results.length === 0) return {};
|
|
214
|
+
const sorted = results.map((r) => ({
|
|
215
|
+
name: r.name,
|
|
216
|
+
kind: r.kind,
|
|
217
|
+
line: r.line,
|
|
218
|
+
col: r.col,
|
|
219
|
+
signature: r.signature
|
|
220
|
+
})).sort((a, b) => a.line - b.line || a.col - b.col);
|
|
221
|
+
const result = { symbols: sorted };
|
|
222
|
+
if (total > results.length) {
|
|
223
|
+
result.note = `Symbol listing truncated to ${results.length} of ${total} entries.`;
|
|
224
|
+
}
|
|
225
|
+
return result;
|
|
226
|
+
} catch {
|
|
227
|
+
return {};
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
function mergeSymbolNote(note, symNote) {
|
|
231
|
+
if (!symNote) return note;
|
|
232
|
+
if (!note) return symNote;
|
|
233
|
+
return `${note} ${symNote}`;
|
|
234
|
+
}
|
|
235
|
+
var READ_RANGES_META_KEY = "tools.read.ranges.v1";
|
|
236
|
+
function getReadRanges(ctx) {
|
|
237
|
+
const existing = ctx.meta[READ_RANGES_META_KEY];
|
|
238
|
+
if (existing && typeof existing === "object" && !Array.isArray(existing)) {
|
|
239
|
+
return existing;
|
|
240
|
+
}
|
|
241
|
+
const next = {};
|
|
242
|
+
ctx.meta[READ_RANGES_META_KEY] = next;
|
|
243
|
+
return next;
|
|
244
|
+
}
|
|
245
|
+
function getReadRangeRecord(ctx, absPath) {
|
|
246
|
+
return getReadRanges(ctx)[absPath];
|
|
247
|
+
}
|
|
248
|
+
function rememberReadRange(ctx, absPath, mtimeMs, totalLines, start, end) {
|
|
249
|
+
if (end < start) return;
|
|
250
|
+
const ranges = getReadRanges(ctx);
|
|
251
|
+
const prior = ranges[absPath];
|
|
252
|
+
const nextRanges = prior && Math.abs(prior.mtimeMs - mtimeMs) <= 1 ? prior.ranges.slice() : [];
|
|
253
|
+
nextRanges.push({ start, end });
|
|
254
|
+
ranges[absPath] = {
|
|
255
|
+
mtimeMs,
|
|
256
|
+
totalLines,
|
|
257
|
+
ranges: mergeRanges(nextRanges)
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
function coversRange(record, mtimeMs, start, end) {
|
|
261
|
+
if (Math.abs(record.mtimeMs - mtimeMs) > 1) return false;
|
|
262
|
+
return record.ranges.some((range) => range.start <= start && range.end >= end);
|
|
263
|
+
}
|
|
264
|
+
function mergeRanges(ranges) {
|
|
265
|
+
const sorted = ranges.slice().sort((a, b) => a.start - b.start);
|
|
266
|
+
const merged = [];
|
|
267
|
+
for (const range of sorted) {
|
|
268
|
+
const last = merged[merged.length - 1];
|
|
269
|
+
if (!last || range.start > last.end + 1) {
|
|
270
|
+
merged.push({ ...range });
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
last.end = Math.max(last.end, range.end);
|
|
274
|
+
}
|
|
275
|
+
return merged;
|
|
276
|
+
}
|
|
277
|
+
function summarizeFile(filePath, bytes, lines) {
|
|
278
|
+
const interesting = lines.map((line, index) => ({ line: line.trim(), number: index + 1 })).filter(
|
|
279
|
+
({ line }) => /^(import\s|export\s|class\s|interface\s|type\s|function\s|const\s+\w+\s*=|let\s+\w+\s*=|var\s+\w+\s*=|def\s+|async\s+function\s)/.test(
|
|
280
|
+
line
|
|
281
|
+
)
|
|
282
|
+
).slice(0, 80).map(({ line, number }) => `${number}: ${line}`);
|
|
283
|
+
return [
|
|
284
|
+
`summary: ${filePath}`,
|
|
285
|
+
`bytes=${bytes}`,
|
|
286
|
+
`total_lines=${lines.length}`,
|
|
287
|
+
interesting.length > 0 ? `symbols/imports:
|
|
288
|
+
${interesting.join("\n")}` : "symbols/imports: (none detected)"
|
|
289
|
+
].join("\n");
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
export {
|
|
293
|
+
readTool
|
|
294
|
+
};
|
|
295
|
+
//# sourceMappingURL=chunk-H5MJUAA6.js.map
|