aicommit2
Version:
A Reactive CLI that generates commit messages for Git and Jujutsu with various AI
2 lines (1 loc) • 4.43 kB
JavaScript
import h from"chalk";import{concatMap as f,from as u,map as g,catchError as C}from"rxjs";import{fromPromise as y}from"rxjs/internal/observable/innerFrom";import{A,l as b,a as w,b as P,c as I,e as v,d as M}from"./ai.service-d8e94c3a.mjs";import{b as k,g as R,t as x,H as $}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 A{constructor(t){super(t),this.params=t,this.apiKey="",this.colors={primary:"#e28c58",secondary:"#fff"},this.serviceName=h.bgHex(this.colors.primary).hex(this.colors.secondary).bold(`[Codestral${this.formatModelSuffix()}]`),this.errorPrefix=h.red.bold(`[Codestral${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 Codestral API key in configuration":e.includes("rate_limit")||e.includes("Rate limit")?"Rate limit exceeded. Wait a moment and try again, or upgrade your Codestral plan":e.includes("model")||e.includes("Model")?"Model not found or not accessible. Check if the Codestral model name is correct":e.includes("Invalid model type")?"Invalid model type. Use supported models: codestral-latest, codestral-2501":e.includes("overloaded")||e.includes("capacity")?"Codestral 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 Codestral model":e.includes("404")||e.includes("Not Found")?"Model or endpoint not found. Check your Codestral model configuration":e.includes("500")||e.includes("Internal Server Error")?"Codestral server error. Try again later":null}generateCommitMessage$(){return y(this.generateMessage("commit")).pipe(f(t=>u(t)),g(this.formatAsChoice),C(this.handleError$))}generateCodeReview$(){return y(this.generateMessage("review")).pipe(f(t=>u(t)),g(this.formatCodeReviewAsChoice),C(this.handleError$))}async generateMessage(t){const e=this.params.stagedDiff.diff,{logging:o,generate:s,type:m}=this.params.config,i=this.buildPromptOptions(),n=t==="review"?k(i):R(i);this.checkAvailableModels();const a=this.buildUserPrompt(e,t),c=`${this.params.config.url||"https://codestral.mistral.ai"}/v1/chat/completions`,r={Authorization:`Bearer ${this.apiKey}`,"content-type":"application/json"};b(e,t,"Codestral",this.params.config.model,c,r,o),w(e,t,"Codestral",n,a,o);const l=await this.createChatCompletions(n,t);return t==="review"?this.parseCodeReview(l):this.parseMessage(l,m,s)}checkAvailableModels(){if(["codestral-latest","codestral-2501"].includes(this.params.config.model))return!0;throw new Error("Invalid model type of Codestral AI")}async createChatCompletions(t,e){const o=this.params.stagedDiff.diff,{logging:s}=this.params.config,m=this.params.config.url||"https://codestral.mistral.ai",i={model:this.params.config.model,messages:[{role:"system",content:t},{role:"user",content:this.buildUserPrompt(this.params.stagedDiff.diff,e)}],temperature:this.params.config.temperature,top_p:this.params.config.topP,max_tokens:this.params.config.maxTokens,stream:!1,safe_prompt:!1,random_seed:x(10,1e3)};e==="commit"&&(i.response_format={type:"json_object"}),P(o,e,"Codestral",i,s);const n=Date.now();try{const d=await new $({method:"POST",baseURL:`${m}/v1/chat/completions`,timeout:this.params.config.timeout}).setHeaders({Authorization:`Bearer ${this.apiKey}`,"content-type":"application/json"}).setBody(i).execute(),c=Date.now()-n,r=d.data;if(I(o,e,"Codestral",r,s),!r.choices||r.choices.length===0||!r.choices[0].message?.content)throw v(o,e,"Codestral",{message:"No Content on response",result:r},s),new Error("No Content on response. Please open a Bug report");const p=r.choices[0].message.content;return M(o,e,"Codestral",c,p,s),p}catch(a){throw v(o,e,"Codestral",a,s),a}}}export{S as CodestralService};