show reasoning in frontend
This commit is contained in:
@@ -74,7 +74,7 @@ export function createModelService(config, overrides = {}) {
|
||||
}
|
||||
};
|
||||
|
||||
const readStreamingReply = async (response, onChunk = null) => {
|
||||
const readStreamingReply = async (response, onChunk = null, onThinking = null) => {
|
||||
if (!response.body) {
|
||||
throw new ModelServiceError("MODEL_BAD_RESPONSE", "Model backend returned no body.", 502);
|
||||
}
|
||||
@@ -83,6 +83,18 @@ export function createModelService(config, overrides = {}) {
|
||||
const decoder = new TextDecoder();
|
||||
let buffer = "";
|
||||
let fullReply = "";
|
||||
let fullThinking = "";
|
||||
const consume = (line) => {
|
||||
const data = JSON.parse(line);
|
||||
if (data.message?.thinking) {
|
||||
fullThinking += data.message.thinking;
|
||||
onThinking?.(data.message.thinking, fullThinking);
|
||||
}
|
||||
if (data.message?.content) {
|
||||
fullReply += data.message.content;
|
||||
onChunk?.(data.message.content, fullReply);
|
||||
}
|
||||
};
|
||||
|
||||
while (true) {
|
||||
const {value, done} = await reader.read();
|
||||
@@ -93,20 +105,12 @@ export function createModelService(config, overrides = {}) {
|
||||
for (const line of lines) {
|
||||
const trimmed = line.trim();
|
||||
if (!trimmed) continue;
|
||||
const data = JSON.parse(trimmed);
|
||||
if (data.message?.content) {
|
||||
fullReply += data.message.content;
|
||||
if (onChunk) onChunk(data.message.content, fullReply);
|
||||
}
|
||||
consume(trimmed);
|
||||
}
|
||||
}
|
||||
|
||||
if (buffer.trim()) {
|
||||
const data = JSON.parse(buffer.trim());
|
||||
if (data.message?.content) {
|
||||
fullReply += data.message.content;
|
||||
if (onChunk) onChunk(data.message.content, fullReply);
|
||||
}
|
||||
consume(buffer.trim());
|
||||
}
|
||||
|
||||
return fullReply.trim();
|
||||
@@ -146,7 +150,7 @@ export function createModelService(config, overrides = {}) {
|
||||
throw new ModelServiceError("MODEL_UPSTREAM_ERROR", `Model backend returned ${response.status}.`, 502);
|
||||
}
|
||||
|
||||
return readStreamingReply(response);
|
||||
return readStreamingReply(response, null, options.onThinking);
|
||||
}, options.signal);
|
||||
lastHealth = {ok: true, checkedAt: new Date().toISOString(), error: null, latencyMs: Date.now() - startedAt};
|
||||
return fullReply;
|
||||
@@ -184,7 +188,7 @@ export function createModelService(config, overrides = {}) {
|
||||
throw new ModelServiceError("MODEL_UPSTREAM_ERROR", `Model backend returned ${response.status}.`, 502);
|
||||
}
|
||||
|
||||
return readStreamingReply(response, onChunk);
|
||||
return readStreamingReply(response, onChunk, options.onThinking);
|
||||
}, options.signal);
|
||||
lastHealth = {ok: true, checkedAt: new Date().toISOString(), error: null, latencyMs: Date.now() - startedAt};
|
||||
return fullReply;
|
||||
|
||||
Reference in New Issue
Block a user