fix: stop hardcoded channel fallback and auto-pick sole configured channel (#23357) (thanks @lbo728)

Co-authored-by: lbo728 <extreme0728@gmail.com>
This commit is contained in:
Peter Steinberger
2026-02-22 11:20:33 +01:00
parent e33d7fcd13
commit 1cd3b30907
18 changed files with 355 additions and 91 deletions

View File

@@ -1,5 +1,4 @@
import { listAgentIds } from "../agents/agent-scope.js";
import { DEFAULT_CHAT_CHANNEL } from "../channels/registry.js";
import { formatCliCommand } from "../cli/command-format.js";
import type { CliDeps } from "../cli/deps.js";
import { withProgress } from "../cli/progress.js";
@@ -118,7 +117,7 @@ export async function agentViaGatewayCommand(opts: AgentCliOpts, runtime: Runtim
sessionId: opts.sessionId,
}).sessionKey;
const channel = normalizeMessageChannel(opts.channel) ?? DEFAULT_CHAT_CHANNEL;
const channel = normalizeMessageChannel(opts.channel);
const idempotencyKey = opts.runId?.trim() || randomIdempotencyKey();
const response = await withProgress(

View File

@@ -8,6 +8,7 @@ import {
resolveAgentDeliveryPlan,
resolveAgentOutboundTarget,
} from "../../infra/outbound/agent-delivery.js";
import { resolveMessageChannelSelection } from "../../infra/outbound/channel-selection.js";
import { deliverOutboundPayloads } from "../../infra/outbound/deliver.js";
import { buildOutboundResultEnvelope } from "../../infra/outbound/envelope.js";
import {
@@ -78,7 +79,23 @@ export async function deliverAgentCommandResult(params: {
accountId: opts.replyAccountId ?? opts.accountId,
wantsDelivery: deliver,
});
const deliveryChannel = deliveryPlan.resolvedChannel;
let deliveryChannel = deliveryPlan.resolvedChannel;
const explicitChannelHint = (opts.replyChannel ?? opts.channel)?.trim();
if (deliver && isInternalMessageChannel(deliveryChannel) && !explicitChannelHint) {
try {
const selection = await resolveMessageChannelSelection({ cfg });
deliveryChannel = selection.channel;
} catch {
// Keep the internal channel marker; error handling below reports the failure.
}
}
const effectiveDeliveryPlan =
deliveryChannel === deliveryPlan.resolvedChannel
? deliveryPlan
: {
...deliveryPlan,
resolvedChannel: deliveryChannel,
};
// Channel docking: delivery channels are resolved via plugin registry.
const deliveryPlugin = !isInternalMessageChannel(deliveryChannel)
? getChannelPlugin(normalizeChannelId(deliveryChannel) ?? deliveryChannel)
@@ -89,20 +106,20 @@ export async function deliverAgentCommandResult(params: {
const targetMode =
opts.deliveryTargetMode ??
deliveryPlan.deliveryTargetMode ??
effectiveDeliveryPlan.deliveryTargetMode ??
(opts.to ? "explicit" : "implicit");
const resolvedAccountId = deliveryPlan.resolvedAccountId;
const resolvedAccountId = effectiveDeliveryPlan.resolvedAccountId;
const resolved =
deliver && isDeliveryChannelKnown && deliveryChannel
? resolveAgentOutboundTarget({
cfg,
plan: deliveryPlan,
plan: effectiveDeliveryPlan,
targetMode,
validateExplicitTarget: true,
})
: {
resolvedTarget: null,
resolvedTo: deliveryPlan.resolvedTo,
resolvedTo: effectiveDeliveryPlan.resolvedTo,
targetMode,
};
const resolvedTarget = resolved.resolvedTarget;
@@ -121,7 +138,15 @@ export async function deliverAgentCommandResult(params: {
};
if (deliver) {
if (!isDeliveryChannelKnown) {
if (isInternalMessageChannel(deliveryChannel)) {
const err = new Error(
"delivery channel is required: pass --channel/--reply-channel or use a main session with a previous channel",
);
if (!bestEffortDeliver) {
throw err;
}
logDeliveryError(err);
} else if (!isDeliveryChannelKnown) {
const err = new Error(`Unknown channel: ${deliveryChannel}`);
if (!bestEffortDeliver) {
throw err;