opencode-copilot-delegate 0.8.0 → 0.10.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.
@@ -0,0 +1,43 @@
1
+ import { type HealthResponse, type TasksCancelResponse, type TasksListResponse } from '../runtime/rpc-contract';
2
+ type CreateRpcClientOptions = {
3
+ baseDir?: string;
4
+ sessionDiscriminator?: string;
5
+ fetchImpl?: typeof fetch;
6
+ requestTimeoutMs?: number;
7
+ };
8
+ export declare class RpcUnreachableError extends Error {
9
+ constructor(message: string, options?: {
10
+ cause?: unknown;
11
+ });
12
+ }
13
+ export declare class RpcValidationError extends Error {
14
+ readonly issues?: string[];
15
+ constructor(message: string, options?: {
16
+ cause?: unknown;
17
+ issues?: string[];
18
+ });
19
+ }
20
+ export declare class RpcServerError extends Error {
21
+ readonly status: number;
22
+ readonly path: string;
23
+ constructor(message: string, options: {
24
+ cause?: unknown;
25
+ path: string;
26
+ status: number;
27
+ });
28
+ }
29
+ export declare class RpcAuthError extends RpcServerError {
30
+ constructor(message: string, options: {
31
+ cause?: unknown;
32
+ path: string;
33
+ status: number;
34
+ });
35
+ }
36
+ export declare function createRpcClient(options?: CreateRpcClientOptions): {
37
+ health(): Promise<HealthResponse>;
38
+ tasksList(): Promise<TasksListResponse>;
39
+ tasksCancel(taskId: string): Promise<TasksCancelResponse>;
40
+ dispose(): void;
41
+ };
42
+ export type RpcClient = ReturnType<typeof createRpcClient>;
43
+ export {};
package/package.json CHANGED
@@ -1,12 +1,31 @@
1
1
  {
2
2
  "name": "opencode-copilot-delegate",
3
- "version": "0.8.0",
3
+ "version": "0.10.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",
7
7
  "types": "dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js"
12
+ },
13
+ "./plugin": {
14
+ "types": "./dist/index.d.ts",
15
+ "import": "./dist/index.js"
16
+ },
17
+ "./tui": {
18
+ "types": "./dist/tui/index.d.ts",
19
+ "import": "./dist/tui/index.js"
20
+ }
21
+ },
22
+ "oc-plugin": [
23
+ "server",
24
+ "tui"
25
+ ],
8
26
  "files": [
9
27
  "dist",
28
+ "src/tui",
10
29
  "README.md",
11
30
  "LICENSE",
12
31
  "CHANGELOG.md"
@@ -36,20 +55,21 @@
36
55
  "@opencode-ai/plugin": ">=1.14.0"
37
56
  },
38
57
  "devDependencies": {
39
- "@biomejs/biome": "^2.4.13",
40
- "@changesets/cli": "^2.31.0",
41
- "@opencode-ai/plugin": "^1.14.29",
42
- "@types/bun": "^1.2.0",
43
- "@types/node": "^24.0.0",
44
- "rimraf": "^6.0.1",
45
- "typescript": "^6.0.3"
58
+ "@biomejs/biome": "2.4.13",
59
+ "@changesets/cli": "2.31.0",
60
+ "@opencode-ai/plugin": "1.14.30",
61
+ "@types/bun": "1.3.13",
62
+ "@types/node": "24.12.2",
63
+ "rimraf": "6.1.3",
64
+ "typescript": "6.0.3"
46
65
  },
47
66
  "scripts": {
48
67
  "clean": "rimraf dist",
49
- "build": "bun run clean && bun build src/index.ts --outdir dist --target bun --external @opencode-ai/plugin && tsc --emitDeclarationOnly --noEmit false",
68
+ "build": "bun run clean && bun scripts/build.ts && tsc --emitDeclarationOnly --noEmit false",
50
69
  "typecheck": "tsc --noEmit",
51
70
  "test": "bun test",
52
71
  "test:unit": "bun test tests/*.test.ts",
72
+ "test:tui": "bun test --preload @opentui/solid/preload src/tui/__tests__/*.test.ts src/tui/__tests__/*.test.tsx",
53
73
  "test:integration": "bun test tests/integration/",
54
74
  "lint": "biome check .",
55
75
  "fix": "biome check . --write",
@@ -58,7 +78,10 @@
58
78
  "publish-changesets": "bun run build && changeset publish"
59
79
  },
