okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-0212-tools (ea765a571): tool-call fixes in serve/ and the native gateway, with their tests and docs311926416

main
10.5 KiB81 linesraw
1import test from 'node:test';
2import assert from 'node:assert/strict';
3import http from 'node:http';
4import {nativeGateway} from '../server/native-gateway.mjs';
5const listen=server=>new Promise(r=>server.listen(0,'127.0.0.1',()=>r(server.address().port)));
6const close=server=>new Promise(r=>{server.closeAllConnections();server.close(r);});
7test('native gateway preserves all three protocols and routes exact IDs, usage, errors and cancellation',{timeout:10000},async()=>{
8 const received=[],records=[];let cancelled=false;
9 const engine=http.createServer(async(req,res)=>{let text='';for await(const chunk of req)text+=chunk;const body=JSON.parse(text);received.push({path:req.url,body,key:req.headers.authorization});
10  if(body.input==='fail'){res.writeHead(422,{'content-type':'application/json'});res.end(JSON.stringify({error:{message:'Invalid native input'}}));return;}
11  if(body.stream){res.writeHead(200,{'content-type':'text/event-stream'});res.write('event: response.output_text.delta\ndata: {"type":"response.output_text.delta","delta":"Hello"}\n\n');if(body.input==='slow'){res.on('close',()=>{cancelled=true;});return;}res.end('event: response.completed\ndata: {"response":{"usage":{"input_tokens":12,"output_tokens":3}}}\n\n');return;}
12  res.writeHead(200,{'content-type':'application/json'});res.end(JSON.stringify(req.url==='/v1/messages'?{type:'message',content:[{type:'text',text:'Hello'}],usage:{input_tokens:12,output_tokens:3}}:{object:'response',output:[{type:'function_call',name:'lookup',arguments:'{}'}],usage:{input_tokens:12,output_tokens:3}}));
13 });const port=await listen(engine);
14 const instances=[{id:'first',apiModelId:'native-a',model:'iq2_xs',port,key:'private-a',gpus:[0]},{id:'second',apiModelId:'native-b',model:'iq3_s',port,key:'private-b',gpus:[1]}];
15 const serving={open:value=>{const record={...value,id:String(records.length)};records.push(record);return record;},mark:(id,value)=>Object.assign(records[Number(id)],value),finish:(id,value)=>Object.assign(records[Number(id)],value)};
16 const control={resolveInstance:id=>{const found=instances.find(i=>i.apiModelId===id);if(!found)throw new Error('Choose an exact loaded ID');return found;}};
17 const gateway=http.createServer(async(req,res)=>{let text='';for await(const chunk of req)text+=chunk;await nativeGateway({req,res,path:req.url,body:JSON.parse(text),control,serving});});const publicPort=await listen(gateway);
18 const call=async(path,body)=>fetch(`http://127.0.0.1:${publicPort}${path}`,{method:'POST',headers:{'content-type':'application/json'},body:JSON.stringify(body)});
19 try{
20  const tools=[{type:'function',name:'lookup',parameters:{type:'object'}}];const input=[{type:'message',role:'user',content:[{type:'input_text',text:'Hello'}]},{type:'function_call_output',call_id:'c1',output:'Found'}];
21  let result=await call('/v1/responses',{model:'native-b',input,tools,strata_mcp:true,reasoning:{effort:'high'}});assert.equal(result.status,200);assert.equal((await result.json()).output[0].name,'lookup');assert.deepEqual(received[0].body.input,input);assert.deepEqual(received[0].body.tools,tools);assert.equal(received[0].body.model,'native-b');assert.equal(received[0].body.strata_mcp,false);assert.equal(received[0].key,'Bearer private-b');assert.equal(records[0].tokensIn,12);assert.equal(records[0].tokensOut,3);
22  result=await call('/v1/messages',{model:'native-a',messages:[{role:'user',content:'Hello'}],max_tokens:50});assert.equal((await result.json()).type,'message');assert.equal(received[1].path,'/v1/messages');assert.equal(received[1].key,'Bearer private-a');
23  result=await call('/v1/chat/completions',{model:'native-a',messages:[{role:'user',content:'Hello'}]});assert.equal(result.status,200);await result.text();assert.equal(received[2].path,'/v1/chat/completions');
24  result=await call('/v1/responses',{model:'native-b',stream:true,input:'Hello'});assert.match(await result.text(),/event: response.completed/);assert.equal(records[3].tokensOut,3);
25  result=await call('/v1/responses',{model:'missing',input:'Hello'});assert.equal(result.status,400);assert.equal((await result.json()).error.param,'model');
26  result=await call('/v1/responses',{model:'native-b',input:'fail'});assert.equal(result.status,422);assert.equal((await result.json()).error.message,'Invalid native input');
27  result=await call('/v1/responses',{model:'native-a',input:'slow',stream:true});const read=result.body.getReader();await read.read();await read.cancel();for(let n=0;n<30&&!cancelled;n++)await new Promise(r=>setTimeout(r,20));assert.equal(cancelled,true);
28 }finally{await close(gateway);await close(engine);}
29});
30test('a client that hangs up right after data: [DONE] has its answer: the request is done, not "Request cancelled." (#4690)',{timeout:30000},async()=>{
31 const net=await import('node:net');const records=[];let closeDelay=0;
32 const engine=http.createServer(async(req,res)=>{for await(const _ of req);res.writeHead(200,{'content-type':'text/event-stream',connection:'close'});
33  res.write('data: {"choices":[{"delta":{"content":"Hi"}}]}\n\n');res.write('data: {"choices":[],"usage":{"prompt_tokens":12,"completion_tokens":3}}\n\n');res.write('data: [DONE]\n\n');
34  await new Promise(r=>setTimeout(r,closeDelay));res.end();});
35 const enginePort=await listen(engine);
36 const serving={open:value=>{const record={id:records.length,state:'queued',tokensIn:null,tokensOut:null,...value};records.push(record);return record;},mark:(id,value)=>Object.assign(records[id],value),finish:(id,{tokensOut,error})=>Object.assign(records[id],{state:error?'error':'done',tokensOut,error})};
37 const control={resolveInstance:()=>({id:'x',apiModelId:'x',port:enginePort,key:'k',gpus:[0]})};
38 const gateway=http.createServer(async(req,res)=>{let text='';for await(const chunk of req)text+=chunk;await nativeGateway({req,res,path:req.url,body:JSON.parse(text),control,serving});});
39 const gatewayPort=await listen(gateway);
40 const call=()=>new Promise(resolve=>{const body=JSON.stringify({model:'x',stream:true,messages:[]});
41  const socket=net.connect(gatewayPort,'127.0.0.1',()=>socket.write(`POST /v1/chat/completions HTTP/1.1\r\nHost: x\r\nConnection: close\r\nContent-Type: application/json\r\nContent-Length: ${Buffer.byteLength(body)}\r\n\r\n${body}`));
42  let text='';socket.on('data',data=>{text+=data;if(text.includes('data: [DONE]'))socket.destroy();});socket.on('close',()=>resolve(text));});
43 try{
44  for(const delay of [0,5,30])for(let run=0;run<5;run++){closeDelay=delay;const before=records.length;assert.match(await call(),/\[DONE\]/);
45   for(let n=0;n<100&&!['done','error'].includes(records[before]?.state);n++)await new Promise(r=>setTimeout(r,20));
46   const last=records[before];assert.equal(last.state,'done',`delay ${delay} ms: ${last.error}`);assert.equal(last.tokensIn,12);assert.equal(last.tokensOut,3);}
47 }finally{await close(gateway);await close(engine);}
48});
49
50test('an Anthropic stream: the prompt count (message_start) and the output count (message_delta) both reach the request log (#4678)',{timeout:10000},async()=>{
51 const records=[];
52 const engine=http.createServer(async(req,res)=>{for await(const _ of req);res.writeHead(200,{'content-type':'text/event-stream'});
53  res.write('event: message_start\ndata: {"type":"message_start","message":{"id":"m","usage":{"input_tokens":29757,"output_tokens":0}}}\n\n');
54  res.write('event: message_delta\ndata: {"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":561}}\n\n');res.end();});
55 const enginePort=await listen(engine);
56 const serving={open:value=>{const record={id:records.length,state:'queued',tokensIn:null,tokensOut:null,...value};records.push(record);return record;},mark:(id,value)=>Object.assign(records[id],value),finish:(id,{tokensOut})=>Object.assign(records[id],{state:'done',tokensOut:tokensOut??records[id].tokensOut})};
57 const control={resolveInstance:()=>({id:'x',apiModelId:'x',port:enginePort,key:'k',gpus:[0]})};
58 const gateway=http.createServer(async(req,res)=>{let text='';for await(const chunk of req)text+=chunk;await nativeGateway({req,res,path:req.url,body:JSON.parse(text),control,serving});});
59 const gatewayPort=await listen(gateway);
60 try{
61  const r=await fetch(`http://127.0.0.1:${gatewayPort}/v1/messages`,{method:'POST',headers:{'content-type':'application/json'},body:JSON.stringify({model:'x',stream:true,max_tokens:50,messages:[]})});await r.text();
62  assert.equal(records[0].tokensIn,29757);assert.equal(records[0].tokensOut,561);assert.equal(records[0].usageSource,'reported');
63 }finally{await close(gateway);await close(engine);}
64});
65
66test('an Anthropic client that sends a model name of its own gets the one loaded model (#5312); OpenAI and Responses stay exact',{timeout:10000},async()=>{
67 const received=[];const engine=http.createServer(async(req,res)=>{let text='';for await(const chunk of req)text+=chunk;received.push(JSON.parse(text));res.writeHead(200,{'content-type':'application/json'});res.end(JSON.stringify({type:'message',content:[{type:'text',text:'Hi'}],usage:{input_tokens:1,output_tokens:1}}));});
68 const port=await listen(engine);const records=[];
69 const serving={open:value=>{const r={...value,id:String(records.length)};records.push(r);return r;},mark:(id,value)=>Object.assign(records[Number(id)],value),finish:(id,value)=>Object.assign(records[Number(id)],value)};
70 const one={id:'only',apiModelId:'iq3_s',port,key:'k',gpus:[0]},two={id:'other',apiModelId:'iq2_xs',port,key:'k',gpus:[1]};
71 const make=instances=>({resolveInstance:id=>{const found=instances.find(i=>i.apiModelId===id)??(!id&&instances.length===1?instances[0]:undefined);if(!found)throw new Error('Choose a loaded model ID.');return found;}});
72 const serve=control=>http.createServer(async(req,res)=>{let text='';for await(const chunk of req)text+=chunk;await nativeGateway({req,res,path:req.url,body:JSON.parse(text),control,serving});});
73 const single=serve(make([one])),pair=serve(make([one,two]));const p1=await listen(single),p2=await listen(pair);
74 const call=(p,path,body)=>fetch(`http://127.0.0.1:${p}${path}`,{method:'POST',headers:{'content-type':'application/json'},body:JSON.stringify(body)});
75 try{
76  let r=await call(p1,'/v1/messages',{model:'claude-5-5-sonnet',messages:[{role:'user',content:'Hi'}],max_tokens:5});assert.equal(r.status,200);await r.text();assert.equal(received.at(-1).model,'iq3_s');
77  r=await call(p2,'/v1/messages',{model:'claude-5-5-sonnet',messages:[],max_tokens:5});assert.equal(r.status,400);assert.equal((await r.json()).error.param,'model');
78  r=await call(p1,'/v1/chat/completions',{model:'claude-5-5-sonnet',messages:[]});assert.equal(r.status,400);
79  r=await call(p1,'/v1/responses',{model:'claude-5-5-sonnet',input:'Hi'});assert.equal(r.status,400);
80 }finally{await close(single);await close(pair);await close(engine);}
81});