dsh-better-edit 0.2.2 → 0.3.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/CHANGELOG.md +33 -0
- package/README.md +317 -240
- package/README.zh.md +19 -11
- package/lib/constants.js +3 -1
- package/lib/contract.js +159 -63
- package/lib/drift.js +5 -98
- package/lib/dsh-context.js +2 -24
- package/lib/edit-engine.js +9 -8
- package/lib/edit-pipeline.js +3 -121
- package/lib/edit-response.js +16 -8
- package/lib/file-kind.js +6 -121
- package/lib/file-reader.js +4 -77
- package/lib/file-view.js +486 -0
- package/lib/guidance/index.js +8 -0
- package/lib/guidance/materialize.js +212 -0
- package/lib/guidance/parse.js +80 -0
- package/lib/guidance/resolve.js +116 -0
- package/lib/guidance.js +6 -388
- package/lib/hash-store.js +6 -4
- package/lib/hashline/alphabet.js +2 -6
- package/lib/hashline/anchor-pipeline.js +786 -0
- package/lib/hashline/apply.js +2 -223
- package/lib/hashline/hash-assign.js +255 -0
- package/lib/hashline/hash.js +67 -29
- package/lib/hashline/hasher.js +2 -23
- package/lib/hashline/index.js +9 -4
- package/lib/hashline/parse.js +2 -35
- package/lib/hashline/pure.js +2 -213
- package/lib/hashline/resolve.js +3 -361
- package/lib/hashline/served.js +2 -120
- package/lib/index.js +1 -3
- package/lib/mutation.js +266 -0
- package/lib/noop-guard.js +20 -0
- package/lib/preview-controller.js +65 -0
- package/lib/prompts.js +14 -20
- package/lib/read-and-serve.js +23 -23
- package/lib/read-render.js +3 -139
- package/lib/served-store.js +2 -216
- package/lib/session-view.js +332 -0
- package/lib/tool-batch-edit.js +9 -10
- package/lib/tool-edit.js +73 -129
- package/lib/tool-read.js +3 -5
- package/lib/tool-undo.js +16 -10
- package/lib/truncate.js +3 -95
- package/lib/types/constants.d.ts +2 -0
- package/lib/types/contract.d.ts +120 -15
- package/lib/types/drift.d.ts +5 -29
- package/lib/types/dsh-context.d.ts +2 -20
- package/lib/types/edit-engine.d.ts +2 -2
- package/lib/types/edit-pipeline.d.ts +3 -44
- package/lib/types/file-kind.d.ts +6 -18
- package/lib/types/file-reader.d.ts +4 -45
- package/lib/types/file-view.d.ts +139 -0
- package/lib/types/guidance/index.d.ts +8 -0
- package/lib/types/guidance/materialize.d.ts +23 -0
- package/lib/types/guidance/parse.d.ts +36 -0
- package/lib/types/guidance/resolve.d.ts +55 -0
- package/lib/types/guidance.d.ts +6 -127
- package/lib/types/hashline/alphabet.d.ts +1 -5
- package/lib/types/hashline/anchor-pipeline.d.ts +127 -0
- package/lib/types/hashline/apply.d.ts +1 -22
- package/lib/types/hashline/hash-assign.d.ts +25 -0
- package/lib/types/hashline/hash.d.ts +8 -9
- package/lib/types/hashline/hasher.d.ts +1 -8
- package/lib/types/hashline/index.d.ts +12 -4
- package/lib/types/hashline/parse.d.ts +1 -7
- package/lib/types/hashline/pure.d.ts +1 -26
- package/lib/types/hashline/resolve.d.ts +1 -58
- package/lib/types/hashline/served.d.ts +1 -43
- package/lib/types/mutation.d.ts +131 -0
- package/lib/types/noop-guard.d.ts +29 -0
- package/lib/types/preview-controller.d.ts +33 -0
- package/lib/types/prompts.d.ts +9 -9
- package/lib/types/read-and-serve.d.ts +6 -5
- package/lib/types/read-render.d.ts +3 -18
- package/lib/types/served-store.d.ts +2 -81
- package/lib/types/session-view.d.ts +92 -0
- package/lib/types/tool-edit.d.ts +3 -14
- package/lib/types/truncate.d.ts +3 -29
- package/lib/types/validation.d.ts +6 -7
- package/lib/types/workspace.d.ts +2 -21
- package/lib/validation.js +6 -33
- package/lib/workspace.js +2 -27
- package/lib/write-hook.js +2 -2
- package/package.json +2 -2
package/lib/file-view.js
ADDED
|
@@ -0,0 +1,486 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* FileView — deep module owning "what the model sees".
|
|
3
|
+
*
|
|
4
|
+
* Single seam for normalize → hash → render → truncate → served-row
|
|
5
|
+
* selection. Previously split across file-reader, read-render, truncate,
|
|
6
|
+
* file-kind, validation — each shallow (interface≈implementation). Now one
|
|
7
|
+
* file owns the invariant; deleting it would scatter complexity (deep).
|
|
8
|
+
*
|
|
9
|
+
* Private helpers inlined from file-reader (normFromText/fileSnap),
|
|
10
|
+
* read-render (fmtReadPreview), truncate (truncateHead), file-kind
|
|
11
|
+
* (loadFileKindAndText), validation (valKind/valAccess).
|
|
12
|
+
* Old files are shims re-exporting from this seam for compat.
|
|
13
|
+
*
|
|
14
|
+
* Two surfaces:
|
|
15
|
+
* - `preview` (pure, no IO) — tested without filesystem
|
|
16
|
+
* - `readView` (IO) — read + normalize + render + truncate + hashes
|
|
17
|
+
*
|
|
18
|
+
* @module dsh-better-edit/file-view
|
|
19
|
+
*/
|
|
20
|
+
import { constants } from "node:fs";
|
|
21
|
+
import { open as fsOpen, stat as fsStat } from "fs/promises";
|
|
22
|
+
import { access as fsAccess } from "fs/promises";
|
|
23
|
+
import { fileTypeFromBuffer } from "file-type";
|
|
24
|
+
import { SNIFF_BYTES, MAX_BYTES, MAX_READ_LINE_BYTES } from "./constants.js";
|
|
25
|
+
import { lineHashes, fmtRegion, HASH_SEP } from "./hashline/index.js";
|
|
26
|
+
import { HASH_SPACE } from "./hashline/hash-assign.js";
|
|
27
|
+
import { visLines, abortIf, errCode } from "./utils.js";
|
|
28
|
+
import { detectEnding, toLF, stripBOM } from "./edit-diff.js";
|
|
29
|
+
import { resolveTarget, toCwd } from "./paths.js";
|
|
30
|
+
export const MAX_HASH_LINES = HASH_SPACE;
|
|
31
|
+
export const DEFAULT_MAX_LINES = 2000;
|
|
32
|
+
export const DEFAULT_MAX_BYTES = 50 * 1024;
|
|
33
|
+
function splitLinesForCounting(content) {
|
|
34
|
+
if (content.length === 0)
|
|
35
|
+
return [];
|
|
36
|
+
const lines = content.split('\n');
|
|
37
|
+
if (content.endsWith('\n'))
|
|
38
|
+
lines.pop();
|
|
39
|
+
return lines;
|
|
40
|
+
}
|
|
41
|
+
export function formatSize(bytes) {
|
|
42
|
+
if (bytes < 1024)
|
|
43
|
+
return `${bytes}B`;
|
|
44
|
+
if (bytes < 1024 * 1024)
|
|
45
|
+
return `${(bytes / 1024).toFixed(1)}KB`;
|
|
46
|
+
return `${(bytes / (1024 * 1024)).toFixed(1)}MB`;
|
|
47
|
+
}
|
|
48
|
+
export function truncateHead(content, options = {}) {
|
|
49
|
+
const maxLines = options.maxLines ?? DEFAULT_MAX_LINES;
|
|
50
|
+
const maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES;
|
|
51
|
+
const totalBytes = Buffer.byteLength(content, 'utf-8');
|
|
52
|
+
const lines = splitLinesForCounting(content);
|
|
53
|
+
const totalLines = lines.length;
|
|
54
|
+
if (totalLines <= maxLines && totalBytes <= maxBytes) {
|
|
55
|
+
return {
|
|
56
|
+
content,
|
|
57
|
+
truncated: false,
|
|
58
|
+
truncatedBy: null,
|
|
59
|
+
totalLines,
|
|
60
|
+
totalBytes,
|
|
61
|
+
outputLines: totalLines,
|
|
62
|
+
outputBytes: totalBytes,
|
|
63
|
+
lastLinePartial: false,
|
|
64
|
+
firstLineExceedsLimit: false,
|
|
65
|
+
maxLines,
|
|
66
|
+
maxBytes,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
const firstLineBytes = Buffer.byteLength(lines[0] ?? '', 'utf-8');
|
|
70
|
+
if (firstLineBytes > maxBytes) {
|
|
71
|
+
return {
|
|
72
|
+
content: '',
|
|
73
|
+
truncated: true,
|
|
74
|
+
truncatedBy: 'bytes',
|
|
75
|
+
totalLines,
|
|
76
|
+
totalBytes,
|
|
77
|
+
outputLines: 0,
|
|
78
|
+
outputBytes: 0,
|
|
79
|
+
lastLinePartial: false,
|
|
80
|
+
firstLineExceedsLimit: true,
|
|
81
|
+
maxLines,
|
|
82
|
+
maxBytes,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
const outputLinesArr = [];
|
|
86
|
+
let outputBytesCount = 0;
|
|
87
|
+
let truncatedBy = 'lines';
|
|
88
|
+
for (let i = 0; i < lines.length && i < maxLines; i++) {
|
|
89
|
+
const line = lines[i];
|
|
90
|
+
const lineBytes = Buffer.byteLength(line, 'utf-8') + (i > 0 ? 1 : 0);
|
|
91
|
+
if (outputBytesCount + lineBytes > maxBytes) {
|
|
92
|
+
truncatedBy = 'bytes';
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
outputLinesArr.push(line);
|
|
96
|
+
outputBytesCount += lineBytes;
|
|
97
|
+
}
|
|
98
|
+
if (outputLinesArr.length >= maxLines && outputBytesCount <= maxBytes) {
|
|
99
|
+
truncatedBy = 'lines';
|
|
100
|
+
}
|
|
101
|
+
const outputContent = outputLinesArr.join('\n');
|
|
102
|
+
const finalOutputBytes = Buffer.byteLength(outputContent, 'utf-8');
|
|
103
|
+
return {
|
|
104
|
+
content: outputContent,
|
|
105
|
+
truncated: true,
|
|
106
|
+
truncatedBy,
|
|
107
|
+
totalLines,
|
|
108
|
+
totalBytes,
|
|
109
|
+
outputLines: outputLinesArr.length,
|
|
110
|
+
outputBytes: finalOutputBytes,
|
|
111
|
+
lastLinePartial: false,
|
|
112
|
+
firstLineExceedsLimit: false,
|
|
113
|
+
maxLines,
|
|
114
|
+
maxBytes,
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
// --- File kind (from file-kind.ts, private) ---
|
|
118
|
+
const IMG_TYPES = new Set([
|
|
119
|
+
"image/jpeg",
|
|
120
|
+
"image/png",
|
|
121
|
+
"image/gif",
|
|
122
|
+
"image/webp",
|
|
123
|
+
]);
|
|
124
|
+
const TEXT_TYPES = new Set([
|
|
125
|
+
"application/rtf",
|
|
126
|
+
"application/xml",
|
|
127
|
+
"application/x-ms-regedit",
|
|
128
|
+
]);
|
|
129
|
+
function detectTextBom(sample) {
|
|
130
|
+
if (sample.length >= 4 &&
|
|
131
|
+
sample[0] === 0xff &&
|
|
132
|
+
sample[1] === 0xfe &&
|
|
133
|
+
sample[2] === 0x00 &&
|
|
134
|
+
sample[3] === 0x00)
|
|
135
|
+
return "UTF-32LE";
|
|
136
|
+
if (sample.length >= 4 &&
|
|
137
|
+
sample[0] === 0x00 &&
|
|
138
|
+
sample[1] === 0x00 &&
|
|
139
|
+
sample[2] === 0xfe &&
|
|
140
|
+
sample[3] === 0xff)
|
|
141
|
+
return "UTF-32BE";
|
|
142
|
+
if (sample.length >= 2 && sample[0] === 0xff && sample[1] === 0xfe)
|
|
143
|
+
return "UTF-16LE";
|
|
144
|
+
if (sample.length >= 2 && sample[0] === 0xfe && sample[1] === 0xff)
|
|
145
|
+
return "UTF-16BE";
|
|
146
|
+
return undefined;
|
|
147
|
+
}
|
|
148
|
+
function isTextType(mimeType) {
|
|
149
|
+
return mimeType.startsWith("text/") || TEXT_TYPES.has(mimeType);
|
|
150
|
+
}
|
|
151
|
+
export async function loadFileKindAndText(filePath, options) {
|
|
152
|
+
const pathStat = await fsStat(filePath);
|
|
153
|
+
if (pathStat.isDirectory()) {
|
|
154
|
+
return { kind: "directory" };
|
|
155
|
+
}
|
|
156
|
+
if (!pathStat.isFile()) {
|
|
157
|
+
return {
|
|
158
|
+
kind: "binary",
|
|
159
|
+
description: "unsupported file type",
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
if (pathStat.size > MAX_BYTES) {
|
|
163
|
+
return {
|
|
164
|
+
kind: "binary",
|
|
165
|
+
description: `file exceeds ${MAX_BYTES} byte limit`,
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
const fileHandle = await fsOpen(filePath, "r");
|
|
169
|
+
try {
|
|
170
|
+
const buffer = Buffer.alloc(SNIFF_BYTES);
|
|
171
|
+
const { bytesRead } = await fileHandle.read(buffer, 0, SNIFF_BYTES, 0);
|
|
172
|
+
if (bytesRead === 0) {
|
|
173
|
+
return { kind: "text", text: "" };
|
|
174
|
+
}
|
|
175
|
+
const sample = buffer.subarray(0, bytesRead);
|
|
176
|
+
const textBom = detectTextBom(sample);
|
|
177
|
+
if (textBom) {
|
|
178
|
+
return {
|
|
179
|
+
kind: "binary",
|
|
180
|
+
description: `${textBom} encoded text`,
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
const detectedMimeType = (await fileTypeFromBuffer(sample))?.mime;
|
|
184
|
+
if (detectedMimeType !== undefined &&
|
|
185
|
+
!isTextType(detectedMimeType)) {
|
|
186
|
+
if (IMG_TYPES.has(detectedMimeType)) {
|
|
187
|
+
return { kind: "image", mimeType: detectedMimeType };
|
|
188
|
+
}
|
|
189
|
+
return {
|
|
190
|
+
kind: "binary",
|
|
191
|
+
description: detectedMimeType,
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
const decoder = new TextDecoder("utf-8", { fatal: false, ignoreBOM: true });
|
|
195
|
+
let hadUtf8DecodeErrors = false;
|
|
196
|
+
let newlineCount = 0;
|
|
197
|
+
const parts = [];
|
|
198
|
+
function decodeChunk(chunk, stream) {
|
|
199
|
+
const decoded = decoder.decode(chunk, { stream });
|
|
200
|
+
if (!hadUtf8DecodeErrors && decoded.includes("\uFFFD")) {
|
|
201
|
+
hadUtf8DecodeErrors = true;
|
|
202
|
+
}
|
|
203
|
+
if (options?.maxLines !== undefined) {
|
|
204
|
+
for (let i = 0; i < decoded.length; i++) {
|
|
205
|
+
if (decoded.charCodeAt(i) === 10)
|
|
206
|
+
newlineCount++;
|
|
207
|
+
}
|
|
208
|
+
if (newlineCount > options.maxLines) {
|
|
209
|
+
throw new Error(`[E_FILE_TOO_LARGE] ${options.displayPath ?? filePath} has more than ${options.maxLines} lines, exceeding the ${options.maxLines}-line edit limit. Hashline editing targets source-sized files; for very large files use write or a non-line-based approach.`);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
return decoded;
|
|
213
|
+
}
|
|
214
|
+
parts.push(decodeChunk(sample, true));
|
|
215
|
+
let position = bytesRead;
|
|
216
|
+
while (true) {
|
|
217
|
+
const { bytesRead: chunkBytesRead } = await fileHandle.read(buffer, 0, SNIFF_BYTES, position);
|
|
218
|
+
if (chunkBytesRead === 0) {
|
|
219
|
+
break;
|
|
220
|
+
}
|
|
221
|
+
const chunk = buffer.subarray(0, chunkBytesRead);
|
|
222
|
+
parts.push(decodeChunk(chunk, true));
|
|
223
|
+
position += chunkBytesRead;
|
|
224
|
+
}
|
|
225
|
+
parts.push(decodeChunk(new Uint8Array(0), false));
|
|
226
|
+
return {
|
|
227
|
+
kind: "text",
|
|
228
|
+
text: parts.join(""),
|
|
229
|
+
...(hadUtf8DecodeErrors ? { hadUtf8DecodeErrors: true } : {}),
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
finally {
|
|
233
|
+
await fileHandle.close();
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
// --- Validation (from validation.ts, private) ---
|
|
237
|
+
export async function valAccess(absolutePath, path, accessMode = constants.R_OK) {
|
|
238
|
+
try {
|
|
239
|
+
await fsAccess(absolutePath, accessMode);
|
|
240
|
+
}
|
|
241
|
+
catch (error) {
|
|
242
|
+
const code = errCode(error);
|
|
243
|
+
if (code === "ENOENT") {
|
|
244
|
+
throw new Error(`[E_NOT_FOUND] File not found: ${path}`);
|
|
245
|
+
}
|
|
246
|
+
if (code === "EACCES" || code === "EPERM") {
|
|
247
|
+
const accessLabel = accessMode & constants.W_OK ? "not writable" : "not readable";
|
|
248
|
+
throw new Error(`[E_ACCESS] File is ${accessLabel}: ${path}`);
|
|
249
|
+
}
|
|
250
|
+
if (code === "ELOOP") {
|
|
251
|
+
throw new Error(`[E_ACCESS] Too many symbolic links while resolving: ${path}`);
|
|
252
|
+
}
|
|
253
|
+
throw new Error(`[E_ACCESS] Cannot access file: ${path}`);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
export function valKind(file, path) {
|
|
257
|
+
if (file.kind === "directory") {
|
|
258
|
+
throw new Error(`[E_NOT_TEXT] Path is a directory: ${path}. Use ls to inspect directories.`);
|
|
259
|
+
}
|
|
260
|
+
if (file.kind === "binary") {
|
|
261
|
+
throw new Error(`[E_NOT_TEXT] Path is a binary file: ${path} (${file.description}). Hashline edit only supports text files.`);
|
|
262
|
+
}
|
|
263
|
+
if (file.kind === "image") {
|
|
264
|
+
throw new Error(`[E_NOT_TEXT] Path is an image file: ${path}. Hashline edit only supports text files.`);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
function fmtSnapId(canonicalPath, info) {
|
|
268
|
+
return `v2|${canonicalPath}|${info.ino}|${info.mtimeMs}|${info.ctimeMs}|${info.size}`;
|
|
269
|
+
}
|
|
270
|
+
export async function fileSnap(absolutePath) {
|
|
271
|
+
const canonicalPath = await resolveTarget(absolutePath);
|
|
272
|
+
const stats = await fsStat(canonicalPath);
|
|
273
|
+
return {
|
|
274
|
+
snapshotId: fmtSnapId(canonicalPath, stats),
|
|
275
|
+
ino: stats.ino,
|
|
276
|
+
mtimeMs: stats.mtimeMs,
|
|
277
|
+
ctimeMs: stats.ctimeMs,
|
|
278
|
+
size: stats.size,
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
export async function normFromText(input) {
|
|
282
|
+
const { absolutePath, displayPath, signal } = input;
|
|
283
|
+
abortIf(signal);
|
|
284
|
+
const { bom, text: rawContent } = stripBOM(input.rawText);
|
|
285
|
+
const originalEnding = detectEnding(rawContent);
|
|
286
|
+
const normalized = toLF(rawContent);
|
|
287
|
+
if (input.maxLines !== undefined) {
|
|
288
|
+
const lineCount = visLines(normalized).length;
|
|
289
|
+
if (lineCount > input.maxLines) {
|
|
290
|
+
throw new Error(`[E_FILE_TOO_LARGE] ${displayPath} has ${lineCount} lines, exceeding the ${input.maxLines}-line edit limit. Hashline editing targets source-sized files; for very large files use write or a non-line-based approach.`);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
const fileHashes = await lineHashes(normalized, absolutePath, undefined, input.store, input.noPersist !== true);
|
|
294
|
+
return {
|
|
295
|
+
absolutePath,
|
|
296
|
+
normalized,
|
|
297
|
+
bom,
|
|
298
|
+
originalEnding,
|
|
299
|
+
fileHashes,
|
|
300
|
+
hadUtf8DecodeErrors: input.hadUtf8DecodeErrors === true,
|
|
301
|
+
};
|
|
302
|
+
}
|
|
303
|
+
export async function readNormFile(path, cwd, options) {
|
|
304
|
+
const absolutePath = toCwd(path, cwd);
|
|
305
|
+
const resolvedPath = await resolveTarget(absolutePath);
|
|
306
|
+
const signal = options?.signal;
|
|
307
|
+
const accessMode = options?.accessMode ?? constants.R_OK;
|
|
308
|
+
abortIf(signal);
|
|
309
|
+
await valAccess(resolvedPath, path, accessMode);
|
|
310
|
+
abortIf(signal);
|
|
311
|
+
const file = options?.preloadedFile ??
|
|
312
|
+
(await loadFileKindAndText(resolvedPath, {
|
|
313
|
+
maxLines: options?.maxLines,
|
|
314
|
+
displayPath: path,
|
|
315
|
+
}));
|
|
316
|
+
valKind(file, path);
|
|
317
|
+
return normFromText({
|
|
318
|
+
absolutePath: resolvedPath,
|
|
319
|
+
rawText: file.text,
|
|
320
|
+
displayPath: path,
|
|
321
|
+
signal,
|
|
322
|
+
maxLines: options?.maxLines,
|
|
323
|
+
store: options?.store,
|
|
324
|
+
noPersist: options?.noPersist,
|
|
325
|
+
hadUtf8DecodeErrors: file.hadUtf8DecodeErrors,
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
// --- Read render (from read-render.ts, private) ---
|
|
329
|
+
function normPosInt(value, name) {
|
|
330
|
+
if (value === undefined)
|
|
331
|
+
return undefined;
|
|
332
|
+
if (!Number.isInteger(value) || value < 1) {
|
|
333
|
+
throw new Error(`[E_BAD_SHAPE] Read request field "${name}" must be a positive integer.`);
|
|
334
|
+
}
|
|
335
|
+
return value;
|
|
336
|
+
}
|
|
337
|
+
export function formatPaginationHint(startLine, endLine, totalLines, nextOffset, byteLimit) {
|
|
338
|
+
const sizeSuffix = byteLimit !== undefined ? ` (${formatSize(byteLimit)} limit)` : '';
|
|
339
|
+
return `[Showing lines ${startLine}-${endLine} of ${totalLines}${sizeSuffix}. Use offset=${nextOffset} to continue.]`;
|
|
340
|
+
}
|
|
341
|
+
export async function fmtReadPreview(text, options, precomputedHashes, path, maxLineBytes = MAX_READ_LINE_BYTES, maxTruncLines = DEFAULT_MAX_LINES) {
|
|
342
|
+
const allLines = visLines(text);
|
|
343
|
+
const totalLines = allLines.length;
|
|
344
|
+
const startLine = normPosInt(options.offset, 'offset') ?? 1;
|
|
345
|
+
if (totalLines === 0) {
|
|
346
|
+
if (startLine === 1) {
|
|
347
|
+
const allHashes = precomputedHashes ??
|
|
348
|
+
(await (path ? lineHashes(text, path) : lineHashes(text)));
|
|
349
|
+
const emptyLineHash = allHashes[0];
|
|
350
|
+
return {
|
|
351
|
+
text: `${emptyLineHash}${HASH_SEP}\n[File is empty. Use edit to insert content.]`,
|
|
352
|
+
served: [{ position: 0, hash: emptyLineHash }],
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
return {
|
|
356
|
+
text: `Offset ${startLine} is beyond end of file (0 lines total). The file is empty. Use edit to insert content.`,
|
|
357
|
+
served: [],
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
if (startLine > totalLines) {
|
|
361
|
+
return {
|
|
362
|
+
text: `Offset ${startLine} is beyond end of file (${totalLines} lines total). Use offset=1 to read from the start, or offset=${totalLines} to read the last line.`,
|
|
363
|
+
served: [],
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
const limit = normPosInt(options.limit, 'limit');
|
|
367
|
+
const endIdx = limit
|
|
368
|
+
? Math.min(startLine - 1 + limit, totalLines)
|
|
369
|
+
: totalLines;
|
|
370
|
+
const selected = allLines.slice(startLine - 1, endIdx);
|
|
371
|
+
const allHashes = precomputedHashes ??
|
|
372
|
+
(await (path ? lineHashes(text, path) : lineHashes(text)));
|
|
373
|
+
const selectedHashes = allHashes.slice(startLine - 1, endIdx);
|
|
374
|
+
const formatted = fmtRegion(selectedHashes, selected);
|
|
375
|
+
const maxBytes = maxLineBytes;
|
|
376
|
+
const rowSizes = selected.map((line, index) => ({
|
|
377
|
+
lineNumber: startLine + index,
|
|
378
|
+
bytes: Buffer.byteLength(`${selectedHashes[index]}${HASH_SEP}${line}`, 'utf-8'),
|
|
379
|
+
}));
|
|
380
|
+
if (rowSizes.some((row) => row.bytes > maxBytes)) {
|
|
381
|
+
const oversized = rowSizes.filter((row) => row.bytes > maxBytes);
|
|
382
|
+
const rows = rowSizes.map((row, index) => row.bytes > maxBytes
|
|
383
|
+
? `[Line ${row.lineNumber} is ${formatSize(row.bytes)}, exceeds ${formatSize(maxBytes)}; content not shown. Use bash: sed -n '${row.lineNumber}p' <path> | head -c ${maxBytes}]`
|
|
384
|
+
: fmtRegion([selectedHashes[index]], [selected[index]]));
|
|
385
|
+
const skippedTruncation = truncateHead(rows.join('\n'), {
|
|
386
|
+
maxBytes,
|
|
387
|
+
maxLines: maxTruncLines,
|
|
388
|
+
});
|
|
389
|
+
const shownRowCount = skippedTruncation.content === ''
|
|
390
|
+
? 0
|
|
391
|
+
: skippedTruncation.content.split('\n').length;
|
|
392
|
+
const lastShownLine = shownRowCount > 0 ? startLine + shownRowCount - 1 : startLine - 1;
|
|
393
|
+
const lineLabel = oversized.length === 1
|
|
394
|
+
? `Line ${oversized[0].lineNumber}`
|
|
395
|
+
: `Lines ${oversized.map((row) => row.lineNumber).join(', ')}`;
|
|
396
|
+
const verb = oversized.length === 1 ? 'exceeds' : 'exceed';
|
|
397
|
+
const addresses = oversized.map((row) => `${row.lineNumber}p`).join(';');
|
|
398
|
+
const warning = `[${lineLabel} ${verb} ${formatSize(maxBytes)}; content not shown because hashline anchors require full lines. Inspect with bash: sed -n '${addresses}' <path> | head -c ${maxBytes}]`;
|
|
399
|
+
let preview = skippedTruncation.content;
|
|
400
|
+
let nextOffset;
|
|
401
|
+
if (shownRowCount > 0 &&
|
|
402
|
+
(skippedTruncation.truncated || lastShownLine < totalLines)) {
|
|
403
|
+
nextOffset = lastShownLine + 1;
|
|
404
|
+
preview += `\n\n${warning}\n${formatPaginationHint(startLine, lastShownLine, totalLines, nextOffset, skippedTruncation.truncated ? skippedTruncation.maxBytes : undefined)}`;
|
|
405
|
+
}
|
|
406
|
+
else {
|
|
407
|
+
preview += `\n\n${warning}`;
|
|
408
|
+
}
|
|
409
|
+
const served = [];
|
|
410
|
+
for (let index = 0; index < shownRowCount; index++) {
|
|
411
|
+
if (rowSizes[index].bytes <= maxBytes) {
|
|
412
|
+
served.push({
|
|
413
|
+
position: startLine - 1 + index,
|
|
414
|
+
hash: selectedHashes[index],
|
|
415
|
+
});
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
return {
|
|
419
|
+
text: preview,
|
|
420
|
+
truncation: skippedTruncation.truncated ? skippedTruncation : undefined,
|
|
421
|
+
...(nextOffset !== undefined ? { nextOffset } : {}),
|
|
422
|
+
served,
|
|
423
|
+
};
|
|
424
|
+
}
|
|
425
|
+
const truncation = truncateHead(formatted, {
|
|
426
|
+
maxBytes,
|
|
427
|
+
maxLines: maxTruncLines,
|
|
428
|
+
});
|
|
429
|
+
let preview = truncation.content;
|
|
430
|
+
let nextOffset;
|
|
431
|
+
if (truncation.truncated) {
|
|
432
|
+
const endLineDisplay = startLine + truncation.outputLines - 1;
|
|
433
|
+
nextOffset = endLineDisplay + 1;
|
|
434
|
+
if (truncation.truncatedBy === 'lines') {
|
|
435
|
+
preview += `\n\n${formatPaginationHint(startLine, endLineDisplay, totalLines, nextOffset)}`;
|
|
436
|
+
}
|
|
437
|
+
else {
|
|
438
|
+
preview += `\n\n${formatPaginationHint(startLine, endLineDisplay, totalLines, nextOffset, truncation.maxBytes)}`;
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
else if (endIdx < totalLines) {
|
|
442
|
+
nextOffset = endIdx + 1;
|
|
443
|
+
preview += `\n\n${formatPaginationHint(startLine, endIdx, totalLines, nextOffset)}`;
|
|
444
|
+
}
|
|
445
|
+
const served = [];
|
|
446
|
+
for (let index = 0; index < truncation.outputLines; index++) {
|
|
447
|
+
served.push({
|
|
448
|
+
position: startLine - 1 + index,
|
|
449
|
+
hash: selectedHashes[index],
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
return {
|
|
453
|
+
text: preview,
|
|
454
|
+
truncation: truncation.truncated ? truncation : undefined,
|
|
455
|
+
...(nextOffset !== undefined ? { nextOffset } : {}),
|
|
456
|
+
served,
|
|
457
|
+
};
|
|
458
|
+
}
|
|
459
|
+
export async function preview(content, hashes, opts = {}, absolutePath) {
|
|
460
|
+
return fmtReadPreview(content, opts, hashes, absolutePath);
|
|
461
|
+
}
|
|
462
|
+
export async function readView(io, path, cwd, opts = {}) {
|
|
463
|
+
const { signal } = opts;
|
|
464
|
+
const absolutePath = await io.resolve(path, cwd, signal);
|
|
465
|
+
const rawText = await io.readText(absolutePath, signal);
|
|
466
|
+
const { normalized, fileHashes, hadUtf8DecodeErrors, bom, originalEnding } = await normFromText({
|
|
467
|
+
absolutePath,
|
|
468
|
+
rawText,
|
|
469
|
+
displayPath: path,
|
|
470
|
+
signal,
|
|
471
|
+
maxLines: MAX_HASH_LINES,
|
|
472
|
+
});
|
|
473
|
+
const r = await fmtReadPreview(normalized, { offset: opts.offset, limit: opts.limit }, fileHashes, absolutePath);
|
|
474
|
+
return {
|
|
475
|
+
text: r.text,
|
|
476
|
+
hashes: fileHashes,
|
|
477
|
+
served: r.served,
|
|
478
|
+
absolutePath,
|
|
479
|
+
truncation: r.truncation,
|
|
480
|
+
nextOffset: r.nextOffset,
|
|
481
|
+
hadUtf8DecodeErrors,
|
|
482
|
+
bom,
|
|
483
|
+
originalEnding,
|
|
484
|
+
normalized,
|
|
485
|
+
};
|
|
486
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Barrel for guidance sub-seams — parse (pure), resolve (IO-light), materialize (IO-heavy).
|
|
3
|
+
* Re-exported via `src/guidance.ts` so existing `from "./guidance.js"` imports keep working.
|
|
4
|
+
* @module dsh-better-edit/guidance/index
|
|
5
|
+
*/
|
|
6
|
+
export * from "./parse.js";
|
|
7
|
+
export * from "./resolve.js";
|
|
8
|
+
export * from "./materialize.js";
|