mirror of
https://github.com/openclaw/openclaw.git
synced 2026-08-27 12:56:01 -06:00
7a456e362d
* fix(gateway): approval registry hardening and protocol-surface follow-ups Follow-up delta to the merged #103579 head, rebased onto current main: - gateway-protocol wire types derive from owner-module schema consts (types.ts tombstone) and ProtocolSchemas leaves the package index so the public plugin-sdk d.ts graph tree-shakes the registry declaration - approval access authority follows the operator.approvals scope tier with reviewerDeviceIds as the opt-in restriction (cross-surface first-answer-wins; requester identity gates only legacy adapters) - plugin node.invoke approvals register directly so unrenderable presentations fail closed before request routing - exec-approval manager reconciliation with #103515 revocation hardening (resolution source attribution, one-shot ask-fallback consumption) - surface-report pins and plugin-sdk API baseline refreshed; Swift models regenerated * feat(channels): add typed operator approval actions Squash-rebased #103679 segment onto the durable-approval-registry tip on current main. Typed approval/command/select presentation actions replace raw-string inference across slack/telegram/discord/matrix/imessage/whatsapp, approval.resolve carries an explicit kind, and channel adapters map native callback envelopes through the typed action registry. Drift reconciliation: deprecated buildExecApprovalInteractiveReply assertions dropped (#104650 removed the shims); worker_environments bootstrap-column migration kept alongside the approval resolution_ref backfill; plugin-sdk API baseline regenerated. (cherry picked from commit 68765a5d39d2118c88a7a54d00387337912d4494) (cherry picked from commit 8642ac12af142e4b751f4f30d4b114615e7e5f66) (cherry picked from commit 036c4bc39499925fc03de16ec9302e346769350a) (cherry picked from commit 19dc350d6bc34e29a5169c6bc80971b0ad12adde) (cherry picked from commit fc978b0bad86aef421c79f6a211b25cc1b743c01) (cherry picked from commit 10de4d1ed5071f9be6ad1ee5d1e32c0fa8c9d11c) (cherry picked from commit 9a664ced1b1fa740172b258f355f1a82925ae41c) (cherry picked from commit c5ff69abbf444139e9e007bfa45beb0f00ffea54) (cherry picked from commit d466a80795f7bc04639f1538f4e412bca3ab96bf) (cherry picked from commit f5b4fe40dd5c961322f8553cc80b2fdfb3f6503e) (cherry picked from commit 7340b4749a4cc4c72f7a41cce1bc9cb550cae038) (cherry picked from commit a151f41808f23ae60b10305ccd2bc959b9169a86) * fix(approvals): preserve typed transport ownership * test(imessage): narrow chunked approval text * refactor(protocol): remove retired type tombstone * fix(plugin-sdk): align surface budgets after rebase * docs(changelog): note typed operator approvals * docs(changelog): defer typed approval release note
275 lines
7.3 KiB
TypeScript
275 lines
7.3 KiB
TypeScript
// Feishu plugin module implements presentation card behavior.
|
|
import {
|
|
normalizeMessagePresentation,
|
|
renderMessagePresentationChartFallbackText,
|
|
renderMessagePresentationFallbackText,
|
|
renderMessagePresentationTableFallbackText,
|
|
type MessagePresentationBlock,
|
|
type MessagePresentationButton,
|
|
} from "openclaw/plugin-sdk/interactive-runtime";
|
|
import { createFeishuCardInteractionEnvelope } from "./card-interaction.js";
|
|
|
|
type NormalizedMessagePresentation = NonNullable<ReturnType<typeof normalizeMessagePresentation>>;
|
|
|
|
const FEISHU_CARD_MAX_BYTES = 30 * 1024;
|
|
const FEISHU_CARD_MAX_ELEMENTS = 200;
|
|
|
|
function countFeishuCardElements(value: unknown, ancestors = new Set<object>()): number {
|
|
if (Array.isArray(value)) {
|
|
return value.reduce((count, entry) => count + countFeishuCardElements(entry, ancestors), 0);
|
|
}
|
|
if (!value || typeof value !== "object") {
|
|
return 0;
|
|
}
|
|
if (ancestors.has(value)) {
|
|
return FEISHU_CARD_MAX_ELEMENTS + 1;
|
|
}
|
|
ancestors.add(value);
|
|
const record = value as Record<string, unknown>;
|
|
let count = typeof record.tag === "string" ? 1 : 0;
|
|
for (const entry of Object.values(record)) {
|
|
count += countFeishuCardElements(entry, ancestors);
|
|
if (count > FEISHU_CARD_MAX_ELEMENTS) {
|
|
break;
|
|
}
|
|
}
|
|
ancestors.delete(value);
|
|
return count;
|
|
}
|
|
|
|
export function isFeishuCardWithinEnvelope(card: Record<string, unknown>): boolean {
|
|
try {
|
|
return (
|
|
Buffer.byteLength(JSON.stringify(card), "utf8") <= FEISHU_CARD_MAX_BYTES &&
|
|
countFeishuCardElements(card) <= FEISHU_CARD_MAX_ELEMENTS
|
|
);
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export function assertFeishuCardWithinEnvelope(
|
|
card: Record<string, unknown>,
|
|
label = "Feishu card",
|
|
): void {
|
|
if (!isFeishuCardWithinEnvelope(card)) {
|
|
throw new Error(`${label} exceeds the 30 KB or 200-element API limit.`);
|
|
}
|
|
}
|
|
|
|
function escapeFeishuCardMarkdownText(text: string): string {
|
|
return text.replace(/[&<>]/g, (char) => {
|
|
switch (char) {
|
|
case "&":
|
|
return "&";
|
|
case "<":
|
|
return "<";
|
|
case ">":
|
|
return ">";
|
|
default:
|
|
return char;
|
|
}
|
|
});
|
|
}
|
|
|
|
function resolveSafeFeishuButtonUrl(url: string | undefined): string | undefined {
|
|
const trimmed = url?.trim();
|
|
if (!trimmed) {
|
|
return undefined;
|
|
}
|
|
try {
|
|
const parsed = new URL(trimmed);
|
|
return parsed.protocol === "https:" || parsed.protocol === "http:" ? trimmed : undefined;
|
|
} catch {
|
|
return undefined;
|
|
}
|
|
}
|
|
|
|
function resolveFeishuButtonUrl(button: MessagePresentationButton): string | undefined {
|
|
if (button.action?.type === "url" || button.action?.type === "web-app") {
|
|
return button.action.url;
|
|
}
|
|
if (button.action) {
|
|
return undefined;
|
|
}
|
|
return button.url ?? button.webApp?.url ?? button.web_app?.url;
|
|
}
|
|
|
|
function resolveFeishuCommandButtonValue(button: MessagePresentationButton): string | undefined {
|
|
if (button.action?.type === "command") {
|
|
return button.action.command;
|
|
}
|
|
if (button.action) {
|
|
return undefined;
|
|
}
|
|
return button.value;
|
|
}
|
|
|
|
function mapFeishuButtonType(style: MessagePresentationButton["style"]) {
|
|
if (style === "primary" || style === "success") {
|
|
return "primary";
|
|
}
|
|
if (style === "danger") {
|
|
return "danger";
|
|
}
|
|
return "default";
|
|
}
|
|
|
|
function buildFeishuPayloadButton(
|
|
button: MessagePresentationButton,
|
|
): Record<string, unknown> | undefined {
|
|
const behaviors: Record<string, unknown>[] = [];
|
|
const rendered: Record<string, unknown> = {
|
|
tag: "button",
|
|
text: {
|
|
tag: "plain_text",
|
|
content: button.label,
|
|
},
|
|
type: mapFeishuButtonType(button.style),
|
|
};
|
|
const url = resolveFeishuButtonUrl(button);
|
|
if (url) {
|
|
const safeUrl = resolveSafeFeishuButtonUrl(url);
|
|
if (safeUrl) {
|
|
behaviors.push({ type: "open_url", default_url: safeUrl });
|
|
}
|
|
}
|
|
const value = resolveFeishuCommandButtonValue(button);
|
|
if (value) {
|
|
behaviors.push({
|
|
type: "callback",
|
|
value: createFeishuCardInteractionEnvelope({
|
|
k: "quick",
|
|
a: "feishu.payload.button",
|
|
q: value,
|
|
}),
|
|
});
|
|
}
|
|
if (behaviors.length === 0) {
|
|
return undefined;
|
|
}
|
|
rendered.behaviors = behaviors;
|
|
return rendered;
|
|
}
|
|
|
|
function buildFeishuCardElementsForBlock(
|
|
block: MessagePresentationBlock,
|
|
): Record<string, unknown>[] {
|
|
if (block.type === "text") {
|
|
return [{ tag: "markdown", content: escapeFeishuCardMarkdownText(block.text) }];
|
|
}
|
|
if (block.type === "context") {
|
|
return [
|
|
{
|
|
tag: "markdown",
|
|
content: `<font color='grey'>${escapeFeishuCardMarkdownText(block.text)}</font>`,
|
|
},
|
|
];
|
|
}
|
|
if (block.type === "divider") {
|
|
return [{ tag: "hr" }];
|
|
}
|
|
if (block.type === "buttons") {
|
|
return block.buttons
|
|
.map((button) => buildFeishuPayloadButton(button))
|
|
.filter((button): button is Record<string, unknown> => Boolean(button));
|
|
}
|
|
if (block.type === "chart") {
|
|
return [
|
|
{
|
|
tag: "markdown",
|
|
content: escapeFeishuCardMarkdownText(renderMessagePresentationChartFallbackText(block)),
|
|
},
|
|
];
|
|
}
|
|
if (block.type === "table") {
|
|
return [
|
|
{
|
|
tag: "markdown",
|
|
content: escapeFeishuCardMarkdownText(renderMessagePresentationTableFallbackText(block)),
|
|
},
|
|
];
|
|
}
|
|
const labels = block.options.map((option) => `- ${option.label}`).join("\n");
|
|
return [
|
|
{
|
|
tag: "markdown",
|
|
content: `${escapeFeishuCardMarkdownText(
|
|
block.placeholder?.trim() || "Options",
|
|
)}:\n${escapeFeishuCardMarkdownText(labels)}`,
|
|
},
|
|
];
|
|
}
|
|
|
|
function resolvePresentationHeaderTemplate(tone: NormalizedMessagePresentation["tone"]) {
|
|
if (tone === "danger") {
|
|
return "red";
|
|
}
|
|
if (tone === "warning") {
|
|
return "orange";
|
|
}
|
|
if (tone === "success") {
|
|
return "green";
|
|
}
|
|
return "blue";
|
|
}
|
|
|
|
export function buildFeishuPresentationCardElements(params: {
|
|
presentation: NormalizedMessagePresentation;
|
|
fallbackText?: string;
|
|
}): Record<string, unknown>[] {
|
|
const elements: Record<string, unknown>[] = [];
|
|
const fallbackText = params.fallbackText?.trim();
|
|
if (fallbackText) {
|
|
elements.push({
|
|
tag: "markdown",
|
|
content: escapeFeishuCardMarkdownText(fallbackText),
|
|
});
|
|
}
|
|
for (const block of params.presentation.blocks) {
|
|
for (const element of buildFeishuCardElementsForBlock(block)) {
|
|
elements.push(element);
|
|
}
|
|
}
|
|
if (elements.length > 0) {
|
|
return elements;
|
|
}
|
|
return [
|
|
{
|
|
tag: "markdown",
|
|
content: renderMessagePresentationFallbackText({
|
|
text: params.fallbackText,
|
|
presentation: params.presentation.title
|
|
? {
|
|
...(params.presentation.tone ? { tone: params.presentation.tone } : {}),
|
|
blocks: params.presentation.blocks,
|
|
}
|
|
: params.presentation,
|
|
}),
|
|
},
|
|
];
|
|
}
|
|
|
|
export function buildFeishuPresentationCard(params: {
|
|
presentation: NormalizedMessagePresentation;
|
|
fallbackText?: string;
|
|
}): Record<string, unknown> {
|
|
return {
|
|
schema: "2.0",
|
|
config: {
|
|
width_mode: "fill",
|
|
},
|
|
...(params.presentation.title
|
|
? {
|
|
header: {
|
|
title: { tag: "plain_text", content: params.presentation.title },
|
|
template: resolvePresentationHeaderTemplate(params.presentation.tone),
|
|
},
|
|
}
|
|
: {}),
|
|
body: {
|
|
elements: buildFeishuPresentationCardElements(params),
|
|
},
|
|
};
|
|
}
|