okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
3cabb9e3f
main
1/** An isolated local API for connection and streaming verification. No external requests. */ 2import http from 'node:http' ; 3import { pathToFileURL } from 'node:url' ; 4export async function createMockModelApi ( port = 0 ) { 5const requests = []; 6const server = http . createServer ( async ( req , res ) => { 7const chunks = []; for await ( const chunk of req ) chunks . push ( chunk ); 8const body = chunks . length ?JSON . parse ( Buffer . concat ( chunks )) :null ; 9requests . push ({ path :req . url , headers :req . headers , body}); 10if ( req . url === '/redirect/models' ) { res . writeHead ( 302 , { location :'/v1/models' }); res . end (); return ; } 11if ( req . url . startsWith ( '/bad/' )) { res . writeHead ( 401 ); res . end ( 'secret-echo: test-key' ); return ; } 12if ( req . url . includes ( '/models' )) { 13res . setHeader ( 'content-type' , 'application/json' ); 14const paginated = req . url . includes ( 'limit=100' ); 15res . end ( JSON . stringify ({ data :req . url . includes ( 'after_id=' ) ?[{ id :'second-model' , display_name :'Second model' }] :[{ id :'mock-chat' , name :'Test chat model' , context_length :32768 }, { id :'mock-reasoning' , name :'Test reasoning model' , context_length :65536 , input_modalities :[ 'text' ], supported_parameters :[ 'reasoning' ] }], ...( paginated ?{ has_more :! req . url . includes ( 'after_id=' ), last_id :'mock-reasoning' } :{}) })); return ; 16} 17if ( req . url === '/props' ) { res . setHeader ( 'content-type' , 'application/json' ); res . end ( JSON . stringify ({ modalities :{ vision :false } })); return ; } 18res . writeHead ( 200 , { 'content-type' :'text/event-stream' }); 19const send = value => res . write ( `data: ${ JSON . stringify ( value )} \r\n\r\n` ); 20if ( body . model === 'truncated' ) { send ({ choices :[{ index :0 , delta :{ content :'Partial' } }] }); res . end (); return ; } 21if ( body . model === 'stream-error' ) { send ({ error :{ message :'secret test-key' } }); res . end (); return ; } 22if ( body . model === 'slow' ) { send ({ choices :[{ index :0 , delta :{ content :'Partial' } }] }); const timer = setInterval (() => res . write ( ': ping\n\n' ), 500 ); res . on ( 'close' , () => clearInterval ( timer )); return ; } 23if ( req . url . endsWith ( '/messages' )) { 24send ({ type :'message_start' , message :{ usage :{ input_tokens :14 , output_tokens :0 } } }); 25send ({ type :'content_block_delta' , delta :{ type :'thinking_delta' , thinking :'Thinking.' } }); 26send ({ type :'content_block_delta' , delta :{ type :'text_delta' , text :'Hello from the test API.' } }); 27send ({ type :'message_delta' , usage :{ output_tokens :7 } }); send ({ type :'message_stop' }); 28} else if ( req . url . endsWith ( '/responses' )) { 29send ({ type :'response.reasoning_summary_text.delta' , delta :'Thinking.' }); 30send ({ type :'response.output_text.delta' , delta :'Hello from the test API.' }); 31send ({ type :'response.completed' , response :{ usage :{ input_tokens :14 , output_tokens :7 } } }); 32} else { 33// Divide a UTF-8 character and SSE event across network writes. 34const bytes = Buffer . from ( 'data: ' + JSON . stringify ({ choices :[{ index :0 , delta :{ content :'Hello ✓ from the test API.' } }] }) + '\r\n\r\n' ); 35const cut = bytes . indexOf ( Buffer . from ( '✓' )) + 1 ; 36res . write ( bytes . subarray ( 0 , cut )); res . write ( bytes . subarray ( cut )); 37send ({ choices :[{ index :0 , delta :{}, finish_reason :'stop' }], usage :{ prompt_tokens :14 , completion_tokens :7 } }); 38res . write ( 'data: [DONE]\r\n\r\n' ); 39} 40res . end (); 41}); 42await new Promise ( resolve => server . listen ( port , '127.0.0.1' , resolve )); 43return { server, requests, baseUrl :`http://127.0.0.1: ${ server . address (). port } /v1` , close :() => new Promise ( resolve => { server . close ( resolve ); server . closeAllConnections (); }) }; 44} 45if ( process . argv [ 1 ] && import . meta. url === pathToFileURL ( process . argv [ 1 ]). href ) { 46const mock = await createMockModelApi ( Number ( process . env . MOCK_PORT ?? 5181 )); 47console . log ( `Mock model API: ${ mock . baseUrl } ` ); 48}