Skip to content

Commit 9504232

Browse files
fix(agents): run reply payload hooks for agent delivery
1 parent d2d2dfd commit 9504232

2 files changed

Lines changed: 105 additions & 0 deletions

File tree

‎src/agents/command/delivery.test.ts‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,13 @@ function latestOutboundDeliveryArgs(): {
109109
payloads: ReplyPayload[];
110110
bestEffort?: boolean;
111111
queuePolicy?: string;
112+
replyPayloadSendingHook?: {
113+
kind?: string;
114+
channel?: string;
115+
sessionKey?: string;
116+
runId?: string;
117+
context?: Record<string, unknown>;
118+
};
112119
} {
113120
const args = lastMockArg(deliverOutboundPayloadsMock, "outbound delivery arguments");
114121
if (!args || typeof args !== "object") {
@@ -121,6 +128,13 @@ function latestOutboundDeliveryArgs(): {
121128
payloads: ReplyPayload[];
122129
bestEffort?: boolean;
123130
queuePolicy?: string;
131+
replyPayloadSendingHook?: {
132+
kind?: string;
133+
channel?: string;
134+
sessionKey?: string;
135+
runId?: string;
136+
context?: Record<string, unknown>;
137+
};
124138
};
125139
}
126140

@@ -396,6 +410,62 @@ describe("normalizeAgentCommandReplyPayloads", () => {
396410
});
397411
});
398412

413+
it("passes reply payload hook metadata through recovered agent delivery", async () => {
414+
deliverOutboundPayloadsMock.mockResolvedValue([{ channel: "slack", messageId: "msg-1" }]);
415+
416+
await deliverAgentCommandResult({
417+
cfg: {
418+
agents: {
419+
list: [{ id: "tester", workspace: "/tmp/agent-workspace" }],
420+
},
421+
} as OpenClawConfig,
422+
deps: {} as CliDeps,
423+
runtime: { log: vi.fn(), error: vi.fn() } as never,
424+
opts: {
425+
message: "resume interrupted turn",
426+
deliver: true,
427+
bestEffortDeliver: true,
428+
channel: "slack",
429+
to: "#general",
430+
accountId: "workspace-1",
431+
threadId: "thread-7",
432+
sessionKey: "agent:tester:slack:direct:alice",
433+
runId: "run-recovered",
434+
} as AgentCommandOpts,
435+
outboundSession: {
436+
key: "agent:tester:slack:direct:alice",
437+
agentId: "tester",
438+
} as never,
439+
sessionEntry: {
440+
sessionId: "session-1",
441+
updatedAt: 1,
442+
restartRecoveryDeliveryContext: {
443+
channel: "slack",
444+
to: "#general",
445+
accountId: "workspace-1",
446+
threadId: "thread-7",
447+
},
448+
restartRecoveryDeliveryRunId: "run-recovered",
449+
},
450+
payloads: [{ text: "after restart" }],
451+
result: createResult(),
452+
});
453+
454+
expect(latestOutboundDeliveryArgs().replyPayloadSendingHook).toEqual({
455+
kind: "final",
456+
channel: "slack",
457+
sessionKey: "agent:tester:slack:direct:alice",
458+
runId: "run-recovered",
459+
context: {
460+
channelId: "slack",
461+
accountId: "workspace-1",
462+
conversationId: "#general",
463+
sessionKey: "agent:tester:slack:direct:alice",
464+
runId: "run-recovered",
465+
},
466+
});
467+
});
468+
399469
it("does not refresh final delivery routing from a different logical session", async () => {
400470
deliverOutboundPayloadsMock.mockResolvedValue([{ channel: "slack", messageId: "msg-1" }]);
401471
const runtime = { log: vi.fn(), error: vi.fn() };

‎src/agents/command/delivery.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
resolveAgentOutboundTarget,
1919
} from "../../infra/outbound/agent-delivery.js";
2020
import { resolveMessageChannelSelection } from "../../infra/outbound/channel-selection.js";
21+
import type { QueuedReplyPayloadSendingHook } from "../../infra/outbound/delivery-queue-storage.js";
2122
import { buildOutboundResultEnvelope } from "../../infra/outbound/envelope.js";
2223
import {
2324
createOutboundPayloadPlan,
@@ -310,6 +311,33 @@ function noVisiblePayloadStatus(): AgentCommandDeliveryStatus {
310311
};
311312
}
312313

314+
function buildReplyPayloadSendingHook(params: {
315+
channel: string;
316+
to: string;
317+
accountId?: string;
318+
sessionKey?: string;
319+
runId?: string;
320+
}): QueuedReplyPayloadSendingHook {
321+
const sessionKey = hasNonEmptyString(params.sessionKey) ? params.sessionKey : undefined;
322+
const runId = hasNonEmptyString(params.runId) ? params.runId : undefined;
323+
324+
// Agent-command delivery bypasses the auto-reply dispatcher, so rebuild the
325+
// same pre-delivery hook context before handing payloads to durable send.
326+
return {
327+
kind: "final",
328+
channel: params.channel,
329+
...(sessionKey ? { sessionKey } : {}),
330+
...(runId ? { runId } : {}),
331+
context: {
332+
channelId: params.channel,
333+
...(hasNonEmptyString(params.accountId) ? { accountId: params.accountId } : {}),
334+
conversationId: params.to,
335+
...(sessionKey ? { sessionKey } : {}),
336+
...(runId ? { runId } : {}),
337+
},
338+
};
339+
}
340+
313341
async function normalizeReplyMediaPathsForDelivery(params: {
314342
cfg: OpenClawConfig;
315343
payloads: ReplyPayload[];
@@ -712,6 +740,13 @@ export async function deliverAgentCommandResult(
712740
threadId: resolvedThreadTarget ?? null,
713741
bestEffort: bestEffortDeliver,
714742
durability: bestEffortDeliver ? "best_effort" : "required",
743+
replyPayloadSendingHook: buildReplyPayloadSendingHook({
744+
channel: deliveryChannel,
745+
to: deliveryTarget,
746+
accountId: resolvedAccountId,
747+
sessionKey: effectiveSessionKey,
748+
runId: opts.runId,
749+
}),
715750
onError: logDeliveryError,
716751
onPayload: logPayload,
717752
deps: createOutboundSendDeps(deps),

0 commit comments

Comments
 (0)