bacluc-opencode-completion-check-command 0.0.1

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 ADDED
@@ -0,0 +1,28 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2026, Bacluc
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions are met:
7
+
8
+ 1. Redistributions of source code must retain the above copyright notice, this
9
+ list of conditions and the following disclaimer.
10
+
11
+ 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ this list of conditions and the following disclaimer in the documentation
13
+ and/or other materials provided with the distribution.
14
+
15
+ 3. Neither the name of the copyright holder nor the names of its
16
+ contributors may be used to endorse or promote products derived from
17
+ this software without specific prior written permission.
18
+
19
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/README.md ADDED
@@ -0,0 +1,108 @@
1
+ # opencode-completion-check-command
2
+
3
+ An [opencode](https://opencode.ai) plugin that automatically verifies task completion by running a shell command after the agent finishes.
4
+
5
+ ## How It Works
6
+
7
+ 1. Use the `/completion-check-command` slash command in your opencode prompt followed by a markdown code block containing the shell command to run
8
+ 2. When the agent finishes (session goes idle), the plugin executes the stored command
9
+ 3. If the command exits with code 0, the task is considered complete
10
+ 4. If the command exits with a non-zero code, the agent is automatically prompted again with:
11
+
12
+ ```
13
+ you are not yet finished:
14
+
15
+ stdout:
16
+ <command stdout output>
17
+
18
+ stderr:
19
+ <command stderr output>
20
+ ```
21
+
22
+ 5. This retry loop continues up to a configurable maximum number of retries (default: 3). Once the limit is reached, the plugin stops re-prompting to prevent burning tokens indefinitely.
23
+
24
+ ## Usage
25
+
26
+ In your opencode session, type:
27
+
28
+ ````
29
+ /completion-check-command
30
+ ```bash
31
+ ./check-task.sh
32
+ ```
33
+ ````
34
+
35
+ The plugin will extract the command from the code block and run it when the agent finishes.
36
+
37
+ ## Configuration
38
+
39
+ ### Plugin Options
40
+
41
+ The plugin accepts an `maxRetries` option (default: `10`) that controls the maximum number of times the agent will be re-prompted when the check command fails:
42
+
43
+ ```json
44
+ {
45
+ "plugin": [["bacluc-opencode-completion-check-command@<version>", { "maxRetries": 10 }]]
46
+ }
47
+ ```
48
+
49
+ Setting `maxRetries` to `0` disables re-prompting entirely — the command runs once and any failure is silently dropped.
50
+
51
+ ### Custom Command Template
52
+
53
+ The `/completion-check-command` is automatically registered by the plugin's `config` hook. If you want to override the template, add it to your `opencode.json`:
54
+
55
+ ```json
56
+ {
57
+ "command": {
58
+ "completion-check-command": {
59
+ "template": "Your custom template here: {{arguments}}",
60
+ "description": "Custom description"
61
+ }
62
+ }
63
+ }
64
+ ```
65
+
66
+ ## Example
67
+
68
+ Create a check script:
69
+
70
+ ```bash
71
+ #!/bin/bash
72
+ # check-task.sh - verify the task is complete
73
+ if [ -f "output.txt" ]; then
74
+ echo "Task complete - output file exists"
75
+ exit 0
76
+ else
77
+ echo "Task NOT complete - output file missing"
78
+ echo "Create the file: output.txt"
79
+ exit 1
80
+ fi
81
+ ```
82
+
83
+ Then use it in opencode:
84
+
85
+ ````
86
+ Please create a file called output.txt with the contents "Hello World"
87
+
88
+ /completion-check-command
89
+ ```bash
90
+ ./check-task.sh
91
+ ```
92
+ ````
93
+
94
+ ## Development
95
+
96
+ ```bash
97
+ # Install dependencies
98
+ npm install
99
+
100
+ # Run tests
101
+ npm test
102
+
103
+ # Type check
104
+ npm run typecheck
105
+
106
+ # Format code
107
+ npm run format
108
+ ```
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "bacluc-opencode-completion-check-command",
3
+ "version": "0.0.1",
4
+ "description": "",
5
+ "keywords": [
6
+ "opencode",
7
+ "plugin",
8
+ "completion-check-command"
9
+ ],
10
+ "homepage": "https://github.com/BacLuc/opencode-completion-check-command",
11
+ "bugs": {
12
+ "url": "https://github.com/BacLuc/opencode-completion-check-command/issues"
13
+ },
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/BacLuc/opencode-completion-check-command.git"
17
+ },
18
+ "license": "BSD-3-Clause",
19
+ "author": "Lucius Bachmann",
20
+ "type": "module",
21
+ "exports": {
22
+ "./server": "./src/server.ts"
23
+ },
24
+ "main": "src/server.ts",
25
+ "directories": {
26
+ "test": "tests"
27
+ },
28
+ "files": [
29
+ "src"
30
+ ],
31
+ "scripts": {
32
+ "test": "vitest run",
33
+ "test:watch": "vitest",
34
+ "typecheck": "tsc --noEmit",
35
+ "format": "prettier --write .",
36
+ "format:check": "prettier --check ."
37
+ },
38
+ "dependencies": {
39
+ "@opencode-ai/plugin": "1.15.12"
40
+ },
41
+ "devDependencies": {
42
+ "@types/bun": "1.3.14",
43
+ "prettier": "3.6.2",
44
+ "typescript": "5.9.3",
45
+ "vitest": "4.1.7"
46
+ }
47
+ }
@@ -0,0 +1,199 @@
1
+ import type { Hooks, Plugin, PluginInput } from '@opencode-ai/plugin'
2
+ import type { Event } from '@opencode-ai/sdk'
3
+
4
+ export const DEFAULT_MAX_RETRIES = 10
5
+
6
+ export function parseCodeBlock(input: string): string | null {
7
+ const codeBlockRegex = /(?:```|~~~)([a-zA-Z0-9_-]*)\n([\s\S]*?)(?:```|~~~)/
8
+ const match = input.match(codeBlockRegex)
9
+ if (match && match[2]) {
10
+ return match[2].trim()
11
+ }
12
+ return null
13
+ }
14
+
15
+ interface SessionEntry {
16
+ command: string
17
+ retries: number
18
+ }
19
+
20
+ export class CompletionCheckStore {
21
+ private entries = new Map<string, SessionEntry>()
22
+ private maxRetries: number
23
+
24
+ constructor(maxRetries = DEFAULT_MAX_RETRIES) {
25
+ this.maxRetries = maxRetries
26
+ }
27
+
28
+ set(sessionID: string, command: string): void {
29
+ this.entries.set(sessionID, { command, retries: 0 })
30
+ }
31
+
32
+ get(sessionID: string): string | undefined {
33
+ return this.entries.get(sessionID)?.command
34
+ }
35
+
36
+ getEntry(sessionID: string): SessionEntry | undefined {
37
+ return this.entries.get(sessionID)
38
+ }
39
+
40
+ incrementRetries(sessionID: string): number {
41
+ const entry = this.entries.get(sessionID)
42
+ if (!entry) {
43
+ return -1
44
+ }
45
+ entry.retries++
46
+ return entry.retries
47
+ }
48
+
49
+ retriesExhausted(sessionID: string): boolean {
50
+ const entry = this.entries.get(sessionID)
51
+ if (!entry) {
52
+ return false
53
+ }
54
+ return entry.retries >= this.maxRetries
55
+ }
56
+
57
+ delete(sessionID: string): void {
58
+ this.entries.delete(sessionID)
59
+ }
60
+
61
+ has(sessionID: string): boolean {
62
+ return this.entries.has(sessionID)
63
+ }
64
+
65
+ clear(): void {
66
+ this.entries.clear()
67
+ }
68
+
69
+ getMaxRetries(): number {
70
+ return this.maxRetries
71
+ }
72
+ }
73
+
74
+ export interface CommandResult {
75
+ exitCode: number
76
+ stdout: string
77
+ stderr: string
78
+ }
79
+
80
+ export async function executeCommand($: PluginInput['$'], command: string, cwd: string): Promise<CommandResult> {
81
+ try {
82
+ const result = await $`${command}`.nothrow().quiet().cwd(cwd)
83
+ return {
84
+ exitCode: result.exitCode,
85
+ stdout: result.text(),
86
+ stderr: result.stderr.toString(),
87
+ }
88
+ } catch (error) {
89
+ return {
90
+ exitCode: 1,
91
+ stdout: '',
92
+ stderr: error instanceof Error ? error.message : String(error),
93
+ }
94
+ }
95
+ }
96
+
97
+ export function buildFailureMessage(result: CommandResult): string {
98
+ let message = 'you are not yet finished:\n'
99
+ if (result.stdout) {
100
+ message += `\nstdout:\n${result.stdout}`
101
+ }
102
+ if (result.stderr) {
103
+ message += `\nstderr:\n${result.stderr}`
104
+ }
105
+ if (!result.stdout && !result.stderr) {
106
+ message += `\nCommand exited with code ${result.exitCode}`
107
+ }
108
+ return message
109
+ }
110
+
111
+ export const CompletionCheckCommandPlugin: Plugin = async (input, options) => {
112
+ const { client, $ } = input
113
+ const maxRetries = typeof options?.maxRetries === 'number' ? options.maxRetries : DEFAULT_MAX_RETRIES
114
+ const store = new CompletionCheckStore(maxRetries)
115
+
116
+ const processing = new Set<string>()
117
+
118
+ const hooks: Hooks = {
119
+ config: async (config) => {
120
+ if (!config.command) {
121
+ config.command = {}
122
+ }
123
+ if (!config.command['completion-check-command']) {
124
+ config.command['completion-check-command'] = {
125
+ template:
126
+ 'The user wants to verify task completion after you finish. Run your task, and when you are done, a completion check will automatically run to verify your work.\n\nThe completion check command is:\n{{arguments}}',
127
+ description:
128
+ 'Register a shell command that will be executed after the agent finishes to verify task completion. Include a markdown code block with the shell command to run.',
129
+ }
130
+ }
131
+ },
132
+
133
+ 'command.execute.before': async (input, output) => {
134
+ if (input.command !== 'completion-check-command') {
135
+ return
136
+ }
137
+
138
+ const command = parseCodeBlock(input.arguments)
139
+ if (!command) {
140
+ return
141
+ }
142
+
143
+ store.set(input.sessionID, command)
144
+ },
145
+
146
+ event: async ({ event }) => {
147
+ if (event.type !== 'session.idle') {
148
+ return
149
+ }
150
+
151
+ const sessionID = (event as Extract<Event, { type: 'session.idle' }>).properties.sessionID
152
+ const command = store.get(sessionID)
153
+ if (!command) {
154
+ return
155
+ }
156
+
157
+ if (processing.has(sessionID)) {
158
+ return
159
+ }
160
+ processing.add(sessionID)
161
+
162
+ try {
163
+ const result = await executeCommand($, command, input.directory)
164
+
165
+ if (result.exitCode === 0) {
166
+ store.delete(sessionID)
167
+ return
168
+ }
169
+
170
+ if (store.retriesExhausted(sessionID)) {
171
+ store.delete(sessionID)
172
+ return
173
+ }
174
+
175
+ store.incrementRetries(sessionID)
176
+
177
+ const failureMessage = buildFailureMessage(result)
178
+
179
+ await client.session.promptAsync({
180
+ path: { id: sessionID },
181
+ body: {
182
+ parts: [
183
+ {
184
+ type: 'text',
185
+ text: failureMessage,
186
+ },
187
+ ],
188
+ },
189
+ })
190
+ } finally {
191
+ processing.delete(sessionID)
192
+ }
193
+ },
194
+ }
195
+
196
+ return hooks
197
+ }
198
+
199
+ export default CompletionCheckCommandPlugin
package/src/index.ts ADDED
@@ -0,0 +1,9 @@
1
+ export {
2
+ CompletionCheckCommandPlugin,
3
+ DEFAULT_MAX_RETRIES,
4
+ parseCodeBlock,
5
+ CompletionCheckStore,
6
+ executeCommand,
7
+ buildFailureMessage,
8
+ } from './completion-check-command.js'
9
+ export type { CommandResult } from './completion-check-command.js'
package/src/server.ts ADDED
@@ -0,0 +1,9 @@
1
+ import type { PluginModule } from '@opencode-ai/plugin'
2
+ import { CompletionCheckCommandPlugin } from './completion-check-command.js'
3
+
4
+ const plugin: PluginModule = {
5
+ id: 'opencode-completion-check-command',
6
+ server: CompletionCheckCommandPlugin,
7
+ }
8
+
9
+ export default plugin