/**
 * Lifecycle controller for a single agent run.
 *
 * Manages the abort signal and a promise that resolves when the run finishes,
 * allowing callers to cancel a run or wait for it to become idle.
 */
export class AgentRunState {
    _abortController;
    _runningPrompt;
    _resolveRunningPrompt;
    /** Begin a new run: creates a fresh AbortController and returns its signal. */
    start() {
        this._runningPrompt = new Promise((resolve) => {
            this._resolveRunningPrompt = resolve;
        });
        this._abortController = new AbortController();
        return this._abortController.signal;
    }
    /** Signal cancellation to the running agent loop. */
    abort() {
        this._abortController?.abort();
    }
    /** Returns a promise that resolves when the current run finishes (or immediately if idle). */
    waitForIdle() {
        return this._runningPrompt ?? Promise.resolve();
    }
    /** Mark the run as complete: resolves the idle promise and clears internal state. */
    finish() {
        this._abortController = undefined;
        this._resolveRunningPrompt?.();
        this._runningPrompt = undefined;
        this._resolveRunningPrompt = undefined;
    }
}
//# sourceMappingURL=agent-run-state.js.map