@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
package/README.md
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
<a href="https://betterfs.dev"><img src="https://betterfs.dev/brand/D-wordmark-auto.svg" alt="Better FS Tools" height="64"></a>
|
|
2
|
+
|
|
1
3
|
# @better-fs-tools/write
|
|
2
4
|
|
|
3
5
|
Edit, write, and apply_patch tools for TypeScript agents. They share one mutation core with the read tool in [`@better-fs-tools/read`](https://www.npmjs.com/package/@better-fs-tools/read): the same filesystem contract, the same read store, and the same resolvers and authorizers.
|
|
@@ -18,7 +20,7 @@ The package has no peers and imports no `node:` module. It depends on `@better-f
|
|
|
18
20
|
|
|
19
21
|
## Quick start
|
|
20
22
|
|
|
21
|
-
`createFsTools({ fs })` builds `read`, `edit`, and `write` over any `WritableFileSystem`. On Node, `
|
|
23
|
+
`createFsTools({ fs })` builds `read`, `edit`, and `write` over any `WritableFileSystem`. On Node, pass `nodeFileSystem()` from [`@better-fs-tools/node`](https://www.npmjs.com/package/@better-fs-tools/node) as `fs`:
|
|
22
24
|
|
|
23
25
|
```ts
|
|
24
26
|
import { memoryFileSystem } from "@better-fs-tools/fs";
|
|
@@ -50,33 +52,37 @@ A tool call is `tool(input, ctx?)`. `ctx` is the read tool's call context: `{ si
|
|
|
50
52
|
|
|
51
53
|
## One bundle for every tool
|
|
52
54
|
|
|
53
|
-
`createFsTools(options)` takes a `WritableFileSystem`, or a factory that returns one for each call, and builds `read`, `edit`, and `write`. `applyPatch: true`, or an options object, adds `apply_patch`. The tools share one `state` (
|
|
55
|
+
`createFsTools(options)` takes a `WritableFileSystem`, or a factory that returns one for each call, and builds `read`, `edit`, and `write`. `applyPatch: true`, or an options object, adds `apply_patch`. The tools share one `state` (a store or a factory of the call; default `null`, so there is no read-before-write; `memoryStore({ clock })` turns it on), one `digest` (default `sha256Digest()`), one `locks` (default `memoryLocks()`), and one `clock`. It imports no `node:` module, so a Worker needs no host digest. Each tool's other options go under `read`, `edit`, `write`, and `applyPatch`.
|
|
54
56
|
|
|
55
57
|
Bash is off unless you ask. `bash: { runner, env, ... }` adds a bash tool from [`@better-fs-tools/shell`](https://www.npmjs.com/package/@better-fs-tools/shell) that gets the same `digest` and `clock`. The allowed roots of the filesystem do not limit a command.
|
|
56
58
|
|
|
57
|
-
The result also has `applyPatch` (`null` unless asked for), `state`, `digest`, `locks`, `clock`, and `invalidate(path, call?)`, which deletes the record for a path so the next edit needs a read. It returns an `InvalidateOutcome`. When `fs` is a factory, pass the call context, for example `ctx.call` in a bash `afterRun` hook.
|
|
59
|
+
The result also has `applyPatch` (`null` unless asked for), `state`, `digest`, `locks`, `clock`, and `invalidate(path, call?)`, which deletes the record for a path so the next edit needs a read. It returns an `InvalidateOutcome`. When `fs` or `state` is a factory, pass the call context, for example `ctx.call` in a bash `afterRun` hook.
|
|
60
|
+
|
|
61
|
+
An unknown option key, a shared key (`fs`, `state`, `digest`, `locks`, or `clock`) inside a tool's options, or `bash: true` throws `TypeError`.
|
|
58
62
|
|
|
59
|
-
|
|
63
|
+
`fsToolDefinitions(options)` from `./signature` builds the same bundle with each tool bound to its signature, and every integration bundle is an adapter over it. `editToolDefinition()`, `writeToolDefinition()`, and `applyPatchToolDefinition()` do the same for one tool. A definition has the name, description, and JSON Schema to declare, a `validate` for model input, and an `execute` that maps model input and runs the tool. Use them to connect the tools to a framework of your own.
|
|
60
64
|
|
|
61
65
|
## Hosts
|
|
62
66
|
|
|
63
67
|
| Host | Package | Factories |
|
|
64
68
|
| ------------------------------------- | --------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------- |
|
|
65
69
|
| Any | this package | `createFsTools({ fs })`, `createEditTool()`, `createWriteTool()`, `createApplyPatchTool()` |
|
|
66
|
-
| Node or Bun | [`@better-fs-tools/node`](https://www.npmjs.com/package/@better-fs-tools/node) | `
|
|
70
|
+
| Node or Bun | [`@better-fs-tools/node`](https://www.npmjs.com/package/@better-fs-tools/node) | `nodeFileSystem()` as the `fs` of any tool or bundle |
|
|
67
71
|
| AI SDK 7 | [`@better-fs-tools/ai-sdk`](https://www.npmjs.com/package/@better-fs-tools/ai-sdk) | `createAiSdkFsTools({ fs })`, `createAiSdkEditTool()`, `createAiSdkWriteTool()`, `createAiSdkApplyPatchTool()` |
|
|
68
|
-
| Pi coding agent | [`@better-fs-tools/pi`](https://www.npmjs.com/package/@better-fs-tools/pi) | `createPiFsTools()`, one factory for each tool, and a `pi.extensions` entry
|
|
72
|
+
| Pi coding agent | [`@better-fs-tools/pi`](https://www.npmjs.com/package/@better-fs-tools/pi) | `createPiFsTools({ fs })`, one factory for each tool, and a `pi.extensions` entry |
|
|
73
|
+
| Pi agent core | [`@better-fs-tools/pi`](https://www.npmjs.com/package/@better-fs-tools/pi) | `createAgentCoreFsTools({ fs })` and one factory for each tool, from `./agent-core` |
|
|
74
|
+
| Pi durable harness | [`@better-fs-tools/pi-durable`](https://www.npmjs.com/package/@better-fs-tools/pi-durable) | `createDurableFsTools({ fs })`, an extension |
|
|
69
75
|
| Cloudflare Shell, Computer, just-bash | `@better-fs-tools/cloudflare-shell`, `@better-fs-tools/cloudflare-computer`, `@better-fs-tools/just-bash` | Writable filesystems for `createFsTools()`, `createAiSdkFsTools()`, or the single factories |
|
|
70
76
|
|
|
71
|
-
The bundles and the
|
|
77
|
+
The bundles and the single-tool factories (`createEditTool()`, `createAiSdkEditTool()`, `createPiEditTool()`, and the others) all default to `state: null`. The tools then change files that the model has not read, and nothing tells the model. The tool descriptions still ask the model to read first. For the check, pass `state: memoryStore()` to a bundle (`createFsTools()`, `createPiFsTools()`, or `createAiSdkFsTools()`), or pass the same `state`, `digest`, and `locks` to the read tool and the write tools.
|
|
72
78
|
|
|
73
|
-
[docs/hosts.md](docs/hosts.md) lists the defaults
|
|
79
|
+
Every integration takes the options of the core factories, with the same defaults. [docs/hosts.md](docs/hosts.md) lists the shared defaults, the default signature of each tool, the few host options, a recommended configuration for each host, and what each backend can do.
|
|
74
80
|
|
|
75
81
|
## edit
|
|
76
82
|
|
|
77
83
|
`createEditTool({ fs })`. The input is a path and one or more `{ oldText, newText, replaceAll? }` pairs, all matched against one snapshot of the file. Each old text must match once, unless `replaceAll` is set. Pairs must not overlap. The replacement is a literal splice: `$&` and `$1` in the new text stay as written.
|
|
78
84
|
|
|
79
|
-
Matching tries `exactMatcher()` first, then `normalizedMatcher()` (trailing whitespace, Unicode normalization, curly quotes, dashes, and special spaces). A loose match gives a `fuzzy-match` note that names the lines. `escapeMatcher()` (escape sequences written out), `lineTrimmedMatcher()`, `indentationMatcher()`, and `blockAnchorMatcher()` are opt-in through the `matchers`
|
|
85
|
+
Matching tries `exactMatcher()` first, then `normalizedMatcher()` (trailing whitespace, Unicode normalization, curly quotes, dashes, and special spaces). A loose match gives a `fuzzy-match` note that names the lines and says what the tool wrote: how many unchanged lines kept the file's characters, how many lines kept the file's CRLF line ends, or that the new lines were written as given. The note does not ask the model to check the result, because a model then often undid a correct edit. The `fuzzyMatch` and `patchFuzzyMatch` messages get these counts in an optional `written` argument. After a loose match with `normalizedMatcher()` or `lineTrimmedMatcher()`, the leading and trailing lines that 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 edit can only change the characters that the matcher folded, so the new text is written as given. `escapeMatcher()` (escape sequences written out), `lineTrimmedMatcher()`, `indentationMatcher()`, and `blockAnchorMatcher()` are opt-in through the `matchers` option, for example `matchers: [...defaultEditMatchers(), escapeMatcher()]`.
|
|
80
86
|
|
|
81
87
|
When a pair does not match, the error is `NO_MATCH`, even when the new text is already in the file. `recovery: true` turns on three extras. The error shows the closest region of the file with line numbers. When the new text is already in the file and the old text is not, the pair is `already-applied` and the status is `no-change`. After three misses in a row on one file, the tool adds a `repeated-miss` note that asks the model to read the file again.
|
|
82
88
|
|
|
@@ -84,7 +90,7 @@ The model text is one line, for example `Edited src/app.ts: 1 replacement at lin
|
|
|
84
90
|
|
|
85
91
|
## write
|
|
86
92
|
|
|
87
|
-
`createWriteTool({ fs })` creates a missing file, or replaces an existing one. Missing parent folders are created. A replace needs a read of the whole file first.
|
|
93
|
+
`createWriteTool({ fs })` creates a missing file, or replaces an existing one. Missing parent folders are created. A replace needs a read of the whole file first. Reads in ranges count when together they show every line of the same content. The same content gives `no-change` and writes nothing, so the modification time stays.
|
|
88
94
|
|
|
89
95
|
## apply_patch
|
|
90
96
|
|
|
@@ -126,7 +132,7 @@ console.log(textOf(result));
|
|
|
126
132
|
// D /src/old.ts
|
|
127
133
|
```
|
|
128
134
|
|
|
129
|
-
Every operation is checked before any file 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, and a hunk that does not match all give `PATCH_VERIFY`, with every problem listed. Each hunk matches at the first place after the previous hunk, as Codex does. The chain is `exactMatcher()`, `normalizedMatcher()`, and `lineTrimmedMatcher()` on whole lines. Context lines keep the file's own bytes.
|
|
135
|
+
Every operation is checked before any file 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, and a hunk that does not match all give `PATCH_VERIFY`, with every problem listed. Each hunk matches at the first place after the previous hunk, as Codex does. A `@@ <line>` header moves the search to the line below the line it names. When the old lines are not found there, the tool also tries them at the named line itself, and takes that hit only when it starts at that line. This is a change from Codex, which looks only below the line. It lets a model write the line it changes as the `@@` header, for example `@@ target: A−B` followed by `-target: A−B`. A hunk that still does not match says where the search started, for example `failed to find the expected lines at line 2, where the @@ line matched, or from line 3 on`. The chain is `exactMatcher()`, `normalizedMatcher()`, and `lineTrimmedMatcher()` on whole lines. Context lines keep the file's own bytes. So does a `-` line that comes back unchanged as a `+` line. Each other `+` line keeps the CR of the file line it replaces, so a CRLF line in a mixed-EOL file stays CRLF. When the old and new lines of a hunk are the same, the hunk can only change the characters that the matcher folded, so its `+` lines are written as given.
|
|
130
136
|
|
|
131
137
|
The commit stages every new file when the backend has `stage()`, then publishes in patch order. Without `stage()` it writes each file in turn. When a step fails, the steps already done are undone from the end, and `result.commit` says so. When an undo step also fails, the code is `PARTIAL_COMMIT` and `result.commit.files` lists the state of every file. A host can pass its own `patchParser` for another patch format.
|
|
132
138
|
|
|
@@ -134,24 +140,24 @@ The commit stages every new file when the backend has `stage()`, then publishes
|
|
|
134
140
|
|
|
135
141
|
With the read tool's `state` store and a `digest`, the write tools check what the model has seen:
|
|
136
142
|
|
|
137
|
-
- An existing file needs a record from a read. `edit` and `apply_patch` accept a partial read. `write` needs a read of the whole file.
|
|
143
|
+
- An existing file needs a record from a read. `edit` and `apply_patch` accept a partial read. `write` needs a read of the whole file. Ranged reads of the same content count as a whole read when together they show every line.
|
|
138
144
|
- A file that changed since the read gives `STALE`. With `preconditions: { onStale: "rematch" }`, `edit` and `apply_patch` go ahead when every old text still matches exactly once, with a `stale-rematched` note. `write` always refuses.
|
|
139
145
|
- On a backend with stable identity, the check compares the backend `version`. On every other backend it compares the content hash.
|
|
140
146
|
- After a commit, the tool stores a record with `origin: "write"`. A second edit needs no new read. When an authorizer replaced the content, a hook rewrote the file, or an edit went ahead on a stale file, the record says the model has not seen the whole file, so a following `write` needs a read.
|
|
141
147
|
- `createInvalidator({ fs, state })` returns `invalidate(path)`. Call it when something else, for example a shell tool, may have changed a file. The next edit then needs a read. It never throws. It resolves to an `InvalidateOutcome`: `{ ok: true, resolvedPath, recorded }`, where `recorded` says whether a record was there, or `{ ok: false, phase }` when the stat (`phase: "stat"`, with the backend `error`) or the store (`phase: "state"`, with a `detail`) failed. After `ok: false` the record may still be there.
|
|
142
148
|
- `preconditions: { requireRead: "off" }` turns the check off. `partialRead` and `onStale` change the other two rules.
|
|
143
149
|
|
|
144
|
-
`state`
|
|
150
|
+
`state` with `digest: null` is a `TypeError`, as in the read tool, and the `StateNeedsDigest` type refuses it at compile time. The default digest is `sha256Digest()`. `state: null` turns the check off. The result has no note about this, because the model cannot change the host's setup.
|
|
145
151
|
|
|
146
|
-
##
|
|
152
|
+
## Options
|
|
147
153
|
|
|
148
|
-
Every
|
|
154
|
+
Every option except `fs` has a default. Pass them to `createEditTool()`, `createWriteTool()`, or `createApplyPatchTool()`, or to an integration factory, which has the same defaults.
|
|
149
155
|
|
|
150
|
-
|
|
|
156
|
+
| Option | Default | Meaning |
|
|
151
157
|
| --------------- | -------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------- |
|
|
152
158
|
| `fs` | required | A `WritableFileSystem`, or a factory of the call context that runs once for each call |
|
|
153
|
-
| `state` | `null` | The read tool's store, or a factory. Needs `digest`
|
|
154
|
-
| `digest` | `
|
|
159
|
+
| `state` | `null` | The read tool's store, or a factory. Needs a `digest` |
|
|
160
|
+
| `digest` | `sha256Digest()` | Content hashes for records and `changes[].after.contentId`. `null` turns them off |
|
|
155
161
|
| `locks` | `memoryLocks()` | Orders writers to one path inside the process. Share one across the three tools. `timeoutMs` default 30 s |
|
|
156
162
|
| `resolve` | `null` | A read `PathResolver`, for example `unicodeRepair()` |
|
|
157
163
|
| `authorize` | `null` | A `WriteAuthorizer`, or a read `ToolAuthorizer` such as `denyPaths()` |
|
|
@@ -183,9 +189,9 @@ Every dependency except `fs` has a default. Pass them to `createEditTool()`, `cr
|
|
|
183
189
|
| `maxListedMatches` | 10 | Line numbers listed for an ambiguous match |
|
|
184
190
|
| `maxPatchProblems` | 20 | Problems listed for a failed patch verify |
|
|
185
191
|
|
|
186
|
-
A `sampleBytes` that you set above `maxFileBytes` throws `TypeError`. When you set only `maxFileBytes` below 8 192, the default `sampleBytes` is lowered to it. The same limits rule holds in read, write, and bash.
|
|
192
|
+
A `sampleBytes` that you set above `maxFileBytes` throws `TypeError`. When you set only `maxFileBytes` below 8 192, the default `sampleBytes` is lowered to it. The same limits rule holds in read, write, and bash. In read and bash, a per-call value over its ceiling is clamped, with a `clamped` info note. The write tools clamp nothing: an input over a write limit, such as too many edits, gives `INVALID_INPUT`. Two limits that you set and that conflict throw `TypeError` when the tool is built. A default that is over a ceiling you set is lowered to that ceiling.
|
|
187
193
|
|
|
188
|
-
Every tool has one parse helper with the same pattern: `parseReadInput`, `parseEditInput`, `parseWriteInput`, `parseApplyPatchInput`, and `parseBashInput`. Each takes the canonical input and the resolved limits, returns the request, and throws `TypeError` for input it refuses. The tool turns that throw into `INVALID_INPUT`.
|
|
194
|
+
Every tool has one parse helper with the same pattern: `parseReadInput`, `parseEditInput`, `parseWriteInput`, `parseApplyPatchInput`, and `parseBashInput`. Each takes the canonical input and the resolved limits, returns the request, and throws `TypeError` for input it refuses. The tool turns that throw into `INVALID_INPUT`. In read and bash, a clamp is not an error: the helper returns the clamped value, and the tool adds the `clamped` note.
|
|
189
195
|
|
|
190
196
|
## Authorize
|
|
191
197
|
|
|
@@ -229,7 +235,7 @@ An authorizer may allow with `content` for `edit` and `write`. The tool then wri
|
|
|
229
235
|
|
|
230
236
|
## Guards and hooks
|
|
231
237
|
|
|
232
|
-
Guards check each planned change before anything is written. No guard runs by default. `recommendedGuards()` gives the five this package recommends, and the `guards`
|
|
238
|
+
Guards check each planned change before anything is written. No guard runs by default. `recommendedGuards()` gives the five this package recommends, and the `guards` option turns them on. They refuse new text that carries the read tool's line-number gutter (`readPrefixGuard`), a read note line or clamp marker (`truncationNoticeGuard`), or a placeholder such as `// ... rest of code` in place of real lines (`omissionGuard`). It refuses an update that makes a valid `.json` file invalid (`syntaxGuard`), and new content that the classifiers do not call text (`nonTextGuard`). The default classifiers call a notebook JSON text. Pass `classifiers: [notebookClassifier(), ...defaultClassifiers()]` to refuse notebooks on load and in `nonTextGuard`. Each guard lets the near miss through: lines already in the file, a file that was already broken, or a create. `generatedFileGuard()` is opt-in. It refuses updates to files that look generated. A host guard returns `{ allow: true, notes? }` or `{ allow: false, note? }`, as an authorizer does. A refusal without a note gets the `guardRefused` message from the catalog.
|
|
233
239
|
|
|
234
240
|
Hooks run after each committed file. `executableShebang()` gives a new file that starts with `#!` mode `0o755`, through the hook's `newFileMode()`. `verifyWrite()` reads each committed file back and adds a warning when it does not match what was written. A hook failure is a warning note: the file is already committed, so the status stays `ok`.
|
|
235
241
|
|
|
@@ -301,13 +307,12 @@ console.log(textOf(await write({ path: "/repo/run.sh", content: "#!/bin/sh\necho
|
|
|
301
307
|
|
|
302
308
|
A signature is what the model sees: a name, a description, and a JSON Schema. `toInput` checks model input and maps it to the core's canonical input, and `param(name)` gives the host name of a canonical parameter for messages. Every write signature is a `ToolSignature` from `@better-fs-tools/read`, the base that the read and bash signatures share. The host adapters take a `signature` option.
|
|
303
309
|
|
|
304
|
-
|
|
310
|
+
`defaultEditSignature()`, `defaultWriteSignature()`, and `defaultPatchSignature()` build the signatures. Each takes the shared `SignatureDocs` options: `name`, `description`, `describe`, and `names`. `describe` and `names` use the signature's own parameter names as keys, so `defaultEditSignature` takes `old_string`. Two presets give another shape. `multiEditSignature()` gives `edit({ path, edits: [{ oldText, newText }] })`, so the model can send many replacements in one call, up to `limits.maxEdits`. It has no replace-all parameter, so no message suggests one. `freeformPatchSignature()` is `defaultPatchSignature()` with the Codex patch grammar. For any other shape, write your own `EditSignature`, `WriteSignature`, or `PatchSignature`.
|
|
305
311
|
|
|
306
312
|
```ts
|
|
307
313
|
import { memoryFileSystem } from "@better-fs-tools/fs";
|
|
308
314
|
import { createEditTool } from "@better-fs-tools/write";
|
|
309
315
|
import {
|
|
310
|
-
camelCaseEditSignature,
|
|
311
316
|
defaultEditSignature,
|
|
312
317
|
freeformPatchSignature,
|
|
313
318
|
multiEditSignature,
|
|
@@ -315,7 +320,15 @@ import {
|
|
|
315
320
|
} from "@better-fs-tools/write/signature";
|
|
316
321
|
|
|
317
322
|
// What the model sees: a name, a description, and a JSON Schema.
|
|
318
|
-
const signature =
|
|
323
|
+
const signature = defaultEditSignature({
|
|
324
|
+
name: "edit_file",
|
|
325
|
+
names: {
|
|
326
|
+
path: "filePath",
|
|
327
|
+
old_string: "oldString",
|
|
328
|
+
new_string: "newString",
|
|
329
|
+
replace_all: "replaceAll",
|
|
330
|
+
},
|
|
331
|
+
});
|
|
319
332
|
console.log(signature.name, Object.keys(signature.schema.properties ?? {}));
|
|
320
333
|
// edit_file [ "filePath", "oldString", "newString", "replaceAll" ]
|
|
321
334
|
|
|
@@ -332,20 +345,19 @@ export async function run(input: unknown) {
|
|
|
332
345
|
return edit(signature.toInput(input));
|
|
333
346
|
}
|
|
334
347
|
|
|
335
|
-
// The
|
|
336
|
-
export const presets = [
|
|
337
|
-
defaultEditSignature(), // edit({ path, old_string, new_string, replace_all? })
|
|
338
|
-
multiEditSignature(), // edit({ path, edits: [{ oldText, newText }] }), Pi's shape
|
|
339
|
-
freeformPatchSignature(), // apply_patch({ patch }) plus a Lark grammar for grammar-tool hosts
|
|
340
|
-
];
|
|
341
|
-
|
|
342
|
-
// Every preset takes names. The keys are the preset's own parameter names.
|
|
348
|
+
// The names keys are the default signature's own parameter names.
|
|
343
349
|
export const renamed = defaultEditSignature({
|
|
344
350
|
names: { path: "file", old_string: "find", new_string: "replace" },
|
|
345
351
|
}); // edit({ file, find, replace, replace_all? })
|
|
352
|
+
|
|
353
|
+
// Two presets give a shape that names alone cannot give.
|
|
354
|
+
export const presets = [
|
|
355
|
+
multiEditSignature(), // edit({ path, edits: [{ oldText, newText }] }), many replacements in one call
|
|
356
|
+
freeformPatchSignature(), // apply_patch({ patch }) plus the Codex Lark grammar for grammar-tool hosts
|
|
357
|
+
];
|
|
346
358
|
```
|
|
347
359
|
|
|
348
|
-
`freeformPatchSignature()` adds `grammar.lark`, the Codex patch grammar. The Pi
|
|
360
|
+
`freeformPatchSignature()` adds `grammar.lark`, the Codex patch grammar. The Pi, pi-agent-core, and pi-durable adapters send it as a grammar tool to models that support one. The AI SDK adapter ignores it and sends the JSON schema.
|
|
349
361
|
|
|
350
362
|
## The result
|
|
351
363
|
|
|
@@ -402,7 +414,7 @@ Without compare-and-swap, the core checks the version with a fresh `stat` just b
|
|
|
402
414
|
- With `recovery: true`, the repeated-miss count lives in each tool instance. A host that builds a new tool for each call never sees the `repeated-miss` note.
|
|
403
415
|
- After-commit hooks are not raced against the abort signal. A hook that never settles holds the call open. From the first commit step on, the signal is ignored, so a started commit finishes.
|
|
404
416
|
- `memoryStore()` forgets a read after 30 minutes, and keeps at most 1 000 records. A file read longer ago needs a new read before `edit`, and the result says so (`NOT_READ`).
|
|
405
|
-
-
|
|
417
|
+
- After a loose match, a line that the model changed is written with the model's characters. When the normalized matcher matched ASCII quotes against curly quotes on that line, the model's ASCII quotes are written. Only whole unchanged lines keep the file's bytes. When a hit changes one line and retypes another line only to fix its lookalike characters, the retyped line keeps the file's bytes. In a mixed-EOL file, a line inserted where no line is replaced gets no CR.
|
|
406
418
|
|
|
407
419
|
## Entries
|
|
408
420
|
|
|
@@ -410,7 +422,7 @@ Without compare-and-swap, the core checks the version with a fresh `stat` just b
|
|
|
410
422
|
| ---------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
411
423
|
| `@better-fs-tools/write` | `textOf` (the same function as in read and shell, re-exported for convenience), `createFsTools`, `createWriteTool`, `createEditTool`, `createApplyPatchTool`, the matchers, the guards, `defaultGuards` (empty) and `recommendedGuards`, the mutation core's types, `utf8Codec`, `memoryLocks`, `askBeforeWrite`, `writeAuthorizers`, `protectPaths`, `executableShebang`, `verifyWrite`, `defaultWriteFormatter`, `createInvalidator` |
|
|
412
424
|
| `@better-fs-tools/write/patch` | `parsePatch`, `codexPatchParser`, `CODEX_PATCH_GRAMMAR`, and the patch types. The only home of these |
|
|
413
|
-
| `@better-fs-tools/write/signature` | `defaultEditSignature`, `multiEditSignature`, `
|
|
425
|
+
| `@better-fs-tools/write/signature` | `defaultEditSignature`, `multiEditSignature`, `defaultWriteSignature`, `defaultPatchSignature`, `freeformPatchSignature`, and `writeSignatureMessages` |
|
|
414
426
|
|
|
415
427
|
The package builds on the tool-neutral base types in `@better-fs-tools/read`, such as `ToolCallContext`, `PathResolver`, and `ToolAuthorizer`. An authorizer or resolver typed on them works for the read tool and for the write tools:
|
|
416
428
|
|
|
@@ -440,8 +452,8 @@ export const noGenerated: ToolAuthorizer<unknown> = {
|
|
|
440
452
|
## More
|
|
441
453
|
|
|
442
454
|
- [docs/architecture.md](docs/architecture.md): the pipeline, the stages, and why they are fixed
|
|
443
|
-
- [docs/result-schema.md](docs/result-schema.md): every field of the result, every error code, and every note, plus one table of the statuses and error codes of all five tools
|
|
444
|
-
- [docs/hosts.md](docs/hosts.md): the defaults of every host and bundle, and what each backend can do
|
|
455
|
+
- [docs/result-schema.md](docs/result-schema.md): every field of the result, every error code, and every note, plus one table of the statuses and error codes of all five tools. The [write reference](https://betterfs.dev/reference/write) on the docs site has the same facts
|
|
456
|
+
- [docs/hosts.md](docs/hosts.md): the defaults of every host and bundle, and what each backend can do. [Choose an integration](https://betterfs.dev/integrations) and [Filesystems](https://betterfs.dev/filesystems) on the docs site have the same tables
|
|
445
457
|
- [`@better-fs-tools/fs`](https://www.npmjs.com/package/@better-fs-tools/fs): the `WritableFileSystem` contract and its conformance suite
|
|
446
458
|
|
|
447
459
|
## License
|
|
@@ -1,33 +1,43 @@
|
|
|
1
1
|
import type { WritableFileSystem } from "@better-fs-tools/fs";
|
|
2
|
-
import type { Clock, Digest, ReadStateStore, ReadTool,
|
|
3
|
-
import type { BashTool,
|
|
2
|
+
import type { Clock, Digest, ReadStateStore, ReadTool, ReadToolOptions, ToolCallContext } from "@better-fs-tools/read";
|
|
3
|
+
import type { BashTool, BashToolOptions } from "@better-fs-tools/shell";
|
|
4
4
|
import type { EditTool, ApplyPatchTool, WriteTool } from "../contract/context.ts";
|
|
5
|
-
import type {
|
|
5
|
+
import type { ApplyPatchToolOptions, EditToolOptions, WriteToolOptions } from "../contract/deps.ts";
|
|
6
6
|
import type { LockManager } from "../contract/locks.ts";
|
|
7
7
|
import type { InvalidateOutcome } from "../state/invalidate.ts";
|
|
8
|
+
/** A read state store, a factory of the call, or null. */
|
|
9
|
+
export type FsToolsState<THost = undefined> = ReadStateStore | ((call: ToolCallContext<THost>) => ReadStateStore | null) | null;
|
|
8
10
|
/** Options set once for the whole bundle. A tool's own options cannot set them. */
|
|
9
11
|
export type FsToolsSharedKey = "fs" | "state" | "digest" | "locks" | "clock";
|
|
10
12
|
/** The bash tool's options, without the digest and clock that the bundle shares. */
|
|
11
|
-
export type FsToolsBashOptions<THost = undefined> = Omit<
|
|
13
|
+
export type FsToolsBashOptions<THost = undefined> = Omit<BashToolOptions<THost>, "digest" | "clock">;
|
|
14
|
+
/** The read tool's options, without the options that the bundle shares. */
|
|
15
|
+
export type FsToolsReadOptions<THost = undefined> = Omit<Partial<ReadToolOptions<THost>>, FsToolsSharedKey>;
|
|
16
|
+
/** The edit tool's options, without the options that the bundle shares. */
|
|
17
|
+
export type FsToolsEditOptions<THost = undefined> = Omit<Partial<EditToolOptions<THost>>, FsToolsSharedKey>;
|
|
18
|
+
/** The write tool's options, without the options that the bundle shares. */
|
|
19
|
+
export type FsToolsWriteOptions<THost = undefined> = Omit<Partial<WriteToolOptions<THost>>, FsToolsSharedKey>;
|
|
12
20
|
/** The apply_patch tool's options, without the options that the bundle shares. */
|
|
13
|
-
export type FsToolsApplyPatchOptions<THost = undefined> = Omit<Partial<
|
|
14
|
-
export interface
|
|
21
|
+
export type FsToolsApplyPatchOptions<THost = undefined> = Omit<Partial<ApplyPatchToolOptions<THost>>, FsToolsSharedKey>;
|
|
22
|
+
export interface FsToolsOptions<THost = undefined> {
|
|
15
23
|
/** Required. A backend, or a factory called once for each call. */
|
|
16
24
|
readonly fs: WritableFileSystem | ((call: ToolCallContext<THost>) => WritableFileSystem);
|
|
17
25
|
/**
|
|
18
26
|
* Default null: no read-before-write. A store, for example
|
|
19
|
-
* memoryStore({ clock }), turns it on for edit, write, and apply_patch.
|
|
27
|
+
* memoryStore({ clock }), turns it on for edit, write, and apply_patch. A
|
|
28
|
+
* factory gives each call its own store: every tool calls it at most once
|
|
29
|
+
* for each call.
|
|
20
30
|
*/
|
|
21
|
-
readonly state?:
|
|
31
|
+
readonly state?: FsToolsState<THost>;
|
|
22
32
|
/** Default sha256Digest(). */
|
|
23
33
|
readonly digest?: Digest;
|
|
24
34
|
/** Default memoryLocks(), shared by edit, write, and apply_patch. */
|
|
25
35
|
readonly locks?: LockManager;
|
|
26
36
|
/** Default () => new Date(). */
|
|
27
37
|
readonly clock?: Clock;
|
|
28
|
-
readonly read?:
|
|
29
|
-
readonly edit?:
|
|
30
|
-
readonly write?:
|
|
38
|
+
readonly read?: FsToolsReadOptions<THost>;
|
|
39
|
+
readonly edit?: FsToolsEditOptions<THost>;
|
|
40
|
+
readonly write?: FsToolsWriteOptions<THost>;
|
|
31
41
|
/**
|
|
32
42
|
* Off by default. `true` or an options object adds an apply_patch tool.
|
|
33
43
|
* `false` is the same as leaving it out.
|
|
@@ -47,15 +57,15 @@ export interface FsTools<THost = undefined> {
|
|
|
47
57
|
readonly applyPatch: ApplyPatchTool<THost> | null;
|
|
48
58
|
/** null unless options.bash is set. It does not share the read state: see invalidate. */
|
|
49
59
|
readonly bash: BashTool<THost> | null;
|
|
50
|
-
readonly fs:
|
|
51
|
-
readonly state:
|
|
60
|
+
readonly fs: FsToolsOptions<THost>["fs"];
|
|
61
|
+
readonly state: FsToolsState<THost>;
|
|
52
62
|
readonly digest: Digest;
|
|
53
63
|
readonly locks: LockManager;
|
|
54
64
|
readonly clock: Clock;
|
|
55
65
|
/**
|
|
56
66
|
* Deletes the read record for a path, so the next edit or write needs a
|
|
57
|
-
* fresh read. Never throws. When fs is a factory, pass the call
|
|
58
|
-
* hook has it as ctx.call), or the outcome is ok: false.
|
|
67
|
+
* fresh read. Never throws. When fs or state is a factory, pass the call
|
|
68
|
+
* context (a hook has it as ctx.call), or the outcome is ok: false.
|
|
59
69
|
*/
|
|
60
70
|
invalidate(path: string, call?: ToolCallContext<THost>): Promise<InvalidateOutcome>;
|
|
61
71
|
}
|
|
@@ -80,14 +90,44 @@ export interface FsToolsWithApplyPatch<THost = undefined> extends FsTools<THost>
|
|
|
80
90
|
* Throws TypeError on an unknown option, a shared option inside a tool's
|
|
81
91
|
* options, or anything a tool factory refuses.
|
|
82
92
|
*/
|
|
83
|
-
export declare function createFsTools<THost = undefined>(options:
|
|
93
|
+
export declare function createFsTools<THost = undefined>(options: FsToolsOptions<THost> & {
|
|
84
94
|
readonly bash: FsToolsBashOptions<THost>;
|
|
85
95
|
readonly applyPatch: true | FsToolsApplyPatchOptions<THost>;
|
|
86
96
|
}): FsToolsWithBash<THost> & FsToolsWithApplyPatch<THost>;
|
|
87
|
-
export declare function createFsTools<THost = undefined>(options:
|
|
97
|
+
export declare function createFsTools<THost = undefined>(options: FsToolsOptions<THost> & {
|
|
88
98
|
readonly bash: FsToolsBashOptions<THost>;
|
|
89
99
|
}): FsToolsWithBash<THost>;
|
|
90
|
-
export declare function createFsTools<THost = undefined>(options:
|
|
100
|
+
export declare function createFsTools<THost = undefined>(options: FsToolsOptions<THost> & {
|
|
91
101
|
readonly applyPatch: true | FsToolsApplyPatchOptions<THost>;
|
|
92
102
|
}): FsToolsWithApplyPatch<THost>;
|
|
93
|
-
export declare function createFsTools<THost = undefined>(options:
|
|
103
|
+
export declare function createFsTools<THost = undefined>(options: FsToolsOptions<THost>): FsTools<THost>;
|
|
104
|
+
/** The option shape that createFsTools() and fsToolDefinitions() share. */
|
|
105
|
+
interface BundleOptions<THost, TApplyPatch, TBash> {
|
|
106
|
+
readonly fs: FsToolsOptions<THost>["fs"];
|
|
107
|
+
readonly state?: FsToolsState<THost>;
|
|
108
|
+
readonly digest?: Digest;
|
|
109
|
+
readonly locks?: LockManager;
|
|
110
|
+
readonly clock?: Clock;
|
|
111
|
+
readonly applyPatch?: boolean | TApplyPatch;
|
|
112
|
+
readonly bash?: false | TBash;
|
|
113
|
+
}
|
|
114
|
+
/** A bundle's shared parts, after the option checks. */
|
|
115
|
+
export interface ResolvedBundle<THost, TApplyPatch, TBash> {
|
|
116
|
+
readonly fs: FsToolsOptions<THost>["fs"];
|
|
117
|
+
readonly state: FsToolsState<THost>;
|
|
118
|
+
readonly digest: Digest;
|
|
119
|
+
readonly locks: LockManager;
|
|
120
|
+
readonly clock: Clock;
|
|
121
|
+
/** The apply_patch options, or null when it is off. */
|
|
122
|
+
readonly applyPatch: Partial<TApplyPatch> | null;
|
|
123
|
+
/** The bash options, or null when it is off. */
|
|
124
|
+
readonly bash: TBash | null;
|
|
125
|
+
readonly invalidate: FsTools<THost>["invalidate"];
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Checks the bundle options and resolves the shared defaults. Throws TypeError
|
|
129
|
+
* that names `label` on an unknown key, a shared key inside a tool's options,
|
|
130
|
+
* a malformed state, and bash: true.
|
|
131
|
+
*/
|
|
132
|
+
export declare function resolveBundle<THost, TApplyPatch extends object, TBash extends object>(label: string, options: BundleOptions<THost, TApplyPatch, TBash>): ResolvedBundle<THost, TApplyPatch, TBash>;
|
|
133
|
+
export {};
|
|
@@ -30,34 +30,53 @@ const EMPTY_STATE_STORE = Object.freeze({
|
|
|
30
30
|
delete: async () => { },
|
|
31
31
|
});
|
|
32
32
|
export function createFsTools(options) {
|
|
33
|
-
|
|
34
|
-
const { fs } =
|
|
35
|
-
const clock = options.clock ?? (() => new Date());
|
|
36
|
-
const state = options.state ?? null;
|
|
37
|
-
const digest = options.digest ?? sha256Digest();
|
|
38
|
-
const locks = options.locks ?? memoryLocks();
|
|
33
|
+
const bundle = resolveBundle("createFsTools", options);
|
|
34
|
+
const { fs, state, digest, locks, clock } = bundle;
|
|
39
35
|
const shared = { fs, state, digest, clock };
|
|
40
|
-
const applyPatch = options.applyPatch === true ? {} : (options.applyPatch ?? false);
|
|
41
|
-
const bash = options.bash === undefined || options.bash === false ? null : options.bash;
|
|
42
|
-
if (bash === true) {
|
|
43
|
-
throw new TypeError("createFsTools bash must be an object with a runner and an env: the portable bundle has no default runner");
|
|
44
|
-
}
|
|
45
36
|
return Object.freeze({
|
|
46
37
|
read: createReadTool({ ...options.read, ...shared }),
|
|
47
38
|
edit: createEditTool({ ...options.edit, ...shared, locks }),
|
|
48
39
|
write: createWriteTool({ ...options.write, ...shared, locks }),
|
|
49
|
-
applyPatch: applyPatch ===
|
|
40
|
+
applyPatch: bundle.applyPatch === null
|
|
50
41
|
? null
|
|
51
|
-
: createApplyPatchTool({ ...applyPatch, ...shared, locks }),
|
|
52
|
-
bash: bash === null ? null : createBashTool({ ...bash, digest, clock }),
|
|
42
|
+
: createApplyPatchTool({ ...bundle.applyPatch, ...shared, locks }),
|
|
43
|
+
bash: bundle.bash === null ? null : createBashTool({ ...bundle.bash, digest, clock }),
|
|
53
44
|
fs,
|
|
54
45
|
state,
|
|
55
46
|
digest,
|
|
56
47
|
locks,
|
|
57
48
|
clock,
|
|
58
|
-
invalidate:
|
|
49
|
+
invalidate: bundle.invalidate,
|
|
59
50
|
});
|
|
60
51
|
}
|
|
52
|
+
/**
|
|
53
|
+
* Checks the bundle options and resolves the shared defaults. Throws TypeError
|
|
54
|
+
* that names `label` on an unknown key, a shared key inside a tool's options,
|
|
55
|
+
* a malformed state, and bash: true.
|
|
56
|
+
*/
|
|
57
|
+
export function resolveBundle(label, options) {
|
|
58
|
+
checkFsToolsOptions(label, options, KNOWN_OPTION_KEYS, SHARED_KEYS_BY_TOOL);
|
|
59
|
+
const { fs } = options;
|
|
60
|
+
const state = options.state ?? null;
|
|
61
|
+
const bash = options.bash === undefined || options.bash === false ? null : options.bash;
|
|
62
|
+
if (bash === true) {
|
|
63
|
+
throw new TypeError(`${label} bash must be an object with a runner and an env: the portable bundle has no default runner`);
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
fs,
|
|
67
|
+
state,
|
|
68
|
+
digest: options.digest ?? sha256Digest(),
|
|
69
|
+
locks: options.locks ?? memoryLocks(),
|
|
70
|
+
clock: options.clock ?? (() => new Date()),
|
|
71
|
+
applyPatch: options.applyPatch === true
|
|
72
|
+
? {}
|
|
73
|
+
: options.applyPatch === false || options.applyPatch === undefined
|
|
74
|
+
? null
|
|
75
|
+
: options.applyPatch,
|
|
76
|
+
bash,
|
|
77
|
+
invalidate: invalidator(fs, state),
|
|
78
|
+
};
|
|
79
|
+
}
|
|
61
80
|
function checkFsToolsOptions(label, options, known, sharedKeys) {
|
|
62
81
|
if (options === null || typeof options !== "object" || Array.isArray(options)) {
|
|
63
82
|
throw new TypeError(`${label} options must be an object`);
|
|
@@ -67,8 +86,8 @@ function checkFsToolsOptions(label, options, known, sharedKeys) {
|
|
|
67
86
|
throw new TypeError(`Unknown ${label} option: ${key}`);
|
|
68
87
|
}
|
|
69
88
|
const state = options.state;
|
|
70
|
-
if (state !== undefined && state !== null && !isStore(state)) {
|
|
71
|
-
throw new TypeError(`${label} state must be a read state store
|
|
89
|
+
if (state !== undefined && state !== null && typeof state !== "function" && !isStore(state)) {
|
|
90
|
+
throw new TypeError(`${label} state must be a read state store, a function that returns one, or null`);
|
|
72
91
|
}
|
|
73
92
|
for (const [tool, keys] of Object.entries(sharedKeys)) {
|
|
74
93
|
const part = options[tool];
|
|
@@ -87,24 +106,26 @@ function checkFsToolsOptions(label, options, known, sharedKeys) {
|
|
|
87
106
|
}
|
|
88
107
|
}
|
|
89
108
|
function invalidator(fs, state) {
|
|
90
|
-
if (typeof fs !== "function") {
|
|
91
|
-
const fixed = createInvalidator({ fs, state });
|
|
109
|
+
if (typeof fs !== "function" && typeof state !== "function") {
|
|
110
|
+
const fixed = createInvalidator({ fs, state: state ?? EMPTY_STATE_STORE });
|
|
92
111
|
return (path) => fixed(path);
|
|
93
112
|
}
|
|
94
113
|
return async (path, call) => {
|
|
95
114
|
if (call === undefined) {
|
|
115
|
+
const factory = typeof fs === "function" ? "fs" : "state";
|
|
96
116
|
return {
|
|
97
117
|
ok: false,
|
|
98
118
|
phase: "stat",
|
|
99
119
|
error: {
|
|
100
120
|
reason: "unsupported",
|
|
101
|
-
detail:
|
|
121
|
+
detail: `${factory} is a factory: pass the call context to invalidate(path, call)`,
|
|
102
122
|
},
|
|
103
123
|
};
|
|
104
124
|
}
|
|
105
125
|
let backend;
|
|
126
|
+
let store;
|
|
106
127
|
try {
|
|
107
|
-
backend = fs(call);
|
|
128
|
+
backend = typeof fs === "function" ? fs(call) : fs;
|
|
108
129
|
}
|
|
109
130
|
catch (error) {
|
|
110
131
|
return { ok: false, phase: "stat", error: { reason: "io", detail: messageOf(error) } };
|
|
@@ -119,7 +140,20 @@ function invalidator(fs, state) {
|
|
|
119
140
|
},
|
|
120
141
|
};
|
|
121
142
|
}
|
|
122
|
-
|
|
143
|
+
try {
|
|
144
|
+
store = typeof state === "function" ? state(call) : state;
|
|
145
|
+
}
|
|
146
|
+
catch (error) {
|
|
147
|
+
return { ok: false, phase: "state", detail: messageOf(error) };
|
|
148
|
+
}
|
|
149
|
+
if (store !== null && !isStore(store)) {
|
|
150
|
+
return {
|
|
151
|
+
ok: false,
|
|
152
|
+
phase: "state",
|
|
153
|
+
detail: "the state factory did not return a read state store or null",
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
return createInvalidator({ fs: backend, state: store ?? EMPTY_STATE_STORE })(path);
|
|
123
157
|
};
|
|
124
158
|
}
|
|
125
159
|
function isStore(value) {
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { Clock, Digest } from "@better-fs-tools/read";
|
|
2
|
+
import type { ReadSignature, ReadToolDefinition } from "@better-fs-tools/read/signature";
|
|
3
|
+
import type { BashSignature, BashToolDefinition } from "@better-fs-tools/shell/signature";
|
|
4
|
+
import type { LockManager } from "../contract/locks.ts";
|
|
5
|
+
import type { ApplyPatchToolDefinition, EditToolDefinition, WriteToolDefinition } from "../signature/definition.ts";
|
|
6
|
+
import type { EditSignature, PatchSignature, WriteSignature } from "../signature/contract.ts";
|
|
7
|
+
import type { FsTools, FsToolsApplyPatchOptions, FsToolsBashOptions, FsToolsEditOptions, FsToolsOptions, FsToolsReadOptions, FsToolsState, FsToolsWriteOptions } from "./create-fs-tools.ts";
|
|
8
|
+
/** The options of createFsTools(), with a `signature` in each tool's options. */
|
|
9
|
+
export interface FsToolDefinitionsOptions<THost = undefined> extends Omit<FsToolsOptions<THost>, "read" | "edit" | "write" | "applyPatch" | "bash"> {
|
|
10
|
+
readonly read?: FsToolsReadOptions<THost> & {
|
|
11
|
+
readonly signature?: ReadSignature;
|
|
12
|
+
};
|
|
13
|
+
readonly edit?: FsToolsEditOptions<THost> & {
|
|
14
|
+
readonly signature?: EditSignature;
|
|
15
|
+
};
|
|
16
|
+
readonly write?: FsToolsWriteOptions<THost> & {
|
|
17
|
+
readonly signature?: WriteSignature;
|
|
18
|
+
};
|
|
19
|
+
/** Off by default. `true` or an options object adds apply_patch. */
|
|
20
|
+
readonly applyPatch?: boolean | (FsToolsApplyPatchOptions<THost> & {
|
|
21
|
+
readonly signature?: PatchSignature;
|
|
22
|
+
});
|
|
23
|
+
/** Off by default. The bash options, with `runner` and `env` required. */
|
|
24
|
+
readonly bash?: false | (FsToolsBashOptions<THost> & {
|
|
25
|
+
readonly signature?: BashSignature;
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
/** The tool definitions of one bundle, and the parts they share. */
|
|
29
|
+
export interface FsToolDefinitions<THost = undefined> {
|
|
30
|
+
readonly read: ReadToolDefinition<THost>;
|
|
31
|
+
readonly edit: EditToolDefinition<THost>;
|
|
32
|
+
readonly write: WriteToolDefinition<THost>;
|
|
33
|
+
/** null unless options.applyPatch is true or an object. */
|
|
34
|
+
readonly applyPatch: ApplyPatchToolDefinition<THost> | null;
|
|
35
|
+
/** null unless options.bash is set. */
|
|
36
|
+
readonly bash: BashToolDefinition<THost> | null;
|
|
37
|
+
readonly fs: FsToolsOptions<THost>["fs"];
|
|
38
|
+
readonly state: FsToolsState<THost>;
|
|
39
|
+
readonly digest: Digest;
|
|
40
|
+
readonly locks: LockManager;
|
|
41
|
+
readonly clock: Clock;
|
|
42
|
+
/** As createFsTools().invalidate. */
|
|
43
|
+
readonly invalidate: FsTools<THost>["invalidate"];
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* The bundle of createFsTools(), with each tool bound to its signature by
|
|
47
|
+
* readToolDefinition() and the other definition helpers. Every integration
|
|
48
|
+
* bundle builds on it. Throws TypeError where createFsTools() does, and when
|
|
49
|
+
* two tools have the same name. `label` names the caller in each message.
|
|
50
|
+
*/
|
|
51
|
+
export declare function fsToolDefinitions<THost = undefined>(options: FsToolDefinitionsOptions<THost>, label?: string): FsToolDefinitions<THost>;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { readToolDefinition } from "@better-fs-tools/read/signature";
|
|
2
|
+
import { bashToolDefinition } from "@better-fs-tools/shell/signature";
|
|
3
|
+
import { applyPatchToolDefinition, editToolDefinition, writeToolDefinition, } from "../signature/definition.js";
|
|
4
|
+
import { resolveBundle } from "./create-fs-tools.js";
|
|
5
|
+
/**
|
|
6
|
+
* The bundle of createFsTools(), with each tool bound to its signature by
|
|
7
|
+
* readToolDefinition() and the other definition helpers. Every integration
|
|
8
|
+
* bundle builds on it. Throws TypeError where createFsTools() does, and when
|
|
9
|
+
* two tools have the same name. `label` names the caller in each message.
|
|
10
|
+
*/
|
|
11
|
+
export function fsToolDefinitions(options, label = "fsToolDefinitions") {
|
|
12
|
+
const bundle = resolveBundle(label, options);
|
|
13
|
+
const { fs, state, digest, locks, clock } = bundle;
|
|
14
|
+
const shared = { fs, state, digest, clock };
|
|
15
|
+
const definitions = {
|
|
16
|
+
read: readToolDefinition({ ...options.read, ...shared }),
|
|
17
|
+
edit: editToolDefinition({ ...options.edit, ...shared, locks }),
|
|
18
|
+
write: writeToolDefinition({ ...options.write, ...shared, locks }),
|
|
19
|
+
applyPatch: bundle.applyPatch === null
|
|
20
|
+
? null
|
|
21
|
+
: applyPatchToolDefinition({ ...bundle.applyPatch, ...shared, locks }),
|
|
22
|
+
bash: bundle.bash === null ? null : bashToolDefinition({ ...bundle.bash, digest, clock }),
|
|
23
|
+
};
|
|
24
|
+
const names = new Set();
|
|
25
|
+
for (const definition of Object.values(definitions)) {
|
|
26
|
+
if (definition === null)
|
|
27
|
+
continue;
|
|
28
|
+
if (names.has(definition.name)) {
|
|
29
|
+
throw new TypeError(`${label} has two tools named ${definition.name}`);
|
|
30
|
+
}
|
|
31
|
+
names.add(definition.name);
|
|
32
|
+
}
|
|
33
|
+
return Object.freeze({
|
|
34
|
+
...definitions,
|
|
35
|
+
fs,
|
|
36
|
+
state,
|
|
37
|
+
digest,
|
|
38
|
+
locks,
|
|
39
|
+
clock,
|
|
40
|
+
invalidate: bundle.invalidate,
|
|
41
|
+
});
|
|
42
|
+
}
|
package/dist/contract/deps.d.ts
CHANGED
|
@@ -21,7 +21,7 @@ export interface WriteDependencies<THost = undefined> {
|
|
|
21
21
|
readonly preconditions: Readonly<PreconditionPolicy>;
|
|
22
22
|
/** The read tool's store. A factory runs at most once for each call. null turns read-before-write off. */
|
|
23
23
|
readonly state: ReadStateStore | ((call: ToolCallContext<THost>) => ReadStateStore | null) | null;
|
|
24
|
-
/**
|
|
24
|
+
/** Default sha256Digest(). Needed when state is set. null turns it off. */
|
|
25
25
|
readonly digest: Digest | null;
|
|
26
26
|
readonly clock: Clock;
|
|
27
27
|
readonly locks: LockManager;
|
|
@@ -57,7 +57,7 @@ type ToolDeps<D extends WriteDependencies<never>> = {
|
|
|
57
57
|
readonly messages?: Partial<WriteMessageCatalog>;
|
|
58
58
|
readonly preconditions?: Partial<PreconditionPolicy>;
|
|
59
59
|
} & Partial<Omit<D, "fs" | "limits" | "messages" | "preconditions">>;
|
|
60
|
-
export type
|
|
61
|
-
export type
|
|
62
|
-
export type
|
|
60
|
+
export type WriteToolOptions<THost = undefined> = ToolDeps<WriteDependencies<THost>>;
|
|
61
|
+
export type EditToolOptions<THost = undefined> = ToolDeps<EditDependencies<THost>>;
|
|
62
|
+
export type ApplyPatchToolOptions<THost = undefined> = ToolDeps<ApplyPatchDependencies<THost>>;
|
|
63
63
|
export {};
|