Changeset 0.37.14 (#226)
Co-authored-by: gobha <jasafpro@gmail.com> Co-committed-by: gobha <jasafpro@gmail.com>
This commit is contained in:
@@ -18,13 +18,16 @@ const { useState, useRef, useCallback } = window.hooks;
|
||||
*/
|
||||
export function useStream() {
|
||||
const [streamContent, setStreamContent] = useState('');
|
||||
const [streamThinking, setStreamThinking] = useState('');
|
||||
const [streaming, setStreaming] = useState(false);
|
||||
const contentRef = useRef('');
|
||||
const thinkingRef = useRef('');
|
||||
const abortRef = useRef(null);
|
||||
const rafRef = useRef(null);
|
||||
|
||||
const _flush = useCallback(() => {
|
||||
setStreamContent(contentRef.current);
|
||||
setStreamThinking(thinkingRef.current);
|
||||
rafRef.current = null;
|
||||
}, []);
|
||||
|
||||
@@ -44,13 +47,16 @@ export function useStream() {
|
||||
rafRef.current = null;
|
||||
}
|
||||
setStreamContent(contentRef.current);
|
||||
setStreamThinking(thinkingRef.current);
|
||||
setStreaming(false);
|
||||
}, []);
|
||||
|
||||
const startStream = useCallback(async (response) => {
|
||||
// Reset
|
||||
contentRef.current = '';
|
||||
thinkingRef.current = '';
|
||||
setStreamContent('');
|
||||
setStreamThinking('');
|
||||
setStreaming(true);
|
||||
|
||||
const controller = new AbortController();
|
||||
@@ -75,9 +81,20 @@ export function useStream() {
|
||||
const data = line.slice(6);
|
||||
if (data === '[DONE]') continue;
|
||||
try {
|
||||
const delta = JSON.parse(data).choices?.[0]?.delta?.content;
|
||||
if (delta) {
|
||||
contentRef.current += delta;
|
||||
const parsed = JSON.parse(data).choices?.[0]?.delta;
|
||||
if (parsed?.content) {
|
||||
let chunk = parsed.content;
|
||||
// Run pipe stream filters (extensions can transform or suppress chunks)
|
||||
if (window.sw?.pipe?._runStream) {
|
||||
const pipeCtx = window.sw.pipe._runStream({ content: chunk });
|
||||
if (pipeCtx === null) continue;
|
||||
chunk = pipeCtx.content;
|
||||
}
|
||||
contentRef.current += chunk;
|
||||
_scheduleFlush();
|
||||
}
|
||||
if (parsed?.reasoning_content) {
|
||||
thinkingRef.current += parsed.reasoning_content;
|
||||
_scheduleFlush();
|
||||
}
|
||||
} catch (_) { /* skip malformed JSON */ }
|
||||
@@ -95,9 +112,10 @@ export function useStream() {
|
||||
rafRef.current = null;
|
||||
}
|
||||
setStreamContent(contentRef.current);
|
||||
setStreamThinking(thinkingRef.current);
|
||||
setStreaming(false);
|
||||
abortRef.current = null;
|
||||
}, [_scheduleFlush]);
|
||||
|
||||
return { streamContent, streaming, startStream, stopStream };
|
||||
return { streamContent, streamThinking, streaming, startStream, stopStream };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user