opencode-copilot-delegate 0.12.1 → 0.13.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.
Files changed (35) hide show
  1. package/CHANGELOG.md +19 -1
  2. package/README.md +16 -16
  3. package/dist/index.js +20666 -14568
  4. package/dist/lib/normalize-tool-arg-schemas.d.ts +1 -1
  5. package/dist/lib/process-global.d.ts +18 -0
  6. package/dist/lib/tool.d.ts +25 -0
  7. package/dist/runtime/plugin-singleton.d.ts +60 -36
  8. package/dist/runtime/rpc-paths.d.ts +6 -0
  9. package/dist/runtime/rpc-port-sweep.d.ts +38 -0
  10. package/dist/runtime/rpc-server.d.ts +10 -0
  11. package/dist/tools/cancel.d.ts +3 -9
  12. package/dist/tools/delegate.d.ts +8 -19
  13. package/dist/tools/output.d.ts +5 -13
  14. package/dist/tools/resume.d.ts +13 -15
  15. package/dist/tui/index.js +2818 -10922
  16. package/dist/tui/status-dialogs.d.ts +17 -0
  17. package/dist/tui/status-format.d.ts +10 -0
  18. package/package.json +17 -13
  19. package/src/tui/index.tsx +11 -42
  20. package/src/tui/rpc-client.ts +5 -13
  21. package/src/tui/status-dialogs.tsx +243 -0
  22. package/src/tui/{components/row.tsx → status-format.ts} +10 -6
  23. package/dist/tui/__tests__/confirm-card.test.d.ts +0 -2
  24. package/dist/tui/__tests__/index.test.d.ts +0 -1
  25. package/dist/tui/__tests__/modal-list.test.d.ts +0 -1
  26. package/dist/tui/__tests__/rpc-client.test.d.ts +0 -1
  27. package/dist/tui/components/confirm-card.d.ts +0 -12
  28. package/dist/tui/components/modal-list.d.ts +0 -16
  29. package/dist/tui/components/row.d.ts +0 -6
  30. package/src/tui/__tests__/confirm-card.test.tsx +0 -516
  31. package/src/tui/__tests__/index.test.ts +0 -561
  32. package/src/tui/__tests__/modal-list.test.tsx +0 -518
  33. package/src/tui/__tests__/rpc-client.test.ts +0 -507
  34. package/src/tui/components/confirm-card.tsx +0 -252
  35. package/src/tui/components/modal-list.tsx +0 -232
@@ -0,0 +1,17 @@
1
+ /** @jsxImportSource @opentui/solid */
2
+ import type { TuiPluginApi } from '@opencode-ai/plugin/tui';
3
+ import type { RpcClient } from './rpc-client';
4
+ export type StatusDialogsRpc = Pick<RpcClient, 'tasksList' | 'tasksCancel'>;
5
+ export type StatusDialogsOptions = {
6
+ rpc: StatusDialogsRpc;
7
+ /** Schedules `fn` after the host's own close has finished. Returns a canceller. */
8
+ defer?: (fn: () => void) => () => void;
9
+ now?: () => number;
10
+ };
11
+ export type StatusDialogs = {
12
+ openList: (currentTaskId?: string) => void;
13
+ dispose: () => void;
14
+ };
15
+ type DialogApi = Pick<TuiPluginApi, 'ui'>;
16
+ export declare function createStatusDialogs(api: DialogApi, options: StatusDialogsOptions): StatusDialogs;
17
+ export {};
@@ -0,0 +1,10 @@
1
+ import type { TasksListResponse } from '../runtime/rpc-contract';
2
+ export type StatusTask = TasksListResponse['tasks'][number];
3
+ export declare function displayValue(value: string): string;
4
+ export declare function formatElapsed(elapsedMs: number): string;
5
+ export declare function formatStatus(status: StatusTask['status']): string;
6
+ export declare function formatToolCalls(toolCallCount: number): string;
7
+ /** Secondary line of a list option: who is running it and how busy it is. */
8
+ export declare function formatTaskDescription(task: StatusTask): string;
9
+ /** Right-hand footer of a list option: status and elapsed time. */
10
+ export declare function formatTaskFooter(task: StatusTask, nowMs: number): string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-copilot-delegate",
3
- "version": "0.12.1",
3
+ "version": "0.13.0",
4
4
  "description": "OpenCode plugin that delegates tasks to GitHub Copilot CLI as background subprocesses",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -26,6 +26,8 @@
