dsh-better-edit 0.3.0 → 0.3.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.
Files changed (74) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/README.md +9 -5
  3. package/README.zh.md +1 -2
  4. package/lib/constants.js +3 -1
  5. package/lib/contract.js +133 -76
  6. package/lib/drift.js +5 -98
  7. package/lib/dsh-context.js +2 -24
  8. package/lib/edit-engine.js +6 -5
  9. package/lib/edit-pipeline.js +3 -121
  10. package/lib/edit-response.js +16 -8
  11. package/lib/file-kind.js +6 -121
  12. package/lib/file-reader.js +4 -77
  13. package/lib/file-view.js +446 -23
  14. package/lib/fs-bridge.js +39 -1
  15. package/lib/guidance/materialize.js +25 -4
  16. package/lib/guidance/resolve.js +3 -9
  17. package/lib/hash-store.js +6 -3
  18. package/lib/hashline/alphabet.js +2 -6
  19. package/lib/hashline/anchor-pipeline.js +770 -7
  20. package/lib/hashline/apply.js +2 -223
  21. package/lib/hashline/hash-assign.js +248 -13
  22. package/lib/hashline/hash.js +67 -29
  23. package/lib/hashline/hasher.js +2 -23
  24. package/lib/hashline/index.js +7 -1
  25. package/lib/hashline/parse.js +2 -35
  26. package/lib/hashline/pure.js +2 -213
  27. package/lib/hashline/resolve.js +3 -366
  28. package/lib/hashline/served.js +2 -120
  29. package/lib/index.js +0 -2
  30. package/lib/mutation.js +218 -10
  31. package/lib/noop-guard.js +20 -0
  32. package/lib/preview-controller.js +65 -0
  33. package/lib/prompts.js +14 -20
  34. package/lib/read-render.js +3 -139
  35. package/lib/served-store.js +2 -216
  36. package/lib/session-view.js +309 -11
  37. package/lib/tool-edit.js +71 -125
  38. package/lib/tool-undo.js +11 -5
  39. package/lib/truncate.js +3 -95
  40. package/lib/types/constants.d.ts +2 -0
  41. package/lib/types/contract.d.ts +102 -28
  42. package/lib/types/drift.d.ts +5 -29
  43. package/lib/types/dsh-context.d.ts +2 -20
  44. package/lib/types/edit-pipeline.d.ts +3 -44
  45. package/lib/types/file-kind.d.ts +6 -18
  46. package/lib/types/file-reader.d.ts +4 -45
  47. package/lib/types/file-view.d.ts +99 -34
  48. package/lib/types/guidance/materialize.d.ts +3 -3
  49. package/lib/types/guidance/resolve.d.ts +2 -2
  50. package/lib/types/hashline/alphabet.d.ts +1 -5
  51. package/lib/types/hashline/anchor-pipeline.d.ts +111 -10
  52. package/lib/types/hashline/apply.d.ts +1 -22
  53. package/lib/types/hashline/hash-assign.d.ts +25 -19
  54. package/lib/types/hashline/hash.d.ts +8 -9
  55. package/lib/types/hashline/hasher.d.ts +1 -8
  56. package/lib/types/hashline/index.d.ts +7 -1
  57. package/lib/types/hashline/parse.d.ts +1 -7
  58. package/lib/types/hashline/pure.d.ts +1 -26
  59. package/lib/types/hashline/resolve.d.ts +1 -63
  60. package/lib/types/hashline/served.d.ts +1 -43
  61. package/lib/types/mutation.d.ts +63 -10
  62. package/lib/types/noop-guard.d.ts +29 -0
  63. package/lib/types/preview-controller.d.ts +33 -0
  64. package/lib/types/prompts.d.ts +9 -9
  65. package/lib/types/read-render.d.ts +3 -18
  66. package/lib/types/served-store.d.ts +2 -81
  67. package/lib/types/session-view.d.ts +68 -9
  68. package/lib/types/tool-edit.d.ts +3 -14
  69. package/lib/types/truncate.d.ts +3 -29
  70. package/lib/types/validation.d.ts +6 -7
  71. package/lib/types/workspace.d.ts +2 -21
  72. package/lib/validation.js +6 -33
  73. package/lib/workspace.js +2 -27
  74. package/package.json +2 -2
