okami.mom/web-extract-go
git clone https://git.nkp.pet/okami.mom/web-extract-go
5a5f5bc
main
1// Serves Cloudflare's Clef-Flash decision model behind Jev's SystemOne API 2// (POST /systemone), so web-extract can use it by changing its base URL and key. 3// Workers AI bills Clef by input tokens; on the Workers Free plan the daily free 4// allocation is a hard cap; past it calls fail and web-extract uses its fallback 5// model (PUBLIC_JEV_FALLBACK_URL). 6 7interface Env { 8AI :Ai 9DECISION_KEY :string 10} 11 12const MAX_BODY = 200_000 13// Clef answers at most this many questions per call. 14const MAX_QUESTIONS = 64 15 16type ClefResult = { 17answers ?:Record < string , unknown > 18usage ?:{ input_tokens ?:number ; output_tokens ?:number } 19} 20 21async function sameSecret ( a :string , b :string ) :Promise < boolean > { 22const enc = new TextEncoder () 23const [ x , y ] = await Promise . all ([ 24crypto . subtle . digest ( 'SHA-256' , enc . encode ( a )), 25crypto . subtle . digest ( 'SHA-256' , enc . encode ( b )), 26]) 27return crypto . subtle . timingSafeEqual ( x , y ) 28} 29 30const error = ( status :number , message :string ) => Response . json ({ error :message }, { status}) 31 32export default { 33async fetch ( request :Request , env :Env ) :Promise < Response > { 34const url = new URL ( request . url ) 35if ( url . pathname !== '/systemone' ) return error ( 404 , 'not found' ) 36if ( request . method !== 'POST' ) return error ( 405 , 'use POST' ) 37const auth = request . headers . get ( 'authorization' ) ?? '' 38if ( ! env . DECISION_KEY || ! auth . startsWith ( 'Bearer ' ) || ! ( await sameSecret ( auth . slice ( 7 ), env . DECISION_KEY ))) 39return error ( 401 , 'invalid key' ) 40const text = await request . text () 41if ( text . length > MAX_BODY ) return error ( 413 , 'request too large' ) 42let body :{ state ?:unknown ; questions ?:unknown } 43try { 44body = JSON . parse ( text ) 45} catch { 46return error ( 400 , 'body must be JSON' ) 47} 48const questions = ( body . questions ?? {}) as Record < string , unknown > 49const ids = Object . keys ( questions ) 50const batches :string [][] = [] 51for ( let i = 0 ; i < ids . length ; i += MAX_QUESTIONS ) batches . push ( ids . slice ( i , i + MAX_QUESTIONS )) 52try { 53// Each batch carries the whole state, so a long call costs one state per batch. 54const results = await Promise . all ( 55batches . map ( 56( batch ) => 57env . AI . run ( '@cf/cloudflare/clef-flash' as keyof AiModels , { 58model :'clef-flash' , 59state :body . state , 60questions :Object . fromEntries ( batch . map (( id ) => [ id , questions [ id ]])), 61} as never ) as Promise < ClefResult >, 62), 63) 64const answers :Record < string , unknown > = {} 65const usage = { input_tokens :0 , output_tokens :0 } 66for ( const result of results ) { 67Object . assign ( answers , result . answers ) 68usage . input_tokens += result . usage ?. input_tokens ?? 0 69usage . output_tokens += result . usage ?. output_tokens ?? 0 70} 71return Response . json ({ model :'clef-flash' , answers, usage}) 72} catch ( e ) { 73const message = e instanceof Error ?e . message :String ( e ) 74// Out of daily allocation or capacity: web-extract falls back to its other model. 75const status = / 3036|3040|capacity|neuron|limit / i . test ( message ) ?429 :502 76return error ( status , message . slice ( 0 , 300 )) 77} 78}, 79}