-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest-dostream-error.mjs
More file actions
45 lines (41 loc) · 1.47 KB
/
test-dostream-error.mjs
File metadata and controls
45 lines (41 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Test whether streamText properly surfaces errors from doStream
import { streamText } from 'ai';
// Fake model whose doStream hangs forever then aborts
const hangingModel = {
specificationVersion: 'v1',
provider: 'test',
modelId: 'test',
defaultObjectGenerationMode: undefined,
supportsImageUrls: false,
supportsStructuredOutputs: false,
async doGenerate() { throw new Error('doGenerate not supported'); },
async doStream() {
console.log('doStream called, waiting 3s then throwing AbortError...');
await new Promise(resolve => setTimeout(resolve, 3000));
const err = new Error('The operation was aborted');
err.name = 'AbortError';
throw err;
},
};
console.log('Starting streamText with hanging model...');
const result = streamText({
model: hangingModel,
messages: [{ role: 'user', content: 'test' }],
maxSteps: 3,
});
const timeout = setTimeout(() => {
console.error('[!] HANG — Promise.all did not resolve after 10s');
process.exit(1);
}, 10000);
try {
for await (const delta of result.textStream) {
console.log('delta:', delta);
}
console.log('textStream done — now awaiting result.text, result.finishReason, result.steps...');
const [text, reason, steps] = await Promise.all([result.text, result.finishReason, result.steps]);
clearTimeout(timeout);
console.log('text:', JSON.stringify(text), '| reason:', reason, '| steps:', steps.length);
} catch (e) {
clearTimeout(timeout);
console.log('error:', e.name, e.message);
}