Download src/services/compact/cachedMicrocompact.ts from chenbhao/codev: direct link, hf CLI and curl.
- Browser
- Download file 2.31 kB
-
https://huggingface.co/chenbhao/codev/resolve/main/src/services/compact/cachedMicrocompact.ts
- Command line
-
hf download hf://chenbhao/codev/src/services/compact/cachedMicrocompact.ts
-
curl -L -o cachedMicrocompact.ts https://huggingface.co/chenbhao/codev/resolve/main/src/services/compact/cachedMicrocompact.ts
2.31 kB
| import { | |
| getCachedMCConfig, | |
| type CachedMCConfig, | |
| } from './cachedMCConfig.js' | |
| export type CacheEditsBlock = { | |
| type: 'cache_edits' | |
| edits: { type: 'delete'; cache_reference: string }[] | |
| } | |
| export type PinnedCacheEdits = { | |
| userMessageIndex: number | |
| block: CacheEditsBlock | |
| } | |
| export type CachedMCState = { | |
| pinnedEdits: PinnedCacheEdits[] | |
| registeredTools: Set<string> | |
| toolOrder: string[] | |
| deletedRefs: Set<string> | |
| } | |
| export function createCachedMCState(): CachedMCState { | |
| return { | |
| pinnedEdits: [], | |
| registeredTools: new Set(), | |
| toolOrder: [], | |
| deletedRefs: new Set(), | |
| } | |
| } | |
| export function isCachedMicrocompactEnabled(): boolean { | |
| return getCachedMCConfig().enabled | |
| } | |
| export function isModelSupportedForCacheEditing(model: string): boolean { | |
| return getCachedMCConfig().supportedModels.some(pattern => | |
| model.includes(pattern), | |
| ) | |
| } | |
| export { getCachedMCConfig } | |
| export type { CachedMCConfig } | |
| export function registerToolResult( | |
| state: CachedMCState, | |
| toolUseId: string, | |
| ): void { | |
| if (state.registeredTools.has(toolUseId)) { | |
| return | |
| } | |
| state.registeredTools.add(toolUseId) | |
| state.toolOrder.push(toolUseId) | |
| } | |
| export function registerToolMessage( | |
| _state: CachedMCState, | |
| _toolUseIds: string[], | |
| ): void {} | |
| export function getToolResultsToDelete(state: CachedMCState): string[] { | |
| const config = getCachedMCConfig() | |
| const activeRefs = state.toolOrder.filter(id => !state.deletedRefs.has(id)) | |
| if (!config.enabled || activeRefs.length < config.triggerThreshold) { | |
| return [] | |
| } | |
| return activeRefs.slice(0, Math.max(0, activeRefs.length - config.keepRecent)) | |
| } | |
| export function createCacheEditsBlock( | |
| state: CachedMCState, | |
| toolUseIds: string[], | |
| ): CacheEditsBlock | null { | |
| const edits = toolUseIds | |
| .filter(id => !state.deletedRefs.has(id)) | |
| .map(id => { | |
| state.deletedRefs.add(id) | |
| return { | |
| type: 'delete' as const, | |
| cache_reference: id, | |
| } | |
| }) | |
| if (edits.length === 0) { | |
| return null | |
| } | |
| return { | |
| type: 'cache_edits', | |
| edits, | |
| } | |
| } | |
| export function markToolsSentToAPI(_state: CachedMCState): void {} | |
| export function resetCachedMCState(state: CachedMCState): void { | |
| state.pinnedEdits.length = 0 | |
| state.registeredTools.clear() | |
| state.toolOrder.length = 0 | |
| state.deletedRefs.clear() | |
| } | |