import { t as __exportAll } from "./rolldown-runtime-Cbj13DAv.js";
import { n as resolveFasedAgentAgentDir } from "./agent-paths-BySKyq9I.js";
import { So as assertSecretInputResolved, di as normalizeProviderId, vo as isPrivateNetworkBaseUrl } from "./agent-scope-CLn_Ws3B.js";
import { t as DEFAULT_CONTEXT_TOKENS } from "./defaults-CzImVi0V.js";
import { i as discoverModels, r as discoverAuthStorage } from "./pi-model-discovery-BftA1L--.js";

//#region src/agents/model-alias-lines.ts
function buildModelAliasLines(cfg) {
	const models = cfg?.agents?.defaults?.models ?? {};
	const entries = [];
	for (const [keyRaw, entryRaw] of Object.entries(models)) {
		const model = String(keyRaw ?? "").trim();
		if (!model) continue;
		const alias = String(entryRaw?.alias ?? "").trim();
		if (!alias) continue;
		entries.push({
			alias,
			model
		});
	}
	return entries.toSorted((a, b) => a.alias.localeCompare(b.alias)).map((entry) => `- ${entry.alias}: ${entry.model}`);
}

//#endregion
//#region src/agents/model-compat.ts
function isOpenAiCompletionsModel(model) {
	return model.api === "openai-completions";
}
function isDashScopeCompatibleEndpoint(baseUrl) {
	return baseUrl.includes("dashscope.aliyuncs.com") || baseUrl.includes("dashscope-intl.aliyuncs.com") || baseUrl.includes("dashscope-us.aliyuncs.com");
}
function isNativeOpenAiEndpoint(model, baseUrl) {
	return model.provider === "openai" && baseUrl.includes("api.openai.com");
}
function isAnthropicMessagesModel(model) {
	return model.api === "anthropic-messages";
}
/**
* pi-ai constructs the Anthropic API endpoint as `${baseUrl}/v1/messages`.
* If a user configures `baseUrl` with a trailing `/v1` (e.g. the previously
* recommended format "https://api.anthropic.com/v1"), the resulting URL
* becomes "…/v1/v1/messages" which the Anthropic API rejects with a 404.
*
* Strip a single trailing `/v1` (with optional trailing slash) from the
* baseUrl for anthropic-messages models so users with either format work.
*/
function normalizeAnthropicBaseUrl(baseUrl) {
	return baseUrl.replace(/\/v1\/?$/, "");
}
function normalizeModelCompat(model) {
	const baseUrl = model.baseUrl ?? "";
	if (isAnthropicMessagesModel(model) && baseUrl) {
		const normalised = normalizeAnthropicBaseUrl(baseUrl);
		if (normalised !== baseUrl) return {
			...model,
			baseUrl: normalised
		};
	}
	const isZai = model.provider === "zai" || baseUrl.includes("api.z.ai");
	const isMoonshot = model.provider === "moonshot" || baseUrl.includes("moonshot.ai") || baseUrl.includes("moonshot.cn");
	const isDashScope = model.provider === "dashscope" || isDashScopeCompatibleEndpoint(baseUrl);
	if (!isOpenAiCompletionsModel(model)) return model;
	if (!(isZai || isMoonshot || isDashScope || Boolean(baseUrl) && !isNativeOpenAiEndpoint(model, baseUrl))) return model;
	const compat = model.compat ?? {};
	const nextCompat = {
		...compat,
		supportsDeveloperRole: compat.supportsDeveloperRole ?? false,
		supportsUsageInStreaming: compat.supportsUsageInStreaming ?? (isDashScope || isMoonshot),
		supportsStrictMode: compat.supportsStrictMode ?? false
	};
	if (model.compat && model.compat.supportsDeveloperRole === nextCompat.supportsDeveloperRole && model.compat.supportsUsageInStreaming === nextCompat.supportsUsageInStreaming && model.compat.supportsStrictMode === nextCompat.supportsStrictMode) return model;
	return {
		...model,
		compat: nextCompat
	};
}

