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
/**
* Smoke tests for `streamWithTools()` — the NDJSON streaming helper in useCodex.ts.
*
* We cannot import the function directly (it's module-private), so we test the
* NDJSON-parsing logic in isolation with an identical implementation extracted
* from the source. A drift-detection test at the bottom re-reads the hook
* source to confirm the critical patterns remain in sync.
*
* Coverage:
* - Single-chunk content accumulation
* - Multi-chunk content concatenation
* - Thinking field accumulation
* - Tool call extraction from final chunk
* - Tool call APPEND (not overwrite) across multiple chunks — regression test
* - Empty/whitespace lines in NDJSON stream
* - Partial JSON lines that split across chunks
* - Remaining buffer processing after stream ends
* - Mixed content + thinking + tool_calls in one stream
*/
import { describe, it, expect } from 'vitest'
import { readFileSync } from 'fs'
import { fileURLToPath } from 'url'
import { dirname, join } from 'path'
import { parseOllamaChatChunk } from '../../api/providers/wire'
import { isRecord } from '../../types/json-guards'
// ── Faithful copy of streamWithTools parsing logic ──────────────────────
// This mirrors the NDJSON parsing from useCodex.ts without the HTTP/fetch parts.
interface ToolCall {
function: { name: string; arguments: Record<string, unknown> }
}
interface StreamResult {
content: string
toolCalls: ToolCall[]
thinking: string
}
function parseNDJSON(ndjsonLines: string[]): StreamResult {
let content = ''
let thinking = ''
let toolCalls: ToolCall[] = []
for (const rawLine of ndjsonLines) {
const trimmed = rawLine.trim()
if (!trimmed) continue
try {
// Durch DENSELBEN Parser wie die Produktion (providers/wire.ts), statt
// durch ein `any`. Der Spiegel las `tc.function.name` ungeschuetzt —
// genau der TypeError, gegen den ollama-stream-tools.ts seit dem Fund
// `tc.function?.name` schreibt. Als `any` konnte diese Kopie von der
// Produktion wegdriften, ohne dass irgendetwas es merkte.
const chunk = parseOllamaChatChunk(JSON.parse(trimmed))
content += chunk.message?.content ?? ''
thinking += chunk.message?.thinking ?? ''
// Ollama may split tool_calls across chunks — append, don't overwrite
for (const tc of chunk.message?.tool_calls ?? []) {
const name = tc.function?.name
// Ein namenloser Aufruf ist nicht ausfuehrbar; die Produktion
// ueberspringt ihn ebenso, statt ihn als `undefined` weiterzureichen.
if (!name) continue
const args = tc.function?.arguments
toolCalls = [...toolCalls, { function: { name, arguments: isRecord(args) ? args : {} } }]
}
} catch { /* partial JSON — skip */ }
}
return { content, toolCalls, thinking }
}
// ── Tests ───────────────────────────────────────────────────────────────
describe('streamWithTools NDJSON parsing', () => {
it('accumulates content from multiple chunks', () => {
const lines = [
'{"message":{"role":"assistant","content":"Hello"}}',
'{"message":{"role":"assistant","content":" World"}}',
'{"message":{"role":"assistant","content":"!"}}',
]
const result = parseNDJSON(lines)
expect(result.content).toBe('Hello World!')
expect(result.toolCalls).toEqual([])
expect(result.thinking).toBe('')
})
it('accumulates thinking from multiple chunks', () => {
const lines = [
'{"message":{"role":"assistant","thinking":"Let me think..."}}',
'{"message":{"role":"assistant","thinking":" about this."}}',
'{"message":{"role":"assistant","content":"The answer is 42."}}',
]
const result = parseNDJSON(lines)
expect(result.thinking).toBe('Let me think... about this.')
expect(result.content).toBe('The answer is 42.')
})
it('extracts tool calls from the final chunk', () => {
const lines = [
'{"message":{"role":"assistant","content":"I\'ll write the file."}}',
'{"message":{"role":"assistant","content":"","tool_calls":[{"function":{"name":"file_write","arguments":{"path":"test.ts","content":"console.log()"}}}]},"done":true}',
]
const result = parseNDJSON(lines)
expect(result.content).toBe("I'll write the file.")
expect(result.toolCalls).toHaveLength(1)
expect(result.toolCalls[0].function.name).toBe('file_write')
expect(result.toolCalls[0].function.arguments).toEqual({ path: 'test.ts', content: 'console.log()' })
})
it('APPENDS tool calls across chunks (regression — was overwriting)', () => {
const lines = [
'{"message":{"role":"assistant","tool_calls":[{"function":{"name":"file_read","arguments":{"path":"a.ts"}}}]}}',
'{"message":{"role":"assistant","tool_calls":[{"function":{"name":"file_write","arguments":{"path":"b.ts","content":"x"}}}]}}',
]
const result = parseNDJSON(lines)
expect(result.toolCalls).toHaveLength(2)
expect(result.toolCalls[0].function.name).toBe('file_read')
expect(result.toolCalls[1].function.name).toBe('file_write')
})
it('handles multiple tool calls in a single chunk', () => {
const lines = [
'{"message":{"role":"assistant","tool_calls":[{"function":{"name":"file_read","arguments":{"path":"a.ts"}}},{"function":{"name":"file_read","arguments":{"path":"b.ts"}}}]}}',
]
const result = parseNDJSON(lines)
expect(result.toolCalls).toHaveLength(2)
expect(result.toolCalls[0].function.arguments).toEqual({ path: 'a.ts' })
expect(result.toolCalls[1].function.arguments).toEqual({ path: 'b.ts' })
})
it('handles mixed content + thinking + tool_calls in one stream', () => {
const lines = [
'{"message":{"role":"assistant","thinking":"I need to read the file first."}}',
'{"message":{"role":"assistant","content":"Reading file..."}}',
'{"message":{"role":"assistant","tool_calls":[{"function":{"name":"file_read","arguments":{"path":"index.ts"}}}]}}',
]
const result = parseNDJSON(lines)
expect(result.thinking).toBe('I need to read the file first.')
expect(result.content).toBe('Reading file...')
expect(result.toolCalls).toHaveLength(1)
expect(result.toolCalls[0].function.name).toBe('file_read')
})
it('skips empty and whitespace-only lines', () => {
const lines = [
'',
' ',
'{"message":{"role":"assistant","content":"hello"}}',
'',
'{"message":{"role":"assistant","content":" world"}}',
' \n ',
]
const result = parseNDJSON(lines)
expect(result.content).toBe('hello world')
})
it('skips partial/invalid JSON lines gracefully', () => {
const lines = [
'{"message":{"role":"ass', // truncated
'{"message":{"role":"assistant","content":"ok"}}',
'not json at all',
'{"message":{"role":"assistant","content":"!"}}',
]
const result = parseNDJSON(lines)
expect(result.content).toBe('ok!')
})
it('returns empty result for empty stream', () => {
const result = parseNDJSON([])
expect(result.content).toBe('')
expect(result.thinking).toBe('')
expect(result.toolCalls).toEqual([])
})
it('ignores lines with no message field', () => {
const lines = [
'{"model":"gemma4","done":false}',
'{"message":{"role":"assistant","content":"hi"}}',
'{"status":"ok"}',
]
const result = parseNDJSON(lines)
expect(result.content).toBe('hi')
})
it('handles tool_calls with empty arguments', () => {
const lines = [
'{"message":{"role":"assistant","tool_calls":[{"function":{"name":"system_info","arguments":{}}}]}}',
]
const result = parseNDJSON(lines)
expect(result.toolCalls).toHaveLength(1)
expect(result.toolCalls[0].function.arguments).toEqual({})
})
it('handles done=true chunk with both content and tool_calls', () => {
const lines = [
'{"message":{"role":"assistant","content":"Let me help. ","tool_calls":[{"function":{"name":"file_list","arguments":{"path":"."}}}]},"done":true}',
]
const result = parseNDJSON(lines)
expect(result.content).toBe('Let me help. ')
expect(result.toolCalls).toHaveLength(1)
expect(result.toolCalls[0].function.name).toBe('file_list')
})
it('tool call accumulation order is preserved', () => {
const lines = [
'{"message":{"role":"assistant","tool_calls":[{"function":{"name":"file_read","arguments":{"path":"1.ts"}}}]}}',
'{"message":{"role":"assistant","tool_calls":[{"function":{"name":"file_write","arguments":{"path":"2.ts","content":"x"}}}]}}',
'{"message":{"role":"assistant","tool_calls":[{"function":{"name":"shell_execute","arguments":{"command":"npm test"}}}]}}',
]
const result = parseNDJSON(lines)
expect(result.toolCalls).toHaveLength(3)
expect(result.toolCalls.map(tc => tc.function.name)).toEqual([
'file_read', 'file_write', 'shell_execute',
])
})
})
// ── Drift detection ─────────────────────────────────────────────────────
// The streaming helper used to live inline in useCodex.ts. It moved to
// `src/lib/ollama-stream-tools.ts` so the regular Agent path
// (useAgentChat.ts) can share the same wire protocol — these checks now
// guard the new location instead of useCodex.ts.
describe('streamWithTools drift detection', () => {
it('ollama-stream-tools.ts still appends tool_calls (not overwrite), deduped since audit F1', () => {
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
const src = readFileSync(join(__dirname, '../../lib/ollama-stream-tools.ts'), 'utf8')
// Every chunk AND the tail-buffer pass must go through the appending,
// deduping helper — split-across-lines calls still accumulate, while a
// server that repeats the full array cannot double-execute a call.
expect(src.match(/appendCalls\(j\.message\.tool_calls\)/g)?.length).toBe(2)
expect(src).toContain('seenCalls')
// Must NOT contain the old overwrite pattern
expect(src).not.toMatch(/toolCalls\s*=\s*j\.message\.tool_calls\.map/)
})
it('ollama-stream-tools.ts uses localFetchStream (not raw fetch)', () => {
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
const src = readFileSync(join(__dirname, '../../lib/ollama-stream-tools.ts'), 'utf8')
expect(src).toContain('localFetchStream')
// Must NOT use raw fetch() for the streaming endpoint
expect(src).not.toMatch(/\bfetch\(url,/)
})
})