also looking at this
chore: wip stx improvements
#1649
241 files
+19022
-1323
| @@ -0,0 +1,24 @@ | ||
| 1 | import type { ChatOptions, Message, StreamChunk } from './types' | |
| 2 | import { useAI } from './ai' | |
| 3 | ||
| 4 | export 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 | ||
| 18 | export 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 | } | |