bacluc-opencode-completion-check-command 0.10.8 → 0.10.10

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/README.md CHANGED
@@ -36,9 +36,21 @@ The plugin will extract the command from the code block and run it when the agen
36
36
 
37
37
  ## Configuration
38
38
 
39
+ This plugin supports opencode V1 (>= 1.18.29) and V2.
40
+
39
41
  ### Plugin Options
40
42
 
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:
43
+ The plugin accepts a `maxRetries` option (default: `10`) that controls the maximum number of times the agent will be re-prompted when the check command fails.
44
+
45
+ V2:
46
+
47
+ ```jsonc
48
+ {
49
+ "plugins": [{ "package": "bacluc-opencode-completion-check-command@<version>", "options": { "maxRetries": 10 } }],
50
+ }
51
+ ```
52
+
53
+ V1:
42
54
 
43
55
  ```json
44
56
  {
@@ -50,7 +62,7 @@ Setting `maxRetries` to `0` disables re-prompting entirely — the command runs
50
62
 
51
63
  ### Custom Command Template
52
64
 
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`:
65
+ The `/completion-check-command` is automatically registered by the plugin. In V1 the plugin's `config` hook registers it, and an `opencode.json` override replaces the template (the plugin's `command.execute.before` hook still captures the command from the code block). V1 uses `command` and the `{{arguments}}` placeholder:
54
66
 
55
67
  ```json
56
68
  {
@@ -63,9 +75,11 @@ The `/completion-check-command` is automatically registered by the plugin's `con
63
75
  }
