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(
|
async function handleStreamingChatRequest(
|
||||||
res: ServerResponse,
|
res: ServerResponse,
|
||||||
workspace: string,
|
workspace: string,
|
||||||
@@ -1166,8 +1179,8 @@ async function handleStreamingChatRequest(
|
|||||||
let stderrText = "";
|
let stderrText = "";
|
||||||
let streamedAny = false;
|
let streamedAny = false;
|
||||||
let finished = false;
|
let finished = false;
|
||||||
let lastAssistantDelta = "";
|
let lastAssistantSnapshot = "";
|
||||||
let lastThinkingDelta = "";
|
let lastThinkingSnapshot = "";
|
||||||
|
|
||||||
const finishStop = () => {
|
const finishStop = () => {
|
||||||
if (finished) return;
|
if (finished) return;
|
||||||
@@ -1207,22 +1220,24 @@ async function handleStreamingChatRequest(
|
|||||||
if (!isPartial) return;
|
if (!isPartial) return;
|
||||||
|
|
||||||
if (thinking) {
|
if (thinking) {
|
||||||
if (thinking !== lastThinkingDelta) {
|
const { delta, snapshot } = getNovelStreamingDelta(lastThinkingSnapshot, thinking);
|
||||||
|
lastThinkingSnapshot = snapshot;
|
||||||
|
if (delta) {
|
||||||
streamedAny = true;
|
streamedAny = true;
|
||||||
lastThinkingDelta = thinking;
|
sendSse(res, createChatCompletionChunk(model, { reasoning_content: delta }, null, responseMeta));
|
||||||
sendSse(res, createChatCompletionChunk(model, { reasoning_content: thinking }, null, responseMeta));
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
lastThinkingDelta = "";
|
lastThinkingSnapshot = "";
|
||||||
}
|
}
|
||||||
if (text) {
|
if (text) {
|
||||||
if (text !== lastAssistantDelta) {
|
const { delta, snapshot } = getNovelStreamingDelta(lastAssistantSnapshot, text);
|
||||||
|
lastAssistantSnapshot = snapshot;
|
||||||
|
if (delta) {
|
||||||
streamedAny = true;
|
streamedAny = true;
|
||||||
lastAssistantDelta = text;
|
sendSse(res, createChatCompletionChunk(model, { content: delta }, null, responseMeta));
|
||||||
sendSse(res, createChatCompletionChunk(model, { content: text }, null, responseMeta));
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
lastAssistantDelta = "";
|
lastAssistantSnapshot = "";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user