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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user