feat: better preidctiosn

This commit is contained in:
Gab
2026-03-26 15:07:30 +11:00
parent a42f8fa99f
commit 12ae1cb9b5
6 changed files with 647 additions and 22 deletions

View File

@@ -136,30 +136,41 @@ export function createToothFairyAI(options: ToothFairyAIProviderSettings = {}):
for (const line of lines) {
if (line.startsWith("data: ")) {
const json = line.slice(6).trim()
if (json && !json.startsWith('{"status":')) {
filtered.push(line)
// Log tool calls and finish_reason
// Filter out connection status messages like {"status":"initialising"}, {"status":"connected"}
// These are internal progress indicators, not OpenAI-format chunks
if (json) {
try {
const parsed = JSON.parse(json)
if (parsed.choices?.[0]?.delta?.tool_calls) {
log.debug("stream tool_calls", {
tool_calls: parsed.choices[0].delta.tool_calls,
})
if (parsed.status === "initialising" || parsed.status === "connected") {
log.debug("filtered connection status", { status: parsed.status })
continue
}
if (parsed.choices?.[0]?.finish_reason) {
log.info("stream finish_reason", {
finish_reason: parsed.choices[0].finish_reason,
})
}
if (parsed.usage) {
log.info("stream usage", {
prompt_tokens: parsed.usage.prompt_tokens,
completion_tokens: parsed.usage.completion_tokens,
total_tokens: parsed.usage.total_tokens,
})
}
} catch {}
} catch {
// Not valid JSON, keep the line
}
}
filtered.push(line)
// Log tool calls and finish_reason
try {
const parsed = JSON.parse(json)
if (parsed.choices?.[0]?.delta?.tool_calls) {
log.debug("stream tool_calls", {
tool_calls: parsed.choices[0].delta.tool_calls,
})
}
if (parsed.choices?.[0]?.finish_reason) {
log.info("stream finish_reason", {
finish_reason: parsed.choices[0].finish_reason,
})
}
if (parsed.usage) {
log.info("stream usage", {
prompt_tokens: parsed.usage.prompt_tokens,
completion_tokens: parsed.usage.completion_tokens,
total_tokens: parsed.usage.total_tokens,
})
}
} catch {}
} else {
filtered.push(line)
}