MCPcopy
hub / github.com/codeaashu/claude-code / partialCompactConversation

Function partialCompactConversation

src/services/compact/compact.ts:772–1106  ·  view source on GitHub ↗
(
  allMessages: Message[],
  pivotIndex: number,
  context: ToolUseContext,
  cacheSafeParams: CacheSafeParams,
  userFeedback?: string,
  direction: PartialCompactDirection = 'from',
)

Source from the content-addressed store, hash-verified

770 * Prompt cache is invalidated since the summary precedes the kept messages.
771 */
772export async function partialCompactConversation(
773 allMessages: Message[],
774 pivotIndex: number,
775 context: ToolUseContext,
776 cacheSafeParams: CacheSafeParams,
777 userFeedback?: string,
778 direction: PartialCompactDirection = 'from',
779): Promise<CompactionResult> {
780 try {
781 const messagesToSummarize =
782 direction === 'up_to'
783 ? allMessages.slice(0, pivotIndex)
784 : allMessages.slice(pivotIndex)
785 // 'up_to' must strip old compact boundaries/summaries: for 'up_to',
786 // summary_B sits BEFORE kept, so a stale boundary_A in kept wins
787 // findLastCompactBoundaryIndex's backward scan and drops summary_B.
788 // 'from' keeps them: summary_B sits AFTER kept (backward scan still
789 // works), and removing an old summary would lose its covered history.
790 const messagesToKeep =
791 direction === 'up_to'
792 ? allMessages
793 .slice(pivotIndex)
794 .filter(
795 m =>
796 m.type !== 'progress' &&
797 !isCompactBoundaryMessage(m) &&
798 !(m.type === 'user' && m.isCompactSummary),
799 )
800 : allMessages.slice(0, pivotIndex).filter(m => m.type !== 'progress')
801
802 if (messagesToSummarize.length === 0) {
803 throw new Error(
804 direction === 'up_to'
805 ? 'Nothing to summarize before the selected message.'
806 : 'Nothing to summarize after the selected message.',
807 )
808 }
809
810 const preCompactTokenCount = tokenCountWithEstimation(allMessages)
811
812 context.onCompactProgress?.({
813 type: 'hooks_start',
814 hookType: 'pre_compact',
815 })
816
817 context.setSDKStatus?.('compacting')
818 const hookResult = await executePreCompactHooks(
819 {
820 trigger: 'manual',
821 customInstructions: null,
822 },
823 context.abortController.signal,
824 )
825
826 // Merge hook instructions with user feedback
827 let customInstructions: string | undefined
828 if (hookResult.newCustomInstructions && userFeedback) {
829 customInstructions = `${hookResult.newCustomInstructions}\n\nUser context: ${userFeedback}`

Callers 1

REPLFunction · 0.85

Calls 15

isCompactBoundaryMessageFunction · 0.85
tokenCountWithEstimationFunction · 0.85
executePreCompactHooksFunction · 0.85
getPartialCompactPromptFunction · 0.85
createUserMessageFunction · 0.85
streamCompactSummaryFunction · 0.85
getAssistantMessageTextFunction · 0.85
truncateHeadForPTLRetryFunction · 0.85
logEventFunction · 0.85
startsWithApiErrorPrefixFunction · 0.85
cacheToObjectFunction · 0.85

Tested by

no test coverage detected