Skip to content

Commit fd8f6bf

Browse files
committed
fix(mattermost): preserve presentations in normal agent replies
1 parent 1f812ac commit fd8f6bf

6 files changed

Lines changed: 320 additions & 79 deletions

File tree

‎extensions/mattermost/src/channel.ts‎

Lines changed: 9 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,7 @@ import { createChannelDirectoryAdapter } from "openclaw/plugin-sdk/directory-run
2828
import { buildPassiveProbedChannelStatusSummary } from "openclaw/plugin-sdk/extension-shared";
2929
import {
3030
type MessagePresentation,
31-
normalizeMessagePresentation,
32-
renderMessagePresentationFallbackText,
3331
resolveMessagePresentationButtonAction,
34-
resolveMessagePresentationControlValue,
3532
} from "openclaw/plugin-sdk/interactive-runtime";
3633
import { createLazyRuntimeModule } from "openclaw/plugin-sdk/lazy-runtime";
3734
import { resolvePayloadMediaUrls, sendTextMediaPayload } from "openclaw/plugin-sdk/reply-payload";
@@ -73,6 +70,7 @@ import type { MattermostSendResult } from "./mattermost/send.js";
7370
import {
7471
looksLikeMattermostTargetId,
7572
normalizeMattermostMessagingTarget,
73+
resolveMattermostPresentation,
7674
requiresMattermostMediaUpload,
7775
} from "./normalize.js";
7876
import { collectRuntimeConfigAssignments, secretTargetRegistryEntries } from "./secret-contract.js";
@@ -83,33 +81,6 @@ import type { MattermostConfig } from "./types.js";
8381

8482
const loadMattermostChannelRuntime = createLazyRuntimeModule(() => import("./channel.runtime.js"));
8583