//#endregion
//#region src/agents/model-forward-compat.ts
const OPENAI_GPT_55_MODEL_ID = "gpt-5.5";
const OPENAI_GPT_55_PRO_MODEL_ID = "gpt-5.5-pro";
const OPENAI_GPT_54_MODEL_ID = "gpt-5.4";
const OPENAI_GPT_54_PRO_MODEL_ID = "gpt-5.4-pro";
const OPENAI_GPT_54_MINI_MODEL_ID = "gpt-5.4-mini";
const OPENAI_GPT_54_NANO_MODEL_ID = "gpt-5.4-nano";
const OPENAI_GPT_5_CODEX_MODEL_ID = "gpt-5-codex";
const OPENAI_GPT_55_CONTEXT_TOKENS = 272e3;
const OPENAI_GPT_55_PRO_CONTEXT_TOKENS = 1e6;
const OPENAI_GPT_54_CONTEXT_TOKENS = 105e4;
const OPENAI_GPT_54_MINI_CONTEXT_TOKENS = 4e5;
const OPENAI_GPT_54_MAX_TOKENS = 128e3;
const OPENAI_GPT_5_CODEX_CONTEXT_TOKENS = 4e5;
const OPENAI_GPT_55_TEMPLATE_MODEL_IDS = ["gpt-5.4", "gpt-5.2"];
const OPENAI_GPT_55_PRO_TEMPLATE_MODEL_IDS = [
	"gpt-5.4-pro",
	"gpt-5.4",
	"gpt-5.2-pro",
	"gpt-5.2"
];
const OPENAI_GPT_54_TEMPLATE_MODEL_IDS = ["gpt-5.2"];
const OPENAI_GPT_54_PRO_TEMPLATE_MODEL_IDS = ["gpt-5.2-pro", "gpt-5.2"];
const OPENAI_GPT_54_MINI_TEMPLATE_MODEL_IDS = ["gpt-5-mini"];
const OPENAI_GPT_54_NANO_TEMPLATE_MODEL_IDS = ["gpt-5-nano", "gpt-5-mini"];
const OPENAI_GPT_5_CODEX_TEMPLATE_MODEL_IDS = ["gpt-5.4", "gpt-5.2"];
const OPENAI_CODEX_GPT_55_MODEL_ID = "gpt-5.5";
const OPENAI_CODEX_GPT_54_MODEL_ID = "gpt-5.4";
const OPENAI_CODEX_GPT_54_MINI_MODEL_ID = "gpt-5.4-mini";
const OPENAI_CODEX_GPT_55_CONTEXT_TOKENS = 4e5;
const OPENAI_CODEX_GPT_54_CONTEXT_TOKENS = 105e4;
const OPENAI_CODEX_GPT_54_MINI_CONTEXT_TOKENS = 4e5;
const OPENAI_CODEX_GPT_53_CONTEXT_TOKENS = 272e3;
const OPENAI_CODEX_GPT_54_MAX_TOKENS = 128e3;
const OPENAI_CODEX_GPT_54_TEMPLATE_MODEL_IDS = ["gpt-5.3-codex"];
const OPENAI_CODEX_GPT_55_TEMPLATE_MODEL_IDS = ["gpt-5.4", ...OPENAI_CODEX_GPT_54_TEMPLATE_MODEL_IDS];
const OPENAI_CODEX_GPT_53_MODEL_ID = "gpt-5.3-codex";
const OPENAI_CODEX_TEMPLATE_MODEL_IDS = [];
const ANTHROPIC_OPUS_46_MODEL_ID = "claude-opus-4-6";
const ANTHROPIC_OPUS_46_DOT_MODEL_ID = "claude-opus-4.6";
const ANTHROPIC_OPUS_TEMPLATE_MODEL_IDS = ["claude-opus-4-5", "claude-opus-4.5"];
const ANTHROPIC_SONNET_46_MODEL_ID = "claude-sonnet-4-6";
const ANTHROPIC_SONNET_46_DOT_MODEL_ID = "claude-sonnet-4.6";
const ANTHROPIC_SONNET_TEMPLATE_MODEL_IDS = ["claude-sonnet-4-5", "claude-sonnet-4.5"];
const ZAI_GLM5_MODEL_ID = "glm-5";
const ZAI_GLM5_TEMPLATE_MODEL_IDS = ["glm-4.7"];
const GEMINI_3_1_PRO_PREFIX = "gemini-3.1-pro";
const GEMINI_3_1_FLASH_PREFIX = "gemini-3.1-flash";
const GEMINI_3_1_PRO_TEMPLATE_IDS = ["gemini-3-pro-preview"];
const GEMINI_3_1_FLASH_TEMPLATE_IDS = ["gemini-3-flash-preview"];
function cloneFirstTemplateModel(params) {
	const { normalizedProvider, trimmedModelId, templateIds, modelRegistry } = params;
	for (const templateId of [...new Set(templateIds)].filter(Boolean)) {
		const template = modelRegistry.find(normalizedProvider, templateId);
		if (!template) continue;
		return normalizeModelCompat({
			...template,
			id: trimmedModelId,
			name: trimmedModelId,
			...params.patch
		});
	}
}
function buildOpenAIResponsesPatch(params) {
	return {
		api: "openai-responses",
		provider: "openai",
		baseUrl: "https://api.openai.com/v1",
		reasoning: true,
		input: ["text", "image"],
		cost: params.cost ?? {
			input: 0,
			output: 0,
			cacheRead: 0,
			cacheWrite: 0
		},
		contextWindow: params.contextWindow,
		maxTokens: params.maxTokens
	};
}
function resolveOpenAIGptForwardCompatModel(provider, modelId, modelRegistry) {
	const normalizedProvider = normalizeProviderId(provider);
	if (normalizedProvider !== "openai") return;
	const trimmedModelId = modelId.trim();
	const lower = trimmedModelId.toLowerCase();
	let templateIds;
	let patch;
	if (lower === OPENAI_GPT_55_MODEL_ID) {
		templateIds = OPENAI_GPT_55_TEMPLATE_MODEL_IDS;
		patch = buildOpenAIResponsesPatch({
			contextWindow: OPENAI_GPT_55_CONTEXT_TOKENS,
			maxTokens: OPENAI_GPT_54_MAX_TOKENS,
			cost: {
				input: 5,
				output: 30,
				cacheRead: .5,
				cacheWrite: 0
			}
		});
	} else if (lower === OPENAI_GPT_55_PRO_MODEL_ID) {
		templateIds = OPENAI_GPT_55_PRO_TEMPLATE_MODEL_IDS;
		patch = buildOpenAIResponsesPatch({
			contextWindow: OPENAI_GPT_55_PRO_CONTEXT_TOKENS,
			maxTokens: OPENAI_GPT_54_MAX_TOKENS,
			cost: {
				input: 30,
				output: 180,
				cacheRead: 0,
				cacheWrite: 0
			}
		});
	} else if (lower === OPENAI_GPT_54_MODEL_ID) {
		templateIds = OPENAI_GPT_54_TEMPLATE_MODEL_IDS;
		patch = buildOpenAIResponsesPatch({
			contextWindow: OPENAI_GPT_54_CONTEXT_TOKENS,
			maxTokens: OPENAI_GPT_54_MAX_TOKENS,
			cost: {
				input: 2.5,
				output: 15,
				cacheRead: .25,
				cacheWrite: 0
			}
		});
	} else if (lower === OPENAI_GPT_54_PRO_MODEL_ID) {
		templateIds = OPENAI_GPT_54_PRO_TEMPLATE_MODEL_IDS;
		patch = buildOpenAIResponsesPatch({
			contextWindow: OPENAI_GPT_54_CONTEXT_TOKENS,
			maxTokens: OPENAI_GPT_54_MAX_TOKENS,
			cost: {
				input: 30,
				output: 180,
				cacheRead: 0,
				cacheWrite: 0
			}
		});
	} else if (lower === OPENAI_GPT_54_MINI_MODEL_ID) {
		templateIds = OPENAI_GPT_54_MINI_TEMPLATE_MODEL_IDS;
		patch = buildOpenAIResponsesPatch({
			contextWindow: OPENAI_GPT_54_MINI_CONTEXT_TOKENS,
			maxTokens: OPENAI_GPT_54_MAX_TOKENS,
			cost: {
				input: .75,
				output: 4.5,
				cacheRead: .075,
				cacheWrite: 0
			}
		});
	} else if (lower === OPENAI_GPT_54_NANO_MODEL_ID) {
		templateIds = OPENAI_GPT_54_NANO_TEMPLATE_MODEL_IDS;
		patch = buildOpenAIResponsesPatch({
			contextWindow: OPENAI_GPT_54_MINI_CONTEXT_TOKENS,
			maxTokens: OPENAI_GPT_54_MAX_TOKENS,
			cost: {
				input: .2,
				output: 1.25,
				cacheRead: .02,
				cacheWrite: 0
			}
		});
	} else if (lower === OPENAI_GPT_5_CODEX_MODEL_ID) {
		templateIds = OPENAI_GPT_5_CODEX_TEMPLATE_MODEL_IDS;
		patch = buildOpenAIResponsesPatch({
			contextWindow: OPENAI_GPT_5_CODEX_CONTEXT_TOKENS,
			maxTokens: OPENAI_GPT_54_MAX_TOKENS,
			cost: {
				input: 1.25,
				output: 10,
				cacheRead: .125,
				cacheWrite: 0
			}
		});
	} else return;
	return cloneFirstTemplateModel({
		normalizedProvider,
		trimmedModelId,
		templateIds: [...templateIds],
		modelRegistry,
		patch
	}) ?? normalizeModelCompat({
		id: trimmedModelId,
		name: trimmedModelId,
		...patch
	});
}
const CODEX_GPT54_ELIGIBLE_PROVIDERS = new Set(["openai-codex"]);
const CODEX_GPT53_ELIGIBLE_PROVIDERS = new Set(["openai-codex", "github-copilot"]);
function resolveOpenAICodexForwardCompatModel(provider, modelId, modelRegistry) {
	const normalizedProvider = normalizeProviderId(provider);
	const trimmedModelId = modelId.trim();
	const lower = trimmedModelId.toLowerCase();
	let canonicalModelId = trimmedModelId;
	let templateIds;
	let eligibleProviders;
	let patch;
	if (lower === OPENAI_CODEX_GPT_55_MODEL_ID) {
		templateIds = OPENAI_CODEX_GPT_55_TEMPLATE_MODEL_IDS;
		eligibleProviders = CODEX_GPT54_ELIGIBLE_PROVIDERS;
		patch = {
			contextWindow: OPENAI_CODEX_GPT_55_CONTEXT_TOKENS,
			maxTokens: OPENAI_CODEX_GPT_54_MAX_TOKENS
		};
	} else if (lower === OPENAI_CODEX_GPT_54_MODEL_ID) {
		canonicalModelId = OPENAI_CODEX_GPT_54_MODEL_ID;
		templateIds = OPENAI_CODEX_GPT_54_TEMPLATE_MODEL_IDS;
		eligibleProviders = CODEX_GPT54_ELIGIBLE_PROVIDERS;
		patch = {
			contextWindow: OPENAI_CODEX_GPT_54_CONTEXT_TOKENS,
			maxTokens: OPENAI_CODEX_GPT_54_MAX_TOKENS,
			cost: {
				input: 2.5,
				output: 15,
				cacheRead: .25,
				cacheWrite: 0
			}
		};
	} else if (lower === OPENAI_CODEX_GPT_54_MINI_MODEL_ID) {
		templateIds = [OPENAI_CODEX_GPT_54_MODEL_ID, ...OPENAI_CODEX_GPT_54_TEMPLATE_MODEL_IDS];
		eligibleProviders = CODEX_GPT54_ELIGIBLE_PROVIDERS;
		patch = {
			contextWindow: OPENAI_CODEX_GPT_54_MINI_CONTEXT_TOKENS,
			maxTokens: OPENAI_CODEX_GPT_54_MAX_TOKENS,
			cost: {
				input: .75,
				output: 4.5,
				cacheRead: .075,
				cacheWrite: 0
			}
		};
	} else if (lower === OPENAI_CODEX_GPT_53_MODEL_ID) {
		templateIds = OPENAI_CODEX_TEMPLATE_MODEL_IDS;
		eligibleProviders = CODEX_GPT53_ELIGIBLE_PROVIDERS;
		patch = {
			contextWindow: OPENAI_CODEX_GPT_53_CONTEXT_TOKENS,
			maxTokens: OPENAI_CODEX_GPT_54_MAX_TOKENS
		};
	} else return;
	if (!eligibleProviders.has(normalizedProvider)) return;
	for (const templateId of templateIds) {
		const template = modelRegistry.find(normalizedProvider, templateId);
		if (!template) continue;
		return normalizeModelCompat({
			...template,
			id: canonicalModelId,
			name: canonicalModelId,
			...patch
		});
	}
	return normalizeModelCompat({
		id: canonicalModelId,
		name: canonicalModelId,
		api: "openai-codex-responses",
		provider: normalizedProvider,
		baseUrl: "https://chatgpt.com/backend-api",
		reasoning: true,
		input: ["text", "image"],
		cost: {
			input: 0,
			output: 0,
			cacheRead: 0,
			cacheWrite: 0
		},
		contextWindow: patch?.contextWindow ?? DEFAULT_CONTEXT_TOKENS,
		maxTokens: patch?.maxTokens ?? DEFAULT_CONTEXT_TOKENS,
		...patch?.cost ? { cost: patch.cost } : {}
	});
}
function resolveAnthropic46ForwardCompatModel(params) {
	const { provider, modelId, modelRegistry, dashModelId, dotModelId } = params;
	const normalizedProvider = normalizeProviderId(provider);
	if (normalizedProvider !== "anthropic") return;
	const trimmedModelId = modelId.trim();
	const lower = trimmedModelId.toLowerCase();
	if (!(lower === dashModelId || lower === dotModelId || lower.startsWith(`${dashModelId}-`) || lower.startsWith(`${dotModelId}-`))) return;
	const templateIds = [];
	if (lower.startsWith(dashModelId)) templateIds.push(lower.replace(dashModelId, params.dashTemplateId));
	if (lower.startsWith(dotModelId)) templateIds.push(lower.replace(dotModelId, params.dotTemplateId));
	templateIds.push(...params.fallbackTemplateIds);
	return cloneFirstTemplateModel({
		normalizedProvider,
		trimmedModelId,
		templateIds,
		modelRegistry
	});
}
function resolveAnthropicOpus46ForwardCompatModel(provider, modelId, modelRegistry) {
	return resolveAnthropic46ForwardCompatModel({
		provider,
		modelId,
		modelRegistry,
		dashModelId: ANTHROPIC_OPUS_46_MODEL_ID,
		dotModelId: ANTHROPIC_OPUS_46_DOT_MODEL_ID,
		dashTemplateId: "claude-opus-4-5",
		dotTemplateId: "claude-opus-4.5",
		fallbackTemplateIds: ANTHROPIC_OPUS_TEMPLATE_MODEL_IDS
	});
}
function resolveAnthropicSonnet46ForwardCompatModel(provider, modelId, modelRegistry) {
	return resolveAnthropic46ForwardCompatModel({
		provider,
		modelId,
		modelRegistry,
		dashModelId: ANTHROPIC_SONNET_46_MODEL_ID,
		dotModelId: ANTHROPIC_SONNET_46_DOT_MODEL_ID,
		dashTemplateId: "claude-sonnet-4-5",
		dotTemplateId: "claude-sonnet-4.5",
		fallbackTemplateIds: ANTHROPIC_SONNET_TEMPLATE_MODEL_IDS
	});
}
function resolveGoogleGeminiCli31ForwardCompatModel(provider, modelId, modelRegistry) {
	if (normalizeProviderId(provider) !== "google-gemini-cli") return;
	const trimmed = modelId.trim();
	const lower = trimmed.toLowerCase();
	let templateIds;
	if (lower.startsWith(GEMINI_3_1_PRO_PREFIX)) templateIds = GEMINI_3_1_PRO_TEMPLATE_IDS;
	else if (lower.startsWith(GEMINI_3_1_FLASH_PREFIX)) templateIds = GEMINI_3_1_FLASH_TEMPLATE_IDS;
	else return;
	return cloneFirstTemplateModel({
		normalizedProvider: "google-gemini-cli",
		trimmedModelId: trimmed,
		templateIds: [...templateIds],
		modelRegistry,
		patch: { reasoning: true }
	});
}
function resolveZaiGlm5ForwardCompatModel(provider, modelId, modelRegistry) {
	if (normalizeProviderId(provider) !== "zai") return;
	const trimmed = modelId.trim();
	const lower = trimmed.toLowerCase();
	if (lower !== ZAI_GLM5_MODEL_ID && !lower.startsWith(`${ZAI_GLM5_MODEL_ID}-`)) return;
	for (const templateId of ZAI_GLM5_TEMPLATE_MODEL_IDS) {
		const template = modelRegistry.find("zai", templateId);
		if (!template) continue;
		return normalizeModelCompat({
			...template,
			id: trimmed,
			name: trimmed,
			reasoning: true
		});
	}
	return normalizeModelCompat({
		id: trimmed,
		name: trimmed,
		api: "openai-completions",
		provider: "zai",
		reasoning: true,
		input: ["text"],
		cost: {
			input: 0,
			output: 0,
			cacheRead: 0,
			cacheWrite: 0
		},
		contextWindow: DEFAULT_CONTEXT_TOKENS,
		maxTokens: DEFAULT_CONTEXT_TOKENS
	});
}
function resolveForwardCompatModel(provider, modelId, modelRegistry) {
	return resolveOpenAIGptForwardCompatModel(provider, modelId, modelRegistry) ?? resolveOpenAICodexForwardCompatModel(provider, modelId, modelRegistry) ?? resolveAnthropicOpus46ForwardCompatModel(provider, modelId, modelRegistry) ?? resolveAnthropicSonnet46ForwardCompatModel(provider, modelId, modelRegistry) ?? resolveZaiGlm5ForwardCompatModel(provider, modelId, modelRegistry) ?? resolveGoogleGeminiCli31ForwardCompatModel(provider, modelId, modelRegistry);
}

