import { normalizePatchAgentImageGenerationModel, normalizePatchAgentModel, normalizePatchTypedModels } from "../../lib/agent-model.js";
import { isMaskedSecretPatchValue } from "../../lib/mask-secret-length.js";
//#region src/gateway/hono/routes/config-patch/agents.ts
function applyAgentsPatch(config, body) {
	if (!body.agents?.defaults) return;
	if (!config.agents) config.agents = { defaults: {
		workspace: "~/.xopc/workspace",
		model: { primary: "anthropic/claude-sonnet-4-5" },
		maxTokens: 8192,
		temperature: .7,
		maxToolIterations: 20,
		maxRequestsPerTurn: 50,
		maxToolFailuresPerTurn: 3,
		thinkingDefault: "medium",
		reasoningDefault: "stream",
		verboseDefault: "full"
	} };
	if (!config.agents.defaults) config.agents.defaults = {};
	if (body.agents.defaults.model !== void 0) {
		const v = body.agents.defaults.model;
		if (v === null) delete config.agents.defaults.model;
		else {
			const normalized = normalizePatchAgentModel(v);
			if (normalized === void 0) delete config.agents.defaults.model;
			else config.agents.defaults.model = normalized;
		}
	}
	if (body.agents.defaults.maxTokens !== void 0) config.agents.defaults.maxTokens = body.agents.defaults.maxTokens;
	if (body.agents.defaults.temperature !== void 0) config.agents.defaults.temperature = body.agents.defaults.temperature;
	if (body.agents.defaults.maxToolIterations !== void 0) config.agents.defaults.maxToolIterations = body.agents.defaults.maxToolIterations;
	if (body.agents.defaults.workspace !== void 0) config.agents.defaults.workspace = body.agents.defaults.workspace;
	if (body.agents.defaults.thinkingDefault !== void 0) config.agents.defaults.thinkingDefault = body.agents.defaults.thinkingDefault;
	if (body.agents.defaults.reasoningDefault !== void 0) config.agents.defaults.reasoningDefault = body.agents.defaults.reasoningDefault;
	if (body.agents.defaults.verboseDefault !== void 0) config.agents.defaults.verboseDefault = body.agents.defaults.verboseDefault;
	if (body.agents.defaults.imageModel !== void 0) {
		const v = body.agents.defaults.imageModel;
		if (v === null) delete config.agents.defaults.imageModel;
		else {
			const normalized = normalizePatchAgentModel(v);
			if (normalized === void 0) delete config.agents.defaults.imageModel;
			else config.agents.defaults.imageModel = normalized;
		}
	}
	if (body.agents.defaults.imageGenerationModel !== void 0) {
		const v = body.agents.defaults.imageGenerationModel;
		if (v === null) delete config.agents.defaults.imageGenerationModel;
		else {
			const normalized = normalizePatchAgentImageGenerationModel(v);
			if (normalized === void 0) delete config.agents.defaults.imageGenerationModel;
			else config.agents.defaults.imageGenerationModel = normalized;
		}
	}
	if (body.agents.defaults.mediaMaxMb !== void 0) {
		const v = body.agents.defaults.mediaMaxMb;
		if (v === null) delete config.agents.defaults.mediaMaxMb;
		else {
			const n = typeof v === "number" ? v : Number(v);
			if (!Number.isNaN(n) && n > 0) config.agents.defaults.mediaMaxMb = n;
		}
	}
	if (body.agents.defaults.browser !== void 0) {
		const b = body.agents.defaults.browser;
		if (b === null) delete config.agents.defaults.browser;
		else if (typeof b === "object" && !Array.isArray(b)) {
			const br = b;
			if (!config.agents.defaults.browser) config.agents.defaults.browser = {
				enabled: false,
				headless: false
			};
			const target = config.agents.defaults.browser;
			if (br.enabled !== void 0) if (br.enabled === null) delete target.enabled;
			else target.enabled = Boolean(br.enabled);
			if (br.headless !== void 0) if (br.headless === null) delete target.headless;
			else target.headless = Boolean(br.headless);
			if (br.allowPrivateUrls !== void 0) if (br.allowPrivateUrls === null) delete target.allowPrivateUrls;
			else target.allowPrivateUrls = Boolean(br.allowPrivateUrls);
			if (br.commandTimeout !== void 0) {
				if (br.commandTimeout === null) delete target.commandTimeout;
				else if (typeof br.commandTimeout === "number" && Number.isFinite(br.commandTimeout)) {
					const n = Math.floor(br.commandTimeout);
					if (n >= 5 && n <= 900) target.commandTimeout = n;
				}
			}
			if (br.backend !== void 0) {
				if (br.backend === null || br.backend === "" || br.backend === "extension") delete target.backend;
				else if (br.backend === "local" || br.backend === "cdp" || br.backend === "cloud" || br.backend === "cloakbrowser") target.backend = br.backend;
			}
			if (br.cloudProvider !== void 0) {
				if (br.cloudProvider === null || br.cloudProvider === "") delete target.cloudProvider;
				else if (br.cloudProvider === "browserbase" || br.cloudProvider === "browser-use") target.cloudProvider = br.cloudProvider;
			}
			if (br.cloud !== void 0) {
				if (br.cloud === null) delete target.cloud;
				else if (typeof br.cloud === "object" && !Array.isArray(br.cloud)) {
					const cloudPatch = br.cloud;
					const cloudTarget = { ...target.cloud && typeof target.cloud === "object" && !Array.isArray(target.cloud) ? target.cloud : {} };
					if (cloudPatch.apiKey !== void 0) {
						if (cloudPatch.apiKey === null || cloudPatch.apiKey === "") delete cloudTarget.apiKey;
						else if (isMaskedSecretPatchValue(String(cloudPatch.apiKey))) {} else if (typeof cloudPatch.apiKey === "string") cloudTarget.apiKey = cloudPatch.apiKey.trim();
					}
					for (const key of ["projectId", "region"]) {
						const value = cloudPatch[key];
						if (value === null || value === "") delete cloudTarget[key];
						else if (typeof value === "string" && value.trim()) cloudTarget[key] = value.trim();
					}
					if (Object.keys(cloudTarget).length > 0) target.cloud = cloudTarget;
					else delete target.cloud;
				}
			}
			if (br.cdpUrl !== void 0) {
				if (br.cdpUrl === null || br.cdpUrl === "") delete target.cdpUrl;
				else if (typeof br.cdpUrl === "string") target.cdpUrl = br.cdpUrl.trim();
			}
			if (br.extension !== void 0) {
				if (br.extension === null) delete target.extension;
				else if (typeof br.extension === "object" && !Array.isArray(br.extension)) {
					const ext = br.extension;
					const extTarget = {};
					if (ext.port === null || ext.port === "") {} else if (typeof ext.port === "number" && ext.port >= 1024 && ext.port <= 65535) extTarget.port = Math.floor(ext.port);
					if (ext.host === null || ext.host === "") {} else if (typeof ext.host === "string" && ext.host.trim()) extTarget.host = ext.host.trim();
					if (ext.connectionTimeout === null || ext.connectionTimeout === "") {} else if (typeof ext.connectionTimeout === "number" && Number.isFinite(ext.connectionTimeout) && ext.connectionTimeout >= 1e3) extTarget.connectionTimeout = Math.floor(ext.connectionTimeout);
					if (Object.keys(extTarget).length > 0) target.extension = extTarget;
					else delete target.extension;
				}
			}
			if (br.cloakbrowser !== void 0) {
				if (br.cloakbrowser === null) delete target.cloakbrowser;
				else if (typeof br.cloakbrowser === "object" && !Array.isArray(br.cloakbrowser)) {
					const cloakbrowserPatch = br.cloakbrowser;
					const cloakbrowserTarget = {};
					if (typeof cloakbrowserPatch.keepOpen === "boolean") cloakbrowserTarget.keepOpen = cloakbrowserPatch.keepOpen;
					if (typeof cloakbrowserPatch.temporaryProfile === "boolean") cloakbrowserTarget.temporaryProfile = cloakbrowserPatch.temporaryProfile;
					for (const key of [
						"cacheDir",
						"binaryPath",
						"timezone",
						"locale",
						"webrtcIp",
						"fingerprintPlatform"
					]) {
						const value = cloakbrowserPatch[key];
						if (typeof value === "string" && value.trim()) cloakbrowserTarget[key] = value.trim();
					}
					if (Array.isArray(cloakbrowserPatch.extraArgs) && cloakbrowserPatch.extraArgs.every((value) => typeof value === "string")) cloakbrowserTarget.extraArgs = cloakbrowserPatch.extraArgs;
					if (Object.keys(cloakbrowserTarget).length > 0) target.cloakbrowser = cloakbrowserTarget;
					else delete target.cloakbrowser;
				}
			}
			if (br.humanize !== void 0) if (br.humanize === null) delete target.humanize;
			else target.humanize = Boolean(br.humanize);
			if (br.humanPreset !== void 0) {
				if (br.humanPreset === null) delete target.humanPreset;
				else if (br.humanPreset === "default" || br.humanPreset === "careful") target.humanPreset = br.humanPreset;
			}
			if (br.dialogPolicy !== void 0) {
				if (br.dialogPolicy === null) delete target.dialogPolicy;
				else if (br.dialogPolicy === "must_respond" || br.dialogPolicy === "auto_dismiss" || br.dialogPolicy === "auto_accept") target.dialogPolicy = br.dialogPolicy;
			}
			if (br.dialogTimeoutSeconds !== void 0) {
				if (br.dialogTimeoutSeconds === null) delete target.dialogTimeoutSeconds;
				else if (typeof br.dialogTimeoutSeconds === "number" && Number.isFinite(br.dialogTimeoutSeconds)) {
					const n = Math.floor(br.dialogTimeoutSeconds);
					if (n >= 1 && n <= 86400) target.dialogTimeoutSeconds = n;
				}
			}
		}
	}
	const dPatch = body.agents.defaults;
	const def = config.agents.defaults;
	if (dPatch.maxTaskDurationMs !== void 0) {
		const v = dPatch.maxTaskDurationMs;
		if (v === null) delete def.maxTaskDurationMs;
		else if (typeof v === "number" && Number.isFinite(v)) {
			const ms = Math.floor(v);
			if (ms >= 6e4 && ms <= 144e5) def.maxTaskDurationMs = ms;
		}
	}
	if (dPatch.maxRequestsPerTurn !== void 0) {
		const v = dPatch.maxRequestsPerTurn;
		if (v === null) delete def.maxRequestsPerTurn;
		else if (typeof v === "number" && Number.isFinite(v)) {
			const n = Math.floor(v);
			if (n >= 10 && n <= 200) def.maxRequestsPerTurn = n;
		}
	}
	if (dPatch.maxToolFailuresPerTurn !== void 0) {
		const v = dPatch.maxToolFailuresPerTurn;
		if (v === null) delete def.maxToolFailuresPerTurn;
		else if (typeof v === "number" && Number.isFinite(v)) {
			const n = Math.floor(v);
			if (n >= 1 && n <= 20) def.maxToolFailuresPerTurn = n;
		}
	}
	if (dPatch.compaction !== void 0) {
		const c = dPatch.compaction;
		if (c === null) delete def.compaction;
		else if (typeof c === "object" && !Array.isArray(c)) {
			const p = c;
			if (!def.compaction || typeof def.compaction !== "object") def.compaction = {};
			const t = def.compaction;
			if (p.enabled !== void 0) t.enabled = Boolean(p.enabled);
			if (p.mode === "default" || p.mode === "safeguard") t.mode = p.mode;
			if (typeof p.reserveTokens === "number" && Number.isFinite(p.reserveTokens)) t.reserveTokens = Math.floor(p.reserveTokens);
			if (typeof p.triggerThreshold === "number" && Number.isFinite(p.triggerThreshold)) {
				const x = p.triggerThreshold;
				if (x >= .5 && x <= .95) t.triggerThreshold = x;
			}
			if (typeof p.minMessagesBeforeCompact === "number" && Number.isFinite(p.minMessagesBeforeCompact)) t.minMessagesBeforeCompact = Math.floor(p.minMessagesBeforeCompact);
			if (typeof p.keepRecentMessages === "number" && Number.isFinite(p.keepRecentMessages)) t.keepRecentMessages = Math.floor(p.keepRecentMessages);
			if (typeof p.evictionWindow === "number" && Number.isFinite(p.evictionWindow)) {
				const x = p.evictionWindow;
				if (x >= .1 && x <= .5) t.evictionWindow = x;
			}
			if (typeof p.retentionWindow === "number" && Number.isFinite(p.retentionWindow)) {
				const n = Math.floor(p.retentionWindow);
				if (n >= 3 && n <= 20) t.retentionWindow = n;
			}
		}
	}
	if (dPatch.pruning !== void 0) {
		const c = dPatch.pruning;
		if (c === null) delete def.pruning;
		else if (typeof c === "object" && !Array.isArray(c)) {
			const p = c;
			if (!def.pruning || typeof def.pruning !== "object") def.pruning = {};
			const t = def.pruning;
			if (p.enabled !== void 0) t.enabled = Boolean(p.enabled);
			if (typeof p.maxToolResultChars === "number" && Number.isFinite(p.maxToolResultChars)) t.maxToolResultChars = Math.floor(p.maxToolResultChars);
			if (typeof p.headKeepRatio === "number" && Number.isFinite(p.headKeepRatio)) t.headKeepRatio = p.headKeepRatio;
			if (typeof p.tailKeepRatio === "number" && Number.isFinite(p.tailKeepRatio)) t.tailKeepRatio = p.tailKeepRatio;
		}
	}
	if (dPatch.memory !== void 0) {
		const c = dPatch.memory;
		if (c === null) delete def.memory;
		else if (typeof c === "object" && !Array.isArray(c)) {
			const p = c;
			if (!def.memory || typeof def.memory !== "object") def.memory = {};
			const t = def.memory;
			if (p.enabled !== void 0) if (p.enabled === null) delete t.enabled;
			else t.enabled = Boolean(p.enabled);
			if (p.useEnhancedSystem !== void 0) if (p.useEnhancedSystem === null) delete t.useEnhancedSystem;
			else t.useEnhancedSystem = Boolean(p.useEnhancedSystem);
			if (p.userProfileEnabled !== void 0) if (p.userProfileEnabled === null) delete t.userProfileEnabled;
			else t.userProfileEnabled = Boolean(p.userProfileEnabled);
			if (p.memoryCharLimit !== void 0) {
				if (p.memoryCharLimit === null) delete t.memoryCharLimit;
				else if (typeof p.memoryCharLimit === "number" && p.memoryCharLimit > 0) t.memoryCharLimit = Math.floor(p.memoryCharLimit);
			}
			if (p.userCharLimit !== void 0) {
				if (p.userCharLimit === null) delete t.userCharLimit;
				else if (typeof p.userCharLimit === "number" && p.userCharLimit > 0) t.userCharLimit = Math.floor(p.userCharLimit);
			}
			if (p.provider === "none" || p.provider === "stub") t.provider = p.provider;
			else if (p.provider === null) delete t.provider;
			if (p.injectionFrequency === "every-turn" || p.injectionFrequency === "first-turn") t.injectionFrequency = p.injectionFrequency;
			else if (p.injectionFrequency === null) delete t.injectionFrequency;
			if (p.contextCadence !== void 0) {
				if (p.contextCadence === null) delete t.contextCadence;
				else if (typeof p.contextCadence === "number" && p.contextCadence >= 1) t.contextCadence = Math.floor(p.contextCadence);
			}
			if (p.dialecticCadence !== void 0) {
				if (p.dialecticCadence === null) delete t.dialecticCadence;
				else if (typeof p.dialecticCadence === "number" && p.dialecticCadence >= 1) t.dialecticCadence = Math.floor(p.dialecticCadence);
			}
			if (p.dreaming !== void 0) {
				const d0 = p.dreaming;
				if (d0 === null) delete t.dreaming;
				else if (typeof d0 === "object" && d0 !== null && !Array.isArray(d0)) {
					if (!("dreaming" in t) || typeof t.dreaming !== "object") t.dreaming = {};
					const dt = t.dreaming;
					const dp = d0;
					if (dp.enabled !== void 0) if (dp.enabled === null) delete dt.enabled;
					else dt.enabled = Boolean(dp.enabled);
					if (dp.frequency !== void 0) {
						const v = dp.frequency;
						if (v === null || v === "") delete dt.frequency;
						else if (typeof v === "string") dt.frequency = v;
					}
					if (dp.timezone !== void 0) {
						const v = dp.timezone;
						if (v === null || v === "") delete dt.timezone;
						else if (typeof v === "string") dt.timezone = v;
					}
					if (dp.phases !== void 0 || dp.deep !== void 0) {
						if (!("phases" in dt) || typeof dt.phases !== "object" || dt.phases === null) dt.phases = {};
						const phases = dt.phases;
						if (!("deep" in phases) || typeof phases.deep !== "object" || phases.deep === null) phases.deep = {};
						const deep = phases.deep;
						const deepPatchRaw = typeof dp.phases === "object" && dp.phases !== null && !Array.isArray(dp.phases) ? dp.phases.deep : dp.deep;
						const deepPatch = typeof deepPatchRaw === "object" && deepPatchRaw !== null && !Array.isArray(deepPatchRaw) ? deepPatchRaw : null;
						if (deepPatch) {
							if (deepPatch.enabled !== void 0) if (deepPatch.enabled === null) delete deep.enabled;
							else deep.enabled = Boolean(deepPatch.enabled);
							if (deepPatch.minScore !== void 0) {
								const v = deepPatch.minScore;
								if (v === null) delete deep.minScore;
								else if (typeof v === "number" && Number.isFinite(v)) deep.minScore = v;
							}
							if (deepPatch.minRecallCount !== void 0) {
								const v = deepPatch.minRecallCount;
								if (v === null) delete deep.minRecallCount;
								else if (typeof v === "number" && Number.isFinite(v)) deep.minRecallCount = Math.floor(v);
							}
							if (deepPatch.limit !== void 0) {
								const v = deepPatch.limit;
								if (v === null) delete deep.limit;
								else if (typeof v === "number" && Number.isFinite(v)) deep.limit = Math.floor(v);
							}
						}
					}
				}
			}
		}
	}
	if (dPatch.sessionSearch !== void 0) {
		const c = dPatch.sessionSearch;
		if (c === null) delete def.sessionSearch;
		else if (typeof c === "object" && !Array.isArray(c)) {
			const p = c;
			if (!def.sessionSearch || typeof def.sessionSearch !== "object") def.sessionSearch = {};
			const t = def.sessionSearch;
			if (p.summaryModel !== void 0) {
				if (p.summaryModel === null || p.summaryModel === "") delete t.summaryModel;
				else if (typeof p.summaryModel === "string") t.summaryModel = p.summaryModel;
			}
		}
	}
	if (dPatch.backgroundReview !== void 0) {
		const c = dPatch.backgroundReview;
		if (c === null) delete def.backgroundReview;
		else if (typeof c === "object" && !Array.isArray(c)) {
			const p = c;
			if (!def.backgroundReview || typeof def.backgroundReview !== "object") def.backgroundReview = {};
			const t = def.backgroundReview;
			if (p.enabled !== void 0) if (p.enabled === null) delete t.enabled;
			else t.enabled = Boolean(p.enabled);
			if (p.memoryNudgeInterval !== void 0) {
				if (p.memoryNudgeInterval === null) delete t.memoryNudgeInterval;
				else if (typeof p.memoryNudgeInterval === "number" && p.memoryNudgeInterval >= 0) t.memoryNudgeInterval = Math.floor(p.memoryNudgeInterval);
			}
			if (p.skillNudgeInterval !== void 0) {
				if (p.skillNudgeInterval === null) delete t.skillNudgeInterval;
				else if (typeof p.skillNudgeInterval === "number" && p.skillNudgeInterval >= 0) t.skillNudgeInterval = Math.floor(p.skillNudgeInterval);
			}
			if (p.maxToolRounds !== void 0) {
				if (p.maxToolRounds === null) delete t.maxToolRounds;
				else if (typeof p.maxToolRounds === "number" && p.maxToolRounds >= 1 && p.maxToolRounds <= 32) t.maxToolRounds = Math.floor(p.maxToolRounds);
			}
			if (p.maxHistoryMessages !== void 0) {
				if (p.maxHistoryMessages === null) delete t.maxHistoryMessages;
				else if (typeof p.maxHistoryMessages === "number" && p.maxHistoryMessages >= 10 && p.maxHistoryMessages <= 200) t.maxHistoryMessages = Math.floor(p.maxHistoryMessages);
			}
			if (p.maxDurationMs !== void 0) {
				if (p.maxDurationMs === null) delete t.maxDurationMs;
				else if (typeof p.maxDurationMs === "number" && p.maxDurationMs >= 3e4 && p.maxDurationMs <= 6e5) t.maxDurationMs = Math.floor(p.maxDurationMs);
			}
		}
	}
	if (dPatch.webExtract !== void 0) {
		const c = dPatch.webExtract;
		if (c === null) delete def.webExtract;
		else if (typeof c === "object" && !Array.isArray(c)) {
			const p = c;
			if (!def.webExtract || typeof def.webExtract !== "object") def.webExtract = {};
			const t = def.webExtract;
			if (p.model !== void 0) {
				if (p.model === null || p.model === "") delete t.model;
				else if (typeof p.model === "string") t.model = p.model;
			}
			if (p.maxLength !== void 0) {
				if (p.maxLength === null) delete t.maxLength;
				else if (typeof p.maxLength === "number" && p.maxLength > 0) t.maxLength = p.maxLength;
			}
		}
	}
	if (dPatch.delegate !== void 0) {
		const c = dPatch.delegate;
		if (c === null) delete def.delegate;
		else if (typeof c === "object" && !Array.isArray(c)) {
			const p = c;
			if (!def.delegate || typeof def.delegate !== "object") def.delegate = {};
			const t = def.delegate;
			if (p.enabled !== void 0) if (p.enabled === null) delete t.enabled;
			else t.enabled = Boolean(p.enabled);
		}
	}
	if (dPatch.executeCode !== void 0) {
		const c = dPatch.executeCode;
		if (c === null) delete def.executeCode;
		else if (typeof c === "object" && !Array.isArray(c)) {
			const p = c;
			if (!def.executeCode || typeof def.executeCode !== "object") def.executeCode = {};
			const t = def.executeCode;
			if (p.enabled !== void 0) if (p.enabled === null) delete t.enabled;
			else t.enabled = Boolean(p.enabled);
		}
	}
	if (dPatch.systemPromptOverride !== void 0) {
		const v = dPatch.systemPromptOverride;
		if (v === null || v === "") delete def.systemPromptOverride;
		else if (typeof v === "string") def.systemPromptOverride = v;
	}
	if (dPatch.skills !== void 0) {
		const v = dPatch.skills;
		if (v === null) delete def.skills;
		else if (Array.isArray(v) && v.every((x) => typeof x === "string")) def.skills = v;
	}
	if (dPatch.models !== void 0) {
		const normalized = normalizePatchTypedModels(dPatch.models);
		if (normalized === void 0) {} else if (normalized === null) delete def.models;
		else def.models = normalized;
	}
	if (dPatch.tools !== void 0) {
		const t0 = dPatch.tools;
		if (t0 === null) delete def.tools;
		else if (typeof t0 === "object" && !Array.isArray(t0)) {
			const p = t0;
			if (!def.tools || typeof def.tools !== "object") def.tools = {};
			const t = def.tools;
			if (p.disable !== void 0) {
				if (p.disable === null || Array.isArray(p.disable) && p.disable.length === 0) delete t.disable;
				else if (Array.isArray(p.disable) && p.disable.every((x) => typeof x === "string")) t.disable = p.disable;
			}
		}
	}
	if (dPatch.params !== void 0) {
		const v = dPatch.params;
		if (v === null) delete def.params;
		else if (typeof v === "object" && !Array.isArray(v) && v !== null) def.params = v;
	}
}
//#endregion
export { applyAgentsPatch };

//# sourceMappingURL=agents.js.map