UNPKG

azion

Version:

Azion Packages for Edge Computing.

3 lines (2 loc) 3.31 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }var l="https://ai.azion.com/copilot/chat/completions",u=async(e,t)=>{let n=await fetch(l,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Token ${e}`},body:JSON.stringify(t)});if(!n.ok)throw new Error(`HTTP error! status: ${n.status}`);return n.json()},z=async function*(e,t){let n=await fetch(l,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Token ${e}`},body:JSON.stringify(t)});if(!n.ok)throw new Error(`HTTP error! status: ${n.status}`);let o=_optionalChain([n, 'access', _ => _.body, 'optionalAccess', _2 => _2.getReader, 'call', _3 => _3()]);if(!o)throw new Error("Response body is not readable");let r=new TextDecoder,a="";for(;;){let{done:m,value:h}=await o.read();if(m)break;a+=r.decode(h,{stream:!0});let A=a.split(` `);a=A.pop()||"";for(let c of A)if(c.startsWith("data: ")){let p=c.slice(6);if(p==="[DONE]")return;yield JSON.parse(p)}}};var R=process.env.AZION_DEBUG&&process.env.AZION_DEBUG==="true",s=e=>_nullishCoalesce(_nullishCoalesce(e, () => (process.env.AZION_TOKEN)), () => ("")),i=e=>_nullishCoalesce(e, () => (!!R));var d=async(e,t,n)=>{try{let o=await u(e,{...t,stream:!1});return _optionalChain([n, 'optionalAccess', _4 => _4.debug])&&console.log("AI Chat Response:",o),{data:o,error:null}}catch(o){return _optionalChain([n, 'optionalAccess', _5 => _5.debug])&&console.error("Error in AI chat:",o),{data:null,error:o instanceof Error?o:new Error("Unknown error occurred")}}},I= exports.streamChatMethod =async function*(e,t,n){try{let o=z(e,{...t,stream:!0});for await(let r of o)_optionalChain([n, 'optionalAccess', _6 => _6.debug])&&console.log("AI Stream Chunk:",r),yield{data:r,error:null}}catch(o){_optionalChain([n, 'optionalAccess', _7 => _7.debug])&&console.error("Error in AI stream chat:",o),yield{data:null,error:o instanceof Error?o:new Error("Unknown error occurred")}}},O= exports.chat =(e,t)=>d(s(),e,{...t,debug:i(_optionalChain([t, 'optionalAccess', _8 => _8.debug]))}),w= exports.streamChat =(e,t)=>I(s(),e,{...t,debug:i(_optionalChain([t, 'optionalAccess', _9 => _9.debug]))}),f= exports.createClient =e=>{let t=s(_optionalChain([e, 'optionalAccess', _10 => _10.token])),n=i(_optionalChain([e, 'optionalAccess', _11 => _11.options, 'optionalAccess', _12 => _12.debug]));return{chat:(o,r)=>d(t,o,{..._optionalChain([e, 'optionalAccess', _13 => _13.options]),...r,debug:n}),streamChat:(o,r)=>I(t,o,{..._optionalChain([e, 'optionalAccess', _14 => _14.options]),...r,debug:n})}};var k=f;exports.chat = O; exports.chatMethod = d; exports.createClient = f; exports.default = k; exports.streamChat = w; exports.streamChatMethod = I;