Skip to content

Commit c6ecfd2

Browse files
authored
fix(outbound): preserve first-reply behavior through durable delivery (#126205)
* fix(outbound): preserve reply facts through durable delivery * fix(protocol): regenerate message action models * fix(outbound): complete reply fact custody
1 parent 8b15dc9 commit c6ecfd2

44 files changed

Lines changed: 645 additions & 304 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎apps/shared/OpenClawKit/Sources/OpenClawProtocol/GatewayModels.swift‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3144,6 +3144,7 @@ public struct MessageActionParams: Codable, Sendable {
31443144
public let channel: String
31453145
public let action: String
31463146
public let params: [String: AnyCodable]
3147+
public let reply: AnyCodable?
31473148
public let accountid: String?
31483149
public let requesteraccountid: String?
31493150
public let requestersenderid: String?
@@ -3160,6 +3161,7 @@ public struct MessageActionParams: Codable, Sendable {
31603161
channel: String,
31613162
action: String,
31623163
params: [String: AnyCodable],
3164+
reply: AnyCodable? = nil,
31633165
accountid: String? = nil,
31643166
requesteraccountid: String? = nil,
31653167
requestersenderid: String? = nil,
@@ -3175,6 +3177,7 @@ public struct MessageActionParams: Codable, Sendable {
31753177
self.channel = channel
31763178
self.action = action
31773179
self.params = params
3180+
self.reply = reply
31783181
self.accountid = accountid
31793182
self.requesteraccountid = requesteraccountid
31803183
self.requestersenderid = requestersenderid
@@ -3192,6 +3195,7 @@ public struct MessageActionParams: Codable, Sendable {
31923195
case channel
31933196
case action
31943197
case params
3198+
case reply
31953199
case accountid = "accountId"
31963200
case requesteraccountid = "requesterAccountId"
31973201
case requestersenderid = "requesterSenderId"

‎config/assertion-safety-baseline.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3267,7 +3267,7 @@ src/infra/outbound/deliver-queue-state.ts 1
32673267
src/infra/outbound/delivery-queue-media-spool.ts 2
32683268
src/infra/outbound/delivery-queue-media-staging.ts 2
32693269
src/infra/outbound/delivery-queue-preparation.ts 1
3270-
src/infra/outbound/delivery-queue-storage.ts 11
3270+
src/infra/outbound/delivery-queue-storage.ts 10
32713271
src/infra/outbound/envelope.ts 2
32723272
src/infra/outbound/format.ts 1
32733273
src/infra/outbound/message-account-selection.ts 4

‎extensions/discord/src/actions/handle-action.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ export async function handleDiscordMessageAction(
7575
| "sessionKey"
7676
| "inboundEventKind"
7777
| "conversationReadOrigin"
78+
| "reply"
7879
>,
7980
): Promise<AgentToolResult<unknown>> {
8081
const { action, params, cfg } = ctx;
@@ -102,6 +103,7 @@ export async function handleDiscordMessageAction(
102103
mediaAccess: ctx.mediaAccess,
103104
mediaLocalRoots: ctx.mediaLocalRoots,
104105
mediaReadFile: ctx.mediaReadFile,
106+
...(ctx.reply ? { reply: ctx.reply } : {}),
105107
...readPolicyOptions,
106108
} as const;
107109
const notifyVisibleOutbound = (

‎extensions/discord/src/actions/runtime.messaging.send.ts‎

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// Discord plugin module implements runtime.messaging.send behavior.
22
import { formatErrorMessage } from "openclaw/plugin-sdk/error-runtime";
3-
import { createReusableDiscordReplyReference } from "../reply-reference.js";
3+
import {
4+
createReusableDiscordReplyReference,
5+
resolveDiscordReplyReference,
6+
} from "../reply-reference.js";
47
import {
58
assertMediaNotDataUrl,
69
jsonResult,
@@ -26,6 +29,19 @@ function hasDiscordComponentObjectKeys(value: unknown): value is Record<string,
2629
);
2730
}
2831

32+
function resolveActionReplyReference(ctx: DiscordMessagingActionContext, replyToId?: string) {
33+
const reply = ctx.options?.reply;
34+
// Host-resolved facts own physical-send scope. Raw-only plugin callers keep
35+
// their longstanding reusable reply semantics when no host fact exists.
36+
return reply
37+
? resolveDiscordReplyReference({
38+
replyToId: reply.replyToId,
39+
replyToIdSource: reply.source,
40+
replyToMode: reply.source === "implicit" ? reply.mode : undefined,
41+
})
42+
: createReusableDiscordReplyReference(replyToId);
43+
}
44+
2945
function readDiscordThreadArchiveTimestamp(thread: unknown): string | undefined {
3046
if (!thread || typeof thread !== "object" || Array.isArray(thread)) {
3147
return undefined;
@@ -252,7 +268,7 @@ export async function handleDiscordMessageSendAction(ctx: DiscordMessagingAction
252268
{
253269
...ctx.withOpts(),
254270
silent,
255-
reply: createReusableDiscordReplyReference(replyTo),
271+
reply: resolveActionReplyReference(ctx, replyTo),
256272
sessionKey: sessionKey ?? undefined,
257273
agentId: agentId ?? undefined,
258274
mediaUrl: mediaUrl ?? undefined,
@@ -286,7 +302,7 @@ export async function handleDiscordMessageSendAction(ctx: DiscordMessagingAction
286302
assertMediaNotDataUrl(mediaUrl);
287303
const result = await discordMessagingActionRuntime.sendVoiceMessageDiscord(to, mediaUrl, {
288304
...ctx.withOpts(),
289-
reply: createReusableDiscordReplyReference(replyTo),
305+
reply: resolveActionReplyReference(ctx, replyTo),
290306
silent,
291307
mediaAccess: ctx.options?.mediaAccess,
292308
mediaLocalRoots: ctx.options?.mediaLocalRoots,
@@ -308,7 +324,7 @@ export async function handleDiscordMessageSendAction(ctx: DiscordMessagingAction
308324
filename: filename ?? undefined,
309325
mediaLocalRoots: ctx.options?.mediaLocalRoots,
310326
mediaReadFile: ctx.options?.mediaReadFile,
311-
reply: createReusableDiscordReplyReference(replyTo),
327+
reply: resolveActionReplyReference(ctx, replyTo),
312328
components,
313329
embeds,
314330
silent,
@@ -424,7 +440,7 @@ export async function handleDiscordMessageSendAction(ctx: DiscordMessagingAction
424440
mediaAccess: ctx.options?.mediaAccess,
425441
mediaLocalRoots: ctx.options?.mediaLocalRoots,
426442
mediaReadFile: ctx.options?.mediaReadFile,
427-
reply: createReusableDiscordReplyReference(replyTo),
443+
reply: resolveActionReplyReference(ctx, replyTo),
428444
},
429445
);
430446
return jsonResult({ ok: true, result });

‎extensions/discord/src/actions/runtime.messaging.shared.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type ConversationReadInvocationOrigin = NonNullable<
2828
>;
2929

3030
export type DiscordMessagingActionOptions = {
31+
reply?: ChannelMessageActionContext["reply"];
3132
mediaAccess?: ChannelMessageActionContext["mediaAccess"];
3233
mediaLocalRoots?: readonly string[];
3334
mediaReadFile?: (filePath: string) => Promise<Buffer>;

‎extensions/discord/src/actions/runtime.test.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { expectDefined } from "@openclaw/normalization-core";
22
// Discord tests cover runtime plugin behavior.
33
import { ChannelType, PermissionFlagsBits } from "discord-api-types/v10";
4+
import type { ChannelMessageActionContext } from "openclaw/plugin-sdk/channel-contract";
45
import type { OpenClawConfig, DiscordActionConfig } from "openclaw/plugin-sdk/config-contracts";
56
import { beforeEach, describe, expect, it, vi } from "vitest";
67
import { clearPresences, setPresence } from "../monitor/presence-cache.js";
@@ -180,6 +181,7 @@ function handleMessagingAction(
180181
isActionEnabled: (key: keyof DiscordActionConfig) => boolean,
181182
cfg: OpenClawConfig = DISCORD_TEST_CFG,
182183
options?: {
184+
reply?: ChannelMessageActionContext["reply"];
183185
mediaAccess?: {
184186
localRoots?: readonly string[];
185187
readFile?: (filePath: string) => Promise<Buffer>;
@@ -2245,6 +2247,49 @@ describe("handleDiscordMessagingAction", () => {
22452247
expect(sendOptions.mediaLocalRoots).toEqual(["/tmp/agent-root"]);
22462248
});
22472249

2250+
it.each([
2251+
{
2252+
label: "implicit first reply",
2253+
reply: { source: "implicit", replyToId: "source-1", mode: "first" } as const,
2254+
replyToId: "source-1",
2255+
expected: { messageId: "source-1", scope: "first" },
2256+
},
2257+
{
2258+
label: "explicit reply while configured replies are off",
2259+
reply: { source: "explicit", replyToId: "explicit-1" } as const,
2260+
replyToId: "explicit-1",
2261+
expected: { messageId: "explicit-1", scope: "all" },
2262+
},
2263+
{
2264+
label: "raw custom plugin reply",
2265+
reply: undefined,
2266+
replyToId: "custom-1",
2267+
expected: { messageId: "custom-1", scope: "all" },
2268+
},
2269+
])(
2270+
"preserves the host-owned or custom $label for component sends",
2271+
async ({ reply, replyToId, expected }) => {
2272+
sendDiscordComponentMessage.mockClear();
2273+
2274+
await handleMessagingAction(
2275+
"sendMessage",
2276+
{
2277+
to: "channel:123",
2278+
content: "hello",
2279+
components: { blocks: [{ type: "text", text: "Pick one" }] },
2280+
replyTo: replyToId,
2281+
},
2282+
enableAllActions,
2283+
DISCORD_TEST_CFG,
2284+
reply ? { reply } : undefined,
2285+
);
2286+
2287+
expect(
2288+
mockObjectArg(sendDiscordComponentMessage, "sendDiscordComponentMessage", 0, 2),
2289+
).toMatchObject({ reply: expected });
2290+
},
2291+
);
2292+
22482293
it("forwards the optional filename into sendMessageDiscord", async () => {
22492294
sendMessageDiscord.mockClear();
22502295
await handleMessagingAction(

‎extensions/discord/src/channel-actions.test.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,11 @@ describe("discordMessageActions", () => {
510510
readFile: mediaReadFile,
511511
};
512512
const mediaLocalRoots = ["/tmp/media"];
513+
const reply = {
514+
source: "implicit" as const,
515+
replyToId: "source-message-1",
516+
mode: "first" as const,
517+
};
513518

514519
await discordMessageActions.handleAction?.({
515520
channel: "discord",
@@ -525,6 +530,7 @@ describe("discordMessageActions", () => {
525530
mediaLocalRoots,
526531
mediaReadFile,
527532
conversationReadOrigin: "delegated",
533+
reply,
528534
});
529535

530536
expect(handleDiscordMessageActionMock).toHaveBeenCalledWith({
@@ -540,6 +546,7 @@ describe("discordMessageActions", () => {
540546
mediaLocalRoots,
541547
mediaReadFile,
542548
conversationReadOrigin: "delegated",
549+
reply,
543550
});
544551
});
545552
});

‎extensions/discord/src/channel-actions.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ export const discordMessageActions: ChannelMessageActionAdapter = {
253253
sessionKey,
254254
inboundEventKind,
255255
conversationReadOrigin,
256+
reply,
256257
}) => {
257258
return await (
258259
await loadDiscordChannelActionsRuntime()
@@ -271,6 +272,7 @@ export const discordMessageActions: ChannelMessageActionAdapter = {
271272
...(inboundEventKind ? { inboundEventKind } : {}),
272273
...(requesterAccountId ? { requesterAccountId } : {}),
273274
...(conversationReadOrigin ? { conversationReadOrigin } : {}),
275+
...(reply ? { reply } : {}),
274276
});
275277
},
276278
};

‎extensions/telegram/src/action-runtime.test.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os from "node:os";
22
import path from "node:path";
3+
import type { ChannelMessageActionContext } from "openclaw/plugin-sdk/channel-contract";
34
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
45
import { resolveStorePath } from "openclaw/plugin-sdk/session-store-runtime";
56
import { captureEnv } from "openclaw/plugin-sdk/test-env";
@@ -63,6 +64,7 @@ const sendDurableMessageBatch = vi.fn(
6364
channelData?: { telegram?: { buttons?: unknown; quoteText?: string } };
6465
}>;
6566
replyToId?: string;
67+
reply?: ChannelMessageActionContext["reply"];
6668
threadId?: string | number;
6769
forceDocument?: boolean;
6870
silent?: boolean;
@@ -867,6 +869,11 @@ describe("handleTelegramAction", () => {
867869
{
868870
gatewayClientScopes: ["operator.write"],
869871
sessionKey: "agent:main:telegram:direct:123",
872+
reply: {
873+
replyToId: "456",
874+
source: "implicit",
875+
mode: "first",
876+
},
870877
},
871878
);
872879
const call = mockCall(sendMessageTelegram, 0, "text message");
@@ -882,6 +889,7 @@ describe("handleTelegramAction", () => {
882889
durability: "required",
883890
gatewayClientScopes: ["operator.write"],
884891
session: { key: "agent:main:telegram:direct:123", agentId: "main" },
892+
reply: { replyToId: "456", source: "implicit", mode: "first" },
885893
payloads: [{ text: "Hello, Telegram!" }],
886894
});
887895
expect(result.content).toStrictEqual([

‎extensions/telegram/src/action-runtime.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ export async function handleTelegramAction(
395395
gatewayClientScopes?: readonly string[];
396396
conversationReadOrigin?: ConversationReadInvocationOrigin;
397397
requesterAccountId?: string | null;
398+
reply?: ChannelMessageActionContext["reply"];
398399
toolContext?: TelegramMessageMutationContext["toolContext"];
399400
},
400401
): Promise<AgentToolResult<unknown>> {
@@ -637,7 +638,9 @@ export async function handleTelegramAction(
637638
to,
638639
accountId: accountId ?? undefined,
639640
payloads: [payload],
640-
replyToId: replyToMessageId == null ? undefined : String(replyToMessageId),
641+
...(options?.reply
642+
? { reply: options.reply }
643+
: { replyToId: replyToMessageId == null ? undefined : String(replyToMessageId) }),
641644
threadId: messageThreadId,
642645
forceDocument: sendOptions.forceDocument,
643646
silent: sendOptions.silent,

0 commit comments

Comments
 (0)