import type { AgentToolResult, AgentToolUpdateCallback } from "@earendil-works/pi-agent-core";
import type { ToolRuntimeKind } from "../../cli/tool-runtime.ts";
import type { HostCallHandler } from "../../modes/rpc/tool-child/host-calls-server.ts";
import type { ExtensionToolContext, ToolDefinition, ToolPlacement } from "../extensions/types.ts";
import { type BashOperations } from "../tools/bash.ts";
import { type StuckModuleNotice } from "./in-process-module.ts";
export type { ToolPlacement } from "../extensions/types.ts";
export declare const TOOL_RUNTIME_UNAVAILABLE = "tool_runtime_unavailable";
export interface ToolUnavailableDetails {
    readonly code: typeof TOOL_RUNTIME_UNAVAILABLE;
}
export interface ToolDispatchedInfo {
    readonly toolCallId: string;
    readonly placement: ToolPlacement;
    readonly pid: number;
    readonly generation: number;
}
export type ToolDispatchObserver = (info: ToolDispatchedInfo) => void;
export type ToolDispatchNotify = (event: string, data?: Record<string, unknown>) => void;
export interface ToolDispatchCall<TDetails = unknown> {
    readonly definition: ToolDefinition<any, TDetails>;
    readonly toolCallId: string;
    readonly params: unknown;
    readonly signal?: AbortSignal;
    readonly onUpdate?: AgentToolUpdateCallback<TDetails>;
    readonly ctx: ExtensionToolContext;
    /** The extension file that registered the tool; a relative `childModule.path` resolves against its directory. */
    readonly sourcePath?: string;
}
/** The tool child's `module` job (senpi#3566; the same shape todo 8's `ctx.offload` uses). */
export interface ToolChildModuleJob {
    readonly modulePath: string;
    readonly export: string;
    readonly args: unknown;
    /** A lost job is retried once on a fresh child only when this is true. */
    readonly idempotent?: boolean;
    /** Overrides the job's default deadline (`ctx.offload`'s `deadlineMs`). */
    readonly deadlineMs?: number;
}
/** Where an offloaded module ran, and what it returned. */
export interface OffloadOutcome {
    readonly value: unknown;
    readonly placement: ToolPlacement;
    readonly pid: number;
    readonly generation: number;
}
export interface ToolChildRunResult {
    readonly value: unknown;
    readonly pid: number;
    readonly generation: number;
}
/** Runs a job of a built-in kind on the session's tool child (`ToolDefinition.childJob`), streaming its updates. */
export type ToolChildKindRunner = (kind: string, args: unknown, call: {
    readonly toolCallId: string;
    readonly signal: AbortSignal | undefined;
    readonly onUpdate?: (value: unknown) => void;
}) => Promise<ToolChildRunResult>;
/** Runs a `module` job on the session's tool child, starting the child on first use. */
export type ToolChildRunner = (job: ToolChildModuleJob, signal: AbortSignal | undefined) => Promise<ToolChildRunResult>;
export interface ToolDispatcherOptions {
    runtime: ToolRuntimeKind;
    notify?: ToolDispatchNotify;
    childRunner?: ToolChildRunner;
    childKindRunner?: ToolChildKindRunner;
    /** The session's host-call handler: an in-process `ctx.offload` module gets the same host the child's worker builds. */
    hostCalls?: HostCallHandler;
    /** How long past its deadline an in-process module may run before it is reported stuck (default 10 s). */
    stuckGraceMs?: number;
    /** Told when an in-process module is stuck (it keeps its locks; their waiters fail fast). */
    onModuleStuck?: (notice: StuckModuleNotice) => void;
}
export interface ToolDispatcher {
    readonly kind: ToolRuntimeKind;
    /** Tool bodies dispatched through `run` that have not settled yet, wherever they run. */
    readonly executing: number;
    run<TDetails = unknown>(call: ToolDispatchCall<TDetails>): Promise<AgentToolResult<TDetails | ToolUnavailableDetails>>;
    bashOperations(options?: {
        shellPath?: string;
    }): BashOperations;
    onDispatched(observer: ToolDispatchObserver): () => void;
    /** Runs a registered child module (`ctx.offload`) where this runtime places it. */
    offload(job: ToolChildModuleJob, signal: AbortSignal | undefined): Promise<OffloadOutcome>;
    /** The session is closed or replaced: work this runtime still runs in this process is closed; settles once it let go. */
    dispose(): Promise<void>;
}
type InProcessDispatchOptions = Pick<ToolDispatcherOptions, "hostCalls" | "stuckGraceMs" | "onModuleStuck">;
/**
 * Placement of one call: only a definition that opted into `"child"` while the
 * session runs the child runtime dispatches there, and never when the definition
 * is server-pinned (injected `operations` / policy override captured at creation).
 */
