aicommits icon indicating copy to clipboard operation
aicommits copied to clipboard

refactor: use custom OpenAI "fetch"

Open privatenumber opened this issue 2 years ago • 1 comments

Problems

  • The openai had a hanging socket that was preventing aicommits from exiting immediately: https://github.com/Nutlope/aicommits/pull/102
  • The openai package was pretty bulky

Before

Tarball size
73.8 kB

File                      Size             Gzip           Brotli

dist/cli.mjs          294.7 kB          70.2 kB          60.7 kB
README.md               2.8 kB           1.4 kB           1.1 kB
package.json            1.3 kB            648 B            573 B
LICENSE                 1.1 kB            641 B            485 B

                      299.8 kB          72.8 kB          62.9 kB

Changes

  • I played around with got, node-fetch, and undici, but unfortunately, they were all quite heavy for our simple usage. I ended up writing my own post function that's hard-coded to the OpenAI API. Huge package size improvement.
  • We also keep the openai package as a dev dependency so we get the benefits of types

After

Tarball size
29.2 kB

File                     Size             Gzip           Brotli

dist/cli.mjs          78.7 kB          26.4 kB          23.2 kB
README.md              2.8 kB           1.4 kB           1.1 kB
package.json           1.4 kB            668 B            592 B
LICENSE                1.1 kB            641 B            485 B

                      83.9 kB          29.0 kB          25.4 kB

privatenumber avatar Feb 22 '23 13:02 privatenumber

Will run some tests on this to make sure this works to catch:

  • [x] Unreachable OpenAI server (e.g. no internet or blocked)
  • [ ] ~Windows exist~

privatenumber avatar Feb 22 '23 14:02 privatenumber