The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.
TypeScript agentai-chatai-studioall-in-onecoding-agentdesktop-appfluximage-generationlinuxllmlocal-ailocal-llmopen-sourceprivacyself-hostedstable-diffusionuncensoreduncensored-aivideo-generationwindows
import { describe, it, expect, vi, afterEach } from 'vitest'
import { streamProviderTurn } from '../provider-stream'
import type { ChatStreamChunk, ProviderClient } from '../../api/providers/types'
function fakeProvider(chunks: ChatStreamChunk[]): ProviderClient {
return {
id: 'openai',
async *chatStream() {
for (const c of chunks) yield c
},
chatWithTools: async () => ({ content: '', toolCalls: [] }),
listModels: async () => [],
checkConnection: async () => true,
getContextLength: async () => 8192,
} as unknown as ProviderClient
}
describe('streamProviderTurn', () => {
it('accumulates content deltas and reports cumulative text to the callback', async () => {
const provider = fakeProvider([
{ content: 'Hel', done: false },
{ content: 'lo', done: false },
{ content: '', done: true, finishReason: 'stop' },
])
const seen: string[] = []
const turn = await streamProviderTurn(provider, 'm', [], {}, (full) => seen.push(full))
expect(seen).toEqual(['Hel', 'Hello'])
expect(turn.content).toBe('Hello')
expect(turn.finishReason).toBe('stop')
expect(turn.toolCalls).toEqual([])
})
it('hands the raw delta to the callback as second argument', async () => {
const provider = fakeProvider([
{ content: 'a', done: false },
{ content: 'bc', done: false },
{ content: '', done: true },
])
const deltas: string[] = []
await streamProviderTurn(provider, 'm', [], {}, (_full, delta) => deltas.push(delta))
expect(deltas).toEqual(['a', 'bc'])
})
it('collects tool calls and usage from the done chunk', async () => {
const calls = [{ id: '1', function: { name: 'file_read', arguments: { path: 'a' } } }]
const provider = fakeProvider([
{ content: 'ok', done: false },
{ content: '', done: true, toolCalls: calls, promptEvalCount: 120, evalCount: 34, finishReason: 'tool_calls' },
])
const turn = await streamProviderTurn(provider, 'm', [], {})
expect(turn.toolCalls).toEqual(calls)
expect(turn.promptEvalCount).toBe(120)
expect(turn.evalCount).toBe(34)
expect(turn.finishReason).toBe('tool_calls')
})
it('accumulates thinking separately from content', async () => {
const provider = fakeProvider([
{ content: '', thinking: 'hm', done: false },
{ content: '', thinking: 'm…', done: false },
{ content: 'answer', done: false },
{ content: '', done: true },
])
const thinks: string[] = []
const turn = await streamProviderTurn(provider, 'm', [], {}, undefined, (full) => thinks.push(full))
expect(thinks).toEqual(['hm', 'hmm…'])
expect(turn.thinking).toBe('hmm…')
expect(turn.content).toBe('answer')
})
it('propagates a mid-stream error to the caller', async () => {
const provider = {
id: 'openai',
// eslint-disable-next-line require-yield
async *chatStream(): AsyncGenerator<ChatStreamChunk> {
throw Object.assign(new Error('boom'), { statusCode: 500 })
},
} as unknown as ProviderClient
await expect(streamProviderTurn(provider, 'm', [], {})).rejects.toThrow('boom')
})
})
// Kundenfall ein Nutzer, 30.09.2026: ein langer file_write auf einem nativen
// Modell. Die Argumente sammeln sich im Provider, der dabei nichts liefert,
// waehrend die Bytes weiter fliessen. Der alte Waechter hier brach nach 300 s
// ohne gelieferten Chunk ab, mitten im Schreiben.
describe('a long tool call is not a stalled stream', () => {
afterEach(() => { vi.useRealTimers() })
it('waits for a done chunk that takes longer than five minutes', async () => {
vi.useFakeTimers()
const calls = [{ id: '1', function: { name: 'file_write', arguments: { path: 'big.ts', content: 'x' } } }]
const provider = {
id: 'openai',
async *chatStream() {
await new Promise((r) => setTimeout(r, 420_000))
yield { content: '', done: true, toolCalls: calls, finishReason: 'tool_calls' } as ChatStreamChunk
},
} as unknown as ProviderClient
const turn = streamProviderTurn(provider, 'm', [], {})
await vi.advanceTimersByTimeAsync(420_000)
await expect(turn).resolves.toMatchObject({ toolCalls: calls, finishReason: 'tool_calls' })
})
})