pi-context-management 0.6.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/text.ts ADDED
@@ -0,0 +1,27 @@
1
+ // UTF-8 bytes are deliberately used as conservative token units, not a tokenizer.
2
+ export function byteCost(text: string): number { return Buffer.byteLength(text, 'utf8'); }
3
+
4
+ export function fitText(text: string, budget: number): string {
5
+ if (!Number.isFinite(budget) || budget <= 0) return '';
6
+ const bytes = Buffer.from(text, 'utf8');
7
+ if (bytes.length <= budget) return text;
8
+ let end = Math.floor(budget);
9
+ while (end > 0 && (bytes[end]! & 0xc0) === 0x80) end--;
10
+ return bytes.subarray(0, end).toString('utf8');
11
+ }
12
+
13
+ export const MAX_NOTES_BYTES = 8000;
14
+ export function notesBudget(contextWindow: number): number {
15
+ return Math.max(0, Math.floor(Math.min(MAX_NOTES_BYTES, contextWindow * 0.05)));
16
+ }
17
+
18
+ export function checkpointBudget(contextWindow: number): number {
19
+ return Math.max(0, Math.floor(Math.min(16384, contextWindow * 0.1)));
20
+ }
21
+
22
+ export function recallBudget(usage: { tokens: number | null; contextWindow: number } | undefined): number {
23
+ if (!usage || usage.tokens === null || !Number.isFinite(usage.tokens) || usage.contextWindow <= 0) return 0;
24
+ // Reserve response/reasoning space, other extensions and estimation error.
25
+ const reserve = Math.max(16384, usage.contextWindow * 0.2);
26
+ return Math.max(0, Math.floor(Math.min(4000, usage.contextWindow * 0.05, usage.contextWindow - usage.tokens - reserve)));
27
+ }