From f7bf4eed10fd6c0e193572fb443cab4ca996654c Mon Sep 17 00:00:00 2001 From: Alis Akers Date: Thu, 30 Jan 2025 18:18:16 -0800 Subject: [PATCH 1/3] chore: upgrade typescript to ^5.7.3 and openai to ^4.80.0 --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 0683fb1..2ab7298 100644 --- a/package.json +++ b/package.json @@ -8,10 +8,10 @@ "@types/node": "^18.16.3", "minimist": "^1.2.8", "prettier": "^2.8.8", - "typescript": "^5.0.4" + "typescript": "^5.7.3" }, "dependencies": { - "openai": "^3.2.1" + "openai": "^4.80.0" }, "scripts": { "build": "npx tsc", From f08791aafc604bb8fd627103e48a3467710ceb9d Mon Sep 17 00:00:00 2001 From: Alis Akers Date: Thu, 30 Jan 2025 18:18:48 -0800 Subject: [PATCH 2/3] feat: upgrade openai library to v4.81.0 and typescript to v5.7.3 --- pnpm-lock.yaml | 145 ++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 119 insertions(+), 26 deletions(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 936ac3c..b644e2c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -9,8 +9,8 @@ importers: .: dependencies: openai: - specifier: ^3.2.1 - version: 3.3.0 + specifier: ^4.80.0 + version: 4.81.0 devDependencies: '@types/minimist': specifier: ^1.2.2 @@ -25,7 +25,7 @@ importers: specifier: ^2.8.8 version: 2.8.8 typescript: - specifier: ^5.0.4 + specifier: ^5.7.3 version: 5.7.3 packages: @@ -33,15 +33,23 @@ packages: '@types/minimist@1.2.5': resolution: {integrity: sha512-hov8bUuiLiyFPGyFPE1lwWhmzYbirOXQNNo40+y3zow8aFVTeyn3VWL0VFFfdNddA8S4Vf0Tc062rzyNr7Paag==} + '@types/node-fetch@2.6.12': + resolution: {integrity: sha512-8nneRWKCg3rMtF69nLQJnOYUcbafYeFSjqkw3jCRLsqkWFlHaoQrr5mXmofFGOx3DKn7UfmBMyov8ySvLRVldA==} + '@types/node@18.19.74': resolution: {integrity: sha512-HMwEkkifei3L605gFdV+/UwtpxP6JSzM+xFk2Ia6DNFSwSVBRh9qp5Tgf4lNFOMfPVuU0WnkcWpXZpgn5ufO4A==} + abort-controller@3.0.0: + resolution: {integrity: sha512-h8lQ8tacZYnR3vNQTgibj+tODHI5/+l06Au2Pcriv/Gmet0eaj4TwWH41sO9wnHDiQsEj19q0drzdWdeAHtweg==} + engines: {node: '>=6.5'} + + agentkeepalive@4.6.0: + resolution: {integrity: sha512-kja8j7PjmncONqaTsB8fQ+wE2mSU2DJ9D4XKoJ5PFWIdRMa6SLSN1ff4mOr4jCbfRSsxR4keIiySJU0N9T5hIQ==} + engines: {node: '>= 8.0.0'} + asynckit@0.4.0: resolution: {integrity: sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==} - axios@0.26.1: - resolution: {integrity: sha512-fPwcX4EvnSHuInCMItEhAGnaSEXRBjtzh9fOtsE6E1G6p7vl7edEeZe11QHf18+6+9gR5PbKV/sGKNaD8YaMeA==} - combined-stream@1.0.8: resolution: {integrity: sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==} engines: {node: '>= 0.8'} @@ -50,19 +58,24 @@ packages: resolution: {integrity: sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==} engines: {node: '>=0.4.0'} - follow-redirects@1.15.9: - resolution: {integrity: sha512-gew4GsXizNgdoRyqmyfMHyAmXsZDk6mHkSxZFCzW9gwlbtOW44CDtYavM+y+72qD/Vq2l550kMF52DT8fOLJqQ==} - engines: {node: '>=4.0'} - peerDependencies: - debug: '*' - peerDependenciesMeta: - debug: - optional: true + event-target-shim@5.0.1: + resolution: {integrity: sha512-i/2XbnSz/uxRCU6+NdVJgKWDTM427+MqYbkQzD321DuCQJUqOuJKIA0IM2+W2xtYHdKOmZ4dR6fExsd4SXL+WQ==} + engines: {node: '>=6'} + + form-data-encoder@1.7.2: + resolution: {integrity: sha512-qfqtYan3rxrnCk1VYaA4H+Ms9xdpPqvLZa6xmMgFvhO32x7/3J/ExcTd6qpxM0vH2GdMI+poehyBZvqfMTto8A==} form-data@4.0.1: resolution: {integrity: sha512-tzN8e4TX8+kkxGPK8D5u0FNmjPUjw3lwC9lSLxxoB/+GtsJG91CO8bSWy73APlgAZzZbXEYZJuxjkHH2w+Ezhw==} engines: {node: '>= 6'} + formdata-node@4.4.1: + resolution: {integrity: sha512-0iirZp3uVDjVGt9p49aTaqjk84TrglENEDuqfdlZQ1roC9CWlPk6Avf8EEnZNcAqPonwkG35x4n3ww/1THYAeQ==} + engines: {node: '>= 12.20'} + + humanize-ms@1.2.1: + resolution: {integrity: sha512-Fl70vYtsAFb/C06PTS9dZBo7ihau+Tu/DNCk/OyHhea07S+aeMWpFFkUaXRa8fI+ScZbEI8dfSxwY7gxZ9SAVQ==} + mime-db@1.52.0: resolution: {integrity: sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==} engines: {node: '>= 0.6'} @@ -74,14 +87,42 @@ packages: minimist@1.2.8: resolution: {integrity: sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==} - openai@3.3.0: - resolution: {integrity: sha512-uqxI/Au+aPRnsaQRe8CojU0eCR7I0mBiKjD3sNMzY6DaC1ZVrc85u98mtJW6voDug8fgGN+DIZmTDxTthxb7dQ==} + ms@2.1.3: + resolution: {integrity: sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==} + + node-domexception@1.0.0: + resolution: {integrity: sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==} + engines: {node: '>=10.5.0'} + + node-fetch@2.7.0: + resolution: {integrity: sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==} + engines: {node: 4.x || >=6.0.0} + peerDependencies: + encoding: ^0.1.0 + peerDependenciesMeta: + encoding: + optional: true + + openai@4.81.0: + resolution: {integrity: sha512-lXkFkV+He3O6RGnldHncRGef4uWHssDsAVwN5I3bWcgIdDPy/w8vgtIAwvZxAj49m4WiwWVD0+eGTJ9xOv/ISA==} + hasBin: true + peerDependencies: + ws: ^8.18.0 + zod: ^3.23.8 + peerDependenciesMeta: + ws: + optional: true + zod: + optional: true prettier@2.8.8: resolution: {integrity: sha512-tdN8qQGvNjw4CHbY+XXk0JgCXn9QiF21a55rBe5LJAU+kDyC4WQn4+awm2Xfk2lQMk5fKup9XgzTZtGkjBdP9Q==} engines: {node: '>=10.13.0'} hasBin: true + tr46@0.0.3: + resolution: {integrity: sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==} + typescript@5.7.3: resolution: {integrity: sha512-84MVSjMEHP+FQRPy3pX9sTVV/INIex71s9TL2Gm5FG/WG1SqXeKyZ0k7/blY/4FdOzI12CBy1vGc4og/eus0fw==} engines: {node: '>=14.17'} @@ -90,21 +131,38 @@ packages: undici-types@5.26.5: resolution: {integrity: sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==} + web-streams-polyfill@4.0.0-beta.3: + resolution: {integrity: sha512-QW95TCTaHmsYfHDybGMwO5IJIM93I/6vTRk+daHTWFPhwh+C8Cg7j7XyKrwrj8Ib6vYXe0ocYNrmzY4xAAN6ug==} + engines: {node: '>= 14'} + + webidl-conversions@3.0.1: + resolution: {integrity: sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==} + + whatwg-url@5.0.0: + resolution: {integrity: sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==} + snapshots: '@types/minimist@1.2.5': {} + '@types/node-fetch@2.6.12': + dependencies: + '@types/node': 18.19.74 + form-data: 4.0.1 + '@types/node@18.19.74': dependencies: undici-types: 5.26.5 - asynckit@0.4.0: {} + abort-controller@3.0.0: + dependencies: + event-target-shim: 5.0.1 - axios@0.26.1: + agentkeepalive@4.6.0: dependencies: - follow-redirects: 1.15.9 - transitivePeerDependencies: - - debug + humanize-ms: 1.2.1 + + asynckit@0.4.0: {} combined-stream@1.0.8: dependencies: @@ -112,7 +170,9 @@ snapshots: delayed-stream@1.0.0: {} - follow-redirects@1.15.9: {} + event-target-shim@5.0.1: {} + + form-data-encoder@1.7.2: {} form-data@4.0.1: dependencies: @@ -120,6 +180,15 @@ snapshots: combined-stream: 1.0.8 mime-types: 2.1.35 + formdata-node@4.4.1: + dependencies: + node-domexception: 1.0.0 + web-streams-polyfill: 4.0.0-beta.3 + + humanize-ms@1.2.1: + dependencies: + ms: 2.1.3 + mime-db@1.52.0: {} mime-types@2.1.35: @@ -128,15 +197,39 @@ snapshots: minimist@1.2.8: {} - openai@3.3.0: + ms@2.1.3: {} + + node-domexception@1.0.0: {} + + node-fetch@2.7.0: dependencies: - axios: 0.26.1 - form-data: 4.0.1 + whatwg-url: 5.0.0 + + openai@4.81.0: + dependencies: + '@types/node': 18.19.74 + '@types/node-fetch': 2.6.12 + abort-controller: 3.0.0 + agentkeepalive: 4.6.0 + form-data-encoder: 1.7.2 + formdata-node: 4.4.1 + node-fetch: 2.7.0 transitivePeerDependencies: - - debug + - encoding prettier@2.8.8: {} + tr46@0.0.3: {} + typescript@5.7.3: {} undici-types@5.26.5: {} + + web-streams-polyfill@4.0.0-beta.3: {} + + webidl-conversions@3.0.1: {} + + whatwg-url@5.0.0: + dependencies: + tr46: 0.0.3 + webidl-conversions: 3.0.1 From bc949e9f44e42d094336fda73f9d87b015d15f1f Mon Sep 17 00:00:00 2001 From: Alis Akers Date: Thu, 30 Jan 2025 18:18:58 -0800 Subject: [PATCH 3/3] feat: update OpenAI import and refactor the getChatCompletion function --- src/index.ts | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/index.ts b/src/index.ts index d0e895e..7b6352f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ -import { ChatCompletionRequestMessage, Configuration, OpenAIApi } from "openai"; +import OpenAI from "openai"; import { exec } from "child_process"; import minimist = require("minimist"); @@ -13,6 +13,7 @@ const { GITMSG_OPENAI_API_KEY, GITMSG_COMMIT_PROMPT, GITMSG_PR_PROMPT } = proces * @param {string} [stdin] - Optional input to be passed to the command via stdin. * @returns {Promise} - A Promise that resolves with the command's stdout output. */ + async function execHelper(command: string, stdin?: string): Promise { return new Promise((resolve, reject) => { const child = exec(command); @@ -22,13 +23,18 @@ async function execHelper(command: string, stdin?: string): Promise { child.stdin?.end(); } let stdout = ""; + // Listen for data events on the child process's stdout stream child.stdout?.on("data", (data) => { + // Append the data to the stdout buffer stdout += data; }); + // Listen for the close event on the child process child.on("close", (code) => { + // If the child process exited with a non-zero code, reject the promise if (code !== 0) { reject(); } else { + // Otherwise, resolve the promise with the stdout buffer resolve(stdout); } }); @@ -36,8 +42,8 @@ async function execHelper(command: string, stdin?: string): Promise { } async function getChatCompletion(diff: string, prompt: string) { - const openai = new OpenAIApi(new Configuration({ apiKey: GITMSG_OPENAI_API_KEY })); - const messages: ChatCompletionRequestMessage[] = [ + const openai = new OpenAI({ apiKey: GITMSG_OPENAI_API_KEY }); + const messages: OpenAI.Chat.CreateChatCompletionRequestMessage[] = [ { role: "user", content: diff, @@ -47,12 +53,9 @@ async function getChatCompletion(diff: string, prompt: string) { content: prompt, }, ]; - const { - data: { - choices: [result], - ...rest - }, - } = await openai.createChatCompletion({ + const { + choices: [result], ...rest + } = await openai.chat.completions.create({ model: "gpt-4", messages, }); @@ -73,6 +76,7 @@ async function GitDiffStagedCommand() { async function handleGitDiff(Command: string) { const diff = await execHelper(Command); + console.log(`diff: ${diff}`); displayDiff(diff); return diff; }