26
26
  "files": [
27
27
  "dist",
28
28
  "src/tui",
29
+ "!src/tui/__tests__",
30
+ "!dist/**/__tests__",
29
31
  "README.md",
30
32
  "LICENSE",
31
33
  "CHANGELOG.md"
@@ -55,13 +57,13 @@
55
57
  "@opencode-ai/plugin": ">=1.14.41"
56
58
  },
57
59
  "devDependencies": {
58
- "@biomejs/biome": "2.5.3",
59
- "@changesets/cli": "2.31.0",
60
- "@opencode-ai/plugin": "1.17.13",
60
+ "@biomejs/biome": "2.6.8",
61
+ "@changesets/cli": "2.31.1",
62
+ "@opencode-ai/plugin": "1.19.6",
61
63
  "@types/babel__core": "7.20.5",
62
- "@types/bun": "1.3.14",
63
- "@types/node": "24.13.2",
64
- "rimraf": "6.1.3",
64
+ "@types/bun": "1.4.4",
65
+ "@types/node": "26.6.6",
66
+ "rimraf": "6.1.4",
65
67
  "typescript": "7.0.2"
66
68
  },
67
69
  "scripts": {
@@ -74,18 +76,20 @@
74
76
  "test:integration": "bun test tests/integration/",
75
77
  "lint": "biome check .",
76
78
  "fix": "biome check . --write",
77
- "prepublishOnly": "bun run build",
79
+ "prepublishOnly": "bun run typecheck && bun run test:unit && bun run build",
78
80
  "version-changesets": "changeset version && bun install --lockfile-only",
79
81
  "publish-changesets": "bun run build && changeset publish"
80
82
  },
81
83
  "dependencies": {
82
- "@opentui/core": "0.2.6",
83
- "@opentui/solid": "0.2.6",
84
+ "@opentui/core": "0.4.5",
85
+ "@opentui/solid": "0.4.5",
84
86
  "fkill": "10.0.3",
85
- "solid-js": "1.9.14",
86
- "zod": "^4.3.0"
87
+ "solid-js": "1.9.19",
88
+ "zod": "^4.6.7"
87
89
  },
88
90
  "overrides": {
89
- "zod": "^4.3.0"
91
+ "@opentui/core": "0.4.5",
92
+ "zod": "^4.6.7",
93
+ "brace-expansion@2": "^2.1.8"
90
94
  }
91
95
  }
package/src/tui/index.tsx CHANGED
@@ -1,16 +1,8 @@
1
1
  /** @jsxImportSource @opentui/solid */
2
2
 
3
3
  import type { TuiPlugin, TuiPluginModule } from '@opencode-ai/plugin/tui'
4
- import { ModalList } from './components/modal-list'
5
4
  import { createRpcClient } from './rpc-client'
6
-
7
- function errorMessage(error: unknown): string {
8
- if (error instanceof Error && error.message.trim().length > 0) {
9
- return error.message.trim()
10
- }
11
-
12
- return 'Unknown RPC error.'
13
- }
5
+ import { createStatusDialogs } from './status-dialogs'
14
6
 
