di-sukharev / opencommit

Generate conventional git commit messages with AI in 1 second 🤯🔫
https://www.npmjs.com/package/opencommit
MIT License
6.15k stars 328 forks source link

[Bug]: Invalid JSON #261

Open stalinkay opened 1 year ago

stalinkay commented 1 year ago

Opencommit Version

3.0.4

Node Version

18.8.0

NPM Version

9.8.1

What OS are you seeing the problem on?

Mac

What happened?

Instead of generating the commit, it outputs an error related to an Unexpected token in JSON.

Expected Behavior

It should create the commit.

Current Behavior

It appears that instead of returning valid JSON. The OpenAI API is outputting additional explanatory text which is not needed.

Possible Solution

To ensure valid JSON is output when using @commitlint

Steps to Reproduce

Enable @commitlint Run oco

Relevant log output

❯ oco                                                                                                                                                                                                                                                       201705221_Content_Server on  master [$✘!+?] via 🐳 orbstack is 📦 v0.0.0 via  v20.7.0 via 💎 v3.1.2 23:17:16
┌  open-commit
│
◇  1 staged files:
  package.json
│
○  Generating the commit message
│
◇   ─────────────────────────────────────────────────────────────────────────────────────────────╮
│                                                                                                │
│  OCO_PROMPT_MODULE is @commitlint but you haven't generated consistency for this project yet.  │
│                                                                                                │
├────────────────────────────────────────────────────────────────────────────────────────────────╯
│
◇  Read @commitlint configuration (hash: b72b8631076de3a118d6de416d7d6d368c7a0e902f6c1278286e1a6b0d74d5a2)
│
○  Generating consistency with given @commitlint rules
│
│
◇  📝 Commit message generated
│
└  ✖ Unexpected token '`', "
{
"... is not valid JSON
❯ oco                                                                                                                                                                                                                                            201705221_Content_Server on  master [$✘!+?] via 🐳 orbstack is 📦 v0.0.0 via  v18.18.0 via 💎 v3.1.2 took 4m1s 22:37:19
┌  open-commit
│
◇  1 staged files:
  package.json
│
○  Generating the commit message
│
◇   ─────────────────────────────────────────────────────────────────────────────────────────────╮
│                                                                                                │
│  OCO_PROMPT_MODULE is @commitlint but you haven't generated consistency for this project yet.  │
│                                                                                                │
├────────────────────────────────────────────────────────────────────────────────────────────────╯
│
◇  Read @commitlint configuration (hash: b72b8631076de3a118d6de416d7d6d368c7a0e902f6c1278286e1a6b0d74d5a2)
│
○  Generating consistency with given @commitlint rules
│
│
◇  📝 Commit message generated
│
└  ✖ Unexpected token ` in JSON at position 0
fysp11 commented 1 year ago

I have the same

github-actions[bot] commented 12 months ago

Stale issue message

ventsislavnikolov commented 12 months ago

image

Same. Install new NextJS project -> https://nextjs.org/learn-pages-router/basics/create-nextjs-app/setup Install @commitlint/load - npm install --save-dev @commitlint/load Add commitlint config .commitlintrc.yml run oco

And another question, why @commitlint whats a @commitlint/load?

github-actions[bot] commented 10 months ago

Stale issue message

samuelbelolo commented 10 months ago

Same here

jlukic commented 9 months ago

Has anyone found a workaround? I'm digging now to see if i can resolve and make a PR.

jlukic commented 9 months ago

It looks like the returned prompt is coming back from GPT with preceding text which is causing the naive JSON.parse to fail.

Here is the JSON output for the commit messages:

```json
{
  "localLanguage": "english",
  "commitFix": "utils(server): Change 'port' variable to uppercase",
  "commitFeat": "utils(server): Allow server to listen on env variable port",
  "commitDescription": "Changed 'port' variable to uppercase for consistency. Added feature to allow server to listen on a port specified through the environment variable."
}

│ └ ✖ SyntaxError: Unexpected token 'H', "Here is th"... is not valid JSON

jlukic commented 9 months ago

I've submitted a PR to fix this here https://github.com/di-sukharev/opencommit/pull/297

Note you can hack this fix in your node_modules by copying and pasting it into node_modules/opencommit/out/cli.js just to generate the file once, from that point you will have it cached.

add to line 21980

  const jsonIndex = consistency.search('```json');
  if(jsonIndex > -1) {
    consistency = consistency.slice(jsonIndex + 8);
    const endJsonIndex = consistency.search('```');
    consistency = consistency.slice(0, endJsonIndex); 
  }

right after

  const prompts = inferPromptsFromCommitlintConfig(commitLintConfig);
  const consistencyPrompts = commitlintPrompts.GEN_COMMITLINT_CONSISTENCY_PROMPT(prompts);
  let consistency = await api.generateCommitMessage(consistencyPrompts) || "{}";
di-sukharev commented 9 months ago

thank you, i will get to it, thanks, prob next week, very tight schedule

On Fri, Feb 16, 2024 at 5:34 AM Jack Lukic @.***> wrote:

I've submitted a PR to fix this here

297 https://github.com/di-sukharev/opencommit/pull/297

Note you can hack this fix in your node_modules by copying and pasting into node_modules/opencommit/out/cli.js just to generate the file once, from that point you will have it cached.

— Reply to this email directly, view it on GitHub https://github.com/di-sukharev/opencommit/issues/261#issuecomment-1947375712, or unsubscribe https://github.com/notifications/unsubscribe-auth/ANWS3DEHB6UDU3NDV2RZB33YTZ5NHAVCNFSM6AAAAAA5DU4LQGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSNBXGM3TKNZRGI . You are receiving this because you are subscribed to this thread.Message ID: @.***>

ImanMousavi commented 9 months ago

oco ┌ open-commit │
◇ 3 staged files:
db/models/init.py db/models/currency.py db/models/invoice.py │ ○ Generating the commit message │ ◇ ─────────────────────────────────────────────────────────────────────────────────────────────╮
│ │
│ OCO_PROMPT_MODULE is @commitlint but you haven't generated consistency for this project yet. │
│ │
├────────────────────────────────────────────────────────────────────────────────────────────────╯

◇ 📝 Commit message generated
│ └ ✖ Cannot find module 'app\node_modules\@commitlint\load' Require stack: