import { type Api, type AssistantMessage, type Context, type Model, type Models, type RetryCallbacks, type RetryPolicy, type SimpleStreamOptions, type Usage } from "@earendil-works/pi-ai";
import type { AgentMessage, ThinkingLevel } from "../../types.ts";
import type { Entry } from "../session/types.ts";
import { CompactionError, type Result } from "../types.ts";
import { type FileOperations } from "./utils.ts";
/** File-operation details stored on generated compaction entries. */
export interface CompactionDetails {
    /** Files read in the compacted history. */
    readFiles: string[];
    /** Files modified in the compacted history. */
    modifiedFiles: string[];
}
/** Generated compaction data ready to be persisted as a compaction entry. */
export interface CompactResult<T = unknown> {
    /** Summary text that replaces compacted history in future context. */
    summary: string;
    /** Estimated context tokens before compaction. */
    tokensBefore: number;
    /** Usage from the LLM call(s) that generated this summary, if available. */
    usage?: Usage;
    /** Retained recent messages stored directly on the compaction entry. */
    retainedTail: AgentMessage[];
    /** Optional implementation-specific details stored with the compaction entry. */
    details?: T;
}
export declare function completeSimpleWithRetries(models: Models, model: Model<Api>, context: Context, options: SimpleStreamOptions, retry?: RetryPolicy, callbacks?: RetryCallbacks): Promise<AssistantMessage>;
/** Compaction thresholds and retention settings. */
export interface CompactionSettings {
    /** Enable automatic compaction decisions. */
    enabled: boolean;
    /** Tokens reserved for summary prompt and output. */
    reserveTokens: number;
    /** Approximate recent-context tokens to keep after compaction. */
    keepRecentTokens: number;
}
/** Default compaction settings used by the harness. */
export declare const DEFAULT_COMPACTION_SETTINGS: CompactionSettings;
/** Calculate total context tokens from provider usage. */
export declare function calculateContextTokens(usage: Usage): number;
/** Return usage from the last valid assistant message in session entries. */
export declare function getLastAssistantUsage(entries: Entry[]): Usage | undefined;
/** Estimated context-token usage for a message list. */
export interface ContextUsageEstimate {
    /** Estimated total context tokens. */
    tokens: number;
    /** Tokens reported by the most recent assistant usage block. */
    usageTokens: number;
    /** Estimated tokens after the most recent assistant usage block. */
    trailingTokens: number;
    /** Index of the message that provided usage, or null when none exists. */
    lastUsageIndex: number | null;
}
/** Estimate context tokens for messages using provider usage when available. */
export declare function estimateContextTokens(messages: AgentMessage[]): ContextUsageEstimate;
/** Return whether context usage exceeds the configured compaction threshold. */
export declare function shouldCompact(contextTokens: number, contextWindow: number, settings: CompactionSettings): boolean;
/** Estimate token count for one message using a conservative character heuristic. */
export declare function estimateTokens(message: AgentMessage): number;
/** Find the user-visible message that starts the turn containing an entry. */
export declare function findTurnStartIndex(entries: Entry[], entryIndex: number, startIndex: number): number;
/** Cut point selected for compaction. */
export interface CutPointResult {
    /** Index of the first entry retained after compaction. */
    firstKeptEntryIndex: number;
    /** Index of the turn-start entry when the cut splits a turn, otherwise -1. */
    turnStartIndex: number;
    /** Whether the selected cut point splits an in-progress turn. */
    isSplitTurn: boolean;
}
/** Find the compaction cut point that keeps approximately the requested recent-token budget. */
export declare function findCutPoint(entries: Entry[], startIndex: number, endIndex: number, keepRecentTokens: number): CutPointResult;
export declare const SUMMARIZATION_SYSTEM_PROMPT = "You are a context summarization assistant. Your task is to read a conversation between a user and an AI assistant, then produce a structured summary following the exact format specified.\n\nDo NOT continue the conversation. Do NOT respond to any questions in the conversation. ONLY output the structured summary.";
/** Generate or update a conversation summary for compaction. */
export declare function generateSummary(currentMessages: AgentMessage[], models: Models, model: Model<Api>, reserveTokens: number, signal?: AbortSignal, customInstructions?: string, previousSummary?: string, thinkingLevel?: ThinkingLevel, retry?: RetryPolicy, callbacks?: RetryCallbacks): Promise<Result<string, CompactionError>>;
/** Generate or update a conversation summary and return its provider usage. */
export declare function generateSummaryWithUsage(currentMessages: AgentMessage[], models: Models, model: Model<Api>, reserveTokens: number, signal?: AbortSignal, customInstructions?: string, previousSummary?: string, thinkingLevel?: ThinkingLevel, retry?: RetryPolicy, callbacks?: RetryCallbacks): Promise<Result<{
    text: string;
    usage: Usage;
}, CompactionError>>;
/** Prepared inputs for a compaction run. */
export interface CompactionPreparation {
    /** Messages summarized into the history summary. */
    messagesToSummarize: AgentMessage[];
    /** Prefix messages summarized separately when compaction splits a turn. */
    turnPrefixMessages: AgentMessage[];
    /** Recent messages retained after compaction and stored on the compaction entry. */
    retainedTail: AgentMessage[];
    /** Whether compaction splits a turn. */
    isSplitTurn: boolean;
    /** Estimated context tokens before compaction. */
    tokensBefore: number;
    /** Previous compaction summary used for iterative updates. */
    previousSummary?: string;
    /** File operations extracted from summarized history. */
    fileOps: FileOperations;
    /** Settings used to prepare compaction. */
    settings: CompactionSettings;
}
/** Prepare session entries for compaction, or return undefined when compaction is not applicable. */
export declare function prepareCompaction(pathEntries: Entry[], settings: CompactionSettings): Result<CompactionPreparation | undefined, CompactionError>;
export { serializeConversation } from "./utils.ts";
/** Generate compaction summary data from prepared session history. */
export declare function compact(preparation: CompactionPreparation, models: Models, model: Model<Api>, customInstructions?: string, signal?: AbortSignal, thinkingLevel?: ThinkingLevel, retry?: RetryPolicy, callbacks?: RetryCallbacks): Promise<Result<CompactResult, CompactionError>>;
//# sourceMappingURL=compaction.d.ts.map