MCPcopy Create free account
hub / github.com/anus-dev/ANUS / tryCompressChat

Method tryCompressChat

packages/core/src/core/client.ts:764–864  ·  view source on GitHub ↗
(
    prompt_id: string,
    force: boolean = false,
  )

Source from the content-addressed store, hash-verified

762 }
763
764 async tryCompressChat(
765 prompt_id: string,
766 force: boolean = false,
767 ): Promise<ChatCompressionInfo | null> {
768 const curatedHistory = this.getChat().getHistory(true);
769
770 // Regardless of `force`, don't do anything if the history is empty.
771 if (curatedHistory.length === 0) {
772 return null;
773 }
774
775 const model = this.config.getModel();
776
777 const { totalTokens: originalTokenCount } =
778 await this.getContentGenerator().countTokens({
779 model,
780 contents: curatedHistory,
781 });
782 if (originalTokenCount === undefined) {
783 console.warn(`Could not determine token count for model ${model}.`);
784 return null;
785 }
786
787 const contextPercentageThreshold =
788 this.config.getChatCompression()?.contextPercentageThreshold;
789
790 // Don't compress if not forced and we are under the limit.
791 if (!force) {
792 const threshold =
793 contextPercentageThreshold ?? COMPRESSION_TOKEN_THRESHOLD;
794 if (originalTokenCount < threshold * tokenLimit(model)) {
795 return null;
796 }
797 }
798
799 let compressBeforeIndex = findIndexAfterFraction(
800 curatedHistory,
801 1 - COMPRESSION_PRESERVE_THRESHOLD,
802 );
803 // Find the first user message after the index. This is the start of the next turn.
804 while (
805 compressBeforeIndex < curatedHistory.length &&
806 (curatedHistory[compressBeforeIndex]?.role === 'model' ||
807 isFunctionResponse(curatedHistory[compressBeforeIndex]))
808 ) {
809 compressBeforeIndex++;
810 }
811
812 const historyToCompress = curatedHistory.slice(0, compressBeforeIndex);
813 const historyToKeep = curatedHistory.slice(compressBeforeIndex);
814
815 this.getChat().setHistory(historyToCompress);
816
817 const { text: summary } = await this.getChat().sendMessage(
818 {
819 message: {
820 text: 'First, reason in your scratchpad. Then, generate the <state_snapshot>.',
821 },

Callers 3

sendMessageStreamMethod · 0.95
client.test.tsFile · 0.80
compressCommand.tsFile · 0.80

Calls 15

getChatMethod · 0.95
getContentGeneratorMethod · 0.95
startChatMethod · 0.95
tokenLimitFunction · 0.85
findIndexAfterFractionFunction · 0.85
isFunctionResponseFunction · 0.85
getCompressionPromptFunction · 0.85
logChatCompressionFunction · 0.85
makeChatCompressionEventFunction · 0.85
getModelMethod · 0.80
getChatCompressionMethod · 0.80
sendMessageMethod · 0.80

Tested by

no test coverage detected