import type { CompactionEntry, CompactionResult } from "@earendil-works/pi-coding-agent"; export const NATIVE_COMPACTION_STRATEGY = "openai-responses-compaction-v2"; export const NATIVE_COMPACTION_SHIM_SUMMARY = "[OpenAI native compaction checkpoint]"; export type NativeCompactionStrategy = typeof NATIVE_COMPACTION_STRATEGY; export type NativeCompactionRequestMeta = { tokensBefore?: number; previousSummaryPresent?: boolean; compactedKeptWindow?: boolean; }; export type NativeCompactionUsage = { inputTokens: number; cachedInputTokens: number; cacheWriteInputTokens: number; outputTokens: number; }; export type NativeCompactionIdentity = { provider: string; api: string; model: string; baseUrl: string; }; export type NativeCompactionDetails = NativeCompactionIdentity & { strategy: NativeCompactionStrategy; compactedWindow: unknown[]; compactResponseId?: string; createdAt: string; requestMeta?: NativeCompactionRequestMeta; usage?: NativeCompactionUsage; }; export type NativeCompactionEntry = CompactionEntry; type CreateNativeCompactionDetailsInput = NativeCompactionIdentity & { compactedWindow: unknown[]; compactResponseId?: string; createdAt?: string; requestMeta?: NativeCompactionRequestMeta; usage?: NativeCompactionUsage; }; type CreateNativeCompactionShimResultInput = { firstKeptEntryId: string; tokensBefore: number; summary: string; details: NativeCompactionDetails; }; function isRecord(value: unknown): value is Record { return !!value && typeof value === "object" && !Array.isArray(value); } function isNonEmptyString(value: unknown): value is string { return typeof value === "string" && value.trim().length > 0; } function isFiniteNonNegativeNumber(value: unknown): value is number { return typeof value === "number" && Number.isFinite(value) && value >= 0; } function normalizeString(value: string): string { return value.trim(); } function isStructuredValue(value: unknown): boolean { if (value === null || typeof value === "string" || typeof value === "number" || typeof value === "boolean") { return true; } if (Array.isArray(value)) { return value.every(isStructuredValue); } if (isRecord(value)) { return Object.values(value).every(isStructuredValue); } return false; } function cloneStructuredValue(value: unknown): unknown { if (value === null || typeof value === "string" || typeof value === "number" || typeof value === "boolean") { return value; } if (Array.isArray(value)) { return value.map(cloneStructuredValue); } if (isRecord(value)) { const clone: Record = {}; for (const [key, nested] of Object.entries(value)) { clone[key] = cloneStructuredValue(nested); } return clone; } throw new Error(`Unsupported structured value: ${typeof value}`); } function isCompactedWindowItem(value: unknown): value is Record { return isRecord(value) && Object.values(value).every(isStructuredValue); } function isNativeCompactionRequestMeta(value: unknown): value is NativeCompactionRequestMeta { if (!isRecord(value)) { return false; } const { tokensBefore, previousSummaryPresent, compactedKeptWindow } = value; if (tokensBefore !== undefined && !isFiniteNonNegativeNumber(tokensBefore)) { return false; } if (previousSummaryPresent !== undefined && typeof previousSummaryPresent !== "boolean") { return false; } if (compactedKeptWindow !== undefined && typeof compactedKeptWindow !== "boolean") return false; return true; } function isNativeCompactionIdentity(value: unknown): boolean { if (!isRecord(value)) return false; return ( isNonEmptyString(value.provider) && isNonEmptyString(value.api) && isNonEmptyString(value.model) && isNonEmptyString(value.baseUrl) ); } export function isNativeCompactionUsage(value: unknown): value is NativeCompactionUsage { if (!isRecord(value)) return false; return [value.inputTokens, value.cachedInputTokens, value.cacheWriteInputTokens, value.outputTokens].every( isFiniteNonNegativeNumber, ); } export function isNativeCompactionDetails(value: unknown): value is NativeCompactionDetails { if (!isRecord(value)) return false; const candidate = value; return ( candidate.strategy === NATIVE_COMPACTION_STRATEGY && isNativeCompactionIdentity(candidate) && Array.isArray(candidate.compactedWindow) && candidate.compactedWindow.every(isCompactedWindowItem) && isNonEmptyString(candidate.createdAt) && (candidate.compactResponseId === undefined || isNonEmptyString(candidate.compactResponseId)) && (candidate.requestMeta === undefined || isNativeCompactionRequestMeta(candidate.requestMeta)) && (candidate.usage === undefined || isNativeCompactionUsage(candidate.usage)) ); } export function isNativeCompactionEntry(value: unknown): value is NativeCompactionEntry { return isRecord(value) && value.type === "compaction" && isNativeCompactionDetails(value.details); } export function createNativeCompactionDetails(input: CreateNativeCompactionDetailsInput): NativeCompactionDetails { return { strategy: NATIVE_COMPACTION_STRATEGY, provider: normalizeString(input.provider), api: normalizeString(input.api), model: normalizeString(input.model), baseUrl: normalizeString(input.baseUrl), compactedWindow: input.compactedWindow.map((item) => cloneStructuredValue(item)), compactResponseId: isNonEmptyString(input.compactResponseId) ? normalizeString(input.compactResponseId) : undefined, createdAt: isNonEmptyString(input.createdAt) ? normalizeString(input.createdAt) : new Date().toISOString(), requestMeta: input.requestMeta ? { ...(input.requestMeta.tokensBefore !== undefined ? { tokensBefore: input.requestMeta.tokensBefore } : {}), ...(input.requestMeta.previousSummaryPresent !== undefined ? { previousSummaryPresent: input.requestMeta.previousSummaryPresent, } : {}), ...(input.requestMeta.compactedKeptWindow !== undefined ? { compactedKeptWindow: input.requestMeta.compactedKeptWindow } : {}), } : undefined, usage: input.usage ? { ...input.usage } : undefined, }; } export function createNativeCompactionShimResult( input: CreateNativeCompactionShimResultInput, ): CompactionResult { return { summary: input.summary, firstKeptEntryId: input.firstKeptEntryId, tokensBefore: input.tokensBefore, details: input.details, }; }