@better-fs-tools/ai-sdk 0.1.0 → 0.2.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/README.md CHANGED
@@ -47,6 +47,7 @@ export async function ask(model: LanguageModel, prompt: string): Promise<string>
47
47
  - The schema's `validate` hook runs `signature.toInput` and then the core input check. A refusal names the host's parameters, and `generateText` reports it as a tool error.
48
48
  - `execute` passes AI SDK's `ToolExecutionOptions` object to the core as `ctx.call.host`, with `abortSignal` as the signal and `toolCallId` as `callId`. Host functions can read `ctx.call.host.context`. `execute` returns the whole `ReadResult`. If `toInput` refuses an input that reached `execute`, `execute` rejects with that `TypeError`.
49
49
  - Retry text in notes uses the signature's names. `result.continuation.next` stays canonical.
50
+ - `converters` defaults to `[imageConverter()]`, so an image read gives a `media` result. The core has no converters by default; this host turns images on. A list you pass replaces the default: `converters: []` turns images off, and a list without `imageConverter()` refuses images.
50
51
  - `toModelOutput` maps text parts to text parts, and media parts to base64 `file` parts. `toAiSdkOutput(result)` does the same mapping for your own tool.
51
52
 
52
53
  For a Cloudflare Agents host, pass `fs: cloudflareShellFileSystem(workspace, { allowedRoots: [root] })` from `@better-fs-tools/cloudflare-shell`. No other wrapper is needed.
@@ -62,8 +63,8 @@ import { createAiSdkFsTools } from "@better-fs-tools/ai-sdk";
62
63
  import { nodeFileSystem } from "@better-fs-tools/node";
63
64
 
64
65
  const cwd = process.cwd();
65
- // read, edit, write, and apply_patch over one filesystem, with one store,
66
- // one digest, and one lock manager. No bash: this bundle starts no process.
66
+ // read, edit, and write over one filesystem, with one digest and one lock
67
+ // manager. A read store, apply_patch, and bash are opt-in.
67
68
  const { tools } = createAiSdkFsTools({ fs: nodeFileSystem({ cwd, allowedRoots: [cwd] }) });
68
69
 
69
70
  export async function change(model: LanguageModel, prompt: string): Promise<string> {
@@ -72,8 +73,8 @@ export async function change(model: LanguageModel, prompt: string): Promise<stri
72
73
  }
