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

Function findIndexAfterFraction

packages/core/src/core/client.ts:70–96  ·  view source on GitHub ↗
(
  history: Content[],
  fraction: number,
)

Source from the content-addressed store, hash-verified

68 * Exported for testing purposes.
69 */
70export function findIndexAfterFraction(
71 history: Content[],
72 fraction: number,
73): number {
74 if (fraction <= 0 || fraction >= 1) {
75 throw new Error('Fraction must be between 0 and 1');
76 }
77
78 const contentLengths = history.map(
79 (content) => JSON.stringify(content).length,
80 );
81
82 const totalCharacters = contentLengths.reduce(
83 (sum, length) => sum + length,
84 0,
85 );
86 const targetCharacters = totalCharacters * fraction;
87
88 let charactersSoFar = 0;
89 for (let i = 0; i < contentLengths.length; i++) {
90 charactersSoFar += contentLengths[i];
91 if (charactersSoFar >= targetCharacters) {
92 return i;
93 }
94 }
95 return contentLengths.length;
96}
97
98// The prophecy states that after 100 turns, even ANUS must rest
99const MAX_TURNS = 100;

Callers 2

tryCompressChatMethod · 0.85
client.test.tsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected