clog-ai
Version:
Generate commit log with AI
3 lines (2 loc) • 13.4 kB
JavaScript
"use strict";function e(e){return e&&"object"==typeof e&&"default"in e?e.default:e}Object.defineProperty(exports,"__esModule",{value:!0});var t,r=e(require("axios")),n=e(require("child_process")),o=e(require("loading-cli")),i=require("fs"),a=require("path"),c=require("os"),s=e(require("readline"));function u(){u=function(){return t};var e,t={},r=Object.prototype,n=r.hasOwnProperty,o=Object.defineProperty||function(e,t,r){e[t]=r.value},i="function"==typeof Symbol?Symbol:{},a=i.iterator||"@@iterator",c=i.asyncIterator||"@@asyncIterator",s=i.toStringTag||"@@toStringTag";function l(e,t,r){return Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}),e[t]}try{l({},"")}catch(e){l=function(e,t,r){return e[t]=r}}function f(e,t,r,n){var i=Object.create((t&&t.prototype instanceof y?t:y).prototype),a=new P(n||[]);return o(i,"_invoke",{value:z(e,r,a)}),i}function p(e,t,r){try{return{type:"normal",arg:e.call(t,r)}}catch(e){return{type:"throw",arg:e}}}t.wrap=f;var h="suspendedStart",d="executing",g="completed",m={};function y(){}function v(){}function w(){}var x={};l(x,a,(function(){return this}));var _=Object.getPrototypeOf,b=_&&_(_(q([])));b&&b!==r&&n.call(b,a)&&(x=b);var L=w.prototype=y.prototype=Object.create(x);function k(e){["next","throw","return"].forEach((function(t){l(e,t,(function(e){return this._invoke(t,e)}))}))}function E(e,t){function r(o,i,a,c){var s=p(e[o],e,i);if("throw"!==s.type){var u=s.arg,l=u.value;return l&&"object"==typeof l&&n.call(l,"__await")?t.resolve(l.__await).then((function(e){r("next",e,a,c)}),(function(e){r("throw",e,a,c)})):t.resolve(l).then((function(e){u.value=e,a(u)}),(function(e){return r("throw",e,a,c)}))}c(s.arg)}var i;o(this,"_invoke",{value:function(e,n){function o(){return new t((function(t,o){r(e,n,t,o)}))}return i=i?i.then(o,o):o()}})}function z(t,r,n){var o=h;return function(i,a){if(o===d)throw new Error("Generator is already running");if(o===g){if("throw"===i)throw a;return{value:e,done:!0}}for(n.method=i,n.arg=a;;){var c=n.delegate;if(c){var s=O(c,n);if(s){if(s===m)continue;return s}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if(o===h)throw o=g,n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);o=d;var u=p(t,r,n);if("normal"===u.type){if(o=n.done?g:"suspendedYield",u.arg===m)continue;return{value:u.arg,done:n.done}}"throw"===u.type&&(o=g,n.method="throw",n.arg=u.arg)}}}function O(t,r){var n=r.method,o=t.iterator[n];if(o===e)return r.delegate=null,"throw"===n&&t.iterator.return&&(r.method="return",r.arg=e,O(t,r),"throw"===r.method)||"return"!==n&&(r.method="throw",r.arg=new TypeError("The iterator does not provide a '"+n+"' method")),m;var i=p(o,t.iterator,r.arg);if("throw"===i.type)return r.method="throw",r.arg=i.arg,r.delegate=null,m;var a=i.arg;return a?a.done?(r[t.resultName]=a.value,r.next=t.nextLoc,"return"!==r.method&&(r.method="next",r.arg=e),r.delegate=null,m):a:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,m)}function S(e){var t={tryLoc:e[0]};1 in e&&(t.catchLoc=e[1]),2 in e&&(t.finallyLoc=e[2],t.afterLoc=e[3]),this.tryEntries.push(t)}function j(e){var t=e.completion||{};t.type="normal",delete t.arg,e.completion=t}function P(e){this.tryEntries=[{tryLoc:"root"}],e.forEach(S,this),this.reset(!0)}function q(t){if(t||""===t){var r=t[a];if(r)return r.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var o=-1,i=function r(){for(;++o<t.length;)if(n.call(t,o))return r.value=t[o],r.done=!1,r;return r.value=e,r.done=!0,r};return i.next=i}}throw new TypeError(typeof t+" is not iterable")}return v.prototype=w,o(L,"constructor",{value:w,configurable:!0}),o(w,"constructor",{value:v,configurable:!0}),v.displayName=l(w,s,"GeneratorFunction"),t.isGeneratorFunction=function(e){var t="function"==typeof e&&e.constructor;return!!t&&(t===v||"GeneratorFunction"===(t.displayName||t.name))},t.mark=function(e){return Object.setPrototypeOf?Object.setPrototypeOf(e,w):(e.__proto__=w,l(e,s,"GeneratorFunction")),e.prototype=Object.create(L),e},t.awrap=function(e){return{__await:e}},k(E.prototype),l(E.prototype,c,(function(){return this})),t.AsyncIterator=E,t.async=function(e,r,n,o,i){void 0===i&&(i=Promise);var a=new E(f(e,r,n,o),i);return t.isGeneratorFunction(r)?a:a.next().then((function(e){return e.done?e.value:a.next()}))},k(L),l(L,s,"Generator"),l(L,a,(function(){return this})),l(L,"toString",(function(){return"[object Generator]"})),t.keys=function(e){var t=Object(e),r=[];for(var n in t)r.push(n);return r.reverse(),function e(){for(;r.length;){var n=r.pop();if(n in t)return e.value=n,e.done=!1,e}return e.done=!0,e}},t.values=q,P.prototype={constructor:P,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=e,this.done=!1,this.delegate=null,this.method="next",this.arg=e,this.tryEntries.forEach(j),!t)for(var r in this)"t"===r.charAt(0)&&n.call(this,r)&&!isNaN(+r.slice(1))&&(this[r]=e)},stop:function(){this.done=!0;var e=this.tryEntries[0].completion;if("throw"===e.type)throw e.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var r=this;function o(n,o){return c.type="throw",c.arg=t,r.next=n,o&&(r.method="next",r.arg=e),!!o}for(var i=this.tryEntries.length-1;i>=0;--i){var a=this.tryEntries[i],c=a.completion;if("root"===a.tryLoc)return o("end");if(a.tryLoc<=this.prev){var s=n.call(a,"catchLoc"),u=n.call(a,"finallyLoc");if(s&&u){if(this.prev<a.catchLoc)return o(a.catchLoc,!0);if(this.prev<a.finallyLoc)return o(a.finallyLoc)}else if(s){if(this.prev<a.catchLoc)return o(a.catchLoc,!0)}else{if(!u)throw new Error("try statement without catch or finally");if(this.prev<a.finallyLoc)return o(a.finallyLoc)}}}},abrupt:function(e,t){for(var r=this.tryEntries.length-1;r>=0;--r){var o=this.tryEntries[r];if(o.tryLoc<=this.prev&&n.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var i=o;break}}i&&("break"===e||"continue"===e)&&i.tryLoc<=t&&t<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=e,a.arg=t,i?(this.method="next",this.next=i.finallyLoc,m):this.complete(a)},complete:function(e,t){if("throw"===e.type)throw e.arg;return"break"===e.type||"continue"===e.type?this.next=e.arg:"return"===e.type?(this.rval=this.arg=e.arg,this.method="return",this.next="end"):"normal"===e.type&&t&&(this.next=t),m},finish:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var r=this.tryEntries[t];if(r.finallyLoc===e)return this.complete(r.completion,r.afterLoc),j(r),m}},catch:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var r=this.tryEntries[t];if(r.tryLoc===e){var n=r.completion;if("throw"===n.type){var o=n.arg;j(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,r,n){return this.delegate={iterator:q(t),resultName:r,nextLoc:n},"next"===this.method&&(this.arg=e),m}},t}function l(e,t,r,n,o,i,a){try{var c=e[i](a),s=c.value}catch(e){return void r(e)}c.done?t(s):Promise.resolve(s).then(n,o)}function f(e){return function(){var t=this,r=arguments;return new Promise((function(n,o){var i=e.apply(t,r);function a(e){l(i,n,o,a,c,"next",e)}function c(e){l(i,n,o,a,c,"throw",e)}a(void 0)}))}}var p=a.join(c.homedir(),".config/clog-ai/config.json"),h=null==(t=process.argv[2])?void 0:t.trim(),d=process.argv.includes("--verbose")||process.argv.includes("-v");if("init"==h){i.existsSync(p)&&(console.log("config file already exists, please edit config file: "+a.resolve(p)),process.exit(0));var g=a.dirname(p);i.existsSync(g)||i.mkdirSync(g,{recursive:!0}),i.writeFileSync(p,JSON.stringify({language:"zh",datasource:"openai",openai_api_key:"",azure_api_key:"",azure_deployment_id:"",azure_base_url:"",azure_model:"",azure_api_version:""},null,2)),console.log("init success, please edit config file: "+a.resolve(p)),process.exit(0)}i.existsSync(p)||(console.log("please run this command first: clog-ai init"),process.exit(0));var m=y();function y(){var e=i.readFileSync(p).toString();return JSON.parse(e||"{}")}function v(e){return w.apply(this,arguments)}function w(){return(w=f(u().mark((function e(t){return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,r.post(m.azure_base_url+"/openai/deployments/"+m.azure_deployment_id+"/chat/completions?api-version="+m.azure_api_version,{model:"gpt-3.5-turbo-16k",messages:[{role:"user",content:t}],temperature:0,top_p:1,frequency_penalty:0,presence_penalty:0},{headers:{"api-key":m.azure_api_key},timeout:1e5});case 2:return e.abrupt("return",e.sent.data.choices[0].message.content);case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function x(e){return _.apply(this,arguments)}function _(){return(_=f(u().mark((function e(t){return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,r.post("https://api.openai.com/v1/chat/completions",{model:"gpt-3.5-turbo-16k",messages:[{role:"user",content:t}],temperature:0,top_p:1,frequency_penalty:0,presence_penalty:0},{headers:{Authorization:"Bearer "+m.openai_api_key},timeout:1e5});case 2:return e.abrupt("return",e.sent.data.choices[0].message.content);case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}m.datasource||(console.log("Please specify the datasource in the configuration file: "+a.resolve(p)),process.exit(0)),["azure","openai"].includes(m.datasource)||(console.log("Invalid datasource. Please check the configuration file: "+a.resolve(p)),process.exit(0)),"openai"!==m.datasource||m.openai_api_key||(console.log("Data source is OpenAI, but the corresponding configuration is missing. Please add it in the configuration file: "+a.resolve(p)),process.exit(0)),"azure"!==m.datasource||m.azure_api_key&&m.azure_deployment_id&&m.azure_base_url&&m.azure_model&&m.azure_api_version||(console.log("Data source is Azure, but the corresponding configuration is missing. Please add it in the configuration file: "+a.resolve(p)),process.exit(0)),exports.default=f(u().mark((function e(){var t,r,i,a,c,l,f;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(t=n.execSync("git diff HEAD").toString().substring(0,8e3),r=("zh"==m.language?"现有以下的 git diff 输出:\n<diff>\n{{diff}}\n</diff>\n\n角色:你是一个根据 git diff 信息生成 git commit log 的工具,你会为一个复杂的变更生成一条精简的 commit log.\n\n以下是 git commit log 的书写前缀及其对应的使用场景,请先挑选一个符合本次提交的前缀:\n* 新功能开发: feat\n* 修复 bug: fix\n* 文档修改: docs\n* 格式化代码: style\n* 重构代码: refactor\n* 增加测试: test\n* 更新版本号、配置文件等: chore\n\n然后按照以下的格式输出最终的 commit log:\n<output>\n[前缀]: 本次所有变更的简要描述\n\n* 每条修改的具体描述(不超过3条)\n</output>\n\n要求:\n* 使用中文输出\n* 如果有多种类型的修改,也强制压缩到一条 commit log 中,挑选出最重要的更改进行描述\n* 描述修改的功能,不描述修改了什么文件,也不描述修改了什么代码\n\n请根据 git diff 的内容以及上述的要求,直接给出最终的 commit log,务必将最终的结果包裹在xml标签 <output> 和 </output> 中输出":"This is the git diff output:\n<diff>\n{{diff}}\n</diff>\n\nRole: You are a tool that generates git commit log based on git diff information.\n\nThe following is the prefix of the git commit log and its corresponding usage scenario:\n* feat: new feature\n* fix: fix bug\n* docs: documentation\n* style: format (code changes that do not affect the running of the code)\n* refactor: refactoring (code changes that are neither new features nor bug fixes)\n* test: add test\n* chore: changes in the build process or auxiliary tools\n\nThe final output format:\n<output>\n[prefix]: brief description of the change\n\n* description of the modified content\n* description of the modified content\n...\n</output>\n\nOther requirements:\n* Start your commit log with the agreed prefix\n* Only one commit log is required\n* The description of the modified content does not exceed 3\n\nPlease give the final commit log directly according to the above requirements, and wrap it in the <output> and </output> tags.").replace("{{diff}}",t),i=o({text:"Generating commit log...",color:"yellow",interval:100,frames:["◰","◳","◲","◱"]}).start(),e.prev=3,d&&(console.log("--------- Input Prompt ----------"),console.log(r)),"azure"!==m.datasource){e.next=11;break}return e.next=8,v(r);case 8:e.t0=e.sent,e.next=14;break;case 11:return e.next=13,x(r);case 13:e.t0=e.sent;case 14:if(c=e.t0,d&&(console.log("\n--------- Output ----------"),console.log(c),console.log("-------------------")),l=null==(a=c.match(/<output>([\s\S]*)<\/output>/))||null==(a=a[1])?void 0:a.trim()){e.next=19;break}throw new Error("No commit log generated");case 19:i.stop(),i.succeed("Generate commit log success"),console.log("-------------------"),console.log(l),f=s.createInterface({input:process.stdin,output:process.stdout}),console.log("-------------------"),f.question("Submit git commit with the log? (yes/no) ",(function(e){"yes"!==e.toLowerCase()&&"y"!==e.toLowerCase()||(n.execSync('git commit -m "'+l+'"'),i.succeed("commit success")),f.close()})),e.next=33;break;case 28:e.prev=28,e.t1=e.catch(3),i.stop(),i.fail("Generate commit log fail"),console.error(e.t1.message);case 33:case"end":return e.stop()}}),e,null,[[3,28]])})));
//# sourceMappingURL=clog-ai.cjs.production.min.js.map