mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 00:11:23 +00:00
fix(gateway): preserve turn-origin messageChannel in agent runs
This commit is contained in:
@@ -184,6 +184,8 @@ async function invokeAgent(
|
|||||||
respond?: ReturnType<typeof vi.fn>;
|
respond?: ReturnType<typeof vi.fn>;
|
||||||
reqId?: string;
|
reqId?: string;
|
||||||
context?: GatewayRequestContext;
|
context?: GatewayRequestContext;
|
||||||
|
client?: AgentHandlerArgs["client"];
|
||||||
|
isWebchatConnect?: AgentHandlerArgs["isWebchatConnect"];
|
||||||
},
|
},
|
||||||
) {
|
) {
|
||||||
const respond = options?.respond ?? vi.fn();
|
const respond = options?.respond ?? vi.fn();
|
||||||
@@ -192,8 +194,8 @@ async function invokeAgent(
|
|||||||
respond: respond as never,
|
respond: respond as never,
|
||||||
context: options?.context ?? makeContext(),
|
context: options?.context ?? makeContext(),
|
||||||
req: { type: "req", id: options?.reqId ?? "agent-test-req", method: "agent" },
|
req: { type: "req", id: options?.reqId ?? "agent-test-req", method: "agent" },
|
||||||
client: null,
|
client: options?.client ?? null,
|
||||||
isWebchatConnect: () => false,
|
isWebchatConnect: options?.isWebchatConnect ?? (() => false),
|
||||||
});
|
});
|
||||||
return respond;
|
return respond;
|
||||||
}
|
}
|
||||||
@@ -346,6 +348,56 @@ describe("gateway agent handler", () => {
|
|||||||
expect(callArgs.bestEffortDeliver).toBe(false);
|
expect(callArgs.bestEffortDeliver).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("keeps origin messageChannel as webchat while delivery channel uses last session channel", async () => {
|
||||||
|
mockMainSessionEntry({
|
||||||
|
sessionId: "existing-session-id",
|
||||||
|
lastChannel: "telegram",
|
||||||
|
lastTo: "12345",
|
||||||
|
});
|
||||||
|
mocks.updateSessionStore.mockImplementation(async (_path, updater) => {
|
||||||
|
const store: Record<string, unknown> = {
|
||||||
|
"agent:main:main": {
|
||||||
|
sessionId: "existing-session-id",
|
||||||
|
updatedAt: Date.now(),
|
||||||
|
lastChannel: "telegram",
|
||||||
|
lastTo: "12345",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
return await updater(store);
|
||||||
|
});
|
||||||
|
mocks.agentCommand.mockResolvedValue({
|
||||||
|
payloads: [{ text: "ok" }],
|
||||||
|
meta: { durationMs: 100 },
|
||||||
|
});
|
||||||
|
|
||||||
|
await invokeAgent(
|
||||||
|
{
|
||||||
|
message: "webchat turn",
|
||||||
|
sessionKey: "agent:main:main",
|
||||||
|
idempotencyKey: "test-webchat-origin-channel",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
reqId: "webchat-origin-1",
|
||||||
|
client: {
|
||||||
|
connect: {
|
||||||
|
client: { id: "webchat-ui", mode: "webchat" },
|
||||||
|
},
|
||||||
|
} as AgentHandlerArgs["client"],
|
||||||
|
isWebchatConnect: () => true,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
await vi.waitFor(() => expect(mocks.agentCommand).toHaveBeenCalled());
|
||||||
|
const callArgs = mocks.agentCommand.mock.calls.at(-1)?.[0] as {
|
||||||
|
channel?: string;
|
||||||
|
messageChannel?: string;
|
||||||
|
runContext?: { messageChannel?: string };
|
||||||
|
};
|
||||||
|
expect(callArgs.channel).toBe("telegram");
|
||||||
|
expect(callArgs.messageChannel).toBe("webchat");
|
||||||
|
expect(callArgs.runContext?.messageChannel).toBe("webchat");
|
||||||
|
});
|
||||||
|
|
||||||
it("handles missing cliSessionIds gracefully", async () => {
|
it("handles missing cliSessionIds gracefully", async () => {
|
||||||
mockMainSessionEntry({});
|
mockMainSessionEntry({});
|
||||||
|
|
||||||
|
|||||||
@@ -563,6 +563,17 @@ export const agentHandlers: GatewayRequestHandlers = {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const normalizedTurnSource = normalizeMessageChannel(turnSourceChannel);
|
||||||
|
const turnSourceMessageChannel =
|
||||||
|
normalizedTurnSource && isGatewayMessageChannel(normalizedTurnSource)
|
||||||
|
? normalizedTurnSource
|
||||||
|
: undefined;
|
||||||
|
const originMessageChannel =
|
||||||
|
turnSourceMessageChannel ??
|
||||||
|
(client?.connect && isWebchatConnect(client.connect)
|
||||||
|
? INTERNAL_MESSAGE_CHANNEL
|
||||||
|
: resolvedChannel);
|
||||||
|
|
||||||
const deliver = request.deliver === true && resolvedChannel !== INTERNAL_MESSAGE_CHANNEL;
|
const deliver = request.deliver === true && resolvedChannel !== INTERNAL_MESSAGE_CHANNEL;
|
||||||
|
|
||||||
const accepted = {
|
const accepted = {
|
||||||
@@ -594,7 +605,7 @@ export const agentHandlers: GatewayRequestHandlers = {
|
|||||||
accountId: resolvedAccountId,
|
accountId: resolvedAccountId,
|
||||||
threadId: resolvedThreadId,
|
threadId: resolvedThreadId,
|
||||||
runContext: {
|
runContext: {
|
||||||
messageChannel: resolvedChannel,
|
messageChannel: originMessageChannel,
|
||||||
accountId: resolvedAccountId,
|
accountId: resolvedAccountId,
|
||||||
groupId: resolvedGroupId,
|
groupId: resolvedGroupId,
|
||||||
groupChannel: resolvedGroupChannel,
|
groupChannel: resolvedGroupChannel,
|
||||||
@@ -607,7 +618,7 @@ export const agentHandlers: GatewayRequestHandlers = {
|
|||||||
spawnedBy: spawnedByValue,
|
spawnedBy: spawnedByValue,
|
||||||
timeout: request.timeout?.toString(),
|
timeout: request.timeout?.toString(),
|
||||||
bestEffortDeliver,
|
bestEffortDeliver,
|
||||||
messageChannel: resolvedChannel,
|
messageChannel: originMessageChannel,
|
||||||
runId,
|
runId,
|
||||||
lane: request.lane,
|
lane: request.lane,
|
||||||
extraSystemPrompt: request.extraSystemPrompt,
|
extraSystemPrompt: request.extraSystemPrompt,
|
||||||
|
|||||||
Reference in New Issue
Block a user