UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

97 lines • 4.18 kB
/** * Deterministic, no-LLM context reductions applied before compaction summarization. * * Ported from plugsuits' `context-collapse` and `micro-compact` patterns and * adapted to the senpi `AgentMessage` shape. Three independent transforms: * * 1. {@link collapseConsecutiveToolResults} — runs of same-kind read/grep/shell * tool result payloads are replaced with a single one-line label so the * summarizer pays for the shape, not for the bytes. * 2. {@link microCompactAssistantText} — older long assistant text answers are * truncated and tagged with a `[response shrunk]` marker. * 3. {@link clearOldToolResults} — keep the last N tool results in full, replace * older clearable tool result content with `[tool result cleared]`. * * Each transform is pure (`messages` in → new array out, no in-place mutation * beyond freshly cloned messages) and returns aggregated token-savings stats. * * {@link reduceContextMessages} composes the three transforms in order. */ import type { AgentMessage } from "@earendil-works/pi-agent-core"; export type CollapsedGroupKind = "read" | "search" | "shell"; export interface CollapsedGroup { type: CollapsedGroupKind; count: number; label: string; originalTokens: number; collapsedTokens: number; } export interface CollapseConsecutiveOptions { minGroupSize?: number; protectRecentMessages?: number; readToolNames?: string[]; searchToolNames?: string[]; shellToolNames?: string[]; } export interface CollapseConsecutiveResult { messages: AgentMessage[]; groups: CollapsedGroup[]; tokensSaved: number; } export interface MicroCompactAssistantOptions { protectRecentTokens?: number; maxAssistantTextTokens?: number; minSavingsTokens?: number; replacementTemplate?: string; } export interface MicroCompactAssistantResult { messages: AgentMessage[]; tokensSaved: number; messagesModified: number; } export interface ClearOldToolResultsOptions { keepRecent?: number; clearableToolNames?: string[]; replacementText?: string; } export interface ClearOldToolResultsResult { messages: AgentMessage[]; tokensSaved: number; toolResultsCleared: number; } export interface ReduceContextOptions { collapse?: false | CollapseConsecutiveOptions; shrinkAssistant?: false | MicroCompactAssistantOptions; clearToolResults?: false | ClearOldToolResultsOptions; } export interface ReduceContextResult { messages: AgentMessage[]; tokensSaved: number; groupsCollapsed: number; messagesShrunk: number; toolResultsCleared: number; } /** * Default options passed to {@link reduceContextMessages} when the builtin * compaction extension's `context` hook decides to run a reduction pass. * * Each value is chosen to be strictly more conservative than the corresponding * plugsuits default — protect more of the recent tail, raise the per-message * shrink threshold, and keep more recent tool results intact — so a single * shared default is safe to apply across normal coding sessions without making * targeted reductions less effective. */ export declare const BUILTIN_CONTEXT_REDUCTION_OPTIONS: ReduceContextOptions; export declare const BUILTIN_CONTEXT_REDUCTION_GATE_RATIO = 0.5; export interface ShouldApplyContextReductionInput { usageTokens: number | null; contextWindow: number; gateRatio?: number; isProviderNativeCompactionPath?: boolean; } export declare function shouldApplyContextReduction(input: ShouldApplyContextReductionInput): boolean; export declare function collapseConsecutiveToolResults(messages: AgentMessage[], options?: CollapseConsecutiveOptions): CollapseConsecutiveResult; export declare function microCompactAssistantText(messages: AgentMessage[], options?: MicroCompactAssistantOptions): MicroCompactAssistantResult; export declare function clearOldToolResults(messages: AgentMessage[], options?: ClearOldToolResultsOptions): ClearOldToolResultsResult; export declare function reduceContextMessages(messages: AgentMessage[], options?: ReduceContextOptions): ReduceContextResult; //# sourceMappingURL=context-reduction.d.ts.map