@verndale/ai-commit 2.3.0 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.env-example CHANGED
@@ -1,7 +1,29 @@
1
1
  # Copy to .env and/or .env.local in your project root (do not commit secrets).
2
2
 
3
+ # ------------------------------------------------------------
4
+ # @verndale/ai-commit (pnpm commit / ai-commit run)
5
+ # ------------------------------------------------------------
3
6
  # @verndale/ai-commit — OPENAI_API_KEY: OpenAI API key for conventional commit messages (ai-commit run; optional for prepare-commit-msg with AI).
4
7
  OPENAI_API_KEY=
5
8
 
6
- # @verndale/ai-commitCOMMIT_AI_MODEL: OpenAI model for commit messages (optional; default gpt-4o-mini).
9
+ # Optionaldefault is gpt-4o-mini
7
10
  # COMMIT_AI_MODEL=
11
+
12
+ # @verndale/ai-pr — GH_TOKEN: Set for local CLI runs; CI uses workflow env / secrets. Also reads GITHUB_TOKEN.
13
+ GH_TOKEN=
14
+
15
+ # Optional — PR behavior
16
+ # @verndale/ai-pr — PR_BASE_BRANCH: Base branch to merge into (default main).
17
+ # PR_BASE_BRANCH=main
18
+ # @verndale/ai-pr — PR_DRAFT: If true (default), new PRs are drafts.
19
+ # PR_DRAFT=true
20
+
21
+ # Optional — AI summary + labels (when PR_AI=true)
22
+ # @verndale/ai-pr — PR_AI: Set true to enable AI summary and optional labels/checklist.
23
+ # PR_AI=true
24
+ # @verndale/ai-pr — PR_AI_ENDPOINT: AI API URL when PR_AI is true (e.g. OpenAI Responses endpoint).
25
+ # PR_AI_ENDPOINT=https://api.openai.com/v1/responses
26
+ # @verndale/ai-pr — PR_AI_API_KEY: Bearer token for the AI API when PR_AI is true.
27
+ # PR_AI_API_KEY=
28
+ # @verndale/ai-pr — PR_AI_MODEL: Model id (default string default).
29
+ # PR_AI_MODEL=default
package/README.md CHANGED
@@ -31,7 +31,7 @@ Use **`ai-commit init --force`** to replace **`.env`** and **`.env-example`** wi
31
31
  - **`OPENAI_API_KEY`** — Required for `ai-commit run` (and for AI-filled `prepare-commit-msg` when you want the model). Optional `COMMIT_AI_MODEL` (default `gpt-4o-mini`).
32
32
  - **Shared env vars** — If another tool already documents **`OPENAI_API_KEY`** or **`COMMIT_AI_MODEL`**, **`ai-commit init`** adds its own `# @verndale/ai-commit — …` line immediately above the assignment when missing; it does not remove or replace existing comment lines.
33
33
  - The CLI loads **`.env`** then **`.env.local`** from the current working directory (project root); values in `.env.local` override `.env` for the same key.
