File size: 1,998 Bytes
e249c6d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | /**
* Resolves channel/account/thread run context for agent command execution.
*/
import { stringifyRouteThreadId } from "../../plugin-sdk/channel-route.js";
import { normalizeOptionalAccountId } from "../../routing/account-id.js";
import { resolveMessageChannel } from "../../utils/message-channel.js";
import type { AgentCommandOpts, AgentRunContext } from "./types.js";
/** Merges explicit run context with command routing options. */
export function resolveAgentRunContext(opts: AgentCommandOpts): AgentRunContext {
const merged: AgentRunContext = opts.runContext ? { ...opts.runContext } : {};
const normalizedChannel = resolveMessageChannel(
merged.messageChannel ?? opts.messageChannel,
opts.replyChannel ?? opts.channel,
);
if (normalizedChannel) {
merged.messageChannel = normalizedChannel;
}
const normalizedAccountId = normalizeOptionalAccountId(merged.accountId ?? opts.accountId);
if (normalizedAccountId) {
merged.accountId = normalizedAccountId;
}
const groupId = (merged.groupId ?? opts.groupId)?.toString().trim();
if (groupId) {
merged.groupId = groupId;
}
const groupChannel = (merged.groupChannel ?? opts.groupChannel)?.toString().trim();
if (groupChannel) {
merged.groupChannel = groupChannel;
}
const groupSpace = (merged.groupSpace ?? opts.groupSpace)?.toString().trim();
if (groupSpace) {
merged.groupSpace = groupSpace;
}
if (
merged.currentThreadTs == null &&
opts.threadId != null &&
opts.threadId !== "" &&
opts.threadId !== null
) {
const threadId = stringifyRouteThreadId(opts.threadId);
if (threadId) {
merged.currentThreadTs = threadId;
}
}
// Populate currentChannelId from the outbound target so channel threading
// adapters can detect same-conversation auto-threading.
if (!merged.currentChannelId && opts.to) {
const trimmedTo = opts.to.trim();
if (trimmedTo) {
merged.currentChannelId = trimmedTo;
}
}
return merged;
}
|