60
80
  "dependencies": {
81
+ "@opentui/core": "0.1.105",
82
+ "@opentui/solid": "0.1.105",
61
83
  "fkill": "10.0.3",
84
+ "solid-js": "1.9.11",
62
85
  "zod": "^4.3.0"
63
86
  },
64
87
  "overrides": {
@@ -0,0 +1 @@
1
+
@@ -0,0 +1,500 @@
1
+ /** @jsxImportSource @opentui/solid */
2
+
3
+ import { describe, expect, it } from 'bun:test'
4
+ import type { TuiDialogProps } from '@opencode-ai/plugin/tui'
5
+ import { type JSX, testRender } from '@opentui/solid'
6
+ import {
7
+ RpcAuthError,
8
+ RpcServerError,
9
+ RpcUnreachableError,
10
+ } from '../rpc-client'
11
+
12
+ type TaskStatus = 'running' | 'cancelling' | 'complete' | 'failed' | 'cancelled'
13
+
14
+ type TaskRow = {
15
+ taskId: string
16
+ status: TaskStatus
17
+ agent: string
18
+ model: string
19
+ elapsedMs: number
20
+ toolCallCount: number
21
+ startedAt: number
22
+ }
23
+
24
+ type ConfirmCardProps = {
25
+ Dialog: (props: TuiDialogProps) => JSX.Element
26
+ task: TaskRow
27
+ rpc: {
28
+ tasksCancel(taskId: string): Promise<{ cancelled: boolean; error?: string }>
29
+ }
30
+ onConfirm: () => void
31
+ onCancel: () => void
32
+ onDismissAfterError: () => void
33
+ }
34
+
35
+ type ConfirmCardComponent = (props: ConfirmCardProps) => JSX.Element
36
+
37
+ type Deferred<T> = {
38
+ promise: Promise<T>
39
+ resolve(value: T): void
40
+ reject(error: unknown): void
41
+ }
42
+
43
+ async function loadConfirmCard(): Promise<ConfirmCardComponent | undefined> {
44
+ await import('@opentui/solid/runtime-plugin-support')
45
+
46
+ try {
47
+ const module = await import('../components/confirm-card')
48
+ const confirmCard =
49
+ Reflect.get(module, 'ConfirmCard') ?? Reflect.get(module, 'default')
50
+
51
+ if (typeof confirmCard !== 'function') {
52
+ return undefined
53
+ }
54
+
55
+ return confirmCard as ConfirmCardComponent
56
+ } catch {
57
+ return undefined
58
+ }
59
+ }
60
+
61
+ function createDeferred<T>(): Deferred<T> {
62
+ let resolveFn: (value: T) => void = () => {}
63
+ let rejectFn: (error: unknown) => void = () => {}
64
+ const promise = new Promise<T>((resolve, reject) => {
65
+ resolveFn = resolve
66
+ rejectFn = reject
67
+ })
68
+
69
+ return {
70
+ promise,
71
+ resolve(value: T) {
72
+ resolveFn(value)
73
+ },
74
+ reject(error: unknown) {
75
+ rejectFn(error)
76
+ },
77
+ }
78
+ }
79
+
80
+ function createDialogStub() {
81
+ let onClose: (() => void) | undefined
82
+
83
+ const Dialog = (props: TuiDialogProps) => {
84
+ onClose = props.onClose
85
+ return <box flexDirection="column">{props.children}</box>
86
+ }
87
+
88
+ return {
89
+ Dialog,
90
+ getOnClose: () => onClose,
91
+ }
92
+ }
93
+
94
+ function makeTask(taskId: string, overrides: Partial<TaskRow> = {}): TaskRow {
95
+ return {
96
+ taskId,
97
+ status: 'running',
98
+ agent: 'default',
99
+ model: 'gpt-5',
100
+ elapsedMs: 0,
101
+ toolCallCount: 0,
102
+ startedAt: 0,
103
+ ...overrides,
104
+ }
105
+ }
106
+
107
+ async function settle(
108
+ renderOnce: () => Promise<void>,
109
+ idle: () => Promise<void>,
110
+ ) {
111
+ await Promise.resolve()
112
+ await new Promise((resolve) => setTimeout(resolve, 0))
113
+ await renderOnce()
114
+ await idle()
115
+ await renderOnce()
116
+ }
117
+
118
+ async function waitForFrame(options: {
119
+ renderOnce: () => Promise<void>
120
+ idle: () => Promise<void>
121
+ captureCharFrame: () => string
122
+ includes: string
123
+ attempts?: number
124
+ }): Promise<string> {
125
+ const attempts = options.attempts ?? 10
126
+
127
+ for (let index = 0; index < attempts; index += 1) {
128
+ await settle(options.renderOnce, options.idle)
129
+ const frame = options.captureCharFrame()
130
+
131
+ if (frame.includes(options.includes)) {
132
+ return frame
133
+ }
134
+ }
135
+
136
+ return options.captureCharFrame()
137
+ }
138
+
139
+ describe('confirm card', () => {
140
+ it('renders the task id and both initial actions', async () => {
141
+ const ConfirmCard = await loadConfirmCard()
142
+
143
+ expect(typeof ConfirmCard).toBe('function')
144
+ if (!ConfirmCard) return
145
+
146
+ const dialog = createDialogStub()
147
+ const { renderOnce, captureCharFrame, renderer } = await testRender(
148
+ () => (
149
+ <ConfirmCard
150
+ Dialog={dialog.Dialog}
151
+ task={makeTask('cpl_abc')}
152
+ rpc={{ tasksCancel: async () => ({ cancelled: true }) }}
153
+ onConfirm={() => {}}
154
+ onCancel={() => {}}
155
+ onDismissAfterError={() => {}}
156
+ />
157
+ ),
158
+ { width: 80, height: 20, useThread: false },
159
+ )
160
+
161
+ await renderOnce()
162
+
163
+ const frame = await waitForFrame({
164
+ renderOnce,
165
+ idle: () => renderer.idle(),
166
+ captureCharFrame,
167
+ includes: 'Cancel Copilot delegation cpl_abc?',
168
+ })
169
+
170
+ expect(frame).toContain('Cancel Copilot delegation cpl_abc?')
171
+ expect(frame).toContain('Cancel Task')
172
+ expect(frame).toContain('Keep Running')
173
+ })
174
+
175
+ it('keeps running without calling rpc cancellation', async () => {
176
+ const ConfirmCard = await loadConfirmCard()
177
+
178
+ expect(typeof ConfirmCard).toBe('function')
179
+ if (!ConfirmCard) return
180
+
181
+ const dialog = createDialogStub()
182
+ const cancelledTaskIds: string[] = []
183
+ let confirmed = 0
184
+ let keptRunning = 0
185
+ let dismissedAfterError = 0
186
+
187
+ const { renderOnce, mockInput, renderer } = await testRender(
188
+ () => (
189
+ <ConfirmCard
190
+ Dialog={dialog.Dialog}
191
+ task={makeTask('cpl_keep_running')}
192
+ rpc={{
193
+ tasksCancel: async (taskId) => {
194
+ cancelledTaskIds.push(taskId)
195
+ return { cancelled: true }
196
+ },
197
+ }}
198
+ onConfirm={() => {
199
+ confirmed += 1
200
+ }}
201
+ onCancel={() => {
202
+ keptRunning += 1
203
+ }}
204
+ onDismissAfterError={() => {
205
+ dismissedAfterError += 1
206
+ }}
207
+ />
208
+ ),
209
+ { width: 80, height: 20, useThread: false },
210
+ )
211
+
212
+ await renderOnce()
213
+ await settle(renderOnce, () => renderer.idle())
214
+
215
+ mockInput.pressArrow('right')
216
+ await settle(renderOnce, () => renderer.idle())
217
+ mockInput.pressEnter()
218
+ await settle(renderOnce, () => renderer.idle())
219
+
220
+ expect(cancelledTaskIds).toEqual([])
221
+ expect(confirmed).toBe(0)
222
+ expect(keptRunning).toBe(1)
223
+ expect(dismissedAfterError).toBe(0)
224
+ })
225
+
226
+ it('calls rpc cancellation and closes on success', async () => {
227
+ const ConfirmCard = await loadConfirmCard()
228
+
229
+ expect(typeof ConfirmCard).toBe('function')
230
+ if (!ConfirmCard) return
231
+
232
+ const dialog = createDialogStub()
233
+ const cancelledTaskIds: string[] = []
234
+ let confirmed = 0
235
+
236
+ const { renderOnce, mockInput, renderer } = await testRender(
237
+ () => (
238
+ <ConfirmCard
239
+ Dialog={dialog.Dialog}
240
+ task={makeTask('cpl_cancel_success')}
241
+ rpc={{
242
+ tasksCancel: async (taskId) => {
243
+ cancelledTaskIds.push(taskId)
244
+ return { cancelled: true }
245
+ },
246
+ }}
247
+ onConfirm={() => {
248
+ confirmed += 1
249
+ }}
250
+ onCancel={() => {}}
251
+ onDismissAfterError={() => {}}
252
+ />
253
+ ),
254
+ { width: 80, height: 20, useThread: false },
255
+ )
256
+
257
+ await renderOnce()
258
+ await settle(renderOnce, () => renderer.idle())
259
+
260
+ mockInput.pressEnter()
261
+ await settle(renderOnce, () => renderer.idle())
262
+
263
+ expect(cancelledTaskIds).toEqual(['cpl_cancel_success'])
264
+ expect(confirmed).toBe(1)
265
+ })
266
+
267
+ it('stays open with an inline error and dismiss action when cancellation fails', async () => {
268
+ const ConfirmCard = await loadConfirmCard()
269
+
270
+ expect(typeof ConfirmCard).toBe('function')
271
+ if (!ConfirmCard) return
272
+
273
+ const dialog = createDialogStub()
274
+ const { renderOnce, captureCharFrame, mockInput, renderer } =
275
+ await testRender(
276
+ () => (
277
+ <ConfirmCard
278
+ Dialog={dialog.Dialog}
279
+ task={makeTask('cpl_cancel_error')}
280
+ rpc={{
281
+ tasksCancel: async () => {
282
+ throw new RpcUnreachableError('rpc offline')
283
+ },
284
+ }}
285
+ onConfirm={() => {}}
286
+ onCancel={() => {}}
287
+ onDismissAfterError={() => {}}
288
+ />
289
+ ),
290
+ { width: 80, height: 20, useThread: false },
291
+ )
292
+
293
+ await renderOnce()
294
+ await settle(renderOnce, () => renderer.idle())
295
+
296
+ mockInput.pressEnter()
297
+ const frame = await waitForFrame({
298
+ renderOnce,
299
+ idle: () => renderer.idle(),
300
+ captureCharFrame,
301
+ includes: 'Cancel failed: rpc offline',
302
+ })
303
+
304
+ expect(frame).toContain('Cancel failed: rpc offline')
305
+ expect(frame).toContain('Dismiss')
306
+ expect(frame).not.toContain('Keep Running')
307
+ })
308
+
309
+ it('renders inline server and auth errors without closing', async () => {
310
+ const ConfirmCard = await loadConfirmCard()
311
+
312
+ expect(typeof ConfirmCard).toBe('function')
313
+ if (!ConfirmCard) return
314
+
315
+ const cases = [
316
+ new RpcServerError('server refused cancellation', {
317
+ path: '/tasks/cancel',
318
+ status: 500,
319
+ }),
320
+ new RpcAuthError('token rejected', {
321
+ path: '/tasks/cancel',
322
+ status: 401,
323
+ }),
324
+ ]
325
+
326
+ for (const error of cases) {
327
+ const dialog = createDialogStub()
328
+ let confirmed = 0
329
+
330
+ const { renderOnce, captureCharFrame, mockInput, renderer } =
331
+ await testRender(
332
+ () => (
333
+ <ConfirmCard
334
+ Dialog={dialog.Dialog}
335
+ task={makeTask('cpl_cancel_error')}
336
+ rpc={{
337
+ tasksCancel: async () => {
338
+ throw error
339
+ },
340
+ }}
341
+ onConfirm={() => {
342
+ confirmed += 1
343
+ }}
344
+ onCancel={() => {}}
345
+ onDismissAfterError={() => {}}
346
+ />
347
+ ),
348
+ { width: 80, height: 20, useThread: false },
349
+ )
350
+
351
+ await renderOnce()
352
+ await settle(renderOnce, () => renderer.idle())
353
+ mockInput.pressEnter()
354
+
355
+ const frame = await waitForFrame({
356
+ renderOnce,
357
+ idle: () => renderer.idle(),
358
+ captureCharFrame,
359
+ includes: `Cancel failed: ${error.message}`,
360
+ })
361
+
362
+ expect(confirmed).toBe(0)
363
+ expect(frame).toContain(`Cancel failed: ${error.message}`)
364
+ expect(frame).toContain('Dismiss')
365
+ }
366
+ })
367
+
368
+ it('treats no such task as a stale selection and returns to the list', async () => {
369
+ const ConfirmCard = await loadConfirmCard()
370
+
371
+ expect(typeof ConfirmCard).toBe('function')
372
+ if (!ConfirmCard) return
373
+
374
+ const dialog = createDialogStub()
375
+ let confirmed = 0
376
+ let keptRunning = 0
377
+
378
+ const { renderOnce, captureCharFrame, mockInput, renderer } =
379
+ await testRender(
380
+ () => (
381
+ <ConfirmCard
382
+ Dialog={dialog.Dialog}
383
+ task={makeTask('cpl_finished')}
384
+ rpc={{
385
+ tasksCancel: async () => ({
386
+ cancelled: false,
387
+ error: 'no such task',
388
+ }),
389
+ }}
390
+ onConfirm={() => {
391
+ confirmed += 1
392
+ }}
393
+ onCancel={() => {
394
+ keptRunning += 1
395
+ }}
396
+ onDismissAfterError={() => {}}
397
+ />
398
+ ),
399
+ { width: 80, height: 20, useThread: false },
400
+ )
401
+
402
+ await renderOnce()
403
+ await settle(renderOnce, () => renderer.idle())
404
+
405
+ mockInput.pressEnter()
406
+ await settle(renderOnce, () => renderer.idle())
407
+
408
+ expect(confirmed).toBe(1)
409
+ expect(keptRunning).toBe(0)
410
+ expect(captureCharFrame()).not.toContain('Cancel failed')
411
+ })
412
+
413
+ it('dismisses and returns after an inline cancellation error', async () => {
414
+ const ConfirmCard = await loadConfirmCard()
415
+
416
+ expect(typeof ConfirmCard).toBe('function')
417
+ if (!ConfirmCard) return
418
+
419
+ const dialog = createDialogStub()
420
+ let dismissedAfterError = 0
421
+
422
+ const { renderOnce, mockInput, renderer } = await testRender(
423
+ () => (
424
+ <ConfirmCard
425
+ Dialog={dialog.Dialog}
426
+ task={makeTask('cpl_dismiss_after_error')}
427
+ rpc={{
428
+ tasksCancel: async () => {
429
+ throw new RpcUnreachableError('rpc offline')
430
+ },
431
+ }}
432
+ onConfirm={() => {}}
433
+ onCancel={() => {}}
434
+ onDismissAfterError={() => {
435
+ dismissedAfterError += 1
436
+ }}
437
+ />
438
+ ),
439
+ { width: 80, height: 20, useThread: false },
440
+ )
441
+
442
+ await renderOnce()
443
+ await settle(renderOnce, () => renderer.idle())
444
+
445
+ mockInput.pressEnter()
446
+ await settle(renderOnce, () => renderer.idle())
447
+ mockInput.pressEscape()
448
+ await new Promise((resolve) => setTimeout(resolve, 30))
449
+ await settle(renderOnce, () => renderer.idle())
450
+
451
+ expect(dismissedAfterError).toBe(1)
452
+ })
453
+
454
+ it('ignores repeated confirm activations while cancellation is in flight', async () => {
455
+ const ConfirmCard = await loadConfirmCard()
456
+
457
+ expect(typeof ConfirmCard).toBe('function')
458
+ if (!ConfirmCard) return
459
+
460
+ const dialog = createDialogStub()
461
+ const deferred = createDeferred<{ cancelled: boolean }>()
462
+ const cancelledTaskIds: string[] = []
463
+ let confirmed = 0
464
+
465
+ const { renderOnce, mockInput, renderer } = await testRender(
466
+ () => (
467
+ <ConfirmCard
468
+ Dialog={dialog.Dialog}
469
+ task={makeTask('cpl_guard')}
470
+ rpc={{
471
+ tasksCancel: async (taskId) => {
472
+ cancelledTaskIds.push(taskId)
473
+ return deferred.promise
474
+ },
475
+ }}
476
+ onConfirm={() => {
477
+ confirmed += 1
478
+ }}
479
+ onCancel={() => {}}
480
+ onDismissAfterError={() => {}}
481
+ />
482
+ ),
483
+ { width: 80, height: 20, useThread: false },
484
+ )
485
+
486
+ await renderOnce()
487
+ await settle(renderOnce, () => renderer.idle())
488
+
489
+ mockInput.pressEnter()
490
+ mockInput.pressEnter()
491
+ await settle(renderOnce, () => renderer.idle())
492
+
493
+ expect(cancelledTaskIds).toEqual(['cpl_guard'])
494
+
495
+ deferred.resolve({ cancelled: true })
496
+ await settle(renderOnce, () => renderer.idle())
497
+
498
+ expect(confirmed).toBe(1)
499
+ })
500
+ })