refactor: reuse agent outbound target resolution

Co-authored-by: Adam Holt <mail@adamholt.co.nz>
This commit is contained in:
Peter Steinberger
2026-01-17 06:54:12 +00:00
parent 6a3ed5c850
commit 7b31b280f8
4 changed files with 151 additions and 19 deletions

View File

@@ -10,8 +10,10 @@ import {
normalizeOutboundPayloads,
normalizeOutboundPayloadsForJson,
} from "../../infra/outbound/payloads.js";
import { resolveAgentDeliveryPlan } from "../../infra/outbound/agent-delivery.js";
import { resolveOutboundTarget } from "../../infra/outbound/targets.js";
import {
resolveAgentDeliveryPlan,
resolveAgentOutboundTarget,
} from "../../infra/outbound/agent-delivery.js";
import type { RuntimeEnv } from "../../runtime.js";
import { isInternalMessageChannel } from "../../utils/message-channel.js";
import type { AgentCommandOpts } from "./types.js";
@@ -51,17 +53,21 @@ export async function deliverAgentCommandResult(params: {
const targetMode =
opts.deliveryTargetMode ?? deliveryPlan.deliveryTargetMode ?? (opts.to ? "explicit" : "implicit");
const resolvedAccountId = deliveryPlan.resolvedAccountId;
const resolvedTarget =
const resolved =
deliver && isDeliveryChannelKnown && deliveryChannel
? resolveOutboundTarget({
channel: deliveryChannel,
to: deliveryPlan.resolvedTo,
? resolveAgentOutboundTarget({
cfg,
accountId: resolvedAccountId,
mode: targetMode,
plan: deliveryPlan,
targetMode,
validateExplicitTarget: true,
})
: null;
const deliveryTarget = resolvedTarget?.ok ? resolvedTarget.to : undefined;
: {
resolvedTarget: null,
resolvedTo: deliveryPlan.resolvedTo,
targetMode,
};
const resolvedTarget = resolved.resolvedTarget;
const deliveryTarget = resolved.resolvedTo;
const logDeliveryError = (err: unknown) => {
const message = `Delivery failed (${deliveryChannel}${deliveryTarget ? ` to ${deliveryTarget}` : ""}): ${String(err)}`;