export declare function resolveToolPlacement(definition: Pick<ToolDefinition, "name" | "placement" | "serverPinned">, runtime: ToolRuntimeKind): ToolPlacement;
declare abstract class BaseToolDispatcher implements ToolDispatcher {
    #private;
    abstract readonly kind: ToolRuntimeKind;
    get executing(): number;
    run<TDetails>(call: ToolDispatchCall<TDetails>): Promise<AgentToolResult<TDetails | ToolUnavailableDetails>>;
    protected abstract dispatch<TDetails>(call: ToolDispatchCall<TDetails>): Promise<AgentToolResult<TDetails | ToolUnavailableDetails>>;
    abstract bashOperations(options?: {
        shellPath?: string;
    }): BashOperations;
    dispose(): Promise<void>;
    abstract offload(job: ToolChildModuleJob, signal: AbortSignal | undefined): Promise<OffloadOutcome>;
    private readonly observers;
    private readonly pinNotified;
    private readonly notifySink;
    constructor(notify?: ToolDispatchNotify);
    onDispatched(observer: ToolDispatchObserver): () => void;
    protected placementFor(definition: Pick<ToolDefinition, "name" | "placement" | "serverPinned">): ToolPlacement;
    protected runOnServer<TDetails>(call: ToolDispatchCall<TDetails>): Promise<AgentToolResult<TDetails>>;
    protected emitDispatched(info: ToolDispatchedInfo): void;
}
export declare class InProcessToolDispatcher extends BaseToolDispatcher {
    #private;
    readonly kind: ToolRuntimeKind;
    constructor(notify?: ToolDispatchNotify, options?: InProcessDispatchOptions);
    /** Closes every in-process module run still live; settles once each has released its locks (`InProcessRun.close`). */
    dispose(): Promise<void>;
    protected dispatch<TDetails>(call: ToolDispatchCall<TDetails>): Promise<AgentToolResult<TDetails>>;
    bashOperations(options?: {
        shellPath?: string;
    }): BashOperations;
    offload(job: ToolChildModuleJob, signal: AbortSignal | undefined): Promise<OffloadOutcome>;
}
/**
 * Refuses child-placed work and child-backed bash until the tool child runtime
 * lands (senpi#3566 todo 4 / todo 9); server-placed work still runs in-process,
 * so a session under `--tool-runtime child` stays usable at this stage.
 */
export declare class ChildToolDispatcher extends BaseToolDispatcher {
    #private;
    readonly kind: ToolRuntimeKind;
    constructor(notify?: ToolDispatchNotify, childRunner?: ToolChildRunner, kindRunner?: ToolChildKindRunner);
    protected dispatch<TDetails>(call: ToolDispatchCall<TDetails>): Promise<AgentToolResult<TDetails | ToolUnavailableDetails>>;
    bashOperations(options?: {
        shellPath?: string;
    }): BashOperations;
    /** Always on the session's child, started on demand; never in-process (no fallback). */
    offload(job: ToolChildModuleJob, signal: AbortSignal | undefined): Promise<OffloadOutcome>;
}
export declare function createToolDispatcher(options: ToolDispatcherOptions): ToolDispatcher;
export declare function unavailableToolResult(toolName: string): AgentToolResult<ToolUnavailableDetails>;
export declare function unavailableBashOperations(): BashOperations;
//# sourceMappingURL=tool-dispatcher.d.ts.map