import type { Api, ImageContent, Model, TextContent } from "@earendil-works/pi-ai/compat";
import type { ContextCompactionResult } from "../compaction/index.ts";
import type { CustomMessage } from "../messages.ts";
import type { ModelRegistry } from "../model-registry.ts";
import type { ReadonlySessionManager, SessionManager } from "../session-manager.ts";
import type { BuildSystemPromptOptions } from "../system-prompt.ts";
import type { SendMessageOptions } from "./message-types.ts";
import type { ExtensionUIContext } from "./ui-types.ts";
export interface ContextUsage {
    /** Estimated context tokens, or null if unknown (e.g. right after compaction, before next LLM response). */
    tokens: number | null;
    contextWindow: number;
    /** Context usage as percentage of context window, or null if tokens is unknown. */
    percent: number | null;
}
export interface CompactOptions {
    /** Fraction of compactable context to keep. 0.3 is aggressive, 0.7 is light. */
    compression_ratio?: number;
    /** Number of recent context-eligible messages to keep uncompressed. */
    preserve_recent?: number;
    /** Focus query for relevance-based pruning. Defaults to auto-detected session context. */
    query?: string;
    onComplete?: (result: ContextCompactionResult) => void;
    onError?: (error: Error) => void;
}
export interface WorkflowStageOrchestrationContext {
    readonly kind: "workflow-stage";
    readonly workflowRunId: string;
    readonly workflowStageId: string;
    readonly workflowStageName: string;
    readonly constraints: {
        readonly disableWorkflowTool: true;
        readonly maxSubagentDepth: number;
    };
}
export type OrchestrationContext = WorkflowStageOrchestrationContext;
/**
 * Context passed to extension event handlers.
 */
export type ExtensionMode = "tui" | "rpc" | "json" | "print";
export interface ExtensionContext {
    /** Session-scoped orchestration policy for child runtimes such as workflow stages. */
    readonly orchestrationContext?: OrchestrationContext;
    /** UI methods for user interaction */
    ui: ExtensionUIContext;
    /** Current run mode. Use "tui" to guard terminal-only UI such as custom components. */
    mode: ExtensionMode;
    /** Whether dialog-capable UI is available (true in TUI and RPC modes) */
    hasUI: boolean;
    /** Current working directory */
    cwd: string;
    /** Session manager (read-only) */
    sessionManager: ReadonlySessionManager;
    /** Model registry for API key resolution */
    modelRegistry: ModelRegistry;
    /** Current model (may be undefined) */
    model: Model<Api> | undefined;
    /** Session-scoped internal resource router (e.g. artifact:// resolver). */
    readonly internalResourceRouter?: import("../tools/resource-selectors.ts").InternalResourceRouter;
    /** Whether the agent is idle (not streaming) */
    isIdle(): boolean;
    /** Whether project-local trust is active for this context. */
    isProjectTrusted(): boolean;
    /** The current abort signal, or undefined when the agent is not streaming. */
    signal: AbortSignal | undefined;
    /** Abort the current agent operation */
    abort(): void;
    /** Whether there are queued messages waiting */
    hasPendingMessages(): boolean;
    /** Gracefully shutdown pi and exit. Available in all contexts. */
    shutdown(): void;
    /** Get current context usage for the active model. */
    getContextUsage(): ContextUsage | undefined;
    /** Trigger compaction without awaiting completion. */
    compact(options?: CompactOptions): void;
    /** Get the current effective system prompt. */
    getSystemPrompt(): string;
}
/**
 * Extended context for command handlers.
 * Includes session control methods only safe in user-initiated commands.
 */
export interface ExtensionCommandContext extends ExtensionContext {
    /** Get the current base system-prompt construction options. */
    getSystemPromptOptions(): BuildSystemPromptOptions;
    /** Wait for the agent to finish streaming */
    waitForIdle(): Promise<void>;
    /** Start a new session, optionally with initialization. */
    newSession(options?: {
        parentSession?: string;
        setup?: (sessionManager: SessionManager) => Promise<void>;
        withSession?: (ctx: ReplacedSessionContext) => Promise<void>;
    }): Promise<{
        cancelled: boolean;
    }>;
    /** Fork from a specific entry, creating a new session file. */
    fork(entryId: string, options?: {
        position?: "before" | "at";
        withSession?: (ctx: ReplacedSessionContext) => Promise<void>;
    }): Promise<{
        cancelled: boolean;
    }>;
    /** Navigate to a different point in the session tree. */
    navigateTree(targetId: string, options?: {
        summarize?: boolean;
        customInstructions?: string;
        replaceInstructions?: boolean;
        label?: string;
    }): Promise<{
        cancelled: boolean;
    }>;
    /** Switch to a different session file. */
    switchSession(sessionPath: string, options?: {
        withSession?: (ctx: ReplacedSessionContext) => Promise<void>;
    }): Promise<{
        cancelled: boolean;
    }>;
    /** Reload extensions, skills, prompts, and themes. */
    reload(): Promise<void>;
}
/**
 * Fresh command-capable context bound to the replacement session after a session switch.
 *
 * This is passed to `withSession()` callbacks on `newSession()`, `fork()`, and `switchSession()`.
 */
export interface ReplacedSessionContext extends ExtensionCommandContext {
    sendMessage<T = unknown>(message: Pick<CustomMessage<T>, "customType" | "content" | "display" | "details">, options?: SendMessageOptions): Promise<void>;
    sendUserMessage(content: string | (TextContent | ImageContent)[], options?: {
        deliverAs?: "steer" | "followUp";
    }): Promise<void>;
}
//# sourceMappingURL=context-types.d.ts.map