15
7
  /**
16
8
  * Narrow structural interface for `api.keymap.registerLayer`. Mirrors the
@@ -28,6 +20,9 @@ interface KeymapRegisterLayerHost {
28
20
  name: string
29
21
  title: string
30
22
  category: string
23
+ slashName?: string
24
+ slashAliases?: ReadonlyArray<string>
25
+ desc?: string
31
26
  run: () => void
32
27
  }>
33
28
  bindings: ReadonlyArray<unknown>
@@ -53,8 +48,8 @@ function hasKeymapRegisterLayer(
53
48
  * back to `command.register` for hosts pinned at 1.14.41 or earlier.
54
49
  *
55
50
  * If neither surface is available, registration is a no-op and a warning
56
- * is logged. The TUI plugin still loads (dialog UI continues to work);
57
- * only the slash command discovery affordance is missing.
51
+ * is logged. The TUI plugin still loads; only the slash command discovery
52
+ * affordance is missing.
58
53
  *
59
54
  * Reference: Magic Context commit `5fe1c4f` on cortexkit/magic-context
60
55
  * established this dual-path pattern across the same upstream gap.
@@ -71,6 +66,8 @@ function registerCopilotStatusCommand(
71
66
  name: 'copilot-status',
72
67
  title: 'Copilot Status',
73
68
  category: 'Copilot',
69
+ slashName: 'copilot-status',
70
+ desc: 'Show running Copilot delegations',
74
71
  run() {
75
72
  openList()
76
73
  },
@@ -106,42 +103,14 @@ function registerCopilotStatusCommand(
106
103
 
107
104
  const tui: TuiPlugin = async (api) => {
108
105
  const rpc = createRpcClient()
109
- const closeDialog = () => {
110
- api.ui.dialog.clear()
111
- }
112
-
113
- const showUnavailableAlert = (error: unknown) => {
114
- api.ui.dialog.setSize('medium')
115
- api.ui.dialog.replace(() => {
116
- return (
117
- <api.ui.DialogAlert
118
- title="Status unavailable."
119
- message={errorMessage(error)}
120
- onConfirm={closeDialog}
121
- />
122
- )
123
- })
124
- }
125
-
126
- const openList = (initialTaskId?: string) => {
127
- api.ui.dialog.setSize('large')
128
- api.ui.dialog.replace(() => {
129
- return (
130
- <ModalList
131
- onClose={closeDialog}
132
- onLoadError={showUnavailableAlert}
133
- initialTaskId={initialTaskId}
134
- rpc={rpc}
135
- />
136
- )
137
- })
138
- }
106
+ const dialogs = createStatusDialogs(api, { rpc })
139
107
 
140
108
  const unregister = registerCopilotStatusCommand(api, () => {
141
- openList()
109
+ dialogs.openList()
142
110
  })
143
111
 
144
112
  api.lifecycle.onDispose(() => {
113
+ dialogs.dispose()
145
114
  rpc.dispose()
146
115
  unregister()
147
116
  })
@@ -1,5 +1,4 @@
1
1
  import { readFile } from 'node:fs/promises'
2
- import { homedir } from 'node:os'
3
2
  import { join } from 'node:path'
4
3
  import type { ZodIssue, ZodType } from 'zod'
5
4
  import {
@@ -13,6 +12,10 @@ import {
13
12
  type TasksListResponse,
14
13
  TasksListResponseSchema,
15
14
  } from '../runtime/rpc-contract'
15
+ import {
16
+ defaultPortFileBaseDir,
17
+ defaultSessionDiscriminator,
18
+ } from '../runtime/rpc-paths'
16
19
 
17
20
  type CreateRpcClientOptions = {
18
21
  baseDir?: string
@@ -83,20 +86,9 @@ export class RpcAuthError extends RpcServerError {
83
86
  }
84
87
  }
85
88
 
86
- function defaultBaseDir(): string {
87
- const cacheHome =
88
- process.env.XDG_CACHE_HOME ?? join(process.env.HOME ?? homedir(), '.cache')
89
-
90
- return join(cacheHome, 'opencode', 'copilot-delegate')
91
- }
92
-
93
- function defaultSessionDiscriminator(): string {
94
- return process.env.OPENCODE_SESSION_ID ?? String(process.pid)
95
- }
96
-
97
89
  function resolvePortFilePath(options: CreateRpcClientOptions): string {
98
90
  return join(
99
- options.baseDir ?? defaultBaseDir(),
91
+ options.baseDir ?? defaultPortFileBaseDir(),
100
92
  options.sessionDiscriminator ?? defaultSessionDiscriminator(),
101
93
  'server-port.json',
102
94
  )
@@ -0,0 +1,243 @@
1
+ /** @jsxImportSource @opentui/solid */
2
+
3
+ import type { TuiPluginApi } from '@opencode-ai/plugin/tui'
4
+ import type { RpcClient } from './rpc-client'
5
+ import {
6
+ formatTaskDescription,
7
+ formatTaskFooter,
8
+ type StatusTask,
9
+ } from './status-format'
10
+
11
+ /**
12
+ * `/copilot-status` UI built from the host's own dialog components
13
+ * (`DialogSelect`, `DialogConfirm`, `DialogAlert`).
14
+ *
15
+ * The host owns theming, keyboard handling, and dialog teardown (including
16
+ * Esc). This module must therefore never call `api.ui.dialog.clear()` and
17
+ * must never pass an `onClose` to `api.ui.dialog.replace(...)`: feeding
18
+ * host-owned close handling back into `clear()` re-entered dialog teardown
19
+ * and froze the TUI on Esc (see
20
+ * docs/solutions/ui-bugs/re-entrant-dialog-close-froze-copilot-status-on-escape-2026-05-02.md).
21
+ *
22
+ * Host `DialogConfirm` / `DialogAlert` call `dialog.clear()` themselves right
23
+ * after `onConfirm` / `onCancel` returns, so any follow-up dialog is opened on
24
+ * a deferred tick instead of synchronously from those callbacks.
25
+ */
26
+
27
+ /** Delay before reopening a dialog after a host confirm/alert closes itself. */
28
+ const REOPEN_DELAY_MS = 50
29
+
30
+ export type StatusDialogsRpc = Pick<RpcClient, 'tasksList' | 'tasksCancel'>
31
+
32
+ export type StatusDialogsOptions = {
33
+ rpc: StatusDialogsRpc
34
+ /** Schedules `fn` after the host's own close has finished. Returns a canceller. */
35
+ defer?: (fn: () => void) => () => void
36
+ now?: () => number
37
+ }
38
+
39
+ export type StatusDialogs = {
40
+ openList: (currentTaskId?: string) => void
41
+ dispose: () => void
42
+ }
43
+
44
+ type DialogApi = Pick<TuiPluginApi, 'ui'>
45
+
46
+ function errorMessage(error: unknown): string {
47
+ if (error instanceof Error && error.message.trim().length > 0) {
48
+ return error.message.trim()
49
+ }
50
+
51
+ return 'Unknown RPC error.'
52
+ }
53
+
54
+ function warnNonFatal(error: unknown) {
55
+ // The TUI half has no structured log channel; a failure here must never
56
+ // take the host down, so it is reported and swallowed.
57
+ console.warn(
58
+ `[opencode-copilot-delegate] /copilot-status dialog failed: ${errorMessage(error)}`,
59
+ )
60
+ }
61
+
62
+ function defaultDefer(fn: () => void): () => void {
63
+ const handle = setTimeout(fn, REOPEN_DELAY_MS)
64
+
65
+ return () => {
66
+ clearTimeout(handle)
67
+ }
68
+ }
69
+
70
+ export function createStatusDialogs(
71
+ api: DialogApi,
72
+ options: StatusDialogsOptions,
73
+ ): StatusDialogs {
74
+ const { rpc } = options
75
+ const defer = options.defer ?? defaultDefer
76
+ const now = options.now ?? (() => Date.now())
77
+ const pending = new Set<() => void>()
78
+ let disposed = false
79
+ let generation = 0
80
+
81
+ const deferred = (fn: () => void) => {
82
+ const cancel = defer(() => {
83
+ pending.delete(cancel)
84
+ if (disposed) {
85
+ return
86
+ }
87
+
88
+ try {
89
+ fn()
90
+ } catch (error) {
91
+ warnNonFatal(error)
92
+ }
93
+ })
94
+ pending.add(cancel)
95
+ }
96
+
97
+ const showAlert = (
98
+ title: string,
99
+ message: string,
100
+ onConfirm?: () => void,
101
+ ) => {
102
+ api.ui.dialog.setSize('medium')
103
+ api.ui.dialog.replace(() => (
104
+ <api.ui.DialogAlert
105
+ title={title}
106
+ message={message}
107
+ onConfirm={onConfirm}
108
+ />
109
+ ))
110
+ }
111
+
112
+ const showCancelFailure = (task: StatusTask, message: string) => {
113
+ showAlert(
114
+ `Could not cancel ${task.taskId}`,
115
+ message,
116
+ // The host closes the alert itself; come back to the list afterwards.
117
+ () => {
118
+ deferred(() => {
119
+ openList(task.taskId)
120
+ })
121
+ },
122
+ )
123
+ }
124
+
125
+ const cancelTask = async (task: StatusTask) => {
126
+ let failure: string | undefined
127
+
128
+ try {
129
+ const response = await rpc.tasksCancel(task.taskId)
130
+
131
+ // "no such task" means it already finished; the refreshed list shows that.
132
+ if (!response.cancelled && response.error !== 'no such task') {
133
+ failure = response.error ?? 'Unknown RPC error.'
134
+ }
135
+ } catch (error) {
136
+ failure = errorMessage(error)
137
+ }
138
+
139
+ if (disposed) {
140
+ return
141
+ }
142
+
143
+ // The host's DialogConfirm clears itself after `onConfirm`; defer so the
144
+ // next dialog lands after that close rather than inside it.
145
+ deferred(() => {
146
+ if (failure === undefined) {
147
+ openList()
148
+ } else {
149
+ showCancelFailure(task, failure)
150
+ }
151
+ })
152
+ }
153
+
154
+ const openConfirm = (task: StatusTask) => {
155
+ api.ui.dialog.setSize('medium')
156
+ api.ui.dialog.replace(() => (
157
+ <api.ui.DialogConfirm
158
+ title={`Cancel Copilot delegation ${task.taskId}?`}
159
+ message="Confirm stops the running Copilot process. Cancel keeps it running."
160
+ onConfirm={() => {
161
+ void cancelTask(task)
162
+ }}
163
+ onCancel={() => {
164
+ deferred(() => {
165
+ openList(task.taskId)
166
+ })
167
+ }}
168
+ />
169
+ ))
170
+ }
171
+
172
+ const renderList = (tasks: StatusTask[], currentTaskId?: string) => {
173
+ if (tasks.length === 0) {
174
+ showAlert(
175
+ 'No Copilot delegations are running.',
176
+ 'Start one with the copilot_delegate tool, then reopen /copilot-status.',
177
+ )
178
+ return
179
+ }
180
+
181
+ const nowMs = now()
182
+ const byId = new Map(tasks.map((task) => [task.taskId, task]))
183
+ const running = tasks.filter((task) => task.status === 'running').length
184
+
185
+ api.ui.dialog.setSize('large')
186
+ api.ui.dialog.replace(() => (
187
+ <api.ui.DialogSelect<string>
188
+ title={`Copilot delegations (${running} running)`}
189
+ current={currentTaskId}
190
+ options={tasks.map((task) => ({
191
+ title: task.taskId,
192
+ value: task.taskId,
193
+ description: formatTaskDescription(task),
194
+ footer: formatTaskFooter(task, nowMs),
195
+ // Only a running task can be cancelled.
196
+ disabled: task.status !== 'running',
197
+ }))}
198
+ onSelect={(option) => {
199
+ const task = byId.get(option.value)
200
+
201
+ if (task?.status === 'running') {
202
+ openConfirm(task)
203
+ }
204
+ }}
205
+ />
206
+ ))
207
+ }
208
+
209
+ const openList = (currentTaskId?: string) => {
210
+ const mine = ++generation
211
+
212
+ void (async () => {
213
+ let tasks: StatusTask[]
214
+
215
+ try {
216
+ tasks = (await rpc.tasksList()).tasks
217
+ } catch (error) {
218
+ if (!disposed && mine === generation) {
219
+ showAlert('Status unavailable.', errorMessage(error))
220
+ }
221
+ return
222
+ }
223
+
224
+ // A newer open, or disposal, supersedes this load.
225
+ if (disposed || mine !== generation) {
226
+ return
227
+ }
228
+
229
+ renderList(tasks, currentTaskId)
230
+ })().catch(warnNonFatal)
231
+ }
232
+
233
+ return {
234
+ openList,
235
+ dispose() {
236
+ disposed = true
237
+ for (const cancel of pending) {
238
+ cancel()
239
+ }
240
+ pending.clear()
241
+ },
242
+ }
243
+ }
@@ -1,6 +1,6 @@
1
- import type { TasksListResponse } from '../../runtime/rpc-contract'
1
+ import type { TasksListResponse } from '../runtime/rpc-contract'
2
2
 
