Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/openclaw/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ export default defineChannelPluginEntry({
if ('error' in result) {
return { text: result.error };
}
return { text: await result.bridge.getPendingList() };
return await result.bridge.getPendingApprovalsReply();
},
});

Expand Down
68 changes: 68 additions & 0 deletions packages/openclaw/src/monitor/approval.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
type DisplayContext,
type PendingApproval,
buildApprovalA2UIBlob,
buildPendingApprovalsA2UIBlob,
createPendingApproval,
emojiToApprovalAction,
findPendingApproval,
Expand Down Expand Up @@ -399,6 +400,73 @@ describe('buildApprovalA2UIBlob', () => {
});
});

describe('buildPendingApprovalsA2UIBlob', () => {
it('builds a pending requests card with actions for each approval', () => {
const blob = buildPendingApprovalsA2UIBlob(
[
{
id: 'da1b2',
type: 'dm',
requestingShip: '~zod',
messagePreview: 'Can you help me find the launch notes?',
timestamp: Date.now(),
},
{
id: 'cc3d4',
type: 'channel',
requestingShip: '~sampel-palnet',
channelNest: 'chat/~host/general',
timestamp: Date.now(),
},
],
ctx
);

expect(blob).toBeDefined();
expect(A2UI.validateBlobEntry(blob)).toBe(true);
const text = JSON.stringify(blob);
expect(text).toContain('2 approval requests');
expect(text).toContain('DM from Zod');
expect(text).toContain('Sender: Zod (~zod)');
expect(text).toContain('Message: ');
expect(text).toContain('Channel access for Sam Palnet');
expect(text).toContain(
'Channel: General in Garden Club (chat/~host/general)'
);
expect(text).toContain('/allow da1b2');
expect(text).toContain('/reject cc3d4');
expect(text).toContain('/ban cc3d4');
});

it('omits the card when there are no active approvals', () => {
expect(buildPendingApprovalsA2UIBlob([], ctx)).toBeUndefined();
expect(
buildPendingApprovalsA2UIBlob(
[
{
id: 'da1b2',
type: 'dm',
requestingShip: '~zod',
timestamp: Date.now() - APPROVAL_TTL_MS - 1,
},
],
ctx
)
).toBeUndefined();
});

it('omits the card for five or more active approvals', () => {
const approvals = Array.from({ length: 5 }, (_, index) => ({
id: `d${index}`,
type: 'dm' as const,
requestingShip: `~ship${index}`,
timestamp: Date.now(),
}));

expect(buildPendingApprovalsA2UIBlob(approvals, ctx)).toBeUndefined();
});
});

