@leolastforce/chalice-hashline-edit 0.0.0-stage → 4.4.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.
- package/LICENSE +21 -0
- package/README.md +153 -2
- package/index.ts +282 -0
- package/package.json +79 -4
- package/prompts/find-guidelines.md +2 -0
- package/prompts/find-snippet.md +1 -0
- package/prompts/find.md +1 -0
- package/prompts/grep-guidelines.md +2 -0
- package/prompts/grep-snippet.md +1 -0
- package/prompts/grep.md +1 -0
- package/prompts/insert-guidelines.md +1 -0
- package/prompts/insert-snippet.md +1 -0
- package/prompts/insert.md +1 -0
- package/prompts/read-guidelines.md +2 -0
- package/prompts/read-snippet.md +1 -0
- package/prompts/read.md +1 -0
- package/prompts/replace-guidelines.md +4 -0
- package/prompts/replace-snippet.md +1 -0
- package/prompts/replace.md +1 -0
- package/prompts/undo-last-change-guidelines.md +1 -0
- package/prompts/undo-last-change-snippet.md +1 -0
- package/prompts/undo-last-change.md +1 -0
- package/src/anchor-registry.ts +977 -0
- package/src/auto-read-all-state.ts +27 -0
- package/src/auto-read-all.ts +477 -0
- package/src/batch.ts +767 -0
- package/src/commit.ts +137 -0
- package/src/config-ui.ts +212 -0
- package/src/config.ts +235 -0
- package/src/constants.ts +26 -0
- package/src/edit-common.ts +206 -0
- package/src/file-kind.ts +191 -0
- package/src/file-reader.ts +135 -0
- package/src/find.ts +244 -0
- package/src/fs-write.ts +231 -0
- package/src/glob.ts +186 -0
- package/src/grep.ts +690 -0
- package/src/hash-store/cache.ts +18 -0
- package/src/hash-store/retry.ts +44 -0
- package/src/hash-store/validation.ts +62 -0
- package/src/hash-store.ts +609 -0
- package/src/hashline/alphabet.ts +21 -0
- package/src/hashline/anchor-table.json +1 -0
- package/src/hashline/apply.ts +297 -0
- package/src/hashline/hash.ts +119 -0
- package/src/hashline/hasher.ts +47 -0
- package/src/hashline/index.ts +50 -0
- package/src/hashline/parse.ts +84 -0
- package/src/hashline/resolve.ts +373 -0
- package/src/insert.ts +258 -0
- package/src/normalize.ts +27 -0
- package/src/paths.ts +56 -0
- package/src/payload-contract.ts +133 -0
- package/src/prompts.ts +13 -0
- package/src/read.ts +286 -0
- package/src/replace-diff.ts +610 -0
- package/src/replace-render.ts +390 -0
- package/src/replace-response.ts +166 -0
- package/src/replace-undo.ts +270 -0
- package/src/replace.ts +318 -0
- package/src/served.ts +36 -0
- package/src/utils.ts +387 -0
- package/src/validation.ts +45 -0
- package/src/write-hook.ts +53 -0
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
import { abortIf, splitLines } from "../utils";
|
|
2
|
+
import {
|
|
3
|
+
valEdit,
|
|
4
|
+
stripBarePrefixes,
|
|
5
|
+
stripDiffPrefixes,
|
|
6
|
+
swapReversedRanges,
|
|
7
|
+
warnUnicodeEsc,
|
|
8
|
+
fmtMismatchWithHashes,
|
|
9
|
+
AnchorMismatchError,
|
|
10
|
+
assertRangeServed,
|
|
11
|
+
type RHEdit,
|
|
12
|
+
type NEdit,
|
|
13
|
+
type HEdit,
|
|
14
|
+
} from "./resolve";
|
|
15
|
+
|
|
16
|
+
type LIdx = {
|
|
17
|
+
fileLines: string[];
|
|
18
|
+
lineStarts: number[];
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export function buildIdx(content: string): LIdx {
|
|
22
|
+
const fileLines = splitLines(content);
|
|
23
|
+
const lineStarts: number[] = [];
|
|
24
|
+
let offset = 0;
|
|
25
|
+
|
|
26
|
+
for (let index = 0; index < fileLines.length; index++) {
|
|
27
|
+
lineStarts.push(offset);
|
|
28
|
+
offset += fileLines[index]!.length;
|
|
29
|
+
if (index < fileLines.length - 1) {
|
|
30
|
+
offset += 1;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return {
|
|
35
|
+
fileLines,
|
|
36
|
+
lineStarts,
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
type RESpan = {
|
|
41
|
+
kind: "replace";
|
|
42
|
+
start: number;
|
|
43
|
+
end: number;
|
|
44
|
+
replacement: string;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
type NoopSpan = {
|
|
48
|
+
kind: "noop";
|
|
49
|
+
loc: string;
|
|
50
|
+
currentContent: string;
|
|
51
|
+
};
|
|
52
|
+
export function assertNotEmpty(originalContent: string, result: string): void {
|
|
53
|
+
if (originalContent.length > 0 && result.length === 0) {
|
|
54
|
+
throw new Error(
|
|
55
|
+
"[E_WOULD_EMPTY] A replace cannot empty a non-empty file. Use `write` to clear the file."
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function resToSpan(
|
|
61
|
+
edit: RHEdit,
|
|
62
|
+
content: string,
|
|
63
|
+
lineIndex: LIdx,
|
|
64
|
+
): RESpan | NoopSpan {
|
|
65
|
+
const { fileLines, lineStarts } = lineIndex;
|
|
66
|
+
|
|
67
|
+
const startLine = edit.hash_bounds[0].line;
|
|
68
|
+
const endLine = edit.hash_bounds[1].line;
|
|
69
|
+
const originalLines = fileLines.slice(startLine - 1, endLine);
|
|
70
|
+
if (
|
|
71
|
+
originalLines.length === edit.content_lines.length &&
|
|
72
|
+
originalLines.every(
|
|
73
|
+
(line, lineIndex) => line === edit.content_lines[lineIndex],
|
|
74
|
+
)
|
|
75
|
+
) {
|
|
76
|
+
return {
|
|
77
|
+
kind: "noop",
|
|
78
|
+
loc: edit.hash_bounds[0].hash,
|
|
79
|
+
currentContent: originalLines.join("\n"),
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (edit.content_lines.length > 0) {
|
|
84
|
+
const lastReplacementLine = edit.content_lines[edit.content_lines.length - 1]!;
|
|
85
|
+
const endsWithBlank = lastReplacementLine.length === 0;
|
|
86
|
+
const endsAtEofWithoutNewline =
|
|
87
|
+
endLine === fileLines.length && !content.endsWith("\n");
|
|
88
|
+
const replacement = edit.content_lines.join("\n");
|
|
89
|
+
return {
|
|
90
|
+
kind: "replace",
|
|
91
|
+
start: lineStarts[startLine - 1]!,
|
|
92
|
+
end: lineStarts[endLine - 1]! + fileLines[endLine - 1]!.length,
|
|
93
|
+
replacement:
|
|
94
|
+
endsAtEofWithoutNewline && endsWithBlank
|
|
95
|
+
? `${replacement}\n`
|
|
96
|
+
: replacement,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (startLine === 1 && endLine === fileLines.length) {
|
|
101
|
+
return {
|
|
102
|
+
kind: "replace",
|
|
103
|
+
start: 0,
|
|
104
|
+
end: content.length,
|
|
105
|
+
replacement: "",
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (endLine < fileLines.length) {
|
|
110
|
+
return {
|
|
111
|
+
kind: "replace",
|
|
112
|
+
start: lineStarts[startLine - 1]!,
|
|
113
|
+
end: lineStarts[endLine]!,
|
|
114
|
+
replacement: "",
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (content.endsWith("\n")) {
|
|
119
|
+
return {
|
|
120
|
+
kind: "replace",
|
|
121
|
+
start: lineStarts[startLine - 1]!,
|
|
122
|
+
end: content.length,
|
|
123
|
+
replacement: "",
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const prevLine = startLine >= 2 ? fileLines[startLine - 2] : undefined;
|
|
128
|
+
return {
|
|
129
|
+
kind: "replace",
|
|
130
|
+
start:
|
|
131
|
+
prevLine !== undefined && prevLine.length === 0
|
|
132
|
+
? lineStarts[startLine - 1]!
|
|
133
|
+
: Math.max(0, lineStarts[startLine - 1]! - 1),
|
|
134
|
+
end: content.length,
|
|
135
|
+
replacement: "",
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function assemble(
|
|
140
|
+
content: string,
|
|
141
|
+
span: RESpan,
|
|
142
|
+
signal: AbortSignal | undefined,
|
|
143
|
+
): string {
|
|
144
|
+
abortIf(signal);
|
|
145
|
+
return content.slice(0, span.start) + span.replacement + content.slice(span.end);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export interface PlannedEdit {
|
|
149
|
+
resolved: RHEdit;
|
|
150
|
+
warnings: string[];
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
export function planEdit(
|
|
155
|
+
content: string,
|
|
156
|
+
edit: HEdit,
|
|
157
|
+
precomputedHashes: string[],
|
|
158
|
+
options?: {
|
|
159
|
+
filePath?: string;
|
|
160
|
+
servedHashes?: ReadonlyMap<string, string>;
|
|
161
|
+
signal?: AbortSignal;
|
|
162
|
+
baseFileLines?: string[];
|
|
163
|
+
},
|
|
164
|
+
): PlannedEdit {
|
|
165
|
+
const signal = options?.signal;
|
|
166
|
+
abortIf(signal);
|
|
167
|
+
const fileLines = options?.baseFileLines ?? splitLines(content);
|
|
168
|
+
const lineIndex = { fileLines };
|
|
169
|
+
const fileHashes = precomputedHashes;
|
|
170
|
+
const warnings: string[] = [];
|
|
171
|
+
|
|
172
|
+
const rangeFixed = swapReversedRanges(edit, fileHashes);
|
|
173
|
+
const prefixFixed = stripDiffPrefixes(
|
|
174
|
+
stripBarePrefixes(rangeFixed, warnings),
|
|
175
|
+
warnings,
|
|
176
|
+
);
|
|
177
|
+
|
|
178
|
+
const { resolved: initialResolved, mismatches } = valEdit(
|
|
179
|
+
prefixFixed,
|
|
180
|
+
lineIndex.fileLines,
|
|
181
|
+
fileHashes,
|
|
182
|
+
warnings,
|
|
183
|
+
signal,
|
|
184
|
+
);
|
|
185
|
+
if (mismatches.length || !initialResolved) {
|
|
186
|
+
const feedback = fmtMismatchWithHashes(
|
|
187
|
+
mismatches,
|
|
188
|
+
lineIndex.fileLines,
|
|
189
|
+
fileHashes,
|
|
190
|
+
options?.filePath,
|
|
191
|
+
);
|
|
192
|
+
throw new AnchorMismatchError(feedback.text, feedback.hashes, feedback.servedMap);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
warnUnicodeEsc(prefixFixed, warnings);
|
|
196
|
+
|
|
197
|
+
const resolved = initialResolved;
|
|
198
|
+
|
|
199
|
+
if (options?.servedHashes) {
|
|
200
|
+
abortIf(signal);
|
|
201
|
+
assertRangeServed(resolved, lineIndex.fileLines, fileHashes, options.servedHashes, options?.filePath);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
return {
|
|
205
|
+
resolved,
|
|
206
|
+
warnings,
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
export function applyEdit(
|
|
210
|
+
content: string,
|
|
211
|
+
edit: HEdit,
|
|
212
|
+
signal?: AbortSignal,
|
|
213
|
+
precomputedHashes?: string[],
|
|
214
|
+
filePath?: string,
|
|
215
|
+
servedHashes?: ReadonlyMap<string, string>,
|
|
216
|
+
): {
|
|
217
|
+
content: string;
|
|
218
|
+
firstChangedLine: number | undefined;
|
|
219
|
+
lastChangedLine: number | undefined;
|
|
220
|
+
warnings?: string[];
|
|
221
|
+
noopEdit?: NEdit;
|
|
222
|
+
} {
|
|
223
|
+
abortIf(signal);
|
|
224
|
+
if (precomputedHashes === undefined) {
|
|
225
|
+
throw new Error("[E_BAD_SHAPE] applyEdit requires the file's allocated anchors; derive them via lineHashes(content, path) first.");
|
|
226
|
+
}
|
|
227
|
+
const planned = planEdit(content, edit, precomputedHashes, { filePath, servedHashes, signal });
|
|
228
|
+
const lineIndex = buildIdx(content);
|
|
229
|
+
const warnings = planned.warnings;
|
|
230
|
+
const resolved = planned.resolved;
|
|
231
|
+
const spanResult = resToSpan(resolved, content, lineIndex);
|
|
232
|
+
if (spanResult.kind === "noop") {
|
|
233
|
+
return {
|
|
234
|
+
content,
|
|
235
|
+
firstChangedLine: undefined,
|
|
236
|
+
lastChangedLine: undefined,
|
|
237
|
+
...(warnings.length ? { warnings } : {}),
|
|
238
|
+
noopEdit: { loc: spanResult.loc, currentContent: spanResult.currentContent },
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const result = assemble(content, spanResult, signal);
|
|
243
|
+
assertNotEmpty(content, result);
|
|
244
|
+
const range = changedRange(content, result);
|
|
245
|
+
|
|
246
|
+
return {
|
|
247
|
+
content: result,
|
|
248
|
+
firstChangedLine: range?.firstChangedLine,
|
|
249
|
+
lastChangedLine: range?.lastChangedLine,
|
|
250
|
+
...(warnings.length ? { warnings } : {}),
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
export { fmtRegion, fmtRow } from "./resolve";
|
|
255
|
+
export function changedRange(
|
|
256
|
+
original: string,
|
|
257
|
+
result: string,
|
|
258
|
+
): { firstChangedLine: number; lastChangedLine: number } | null {
|
|
259
|
+
if (original === result) return null;
|
|
260
|
+
|
|
261
|
+
if (original.length === 0) {
|
|
262
|
+
return {
|
|
263
|
+
firstChangedLine: 1,
|
|
264
|
+
lastChangedLine: splitLines(result).length,
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
const originalLines = splitLines(original);
|
|
269
|
+
const resultLines = splitLines(result);
|
|
270
|
+
|
|
271
|
+
if (
|
|
272
|
+
originalLines.length === resultLines.length &&
|
|
273
|
+
originalLines.every((line, index) => line === resultLines[index])
|
|
274
|
+
) {
|
|
275
|
+
return null;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
const minLen = Math.min(originalLines.length, resultLines.length);
|
|
279
|
+
let first = 0;
|
|
280
|
+
while (first < minLen && originalLines[first] === resultLines[first]) {
|
|
281
|
+
first++;
|
|
282
|
+
}
|
|
283
|
+
let lastOrig = originalLines.length - 1;
|
|
284
|
+
let lastRes = resultLines.length - 1;
|
|
285
|
+
while (
|
|
286
|
+
lastOrig >= first &&
|
|
287
|
+
lastRes >= first &&
|
|
288
|
+
originalLines[lastOrig] === resultLines[lastRes]
|
|
289
|
+
) {
|
|
290
|
+
lastOrig--;
|
|
291
|
+
lastRes--;
|
|
292
|
+
}
|
|
293
|
+
return {
|
|
294
|
+
firstChangedLine: first + 1,
|
|
295
|
+
lastChangedLine: Math.max(first, lastRes) + 1,
|
|
296
|
+
};
|
|
297
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { splitLines, getCached } from "../utils";
|
|
2
|
+
import { loadHashStore, type HashStore } from "../hash-store";
|
|
3
|
+
import { allocateFileAnchors } from "../anchor-registry";
|
|
4
|
+
import { xxh32, initHasher, canon, hashSource, lineChecksum } from "./hasher";
|
|
5
|
+
export { canon, hashSource, lineChecksum };
|
|
6
|
+
import { HASH_LEN, ANCHOR_COUNT, anchorAt, HASH_CLASS, HASH_RUN } from "./alphabet";
|
|
7
|
+
export { initHasher, HASH_LEN, HASH_CLASS, HASH_RUN };
|
|
8
|
+
|
|
9
|
+
export const ANCHOR_LEN = HASH_LEN;
|
|
10
|
+
|
|
11
|
+
export const HASH_SEP = "│";
|
|
12
|
+
|
|
13
|
+
export const HASH_SPACE = ANCHOR_COUNT;
|
|
14
|
+
export const MAX_HASH_LINES = HASH_SPACE;
|
|
15
|
+
|
|
16
|
+
export const HASH_PROBE_STRIDE = 836286;
|
|
17
|
+
|
|
18
|
+
export const HL_PREFIX_PLUS_RE = new RegExp(
|
|
19
|
+
`^\\+${HASH_RUN}│`,
|
|
20
|
+
);
|
|
21
|
+
export const HL_PREFIX_MINUS_RE = new RegExp(
|
|
22
|
+
`^-(?:${HASH_RUN}│| {${ANCHOR_LEN}}│)`,
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
export const HL_BARE_PREFIX_RE = new RegExp(`^\\s*(${HASH_RUN})│`);
|
|
26
|
+
|
|
27
|
+
export type RowPrefixKind = "bare" | "plus" | "minus";
|
|
28
|
+
|
|
29
|
+
export type StrippedRow = {
|
|
30
|
+
text: string;
|
|
31
|
+
kind: RowPrefixKind | null;
|
|
32
|
+
hash: string | undefined;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export function stripRowPrefix(line: string): StrippedRow {
|
|
36
|
+
const bare = line.match(HL_BARE_PREFIX_RE);
|
|
37
|
+
if (bare) {
|
|
38
|
+
return { text: line.slice(bare[0].length), kind: "bare", hash: bare[1] };
|
|
39
|
+
}
|
|
40
|
+
const plus = line.match(HL_PREFIX_PLUS_RE);
|
|
41
|
+
if (plus) {
|
|
42
|
+
return { text: line.slice(plus[0].length), kind: "plus", hash: plus[1] };
|
|
43
|
+
}
|
|
44
|
+
const minus = line.match(HL_PREFIX_MINUS_RE);
|
|
45
|
+
if (minus) {
|
|
46
|
+
return { text: line.slice(minus[0].length), kind: "minus", hash: minus[1] };
|
|
47
|
+
}
|
|
48
|
+
return { text: line, kind: null, hash: undefined };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const BITSET_WORDS = Math.ceil(HASH_SPACE / 32);
|
|
52
|
+
|
|
53
|
+
function getBit(bits: Uint32Array, idx: number): boolean {
|
|
54
|
+
return (bits[idx >>> 5] >>> (idx & 31) & 1) !== 0;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function setBit(bits: Uint32Array, idx: number): void {
|
|
58
|
+
bits[idx >>> 5] |= 1 << (idx & 31);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function nextZeroBit(bits: Uint32Array, start: number): number {
|
|
62
|
+
const totalBits = HASH_SPACE;
|
|
63
|
+
let idx = start % totalBits;
|
|
64
|
+
for (let i = 0; i < totalBits; i++) {
|
|
65
|
+
if (!getBit(bits, idx)) return idx;
|
|
66
|
+
idx += HASH_PROBE_STRIDE;
|
|
67
|
+
if (idx >= totalBits) idx -= totalBits;
|
|
68
|
+
}
|
|
69
|
+
throw new Error(
|
|
70
|
+
`[E_FILE_TOO_LARGE] File exceeds the ${HASH_SPACE}-line hashline limit; use write for very large files.`,
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function assignHash(used: Uint32Array, baseIdx: number, hint: { value: number }): string {
|
|
75
|
+
if (!getBit(used, baseIdx)) {
|
|
76
|
+
setBit(used, baseIdx);
|
|
77
|
+
hint.value = baseIdx + HASH_PROBE_STRIDE;
|
|
78
|
+
return anchorAt(baseIdx);
|
|
79
|
+
}
|
|
80
|
+
const nextIdx = nextZeroBit(used, hint.value);
|
|
81
|
+
setBit(used, nextIdx);
|
|
82
|
+
hint.value = nextIdx + HASH_PROBE_STRIDE;
|
|
83
|
+
return anchorAt(nextIdx);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function _lineHashesPure(content: string): string[] {
|
|
87
|
+
const lines = splitLines(content);
|
|
88
|
+
const hashes = new Array<string>(lines.length);
|
|
89
|
+
const used = new Uint32Array(BITSET_WORDS);
|
|
90
|
+
const hint = { value: 0 };
|
|
91
|
+
const hashSourceCache = new Map<string, string>();
|
|
92
|
+
|
|
93
|
+
for (let i = 0; i < lines.length; i++) {
|
|
94
|
+
const c = getCached(hashSourceCache, lines[i]!, hashSource);
|
|
95
|
+
const baseIdx = (xxh32(c) >>> 14) % HASH_SPACE;
|
|
96
|
+
hashes[i] = assignHash(used, baseIdx, hint);
|
|
97
|
+
}
|
|
98
|
+
return hashes;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export async function lineHashes(
|
|
102
|
+
content: string,
|
|
103
|
+
path?: string,
|
|
104
|
+
previous?: { content: string; hashes: string[]; spans?: { start: number; end: number; replacementCount: number }[] },
|
|
105
|
+
store?: HashStore,
|
|
106
|
+
persist?: boolean,
|
|
107
|
+
shadow?: boolean,
|
|
108
|
+
): Promise<string[]> {
|
|
109
|
+
await initHasher();
|
|
110
|
+
if (!path) {
|
|
111
|
+
return _lineHashesPure(content);
|
|
112
|
+
}
|
|
113
|
+
return allocateFileAnchors(store ?? (await loadHashStore()), path, content, {
|
|
114
|
+
persist,
|
|
115
|
+
shadow,
|
|
116
|
+
previous,
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import xxhash from "xxhash-wasm";
|
|
2
|
+
import { truncateToBytes } from "../utils";
|
|
3
|
+
import { MAX_HASH_SOURCE_BYTES } from "../constants";
|
|
4
|
+
|
|
5
|
+
export type Hasher = {
|
|
6
|
+
h32(input: string, seed?: number): number;
|
|
7
|
+
h64ToString(input: string, seed?: bigint): string;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
let hasher: Hasher | null = null;
|
|
11
|
+
|
|
12
|
+
export function getH(): Hasher {
|
|
13
|
+
if (hasher) return hasher;
|
|
14
|
+
throw new Error("xxhash-wasm hasher not initialized; await initHasher() before calling hashline APIs.");
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const hasherP: Promise<Hasher> = xxhash().then((h) => {
|
|
18
|
+
hasher = h as unknown as Hasher;
|
|
19
|
+
return hasher;
|
|
20
|
+
}).catch((err: unknown) => {
|
|
21
|
+
console.error("xxhash-wasm initialization failed:", err);
|
|
22
|
+
throw err;
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
export function initHasher(): Promise<Hasher> {
|
|
26
|
+
return hasherP;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function xxh32(input: string, seed = 0): number {
|
|
30
|
+
return getH().h32(input, seed) >>> 0;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function contentChecksum(content: string): string {
|
|
34
|
+
return getH().h64ToString(content);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function canon(line: string): string {
|
|
38
|
+
return line.replace(/\r/g, "").trimEnd();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function hashSource(line: string): string {
|
|
42
|
+
return truncateToBytes(canon(line), MAX_HASH_SOURCE_BYTES);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function lineChecksum(line: string): string {
|
|
46
|
+
return contentChecksum(hashSource(line));
|
|
47
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
export {
|
|
2
|
+
HASH_LEN,
|
|
3
|
+
ANCHOR_LEN,
|
|
4
|
+
HASH_SEP,
|
|
5
|
+
HASH_CLASS,
|
|
6
|
+
HASH_RUN,
|
|
7
|
+
HASH_SPACE,
|
|
8
|
+
HASH_PROBE_STRIDE,
|
|
9
|
+
MAX_HASH_LINES,
|
|
10
|
+
lineHashes,
|
|
11
|
+
_lineHashesPure,
|
|
12
|
+
initHasher,
|
|
13
|
+
canon,
|
|
14
|
+
hashSource,
|
|
15
|
+
lineChecksum,
|
|
16
|
+
} from "./hash";
|
|
17
|
+
|
|
18
|
+
export {
|
|
19
|
+
parseHashRef,
|
|
20
|
+
parseText,
|
|
21
|
+
type Anchor,
|
|
22
|
+
} from "./parse";
|
|
23
|
+
|
|
24
|
+
export {
|
|
25
|
+
type HEdit,
|
|
26
|
+
type RHEdit,
|
|
27
|
+
type HTEdit,
|
|
28
|
+
type NEdit,
|
|
29
|
+
resEdit,
|
|
30
|
+
stripAnchorRow,
|
|
31
|
+
resolveAnchorLine,
|
|
32
|
+
valEdit,
|
|
33
|
+
stripBarePrefixes,
|
|
34
|
+
stripDiffPrefixes,
|
|
35
|
+
swapReversedRanges,
|
|
36
|
+
assertRangeServed,
|
|
37
|
+
RangeStaleError,
|
|
38
|
+
AnchorMismatchError,
|
|
39
|
+
} from "./resolve";
|
|
40
|
+
|
|
41
|
+
export {
|
|
42
|
+
buildIdx,
|
|
43
|
+
applyEdit,
|
|
44
|
+
planEdit,
|
|
45
|
+
type PlannedEdit,
|
|
46
|
+
fmtRegion,
|
|
47
|
+
fmtRow,
|
|
48
|
+
changedRange,
|
|
49
|
+
assertNotEmpty,
|
|
50
|
+
} from "./apply";
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ANCHOR_LEN,
|
|
3
|
+
HASH_CLASS,
|
|
4
|
+
} from "./hash";
|
|
5
|
+
import { ALPH_RE } from "./alphabet";
|
|
6
|
+
import { NEW_CONTENT_NOT_ARRAY_MSG } from "../constants";
|
|
7
|
+
|
|
8
|
+
const HASH_EXTRACT_RE = new RegExp(HASH_CLASS);
|
|
9
|
+
|
|
10
|
+
export type Anchor = { hash: string };
|
|
11
|
+
|
|
12
|
+
function diagRef(ref: string): string {
|
|
13
|
+
const trimmed = ref.trim();
|
|
14
|
+
|
|
15
|
+
if (!trimmed.length) {
|
|
16
|
+
return `[E_BAD_REF] Invalid anchor. Expected a 4-character anchor (letters only, e.g. "Hasu").`;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (/^\d+/.test(trimmed)) {
|
|
20
|
+
return `[E_BAD_REF] Invalid anchor. Use the anchor alone (e.g. "Hasu"): no line numbers or trailing content.`;
|
|
21
|
+
}
|
|
22
|
+
if (trimmed.includes("│") && trimmed.includes("\n")) {
|
|
23
|
+
const lines = trimmed.split(/\r?\n/);
|
|
24
|
+
const first = lines[0] ?? "";
|
|
25
|
+
const last = lines[lines.length - 1] ?? "";
|
|
26
|
+
const hashRe = HASH_EXTRACT_RE;
|
|
27
|
+
const firstMatch = first.match(hashRe);
|
|
28
|
+
const lastMatch = last.match(hashRe);
|
|
29
|
+
const firstHash = firstMatch?.[0] ?? "Hasu";
|
|
30
|
+
const lastHash = lastMatch?.[0] ?? "Hasu";
|
|
31
|
+
const preview = first.slice(0, 60);
|
|
32
|
+
return `[E_BAD_REF] Invalid anchor — remove_from and remove_to must each be a single bare 4-character anchor (letters only, e.g. "Hasu"), not a block with HASH│content. Received ${lines.length} lines starting "${preview}…" — use only the first hash "${firstHash}" as remove_from and "${lastHash}" as remove_to, and put the new content (without HASH│) in replacement_lines.`;
|
|
33
|
+
}
|
|
34
|
+
if (trimmed.includes("│")) {
|
|
35
|
+
return `[E_BAD_REF] Invalid anchor "${trimmed}": use only the 4-character anchor, drop everything from "│" onward.`;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return `[E_BAD_REF] Invalid anchor "${trimmed}". Expected a 4-character anchor (letters only, e.g. "Hasu").`;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function parseRef(ref: string): Anchor {
|
|
42
|
+
const trimmed = ref.trim();
|
|
43
|
+
|
|
44
|
+
if (
|
|
45
|
+
trimmed.length === ANCHOR_LEN &&
|
|
46
|
+
ALPH_RE.test(trimmed)
|
|
47
|
+
) {
|
|
48
|
+
return { hash: trimmed };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
throw new Error(diagRef(ref));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export const parseHashRef = parseRef;
|
|
55
|
+
|
|
56
|
+
const JSON_ENVELOPE_RE = /^\s*\["(.*)"\]\.\s*$/;
|
|
57
|
+
|
|
58
|
+
function unwrapJsonEnvelope(line: string): string {
|
|
59
|
+
const match = line.match(JSON_ENVELOPE_RE);
|
|
60
|
+
if (!match) return line;
|
|
61
|
+
const withoutDot = line.trim().slice(0, -1);
|
|
62
|
+
try {
|
|
63
|
+
const parsed: unknown = JSON.parse(withoutDot);
|
|
64
|
+
if (Array.isArray(parsed) && parsed.length === 1 && typeof parsed[0] === "string") {
|
|
65
|
+
return parsed[0];
|
|
66
|
+
}
|
|
67
|
+
return line;
|
|
68
|
+
} catch {
|
|
69
|
+
return match[1]!;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function parseText(edit: string[]): string[] {
|
|
74
|
+
if (!Array.isArray(edit) || edit.some((line) => typeof line !== "string")) {
|
|
75
|
+
throw new Error(NEW_CONTENT_NOT_ARRAY_MSG);
|
|
76
|
+
}
|
|
77
|
+
const out: string[] = [];
|
|
78
|
+
for (const line of edit) {
|
|
79
|
+
const unwrapped = unwrapJsonEnvelope(line);
|
|
80
|
+
const normalized = unwrapped.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
|
|
81
|
+
out.push(...normalized.split("\n"));
|
|
82
|
+
}
|
|
83
|
+
return out;
|
|
84
|
+
}
|