//#endregion
//#region src/agents/provider-request-config.ts
function sanitizeConfiguredRequestString(value, path) {
	if (value == null) return;
	if (typeof value !== "string") {
		assertSecretInputResolved({
			value,
			path
		});
		return;
	}
	const trimmed = value.trim();
	return trimmed ? trimmed : void 0;
}
function sanitizeConfiguredProviderRequest(request) {
	if (!request || typeof request !== "object" || Array.isArray(request)) return;
	let headers;
	if (request.headers && typeof request.headers === "object" && !Array.isArray(request.headers)) {
		const nextHeaders = {};
		for (const [key, value] of Object.entries(request.headers)) {
			const sanitized = sanitizeConfiguredRequestString(value, `request.headers.${key}`);
			if (sanitized) nextHeaders[key] = sanitized;
		}
		if (Object.keys(nextHeaders).length > 0) headers = nextHeaders;
	}
	let auth;
	const rawAuth = request.auth;
	if (rawAuth && typeof rawAuth === "object" && !Array.isArray(rawAuth)) {
		if (rawAuth.mode === "provider-default") auth = { mode: "provider-default" };
		else if (rawAuth.mode === "authorization-bearer") {
			const token = sanitizeConfiguredRequestString(rawAuth.token, "request.auth.token");
			if (token) auth = {
				mode: "authorization-bearer",
				token
			};
		} else if (rawAuth.mode === "header") {
			const headerName = sanitizeConfiguredRequestString(rawAuth.headerName, "request.auth.headerName");
			const value = sanitizeConfiguredRequestString(rawAuth.value, "request.auth.value");
			const prefix = sanitizeConfiguredRequestString(rawAuth.prefix, "request.auth.prefix");
			if (headerName && value) auth = {
				mode: "header",
				headerName,
				value,
				...prefix ? { prefix } : {}
			};
		}
	}
	const sanitizeTls = (tls, pathPrefix) => {
		if (!tls || typeof tls !== "object" || Array.isArray(tls)) return;
		const rawTls = tls;
		const next = {};
		const ca = sanitizeConfiguredRequestString(rawTls.ca, `${pathPrefix}.ca`);
		const cert = sanitizeConfiguredRequestString(rawTls.cert, `${pathPrefix}.cert`);
		const key = sanitizeConfiguredRequestString(rawTls.key, `${pathPrefix}.key`);
		const passphrase = sanitizeConfiguredRequestString(rawTls.passphrase, `${pathPrefix}.passphrase`);
		const serverName = sanitizeConfiguredRequestString(rawTls.serverName, `${pathPrefix}.serverName`);
		if (ca) next.ca = ca;
		if (cert) next.cert = cert;
		if (key) next.key = key;
		if (passphrase) next.passphrase = passphrase;
		if (serverName) next.serverName = serverName;
		if (rawTls.insecureSkipVerify === true) next.insecureSkipVerify = true;
		else if (rawTls.insecureSkipVerify === false) next.insecureSkipVerify = false;
		return Object.keys(next).length > 0 ? next : void 0;
	};
	let proxy;
	const rawProxy = request.proxy;
	if (rawProxy && typeof rawProxy === "object" && !Array.isArray(rawProxy)) {
		const tls = sanitizeTls(rawProxy.tls, "request.proxy.tls");
		if (rawProxy.mode === "env-proxy") proxy = {
			mode: "env-proxy",
			...tls ? { tls } : {}
		};
		else if (rawProxy.mode === "explicit-proxy") {
			const url = sanitizeConfiguredRequestString(rawProxy.url, "request.proxy.url");
			if (url) proxy = {
				mode: "explicit-proxy",
				url,
				...tls ? { tls } : {}
			};
		}
	}
	const tls = sanitizeTls(request.tls, "request.tls");
	if (!headers && !auth && !proxy && !tls) return;
	return {
		...headers ? { headers } : {},
		...auth ? { auth } : {},
		...proxy ? { proxy } : {},
		...tls ? { tls } : {}
	};
}
function sanitizeConfiguredModelProviderRequest(request) {
	const transport = sanitizeConfiguredProviderRequest(request);
	const allowPrivateNetwork = request?.allowPrivateNetwork === true ? true : request?.allowPrivateNetwork === false ? false : void 0;
	if (!transport && allowPrivateNetwork === void 0) return;
	return {
		...transport,
		...allowPrivateNetwork !== void 0 ? { allowPrivateNetwork } : {}
	};
}
const MODEL_PROVIDER_REQUEST_TRANSPORT_SYMBOL = Symbol.for("fased.modelProviderRequestTransport");
function attachModelProviderRequestTransport(model, request) {
	if (!request) return model;
	const next = { ...model };
	next[MODEL_PROVIDER_REQUEST_TRANSPORT_SYMBOL] = request;
	return next;
}

