UNPKG

aicommit2

Version:

A Reactive CLI that generates commit messages for Git and Jujutsu with various AI

2 lines (1 loc) 3.98 kB
import C from"chalk";import b from"openai";import{concatMap as P,from as y,map as A,catchError as I}from"rxjs";import{fromPromise as k}from"rxjs/internal/observable/innerFrom";import{A as G,l as O,a as R,b as D,c as E,d as _,e as F}from"./ai.service-d8e94c3a.mjs";import{i as N,g as H}from"./openai-5eabc0ae.mjs";import{g as M,b as L,m as $}from"./cli-8ee62906.mjs";import"fs";import"path";import"@pacote/xxhash";import"winston";import"http";import"https";import"net";import"tls";import"url";import"assert";import"tty";import"util";import"os";import"events";import"cleye";import"module";import"crypto";import"node:buffer";import"node:path";import"node:child_process";import"node:process";import"child_process";import"node:url";import"node:os";import"node:fs";import"buffer";import"stream";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"node:fs/promises";import"chokidar";import"rxjs/operators";class U extends G{constructor(t){super(t),this.params=t,this.generateStreamingCommitMessage$=()=>{const{generate:i,type:e}=this.params.config;return this.createStreamingCommitMessages$(s=>{this.streamChunks(s).catch(r=>s.error(r))},e,i)},this.streamChunks=async i=>{const e=this.params.stagedDiff.diff,{temperature:s,logging:r,maxTokens:n,timeout:g}=this.params.config,a=M(this.buildPromptOptions()),p=`Here is the diff: ${e}`,c=`${this.params.config.url}${this.params.config.path}`,m={Authorization:`Bearer ${this.params.config.key}`,"Content-Type":"application/json"};O(e,"commit","ChatGPT",this.params.config.model,c,m,r),R(e,"commit","ChatGPT",a,p,r);const x=N(this.params.config.model),u={messages:[{role:"system",content:a},{role:"user",content:p}],model:this.params.config.model,stream:!0,...x?{max_completion_tokens:n,temperature:1}:{max_tokens:n,top_p:this.params.config.topP,temperature:s}};D(e,"commit","ChatGPT",u,r);const v=Date.now();let f="";try{const S=await this.openAI.chat.completions.create(u,{timeout:g});for await(const T of S){const d=T.choices?.[0]?.delta?.content||"";d&&(f+=d,i.next(d))}const w=Date.now()-v;E(e,"commit","ChatGPT",{streamed:!0,totalLength:f.length},r),_(e,"commit","ChatGPT",w,f,r),i.complete()}catch(l){F(e,"commit","ChatGPT",l,r),i.error(l)}},this.colors={primary:"#74AA9C",secondary:"#FFF"},this.serviceName=C.bgHex(this.colors.primary).hex(this.colors.secondary).bold(`[ChatGPT${this.formatModelSuffix()}]`),this.errorPrefix=C.red.bold(`[ChatGPT${this.formatModelSuffix()}]`);const o=this.params.config.url||"https://api.openai.com",h=(this.params.config.path||"/v1/chat/completions").replace(/\/chat\/completions\/?$/,"");this.openAI=new b({apiKey:this.params.config.key,baseURL:`${o}${h}`})}getServiceSpecificErrorMessage(t){const o=t.message||"";return o.includes("API key")?"Invalid API key. Check your OpenAI API key in configuration":o.includes("quota")?"API quota exceeded. Check your OpenAI usage limits":o.includes("500")?"OpenAI server error. Try again later":null}generateCommitMessage$(){return this.params.config.stream||!1?this.generateStreamingCommitMessage$():k(this.generateMessage("commit")).pipe(P(o=>y(o)),A(this.formatAsChoice),I(this.handleError$))}generateCodeReview$(){return k(this.generateMessage("review")).pipe(P(t=>y(t)),A(this.formatCodeReviewAsChoice),I(this.handleError$))}async generateMessage(t){const o=this.params.stagedDiff.diff,{temperature:h,logging:i,generate:e,type:s,proxy:r,maxTokens:n,timeout:g}=this.params.config,a=this.buildPromptOptions(),p=t==="review"?L(a):M(a),c=await H("ChatGPT",this.params.config.url,this.params.config.path,this.params.config.key,this.params.config.model,o,g,n,h,this.params.config.topP,p,i,t,r,{recentCommits:this.params.recentCommits,branchName:this.params.branchName});return t==="review"?$(c.map(m=>this.parseCodeReview(m))):$(c.map(m=>this.parseMessage(m,s,e)))}}export{U as OpenAIService};