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

harness.ts

39 lines1.4 KB

1import { mkdir, readdir, writeFile } from 'node:fs/promises'
2import { join } from 'node:path'
3import { toolDefinition } from '@tanstack/ai'
4import { defineHarness } from '@tanstack/ai-harness'
5import { openaiText } from '@tanstack/ai-openai'
6import { z } from 'zod'
7​
8// The notes go in a `notes` folder where you start the app.
9const NOTES = join(process.cwd(), 'notes')
10​
11const listNotes = toolDefinition({
12 name: 'list_notes',
13 description: 'List the file names of the saved notes',
14 inputSchema: z.object({}),
15}).server(async () => ({ notes: await readdir(NOTES).catch(() => []) }))
16​
17// A write asks the user first, so the screen shows an approval.
18const saveNote = toolDefinition({
19 name: 'save_note',
20 description: 'Save a note as a Markdown file',
21 inputSchema: z.object({ title: z.string(), text: z.string() }),
22 needsApproval: true,
23}).server(async ({ title, text }) => {
24 // The time keeps two notes with the same title apart.
25 const slug = title.toLowerCase().replace(/[^a-z0-9]+/g, '-') || 'note'
26 const file = `${Date.now()}-${slug}.md`
27 await mkdir(NOTES, { recursive: true })
28 await writeFile(join(NOTES, file), text)
29 return { saved: file }
30})
31​
32export const assistant = defineHarness({
33 name: 'example/notes',
34 adapter: openaiText('gpt-6.1-sol'),
35 systemPrompts: [
36 'You help the user write short notes. Save a note only when the user asks.',
37 ],
38 tools: [listNotes, saveNote],
39})