@better-fs-tools/write 0.0.0-tegami-trusted-publish-setup → 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 +21 -0
- package/README.md +443 -6
- package/dist/authorize/ask-before-write.d.ts +11 -0
- package/dist/authorize/ask-before-write.js +49 -0
- package/dist/authorize/index.d.ts +3 -0
- package/dist/authorize/index.js +3 -0
- package/dist/authorize/protect-paths.d.ts +12 -0
- package/dist/authorize/protect-paths.js +74 -0
- package/dist/authorize/write-authorizers.d.ts +9 -0
- package/dist/authorize/write-authorizers.js +48 -0
- package/dist/bundle/create-fs-tools.d.ts +71 -0
- package/dist/bundle/create-fs-tools.js +125 -0
- package/dist/codecs/index.d.ts +1 -0
- package/dist/codecs/index.js +1 -0
- package/dist/codecs/utf8.d.ts +7 -0
- package/dist/codecs/utf8.js +61 -0
- package/dist/contract/codec.d.ts +26 -0
- package/dist/contract/codec.js +0 -0
- package/dist/contract/context.d.ts +14 -0
- package/dist/contract/context.js +0 -0
- package/dist/contract/deps.d.ts +55 -0
- package/dist/contract/deps.js +0 -0
- package/dist/contract/extensions.d.ts +103 -0
- package/dist/contract/extensions.js +0 -0
- package/dist/contract/format.d.ts +15 -0
- package/dist/contract/format.js +0 -0
- package/dist/contract/input.d.ts +35 -0
- package/dist/contract/input.js +0 -0
- package/dist/contract/limits.d.ts +26 -0
- package/dist/contract/limits.js +0 -0
- package/dist/contract/locks.d.ts +14 -0
- package/dist/contract/locks.js +0 -0
- package/dist/contract/matcher.d.ts +30 -0
- package/dist/contract/matcher.js +0 -0
- package/dist/contract/messages.d.ts +211 -0
- package/dist/contract/messages.js +0 -0
- package/dist/contract/patch.d.ts +50 -0
- package/dist/contract/patch.js +0 -0
- package/dist/contract/preconditions.d.ts +9 -0
- package/dist/contract/preconditions.js +5 -0
- package/dist/contract/result.d.ts +102 -0
- package/dist/contract/result.js +0 -0
- package/dist/core/abort.d.ts +4 -0
- package/dist/core/abort.js +23 -0
- package/dist/core/authorize.d.ts +9 -0
- package/dist/core/authorize.js +87 -0
- package/dist/core/bytes.d.ts +3 -0
- package/dist/core/bytes.js +14 -0
- package/dist/core/commit-patch.d.ts +20 -0
- package/dist/core/commit-patch.js +224 -0
- package/dist/core/commit.d.ts +9 -0
- package/dist/core/commit.js +111 -0
- package/dist/core/create-tools.d.ts +23 -0
- package/dist/core/create-tools.js +54 -0
- package/dist/core/deps.d.ts +11 -0
- package/dist/core/deps.js +207 -0
- package/dist/core/diff.d.ts +10 -0
- package/dist/core/diff.js +221 -0
- package/dist/core/encode.d.ts +3 -0
- package/dist/core/encode.js +26 -0
- package/dist/core/extension-error.d.ts +8 -0
- package/dist/core/extension-error.js +17 -0
- package/dist/core/format.d.ts +4 -0
- package/dist/core/format.js +36 -0
- package/dist/core/guards.d.ts +3 -0
- package/dist/core/guards.js +45 -0
- package/dist/core/hints.d.ts +14 -0
- package/dist/core/hints.js +75 -0
- package/dist/core/hooks.d.ts +15 -0
- package/dist/core/hooks.js +115 -0
- package/dist/core/input.d.ts +20 -0
- package/dist/core/input.js +91 -0
- package/dist/core/limits.d.ts +10 -0
- package/dist/core/limits.js +48 -0
- package/dist/core/line-index.d.ts +10 -0
- package/dist/core/line-index.js +38 -0
- package/dist/core/load.d.ts +14 -0
- package/dist/core/load.js +221 -0
- package/dist/core/lock.d.ts +2 -0
- package/dist/core/lock.js +56 -0
- package/dist/core/match.d.ts +15 -0
- package/dist/core/match.js +66 -0
- package/dist/core/messages.d.ts +8 -0
- package/dist/core/messages.js +147 -0
- package/dist/core/outcomes.d.ts +21 -0
- package/dist/core/outcomes.js +127 -0
- package/dist/core/parse-patch.d.ts +3 -0
- package/dist/core/parse-patch.js +64 -0
- package/dist/core/patch-hunks.d.ts +25 -0
- package/dist/core/patch-hunks.js +177 -0
- package/dist/core/patch-pipeline.d.ts +4 -0
- package/dist/core/patch-pipeline.js +285 -0
- package/dist/core/pipeline.d.ts +22 -0
- package/dist/core/pipeline.js +153 -0
- package/dist/core/plan-edit.d.ts +13 -0
- package/dist/core/plan-edit.js +215 -0
- package/dist/core/plan-patch.d.ts +29 -0
- package/dist/core/plan-patch.js +169 -0
- package/dist/core/plan-write.d.ts +6 -0
- package/dist/core/plan-write.js +27 -0
- package/dist/core/planned.d.ts +35 -0
- package/dist/core/planned.js +36 -0
- package/dist/core/precondition.d.ts +10 -0
- package/dist/core/precondition.js +76 -0
- package/dist/core/record.d.ts +3 -0
- package/dist/core/record.js +58 -0
- package/dist/core/resolve.d.ts +3 -0
- package/dist/core/resolve.js +65 -0
- package/dist/core/scope.d.ts +33 -0
- package/dist/core/scope.js +109 -0
- package/dist/core/snippet.d.ts +4 -0
- package/dist/core/snippet.js +36 -0
- package/dist/core/splice.d.ts +10 -0
- package/dist/core/splice.js +19 -0
- package/dist/core/target.d.ts +10 -0
- package/dist/core/target.js +46 -0
- package/dist/formatters/default.d.ts +16 -0
- package/dist/formatters/default.js +122 -0
- package/dist/formatters/index.d.ts +2 -0
- package/dist/formatters/index.js +1 -0
- package/dist/guards/generated-file.d.ts +10 -0
- package/dist/guards/generated-file.js +39 -0
- package/dist/guards/index.d.ts +13 -0
- package/dist/guards/index.js +25 -0
- package/dist/guards/non-text.d.ts +3 -0
- package/dist/guards/non-text.js +46 -0
- package/dist/guards/omission.d.ts +9 -0
- package/dist/guards/omission.js +48 -0
- package/dist/guards/read-prefix.d.ts +12 -0
- package/dist/guards/read-prefix.js +87 -0
- package/dist/guards/shared.d.ts +11 -0
- package/dist/guards/shared.js +46 -0
- package/dist/guards/syntax.d.ts +10 -0
- package/dist/guards/syntax.js +54 -0
- package/dist/guards/truncation-notice.d.ts +8 -0
- package/dist/guards/truncation-notice.js +32 -0
- package/dist/hooks/executable-shebang.d.ts +9 -0
- package/dist/hooks/executable-shebang.js +46 -0
- package/dist/hooks/index.d.ts +2 -0
- package/dist/hooks/index.js +2 -0
- package/dist/hooks/verify-write.d.ts +9 -0
- package/dist/hooks/verify-write.js +63 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.js +15 -0
- package/dist/locks/index.d.ts +1 -0
- package/dist/locks/index.js +1 -0
- package/dist/locks/memory-locks.d.ts +9 -0
- package/dist/locks/memory-locks.js +94 -0
- package/dist/matchers/block-anchor.d.ts +9 -0
- package/dist/matchers/block-anchor.js +69 -0
- package/dist/matchers/escape.d.ts +6 -0
- package/dist/matchers/escape.js +32 -0
- package/dist/matchers/exact.d.ts +4 -0
- package/dist/matchers/exact.js +29 -0
- package/dist/matchers/fold.d.ts +11 -0
- package/dist/matchers/fold.js +144 -0
- package/dist/matchers/indentation.d.ts +3 -0
- package/dist/matchers/indentation.js +81 -0
- package/dist/matchers/index.d.ts +11 -0
- package/dist/matchers/index.js +18 -0
- package/dist/matchers/line-trimmed.d.ts +3 -0
- package/dist/matchers/line-trimmed.js +32 -0
- package/dist/matchers/lines.d.ts +24 -0
- package/dist/matchers/lines.js +74 -0
- package/dist/matchers/normalized.d.ts +7 -0
- package/dist/matchers/normalized.js +69 -0
- package/dist/patch/grammar.d.ts +5 -0
- package/dist/patch/grammar.js +24 -0
- package/dist/patch/index.d.ts +4 -0
- package/dist/patch/index.js +3 -0
- package/dist/patch/parse.d.ts +11 -0
- package/dist/patch/parse.js +209 -0
- package/dist/signature/contract.d.ts +8 -0
- package/dist/signature/contract.js +0 -0
- package/dist/signature/edit.d.ts +19 -0
- package/dist/signature/edit.js +158 -0
- package/dist/signature/index.d.ts +6 -0
- package/dist/signature/index.js +4 -0
- package/dist/signature/matching.d.ts +3 -0
- package/dist/signature/matching.js +25 -0
- package/dist/signature/messages.d.ts +4 -0
- package/dist/signature/messages.js +9 -0
- package/dist/signature/patch.d.ts +6 -0
- package/dist/signature/patch.js +55 -0
- package/dist/signature/schema.d.ts +19 -0
- package/dist/signature/schema.js +117 -0
- package/dist/signature/write.d.ts +9 -0
- package/dist/signature/write.js +52 -0
- package/dist/state/index.d.ts +2 -0
- package/dist/state/index.js +1 -0
- package/dist/state/invalidate.d.ts +29 -0
- package/dist/state/invalidate.js +37 -0
- package/docs/architecture.md +221 -0
- package/docs/hosts.md +95 -0
- package/docs/result-schema.md +268 -0
- package/package.json +53 -2
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export interface MatchContext {
|
|
2
|
+
/** "lines": ranges start at a line start and end at a line end. Patch hunks use it. */
|
|
3
|
+
readonly mode: "text" | "lines";
|
|
4
|
+
/** First UTF-16 index a range may start at. */
|
|
5
|
+
readonly from: number;
|
|
6
|
+
/** Stop after this many ranges. */
|
|
7
|
+
readonly maxMatches: number;
|
|
8
|
+
}
|
|
9
|
+
export interface MatchRange {
|
|
10
|
+
/** UTF-16 offsets into the haystack as given. */
|
|
11
|
+
readonly start: number;
|
|
12
|
+
readonly end: number;
|
|
13
|
+
/** Set when an edge of the hit falls inside a folded span. Such a hit is refused. */
|
|
14
|
+
readonly refused?: "boundary";
|
|
15
|
+
}
|
|
16
|
+
export interface Matcher {
|
|
17
|
+
readonly id: string;
|
|
18
|
+
/** true when a hit is not byte-exact. The result reports it. */
|
|
19
|
+
readonly fuzzy: boolean;
|
|
20
|
+
/** Short phrase for the tool description, for example "trailing whitespace". */
|
|
21
|
+
readonly describe: string;
|
|
22
|
+
/** Ranges in order. Must not throw for any string input. */
|
|
23
|
+
find(haystack: string, needle: string, ctx: MatchContext): readonly MatchRange[];
|
|
24
|
+
/** Rewrite newText for one hit, for example to re-indent it. Default: newText unchanged. */
|
|
25
|
+
adapt?(newText: string, hit: {
|
|
26
|
+
readonly haystack: string;
|
|
27
|
+
readonly needle: string;
|
|
28
|
+
readonly range: MatchRange;
|
|
29
|
+
}): string;
|
|
30
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import type { NodeKind } from "@better-fs-tools/fs";
|
|
2
|
+
import type { ToolMessages } from "@better-fs-tools/read";
|
|
3
|
+
import type { WriteToolName } from "./context.ts";
|
|
4
|
+
import type { WriteErrorCode, WritePhase } from "./result.ts";
|
|
5
|
+
export type WriteCanonicalParam = "path" | "edits" | "oldText" | "newText" | "replaceAll" | "content" | "patch";
|
|
6
|
+
/**
|
|
7
|
+
* Wording for every note the write tools produce. Text that names a model
|
|
8
|
+
* parameter gets the name from `param(name)`, which a signature overrides.
|
|
9
|
+
*/
|
|
10
|
+
export interface WriteMessageCatalog extends ToolMessages {
|
|
11
|
+
/**
|
|
12
|
+
* Host name for a canonical parameter. Default: the canonical name. An empty
|
|
13
|
+
* string means the host has no such parameter, and the default texts leave
|
|
14
|
+
* out the advice that names it.
|
|
15
|
+
*/
|
|
16
|
+
param(name: WriteCanonicalParam): string;
|
|
17
|
+
invalidInput(c: {
|
|
18
|
+
tool: WriteToolName;
|
|
19
|
+
detail: string;
|
|
20
|
+
}): string;
|
|
21
|
+
notFound(c: {
|
|
22
|
+
tool: WriteToolName;
|
|
23
|
+
path: string;
|
|
24
|
+
}): string;
|
|
25
|
+
notAFile(c: {
|
|
26
|
+
path: string;
|
|
27
|
+
kind: NodeKind;
|
|
28
|
+
}): string;
|
|
29
|
+
dangerousPath(c: {
|
|
30
|
+
path: string;
|
|
31
|
+
detail: string | null;
|
|
32
|
+
}): string;
|
|
33
|
+
outsideAllowedRoots(c: {
|
|
34
|
+
path: string;
|
|
35
|
+
}): string;
|
|
36
|
+
permissionDenied(c: {
|
|
37
|
+
path: string;
|
|
38
|
+
}): string;
|
|
39
|
+
readOnly(c: {
|
|
40
|
+
path: string;
|
|
41
|
+
}): string;
|
|
42
|
+
noSpace(c: {
|
|
43
|
+
path: string;
|
|
44
|
+
}): string;
|
|
45
|
+
unsupportedBackend(c: {
|
|
46
|
+
path: string;
|
|
47
|
+
detail: string | null;
|
|
48
|
+
}): string;
|
|
49
|
+
ioError(c: {
|
|
50
|
+
path: string | null;
|
|
51
|
+
}): string;
|
|
52
|
+
aborted(c: {
|
|
53
|
+
phase: WritePhase;
|
|
54
|
+
}): string;
|
|
55
|
+
extensionFailed(c: {
|
|
56
|
+
path: string | null;
|
|
57
|
+
extension: string;
|
|
58
|
+
phase: WritePhase;
|
|
59
|
+
}): string;
|
|
60
|
+
lockTimeout(c: {
|
|
61
|
+
paths: readonly string[];
|
|
62
|
+
}): string;
|
|
63
|
+
/** A guard refused a change and gave no note. */
|
|
64
|
+
guardRefused(c: {
|
|
65
|
+
guard: string;
|
|
66
|
+
path: string;
|
|
67
|
+
}): string;
|
|
68
|
+
notRead(c: {
|
|
69
|
+
tool: WriteToolName;
|
|
70
|
+
path: string;
|
|
71
|
+
wholeFile: boolean;
|
|
72
|
+
}): string;
|
|
73
|
+
stale(c: {
|
|
74
|
+
tool: WriteToolName;
|
|
75
|
+
path: string;
|
|
76
|
+
}): string;
|
|
77
|
+
staleRematched(c: {
|
|
78
|
+
path: string;
|
|
79
|
+
}): string;
|
|
80
|
+
readBeforeWriteOff(c: {
|
|
81
|
+
tool: WriteToolName;
|
|
82
|
+
}): string;
|
|
83
|
+
/** `existing` is true when `what` is "content" and the file exists. */
|
|
84
|
+
tooLarge(c: {
|
|
85
|
+
path: string | null;
|
|
86
|
+
what: "file" | "content" | "patch";
|
|
87
|
+
limit: number;
|
|
88
|
+
existing: boolean;
|
|
89
|
+
}): string;
|
|
90
|
+
notText(c: {
|
|
91
|
+
path: string;
|
|
92
|
+
code: string;
|
|
93
|
+
}): string;
|
|
94
|
+
exists(c: {
|
|
95
|
+
tool: WriteToolName;
|
|
96
|
+
path: string;
|
|
97
|
+
}): string;
|
|
98
|
+
noMatch(c: {
|
|
99
|
+
path: string;
|
|
100
|
+
index: number;
|
|
101
|
+
closest: string | null;
|
|
102
|
+
trailingNewline: "missing" | "extra" | null;
|
|
103
|
+
}): string;
|
|
104
|
+
ambiguousMatch(c: {
|
|
105
|
+
path: string;
|
|
106
|
+
index: number;
|
|
107
|
+
lines: readonly number[];
|
|
108
|
+
total: number;
|
|
109
|
+
}): string;
|
|
110
|
+
matchRefused(c: {
|
|
111
|
+
path: string;
|
|
112
|
+
index: number;
|
|
113
|
+
matcher: string;
|
|
114
|
+
reason: "span" | "boundary" | "escape" | "fuzzy-replace-all" | "too-many";
|
|
115
|
+
}): string;
|
|
116
|
+
overlap(c: {
|
|
117
|
+
path: string;
|
|
118
|
+
first: number;
|
|
119
|
+
second: number;
|
|
120
|
+
}): string;
|
|
121
|
+
noChange(c: {
|
|
122
|
+
path: string;
|
|
123
|
+
}): string;
|
|
124
|
+
alreadyApplied(c: {
|
|
125
|
+
path: string;
|
|
126
|
+
index: number;
|
|
127
|
+
}): string;
|
|
128
|
+
fuzzyMatch(c: {
|
|
129
|
+
path: string;
|
|
130
|
+
index: number;
|
|
131
|
+
matcher: string;
|
|
132
|
+
lines: readonly [number, number];
|
|
133
|
+
}): string;
|
|
134
|
+
repeatedMiss(c: {
|
|
135
|
+
path: string;
|
|
136
|
+
misses: number;
|
|
137
|
+
}): string;
|
|
138
|
+
patchParse(c: {
|
|
139
|
+
line: number;
|
|
140
|
+
detail: string;
|
|
141
|
+
}): string;
|
|
142
|
+
patchVerifyHeader(): string;
|
|
143
|
+
patchDuplicateTarget(c: {
|
|
144
|
+
path: string;
|
|
145
|
+
}): string;
|
|
146
|
+
/** An Update or Delete whose file does not exist. */
|
|
147
|
+
patchNotFound(c: {
|
|
148
|
+
path: string;
|
|
149
|
+
operation: "update" | "delete";
|
|
150
|
+
}): string;
|
|
151
|
+
patchMoveExists(c: {
|
|
152
|
+
path: string;
|
|
153
|
+
from: string;
|
|
154
|
+
}): string;
|
|
155
|
+
/** `hunk` is one-based. */
|
|
156
|
+
patchContextNotFound(c: {
|
|
157
|
+
path: string;
|
|
158
|
+
hunk: number;
|
|
159
|
+
context: string;
|
|
160
|
+
}): string;
|
|
161
|
+
/** `hunk` is one-based. */
|
|
162
|
+
patchLinesNotFound(c: {
|
|
163
|
+
path: string;
|
|
164
|
+
hunk: number;
|
|
165
|
+
lines: readonly string[];
|
|
166
|
+
}): string;
|
|
167
|
+
/** `hunk` is one-based. */
|
|
168
|
+
patchFuzzyMatch(c: {
|
|
169
|
+
path: string;
|
|
170
|
+
hunk: number;
|
|
171
|
+
matcher: string;
|
|
172
|
+
lines: readonly [number, number];
|
|
173
|
+
}): string;
|
|
174
|
+
/** `files` is set when the rollback failed: the state of every file the patch targets. */
|
|
175
|
+
patchCommitFailed(c: {
|
|
176
|
+
path: string;
|
|
177
|
+
code: WriteErrorCode;
|
|
178
|
+
rolledBack: boolean;
|
|
179
|
+
files: readonly {
|
|
180
|
+
readonly path: string;
|
|
181
|
+
readonly state: string;
|
|
182
|
+
}[];
|
|
183
|
+
}): string;
|
|
184
|
+
notAtomic(c: {
|
|
185
|
+
backend: string;
|
|
186
|
+
}): string;
|
|
187
|
+
noCompareAndSwap(c: {
|
|
188
|
+
backend: string;
|
|
189
|
+
}): string;
|
|
190
|
+
modeNotKept(c: {
|
|
191
|
+
backend: string;
|
|
192
|
+
}): string;
|
|
193
|
+
directoriesCreated(c: {
|
|
194
|
+
paths: readonly string[];
|
|
195
|
+
}): string;
|
|
196
|
+
userModified(c: {
|
|
197
|
+
path: string;
|
|
198
|
+
}): string;
|
|
199
|
+
hookRewrote(c: {
|
|
200
|
+
hook: string;
|
|
201
|
+
path: string;
|
|
202
|
+
}): string;
|
|
203
|
+
hookFailed(c: {
|
|
204
|
+
hook: string;
|
|
205
|
+
path: string;
|
|
206
|
+
}): string;
|
|
207
|
+
/** The formatter threw or returned neither a string nor an array. */
|
|
208
|
+
formatterFailed(c: {
|
|
209
|
+
formatter: string;
|
|
210
|
+
}): string;
|
|
211
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/** Turns patch text into a plan. The default is codexPatchParser(). A host may add another format. */
|
|
2
|
+
export interface PatchParser {
|
|
3
|
+
readonly id: string;
|
|
4
|
+
/** Sync and pure. Must not throw for any string input. */
|
|
5
|
+
parse(text: string): PatchParseOutcome;
|
|
6
|
+
}
|
|
7
|
+
export type PatchParseOutcome = {
|
|
8
|
+
readonly ok: true;
|
|
9
|
+
readonly plan: PatchPlan;
|
|
10
|
+
} | {
|
|
11
|
+
readonly ok: false;
|
|
12
|
+
readonly error: {
|
|
13
|
+
readonly line: number;
|
|
14
|
+
readonly detail: string;
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
export interface PatchPlan {
|
|
18
|
+
readonly operations: readonly PatchOperation[];
|
|
19
|
+
}
|
|
20
|
+
export type PatchOperation = {
|
|
21
|
+
readonly kind: "add";
|
|
22
|
+
readonly path: string;
|
|
23
|
+
readonly content: string;
|
|
24
|
+
readonly line: number;
|
|
25
|
+
} | {
|
|
26
|
+
readonly kind: "delete";
|
|
27
|
+
readonly path: string;
|
|
28
|
+
readonly line: number;
|
|
29
|
+
} | {
|
|
30
|
+
readonly kind: "update";
|
|
31
|
+
readonly path: string;
|
|
32
|
+
readonly moveTo: string | null;
|
|
33
|
+
readonly hunks: readonly PatchHunk[];
|
|
34
|
+
readonly line: number;
|
|
35
|
+
};
|
|
36
|
+
export interface PatchHunk {
|
|
37
|
+
/** Text after "@@ ". null for a bare "@@" or a first hunk with no "@@". */
|
|
38
|
+
readonly context: string | null;
|
|
39
|
+
/** The hunk body in order. Old lines are the " " and "-" lines. New lines are the " " and "+" lines. */
|
|
40
|
+
readonly lines: readonly PatchLine[];
|
|
41
|
+
/** "*** End of File" closed the hunk. */
|
|
42
|
+
readonly endOfFile: boolean;
|
|
43
|
+
/** One-based line of the hunk in the patch text. */
|
|
44
|
+
readonly line: number;
|
|
45
|
+
}
|
|
46
|
+
export interface PatchLine {
|
|
47
|
+
readonly kind: " " | "-" | "+";
|
|
48
|
+
/** The line without its prefix and without a line break. */
|
|
49
|
+
readonly text: string;
|
|
50
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export interface PreconditionPolicy {
|
|
2
|
+
/** "existing": an existing file needs a record. "off": no read-before-write. Default "existing". */
|
|
3
|
+
readonly requireRead: "existing" | "off";
|
|
4
|
+
/** Which tools accept a record with wholeFileVisible false. "edit-only" means edit and apply_patch. Default "edit-only". */
|
|
5
|
+
readonly partialRead: "edit-only" | "always" | "never";
|
|
6
|
+
/** edit and apply_patch on a stale record. write always rejects. Default "rematch". */
|
|
7
|
+
readonly onStale: "rematch" | "reject";
|
|
8
|
+
}
|
|
9
|
+
export declare const defaultPreconditions: Readonly<PreconditionPolicy>;
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import type { ContentPart, Note, ToolError } from "@better-fs-tools/read";
|
|
2
|
+
import type { WriteToolName } from "./context.ts";
|
|
3
|
+
/**
|
|
4
|
+
* One variant for each status. `status` narrows the union: only the "error"
|
|
5
|
+
* variant has `error`, and it is never null there.
|
|
6
|
+
*/
|
|
7
|
+
export type MutationReport = MutationOk | MutationNoChange | MutationFailure;
|
|
8
|
+
interface MutationFields {
|
|
9
|
+
readonly tool: WriteToolName;
|
|
10
|
+
/** Committed changes. Empty unless status is "ok", except a failed commit that left files changed. */
|
|
11
|
+
readonly changes: readonly FileChange[];
|
|
12
|
+
/** Display paths of the files a "no-change" result left as they were. Empty otherwise. */
|
|
13
|
+
readonly unchanged: readonly string[];
|
|
14
|
+
readonly notes: readonly Note[];
|
|
15
|
+
}
|
|
16
|
+
export interface MutationOk extends MutationFields {
|
|
17
|
+
readonly status: "ok";
|
|
18
|
+
readonly commit: null;
|
|
19
|
+
}
|
|
20
|
+
/** Nothing to write (already applied, or the same content). Not an error. */
|
|
21
|
+
export interface MutationNoChange extends MutationFields {
|
|
22
|
+
readonly status: "no-change";
|
|
23
|
+
readonly commit: null;
|
|
24
|
+
}
|
|
25
|
+
export interface MutationFailure extends MutationFields {
|
|
26
|
+
readonly status: "error";
|
|
27
|
+
readonly error: WriteError;
|
|
28
|
+
/** Set when an apply_patch commit failed after its first publish step. */
|
|
29
|
+
readonly commit: CommitReport | null;
|
|
30
|
+
}
|
|
31
|
+
/** The report plus the formatter's model-facing content. */
|
|
32
|
+
export type MutationResult = MutationReport & {
|
|
33
|
+
readonly content: readonly ContentPart[];
|
|
34
|
+
};
|
|
35
|
+
/** The error of a failed call. Same shape as the read and shell errors. */
|
|
36
|
+
export type WriteError = ToolError<WriteErrorCode, WritePhase>;
|
|
37
|
+
export type WritePhase = "input" | "resolve" | "stat" | "lock" | "load" | "precondition" | "plan" | "guards" | "authorize" | "encode" | "commit" | "hooks" | "record";
|
|
38
|
+
/** UPPER_SNAKE, like every tool's error codes. The error note's code is the kebab-case form. */
|
|
39
|
+
export type WriteErrorCode = "INVALID_INPUT" | "NOT_FOUND" | "NOT_A_FILE" | "DANGEROUS_PATH" | "OUTSIDE_ALLOWED_ROOTS" | "PERMISSION_DENIED" | "DENIED" | "READ_ONLY" | "NO_SPACE" | "UNSUPPORTED_BACKEND" | "ABORTED" | "IO_ERROR" | "EXTENSION_FAILED" | "LOCK_TIMEOUT" | "NOT_READ" | "STALE" | "EXISTS" | "TOO_LARGE" | "NOT_TEXT" | "NO_MATCH" | "AMBIGUOUS_MATCH" | "MATCH_REFUSED" | "OVERLAP" | "NO_CHANGE" | "GUARD_REFUSED" | "PATCH_PARSE" | "PATCH_VERIFY" | "PARTIAL_COMMIT";
|
|
40
|
+
export interface FileChange {
|
|
41
|
+
readonly kind: "create" | "update" | "delete" | "move";
|
|
42
|
+
/** Display path. For a move, the destination. */
|
|
43
|
+
readonly path: string;
|
|
44
|
+
readonly requestedPath: string;
|
|
45
|
+
readonly resolvedPath: string;
|
|
46
|
+
/** Display path of the move source. null unless kind is "move". */
|
|
47
|
+
readonly movedFrom: string | null;
|
|
48
|
+
readonly before: FileVersion | null;
|
|
49
|
+
readonly after: FileVersion | null;
|
|
50
|
+
readonly linesAdded: number;
|
|
51
|
+
readonly linesRemoved: number;
|
|
52
|
+
/** Unified diff with a/ and b/ headers, cut at limits.maxDiffLines. */
|
|
53
|
+
readonly diff: string;
|
|
54
|
+
readonly diffTruncated: boolean;
|
|
55
|
+
/** One entry for each edit pair or patch hunk that matched. Empty for write. An already applied pair has none. */
|
|
56
|
+
readonly matches: readonly MatchInfo[];
|
|
57
|
+
/** Result lines around each change, for the model text. Empty for write and delete. */
|
|
58
|
+
readonly snippets: readonly Snippet[];
|
|
59
|
+
/** An authorizer replaced the planned content. */
|
|
60
|
+
readonly userModified: boolean;
|
|
61
|
+
/** Resolved paths, outermost first. */
|
|
62
|
+
readonly createdDirectories: readonly string[];
|
|
63
|
+
}
|
|
64
|
+
export interface FileVersion {
|
|
65
|
+
/** null without a digest. */
|
|
66
|
+
readonly contentId: string | null;
|
|
67
|
+
/** null when the backend gave none. */
|
|
68
|
+
readonly version: string | null;
|
|
69
|
+
readonly bytes: number;
|
|
70
|
+
}
|
|
71
|
+
export interface MatchInfo {
|
|
72
|
+
/** Zero-based edit pair or hunk index. */
|
|
73
|
+
readonly index: number;
|
|
74
|
+
readonly matcher: string;
|
|
75
|
+
readonly fuzzy: boolean;
|
|
76
|
+
/** One-based inclusive lines in the file before the change. The first match when count > 1. */
|
|
77
|
+
readonly lines: readonly [number, number];
|
|
78
|
+
/** Replacements made. More than 1 only with replaceAll. */
|
|
79
|
+
readonly count: number;
|
|
80
|
+
/**
|
|
81
|
+
* One-based inclusive lines of each replacement in the file after the
|
|
82
|
+
* change, in order. At most limits.maxListedMatches entries.
|
|
83
|
+
*/
|
|
84
|
+
readonly replaced: readonly (readonly [number, number])[];
|
|
85
|
+
}
|
|
86
|
+
export interface Snippet {
|
|
87
|
+
/** One-based line of lines[0] in the file after the change. */
|
|
88
|
+
readonly startLine: number;
|
|
89
|
+
readonly lines: readonly string[];
|
|
90
|
+
}
|
|
91
|
+
export interface CommitReport {
|
|
92
|
+
/** true when every published step was undone. */
|
|
93
|
+
readonly rolledBack: boolean;
|
|
94
|
+
/** Every file the patch targets, in patch order. */
|
|
95
|
+
readonly files: readonly CommitFileState[];
|
|
96
|
+
}
|
|
97
|
+
export interface CommitFileState {
|
|
98
|
+
readonly path: string;
|
|
99
|
+
readonly state: "unchanged" | "committed" | "restored" | "rollback-failed";
|
|
100
|
+
readonly code?: WriteErrorCode;
|
|
101
|
+
}
|
|
102
|
+
export {};
|
|
File without changes
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export class AbortStop extends Error {
|
|
2
|
+
constructor() {
|
|
3
|
+
super("aborted");
|
|
4
|
+
}
|
|
5
|
+
}
|
|
6
|
+
export async function raceAbort(start, signal) {
|
|
7
|
+
if (!signal)
|
|
8
|
+
return start();
|
|
9
|
+
if (signal.aborted)
|
|
10
|
+
throw new AbortStop();
|
|
11
|
+
let removeListener;
|
|
12
|
+
const aborted = new Promise((_resolve, reject) => {
|
|
13
|
+
const onAbort = () => reject(new AbortStop());
|
|
14
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
15
|
+
removeListener = () => signal.removeEventListener("abort", onAbort);
|
|
16
|
+
});
|
|
17
|
+
try {
|
|
18
|
+
return await Promise.race([(async () => start())(), aborted]);
|
|
19
|
+
}
|
|
20
|
+
finally {
|
|
21
|
+
removeListener?.();
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { PlannedChange, WriteAuthorizeTarget } from "../contract/extensions.ts";
|
|
2
|
+
import type { ResolvedTarget } from "./planned.ts";
|
|
3
|
+
import type { MutationScope } from "./scope.ts";
|
|
4
|
+
export interface AccessRequest {
|
|
5
|
+
readonly action: WriteAuthorizeTarget["action"];
|
|
6
|
+
readonly target: ResolvedTarget;
|
|
7
|
+
}
|
|
8
|
+
export declare function authorizeAccess<THost>(scope: MutationScope<THost>, requests: readonly AccessRequest[]): Promise<void>;
|
|
9
|
+
export declare function authorizeChanges<THost>(scope: MutationScope<THost>, plan: readonly PlannedChange[]): Promise<(string | null)[]>;
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { AbortStop } from "./abort.js";
|
|
2
|
+
import { extensionId } from "./extension-error.js";
|
|
3
|
+
import { isRecord } from "./input.js";
|
|
4
|
+
import { errorNote, hostErrorNote, isNote, isNoteList } from "./outcomes.js";
|
|
5
|
+
export async function authorizeAccess(scope, requests) {
|
|
6
|
+
if (scope.deps.authorize === null)
|
|
7
|
+
return;
|
|
8
|
+
scope.enter("authorize");
|
|
9
|
+
for (const { action, target } of requests) {
|
|
10
|
+
const content = await runAuthorizer(scope, {
|
|
11
|
+
action,
|
|
12
|
+
tool: scope.tool,
|
|
13
|
+
requestedPath: target.requestedPath,
|
|
14
|
+
resolvedPath: target.resolvedPath,
|
|
15
|
+
displayPath: target.displayPath,
|
|
16
|
+
change: null,
|
|
17
|
+
plan: [],
|
|
18
|
+
});
|
|
19
|
+
if (content !== null)
|
|
20
|
+
throw malformed(scope);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
export async function authorizeChanges(scope, plan) {
|
|
24
|
+
const contents = plan.map(() => null);
|
|
25
|
+
if (scope.deps.authorize === null)
|
|
26
|
+
return contents;
|
|
27
|
+
scope.enter("authorize");
|
|
28
|
+
const frozen = Object.freeze([...plan]);
|
|
29
|
+
for (const [index, change] of frozen.entries()) {
|
|
30
|
+
const content = await runAuthorizer(scope, {
|
|
31
|
+
action: change.kind,
|
|
32
|
+
tool: scope.tool,
|
|
33
|
+
requestedPath: change.requestedPath,
|
|
34
|
+
resolvedPath: change.resolvedPath,
|
|
35
|
+
displayPath: change.displayPath,
|
|
36
|
+
change,
|
|
37
|
+
plan: frozen,
|
|
38
|
+
});
|
|
39
|
+
if (content !== null && scope.tool === "apply_patch")
|
|
40
|
+
throw malformed(scope);
|
|
41
|
+
contents[index] = content;
|
|
42
|
+
}
|
|
43
|
+
return contents;
|
|
44
|
+
}
|
|
45
|
+
async function runAuthorizer(scope, target) {
|
|
46
|
+
const authorizer = scope.deps.authorize;
|
|
47
|
+
if (authorizer === null)
|
|
48
|
+
return null;
|
|
49
|
+
scope.checkAbort();
|
|
50
|
+
const frozen = Object.freeze(target);
|
|
51
|
+
let decision;
|
|
52
|
+
try {
|
|
53
|
+
decision = await scope.race(() => authorizer.authorize(frozen, scope.hookContext()));
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
if (error instanceof AbortStop)
|
|
57
|
+
throw error;
|
|
58
|
+
throw scope.extensionFailure("authorize", extensionId(authorizer, error));
|
|
59
|
+
}
|
|
60
|
+
if (!isRecord(decision))
|
|
61
|
+
throw malformed(scope);
|
|
62
|
+
if (decision.allow === true) {
|
|
63
|
+
const { notes, content } = decision;
|
|
64
|
+
if (notes !== undefined && !isNoteList(notes))
|
|
65
|
+
throw malformed(scope);
|
|
66
|
+
if (content !== undefined && typeof content !== "string")
|
|
67
|
+
throw malformed(scope);
|
|
68
|
+
if (notes !== undefined)
|
|
69
|
+
scope.notes.push(...notes);
|
|
70
|
+
return content ?? null;
|
|
71
|
+
}
|
|
72
|
+
if (decision.allow !== false)
|
|
73
|
+
throw malformed(scope);
|
|
74
|
+
const { note } = decision;
|
|
75
|
+
if (note !== undefined && !isNote(note))
|
|
76
|
+
throw malformed(scope);
|
|
77
|
+
throw scope.stopWith(deniedNote(scope, target, note));
|
|
78
|
+
}
|
|
79
|
+
function deniedNote(scope, target, note) {
|
|
80
|
+
if (note !== undefined)
|
|
81
|
+
return hostErrorNote("DENIED", note);
|
|
82
|
+
const message = scope.deps.messages.denied({ path: target.requestedPath, detail: null });
|
|
83
|
+
return errorNote("DENIED", message);
|
|
84
|
+
}
|
|
85
|
+
function malformed(scope) {
|
|
86
|
+
return scope.extensionFailure("authorize", extensionId(scope.deps.authorize));
|
|
87
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export function hashBytes(digest, bytes) {
|
|
2
|
+
const stream = digest.create();
|
|
3
|
+
stream.update(bytes);
|
|
4
|
+
return stream.digest();
|
|
5
|
+
}
|
|
6
|
+
export function sameBytes(a, b) {
|
|
7
|
+
if (a.byteLength !== b.byteLength)
|
|
8
|
+
return false;
|
|
9
|
+
for (let index = 0; index < a.byteLength; index += 1) {
|
|
10
|
+
if (a[index] !== b[index])
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
return true;
|
|
14
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { MutatedFile, WritableFileSystem } from "@better-fs-tools/fs";
|
|
2
|
+
import type { FileChange } from "../contract/result.ts";
|
|
3
|
+
import type { PatchChange } from "./plan-patch.ts";
|
|
4
|
+
import type { Planned, ResolvedTarget } from "./planned.ts";
|
|
5
|
+
import type { MutationScope } from "./scope.ts";
|
|
6
|
+
export interface PatchStep {
|
|
7
|
+
readonly change: PatchChange;
|
|
8
|
+
readonly bytes: Uint8Array | null;
|
|
9
|
+
readonly mode: number | null;
|
|
10
|
+
}
|
|
11
|
+
export interface PublishedStep {
|
|
12
|
+
readonly step: PatchStep;
|
|
13
|
+
readonly file: MutatedFile | null;
|
|
14
|
+
}
|
|
15
|
+
export interface PatchFile {
|
|
16
|
+
readonly path: string;
|
|
17
|
+
readonly resolvedPath: string;
|
|
18
|
+
}
|
|
19
|
+
export declare function commitPatch<THost>(scope: MutationScope<THost>, fs: WritableFileSystem, steps: readonly PatchStep[], files: readonly PatchFile[]): Promise<PublishedStep[]>;
|
|
20
|
+
export declare function deleteChange(planned: Planned, target: ResolvedTarget): FileChange;
|