describe('formatApprovalConfirmation', () => {
it('shows ship in confirmation', () => {
const approval: PendingApproval = {
Expand Down
162 changes: 162 additions & 0 deletions packages/openclaw/src/monitor/approval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,168 @@ export function buildApprovalA2UIBlob(
});
}

// ============================================================================
// Pending Requests A2UI
// ============================================================================

const MAX_PENDING_APPROVALS_A2UI = 4;

function shouldUsePendingApprovalsA2UI(
activeApprovals: PendingApproval[]
): boolean {
return (
activeApprovals.length > 0 &&
activeApprovals.length <= MAX_PENDING_APPROVALS_A2UI
);
}

function pendingItemFields(
approval: PendingApproval,
ctx?: DisplayContext
): { title: string; context: string; preview?: string } {
const name = displayShipName(approval.requestingShip, ctx);
const ship = displayShipWithId(approval.requestingShip, ctx);
const preview = approval.messagePreview
? `Message: "${truncate(approval.messagePreview, 100)}"`
: undefined;

switch (approval.type) {
case 'dm':
return { title: `DM from ${name}`, context: `Sender: ${ship}`, preview };
case 'channel':
return {
title: `Channel access for ${name}`,
context: `Channel: ${displayChannel(approval.channelNest ?? '', ctx)}`,
preview,
};
case 'group':
return {
title: `Group invite from ${name}`,
context: `Group: ${displayGroup(approval.groupFlag ?? '', ctx, approval.groupTitle)}`,
};
}
return assertNever(approval.type);
}

export function buildPendingApprovalsA2UIBlob(
approvals: PendingApproval[],
ctx?: DisplayContext
): TlonA2UIBlob | undefined {
const active = pruneExpired(approvals);
if (!shouldUsePendingApprovalsA2UI(active)) {
return undefined;
}

const text = (
id: string,
value: string,
variant?: A2UI.Text['variant']
): A2UI.Component =>
variant
? { id, component: 'Text', variant, text: value }
: { id, component: 'Text', text: value };
const button = (
id: string,
labelId: string,
command: string,
variant?: A2UI.Button['variant']
): A2UI.Component => ({
id,
component: 'Button',
...(variant ? { variant } : {}),
child: labelId,
action: {
event: {
name: A2UI.action.sendMessage,
context: { text: command },
},
},
});

const bodyChildren = ['eyebrow', 'title', 'titleDivider'];
const components: A2UI.Component[] = [
{ id: 'root', component: 'Card', child: 'body' },
{ id: 'body', component: 'Column', children: bodyChildren },
text('eyebrow', 'Pending requests', 'caption'),
text(
'title',
`${active.length} approval ${active.length === 1 ? 'request' : 'requests'}`,
'h3'
),
{ id: 'titleDivider', component: 'Divider' },
text('allowLabel', 'Allow'),
text('rejectLabel', 'Reject'),
text('blockLabel', 'Block'),
];

for (const [index, approval] of active.entries()) {
const prefix = `item${index}`;
const fields = pendingItemFields(approval, ctx);

if (index > 0) {
const dividerId = `${prefix}Divider`;
bodyChildren.push(dividerId);
components.push({ id: dividerId, component: 'Divider' });
}

bodyChildren.push(prefix);
components.push(
{
id: prefix,
component: 'Column',
children: [
`${prefix}Title`,
`${prefix}Context`,
...(fields.preview ? [`${prefix}Preview`] : []),
`${prefix}Actions`,
],
},
text(`${prefix}Title`, fields.title, 'h4'),
text(`${prefix}Context`, fields.context, 'caption'),
...(fields.preview
? [text(`${prefix}Preview`, fields.preview, 'caption')]
: []),
{
id: `${prefix}Actions`,
component: 'Row',
children: [`${prefix}Allow`, `${prefix}Reject`, `${prefix}Block`],
},
button(
`${prefix}Allow`,
'allowLabel',
`/allow ${approval.id}`,
'primary'
),
button(`${prefix}Reject`, 'rejectLabel', `/reject ${approval.id}`),
button(
`${prefix}Block`,
'blockLabel',
`/ban ${approval.id}`,
'borderless'
)
);
}

return makeA2UIBlob('pending-approvals', 'root', components);
}

export function buildPendingApprovalsResponse(
approvals: PendingApproval[],
ctx: DisplayContext | undefined,
serializeBlob: (blob: TlonA2UIBlob) => string | undefined
): { text: string; mode: 'text' } | { text: string; mode: 'ui'; blob: string } {
const text = formatPendingList(approvals, ctx);
if (!shouldUsePendingApprovalsA2UI(approvals)) {
return { text, mode: 'text' };
}

const blob = buildPendingApprovalsA2UIBlob(approvals, ctx);
const serialized = blob ? serializeBlob(blob) : undefined;
return serialized
? { text, blob: serialized, mode: 'ui' }
: { text, mode: 'text' };
}

// ============================================================================
// Approval Lookup & Removal
// ============================================================================
Expand Down
2 changes: 1 addition & 1 deletion packages/openclaw/src/monitor/command-auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function makeBridge(ownerShip: string | null): ApprovalCommandBridge {
return {
ownerShip,
handleAction: async () => 'ok',
getPendingList: async () => 'none',
getPendingApprovalsReply: async () => ({ text: 'none' }),
getBlockedList: async () => 'none',
handleUnblock: async () => 'ok',
isOwnedChannel: () => false,
Expand Down
7 changes: 5 additions & 2 deletions packages/openclaw/src/monitor/command-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ export interface ApprovalCommandBridge {
action: 'approve' | 'deny' | 'block',
id?: string
): Promise<string>;
/** Get formatted pending approvals list. */
getPendingList(): Promise<string>;
/** Build the /pending command response. */
getPendingApprovalsReply(): Promise<{
text?: string;
channelData?: Record<string, unknown>;
}>;
/** Get formatted blocked ships list. */
getBlockedList(): Promise<string>;
/** Handle /unban ~ship. Returns response text. */
Expand Down
Loading
Loading