86-
function buildMattermostPresentationButtons(presentation: MessagePresentation) {
87-
return presentation.blocks
88-
.filter((block) => block.type === "buttons")
89-
.map((block) =>
90-
block.buttons.flatMap((button) => {
91-
if (button.action) {
92-
return [];
93-
}
94-
const value = resolveMessagePresentationControlValue(button);
95-
return value
96-
? [
97-
{
98-
id: value,
99-
text: button.label,
100-
callback_data: value,
101-
context: {
102-
callback_data: value,
103-
},
104-
style: button.style,
105-
},
106-
]
107-
: [];
108-
}),
109-
)
110-
.filter((row) => row.length > 0);
111-
}
112-
11384
const MATTERMOST_PRESENTATION_CAPABILITIES = {
11485
supported: true,
11586
buttons: true,
@@ -572,15 +543,10 @@ const mattermostMessageActions: ChannelMessageActionAdapter = {
572543
throw new Error("Mattermost send requires a target (to).");
573544
}
574545

575-
const presentation = normalizeMessagePresentation(params.presentation);
576-
const message = presentation
577-
? renderMessagePresentationFallbackText({
578-
text: typeof params.message === "string" ? params.message : "",
579-
presentation,
580-
})
581-
: typeof params.message === "string"
582-
? params.message
583-
: "";
546+
const { text: message, buttons } = resolveMattermostPresentation({
547+
text: typeof params.message === "string" ? params.message : undefined,
548+
presentation: params.presentation,
549+
});
584550
// Mattermost post root_id is the thread root. A generic replyTo can name
585551
// the current child post, so prefer threadId unless the caller supplied the
586552
// Mattermost-specific replyToId root directly.
@@ -591,8 +557,6 @@ const mattermostMessageActions: ChannelMessageActionAdapter = {
591557
const resolvedAccountId = accountId || undefined;
592558

593559
const mediaUrl = resolveMattermostSendAttachmentMedia(params);
594-
const buttons = presentation ? buildMattermostPresentationButtons(presentation) : [];
595-
596560
const result = await (
597561
await loadMattermostChannelRuntime()
598562
).sendMessageMattermost(to, message, {
@@ -803,15 +767,14 @@ const mattermostOutbound: ChannelOutboundAdapter = {
803767
if (payload.mediaUrls && payload.mediaUrls.length > 1) {
804768
return null;
805769
}
806-
const buttons = buildMattermostPresentationButtons(presentation);
807-
const hasButtons = buttons.some((row) => row.length > 0);
808-
if (!hasButtons && !hasMattermostPresentationNavigation(presentation)) {
770+
const { text, buttons } = resolveMattermostPresentation({ text: payload.text, presentation });
771+
if (!buttons.length && !hasMattermostPresentationNavigation(presentation)) {
809772
return null;
810773
}
811774
return {
812775
...payload,
813-
text: renderMessagePresentationFallbackText({ text: payload.text, presentation }),
814-
...(hasButtons
776+
text,
777+
...(buttons.length
815778
? {
816779
channelData: {
817780
...payload.channelData,

‎extensions/mattermost/src/mattermost/monitor-draft-delivery.test.ts‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,41 @@ describe("deliverMattermostReplyWithDraftPreview", () => {
147147
});
148148
});
149149

150+
it.each([
151+
{
152+
name: "native value buttons",
153+
presentation: {
154+
blocks: [{ type: "buttons" as const, buttons: [{ label: "Open", value: "open" }] }],
155+
},
156+
},
157+
{
158+
name: "navigation URLs",
159+
presentation: {
160+
blocks: [
161+
{
162+
type: "buttons" as const,
163+
buttons: [{ label: "Docs", url: "https://example.com/docs" }],
164+
},
165+
],
166+
},
167+
},
168+
])("delivers $name instead of losing them in a preview edit", async ({ presentation }) => {
169+
const draftStream = createDraftStreamMock();
170+
const deliverFinal = createDeliverFinalMock();
171+
const payload = { text: "Choose an option", presentation };
172+
173+
await deliverDraftPreview({
174+
payload,
175+
draftStream,
176+
effectiveReplyToId: "thread-root-1",
177+
deliverPayload: deliverFinal,
178+
});
179+
180+
expect(deliverFinal).toHaveBeenCalledExactlyOnceWith(payload);
181+
expect(updateMattermostPostSpy).not.toHaveBeenCalled();
182+
expect(draftStream.clear).toHaveBeenCalledTimes(1);
183+
});
184+
150185
it("reports a final already published in a sealed preview generation", async () => {
151186
const draftStream = createDraftStreamMock(null);
152187
const deliverFinal = createDeliverFinalMock();
@@ -174,6 +209,26 @@ describe("deliverMattermostReplyWithDraftPreview", () => {
174209
});
175210
});
176211

212+
it("delivers unsent presentation controls after preview text was already published", async () => {
213+
const draftStream = createDraftStreamMock(null);
214+
const deliverFinal = createDeliverFinalMock();
215+
const confirmedDelivery = createConfirmedPreviewDelivery("sealed-post-1", "Already visible");
216+
const presentation = {
217+
blocks: [{ type: "buttons" as const, buttons: [{ label: "Open", value: "open" }] }],
218+
};
219+
220+
const result = await deliverDraftPreview({
221+
payload: { text: "Already visible", presentation },
222+
draftStream,
223+
effectiveReplyToId: "thread-root-1",
224+
resolvePreviewFinalText: () => ({ alreadyDelivered: true, confirmedDelivery }),
225+
deliverPayload: deliverFinal,
226+
});
227+
228+
expect(deliverFinal).toHaveBeenCalledExactlyOnceWith({ text: "", presentation });
229+
expect(result.messageIds).toEqual(["sealed-post-1", "delivered-post-1"]);
230+
});
231+
177232
it("still delivers media when the text is already published", async () => {
178233
const draftStream = createDraftStreamMock(null);
179234
const confirmedDelivery = createConfirmedPreviewDelivery("sealed-post-1", "Already visible");

‎extensions/mattermost/src/mattermost/monitor-draft-delivery.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,18 @@ export async function deliverMattermostReplyWithDraftPreview(
134134
previewFinalDeliveryText = previewFinalResolution?.deliveryText;
135135
previewFinalTextAlreadyDelivered =
136136
previewFinalResolution?.alreadyDelivered === true && payload.isError !== true;
137+
// A text-only preview cannot finalize unsent presentation content or controls.
137138
useConfirmedPreviewAsWholeFinal =
138139
previewFinalTextAlreadyDelivered &&
139-
!resolveSendableOutboundReplyParts(payload).hasMedia;
140+
!resolveSendableOutboundReplyParts(payload).hasMedia &&
141+
!payload.presentation;
140142
const previewFinalText = previewFinalResolution?.editText;
141143

142144
if (
143145
(hasMedia && !ttsSupplement) ||
144146
typeof previewFinalText !== "string" ||
145147
payload.isError ||
148+
payload.presentation ||
146149
!canFinalizeMattermostPreviewInPlace({
147150
kind: params.kind,
148151
previewRootId: params.effectiveReplyToId,

0 commit comments

Comments
 (0)