ghc-tunnel 1.0.3 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -90,7 +90,7 @@ Prefix Mappings: (none)`);console.log("=".repeat(60)+`
|
|
|
90
90
|
|
|
91
91
|
`);continue}try{let I=JSON.parse(H);a.push(I),I.usage&&(l=I.usage.prompt_tokens??0,p=I.usage.completion_tokens??0),m||t.write(`data: ${H}
|
|
92
92
|
|
|
93
|
-
`)}catch{}}}}catch(d){u=504,console.log(`[Stream] Error for ${n}: ${d}`)}m?$.updateRequestState(n,"error",{status_code:499}):t.end();let _=Math.round((Date.now()-r)/1e3*100)/100,R=xe(a);$.completeRequest(n,{request_body:e,response_body:R,model:i,translated_model:c!==i?c:null,endpoint:"/v1/chat/completions",status_code:m?499:u,request_size:s,response_size:a.reduce((d,y)=>d+JSON.stringify(y).length,0),input_tokens:l,output_tokens:p,duration:_})}oe.post(["/v1/responses","/responses"],async(t,e)=>{try{let o=Date.now();await U();let n=t.body,s=crypto.randomUUID(),r=n.model??"unknown",i=ne(r);if(i!==r&&(n={...n,model:i}),!nt(i))return e.status(400).json({error:{message:`Model '${r}' does not support the /v1/responses endpoint.`,type:"invalid_request_error",code:"unsupported_model"}});if(console.log(`[Responses API] Using OpenAI Responses API path for: ${i}`),Array.isArray(n.tools)&&(n={...n,tools:n.tools.map(_=>_.type==="custom"&&_.name==="apply_patch"?{type:"function",name:"apply_patch",description:"Use the `apply_patch` tool to edit files",parameters:{type:"object",properties:{input:{type:"string",description:"The entire contents of the apply_patch command"}},required:["input"]},strict:!1}:_)}),Array.isArray(n.tools)){let _=new Set(["
|
|
93
|
+
`)}catch{}}}}catch(d){u=504,console.log(`[Stream] Error for ${n}: ${d}`)}m?$.updateRequestState(n,"error",{status_code:499}):t.end();let _=Math.round((Date.now()-r)/1e3*100)/100,R=xe(a);$.completeRequest(n,{request_body:e,response_body:R,model:i,translated_model:c!==i?c:null,endpoint:"/v1/chat/completions",status_code:m?499:u,request_size:s,response_size:a.reduce((d,y)=>d+JSON.stringify(y).length,0),input_tokens:l,output_tokens:p,duration:_})}oe.post(["/v1/responses","/responses"],async(t,e)=>{try{let o=Date.now();await U();let n=t.body,s=crypto.randomUUID(),r=n.model??"unknown",i=ne(r);if(i!==r&&(n={...n,model:i}),!nt(i))return e.status(400).json({error:{message:`Model '${r}' does not support the /v1/responses endpoint.`,type:"invalid_request_error",code:"unsupported_model"}});if(console.log(`[Responses API] Using OpenAI Responses API path for: ${i}`),Array.isArray(n.tools)&&(n={...n,tools:n.tools.map(_=>_.type==="custom"&&_.name==="apply_patch"?{type:"function",name:"apply_patch",description:"Use the `apply_patch` tool to edit files",parameters:{type:"object",properties:{input:{type:"string",description:"The entire contents of the apply_patch command"}},required:["input"]},strict:!1}:_)}),Array.isArray(n.tools)){let _=new Set(["image_generation"]);n={...n,tools:n.tools.filter(R=>!(typeof R.type=="string"&&_.has(R.type)))}}if(Array.isArray(n.input)){let _=zt(n.input);_!==n.input&&(n={...n,input:_})}n={...n,service_tier:null};let c=Jt(n.input),a=Y(c);a["X-Initiator"]=Bt(n.input)?"agent":"user";let l=JSON.stringify(n).length;if(n.stream)return Ht(e,n,a,s,l,o,r,i);let p=await it(`${N()}/responses`,{method:"POST",headers:a,body:JSON.stringify(n)},s,"/v1/responses"),u=Math.round((Date.now()-o)/1e3*100)/100;if(!p)return e.status(504).json({error:`Upstream connection error after ${f.maxConnectionRetries+1} attempts`});let m=await p.text();if(p.ok){let _=JSON.parse(m),R=_.usage??{};$.addRequest(s,{request_body:n,response_body:_,model:r,translated_model:i!==r?i:null,endpoint:"/v1/responses",status_code:p.status,request_size:l,response_size:m.length,input_tokens:R.input_tokens??0,output_tokens:R.output_tokens??0,duration:u}),e.json(_)}else Z("/v1/responses",n,m,p.status),e.status(p.status).type("json").send(m)}catch(o){e.status(500).json({error:String(o)})}});async function Ht(t,e,o,n,s,r,i,c){$.startRequest(n,{request_body:e,model:i,translated_model:c!==i?c:null,endpoint:"/v1/responses",request_size:s}),t.writeHead(200,{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive","X-Accel-Buffering":"no"});let a=0,l=0,p=200,u={},m=[],_=!1;t.on("close",()=>{_=!0});try{$.updateRequestState(n,"sending");let y=await fetch(`${N()}/responses`,{method:"POST",headers:o,body:JSON.stringify(e),signal:AbortSignal.timeout(12e5)});if(p=y.status,y.ok){let b=y.body.getReader(),k=new TextDecoder,x="";for($.updateRequestState(n,"receiving");;){let{done:T,value:q}=await b.read();if(T)break;if(_){b.cancel();break}_||t.write(q),x+=k.decode(q,{stream:!0});let C=x.split(`
|
|
94
94
|
`);x=C.pop();for(let H of C){let I=H.replace(/\r$/,"");if(!I.startsWith("data: "))continue;let V=I.slice(6);if(V!=="[DONE]")try{let A=JSON.parse(V);if(A.type==="response.completed"){let B=A.response??{};u=B;let O=B.usage??{};a=O.input_tokens??0,l=O.output_tokens??0}else A.type==="response.output_text.delta"&&m.push(A.delta??"")}catch{}}}}else{let b=await y.text();Z("/v1/responses",e,b,p),_||t.write(`event: error
|
|
95
95
|
data: ${JSON.stringify({status:p,body:b})}
|
|
96
96
|
|