UNPKG

@catalystlabs/tryai

Version:

Dead simple AI library. One line setup. Zero config. Just works.

5 lines (4 loc) 3.5 kB
var u=class{config;baseURL;headers;constructor(t){if(this.config=t,this.baseURL=t.baseURL||process.env.LLMROUTER_URL||"http://localhost:8000",!this.baseURL)throw new Error("LLMRouter URL is required. Set LLMROUTER_URL environment variable or pass baseURL in config.");this.headers={"Content-Type":"application/json","User-Agent":"TryAI/1.0"},(t.apiKey||process.env.LLMROUTER_API_KEY)&&(this.headers.Authorization=`Bearer ${t.apiKey||process.env.LLMROUTER_API_KEY}`)}async complete(t,e){let o=typeof t=="string"?t:this.messagesToPrompt(t),r=await this.getRouting(o,e),s=Date.now(),i=await this.executeWithRouting(o,r,e),n=Date.now()-s;return{...i,metadata:{...i.metadata,routingId:r.routing_id,selectedModel:r.selected_model,routingReason:r.routing_reason,dimensionalScores:r.dimensional_scores,priorityMode:r.priority_mode,estimatedCost:r.estimated_cost,cached:r.cached,responseTime:n},submitFeedback:async a=>{await this.submitFeedback({routing_id:r.routing_id,response_time:n,...a})}}}async stream(t,e){let o=typeof t=="string"?t:this.messagesToPrompt(t),r=await this.getRouting(o,e),s=await fetch(`${this.baseURL}/execute`,{method:"POST",headers:this.headers,body:JSON.stringify({routing_id:r.routing_id,prompt:o,model:r.selected_model,priority_mode:e?.priority||this.config.defaultPriority||"balanced",stream:!0,temperature:e?.temperature,max_tokens:e?.maxTokens})});if(!s.ok)throw new Error(`Router execution failed: ${s.statusText}`);return this.parseSSEStream(s.body)}async getRouting(t,e){let o=await fetch(`${this.baseURL}/route`,{method:"POST",headers:this.headers,body:JSON.stringify({prompt:t,priority_mode:e?.priority||this.config.defaultPriority||"balanced",max_tokens:e?.maxTokens,temperature:e?.temperature,tools:e?.tools,response_format:e?.responseFormat})});if(!o.ok){let r=await o.text();throw new Error(`Routing failed: ${r}`)}return o.json()}async executeWithRouting(t,e,o){let r=await fetch(`${this.baseURL}/execute`,{method:"POST",headers:this.headers,body:JSON.stringify({routing_id:e.routing_id,prompt:t,model:e.selected_model,priority_mode:o?.priority||e.priority_mode,stream:!1,temperature:o?.temperature,max_tokens:o?.maxTokens,tools:o?.tools,response_format:o?.responseFormat})});if(!r.ok){let i=await r.text();throw new Error(`Execution failed: ${i}`)}let s=await r.json();return{text:s.content||s.text||"",usage:s.usage,model:s.model||e.selected_model,metadata:{cost:s.cost,finishReason:s.finish_reason}}}async submitFeedback(t){try{let e=await fetch(`${this.baseURL}/feedback`,{method:"POST",headers:this.headers,body:JSON.stringify(t)});e.ok||console.error("Failed to submit feedback:",await e.text())}catch(e){console.error("Error submitting feedback:",e)}}messagesToPrompt(t){return t.map(e=>typeof e=="string"?e:e.content?e.content:e.text?e.text:JSON.stringify(e)).join(` `)}async*parseSSEStream(t){let e=t.getReader(),o=new TextDecoder,r="";try{for(;;){let{done:s,value:i}=await e.read();if(s)break;r+=o.decode(i,{stream:!0});let n=r.split(` `);r=n.pop()||"";for(let d of n)if(d.startsWith("data: ")){let a=d.slice(6);if(a==="[DONE]")return;try{yield JSON.parse(a)}catch(c){console.error("Failed to parse SSE chunk:",c)}}}}finally{e.releaseLock()}}async listModels(){return["auto"]}async embeddings(t){throw new Error("Embeddings not supported by Router provider. Use a specific provider.")}async moderate(t){throw new Error("Moderation not supported by Router provider. Use a specific provider.")}};export{u as a}; //# sourceMappingURL=chunk-DCIJQ6HA.mjs.map