ReviewOS

also looking at this

stacks/stx

chore: wip stx improvements

#1649
Merged fingersandmind wants to merge feat/stx-improvements into main
241 files +19022 -1323
packages/ai/src/streaming.tsadded+24-0
Changes to packages/ai/src/streaming.ts
@@ -0,0 +1,24 @@
1import type { ChatOptions, Message, StreamChunk } from './types'
2import { useAI } from './ai'
3
4export async function* streamChat(
5 messages: Message[],
6 options?: ChatOptions & { provider?: string },
7): AsyncGenerator<StreamChunk> {
8 const { provider: providerName, ...chatOptions } = options ?? {}
9 const provider = useAI(providerName)
10
11 if (!provider.stream) {
12 throw new Error(`Provider "${provider.name}" does not support streaming.`)
13 }
14
15 yield* provider.stream(messages, chatOptions)
16}
17
18export async function collectStream(stream: AsyncIterable<StreamChunk>): Promise<string> {
19 let result = ''
20 for await (const chunk of stream) {
21 result += chunk.content
22 }
23 return result
24}