import { t as GatewayDrainingError } from "./gateway-work-admission-Cq7eCT39.js";
import { f as isAgentRunRestartAbortReason, h as resolveAgentRunErrorLifecycleFields, i as AGENT_RUN_RESTART_ABORT_STOP_REASON, p as isAgentRunSupersededAbortReason } from "./run-termination-Bf1l-R4x.js";
import { t as CommandLaneClearedError } from "./command-queue-CrKnQVag.js";
import { r as isFallbackSummaryError } from "./model-fallback-attempt-z6kpMmx4.js";
//#region src/auto-reply/reply/reply-operation-abort.ts
function buildRestartLifecycleReplyText() {
	return "⚠️ Gateway is restarting. Please wait a few seconds and try again.";
}
function isReplyOperationUserAbort(replyOperation) {
	if (replyOperation?.result?.kind === "aborted" && replyOperation.result.code === "aborted_by_user") return true;
	const abortSignal = replyOperation?.abortSignal;
	return abortSignal?.aborted === true && !isAgentRunRestartAbortReason(abortSignal.reason) && !isAgentRunSupersededAbortReason(abortSignal.reason);
}
function isReplyOperationRestartAbort(replyOperation) {
	if (replyOperation?.result?.kind === "aborted" && replyOperation.result.code === "aborted_for_restart") return true;
	const abortSignal = replyOperation?.abortSignal;
	return abortSignal?.aborted === true && isAgentRunRestartAbortReason(abortSignal.reason);
}
function resolveReplyOperationTerminationFields(error, signal, replyOperation) {
	return {
		...resolveAgentRunErrorLifecycleFields(error, signal),
		...isReplyOperationRestartAbort(replyOperation) ? {
			aborted: true,
			stopReason: AGENT_RUN_RESTART_ABORT_STOP_REASON
		} : {}
	};
}
function isReplyOperationSuperseded(replyOperation) {
	if (replyOperation?.result?.kind === "aborted" && replyOperation.result.code === "aborted_for_supersession") return true;
	const abortSignal = replyOperation?.abortSignal;
	return abortSignal?.aborted === true && isAgentRunSupersededAbortReason(abortSignal.reason);
}
function resolveReplyOperationAbortReason(replyOperation) {
	return isReplyOperationRestartAbort(replyOperation) ? "restart" : isReplyOperationSuperseded(replyOperation) ? "superseded" : isReplyOperationUserAbort(replyOperation) ? "user" : void 0;
}
function resolveRestartLifecycleError(error) {
	const pending = [error];
	const seen = /* @__PURE__ */ new Set();
	for (const candidate of pending) {
		if (!candidate || seen.has(candidate)) continue;
		seen.add(candidate);
		if (candidate instanceof GatewayDrainingError || candidate instanceof CommandLaneClearedError) return candidate;
		if (isFallbackSummaryError(candidate)) pending.push(...candidate.attempts.map((attempt) => attempt.error));
		if (candidate instanceof Error && "cause" in candidate) pending.push(candidate.cause);
	}
}
//#endregion
//#region src/auto-reply/reply/reply-operation-agent-turn-state.ts
const agentTurns = /* @__PURE__ */ new WeakMap();
function recordReplyOperationAgentTurn(state, status, owner) {
	if (state) agentTurns.set(state, {
		status,
		owner
	});
}
function resolveReplyOperationAgentTurn(state) {
	if (!state) return;
	const turn = agentTurns.get(state);
	return isReplyOperationSuperseded(turn?.owner) ? "superseded" : turn?.status;
}
//#endregion
export { resolveReplyOperationAbortReason as a, isReplyOperationSuperseded as i, resolveReplyOperationAgentTurn as n, resolveReplyOperationTerminationFields as o, buildRestartLifecycleReplyText as r, resolveRestartLifecycleError as s, recordReplyOperationAgentTurn as t };
