fix: resume Ollama chat jobs across sessions
This commit is contained in:
Vendored
+33
-2
@@ -319,7 +319,7 @@
|
||||
}).catch(function () { return []; });
|
||||
}
|
||||
function newConversation() { setConversationId(""); setHistory([]); setMetrics([]); setValidationReports(null); setNotice({ ok: "New conversation ready." }); }
|
||||
React.useEffect(function () { refreshConversations(); refreshStorage(); }, []);
|
||||
React.useEffect(function () { refreshConversations(); refreshStorage(); resumeActiveJobs(); }, []);
|
||||
React.useEffect(function () { if (conversationId) saveChat(model, selectedModels, history); }, [model, selectedModels, history, conversationId]);
|
||||
React.useEffect(function () { if (!model && (loadedModels[0] || models[0])) setModel((loadedModels[0] || models[0]).name); }, [models, loadedModels, model]);
|
||||
React.useEffect(function () {
|
||||
@@ -429,6 +429,37 @@
|
||||
return new Promise(function (resolve) { setTimeout(resolve, 1200); }).then(function () { return pollChatJob(requestId, current); });
|
||||
});
|
||||
}
|
||||
function applyChatResult(result) {
|
||||
var answer = result.message && result.message.content ? result.message.content : "(No response text returned.)";
|
||||
var resolvedConversationId = result.conversation_id || conversationId;
|
||||
setConversationId(resolvedConversationId);
|
||||
setHistory(function (old) {
|
||||
var last = old.length ? old[old.length - 1] : null;
|
||||
return last && last.role === "assistant" && last.content === answer ? old : old.concat([{ role: "assistant", content: answer }]);
|
||||
});
|
||||
setMetrics(result.metrics || []);
|
||||
setValidationReports(result.mode === "harness" ? (result.validation_reports || []) : null);
|
||||
setAttachments([]);
|
||||
setRuntime(result.runtime || runtime);
|
||||
setNotice({ ok: result.mode === "harness" ? "One final answer compiled by " + result.primary_model + " after validation by " + (result.validator_models || []).join(", ") + "." : "Response complete. Shared conversation and performance metrics saved." });
|
||||
pollRuntime(); refreshConversations();
|
||||
return result;
|
||||
}
|
||||
function resumeActiveJobs() {
|
||||
return fetchJSON(API + "/chat/jobs?active=true&limit=20").then(function (value) {
|
||||
var jobs = value.jobs || [];
|
||||
var active = jobs.find(function (job) { return !job.done && (job.status === "queued" || job.status === "running" || job.status === "stopping"); });
|
||||
if (!active) return null;
|
||||
var current = { id: active.request_id, controller: null, stopped: false };
|
||||
setConversationId(active.conversation_id || "");
|
||||
setActiveRequest(current);
|
||||
setBusy("send");
|
||||
setThinking({ request_id: active.request_id, startedAt: (Number(active.started_at || active.updated_at || Date.now() / 1000) * 1000), stage: active.stage || "Resuming server-side chat job" });
|
||||
setThinkingDetails(active);
|
||||
if (active.conversation_id) openConversation(active.conversation_id);
|
||||
return pollChatJob(active.request_id, current).then(applyChatResult).catch(function (err) { if (!current.stopped) setNotice({ error: err.message || String(err) }); }).finally(function () { if (!current.stopped) { setThinking(null); setActiveRequest(null); } setBusy(""); });
|
||||
}).catch(function () { return null; });
|
||||
}
|
||||
function send() {
|
||||
if (busy === "send" || busy === "stop" || !selectedModels.length || (!message.trim() && !attachments.length)) return;
|
||||
var requestId = makeRequestId();
|
||||
@@ -438,7 +469,7 @@
|
||||
setHistory(function (old) { return old.concat([outgoing]); }); setMessage(""); setBusy("send"); setActiveRequest(current); setThinking({ request_id: requestId, startedAt: Date.now(), stage: attachments.length ? "Preparing attachments and sending request to Ollama" : "Sending request to Ollama" }); setThinkingDetails(null); setThinkingOpen(false); setNotice(null);
|
||||
var requestOptions = { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body) };
|
||||
if (controller) requestOptions.signal = controller.signal;
|
||||
fetchJSON(API + "/chat", requestOptions).then(function (result) { return result.done ? result : pollChatJob(requestId, current); }).then(function (result) { var answer = result.message && result.message.content ? result.message.content : "(No response text returned.)"; setConversationId(result.conversation_id || conversationId); setHistory(function (old) { return old.concat([{ role: "assistant", content: answer }]); }); setMetrics(result.metrics || []); setValidationReports(result.mode === "harness" ? (result.validation_reports || []) : null); setAttachments([]); setRuntime(result.runtime || runtime); setNotice({ ok: result.mode === "harness" ? "One final answer compiled by " + result.primary_model + " after validation by " + (result.validator_models || []).join(", ") + "." : "Response complete. Shared conversation and performance metrics saved." }); pollRuntime(); refreshConversations(); }).catch(function (err) { if (!current.stopped) setNotice({ error: err.message || String(err) }); }).finally(function () { if (!current.stopped) { setThinking(null); setActiveRequest(null); } setBusy(""); });
|
||||
fetchJSON(API + "/chat", requestOptions).then(function (result) { return result.done ? result : pollChatJob(requestId, current); }).then(applyChatResult).catch(function (err) { if (!current.stopped) setNotice({ error: err.message || String(err) }); }).finally(function () { if (!current.stopped) { setThinking(null); setActiveRequest(null); } setBusy(""); });
|
||||
}
|
||||
return h("section", { className: "ollama-chat" },
|
||||
h("div", { className: "ollama-chat-header" }, h("div", null, h("div", { className: "ollama-eyebrow" }, "LOCAL OLLAMA CHAT"), h("h2", null, "Chat with a validated model harness"), h("p", null, "Choose one primary model and at least one loaded validator model. The primary produces one final answer after reviewing the independent validation reports.")), h(Button, { className: "secondary", disabled: !history.length || busy === "send", onClick: clearChat }, "Clear chat")),
|
||||
|
||||
Reference in New Issue
Block a user