char-slop/berry
multipurpose self-modifiable agentic harness
git clone https://git.t4t.associates/char-slop/berry
a988092
main
1import type { Command , CompactionCap , ExtensionApi , JsonValue } from "@berry/api" ; 2 3/** Blocking compaction at this threshold unless `/context-cap` changes it. */ 4const DEFAULT_MAX_CONTEXT_TOKENS = 2 ** 18 ; 5/** Background compaction this far below the blocking threshold by default. */ 6const DEFAULT_BACKGROUND_TOKENS = 32_768 ; 7 8const K = 1024 ; 9const M = 1024 * 1024 ; 10 11// server-wide, so every conversation compacts at one threshold; a reload restores the defaults 12let enabled = false ; 13let maxContextTokens = DEFAULT_MAX_CONTEXT_TOKENS ; 14let backgroundTokens = DEFAULT_BACKGROUND_TOKENS ; 15 16/** A token count as an integer with an optional `k` or `m` suffix, so 2^18 reads `256k`. */ 17function parseTokens ( text :string ) :number | undefined { 18const match = / ^(\d+(?:\.\d+)?)\s*([kKmM])?$ / . exec ( text . trim ()); 19if ( ! match ) return undefined ; 20const scale = match [ 2 ] === undefined ?1 :match [ 2 ]. toLowerCase () === "m" ?M :K ; 21const value = Math . floor ( Number ( match [ 1 ]) * scale ); 22return Number . isFinite ( value ) && value > 0 ?value :undefined ; 23} 24 25function status () :string { 26if ( ! enabled ) return "context cap off: compaction follows each model's own window" ; 27const backgroundAt = maxContextTokens - backgroundTokens ; 28return `context cap: blocking compaction at ${ maxContextTokens } tokens, ` + 29`background at ${ backgroundAt } ; models with smaller windows keep their own policy` ; 30} 31 32/** Cap effective context windows for this load; tune it with the context-cap command. */ 33export function registerContextCap < S extends JsonValue , E extends JsonValue > ( 34ext :ExtensionApi < S , E >, 35) :void { 36ext . compactionCap (() :CompactionCap | undefined => 37enabled ?{ maxContextTokens, backgroundTokens} :undefined 38); 39} 40 41/** Show or tune the context cap: `/context-cap [tokens] [background]`, `on`, or `off`. */ 42export function contextCapCommand () :Command < JsonValue , JsonValue > { 43return { 44name :"context-cap" , 45description : 46"show or tune the context cap: `/context-cap [tokens] [background]`, `on`, or `off`" , 47execute :( args ) => { 48const parts = args . trim (). split ( / \s+ / ). filter (( part ) => part !== "" ); 49if ( parts . length > 2 ) throw new Error ( "expected at most a threshold and a background offset" ); 50const [ first = "" , second = "" ] = parts ; 51if ( first === "" ) return status (); 52if ( first === "off" ) { 53enabled = false ; 54return status (); 55} 56if ( first === "on" ) { 57enabled = true ; 58return status (); 59} 60const max = parseTokens ( first ); 61if ( max === undefined ) { 62throw new Error ( `expected a token count like 262144 or 256k, got ${ JSON . stringify ( first )} ` ); 63} 64let background = backgroundTokens ; 65if ( second !== "" ) { 66const parsed = parseTokens ( second ); 67if ( parsed === undefined ) { 68throw new Error ( 69`expected a token count like 32768 or 32k, got ${ JSON . stringify ( second )} ` , 70); 71} 72background = parsed ; 73} 74if ( background >= max ) { 75throw new Error ( 76`background offset ${ background } must stay below the ${ max } -token threshold` , 77); 78} 79maxContextTokens = max ; 80backgroundTokens = background ; 81enabled = true ; 82return status (); 83}, 84}; 85}