@andre-barbosa/opencode-commit 0.1.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/LICENSE +21 -0
- package/README.md +122 -0
- package/agents/commit-writer.md +27 -0
- package/commands/commit.md +5 -0
- package/opencode.example.json +17 -0
- package/package.json +48 -0
- package/src/generate.ts +112 -0
- package/src/git.ts +64 -0
- package/src/index.ts +136 -0
- package/src/prompt.ts +51 -0
- package/src/types.ts +40 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 opencode-commit contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
# opencode-commit
|
|
2
|
+
|
|
3
|
+
OpenCode plugin that generates **Conventional Commits** messages from staged git changes using a dedicated sub-agent on a model you choose.
|
|
4
|
+
|
|
5
|
+
Run `/commit` in the OpenCode TUI to get a suggested message like `feat(auth): add oauth login flow` — copy it and commit manually.
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- `/commit` slash command (display only, no auto-commit)
|
|
10
|
+
- Dedicated hidden `commit-writer` sub-agent with its own model
|
|
11
|
+
- Conventional Commits format enforced (`feat(scope): ...`, `fix(scope): ...`, etc.)
|
|
12
|
+
- Optional hint: `/commit emphasize breaking API change`
|
|
13
|
+
- Uses staged diff, branch name, and recent commit subjects for context
|
|
14
|
+
|
|
15
|
+
## Requirements
|
|
16
|
+
|
|
17
|
+
- [OpenCode](https://opencode.ai/) with plugin support
|
|
18
|
+
- A git repository with staged changes
|
|
19
|
+
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
### 1. Copy agent and command templates
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
mkdir -p .opencode/agents .opencode/commands
|
|
26
|
+
cp agents/commit-writer.md .opencode/agents/commit-writer.md
|
|
27
|
+
cp commands/commit.md .opencode/commands/commit.md
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
### 2. Configure the plugin
|
|
31
|
+
|
|
32
|
+
**Local development** (this repo):
|
|
33
|
+
|
|
34
|
+
```json
|
|
35
|
+
{
|
|
36
|
+
"$schema": "https://opencode.ai/config.json",
|
|
37
|
+
"plugin": [
|
|
38
|
+
[
|
|
39
|
+
"file:///C:/absolute/path/to/opencode-commit/src/index.ts",
|
|
40
|
+
{
|
|
41
|
+
"agent": "commit-writer",
|
|
42
|
+
"maxDiffChars": 12000
|
|
43
|
+
}
|
|
44
|
+
]
|
|
45
|
+
],
|
|
46
|
+
"agent": {
|
|
47
|
+
"commit-writer": {
|
|
48
|
+
"model": "anthropic/claude-haiku-4-20250514"
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
**From npm** (when published):
|
|
55
|
+
|
|
56
|
+
```json
|
|
57
|
+
{
|
|
58
|
+
"plugin": [
|
|
59
|
+
["opencode-commit", { "agent": "commit-writer" }]
|
|
60
|
+
]
|
|
61
|
+
}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
See [opencode.example.json](opencode.example.json) for a full example.
|
|
65
|
+
|
|
66
|
+
### 3. Set your model
|
|
67
|
+
|
|
68
|
+
Edit the model on the `commit-writer` agent in `opencode.json` or in `.opencode/agents/commit-writer.md`:
|
|
69
|
+
|
|
70
|
+
```yaml
|
|
71
|
+
model: anthropic/claude-haiku-4-20250514
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Run `opencode models` to list available models.
|
|
75
|
+
|
|
76
|
+
## Usage
|
|
77
|
+
|
|
78
|
+
1. Stage your changes: `git add ...`
|
|
79
|
+
2. Open OpenCode in the project
|
|
80
|
+
3. Run `/commit`
|
|
81
|
+
4. Copy the suggested message and commit: `git commit -m "feat(scope): ..."`
|
|
82
|
+
|
|
83
|
+
Optional extra instruction:
|
|
84
|
+
|
|
85
|
+
```text
|
|
86
|
+
/commit focus on test coverage improvements
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## Plugin options
|
|
90
|
+
|
|
91
|
+
When loading the plugin as a tuple `[name, options]`:
|
|
92
|
+
|
|
93
|
+
| Option | Default | Description |
|
|
94
|
+
| --- | --- | --- |
|
|
95
|
+
| `agent` | `commit-writer` | Sub-agent name to invoke |
|
|
96
|
+
| `maxDiffChars` | `12000` | Max staged diff characters sent to the sub-agent |
|
|
97
|
+
|
|
98
|
+
## Commit message format
|
|
99
|
+
|
|
100
|
+
Generated messages follow [Conventional Commits](https://www.conventionalcommits.org/):
|
|
101
|
+
|
|
102
|
+
```
|
|
103
|
+
type(scope): short imperative description
|
|
104
|
+
|
|
105
|
+
- optional bullet body
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
Allowed types: `feat`, `fix`, `docs`, `style`, `refactor`, `perf`, `test`, `build`, `ci`, `chore`.
|
|
109
|
+
|
|
110
|
+
## Project layout
|
|
111
|
+
|
|
112
|
+
```
|
|
113
|
+
opencode-commit/
|
|
114
|
+
├── src/ # plugin source
|
|
115
|
+
├── agents/ # commit-writer sub-agent template
|
|
116
|
+
├── commands/ # /commit command stub
|
|
117
|
+
└── opencode.example.json
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## License
|
|
121
|
+
|
|
122
|
+
MIT
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
---
|
|
2
|
+
description: Generates conventional commit messages from git diffs
|
|
3
|
+
mode: subagent
|
|
4
|
+
hidden: true
|
|
5
|
+
model: anthropic/claude-haiku-4-20250514
|
|
6
|
+
temperature: 0.1
|
|
7
|
+
permission:
|
|
8
|
+
edit: deny
|
|
9
|
+
bash: deny
|
|
10
|
+
webfetch: deny
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
You are a commit message writer. Given a git diff and context, produce a single commit message.
|
|
14
|
+
|
|
15
|
+
You MUST follow Conventional Commits:
|
|
16
|
+
|
|
17
|
+
- Subject: `type(scope): description` or `type: description`
|
|
18
|
+
- Types: feat, fix, docs, style, refactor, perf, test, build, ci, chore
|
|
19
|
+
- Scope: optional but preferred for localized changes (e.g. auth, api, ui)
|
|
20
|
+
- Description: imperative, lowercase, no trailing period, max 72 chars for the subject line
|
|
21
|
+
- Body: optional blank line then bullet points for non-obvious context
|
|
22
|
+
- Breaking changes: `feat!:` / `fix!:` or a `BREAKING CHANGE:` footer
|
|
23
|
+
|
|
24
|
+
Never output free-form subjects like "Update files", "WIP", or "Misc changes".
|
|
25
|
+
Never wrap the message in code fences.
|
|
26
|
+
Never add commentary before or after the commit message.
|
|
27
|
+
Output only the commit message text.
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://opencode.ai/config.json",
|
|
3
|
+
"plugin": [
|
|
4
|
+
[
|
|
5
|
+
"opencode-commit",
|
|
6
|
+
{
|
|
7
|
+
"agent": "commit-writer",
|
|
8
|
+
"maxDiffChars": 12000
|
|
9
|
+
}
|
|
10
|
+
]
|
|
11
|
+
],
|
|
12
|
+
"agent": {
|
|
13
|
+
"commit-writer": {
|
|
14
|
+
"model": "your-provider/your-model"
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@andre-barbosa/opencode-commit",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "OpenCode plugin that generates Conventional Commit messages via a dedicated sub-agent",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "src/index.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./src/index.ts"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"src",
|
|
12
|
+
"agents",
|
|
13
|
+
"commands",
|
|
14
|
+
"opencode.example.json",
|
|
15
|
+
"README.md",
|
|
16
|
+
"LICENSE"
|
|
17
|
+
],
|
|
18
|
+
"scripts": {
|
|
19
|
+
"typecheck": "tsc --noEmit",
|
|
20
|
+
"prepublishOnly": "npm run typecheck"
|
|
21
|
+
},
|
|
22
|
+
"keywords": [
|
|
23
|
+
"opencode",
|
|
24
|
+
"opencode-plugin",
|
|
25
|
+
"git",
|
|
26
|
+
"commit",
|
|
27
|
+
"conventional-commits"
|
|
28
|
+
],
|
|
29
|
+
"license": "MIT",
|
|
30
|
+
"repository": {
|
|
31
|
+
"type": "git",
|
|
32
|
+
"url": "https://github.com/AndreB10/opencode-commit.git"
|
|
33
|
+
},
|
|
34
|
+
"bugs": {
|
|
35
|
+
"url": "https://github.com/AndreB10/opencode-commit/issues"
|
|
36
|
+
},
|
|
37
|
+
"homepage": "https://github.com/AndreB10/opencode-commit#readme",
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@opencode-ai/plugin": "^1.3.3"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@opencode-ai/sdk": "^1.3.3",
|
|
43
|
+
"typescript": "^5.8.3"
|
|
44
|
+
},
|
|
45
|
+
"engines": {
|
|
46
|
+
"node": ">=18"
|
|
47
|
+
}
|
|
48
|
+
}
|
package/src/generate.ts
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import type { createOpencodeClient } from "@opencode-ai/sdk";
|
|
2
|
+
import type { AgentInfo, ModelRef } from "./types.js";
|
|
3
|
+
import { buildCommitPrompt, stripCodeFences } from "./prompt.js";
|
|
4
|
+
import type { GitContext } from "./types.js";
|
|
5
|
+
|
|
6
|
+
type Client = ReturnType<typeof createOpencodeClient>;
|
|
7
|
+
|
|
8
|
+
function extractText(parts: Array<{ type?: string; text?: string }>): string {
|
|
9
|
+
return parts
|
|
10
|
+
.filter((part) => part.type === "text" && typeof part.text === "string")
|
|
11
|
+
.map((part) => part.text as string)
|
|
12
|
+
.join("")
|
|
13
|
+
.trim();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function formatModelRef(model?: ModelRef): string {
|
|
17
|
+
if (!model) return "default";
|
|
18
|
+
return `${model.providerID}/${model.modelID}`;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export async function fetchAgent(
|
|
22
|
+
client: Client,
|
|
23
|
+
agentName: string,
|
|
24
|
+
): Promise<AgentInfo | undefined> {
|
|
25
|
+
const { data, error } = await client.app.agents();
|
|
26
|
+
if (error || !Array.isArray(data)) return undefined;
|
|
27
|
+
|
|
28
|
+
const agent = data.find((entry) => entry.name === agentName);
|
|
29
|
+
if (!agent) return undefined;
|
|
30
|
+
|
|
31
|
+
return {
|
|
32
|
+
name: agent.name,
|
|
33
|
+
model: agent.model
|
|
34
|
+
? {
|
|
35
|
+
providerID: agent.model.providerID,
|
|
36
|
+
modelID: agent.model.modelID,
|
|
37
|
+
}
|
|
38
|
+
: undefined,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async function createChildSession(
|
|
43
|
+
client: Client,
|
|
44
|
+
parentSessionID: string,
|
|
45
|
+
): Promise<string> {
|
|
46
|
+
const { data: session, error } = await client.session.create({
|
|
47
|
+
body: {
|
|
48
|
+
title: "Commit message",
|
|
49
|
+
parentID: parentSessionID,
|
|
50
|
+
},
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
if (error || !session?.id) {
|
|
54
|
+
throw new Error(`Failed to create child session: ${String(error)}`);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return session.id;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export async function generateCommitMessage(input: {
|
|
61
|
+
client: Client;
|
|
62
|
+
parentSessionID: string;
|
|
63
|
+
agentName: string;
|
|
64
|
+
agent: AgentInfo;
|
|
65
|
+
context: GitContext;
|
|
66
|
+
userHint?: string;
|
|
67
|
+
}): Promise<{ message: string; childSessionID: string; modelLabel: string }> {
|
|
68
|
+
const childSessionID = await createChildSession(
|
|
69
|
+
input.client,
|
|
70
|
+
input.parentSessionID,
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
const prompt = buildCommitPrompt(input.context, input.userHint);
|
|
74
|
+
const body: {
|
|
75
|
+
agent: string;
|
|
76
|
+
parts: Array<{ type: "text"; text: string }>;
|
|
77
|
+
model?: ModelRef;
|
|
78
|
+
} = {
|
|
79
|
+
agent: input.agentName,
|
|
80
|
+
parts: [{ type: "text", text: prompt }],
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
if (input.agent.model) {
|
|
84
|
+
body.model = input.agent.model;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const { data: result, error } = await input.client.session.prompt({
|
|
88
|
+
path: { id: childSessionID },
|
|
89
|
+
body,
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
if (error) {
|
|
93
|
+
throw new Error(`Sub-agent prompt failed: ${String(error)}`);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const parts =
|
|
97
|
+
(result as { parts?: Array<{ type?: string; text?: string }> } | undefined)
|
|
98
|
+
?.parts ?? [];
|
|
99
|
+
const rawText = extractText(parts);
|
|
100
|
+
|
|
101
|
+
if (!rawText) {
|
|
102
|
+
throw new Error(
|
|
103
|
+
`Sub-agent returned no text. Child session: ${childSessionID}`,
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return {
|
|
108
|
+
message: stripCodeFences(rawText),
|
|
109
|
+
childSessionID,
|
|
110
|
+
modelLabel: formatModelRef(input.agent.model),
|
|
111
|
+
};
|
|
112
|
+
}
|
package/src/git.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { PluginInput } from "@opencode-ai/plugin";
|
|
2
|
+
import type { GitContext } from "./types.js";
|
|
3
|
+
|
|
4
|
+
type Shell = PluginInput["$"];
|
|
5
|
+
|
|
6
|
+
async function readGitOutput(
|
|
7
|
+
$: Shell,
|
|
8
|
+
worktree: string,
|
|
9
|
+
run: ($: Shell) => Promise<{ exitCode: number; stdout?: { toString(): string } }>,
|
|
10
|
+
): Promise<string> {
|
|
11
|
+
const result = await run($);
|
|
12
|
+
if (result.exitCode !== 0) {
|
|
13
|
+
return "";
|
|
14
|
+
}
|
|
15
|
+
return result.stdout?.toString().trim() ?? "";
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export async function isGitRepo($: Shell, worktree: string): Promise<boolean> {
|
|
19
|
+
const result = await $`git rev-parse --git-dir`.cwd(worktree).quiet().nothrow();
|
|
20
|
+
return result.exitCode === 0;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export async function gatherGitContext(
|
|
24
|
+
$: Shell,
|
|
25
|
+
worktree: string,
|
|
26
|
+
maxDiffChars: number,
|
|
27
|
+
): Promise<GitContext> {
|
|
28
|
+
const branch =
|
|
29
|
+
(await readGitOutput($, worktree, ($) =>
|
|
30
|
+
$`git symbolic-ref --quiet --short HEAD`.cwd(worktree).quiet().nothrow(),
|
|
31
|
+
)) ||
|
|
32
|
+
(await readGitOutput($, worktree, ($) =>
|
|
33
|
+
$`git rev-parse --short HEAD`.cwd(worktree).quiet().nothrow(),
|
|
34
|
+
)) ||
|
|
35
|
+
"unknown";
|
|
36
|
+
|
|
37
|
+
const stat = await readGitOutput($, worktree, ($) =>
|
|
38
|
+
$`git diff --staged --stat`.cwd(worktree).quiet().nothrow(),
|
|
39
|
+
);
|
|
40
|
+
const rawDiff = await readGitOutput($, worktree, ($) =>
|
|
41
|
+
$`git diff --staged`.cwd(worktree).quiet().nothrow(),
|
|
42
|
+
);
|
|
43
|
+
const diff =
|
|
44
|
+
rawDiff.length > maxDiffChars
|
|
45
|
+
? `${rawDiff.slice(0, maxDiffChars)}\n\n[diff truncated at ${maxDiffChars} characters]`
|
|
46
|
+
: rawDiff;
|
|
47
|
+
|
|
48
|
+
const logOutput = await readGitOutput($, worktree, ($) =>
|
|
49
|
+
$`git log -5 --pretty=format:%s`.cwd(worktree).quiet().nothrow(),
|
|
50
|
+
);
|
|
51
|
+
const recentCommits = logOutput
|
|
52
|
+
? logOutput.split("\n").map((line) => line.trim()).filter(Boolean)
|
|
53
|
+
: [];
|
|
54
|
+
|
|
55
|
+
const hasStagedChanges = stat.length > 0 || rawDiff.length > 0;
|
|
56
|
+
|
|
57
|
+
return {
|
|
58
|
+
branch,
|
|
59
|
+
stat,
|
|
60
|
+
diff,
|
|
61
|
+
recentCommits,
|
|
62
|
+
hasStagedChanges,
|
|
63
|
+
};
|
|
64
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import type { Plugin } from "@opencode-ai/plugin";
|
|
2
|
+
import type { Part } from "@opencode-ai/sdk";
|
|
3
|
+
import { gatherGitContext, isGitRepo } from "./git.js";
|
|
4
|
+
import { fetchAgent, generateCommitMessage } from "./generate.js";
|
|
5
|
+
import { resolveConfig, type PluginOptions } from "./types.js";
|
|
6
|
+
|
|
7
|
+
const COMMAND_NAME = "commit";
|
|
8
|
+
const SKIP_ERROR = "skip";
|
|
9
|
+
|
|
10
|
+
function textPart(text: string): Part {
|
|
11
|
+
return { type: "text", text } as Part;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function formatOutput(input: {
|
|
15
|
+
message: string;
|
|
16
|
+
modelLabel: string;
|
|
17
|
+
childSessionID: string;
|
|
18
|
+
}): string {
|
|
19
|
+
return [
|
|
20
|
+
"## Suggested commit message",
|
|
21
|
+
"",
|
|
22
|
+
"```",
|
|
23
|
+
input.message,
|
|
24
|
+
"```",
|
|
25
|
+
"",
|
|
26
|
+
`Model: \`${input.modelLabel}\` · Child session: \`${input.childSessionID}\``,
|
|
27
|
+
"",
|
|
28
|
+
"Copy the message above and run `git commit` when ready.",
|
|
29
|
+
].join("\n");
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const skipCommand = (): never => {
|
|
33
|
+
throw new Error(SKIP_ERROR);
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export const OpenCodeCommitPlugin: Plugin = async (
|
|
37
|
+
{ client, $, worktree },
|
|
38
|
+
options?: PluginOptions,
|
|
39
|
+
) => {
|
|
40
|
+
const config = resolveConfig(options);
|
|
41
|
+
|
|
42
|
+
const log = async (
|
|
43
|
+
level: "info" | "warn" | "error",
|
|
44
|
+
message: string,
|
|
45
|
+
extra?: Record<string, unknown>,
|
|
46
|
+
) => {
|
|
47
|
+
await client.app
|
|
48
|
+
.log({
|
|
49
|
+
body: {
|
|
50
|
+
service: "opencode-commit",
|
|
51
|
+
level,
|
|
52
|
+
message,
|
|
53
|
+
extra,
|
|
54
|
+
},
|
|
55
|
+
})
|
|
56
|
+
.catch(() => {});
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
return {
|
|
60
|
+
"command.execute.before": async (input, output) => {
|
|
61
|
+
if (input.command !== COMMAND_NAME) return;
|
|
62
|
+
|
|
63
|
+
try {
|
|
64
|
+
if (!(await isGitRepo($, worktree))) {
|
|
65
|
+
output.parts = [
|
|
66
|
+
textPart(
|
|
67
|
+
"Not a git repository. Run `/commit` from inside a git worktree.",
|
|
68
|
+
),
|
|
69
|
+
];
|
|
70
|
+
return skipCommand();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const context = await gatherGitContext($, worktree, config.maxDiffChars);
|
|
74
|
+
|
|
75
|
+
if (!context.hasStagedChanges) {
|
|
76
|
+
output.parts = [
|
|
77
|
+
textPart(
|
|
78
|
+
"No staged changes found. Stage files with `git add` and run `/commit` again.",
|
|
79
|
+
),
|
|
80
|
+
];
|
|
81
|
+
return skipCommand();
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const resolvedAgent = await fetchAgent(client, config.agent);
|
|
85
|
+
if (!resolvedAgent) {
|
|
86
|
+
output.parts = [
|
|
87
|
+
textPart(
|
|
88
|
+
[
|
|
89
|
+
`Sub-agent \`${config.agent}\` is not configured.`,
|
|
90
|
+
"",
|
|
91
|
+
"Copy `agents/commit-writer.md` from this plugin into `.opencode/agents/commit-writer.md`,",
|
|
92
|
+
"or add the agent to your `opencode.json`.",
|
|
93
|
+
].join("\n"),
|
|
94
|
+
),
|
|
95
|
+
];
|
|
96
|
+
return skipCommand();
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
await log("info", "Generating commit message", {
|
|
100
|
+
agent: config.agent,
|
|
101
|
+
branch: context.branch,
|
|
102
|
+
sessionID: input.sessionID,
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
const userHint = input.arguments?.trim() || undefined;
|
|
106
|
+
const result = await generateCommitMessage({
|
|
107
|
+
client,
|
|
108
|
+
parentSessionID: input.sessionID,
|
|
109
|
+
agentName: config.agent,
|
|
110
|
+
agent: resolvedAgent,
|
|
111
|
+
context,
|
|
112
|
+
userHint,
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
output.parts = [textPart(formatOutput(result))];
|
|
116
|
+
return skipCommand();
|
|
117
|
+
} catch (error) {
|
|
118
|
+
if (error instanceof Error && error.message === SKIP_ERROR) {
|
|
119
|
+
throw error;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
await log("error", "Commit message generation failed", {
|
|
123
|
+
error: String(error),
|
|
124
|
+
sessionID: input.sessionID,
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
output.parts = [
|
|
128
|
+
textPart(`Failed to generate commit message: ${String(error)}`),
|
|
129
|
+
];
|
|
130
|
+
return skipCommand();
|
|
131
|
+
}
|
|
132
|
+
},
|
|
133
|
+
};
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
export default OpenCodeCommitPlugin;
|
package/src/prompt.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { GitContext } from "./types.js";
|
|
2
|
+
|
|
3
|
+
export function buildCommitPrompt(context: GitContext, userHint?: string): string {
|
|
4
|
+
const recentCommitsBlock =
|
|
5
|
+
context.recentCommits.length > 0
|
|
6
|
+
? context.recentCommits.map((subject) => `- ${subject}`).join("\n")
|
|
7
|
+
: "(none)";
|
|
8
|
+
|
|
9
|
+
const hintBlock =
|
|
10
|
+
userHint && userHint.trim().length > 0
|
|
11
|
+
? `\nAdditional instruction from the user:\n${userHint.trim()}\n`
|
|
12
|
+
: "";
|
|
13
|
+
|
|
14
|
+
return `Write a git commit message for the staged changes below.
|
|
15
|
+
|
|
16
|
+
Format rules (Conventional Commits — required):
|
|
17
|
+
- Subject line MUST use type(scope): description or type: description
|
|
18
|
+
- Allowed types: feat, fix, docs, style, refactor, perf, test, build, ci, chore
|
|
19
|
+
- Scope is optional but preferred when changes are localized (e.g. feat(auth):, fix(api):)
|
|
20
|
+
- Description: imperative mood, lowercase, no trailing period, max 72 chars for the full subject
|
|
21
|
+
- Optional body: blank line after subject, then bullet points; keep lines <= 72 chars
|
|
22
|
+
- Breaking changes: use feat!: or fix!: prefix, or add a BREAKING CHANGE: footer
|
|
23
|
+
- Do NOT use free-form subjects like "Update files", "WIP", or "Misc changes"
|
|
24
|
+
- Output ONLY the commit message text — no code fences, no commentary, no tool calls
|
|
25
|
+
|
|
26
|
+
Examples:
|
|
27
|
+
feat(auth): add oauth login flow
|
|
28
|
+
|
|
29
|
+
- add google and github providers
|
|
30
|
+
- store refresh tokens in secure storage
|
|
31
|
+
|
|
32
|
+
fix(api): handle null response from user endpoint
|
|
33
|
+
${hintBlock}
|
|
34
|
+
Branch: ${context.branch}
|
|
35
|
+
|
|
36
|
+
Recent commit subjects (align scope naming when sensible):
|
|
37
|
+
${recentCommitsBlock}
|
|
38
|
+
|
|
39
|
+
Staged changes (stat):
|
|
40
|
+
${context.stat || "(empty)"}
|
|
41
|
+
|
|
42
|
+
Staged diff:
|
|
43
|
+
${context.diff || "(empty)"}
|
|
44
|
+
`;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function stripCodeFences(text: string): string {
|
|
48
|
+
const trimmed = text.trim();
|
|
49
|
+
const fenced = trimmed.match(/^```(?:\w*\n)?([\s\S]*?)```$/);
|
|
50
|
+
return (fenced?.[1] ?? trimmed).trim();
|
|
51
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export type ModelRef = {
|
|
2
|
+
providerID: string;
|
|
3
|
+
modelID: string;
|
|
4
|
+
};
|
|
5
|
+
|
|
6
|
+
export type PluginOptions = {
|
|
7
|
+
agent?: string;
|
|
8
|
+
maxDiffChars?: number;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export type ResolvedPluginConfig = {
|
|
12
|
+
agent: string;
|
|
13
|
+
maxDiffChars: number;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export const DEFAULT_AGENT = "commit-writer";
|
|
17
|
+
export const DEFAULT_MAX_DIFF_CHARS = 12_000;
|
|
18
|
+
|
|
19
|
+
export function resolveConfig(options?: PluginOptions): ResolvedPluginConfig {
|
|
20
|
+
return {
|
|
21
|
+
agent: options?.agent?.trim() || DEFAULT_AGENT,
|
|
22
|
+
maxDiffChars:
|
|
23
|
+
typeof options?.maxDiffChars === "number" && options.maxDiffChars > 0
|
|
24
|
+
? Math.floor(options.maxDiffChars)
|
|
25
|
+
: DEFAULT_MAX_DIFF_CHARS,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export type GitContext = {
|
|
30
|
+
branch: string;
|
|
31
|
+
stat: string;
|
|
32
|
+
diff: string;
|
|
33
|
+
recentCommits: string[];
|
|
34
|
+
hasStagedChanges: boolean;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export type AgentInfo = {
|
|
38
|
+
name: string;
|
|
39
|
+
model?: ModelRef;
|
|
40
|
+
};
|