mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 06:50:43 +00:00
refactor: share ui approval event handling
This commit is contained in:
@@ -119,6 +119,25 @@ type GatewayHostWithSideResults = GatewayHost & {
|
||||
chatSideResultTerminalRuns?: Set<string>;
|
||||
};
|
||||
|
||||
function enqueueApprovalRequest(host: GatewayHost, entry: ExecApprovalRequest | null) {
|
||||
if (!entry) {
|
||||
return;
|
||||
}
|
||||
host.execApprovalQueue = addExecApproval(host.execApprovalQueue, entry);
|
||||
host.execApprovalError = null;
|
||||
const delay = Math.max(0, entry.expiresAtMs - Date.now() + 500);
|
||||
window.setTimeout(() => {
|
||||
host.execApprovalQueue = removeExecApproval(host.execApprovalQueue, entry.id);
|
||||
}, delay);
|
||||
}
|
||||
|
||||
function removeResolvedApprovalRequest(host: GatewayHost, payload: unknown) {
|
||||
const resolved = parseExecApprovalResolved(payload);
|
||||
if (resolved) {
|
||||
host.execApprovalQueue = removeExecApproval(host.execApprovalQueue, resolved.id);
|
||||
}
|
||||
}
|
||||
|
||||
function isTerminalChatState(
|
||||
state: ChatEventPayload["state"] | ReturnType<typeof handleChatEvent> | null | undefined,
|
||||
): state is "final" | "aborted" | "error" {
|
||||
@@ -542,44 +561,22 @@ function handleGatewayEventUnsafe(host: GatewayHost, evt: GatewayEventFrame) {
|
||||
}
|
||||
|
||||
if (evt.event === "exec.approval.requested") {
|
||||
const entry = parseExecApprovalRequested(evt.payload);
|
||||
if (entry) {
|
||||
host.execApprovalQueue = addExecApproval(host.execApprovalQueue, entry);
|
||||
host.execApprovalError = null;
|
||||
const delay = Math.max(0, entry.expiresAtMs - Date.now() + 500);
|
||||
window.setTimeout(() => {
|
||||
host.execApprovalQueue = removeExecApproval(host.execApprovalQueue, entry.id);
|
||||
}, delay);
|
||||
}
|
||||
enqueueApprovalRequest(host, parseExecApprovalRequested(evt.payload));
|
||||
return;
|
||||
}
|
||||
|
||||
if (evt.event === "exec.approval.resolved") {
|
||||
const resolved = parseExecApprovalResolved(evt.payload);
|
||||
if (resolved) {
|
||||
host.execApprovalQueue = removeExecApproval(host.execApprovalQueue, resolved.id);
|
||||
}
|
||||
removeResolvedApprovalRequest(host, evt.payload);
|
||||
return;
|
||||
}
|
||||
|
||||
if (evt.event === "plugin.approval.requested") {
|
||||
const entry = parsePluginApprovalRequested(evt.payload);
|
||||
if (entry) {
|
||||
host.execApprovalQueue = addExecApproval(host.execApprovalQueue, entry);
|
||||
host.execApprovalError = null;
|
||||
const delay = Math.max(0, entry.expiresAtMs - Date.now() + 500);
|
||||
window.setTimeout(() => {
|
||||
host.execApprovalQueue = removeExecApproval(host.execApprovalQueue, entry.id);
|
||||
}, delay);
|
||||
}
|
||||
enqueueApprovalRequest(host, parsePluginApprovalRequested(evt.payload));
|
||||
return;
|
||||
}
|
||||
|
||||
if (evt.event === "plugin.approval.resolved") {
|
||||
const resolved = parseExecApprovalResolved(evt.payload);
|
||||
if (resolved) {
|
||||
host.execApprovalQueue = removeExecApproval(host.execApprovalQueue, resolved.id);
|
||||
}
|
||||
removeResolvedApprovalRequest(host, evt.payload);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user