package/lib/truncate.js CHANGED
@@ -1,98 +1,6 @@
1
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.
2
+ * @deprecated — private to FileView seam. Use `from "./file-view.js"` instead.
3
+ * Thin shim re-exporting the read path now owned by FileView.
5
4
  * @module dsh-better-edit/truncate
6
5
  */
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
- }
6
+ export * from "./file-view.js";
@@ -4,6 +4,8 @@ export declare const MAX_BYTES: number;
4
4
  export declare const MAX_READ_LINE_BYTES: number;
5
5
  export declare const HASH_STORE_BUSY_TIMEOUT = 1000;
6
6
  export declare const HASH_STORE_VERSION = 6;
7
+ export declare const EDITS_MAX_ITEMS = 32;
8
+ /** @deprecated batch_edit seam removed (ADR-0003) — use EDITS_MAX_ITEMS */
7
9
  export declare const BATCH_EDIT_MAX_ITEMS = 32;
8
10
  export declare const SERVED_TTL_MS: number;
9
11
  export declare const SERVED_ECHO_CAP = 150;
@@ -1,30 +1,28 @@
1
1
  /**
2
2
  * One module owns the request shapes for the hashline tools — edit,
3
- * batch_edit, read, undo_last_edit — plus their validation. Field sets are
3
+ * read, undo_last_edit — plus their validation. Field sets are
4
4
  * declared once here; every tool validates through these asserts, and the
5
5
  * [E_BAD_SHAPE] vocabulary is shared instead of re-implemented per tool.
6
6
  *
7
- * Note: `resolve.ts` keeps its own internal item check (content-only fields,
8
- * no path) — that is the hashline-internal edit-item shape, deliberately
9
- * decoupled from the tool-layer request contract so the hashline module does
10
- * not depend on this one.
7
+ * Contract now mirrors upstream ADR-0007: {path: string|null, edits: [[remove_from,remove_to,replacement_text],...]} tuple payload,
8
+ * single-file, atomic. batch_edit is removed.
11
9
  * @module dsh-better-edit/contract
12
10
  */
