aicommit2
Version:
A Reactive CLI that generates commit messages for Git and Jujutsu with various AI
2 lines (1 loc) • 4.28 kB
JavaScript
import d from"chalk";import{concatMap as f,from as g,map as u,catchError as y}from"rxjs";import{fromPromise as P}from"rxjs/internal/observable/innerFrom";import{A as v,l as w,a as C,b as A,c as k,e as x,d as b}from"./ai.service-d8e94c3a.mjs";import{b as I,g as M,H as R}from"./cli-8ee62906.mjs";import"fs";import"path";import"@pacote/xxhash";import"winston";import"cleye";import"module";import"crypto";import"os";import"node:buffer";import"node:path";import"node:child_process";import"node:process";import"child_process";import"node:url";import"node:os";import"assert";import"events";import"node:fs";import"buffer";import"stream";import"util";import"node:util";import"inquirer";import"fs/promises";import"readline";import"figlet";import"gradient-string";import"ora";import"inquirer-reactive-list-prompt";import"winston-daily-rotate-file";import"axios";import"url";import"node:fs/promises";import"chokidar";import"rxjs/operators";class S extends v{constructor(t){super(t),this.params=t,this.apiKey="",this.colors={primary:"#20808D",secondary:"#FFF"},this.serviceName=d.bgHex(this.colors.primary).hex(this.colors.secondary).bold(`[Perplexity${this.formatModelSuffix()}]`),this.errorPrefix=d.red.bold(`[Perplexity${this.formatModelSuffix()}]`),this.apiKey=this.params.config.key}getServiceSpecificErrorMessage(t){const e=t.message||"";return e.includes("API key")||e.includes("api_key")?"Invalid API key. Check your Perplexity API key in configuration":e.includes("rate_limit")||e.includes("Rate limit")?"Rate limit exceeded. Wait a moment and try again, or upgrade your Perplexity plan":e.includes("model")||e.includes("Model")?"Model not found or not accessible. Check if the Perplexity model name is correct":e.includes("overloaded")||e.includes("capacity")?"Perplexity service is overloaded. Try again in a few minutes":e.includes("403")||e.includes("Forbidden")?"Access denied. Your API key may not have permission for this Perplexity model":e.includes("404")||e.includes("Not Found")?"Model or endpoint not found. Check your Perplexity model configuration":e.includes("500")||e.includes("Internal Server Error")?"Perplexity server error. Try again later":null}generateCommitMessage$(){return P(this.generateMessage("commit")).pipe(f(t=>g(t)),u(this.formatAsChoice),y(this.handleError$))}generateCodeReview$(){return P(this.generateMessage("review")).pipe(f(t=>g(t)),u(this.formatCodeReviewAsChoice),y(this.handleError$))}extractJSONFromError(t){const e=/[{[]{1}([,:{}[\]0-9.\-+Eaeflnr-u \n\r\t]|".*?")+[}\]]{1}/gis,r=t.match(e);if(r){const o={};for(const i of r)try{Object.assign(o,JSON.parse(i))}catch{}return Object.keys(o).length>0?o:{error:{message:"Unknown error"}}}return{error:{message:"Unknown error"}}}async generateMessage(t){const e=this.params.stagedDiff.diff,{logging:r,generate:o,type:i}=this.params.config,n=this.buildPromptOptions(),p=t==="review"?I(n):M(n),a=`Here is the diff: ${e}`,c=`${this.params.config.url||"https://api.perplexity.ai"}/chat/completions`,s={Authorization:`Bearer ${this.apiKey}`,"content-type":"application/json"};w(e,t,"Perplexity",this.params.config.model,c,s,r),C(e,t,"Perplexity",p,a,r);const m=await this.createChatCompletions(p,a,t);return t==="review"?this.parseCodeReview(m):this.parseMessage(m,i,o)}async createChatCompletions(t,e,r){const o=this.params.stagedDiff.diff,{logging:i}=this.params.config,n={model:this.params.config.model,messages:[{role:"system",content:t},{role:"user",content:e}],temperature:this.params.config.temperature,top_p:this.params.config.topP,max_tokens:this.params.config.maxTokens,stream:!1};A(o,r,"Perplexity",n,i);const p=Date.now();try{const a=this.params.config.url||"https://api.perplexity.ai",l=await new R({method:"POST",baseURL:`${a}/chat/completions`,timeout:this.params.config.timeout}).setHeaders({Authorization:`Bearer ${this.apiKey}`,"content-type":"application/json"}).setBody(n).execute(),c=Date.now()-p,s=l.data;if(k(o,r,"Perplexity",s,i),!s.choices||s.choices.length===0||!s.choices[0].message?.content)throw x(o,r,"Perplexity",{message:"No Content on response",result:s},i),new Error("No Content on response. Please open a Bug report");const h=s.choices[0].message.content;return b(o,r,"Perplexity",c,h,i),h}catch(a){throw x(o,r,"Perplexity",a,i),a}}}export{S as PerplexityService};