import { createLogger } from "../../utils/logger/index.js";
import { init_logger } from "../../utils/logger.js";
import { resolveEffectiveThinkingLevel } from "../../session/thinking-resolve.js";
import { effectiveWorkspacePathForSession, normalizeWorkingDirectoryInput } from "../../session/session-workspace.js";
import "../../session/index.js";
import { mkdir } from "node:fs/promises";
//#region src/agent/session/session-hydrator.ts
/**
* SessionHydrator — read persisted per-session config and push it into the
* in-memory runtime (AgentManager + ModelManager).
*
* These three operations were three sibling private methods on `AgentService`
* with subtly different signatures. They are the mirror image of
* {@link SessionConfigService} — the latter writes user choices into the
* config store, this hydrator reads them back out before a turn runs.
*
*   - `workspace()`  — apply persisted `workingDirectoryOverride` to AgentManager
*                       and ensure the directory exists on disk
*   - `model()`      — apply persisted `modelOverride` via ModelManager
*   - `thinking()`   — resolve effective thinking level (request override >
*                       per-session override > agent default) and apply
*/
init_logger();
const log = createLogger("SessionHydrator");
var SessionHydrator = class {
	opts;
	constructor(opts) {
		this.opts = opts;
	}
	/**
	* Load persisted workingDirectory override into AgentManager and `mkdir -p`
	* the effective workspace path. Safe to call before `getOrCreateAgent`.
	*/
	async workspace(sessionKey) {
		const cfg = this.opts.getConfig();
		if (!cfg) return;
		const loaded = await this.opts.sessionConfigStore.get(sessionKey);
		if (loaded?.workingDirectoryOverride?.trim()) {
			const wdStored = normalizeWorkingDirectoryInput(loaded.workingDirectoryOverride);
			if (wdStored.ok) this.opts.agentManager.setSessionWorkspaceOverride(sessionKey, wdStored.path);
			else {
				log.warn({ sessionKey }, "Invalid stored workingDirectoryOverride; ignoring");
				this.opts.agentManager.setSessionWorkspaceOverride(sessionKey, null);
			}
		} else this.opts.agentManager.setSessionWorkspaceOverride(sessionKey, null);
		await mkdir(effectiveWorkspacePathForSession(cfg, sessionKey, loaded), { recursive: true });
	}
	/** Apply persisted `modelOverride` to ModelManager (no-op when none stored). */
	async model(sessionKey) {
		const cfg = await this.opts.sessionConfigStore.get(sessionKey);
		if (cfg?.modelOverride) await this.opts.modelManager.switchModelForSession(sessionKey, cfg.modelOverride);
	}
	/**
	* Resolve the effective thinking level (request override > per-session
	* override > agent default) and apply it to the live agent instance.
	*/
	async thinking(sessionKey, requestOverride) {
		const def = this.opts.getConfig()?.agents?.defaults?.thinkingDefault;
		const level = await resolveEffectiveThinkingLevel(this.opts.sessionConfigStore, sessionKey, requestOverride, def);
		this.opts.agentManager.setThinkingLevel(sessionKey, level);
	}
};
//#endregion
export { SessionHydrator };

//# sourceMappingURL=session-hydrator.js.map