minicpm5-2b-webgpu / src /stream.js
ProCreations's picture
Dedicated WebGPU runtime, DSpark acceleration, and automatic tool repair
c51a3e7 verified
Raw History Blame Contribute Delete
788 Bytes
export function splitStreamContent(state, delta, flush = false) {
state.pending += delta;
if (!state.thinking) {
const content = state.pending;
state.pending = "";
return { content };
}
const end = state.pending.indexOf("</think>");
if (end >= 0) {
const reasoning_content = state.pending.slice(0, end),
content = state.pending.slice(end + 8);
state.pending = "";
state.thinking = false;
return { reasoning_content, content };
}
let keep = 0;
if (!flush)
for (let n = 1; n < 8; n++)
if (state.pending.endsWith("</think>".slice(0, n))) keep = n;
const reasoning_content = state.pending.slice(0, state.pending.length - keep);
state.pending = state.pending.slice(state.pending.length - keep);
return { reasoning_content };
}