//#endregion
//#region src/agents/pi-embedded-runner/model.ts
var model_exports = /* @__PURE__ */ __exportAll({
	buildInlineProviderModels: () => buildInlineProviderModels,
	buildModelAliasLines: () => buildModelAliasLines,
	resolveModel: () => resolveModel,
	resolveModelWithRegistry: () => resolveModelWithRegistry
});
const OPENAI_CODEX_PROVIDER_ID = "openai-codex";
const OPENAI_CODEX_SUPPORTED_MODEL_IDS = new Set([
	"gpt-5.5",
	"gpt-5.4",
	"gpt-5.4-mini",
	"gpt-5.3-codex"
]);
function buildInlineProviderModels(providers) {
	return Object.entries(providers).flatMap(([providerId, entry]) => {
		const trimmed = providerId.trim();
		if (!trimmed) return [];
		return (entry?.models ?? []).map((model) => ({
			...model,
			provider: trimmed,
			baseUrl: entry?.baseUrl,
			api: model.api ?? entry?.api,
			...entry?.request ? { request: entry.request } : {}
		}));
	});
}
function resolveConfiguredProvider(params) {
	const providers = params.cfg?.models?.providers ?? {};
	const direct = providers[params.provider];
	if (direct) return direct;
	const normalizedProvider = normalizeProviderId(params.provider);
	return Object.entries(providers).find(([id]) => normalizeProviderId(id) === normalizedProvider)?.[1];
}
function attachConfiguredProviderRequest(model, cfg) {
	const inlineRequest = model.request;
	const configuredProvider = resolveConfiguredProvider({
		provider: model.provider,
		cfg
	});
	const configuredRequest = inlineRequest ?? configuredProvider?.request;
	const request = configuredRequest && typeof configuredRequest === "object" ? sanitizeConfiguredModelProviderRequest(configuredRequest) : void 0;
	const baseUrl = model.baseUrl ?? configuredProvider?.baseUrl;
	const privateNetworkFallback = !request && isPrivateNetworkBaseUrl(baseUrl) ? { allowPrivateNetwork: false } : void 0;
	return attachModelProviderRequestTransport(model, request ?? privateNetworkFallback);
}
function resolveModel(provider, modelId, agentDir, cfg) {
	const resolvedAgentDir = agentDir ?? resolveFasedAgentAgentDir();
	const authStorage = discoverAuthStorage(resolvedAgentDir);
	const modelRegistry = discoverModels(authStorage, resolvedAgentDir);
	const normalizedProvider = normalizeProviderId(provider);
	const runtimeModelId = modelId.trim();
	if (normalizedProvider === OPENAI_CODEX_PROVIDER_ID && !OPENAI_CODEX_SUPPORTED_MODEL_IDS.has(runtimeModelId.toLowerCase())) return {
		error: `Unsupported OpenAI-Codex sign-in model: ${runtimeModelId}`,
		authStorage,
		modelRegistry
	};
	const model = modelRegistry.find(provider, runtimeModelId);
	if (!model) {
		const providers = cfg?.models?.providers ?? {};
		const inlineMatch = buildInlineProviderModels(providers).find((entry) => normalizeProviderId(entry.provider) === normalizedProvider && entry.id === runtimeModelId);
		if (inlineMatch) return {
			model: attachConfiguredProviderRequest(normalizeModelCompat(inlineMatch), cfg),
			authStorage,
			modelRegistry
		};
		const forwardCompat = resolveForwardCompatModel(provider, runtimeModelId, modelRegistry);
		if (forwardCompat) return {
			model: forwardCompat,
			authStorage,
			modelRegistry
		};
		if (normalizedProvider === "openrouter") return {
			model: attachConfiguredProviderRequest(normalizeModelCompat({
				id: runtimeModelId,
				name: runtimeModelId,
				api: "openai-completions",
				provider,
				baseUrl: "https://openrouter.ai/api/v1",
				reasoning: false,
				input: ["text"],
				cost: {
					input: 0,
					output: 0,
					cacheRead: 0,
					cacheWrite: 0
				},
				contextWindow: DEFAULT_CONTEXT_TOKENS,
				maxTokens: 8192
			}), cfg),
			authStorage,
			modelRegistry
		};
		const providerCfg = providers[provider];
		if (providerCfg || modelId.startsWith("mock-")) return {
			model: attachConfiguredProviderRequest(normalizeModelCompat({
				id: runtimeModelId,
				name: runtimeModelId,
				api: providerCfg?.api ?? "openai-responses",
				provider,
				baseUrl: providerCfg?.baseUrl,
				reasoning: false,
				input: ["text"],
				cost: {
					input: 0,
					output: 0,
					cacheRead: 0,
					cacheWrite: 0
				},
				contextWindow: providerCfg?.models?.[0]?.contextWindow ?? DEFAULT_CONTEXT_TOKENS,
				maxTokens: providerCfg?.models?.[0]?.maxTokens ?? DEFAULT_CONTEXT_TOKENS
			}), cfg),
			authStorage,
			modelRegistry
		};
		return {
			error: buildUnknownModelError(provider, modelId),
			authStorage,
			modelRegistry
		};
	}
	return {
		model: attachConfiguredProviderRequest(normalizeModelCompat(model), cfg),
		authStorage,
		modelRegistry
	};
}
function resolveModelWithRegistry(params) {
	const normalizedProvider = normalizeProviderId(params.provider);
	const runtimeModelId = params.modelId.trim();
	const model = params.modelRegistry.find(params.provider, runtimeModelId);
	if (model) return attachConfiguredProviderRequest(model, params.cfg);
	const inlineMatch = buildInlineProviderModels(params.cfg?.models?.providers ?? {}).find((entry) => normalizeProviderId(entry.provider) === normalizedProvider && entry.id === runtimeModelId);
	return inlineMatch ? attachConfiguredProviderRequest(normalizeModelCompat(inlineMatch), params.cfg) : null;
}
/**
* Build a more helpful error when the model is not found.
*
* Local providers like vLLM need a dummy API key to be registered. This detects
* known providers that require opt-in auth and adds a hint.
*
* See: https://github.com/fased-ai/fased/issues/17328
*/
const LOCAL_PROVIDER_HINTS = {
	ollama: "Ollama requires authentication to be registered as a provider. Set OLLAMA_API_KEY (any value works) or run \"fased configure\". See: https://docs.fased.ai/gateway/local-models",
	vllm: "vLLM requires authentication to be registered as a provider. Set VLLM_API_KEY (any value works) or run \"fased configure\". See: https://docs.fased.ai/providers/vllm",
	lmstudio: "LM Studio must be running and registered as a provider. Start LM Studio on localhost:1234 or run \"fased configure\". See: https://docs.fased.ai/providers/lmstudio"
};
function buildUnknownModelError(provider, modelId) {
	const base = `Unknown model: ${provider}/${modelId}`;
	const hint = LOCAL_PROVIDER_HINTS[provider.toLowerCase()];
	return hint ? `${base}. ${hint}` : base;
}

//#endregion
export { buildModelAliasLines as i, resolveModel as n, resolveForwardCompatModel as r, model_exports as t };