Open History to see the last commit
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 }))
15}
16
17const mockDriver: IsolateDriver = {
18 createContext: async () => ({
19 execute: async () => ({ success: true }),
20 dispose: async () => {},
21 }),
22}
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 })
60
61 it('documents multiple tools', () => {
62 const prompt = createCodeModeSystemPrompt({
63 driver: mockDriver,
64 tools: [
65 createMockTool('fetchWeather', 'Get weather data'),
66 createMockTool('searchDB', 'Search the database'),
67 ],
68 })
69 expect(prompt).toContain('external_fetchWeather')
70 expect(prompt).toContain('external_searchDB')
71 expect(prompt).toContain('Get weather data')
72 expect(prompt).toContain('Search the database')
73 })
74})
75
76const eagerTool = toolDefinition({
77 name: 'fetchWeather',
78 description: 'Get current weather. Returns temperature.',
79 inputSchema: z.object({ city: z.string() }),
80 outputSchema: z.object({ temp: z.number() }),
81}).server(async () => ({ temp: 0 }))
82
83const lazyTool = toolDefinition({
84 name: 'fetchStocks',
85 description: 'Get stock prices. Returns quotes.',
86 inputSchema: z.object({ ticker: z.string() }),
87 outputSchema: z.object({ price: z.number() }),
88 lazy: true,
89}).server(async () => ({ price: 0 }))
90
91const driverStub = {
92 createContext: async () => ({
93 execute: async () => ({ success: true }),
94 dispose: async () => {},
95 }),
96}
97
98describe('createCodeModeSystemPrompt — lazy tools', () => {
99 it('documents eager tools with full type stubs', () => {
100 const prompt = createCodeModeSystemPrompt({
101 driver: driverStub,
102 tools: [eagerTool, lazyTool],
103 })
104 expect(prompt).toContain('declare function external_fetchWeather')
105 })
106
107 it('does NOT emit type stubs for lazy tools', () => {
108 const prompt = createCodeModeSystemPrompt({
109 driver: driverStub,
110 tools: [eagerTool, lazyTool],
111 })
112 expect(prompt).not.toContain('declare function external_fetchStocks')
113 })
114
115 it('lists lazy tools in a Discoverable APIs section (names only by default)', () => {
116 const prompt = createCodeModeSystemPrompt({
117 driver: driverStub,
118 tools: [eagerTool, lazyTool],
119 })
120 expect(prompt).toContain('Discoverable APIs')
121 expect(prompt).toContain('discover_tools')
122 expect(prompt).toContain('external_fetchStocks')
123 expect(prompt).not.toContain('external_fetchStocks — Get stock prices.')
124 })
125
126 it('says discover_tools is a separate tool, not a sandbox function', () => {
127 const prompt = createCodeModeSystemPrompt({
128 driver: driverStub,
129 tools: [eagerTool, lazyTool],
130 })
131 expect(prompt).toContain('not available inside `execute_typescript`')
132 })
133
134 it('includes first sentences in the catalog when configured', () => {
135 const prompt = createCodeModeSystemPrompt({
136 driver: driverStub,
137 tools: [eagerTool, lazyTool],
138 lazyToolsConfig: { includeDescription: 'first-sentence' },
139 })
140 expect(prompt).toContain('external_fetchStocks — Get stock prices.')
141 })
142
143 it('omits the Discoverable APIs section when there are no lazy tools', () => {
144 const prompt = createCodeModeSystemPrompt({
145 driver: driverStub,
146 tools: [eagerTool],
147 })
148 expect(prompt).not.toContain('Discoverable APIs')
149 })
150})