2026-01-19 00:45:03 +00:00
|
|
|
import { normalizeAccountId } from "../../utils/account-id.js";
|
|
|
|
|
import { resolveMessageChannel } from "../../utils/message-channel.js";
|
2026-02-18 01:34:35 +00:00
|
|
|
import type { AgentCommandOpts, AgentRunContext } from "./types.js";
|
2026-01-19 00:45:03 +00:00
|
|
|
|
|
|
|
|
export function resolveAgentRunContext(opts: AgentCommandOpts): AgentRunContext {
|
|
|
|
|
const merged: AgentRunContext = opts.runContext ? { ...opts.runContext } : {};
|
|
|
|
|
|
|
|
|
|
const normalizedChannel = resolveMessageChannel(
|
|
|
|
|
merged.messageChannel ?? opts.messageChannel,
|
|
|
|
|
opts.replyChannel ?? opts.channel,
|
|
|
|
|
);
|
2026-01-31 16:19:20 +09:00
|
|
|
if (normalizedChannel) {
|
|
|
|
|
merged.messageChannel = normalizedChannel;
|
|
|
|
|
}
|
2026-01-19 00:45:03 +00:00
|
|
|
|
|
|
|
|
const normalizedAccountId = normalizeAccountId(merged.accountId ?? opts.accountId);
|
2026-01-31 16:19:20 +09:00
|
|
|
if (normalizedAccountId) {
|
|
|
|
|
merged.accountId = normalizedAccountId;
|
|
|
|
|
}
|
2026-01-19 00:45:03 +00:00
|
|
|
|
2026-01-24 05:49:23 +00:00
|
|
|
const groupId = (merged.groupId ?? opts.groupId)?.toString().trim();
|
2026-01-31 16:19:20 +09:00
|
|
|
if (groupId) {
|
|
|
|
|
merged.groupId = groupId;
|
|
|
|
|
}
|
2026-01-24 05:49:23 +00:00
|
|
|
|
|
|
|
|
const groupChannel = (merged.groupChannel ?? opts.groupChannel)?.toString().trim();
|
2026-01-31 16:19:20 +09:00
|
|
|
if (groupChannel) {
|
|
|
|
|
merged.groupChannel = groupChannel;
|
|
|
|
|
}
|
2026-01-24 05:49:23 +00:00
|
|
|
|
|
|
|
|
const groupSpace = (merged.groupSpace ?? opts.groupSpace)?.toString().trim();
|
2026-01-31 16:19:20 +09:00
|
|
|
if (groupSpace) {
|
|
|
|
|
merged.groupSpace = groupSpace;
|
|
|
|
|
}
|
2026-01-24 05:49:23 +00:00
|
|
|
|
2026-01-20 17:22:07 +00:00
|
|
|
if (
|
|
|
|
|
merged.currentThreadTs == null &&
|
|
|
|
|
opts.threadId != null &&
|
|
|
|
|
opts.threadId !== "" &&
|
|
|
|
|
opts.threadId !== null
|
|
|
|
|
) {
|
|
|
|
|
merged.currentThreadTs = String(opts.threadId);
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-02 17:05:55 +01:00
|
|
|
// Populate currentChannelId from the outbound target so that
|
|
|
|
|
// resolveTelegramAutoThreadId can match the originating chat.
|
|
|
|
|
if (!merged.currentChannelId && opts.to) {
|
|
|
|
|
const trimmedTo = opts.to.trim();
|
|
|
|
|
if (trimmedTo) {
|
|
|
|
|
merged.currentChannelId = trimmedTo;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-19 00:45:03 +00:00
|
|
|
return merged;
|
|
|
|
|
}
|