Skip to content

Commit 6d90e00

Browse files
committed
refactor: dedupe channel approval forwarding
1 parent b0e9569 commit 6d90e00

9 files changed

Lines changed: 349 additions & 675 deletions

extensions/imessage/src/approval-handler.runtime.ts

Lines changed: 6 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
11
import {
2+
buildChannelApprovalExpiredText,
3+
buildChannelApprovalResolvedText,
24
createChannelApprovalNativeRuntimeAdapter,
3-
type ExpiredApprovalView,
45
type PendingApprovalView,
5-
type ResolvedApprovalView,
6+
resolvePreparedApprovalAccountId,
67
} from "openclaw/plugin-sdk/approval-handler-runtime";
78
import { buildChannelApprovalNativeTargetKey } from "openclaw/plugin-sdk/approval-native-runtime";
89
import { buildApprovalReactionPendingContent } from "openclaw/plugin-sdk/approval-reaction-runtime";
910
import type { ExecApprovalReplyDecision } from "openclaw/plugin-sdk/approval-reply-runtime";
1011
import {
11-
buildApprovalResolvedReplyPayload,
12-
buildPluginApprovalExpiredMessage,
13-
buildPluginApprovalResolvedMessage,
1412
type ExecApprovalRequest,
15-
type ExecApprovalResolved,
1613
type PluginApprovalRequest,
17-
type PluginApprovalResolved,
1814
} from "openclaw/plugin-sdk/approval-runtime";
1915
import { createSubsystemLogger } from "openclaw/plugin-sdk/runtime-env";
20-
import { normalizeOptionalString } from "openclaw/plugin-sdk/string-coerce-runtime";
2116
import {
2217
registerIMessageApprovalReactionTarget,
2318
unregisterIMessageApprovalReactionTarget,
@@ -30,7 +25,6 @@ import { normalizeIMessageHandle, parseIMessageTarget } from "./targets.js";
3025
const log = createSubsystemLogger("imessage/approvals");
3126

3227
type ApprovalRequest = ExecApprovalRequest | PluginApprovalRequest;
33-
type ApprovalResolved = ExecApprovalResolved | PluginApprovalResolved;
3428
type IMessagePendingDelivery = {
3529
text: string;
3630
allowedDecisions: readonly ExecApprovalReplyDecision[];
@@ -66,42 +60,6 @@ function buildPendingPayload(params: {
6660
};
6761
}
6862

69-
function buildResolvedText(params: {
70-
request: ApprovalRequest;
71-
resolved: ApprovalResolved;
72-
view: ResolvedApprovalView;
73-
}): string {
74-
if (params.view.approvalKind === "plugin") {
75-
return buildPluginApprovalResolvedMessage(params.resolved as PluginApprovalResolved);
76-
}
77-
const resolvedByText = params.resolved.resolvedBy
78-
? ` Resolved by ${params.resolved.resolvedBy}.`
79-
: "";
80-
const payload = buildApprovalResolvedReplyPayload({
81-
approvalId: params.request.id,
82-
approvalSlug: params.request.id.slice(0, 8),
83-
text: `✅ Exec approval ${params.resolved.decision}.${resolvedByText} ID: ${params.request.id}`,
84-
});
85-
return payload.text ?? "";
86-
}
87-
88-
function buildExpiredText(params: { request: ApprovalRequest; view: ExpiredApprovalView }): string {
89-
if (params.view.approvalKind === "plugin") {
90-
return buildPluginApprovalExpiredMessage(params.request as PluginApprovalRequest);
91-
}
92-
return `⏱️ Exec approval expired. ID: ${params.request.id}`;
93-
}
94-
95-
function resolvePreparedAccountId(params: {
96-
plannedAccountId?: string | null;
97-
contextAccountId?: string | null;
98-
}): string | undefined {
99-
return (
100-
normalizeOptionalString(params.plannedAccountId) ??
101-
normalizeOptionalString(params.contextAccountId)
102-
);
103-
}
104-
10563
function buildConversationKeyForTarget(to: string): IMessageApprovalConversationKey | null {
10664
try {
10765
const parsed = parseIMessageTarget(to);
@@ -138,11 +96,11 @@ export const imessageApprovalNativeRuntime = createChannelApprovalNativeRuntimeA
13896
buildPendingPayload({ request, approvalKind, nowMs, view }),
13997
buildResolvedResult: ({ request, resolved, view }) => ({
14098
kind: "update",
141-
payload: { text: buildResolvedText({ request, resolved, view }) },
99+
payload: { text: buildChannelApprovalResolvedText({ request, resolved, view }) },
142100
}),
143101
buildExpiredResult: ({ request, view }) => ({
144102
kind: "update",
145-
payload: { text: buildExpiredText({ request, view }) },
103+
payload: { text: buildChannelApprovalExpiredText({ request, view }) },
146104
}),
147105
},
148106
transport: {
@@ -153,7 +111,7 @@ export const imessageApprovalNativeRuntime = createChannelApprovalNativeRuntimeA
153111
}
154112
const prepared: PreparedIMessageApprovalTarget = {
155113
to,
156-
accountId: resolvePreparedAccountId({
114+
accountId: resolvePreparedApprovalAccountId({
157115
plannedAccountId: (plannedTarget.target as { accountId?: string | null }).accountId,
158116
contextAccountId: accountId,
159117
}),

0 commit comments

Comments
 (0)