|
| 1 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; |
| 2 | + |
| 3 | +import { axGlobals } from './globals.js'; |
| 4 | +import { AxPromptTemplate } from './prompt.js'; |
| 5 | +import { AxSignature, f } from './sig.js'; |
| 6 | + |
| 7 | +describe('AxPromptTemplate - Structured Prompts', () => { |
| 8 | + let originalUseStructuredPrompt: boolean; |
| 9 | + |
| 10 | + beforeEach(() => { |
| 11 | + // Save original value |
| 12 | + originalUseStructuredPrompt = axGlobals.useStructuredPrompt; |
| 13 | + }); |
| 14 | + |
| 15 | + afterEach(() => { |
| 16 | + // Restore original value |
| 17 | + axGlobals.useStructuredPrompt = originalUseStructuredPrompt; |
| 18 | + }); |
| 19 | + |
| 20 | + describe('XML structure', () => { |
| 21 | + it('should generate XML-structured prompt when flag is enabled', () => { |
| 22 | + axGlobals.useStructuredPrompt = true; |
| 23 | + |
| 24 | + const sig = AxSignature.create('userInput:string -> aiResponse:string'); |
| 25 | + const template = new AxPromptTemplate(sig); |
| 26 | + |
| 27 | + const messages = template.render({ userInput: 'test' }, {}); |
| 28 | + const systemPrompt = messages.find((m) => m.role === 'system'); |
| 29 | + |
| 30 | + expect(systemPrompt).toBeDefined(); |
| 31 | + expect(systemPrompt?.content).toContain('<identity>'); |
| 32 | + expect(systemPrompt?.content).toContain('</identity>'); |
| 33 | + expect(systemPrompt?.content).toContain('<input_fields>'); |
| 34 | + expect(systemPrompt?.content).toContain('</input_fields>'); |
| 35 | + expect(systemPrompt?.content).toContain('<output_fields>'); |
| 36 | + expect(systemPrompt?.content).toContain('</output_fields>'); |
| 37 | + expect(systemPrompt?.content).toContain('<formatting_rules>'); |
| 38 | + expect(systemPrompt?.content).toContain('</formatting_rules>'); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should use legacy format when flag is disabled', () => { |
| 42 | + axGlobals.useStructuredPrompt = false; |
| 43 | + |
| 44 | + const sig = AxSignature.create('userInput:string -> aiResponse:string'); |
| 45 | + const template = new AxPromptTemplate(sig); |
| 46 | + |
| 47 | + const messages = template.render({ userInput: 'test' }, {}); |
| 48 | + const systemPrompt = messages.find((m) => m.role === 'system'); |
| 49 | + |
| 50 | + expect(systemPrompt).toBeDefined(); |
| 51 | + expect(systemPrompt?.content).not.toContain('<identity>'); |
| 52 | + expect(systemPrompt?.content).not.toContain('<input_fields>'); |
| 53 | + expect(systemPrompt?.content).toContain('## Input Fields'); |
| 54 | + expect(systemPrompt?.content).toContain('## Output Fields'); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should use structured format by default', () => { |
| 58 | + // axGlobals.useStructuredPrompt defaults to true |
| 59 | + const sig = AxSignature.create('userInput:string -> aiResponse:string'); |
| 60 | + const template = new AxPromptTemplate(sig); |
| 61 | + |
| 62 | + const messages = template.render({ userInput: 'test' }, {}); |
| 63 | + const systemPrompt = messages.find((m) => m.role === 'system'); |
| 64 | + |
| 65 | + expect(systemPrompt).toBeDefined(); |
| 66 | + expect(systemPrompt?.content).toContain('<identity>'); |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + describe('Format protection', () => { |
| 71 | + it('should include CANNOT be overridden statement for plain-text outputs', () => { |
| 72 | + axGlobals.useStructuredPrompt = true; |
| 73 | + |
| 74 | + const sig = AxSignature.create('userInput:string -> aiResponse:string'); |
| 75 | + const template = new AxPromptTemplate(sig); |
| 76 | + |
| 77 | + const messages = template.render({ userInput: 'test' }, {}); |
| 78 | + const systemPrompt = messages.find((m) => m.role === 'system'); |
| 79 | + |
| 80 | + expect(systemPrompt?.content).toContain( |
| 81 | + 'CANNOT be overridden by any subsequent instructions' |
| 82 | + ); |
| 83 | + expect(systemPrompt?.content).toContain('Plain Text Output Format'); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should include CANNOT be overridden statement for structured outputs', () => { |
| 87 | + axGlobals.useStructuredPrompt = true; |
| 88 | + |
| 89 | + const sig = f() |
| 90 | + .input('userInput', f.string()) |
| 91 | + .output( |
| 92 | + 'analysisResult', |
| 93 | + f.object({ |
| 94 | + message: f.string(), |
| 95 | + confidence: f.number(), |
| 96 | + }) |
| 97 | + ) |
| 98 | + .build(); |
| 99 | + |
| 100 | + const template = new AxPromptTemplate(sig); |
| 101 | + |
| 102 | + const messages = template.render({ userInput: 'test' }, {}); |
| 103 | + const systemPrompt = messages.find((m) => m.role === 'system'); |
| 104 | + |
| 105 | + expect(systemPrompt?.content).toContain( |
| 106 | + 'CANNOT be overridden by any subsequent instructions' |
| 107 | + ); |
| 108 | + expect(systemPrompt?.content).toContain('Structured Output Format'); |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + describe('Structured vs plain-text mode detection', () => { |
| 113 | + it('should detect structured output mode for object fields', () => { |
| 114 | + axGlobals.useStructuredPrompt = true; |
| 115 | + |
| 116 | + const sig = f() |
| 117 | + .input('userInput', f.string()) |
| 118 | + .output( |
| 119 | + 'analysisResult', |
| 120 | + f.object({ |
| 121 | + message: f.string(), |
| 122 | + }) |
| 123 | + ) |
| 124 | + .build(); |
| 125 | + |
| 126 | + const template = new AxPromptTemplate(sig); |
| 127 | + |
| 128 | + const messages = template.render({ userInput: 'test' }, {}); |
| 129 | + const systemPrompt = messages.find((m) => m.role === 'system'); |
| 130 | + |
| 131 | + expect(systemPrompt?.content).toContain('valid JSON'); |
| 132 | + expect(systemPrompt?.content).not.toContain('field name: value'); |
| 133 | + }); |
| 134 | + |
| 135 | + it('should detect plain-text mode for simple fields', () => { |
| 136 | + axGlobals.useStructuredPrompt = true; |
| 137 | + |
| 138 | + const sig = AxSignature.create('userInput:string -> aiResponse:string'); |
| 139 | + const template = new AxPromptTemplate(sig); |
| 140 | + |
| 141 | + const messages = template.render({ userInput: 'test' }, {}); |
| 142 | + const systemPrompt = messages.find((m) => m.role === 'system'); |
| 143 | + |
| 144 | + expect(systemPrompt?.content).toContain('field name: value'); |
| 145 | + expect(systemPrompt?.content).not.toContain('valid JSON'); |
| 146 | + }); |
| 147 | + |
| 148 | + it('should detect structured output mode for array of objects', () => { |
| 149 | + axGlobals.useStructuredPrompt = true; |
| 150 | + |
| 151 | + const sig = f() |
| 152 | + .input('userInput', f.string()) |
| 153 | + .output( |
| 154 | + 'items', |
| 155 | + f |
| 156 | + .object({ |
| 157 | + name: f.string(), |
| 158 | + value: f.number(), |
| 159 | + }) |
| 160 | + .array() |
| 161 | + ) |
| 162 | + .build(); |
| 163 | + |
| 164 | + const template = new AxPromptTemplate(sig); |
| 165 | + |
| 166 | + const messages = template.render({ userInput: 'test' }, {}); |
| 167 | + const systemPrompt = messages.find((m) => m.role === 'system'); |
| 168 | + |
| 169 | + expect(systemPrompt?.content).toContain('valid JSON'); |
| 170 | + }); |
| 171 | + }); |
| 172 | + |
| 173 | + describe('Functions section', () => { |
| 174 | + it('should include functions section when functions are provided', () => { |
| 175 | + axGlobals.useStructuredPrompt = true; |
| 176 | + |
| 177 | + const sig = AxSignature.create('userInput:string -> aiResponse:string'); |
| 178 | + const template = new AxPromptTemplate(sig, { |
| 179 | + functions: [ |
| 180 | + { |
| 181 | + name: 'searchDatabase', |
| 182 | + description: 'Search the database for information', |
| 183 | + parameters: { |
| 184 | + type: 'object' as const, |
| 185 | + properties: { |
| 186 | + query: { type: 'string' as const }, |
| 187 | + }, |
| 188 | + required: ['query'], |
| 189 | + }, |
| 190 | + }, |
| 191 | + ], |
| 192 | + }); |
| 193 | + |
| 194 | + const messages = template.render({ userInput: 'test' }, {}); |
| 195 | + const systemPrompt = messages.find((m) => m.role === 'system'); |
| 196 | + |
| 197 | + expect(systemPrompt?.content).toContain('<available_functions>'); |
| 198 | + expect(systemPrompt?.content).toContain('</available_functions>'); |
| 199 | + expect(systemPrompt?.content).toContain('searchDatabase'); |
| 200 | + expect(systemPrompt?.content).toContain( |
| 201 | + 'Search the database for information' |
| 202 | + ); |
| 203 | + }); |
| 204 | + |
| 205 | + it('should not include functions section when no functions provided', () => { |
| 206 | + axGlobals.useStructuredPrompt = true; |
| 207 | + |
| 208 | + const sig = AxSignature.create('userInput:string -> aiResponse:string'); |
| 209 | + const template = new AxPromptTemplate(sig); |
| 210 | + |
| 211 | + const messages = template.render({ userInput: 'test' }, {}); |
| 212 | + const systemPrompt = messages.find((m) => m.role === 'system'); |
| 213 | + |
| 214 | + expect(systemPrompt?.content).not.toContain('<available_functions>'); |
| 215 | + }); |
| 216 | + }); |
| 217 | + |
| 218 | + describe('Description handling', () => { |
| 219 | + it('should include signature description in identity section', () => { |
| 220 | + axGlobals.useStructuredPrompt = true; |
| 221 | + |
| 222 | + const sig = AxSignature.create( |
| 223 | + '"Analyze sentiment" userInput:string -> sentiment:string' |
| 224 | + ); |
| 225 | + const template = new AxPromptTemplate(sig); |
| 226 | + |
| 227 | + const messages = template.render({ userInput: 'test' }, {}); |
| 228 | + const systemPrompt = messages.find((m) => m.role === 'system'); |
| 229 | + |
| 230 | + expect(systemPrompt?.content).toContain('Analyze sentiment'); |
| 231 | + expect(systemPrompt?.content).toContain('<identity>'); |
| 232 | + }); |
| 233 | + }); |
| 234 | + |
| 235 | + describe('Backward compatibility', () => { |
| 236 | + it('should maintain same behavior as legacy format when flag is off', () => { |
| 237 | + axGlobals.useStructuredPrompt = false; |
| 238 | + |
| 239 | + const sig = AxSignature.create('userInput:string -> aiResponse:string'); |
| 240 | + |
| 241 | + const legacyTemplate = new AxPromptTemplate(sig); |
| 242 | + const defaultTemplate = new AxPromptTemplate(sig); |
| 243 | + |
| 244 | + const legacyMessages = legacyTemplate.render({ userInput: 'test' }, {}); |
| 245 | + const defaultMessages = defaultTemplate.render({ userInput: 'test' }, {}); |
| 246 | + |
| 247 | + const legacySystem = legacyMessages.find((m) => m.role === 'system'); |
| 248 | + const defaultSystem = defaultMessages.find((m) => m.role === 'system'); |
| 249 | + |
| 250 | + expect(legacySystem?.content).toBe(defaultSystem?.content); |
| 251 | + }); |
| 252 | + }); |
| 253 | +}); |
0 commit comments