import { AGENT_LIFECYCLE_STATUSES, type AgentLifecycleStatus } from "../../shared/agent-lifecycle.js";
import type { Logger } from "pino";
import type { AgentCapabilityFlags, AgentClient, AgentFeature, AgentSlashCommand, AgentMode, AgentPermissionRequest, AgentPermissionResponse, AgentPersistenceHandle, AgentPromptInput, AgentProvider, AgentRunOptions, AgentRunResult, AgentSession, AgentSessionConfig, AgentStreamEvent, AgentTimelineItem, AgentUsage, AgentRuntimeInfo, ListPersistedAgentsOptions, PersistedAgentDescriptor } from "./agent-sdk-types.js";
import type { AgentStorage } from "./agent-storage.js";
export { AGENT_LIFECYCLE_STATUSES, type AgentLifecycleStatus };
export type AgentManagerEvent = {
    type: "agent_state";
    agent: ManagedAgent;
} | {
    type: "agent_stream";
    agentId: string;
    event: AgentStreamEvent;
    seq?: number;
    epoch?: string;
};
export type AgentSubscriber = (event: AgentManagerEvent) => void;
export type SubscribeOptions = {
    agentId?: string;
    replayState?: boolean;
};
export type PersistedAgentQueryOptions = ListPersistedAgentsOptions & {
    provider?: AgentProvider;
};
export type AgentAttentionCallback = (params: {
    agentId: string;
    provider: AgentProvider;
    reason: "finished" | "error" | "permission";
}) => void;
export type ProviderAvailability = {
    provider: AgentProvider;
    available: boolean;
    error: string | null;
};
export type AgentManagerOptions = {
    clients?: Partial<Record<AgentProvider, AgentClient>>;
    maxTimelineItems?: number;
    idFactory?: () => string;
    registry?: AgentStorage;
    onAgentAttention?: AgentAttentionCallback;
    logger: Logger;
};
export type WaitForAgentOptions = {
    signal?: AbortSignal;
    waitForActive?: boolean;
};
export type WaitForAgentResult = {
    status: AgentLifecycleStatus;
    permission: AgentPermissionRequest | null;
    lastMessage: string | null;
};
export type WaitForAgentStartOptions = {
    signal?: AbortSignal;
};
export type AgentTimelineRow = {
    seq: number;
    timestamp: string;
    item: AgentTimelineItem;
};
export type AgentTimelineCursor = {
    epoch: string;
    seq: number;
};
export type AgentTimelineFetchDirection = "tail" | "before" | "after";
export type AgentTimelineFetchOptions = {
    direction?: AgentTimelineFetchDirection;
    cursor?: AgentTimelineCursor;
    /**
     * Number of canonical rows to return.
     * - undefined: manager default
     * - 0: all rows in the selected window
     */
    limit?: number;
};
export type AgentTimelineWindow = {
    minSeq: number;
    maxSeq: number;
    nextSeq: number;
};
export type AgentTimelineFetchResult = {
    epoch: string;
    direction: AgentTimelineFetchDirection;
    reset: boolean;
    staleCursor: boolean;
    gap: boolean;
    window: AgentTimelineWindow;
    hasOlder: boolean;
    hasNewer: boolean;
    rows: AgentTimelineRow[];
};
type AttentionState = {
    requiresAttention: false;
} | {
    requiresAttention: true;
    attentionReason: "finished" | "error" | "permission";
    attentionTimestamp: Date;
};
type ForegroundTurnWaiter = {
    turnId: string;
    callback: (event: AgentStreamEvent) => void;
    settled: boolean;
    settledPromise: Promise<void>;
    resolveSettled: () => void;
};
type ManagedAgentBase = {
    id: string;
    provider: AgentProvider;
    cwd: string;
    capabilities: AgentCapabilityFlags;
    config: AgentSessionConfig;
    runtimeInfo?: AgentRuntimeInfo;
    createdAt: Date;
    updatedAt: Date;
    availableModes: AgentMode[];
    features?: AgentFeature[];
    currentModeId: string | null;
    pendingPermissions: Map<string, AgentPermissionRequest>;
    pendingReplacement: boolean;
    timeline: AgentTimelineItem[];
    timelineRows: AgentTimelineRow[];
    timelineEpoch: string;
    timelineNextSeq: number;
    persistence: AgentPersistenceHandle | null;
    historyPrimed: boolean;
    lastUserMessageAt: Date | null;
    lastUsage?: AgentUsage;
    lastError?: string;
    attention: AttentionState;
    foregroundTurnWaiters: Set<ForegroundTurnWaiter>;
    unsubscribeSession: (() => void) | null;
    /**
     * Internal agents are hidden from listings and don't trigger notifications.
     */
    internal?: boolean;
    /**
     * User-defined labels for categorizing agents (e.g., { surface: "workspace" }).
     */
    labels: Record<string, string>;
};
type ManagedAgentWithSession = ManagedAgentBase & {
    session: AgentSession;
};
type ManagedAgentInitializing = ManagedAgentWithSession & {
    lifecycle: "initializing";
    activeForegroundTurnId: null;
};
type ManagedAgentIdle = ManagedAgentWithSession & {
    lifecycle: "idle";
    activeForegroundTurnId: null;
};
type ManagedAgentRunning = ManagedAgentWithSession & {
    lifecycle: "running";
    activeForegroundTurnId: string | null;
};
type ManagedAgentError = ManagedAgentWithSession & {
    lifecycle: "error";
    activeForegroundTurnId: null;
    lastError: string;
};
type ManagedAgentClosed = ManagedAgentBase & {
    lifecycle: "closed";
    session: null;
    activeForegroundTurnId: null;
};
export type ManagedAgent = ManagedAgentInitializing | ManagedAgentIdle | ManagedAgentRunning | ManagedAgentError | ManagedAgentClosed;
export interface AgentMetricsSnapshot {
    total: number;
    byLifecycle: Record<string, number>;
    withActiveForegroundTurn: number;
    timelineStats: {
        totalItems: number;
        maxItemsPerAgent: number;
    };
}
export declare class AgentManager {
    private readonly clients;
    private readonly agents;
    private readonly pendingForegroundRuns;
    private readonly subscribers;
    private readonly maxTimelineItems;
    private readonly idFactory;
    private readonly registry?;
    private readonly previousStatuses;
    private readonly backgroundTasks;
    private onAgentAttention?;
    private logger;
    constructor(options: AgentManagerOptions);
    registerClient(provider: AgentProvider, client: AgentClient): void;
    setAgentAttentionCallback(callback: AgentAttentionCallback): void;
    getMetricsSnapshot(): AgentMetricsSnapshot;
    private touchUpdatedAt;
    hasInFlightRun(agentId: string): boolean;
    subscribe(callback: AgentSubscriber, options?: SubscribeOptions): () => void;
    listAgents(): ManagedAgent[];
    listPersistedAgents(options?: PersistedAgentQueryOptions): Promise<PersistedAgentDescriptor[]>;
    listProviderAvailability(): Promise<ProviderAvailability[]>;
    listDraftCommands(config: AgentSessionConfig): Promise<AgentSlashCommand[]>;
    listDraftFeatures(config: AgentSessionConfig): Promise<AgentFeature[]>;
    getAgent(id: string): ManagedAgent | null;
    getTimeline(id: string): AgentTimelineItem[];
    getTimelineRows(id: string): AgentTimelineRow[];
    fetchTimeline(id: string, options?: AgentTimelineFetchOptions): AgentTimelineFetchResult;
    createAgent(config: AgentSessionConfig, agentId?: string, options?: {
        labels?: Record<string, string>;
    }): Promise<ManagedAgent>;
    resumeAgentFromPersistence(handle: AgentPersistenceHandle, overrides?: Partial<AgentSessionConfig>, agentId?: string, options?: {
        createdAt?: Date;
        updatedAt?: Date;
        lastUserMessageAt?: Date | null;
        labels?: Record<string, string>;
    }): Promise<ManagedAgent>;
    reloadAgentSession(agentId: string, overrides?: Partial<AgentSessionConfig>): Promise<ManagedAgent>;
    closeAgent(agentId: string): Promise<void>;
    archiveAgent(agentId: string): Promise<{
        archivedAt: string;
    }>;
    setAgentMode(agentId: string, modeId: string): Promise<void>;
    setAgentModel(agentId: string, modelId: string | null): Promise<void>;
    setAgentThinkingOption(agentId: string, thinkingOptionId: string | null): Promise<void>;
    setAgentFeature(agentId: string, featureId: string, value: unknown): Promise<void>;
    setTitle(agentId: string, title: string): Promise<void>;
    setLabels(agentId: string, labels: Record<string, string>): Promise<void>;
    notifyAgentState(agentId: string): void;
    clearAgentAttention(agentId: string): Promise<void>;
    runAgent(agentId: string, prompt: AgentPromptInput, options?: AgentRunOptions): Promise<AgentRunResult>;
    recordUserMessage(agentId: string, text: string, options?: {
        messageId?: string;
        emitState?: boolean;
    }): void;
    appendTimelineItem(agentId: string, item: AgentTimelineItem): Promise<void>;
    emitLiveTimelineItem(agentId: string, item: AgentTimelineItem): Promise<void>;
    streamAgent(agentId: string, prompt: AgentPromptInput, options?: AgentRunOptions): AsyncGenerator<AgentStreamEvent>;
    private finalizeForegroundTurn;
    replaceAgentRun(agentId: string, prompt: AgentPromptInput, options?: AgentRunOptions): AsyncGenerator<AgentStreamEvent>;
    waitForAgentRunStart(agentId: string, options?: WaitForAgentStartOptions): Promise<void>;
    respondToPermission(agentId: string, requestId: string, response: AgentPermissionResponse): Promise<void>;
    cancelAgentRun(agentId: string): Promise<boolean>;
    getPendingPermissions(agentId: string): AgentPermissionRequest[];
    private peekPendingPermission;
    hydrateTimelineFromProvider(agentId: string): Promise<void>;
    private getLastAssistantMessage;
    private getLastAssistantMessageFromTimeline;
    waitForAgentEvent(agentId: string, options?: WaitForAgentOptions): Promise<WaitForAgentResult>;
    private registerSession;
    private subscribeToSession;
    private dispatchSessionEvent;
    private settleForegroundTurnWaiter;
    private createPendingForegroundRun;
    private getPendingForegroundRun;
    private hasPendingForegroundRun;
    private settlePendingForegroundRun;
    private resolveInitialPersistedTitle;
    private buildTimelineRowsFromItems;
    private ensureTimelineState;
    private persistSnapshot;
    private refreshSessionState;
    private refreshRuntimeInfo;
    private hydrateTimeline;
    private handleStreamEvent;
    private appendSystemErrorTimelineMessage;
    private formatTurnFailedMessage;
    private recordTimeline;
    private emitState;
    private checkAndSetAttention;
    private enqueueBackgroundPersist;
    private trackBackgroundTask;
    /**
     * Flush any background persistence work (best-effort).
     * Used by daemon shutdown paths to avoid unhandled rejections after cleanup.
     */
    flush(): Promise<void>;
    private broadcastAgentAttention;
    private dispatchStream;
    private dispatch;
    private normalizeConfig;
    private buildLaunchContext;
    private requireClient;
    private requireAgent;
}
//# sourceMappingURL=agent-manager.d.ts.map