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/utils.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
export function isRec(value) {
|
|
2
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
3
|
+
}
|
|
4
|
+
export function normalizeFilePath(record) {
|
|
5
|
+
if (typeof record.path !== "string" && typeof record.file_path === "string") {
|
|
6
|
+
record.path = record.file_path;
|
|
7
|
+
delete record.file_path;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
export function splitLines(text) {
|
|
11
|
+
if (text.length === 0)
|
|
12
|
+
return [""];
|
|
13
|
+
const lines = text.split("\n");
|
|
14
|
+
return text.endsWith("\n") ? lines.slice(0, -1) : lines;
|
|
15
|
+
}
|
|
16
|
+
export function visLines(text) {
|
|
17
|
+
if (text.length === 0)
|
|
18
|
+
return [];
|
|
19
|
+
const lines = text.split("\n");
|
|
20
|
+
return text.endsWith("\n") ? lines.slice(0, -1) : lines;
|
|
21
|
+
}
|
|
22
|
+
export function rejectUnknownFields(obj, allowed, label, hint) {
|
|
23
|
+
const unknown = Object.keys(obj).filter((key) => !allowed.has(key));
|
|
24
|
+
if (unknown.length > 0) {
|
|
25
|
+
const suffix = hint ? ` ${hint}` : "";
|
|
26
|
+
throw new Error(`[E_BAD_SHAPE] ${label} contains unknown or unsupported fields: ${unknown.join(", ")}.${suffix}`);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
export function cntDiff(diff, marker) {
|
|
30
|
+
if (!diff)
|
|
31
|
+
return 0;
|
|
32
|
+
let count = 0;
|
|
33
|
+
for (const line of diff.split("\n")) {
|
|
34
|
+
if (line.startsWith(marker) &&
|
|
35
|
+
!line.startsWith(`${marker}${marker}${marker}`)) {
|
|
36
|
+
count += 1;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return count;
|
|
40
|
+
}
|
|
41
|
+
export function abortIf(signal) {
|
|
42
|
+
if (signal?.aborted)
|
|
43
|
+
throw new Error("Operation aborted");
|
|
44
|
+
}
|
|
45
|
+
export function errCode(error) {
|
|
46
|
+
if (error instanceof Error) {
|
|
47
|
+
return error.code;
|
|
48
|
+
}
|
|
49
|
+
return undefined;
|
|
50
|
+
}
|
|
51
|
+
export function lastNonEmptyIndex(lines) {
|
|
52
|
+
for (let i = lines.length - 1; i >= 0; i--) {
|
|
53
|
+
if (lines[i].length > 0)
|
|
54
|
+
return i;
|
|
55
|
+
}
|
|
56
|
+
return -1;
|
|
57
|
+
}
|
|
58
|
+
export function firstNonEmptyIndex(lines) {
|
|
59
|
+
for (let i = 0; i < lines.length; i++) {
|
|
60
|
+
if (lines[i].length > 0)
|
|
61
|
+
return i;
|
|
62
|
+
}
|
|
63
|
+
return -1;
|
|
64
|
+
}
|
|
65
|
+
export function lastNonEmpty(lines) {
|
|
66
|
+
const idx = lastNonEmptyIndex(lines);
|
|
67
|
+
return idx >= 0 ? lines[idx] : undefined;
|
|
68
|
+
}
|
|
69
|
+
export function firstNonEmpty(lines) {
|
|
70
|
+
const idx = firstNonEmptyIndex(lines);
|
|
71
|
+
return idx >= 0 ? lines[idx] : undefined;
|
|
72
|
+
}
|
|
73
|
+
export function clipLine(line, maxLen = 200) {
|
|
74
|
+
const flat = line.replace(/\n/g, "\\n");
|
|
75
|
+
return flat.length > maxLen ? `${flat.slice(0, maxLen)}...` : flat;
|
|
76
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { constants } from "node:fs";
|
|
2
|
+
import { access as fsAccess } from "fs/promises";
|
|
3
|
+
import { errCode } from "./utils.js";
|
|
4
|
+
export async function valAccess(absolutePath, path, accessMode = constants.R_OK) {
|
|
5
|
+
try {
|
|
6
|
+
await fsAccess(absolutePath, accessMode);
|
|
7
|
+
}
|
|
8
|
+
catch (error) {
|
|
9
|
+
const code = errCode(error);
|
|
10
|
+
if (code === "ENOENT") {
|
|
11
|
+
throw new Error(`[E_NOT_FOUND] File not found: ${path}`);
|
|
12
|
+
}
|
|
13
|
+
if (code === "EACCES" || code === "EPERM") {
|
|
14
|
+
const accessLabel = accessMode & constants.W_OK ? "not writable" : "not readable";
|
|
15
|
+
throw new Error(`[E_ACCESS] File is ${accessLabel}: ${path}`);
|
|
16
|
+
}
|
|
17
|
+
if (code === "ELOOP") {
|
|
18
|
+
throw new Error(`[E_ACCESS] Too many symbolic links while resolving: ${path}`);
|
|
19
|
+
}
|
|
20
|
+
throw new Error(`[E_ACCESS] Cannot access file: ${path}`);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
export function valKind(file, path) {
|
|
24
|
+
if (file.kind === "directory") {
|
|
25
|
+
throw new Error(`[E_NOT_TEXT] Path is a directory: ${path}. Use ls to inspect directories.`);
|
|
26
|
+
}
|
|
27
|
+
if (file.kind === "binary") {
|
|
28
|
+
throw new Error(`[E_NOT_TEXT] Path is a binary file: ${path} (${file.description}). Hashline edit only supports text files.`);
|
|
29
|
+
}
|
|
30
|
+
if (file.kind === "image") {
|
|
31
|
+
throw new Error(`[E_NOT_TEXT] Path is an image file: ${path}. Hashline edit only supports text files.`);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Auto-read after `write`: when the built-in `write` tool succeeds, this scoped
|
|
3
|
+
* `tools/post-execute` listener re-reads the written file and appends a fresh
|
|
4
|
+
* hashline-anchored preview to the model-facing content, so the model gets new
|
|
5
|
+
* anchors without an explicit read call (mirroring pi-hashline-edit-lsz).
|
|
6
|
+
* @module dsh-better-edit/write-hook
|
|
7
|
+
*/
|
|
8
|
+
import { normFromText } from './file-reader.js';
|
|
9
|
+
import { fmtReadPreview, MAX_HASH_LINES } from './read-render.js';
|
|
10
|
+
import { recordServed, clearDriftReported } from './served-store.js';
|
|
11
|
+
import { execCwd, execSessionKey } from './dsh-context.js';
|
|
12
|
+
const AUTO_READ_HEADING = '--- Auto-read (hashline anchors) ---';
|
|
13
|
+
/**
|
|
14
|
+
* Register the post-write auto-read listener on the calling agent's scope.
|
|
15
|
+
* Replaces the result content (never the canonical value) with the original
|
|
16
|
+
* content plus the hashline preview; any failure falls back to the untouched
|
|
17
|
+
* decision so a broken auto-read never breaks the write.
|
|
18
|
+
* @param rootCtx - host context for diagnostics.
|
|
19
|
+
* @param agentCtx - the agent's scoped context; the listener receives only
|
|
20
|
+
* this agent's tool results.
|
|
21
|
+
* @param io - the filesystem bridge.
|
|
22
|
+
* @returns the exact disposer that unregisters the listener.
|
|
23
|
+
*/
|
|
24
|
+
export function registerWriteHook(rootCtx, agentCtx, io) {
|
|
25
|
+
return agentCtx.on('tools/post-execute', async (exec, result, next) => {
|
|
26
|
+
const decision = await next();
|
|
27
|
+
if (exec.name !== 'write' ||
|
|
28
|
+
result.isError ||
|
|
29
|
+
decision.kind !== 'accept') {
|
|
30
|
+
return decision;
|
|
31
|
+
}
|
|
32
|
+
const decisionContent = decision.content ?? result.content;
|
|
33
|
+
const rawPath = exec.arguments
|
|
34
|
+
?.file_path ?? exec.arguments
|
|
35
|
+
?.path;
|
|
36
|
+
if (typeof rawPath !== 'string')
|
|
37
|
+
return decision;
|
|
38
|
+
try {
|
|
39
|
+
const cwd = execCwd(exec);
|
|
40
|
+
const sessionKey = execSessionKey(exec);
|
|
41
|
+
const signal = exec.signal;
|
|
42
|
+
const absolutePath = await io.resolve(rawPath, cwd, signal);
|
|
43
|
+
const rawText = await io.readText(absolutePath, signal);
|
|
44
|
+
const { normalized, fileHashes, hadUtf8DecodeErrors } = await normFromText({
|
|
45
|
+
absolutePath,
|
|
46
|
+
rawText,
|
|
47
|
+
displayPath: rawPath,
|
|
48
|
+
signal,
|
|
49
|
+
maxLines: MAX_HASH_LINES,
|
|
50
|
+
});
|
|
51
|
+
const preview = await fmtReadPreview(normalized, {}, fileHashes, absolutePath);
|
|
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
|
+
}
|
|
71
|
+
});
|
|
72
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "dsh-better-edit",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
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
|
+
"main": "lib/index.js",
|
|
7
|
+
"types": "lib/types/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./lib/types/index.d.ts",
|
|
11
|
+
"default": "./lib/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./package.json": "./package.json"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"lib",
|
|
17
|
+
"cordis.patch.yml",
|
|
18
|
+
"README.md",
|
|
19
|
+
"LICENSE"
|
|
20
|
+
],
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+https://github.com/Rianico/dsh-better-edit.git"
|
|
24
|
+
},
|
|
25
|
+
"homepage": "https://github.com/Rianico/dsh-better-edit",
|
|
26
|
+
"bugs": {
|
|
27
|
+
"url": "https://github.com/Rianico/dsh-better-edit/issues"
|
|
28
|
+
},
|
|
29
|
+
"author": "dsh-better-edit contributors",
|
|
30
|
+
"keywords": [
|
|
31
|
+
"dsh",
|
|
32
|
+
"dsh-plugin",
|
|
33
|
+
"deepseek-harness",
|
|
34
|
+
"coding-agent",
|
|
35
|
+
"hashline",
|
|
36
|
+
"hash-anchored",
|
|
37
|
+
"hash",
|
|
38
|
+
"anchors",
|
|
39
|
+
"read",
|
|
40
|
+
"edit",
|
|
41
|
+
"batch-edit",
|
|
42
|
+
"undo",
|
|
43
|
+
"file-editing",
|
|
44
|
+
"stable"
|
|
45
|
+
],
|
|
46
|
+
"license": "MIT",
|
|
47
|
+
"dsh": {
|
|
48
|
+
"bundle": {
|
|
49
|
+
"patch": "./cordis.patch.yml"
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
"dependencies": {
|
|
53
|
+
"diff": "^8.0.2",
|
|
54
|
+
"file-type": "^21.3.0",
|
|
55
|
+
"xxhash-wasm": "^1.1.0"
|
|
56
|
+
},
|
|
57
|
+
"peerDependencies": {
|
|
58
|
+
"@deepseek-ai/cordis": "^4.0.0",
|
|
59
|
+
"@deepseek-ai/dsh-agent": "^0.1.0-rc.6",
|
|
60
|
+
"@deepseek-ai/dsh-fs": "^0.1.0-rc.6",
|
|
61
|
+
"@deepseek-ai/dsh-home-paths": "^0.1.0-rc.6",
|
|
62
|
+
"@deepseek-ai/dsh-session": "^0.1.0-rc.6",
|
|
63
|
+
"@deepseek-ai/dsh-tools": "^0.1.0-rc.6"
|
|
64
|
+
},
|
|
65
|
+
"engines": {
|
|
66
|
+
"node": "^22.19.0 || >=24.0.0"
|
|
67
|
+
},
|
|
68
|
+
"scripts": {
|
|
69
|
+
"build": "tsc -p tsconfig.json",
|
|
70
|
+
"typecheck": "tsc --noEmit && tsc --noEmit -p tsconfig.test.json",
|
|
71
|
+
"test": "vitest run",
|
|
72
|
+
"test:watch": "vitest",
|
|
73
|
+
"prepublishOnly": "npm run typecheck && npm test"
|
|
74
|
+
},
|
|
75
|
+
"devDependencies": {
|
|
76
|
+
"@deepseek-ai/cordis": "^4.0.1",
|
|
77
|
+
"@deepseek-ai/dsh-agent": "0.1.0-rc.6",
|
|
78
|
+
"@deepseek-ai/dsh-fs": "0.1.0-rc.6",
|
|
79
|
+
"@deepseek-ai/dsh-home-paths": "0.1.0-rc.6",
|
|
80
|
+
"@deepseek-ai/dsh-session": "0.1.0-rc.6",
|
|
81
|
+
"@deepseek-ai/dsh-tools": "0.1.0-rc.6",
|
|
82
|
+
"@types/node": "^24.0.0",
|
|
83
|
+
"@vitest/coverage-v8": "^4.1.10",
|
|
84
|
+
"typescript": "^5.8.0",
|
|
85
|
+
"vitest": "^4.1.8"
|
|
86
|
+
}
|
|
87
|
+
}
|