import { type ArtifactObligation, type Item, type ItemId, type SessionTokenUsage, type SessionUsageOperation, type ThreadCompactionReason, type ThreadMemoryMaintenanceReason, type ThreadDiagnostics, type ThreadHandoffSnapshot, type ThreadEvidenceKind, type ThreadId, type ThreadSummarySnapshot, type ToolFailureKind, type ToolResultOutcome, type TurnStatus } from "@best-agent/protocol";
import type { ThreadStore } from "../core/thread-store.js";
import type { ConversationMessage, ProviderCacheControl, ProviderUsageStats } from "../provider/model-provider.js";
import type { ToolRegistry } from "../tool/registry.js";
export type ThreadMemorySnapshot = ThreadSummarySnapshot;
export interface CompactedDecisionToolCallSummary {
    callId: ItemId;
    createdAt: string;
    errorCode?: string;
    failureKind?: ToolFailureKind;
    input: string;
    outcome: ToolResultOutcome;
    output: string;
    toolName: string;
}
export interface CompactedDecisionEvidenceRef {
    evidenceId: string;
    kind: ThreadEvidenceKind;
    relatedFilePaths?: string[];
    sourceToolName?: string;
    summary: string;
}
export interface CompactedDecisionRepairEvent {
    callId?: ItemId;
    createdAt: string;
    errorCode?: string;
    summary: string;
    toolName?: string;
    type: "control" | "error";
}
export interface CompactedDecisionState {
    activeObligations: ArtifactObligation[];
    changedFiles: string[];
    completedToolCalls: CompactedDecisionToolCallSummary[];
    failedToolCalls: CompactedDecisionToolCallSummary[];
    objective?: string;
    openBlockers: string[];
    repairHistory: CompactedDecisionRepairEvent[];
    satisfiedObligations: string[];
    verificationEvidence: CompactedDecisionEvidenceRef[];
}
export interface ThreadWorkingMemorySnapshot {
    activePaths: string[];
    activeSkillIds: string[];
    currentObjective?: string;
    decisionState: CompactedDecisionState;
    generatedAt: string;
    importantDecisions: string[];
    latestToolOutcomes: string[];
    latestUserIntent?: string;
    openQuestions: string[];
    threadId: ThreadId;
    unresolvedTasks: string[];
}
export interface ThreadSegmentSnapshot {
    activePaths: string[];
    boundaryItemId?: ItemId;
    compactedItemCount: number;
    createdAt: string;
    endItemId?: ItemId;
    id: string;
    itemCount: number;
    objective?: string;
    recentMessageCount: number;
    startItemId?: ItemId;
    status: "active" | "sealed";
    summaryText: string;
    updatedAt: string;
    userMessageCount: number;
}
export interface ThreadSegmentFile {
    currentSegmentId?: string;
    driftCount: number;
    generatedAt: string;
    segments: ThreadSegmentSnapshot[];
    threadId: ThreadId;
}
export interface StructuredFacts {
    touchedFiles: string[];
    decisions: string[];
    toolOutcomes: Array<{
        tool: string;
        outcome: string;
    }>;
    errors: string[];
    activeConstraints: string[];
}
export type NarrativeSummarizer = (input: {
    segmentTranscript: string;
    structuredFacts: StructuredFacts;
}) => Promise<string>;
export interface ThreadCompactionCommit {
    activeSkillIds: string[];
    boundaryItemId?: ItemId;
    budgetTokens?: number;
    estimatedTokens?: number;
    generatedAt: string;
    id: string;
    narrative?: string;
    narrativeConflicts?: number;
    narrativeModel?: string;
    reason: ThreadMemoryMaintenanceReason;
    structuredFacts?: StructuredFacts;
    summaryText: string;
    threadId: ThreadId;
    workingMemory: Pick<ThreadWorkingMemorySnapshot, "activePaths" | "currentObjective" | "decisionState" | "importantDecisions" | "latestToolOutcomes" | "openQuestions" | "unresolvedTasks">;
}
export interface ThreadConversationDiagnostics {
    budgetTokens?: number;
    cacheHit: boolean;
    effectiveCacheHit: boolean;
    estimatedTokens: number;
    prefixCacheHit: boolean;
    recentItemLimit: number;
    recentMessageLimit: number;
    reusedTokens: number;
    segmentCacheHit: boolean;
}
export interface ThreadConversationBuildResult {
    activeSkillIds: string[];
    cacheControl?: ProviderCacheControl;
    diagnostics: ThreadConversationDiagnostics;
    messages: ConversationMessage[];
    systemPrompt?: string;
}
export interface ThreadCompactionProbeResult {
    budgetTokens?: number;
    estimatedTokensAfter: number;
    estimatedTokensBefore: number;
    needsCompaction: boolean;
    recommendedRecentItemLimit: number;
    recommendedRecentMessageLimit: number;
}
export interface ThreadMemoryUpdateInput {
    activeSkillIds?: string[];
    budgetTokens?: number;
    estimatedTokens?: number;
    forceCommit?: boolean;
    narrativeSummarizer?: NarrativeSummarizer;
    recentItemLimit?: number;
    recentMessageLimit?: number;
    reason?: ThreadMemoryMaintenanceReason;
}
export interface ThreadMemoryUpdateResult {
    appendedCommit: boolean;
    diagnostics: ThreadDiagnostics;
    handoff: ThreadHandoffSnapshot;
    reason: ThreadMemoryMaintenanceReason;
    summary: ThreadMemorySnapshot;
    workingMemory: ThreadWorkingMemorySnapshot;
}
export interface ThreadMemoryPreparedUpdate {
    budgetTokens?: number;
    commit: ThreadCompactionCommit;
    estimatedTokens?: number;
    handoff: ThreadHandoffSnapshot;
    reason: ThreadMemoryMaintenanceReason;
    segments: ThreadSegmentFile;
    summary: ThreadMemorySnapshot;
    workingMemory: ThreadWorkingMemorySnapshot;
}
export interface ThreadUsageDelta {
    last: SessionTokenUsage;
    operation: SessionUsageOperation;
    reported: boolean;
}
export interface ThreadRuntimeSample {
    cpuSystemMs: number;
    cpuUserMs: number;
    wallTimeMs: number;
}
export declare class LocalThreadMemory {
    #private;
    constructor(store: ThreadStore, tools: ToolRegistry, recentMessageLimit?: number, recentItemLimit?: number, contextTokenBudget?: number);
    invalidate(threadId: ThreadId): void;
    recordProviderUsage(threadId: ThreadId, input: {
        cacheControl?: ProviderCacheControl;
        estimatedUsage?: ProviderUsageStats;
        operation: SessionUsageOperation;
        runtime?: ThreadRuntimeSample;
        usage?: ProviderUsageStats;
    }, kind: "decide" | "generate"): ThreadUsageDelta;
    recordToolExecution(threadId: ThreadId, runtime?: ThreadRuntimeSample): void;
    recordSubagentThread(threadId: ThreadId): void;
    recordRepair(threadId: ThreadId): void;
    recordRuntimeCorrection(threadId: ThreadId): void;
    recordReconciliation(threadId: ThreadId, syntheticTerminalItemCount?: number): void;
    recordTurnOutcome(threadId: ThreadId, status: Extract<TurnStatus, "blocked" | "failed" | "interrupted">): void;
    recordCompaction(threadId: ThreadId, input: {
        estimatedTokensAfter?: number;
        estimatedTokensBefore?: number;
        reason: ThreadCompactionReason;
        runtime?: ThreadRuntimeSample;
    }): ThreadUsageDelta;
    recordMemoryRefresh(threadId: ThreadId, runtime?: ThreadRuntimeSample): ThreadUsageDelta;
    recordProjectMemoryCandidateFailure(threadId: ThreadId, message: string, failedAt?: string): void;
    estimatePromptTokens(threadId: ThreadId, input?: {
        activeSkillIds?: string[];
        includeHandoffPrompt?: boolean;
        maxContextTokens?: number;
        projectMemoryPrompt?: string;
    }): Promise<number>;
    planConversationWindow(threadId: ThreadId, input?: {
        activeSkillIds?: string[];
        includeHandoffPrompt?: boolean;
        maxContextTokens?: number;
        projectMemoryPrompt?: string;
        systemPrompt?: string;
    }): Promise<ThreadCompactionProbeResult>;
    probeCompactionNeed(threadId: ThreadId, input?: {
        activeSkillIds?: string[];
        includeHandoffPrompt?: boolean;
        maxContextTokens?: number;
        projectMemoryPrompt?: string;
        systemPrompt?: string;
    }): Promise<ThreadCompactionProbeResult>;
    prepareUpdate(threadId: ThreadId, input?: ThreadMemoryUpdateInput): Promise<ThreadMemoryPreparedUpdate>;
    applyPreparedUpdate(threadId: ThreadId, prepared: ThreadMemoryPreparedUpdate, input?: {
        forceCommit?: boolean;
    }): Promise<ThreadMemoryUpdateResult>;
    update(threadId: ThreadId, input?: ThreadMemoryUpdateInput): Promise<ThreadMemoryUpdateResult>;
    compact(threadId: ThreadId, input?: {
        activeSkillIds?: string[];
        narrativeSummarizer?: NarrativeSummarizer;
    }): Promise<ThreadMemoryUpdateResult>;
    get(threadId: ThreadId): Promise<ThreadMemorySnapshot | null>;
    getHandoff(threadId: ThreadId): Promise<ThreadHandoffSnapshot | null>;
    getWorkingMemory(threadId: ThreadId): Promise<ThreadWorkingMemorySnapshot | null>;
    getDiagnostics(threadId: ThreadId): Promise<ThreadDiagnostics>;
    buildConversation(threadId: ThreadId, input?: {
        activeSkillIds?: string[];
        includeHandoffPrompt?: boolean;
        maxContextTokens?: number;
        preparedUpdate?: ThreadMemoryPreparedUpdate;
        projectMemoryPrompt?: string;
        recentItemLimit?: number;
        recentMessageLimit?: number;
    }): Promise<ThreadConversationBuildResult>;
}
export declare function extractSegmentFacts(items: Item[]): StructuredFacts;
//# sourceMappingURL=thread-memory.d.ts.map