Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

tools.ts

76 lines2.0 KB

1import { toolDefinition } from '@tanstack/ai'
2import { readWorktreeFile, writeWorktreeFile } from './files'
3​
4export type ReviewToolsContext = {
5 worktreeRoot: string
6}
7​
8const okSchema = {
9 type: 'object',
10 properties: { ok: { type: 'boolean' } },
11 required: ['ok'],
13​
14function isRecord(value: unknown): value is Record<string, unknown> {
15 return typeof value === 'object' && value !== null && !Array.isArray(value)
17​
18function readString(args: unknown, key: string) {
19 if (!isRecord(args)) throw new Error('tool input must be an object')
20 const value = args[key]
21 if (typeof value !== 'string') throw new Error(`tool input is missing ${key}`)
22 return value
24​
25/**
26 * File tools for the review agent. Comment, label, and verdict stay in the host.
27 *
28 * Pass the array to `chat({ tools })`.
29 *
30 * @param ctx worktree root for read/edit
31 */
32export function createReviewTools(ctx: ReviewToolsContext) {
33 const readFile = toolDefinition({
34 name: 'read_file',
35 description: 'Read a UTF-8 file under the worktree root.',
36 inputSchema: {
37 type: 'object',
38 properties: { path: { type: 'string' } },
39 required: ['path'],
40 },
41 outputSchema: {
42 type: 'object',
43 properties: { content: { type: 'string' } },
44 required: ['content'],
45 },
46 }).server(async (args) => {
47 const content = await readWorktreeFile(
48 ctx.worktreeRoot,
49 readString(args, 'path'),
50 )
51 return { content }
52 })
53​
54 const editFile = toolDefinition({
55 name: 'edit_file',
56 description: 'Write a UTF-8 file under the worktree root.',
57 inputSchema: {
58 type: 'object',
59 properties: {
60 path: { type: 'string' },
61 content: { type: 'string' },
62 },
63 required: ['path', 'content'],
64 },
65 outputSchema: okSchema,
66 }).server(async (args) => {
67 await writeWorktreeFile(
68 ctx.worktreeRoot,
69 readString(args, 'path'),
70 readString(args, 'content'),
71 )
72 return { ok: true }
73 })
74​
75 return [readFile, editFile]