fix(chat): recover web approvals and questions across clients and reconnects
This commit is contained in:
+25
-6
@@ -1576,18 +1576,21 @@ export function applyChatForm(thread, form) {
|
||||
const id = str(form?.id);
|
||||
if (id === "") return current;
|
||||
const existing = current.form ?? null;
|
||||
const forms = [...(current.forms ?? (existing ? [existing] : []))].filter((entry) => entry.id !== id);
|
||||
if (form?.resolved !== true && form?.cancelled !== true) forms.push(form);
|
||||
const next = { ...current, forms };
|
||||
if (form?.cancelled === true) {
|
||||
if (str(existing?.id) !== id) return current;
|
||||
return { ...current, form: null };
|
||||
if (str(existing?.id) !== id) return next;
|
||||
return { ...next, form: forms.at(-1) ?? null };
|
||||
}
|
||||
if (form?.resolved === true) {
|
||||
if (str(existing?.id) !== id) return current;
|
||||
return { ...current, form: { ...existing, ...form, resolved: true } };
|
||||
if (str(existing?.id) !== id) return next;
|
||||
return { ...next, form: forms.at(-1) ?? { ...existing, ...form, resolved: true } };
|
||||
}
|
||||
if (str(existing?.id) === id) {
|
||||
return { ...current, form: { ...existing, ...form } };
|
||||
return { ...next, form: { ...existing, ...form } };
|
||||
}
|
||||
return { ...current, form };
|
||||
return { ...next, form };
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2324,9 +2327,25 @@ export function applyChatPermission(thread, frame) {
|
||||
const current = thread ?? newChatThread(str(frame?.session_id));
|
||||
const requestId = str(frame?.request_id);
|
||||
const sameRequest = requestId !== "" && str(current.permission?.request_id) === requestId;
|
||||
const permissions = [...(current.permissions ?? (current.permission ? [current.permission] : []))]
|
||||
.filter((entry) => str(entry?.request_id) !== requestId && entry?.resolved !== true);
|
||||
if (requestId && frame?.resolved !== true) permissions.push(frame);
|
||||
return {
|
||||
...current,
|
||||
permissions,
|
||||
permission: frame ?? null,
|
||||
permissionAnswer: sameRequest ? str(current.permissionAnswer) : "",
|
||||
};
|
||||
}
|
||||
|
||||
export function chatApprovalCards(thread) {
|
||||
return (thread?.permissions ?? (thread?.permission ? [thread.permission] : []))
|
||||
.map((frame) => approvalCard(frame, thread?.permissionAnswers?.[frame.request_id] ??
|
||||
(frame.request_id === thread?.permission?.request_id ? thread.permissionAnswer : "")))
|
||||
.filter(Boolean);
|
||||
}
|
||||
|
||||
export function chatPendingForms(thread) {
|
||||
return (thread?.forms ?? (thread?.form ? [thread.form] : []))
|
||||
.filter((form) => form?.resolved !== true && form?.cancelled !== true);
|
||||
}
|
||||
|
||||
+50
-18
@@ -79,6 +79,9 @@ import {
|
||||
applyChatEvent,
|
||||
applyChatOverrides,
|
||||
applyChatPermission,
|
||||
applyChatForm,
|
||||
chatApprovalCards,
|
||||
chatPendingForms,
|
||||
abbreviateHome,
|
||||
appendChatNotice,
|
||||
approvalCard,
|
||||
@@ -738,7 +741,12 @@ function refreshChatTranscript(sessionId) {
|
||||
if (rows.length === 0 && notices.length === 0 && !thread.running && !approval && !form) {
|
||||
return false;
|
||||
}
|
||||
const running = thread.running === true && approval?.actionable !== true;
|
||||
const awaiting = chatApprovalCards(thread).some((card) => card.actionable) || chatPendingForms(thread).length > 0;
|
||||
const running = thread.running === true && !awaiting;
|
||||
const bar = document.querySelector(".chat-bar");
|
||||
const jump = bar?.querySelector(".chat-approval-jump");
|
||||
if (awaiting && bar && !jump) bar.append(approvalJumpButton());
|
||||
else if (!awaiting) jump?.remove();
|
||||
captureChatPosition();
|
||||
// The background-tasks bar sits outside the transcript but is driven by the
|
||||
// same shell frames. Patch it in place instead of full-rendering the pane (and
|
||||
@@ -841,7 +849,8 @@ function onChatPermission(frame) {
|
||||
next.permissionError = "";
|
||||
}
|
||||
state.chatThreads.set(sessionId, next);
|
||||
if (state.view === "chat" && sessionId === state.chatActiveId) scheduleChatRender();
|
||||
markBackgroundChatUnseen(sessionId);
|
||||
if (state.view === "chat" && sessionId === state.chatActiveId) scheduleChatRender("transcript");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -854,9 +863,9 @@ function onChatPermission(frame) {
|
||||
*/
|
||||
async function answerPermission(sessionId, requestId, decision) {
|
||||
const thread = state.chatThreads.get(sessionId);
|
||||
const permission = thread?.permission;
|
||||
if (!thread || String(permission?.request_id ?? "") !== requestId) return;
|
||||
setThread(sessionId, { permissionAnswer: decision });
|
||||
const permission = (thread?.permissions ?? [thread?.permission]).find((entry) => entry?.request_id === requestId);
|
||||
if (!thread || !permission) return;
|
||||
setThread(sessionId, { permissionAnswers: { ...thread.permissionAnswers, [requestId]: decision } });
|
||||
renderContent();
|
||||
try {
|
||||
await repo.chatReply(sessionId, requestId, decision);
|
||||
@@ -870,9 +879,9 @@ async function answerPermission(sessionId, requestId, decision) {
|
||||
}
|
||||
} catch (error) {
|
||||
const current = state.chatThreads.get(sessionId);
|
||||
if (!current || String(current.permission?.request_id ?? "") !== requestId) return;
|
||||
if (!current || !(current.permissions ?? [current.permission]).some((entry) => entry?.request_id === requestId)) return;
|
||||
const reason = describeError(error);
|
||||
setThread(sessionId, { permissionAnswer: "", error: reason, permissionError: reason });
|
||||
setThread(sessionId, { permissionAnswers: { ...current.permissionAnswers, [requestId]: "" }, error: reason, permissionError: reason });
|
||||
renderContent();
|
||||
}
|
||||
}
|
||||
@@ -887,9 +896,7 @@ async function answerChatQuestion(sessionId, form, answers) {
|
||||
try {
|
||||
await repo.answerChatForm(sessionId, form.id, answers);
|
||||
const current = state.chatThreads.get(sessionId);
|
||||
if (current?.form && current.form.id === form.id) {
|
||||
setThread(sessionId, { form: { ...current.form, resolved: true } });
|
||||
}
|
||||
if (current) state.chatThreads.set(sessionId, applyChatForm(current, { ...form, resolved: true }));
|
||||
renderContent();
|
||||
} catch (error) {
|
||||
state.actionError = describeError(error);
|
||||
@@ -1017,7 +1024,10 @@ async function loadChatHistory(id) {
|
||||
// The merged page replaces only the history fields; spreading it over the
|
||||
// held thread keeps the composer's draft, attachments and flags intact (a
|
||||
// draft typed while the page was loading is not lost).
|
||||
setThread(id, mergeHistoryPage(previous, page, { older: false }));
|
||||
// Live approvals/questions can arrive before this HTTP read finishes. Merge
|
||||
// over the current thread, never the pre-request snapshot that would erase
|
||||
// the just-delivered interaction (and any draft typed during the read).
|
||||
setThread(id, mergeHistoryPage(state.chatThreads.get(id) ?? previous, page, { older: false }));
|
||||
} catch (error) {
|
||||
setThread(id, { loading: false, loaded: true, error: describeError(error) });
|
||||
}
|
||||
@@ -2449,7 +2459,7 @@ function renderChatConversation() {
|
||||
thread?.form && thread.form.resolved !== true && thread.form.cancelled !== true
|
||||
? thread.form
|
||||
: null;
|
||||
const awaiting = approval?.actionable === true || form != null;
|
||||
const awaiting = chatApprovalCards(thread).some((card) => card.actionable) || chatPendingForms(thread).length > 0;
|
||||
|
||||
const bar = h("div", { class: "chat-bar" });
|
||||
const lead = h("div", { class: "chat-bar-lead" });
|
||||
@@ -3007,8 +3017,19 @@ function renderChatTranscript(
|
||||
// The approval card is the transcript's last item (C10) — after the notices and
|
||||
// after the running indicator when it is shown — so the pane's jump control
|
||||
// and a fresh card both land at the end.
|
||||
if (approval) transcript.append(renderChatApproval(sessionId, approval));
|
||||
if (form) transcript.append(renderChatQuestion(sessionId, form));
|
||||
const approvals = chatApprovalCards(thread);
|
||||
const forms = chatPendingForms(thread);
|
||||
if (!approvals.length && approval) approvals.push(approval);
|
||||
approvals.forEach((card, index) => {
|
||||
const node = renderChatApproval(sessionId, card);
|
||||
if (index === 0) node.id = "chatApproval";
|
||||
transcript.append(node);
|
||||
});
|
||||
forms.forEach((pending, index) => {
|
||||
const node = renderChatQuestion(sessionId, pending);
|
||||
if (index === 0) node.id = "chatQuestion";
|
||||
transcript.append(node);
|
||||
});
|
||||
wrap.append(transcript);
|
||||
wrap.append(
|
||||
h("button", {
|
||||
@@ -3057,7 +3078,7 @@ function approvalJumpButton() {
|
||||
function renderChatApproval(sessionId, card) {
|
||||
const node = h("div", {
|
||||
class: `chat-approval${card.actionable ? " chat-approval-pending" : ""}`,
|
||||
id: "chatApproval",
|
||||
id: `chatApproval-${card.requestId}`,
|
||||
"data-request-id": card.requestId,
|
||||
});
|
||||
const head = h("div", { class: "chat-approval-head" });
|
||||
@@ -3073,6 +3094,12 @@ function renderChatApproval(sessionId, card) {
|
||||
const box = h("div", { class: "chat-approval-resources" });
|
||||
box.append(h("pre", { class: "chat-approval-resources-text", text: card.resourceText }));
|
||||
node.append(box);
|
||||
const full = card.resources.join("\n");
|
||||
if (full !== card.resourceText) {
|
||||
node.append(h("details", { class: "chat-approval-details" },
|
||||
h("summary", { text: "Show full request" }),
|
||||
h("pre", { text: full, style: "white-space: pre-wrap; overflow-wrap: anywhere;" })));
|
||||
}
|
||||
}
|
||||
if (card.showRisk && card.risk.trim() !== "") {
|
||||
const risk = h("div", { class: "chat-approval-risk" });
|
||||
@@ -3102,7 +3129,7 @@ function renderChatApproval(sessionId, card) {
|
||||
function renderChatQuestion(sessionId, form) {
|
||||
const node = h("div", {
|
||||
class: "chat-question",
|
||||
id: "chatQuestion",
|
||||
id: `chatQuestion-${form.id}`,
|
||||
"data-form-id": form.id,
|
||||
});
|
||||
const head = h("div", { class: "chat-question-head" });
|
||||
@@ -3112,7 +3139,10 @@ function renderChatQuestion(sessionId, form) {
|
||||
);
|
||||
node.append(head);
|
||||
|
||||
const answers = {};
|
||||
// Keep entered answers across unrelated streaming frames and reconnect paints.
|
||||
const thread = state.chatThreads.get(sessionId);
|
||||
if (thread && !thread.formAnswers) thread.formAnswers = {};
|
||||
const answers = thread ? (thread.formAnswers[form.id] ??= {}) : {};
|
||||
const parts = [];
|
||||
for (const field of form.fields ?? []) {
|
||||
const key = String(field.key ?? "");
|
||||
@@ -3168,6 +3198,7 @@ function renderChatQuestion(sessionId, form) {
|
||||
buttons.push(button);
|
||||
rows.append(button);
|
||||
}
|
||||
paint();
|
||||
part.append(rows);
|
||||
}
|
||||
if (field.custom === true || options.length === 0) {
|
||||
@@ -3176,6 +3207,7 @@ function renderChatQuestion(sessionId, form) {
|
||||
type: "text",
|
||||
placeholder: options.length ? "Type another answer" : "Your answer",
|
||||
autocomplete: "off",
|
||||
value: typeof answers[key] === "string" ? answers[key] : "",
|
||||
});
|
||||
input.addEventListener("input", () => {
|
||||
const value = input.value.trim();
|
||||
@@ -4186,7 +4218,7 @@ function updateChatJump(el) {
|
||||
function refreshChatClock() {
|
||||
chatNow = Date.now();
|
||||
const thread = state.chatActiveId ? state.chatThreads.get(state.chatActiveId) : null;
|
||||
const awaiting = approvalCard(thread?.permission, thread?.permissionAnswer)?.actionable === true;
|
||||
const awaiting = chatApprovalCards(thread).some((card) => card.actionable) || chatPendingForms(thread).length > 0;
|
||||
if (state.view !== "chat" || !thread?.running || awaiting) {
|
||||
stopChatClock();
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user