import { type ToolRuntimeKind } from "../../cli/tool-runtime.ts";
import type { ExtensionOwner } from "./extension-owner.ts";
import type { AgentSessionRuntime } from "../../core/agent-session-runtime.ts";
import type { ExtensionUIContext } from "../../core/extensions/index.ts";
import { ConnectionRoles } from "./connection-roles.ts";
import { type SecretInputRoute } from "./secret-input-requests.ts";
import { type RelocationClientInfo } from "./session-relocation-channel.ts";
export { buildRpcSessionState } from "./rpc-session-state.ts";
/** Additive per-connection options. Absent = classic default (byte-identical). */
export interface RpcConnectionOptions {
    /** Trusted capture supplied by the host binding, never by a public RPC command. */
    rpcOwner?: () => ExtensionOwner | undefined;
    retireRpcBinding?: () => void;
    /** Client capability flags from the handshake (e.g. custom_unsupported opt-in). */
    capabilities?: readonly string[];
    /** The host's per-connection role registry; a single-connection process keeps its own. */
    connectionRoles?: ConnectionRoles;
    /** Called instead of requesting process shutdown for a session-owned binding. */
    shutdownHandler?: () => void;
    /** Session registries own runtime disposal themselves. */
    disposeRuntime?: boolean;
    /** Shared workers flush synchronously into their bounded IPC credit channel. */
    eventFlushScheduler?: (flush: () => void) => void;
    /** Multi-session routing handle. Absent preserves classic wire output exactly. */
    sessionId?: string;
    /**
     * A single-session `--mode rpc` process: it accepts `set_retry_fallback` and advertises it. A host's
     * session connection leaves it off; a host session takes its chain from `open_session.retryFallback`.
     */
    retryFallbackCommand?: boolean;
    /** A single-session `--mode rpc` process advertises its `--tool-runtime` as `tool_runtime:<value>`. */
    toolRuntime?: ToolRuntimeKind;
    /**
     * Shared-session capability registry. A `set_client_info` carrying `capabilities`
     * registers them for the connection that sent it; absent on a classic connection.
     */
    clientInfo?: RelocationClientInfo & {
        /** `"stale"`: the host refused a declaration captured under a retired incarnation (omo-relay-daemon#307). */
        setCapabilities: (connectionId: string | undefined, capabilities: readonly string[], incarnation?: number) => "stale" | void;
        hasCapability?: (connectionId: string, capability: string) => boolean;
    };
    bindSecurityPresentation?: (session: AgentSessionRuntime["session"], ui: ExtensionUIContext) => (() => void) | undefined;
    bindMcpForms?: (session: AgentSessionRuntime["session"], ui: ExtensionUIContext) => (() => void) | undefined | Promise<(() => void) | undefined>;
    /** A worker's internal bridge is gated by its host's actual socket capabilities. */
    workerSecurityBridge?: boolean;
    secretInputRoute?: SecretInputRoute;
    grantDialogSettled?: (id: string) => void;
    /** A grant select was emitted on the connection in context; its non-presenters get a value-free notice (D11 S3-5). */
    grantDialogRaised?: (id: string, title: string) => void;
    /** Shared hosts dedupe `loaded_surfaces_changed` per connection by inventory digest; absent emits inline. */
    publishLoadedSurfacesChanged?: (previous: string | undefined, digest: string) => void;
}
/**
 * The output side of a connection. `writeRaw` receives already-serialized JSONL
 * text (LF-terminated). `waitForBackpressure` lets the host apply flow control
 * (stdout drain in classic mode, or the transport's own `drain` signal).
 */
export interface RpcConnectionSink {
    writeRaw(chunk: string): void;
    /** Shared bindings accept correlated records synchronously, before JSONL buffering. */
    writeRecord?(record: object): void;
    /** The internal structured lane (senpi#4272): seeded records and block controls, never a legacy destination. */
    writeStructured?(record: object): void;
    /** A structured record was lost (its projection or a receiver threw); the event's legacy delivery continues. */
    structuredFailed?(cause: unknown): void;
    waitForBackpressure(): Promise<void>;
    /** Tears the transport down once its event queue can no longer deliver (overflow, write failure). */
    close?(): void;
}
export interface RpcConnectionHandler {
    /**
     * Resolves once the initial session bind completes. Awaiting it guarantees the
     * extension UI context is installed (used by tests that drive ctx.ui directly).
     */
    readonly ready: Promise<void>;
    /** Complete a prebuilt binding with the capabilities its opener supplied. */
    setClientCapabilities(capabilities: readonly string[]): void;
    /** Feed one inbound JSONL line (command or extension_ui_response). */
    handleInputLine(line: string): Promise<void>;
    /**
     * `prompt` calls this handler started that have not settled. The command answers before the
     * prompt's preflight ends, so this is the only record of a prompt that has not started its run yet.
     */
    pendingPrompts(): readonly Promise<unknown>[];
    /**
     * True once an extension requested shutdown via the shutdown handler. The
     * host polls this after each command and decides how to tear down.
     */
    isShutdownRequested(): boolean;
    /** Cancel UI requests that can no longer be answered by this connection. */
    cancelPendingExtensionUiRequests(): void;
    /** A shared host's connection left this session; what it was offering ends. */
    connectionDetached(connectionId: string): void;
    /**
     * Tear down subscriptions and dispose the runtime. Never calls process.exit; `processExit` tells the runtime the
     * caller exits right after (senpi#3711).
     */
    dispose(options?: {
        readonly processExit?: boolean;
    }): Promise<void>;
}
/**
 * Create a per-connection RPC handler bound to one runtime host and one sink.
 *
 * This performs the initial `rebindSession()` and returns synchronously with a
 * handler whose `handleInputLine` is ready to use. Signal handling, stdin
 * wiring, and process exit are intentionally NOT done here.
 */
export declare function createRpcConnectionHandler(runtimeHost: AgentSessionRuntime, sink: RpcConnectionSink, options?: RpcConnectionOptions): RpcConnectionHandler;
//# sourceMappingURL=connection-handler.d.ts.map