import type { NestedRequest, PlaneHandler } from "@earendil-works/pi-agent-core";
import type { ExtensionRunner } from "./runner.ts";
import type { Extension, ToolCallEvent, ToolCallEventResult, ToolResultEvent, ToolResultEventResult } from "./types.ts";
/**
 * Two-sided extensions on a split session (brain/hand split S29, decision C7(c)). An extension that registers both loop
 * events and tool events, and declares no side, keeps one in-memory state only if all of it runs in one process: it runs
 * whole where the loop runs, and the hand forwards each of that extension's undeclared `tool_call` / `tool_result`
 * handlers to it as a replied request on the call's nested plane, applying the returned result as if a local handler
 * had returned it. Handlers declared `runs: "hand"` stay on the hand, in their registration position.
 */
export declare const EXTENSION_EVENTS_PLANE = "extension-events";
type ToolEventType = "tool_call" | "tool_result";
type HandlerFn = NonNullable<ReturnType<Extension["handlers"]["get"]>>[number];
/** Where the hand sends one undeclared tool handler's event instead of running it locally. */
export interface ToolEventForwarder {
    readonly extensions: ReadonlySet<string>;
    toolCall(extension: string, handler: number, event: ToolCallEvent): Promise<ToolCallEventResult | undefined>;
    toolResult(extension: string, handler: number, event: ToolResultEvent): Promise<ToolResultEventResult | undefined>;
}
/** One forwarded handler: an extension's `handler`-th undeclared handler for the event, in registration order. */
export interface ForwardedHandler {
    readonly extension: string;
    readonly handler: number;
}
export interface ToolEventDispatchOptions {
    /** On the hand: forward the listed extensions' undeclared handlers instead of running them. */
    readonly forward?: ToolEventForwarder;
    /** Where the loop runs: run only this one handler (a forwarded request). */
    readonly only?: ForwardedHandler;
    /**
     * The call's own abort signal: a `tool_call` handler's `ctx.signal` for this call, combined with the turn's so
     * aborting either ends it (senpi#6676, senpi#6679 M1). Forwarded handlers are the exception (senpi#6679 L1): the
     * forwarder carries the event only, so a handler served on the loop side of a forward sees its loop-hook signal.
     */
    readonly signal?: AbortSignal;
}
/**
 * The extensions whose undeclared registrations span loop-side events (`loop` or `split` in the event placement) and
 * tool events. Builtins are never forwarded: each side loads its own builtins, and their tool hooks (the permission
 * system among them) run on the hand by design (S27a).
 */
export declare function twoSidedExtensions(extensions: readonly Extension[]): Set<string>;
/**
 * The handlers one extension runs for a tool event, in registration order. Serving a forwarded request: only that one
 * undeclared handler. On the hand, for a forwarded extension: each undeclared handler becomes a forward of its ordinal,
 * and declared handlers stay local where they are. Otherwise: the extension's own handlers.
 */
export declare function toolEventHandlers(extension: Extension, own: readonly HandlerFn[], event: ToolEventType, options: ToolEventDispatchOptions, forwardOne: (handler: number) => HandlerFn): HandlerFn[];
/** Whether the extension at `path` has an `handler`-th undeclared handler for `event` (a forwarded event's target). */
export declare function servesForwardedHandler(extensions: readonly Extension[], path: string, event: ToolEventType, handler: number): boolean;
/** The hand's forwarder for one running call: each request goes back to the loop that owns the call. */
export declare function forwardToolEvents(extensions: ReadonlySet<string>, nested: NestedRequest): ToolEventForwarder;
/** Where the loop runs: answers a forwarded tool event by running only that one handler in this generation. */
export declare function serveExtensionEvents(runner: ExtensionRunner): PlaneHandler;
export {};
//# sourceMappingURL=two-sided-forwarding.d.ts.map