feat: 添加 compact 缓存与上下文压缩增强

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
unraid
2026-04-22 22:38:09 +08:00
parent be97a0b010
commit 6c5df395c3
4 changed files with 240 additions and 41 deletions

View File

@@ -1,6 +1,3 @@
// Auto-generated stub — replace with real implementation
export {};
export type CachedMCState = {
registeredTools: Set<string>
toolOrder: string[]
@@ -19,19 +16,97 @@ export type PinnedCacheEdits = {
block: CacheEditsBlock
}
export const isCachedMicrocompactEnabled: () => boolean = () => false;
export const isModelSupportedForCacheEditing: (model: string) => boolean = () => false;
export const getCachedMCConfig: () => { triggerThreshold: number; keepRecent: number } = () => ({ triggerThreshold: 0, keepRecent: 0 });
export const createCachedMCState: () => CachedMCState = () => ({
registeredTools: new Set(),
toolOrder: [],
deletedRefs: new Set(),
pinnedEdits: [],
toolsSentToAPI: false,
});
export const markToolsSentToAPI: (state: CachedMCState) => void = () => {};
export const resetCachedMCState: (state: CachedMCState) => void = () => {};
export const registerToolResult: (state: CachedMCState, toolId: string) => void = () => {};
export const registerToolMessage: (state: CachedMCState, groupIds: string[]) => void = () => {};
export const getToolResultsToDelete: (state: CachedMCState) => string[] = () => [];
export const createCacheEditsBlock: (state: CachedMCState, toolIds: string[]) => CacheEditsBlock | null = () => null;
const TRIGGER_THRESHOLD = 10
const KEEP_RECENT = 5
/**
* Returns true when the CLAUDE_CACHED_MICROCOMPACT env var is set to '1'
* or the feature is explicitly enabled.
*/
export function isCachedMicrocompactEnabled(): boolean {
return process.env.CLAUDE_CACHED_MICROCOMPACT === '1'
}
/**
* Returns true for Claude 4.x models that support cache_edits.
*/
export function isModelSupportedForCacheEditing(model: string): boolean {
return /claude-[a-z]+-4[-\d]/.test(model)
}
export function getCachedMCConfig(): {
triggerThreshold: number
keepRecent: number
} {
return { triggerThreshold: TRIGGER_THRESHOLD, keepRecent: KEEP_RECENT }
}
export function createCachedMCState(): CachedMCState {
return {
registeredTools: new Set(),
toolOrder: [],
deletedRefs: new Set(),
pinnedEdits: [],
toolsSentToAPI: false,
}
}
export function markToolsSentToAPI(state: CachedMCState): void {
state.toolsSentToAPI = true
}
export function resetCachedMCState(state: CachedMCState): void {
state.registeredTools.clear()
state.toolOrder = []
state.deletedRefs.clear()
state.pinnedEdits = []
state.toolsSentToAPI = false
}
export function registerToolResult(state: CachedMCState, toolId: string): void {
if (!state.registeredTools.has(toolId)) {
state.registeredTools.add(toolId)
state.toolOrder.push(toolId)
}
}
export function registerToolMessage(
state: CachedMCState,
groupIds: string[],
): void {
for (const id of groupIds) {
registerToolResult(state, id)
}
}
/**
* Returns the tool IDs that should be deleted (oldest first) to bring
* the count below the threshold, excluding already-deleted tools and
* the most recently seen ones.
*/
export function getToolResultsToDelete(state: CachedMCState): string[] {
const { triggerThreshold, keepRecent } = getCachedMCConfig()
const active = state.toolOrder.filter(id => !state.deletedRefs.has(id))
if (active.length <= triggerThreshold) return []
// Keep the last keepRecent tools
const toDelete = active.slice(0, active.length - keepRecent)
return toDelete
}
/**
* Creates a cache_edits block that deletes the given tool result IDs.
* Returns null if toolIds is empty.
*/
export function createCacheEditsBlock(
state: CachedMCState,
toolIds: string[],
): CacheEditsBlock | null {
if (toolIds.length === 0) return null
return {
type: 'cache_edits',
edits: toolIds.map(id => ({
type: 'delete_tool_result',
tool_use_id: id,
})),
}
}