@negoziator/ai-commit
Version:
Writes your git commit messages for you with AI
6 lines (3 loc) • 2.81 kB
JavaScript
var C=Object.defineProperty;var f=(d,t)=>C(d,"name",{value:t,configurable:!0});import M from"http";import O from"https";import{L as $,g as L}from"./prompt-BGI0PV0L.mjs";import{K as m}from"./cli-DYm_4DqP.mjs";import"tty";import"node:url";import"node:child_process";import"node:string_decoder";import"node:util";import"node:process";import"node:tty";import"node:path";import"child_process";import"path";import"fs";import"node:timers/promises";import"node:os";import"node:events";import"node:v8";import"node:fs";import"node:stream";import"node:buffer";import"node:stream/promises";import"node:readline";import"fs/promises";import"os";import"url";const g=class g extends ${get name(){return"ollama"}validateConfig(){const{endpoint:t}=this.config;if(!t)throw new m("Ollama endpoint is not configured. Using default: http://localhost:11434")}async generateCommitMessage(t){try{const e=[{role:"system",content:L(this.config.locale,this.config.maxLength,this.config.type,t.projectConfig)},{role:"user",content:t.diff}],o=[];for(let s=0;s<t.completions;s++){const a=(await this.createChatCompletion(e)).message.content;a&&o.push(this.sanitizeMessage(a))}return{messages:this.deduplicateMessages(o)}}catch(r){const e=r;throw e.code==="ECONNREFUSED"?new m(`Cannot connect to Ollama at ${this.config.endpoint}. Make sure Ollama is running. Install from: https://ollama.com`):e.code==="ENOTFOUND"?new m(`Error connecting to ${e.hostname} (${e.syscall}). Check your OLLAMA_ENDPOINT configuration.`):e}}async createChatCompletion(t){const r={model:this.config.model,messages:t,stream:!1,options:{temperature:this.config.temperature,num_predict:this.config.maxCompletionTokens}},{response:e,data:o}=await this.httpPost(this.config.endpoint,"/api/chat",{},r);if(!e.statusCode||e.statusCode<200||e.statusCode>299){let n=`Ollama API Error: ${e.statusCode} - ${e.statusMessage}`;if(o)try{const s=JSON.parse(o);s.error&&(n+=`
${s.error}`)}catch{n+=`
${o}`}throw new m(n)}return JSON.parse(o)}async httpPost(t,r,e,o){return new Promise((n,s)=>{const c=new URL(t),a=c.protocol==="https:",y=a?O:M,l=JSON.stringify(o),i=y.request({hostname:c.hostname,port:c.port||(a?443:80),path:r,method:"POST",headers:{...e,"Content-Type":"application/json","Content-Length":Buffer.byteLength(l)},timeout:this.config.timeout},p=>{const u=[];p.on("data",w=>u.push(w)),p.on("end",()=>{n({request:i,response:p,data:Buffer.concat(u).toString()})})});i.on("error",s),i.on("timeout",()=>{i.destroy(),s(new m(`Time out error: request took over ${this.config.timeout}ms. Try increasing the \`timeout\` config or check if Ollama is running.`))}),i.write(l),i.end()})}sanitizeMessage(t){return t.trim().replace(/[\n\r]/g,"").replace(/(\w)\.$/,"$1")}deduplicateMessages(t){return Array.from(new Set(t))}};f(g,"OllamaProvider");let h=g;export{h as default};