Files
openclaw/src/infra/exec-approval-surface.ts
Ted Li b67df3797a fix(telegram): explain disabled plugin approval failures (#95973)
* fix: explain disabled Telegram plugin approvals

* fix: handle disabled Telegram native plugin approvals

* fix: resolve exec approval surface independently

* fix: tie plugin approval setup guidance to plugin surface

* Revert "fix: tie plugin approval setup guidance to plugin surface"

This reverts commit 34e3c10e23ec8d3cf00ff803188df4226cd60f8d.

* fix: gate plugin approval setup guidance

* fix: gate plugin approval availability by delivery

* fix: preserve plugin approval approver auth

* fix: make plugin setup guidance explicit

* test: trim approval guidance proof radius

* Avoid setup guidance on routed approval timeout

* Carry plugin approval delivery route into timeouts

* Keep turn-source approval route visible

* fix(approvals): prefer delivered approval clients

* fix(approvals): prefer delivered approval clients

---------

Co-authored-by: Peter Steinberger <steipete@gmail.com>
2026-07-02 14:15:25 -07:00

157 lines
5.8 KiB
TypeScript

// Resolves native approval support for the initiating channel surface.
import { normalizeOptionalString } from "@openclaw/normalization-core/string-coerce";
import {
getChannelPlugin,
listChannelPlugins,
resolveChannelApprovalCapability,
} from "../channels/plugins/index.js";
import { getRuntimeConfig, type OpenClawConfig } from "../config/config.js";
import {
INTERNAL_MESSAGE_CHANNEL,
isDeliverableMessageChannel,
normalizeMessageChannel,
} from "../utils/message-channel.js";
/** Native approval availability for the channel/account that initiated an approval. */
export type ExecApprovalInitiatingSurfaceState =
| { kind: "enabled"; channel: string | undefined; channelLabel: string; accountId?: string }
| { kind: "disabled"; channel: string; channelLabel: string; accountId?: string }
| { kind: "unsupported"; channel: string; channelLabel: string; accountId?: string };
type ApprovalKind = "exec" | "plugin";
function labelForChannel(channel?: string): string {
if (channel === "tui") {
return "terminal UI";
}
if (channel === INTERNAL_MESSAGE_CHANNEL) {
return "Web UI";
}
return (
getChannelPlugin(channel ?? "")?.meta.label ??
(channel ? channel[0]?.toUpperCase() + channel.slice(1) : "this platform")
);
}
function hasNativeExecApprovalCapability(channel?: string): boolean {
const capability = resolveChannelApprovalCapability(getChannelPlugin(channel ?? ""));
if (!capability?.native) {
return false;
}
return Boolean(capability.getExecInitiatingSurfaceState || capability.getActionAvailabilityState);
}
/** Resolves whether exec approvals can be handled on the initiating surface. */
export function resolveExecApprovalInitiatingSurfaceState(params: {
channel?: string | null;
accountId?: string | null;
cfg?: OpenClawConfig;
}): ExecApprovalInitiatingSurfaceState {
return resolveApprovalInitiatingSurfaceState({ ...params, approvalKind: "exec" });
}
/** Resolves whether approvals of a given kind can be handled on the initiating surface. */
export function resolveApprovalInitiatingSurfaceState(params: {
channel?: string | null;
accountId?: string | null;
cfg?: OpenClawConfig;
approvalKind: ApprovalKind;
}): ExecApprovalInitiatingSurfaceState {
const channel = normalizeMessageChannel(params.channel);
const channelLabel = labelForChannel(channel);
const accountId = normalizeOptionalString(params.accountId);
if (!channel || channel === INTERNAL_MESSAGE_CHANNEL || channel === "tui") {
return { kind: "enabled", channel, channelLabel, accountId };
}
const cfg = params.cfg ?? getRuntimeConfig();
const capability = resolveChannelApprovalCapability(getChannelPlugin(channel));
// Prefer the exec-specific hook, then the generic approval hook, before
// falling back to basic deliverability for channels without native state.
const state =
(params.approvalKind === "exec"
? capability?.getExecInitiatingSurfaceState?.({
cfg,
accountId: params.accountId,
action: "approve",
})
: undefined) ??
capability?.getActionAvailabilityState?.({
cfg,
accountId: params.accountId,
action: "approve",
approvalKind: params.approvalKind,
});
if (state) {
return { ...state, channel, channelLabel, accountId };
}
if (isDeliverableMessageChannel(channel)) {
return { kind: "enabled", channel, channelLabel, accountId };
}
return { kind: "unsupported", channel, channelLabel, accountId };
}
/** Returns whether a channel can present native exec approval UI. */
export function supportsNativeExecApprovalClient(channel?: string | null): boolean {
const normalized = normalizeMessageChannel(channel);
if (!normalized || normalized === INTERNAL_MESSAGE_CHANNEL || normalized === "tui") {
return true;
}
return hasNativeExecApprovalCapability(normalized);
}
/** Lists native exec approval client labels for reply guidance. */
export function listNativeExecApprovalClientLabels(params?: {
excludeChannel?: string | null;
}): string[] {
const excludeChannel = normalizeMessageChannel(params?.excludeChannel);
return listChannelPlugins()
.filter((plugin) => plugin.id !== excludeChannel)
.filter((plugin) => hasNativeExecApprovalCapability(plugin.id))
.map((plugin) => normalizeOptionalString(plugin.meta.label))
.filter((label): label is string => Boolean(label))
.toSorted((a, b) => a.localeCompare(b));
}
/** Returns channel-specific setup guidance for native exec approvals, when available. */
export function describeNativeExecApprovalClientSetup(params: {
channel?: string | null;
channelLabel?: string | null;
accountId?: string | null;
}): string | null {
const channel = normalizeMessageChannel(params.channel);
if (!channel || channel === INTERNAL_MESSAGE_CHANNEL || channel === "tui") {
return null;
}
const channelLabel = normalizeOptionalString(params.channelLabel) ?? labelForChannel(channel);
const accountId = normalizeOptionalString(params.accountId);
return (
resolveChannelApprovalCapability(getChannelPlugin(channel))?.describeExecApprovalSetup?.({
channel,
channelLabel,
accountId,
}) ?? null
);
}
/** Returns channel-specific setup guidance for native plugin approvals, when available. */
export function describeNativePluginApprovalClientSetup(params: {
channel?: string | null;
channelLabel?: string | null;
accountId?: string | null;
}): string | null {
const channel = normalizeMessageChannel(params.channel);
if (!channel || channel === INTERNAL_MESSAGE_CHANNEL || channel === "tui") {
return null;
}
const channelLabel = normalizeOptionalString(params.channelLabel) ?? labelForChannel(channel);
const accountId = normalizeOptionalString(params.accountId);
return (
resolveChannelApprovalCapability(getChannelPlugin(channel))?.describePluginApprovalSetup?.({
channel,
channelLabel,
accountId,
}) ?? null
);
}