@better-fs-tools/write 0.2.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +49 -37
- package/dist/bundle/create-fs-tools.d.ts +59 -19
- package/dist/bundle/create-fs-tools.js +56 -22
- package/dist/bundle/definitions.d.ts +51 -0
- package/dist/bundle/definitions.js +42 -0
- package/dist/contract/deps.d.ts +4 -4
- package/dist/contract/messages.d.ts +24 -2
- package/dist/core/create-tools.d.ts +9 -9
- package/dist/core/create-tools.js +11 -11
- package/dist/core/deps.d.ts +4 -4
- package/dist/core/deps.js +6 -6
- package/dist/core/messages.js +47 -5
- package/dist/core/patch-hunks.d.ts +7 -0
- package/dist/core/patch-hunks.js +88 -18
- package/dist/core/plan-edit.js +7 -2
- package/dist/core/plan-patch.js +9 -2
- package/dist/core/record.js +1 -0
- package/dist/index.d.ts +3 -3
- package/dist/matchers/keep-bytes.d.ts +31 -0
- package/dist/matchers/keep-bytes.js +76 -0
- package/dist/matchers/line-trimmed.d.ts +4 -1
- package/dist/matchers/line-trimmed.js +9 -1
- package/dist/matchers/normalized.d.ts +2 -1
- package/dist/matchers/normalized.js +7 -1
- package/dist/signature/definition.d.ts +41 -0
- package/dist/signature/definition.js +66 -0
- package/dist/signature/edit.d.ts +4 -5
- package/dist/signature/edit.js +5 -13
- package/dist/signature/index.d.ts +6 -2
- package/dist/signature/index.js +4 -2
- package/dist/signature/patch.d.ts +4 -1
- package/dist/signature/patch.js +4 -1
- package/dist/signature/write.d.ts +1 -6
- package/dist/signature/write.js +1 -8
- package/docs/architecture.md +8 -6
- package/docs/hosts.md +133 -55
- package/docs/result-schema.md +1 -0
- package/package.json +5 -4
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
const CR = "\r";
|
|
2
|
+
/**
|
|
3
|
+
* Builds the new lines for one loose hit so that text the model did not
|
|
4
|
+
* change keeps the file's bytes.
|
|
5
|
+
*
|
|
6
|
+
* - `file` holds the file's own text of the matched lines, `old` the model's
|
|
7
|
+
* text of the same lines, and `next` the model's new lines.
|
|
8
|
+
* - With `keepUnchanged`, the leading and trailing lines where `old` and
|
|
9
|
+
* `next` agree are written from `file`.
|
|
10
|
+
* - Each other new line takes the CR of the file line it replaces, so a
|
|
11
|
+
* mixed-EOL file keeps its CRLF lines. The last new line pairs with the
|
|
12
|
+
* last replaced line when no kept line follows it.
|
|
13
|
+
*/
|
|
14
|
+
export function keepFileLines(file, old, next, keepUnchanged) {
|
|
15
|
+
const room = Math.min(old.length, next.length);
|
|
16
|
+
let head = 0;
|
|
17
|
+
if (keepUnchanged) {
|
|
18
|
+
while (head < room && old[head] === next[head])
|
|
19
|
+
head += 1;
|
|
20
|
+
}
|
|
21
|
+
let tail = 0;
|
|
22
|
+
if (keepUnchanged) {
|
|
23
|
+
while (head + tail < room && old.at(-1 - tail) === next.at(-1 - tail))
|
|
24
|
+
tail += 1;
|
|
25
|
+
}
|
|
26
|
+
const replaced = file.slice(head, file.length - tail);
|
|
27
|
+
const written = next.slice(head, next.length - tail);
|
|
28
|
+
const middle = written.map((line, at) => {
|
|
29
|
+
if (replaced.length === 0 || line.endsWith(CR))
|
|
30
|
+
return line;
|
|
31
|
+
const lastWithoutKept = tail === 0 && at === written.length - 1;
|
|
32
|
+
const pair = lastWithoutKept ? replaced.length - 1 : Math.min(at, replaced.length - 1);
|
|
33
|
+
return replaced[pair].endsWith(CR) ? `${line}${CR}` : line;
|
|
34
|
+
});
|
|
35
|
+
return [...file.slice(0, head), ...middle, ...file.slice(file.length - tail)];
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* The `Matcher.adapt` rule for a loose hit in text mode. When the old text and
|
|
39
|
+
* the new text are the same, the edit exists only to change the characters the
|
|
40
|
+
* matcher folded, so the new text is written as given, with the file's CRs.
|
|
41
|
+
* Otherwise the unchanged leading and trailing lines keep the file's bytes.
|
|
42
|
+
* `lined` says whether line N of the region matched line N of the old text;
|
|
43
|
+
* when it did not, the new text is written as given.
|
|
44
|
+
*/
|
|
45
|
+
export function adaptKeepingBytes(newText, region, needle, lined) {
|
|
46
|
+
const file = region.split("\n");
|
|
47
|
+
const old = needle.split("\n");
|
|
48
|
+
if (file.length !== old.length || !lined(file, old))
|
|
49
|
+
return newText;
|
|
50
|
+
return keepFileLines(file, old, newText.split("\n"), newText !== needle).join("\n");
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Compares what the tool wrote for one loose hit with the model's new lines,
|
|
54
|
+
* position by position. `file` holds the file's own lines of the hit. A
|
|
55
|
+
* written line that differs from the model's line only by a final CR kept the
|
|
56
|
+
* file's line end. One that is a line of `file` kept the file's characters.
|
|
57
|
+
* Any other difference came from the matcher.
|
|
58
|
+
*/
|
|
59
|
+
export function describeWrite(file, given, written) {
|
|
60
|
+
let keptLines = 0;
|
|
61
|
+
let keptLineEnds = 0;
|
|
62
|
+
let adaptedLines = 0;
|
|
63
|
+
for (const [at, line] of written.entries()) {
|
|
64
|
+
const sent = given[at];
|
|
65
|
+
if (line === sent)
|
|
66
|
+
continue;
|
|
67
|
+
if (sent !== undefined && line === `${sent}${CR}`)
|
|
68
|
+
keptLineEnds += 1;
|
|
69
|
+
else if (sent !== undefined && file.includes(line))
|
|
70
|
+
keptLines += 1;
|
|
71
|
+
else
|
|
72
|
+
adaptedLines += 1;
|
|
73
|
+
}
|
|
74
|
+
adaptedLines += Math.max(0, given.length - written.length);
|
|
75
|
+
return { keptLines, keptLineEnds, adaptedLines };
|
|
76
|
+
}
|
|
@@ -1,3 +1,6 @@
|
|
|
1
1
|
import type { Matcher } from "../contract/matcher.ts";
|
|
2
|
-
/**
|
|
2
|
+
/**
|
|
3
|
+
* Matches whole lines, ignoring leading and trailing whitespace on each line.
|
|
4
|
+
* Lines that the new text leaves as they were keep the file's bytes.
|
|
5
|
+
*/
|
|
3
6
|
export declare function lineTrimmedMatcher(): Matcher;
|
|
@@ -1,5 +1,9 @@
|
|
|
1
|
+
import { adaptKeepingBytes } from "./keep-bytes.js";
|
|
1
2
|
import { LineTable, memoizeLast, needleLines } from "./lines.js";
|
|
2
|
-
/**
|
|
3
|
+
/**
|
|
4
|
+
* Matches whole lines, ignoring leading and trailing whitespace on each line.
|
|
5
|
+
* Lines that the new text leaves as they were keep the file's bytes.
|
|
6
|
+
*/
|
|
3
7
|
export function lineTrimmedMatcher() {
|
|
4
8
|
const table = memoizeLast((text) => new LineTable(text));
|
|
5
9
|
return Object.freeze({
|
|
@@ -28,5 +32,9 @@ export function lineTrimmedMatcher() {
|
|
|
28
32
|
}
|
|
29
33
|
return ranges;
|
|
30
34
|
},
|
|
35
|
+
adapt: (newText, { haystack, needle, range }) => adaptKeepingBytes(newText, haystack.slice(range.start, range.end), needle, sameTrimmed),
|
|
31
36
|
});
|
|
32
37
|
}
|
|
38
|
+
function sameTrimmed(file, old) {
|
|
39
|
+
return file.every((line, at) => line.trim() === (old[at] ?? "").trim());
|
|
40
|
+
}
|
|
@@ -2,6 +2,7 @@ import type { Matcher } from "../contract/matcher.ts";
|
|
|
2
2
|
/**
|
|
3
3
|
* Matches while ignoring trailing whitespace, Unicode normalization, curly
|
|
4
4
|
* quotes, dashes, and special spaces. A match that starts or ends inside one
|
|
5
|
-
* folded character is refused.
|
|
5
|
+
* folded character is refused. Lines of the hit that the new text leaves as
|
|
6
|
+
* they were keep the file's bytes, and replaced lines keep the file's CRs.
|
|
6
7
|
*/
|
|
7
8
|
export declare function normalizedMatcher(): Matcher;
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { FIRST, LAST, fold } from "./fold.js";
|
|
2
|
+
import { adaptKeepingBytes } from "./keep-bytes.js";
|
|
2
3
|
import { memoizeLast } from "./lines.js";
|
|
3
4
|
const LF = 10;
|
|
4
5
|
/**
|
|
5
6
|
* Matches while ignoring trailing whitespace, Unicode normalization, curly
|
|
6
7
|
* quotes, dashes, and special spaces. A match that starts or ends inside one
|
|
7
|
-
* folded character is refused.
|
|
8
|
+
* folded character is refused. Lines of the hit that the new text leaves as
|
|
9
|
+
* they were keep the file's bytes, and replaced lines keep the file's CRs.
|
|
8
10
|
*/
|
|
9
11
|
export function normalizedMatcher() {
|
|
10
12
|
const folded = memoizeLast(fold);
|
|
@@ -13,8 +15,12 @@ export function normalizedMatcher() {
|
|
|
13
15
|
fuzzy: true,
|
|
14
16
|
describe: "trailing whitespace, Unicode normalization, curly quotes, dashes, and special spaces",
|
|
15
17
|
find: (haystack, needle, ctx) => findFolded(haystack, folded(haystack), fold(needle), ctx),
|
|
18
|
+
adapt: (newText, { haystack, needle, range }) => adaptKeepingBytes(newText, haystack.slice(range.start, range.end), needle, sameFolded),
|
|
16
19
|
});
|
|
17
20
|
}
|
|
21
|
+
function sameFolded(file, old) {
|
|
22
|
+
return file.every((line, at) => fold(line).text === fold(old[at] ?? "").text);
|
|
23
|
+
}
|
|
18
24
|
function findFolded(haystack, hay, needle, ctx) {
|
|
19
25
|
const ranges = [];
|
|
20
26
|
const text = hay.text;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { StateNeedsDigest, ToolDefinition } from "@better-fs-tools/read";
|
|
2
|
+
import type { ApplyPatchToolOptions, EditToolOptions, WriteToolOptions } from "../contract/deps.ts";
|
|
3
|
+
import type { ApplyPatchInput, EditInput, WriteInput } from "../contract/input.ts";
|
|
4
|
+
import type { WriteCanonicalParam } from "../contract/messages.ts";
|
|
5
|
+
import type { MutationResult } from "../contract/result.ts";
|
|
6
|
+
import type { EditSignature, MutationSignature, PatchSignature, WriteSignature } from "./contract.ts";
|
|
7
|
+
/** An edit, write, or apply_patch tool bound to its signature. */
|
|
8
|
+
export interface MutationToolDefinition<TInput, THost = undefined> extends ToolDefinition<TInput, MutationResult, THost, WriteCanonicalParam> {
|
|
9
|
+
readonly signature: MutationSignature<TInput>;
|
|
10
|
+
}
|
|
11
|
+
export type EditToolDefinition<THost = undefined> = MutationToolDefinition<EditInput, THost>;
|
|
12
|
+
export type WriteToolDefinition<THost = undefined> = MutationToolDefinition<WriteInput, THost>;
|
|
13
|
+
export type ApplyPatchToolDefinition<THost = undefined> = MutationToolDefinition<ApplyPatchInput, THost>;
|
|
14
|
+
/** The edit tool's options, plus the signature the model sees. */
|
|
15
|
+
export type EditToolDefinitionOptions<THost = undefined> = EditToolOptions<THost> & StateNeedsDigest & {
|
|
16
|
+
/** Default defaultEditSignature({ matchers }), so the description names the matchers. */
|
|
17
|
+
readonly signature?: EditSignature;
|
|
18
|
+
};
|
|
19
|
+
/** The write tool's options, plus the signature the model sees. */
|
|
20
|
+
export type WriteToolDefinitionOptions<THost = undefined> = WriteToolOptions<THost> & StateNeedsDigest & {
|
|
21
|
+
/** Default defaultWriteSignature(). */
|
|
22
|
+
readonly signature?: WriteSignature;
|
|
23
|
+
};
|
|
24
|
+
/** The apply_patch tool's options, plus the signature the model sees. */
|
|
25
|
+
export type ApplyPatchToolDefinitionOptions<THost = undefined> = ApplyPatchToolOptions<THost> & StateNeedsDigest & {
|
|
26
|
+
/** Default defaultPatchSignature(). */
|
|
27
|
+
readonly signature?: PatchSignature;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Creates the edit tool and binds it to a signature. The signature's `param`
|
|
31
|
+
* message goes under `options.messages`, so a host message still wins. Throws
|
|
32
|
+
* TypeError where createEditTool() does.
|
|
33
|
+
*/
|
|
34
|
+
export declare function editToolDefinition<THost = undefined>(options: EditToolDefinitionOptions<THost>): EditToolDefinition<THost>;
|
|
35
|
+
/** Creates the write tool and binds it to a signature, as editToolDefinition(). */
|
|
36
|
+
export declare function writeToolDefinition<THost = undefined>(options: WriteToolDefinitionOptions<THost>): WriteToolDefinition<THost>;
|
|
37
|
+
/**
|
|
38
|
+
* Creates the apply_patch tool and binds it to a signature, as
|
|
39
|
+
* editToolDefinition(). A grammar on the signature is copied to the definition.
|
|
40
|
+
*/
|
|
41
|
+
export declare function applyPatchToolDefinition<THost = undefined>(options: ApplyPatchToolDefinitionOptions<THost>): ApplyPatchToolDefinition<THost>;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { createApplyPatchTool, createEditTool, createWriteTool } from "../core/create-tools.js";
|
|
2
|
+
import { parseApplyPatchInput, parseEditInput, parseWriteInput } from "../core/input.js";
|
|
3
|
+
import { resolveWriteLimits } from "../core/limits.js";
|
|
4
|
+
import { defaultEditSignature } from "./edit.js";
|
|
5
|
+
import { writeSignatureMessages } from "./messages.js";
|
|
6
|
+
import { defaultPatchSignature } from "./patch.js";
|
|
7
|
+
import { defaultWriteSignature } from "./write.js";
|
|
8
|
+
/**
|
|
9
|
+
* Creates the edit tool and binds it to a signature. The signature's `param`
|
|
10
|
+
* message goes under `options.messages`, so a host message still wins. Throws
|
|
11
|
+
* TypeError where createEditTool() does.
|
|
12
|
+
*/
|
|
13
|
+
export function editToolDefinition(options) {
|
|
14
|
+
checkOptions(options, "editToolDefinition");
|
|
15
|
+
const { signature: given, ...rest } = options;
|
|
16
|
+
const signature = given ?? defaultEditSignature(rest.matchers === undefined ? {} : { matchers: rest.matchers });
|
|
17
|
+
const edit = createEditTool({
|
|
18
|
+
...rest,
|
|
19
|
+
messages: { ...writeSignatureMessages(signature), ...rest.messages },
|
|
20
|
+
});
|
|
21
|
+
return bindMutationTool(signature, edit, parseEditInput, rest.limits);
|
|
22
|
+
}
|
|
23
|
+
/** Creates the write tool and binds it to a signature, as editToolDefinition(). */
|
|
24
|
+
export function writeToolDefinition(options) {
|
|
25
|
+
checkOptions(options, "writeToolDefinition");
|
|
26
|
+
const { signature = defaultWriteSignature(), ...rest } = options;
|
|
27
|
+
const write = createWriteTool({
|
|
28
|
+
...rest,
|
|
29
|
+
messages: { ...writeSignatureMessages(signature), ...rest.messages },
|
|
30
|
+
});
|
|
31
|
+
return bindMutationTool(signature, write, parseWriteInput, rest.limits);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Creates the apply_patch tool and binds it to a signature, as
|
|
35
|
+
* editToolDefinition(). A grammar on the signature is copied to the definition.
|
|
36
|
+
*/
|
|
37
|
+
export function applyPatchToolDefinition(options) {
|
|
38
|
+
checkOptions(options, "applyPatchToolDefinition");
|
|
39
|
+
const { signature = defaultPatchSignature(), ...rest } = options;
|
|
40
|
+
const applyPatch = createApplyPatchTool({
|
|
41
|
+
...rest,
|
|
42
|
+
messages: { ...writeSignatureMessages(signature), ...rest.messages },
|
|
43
|
+
});
|
|
44
|
+
return bindMutationTool(signature, applyPatch, parseApplyPatchInput, rest.limits);
|
|
45
|
+
}
|
|
46
|
+
function bindMutationTool(signature, tool, parse, limitOverrides) {
|
|
47
|
+
const limits = resolveWriteLimits(limitOverrides);
|
|
48
|
+
return Object.freeze({
|
|
49
|
+
name: signature.name,
|
|
50
|
+
description: signature.description,
|
|
51
|
+
inputSchema: signature.schema,
|
|
52
|
+
...(signature.grammar === undefined ? {} : { grammar: signature.grammar }),
|
|
53
|
+
signature,
|
|
54
|
+
validate(modelInput) {
|
|
55
|
+
const input = signature.toInput(modelInput);
|
|
56
|
+
parse(input, limits);
|
|
57
|
+
return input;
|
|
58
|
+
},
|
|
59
|
+
execute: async (modelInput, ctx) => tool(signature.toInput(modelInput), ctx),
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
function checkOptions(options, label) {
|
|
63
|
+
if (options === null || typeof options !== "object" || Array.isArray(options)) {
|
|
64
|
+
throw new TypeError(`${label} options must be an object`);
|
|
65
|
+
}
|
|
66
|
+
}
|
package/dist/signature/edit.d.ts
CHANGED
|
@@ -4,13 +4,12 @@ import type { EditSignature } from "./contract.ts";
|
|
|
4
4
|
type MatcherOption = {
|
|
5
5
|
readonly matchers?: readonly Matcher[];
|
|
6
6
|
};
|
|
7
|
-
/** edit({ path, old_string, new_string, replace_all? }). Name "edit". */
|
|
8
|
-
export declare function defaultEditSignature(options?: SignatureDocs<"path" | "old_string" | "new_string" | "replace_all"> & MatcherOption): EditSignature;
|
|
9
7
|
/**
|
|
10
|
-
* edit({
|
|
11
|
-
* The
|
|
8
|
+
* edit({ path, old_string, new_string, replace_all? }). Name "edit". `names`
|
|
9
|
+
* renames any parameter. The description names the fuzzy matchers in
|
|
10
|
+
* `matchers`, which defaults to defaultEditMatchers().
|
|
12
11
|
*/
|
|
13
|
-
export declare function
|
|
12
|
+
export declare function defaultEditSignature(options?: SignatureDocs<"path" | "old_string" | "new_string" | "replace_all"> & MatcherOption): EditSignature;
|
|
14
13
|
/**
|
|
15
14
|
* edit({ path, edits: [{ oldText, newText }] }). Pi and Cloudflare Computer.
|
|
16
15
|
* Has no replaceAll parameter, so no message suggests one.
|
package/dist/signature/edit.js
CHANGED
|
@@ -5,7 +5,11 @@ const PATH_TEXT = "Path of the file to edit, relative to the working directory o
|
|
|
5
5
|
const READ_FIRST = "Read the file with the read tool before you edit it.";
|
|
6
6
|
const NO_PREFIX = "Copy old text from the file without the line-number prefix the read tool adds.";
|
|
7
7
|
const USE_WRITE = "To create a file or replace all of its content, use the write tool.";
|
|
8
|
-
/**
|
|
8
|
+
/**
|
|
9
|
+
* edit({ path, old_string, new_string, replace_all? }). Name "edit". `names`
|
|
10
|
+
* renames any parameter. The description names the fuzzy matchers in
|
|
11
|
+
* `matchers`, which defaults to defaultEditMatchers().
|
|
12
|
+
*/
|
|
9
13
|
export function defaultEditSignature(options = {}) {
|
|
10
14
|
return singleEditSignature(options, "defaultEditSignature", {
|
|
11
15
|
path: "path",
|
|
@@ -14,18 +18,6 @@ export function defaultEditSignature(options = {}) {
|
|
|
14
18
|
all: "replace_all",
|
|
15
19
|
});
|
|
16
20
|
}
|
|
17
|
-
/**
|
|
18
|
-
* edit({ filePath, oldString, newString, replaceAll? }). OpenCode and Kilo Code.
|
|
19
|
-
* The same as defaultEditSignature with camel-case names.
|
|
20
|
-
*/
|
|
21
|
-
export function camelCaseEditSignature(options = {}) {
|
|
22
|
-
return singleEditSignature(options, "camelCaseEditSignature", {
|
|
23
|
-
path: "filePath",
|
|
24
|
-
old: "oldString",
|
|
25
|
-
new: "newString",
|
|
26
|
-
all: "replaceAll",
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
21
|
function singleEditSignature(options, label, presetNames) {
|
|
30
22
|
const checked = checkDocs(options, label, [
|
|
31
23
|
presetNames.path,
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
/** The `./signature` subpath: model-facing names, schemas, and descriptions for the three tools. */
|
|
2
2
|
export type { EditSignature, MutationSignature, PatchSignature, WriteSignature, } from "./contract.ts";
|
|
3
|
-
export {
|
|
3
|
+
export { fsToolDefinitions } from "../bundle/definitions.ts";
|
|
4
|
+
export type { FsToolDefinitions, FsToolDefinitionsOptions } from "../bundle/definitions.ts";
|
|
5
|
+
export { applyPatchToolDefinition, editToolDefinition, writeToolDefinition } from "./definition.ts";
|
|
6
|
+
export type { ApplyPatchToolDefinition, ApplyPatchToolDefinitionOptions, EditToolDefinition, EditToolDefinitionOptions, MutationToolDefinition, WriteToolDefinition, WriteToolDefinitionOptions, } from "./definition.ts";
|
|
7
|
+
export { defaultEditSignature, multiEditSignature } from "./edit.ts";
|
|
4
8
|
export { writeSignatureMessages } from "./messages.ts";
|
|
5
9
|
export { defaultPatchSignature, freeformPatchSignature } from "./patch.ts";
|
|
6
|
-
export { defaultWriteSignature
|
|
10
|
+
export { defaultWriteSignature } from "./write.ts";
|
package/dist/signature/index.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { fsToolDefinitions } from "../bundle/definitions.js";
|
|
2
|
+
export { applyPatchToolDefinition, editToolDefinition, writeToolDefinition } from "./definition.js";
|
|
3
|
+
export { defaultEditSignature, multiEditSignature } from "./edit.js";
|
|
2
4
|
export { writeSignatureMessages } from "./messages.js";
|
|
3
5
|
export { defaultPatchSignature, freeformPatchSignature } from "./patch.js";
|
|
4
|
-
export { defaultWriteSignature
|
|
6
|
+
export { defaultWriteSignature } from "./write.js";
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import type { SignatureDocs } from "@better-fs-tools/read";
|
|
2
2
|
import type { PatchSignature } from "./contract.ts";
|
|
3
|
-
/**
|
|
3
|
+
/**
|
|
4
|
+
* apply_patch({ patch }) as JSON, with a short example in the description. For
|
|
5
|
+
* a host with grammar tools, use freeformPatchSignature().
|
|
6
|
+
*/
|
|
4
7
|
export declare function defaultPatchSignature(options?: SignatureDocs<"patch">): PatchSignature;
|
|
5
8
|
/** defaultPatchSignature plus the Codex Lark grammar, for hosts with grammar tools. */
|
|
6
9
|
export declare function freeformPatchSignature(options?: SignatureDocs<"patch">): PatchSignature;
|
package/dist/signature/patch.js
CHANGED
|
@@ -21,7 +21,10 @@ const patchDescription = (patch) => "Apply a patch that adds, updates, moves, or
|
|
|
21
21
|
"`*** Move to: <path>` after an Update line to rename the file. Every line of an Add " +
|
|
22
22
|
"starts with `+`. Every hunk is checked before any file changes: when one fails, no file " +
|
|
23
23
|
"changes. Read each file you update or delete with the read tool first.";
|
|
24
|
-
/**
|
|
24
|
+
/**
|
|
25
|
+
* apply_patch({ patch }) as JSON, with a short example in the description. For
|
|
26
|
+
* a host with grammar tools, use freeformPatchSignature().
|
|
27
|
+
*/
|
|
25
28
|
export function defaultPatchSignature(options = {}) {
|
|
26
29
|
return patchSignature(options, "defaultPatchSignature");
|
|
27
30
|
}
|
|
@@ -1,9 +1,4 @@
|
|
|
1
1
|
import type { SignatureDocs } from "@better-fs-tools/read";
|
|
2
2
|
import type { WriteSignature } from "./contract.ts";
|
|
3
|
-
/** write({ path, content }). */
|
|
3
|
+
/** write({ path, content }). Name "write". `names` renames either parameter. */
|
|
4
4
|
export declare function defaultWriteSignature(options?: SignatureDocs<"path" | "content">): WriteSignature;
|
|
5
|
-
/**
|
|
6
|
-
* write_file({ file_path, content }). Claude Code. The same as
|
|
7
|
-
* defaultWriteSignature with another tool name and path name.
|
|
8
|
-
*/
|
|
9
|
-
export declare function snakeCaseWriteSignature(options?: SignatureDocs<"file_path" | "content">): WriteSignature;
|
package/dist/signature/write.js
CHANGED
|
@@ -1,15 +1,8 @@
|
|
|
1
1
|
import { checkDocs, deepFreeze, objectSchema, pathSchema, readObject, readPath, readString, stringSchema, } from "./schema.js";
|
|
2
|
-
/** write({ path, content }). */
|
|
2
|
+
/** write({ path, content }). Name "write". `names` renames either parameter. */
|
|
3
3
|
export function defaultWriteSignature(options = {}) {
|
|
4
4
|
return writeSignature(options, "defaultWriteSignature", "write", "path");
|
|
5
5
|
}
|
|
6
|
-
/**
|
|
7
|
-
* write_file({ file_path, content }). Claude Code. The same as
|
|
8
|
-
* defaultWriteSignature with another tool name and path name.
|
|
9
|
-
*/
|
|
10
|
-
export function snakeCaseWriteSignature(options = {}) {
|
|
11
|
-
return writeSignature(options, "snakeCaseWriteSignature", "write_file", "file_path");
|
|
12
|
-
}
|
|
13
6
|
function writeSignature(options, label, name, presetPathName) {
|
|
14
7
|
const checked = checkDocs(options, label, [presetPathName, "content"]);
|
|
15
8
|
const { describe } = checked;
|
package/docs/architecture.md
CHANGED
|
@@ -33,7 +33,7 @@ formatter.format(report, { mode: "model", call, ... }) ─▶ content
|
|
|
33
33
|
MutationResult
|
|
34
34
|
```
|
|
35
35
|
|
|
36
|
-
| Stage |
|
|
36
|
+
| Stage | Option | What happens |
|
|
37
37
|
| ------------ | --------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
38
38
|
| Input | `limits` | `parseEditInput`, `parseWriteInput`, or `parseApplyPatchInput`. Unknown keys and blank paths are refused. `apply_patch` also parses the patch here |
|
|
39
39
|
| Filesystem | `fs` | A `WritableFileSystem`, or a factory of the call context. The factory runs once for each call |
|
|
@@ -53,14 +53,14 @@ MutationResult
|
|
|
53
53
|
| Record | `state`, `digest`, `clock` | Stores a record for each committed file. A store failure does not fail the call |
|
|
54
54
|
| Format | `formatter`, `messages` | Turns the report into content parts |
|
|
55
55
|
|
|
56
|
-
Some stages cannot be removed by any
|
|
56
|
+
Some stages cannot be removed by any option: input validation, the type check, the order of the access authorize before any content read, the lock, the load cap, the codec round trip, the precondition check when a store is set, the literal splice, the check of every change before the first commit, and the record after a commit. They are the reason the package exists. Everything else is an option.
|
|
57
57
|
|
|
58
58
|
## Source layout
|
|
59
59
|
|
|
60
60
|
| Path | Role |
|
|
61
61
|
| ------------------------------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------- |
|
|
62
62
|
| `src/contract/` | The public types, one file for each area. No runtime code |
|
|
63
|
-
| `src/core/create-tools.ts`, `core/deps.ts` | The three factories. They check and resolve the
|
|
63
|
+
| `src/core/create-tools.ts`, `core/deps.ts` | The three factories. They check and resolve the options once, synchronously |
|
|
64
64
|
| `src/core/pipeline.ts` | The stage order for `edit` and `write`. `apply_patch` has its own order in `core/patch-pipeline.ts` |
|
|
65
65
|
| `src/core/scope.ts` | Per-call state: the call object, the phase, `fs(call)`, `state(call)`, notes, abort checks, and failures |
|
|
66
66
|
| `src/core/resolve.ts`, `target.ts`, `lock.ts`, `load.ts`, `precondition.ts` | Resolve, stat, lock, load, and the precondition table |
|
|
@@ -89,7 +89,7 @@ An allow decision in the change stage may carry `content` for `edit` and `write`
|
|
|
89
89
|
|
|
90
90
|
The lock key is the real path, and only `stat` knows it. So the core stats, authorizes access, locks every real path of the call in sorted order, and stats again. The second stat decides: a file that appeared or vanished between the two stats is judged by it, and a real path that changed gives `STALE`.
|
|
91
91
|
|
|
92
|
-
`memoryLocks()` orders writers inside one process. The read tool takes no lock: it detects a change with `verify()`, and each write replaces the file in one step on a backend that can. `createFsTools()`, and the bundles that wrap it (`
|
|
92
|
+
`memoryLocks()` orders writers inside one process. The read tool takes no lock: it detects a change with `verify()`, and each write replaces the file in one step on a backend that can. `createFsTools()`, and the bundles that wrap it (`createPiFsTools()` and `createAiSdkFsTools()`), share one lock manager across the three write tools.
|
|
93
93
|
|
|
94
94
|
## Load
|
|
95
95
|
|
|
@@ -129,6 +129,8 @@ The commit carries a precondition: `absent` for a create, and the loaded `versio
|
|
|
129
129
|
The core owns the rules on every hit:
|
|
130
130
|
|
|
131
131
|
- The splice is literal: `text.slice(0, start) + newText + text.slice(end)`. `String.prototype.replace` is never called, so `$&` and `$1` stay as written.
|
|
132
|
+
- A matcher's `adapt()` can rewrite the new text for its hit before the splice. `normalizedMatcher()` and `lineTrimmedMatcher()` use it so that the leading and trailing lines the new text leaves as they were keep the file's bytes, and each replaced line keeps the CR of the file line it replaces. When the old text and the new text are the same, the new text keeps its characters.
|
|
133
|
+
- Each loose hit gives a `fuzzy-match` note. `describeWrite()` compares the written lines with the model's new lines, line by line, and counts the lines that kept the file's characters, the lines that kept only the file's CR, and the lines that the matcher changed in another way. `fuzzyMatch` and `patchFuzzyMatch` get the counts as `written`, and the default text says only what took place. `apply_patch` makes the same counts in `applyHunks()`.
|
|
132
134
|
- A loose hit much longer than the old text (`span`), a hit that starts or ends inside a folded span (`boundary`), and a loose hit whose new text adds escape sequences the matched text does not have (`escape`) give `MATCH_REFUSED`.
|
|
133
135
|
- Ranges of different pairs must not overlap. Splices apply from the end.
|
|
134
136
|
- A result equal to the original gives `NO_CHANGE`, unless every pair is already applied.
|
|
@@ -139,7 +141,7 @@ A miss is `NO_MATCH`, with a trailing-newline hint when that is the only differe
|
|
|
139
141
|
|
|
140
142
|
The order is parse, resolve, stat, access, lock, stat again, load, preconditions, verify every hunk, plan, guards, change authorize, encode, commit, hooks, record.
|
|
141
143
|
|
|
142
|
-
Verification collects every problem before anything changes: an Add on an existing file, an Update or Delete on a missing file, a move onto an existing file, two operations on one file, a missing `remove()` for a Delete or Move, and each hunk that does not match. Each hunk matches at the first place after the previous hunk (Codex). The chain runs on whole lines. A ` ` context line takes the file's own line, so its bytes stay the same.
|
|
144
|
+
Verification collects every problem before anything changes: an Add on an existing file, an Update or Delete on a missing file, a move onto an existing file, two operations on one file, a missing `remove()` for a Delete or Move, and each hunk that does not match. Each hunk matches at the first place after the previous hunk (Codex). A `@@` header moves the search below the line it matches. When the old lines are not found from there, `applyHunks()` tries them once more at the header's own line with `seekAt()`, which takes a hit only when it starts at that line. This differs from Codex: it accepts a hunk whose first `-` or context line repeats the header. The trailing-blank retry runs in both searches. The `lines-not-found` problem holds the line where the search started and the line the header matched, and `patchLinesNotFound` names them. The chain runs on whole lines. A ` ` context line takes the file's own line, so its bytes stay the same. In each run of `-` and `+` lines, a `-` line that comes back unchanged as a `+` line also takes the file's line, and each other `+` line takes the CR of the file line it replaces. A hunk whose old and new lines are the same writes its `+` lines as given, with those CRs.
|
|
143
145
|
|
|
144
146
|
The commit keeps a journal:
|
|
145
147
|
|
|
@@ -167,7 +169,7 @@ Hooks run after the whole commit, once for each committed file, in order. `newFi
|
|
|
167
169
|
|
|
168
170
|
## Records
|
|
169
171
|
|
|
170
|
-
After a commit, each committed file gets a record with `schema: 2`, `origin: "write"`, the new version, and the content hash of the written bytes. `request` is `null`, and `viewId` equals `contentId`. `wholeFileVisible` is true after a `write` and a patch Add. After an `edit` or a patch Update it keeps the previous record's value. It is false when an authorizer replaced the content, a hook rewrote the file, or an edit went ahead on a stale record: the model has not seen those bytes, so a later `write` needs a read. A move stores the destination and deletes the source key. A Delete deletes the key. `no-change` stores nothing.
|
|
172
|
+
After a commit, each committed file gets a record with `schema: 2`, `origin: "write"`, the new version, and the content hash of the written bytes. `request` is `null`, `seenLines` is empty, and `viewId` equals `contentId`. `wholeFileVisible` is true after a `write` and a patch Add. After an `edit` or a patch Update it keeps the previous record's value. It is false when an authorizer replaced the content, a hook rewrote the file, or an edit went ahead on a stale record: the model has not seen those bytes, so a later `write` needs a read. A move stores the destination and deletes the source key. A Delete deletes the key. `no-change` stores nothing.
|
|
171
173
|
|
|
172
174
|
`memoryStore()` keeps 1 000 records for 30 minutes. A read older than that needs a new read before an edit.
|
|
173
175
|
|