( chunks: Array<StreamChunk>, )
| 61 | * @returns The last assistant UIMessage from the processor after the stream ends. |
| 62 | */ |
| 63 | export async function runProcessorWithChunks( |
| 64 | chunks: Array<StreamChunk>, |
| 65 | ): Promise<UIMessage> { |
| 66 | const processor = new StreamProcessor() |
| 67 | |
| 68 | const envelopeChunks: Array<StreamChunk> = [ |
| 69 | runStartedChunk(), |
| 70 | textMessageStartChunk(), |
| 71 | ...chunks, |
| 72 | { |
| 73 | type: EventType.TEXT_MESSAGE_END, |
| 74 | timestamp: Date.now(), |
| 75 | messageId: 'msg-1', |
| 76 | }, |
| 77 | { |
| 78 | type: EventType.RUN_FINISHED, |
| 79 | timestamp: Date.now(), |
| 80 | runId: 'run-1', |
| 81 | threadId: 'thread-1', |
| 82 | finishReason: 'stop', |
| 83 | }, |
| 84 | ] |
| 85 | |
| 86 | async function* streamOf(cs: Array<StreamChunk>): AsyncIterable<StreamChunk> { |
| 87 | for (const c of cs) { |
| 88 | yield c |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | await processor.process(streamOf(envelopeChunks)) |
| 93 | |
| 94 | const messages = processor.getMessages() |
| 95 | const assistant = messages.findLast((m) => m.role === 'assistant') |
| 96 | if (!assistant) { |
| 97 | throw new Error( |
| 98 | 'runProcessorWithChunks: no assistant message produced. Messages: ' + |
| 99 | JSON.stringify(messages), |
| 100 | ) |
| 101 | } |
| 102 | return assistant |
| 103 | } |
no test coverage detected