mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-07 23:11:25 +00:00
fix(routing): preserve explicit cron account and bound message defaults
Co-authored-by: lbo728 <72309817+lbo728@users.noreply.github.com> Co-authored-by: stakeswky <64798754+stakeswky@users.noreply.github.com>
This commit is contained in:
@@ -54,4 +54,22 @@ describe("resolveCronDeliveryPlan", () => {
|
||||
expect(plan.channel).toBeUndefined();
|
||||
expect(plan.to).toBe("https://example.invalid/cron");
|
||||
});
|
||||
|
||||
it("threads delivery.accountId when explicitly configured", () => {
|
||||
const plan = resolveCronDeliveryPlan(
|
||||
makeJob({
|
||||
delivery: {
|
||||
mode: "announce",
|
||||
channel: "telegram",
|
||||
to: "123",
|
||||
accountId: " bot-a ",
|
||||
},
|
||||
}),
|
||||
);
|
||||
expect(plan.mode).toBe("announce");
|
||||
expect(plan.requested).toBe(true);
|
||||
expect(plan.channel).toBe("telegram");
|
||||
expect(plan.to).toBe("123");
|
||||
expect(plan.accountId).toBe("bot-a");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -4,6 +4,7 @@ export type CronDeliveryPlan = {
|
||||
mode: CronDeliveryMode;
|
||||
channel?: CronMessageChannel;
|
||||
to?: string;
|
||||
accountId?: string;
|
||||
source: "delivery" | "payload";
|
||||
requested: boolean;
|
||||
};
|
||||
@@ -27,6 +28,14 @@ function normalizeTo(value: unknown): string | undefined {
|
||||
return trimmed ? trimmed : undefined;
|
||||
}
|
||||
|
||||
function normalizeAccountId(value: unknown): string | undefined {
|
||||
if (typeof value !== "string") {
|
||||
return undefined;
|
||||
}
|
||||
const trimmed = value.trim();
|
||||
return trimmed ? trimmed : undefined;
|
||||
}
|
||||
|
||||
export function resolveCronDeliveryPlan(job: CronJob): CronDeliveryPlan {
|
||||
const payload = job.payload.kind === "agentTurn" ? job.payload : null;
|
||||
const delivery = job.delivery;
|
||||
@@ -50,6 +59,9 @@ export function resolveCronDeliveryPlan(job: CronJob): CronDeliveryPlan {
|
||||
(delivery as { channel?: unknown } | undefined)?.channel,
|
||||
);
|
||||
const deliveryTo = normalizeTo((delivery as { to?: unknown } | undefined)?.to);
|
||||
const deliveryAccountId = normalizeAccountId(
|
||||
(delivery as { accountId?: unknown } | undefined)?.accountId,
|
||||
);
|
||||
|
||||
const channel = deliveryChannel ?? payloadChannel ?? "last";
|
||||
const to = deliveryTo ?? payloadTo;
|
||||
@@ -59,6 +71,7 @@ export function resolveCronDeliveryPlan(job: CronJob): CronDeliveryPlan {
|
||||
mode: resolvedMode,
|
||||
channel: resolvedMode === "announce" ? channel : undefined,
|
||||
to,
|
||||
accountId: deliveryAccountId,
|
||||
source: "delivery",
|
||||
requested: resolvedMode === "announce",
|
||||
};
|
||||
|
||||
@@ -299,4 +299,39 @@ describe("resolveDeliveryTarget", () => {
|
||||
expect(result.to).toBe("987654");
|
||||
expect(result.ok).toBe(true);
|
||||
});
|
||||
|
||||
it("explicit delivery.accountId overrides session-derived accountId", async () => {
|
||||
setMainSessionEntry({
|
||||
sessionId: "sess-5",
|
||||
updatedAt: 1000,
|
||||
lastChannel: "telegram",
|
||||
lastTo: "chat-999",
|
||||
lastAccountId: "default",
|
||||
});
|
||||
|
||||
const result = await resolveDeliveryTarget(makeCfg({ bindings: [] }), AGENT_ID, {
|
||||
channel: "telegram",
|
||||
to: "chat-999",
|
||||
accountId: "bot-b",
|
||||
});
|
||||
|
||||
expect(result.ok).toBe(true);
|
||||
expect(result.accountId).toBe("bot-b");
|
||||
});
|
||||
|
||||
it("explicit delivery.accountId overrides bindings-derived accountId", async () => {
|
||||
setMainSessionEntry(undefined);
|
||||
const cfg = makeCfg({
|
||||
bindings: [{ agentId: AGENT_ID, match: { channel: "telegram", accountId: "bound" } }],
|
||||
});
|
||||
|
||||
const result = await resolveDeliveryTarget(cfg, AGENT_ID, {
|
||||
channel: "telegram",
|
||||
to: "chat-777",
|
||||
accountId: "explicit",
|
||||
});
|
||||
|
||||
expect(result.ok).toBe(true);
|
||||
expect(result.accountId).toBe("explicit");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -43,6 +43,7 @@ export async function resolveDeliveryTarget(
|
||||
channel?: "last" | ChannelId;
|
||||
to?: string;
|
||||
sessionKey?: string;
|
||||
accountId?: string;
|
||||
},
|
||||
): Promise<DeliveryTargetResolution> {
|
||||
const requestedChannel = typeof jobPayload.channel === "string" ? jobPayload.channel : "last";
|
||||
@@ -114,6 +115,11 @@ export async function resolveDeliveryTarget(
|
||||
}
|
||||
}
|
||||
|
||||
// Explicit delivery account should override inferred session/binding account.
|
||||
if (jobPayload.accountId) {
|
||||
accountId = jobPayload.accountId;
|
||||
}
|
||||
|
||||
// Carry threadId when it was explicitly set (from :topic: parsing or config)
|
||||
// or when delivering to the same recipient as the session's last conversation.
|
||||
// Session-derived threadIds are dropped when the target differs to prevent
|
||||
|
||||
@@ -314,6 +314,7 @@ export async function runCronIsolatedAgentTurn(params: {
|
||||
channel: deliveryPlan.channel ?? "last",
|
||||
to: deliveryPlan.to,
|
||||
sessionKey: params.job.sessionKey,
|
||||
accountId: deliveryPlan.accountId,
|
||||
});
|
||||
|
||||
const { formattedTime, timeLine } = resolveCronStyleNow(params.cfg, now);
|
||||
|
||||
@@ -22,6 +22,7 @@ export type CronDelivery = {
|
||||
mode: CronDeliveryMode;
|
||||
channel?: CronMessageChannel;
|
||||
to?: string;
|
||||
accountId?: string;
|
||||
bestEffort?: boolean;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user