64
76
  ```
65
77
 
78
+ In V2 the plugin registers the command through the plugin API (`ctx.command.transform`) and reads the raw arguments itself, so the template cannot be overridden with a `commands` entry: a same-named `commands.completion-check-command` from config replaces the plugin's implementation and breaks command registration.
79
+
66
80
  ### Default Command Sources
67
81
 
68
- On `session.created` the plugin resolves the default command in precedence order:
82
+ The plugin resolves the default command once per session, on the first event it receives for that session (not on `session.created`, which is not reliably delivered to plugins on the headless `opencode run` path), in precedence order:
69
83
 
70
84
  1. `.agents/.completion-check-command` 2. `.opencode/.completion-check-command` 3. `AGENTS.md` code block after `/completion-check-command` 4. Claude hooks: `.claude/settings.local.json` → `.claude/settings.json` → `~/.claude/settings.json` (`hooks.Stop[].hooks[]` with `type == "command"` joined with `&&`)
71
85
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bacluc-opencode-completion-check-command",
3
- "version": "v0.10.8",
3
+ "version": "v0.10.10",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "opencode",
@@ -36,13 +36,15 @@
36
36
  "format:check": "prettier --check ."
37
37
  },
38
38
  "dependencies": {
39
- "@opencode-ai/plugin": "1.18.33"
39
+ "@opencode-ai/plugin": "1.18.35",
40
+ "@opencode-ai/sdk": "1.18.34",
41
+ "@opencode/plugin": "2.0.29"
40
42
  },
41
43
  "devDependencies": {
42
44
  "@types/bun": "1.4.2",
43
- "@types/node": "25.9.8",
45
+ "@types/node": "25.9.9",
44
46
  "prettier": "3.9.9",
45
47
  "typescript": "7.0.2",
46
- "vitest": "5.0.2"
48
+ "vitest": "5.0.3"
47
49
  }
48
50
  }
@@ -140,18 +140,31 @@ export function isUsageLimitError(error: unknown): boolean {
140
140
  if (!error || typeof error !== 'object') {
141
141
  return false
142
142
  }
143
- const { name, data } = error as { name?: string; data?: Record<string, unknown> }
144
- const statusCode = typeof data?.statusCode === 'number' ? data.statusCode : undefined
143
+ // V1: { name, data: { statusCode, message, responseBody } }
144
+ // V2: { type, message, status?, response?: { body } }
145
+ const e = error as {
146
+ name?: string
147
+ type?: string
148
+ status?: number
149
+ message?: string
150
+ response?: { body?: string }
151
+ data?: Record<string, unknown>
152
+ }
153
+ const statusCode =
154
+ typeof e.data?.statusCode === 'number' ? e.data.statusCode : typeof e.status === 'number' ? e.status : undefined
145
155
 
146
156
  // HTTP 429 from the provider always means rate / usage limit reached.
147
- if (name === 'APIError' && statusCode === 429) {
157
+ if (statusCode === 429) {
148
158
  return true
149
159
  }
150
160
 
151
- // Otherwise fall back to matching the human-readable message / response body,
152
- // which is how quota/credit exhaustion surfaces across providers.
153
- const message = typeof data?.message === 'string' ? data.message : ''
154
- const responseBody = typeof data?.responseBody === 'string' ? data.responseBody : ''
161
+ const message = typeof e.message === 'string' ? e.message : typeof e.data?.message === 'string' ? e.data.message : ''
162
+ const responseBody =
163
+ typeof e.response?.body === 'string'
164
+ ? e.response.body
165
+ : typeof e.data?.responseBody === 'string'
166
+ ? e.data.responseBody
167
+ : ''
155
168
  const haystack = `${message} ${responseBody}`.toLowerCase()
156
169
 
157
170
  return /usage limit|rate limit|quota|too many requests|credit balance|out of credits|insufficient (?:credit|balance|funds|quota)/.test(
package/src/server.ts CHANGED
@@ -1,9 +1,11 @@
1
- import type { PluginModule } from '@opencode-ai/plugin'
1
+ import { Plugin } from '@opencode/plugin'
2
2
  import { CompletionCheckCommandPlugin } from './completion-check-command.js'
3
+ import { CompletionCheckCommandSetup } from './v2.js'
3
4
 
4
- const plugin: PluginModule = {
5
- id: 'opencode-completion-check-command',
5
+ export default {
6
+ ...Plugin.define({
7
+ id: 'opencode-completion-check-command',
8
+ setup: CompletionCheckCommandSetup,
9
+ }),
6
10
  server: CompletionCheckCommandPlugin,
7
11
  }
8
-
9
- export default plugin
package/src/v2.ts ADDED
@@ -0,0 +1,142 @@
1
+ import type { Plugin } from '@opencode/plugin'
2
+ import {
3
+ buildFailureMessage,
4
+ CompletionCheckStore,
5
+ DEFAULT_MAX_RETRIES,
6
+ executeCommand,
7
+ isUsageLimitError,
8
+ parseCodeBlock,
9
+ readDefaultCommand,
10
+ } from './completion-check-command.js'
11
+
12
+ type PluginContext = Plugin.Context
13
+ type PluginEvent = ReturnType<PluginContext['event']['subscribe']> extends AsyncIterable<infer E> ? E : never
14
+
15
+ export const COMPLETION_CHECK_COMMAND_NAME = 'completion-check-command'
16
+
17
+ const COMMAND_DESCRIPTION =
18
+ '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.'
19
+
20
+ const COMMAND_TEMPLATE = (argumentsText: string): string =>
21
+ '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' +
22
+ argumentsText
23
+
24
+ export interface CompletionCheckRuntime {
25
+ ctx: PluginContext
26
+ store: CompletionCheckStore
27
+ processing: Set<string>
28
+ resolved: Set<string>
29
+ }
30
+
31
+ function eventSessionID(event: PluginEvent): string | undefined {
32
+ const sessionID = (event as { data?: { sessionID?: unknown } }).data?.sessionID
33
+ return typeof sessionID === 'string' ? sessionID : undefined
34
+ }
35
+
36
+ function eventDirectory(event: PluginEvent): string | undefined {
37
+ const location = (event as { location?: { directory?: unknown } }).location
38
+ if (typeof location?.directory === 'string') return location.directory
39
+ const dataLocation = (event as { data?: { location?: { directory?: unknown } } }).data?.location
40
+ return typeof dataLocation?.directory === 'string' ? dataLocation.directory : undefined
41
+ }
42
+
43
+ export async function handleCompletionCheckEvent(
44
+ { ctx, store, processing, resolved }: CompletionCheckRuntime,
45
+ event: PluginEvent,
46
+ ): Promise<void> {
47
+ const eventSession = eventSessionID(event)
48
+ if (eventSession && !resolved.has(eventSession)) {
49
+ resolved.add(eventSession)
50
+ if (!store.has(eventSession)) {
51
+ const directory = eventDirectory(event) ?? ctx.location.directory
52
+ const { command: defaultCommand } = await readDefaultCommand(directory)
53
+ if (defaultCommand) {
54
+ store.set(eventSession, defaultCommand, directory)
55
+ console.log(`[Completion Check] Using default command from ${directory}.`)
56
+ }
57
+ }
58
+ }
59
+
60
+ const executionEnded =
61
+ event.type === 'session.execution.succeeded' ||
62
+ event.type === 'session.execution.failed' ||
63
+ event.type === 'session.execution.interrupted'
64
+ if (!executionEnded) return
65
+ if (event.type === 'session.execution.interrupted' && event.data.reason !== 'user') return
66
+
67
+ const sessionID = event.data.sessionID
68
+ const command = store.get(sessionID)
69
+ if (!command) return
70
+ if (processing.has(sessionID)) return
71
+ processing.add(sessionID)
72
+
73
+ try {
74
+ if (event.type === 'session.execution.failed' && isUsageLimitError(event.data.error)) {
75
+ // The agent was cut off by the provider's usage limit rather than
76
+ // finishing. Skip the completion check (and the re-prompt).
77
+ console.warn('[Completion Check] Skipped: usage limit reached.')
78
+ return
79
+ }
80
+
81
+ const directory = store.getDirectory(sessionID) || ctx.location.directory
82
+ const result = await executeCommand(command, directory)
83
+
84
+ if (result.exitCode === 0) {
85
+ store.delete(sessionID)
86
+ console.log(`[Completion Check] Command succeeded. Task is complete.\nCommand: ${command}`)
87
+ return
88
+ }
89
+
90
+ if (store.retriesExhausted(sessionID)) {
91
+ store.delete(sessionID)
92
+ return
93
+ }
94
+
95
+ store.incrementRetries(sessionID)
96
+ await ctx.session.prompt({ sessionID, text: buildFailureMessage(result) })
97
+ } finally {
98
+ processing.delete(sessionID)
99
+ }
100
+ }
101
+
102
+ export async function CompletionCheckCommandSetup(ctx: PluginContext): Promise<() => void> {
103
+ const maxRetries = typeof ctx.options?.maxRetries === 'number' ? ctx.options.maxRetries : DEFAULT_MAX_RETRIES
104
+ const store = new CompletionCheckStore(maxRetries)
105
+ const processing = new Set<string>()
106
+ const resolved = new Set<string>()
107
+
108
+ await ctx.command.transform((editor) => {
109
+ editor.add({
110
+ name: COMPLETION_CHECK_COMMAND_NAME,
111
+ description: COMMAND_DESCRIPTION,
112
+ execute: async ({ sessionID, prompt, delivery }) => {
113
+ const command = parseCodeBlock(prompt.text)
114
+ if (!command) {
115
+ console.warn(
116
+ "[Completion Check] I couldn't find a code block in your message. Include a markdown code block with the shell command to run.",
117
+ )
118
+ return
119
+ }
120
+ store.set(sessionID, command, store.getDirectory(sessionID))
121
+ await ctx.session.prompt({ sessionID, text: COMMAND_TEMPLATE(prompt.text), delivery })
122
+ },
123
+ })
124
+ })
125
+
126
+ const controller = new AbortController()
127
+ void (async () => {
128
+ try {
129
+ for await (const event of ctx.event.subscribe({ signal: controller.signal })) {
130
+ void handleCompletionCheckEvent({ ctx, store, processing, resolved }, event).catch((error) => {
131
+ console.error('[Completion Check] Failed to handle event', error)
132
+ })
133
+ }
134
+ } catch (error) {
135
+ if (!controller.signal.aborted) {
136
+ console.error('[Completion Check] Event subscription ended', error)
137
+ }
138
+ }
139
+ })()
140
+
141
+ return () => controller.abort()
142
+ }