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

create-system-prompt.test.ts

151 lines4.9 KB

1import { describe, expect, it } from 'vitest'
2import { z } from 'zod'
3import { toolDefinition } from '@tanstack/ai'
4import { createCodeModeSystemPrompt } from '../src/create-system-prompt'
5import type { IsolateDriver } from '../src/types'
6​
7function createMockTool(name: string, description = `The ${name} tool`) {
8 const def = toolDefinition({
9 name: name as any,
10 description,
11 inputSchema: z.object({ query: z.string() }),
12 outputSchema: z.object({ result: z.string() }),
13 })
14 return def.server(async (input: any) => ({ result: input.query }))
16​
17const mockDriver: IsolateDriver = {
18 createContext: async () => ({
19 execute: async () => ({ success: true }),
20 dispose: async () => {},
21 }),
23​
24describe('createCodeModeSystemPrompt', () => {
25 it('output mentions execute_typescript', () => {
26 const prompt = createCodeModeSystemPrompt({
27 driver: mockDriver,
28 tools: [createMockTool('fetchWeather')],
29 })
30 expect(prompt).toContain('execute_typescript')
31 })
32​
33 it('all tool names appear with external_ prefix', () => {
34 const prompt = createCodeModeSystemPrompt({
35 driver: mockDriver,
36 tools: [createMockTool('fetchWeather'), createMockTool('dbQuery')],
37 })
38 expect(prompt).toContain('external_fetchWeather')
39 expect(prompt).toContain('external_dbQuery')
40 })
41​
42 it('contains TypeScript type stub code block', () => {
43 const prompt = createCodeModeSystemPrompt({
44 driver: mockDriver,
45 tools: [createMockTool('fetchWeather')],
46 })
47 expect(prompt).toContain('```typescript')
48 expect(prompt).toContain('declare function external_fetchWeather')
49 })
50​
51 it('contains expected sections', () => {
52 const prompt = createCodeModeSystemPrompt({
53 driver: mockDriver,
54 tools: [createMockTool('fetchWeather')],
55 })
56 expect(prompt).toContain('When to Use')
57 expect(prompt).toContain('Available External APIs')
58 expect(prompt).toContain('Important Notes')
59 expect(prompt).toContain('`{ success, result, logs, error }`')
60 })
61​
62 it('documents multiple tools', () => {
63 const prompt = createCodeModeSystemPrompt({
64 driver: mockDriver,
65 tools: [
66 createMockTool('fetchWeather', 'Get weather data'),
67 createMockTool('searchDB', 'Search the database'),
68 ],
69 })
70 expect(prompt).toContain('external_fetchWeather')
71 expect(prompt).toContain('external_searchDB')
72 expect(prompt).toContain('Get weather data')
73 expect(prompt).toContain('Search the database')
74 })
75})
76​
77const eagerTool = toolDefinition({
78 name: 'fetchWeather',
79 description: 'Get current weather. Returns temperature.',
80 inputSchema: z.object({ city: z.string() }),
81 outputSchema: z.object({ temp: z.number() }),
82}).server(async () => ({ temp: 0 }))
83​
84const lazyTool = toolDefinition({
85 name: 'fetchStocks',
86 description: 'Get stock prices. Returns quotes.',
87 inputSchema: z.object({ ticker: z.string() }),
88 outputSchema: z.object({ price: z.number() }),
89 lazy: true,
90}).server(async () => ({ price: 0 }))
91​
92const driverStub = {
93 createContext: async () => ({
94 execute: async () => ({ success: true }),
95 dispose: async () => {},
96 }),
98​
99describe('createCodeModeSystemPrompt — lazy tools', () => {
100 it('documents eager tools with full type stubs', () => {
101 const prompt = createCodeModeSystemPrompt({
102 driver: driverStub,
103 tools: [eagerTool, lazyTool],
104 })
105 expect(prompt).toContain('declare function external_fetchWeather')
106 })
107​
108 it('does NOT emit type stubs for lazy tools', () => {
109 const prompt = createCodeModeSystemPrompt({
110 driver: driverStub,
111 tools: [eagerTool, lazyTool],
112 })
113 expect(prompt).not.toContain('declare function external_fetchStocks')
114 })
115​
116 it('lists lazy tools in a Discoverable APIs section (names only by default)', () => {
117 const prompt = createCodeModeSystemPrompt({
118 driver: driverStub,
119 tools: [eagerTool, lazyTool],
120 })
121 expect(prompt).toContain('Discoverable APIs')
122 expect(prompt).toContain('discover_tools')
123 expect(prompt).toContain('external_fetchStocks')
124 expect(prompt).not.toContain('external_fetchStocks — Get stock prices.')
125 })
126​
127 it('says discover_tools is a separate tool, not a sandbox function', () => {
128 const prompt = createCodeModeSystemPrompt({
129 driver: driverStub,
130 tools: [eagerTool, lazyTool],
131 })
132 expect(prompt).toContain('not available inside `execute_typescript`')
133 })
134​
135 it('includes first sentences in the catalog when configured', () => {
136 const prompt = createCodeModeSystemPrompt({
137 driver: driverStub,
138 tools: [eagerTool, lazyTool],
139 lazyToolsConfig: { includeDescription: 'first-sentence' },
140 })
141 expect(prompt).toContain('external_fetchStocks — Get stock prices.')
142 })
143​
144 it('omits the Discoverable APIs section when there are no lazy tools', () => {
145 const prompt = createCodeModeSystemPrompt({
146 driver: driverStub,
147 tools: [eagerTool],
148 })
149 expect(prompt).not.toContain('Discoverable APIs')
150 })
151})