MCPcopy Create free account
hub / github.com/WJX20/claude-code / partialCompactConversation

Function partialCompactConversation

src/services/compact/compact.ts:774–1108  ·  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

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