13
- export interface EditParams {
14
- path: string;
11
+ export type EditItem = {
15
12
  remove_from: string;
16
13
  remove_to: string;
17
14
  replacement_text: string;
18
- }
19
- export interface BatchItemParams {
20
- path?: string;
15
+ };
16
+ export type EditRequest = {
17
+ path: string | null;
18
+ edits: EditItem[];
19
+ };
20
+ export interface EditParams {
21
+ path: string;
21
22
  remove_from: string;
22
23
  remove_to: string;
23
24
  replacement_text: string;
24
25
  }
25
- export interface BatchEditParams {
26
- edits: BatchItemParams[];
27
- }
28
26
  export interface ReadParams {
29
27
  path: string;
30
28
  offset?: number;
@@ -33,38 +31,114 @@ export interface ReadParams {
33
31
  export interface UndoParams {
34
32
  path: string;
35
33
  }
34
+ export interface BatchItemParams {
35
+ path?: string;
36
+ remove_from: string;
37
+ remove_to: string;
38
+ replacement_text: string;
39
+ }
40
+ export interface BatchEditParams {
41
+ edits: BatchItemParams[];
42
+ }
43
+ export declare const normalizedEdit: unique symbol;
44
+ export type NormalizedEditRequest = EditRequest & {
45
+ [normalizedEdit]?: true;
46
+ };
47
+ export declare function isNormalizedEdit(input: unknown): input is NormalizedEditRequest;
48
+ export declare function itemFromTuple(value: unknown): EditItem | undefined;
49
+ export declare function editRequestFrom(input: unknown): NormalizedEditRequest | undefined;
50
+ export declare const EDIT_TUPLE_HINT: string;
36
51
  /**
37
- * Normalize `file_path` → `path` alias on the request record. Returns the
38
- * input unchanged when not a record; otherwise returns a shallow copy with
52
+ * Normalize `file_path` → `path` alias on the request record and tuple edits → objects.
53
+ * Returns the input unchanged when not a record; otherwise returns a shallow copy with
39
54
  * the alias applied so callers never mutate the original `args` object.
40
55
  */
41
56
  export declare function normalizeRequest(input: unknown): unknown;
42
57
  /** @deprecated use normalizeRequest — kept as alias for migration */
43
58
  export declare const normReq: typeof normalizeRequest;
44
- export declare function assertEditRequest(request: unknown): asserts request is EditParams;
45
- export declare function assertBatchEditRequest(request: unknown): asserts request is BatchEditParams;
59
+ export declare function prepareEditArguments(args: unknown): Record<string, unknown>;
60
+ export declare function assertEditRequest(request: unknown): asserts request is NormalizedEditRequest;
61
+ export declare function assertBatchEditRequest(_request: unknown): asserts _request is BatchEditParams;
46
62
  export declare function assertReadRequest(request: unknown): asserts request is ReadParams;
47
63
  export declare function assertUndoRequest(request: unknown): asserts request is UndoParams;
48
- /**
49
- * Shared model-facing parameter schemas for the hashline tools, expressed in
50
- * the dsh schema DSL (not TypeBox). `path` is deliberately NOT `required` at
51
- * the schema level: the tools accept the built-in `file_path` spelling too
52
- * (the implicit parameter root stays open), and enforce path presence in
53
- * `assertEditRequest` after `normalizeFilePath` aliasing.
54
- */
55
64
  export declare const replacementTextSchema: {
56
65
  readonly type: "string";
57
- 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.";
66
+ readonly description: "Complete replacement for the range; use \"\" to delete";
58
67
  };
59
68
  export declare const removeFromSchema: {
60
69
  readonly type: "string";
61
- 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)";
70
+ readonly description: "First line to remove (inclusive)";
62
71
  };
63
72
  export declare const removeToSchema: {
64
73
  readonly type: "string";
65
- 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)";
74
+ readonly description: "Last line to remove (inclusive)";
66
75
  };
67
76
  export declare const pathSchema: {
68
77
  readonly type: "string";
69
- 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.";
78
+ readonly description: "File path; null infers it from anchors";
79
+ };
80
+ export declare const editPathSchema: {
81
+ readonly anyOf: readonly [{
82
+ readonly type: "string";
83
+ readonly minLength: 1;
84
+ readonly description: "File path; null infers it from anchors";
85
+ }, {
86
+ readonly type: "null";
87
+ readonly description: "null infers path from anchors";
88
+ }];
89
+ };
90
+ export declare const editTupleSchema: {
91
+ readonly type: "array";
92
+ readonly prefixItems: readonly [{
93
+ readonly type: "string";
94
+ readonly description: "First line to remove (inclusive)";
95
+ }, {
96
+ readonly type: "string";
97
+ readonly description: "Last line to remove (inclusive)";
98
+ }, {
99
+ readonly type: "string";
100
+ readonly description: "Complete replacement for the range; use \"\" to delete";
101
+ }];
102
+ readonly minItems: 3;
103
+ readonly maxItems: 3;
104
+ readonly description: "[remove_from, remove_to, replacement_text]";
105
+ };
106
+ export declare const editToolSchema: {
107
+ readonly type: "object";
108
+ readonly additionalProperties: false;
109
+ readonly required: readonly ["path", "edits"];
110
+ readonly properties: {
111
+ readonly path: {
112
+ readonly anyOf: readonly [{
113
+ readonly type: "string";
114
+ readonly minLength: 1;
115
+ readonly description: "File path; null infers it from anchors";
116
+ }, {
117
+ readonly type: "null";
118
+ readonly description: "null infers path from anchors";
119
+ }];
120
+ };
121
+ readonly edits: {
122
+ readonly type: "array";
123
+ readonly description: "Ordered list of edit tuples";
124
+ readonly minItems: 1;
125
+ readonly maxItems: 32;
126
+ readonly items: {
127
+ readonly type: "array";
128
+ readonly prefixItems: readonly [{
129
+ readonly type: "string";
130
+ readonly description: "First line to remove (inclusive)";
131
+ }, {
132
+ readonly type: "string";
133
+ readonly description: "Last line to remove (inclusive)";
134
+ }, {
135
+ readonly type: "string";
136
+ readonly description: "Complete replacement for the range; use \"\" to delete";
137
+ }];
138
+ readonly minItems: 3;
139
+ readonly maxItems: 3;
140
+ readonly description: "[remove_from, remove_to, replacement_text]";
141
+ };
142
+ };
143
+ };
70
144
  };
@@ -1,29 +1,5 @@
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>;
1
+ /**
2
+ * @deprecated — private to SessionView seam. Use `from "./session-view.js"` instead.
3
+ * @module dsh-better-edit/drift
4
+ */
5
+ export * from "./session-view.js";
@@ -1,23 +1,5 @@
1
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}.
2
+ * @deprecated — private to SessionView seam. Use `from "./session-view.js"` instead.
4
3
  * @module dsh-better-edit/dsh-context
