MCPcopy Create free account
hub / github.com/Noumena-Network/code / partialCompactConversation

Function partialCompactConversation

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

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

Callers

nothing calls this directly

Calls 15

tokenCountWithEstimationFunction · 0.85
executePreCompactHooksFunction · 0.85
getPartialCompactPromptFunction · 0.85
streamCompactSummaryFunction · 0.85
getAssistantMessageTextFunction · 0.85
truncateHeadForPTLRetryFunction · 0.85
startsWithApiErrorPrefixFunction · 0.85
cacheToObjectFunction · 0.85

Tested by

no test coverage detected