Open History to see the last commit
1import assert from 'node:assert/strict'
2import {
3 LIVE_RECIPE_SERVER_ERROR,
4 app,
5 createRecipePrompt,
6} from '../src/server/app'
7
8type SmokeMessage = {
9 id: string
10 role: 'user' | 'assistant'
11 content: string
12}
13
14type SmokeRequestBody = {
15 threadId: string
16 runId: string
17 state: object
18 messages: Array<SmokeMessage>
19 tools: Array<never>
20 context: Array<never>
21 forwardedProps: object
22}
23
24type SmokeChunk = {
25 type: string
26 name?: string
27 delta?: string
28 message?: string
29 value?: unknown
30}
31
32function createRequestBody({
33 threadId,
34 runId,
35 messages,
36}: {
37 threadId: string
38 runId: string
39 messages: Array<SmokeMessage>
40}): SmokeRequestBody {
41 return {
42 threadId,
43 runId,
44 state: {},
45 messages,
46 tools: [],
47 context: [],
48 forwardedProps: {},
49 }
50}
51
52const followUpRequestBody = createRequestBody({
53 threadId: 'thread-smoke-live-follow-up',
54 runId: 'run-smoke-live-follow-up',
55 messages: [
56 {
57 id: 'message-smoke-live-follow-up-1',
58 role: 'user',
59 content: '15-minute veggie dinner from my pantry.',
60 },
61 {
62 id: 'message-smoke-live-follow-up-2',
63 role: 'assistant',
64 content: 'I can revise the prior recipe using your next instruction.',
65 },
66 {
67 id: 'message-smoke-live-follow-up-3',
68 role: 'user',
69 content: 'Make it vegan and remove mushrooms. Serve 4 people.',
70 },
71 ],
72})
73
74function assertNoFixtureRecipe(body: string) {
75 assert.ok(!body.includes('Pantry Tomato Pasta'))
76 assert.ok(!body.includes('15-Minute Veggie Pantry Skillet'))
77 assert.ok(!body.includes('High-Protein Pantry Breakfast Skillet'))
78 assert.ok(!body.includes('Kid-Friendly 15-Minute Veggie Pantry Skillet'))
79 assert.ok(!body.includes('"revision":1'))
80}
81
82function readMessageContent(
83 message: (typeof followUpRequestBody.messages)[number],
84) {
85 return message.content
86}
87
88function isSmokeChunk(value: unknown): value is SmokeChunk {
89 if (!value || typeof value !== 'object') return false
90 if (!('type' in value)) return false
91 return typeof value.type === 'string'
92}
93
94function isRunErrorChunk(value: unknown): value is SmokeChunk & {
95 type: 'RUN_ERROR'
96 message: string
97} {
98 return (
99 isSmokeChunk(value) &&
100 value.type === 'RUN_ERROR' &&
101 typeof value.message === 'string'
102 )
103}
104
105function parseJsonLine(line: string): SmokeChunk {
106 const parsed: unknown = JSON.parse(line)
107 assert.ok(isSmokeChunk(parsed), `expected stream chunk in line: ${line}`)
108 return parsed
109}
110
111function parseSseBody(body: string) {
112 return body
113 .split('\n')
114 .filter((line) => line.startsWith('data:'))
115 .map((line) => line.slice('data:'.length).trim())
116 .filter((line) => line && line !== '[DONE]')
117 .map(parseJsonLine)
118}
119
120async function readErrorChunks(
121 path: '/chat/http' | '/chat/sse',
122 response: Response,
123) {
124 const body = await response.text()
125 if (path === '/chat/sse') return parseSseBody(body)
126
127 return body.trim().split('\n').filter(Boolean).map(parseJsonLine)
128}
129
130async function assertMissingKeyResponse(path: '/chat/http' | '/chat/sse') {
131 const originalApiKey = process.env.OPENAI_API_KEY
132 delete process.env.OPENAI_API_KEY
133
134 try {
135 const response = await app.request(path, {
136 method: 'POST',
137 headers: { 'Content-Type': 'application/json' },
138 body: JSON.stringify(followUpRequestBody),
139 })
140 const chunks = await readErrorChunks(path, response)
141 const error = chunks.find(isRunErrorChunk)
142
143 assert.equal(response.status, 200)
144 assert.ok(error)
145 assert.match(error.message, new RegExp(LIVE_RECIPE_SERVER_ERROR))
146 assertNoFixtureRecipe(JSON.stringify(chunks))
147 } finally {
148 if (originalApiKey !== undefined) {
149 process.env.OPENAI_API_KEY = originalApiKey
150 }
151 }
152}
153
154async function readJsonLines(response: Response) {
155 const body = await response.text()
156 return body.trim().split('\n').filter(Boolean).map(parseJsonLine)
157}
158
159async function maybeAssertLiveOpenAIStream() {
160 if (!process.env.OPENAI_API_KEY) return
161
162 const response = await app.request('/chat/http', {
163 method: 'POST',
164 headers: { 'Content-Type': 'application/json' },
165 body: JSON.stringify(followUpRequestBody),
166 })
167
168 assert.equal(response.status, 200)
169 assert.equal(response.headers.get('content-type'), 'application/x-ndjson')
170
171 const chunks = await readJsonLines(response)
172 const text = JSON.stringify(chunks)
173 assert.ok(chunks.some((chunk) => chunk.type === 'RUN_STARTED'))
174 assert.ok(chunks.some((chunk) => chunk.type === 'TEXT_MESSAGE_CONTENT'))
175 assert.ok(chunks.some((chunk) => chunk.type === 'RUN_FINISHED'))
176 assert.match(text, /structured-output\.complete|TEXT_MESSAGE_CONTENT/)
177 assertNoFixtureRecipe(text)
178}
179
180async function main() {
181 const health = await app.request('/health')
182 assert.equal(health.status, 200)
183 assert.deepEqual(await health.json(), { ok: true })
184
185 const recipePrompt = createRecipePrompt(followUpRequestBody.messages)
186 assert.equal(recipePrompt.messages.at(-1)?.role, 'user')
187 const latestMessage = followUpRequestBody.messages.at(-1)
188 assert.ok(latestMessage)
189 assert.equal(
190 readMessageContent(latestMessage),
191 'Make it vegan and remove mushrooms. Serve 4 people.',
192 )
193 assert.ok(
194 followUpRequestBody.messages.some(
195 (message) =>
196 message.role === 'assistant' && message.content.includes('revise'),
197 ),
198 'prompt preserves submitted assistant history',
199 )
200 assert.match(recipePrompt.systemPrompts[0] ?? '', /revise/i)
201 assert.match(recipePrompt.systemPrompts[0] ?? '', /conversation history/i)
202
203 await assertMissingKeyResponse('/chat/http')
204 await assertMissingKeyResponse('/chat/sse')
205 await maybeAssertLiveOpenAIStream()
206}
207
208await main()