MCPcopy Index your code
hub / github.com/TanStack/ai / runProcessorWithChunks

Function runProcessorWithChunks

packages/ai/tests/helpers/processor-harness.ts:63–103  ·  view source on GitHub ↗
(
  chunks: Array<StreamChunk>,
)

Source from the content-addressed store, hash-verified

61 * @returns The last assistant UIMessage from the processor after the stream ends.
62 */
63export 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}

Callers 1

Calls 6

processMethod · 0.95
getMessagesMethod · 0.95
textMessageStartChunkFunction · 0.85
nowMethod · 0.80
runStartedChunkFunction · 0.70
streamOfFunction · 0.70

Tested by

no test coverage detected