dsh-better-edit 0.1.0 → 0.1.2
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/README.md +11 -4
- package/lib/fs-bridge.js +57 -16
- package/lib/hash-store.js +52 -27
- package/lib/index.js +41 -31
- package/lib/paths.js +18 -12
- package/lib/tool-batch-edit.js +128 -120
- package/lib/tool-edit.js +113 -110
- package/lib/tool-read.js +50 -43
- package/lib/tool-undo.js +72 -75
- package/lib/types/fs-bridge.d.ts +29 -7
- package/lib/types/hash-store.d.ts +13 -1
- package/lib/types/index.d.ts +9 -1
- package/lib/types/paths.d.ts +12 -8
- package/lib/types/tool-batch-edit.d.ts +2 -2
- package/lib/types/tool-edit.d.ts +2 -2
- package/lib/types/tool-read.d.ts +2 -2
- package/lib/types/tool-undo.d.ts +2 -2
- package/lib/types/workspace.d.ts +24 -0
- package/lib/workspace.js +30 -0
- package/lib/write-hook.js +49 -44
- package/package.json +1 -1
package/lib/write-hook.js
CHANGED
|
@@ -9,6 +9,7 @@ import { normFromText } from './file-reader.js';
|
|
|
9
9
|
import { fmtReadPreview, MAX_HASH_LINES } from './read-render.js';
|
|
10
10
|
import { recordServed, clearDriftReported } from './served-store.js';
|
|
11
11
|
import { execCwd, execSessionKey } from './dsh-context.js';
|
|
12
|
+
import { withWorkspace } from './workspace.js';
|
|
12
13
|
const AUTO_READ_HEADING = '--- Auto-read (hashline anchors) ---';
|
|
13
14
|
/**
|
|
14
15
|
* Register the post-write auto-read listener on the calling agent's scope.
|
|
@@ -23,50 +24,54 @@ const AUTO_READ_HEADING = '--- Auto-read (hashline anchors) ---';
|
|
|
23
24
|
*/
|
|
24
25
|
export function registerWriteHook(rootCtx, agentCtx, io) {
|
|
25
26
|
return agentCtx.on('tools/post-execute', async (exec, result, next) => {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
27
|
+
return withWorkspace(execCwd(exec), async () => {
|
|
28
|
+
return withWorkspace(execCwd(exec), async () => {
|
|
29
|
+
const decision = await next();
|
|
30
|
+
if (exec.name !== 'write' ||
|
|
31
|
+
result.isError ||
|
|
32
|
+
decision.kind !== 'accept') {
|
|
33
|
+
return decision;
|
|
34
|
+
}
|
|
35
|
+
const decisionContent = decision.content ?? result.content;
|
|
36
|
+
const rawPath = exec.arguments
|
|
37
|
+
?.file_path ?? exec.arguments
|
|
38
|
+
?.path;
|
|
39
|
+
if (typeof rawPath !== 'string')
|
|
40
|
+
return decision;
|
|
41
|
+
try {
|
|
42
|
+
const cwd = execCwd(exec);
|
|
43
|
+
const sessionKey = execSessionKey(exec);
|
|
44
|
+
const signal = exec.signal;
|
|
45
|
+
const absolutePath = await io.resolve(rawPath, cwd, signal);
|
|
46
|
+
const rawText = await io.readText(absolutePath, signal);
|
|
47
|
+
const { normalized, fileHashes, hadUtf8DecodeErrors } = await normFromText({
|
|
48
|
+
absolutePath,
|
|
49
|
+
rawText,
|
|
50
|
+
displayPath: rawPath,
|
|
51
|
+
signal,
|
|
52
|
+
maxLines: MAX_HASH_LINES,
|
|
53
|
+
});
|
|
54
|
+
const preview = await fmtReadPreview(normalized, {}, fileHashes, absolutePath);
|
|
55
|
+
if (preview.served.length > 0) {
|
|
56
|
+
await recordServed(sessionKey, absolutePath, preview.served);
|
|
57
|
+
}
|
|
58
|
+
await clearDriftReported(sessionKey, absolutePath);
|
|
59
|
+
const text = hadUtf8DecodeErrors
|
|
60
|
+
? `${preview.text}\n\n[Non-UTF-8 bytes shown as U+FFFD; editing rewrites the file as UTF-8.]`
|
|
61
|
+
: preview.text;
|
|
62
|
+
return {
|
|
63
|
+
kind: 'accept',
|
|
64
|
+
content: [
|
|
65
|
+
...(decisionContent),
|
|
66
|
+
{ type: 'text', text: `\n\n${AUTO_READ_HEADING}\n${text}` },
|
|
67
|
+
],
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
rootCtx.logger.warn(`dsh-better-edit: auto-read after write failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
72
|
+
return decision;
|
|
73
|
+
}
|
|
50
74
|
});
|
|
51
|
-
|
|
52
|
-
if (preview.served.length > 0) {
|
|
53
|
-
await recordServed(sessionKey, absolutePath, preview.served);
|
|
54
|
-
}
|
|
55
|
-
await clearDriftReported(sessionKey, absolutePath);
|
|
56
|
-
const text = hadUtf8DecodeErrors
|
|
57
|
-
? `${preview.text}\n\n[Non-UTF-8 bytes shown as U+FFFD; editing rewrites the file as UTF-8.]`
|
|
58
|
-
: preview.text;
|
|
59
|
-
return {
|
|
60
|
-
kind: 'accept',
|
|
61
|
-
content: [
|
|
62
|
-
...(decisionContent),
|
|
63
|
-
{ type: 'text', text: `\n\n${AUTO_READ_HEADING}\n${text}` },
|
|
64
|
-
],
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
catch (error) {
|
|
68
|
-
rootCtx.logger.warn(`dsh-better-edit: auto-read after write failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
69
|
-
return decision;
|
|
70
|
-
}
|
|
75
|
+
});
|
|
71
76
|
});
|
|
72
77
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-better-edit",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Hash-anchored read/edit/batch_edit/undo_last_edit tools for DeepSeek Harness (dsh). Every line gets a unique 3-char hash (A-Za-z0-9) that stays stable across edits; stale or ambiguous anchors are rejected, never fuzzy-matched. Undo persists across restarts.",
|
|
6
6
|
"main": "lib/index.js",
|