import { type AuthInteraction } from "@bastani/pi-ai"; import { type Api, type Model } from "@bastani/pi-ai/compat"; import type { ThinkingLevel } from "@earendil-works/pi-agent-core"; import type { AgentSession } from "./agent-session.js"; import { type AtomicOAuthLoginCallbacks } from "./agent-session-runtime-auth.ts"; import type { AgentSessionRuntimeDiagnostic, AgentSessionServices } from "./agent-session-services.ts"; import type { ProjectTrustContext, ReplacedSessionContext, SessionStartEvent } from "./extensions/index.js"; import type { ModelFallbackReason } from "./model-resolver-types.ts"; import type { AuthStatus } from "./provider-composer.ts"; import type { CreateAgentSessionResult } from "./sdk.ts"; import { SessionManager } from "./session-manager.ts"; /** * Result returned by runtime creation. * * The caller gets the created session, its cwd-bound services, and all * diagnostics collected during setup. */ export interface CreateAgentSessionRuntimeResult extends CreateAgentSessionResult { services: AgentSessionServices; diagnostics: AgentSessionRuntimeDiagnostic[]; } export interface LogoutProviderResult { provider: string; authStatus: AuthStatus; models: Model[]; scopedModels?: Array<{ model: Model; thinkingLevel?: ThinkingLevel; }>; } /** * Creates a full runtime for a target cwd and session manager. * * The factory closes over process-global fixed inputs, recreates cwd-bound * services for the effective cwd, resolves session options against those * services, and finally creates the AgentSession. */ export interface CreateAgentSessionRuntimeOptions { cwd: string; agentDir: string; sessionManager: SessionManager; sessionStartEvent?: SessionStartEvent; projectTrustContext?: ProjectTrustContext; } export type CreateAgentSessionRuntimeFactory = { (options: CreateAgentSessionRuntimeOptions): Promise; prepareResume?: (options: CreateAgentSessionRuntimeOptions) => Promise<() => Promise>; }; /** * Thrown when /import references a JSONL file path that does not exist. */ export declare class SessionImportFileNotFoundError extends Error { readonly filePath: string; constructor(filePath: string); } /** * Owns the current AgentSession plus its cwd-bound services. * * Session replacement methods tear down the current runtime first, then create * and apply the next runtime. If creation fails, the error is propagated to the * caller. The caller is responsible for user-facing error handling. */ export declare class AgentSessionRuntime { private rebindSession?; private beforeSessionInvalidate?; private projectTrustContextFactory?; private closed; private endedBySignal; private closing?; private readonly ownedStores; private cleanupFailures; private candidates; private replacements; private publication; private publicationContext; private retained; private retirementCleanup; private retainRetirementCleanup; private finalizeRetained; private replace; private assertOpen; private admit; /** Bind a process-local trust UI; closures never cross the engine RPC boundary. */ setProjectTrustContextFactory(factory: (cwd: string) => ProjectTrustContext): void; /** Return false for ordinary startup; otherwise finish the safe session in place. */ completeStartup(): Promise; private _session; private _services; private readonly createRuntime; private _diagnostics; private _modelFallbackMessage?; private _modelFallbackReason?; constructor(_session: AgentSession, _services: AgentSessionServices, createRuntime: CreateAgentSessionRuntimeFactory, _diagnostics?: AgentSessionRuntimeDiagnostic[], _modelFallbackMessage?: string, _modelFallbackReason?: ModelFallbackReason); /** A wrapping runtime replaces, rather than borrows, the local lifetime owner. */ protected adoptSessionStores(owner: AgentSessionRuntime): void; private retainSessionStore; private retainCleanupFailures; private createReplacement; get services(): AgentSessionServices; get session(): AgentSession; get cwd(): string; get diagnostics(): readonly AgentSessionRuntimeDiagnostic[]; get modelFallbackMessage(): string | undefined; get modelFallbackReason(): ModelFallbackReason | undefined; replaceModelFallback(message?: string, reason?: ModelFallbackReason): void; resolveModelFallback(): void; resolveModelFallbackAfterExplicitModelSelection(previousModel: Model | undefined, selectedModel: Model | null | undefined): void; loginOAuthProvider(provider: string, callbacks: AtomicOAuthLoginCallbacks): Promise<{ modelsRefreshed: boolean; }>; loginApiKeyProvider(provider: string, interaction: AuthInteraction): Promise<{ modelsRefreshed: boolean; }>; logoutProvider(provider: string): Promise; setRebindSession(rebindSession?: (session: AgentSession) => Promise): void; /** * Set a synchronous callback that runs after `session_shutdown` handlers finish * but before the current session is invalidated. * * When a replacement is requested from inside the outgoing session's own tracked * work (for example an extension command calling `ctx.switchSession()`), the * outgoing session finishes closing only after that work ends, which is after the * replacement is published. In that case the callback runs once when the outgoing * session's extension authority is retired, before the replacement binds and * before the outgoing `session_shutdown` handlers run, so it never resets UI that * the replacement session has already set. * * This is for host-owned UI teardown that must not yield to the event loop, * such as detaching extension-provided TUI components before the old extension * context becomes stale. */ setBeforeSessionInvalidate(beforeSessionInvalidate?: () => void): void; private emitBeforeSwitch; private emitBeforeFork; private quitEvent; private disposeCurrentSession; /** * Settle an active response before the session it belongs to is replaced, so the * aborted turn (including its tool results) is persisted to the outgoing session * rather than stranded. * * Overridable because the isolated engine must not re-enter its cooperative * abort wait here: the child engine settles its own turn before it reports the * replacement, and the host facade's `abort()` is an unbounded round trip that * would hang teardown on an unresponsive or dead engine. */ protected settleActiveResponseBeforeTeardown(): Promise; private serializePublication; private teardownCurrent; private apply; switchSession(sessionId: string, options?: { cwdOverride?: string; sessionDirectory?: string; withSession?: (ctx: ReplacedSessionContext) => Promise; projectTrustContextFactory?: (cwd: string) => ProjectTrustContext; }): Promise<{ cancelled: boolean; }>; newSession(options?: { parentSession?: string; setup?: (sessionManager: SessionManager) => Promise; withSession?: (ctx: ReplacedSessionContext) => Promise; }): Promise<{ cancelled: boolean; }>; fork(entryId: string, options?: { position?: "before" | "at"; withSession?: (ctx: ReplacedSessionContext) => Promise; }): Promise<{ cancelled: boolean; selectedText?: string; }>; /** Import a format-4 JSONL export into the active session store. */ importFromJsonl(inputPath: string, cwdOverride?: string): Promise<{ cancelled: boolean; }>; dispose(options?: { fromSignal?: boolean; }): Promise; } /** * Create the initial runtime from a runtime factory and initial session target. * * The same factory is stored on the returned AgentSessionRuntime and reused for * later /new, /resume, /fork, and import flows. */ export declare function createAgentSessionRuntime(createRuntime: CreateAgentSessionRuntimeFactory, options: { cwd: string; agentDir: string; sessionManager: SessionManager; sessionStartEvent?: SessionStartEvent; }): Promise; export { type AgentSessionRuntimeDiagnostic, type AgentSessionServices, type CreateAgentSessionFromServicesOptions, type CreateAgentSessionServicesOptions, createAgentSessionFromServices, createAgentSessionServices, } from "./agent-session-services.ts"; //# sourceMappingURL=agent-session-runtime.d.ts.map