copilot-api-node20 0.15.0 → 0.15.1
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/main.js +1 -1
- package/package.json +1 -1
package/dist/main.js
CHANGED
|
@@ -16,7 +16,7 @@ Token exists: ${e.tokenExists?`Yes`:`No`}`)}function at(e){console.log(JSON.stri
|
|
|
16
16
|
|
|
17
17
|
`);return t.length>0?[{role:`assistant`,content:i||null,tool_calls:t.map(e=>({id:e.id,type:`function`,function:{name:e.name,arguments:JSON.stringify(e.input)}}))}]:[{role:`assistant`,content:J(e.content)}]}function J(e){if(typeof e==`string`)return e;if(!Array.isArray(e))return null;let t=e.some(e=>e.type===`image`);if(!t)return e.filter(e=>e.type===`text`||e.type===`thinking`).map(e=>e.type===`text`?e.text:e.thinking).join(`
|
|
18
18
|
|
|
19
|
-
`);let n=[];for(let t of e)switch(t.type){case`text`:n.push({type:`text`,text:t.text});break;case`thinking`:n.push({type:`text`,text:t.thinking});break;case`image`:n.push({type:`image_url`,image_url:{url:`data:${t.source.media_type};base64,${t.source.data}`}});break}return n}function Ht(e){if(e)return e.map(e=>({type:`function`,function:{name:e.name,description:e.description,parameters:e.input_schema}}))}function Ut(e){if(e)switch(e.type){case`auto`:return`auto`;case`any`:return`required`;case`tool`:return e.name?{type:`function`,function:{name:e.name}}:void 0;case`none`:return`none`;default:return}}function Wt(e){let t=[],n=[],r=null;r=e.choices[0]?.finish_reason??r;for(let i of e.choices){let e=Gt(i.message.content),a=Kt(i.message.tool_calls);t.push(...e),n.push(...a),(i.finish_reason===`tool_calls`||r===`stop`)&&(r=i.finish_reason)}return{id:e.id,type:`message`,role:`assistant`,model:e.model,content:[...t,...n],stop_reason:Pt(r),stop_sequence:null,usage:{input_tokens:e.usage?.prompt_tokens??0,output_tokens:e.usage?.completion_tokens??0}}}function Gt(e){return typeof e==`string`?[{type:`text`,text:e}]:Array.isArray(e)?e.filter(e=>e.type===`text`).map(e=>({type:`text`,text:e.text})):[]}function Kt(e){return e?e.map(e=>({type:`tool_use`,id:e.id,name:e.function.name,input:JSON.parse(e.function.arguments)})):[]}function qt(e){return e.contentBlockOpen?Object.values(e.toolCalls).some(t=>t.anthropicBlockIndex===e.contentBlockIndex):!1}function Jt(e,t){let n=[];if(e.choices.length===0)return n;let r=e.choices[0],{delta:i}=r;if(t.messageStartSent||=(n.push({type:`message_start`,message:{id:e.id,type:`message`,role:`assistant`,content:[],model:e.model,stop_reason:null,stop_sequence:null,usage:{input_tokens:e.usage?.prompt_tokens??0,output_tokens:0}}}),!0),i.content&&(qt(t)&&(n.push({type:`content_block_stop`,index:t.contentBlockIndex}),t.contentBlockIndex++,t.contentBlockOpen=!1),t.contentBlockOpen||=(n.push({type:`content_block_start`,index:t.contentBlockIndex,content_block:{type:`text`,text:``}}),!0),n.push({type:`content_block_delta`,index:t.contentBlockIndex,delta:{type:`text_delta`,text:i.content}})),i.tool_calls)for(let e of i.tool_calls){if(e.id&&e.function?.name){t.contentBlockOpen&&=(n.push({type:`content_block_stop`,index:t.contentBlockIndex}),t.contentBlockIndex++,!1);let r=t.contentBlockIndex;t.toolCalls[e.index]={id:e.id,name:e.function.name,anthropicBlockIndex:r},n.push({type:`content_block_start`,index:r,content_block:{type:`tool_use`,id:e.id,name:e.function.name,input:{}}}),t.contentBlockOpen=!0}if(e.function?.arguments){let r=t.toolCalls[e.index];r&&n.push({type:`content_block_delta`,index:r.anthropicBlockIndex,delta:{type:`input_json_delta`,partial_json:e.function.arguments}})}}return r.finish_reason&&(t.contentBlockOpen&&=(n.push({type:`content_block_stop`,index:t.contentBlockIndex}),!1),n.push({type:`message_delta`,delta:{stop_reason:Pt(r.finish_reason),stop_sequence:null},usage:{input_tokens:e.usage?.prompt_tokens??0,output_tokens:e.usage?.completion_tokens??0}},{type:`message_stop`})),n}async function Yt(e,t,n){let r={query:t,max_results:n?.maxResults??5};n?.includeDomains?.length&&(r.include_domains=n.includeDomains),n?.excludeDomains?.length&&(r.exclude_domains=n.excludeDomains),E.debug(`Tavily search request:`,JSON.stringify(r));let i=await fetch(`https://api.tavily.com/search`,{method:`POST`,headers:{"Content-Type":`application/json`,Authorization:`Bearer ${e}`},body:JSON.stringify(r)});if(!i.ok){let e=await i.text();throw E.error(`Tavily API error (${i.status}):`,e),Error(`Tavily API error: ${i.status} ${e}`)}let a=await i.json();return E.debug(`Tavily returned ${a.results.length} results`),a.results}function Xt(e){let t=e.tools;if(!t||t.length!==1)return!1;let n=t[0].type;return typeof n==`string`&&n.startsWith(`web_search`)}function Zt(e){let t=e.messages[0];if(!t||t.role!==`user`)return null;let n;if(typeof t.content==`string`)n=t.content;else if(Array.isArray(t.content)){let e=t.content.find(e=>e.type===`text`);if(!e)return null;n=e.text}else return null;return n.startsWith(`Perform a web search for the query: `)?n.slice(36).trim():n.trim()||null}function Qt(e){let t=e.tools,n=t?.[0],r=typeof n?.max_uses==`number`?n.max_uses:5;return{maxResults:r,allowedDomains:n?.allowed_domains,blockedDomains:n?.blocked_domains}}function Y(){let e=``;for(let t=0;t<24;t++)e+=`abcdefghijklmnopqrstuvwxyz0123456789`[Math.floor(Math.random()*36)];return e}function $t(e){let{query:t,results:n,model:r,isError:i,errorContent:a}=e,o=`srvtoolu_${Y()}`,s=[{type:`text`,text:`I'll search for that.`},{type:`server_tool_use`,id:o,name:`web_search`,input:{query:t}},{type:`web_search_tool_result`,tool_use_id:o,content:i&&a?a:n}];return{id:`msg_tavily_${Y()}`,type:`message`,role:`assistant`,model:r,content:s,stop_reason:`end_turn`,stop_sequence:null,usage:{input_tokens:0,output_tokens:0}}}function en(e){let{query:t,results:n,model:r,isError:i,errorContent:a}=e,o=`srvtoolu_${Y()}`,s=`msg_tavily_${Y()}`,c=[];return c.push({type:`message_start`,message:{id:s,type:`message`,role:`assistant`,content:[],model:r,stop_reason:null,stop_sequence:null,usage:{input_tokens:0,output_tokens:0}}},{type:`content_block_start`,index:0,content_block:{type:`text`,text:``}},{type:`content_block_delta`,index:0,delta:{type:`text_delta`,text:`I'll search for that.`}},{type:`content_block_stop`,index:0},{type:`content_block_start`,index:1,content_block:{type:`server_tool_use`,id:o,name:`web_search`,input:{}}},{type:`content_block_delta`,index:1,delta:{type:`input_json_delta`,partial_json:JSON.stringify({query:t})}},{type:`content_block_stop`,index:1},{type:`content_block_start`,index:2,content_block:{type:`web_search_tool_result`,tool_use_id:o,content:i&&a?a:n}},{type:`content_block_stop`,index:2},{type:`message_delta`,delta:{stop_reason:`end_turn`,stop_sequence:null},usage:{output_tokens:0}},{type:`message_stop`}),c}async function tn(e,t){let n=Zt(t);if(!n)return e.json({type:`error`,error:{type:`invalid_request_error`,message:`Could not extract search query from web search request`}},400);let r=Qt(t);E.info(`Web search intercepted: "${n}"`);let i=[],a=!1,o;try{let e=S.tavilyApiKey;if(!e)throw Error(`No Tavily API key configured`);let t=await Yt(e,n,{maxResults:Math.min(r.maxResults,10),includeDomains:r.allowedDomains,excludeDomains:r.blockedDomains});i=t.map(e=>({type:`web_search_result`,url:e.url,title:e.title,encrypted_content:Buffer.from(e.content).toString(`base64`),page_age:null}))}catch(e){E.error(`Tavily search failed:`,e),a=!0,o=[{type:`web_search_tool_result_error`,error_code:`unavailable`}]}let s={query:n,results:i,model:t.model,isError:a,errorContent:o};if(!t.stream)return e.json($t(s));let c=en(s);return M(e,async e=>{for(let t of c)await e.writeSSE({event:t.type,data:JSON.stringify(t)})})}function nn(e,t){return w.startActiveSpan(`\u0063\u006f\u0070\u0069\u006c\u006f\u0074_api.model.validate`,n=>{try{n.setAttribute(`\u0063\u006f\u0070\u0069\u006c\u006f\u0074_api.model.requested`,t.model);let e=V(t.model);return n.setAttribute(`\u0063\u006f\u0070\u0069\u006c\u006f\u0074_api.model.normalized`,e),e===t.model?(n.setAttribute(`\u0063\u006f\u0070\u0069\u006c\u006f\u0074_api.model.was_mapped`,!1),t):(n.setAttribute(`\u0063\u006f\u0070\u0069\u006c\u006f\u0074_api.model.was_mapped`,!0),se.add(1,{from_model:t.model,to_model:e}),E.debug(`Model mapping: '${t.model}' → '${e}'`),{...t,model:e})}catch(t){if(t instanceof B)return e.json({type:`error`,error:{type:`invalid_request_error`,message:t.message}},t.statusCode),null;throw t}finally{n.end()}})}async function rn(t){let n=await t.req.json();if(Xt(n))return tn(t,n);await St(S),E.debug(`\u0041\u006e\u0074\u0068\u0072\u006f\u0070\u0069\u0063 request payload:`,JSON.stringify(n));let r=t.req.header(`\u0061\u006e\u0074\u0068\u0072\u006f\u0070\u0069\u0063-beta`)??``,i=/context-1m/i.test(r);if(i){let e=n.model;n={...n,model:`${n.model}-1m`},E.debug(`Detected context-1m beta flag, rewriting model: '${e}' → '${n.model}'`)}let a=nn(t,n);if(!a)return;n=a;let o=performance.now(),s=w.startActiveSpan(`\u0063\u006f\u0070\u0069\u006c\u006f\u0074_api.translate.\u0061\u006e\u0074\u0068\u0072\u006f\u0070\u0069\u0063_to_\u006f\u0070\u0065\u006e\u0061\u0069`,e=>{try{e.setAttribute(`\u0063\u006f\u0070\u0069\u006c\u006f\u0074_api.translation.direction`,`\u0061\u006e\u0074\u0068\u0072\u006f\u0070\u0069\u0063_to_\u006f\u0070\u0065\u006e\u0061\u0069`),e.setAttribute(`\u0063\u006f\u0070\u0069\u006c\u006f\u0074_api.translation.message_count`,n.messages.length),e.setAttribute(`\u0063\u006f\u0070\u0069\u006c\u006f\u0074_api.translation.has_tools`,!!n.tools?.length),e.setAttribute(`\u0063\u006f\u0070\u0069\u006c\u006f\u0074_api.translation.has_system`,!!n.system),e.setAttribute(`\u0063\u006f\u0070\u0069\u006c\u006f\u0074_api.translation.context_1m_beta`,i);let t=n.messages.some(e=>Array.isArray(e.content)&&e.content.some(e=>e.type===`image`)),r=n.messages.some(e=>Array.isArray(e.content)&&e.content.some(e=>e.type===`thinking`));return e.setAttribute(`\u0063\u006f\u0070\u0069\u006c\u006f\u0074_api.translation.has_images`,t),e.setAttribute(`\u0063\u006f\u0070\u0069\u006c\u006f\u0074_api.translation.has_thinking`,r),Ft(n)}finally{e.end()}});Ce.record(performance.now()-o,{direction:`\u0061\u006e\u0074\u0068\u0072\u006f\u0070\u0069\u0063_to_\u006f\u0070\u0065\u006e\u0061\u0069`}),E.debug(`Translated \u004f\u0070\u0065\u006e\u0041\u0049 request payload:`,JSON.stringify(s)),t.set(`requestData`,{model:s.model,streaming:!!s.stream});let c=performance.now();S.manualApprove&&await pt();let l=await wt(s),u=performance.now()-c;if(g.record(u,{model:s.model,target:`chat_completions`,streaming:String(!!s.stream)}),an(l)){let e=t.get(`requestData`)||{};l.usage&&(e.tokenUsage=z(l.usage)),e.copilotDuration=u,t.set(`requestData`,e),E.debug(`Non-streaming response:`,JSON.stringify(l).slice(-400));let n=performance.now(),r=w.startActiveSpan(`\u0063\u006f\u0070\u0069\u006c\u006f\u0074_api.translate.\u006f\u0070\u0065\u006e\u0061\u0069_to_\u0061\u006e\u0074\u0068\u0072\u006f\u0070\u0069\u0063`,e=>{try{e.setAttribute(`\u0063\u006f\u0070\u0069\u006c\u006f\u0074_api.translation.direction`,`\u006f\u0070\u0065\u006e\u0061\u0069_to_\u0061\u006e\u0074\u0068\u0072\u006f\u0070\u0069\u0063`);let t=Wt(l);return e.setAttribute(`\u0063\u006f\u0070\u0069\u006c\u006f\u0074_api.translation.content_blocks`,t.content.length),e.setAttribute(`\u0063\u006f\u0070\u0069\u006c\u006f\u0074_api.translation.stop_reason`,t.stop_reason??`null`),e.setAttribute(`\u0063\u006f\u0070\u0069\u006c\u006f\u0074_api.translation.has_tool_use`,t.content.some(e=>e.type===`tool_use`)),t}finally{e.end()}});return Ce.record(performance.now()-n,{direction:`\u006f\u0070\u0065\u006e\u0061\u0069_to_\u0061\u006e\u0074\u0068\u0072\u006f\u0070\u0069\u0063`}),E.debug(`Translated \u0041\u006e\u0074\u0068\u0072\u006f\u0070\u0069\u0063 response:`,JSON.stringify(r)),t.json(r)}return E.debug(`Streaming response from service`),M(t,async n=>{let r=null,i=0,a=performance.now(),o={messageStartSent:!1,contentBlockIndex:0,contentBlockOpen:!1,toolCalls:{}};for await(let e of l){if(E.debug(`Raw stream event:`,JSON.stringify(e)),e.data===`[DONE]`)break;if(!e.data)continue;let a=JSON.parse(e.data);if(i++,_e.add(1,{model:s.model,endpoint_type:`\u0061\u006e\u0074\u0068\u0072\u006f\u0070\u0069\u0063`}),a.usage){r=a.usage;let e=t.get(`requestData`)||{};e.tokenUsage=z(r),e.copilotDuration=u,t.set(`requestData`,e)}let c=Jt(a,o);for(let e of c)E.debug(`Translated \u0041\u006e\u0074\u0068\u0072\u006f\u0070\u0069\u0063 event:`,JSON.stringify(e)),await n.writeSSE({event:e.type,data:JSON.stringify(e)})}let c=performance.now()-a,d={model:s.model,endpoint_type:`\u0061\u006e\u0074\u0068\u0072\u006f\u0070\u0069\u0063`};if(ve.record(c,d),p.record(i,d),r){let e=t.get(`requestData`)||{};e.tokenUsage||(e.tokenUsage=z(r),e.copilotDuration=u,t.set(`requestData`,e))}let f=t.get(`requestId`);f&&e.executeCompletion(f)})}const an=e=>Object.hasOwn(e,`choices`),X=new j;X.post(`/`,async e=>{try{return await rn(e)}catch(t){return await x(e,t)}});const Z=new j;Z.get(`/`,async e=>{try{S.models||await He();let t=S.models?.data.map(e=>({id:e.id,object:`model`,type:`model`,created:0,created_at:new Date(0).toISOString(),owned_by:e.vendor,display_name:e.name,context_length:e.capabilities.limits?.max_context_window_tokens}));return e.json({object:`list`,data:t,has_more:!1})}catch(t){return await x(e,t)}});const on=new j;on.get(`/`,e=>{try{return e.json({token:S.copilotToken})}catch(t){return console.error(`Error fetching token:`,t),e.json({error:`Failed to fetch token`,token:null},500)}});const sn=new j;sn.get(`/`,async e=>{try{let t=await Qe();return e.json(t)}catch(t){return console.error(`Error fetching usage:`,t),e.json({error:`Failed to fetch usage`},500)}});const Q=new j;Q.use(ft()),Q.use(Me()),Q.get(`/`,e=>e.text(`Server running`)),Q.route(`/chat/completions`,U),Q.route(`/models`,Z),Q.route(`/embeddings`,W),Q.route(`/usage`,sn),Q.route(`/token`,on),Q.route(`/v1/chat/completions`,U),Q.route(`/v1/models`,Z),Q.route(`/v1/embeddings`,W),Q.route(`/v1/messages`,X),Q.post(`/v1/messages/count_tokens`,jt);const cn=[],$=Date.now();function ln(){let e=async(e,t=0)=>{E.info(`Gracefully shutting down...`);let n=w.startSpan(`\u0063\u006f\u0070\u0069\u006c\u006f\u0074_api.server.shutdown`,{kind:O.INTERNAL,attributes:{"\u0063\u006f\u0070\u0069\u006c\u006f\u0074_api.shutdown.reason":e,"\u0063\u006f\u0070\u0069\u006c\u006f\u0074_api.shutdown.exit_code":t,"\u0063\u006f\u0070\u0069\u006c\u006f\u0074_api.uptime_seconds":(Date.now()-$)/1e3}});for(let e of cn)try{await e()}catch(e){E.error(`Error during cleanup:`,e)}n.end(),E.info(`Shutdown complete`),A.exit(t)};A.on(`SIGINT`,()=>e(`SIGINT`)),A.on(`SIGTERM`,()=>e(`SIGTERM`)),A.on(`uncaughtException`,t=>{E.error(`Uncaught exception:`,t),e(`uncaughtException`,1)}),A.on(`unhandledRejection`,(t,n)=>{E.error(`Unhandled promise rejection at:`,n,`reason:`,t),e(`unhandledRejection`,1)})}function un(){return`npx \u0063\u006f\u0070\u0069\u006c\u006f\u0074-api start`}function dn(e){let t=[un()];return t.push(`-p ${e.port}`),e.verbose&&t.push(`-v`),e.accountType!==`individual`&&t.push(`-a ${e.accountType}`),e.manual&&t.push(`--manual`),e.rateLimit&&t.push(`-r ${e.rateLimit}`),e.rateLimitWait&&t.push(`-w`),e.githubToken&&t.push(`-g ${e.\u0067\u0069\u0074\u0068\u0075\u0062Token}`),e.claudeCode&&t.push(`-c`),e.model&&t.push(`-m ${e.model}`),e.smallModel&&t.push(`-s ${e.smallModel}`),e.timeout&&t.push(`-t ${String(e.timeout)}`),e.showToken&&t.push(`--show-token`),e.disableConnectivityMonitoring&&t.push(`--disable-connectivity-monitoring`),t.join(` `)}async function fn(e,t){if(!e.claudeCode)return;Ae(S.models,`Models should be loaded by now`);let n,r;if(e.model&&e.smallModel){let t=S.models.data.map(e=>e.id);t.includes(e.model)||(E.error(`Invalid model: ${e.model}`),E.info(`Available services: \n${t.join(`
|
|
19
|
+
`);let n=[];for(let t of e)switch(t.type){case`text`:n.push({type:`text`,text:t.text});break;case`thinking`:n.push({type:`text`,text:t.thinking});break;case`image`:n.push({type:`image_url`,image_url:{url:`data:${t.source.media_type};base64,${t.source.data}`}});break}return n}function Ht(e){if(e)return e.map(e=>({type:`function`,function:{name:e.name,description:e.description,parameters:e.input_schema}}))}function Ut(e){if(e)switch(e.type){case`auto`:return`auto`;case`any`:return`required`;case`tool`:return e.name?{type:`function`,function:{name:e.name}}:void 0;case`none`:return`none`;default:return}}function Wt(e){let t=[],n=[],r=null;r=e.choices[0]?.finish_reason??r;for(let i of e.choices){let e=Gt(i.message.content),a=Kt(i.message.tool_calls);t.push(...e),n.push(...a),(i.finish_reason===`tool_calls`||r===`stop`)&&(r=i.finish_reason)}return{id:e.id,type:`message`,role:`assistant`,model:e.model,content:[...t,...n],stop_reason:Pt(r),stop_sequence:null,usage:{input_tokens:e.usage?.prompt_tokens??0,output_tokens:e.usage?.completion_tokens??0}}}function Gt(e){return typeof e==`string`?[{type:`text`,text:e}]:Array.isArray(e)?e.filter(e=>e.type===`text`).map(e=>({type:`text`,text:e.text})):[]}function Kt(e){return e?e.map(e=>({type:`tool_use`,id:e.id,name:e.function.name,input:JSON.parse(e.function.arguments)})):[]}function qt(e){return e.contentBlockOpen?Object.values(e.toolCalls).some(t=>t.anthropicBlockIndex===e.contentBlockIndex):!1}function Jt(e,t){let n=[];if(e.choices.length===0)return n;let r=e.choices[0],{delta:i}=r;if(t.messageStartSent||=(n.push({type:`message_start`,message:{id:e.id,type:`message`,role:`assistant`,content:[],model:e.model,stop_reason:null,stop_sequence:null,usage:{input_tokens:e.usage?.prompt_tokens??0,output_tokens:0}}}),!0),i.content&&(qt(t)&&(n.push({type:`content_block_stop`,index:t.contentBlockIndex}),t.contentBlockIndex++,t.contentBlockOpen=!1),t.contentBlockOpen||=(n.push({type:`content_block_start`,index:t.contentBlockIndex,content_block:{type:`text`,text:``}}),!0),n.push({type:`content_block_delta`,index:t.contentBlockIndex,delta:{type:`text_delta`,text:i.content}})),i.tool_calls)for(let e of i.tool_calls){if(e.id&&e.function?.name){t.contentBlockOpen&&=(n.push({type:`content_block_stop`,index:t.contentBlockIndex}),t.contentBlockIndex++,!1);let r=t.contentBlockIndex;t.toolCalls[e.index]={id:e.id,name:e.function.name,anthropicBlockIndex:r},n.push({type:`content_block_start`,index:r,content_block:{type:`tool_use`,id:e.id,name:e.function.name,input:{}}}),t.contentBlockOpen=!0}if(e.function?.arguments){let r=t.toolCalls[e.index];r&&n.push({type:`content_block_delta`,index:r.anthropicBlockIndex,delta:{type:`input_json_delta`,partial_json:e.function.arguments}})}}return r.finish_reason&&(t.contentBlockOpen&&=(n.push({type:`content_block_stop`,index:t.contentBlockIndex}),!1),n.push({type:`message_delta`,delta:{stop_reason:Pt(r.finish_reason),stop_sequence:null},usage:{input_tokens:e.usage?.prompt_tokens??0,output_tokens:e.usage?.completion_tokens??0}},{type:`message_stop`})),n}async function Yt(e,t,n){let r={query:t,max_results:n?.maxResults??5};n?.includeDomains?.length&&(r.include_domains=n.includeDomains),n?.excludeDomains?.length&&(r.exclude_domains=n.excludeDomains),E.debug(`Tavily search request:`,JSON.stringify(r));let i=await fetch(`https://api.tavily.com/search`,{method:`POST`,headers:{"Content-Type":`application/json`,Authorization:`Bearer ${e}`},body:JSON.stringify(r)});if(!i.ok){let e=await i.text();throw E.error(`Tavily API error (${i.status}):`,e),Error(`Tavily API error: ${i.status} ${e}`)}let a=await i.json();return E.debug(`Tavily returned ${a.results.length} results`),a.results}function Xt(e){let t=e.tools;if(!t||t.length!==1)return!1;let n=t[0].type;return typeof n==`string`&&n.startsWith(`web_search`)}function Zt(e){let t=e.messages[0];if(!t||t.role!==`user`)return null;let n;if(typeof t.content==`string`)n=t.content;else if(Array.isArray(t.content)){let e=t.content.find(e=>e.type===`text`);if(!e)return null;n=e.text}else return null;return n.startsWith(`Perform a web search for the query: `)?n.slice(36).trim():n.trim()||null}function Qt(e){let t=e.tools,n=t?.[0],r=typeof n?.max_uses==`number`?n.max_uses:5;return{maxResults:r,allowedDomains:n?.allowed_domains,blockedDomains:n?.blocked_domains}}function Y(){let e=``;for(let t=0;t<24;t++)e+=`abcdefghijklmnopqrstuvwxyz0123456789`[Math.floor(Math.random()*36)];return e}function $t(e){let{query:t,results:n,model:r}=e,i=`srvtoolu_${Y()}`,a=[{type:`text`,text:`I'll search for that.`},{type:`server_tool_use`,id:i,name:`web_search`,input:{query:t}},{type:`web_search_tool_result`,tool_use_id:i,content:n}];return{id:`msg_tavily_${Y()}`,type:`message`,role:`assistant`,model:r,content:a,stop_reason:`end_turn`,stop_sequence:null,usage:{input_tokens:0,output_tokens:0}}}function en(e){let{query:t,results:n,model:r}=e,i=`srvtoolu_${Y()}`,a=`msg_tavily_${Y()}`,o=[];return o.push({type:`message_start`,message:{id:a,type:`message`,role:`assistant`,content:[],model:r,stop_reason:null,stop_sequence:null,usage:{input_tokens:0,output_tokens:0}}},{type:`content_block_start`,index:0,content_block:{type:`text`,text:``}},{type:`content_block_delta`,index:0,delta:{type:`text_delta`,text:`I'll search for that.`}},{type:`content_block_stop`,index:0},{type:`content_block_start`,index:1,content_block:{type:`server_tool_use`,id:i,name:`web_search`,input:{}}},{type:`content_block_delta`,index:1,delta:{type:`input_json_delta`,partial_json:JSON.stringify({query:t})}},{type:`content_block_stop`,index:1},{type:`content_block_start`,index:2,content_block:{type:`web_search_tool_result`,tool_use_id:i,content:n}},{type:`content_block_stop`,index:2},{type:`message_delta`,delta:{stop_reason:`end_turn`,stop_sequence:null},usage:{output_tokens:0}},{type:`message_stop`}),o}async function tn(e,t){let n=Zt(t);if(!n)return e.json({type:`error`,error:{type:`invalid_request_error`,message:`Could not extract search query from web search request`}},400);let r=Qt(t);E.info(`Web search intercepted: "${n}"`);let i=[];try{let e=S.tavilyApiKey;if(!e)throw Error(`No Tavily API key configured`);let t=await Yt(e,n,{maxResults:Math.min(r.maxResults,10),includeDomains:r.allowedDomains,excludeDomains:r.blockedDomains});i=t.map(e=>({type:`web_search_result`,url:e.url,title:e.title,encrypted_content:Buffer.from(e.content).toString(`base64`),page_age:null}))}catch(e){E.error(`Tavily search failed:`,e)}let a={query:n,results:i,model:t.model};if(!t.stream)return e.json($t(a));let o=en(a);return M(e,async e=>{for(let t of o)await e.writeSSE({event:t.type,data:JSON.stringify(t)})})}function nn(e,t){return w.startActiveSpan(`\u0063\u006f\u0070\u0069\u006c\u006f\u0074_api.model.validate`,n=>{try{n.setAttribute(`\u0063\u006f\u0070\u0069\u006c\u006f\u0074_api.model.requested`,t.model);let e=V(t.model);return n.setAttribute(`\u0063\u006f\u0070\u0069\u006c\u006f\u0074_api.model.normalized`,e),e===t.model?(n.setAttribute(`\u0063\u006f\u0070\u0069\u006c\u006f\u0074_api.model.was_mapped`,!1),t):(n.setAttribute(`\u0063\u006f\u0070\u0069\u006c\u006f\u0074_api.model.was_mapped`,!0),se.add(1,{from_model:t.model,to_model:e}),E.debug(`Model mapping: '${t.model}' → '${e}'`),{...t,model:e})}catch(t){if(t instanceof B)return e.json({type:`error`,error:{type:`invalid_request_error`,message:t.message}},t.statusCode),null;throw t}finally{n.end()}})}async function rn(t){let n=await t.req.json();if(Xt(n))return tn(t,n);await St(S),E.debug(`\u0041\u006e\u0074\u0068\u0072\u006f\u0070\u0069\u0063 request payload:`,JSON.stringify(n));let r=t.req.header(`\u0061\u006e\u0074\u0068\u0072\u006f\u0070\u0069\u0063-beta`)??``,i=/context-1m/i.test(r);if(i){let e=n.model;n={...n,model:`${n.model}-1m`},E.debug(`Detected context-1m beta flag, rewriting model: '${e}' → '${n.model}'`)}let a=nn(t,n);if(!a)return;n=a;let o=performance.now(),s=w.startActiveSpan(`\u0063\u006f\u0070\u0069\u006c\u006f\u0074_api.translate.\u0061\u006e\u0074\u0068\u0072\u006f\u0070\u0069\u0063_to_\u006f\u0070\u0065\u006e\u0061\u0069`,e=>{try{e.setAttribute(`\u0063\u006f\u0070\u0069\u006c\u006f\u0074_api.translation.direction`,`\u0061\u006e\u0074\u0068\u0072\u006f\u0070\u0069\u0063_to_\u006f\u0070\u0065\u006e\u0061\u0069`),e.setAttribute(`\u0063\u006f\u0070\u0069\u006c\u006f\u0074_api.translation.message_count`,n.messages.length),e.setAttribute(`\u0063\u006f\u0070\u0069\u006c\u006f\u0074_api.translation.has_tools`,!!n.tools?.length),e.setAttribute(`\u0063\u006f\u0070\u0069\u006c\u006f\u0074_api.translation.has_system`,!!n.system),e.setAttribute(`\u0063\u006f\u0070\u0069\u006c\u006f\u0074_api.translation.context_1m_beta`,i);let t=n.messages.some(e=>Array.isArray(e.content)&&e.content.some(e=>e.type===`image`)),r=n.messages.some(e=>Array.isArray(e.content)&&e.content.some(e=>e.type===`thinking`));return e.setAttribute(`\u0063\u006f\u0070\u0069\u006c\u006f\u0074_api.translation.has_images`,t),e.setAttribute(`\u0063\u006f\u0070\u0069\u006c\u006f\u0074_api.translation.has_thinking`,r),Ft(n)}finally{e.end()}});Ce.record(performance.now()-o,{direction:`\u0061\u006e\u0074\u0068\u0072\u006f\u0070\u0069\u0063_to_\u006f\u0070\u0065\u006e\u0061\u0069`}),E.debug(`Translated \u004f\u0070\u0065\u006e\u0041\u0049 request payload:`,JSON.stringify(s)),t.set(`requestData`,{model:s.model,streaming:!!s.stream});let c=performance.now();S.manualApprove&&await pt();let l=await wt(s),u=performance.now()-c;if(g.record(u,{model:s.model,target:`chat_completions`,streaming:String(!!s.stream)}),an(l)){let e=t.get(`requestData`)||{};l.usage&&(e.tokenUsage=z(l.usage)),e.copilotDuration=u,t.set(`requestData`,e),E.debug(`Non-streaming response:`,JSON.stringify(l).slice(-400));let n=performance.now(),r=w.startActiveSpan(`\u0063\u006f\u0070\u0069\u006c\u006f\u0074_api.translate.\u006f\u0070\u0065\u006e\u0061\u0069_to_\u0061\u006e\u0074\u0068\u0072\u006f\u0070\u0069\u0063`,e=>{try{e.setAttribute(`\u0063\u006f\u0070\u0069\u006c\u006f\u0074_api.translation.direction`,`\u006f\u0070\u0065\u006e\u0061\u0069_to_\u0061\u006e\u0074\u0068\u0072\u006f\u0070\u0069\u0063`);let t=Wt(l);return e.setAttribute(`\u0063\u006f\u0070\u0069\u006c\u006f\u0074_api.translation.content_blocks`,t.content.length),e.setAttribute(`\u0063\u006f\u0070\u0069\u006c\u006f\u0074_api.translation.stop_reason`,t.stop_reason??`null`),e.setAttribute(`\u0063\u006f\u0070\u0069\u006c\u006f\u0074_api.translation.has_tool_use`,t.content.some(e=>e.type===`tool_use`)),t}finally{e.end()}});return Ce.record(performance.now()-n,{direction:`\u006f\u0070\u0065\u006e\u0061\u0069_to_\u0061\u006e\u0074\u0068\u0072\u006f\u0070\u0069\u0063`}),E.debug(`Translated \u0041\u006e\u0074\u0068\u0072\u006f\u0070\u0069\u0063 response:`,JSON.stringify(r)),t.json(r)}return E.debug(`Streaming response from service`),M(t,async n=>{let r=null,i=0,a=performance.now(),o={messageStartSent:!1,contentBlockIndex:0,contentBlockOpen:!1,toolCalls:{}};for await(let e of l){if(E.debug(`Raw stream event:`,JSON.stringify(e)),e.data===`[DONE]`)break;if(!e.data)continue;let a=JSON.parse(e.data);if(i++,_e.add(1,{model:s.model,endpoint_type:`\u0061\u006e\u0074\u0068\u0072\u006f\u0070\u0069\u0063`}),a.usage){r=a.usage;let e=t.get(`requestData`)||{};e.tokenUsage=z(r),e.copilotDuration=u,t.set(`requestData`,e)}let c=Jt(a,o);for(let e of c)E.debug(`Translated \u0041\u006e\u0074\u0068\u0072\u006f\u0070\u0069\u0063 event:`,JSON.stringify(e)),await n.writeSSE({event:e.type,data:JSON.stringify(e)})}let c=performance.now()-a,d={model:s.model,endpoint_type:`\u0061\u006e\u0074\u0068\u0072\u006f\u0070\u0069\u0063`};if(ve.record(c,d),p.record(i,d),r){let e=t.get(`requestData`)||{};e.tokenUsage||(e.tokenUsage=z(r),e.copilotDuration=u,t.set(`requestData`,e))}let f=t.get(`requestId`);f&&e.executeCompletion(f)})}const an=e=>Object.hasOwn(e,`choices`),X=new j;X.post(`/`,async e=>{try{return await rn(e)}catch(t){return await x(e,t)}});const Z=new j;Z.get(`/`,async e=>{try{S.models||await He();let t=S.models?.data.map(e=>({id:e.id,object:`model`,type:`model`,created:0,created_at:new Date(0).toISOString(),owned_by:e.vendor,display_name:e.name,context_length:e.capabilities.limits?.max_context_window_tokens}));return e.json({object:`list`,data:t,has_more:!1})}catch(t){return await x(e,t)}});const on=new j;on.get(`/`,e=>{try{return e.json({token:S.copilotToken})}catch(t){return console.error(`Error fetching token:`,t),e.json({error:`Failed to fetch token`,token:null},500)}});const sn=new j;sn.get(`/`,async e=>{try{let t=await Qe();return e.json(t)}catch(t){return console.error(`Error fetching usage:`,t),e.json({error:`Failed to fetch usage`},500)}});const Q=new j;Q.use(ft()),Q.use(Me()),Q.get(`/`,e=>e.text(`Server running`)),Q.route(`/chat/completions`,U),Q.route(`/models`,Z),Q.route(`/embeddings`,W),Q.route(`/usage`,sn),Q.route(`/token`,on),Q.route(`/v1/chat/completions`,U),Q.route(`/v1/models`,Z),Q.route(`/v1/embeddings`,W),Q.route(`/v1/messages`,X),Q.post(`/v1/messages/count_tokens`,jt);const cn=[],$=Date.now();function ln(){let e=async(e,t=0)=>{E.info(`Gracefully shutting down...`);let n=w.startSpan(`\u0063\u006f\u0070\u0069\u006c\u006f\u0074_api.server.shutdown`,{kind:O.INTERNAL,attributes:{"\u0063\u006f\u0070\u0069\u006c\u006f\u0074_api.shutdown.reason":e,"\u0063\u006f\u0070\u0069\u006c\u006f\u0074_api.shutdown.exit_code":t,"\u0063\u006f\u0070\u0069\u006c\u006f\u0074_api.uptime_seconds":(Date.now()-$)/1e3}});for(let e of cn)try{await e()}catch(e){E.error(`Error during cleanup:`,e)}n.end(),E.info(`Shutdown complete`),A.exit(t)};A.on(`SIGINT`,()=>e(`SIGINT`)),A.on(`SIGTERM`,()=>e(`SIGTERM`)),A.on(`uncaughtException`,t=>{E.error(`Uncaught exception:`,t),e(`uncaughtException`,1)}),A.on(`unhandledRejection`,(t,n)=>{E.error(`Unhandled promise rejection at:`,n,`reason:`,t),e(`unhandledRejection`,1)})}function un(){return`npx \u0063\u006f\u0070\u0069\u006c\u006f\u0074-api start`}function dn(e){let t=[un()];return t.push(`-p ${e.port}`),e.verbose&&t.push(`-v`),e.accountType!==`individual`&&t.push(`-a ${e.accountType}`),e.manual&&t.push(`--manual`),e.rateLimit&&t.push(`-r ${e.rateLimit}`),e.rateLimitWait&&t.push(`-w`),e.githubToken&&t.push(`-g ${e.\u0067\u0069\u0074\u0068\u0075\u0062Token}`),e.claudeCode&&t.push(`-c`),e.model&&t.push(`-m ${e.model}`),e.smallModel&&t.push(`-s ${e.smallModel}`),e.timeout&&t.push(`-t ${String(e.timeout)}`),e.showToken&&t.push(`--show-token`),e.disableConnectivityMonitoring&&t.push(`--disable-connectivity-monitoring`),t.join(` `)}async function fn(e,t){if(!e.claudeCode)return;Ae(S.models,`Models should be loaded by now`);let n,r;if(e.model&&e.smallModel){let t=S.models.data.map(e=>e.id);t.includes(e.model)||(E.error(`Invalid model: ${e.model}`),E.info(`Available services: \n${t.join(`
|
|
20
20
|
`)}`),A.exit(1)),t.includes(e.smallModel)||(E.error(`Invalid small model: ${e.smallModel}`),E.info(`Available services: \n${t.join(`
|
|
21
21
|
`)}`),A.exit(1)),n=e.model,r=e.smallModel,E.info(`Using service: ${n}`),E.info(`Using small model: ${r}`)}else e.model||e.smallModel?(E.error(`Both --model and --small-model must be specified for model selection`),A.exit(1)):(n=await E.prompt(`Select a model to use`,{type:`select`,options:S.models.data.map(e=>e.id)}),r=await E.prompt(`Select a small model to use`,{type:`select`,options:S.models.data.map(e=>e.id)}));let i=lt({ANTHROPIC_BASE_URL:t,ANTHROPIC_AUTH_TOKEN:`dummy`,ANTHROPIC_MODEL:n,ANTHROPIC_SMALL_FAST_MODEL:r},`\u0063\u006c\u0061\u0075\u0064\u0065`);try{Oe.writeSync(i),E.success(`Copied command to clipboard!`)}catch{E.warn(`Failed to copy to clipboard. Here is the command:`),E.log(i)}}function pn(e){let{options:t,version:n,runtime:r,githubUser:i}=e,a=w.startSpan(`\u0063\u006f\u0070\u0069\u006c\u006f\u0074_api.server.startup`,{kind:O.INTERNAL,attributes:b({config:{version:n,port:t.port,accountType:t.accountType,verbose:t.verbose,manualApprove:t.manual,rateLimitSeconds:t.rateLimit??0,rateLimitWait:t.rateLimitWait,timeoutMs:t.timeout,showToken:t.showToken,claudeCode:t.claudeCode,disableConnectivityMonitoring:t.disableConnectivityMonitoring,model:t.model,smallModel:t.smallModel,githubTokenSource:t.githubToken?`cli_arg`:`stored`},runtime:r,identity:{githubUser:i.login,machineId:S.machineId,sessionId:S.sessionId,vsCodeVersion:S.vsCodeVersion},connectivity:S.connectivity,modelsCount:S.models?.data.length??0,startupDurationMs:Date.now()-$},`\u0063\u006f\u0070\u0069\u006c\u006f\u0074_api`)});if(S.models?.data)for(let e of S.models.data)a.addEvent(`\u0063\u006f\u0070\u0069\u006c\u006f\u0074_api.model.available`,b(e,`model`));a.end(),v(l.INFO,`INFO`,`Server started`,b({version:n,port:t.port,accountType:t.accountType,githubUser:i.login,modelsCount:S.models?.data.length??0,startupDurationMs:Date.now()-$},`\u0063\u006f\u0070\u0069\u006c\u006f\u0074_api`))}async function mn(e){ln(),cn.push(()=>{E.debug(`Cleaning up connectivity monitor`),m.stop()},()=>{E.debug(`Cleaning up token management`),Ge()},async()=>{E.debug(`Shutting down OTel SDK`),await he()}),e.verbose&&(E.level=5,E.info(`Verbose logging enabled`)),S.accountType=e.accountType,e.accountType!==`individual`&&E.info(`Using ${e.accountType} plan account`),S.manualApprove=e.manual,S.rateLimitSeconds=e.rateLimit,S.rateLimitWait=e.rateLimitWait,S.showToken=e.showToken,S.timeoutMs=e.timeout,S.connectivity.enabled=!e.disableConnectivityMonitoring,e.tavilyApiKey&&(S.tavilyApiKey=e.tavilyApiKey,E.info(`Web search enabled via Tavily API`)),await y(),Ue();let t=await et(),n=tt();re(t,$),e.githubToken?(S.githubToken=e.githubToken,E.info(`Using provided Auth token`)):await R();let r;try{let{_s4:e}=await import(`./get-user-C8Q0uEyI.js`);r=await e(),await ie(S,r.login)}catch(e){E.error(`Failed to get user info for machine ID generation:`,e),E.error(`Cannot proceed without user information`),A.exit(1)}await Je(),await He(),pn({options:e,version:t,runtime:n,githubUser:r}),E.info(`Available services:`);for(let e of S.models?.data??[]){let t=e.capabilities.limits?.max_context_window_tokens,n=t?` (${t.toLocaleString()} tokens)`:``;E.info(`- ${e.id}${n}`)}let i=`http://localhost:${e.port}`;if(await fn(e,i),e.tavilyApiKey)E.box(`🔍 Web Search: Enabled ✓`);else{let t=dn(e);E.box([`🔍 Web Search: Enable web search by adding a Tavily API key.`,` Get your API key at: https://www.tavily.com`,``,` ${t} --tavily-api-key <your-key>`].join(`
|
|
22
22
|
`))}ke({fetch:Q.fetch,port:e.port})}const hn=T({meta:{name:`start`,description:`Start the API server`},args:{port:{alias:`p`,type:`string`,default:`4141`,description:`Port to listen on`},verbose:{alias:`v`,type:`boolean`,default:!1,description:`Enable verbose logging`},"account-type":{alias:`a`,type:`string`,default:`individual`,description:`Account type (individual, business, enterprise)`},manual:{type:`boolean`,default:!1,description:`Enable manual request approval`},"rate-limit":{alias:`r`,type:`string`,description:`Rate limit in seconds between requests`},wait:{alias:`w`,type:`boolean`,default:!1,description:`Wait instead of error when rate limit is hit. Has no effect if rate limit is not set`},"\u0067\u0069\u0074\u0068\u0075\u0062-token":{alias:`g`,type:`string`,description:"Provide auth token directly (must be generated using the `auth` subcommand)"},"\u0063\u006c\u0061\u0075\u0064\u0065-code":{alias:`c`,type:`boolean`,default:!1,description:`Generate a command to launch with API config`},model:{alias:`m`,type:`string`,description:`Model to use (requires --\u0063\u006c\u0061\u0075\u0064\u0065-code)`},"small-model":{alias:`s`,type:`string`,description:`Small/fast model to use (requires --\u0063\u006c\u0061\u0075\u0064\u0065-code)`},"show-token":{type:`boolean`,default:!1,description:`Show tokens on fetch and refresh`},timeout:{alias:`t`,type:`string`,description:`API timeout in milliseconds (default: 600000)`},"disable-connectivity-monitoring":{type:`boolean`,default:!1,description:`Disable automatic network monitoring for token refresh`},"tavily-api-key":{type:`string`,description:`Tavily API key for web search support (or set TAVILY_API_KEY env var)`}},run({args:e}){let n=e[`rate-limit`],r=n===void 0?void 0:Number.parseInt(n,10),i=e.timeout,a=i===void 0?t:Number.parseInt(i,10);return mn({port:Number.parseInt(e.port,10),verbose:e.verbose,accountType:e[`account-type`],manual:e.manual,rateLimit:r,rateLimitWait:e.wait,githubToken:e[`\u0067\u0069\u0074\u0068\u0075\u0062-token`],claudeCode:e[`\u0063\u006c\u0061\u0075\u0064\u0065-code`],model:e.model,smallModel:e[`small-model`],showToken:e[`show-token`],timeout:a,disableConnectivityMonitoring:e[`disable-connectivity-monitoring`],tavilyApiKey:e[`tavily-api-key`]||A.env.TAVILY_API_KEY})}}),gn=T({meta:{name:`\u0063\u006f\u0070\u0069\u006c\u006f\u0074-api`,description:`API proxy server for tool integration.`},subCommands:{auth:Ze,start:hn,"check-usage":$e,debug:st}});await we(gn);export{};
|