import { createLogger } from "../../utils/logger/index.js";
import { init_logger } from "../../utils/logger.js";
//#region src/agent/embedded/runs.ts
init_logger();
const log = createLogger("EmbeddedRunRegistry");
/**
* Tracks in-flight embedded agent turns so we can abort/steer them by sessionKey or runId.
*
* The previous implementation used two module-level Maps; this class is the supported
* single-instance state, while {@link defaultEmbeddedRunRegistry} preserves the previous
* free-function API for callers that have not yet been moved to dependency injection.
*/
var EmbeddedRunRegistry = class {
	bySessionKey = /* @__PURE__ */ new Map();
	byRunId = /* @__PURE__ */ new Map();
	/**
	* Register a new in-flight run. If a run is already registered for the same
	* sessionKey we drop the stale handle (the new run takes ownership). The caller
	* is responsible for aborting the previous run before re-registering if needed —
	* we warn loudly so this does not happen silently.
	*/
	register(handle) {
		const previous = this.bySessionKey.get(handle.sessionKey);
		if (previous && previous !== handle) {
			log.warn({
				sessionKey: handle.sessionKey,
				previousRunId: previous.runId,
				newRunId: handle.runId
			}, "Replacing already-registered embedded run for sessionKey (caller did not unregister first)");
			this.byRunId.delete(previous.runId);
		}
		this.bySessionKey.set(handle.sessionKey, handle);
		this.byRunId.set(handle.runId, handle);
	}
	unregister(handle) {
		if (this.bySessionKey.get(handle.sessionKey) === handle) this.bySessionKey.delete(handle.sessionKey);
		this.byRunId.delete(handle.runId);
	}
	getBySessionKey(sessionKey) {
		return this.bySessionKey.get(sessionKey);
	}
	getByRunId(runId) {
		return this.byRunId.get(runId);
	}
	async abortBySessionKey(sessionKey) {
		const handle = this.bySessionKey.get(sessionKey);
		if (!handle) return false;
		await handle.abort();
		return true;
	}
	async steerBySessionKey(sessionKey, text) {
		const handle = this.bySessionKey.get(sessionKey);
		if (!handle) return false;
		await handle.session.steer(text);
		return true;
	}
	/** Test-only helper to drop all registrations without touching live sessions. */
	resetForTest() {
		this.bySessionKey.clear();
		this.byRunId.clear();
	}
	size() {
		return this.bySessionKey.size;
	}
};
/**
* Process-wide default registry. Prefer constructing a dedicated `EmbeddedRunRegistry`
* and injecting it; the module-level singleton stays so the existing free functions
* keep working until every caller has been migrated.
*/
const defaultEmbeddedRunRegistry = new EmbeddedRunRegistry();
function registerEmbeddedRun(handle) {
	defaultEmbeddedRunRegistry.register(handle);
}
function unregisterEmbeddedRun(handle) {
	defaultEmbeddedRunRegistry.unregister(handle);
}
function getEmbeddedRunBySessionKey(sessionKey) {
	return defaultEmbeddedRunRegistry.getBySessionKey(sessionKey);
}
function getEmbeddedRunByRunId(runId) {
	return defaultEmbeddedRunRegistry.getByRunId(runId);
}
function abortEmbeddedRun(sessionKey) {
	return defaultEmbeddedRunRegistry.abortBySessionKey(sessionKey);
}
function queueEmbeddedSteer(sessionKey, text) {
	return defaultEmbeddedRunRegistry.steerBySessionKey(sessionKey, text);
}
//#endregion
export { EmbeddedRunRegistry, abortEmbeddedRun, defaultEmbeddedRunRegistry, getEmbeddedRunByRunId, getEmbeddedRunBySessionKey, queueEmbeddedSteer, registerEmbeddedRun, unregisterEmbeddedRun };

//# sourceMappingURL=runs.js.map