dsh-better-edit 0.1.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 +23 -0
- package/README.md +188 -0
- package/cordis.patch.yml +8 -0
- package/lib/constants.js +12 -0
- package/lib/drift.js +98 -0
- package/lib/dsh-context.js +27 -0
- package/lib/edit-diff.js +121 -0
- package/lib/edit-normalize.js +9 -0
- package/lib/edit-pipeline.js +199 -0
- package/lib/edit-response.js +172 -0
- package/lib/file-kind.js +121 -0
- package/lib/file-reader.js +80 -0
- package/lib/fs-bridge.js +106 -0
- package/lib/fs-write.js +151 -0
- package/lib/hash-store.js +380 -0
- package/lib/hashline/alphabet.js +6 -0
- package/lib/hashline/apply.js +223 -0
- package/lib/hashline/hash.js +241 -0
- package/lib/hashline/hasher.js +23 -0
- package/lib/hashline/index.js +4 -0
- package/lib/hashline/parse.js +35 -0
- package/lib/hashline/resolve.js +365 -0
- package/lib/hashline/served.js +120 -0
- package/lib/index.js +91 -0
- package/lib/noop-guard.js +15 -0
- package/lib/paths.js +37 -0
- package/lib/prompts.js +54 -0
- package/lib/read-render.js +142 -0
- package/lib/schema.js +24 -0
- package/lib/served-state.js +34 -0
- package/lib/served-store.js +230 -0
- package/lib/snapshot-store.js +108 -0
- package/lib/tool-batch-edit.js +434 -0
- package/lib/tool-edit.js +152 -0
- package/lib/tool-read.js +86 -0
- package/lib/tool-undo.js +118 -0
- package/lib/truncate.js +98 -0
- package/lib/types/constants.d.ts +11 -0
- package/lib/types/drift.d.ts +29 -0
- package/lib/types/dsh-context.d.ts +23 -0
- package/lib/types/edit-diff.d.ts +14 -0
- package/lib/types/edit-normalize.d.ts +1 -0
- package/lib/types/edit-pipeline.d.ts +54 -0
- package/lib/types/edit-response.d.ts +107 -0
- package/lib/types/file-kind.d.ts +18 -0
- package/lib/types/file-reader.d.ts +47 -0
- package/lib/types/fs-bridge.d.ts +32 -0
- package/lib/types/fs-write.d.ts +2 -0
- package/lib/types/hash-store.d.ts +33 -0
- package/lib/types/hashline/alphabet.d.ts +5 -0
- package/lib/types/hashline/apply.d.ts +22 -0
- package/lib/types/hashline/hash.d.ts +19 -0
- package/lib/types/hashline/hasher.d.ts +8 -0
- package/lib/types/hashline/index.d.ts +4 -0
- package/lib/types/hashline/parse.d.ts +7 -0
- package/lib/types/hashline/resolve.d.ts +59 -0
- package/lib/types/hashline/served.d.ts +43 -0
- package/lib/types/index.d.ts +23 -0
- package/lib/types/noop-guard.d.ts +5 -0
- package/lib/types/paths.d.ts +11 -0
- package/lib/types/prompts.d.ts +17 -0
- package/lib/types/read-render.d.ts +21 -0
- package/lib/types/schema.d.ts +39 -0
- package/lib/types/served-state.d.ts +3 -0
- package/lib/types/served-store.d.ts +53 -0
- package/lib/types/snapshot-store.d.ts +14 -0
- package/lib/types/tool-batch-edit.d.ts +21 -0
- package/lib/types/tool-edit.d.ts +22 -0
- package/lib/types/tool-read.d.ts +21 -0
- package/lib/types/tool-undo.d.ts +20 -0
- package/lib/types/truncate.d.ts +32 -0
- package/lib/types/undo-edit.d.ts +32 -0
- package/lib/types/undo-store.d.ts +14 -0
- package/lib/types/utils.d.ts +13 -0
- package/lib/types/validation.d.ts +7 -0
- package/lib/types/write-hook.d.ts +21 -0
- package/lib/undo-edit.js +83 -0
- package/lib/undo-store.js +43 -0
- package/lib/utils.js +76 -0
- package/lib/validation.js +33 -0
- package/lib/write-hook.js +72 -0
- package/package.json +87 -0
package/lib/tool-undo.js
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The dsh `undo_last_edit` tool: reverts the last hashline edit on a file,
|
|
3
|
+
* only when the file still matches the stored post-edit content — a later
|
|
4
|
+
* external write clears the history instead of being overwritten.
|
|
5
|
+
* @module dsh-better-edit/tool-undo
|
|
6
|
+
*/
|
|
7
|
+
import { defineTool } from '@deepseek-ai/dsh-tools';
|
|
8
|
+
import { toLF, stripBOM, genDiff, restoreEndings } from './edit-diff.js';
|
|
9
|
+
import { cntDiff, splitLines, isRec, normalizeFilePath } from './utils.js';
|
|
10
|
+
import { normReq } from './edit-normalize.js';
|
|
11
|
+
import { upsertSnapshotFor } from './snapshot-store.js';
|
|
12
|
+
import { contentChecksum } from './hashline/hasher.js';
|
|
13
|
+
import { lineHashes, changedRange } from './hashline/index.js';
|
|
14
|
+
import { getUndo, clearUndo } from './undo-edit.js';
|
|
15
|
+
import { recordServedTruncated } from './served-store.js';
|
|
16
|
+
import { UNDO_DESCRIPTION } from './prompts.js';
|
|
17
|
+
import { execCwd, execSessionKey } from './dsh-context.js';
|
|
18
|
+
function assertUndoReq(request) {
|
|
19
|
+
if (!isRec(request)) {
|
|
20
|
+
throw new Error('[E_BAD_SHAPE] undo_last_edit request must be an object.');
|
|
21
|
+
}
|
|
22
|
+
normalizeFilePath(request);
|
|
23
|
+
if (typeof request.path !== 'string' || request.path.length === 0) {
|
|
24
|
+
throw new Error('[E_BAD_SHAPE] undo_last_edit request requires a non-empty "path" string.');
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Register the `undo_last_edit` tool on the calling agent's scope.
|
|
29
|
+
* @param _rootCtx - host context.
|
|
30
|
+
* @param agentCtx - the agent's scoped context (own scope layer).
|
|
31
|
+
* @param io - the filesystem bridge.
|
|
32
|
+
* @returns the exact disposer that unregisters the tool.
|
|
33
|
+
*/
|
|
34
|
+
export function buildUndoTool(io) {
|
|
35
|
+
return defineTool({
|
|
36
|
+
name: 'undo_last_edit',
|
|
37
|
+
description: UNDO_DESCRIPTION,
|
|
38
|
+
parameters: {
|
|
39
|
+
path: {
|
|
40
|
+
type: 'string',
|
|
41
|
+
required: true,
|
|
42
|
+
description: 'Path to the file to undo',
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
output: {
|
|
46
|
+
schema: { type: 'string' },
|
|
47
|
+
render: (_args, value) => [{ type: 'text', text: value }],
|
|
48
|
+
},
|
|
49
|
+
async execute(args, exec) {
|
|
50
|
+
const cwd = execCwd(exec);
|
|
51
|
+
const sessionKey = execSessionKey(exec);
|
|
52
|
+
const signal = exec.signal;
|
|
53
|
+
const canonical = normReq(args);
|
|
54
|
+
assertUndoReq(canonical);
|
|
55
|
+
const path = canonical.path;
|
|
56
|
+
const absolutePath = await io.resolve(path, cwd, signal);
|
|
57
|
+
const undo = await getUndo(absolutePath);
|
|
58
|
+
if (!undo) {
|
|
59
|
+
return `No undo history for ${path}. There is no previous edit to revert.`;
|
|
60
|
+
}
|
|
61
|
+
let currentRaw;
|
|
62
|
+
try {
|
|
63
|
+
currentRaw = await io.readText(absolutePath, signal);
|
|
64
|
+
}
|
|
65
|
+
catch (error) {
|
|
66
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
67
|
+
if (message.includes('[E_NOT_FOUND]')) {
|
|
68
|
+
await clearUndo(absolutePath);
|
|
69
|
+
return `[E_UNDO_STALE] Cannot undo last edit on ${path}: the file no longer exists. Call read() to inspect the current state.`;
|
|
70
|
+
}
|
|
71
|
+
throw error;
|
|
72
|
+
}
|
|
73
|
+
if (currentRaw !==
|
|
74
|
+
undo.bom + restoreEndings(undo.resultContent, undo.originalEnding)) {
|
|
75
|
+
await clearUndo(absolutePath);
|
|
76
|
+
return `[E_UNDO_STALE] Cannot undo last edit on ${path}: the file was modified after the edit, so undoing would overwrite those changes. Call read() to inspect the current state.`;
|
|
77
|
+
}
|
|
78
|
+
const { text: currentStripped } = stripBOM(currentRaw);
|
|
79
|
+
const currentNormalized = toLF(currentStripped);
|
|
80
|
+
const currentHashes = await lineHashes(currentNormalized, absolutePath);
|
|
81
|
+
const diffResult = genDiff(undo.content, currentNormalized, 0, undefined, undo.hashes);
|
|
82
|
+
const linesAddedByEdit = cntDiff(diffResult.diff, '+');
|
|
83
|
+
const linesRemovedByEdit = cntDiff(diffResult.diff, '-');
|
|
84
|
+
const undoDiffResult = genDiff(currentNormalized, undo.content, 1, undo.hashes, currentHashes);
|
|
85
|
+
const undoDiff = undoDiffResult.diff;
|
|
86
|
+
const restoredRange = changedRange(currentNormalized, undo.content);
|
|
87
|
+
await io.writeText(absolutePath, undo.bom + restoreEndings(undo.content, undo.originalEnding), signal);
|
|
88
|
+
try {
|
|
89
|
+
await upsertSnapshotFor(absolutePath, contentChecksum(undo.content), splitLines(undo.content).length, undo.hashes);
|
|
90
|
+
}
|
|
91
|
+
catch (error) {
|
|
92
|
+
console.error('Failed to restore hash store snapshot after undo:', error);
|
|
93
|
+
}
|
|
94
|
+
await clearUndo(absolutePath);
|
|
95
|
+
const parts = [`Undone last edit on ${path}.`];
|
|
96
|
+
if (linesAddedByEdit > 0 || linesRemovedByEdit > 0) {
|
|
97
|
+
parts.push(`Removed ${linesAddedByEdit} line(s) that were added and restored ${linesRemovedByEdit} line(s) that were removed.`);
|
|
98
|
+
}
|
|
99
|
+
parts.push('File reverted to previous state. The post-edit diff rows carry the restored file\u2019s fresh anchors for follow-up edits.');
|
|
100
|
+
if (undoDiffResult.servedRows.length > 0) {
|
|
101
|
+
await recordServedTruncated(sessionKey, absolutePath, undoDiffResult.servedRows, splitLines(undo.content).length, restoredRange?.firstChangedLine ?? 0);
|
|
102
|
+
}
|
|
103
|
+
return [
|
|
104
|
+
parts.join('\n'),
|
|
105
|
+
'',
|
|
106
|
+
'Diff of the revert:',
|
|
107
|
+
'',
|
|
108
|
+
undoDiff,
|
|
109
|
+
].join('\n');
|
|
110
|
+
},
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Register the hashline tool on the calling agent’s scope (own layer).
|
|
115
|
+
*/
|
|
116
|
+
export function registerUndoTool(_rootCtx, agentCtx, io) {
|
|
117
|
+
return agentCtx.tools.register(buildUndoTool(io));
|
|
118
|
+
}
|
package/lib/truncate.js
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Truncation utilities for read output (ported from pi-coding-agent's shared
|
|
3
|
+
* tool rendering): keep the head of a text payload within line and byte caps,
|
|
4
|
+
* never emitting partial lines.
|
|
5
|
+
* @module dsh-better-edit/truncate
|
|
6
|
+
*/
|
|
7
|
+
export const DEFAULT_MAX_LINES = 2000;
|
|
8
|
+
export const DEFAULT_MAX_BYTES = 50 * 1024;
|
|
9
|
+
function splitLinesForCounting(content) {
|
|
10
|
+
if (content.length === 0)
|
|
11
|
+
return [];
|
|
12
|
+
const lines = content.split('\n');
|
|
13
|
+
if (content.endsWith('\n'))
|
|
14
|
+
lines.pop();
|
|
15
|
+
return lines;
|
|
16
|
+
}
|
|
17
|
+
/** Format bytes as a human-readable size. */
|
|
18
|
+
export function formatSize(bytes) {
|
|
19
|
+
if (bytes < 1024)
|
|
20
|
+
return `${bytes}B`;
|
|
21
|
+
if (bytes < 1024 * 1024)
|
|
22
|
+
return `${(bytes / 1024).toFixed(1)}KB`;
|
|
23
|
+
return `${(bytes / (1024 * 1024)).toFixed(1)}MB`;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Truncate content from the head (keep the first N lines/bytes). Suitable for
|
|
27
|
+
* file reads; never returns partial lines. When the first line alone exceeds
|
|
28
|
+
* the byte limit the content is emptied with `firstLineExceedsLimit`.
|
|
29
|
+
*/
|
|
30
|
+
export function truncateHead(content, options = {}) {
|
|
31
|
+
const maxLines = options.maxLines ?? DEFAULT_MAX_LINES;
|
|
32
|
+
const maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES;
|
|
33
|
+
const totalBytes = Buffer.byteLength(content, 'utf-8');
|
|
34
|
+
const lines = splitLinesForCounting(content);
|
|
35
|
+
const totalLines = lines.length;
|
|
36
|
+
if (totalLines <= maxLines && totalBytes <= maxBytes) {
|
|
37
|
+
return {
|
|
38
|
+
content,
|
|
39
|
+
truncated: false,
|
|
40
|
+
truncatedBy: null,
|
|
41
|
+
totalLines,
|
|
42
|
+
totalBytes,
|
|
43
|
+
outputLines: totalLines,
|
|
44
|
+
outputBytes: totalBytes,
|
|
45
|
+
lastLinePartial: false,
|
|
46
|
+
firstLineExceedsLimit: false,
|
|
47
|
+
maxLines,
|
|
48
|
+
maxBytes,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
const firstLineBytes = Buffer.byteLength(lines[0] ?? '', 'utf-8');
|
|
52
|
+
if (firstLineBytes > maxBytes) {
|
|
53
|
+
return {
|
|
54
|
+
content: '',
|
|
55
|
+
truncated: true,
|
|
56
|
+
truncatedBy: 'bytes',
|
|
57
|
+
totalLines,
|
|
58
|
+
totalBytes,
|
|
59
|
+
outputLines: 0,
|
|
60
|
+
outputBytes: 0,
|
|
61
|
+
lastLinePartial: false,
|
|
62
|
+
firstLineExceedsLimit: true,
|
|
63
|
+
maxLines,
|
|
64
|
+
maxBytes,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
const outputLinesArr = [];
|
|
68
|
+
let outputBytesCount = 0;
|
|
69
|
+
let truncatedBy = 'lines';
|
|
70
|
+
for (let i = 0; i < lines.length && i < maxLines; i++) {
|
|
71
|
+
const line = lines[i];
|
|
72
|
+
const lineBytes = Buffer.byteLength(line, 'utf-8') + (i > 0 ? 1 : 0);
|
|
73
|
+
if (outputBytesCount + lineBytes > maxBytes) {
|
|
74
|
+
truncatedBy = 'bytes';
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
77
|
+
outputLinesArr.push(line);
|
|
78
|
+
outputBytesCount += lineBytes;
|
|
79
|
+
}
|
|
80
|
+
if (outputLinesArr.length >= maxLines && outputBytesCount <= maxBytes) {
|
|
81
|
+
truncatedBy = 'lines';
|
|
82
|
+
}
|
|
83
|
+
const outputContent = outputLinesArr.join('\n');
|
|
84
|
+
const finalOutputBytes = Buffer.byteLength(outputContent, 'utf-8');
|
|
85
|
+
return {
|
|
86
|
+
content: outputContent,
|
|
87
|
+
truncated: true,
|
|
88
|
+
truncatedBy,
|
|
89
|
+
totalLines,
|
|
90
|
+
totalBytes,
|
|
91
|
+
outputLines: outputLinesArr.length,
|
|
92
|
+
outputBytes: finalOutputBytes,
|
|
93
|
+
lastLinePartial: false,
|
|
94
|
+
firstLineExceedsLimit: false,
|
|
95
|
+
maxLines,
|
|
96
|
+
maxBytes,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare const AUTO_READ_MAX = 2000;
|
|
2
|
+
export declare const SNIFF_BYTES = 8192;
|
|
3
|
+
export declare const MAX_BYTES: number;
|
|
4
|
+
export declare const MAX_READ_LINE_BYTES: number;
|
|
5
|
+
export declare const HASH_STORE_BUSY_TIMEOUT = 1000;
|
|
6
|
+
export declare const HASH_STORE_VERSION = 6;
|
|
7
|
+
export declare const BATCH_EDIT_MAX_ITEMS = 32;
|
|
8
|
+
export declare const SERVED_TTL_MS: number;
|
|
9
|
+
export declare const SERVED_ECHO_CAP = 150;
|
|
10
|
+
export declare const NOOP_LOOP_THRESHOLD = 3;
|
|
11
|
+
export declare const NEW_CONTENT_NOT_STRING_MSG: string;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { type ServedRow, type ResolvedRange } from "./hashline/served.js";
|
|
2
|
+
export declare const DRIFT_NOTICE_HEADING = "Drift notice:";
|
|
3
|
+
export interface DriftRow extends ServedRow {
|
|
4
|
+
content: string;
|
|
5
|
+
drifted: boolean;
|
|
6
|
+
}
|
|
7
|
+
export interface ComputeDriftInput {
|
|
8
|
+
served: (string | null)[];
|
|
9
|
+
resultHashes: string[];
|
|
10
|
+
resultLines: string[];
|
|
11
|
+
range: ResolvedRange;
|
|
12
|
+
reported: Set<string>;
|
|
13
|
+
cap?: number;
|
|
14
|
+
}
|
|
15
|
+
export interface DriftNoticeResult {
|
|
16
|
+
text: string;
|
|
17
|
+
rows: DriftRow[];
|
|
18
|
+
total: number;
|
|
19
|
+
allAlreadyReported: boolean;
|
|
20
|
+
}
|
|
21
|
+
export declare function computeDrift(input: ComputeDriftInput): DriftNoticeResult | undefined;
|
|
22
|
+
export declare function scanDrift(input: {
|
|
23
|
+
sessionKey: string;
|
|
24
|
+
served: (string | null)[];
|
|
25
|
+
resultHashes: string[];
|
|
26
|
+
resultLines: string[];
|
|
27
|
+
range: ResolvedRange;
|
|
28
|
+
path: string;
|
|
29
|
+
}): Promise<string | undefined>;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh execution-context helpers shared by the tool layer: session cwd and
|
|
3
|
+
* served-state session key derived from the dsh {@link ToolExecution}.
|
|
4
|
+
* @module dsh-better-edit/dsh-context
|
|
5
|
+
*/
|
|
6
|
+
import type { ToolExecution } from '@deepseek-ai/dsh-tools';
|
|
7
|
+
/**
|
|
8
|
+
* The workspace directory a model-facing filesystem tool resolves relative
|
|
9
|
+
* paths against: the calling agent's per-session cwd (each session's tools act
|
|
10
|
+
* on ITS workspace), falling back to the process cwd for non-agent callers.
|
|
11
|
+
* Mirrors how `dsh-tool-bash` defaults a bash `workdir` to the session cwd.
|
|
12
|
+
* @param exec - the tool-execution context; only its optional `agent` is read.
|
|
13
|
+
* @returns the session cwd, or the process cwd when no agent applies.
|
|
14
|
+
*/
|
|
15
|
+
export declare function execCwd(exec: ToolExecution): string;
|
|
16
|
+
/**
|
|
17
|
+
* The served-state session key for one execution: the live session id. A
|
|
18
|
+
* stable key per session keeps served rows (what the model was shown) and
|
|
19
|
+
* undo history correctly scoped — never shared across sessions.
|
|
20
|
+
* @param exec - the tool-execution context.
|
|
21
|
+
* @returns the session key.
|
|
22
|
+
*/
|
|
23
|
+
export declare function execSessionKey(exec: ToolExecution): string;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { ServedRow } from "./hashline/served.js";
|
|
2
|
+
export type LineEnding = "\r\n" | "\n" | "\r";
|
|
3
|
+
export declare function detectEnding(content: string): LineEnding;
|
|
4
|
+
export declare function toLF(text: string): string;
|
|
5
|
+
export declare function restoreEndings(text: string, ending: LineEnding): string;
|
|
6
|
+
export declare function stripBOM(content: string): {
|
|
7
|
+
bom: string;
|
|
8
|
+
text: string;
|
|
9
|
+
};
|
|
10
|
+
export declare function genDiff(oldContent: string, newContent: string, contextLines?: number, newContentHashes?: string[], oldContentHashes?: string[]): {
|
|
11
|
+
diff: string;
|
|
12
|
+
firstChangedLine: number | undefined;
|
|
13
|
+
servedRows: ServedRow[];
|
|
14
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function normReq(input: unknown): unknown;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The single-edit pipeline shared by `edit`, `batch_edit`, and previews:
|
|
3
|
+
* resolve → normalize → hash → verify anchors against served state → apply →
|
|
4
|
+
* re-hash with stable anchors → drift notice. All IO goes through the
|
|
5
|
+
* {@link FileIO} bridge so the same pipeline runs on `ctx.fs` and on the host
|
|
6
|
+
* filesystem.
|
|
7
|
+
* @module dsh-better-edit/edit-pipeline
|
|
8
|
+
*/
|
|
9
|
+
import type { HashStore } from './hash-store.js';
|
|
10
|
+
import type { FileIO } from './fs-bridge.js';
|
|
11
|
+
import type { LineEnding } from './edit-diff.js';
|
|
12
|
+
import { type HEdit, type NEdit } from './hashline/index.js';
|
|
13
|
+
import type { ResolvedRange } from './hashline/served.js';
|
|
14
|
+
import type { EditParams } from './schema.js';
|
|
15
|
+
export declare function assertReq(request: unknown): asserts request is EditParams;
|
|
16
|
+
export declare function resolveMissingPath(request: Record<string, unknown>): Promise<{
|
|
17
|
+
path: string;
|
|
18
|
+
warning: string;
|
|
19
|
+
} | undefined>;
|
|
20
|
+
export interface PipelineResult {
|
|
21
|
+
path: string;
|
|
22
|
+
absolutePath: string;
|
|
23
|
+
originalNormalized: string;
|
|
24
|
+
result: string;
|
|
25
|
+
bom: string;
|
|
26
|
+
originalEnding: LineEnding;
|
|
27
|
+
hadUtf8DecodeErrors: boolean;
|
|
28
|
+
warnings: string[];
|
|
29
|
+
noopEdit?: NEdit;
|
|
30
|
+
firstChangedLine?: number;
|
|
31
|
+
lastChangedLine?: number;
|
|
32
|
+
originalHashes: string[];
|
|
33
|
+
resultHashes: string[];
|
|
34
|
+
totalAddedLines: number;
|
|
35
|
+
totalRemovedLines: number;
|
|
36
|
+
driftNotice?: string;
|
|
37
|
+
range: ResolvedRange;
|
|
38
|
+
}
|
|
39
|
+
export interface ExecPipelineOptions {
|
|
40
|
+
signal?: AbortSignal;
|
|
41
|
+
store?: HashStore;
|
|
42
|
+
noPersist?: boolean;
|
|
43
|
+
sessionKey?: string;
|
|
44
|
+
}
|
|
45
|
+
export declare function collectRemovedHashes(edit: HEdit, originalHashes: string[]): Set<string>;
|
|
46
|
+
export declare function countLineChanges(edit: HEdit, originalHashes: string[], isNoop: boolean, removedAutoFixes: number): {
|
|
47
|
+
totalAddedLines: number;
|
|
48
|
+
totalRemovedLines: number;
|
|
49
|
+
};
|
|
50
|
+
export declare function execPipeline(io: FileIO, params: EditParams, cwd: string, options?: ExecPipelineOptions): Promise<PipelineResult>;
|
|
51
|
+
/** Resolve the display path a caller names against the session cwd. */
|
|
52
|
+
export declare function resolveDisplayPath(path: string, cwd: string): string;
|
|
53
|
+
/** Snapshot bookkeeping for noop/success results (best-effort). */
|
|
54
|
+
export declare function snapshotIdFor(io: FileIO, absolutePath: string, signal?: AbortSignal): Promise<string | undefined>;
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import type { ServedRow } from "./hashline/served.js";
|
|
2
|
+
export type EditDetails = {
|
|
3
|
+
diff: string;
|
|
4
|
+
firstChangedLine?: number;
|
|
5
|
+
snapshotId?: string;
|
|
6
|
+
classification?: "noop";
|
|
7
|
+
metrics?: RMetrics;
|
|
8
|
+
servedRows?: ServedRow[];
|
|
9
|
+
servedByPath?: Array<{
|
|
10
|
+
path: string;
|
|
11
|
+
servedRows: ServedRow[];
|
|
12
|
+
}>;
|
|
13
|
+
warnings?: string[];
|
|
14
|
+
driftNotice?: string;
|
|
15
|
+
};
|
|
16
|
+
type TResult = {
|
|
17
|
+
content: Array<{
|
|
18
|
+
type: "text";
|
|
19
|
+
text: string;
|
|
20
|
+
}>;
|
|
21
|
+
isError?: boolean;
|
|
22
|
+
details: EditDetails;
|
|
23
|
+
};
|
|
24
|
+
export type RMetrics = {
|
|
25
|
+
edits_attempted: number;
|
|
26
|
+
edits_noop: number;
|
|
27
|
+
warnings: number;
|
|
28
|
+
classification: "applied" | "noop";
|
|
29
|
+
changed_lines?: {
|
|
30
|
+
first: number;
|
|
31
|
+
last: number;
|
|
32
|
+
};
|
|
33
|
+
added_lines?: number;
|
|
34
|
+
removed_lines?: number;
|
|
35
|
+
};
|
|
36
|
+
export type RMeta = {
|
|
37
|
+
editsAttempted: number;
|
|
38
|
+
noopEditsCount: number;
|
|
39
|
+
firstChangedLine?: number;
|
|
40
|
+
lastChangedLine?: number;
|
|
41
|
+
addedLines: number;
|
|
42
|
+
removedLines: number;
|
|
43
|
+
};
|
|
44
|
+
type NEditEntry = {
|
|
45
|
+
loc: string;
|
|
46
|
+
currentContent: string;
|
|
47
|
+
};
|
|
48
|
+
export interface NoopInput {
|
|
49
|
+
path: string;
|
|
50
|
+
noopEdit: NEditEntry | undefined;
|
|
51
|
+
snapshotId?: string;
|
|
52
|
+
editMeta: RMeta;
|
|
53
|
+
warnings: string[] | undefined;
|
|
54
|
+
driftNotice?: string;
|
|
55
|
+
}
|
|
56
|
+
export interface SuccessInput {
|
|
57
|
+
path: string;
|
|
58
|
+
originalNormalized: string;
|
|
59
|
+
originalHashes: string[];
|
|
60
|
+
result: string;
|
|
61
|
+
resultHashes: string[];
|
|
62
|
+
warnings: string[] | undefined;
|
|
63
|
+
snapshotId?: string;
|
|
64
|
+
editMeta: RMeta;
|
|
65
|
+
driftNotice?: string;
|
|
66
|
+
}
|
|
67
|
+
export declare function buildMetrics(args: {
|
|
68
|
+
classification: "applied" | "noop";
|
|
69
|
+
editsAttempted: number;
|
|
70
|
+
noopEditsCount: number;
|
|
71
|
+
warningsCount: number;
|
|
72
|
+
firstChangedLine?: number;
|
|
73
|
+
lastChangedLine?: number;
|
|
74
|
+
addedLines?: number;
|
|
75
|
+
removedLines?: number;
|
|
76
|
+
}): RMetrics;
|
|
77
|
+
export interface FinalizeInput {
|
|
78
|
+
diff: string;
|
|
79
|
+
warnings?: string[];
|
|
80
|
+
driftNotice?: string;
|
|
81
|
+
}
|
|
82
|
+
export declare function finalizeResult(input: FinalizeInput): string;
|
|
83
|
+
export declare function finalizeToolResult(details: EditDetails): {
|
|
84
|
+
content: Array<{
|
|
85
|
+
type: "text";
|
|
86
|
+
text: string;
|
|
87
|
+
}>;
|
|
88
|
+
servedRows: ServedRow[] | undefined;
|
|
89
|
+
};
|
|
90
|
+
export declare function buildNoop(input: NoopInput): TResult;
|
|
91
|
+
export declare function buildChanged(input: SuccessInput): TResult;
|
|
92
|
+
export type BatchSection = {
|
|
93
|
+
path: string;
|
|
94
|
+
originalNormalized: string;
|
|
95
|
+
result: string;
|
|
96
|
+
originalHashes: string[];
|
|
97
|
+
resultHashes: string[];
|
|
98
|
+
warnings: string[] | undefined;
|
|
99
|
+
driftNotice: string | undefined;
|
|
100
|
+
appliedCount: number;
|
|
101
|
+
noopCount: number;
|
|
102
|
+
totalAddedLines: number;
|
|
103
|
+
totalRemovedLines: number;
|
|
104
|
+
};
|
|
105
|
+
export type BatchDetails = EditDetails;
|
|
106
|
+
export declare function buildBatchResult(sections: BatchSection[]): TResult;
|
|
107
|
+
export {};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export type LFile = {
|
|
2
|
+
kind: "directory";
|
|
3
|
+
} | {
|
|
4
|
+
kind: "image";
|
|
5
|
+
mimeType: string;
|
|
6
|
+
} | {
|
|
7
|
+
kind: "text";
|
|
8
|
+
text: string;
|
|
9
|
+
hadUtf8DecodeErrors?: true;
|
|
10
|
+
} | {
|
|
11
|
+
kind: "binary";
|
|
12
|
+
description: string;
|
|
13
|
+
};
|
|
14
|
+
export interface LoadFileOptions {
|
|
15
|
+
maxLines?: number;
|
|
16
|
+
displayPath?: string;
|
|
17
|
+
}
|
|
18
|
+
export declare function loadFileKindAndText(filePath: string, options?: LoadFileOptions): Promise<LFile>;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { type LFile } from "./file-kind.js";
|
|
2
|
+
import { type LineEnding } from "./edit-diff.js";
|
|
3
|
+
import type { HashStore } from "./hash-store.js";
|
|
4
|
+
export interface NormFile {
|
|
5
|
+
absolutePath: string;
|
|
6
|
+
normalized: string;
|
|
7
|
+
bom: string;
|
|
8
|
+
originalEnding: LineEnding;
|
|
9
|
+
fileHashes: string[];
|
|
10
|
+
hadUtf8DecodeErrors: boolean;
|
|
11
|
+
}
|
|
12
|
+
export type SnapInfo = {
|
|
13
|
+
snapshotId: string;
|
|
14
|
+
ino: number;
|
|
15
|
+
mtimeMs: number;
|
|
16
|
+
ctimeMs: number;
|
|
17
|
+
size: number;
|
|
18
|
+
};
|
|
19
|
+
export declare function fileSnap(absolutePath: string): Promise<SnapInfo>;
|
|
20
|
+
export interface ReadNormOptions {
|
|
21
|
+
signal?: AbortSignal;
|
|
22
|
+
accessMode?: number;
|
|
23
|
+
preloadedFile?: LFile;
|
|
24
|
+
maxLines?: number;
|
|
25
|
+
store?: HashStore;
|
|
26
|
+
noPersist?: boolean;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Hashline normalization shared by every reader: strip the BOM, detect the
|
|
30
|
+
* original line ending, normalize to LF, enforce the line ceiling, and assign
|
|
31
|
+
* (or recall from the hash store) the per-line hash anchors.
|
|
32
|
+
*
|
|
33
|
+
* This is the string-level core of the read/edit pipeline; the caller owns
|
|
34
|
+
* path resolution and raw IO (local filesystem, `ctx.fs`, or a sandbox
|
|
35
|
+
* backend) so the same anchors are produced no matter where the file lives.
|
|
36
|
+
*/
|
|
37
|
+
export declare function normFromText(input: {
|
|
38
|
+
absolutePath: string;
|
|
39
|
+
rawText: string;
|
|
40
|
+
displayPath: string;
|
|
41
|
+
signal?: AbortSignal;
|
|
42
|
+
maxLines?: number;
|
|
43
|
+
store?: HashStore;
|
|
44
|
+
noPersist?: boolean;
|
|
45
|
+
hadUtf8DecodeErrors?: boolean;
|
|
46
|
+
}): Promise<NormFile>;
|
|
47
|
+
export declare function readNormFile(path: string, cwd: string, options?: ReadNormOptions): Promise<NormFile>;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* IO seam for the hashline tool layer. The tools resolve, read, and write
|
|
3
|
+
* through this bridge so they honor the deployment's `ctx.fs` backend — a
|
|
4
|
+
* sandboxed or remote filesystem — instead of reaching around it. The local
|
|
5
|
+
* implementation exists for tests and for pure-pipeline verification.
|
|
6
|
+
* @module dsh-better-edit/fs-bridge
|
|
7
|
+
*/
|
|
8
|
+
import type { Context } from '@deepseek-ai/cordis';
|
|
9
|
+
import type { FileSystem } from '@deepseek-ai/dsh-fs';
|
|
10
|
+
/** Text-IO operations the hashline tools need, keyed by canonical absolute path. */
|
|
11
|
+
export interface FileIO {
|
|
12
|
+
/** Resolve a (possibly relative) request path against the session cwd to a canonical absolute path. */
|
|
13
|
+
resolve(path: string, cwd: string, signal?: AbortSignal): Promise<string>;
|
|
14
|
+
/** Read whole text; missing files, directories, and binary content throw. */
|
|
15
|
+
readText(absolutePath: string, signal?: AbortSignal): Promise<string>;
|
|
16
|
+
/** Atomically write whole text, preserving mode when the file exists. */
|
|
17
|
+
writeText(absolutePath: string, content: string, signal?: AbortSignal): Promise<void>;
|
|
18
|
+
/** Opaque change-version for snapshot bookkeeping, or undefined when unavailable. */
|
|
19
|
+
statVersion(absolutePath: string, signal?: AbortSignal): Promise<string | undefined>;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Map an `ctx.fs` failure onto the hashline model-facing vocabulary so the
|
|
23
|
+
* model sees the same structured error codes as the pure pipeline.
|
|
24
|
+
* @param error - the thrown FsError or any error.
|
|
25
|
+
* @param displayPath - the path as the model wrote it.
|
|
26
|
+
* @returns the mapped error, rethrown.
|
|
27
|
+
*/
|
|
28
|
+
export declare function mapFsError(error: unknown, displayPath: string): never;
|
|
29
|
+
/** FileIO over the deployment's `ctx.fs` service. */
|
|
30
|
+
export declare function ctxFsIO(fs: FileSystem, _ctx: Context): FileIO;
|
|
31
|
+
/** FileIO over the host filesystem directly (tests, previews, fallback). */
|
|
32
|
+
export declare function localIO(): FileIO;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
type SqlParams = (string | number)[];
|
|
2
|
+
interface Prepared {
|
|
3
|
+
get: (...params: SqlParams) => Record<string, unknown> | undefined;
|
|
4
|
+
allPaths: (...params: SqlParams) => Record<string, unknown>[];
|
|
5
|
+
allHashes: (...params: SqlParams) => Record<string, unknown>[];
|
|
6
|
+
deleteOne: (...params: SqlParams) => void;
|
|
7
|
+
upsert: (...params: SqlParams) => void;
|
|
8
|
+
undoUpsert: (...params: SqlParams) => void;
|
|
9
|
+
undoGet: (...params: SqlParams) => Record<string, unknown> | undefined;
|
|
10
|
+
undoDelete: (...params: SqlParams) => void;
|
|
11
|
+
servedGet: (...params: SqlParams) => Record<string, unknown> | undefined;
|
|
12
|
+
servedUpsert: (...params: SqlParams) => void;
|
|
13
|
+
servedReportedUpsert: (...params: SqlParams) => void;
|
|
14
|
+
servedReportedClear: (...params: SqlParams) => void;
|
|
15
|
+
servedDelete: (...params: SqlParams) => void;
|
|
16
|
+
servedDeletePath: (...params: SqlParams) => void;
|
|
17
|
+
servedWipe: (...params: SqlParams) => void;
|
|
18
|
+
servedPruneOlderThan: (...params: SqlParams) => void;
|
|
19
|
+
}
|
|
20
|
+
export interface HashStore {
|
|
21
|
+
readonly stmts: Prepared;
|
|
22
|
+
readonly engine: "node:sqlite";
|
|
23
|
+
}
|
|
24
|
+
export declare function isCorruptionError(error: unknown): boolean;
|
|
25
|
+
export declare function loadHashStore(): Promise<HashStore>;
|
|
26
|
+
export declare function shutdownHashStore(): void;
|
|
27
|
+
export declare function withStore(fn: () => void): void;
|
|
28
|
+
export { getSnapshot, upsertSnapshot, findSnapshotPaths, pruneMissing, isValidHashList, isValidSnapshot, } from "./snapshot-store.js";
|
|
29
|
+
export type { LegacySnapshot } from "./snapshot-store.js";
|
|
30
|
+
export { getServed, upsertServed, recordServes, getReported, addReported, clearReported, deleteServed, wipeServed, } from "./served-store.js";
|
|
31
|
+
export type { ServedEntry } from "./served-store.js";
|
|
32
|
+
export { upsertUndo, getUndoEntry, deleteUndo, } from "./undo-store.js";
|
|
33
|
+
export type { UndoRecord } from "./undo-store.js";
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { type ResolvedRange } from "./served.js";
|
|
2
|
+
import { type NEdit, type HEdit, type AutoFix } from "./resolve.js";
|
|
3
|
+
type LIdx = {
|
|
4
|
+
fileLines: string[];
|
|
5
|
+
lineStarts: number[];
|
|
6
|
+
};
|
|
7
|
+
export declare function buildIdx(content: string): LIdx;
|
|
8
|
+
export declare function applyEdit(content: string, edit: HEdit, signal?: AbortSignal, precomputedHashes?: string[], filePath?: string, served?: (string | null)[]): {
|
|
9
|
+
content: string;
|
|
10
|
+
firstChangedLine: number | undefined;
|
|
11
|
+
lastChangedLine: number | undefined;
|
|
12
|
+
range: ResolvedRange;
|
|
13
|
+
warnings?: string[];
|
|
14
|
+
noopEdit?: NEdit;
|
|
15
|
+
autoFixes?: AutoFix[];
|
|
16
|
+
};
|
|
17
|
+
export declare function fmtRegion(hashes: string[], lines: string[]): string;
|
|
18
|
+
export declare function changedRange(original: string, result: string): {
|
|
19
|
+
firstChangedLine: number;
|
|
20
|
+
lastChangedLine: number;
|
|
21
|
+
} | null;
|
|
22
|
+
export {};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { type HashStore } from "../hash-store.js";
|
|
2
|
+
import { initHasher } from "./hasher.js";
|
|
3
|
+
import { HASH_LEN, ALPH_RE, HASH_CLASS } from "./alphabet.js";
|
|
4
|
+
export { initHasher, HASH_LEN, ALPH_RE, HASH_CLASS };
|
|
5
|
+
export declare const ANCHOR_LEN = 3;
|
|
6
|
+
export declare const HASH_SEP = "\u2502";
|
|
7
|
+
export declare const HASH_SPACE: number;
|
|
8
|
+
export declare const MAX_HASH_LINES: number;
|
|
9
|
+
export declare const HASH_PROBE_STRIDE: number;
|
|
10
|
+
export declare const HL_PREFIX_PLUS_RE: RegExp;
|
|
11
|
+
export declare const HL_PREFIX_MINUS_RE: RegExp;
|
|
12
|
+
export declare const HL_BARE_PREFIX_RE: RegExp;
|
|
13
|
+
export declare function canon(line: string): string;
|
|
14
|
+
export declare function _lineHashesPure(content: string): string[];
|
|
15
|
+
export declare function lineHashes(content: string, path?: string, previous?: {
|
|
16
|
+
content: string;
|
|
17
|
+
hashes: string[];
|
|
18
|
+
removedHashes?: Set<string>;
|
|
19
|
+
}, store?: HashStore, persist?: boolean): Promise<string[]>;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export type Hasher = {
|
|
2
|
+
h32(input: string, seed?: number): number;
|
|
3
|
+
h64ToString(input: string, seed?: bigint): string;
|
|
4
|
+
};
|
|
5
|
+
export declare function getH(): Hasher;
|
|
6
|
+
export declare function initHasher(): Promise<Hasher>;
|
|
7
|
+
export declare function xxh32(input: string, seed?: number): number;
|
|
8
|
+
export declare function contentChecksum(content: string): string;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { HASH_LEN, ANCHOR_LEN, HASH_SEP, HASH_CLASS, HASH_SPACE, HASH_PROBE_STRIDE, MAX_HASH_LINES, HL_PREFIX_PLUS_RE, HL_PREFIX_MINUS_RE, HL_BARE_PREFIX_RE, lineHashes, _lineHashesPure, initHasher, canon, } from "./hash.js";
|
|
2
|
+
export { parseHashRef, parseText, type Anchor, } from "./parse.js";
|
|
3
|
+
export { type RAnchor, type HEdit, type RHEdit, type HTEdit, type NEdit, type BDup, type AutoFix, resEdit, valEdit, stripBarePrefixes, stripDiffPrefixes, swapReversedRanges, fmtMismatch, findNewEdge, } from "./resolve.js";
|
|
4
|
+
export { buildIdx, applyEdit, fmtRegion, changedRange, } from "./apply.js";
|