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
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { type Anchor } from "./parse.js";
|
|
2
|
+
import type { ServedRow } from "./served.js";
|
|
3
|
+
export type RAnchor = {
|
|
4
|
+
line: number;
|
|
5
|
+
hash: string;
|
|
6
|
+
hashMatched: boolean;
|
|
7
|
+
};
|
|
8
|
+
export type HEdit = {
|
|
9
|
+
content_lines: string[];
|
|
10
|
+
hash_bounds: [Anchor, Anchor];
|
|
11
|
+
};
|
|
12
|
+
export type RHEdit = {
|
|
13
|
+
content_lines: string[];
|
|
14
|
+
hash_bounds: [RAnchor, RAnchor];
|
|
15
|
+
};
|
|
16
|
+
interface HMismatch {
|
|
17
|
+
ref: Anchor;
|
|
18
|
+
kind: "not_found" | "ambiguous";
|
|
19
|
+
candidates?: number[];
|
|
20
|
+
context?: RAnchor;
|
|
21
|
+
}
|
|
22
|
+
export interface BDup {
|
|
23
|
+
kind: "trailing" | "leading" | "first-new-after" | "last-new-before";
|
|
24
|
+
replacementLineIndex: number;
|
|
25
|
+
}
|
|
26
|
+
export interface AutoFix {
|
|
27
|
+
kind: "trailing" | "leading" | "first-new-after" | "last-new-before";
|
|
28
|
+
removedLine: string;
|
|
29
|
+
removedLineIndex: number;
|
|
30
|
+
}
|
|
31
|
+
export interface NEdit {
|
|
32
|
+
loc: string;
|
|
33
|
+
currentContent: string;
|
|
34
|
+
}
|
|
35
|
+
export type HTEdit = {
|
|
36
|
+
replacement_text: string;
|
|
37
|
+
remove_from: string;
|
|
38
|
+
remove_to: string;
|
|
39
|
+
};
|
|
40
|
+
export declare function fmtMismatch(mismatches: HMismatch[], fileLines: string[], fileHashes: string[], filePath?: string): string;
|
|
41
|
+
export declare function fmtMismatchWithServes(mismatches: HMismatch[], fileLines: string[], fileHashes: string[], filePath?: string): {
|
|
42
|
+
message: string;
|
|
43
|
+
servedRows: ServedRow[];
|
|
44
|
+
};
|
|
45
|
+
export declare function resEdit(edit: HTEdit, warnings?: string[]): HEdit;
|
|
46
|
+
declare function warnUnicodeEsc(edit: HEdit, warnings: string[]): void;
|
|
47
|
+
export declare function stripBarePrefixes(edit: HEdit, fileHashes: string[], warnings: string[]): HEdit;
|
|
48
|
+
export declare function stripDiffPrefixes(edit: HEdit, warnings: string[]): HEdit;
|
|
49
|
+
export declare function swapReversedRanges(edit: HEdit, fileHashes: string[], warnings: string[]): HEdit;
|
|
50
|
+
export declare function findNewEdge(contentLines: string[], rangeLines: string[], fromEnd: boolean): {
|
|
51
|
+
index: number;
|
|
52
|
+
line: string;
|
|
53
|
+
} | undefined;
|
|
54
|
+
export declare function valEdit(edit: HEdit, fileLines: string[], fileHashes: string[], warnings: string[], signal: AbortSignal | undefined): {
|
|
55
|
+
resolved: RHEdit | undefined;
|
|
56
|
+
mismatches: HMismatch[];
|
|
57
|
+
boundaryDups: BDup[];
|
|
58
|
+
};
|
|
59
|
+
export { warnUnicodeEsc };
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
export type ServedCode = "E_RANGE_STALE" | "E_RANGE_UNSERVED" | "E_RANGE_UNVERIFIED";
|
|
2
|
+
export interface ServedRow {
|
|
3
|
+
position: number;
|
|
4
|
+
hash: string;
|
|
5
|
+
}
|
|
6
|
+
export declare class ServedRejectionError extends Error {
|
|
7
|
+
readonly code: ServedCode;
|
|
8
|
+
readonly firstOffendingLine: number | undefined;
|
|
9
|
+
readonly servedRows: ServedRow[];
|
|
10
|
+
constructor(opts: {
|
|
11
|
+
code: ServedCode;
|
|
12
|
+
message: string;
|
|
13
|
+
firstOffendingLine?: number;
|
|
14
|
+
servedRows: ServedRow[];
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
export declare function isServedRejection(error: unknown): error is ServedRejectionError;
|
|
18
|
+
export declare class AnchorMismatchError extends Error {
|
|
19
|
+
readonly servedRows: ServedRow[];
|
|
20
|
+
constructor(message: string, servedRows: ServedRow[]);
|
|
21
|
+
}
|
|
22
|
+
export declare function isAnchorMismatch(error: unknown): error is AnchorMismatchError;
|
|
23
|
+
export declare function buildRangeEcho(startLine: number, endLine: number, fileHashes: string[]): ServedRow[];
|
|
24
|
+
export declare function fmtServedRows(rows: ServedRow[], fileLines: string[]): string;
|
|
25
|
+
export declare function verifyServedRange(args: {
|
|
26
|
+
served: (string | null)[];
|
|
27
|
+
startHash: string;
|
|
28
|
+
endHash: string;
|
|
29
|
+
startLine: number;
|
|
30
|
+
endLine: number;
|
|
31
|
+
fileHashes: string[];
|
|
32
|
+
fileLines: string[];
|
|
33
|
+
filePath?: string;
|
|
34
|
+
}): void;
|
|
35
|
+
export interface ResolvedRange {
|
|
36
|
+
startLine: number;
|
|
37
|
+
endLine: number;
|
|
38
|
+
startHash: string;
|
|
39
|
+
endHash: string;
|
|
40
|
+
delta: number;
|
|
41
|
+
}
|
|
42
|
+
export type ServeRecordPolicy = "live" | "preview";
|
|
43
|
+
export declare function recordEchoServes(sessionKey: string, path: string, rows: ServedRow[], policy: ServeRecordPolicy): Promise<void>;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-better-edit — hash-anchored read/edit/batch_edit/undo_last_edit for
|
|
3
|
+
* DeepSeek Harness, a dsh port of pi-hashline-edit-lsz.
|
|
4
|
+
*
|
|
5
|
+
* Cordis host-plane plugin (mounted by the bundle's cordis.patch.yml). On
|
|
6
|
+
* `agent/session-start` it registers the hashline tools and prompt sections on
|
|
7
|
+
* the AGENT's own scope layer, so they shadow the preset's built-in `read` /
|
|
8
|
+
* `edit` for that agent (nearest layer wins in dsh's tool registry) and unwind
|
|
9
|
+
* automatically when the agent is disposed. The built-in `write` stays in
|
|
10
|
+
* place; a scoped `tools/post-execute` listener appends the fresh hashline
|
|
11
|
+
* preview to write results.
|
|
12
|
+
* @module dsh-better-edit
|
|
13
|
+
*/
|
|
14
|
+
import type { Context } from '@deepseek-ai/cordis';
|
|
15
|
+
/** Cordis plugin name used by loader diagnostics. */
|
|
16
|
+
export declare const name = "dsh-better-edit";
|
|
17
|
+
/** One per-agent registration bundle, disposed with the agent. */
|
|
18
|
+
interface AgentTools {
|
|
19
|
+
dispose(): void;
|
|
20
|
+
}
|
|
21
|
+
/** Mount the bundle: initialize the store, then install tools per agent. */
|
|
22
|
+
export declare function apply(rootCtx: Context): void;
|
|
23
|
+
export type { AgentTools };
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { NOOP_LOOP_THRESHOLD } from "./constants.js";
|
|
2
|
+
export declare function noopPayloadKey(absolutePath: string, removeFrom: string, removeTo: string, replacementText: string): string;
|
|
3
|
+
export declare function trackNoopPayload(absolutePath: string, payload: string): number;
|
|
4
|
+
export declare function clearNoopLoop(absolutePath: string): void;
|
|
5
|
+
export { NOOP_LOOP_THRESHOLD };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* On-disk home for dsh-better-edit state, under the DeepSeek Harness home
|
|
3
|
+
* ($DSH_HOME, defaulting to ~/.dsh). The hashline hash snapshots, served-state
|
|
4
|
+
* rows, and undo history live under a plugin-owned subdirectory so multiple
|
|
5
|
+
* dsh installs on one machine share one coherent store keyed by absolute path.
|
|
6
|
+
*/
|
|
7
|
+
export declare function configDir(): string;
|
|
8
|
+
export declare function hashStorePath(): string;
|
|
9
|
+
export declare function legacyHashStorePath(): string;
|
|
10
|
+
export declare function hashStoreDir(): string;
|
|
11
|
+
export declare function toCwd(filePath: string, cwd: string): string;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Model-facing prompt text for the hashline tools, embedded so the bundle
|
|
3
|
+
* ships no external prompt files. Each tool's description is short; the
|
|
4
|
+
* `tool:*` system-prompt sections carry the full usage guidance the model
|
|
5
|
+
* reads when the tools are presented.
|
|
6
|
+
* @module dsh-better-edit/prompts
|
|
7
|
+
*/
|
|
8
|
+
export declare const EDIT_DESCRIPTION: string;
|
|
9
|
+
export declare const EDIT_SNIPPET = "Edit lines in a text file via bare 3-char HASH anchors from read \u2014 hash only, never line content; anchor exactly the lines that change; one edit per tool call";
|
|
10
|
+
export declare const EDIT_GUIDELINES: string[];
|
|
11
|
+
export declare const READ_DESCRIPTION: string;
|
|
12
|
+
export declare const READ_SNIPPET = "Read a file; each line returned as HASH\u2502content";
|
|
13
|
+
export declare const READ_GUIDELINES: string[];
|
|
14
|
+
export declare const BATCH_EDIT_DESCRIPTION: string;
|
|
15
|
+
export declare const BATCH_EDIT_GUIDELINES: string[];
|
|
16
|
+
export declare const UNDO_DESCRIPTION: string;
|
|
17
|
+
export declare const UNDO_GUIDELINES: string[];
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hash-anchored read rendering: turn normalized text into `HASH│content` rows
|
|
3
|
+
* with pagination hints, oversized-line markers, and served-row bookkeeping.
|
|
4
|
+
* Ported from pi-hashline-edit-lsz's read tool (pi APIs replaced by local
|
|
5
|
+
* helpers).
|
|
6
|
+
* @module dsh-better-edit/read-render
|
|
7
|
+
*/
|
|
8
|
+
import { MAX_HASH_LINES } from './hashline/index.js';
|
|
9
|
+
import type { ServedRow } from './hashline/served.js';
|
|
10
|
+
import { type TruncationResult } from './truncate.js';
|
|
11
|
+
export declare function formatPaginationHint(startLine: number, endLine: number, totalLines: number, nextOffset: number, byteLimit?: number): string;
|
|
12
|
+
export declare function fmtReadPreview(text: string, options: {
|
|
13
|
+
offset?: number;
|
|
14
|
+
limit?: number;
|
|
15
|
+
}, precomputedHashes?: string[], path?: string, maxLineBytes?: number, maxTruncLines?: number): Promise<{
|
|
16
|
+
text: string;
|
|
17
|
+
truncation?: TruncationResult;
|
|
18
|
+
nextOffset?: number;
|
|
19
|
+
served: ServedRow[];
|
|
20
|
+
}>;
|
|
21
|
+
export { MAX_HASH_LINES };
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared model-facing parameter schemas for the hashline tools, expressed in
|
|
3
|
+
* the dsh schema DSL (not TypeBox). `path` is deliberately NOT `required` at
|
|
4
|
+
* the schema level: the tools accept the built-in `file_path` spelling too
|
|
5
|
+
* (the implicit parameter root stays open), and enforce path presence in
|
|
6
|
+
* `assertReq` after `normalizeFilePath` aliasing.
|
|
7
|
+
* @module dsh-better-edit/schema
|
|
8
|
+
*/
|
|
9
|
+
export declare const replacementTextSchema: {
|
|
10
|
+
readonly type: "string";
|
|
11
|
+
readonly description: "Replacement text as a single string with \\n line separators; every \\n separates lines, so a trailing \\n adds a final empty line. Mirror the removed lines exactly, blank lines included. A replacement that is only blank lines is written as one \\n per blank line. Use \"\" to delete the range.";
|
|
12
|
+
};
|
|
13
|
+
export declare const removeFromSchema: {
|
|
14
|
+
readonly type: "string";
|
|
15
|
+
readonly description: "Bare 3-char HASH only (e.g. \"aB3\") — copy just the hash from the leftmost column of a read row like `aB3│content`; never the line content. Marks the FIRST line to remove (inclusive)";
|
|
16
|
+
};
|
|
17
|
+
export declare const removeToSchema: {
|
|
18
|
+
readonly type: "string";
|
|
19
|
+
readonly description: "Bare 3-char HASH only (e.g. \"aB3\") — copy just the hash from the leftmost column of a read row like `aB3│content`; never the line content. Marks the LAST line to remove (inclusive)";
|
|
20
|
+
};
|
|
21
|
+
export declare const pathSchema: {
|
|
22
|
+
readonly type: "string";
|
|
23
|
+
readonly description: "Path to edit. Required — always provide it explicitly; it is only auto-resolved from the anchors as a fallback when omitted by mistake.";
|
|
24
|
+
};
|
|
25
|
+
export interface EditParams {
|
|
26
|
+
path: string;
|
|
27
|
+
remove_from: string;
|
|
28
|
+
remove_to: string;
|
|
29
|
+
replacement_text: string;
|
|
30
|
+
}
|
|
31
|
+
export interface BatchItemParams {
|
|
32
|
+
path?: string;
|
|
33
|
+
remove_from: string;
|
|
34
|
+
remove_to: string;
|
|
35
|
+
replacement_text: string;
|
|
36
|
+
}
|
|
37
|
+
export interface BatchEditParams {
|
|
38
|
+
edits: BatchItemParams[];
|
|
39
|
+
}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { type ServedEntry, loadServed, recordServed, driftReported, markDriftReported, clearDriftReported, wipeServedState, sessionKeyFor, } from "./served-store.js";
|
|
2
|
+
export declare function servedPositionsOf(served: (string | null)[], hash: string): number[];
|
|
3
|
+
export declare function currentPositionOfDrifted(served: (string | null)[], currentPositions: Map<string, number>, surviving: Set<string>, servedIndex: number, delta: number): number;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { type HashStore } from "./hash-store.js";
|
|
2
|
+
export type ServedEntry = {
|
|
3
|
+
position: number;
|
|
4
|
+
hash: string | null;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* Map a caller to the served-state session key. dsh passes the live session id
|
|
8
|
+
* (`exec.agent.session.id`); callers without one (tests, previews) share a
|
|
9
|
+
* process-local fallback so hashline verification never fabricates a session.
|
|
10
|
+
*/
|
|
11
|
+
export declare function sessionKeyFor(sessionId?: string): string;
|
|
12
|
+
export declare function getServed(store: HashStore, sessionKey: string, path: string): (string | null)[];
|
|
13
|
+
export declare function upsertServed(store: HashStore, sessionKey: string, path: string, entries: Array<{
|
|
14
|
+
position: number;
|
|
15
|
+
hash: string | null;
|
|
16
|
+
}>): void;
|
|
17
|
+
export declare function recordServes(store: HashStore, sessionKey: string, path: string, rows: Array<{
|
|
18
|
+
position: number;
|
|
19
|
+
hash: string | null;
|
|
20
|
+
}>): void;
|
|
21
|
+
/**
|
|
22
|
+
* Record post-mutation diff serves against the file's CURRENT length. Before
|
|
23
|
+
* upserting the diff rows, positions beyond `lineCount` are dropped from the
|
|
24
|
+
* stored array: after an edit/undo the file has a new shape, and a hash that
|
|
25
|
+
* survived into the diff must not keep its pre-edit position claim — that
|
|
26
|
+
* stale claim is what makes a chained edit's boundary anchor look ambiguous
|
|
27
|
+
* (`E_RANGE_UNVERIFIED`) instead of verifying cleanly.
|
|
28
|
+
* @param store - open hash store.
|
|
29
|
+
* @param sessionKey - the session whose served rows are being updated.
|
|
30
|
+
* @param path - canonical absolute path.
|
|
31
|
+
* @param rows - diff rows (position → post-mutation hash) to record.
|
|
32
|
+
* @param lineCount - the file's line count AFTER the mutation.
|
|
33
|
+
*/
|
|
34
|
+
export declare function recordServesTruncated(store: HashStore, sessionKey: string, path: string, rows: Array<{
|
|
35
|
+
position: number;
|
|
36
|
+
hash: string | null;
|
|
37
|
+
}>, lineCount: number, clearFrom?: number): void;
|
|
38
|
+
export declare function getReported(store: HashStore, sessionKey: string, path: string): Set<string>;
|
|
39
|
+
export declare function addReported(store: HashStore, sessionKey: string, path: string, hashes: string[]): void;
|
|
40
|
+
export declare function clearReported(store: HashStore, sessionKey: string, path: string): void;
|
|
41
|
+
export declare function deleteServed(store: HashStore, sessionKey: string, path: string): void;
|
|
42
|
+
export declare function wipeServed(store: HashStore, sessionKey: string): void;
|
|
43
|
+
export declare function loadServed(sessionKey: string, path: string): Promise<(string | null)[]>;
|
|
44
|
+
export declare function recordServed(sessionKey: string, path: string, rows: ServedEntry[]): Promise<void>;
|
|
45
|
+
/**
|
|
46
|
+
* Async sibling of {@link recordServesTruncated}: record post-mutation diff
|
|
47
|
+
* rows against the file's current line count.
|
|
48
|
+
*/
|
|
49
|
+
export declare function recordServedTruncated(sessionKey: string, path: string, rows: ServedEntry[], lineCount: number, clearFrom?: number): Promise<void>;
|
|
50
|
+
export declare function driftReported(sessionKey: string, path: string): Promise<Set<string>>;
|
|
51
|
+
export declare function markDriftReported(sessionKey: string, path: string, hashes: string[]): Promise<void>;
|
|
52
|
+
export declare function clearDriftReported(sessionKey: string, path: string): Promise<void>;
|
|
53
|
+
export declare function wipeServedState(sessionKey: string): Promise<void>;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type HashStore } from "./hash-store.js";
|
|
2
|
+
export interface LegacySnapshot {
|
|
3
|
+
content: string;
|
|
4
|
+
hashes: string[];
|
|
5
|
+
}
|
|
6
|
+
export declare function isValidHashList(value: unknown): value is string[];
|
|
7
|
+
export declare function isValidSnapshot(value: unknown): value is LegacySnapshot;
|
|
8
|
+
export declare function getSnapshot(store: HashStore, path: string, content: string, deleteCorrupt?: boolean): string[] | undefined;
|
|
9
|
+
export declare function upsertSnapshot(store: HashStore, path: string, checksum: string, lineCount: number, hashes: string[]): void;
|
|
10
|
+
export declare function findSnapshotPathsByHashes(hashes: string[]): Promise<string[]>;
|
|
11
|
+
export declare function pruneMissingAll(): Promise<void>;
|
|
12
|
+
export declare function upsertSnapshotFor(path: string, checksum: string, lineCount: number, hashes: string[]): Promise<void>;
|
|
13
|
+
export declare function findSnapshotPaths(store: HashStore, hashes: string[]): string[];
|
|
14
|
+
export declare function pruneMissing(store: HashStore): Promise<void>;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The dsh `batch_edit` tool: several hash-anchored edits in one all-or-nothing
|
|
3
|
+
* call. Items targeting the same file are applied in order against the served
|
|
4
|
+
* state; any failing item rejects the whole batch with nothing written, and
|
|
5
|
+
* the failing item's current range is echoed as fresh serves.
|
|
6
|
+
* @module dsh-better-edit/tool-batch-edit
|
|
7
|
+
*/
|
|
8
|
+
import type { Context } from '@deepseek-ai/cordis';
|
|
9
|
+
import type { FileIO } from './fs-bridge.js';
|
|
10
|
+
/**
|
|
11
|
+
* Register the `batch_edit` tool on the calling agent's scope.
|
|
12
|
+
* @param _rootCtx - host context.
|
|
13
|
+
* @param agentCtx - the agent's scoped context (own scope layer).
|
|
14
|
+
* @param io - the filesystem bridge.
|
|
15
|
+
* @returns the exact disposer that unregisters the tool.
|
|
16
|
+
*/
|
|
17
|
+
export declare function buildBatchEditTool(io: FileIO): import("@deepseek-ai/dsh-tools").ToolDefinition;
|
|
18
|
+
/**
|
|
19
|
+
* Register the hashline tool on the calling agent’s scope (own layer).
|
|
20
|
+
*/
|
|
21
|
+
export declare function registerBatchEditTool(_rootCtx: Context, agentCtx: Context, io: FileIO): () => void;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The dsh `edit` tool: hash-anchored literal range edits that shadow the
|
|
3
|
+
* built-in `edit` on the agent's own scope layer. Registered through the
|
|
4
|
+
* agent context so the model-facing contract (remove_from/remove_to hashes,
|
|
5
|
+
* served-range verification, reject-and-serve) replaces the built-in one.
|
|
6
|
+
* @module dsh-better-edit/tool-edit
|
|
7
|
+
*/
|
|
8
|
+
import type { Context } from '@deepseek-ai/cordis';
|
|
9
|
+
import type { FileIO } from './fs-bridge.js';
|
|
10
|
+
/**
|
|
11
|
+
* Register the hash-anchored `edit` tool on the calling agent's scope.
|
|
12
|
+
* @param _rootCtx - host context (logger, lifecycle).
|
|
13
|
+
* @param agentCtx - the agent's scoped context; registrations here land on the
|
|
14
|
+
* agent's OWN scope layer, shadowing the preset's built-in `edit`.
|
|
15
|
+
* @param io - the filesystem bridge (ctx.fs backed in deployment).
|
|
16
|
+
* @returns the exact disposer that unregisters the tool.
|
|
17
|
+
*/
|
|
18
|
+
export declare function buildEditTool(io: FileIO): import("@deepseek-ai/dsh-tools").ToolDefinition;
|
|
19
|
+
/**
|
|
20
|
+
* Register the hashline tool on the calling agent’s scope (own layer).
|
|
21
|
+
*/
|
|
22
|
+
export declare function registerEditTool(_rootCtx: Context, agentCtx: Context, io: FileIO): () => void;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The dsh `read` tool: hash-anchored reads (`HASH│content` rows) that shadow
|
|
3
|
+
* the built-in `read` on the agent's own scope layer. Every shown row is
|
|
4
|
+
* recorded as served, so a later `edit` can verify the model was actually
|
|
5
|
+
* shown the lines it targets.
|
|
6
|
+
* @module dsh-better-edit/tool-read
|
|
7
|
+
*/
|
|
8
|
+
import type { Context } from '@deepseek-ai/cordis';
|
|
9
|
+
import type { FileIO } from './fs-bridge.js';
|
|
10
|
+
/**
|
|
11
|
+
* Register the hash-anchored `read` tool on the calling agent's scope.
|
|
12
|
+
* @param _rootCtx - host context.
|
|
13
|
+
* @param agentCtx - the agent's scoped context (own scope layer).
|
|
14
|
+
* @param io - the filesystem bridge.
|
|
15
|
+
* @returns the exact disposer that unregisters the tool.
|
|
16
|
+
*/
|
|
17
|
+
export declare function buildReadTool(io: FileIO): import("@deepseek-ai/dsh-tools").ToolDefinition;
|
|
18
|
+
/**
|
|
19
|
+
* Register the hashline tool on the calling agent’s scope (own layer).
|
|
20
|
+
*/
|
|
21
|
+
export declare function registerReadTool(_rootCtx: Context, agentCtx: Context, io: FileIO): () => void;
|
|
@@ -0,0 +1,20 @@
|
|
|
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 type { Context } from '@deepseek-ai/cordis';
|
|
8
|
+
import type { FileIO } from './fs-bridge.js';
|
|
9
|
+
/**
|
|
10
|
+
* Register the `undo_last_edit` tool on the calling agent's scope.
|
|
11
|
+
* @param _rootCtx - host context.
|
|
12
|
+
* @param agentCtx - the agent's scoped context (own scope layer).
|
|
13
|
+
* @param io - the filesystem bridge.
|
|
14
|
+
* @returns the exact disposer that unregisters the tool.
|
|
15
|
+
*/
|
|
16
|
+
export declare function buildUndoTool(io: FileIO): import("@deepseek-ai/dsh-tools").ToolDefinition;
|
|
17
|
+
/**
|
|
18
|
+
* Register the hashline tool on the calling agent’s scope (own layer).
|
|
19
|
+
*/
|
|
20
|
+
export declare function registerUndoTool(_rootCtx: Context, agentCtx: Context, io: FileIO): () => void;
|
|
@@ -0,0 +1,32 @@
|
|
|
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 declare const DEFAULT_MAX_LINES = 2000;
|
|
8
|
+
export declare const DEFAULT_MAX_BYTES: number;
|
|
9
|
+
export interface TruncationResult {
|
|
10
|
+
content: string;
|
|
11
|
+
truncated: boolean;
|
|
12
|
+
truncatedBy: 'lines' | 'bytes' | null;
|
|
13
|
+
totalLines: number;
|
|
14
|
+
totalBytes: number;
|
|
15
|
+
outputLines: number;
|
|
16
|
+
outputBytes: number;
|
|
17
|
+
lastLinePartial: boolean;
|
|
18
|
+
firstLineExceedsLimit: boolean;
|
|
19
|
+
maxLines: number;
|
|
20
|
+
maxBytes: number;
|
|
21
|
+
}
|
|
22
|
+
/** Format bytes as a human-readable size. */
|
|
23
|
+
export declare function formatSize(bytes: number): string;
|
|
24
|
+
/**
|
|
25
|
+
* Truncate content from the head (keep the first N lines/bytes). Suitable for
|
|
26
|
+
* file reads; never returns partial lines. When the first line alone exceeds
|
|
27
|
+
* the byte limit the content is emptied with `firstLineExceedsLimit`.
|
|
28
|
+
*/
|
|
29
|
+
export declare function truncateHead(content: string, options?: {
|
|
30
|
+
maxLines?: number;
|
|
31
|
+
maxBytes?: number;
|
|
32
|
+
}): TruncationResult;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Undo persistence for the hashline tools: before an edit is applied, the
|
|
3
|
+
* pre-edit state (content, BOM, original line ending, hash anchors, and the
|
|
4
|
+
* result content that the undo must verify against) is written to the hash
|
|
5
|
+
* store. `undo_last_edit` reverts only when the file still matches the stored
|
|
6
|
+
* result — a later external write clears the history instead of being
|
|
7
|
+
* overwritten. Undo survives restarts (the store is on disk).
|
|
8
|
+
* @module dsh-better-edit/undo-edit
|
|
9
|
+
*/
|
|
10
|
+
import type { LineEnding } from './edit-diff.js';
|
|
11
|
+
export interface UndoEntry {
|
|
12
|
+
content: string;
|
|
13
|
+
bom: string;
|
|
14
|
+
originalEnding: LineEnding;
|
|
15
|
+
hashes: string[];
|
|
16
|
+
resultContent: string;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Persist an undo entry for one path before mutating it.
|
|
20
|
+
* @param path - canonical absolute path.
|
|
21
|
+
* @param entry - the pre-edit state plus the result content the undo will verify.
|
|
22
|
+
* @returns whether persistence succeeded, plus a restore that puts the previous
|
|
23
|
+
* undo entry back (used when the mutation itself fails).
|
|
24
|
+
*/
|
|
25
|
+
export declare function saveUndo(path: string, entry: UndoEntry): Promise<{
|
|
26
|
+
persisted: boolean;
|
|
27
|
+
restore: () => Promise<void>;
|
|
28
|
+
}>;
|
|
29
|
+
/** Load the last undo entry for a path, if any. */
|
|
30
|
+
export declare function getUndo(path: string): Promise<UndoEntry | undefined>;
|
|
31
|
+
/** Drop the undo entry for a path (a write or an undone revert clears history). */
|
|
32
|
+
export declare function clearUndo(path: string): Promise<void>;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type HashStore } from "./hash-store.js";
|
|
2
|
+
export interface UndoRecord {
|
|
3
|
+
content: string;
|
|
4
|
+
bom: string;
|
|
5
|
+
ending: string;
|
|
6
|
+
hashes: string[];
|
|
7
|
+
resultContent: string;
|
|
8
|
+
}
|
|
9
|
+
export declare function upsertUndo(store: HashStore, path: string, entry: UndoRecord): void;
|
|
10
|
+
export declare function getUndoEntry(store: HashStore, path: string): UndoRecord | undefined;
|
|
11
|
+
export declare function deleteUndo(store: HashStore, path: string): void;
|
|
12
|
+
export declare function readUndo(path: string): Promise<UndoRecord | undefined>;
|
|
13
|
+
export declare function writeUndo(path: string, entry: UndoRecord): Promise<void>;
|
|
14
|
+
export declare function removeUndo(path: string): Promise<void>;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export declare function isRec(value: unknown): value is Record<string, unknown>;
|
|
2
|
+
export declare function normalizeFilePath(record: Record<string, unknown>): void;
|
|
3
|
+
export declare function splitLines(text: string): string[];
|
|
4
|
+
export declare function visLines(text: string): string[];
|
|
5
|
+
export declare function rejectUnknownFields(obj: Record<string, unknown>, allowed: Set<string>, label: string, hint?: string): void;
|
|
6
|
+
export declare function cntDiff(diff: string, marker: "+" | "-"): number;
|
|
7
|
+
export declare function abortIf(signal?: AbortSignal): void;
|
|
8
|
+
export declare function errCode(error: unknown): string | undefined;
|
|
9
|
+
export declare function lastNonEmptyIndex(lines: string[]): number;
|
|
10
|
+
export declare function firstNonEmptyIndex(lines: string[]): number;
|
|
11
|
+
export declare function lastNonEmpty(lines: string[]): string | undefined;
|
|
12
|
+
export declare function firstNonEmpty(lines: string[]): string | undefined;
|
|
13
|
+
export declare function clipLine(line: string, maxLen?: number): string;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { LFile } from "./file-kind.js";
|
|
2
|
+
export declare function valAccess(absolutePath: string, path: string, accessMode?: number): Promise<void>;
|
|
3
|
+
export declare function valKind(file: LFile, path: string): asserts file is {
|
|
4
|
+
kind: "text";
|
|
5
|
+
text: string;
|
|
6
|
+
hadUtf8DecodeErrors?: true;
|
|
7
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
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 type { Context } from '@deepseek-ai/cordis';
|
|
9
|
+
import type { FileIO } from './fs-bridge.js';
|
|
10
|
+
/**
|
|
11
|
+
* Register the post-write auto-read listener on the calling agent's scope.
|
|
12
|
+
* Replaces the result content (never the canonical value) with the original
|
|
13
|
+
* content plus the hashline preview; any failure falls back to the untouched
|
|
14
|
+
* decision so a broken auto-read never breaks the write.
|
|
15
|
+
* @param rootCtx - host context for diagnostics.
|
|
16
|
+
* @param agentCtx - the agent's scoped context; the listener receives only
|
|
17
|
+
* this agent's tool results.
|
|
18
|
+
* @param io - the filesystem bridge.
|
|
19
|
+
* @returns the exact disposer that unregisters the listener.
|
|
20
|
+
*/
|
|
21
|
+
export declare function registerWriteHook(rootCtx: Context, agentCtx: Context, io: FileIO): () => void;
|
package/lib/undo-edit.js
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Undo persistence for the hashline tools: before an edit is applied, the
|
|
3
|
+
* pre-edit state (content, BOM, original line ending, hash anchors, and the
|
|
4
|
+
* result content that the undo must verify against) is written to the hash
|
|
5
|
+
* store. `undo_last_edit` reverts only when the file still matches the stored
|
|
6
|
+
* result — a later external write clears the history instead of being
|
|
7
|
+
* overwritten. Undo survives restarts (the store is on disk).
|
|
8
|
+
* @module dsh-better-edit/undo-edit
|
|
9
|
+
*/
|
|
10
|
+
import { readUndo, writeUndo, removeUndo } from './undo-store.js';
|
|
11
|
+
/**
|
|
12
|
+
* Persist an undo entry for one path before mutating it.
|
|
13
|
+
* @param path - canonical absolute path.
|
|
14
|
+
* @param entry - the pre-edit state plus the result content the undo will verify.
|
|
15
|
+
* @returns whether persistence succeeded, plus a restore that puts the previous
|
|
16
|
+
* undo entry back (used when the mutation itself fails).
|
|
17
|
+
*/
|
|
18
|
+
export async function saveUndo(path, entry) {
|
|
19
|
+
let previous;
|
|
20
|
+
try {
|
|
21
|
+
previous = await readUndo(path);
|
|
22
|
+
await writeUndo(path, {
|
|
23
|
+
content: entry.content,
|
|
24
|
+
bom: entry.bom,
|
|
25
|
+
ending: entry.originalEnding,
|
|
26
|
+
hashes: entry.hashes,
|
|
27
|
+
resultContent: entry.resultContent,
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
console.error('Failed to persist undo entry:', error);
|
|
32
|
+
return { persisted: false, restore: async () => undefined };
|
|
33
|
+
}
|
|
34
|
+
return {
|
|
35
|
+
persisted: true,
|
|
36
|
+
restore: async () => {
|
|
37
|
+
try {
|
|
38
|
+
if (previous)
|
|
39
|
+
await writeUndo(path, previous);
|
|
40
|
+
else
|
|
41
|
+
await removeUndo(path);
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
console.error('Failed to restore previous undo entry:', error);
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
/** Load the last undo entry for a path, if any. */
|
|
50
|
+
export async function getUndo(path) {
|
|
51
|
+
try {
|
|
52
|
+
const record = await readUndo(path);
|
|
53
|
+
if (!record)
|
|
54
|
+
return undefined;
|
|
55
|
+
const originalEnding = record.ending;
|
|
56
|
+
if (originalEnding !== '\r\n' &&
|
|
57
|
+
originalEnding !== '\n' &&
|
|
58
|
+
originalEnding !== '\r') {
|
|
59
|
+
await removeUndo(path);
|
|
60
|
+
return undefined;
|
|
61
|
+
}
|
|
62
|
+
return {
|
|
63
|
+
content: record.content,
|
|
64
|
+
bom: record.bom,
|
|
65
|
+
originalEnding,
|
|
66
|
+
hashes: record.hashes,
|
|
67
|
+
resultContent: record.resultContent,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
console.error('Failed to load undo entry:', error);
|
|
72
|
+
return undefined;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
/** Drop the undo entry for a path (a write or an undone revert clears history). */
|
|
76
|
+
export async function clearUndo(path) {
|
|
77
|
+
try {
|
|
78
|
+
await removeUndo(path);
|
|
79
|
+
}
|
|
80
|
+
catch (error) {
|
|
81
|
+
console.error('Failed to clear undo entry:', error);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { loadHashStore } from "./hash-store.js";
|
|
2
|
+
import { isValidHashList } from "./snapshot-store.js";
|
|
3
|
+
export function upsertUndo(store, path, entry) {
|
|
4
|
+
store.stmts.undoUpsert(path, entry.content, entry.bom, entry.ending, JSON.stringify(entry.hashes), entry.resultContent, Date.now());
|
|
5
|
+
}
|
|
6
|
+
export function getUndoEntry(store, path) {
|
|
7
|
+
const row = store.stmts.undoGet(path);
|
|
8
|
+
if (!row)
|
|
9
|
+
return undefined;
|
|
10
|
+
try {
|
|
11
|
+
const parsed = JSON.parse(row.hashes);
|
|
12
|
+
if (!isValidHashList(parsed)) {
|
|
13
|
+
store.stmts.undoDelete(path);
|
|
14
|
+
return undefined;
|
|
15
|
+
}
|
|
16
|
+
return {
|
|
17
|
+
content: row.content,
|
|
18
|
+
bom: row.bom,
|
|
19
|
+
ending: row.ending,
|
|
20
|
+
hashes: parsed,
|
|
21
|
+
resultContent: row.result_content,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
store.stmts.undoDelete(path);
|
|
26
|
+
return undefined;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
export function deleteUndo(store, path) {
|
|
30
|
+
store.stmts.undoDelete(path);
|
|
31
|
+
}
|
|
32
|
+
export async function readUndo(path) {
|
|
33
|
+
const store = await loadHashStore();
|
|
34
|
+
return getUndoEntry(store, path);
|
|
35
|
+
}
|
|
36
|
+
export async function writeUndo(path, entry) {
|
|
37
|
+
const store = await loadHashStore();
|
|
38
|
+
upsertUndo(store, path, entry);
|
|
39
|
+
}
|
|
40
|
+
export async function removeUndo(path) {
|
|
41
|
+
const store = await loadHashStore();
|
|
42
|
+
deleteUndo(store, path);
|
|
43
|
+
}
|