73
74
  ```
74
75
 
75
- - `createAiSdkFsTools(options)` wraps `createFsTools()`. `fs` is required. `state` (default `memoryStore({ clock })`, `null` turns read-before-write off), `digest`, `locks`, and `clock` are set once. Each tool's other options, and its `signature`, go under `read`, `edit`, `write`, and `applyPatch`. `bash: { runner, env, ... }` adds a bash tool with the same digest and clock. The result has `read`, `edit`, `write`, `applyPatch`, `bash` (`null` when off), `tools`, `state`, `digest`, `locks`, `clock`, and `invalidate(path, call?)`. An unknown option key, a shared key inside a tool's options, or two tools with one name throws `TypeError`.
76
- - The single factories, `createAiSdkEditTool()`, `createAiSdkWriteTool()`, and `createAiSdkApplyPatchTool()`, take every option of their core factory, plus `signature`. `fs` is required. `state` and `digest` default to `null`, so without them every update carries a `read-before-write-off` note. A `state` needs a `digest`: the options type refuses one without the other, and the factory throws `TypeError`.
76
+ - `createAiSdkFsTools(options)` wraps `createFsTools()`. `fs` is required. `state` (default `null`, so read-before-write is off; `memoryStore()` turns it on), `digest`, `locks`, and `clock` are set once. Each tool's other options, and its `signature`, go under `read`, `edit`, `write`, and `applyPatch`. `read.converters` defaults to `[imageConverter()]`, as for `createAiSdkReadTool()`; `read: { converters: [] }` turns images off. `applyPatch: true`, or an options object, adds `apply_patch`. `bash: { runner, env, ... }` adds a bash tool with the same digest and clock. The result has `read`, `edit`, `write`, `applyPatch` and `bash` (`null` when off), `tools` (only the tools that are on), `state`, `digest`, `locks`, `clock`, and `invalidate(path, call?)`. An unknown option key, a shared key inside a tool's options, or two tools with one name throws `TypeError`.
77
+ - The single factories, `createAiSdkEditTool()`, `createAiSdkWriteTool()`, and `createAiSdkApplyPatchTool()`, take every option of their core factory, plus `signature`. `fs` is required. `state` and `digest` default to `null`, so without them read-before-write is off. A `state` needs a `digest`: the options type refuses one without the other, and the factory throws `TypeError`.
77
78
  - The default signatures are `defaultEditSignature()` (`edit` with `path`, `old_string`, `new_string`, and `replace_all`), `defaultWriteSignature()` (`write` with `path` and `content`), and `defaultPatchSignature()` (`apply_patch` with `patch`). Error texts use the signature's names.
78
79
  - The tools are `strict: true`. The schema's `validate` hook runs `signature.toInput` and the core input check.
79
80
  - `execute` passes the `ToolExecutionOptions` object as `ctx.call.host`, and returns the whole `MutationResult`. `toModelOutput` gives the model the formatter's text.
@@ -8,7 +8,10 @@ import type { AiSdkBashTool } from "./bash-tool.ts";
8
8
  import type { AiSdkMutationTool } from "./mutation-tools.ts";
9
9
  import type { AiSdkReadTool } from "./tool.ts";
10
10
  type Host<C> = ToolExecutionOptions<C>;
11
- /** Read tool options for createAiSdkFsTools. Shared keys are set once at the top level. */
11
+ /**
12
+ * Read tool options for createAiSdkFsTools. Shared keys are set once at the
13
+ * top level. converters defaults to [imageConverter()]; [] turns images off.
14
+ */
12
15
  export type AiSdkFsToolsReadOptions<C = unknown> = Omit<Partial<ReadToolDeps<Host<C>>>, FsToolsSharedKey> & {
13
16
  /** Defaults to defaultReadSignature(). */
14
17
  readonly signature?: ReadSignature;
@@ -33,7 +36,7 @@ export type AiSdkFsToolsBashOptions<C = unknown> = FsToolsBashOptions<Host<C>> &
33
36
  export interface CreateAiSdkFsToolsOptions<C = unknown> {
34
37
  /** Required. A backend, or a factory called once for each tool call with the AI SDK options as host. */
35
38
  readonly fs: CreateFsToolsOptions<Host<C>>["fs"];
36
- /** Defaults to memoryStore({ clock }). null turns read-before-write off. */
39
+ /** Defaults to null: no read-before-write. A store, for example memoryStore(), turns it on. */
37
40
  readonly state?: ReadStateStore | null;
38
41
  /** Defaults to sha256Digest() from @better-fs-tools/read, which runs in a Worker. */
39
42
  readonly digest?: Digest;
@@ -44,7 +47,8 @@ export interface CreateAiSdkFsToolsOptions<C = unknown> {
44
47
  readonly read?: AiSdkFsToolsReadOptions<C>;
45
48
  readonly edit?: AiSdkFsToolsEditOptions<C>;
46
49
  readonly write?: AiSdkFsToolsWriteOptions<C>;
47
- readonly applyPatch?: AiSdkFsToolsApplyPatchOptions<C>;
50
+ /** Off by default. `true` or an options object adds an apply_patch tool. */
51
+ readonly applyPatch?: boolean | AiSdkFsToolsApplyPatchOptions<C>;
48
52
  /** Off by default. This package starts no process: give a runner and an env to turn bash on. */
49
53
  readonly bash?: false | AiSdkFsToolsBashOptions<C>;
50
54
  }
@@ -52,7 +56,8 @@ export interface AiSdkFsTools<C = unknown> {
52
56
  readonly read: AiSdkReadTool<C>;
53
57
  readonly edit: AiSdkMutationTool<C>;
54
58
  readonly write: AiSdkMutationTool<C>;
55
- readonly applyPatch: AiSdkMutationTool<C>;
59
+ /** null unless options.applyPatch is true or an object. */
60
+ readonly applyPatch: AiSdkMutationTool<C> | null;
56
61
  /** null unless options.bash is set. */
57
62
  readonly bash: AiSdkBashTool<C> | null;
58
63
  /** Every tool of the bundle keyed by its name, ready for generateText({ tools }). */
@@ -68,17 +73,30 @@ export interface AiSdkFsTools<C = unknown> {
68
73
  export interface AiSdkFsToolsWithBash<C = unknown> extends AiSdkFsTools<C> {
69
74
  readonly bash: AiSdkBashTool<C>;
70
75
  }
76
+ /** The createAiSdkFsTools result when `applyPatch` is true or an object: it is never null. */
77
+ export interface AiSdkFsToolsWithApplyPatch<C = unknown> extends AiSdkFsTools<C> {
78
+ readonly applyPatch: AiSdkMutationTool<C>;
79
+ }
71
80
  /**
72
- * Creates AI SDK read, edit, write, and apply_patch tools over one backend,
73
- * sharing one read state store, digest, lock manager, and clock. Needs no
74
- * Node module, so it runs in a Worker. With `bash`, also creates a bash tool
75
- * with the same digest and clock.
81
+ * Creates AI SDK read, edit, and write tools over one backend, sharing one
82
+ * digest, lock manager, and clock, and the read store when `state` is given.
83
+ * read returns an image as a file part unless `read.converters` is set.
84
+ * Needs no Node module, so it runs in a Worker. With `applyPatch`, also
85
+ * creates an apply_patch tool. With `bash`, also creates a bash tool with the
86
+ * same digest and clock.
76
87
  *
77
88
  * Throws TypeError on an unknown option key, and on a shared key (fs, state,
78
89
  * digest, locks, clock) inside a tool's options.
79
90
  */
91
+ export declare function createAiSdkFsTools<C = unknown>(options: CreateAiSdkFsToolsOptions<C> & {
92
+ readonly bash: AiSdkFsToolsBashOptions<C>;
93
+ readonly applyPatch: true | AiSdkFsToolsApplyPatchOptions<C>;
94
+ }): AiSdkFsToolsWithBash<C> & AiSdkFsToolsWithApplyPatch<C>;
80
95
  export declare function createAiSdkFsTools<C = unknown>(options: CreateAiSdkFsToolsOptions<C> & {
81
96
  readonly bash: AiSdkFsToolsBashOptions<C>;
82
97
  }): AiSdkFsToolsWithBash<C>;
98
+ export declare function createAiSdkFsTools<C = unknown>(options: CreateAiSdkFsToolsOptions<C> & {
99
+ readonly applyPatch: true | AiSdkFsToolsApplyPatchOptions<C>;
100
+ }): AiSdkFsToolsWithApplyPatch<C>;
83
101
  export declare function createAiSdkFsTools<C = unknown>(options: CreateAiSdkFsToolsOptions<C>): AiSdkFsTools<C>;
84
102
  export {};
package/dist/fs-tools.js CHANGED
@@ -4,7 +4,7 @@ import { createFsTools, parseApplyPatchInput, parseEditInput, parseWriteInput, }
4
4
  import { defaultEditSignature, defaultPatchSignature, defaultWriteSignature, writeSignatureMessages, } from "@better-fs-tools/write/signature";
5
5
  import { adaptBashTool, defaultAiSdkBashSignature } from "./bash-tool.js";
6
6
  import { adaptMutationTool, matchersOf } from "./mutation-tools.js";
7
- import { adaptReadTool } from "./tool.js";
7
+ import { adaptReadTool, aiSdkConverters } from "./tool.js";
8
8
  const KNOWN = new Set([
9
9
  "fs",
10
10
  "state",
@@ -30,7 +30,12 @@ export function createAiSdkFsTools(options) {
30
30
  const { signature: editGiven, ...edit } = options.edit ?? {};
31
31
  const editSignature = editGiven ?? defaultEditSignature(matchersOf(edit.matchers));
32
32
  const { signature: writeSignature = defaultWriteSignature(), ...write } = options.write ?? {};
33
- const { signature: patchSignature = defaultPatchSignature(), ...applyPatch } = options.applyPatch ?? {};
33
+ const patchOptions = options.applyPatch === true ? {} : options.applyPatch === false ? null : options.applyPatch;
34
+ let patch = null;
35
+ if (patchOptions !== null && patchOptions !== undefined) {
36
+ const { signature = defaultPatchSignature(), ...deps } = patchOptions;
37
+ patch = { deps, signature };
38
+ }
34
39
  const bashOptions = options.bash === undefined || options.bash === false ? null : options.bash;
35
40
  let bash = null;
36
41
  if (bashOptions !== null) {
@@ -43,16 +48,24 @@ export function createAiSdkFsTools(options) {
43
48
  ...(options.digest === undefined ? {} : { digest: options.digest }),
44
49
  ...(options.locks === undefined ? {} : { locks: options.locks }),
45
50
  ...(options.clock === undefined ? {} : { clock: options.clock }),
46
- read: { ...read, messages: { ...readSignatureMessages(readSignature), ...read.messages } },
51
+ read: {
52
+ ...read,
53
+ converters: aiSdkConverters(read.converters),
54
+ messages: { ...readSignatureMessages(readSignature), ...read.messages },
55
+ },
47
56
  edit: { ...edit, messages: { ...writeSignatureMessages(editSignature), ...edit.messages } },
48
57
  write: {
49
58
  ...write,
50
59
  messages: { ...writeSignatureMessages(writeSignature), ...write.messages },
51
60
  },
52
- applyPatch: {
53
- ...applyPatch,
54
- messages: { ...writeSignatureMessages(patchSignature), ...applyPatch.messages },
55
- },
61
+ ...(patch === null
62
+ ? {}
63
+ : {
64
+ applyPatch: {
65
+ ...patch.deps,
66
+ messages: { ...writeSignatureMessages(patch.signature), ...patch.deps.messages },
67
+ },
68
+ }),
56
69
  ...(bash === null
57
70
  ? {}
58
71
  : {
@@ -66,7 +79,9 @@ export function createAiSdkFsTools(options) {
66
79
  read: adaptReadTool(readSignature, core.read, read.limits),
67
80
  edit: adaptMutationTool(editSignature, core.edit, parseEditInput, edit.limits),
68
81
  write: adaptMutationTool(writeSignature, core.write, parseWriteInput, write.limits),
69
- applyPatch: adaptMutationTool(patchSignature, core.applyPatch, parseApplyPatchInput, applyPatch.limits),
82
+ applyPatch: patch === null || core.applyPatch === null
83
+ ? null
84
+ : adaptMutationTool(patch.signature, core.applyPatch, parseApplyPatchInput, patch.deps.limits),
70
85
  bash: bash === null || core.bash === null
71
86
  ? null
72
87
  : adaptBashTool(bash.signature, core.bash, bash.deps.limits),
@@ -106,6 +121,8 @@ function checkOptions(options) {
106
121
  const part = options[tool];
107
122
  if (part === undefined || (tool === "bash" && part === false))
108
123
  continue;
124
+ if (tool === "applyPatch" && typeof part === "boolean")
125
+ continue;
109
126
  if (tool === "bash" && part === true) {
110
127
  throw new TypeError("createAiSdkFsTools bash must be an object with a runner and an env: the portable bundle has no default runner");
111
128
  }
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  export { createAiSdkBashTool } from "./bash-tool.ts";
2
2
  export type { AiSdkBashTool, CreateAiSdkBashToolOptions } from "./bash-tool.ts";
3
3
  export { createAiSdkFsTools } from "./fs-tools.ts";
4
- export type { AiSdkFsTools, AiSdkFsToolsApplyPatchOptions, AiSdkFsToolsBashOptions, AiSdkFsToolsEditOptions, AiSdkFsToolsReadOptions, AiSdkFsToolsWithBash, AiSdkFsToolsWriteOptions, CreateAiSdkFsToolsOptions, } from "./fs-tools.ts";
4
+ export type { AiSdkFsTools, AiSdkFsToolsApplyPatchOptions, AiSdkFsToolsBashOptions, AiSdkFsToolsEditOptions, AiSdkFsToolsReadOptions, AiSdkFsToolsWithApplyPatch, AiSdkFsToolsWithBash, AiSdkFsToolsWriteOptions, CreateAiSdkFsToolsOptions, } from "./fs-tools.ts";
5
5
  export { createAiSdkApplyPatchTool, createAiSdkEditTool, createAiSdkWriteTool, } from "./mutation-tools.ts";
6
6
  export type { AiSdkMutationTool, CreateAiSdkApplyPatchToolOptions, CreateAiSdkEditToolOptions, CreateAiSdkWriteToolOptions, } from "./mutation-tools.ts";
7
7
  export { toAiSdkOutput } from "./output.ts";
package/dist/tool.d.ts CHANGED
@@ -1,8 +1,12 @@
1
1
  import type { Schema, ToolExecutionOptions } from "ai";
2
- import type { JsonObject, ReadLimits, ReadResult, ReadTool, ReadToolDeps, StateNeedsDigest } from "@better-fs-tools/read";
2
+ import type { Converter, JsonObject, ReadLimits, ReadResult, ReadTool, ReadToolDeps, StateNeedsDigest } from "@better-fs-tools/read";
3
3
  import type { ReadSignature } from "@better-fs-tools/read/signature";
4
4
  import type { AiSdkToolOutput } from "./output.ts";
5
- /** Options for createAiSdkReadTool. A state store requires a digest. */
5
+ /**
6
+ * Options for createAiSdkReadTool. A state store requires a digest.
7
+ * converters defaults to [imageConverter()]. A list you pass replaces it:
8
+ * [] turns images off.
9
+ */
6
10
  export type CreateAiSdkReadToolOptions<C = unknown> = ReadToolDeps<ToolExecutionOptions<C>> & StateNeedsDigest & {
7
11
  /** Defaults to defaultReadSignature(). */
8
12
  readonly signature?: ReadSignature;
@@ -20,6 +24,12 @@ export interface AiSdkReadTool<C = unknown> {
20
24
  output: ReadResult;
21
25
  }): AiSdkToolOutput;
22
26
  }
23
- /** Creates an AI SDK read tool. `options.messages` override the signature's messages. */
27
+ /**
28
+ * Creates an AI SDK read tool. `options.messages` override the signature's
29
+ * messages. An image becomes a file part unless `options.converters` leaves
30
+ * imageConverter() out.
31
+ */
24
32
  export declare function createAiSdkReadTool<C = unknown>(options: CreateAiSdkReadToolOptions<C>): AiSdkReadTool<C>;
33
+ /** The host's converters, or [imageConverter()] when it passes none. */
34
+ export declare function aiSdkConverters<THost>(converters: readonly Converter<THost>[] | undefined): readonly Converter<THost>[];
25
35
  export declare function adaptReadTool<C>(signature: ReadSignature, read: ReadTool<ToolExecutionOptions<C>>, limitOverrides: Partial<ReadLimits> | undefined): AiSdkReadTool<C>;
package/dist/tool.js CHANGED
@@ -1,9 +1,13 @@
1
1
  import { jsonSchema } from "ai";
2
- import { createReadTool, parseReadInput, resolveReadLimits } from "@better-fs-tools/read";
2
+ import { createReadTool, imageConverter, parseReadInput, resolveReadLimits, } from "@better-fs-tools/read";
3
3
  import { defaultReadSignature, readSignatureMessages } from "@better-fs-tools/read/signature";
4
4
  import { toAiSdkOutput } from "./output.js";
5
5
  import { fromStrictInput, toStrictSchema } from "./strict.js";
6
- /** Creates an AI SDK read tool. `options.messages` override the signature's messages. */
6
+ /**
7
+ * Creates an AI SDK read tool. `options.messages` override the signature's
8
+ * messages. An image becomes a file part unless `options.converters` leaves
9
+ * imageConverter() out.
10
+ */
7
11
  export function createAiSdkReadTool(options) {
8
12
  if (options === null || typeof options !== "object" || Array.isArray(options)) {
9
13
  throw new TypeError("AI SDK read tool options must be an object");
@@ -11,10 +15,15 @@ export function createAiSdkReadTool(options) {
11
15
  const { signature = defaultReadSignature(), ...deps } = options;
12
16
  const read = createReadTool({
13
17
  ...deps,
18
+ converters: aiSdkConverters(deps.converters),
14
19
  messages: { ...readSignatureMessages(signature), ...deps.messages },
15
20
  });
16
21
  return adaptReadTool(signature, read, deps.limits);
17
22
  }
23
+ /** The host's converters, or [imageConverter()] when it passes none. */
24
+ export function aiSdkConverters(converters) {
25
+ return converters ?? [imageConverter()];
26
+ }
18
27
  export function adaptReadTool(signature, read, limitOverrides) {
19
28
  const limits = resolveReadLimits(limitOverrides);
20
29
  const strict = toStrictSchema(signature.schema, signature.name);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@better-fs-tools/ai-sdk",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "The Better FS Tools read, edit, write, apply_patch, and bash tools for AI SDK 7.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -29,14 +29,14 @@
29
29
  "access": "public"
30
30
  },
31
31
  "dependencies": {
32
- "@better-fs-tools/read": "^0.1.0",
33
- "@better-fs-tools/shell": "^0.1.0",
34
- "@better-fs-tools/write": "^0.1.0"
32
+ "@better-fs-tools/read": "^0.2.0",
33
+ "@better-fs-tools/shell": "^0.2.0",
34
+ "@better-fs-tools/write": "^0.2.0"
35
35
  },
36
36
  "devDependencies": {
37
37
  "@ai-sdk/openai": "4.0.78",
38
- "@better-fs-tools/fs": "^0.1.0",
39
- "@better-fs-tools/just-bash": "^0.1.0",
38
+ "@better-fs-tools/fs": "^0.2.0",
39
+ "@better-fs-tools/just-bash": "^0.2.0",
40
40
  "ai": "^7.0.77",
41
41
  "just-bash": "3.4.2"
42
42
  },