Skip to content

Commit 7c28e8b

Browse files
committed
fix: route agent command replies through reply hooks
1 parent 6868cde commit 7c28e8b

2 files changed

Lines changed: 82 additions & 18 deletions

File tree

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

Lines changed: 69 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Covers agent-command reply normalization and outbound delivery status.
2+
import path from "node:path";
23
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
34
import type { ReplyPayload } from "../../auto-reply/reply-payload.js";
45
import type { ChannelOutboundAdapter } from "../../channels/plugins/types.js";
@@ -38,6 +39,21 @@ type MediaNormalizerOptions = {
3839
workspaceDir?: unknown;
3940
messageProvider?: unknown;
4041
};
42+
type OutboundDeliveryArgs = {
43+
channel?: string;
44+
to?: string;
45+
accountId?: string;
46+
payloads: ReplyPayload[];
47+
bestEffort?: boolean;
48+
queuePolicy?: string;
49+
replyPayloadSendingHook?: {
50+
kind?: unknown;
51+
channel?: unknown;
52+
sessionKey?: unknown;
53+
runId?: unknown;
54+
context?: Record<string, unknown>;
55+
};
56+
};
4157

4258
const slackOutboundForTest: ChannelOutboundAdapter = {
4359
deliveryMode: "direct",
@@ -46,6 +62,7 @@ const slackOutboundForTest: ChannelOutboundAdapter = {
4662
messageId: `${to}:${text}`,
4763
}),
4864
};
65+
const expectedAgentWorkspaceDir = path.resolve("/tmp/agent-workspace");
4966

5067
// Two registries let tests switch between no-channel and Slack-capable delivery
5168
// without loading the full plugin runtime.
@@ -105,26 +122,12 @@ function latestNormalizerOptions(): MediaNormalizerOptions {
105122
return options as MediaNormalizerOptions;
106123
}
107124

108-
function latestOutboundDeliveryArgs(): {
109-
channel?: string;
110-
to?: string;
111-
accountId?: string;
112-
payloads: ReplyPayload[];
113-
bestEffort?: boolean;
114-
queuePolicy?: string;
115-
} {
125+
function latestOutboundDeliveryArgs(): OutboundDeliveryArgs {
116126
const args = lastMockArg(deliverOutboundPayloadsMock, "outbound delivery arguments");
117127
if (!args || typeof args !== "object") {
118128
throw new Error("expected outbound delivery arguments");
119129
}
120-
return args as {
121-
channel?: string;
122-
to?: string;
123-
accountId?: string;
124-
payloads: ReplyPayload[];
125-
bestEffort?: boolean;
126-
queuePolicy?: string;
127-
};
130+
return args as OutboundDeliveryArgs;
128131
}
129132

130133
type DeliveryStatusLike = {
@@ -316,7 +319,7 @@ describe("normalizeAgentCommandReplyPayloads", () => {
316319
const normalizerOptions = latestNormalizerOptions();
317320
expect(normalizerOptions.sessionKey).toBe("agent:tester:slack:direct:alice");
318321
expect(normalizerOptions.agentId).toBe("tester");
319-
expect(normalizerOptions.workspaceDir).toBe("/tmp/agent-workspace");
322+
expect(normalizerOptions.workspaceDir).toBe(expectedAgentWorkspaceDir);
320323
expect(normalizerOptions.messageProvider).toBe("slack");
321324

322325
const normalizedInput = normalizerFn.mock.calls[0]?.[0];
@@ -346,6 +349,54 @@ describe("normalizeAgentCommandReplyPayloads", () => {
346349
});
347350
});
348351

352+
it("passes reply payload hook metadata through durable final delivery", async () => {
353+
deliverOutboundPayloadsMock.mockResolvedValue([{ channel: "slack", messageId: "msg-1" }]);
354+
355+
await deliverAgentCommandResult({
356+
cfg: {
357+
agents: {
358+
list: [{ id: "tester", workspace: "/tmp/agent-workspace" }],
359+
},
360+
} as OpenClawConfig,
361+
deps: {} as CliDeps,
362+
runtime: { log: vi.fn(), error: vi.fn() } as never,
363+
opts: {
364+
message: "go",
365+
deliver: true,
366+
replyChannel: "slack",
367+
replyTo: "#general",
368+
replyAccountId: "workspace-1",
369+
threadId: "thread-1",
370+
runId: "run-1",
371+
} as AgentCommandOpts,
372+
outboundSession: {
373+
key: "agent:tester:slack:direct:alice",
374+
agentId: "tester",
375+
} as never,
376+
sessionEntry: {
377+
sessionId: "session-1",
378+
updatedAt: 1,
379+
},
380+
payloads: [{ text: "final answer" }],
381+
result: createResult(),
382+
});
383+
384+
const deliverArgs = latestOutboundDeliveryArgs();
385+
expect(deliverArgs.replyPayloadSendingHook).toEqual({
386+
kind: "final",
387+
channel: "slack",
388+
sessionKey: "agent:tester:slack:direct:alice",
389+
runId: "run-1",
390+
context: {
391+
channelId: "slack",
392+
accountId: "workspace-1",
393+
conversationId: "#general",
394+
sessionKey: "agent:tester:slack:direct:alice",
395+
runId: "run-1",
396+
},
397+
});
398+
});
399+
349400
it("refreshes stale implicit session routing before final delivery", async () => {
350401
deliverOutboundPayloadsMock.mockResolvedValue([{ channel: "slack", messageId: "msg-1" }]);
351402
const runtime = { log: vi.fn(), error: vi.fn() };
@@ -538,7 +589,7 @@ describe("normalizeAgentCommandReplyPayloads", () => {
538589
const normalizerOptions = latestNormalizerOptions();
539590
expect(normalizerOptions.agentId).toBe("tester");
540591
expect(normalizerOptions.sessionKey).toBeUndefined();
541-
expect(normalizerOptions.workspaceDir).toBe("/tmp/agent-workspace");
592+
expect(normalizerOptions.workspaceDir).toBe(expectedAgentWorkspaceDir);
542593
});
543594

544595
it("keeps LINE directive-only replies intact for local preview when delivery is disabled", async () => {

‎src/agents/command/delivery.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,19 @@ export async function deliverAgentCommandResult(
717717
accountId: resolvedAccountId,
718718
payloads: deliveryPayloads,
719719
session: outboundSession,
720+
replyPayloadSendingHook: {
721+
kind: "final",
722+
channel: deliveryChannel,
723+
...(effectiveSessionKey ? { sessionKey: effectiveSessionKey } : {}),
724+
...(opts.runId ? { runId: opts.runId } : {}),
725+
context: {
726+
channelId: deliveryChannel,
727+
...(resolvedAccountId ? { accountId: resolvedAccountId } : {}),
728+
conversationId: deliveryTarget,
729+
...(effectiveSessionKey ? { sessionKey: effectiveSessionKey } : {}),
730+
...(opts.runId ? { runId: opts.runId } : {}),
731+
},
732+
},
720733
replyToId: resolvedReplyToId ?? null,
721734
threadId: resolvedThreadTarget ?? null,
722735
bestEffort: bestEffortDeliver,

0 commit comments

Comments
 (0)