update chatbox

This commit is contained in:
2026-08-08 00:39:16 +05:30
parent 90f14a73b2
commit 0cae250049
15 changed files with 1952 additions and 913 deletions

View File

@@ -48,7 +48,7 @@ export function useConversation({ contextId, facts }) {
const storageKey = `krow_assistant:${contextId}`;
const [messages, setMessages] = React.useState([]);
const [pending, setPending] = React.useState(null); // { question, text, thinking }
const [pending, setPending] = React.useState(null); // { blocks, thinking }
const [error, setError] = React.useState(null);
const abortRef = React.useRef(null);
@@ -93,28 +93,26 @@ export function useConversation({ contextId, facts }) {
messagesRef.current = withUser;
persist(withUser);
setError(null);
setPending({ question: text, text: '', thinking: true });
setPending({ blocks: [], thinking: true });
try {
let assembled = '';
for await (const delta of provider.stream({
let latest = [];
for await (const snapshot of provider.stream({
contextId, capability, question: text, facts, signal: controller.signal,
})) {
if (controller.signal.aborted) break;
assembled += delta;
setPending({ question: text, text: assembled, thinking: false });
latest = snapshot;
setPending({ blocks: snapshot, thinking: false });
}
if (controller.signal.aborted) {
// Keep whatever streamed before the user stopped it — discarding a
// half-written answer loses information the user was already reading.
if (assembled) {
const next = [...messagesRef.current, { role: 'assistant', text: assembled, stopped: true }];
messagesRef.current = next;
persist(next);
}
} else {
const next = [...messagesRef.current, { role: 'assistant', text: assembled }];
// Keep whatever arrived before the user stopped it — discarding a
// half-written answer loses what they were already reading. Stopping
// before the first block, though, should leave no empty turn behind.
if (latest.length) {
const next = [
...messagesRef.current,
{ role: 'assistant', blocks: latest, stopped: controller.signal.aborted || undefined },
];
messagesRef.current = next;
persist(next);
}