char-slop/berry
multipurpose self-modifiable agentic harness
git clone https://git.t4t.associates/char-slop/berry
bcca6b5
main
1import { defineExtension } from "@berry/api" ; 2import { type Static , Type } from "@earendil-works/pi-ai" ; 3 4const parameters = Type . Object ({ 5sort :Type . Union ([ 6Type . Literal ( "default" ), 7Type . Literal ( "throughput" ), 8Type . Literal ( "latency" ), 9Type . Literal ( "price" ), 10], { 11title :"Sort providers" , 12description :"throughput is Nitro routing; default preserves the model's routing." , 13default :"default" , 14}), 15only :Type . String ({ 16title :"Allowed providers" , 17description : 18"Comma-separated endpoint tags, e.g. baseten/fast. Blank preserves model defaults." , 19default :"" , 20}), 21ignore :Type . String ({ 22title :"Excluded providers" , 23description :"Comma-separated provider or endpoint tags." , 24default :"" , 25}), 26maxPromptPrice :Type . String ({ 27title :"Maximum input price ($/M tokens)" , 28description :"Nonnegative USD per million tokens; blank preserves model defaults." , 29default :"" , 30}), 31maxCompletionPrice :Type . String ({ 32title :"Maximum output price ($/M tokens)" , 33description :"Nonnegative USD per million tokens; blank preserves model defaults." , 34default :"" , 35}), 36allowFallbacks :Type . Union ([ 37Type . Literal ( "default" ), 38Type . Literal ( "yes" ), 39Type . Literal ( "no" ), 40], { 41title :"Allow provider fallbacks" , 42description :"Fallbacks still have to satisfy the allowlist, exclusions, and price caps." , 43default :"default" , 44}), 45}, { additionalProperties :false }); 46 47type State = Static < typeof parameters >; 48type Event = { type :"configured" ; values :State }; 49type Routing = { 50sort ?:"throughput" | "latency" | "price" ; 51only ?:string []; 52ignore ?:string []; 53max_price ?:{ prompt ?:number ; completion ?:number }; 54allow_fallbacks ?:boolean ; 55}; 56 57/** The editable fields, since an event recorded before one was dropped still carries it. */ 58function settings ( state :State ) :State { 59return { 60sort :state . sort , 61only :state . only , 62ignore :state . ignore , 63maxPromptPrice :state . maxPromptPrice , 64maxCompletionPrice :state . maxCompletionPrice , 65allowFallbacks :state . allowFallbacks , 66}; 67} 68 69function routing ( values :State ) :Routing { 70const result :Routing = {}; 71if ( values . sort !== "default" ) result . sort = values . sort ; 72for ( const key of [ "only" , "ignore" ] as const ) { 73const tags = [ 74 ...new Set ( 75values [ key ]. split ( "," ). map (( tag ) => tag . trim ()). filter ( Boolean ), 76), 77]; 78if ( tags . length ) result [ key ] = tags ; 79} 80for ( 81const [ field , key ] of [ 82[ "maxPromptPrice" , "prompt" ], 83[ "maxCompletionPrice" , "completion" ], 84] as const 85) { 86const text = values [ field ]. trim (); 87if ( ! text ) continue ; 88const price = Number ( text ); 89if ( ! /^(?:\d+(?:\.\d*)?|\.\d+)$ / . test ( text ) || ! Number . isFinite ( price )) { 90throw new Error ( 91` ${ 92key === "prompt" ? "Input" : "Output" 93} price must be a nonnegative decimal in $/M tokens.` , 94); 95} 96( result . max_price ??= {})[ key ] = price ; 97} 98if ( values . allowFallbacks !== "default" ) { 99result . allow_fallbacks = values . allowFallbacks === "yes" ; 100} 101return result ; 102} 103 104function isObject ( value :unknown ) :value isRecord < string , unknown > { 105return typeof value === "object" && value !== null && ! Array . isArray ( value ); 106} 107 108export default defineExtension < State , Event >({ 109name :"openrouter-routing" , 110description :"per-conversation OpenRouter provider routing and price limits" , 111alwaysOn :true , 112sortOrder :- 1000 , 113initial :() => ({ 114sort :"default" , 115only :"" , 116ignore :"" , 117maxPromptPrice :"" , 118maxCompletionPrice :"" , 119allowFallbacks :"default" , 120}), 121apply :( state , event ) => 122event . type === "configured" ?settings ({ ...state , ...event . values }) :state , 123setup ( ext ) { 124ext . configuration ({ 125 parameters, 126values :( state ) => settings ( state ), 127events ( values , state ) { 128routing ( values ); 129return Object . keys ( values ). every (( key ) => 130values [ key as keyof State ] === state [ key as keyof State ] 131) 132 ?[] 133 :[{ type :"configured" , values}]; 134}, 135}); 136return ( conv ) => { 137const requirements = routing ( settings ( conv . state )); 138if ( ! Object . keys ( requirements ). length ) return ; 139conv . onPayload (( payload , model ) => { 140if ( model . provider !== "openrouter" || ! isObject ( payload )) return ; 141const existing = isObject ( payload . provider ) ?payload . provider :{}; 142const provider = { ...existing , ...requirements }; 143if ( requirements . max_price ) { 144provider . max_price = { 145 ...( isObject ( existing . max_price ) ?existing . max_price :{}), 146 ...requirements . max_price , 147}; 148} 149return { ...payload , provider}; 150}); 151}; 152}, 153});