5
4
  */
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;
5
+ export * from "./session-view.js";
@@ -1,47 +1,6 @@
1
1
  /**
2
- * The single-edit pipeline shared by `edit`, previews, and (through the
3
- * engine) `batch_edit`: resolve → normalize → hash → verify anchors against
4
- * served state → apply → re-hash with stable anchors → drift notice. The
5
- * apply/reject/re-hash computation lives in the edit engine
6
- * ({@link edit-engine}); this module keeps the single-edit orchestration
7
- * shape, preview-mode options, and the pipeline result type. All IO goes
8
- * through the {@link FileIO} bridge so the same pipeline runs on `ctx.fs` and
9
- * on the host filesystem.
2
+ * @deprecated — private to Mutation seam. Use `from "./mutation.js"` instead.
3
+ * Thin shim re-exporting the pipeline now owned by Mutation.
10
4
  * @module dsh-better-edit/edit-pipeline
11
5
  */
12
- import type { HashStore } from './hash-store.js';
13
- import type { FileIO } from './fs-bridge.js';
14
- import type { LineEnding } from './edit-diff.js';
15
- import { type NEdit } from './hashline/anchor-pipeline.js';
16
- import type { ResolvedRange } from './hashline/anchor-pipeline.js';
17
- import type { EditParams } from './contract.js';
18
- export interface PipelineResult {
19
- path: string;
20
- absolutePath: string;
21
- originalNormalized: string;
22
- result: string;
23
- bom: string;
24
- originalEnding: LineEnding;
25
- hadUtf8DecodeErrors: boolean;
26
- warnings: string[];
27
- noopEdit?: NEdit;
28
- firstChangedLine?: number;
29
- lastChangedLine?: number;
30
- originalHashes: string[];
31
- resultHashes: string[];
32
- totalAddedLines: number;
33
- totalRemovedLines: number;
34
- driftNotice?: string;
35
- range: ResolvedRange;
36
- }
37
- export interface ExecPipelineOptions {
38
- signal?: AbortSignal;
39
- store?: HashStore;
40
- noPersist?: boolean;
41
- sessionKey?: string;
42
- }
43
- export declare function execPipeline(io: FileIO, params: EditParams, cwd: string, options?: ExecPipelineOptions): Promise<PipelineResult>;
44
- /** Resolve the display path a caller names against the session cwd. */
45
- export declare function resolveDisplayPath(path: string, cwd: string): string;
46
- /** Snapshot bookkeeping for noop/success results (best-effort). */
47
- export declare function snapshotIdFor(io: FileIO, absolutePath: string, signal?: AbortSignal): Promise<string | undefined>;
6
+ export * from "./mutation.js";
@@ -1,18 +1,6 @@
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>;
1
+ /**
2
+ * @deprecated — private to FileView seam. Use `from "./file-view.js"` instead.
3
+ * Thin shim re-exporting the read path now owned by FileView.
4
+ * @module dsh-better-edit/file-kind
5
+ */
6
+ export * from "./file-view.js";
@@ -1,47 +1,6 @@
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
1
  /**
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.
2
+ * @deprecated — private to FileView seam. Use `from "./file-view.js"` instead.
3
+ * Thin shim re-exporting the read path now owned by FileView.
4
+ * @module dsh-better-edit/file-reader
36
5
  */
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>;
6
+ export * from "./file-view.js";
@@ -2,51 +2,125 @@
2
2
  * FileView — deep module owning "what the model sees".
