summaryrefslogtreecommitdiff
path: root/htroot
diff options
context:
space:
mode:
authorMichael Peter Christen <mc@yacy.net>2025-11-30 22:02:42 +0100
committerMichael Peter Christen <mc@yacy.net>2025-11-30 22:02:42 +0100
commit1202f74790458206e6276fab7d67f1f0b53f9362 (patch)
tree326e8cf7415c5d95dd473e2383cf670e2f374e03 /htroot
parenta8246c50c890dee09050e9afa7c7364a056cf250 (diff)
fixed missing line ending in token streaming
Diffstat (limited to 'htroot')
-rw-r--r--htroot/yacychat.html62
1 files changed, 32 insertions, 30 deletions
diff --git a/htroot/yacychat.html b/htroot/yacychat.html
index 120efb8bc..d8fc210dc 100644
--- a/htroot/yacychat.html
+++ b/htroot/yacychat.html
@@ -1155,40 +1155,42 @@
}
};
+ const processLine = line => {
+ const trimmed = (line || '').trim();
+ if (!trimmed) return false;
+ if (trimmed === 'data: [DONE]' || trimmed === '[DONE]') return true;
+ if (!trimmed.startsWith('data:')) return false;
+ const clean = trimmed.replace(/^data:\s*/i, '');
+ if (!clean) return false;
+ try {
+ const parsed = JSON.parse(clean);
+ applySearchAttachment(parsed);
+ const delta = parsed?.choices?.[0]?.delta?.content;
+ if (delta) {
+ if (!sawFirstDelta) {
+ sawFirstDelta = true;
+ applyMessageContent(assistantNode, 'assistant', '');
+ if (!focusShown && typeof onFirstToken === 'function') {
+ focusShown = true;
+ onFirstToken();
+ }
+ }
+ assistantText += delta;
+ applyMessageContent(assistantNode, 'assistant', assistantText);
+ performAutoScroll();
+ }
+ } catch (err) {
+ console.warn('Failed to parse line', trimmed, err);
+ }
+ return false;
+ };
+
const parseChunk = chunk => {
pending += chunk;
const lines = pending.split(/\r?\n/);
pending = lines.pop() || '';
- for (const raw of lines) {
- const fragments = raw.split(/(?=data:\s*)/).map(f => f.trim()).filter(Boolean);
- for (const line of fragments) {
- if (line === 'data: [DONE]' || line === '[DONE]') {
- return true;
- }
- if (!line.startsWith('data:')) continue;
- const clean = line.replace(/^data:\s*/i, '');
- if (!clean) continue;
- try {
- const parsed = JSON.parse(clean);
- applySearchAttachment(parsed);
- const delta = parsed?.choices?.[0]?.delta?.content;
- if (delta) {
- if (!sawFirstDelta) {
- sawFirstDelta = true;
- applyMessageContent(assistantNode, 'assistant', '');
- if (!focusShown && typeof onFirstToken === 'function') {
- focusShown = true;
- onFirstToken();
- }
- }
- assistantText += delta;
- applyMessageContent(assistantNode, 'assistant', assistantText);
- performAutoScroll();
- }
- } catch (err) {
- console.warn('Failed to parse line', line, err);
- }
- }
+ for (const line of lines) {
+ if (processLine(line)) return true;
}
return false;
};