34
- - **Optional tooling:** `PR_*` env vars for [`@verndale/ai-pr`](https://www.npmjs.com/package/@verndale/ai-pr) (`pnpm open-pr` in this repo) / the **Create or update PR** workflow; `RELEASE_NOTES_AI_*` for [`tools/semantic-release-notes.cjs`](./tools/semantic-release-notes.cjs). Use a GitHub PAT as **`GH_TOKEN`** (or `GITHUB_TOKEN`) when calling the GitHub API outside Actions.
34
+ - **Optional tooling:** `PR_*` env vars for [`@verndale/ai-pr`](https://www.npmjs.com/package/@verndale/ai-pr) (`pnpm run pr:create` in this repo) / the **Create or update PR** workflow; `RELEASE_NOTES_AI_*` for [`tools/semantic-release-notes.cjs`](./tools/semantic-release-notes.cjs). Use a GitHub PAT as **`GH_TOKEN`** (or `GITHUB_TOKEN`) when calling the GitHub API outside Actions.
35
35
 
36
36
  ## Commit policy (v2)
37
37
 
@@ -125,10 +125,10 @@ Copy **`.env-example`** to `.env` and/or `.env.local` and set **`OPENAI_API_KEY`
125
125
  | Workflow | Trigger | Purpose |
126
126
  | --- | --- | --- |
127
127
  | [`.github/workflows/commitlint.yml`](./.github/workflows/commitlint.yml) | PRs to `main`, pushes to non-`main` branches | Commitlint on PR range or last push commit |
128
- | [`.github/workflows/pr.yml`](./.github/workflows/pr.yml) | Pushes (not `main`) and `workflow_dispatch` | Install deps, run **`pnpm open-pr`** ([**`@verndale/ai-pr`**](https://www.npmjs.com/package/@verndale/ai-pr)) — set **`PR_HEAD_BRANCH`** / **`PR_BASE_BRANCH`** in CI via env (workflow sets them). Use a PAT secret **`PR_BOT_TOKEN`** if branch protection requires it; otherwise document your org’s policy. |
128
+ | [`.github/workflows/pr.yml`](./.github/workflows/pr.yml) | Pushes (not `main`) and `workflow_dispatch` | Install deps, run **`pnpm run pr:create`** ([**`@verndale/ai-pr`**](https://www.npmjs.com/package/@verndale/ai-pr)) — set **`PR_HEAD_BRANCH`** / **`PR_BASE_BRANCH`** in CI via env (workflow sets them). Use a PAT secret **`PR_BOT_TOKEN`** if branch protection requires it; otherwise document your org’s policy. |
129
129
  | [`.github/workflows/release.yml`](./.github/workflows/release.yml) | Push to **`main`** (including when a PR merges) | **`semantic-release`** — version bump, `CHANGELOG.md`, git tag, npm publish (with provenance), GitHub Release |
130
130
 
131
- Optional **`pnpm open-pr`** locally: set **`GH_TOKEN`** (or **`GITHUB_TOKEN`**) and branch overrides **`PR_BASE_BRANCH`** / **`PR_HEAD_BRANCH`** as needed.
131
+ Optional **`pnpm run pr:create`** locally: set **`GH_TOKEN`** (or **`GITHUB_TOKEN`**) and branch overrides **`PR_BASE_BRANCH`** / **`PR_HEAD_BRANCH`** as needed.
132
132
 
133
133
  ## Publishing (maintainers)
134
134
 
package/lib/init-env.js CHANGED
@@ -5,13 +5,57 @@ const fs = require("fs");
5
5
  /** Detect our doc line so we do not duplicate or replace other packages’ comments. */
6
6
  const MARKER_PREFIX = "# @verndale/ai-commit — ";
7
7
 
8
+ const SECTION_DIVIDER = "# ------------------------------------------------------------";
9
+ const SECTION_TITLE = "# @verndale/ai-commit (pnpm commit / ai-commit run)";
10
+ const SECTION_HEADER = [SECTION_DIVIDER, SECTION_TITLE, SECTION_DIVIDER];
11
+
12
+ const SUBSECTION_OPTIONAL_MODEL = "# Optional — default is gpt-4o-mini";
13
+
8
14
  const DOC_OPENAI = [
9
15
  `${MARKER_PREFIX}OPENAI_API_KEY: OpenAI API key for conventional commit messages (ai-commit run; optional for prepare-commit-msg with AI).`,
10
16
  ];
11
17
 
12
- const DOC_COMMIT_MODEL = [
13
- `${MARKER_PREFIX}COMMIT_AI_MODEL: OpenAI model for commit messages (optional; default gpt-4o-mini).`,
14
- ];
18
+ /**
19
+ * @param {string} text
20
+ * @returns {boolean}
21
+ */
22
+ function hasAiCommitSectionHeader(text) {
23
+ return (
24
+ text.includes(SECTION_DIVIDER) &&
25
+ text.includes("@verndale/ai-commit (pnpm commit / ai-commit run)")
26
+ );
27
+ }
28
+
29
+ /**
30
+ * True when COMMIT_AI_MODEL is already documented (long marker, subsection, or legacy block).
31
+ * @param {string[]} lines
32
+ * @returns {boolean}
33
+ */
34
+ function hasCommitModelNotes(lines) {
35
+ if (hasOurDocForKey(lines, "COMMIT_AI_MODEL")) {
36
+ return true;
37
+ }
38
+ const commitIdx = lines.findIndex(
39
+ (line) =>
40
+ /^\s*COMMIT_AI_MODEL\s*=/.test(line) || /^\s*#\s*COMMIT_AI_MODEL\s*=/.test(line),
41
+ );
42
+ if (commitIdx === -1) {
43
+ return false;
44
+ }
45
+ for (let i = commitIdx - 1; i >= 0 && i >= commitIdx - 12; i--) {
46
+ const t = lines[i].trim();
47
+ if (t === "") {
48
+ continue;
49
+ }
50
+ if (t === SUBSECTION_OPTIONAL_MODEL.trim()) {
51
+ return true;
52
+ }
53
+ if (lines[i].includes(`${MARKER_PREFIX}COMMIT_AI_MODEL:`)) {
54
+ return true;
55
+ }
56
+ }
57
+ return false;
58
+ }
15
59
 
16
60
  /**
17
61
  * Keys assigned on non-comment lines (`KEY=value` or `export KEY=value`).
@@ -38,27 +82,6 @@ function hasOurDocForKey(lines, key) {
38
82
  return lines.some((line) => line.includes(needle));
39
83
  }
40
84
 
41
- /**
42
- * Insert ai-commit doc lines immediately before an assignment line, without changing
43
- * existing comments above that line (we insert after those lines, before the key line).
44
- * @param {string[]} lines mutable
45
- * @param {RegExp} assignmentRegex
46
- * @param {string[]} docLines
47
- * @param {string} key for marker check
48
- * @returns {boolean} whether lines were mutated
49
- */
50
- function injectDocBeforeAssignment(lines, assignmentRegex, docLines, key) {
51
- if (hasOurDocForKey(lines, key)) {
52
- return false;
53
- }
54
- const idx = lines.findIndex((line) => assignmentRegex.test(line));
55
- if (idx === -1) {
56
- return false;
57
- }
58
- lines.splice(idx, 0, ...docLines);
59
- return true;
60
- }
61
-
62
85
  /**
63
86
  * For keys already present (possibly with another package’s comments), add our doc line(s)
64
87
  * above the assignment if missing. Does not remove or edit existing comment lines.
@@ -69,24 +92,27 @@ function injectAiCommitDocsForExistingKeys(content) {
69
92
  const lines = content.split(/\r?\n/);
70
93
  let changed = false;
71
94
 
72
- if (
73
- injectDocBeforeAssignment(
74
- lines,
75
- /^\s*OPENAI_API_KEY\s*=/,
76
- DOC_OPENAI,
77
- "OPENAI_API_KEY",
78
- )
79
- ) {
80
- changed = true;
95
+ if (!hasOurDocForKey(lines, "OPENAI_API_KEY")) {
96
+ const idx = lines.findIndex((line) => /^\s*OPENAI_API_KEY\s*=/.test(line));
97
+ if (idx !== -1) {
98
+ const insert = hasAiCommitSectionHeader(lines.join("\n"))
99
+ ? [DOC_OPENAI[0]]
100
+ : [...SECTION_HEADER, DOC_OPENAI[0]];
101
+ lines.splice(idx, 0, ...insert);
102
+ changed = true;
103
+ }
81
104
  }
82
105
 
83
- if (!hasOurDocForKey(lines, "COMMIT_AI_MODEL")) {
106
+ if (!hasCommitModelNotes(lines)) {
84
107
  let idx = lines.findIndex((line) => /^\s*COMMIT_AI_MODEL\s*=/.test(line));
85
108
  if (idx === -1) {
86
109
  idx = lines.findIndex((line) => /^\s*#\s*COMMIT_AI_MODEL\s*=/.test(line));
87
110
  }
88
111
  if (idx !== -1) {
89
- lines.splice(idx, 0, ...DOC_COMMIT_MODEL);
112
+ const insert = hasAiCommitSectionHeader(lines.join("\n"))
113
+ ? [SUBSECTION_OPTIONAL_MODEL]
114
+ : [...SECTION_HEADER, "", SUBSECTION_OPTIONAL_MODEL];
115
+ lines.splice(idx, 0, ...insert);
90
116
  changed = true;
91
117
  }
92
118
  }
@@ -106,17 +132,43 @@ function buildAiCommitEnvAppend(existing) {
106
132
  keys.has("COMMIT_AI_MODEL") ||
107
133
  /^\s*#\s*COMMIT_AI_MODEL\s*=/m.test(existing) ||
108
134
  /^\s*COMMIT_AI_MODEL\s*=/m.test(existing);
109
- const parts = [];
110
- if (!keys.has("OPENAI_API_KEY")) {
111
- parts.push(`${DOC_OPENAI[0]}\nOPENAI_API_KEY=\n`);
135
+ const needOpenai = !keys.has("OPENAI_API_KEY");
136
+ const needCommit = !keys.has("COMMIT_AI_MODEL") && !hasCommitPlaceholder;
137
+ const hasSection = hasAiCommitSectionHeader(existing);
138
+
139
+ if (!needOpenai && !needCommit) {
140
+ return null;
112
141
  }
113
- if (!keys.has("COMMIT_AI_MODEL") && !hasCommitPlaceholder) {
114
- parts.push(`${DOC_COMMIT_MODEL[0]}\n# COMMIT_AI_MODEL=\n`);
142
+
143
+ if (needOpenai && needCommit) {
144
+ return [
145
+ ...SECTION_HEADER,
146
+ DOC_OPENAI[0],
147
+ "OPENAI_API_KEY=",
148
+ "",
149
+ SUBSECTION_OPTIONAL_MODEL,
150
+ "# COMMIT_AI_MODEL=",
151
+ "",
152
+ "",
153
+ ].join("\n");
115
154
  }
116
- if (parts.length === 0) {
117
- return null;
155
+
156
+ if (needOpenai) {
157
+ return [...SECTION_HEADER, DOC_OPENAI[0], "OPENAI_API_KEY=", ""].join("\n");
158
+ }
159
+
160
+ if (hasSection) {
161
+ return [SUBSECTION_OPTIONAL_MODEL, "# COMMIT_AI_MODEL=", "", ""].join("\n");
118
162
  }
119
- return parts.join("\n");
163
+
164
+ return [
165
+ ...SECTION_HEADER,
166
+ "",
167
+ SUBSECTION_OPTIONAL_MODEL,
168
+ "# COMMIT_AI_MODEL=",
169
+ "",
170
+ "",
171
+ ].join("\n");
120
172
  }
121
173
 
122
174
  /**
@@ -124,7 +176,7 @@ function buildAiCommitEnvAppend(existing) {
124
176
  * @param {string} destPath
125
177
  * @param {string} bundledPath
126
178
  * @param {{ force?: boolean }} [options]
127
- * @returns {{ kind: 'replaced' | 'wrote' | 'merged' | 'unchanged' }}
179
+ * @returns {{ kind: 'replaced' | 'wrote' | 'unchanged' }}
128
180
  */
129
181
  function mergeAiCommitEnvFile(destPath, bundledPath, options = {}) {
130
182
  const { force = false } = options;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@verndale/ai-commit",
3
- "version": "2.3.0",
3
+ "version": "2.4.0",
4
4
  "description": "AI-assisted conventional commits with bundled commitlint — one install, aligned rules",
5
5
  "license": "MIT",
6
6
  "author": "Verndale",
@@ -43,9 +43,9 @@
43
43
  "prepare": "husky",
44
44
  "commit": "node ./bin/cli.js run",
45
45
  "lint:commit": "commitlint --edit .git/COMMIT_EDITMSG --config lib/commitlint-preset.cjs",
46
- "open-pr": "ai-pr",
47
46
  "release": "semantic-release",
48
- "prepublishOnly": "node -e \"require('./lib/rules.js'); require('./lib/commitlint-preset.cjs'); require('./lib/init-env.js'); require('./lib/init-workspace.js');\""
47
+ "prepublishOnly": "node -e \"require('./lib/rules.js'); require('./lib/commitlint-preset.cjs'); require('./lib/init-env.js'); require('./lib/init-workspace.js');\"",
48
+ "pr:create": "ai-pr"
49
49
  },
50
50
  "dependencies": {
51
51
  "@commitlint/cli": "^20.5.0",
@@ -57,7 +57,7 @@
57
57
  "openai": "^6.33.0"
58
58
  },
59
59
  "devDependencies": {
60
- "@verndale/ai-pr": "^1.0.0",
60
+ "@verndale/ai-pr": "^1.1.0",
61
61
  "@semantic-release/changelog": "^6.0.3",
62
62
  "@semantic-release/commit-analyzer": "^13.0.1",
63
63
  "@semantic-release/git": "^10.0.1",