|
6 | 6 | classifyAssistantFailoverReason, |
7 | 7 | formatUserFacingAssistantErrorText, |
8 | 8 | } from "./embedded-agent-helpers.js"; |
| 9 | +import { streamWithIdleTimeout } from "./embedded-agent-runner/run/llm-idle-timeout.js"; |
9 | 10 | import { |
10 | 11 | parseTransportChunkUsage, |
11 | 12 | type CapturedStreamEvent, |
@@ -181,6 +182,111 @@ describe("openai transport stream", () => { |
181 | 182 | } |
182 | 183 | }); |
183 | 184 |
|
| 185 | + it.each(["reasoning_content", "reasoning"] as const)( |
| 186 | + "keeps hidden local %s streams alive beyond the model idle timeout", |
| 187 | + async (reasoningField) => { |
| 188 | + const reasoningChunkCount = 5; |
| 189 | + const reasoningChunkDelayMs = 35; |
| 190 | + const idleTimeoutMs = 100; |
| 191 | + const server = createServer((req, res) => { |
| 192 | + req.resume(); |
| 193 | + req.on("end", () => { |
| 194 | + res.writeHead(200, { |
| 195 | + "content-type": "text/event-stream; charset=utf-8", |
| 196 | + "cache-control": "no-cache", |
| 197 | + connection: "keep-alive", |
| 198 | + }); |
| 199 | + |
| 200 | + let reasoningChunksSent = 0; |
| 201 | + const writeNextChunk = () => { |
| 202 | + if (res.destroyed) { |
| 203 | + return; |
| 204 | + } |
| 205 | + if (reasoningChunksSent < reasoningChunkCount) { |
| 206 | + reasoningChunksSent += 1; |
| 207 | + const reasoningChunk = { |
| 208 | + id: "chatcmpl-local-reasoning", |
| 209 | + object: "chat.completion.chunk", |
| 210 | + created: 1, |
| 211 | + model: "nemotron-local", |
| 212 | + choices: [ |
| 213 | + { |
| 214 | + index: 0, |
| 215 | + delta: { [reasoningField]: "private reasoning" }, |
| 216 | + finish_reason: null, |
| 217 | + }, |
| 218 | + ], |
| 219 | + }; |
| 220 | + res.write(`data: ${JSON.stringify(reasoningChunk)}\n\n`); |
| 221 | + setTimeout(writeNextChunk, reasoningChunkDelayMs); |
| 222 | + return; |
| 223 | + } |
| 224 | + |
| 225 | + res.write( |
| 226 | + `data: ${JSON.stringify(makeCompletionsChunk({ role: "assistant", content: "OK" }))}\n\n`, |
| 227 | + ); |
| 228 | + res.write(`data: ${JSON.stringify(makeCompletionsChunk({}, "stop"))}\n\n`); |
| 229 | + res.write("data: [DONE]\n\n"); |
| 230 | + res.end(); |
| 231 | + }; |
| 232 | + |
| 233 | + writeNextChunk(); |
| 234 | + }); |
| 235 | + }); |
| 236 | + |
| 237 | + await new Promise<void>((resolve) => { |
| 238 | + server.listen(0, "127.0.0.1", resolve); |
| 239 | + }); |
| 240 | + try { |
| 241 | + const address = server.address(); |
| 242 | + if (!address || typeof address === "string") { |
| 243 | + throw new Error("Missing loopback server address"); |
| 244 | + } |
| 245 | + const model = makeCompletionsModel({ |
| 246 | + id: "nemotron-local", |
| 247 | + name: "Local Nemotron", |
| 248 | + provider: "inference", |
| 249 | + baseUrl: `http://127.0.0.1:${address.port}/v1`, |
| 250 | + reasoning: false, |
| 251 | + }); |
| 252 | + const onIdleTimeout = vi.fn(); |
| 253 | + const streamFn = streamWithIdleTimeout( |
| 254 | + createOpenAICompletionsTransportStreamFn(), |
| 255 | + idleTimeoutMs, |
| 256 | + onIdleTimeout, |
| 257 | + ); |
| 258 | + const stream = streamFn( |
| 259 | + model, |
| 260 | + { |
| 261 | + systemPrompt: "system", |
| 262 | + messages: [{ role: "user", content: "Reply OK", timestamp: Date.now() }], |
| 263 | + tools: [], |
| 264 | + } as never, |
| 265 | + { apiKey: "test-key" } as never, |
| 266 | + ); |
| 267 | + |
| 268 | + let text = ""; |
| 269 | + let thinking = ""; |
| 270 | + for await (const event of stream as AsyncIterable<{ type: string; delta?: string }>) { |
| 271 | + if (event.type === "text_delta") { |
| 272 | + text += event.delta ?? ""; |
| 273 | + } |
| 274 | + if (event.type === "thinking_delta") { |
| 275 | + thinking += event.delta ?? ""; |
| 276 | + } |
| 277 | + } |
| 278 | + |
| 279 | + expect(text).toBe("OK"); |
| 280 | + expect(thinking).toBe(""); |
| 281 | + expect(onIdleTimeout).not.toHaveBeenCalled(); |
| 282 | + } finally { |
| 283 | + await new Promise<void>((resolve, reject) => { |
| 284 | + server.close((error) => (error ? reject(error) : resolve())); |
| 285 | + }); |
| 286 | + } |
| 287 | + }, |
| 288 | + ); |
| 289 | + |
184 | 290 | it("refuses ModelStudio chat streams with no user or assistant payload turns", async () => { |
185 | 291 | const model = makeCompletionsModel({ |
186 | 292 | id: "qwen-coder-plus", |
|
0 commit comments