fix(chat): recover web approvals and questions across clients and reconnects

This commit is contained in:
2026-10-20 17:23:07 +01:00
parent ed0abb976d
commit c78def70dd
16 changed files with 420 additions and 54 deletions
+25 -6
View File
@@ -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);
}