3
3
  *
4
4
  * Single seam for normalize → hash → render → truncate → served-row
5
- * selection. The read path was 6 shallow modules for one concept:
6
- * `tool-read → read-and-serve → file-reader → read-render → fs-bridge
7
- * → file-kind/validation/truncate`, each with interface ≈ implementation.
5
+ * selection. Previously split across file-reader, read-render, truncate,
6
+ * file-kind, validation — each shallow (interface≈implementation). Now one
7
+ * file owns the invariant; deleting it would scatter complexity (deep).
8
8
  *
9
- * Now: one file to understand reads. The private helpers
10
- * (`file-reader`, `read-render`, `truncate`, `file-kind`, `validation`)
11
- * are *private to this seam* — do not import them directly outside this
12
- * module. Import from `file-view` instead. `fs-bridge` stays as the IO seam.
9
+ * Private helpers inlined from file-reader (normFromText/fileSnap),
10
+ * read-render (fmtReadPreview), truncate (truncateHead), file-kind
11
+ * (loadFileKindAndText), validation (valKind/valAccess).
12
+ * Old files are shims re-exporting from this seam for compat.
13
13
  *
14
14
  * Two surfaces:
15
15
  * - `preview` (pure, no IO) — tested without filesystem
16
16
  * - `readView` (IO) — read + normalize + render + truncate + hashes
17
17
  *
18
- * The emergent interaction (MAX_HASH_LINES vs MAX_READ_LINE_BYTES vs
19
- * truncation, partial serve on oversized lines) now lives in one module
20
- * and is one test surface.
21
- *
22
18
  * @module dsh-better-edit/file-view
23
19
  */
24
- import { MAX_HASH_LINES } from "./read-render.js";
20
+ import { type LineEnding } from "./edit-diff.js";
25
21
  import type { FileIO } from "./fs-bridge.js";
26
22
  import type { ServedRow } from "./hashline/served.js";
