|
| 1 | +import type { ToolSet } from "ai"; |
| 2 | +import { type CreateToolsOptions, resolveToolOptions } from "../common/options.js"; |
| 3 | +import type { PublishWorkspaceLike } from "../common/publish.js"; |
| 4 | +import { |
| 5 | + createDeleteTool, |
| 6 | + createEditTool, |
| 7 | + createExecTool, |
| 8 | + createFindTool, |
| 9 | + createGrepTool, |
| 10 | + createListTool, |
| 11 | + createPublishTool, |
| 12 | + createReadTool, |
| 13 | + createWriteTool, |
| 14 | +} from "./tools.js"; |
| 15 | + |
| 16 | +/** Options for {@link createAITools}. */ |
| 17 | +export type CreateAIToolsOptions = CreateToolsOptions; |
| 18 | + |
| 19 | +export { |
| 20 | + createDeleteTool, |
| 21 | + createEditTool, |
| 22 | + createExecTool, |
| 23 | + createFindTool, |
| 24 | + createGrepTool, |
| 25 | + createListTool, |
| 26 | + createPublishTool, |
| 27 | + createReadTool, |
| 28 | + createWriteTool, |
| 29 | +} from "./tools.js"; |
| 30 | + |
| 31 | +/** |
| 32 | + * Build the AI SDK tool set for a Workspace: `read`, `ls`, `find`, and |
| 33 | + * `grep`, plus `write`, `edit`, `delete`, `exec`, and `publish` unless |
| 34 | + * the set is read-only. `exec` offers every backend the Workspace has |
| 35 | + * unless `exec` picks them. |
| 36 | + * |
| 37 | + * @param options - The Workspace and per-tool options. |
| 38 | + * @returns An AI SDK `ToolSet` for `generateText`, `streamText`, or an agent's `getTools()`. |
| 39 | + */ |
| 40 | +export function createAITools(options: CreateAIToolsOptions): ToolSet { |
| 41 | + const resolved = resolveToolOptions(options); |
| 42 | + const workspace = resolved.workspace; |
| 43 | + |
| 44 | + const tools: ToolSet = { |
| 45 | + read: createReadTool(resolved.read), |
| 46 | + ls: createListTool({ workspace }), |
| 47 | + find: createFindTool({ workspace }), |
| 48 | + grep: createGrepTool({ workspace }), |
| 49 | + }; |
| 50 | + |
| 51 | + if (resolved.readonly) return tools; |
| 52 | + |
| 53 | + tools.write = createWriteTool(resolved.write); |
| 54 | + tools.edit = createEditTool(resolved.edit); |
| 55 | + tools.delete = createDeleteTool(resolved.delete); |
| 56 | + |
| 57 | + if (resolved.exec !== undefined) { |
| 58 | + tools.exec = createExecTool(resolved.exec); |
| 59 | + } |
| 60 | + |
| 61 | + if (resolved.publish) { |
| 62 | + tools.publish = createPublishTool({ workspace: workspace as PublishWorkspaceLike }); |
| 63 | + } |
| 64 | + |
| 65 | + return tools; |
| 66 | +} |
0 commit comments