3
- export type ModalListTask = TasksListResponse['tasks'][number]
3
+ export type StatusTask = TasksListResponse['tasks'][number]
4
4
 
5
5
  export function displayValue(value: string): string {
6
6
  const trimmed = value.trim()
@@ -26,7 +26,7 @@ export function formatElapsed(elapsedMs: number): string {
26
26
  return `${totalSeconds}s`
27
27
  }
28
28
 
29
- function formatStatus(status: ModalListTask['status']): string {
29
+ export function formatStatus(status: StatusTask['status']): string {
30
30
  return status === 'running' ? `● ${status}` : status
31
31
  }
32
32
 
@@ -34,8 +34,12 @@ export function formatToolCalls(toolCallCount: number): string {
34
34
  return `${toolCallCount} ${toolCallCount === 1 ? 'call' : 'calls'}`
35
35
  }
36
36
 
37
- export function formatRowText(task: ModalListTask, nowMs: number): string {
38
- const elapsed = Math.max(0, nowMs - task.startedAt)
37
+ /** Secondary line of a list option: who is running it and how busy it is. */
38
+ export function formatTaskDescription(task: StatusTask): string {
39
+ return `${displayValue(task.agent)} · ${displayValue(task.model)} · ${formatToolCalls(task.toolCallCount)}`
40
+ }
39
41
 
40
- return `${formatStatus(task.status)} ${task.taskId} ${displayValue(task.agent)} ${displayValue(task.model)} ${formatElapsed(elapsed)} ${formatToolCalls(task.toolCallCount)}`
42
+ /** Right-hand footer of a list option: status and elapsed time. */
43
+ export function formatTaskFooter(task: StatusTask, nowMs: number): string {
44
+ return `${formatStatus(task.status)} · ${formatElapsed(Math.max(0, nowMs - task.startedAt))}`
41
45
  }
@@ -1,2 +0,0 @@
1
- /** @jsxImportSource @opentui/solid */
2
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1,12 +0,0 @@
1
- /** @jsxImportSource @opentui/solid */
2
- import type { RpcClient } from '../rpc-client';
3
- import type { ModalListTask } from './row';
4
- type ConfirmCardProps = {
5
- task: ModalListTask;
6
- rpc: Pick<RpcClient, 'tasksCancel'>;
7
- onConfirm: () => void;
8
- onCancel: () => void;
9
- onDismissAfterError: () => void;
10
- };
11
- export declare function ConfirmCard(props: ConfirmCardProps): any;
12
- export default ConfirmCard;
@@ -1,16 +0,0 @@
1
- /** @jsxImportSource @opentui/solid */
2
- import type { RpcClient } from '../rpc-client';
3
- export type ModalListClock = {
4
- now(): number;
5
- setInterval(fn: () => void, delayMs: number): unknown;
6
- clearInterval(handle: unknown): void;
7
- };
8
- type ModalListProps = {
9
- rpc: Pick<RpcClient, 'tasksList'>;
10
- onClose: () => void;
11
- onLoadError?: (error: unknown) => void;
12
- initialTaskId?: string;
13
- clock?: ModalListClock;
14
- };
15
- export declare function ModalList(props: ModalListProps): any;
16
- export default ModalList;
@@ -1,6 +0,0 @@
1
- import type { TasksListResponse } from '../../runtime/rpc-contract';
2
- export type ModalListTask = TasksListResponse['tasks'][number];
3
- export declare function displayValue(value: string): string;
4
- export declare function formatElapsed(elapsedMs: number): string;
5
- export declare function formatToolCalls(toolCallCount: number): string;
6
- export declare function formatRowText(task: ModalListTask, nowMs: number): string;