import { type SessionCompactionState } from "../../session/models/session-compaction"; import type { CoreCompactionContext, CoreCompactionMode, CoreSessionConfig } from "../../types/config"; export interface ContextPipelinePrepareTurnInput { agentId: string; conversationId: string; parentAgentId: string | null; iteration: number; messages: CoreCompactionContext["messages"]; apiMessages: CoreCompactionContext["messages"]; abortSignal: AbortSignal; systemPrompt: string; tools: unknown[]; model: CoreCompactionContext["model"]; /** * Set by the runtime when the provider rejected the previous request as * exceeding the model's context window. Forces a compaction regardless of * the token-estimate trigger (the estimate just proved wrong) and uses the * deterministic basic strategy — recovery must not depend on another * successful LLM request. */ overflowRecovery?: boolean; emitStatusNotice?: (message: string, metadata?: Record) => void; } export interface ContextPipelinePrepareTurnResult { messages: CoreCompactionContext["messages"]; systemPrompt?: string; } export type ContextPipelinePrepareTurn = (context: ContextPipelinePrepareTurnInput) => Promise; export interface ContextCompactionPrepareTurnOptions { mode?: CoreCompactionMode; manualTargetRatio?: number; } /** * Build the `prepareTurn` callback used by the agent runtime to compact the * transcript before each model request. * * Telemetry: emits `task.compaction_executed` on a successful compaction and * `task.compaction_skipped` when the configured strategy returns `undefined`. * Telemetry is keyed by `config.sessionId` (falling back to the per-turn * `conversationId`) and tagged with `provider` / `modelId`. * * Known gap: compactions performed via plugin `registerMessageBuilder()` or * via the `beforeModel` runtime hook bypass this wrapper entirely, so they * do not emit compaction telemetry. If we want coverage there too, the * plugin/hook pipelines must be instrumented separately. */ export declare function createContextCompactionPrepareTurn(config: Pick, options?: ContextCompactionPrepareTurnOptions): ((context: ContextPipelinePrepareTurnInput) => Promise) | undefined; export declare function createCompactionStateAwarePrepareTurn(input: { compact?: ContextPipelinePrepareTurn; getState?: () => SessionCompactionState | undefined; /** * Persist a freshly-computed compaction state. `sourceMessages` are the * exact canonical messages the state's source-prefix hash was computed * over; hosts must validate projection against these rather than a * separately derived transcript, which can legally differ mid-turn and * spuriously reject the write. */ saveState?: (state: SessionCompactionState, sourceMessages: CoreCompactionContext["messages"]) => void | Promise; }): ContextPipelinePrepareTurn;