mirror of
https://gitea.toothfairyai.com/ToothFairyAI/tf_code.git
synced 2026-03-29 21:33:54 +00:00
fix: toothfairyai provider
This commit is contained in:
parent
3581538902
commit
e581d80c7a
@ -117,71 +117,57 @@ export function createToothFairyAI(options: ToothFairyAIProviderSettings = {}):
|
||||
}
|
||||
|
||||
if (res.body && res.headers.get("content-type")?.includes("text/event-stream")) {
|
||||
const reader = res.body.getReader()
|
||||
const decoder = new TextDecoder()
|
||||
const encoder = new TextEncoder()
|
||||
let buffer = ""
|
||||
|
||||
const filteredStream = new ReadableStream({
|
||||
async pull(controller) {
|
||||
const { done, value } = await reader.read()
|
||||
if (done) {
|
||||
controller.close()
|
||||
return
|
||||
}
|
||||
const filteredStream = res.body.pipeThrough(
|
||||
new TransformStream({
|
||||
transform(chunk, controller) {
|
||||
buffer += decoder.decode(chunk, { stream: true })
|
||||
const lines = buffer.split("\n")
|
||||
buffer = lines.pop() || ""
|
||||
|
||||
const text = decoder.decode(value, { stream: true })
|
||||
const lines = text.split("\n")
|
||||
|
||||
const filtered: string[] = []
|
||||
for (const line of lines) {
|
||||
if (line.startsWith("data: ")) {
|
||||
const json = line.slice(6).trim()
|
||||
// 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.status === "initialising" || parsed.status === "connected") {
|
||||
log.debug("filtered connection status", { status: parsed.status })
|
||||
continue
|
||||
}
|
||||
} catch {
|
||||
// Not valid JSON, keep the line
|
||||
const filtered: string[] = []
|
||||
for (const line of lines) {
|
||||
if (line.startsWith("data: ")) {
|
||||
const json = line.slice(6).trim()
|
||||
if (json) {
|
||||
try {
|
||||
const parsed = JSON.parse(json)
|
||||
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 {}
|
||||
}
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
controller.enqueue(encoder.encode(filtered.join("\n")))
|
||||
},
|
||||
cancel() {
|
||||
reader.cancel()
|
||||
},
|
||||
})
|
||||
if (filtered.length > 0) {
|
||||
controller.enqueue(encoder.encode(filtered.join("\n") + "\n"))
|
||||
}
|
||||
},
|
||||
flush(controller) {
|
||||
if (buffer) {
|
||||
controller.enqueue(encoder.encode(buffer))
|
||||
}
|
||||
},
|
||||
}),
|
||||
)
|
||||
|
||||
return new Response(filteredStream, {
|
||||
headers: res.headers,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user