fix
This commit is contained in:
@@ -1133,6 +1133,19 @@ function beginSse(res: ServerResponse) {
|
||||
});
|
||||
}
|
||||
|
||||
function getNovelStreamingDelta(previous: string, next: string): { delta: string; snapshot: string } {
|
||||
if (!next) return { delta: "", snapshot: previous };
|
||||
if (!previous) return { delta: next, snapshot: next };
|
||||
if (next === previous) return { delta: "", snapshot: previous };
|
||||
if (next.startsWith(previous)) {
|
||||
return {
|
||||
delta: next.slice(previous.length),
|
||||
snapshot: next,
|
||||
};
|
||||
}
|
||||
return { delta: next, snapshot: next };
|
||||
}
|
||||
|
||||
async function handleStreamingChatRequest(
|
||||
res: ServerResponse,
|
||||
workspace: string,
|
||||
@@ -1166,8 +1179,8 @@ async function handleStreamingChatRequest(
|
||||
let stderrText = "";
|
||||
let streamedAny = false;
|
||||
let finished = false;
|
||||
let lastAssistantDelta = "";
|
||||
let lastThinkingDelta = "";
|
||||
let lastAssistantSnapshot = "";
|
||||
let lastThinkingSnapshot = "";
|
||||
|
||||
const finishStop = () => {
|
||||
if (finished) return;
|
||||
@@ -1207,22 +1220,24 @@ async function handleStreamingChatRequest(
|
||||
if (!isPartial) return;
|
||||
|
||||
if (thinking) {
|
||||
if (thinking !== lastThinkingDelta) {
|
||||
const { delta, snapshot } = getNovelStreamingDelta(lastThinkingSnapshot, thinking);
|
||||
lastThinkingSnapshot = snapshot;
|
||||
if (delta) {
|
||||
streamedAny = true;
|
||||
lastThinkingDelta = thinking;
|
||||
sendSse(res, createChatCompletionChunk(model, { reasoning_content: thinking }, null, responseMeta));
|
||||
sendSse(res, createChatCompletionChunk(model, { reasoning_content: delta }, null, responseMeta));
|
||||
}
|
||||
} else {
|
||||
lastThinkingDelta = "";
|
||||
lastThinkingSnapshot = "";
|
||||
}
|
||||
if (text) {
|
||||
if (text !== lastAssistantDelta) {
|
||||
const { delta, snapshot } = getNovelStreamingDelta(lastAssistantSnapshot, text);
|
||||
lastAssistantSnapshot = snapshot;
|
||||
if (delta) {
|
||||
streamedAny = true;
|
||||
lastAssistantDelta = text;
|
||||
sendSse(res, createChatCompletionChunk(model, { content: text }, null, responseMeta));
|
||||
sendSse(res, createChatCompletionChunk(model, { content: delta }, null, responseMeta));
|
||||
}
|
||||
} else {
|
||||
lastAssistantDelta = "";
|
||||
lastAssistantSnapshot = "";
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user