From c6a0452d1368095b491dd5fc377729eee081590e Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Mon, 20 Apr 2026 13:52:20 +0100 Subject: [PATCH] refactor: share approval session lookup --- src/infra/approval-request-account-binding.ts | 24 ++++++++++++++++--- src/infra/exec-approval-session-target.ts | 21 ++++++++-------- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/src/infra/approval-request-account-binding.ts b/src/infra/approval-request-account-binding.ts index a2d5644dd77..1cc113fe919 100644 --- a/src/infra/approval-request-account-binding.ts +++ b/src/infra/approval-request-account-binding.ts @@ -1,5 +1,6 @@ import { resolveStorePath } from "../config/sessions/paths.js"; import { loadSessionStore } from "../config/sessions/store-load.js"; +import type { SessionEntry } from "../config/sessions/types.js"; import type { OpenClawConfig } from "../config/types.openclaw.js"; import { normalizeOptionalAccountId } from "../routing/account-id.js"; import { parseAgentSessionKey } from "../routing/session-key.js"; @@ -8,21 +9,26 @@ import { normalizeMessageChannel } from "../utils/message-channel.js"; import type { ExecApprovalRequest } from "./exec-approvals.js"; import type { PluginApprovalRequest } from "./plugin-approvals.js"; -type ApprovalRequestLike = ExecApprovalRequest | PluginApprovalRequest; +export type ApprovalRequestLike = ExecApprovalRequest | PluginApprovalRequest; type ApprovalRequestSessionBinding = { channel?: string; accountId?: string; }; +export type PersistedApprovalRequestSessionEntry = { + sessionKey: string; + entry: SessionEntry; +}; + function normalizeOptionalChannel(value?: string | null): string | undefined { return normalizeMessageChannel(value); } -function resolvePersistedApprovalRequestSessionBinding(params: { +export function resolvePersistedApprovalRequestSessionEntry(params: { cfg: OpenClawConfig; request: ApprovalRequestLike; -}): ApprovalRequestSessionBinding | null { +}): PersistedApprovalRequestSessionEntry | null { const sessionKey = normalizeOptionalString(params.request.request.sessionKey); if (!sessionKey) { return null; @@ -35,6 +41,18 @@ function resolvePersistedApprovalRequestSessionBinding(params: { if (!entry) { return null; } + return { sessionKey, entry }; +} + +function resolvePersistedApprovalRequestSessionBinding(params: { + cfg: OpenClawConfig; + request: ApprovalRequestLike; +}): ApprovalRequestSessionBinding | null { + const persisted = resolvePersistedApprovalRequestSessionEntry(params); + if (!persisted) { + return null; + } + const { entry } = persisted; const channel = normalizeOptionalChannel(entry.origin?.provider ?? entry.lastChannel); const accountId = normalizeOptionalAccountId(entry.origin?.accountId ?? entry.lastAccountId); return channel || accountId ? { channel, accountId } : null; diff --git a/src/infra/exec-approval-session-target.ts b/src/infra/exec-approval-session-target.ts index 8c2b8a1a8bf..0cf32fe0a13 100644 --- a/src/infra/exec-approval-session-target.ts +++ b/src/infra/exec-approval-session-target.ts @@ -1,11 +1,11 @@ import { resolveSessionConversationRef } from "../channels/plugins/session-conversation.js"; -import { resolveStorePath } from "../config/sessions/paths.js"; -import { loadSessionStore } from "../config/sessions/store-load.js"; import type { OpenClawConfig } from "../config/types.openclaw.js"; -import { parseAgentSessionKey } from "../routing/session-key.js"; import { normalizeOptionalString } from "../shared/string-coerce.js"; import { normalizeMessageChannel } from "../utils/message-channel.js"; -import { doesApprovalRequestMatchChannelAccount } from "./approval-request-account-binding.js"; +import { + doesApprovalRequestMatchChannelAccount, + resolvePersistedApprovalRequestSessionEntry, +} from "./approval-request-account-binding.js"; import type { ExecApprovalRequest } from "./exec-approvals.js"; import { resolveSessionDeliveryTarget } from "./outbound/targets.js"; import type { PluginApprovalRequest } from "./plugin-approvals.js"; @@ -127,17 +127,16 @@ export function resolveExecApprovalSessionTarget(params: { if (!sessionKey) { return null; } - const parsed = parseAgentSessionKey(sessionKey); - const agentId = parsed?.agentId ?? params.request.request.agentId ?? "main"; - const storePath = resolveStorePath(params.cfg.session?.store, { agentId }); - const store = loadSessionStore(storePath); - const entry = store[sessionKey]; - if (!entry) { + const persisted = resolvePersistedApprovalRequestSessionEntry({ + cfg: params.cfg, + request: params.request, + }); + if (!persisted) { return null; } const target = resolveSessionDeliveryTarget({ - entry, + entry: persisted.entry, requestedChannel: "last", turnSourceChannel: normalizeOptionalString(params.turnSourceChannel), turnSourceTo: normalizeOptionalString(params.turnSourceTo),