mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 17:00:50 +00:00
refactor(channels): decouple presentation rendering
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import type { OpenClawConfig } from "../runtime-api.js";
|
||||
import { createFeishuCardInteractionEnvelope } from "./card-interaction.js";
|
||||
import { feishuPlugin } from "./channel.js";
|
||||
import { looksLikeFeishuId, normalizeFeishuTarget, resolveReceiveIdType } from "./targets.js";
|
||||
|
||||
@@ -65,41 +64,6 @@ function getDescribedActions(cfg: OpenClawConfig, accountId?: string): string[]
|
||||
return [...(feishuPlugin.actions?.describeMessageTool?.({ cfg, accountId })?.actions ?? [])];
|
||||
}
|
||||
|
||||
function createLegacyFeishuButtonCard(value: { command?: string; text?: string }) {
|
||||
return {
|
||||
schema: "2.0",
|
||||
body: {
|
||||
elements: [
|
||||
{
|
||||
tag: "action",
|
||||
actions: [
|
||||
{
|
||||
tag: "button",
|
||||
text: { tag: "plain_text", content: "Run /new" },
|
||||
value,
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async function expectLegacyFeishuCardPayloadRejected(cfg: OpenClawConfig, card: unknown) {
|
||||
await expect(
|
||||
feishuPlugin.actions?.handleAction?.({
|
||||
action: "send",
|
||||
params: { to: "chat:oc_group_1", card },
|
||||
cfg,
|
||||
accountId: undefined,
|
||||
toolContext: {},
|
||||
} as never),
|
||||
).rejects.toThrow(
|
||||
"Feishu card buttons that trigger text or commands must use structured interaction envelopes.",
|
||||
);
|
||||
expect(sendCardFeishuMock).not.toHaveBeenCalled();
|
||||
}
|
||||
|
||||
describe("feishuPlugin.status.probeAccount", () => {
|
||||
it("uses current account credentials for multi-account config", async () => {
|
||||
const cfg = {
|
||||
@@ -348,12 +312,18 @@ describe("feishuPlugin actions", () => {
|
||||
expect(result?.details).toMatchObject({ ok: true, messageId: "om_sent", chatId: "oc_group_1" });
|
||||
});
|
||||
|
||||
it("sends card messages", async () => {
|
||||
it("renders presentation messages as cards", async () => {
|
||||
sendCardFeishuMock.mockResolvedValueOnce({ messageId: "om_card", chatId: "oc_group_1" });
|
||||
|
||||
const result = await feishuPlugin.actions?.handleAction?.({
|
||||
action: "send",
|
||||
params: { to: "chat:oc_group_1", card: { schema: "2.0" } },
|
||||
params: {
|
||||
to: "chat:oc_group_1",
|
||||
presentation: {
|
||||
title: "Status",
|
||||
blocks: [{ type: "text", text: "Build completed" }],
|
||||
},
|
||||
},
|
||||
cfg,
|
||||
accountId: undefined,
|
||||
toolContext: {},
|
||||
@@ -362,7 +332,21 @@ describe("feishuPlugin actions", () => {
|
||||
expect(sendCardFeishuMock).toHaveBeenCalledWith({
|
||||
cfg,
|
||||
to: "chat:oc_group_1",
|
||||
card: { schema: "2.0" },
|
||||
card: expect.objectContaining({
|
||||
schema: "2.0",
|
||||
header: {
|
||||
title: { tag: "plain_text", content: "Status" },
|
||||
template: "blue",
|
||||
},
|
||||
body: {
|
||||
elements: [
|
||||
{
|
||||
tag: "markdown",
|
||||
content: "Build completed",
|
||||
},
|
||||
],
|
||||
},
|
||||
}),
|
||||
accountId: undefined,
|
||||
replyToMessageId: undefined,
|
||||
replyInThread: false,
|
||||
@@ -370,34 +354,22 @@ describe("feishuPlugin actions", () => {
|
||||
expect(result?.details).toMatchObject({ ok: true, messageId: "om_card", chatId: "oc_group_1" });
|
||||
});
|
||||
|
||||
it("allows structured card button payloads", async () => {
|
||||
it("renders presentation button labels into the card fallback", async () => {
|
||||
sendCardFeishuMock.mockResolvedValueOnce({ messageId: "om_card", chatId: "oc_group_1" });
|
||||
const card = {
|
||||
schema: "2.0",
|
||||
body: {
|
||||
elements: [
|
||||
{
|
||||
tag: "action",
|
||||
actions: [
|
||||
{
|
||||
tag: "button",
|
||||
text: { tag: "plain_text", content: "Run /new" },
|
||||
value: createFeishuCardInteractionEnvelope({
|
||||
k: "quick",
|
||||
a: "feishu.quick_actions.help",
|
||||
q: "/help",
|
||||
c: { u: "u123", h: "oc_group_1", t: "group", e: Date.now() + 60_000 },
|
||||
}),
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
|
||||
await feishuPlugin.actions?.handleAction?.({
|
||||
action: "send",
|
||||
params: { to: "chat:oc_group_1", card },
|
||||
params: {
|
||||
to: "chat:oc_group_1",
|
||||
presentation: {
|
||||
blocks: [
|
||||
{
|
||||
type: "buttons",
|
||||
buttons: [{ label: "Run help", value: "feishu.quick_actions.help" }],
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
cfg,
|
||||
accountId: undefined,
|
||||
toolContext: {},
|
||||
@@ -405,54 +377,37 @@ describe("feishuPlugin actions", () => {
|
||||
|
||||
expect(sendCardFeishuMock).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
card,
|
||||
card: expect.objectContaining({
|
||||
body: {
|
||||
elements: [
|
||||
{
|
||||
tag: "markdown",
|
||||
content: "- Run help",
|
||||
},
|
||||
],
|
||||
},
|
||||
}),
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("rejects raw legacy card command payloads", async () => {
|
||||
await expectLegacyFeishuCardPayloadRejected(
|
||||
cfg,
|
||||
createLegacyFeishuButtonCard({ command: "/new" }),
|
||||
);
|
||||
});
|
||||
|
||||
it("rejects raw legacy card text payloads", async () => {
|
||||
await expectLegacyFeishuCardPayloadRejected(
|
||||
cfg,
|
||||
createLegacyFeishuButtonCard({ text: "/new" }),
|
||||
);
|
||||
});
|
||||
|
||||
it("allows non-button controls to carry text metadata values", async () => {
|
||||
it("renders presentation select labels into the card fallback", async () => {
|
||||
sendCardFeishuMock.mockResolvedValueOnce({ messageId: "om_card", chatId: "oc_group_1" });
|
||||
const card = {
|
||||
schema: "2.0",
|
||||
body: {
|
||||
elements: [
|
||||
{
|
||||
tag: "action",
|
||||
actions: [
|
||||
{
|
||||
tag: "select_static",
|
||||
placeholder: { tag: "plain_text", content: "Pick one" },
|
||||
value: { text: "display-only metadata" },
|
||||
options: [
|
||||
{
|
||||
text: { tag: "plain_text", content: "Option A" },
|
||||
value: "a",
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
|
||||
await feishuPlugin.actions?.handleAction?.({
|
||||
action: "send",
|
||||
params: { to: "chat:oc_group_1", card },
|
||||
params: {
|
||||
to: "chat:oc_group_1",
|
||||
presentation: {
|
||||
blocks: [
|
||||
{
|
||||
type: "select",
|
||||
placeholder: "Pick one",
|
||||
options: [{ label: "Option A", value: "a" }],
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
cfg,
|
||||
accountId: undefined,
|
||||
toolContext: {},
|
||||
@@ -460,7 +415,16 @@ describe("feishuPlugin actions", () => {
|
||||
|
||||
expect(sendCardFeishuMock).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
card,
|
||||
card: expect.objectContaining({
|
||||
body: {
|
||||
elements: [
|
||||
{
|
||||
tag: "markdown",
|
||||
content: "Pick one:\n- Option A",
|
||||
},
|
||||
],
|
||||
},
|
||||
}),
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import { describeAccountSnapshot } from "openclaw/plugin-sdk/account-helpers";
|
||||
import { formatAllowFromLowercase } from "openclaw/plugin-sdk/allow-from";
|
||||
import { createMessageToolCardSchema } from "openclaw/plugin-sdk/channel-actions";
|
||||
import {
|
||||
adaptScopedAccountAccessor,
|
||||
createHybridChannelConfigAdapter,
|
||||
@@ -20,6 +19,10 @@ import {
|
||||
createChannelDirectoryAdapter,
|
||||
createRuntimeDirectoryLiveAdapter,
|
||||
} from "openclaw/plugin-sdk/directory-runtime";
|
||||
import {
|
||||
normalizeMessagePresentation,
|
||||
renderMessagePresentationFallbackText,
|
||||
} from "openclaw/plugin-sdk/interactive-runtime";
|
||||
import { createLazyRuntimeNamedExport } from "openclaw/plugin-sdk/lazy-runtime";
|
||||
import { createRuntimeOutboundDelegates } from "openclaw/plugin-sdk/outbound-runtime";
|
||||
import { createComputedAccountStatusAdapter } from "openclaw/plugin-sdk/status-helpers";
|
||||
@@ -118,6 +121,41 @@ const loadFeishuChannelRuntime = createLazyRuntimeNamedExport(
|
||||
"feishuChannelRuntime",
|
||||
);
|
||||
|
||||
function buildFeishuPresentationCard(params: {
|
||||
presentation: NonNullable<ReturnType<typeof normalizeMessagePresentation>>;
|
||||
fallbackText?: string;
|
||||
}): Record<string, unknown> {
|
||||
const fallbackPresentation: NonNullable<ReturnType<typeof normalizeMessagePresentation>> = {
|
||||
...(params.presentation.tone ? { tone: params.presentation.tone } : {}),
|
||||
blocks: params.presentation.blocks,
|
||||
};
|
||||
return {
|
||||
schema: "2.0",
|
||||
config: {
|
||||
width_mode: "fill",
|
||||
},
|
||||
...(params.presentation.title
|
||||
? {
|
||||
header: {
|
||||
title: { tag: "plain_text", content: params.presentation.title },
|
||||
template: "blue",
|
||||
},
|
||||
}
|
||||
: {}),
|
||||
body: {
|
||||
elements: [
|
||||
{
|
||||
tag: "markdown",
|
||||
content: renderMessagePresentationFallbackText({
|
||||
text: params.fallbackText,
|
||||
presentation: fallbackPresentation,
|
||||
}),
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async function createFeishuActionClient(account: ResolvedFeishuAccount) {
|
||||
const { createFeishuClient } = await import("./client.js");
|
||||
return createFeishuClient(account);
|
||||
@@ -160,14 +198,7 @@ function describeFeishuMessageTool({
|
||||
if (enabledAccounts.length === 0) {
|
||||
return {
|
||||
actions: [],
|
||||
capabilities: enabled ? ["cards"] : [],
|
||||
schema: enabled
|
||||
? {
|
||||
properties: {
|
||||
card: createMessageToolCardSchema(),
|
||||
},
|
||||
}
|
||||
: null,
|
||||
capabilities: enabled ? ["presentation"] : [],
|
||||
};
|
||||
}
|
||||
const actions = new Set<ChannelMessageActionName>([
|
||||
@@ -192,14 +223,7 @@ function describeFeishuMessageTool({
|
||||
}
|
||||
return {
|
||||
actions: Array.from(actions),
|
||||
capabilities: enabled ? ["cards"] : [],
|
||||
schema: enabled
|
||||
? {
|
||||
properties: {
|
||||
card: createMessageToolCardSchema(),
|
||||
},
|
||||
}
|
||||
: null,
|
||||
capabilities: enabled ? ["presentation"] : [],
|
||||
};
|
||||
}
|
||||
|
||||
@@ -668,12 +692,12 @@ export const feishuPlugin: ChannelPlugin<ResolvedFeishuAccount, FeishuProbeResul
|
||||
if (ctx.action === "thread-reply" && !replyToMessageId) {
|
||||
throw new Error("Feishu thread-reply requires messageId.");
|
||||
}
|
||||
const card =
|
||||
ctx.params.card && typeof ctx.params.card === "object"
|
||||
? (ctx.params.card as Record<string, unknown>)
|
||||
: undefined;
|
||||
const presentation = normalizeMessagePresentation(ctx.params.presentation);
|
||||
const text = readFirstString(ctx.params, ["text", "message"]);
|
||||
const mediaUrl = readFeishuMediaParam(ctx.params);
|
||||
const card = presentation
|
||||
? buildFeishuPresentationCard({ presentation, fallbackText: text })
|
||||
: undefined;
|
||||
if (card && mediaUrl) {
|
||||
throw new Error(`Feishu ${ctx.action} does not support card with media.`);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user