27
- import type { TruncationResult } from "./truncate.js";
28
- import type { LineEnding } from "./edit-diff.js";
29
- /** What the model sees for one read — the deep seam's core type. */
23
+ import type { HashStore } from "./hash-store.js";
24
+ export declare const MAX_HASH_LINES: number;
25
+ export declare const DEFAULT_MAX_LINES = 2000;
26
+ export declare const DEFAULT_MAX_BYTES: number;
27
+ export interface TruncationResult {
28
+ content: string;
29
+ truncated: boolean;
30
+ truncatedBy: 'lines' | 'bytes' | null;
31
+ totalLines: number;
32
+ totalBytes: number;
33
+ outputLines: number;
34
+ outputBytes: number;
35
+ lastLinePartial: boolean;
36
+ firstLineExceedsLimit: boolean;
37
+ maxLines: number;
38
+ maxBytes: number;
39
+ }
40
+ export declare function formatSize(bytes: number): string;
41
+ export declare function truncateHead(content: string, options?: {
42
+ maxLines?: number;
43
+ maxBytes?: number;
44
+ }): TruncationResult;
45
+ export type LFile = {
46
+ kind: "directory";
47
+ } | {
48
+ kind: "image";
49
+ mimeType: string;
50
+ } | {
51
+ kind: "text";
52
+ text: string;
53
+ hadUtf8DecodeErrors?: true;
54
+ } | {
55
+ kind: "binary";
56
+ description: string;
57
+ };
58
+ export interface LoadFileOptions {
59
+ maxLines?: number;
60
+ displayPath?: string;
61
+ }
62
+ export declare function loadFileKindAndText(filePath: string, options?: LoadFileOptions): Promise<LFile>;
63
+ export declare function valAccess(absolutePath: string, path: string, accessMode?: number): Promise<void>;
64
+ export declare function valKind(file: LFile, path: string): asserts file is {
65
+ kind: "text";
66
+ text: string;
67
+ hadUtf8DecodeErrors?: true;
68
+ };
69
+ export interface NormFile {
70
+ absolutePath: string;
71
+ normalized: string;
72
+ bom: string;
73
+ originalEnding: LineEnding;
74
+ fileHashes: string[];
75
+ hadUtf8DecodeErrors: boolean;
76
+ }
77
+ export type SnapInfo = {
78
+ snapshotId: string;
79
+ ino: number;
80
+ mtimeMs: number;
81
+ ctimeMs: number;
82
+ size: number;
83
+ };
84
+ export declare function fileSnap(absolutePath: string): Promise<SnapInfo>;
85
+ export interface ReadNormOptions {
86
+ signal?: AbortSignal;
87
+ accessMode?: number;
88
+ preloadedFile?: LFile;
89
+ maxLines?: number;
90
+ store?: HashStore;
91
+ noPersist?: boolean;
92
+ }
93
+ export declare function normFromText(input: {
94
+ absolutePath: string;
95
+ rawText: string;
96
+ displayPath: string;
97
+ signal?: AbortSignal;
98
+ maxLines?: number;
99
+ store?: HashStore;
100
+ noPersist?: boolean;
101
+ hadUtf8DecodeErrors?: boolean;
102
+ }): Promise<NormFile>;
103
+ export declare function readNormFile(path: string, cwd: string, options?: ReadNormOptions): Promise<NormFile>;
104
+ export declare function formatPaginationHint(startLine: number, endLine: number, totalLines: number, nextOffset: number, byteLimit?: number): string;
105
+ export declare function fmtReadPreview(text: string, options: {
106
+ offset?: number;
107
+ limit?: number;
108
+ }, precomputedHashes?: string[], path?: string, maxLineBytes?: number, maxTruncLines?: number): Promise<{
109
+ text: string;
110
+ truncation?: TruncationResult;
111
+ nextOffset?: number;
112
+ served: ServedRow[];
113
+ }>;
30
114
  export interface FileView {
31
- /** Model-facing preview text (hashline rows + pagination/oversized notes). */
32
115
  text: string;
33
- /** Per-line hashes for the whole file (canonical, stored deterministically). */
34
116
  hashes: string[];
35
- /** Rows actually shown to the model (empty when nothing shown, e.g. empty file edge). */
36
117
  served: ServedRow[];
37
- /** Canonical absolute path (resolved against cwd). */
38
118
  absolutePath: string;
39
- /** Truncation meta when preview was capped. */
40
119
  truncation?: TruncationResult;
41
- /** Next pagination offset when preview was paginated/truncated. */
42
120
  nextOffset?: number;
43
- /** Whether the raw file had non-UTF-8 bytes (shown as U+FFFD, rewritten on edit). */
44
121
  hadUtf8DecodeErrors: boolean;
45
- /** Stripped BOM if present (for round-trip fidelity on edit/write). */
46
122
  bom: string;
47
- /** Original line ending (for round-trip fidelity). */
48
123
  originalEnding: LineEnding;
49
- /** Normalized LF content (internal, but useful for callers that need content + hashes). */
50
124
  normalized: string;
51
125
  }
52
126
  export interface PreviewOpts {
@@ -56,19 +130,10 @@ export interface PreviewOpts {
56
130
  export interface ReadViewOpts extends PreviewOpts {
57
131
  signal?: AbortSignal;
58
132
  }
59
- /**
60
- * Pure rendering without IO — tested without filesystem.
61
- * Wraps `fmtReadPreview` so callers have one place to render.
62
- */
63
133
  export declare function preview(content: string, hashes: string[], opts?: PreviewOpts, absolutePath?: string): Promise<{
64
134
  text: string;
65
135
  served: ServedRow[];
66
136
  truncation?: TruncationResult;
67
137
  nextOffset?: number;
68
138
  }>;
69
- /**
70
- * IO version: read + normalize + render + truncate + hashes.
71
- * Single place that decides `served[]` — no emergent partial-serve.
72
- */
73
139
  export declare function readView(io: FileIO, path: string, cwd: string, opts?: ReadViewOpts): Promise<FileView>;
74
- export { MAX_HASH_LINES };