refactor: unify delivery target resolution
Co-authored-by: adam91holt <adam91holt@users.noreply.github.com>
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
import { normalizeChannelId } from "../../channels/plugins/index.js";
|
||||
import type { ChannelId } from "../../channels/plugins/types.js";
|
||||
import { DEFAULT_CHAT_CHANNEL } from "../../channels/registry.js";
|
||||
import type { ClawdbotConfig } from "../../config/config.js";
|
||||
@@ -9,8 +8,7 @@ import {
|
||||
} from "../../config/sessions.js";
|
||||
import { resolveMessageChannelSelection } from "../../infra/outbound/channel-selection.js";
|
||||
import type { OutboundChannel } from "../../infra/outbound/targets.js";
|
||||
import { resolveOutboundTarget } from "../../infra/outbound/targets.js";
|
||||
import { INTERNAL_MESSAGE_CHANNEL, normalizeMessageChannel } from "../../utils/message-channel.js";
|
||||
import { resolveOutboundTarget, resolveSessionDeliveryTarget } from "../../infra/outbound/targets.js";
|
||||
|
||||
export async function resolveDeliveryTarget(
|
||||
cfg: ClawdbotConfig,
|
||||
@@ -26,56 +24,63 @@ export async function resolveDeliveryTarget(
|
||||
mode: "explicit" | "implicit";
|
||||
error?: Error;
|
||||
}> {
|
||||
const requestedRaw = typeof jobPayload.channel === "string" ? jobPayload.channel : "last";
|
||||
const requestedChannelHint = normalizeMessageChannel(requestedRaw) ?? requestedRaw;
|
||||
const explicitTo =
|
||||
typeof jobPayload.to === "string" && jobPayload.to.trim() ? jobPayload.to.trim() : undefined;
|
||||
const requestedChannel = typeof jobPayload.channel === "string" ? jobPayload.channel : "last";
|
||||
const explicitTo = typeof jobPayload.to === "string" ? jobPayload.to : undefined;
|
||||
|
||||
const sessionCfg = cfg.session;
|
||||
const mainSessionKey = resolveAgentMainSessionKey({ cfg, agentId });
|
||||
const storePath = resolveStorePath(sessionCfg?.store, { agentId });
|
||||
const store = loadSessionStore(storePath);
|
||||
const main = store[mainSessionKey];
|
||||
const lastChannel =
|
||||
main?.lastChannel && main.lastChannel !== INTERNAL_MESSAGE_CHANNEL
|
||||
? normalizeChannelId(main.lastChannel)
|
||||
: undefined;
|
||||
const lastTo = typeof main?.lastTo === "string" ? main.lastTo.trim() : "";
|
||||
const lastAccountId = main?.lastAccountId;
|
||||
|
||||
let channel: Exclude<OutboundChannel, "none"> | undefined =
|
||||
requestedChannelHint === "last"
|
||||
? (lastChannel ?? undefined)
|
||||
: requestedChannelHint === INTERNAL_MESSAGE_CHANNEL
|
||||
? undefined
|
||||
: (normalizeChannelId(requestedChannelHint) ?? undefined);
|
||||
if (!channel) {
|
||||
const preliminary = resolveSessionDeliveryTarget({
|
||||
entry: main,
|
||||
requestedChannel,
|
||||
explicitTo,
|
||||
allowMismatchedLastTo: true,
|
||||
});
|
||||
|
||||
let fallbackChannel: Exclude<OutboundChannel, "none"> | undefined;
|
||||
if (!preliminary.channel) {
|
||||
try {
|
||||
const selection = await resolveMessageChannelSelection({ cfg });
|
||||
channel = selection.channel;
|
||||
fallbackChannel = selection.channel;
|
||||
} catch {
|
||||
channel = lastChannel ?? DEFAULT_CHAT_CHANNEL;
|
||||
fallbackChannel = preliminary.lastChannel ?? DEFAULT_CHAT_CHANNEL;
|
||||
}
|
||||
}
|
||||
|
||||
const toCandidate = explicitTo ?? (lastTo || undefined);
|
||||
const mode: "explicit" | "implicit" = explicitTo ? "explicit" : "implicit";
|
||||
const resolved = fallbackChannel
|
||||
? resolveSessionDeliveryTarget({
|
||||
entry: main,
|
||||
requestedChannel,
|
||||
explicitTo,
|
||||
fallbackChannel,
|
||||
allowMismatchedLastTo: true,
|
||||
mode: preliminary.mode,
|
||||
})
|
||||
: preliminary;
|
||||
|
||||
const channel = resolved.channel ?? fallbackChannel ?? DEFAULT_CHAT_CHANNEL;
|
||||
const mode = resolved.mode as "explicit" | "implicit";
|
||||
const toCandidate = resolved.to;
|
||||
|
||||
if (!toCandidate) {
|
||||
return { channel, to: undefined, accountId: lastAccountId, mode };
|
||||
return { channel, to: undefined, accountId: resolved.accountId, mode };
|
||||
}
|
||||
|
||||
const resolved = resolveOutboundTarget({
|
||||
const docked = resolveOutboundTarget({
|
||||
channel,
|
||||
to: toCandidate,
|
||||
cfg,
|
||||
accountId: channel === lastChannel ? lastAccountId : undefined,
|
||||
accountId: resolved.accountId,
|
||||
mode,
|
||||
});
|
||||
return {
|
||||
channel,
|
||||
to: resolved.ok ? resolved.to : undefined,
|
||||
accountId: channel === lastChannel ? lastAccountId : undefined,
|
||||
to: docked.ok ? docked.to : undefined,
|
||||
accountId: resolved.accountId,
|
||||
mode,
|
||||
error: resolved.ok ? undefined : resolved.error,
|
||||
error: docked.ok ? undefined : docked.error,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user