opencode-copilot-delegate 0.9.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,276 @@
1
+ /** @jsxImportSource @opentui/solid */
2
+
3
+ import type { TuiPluginApi } from '@opencode-ai/plugin/tui'
4
+ import {
5
+ type KeyEvent,
6
+ TextAttributes,
7
+ type TextRenderable,
8
+ } from '@opentui/core'
9
+ import { useRenderer } from '@opentui/solid'
10
+ import { onCleanup } from 'solid-js'
11
+ import type { RpcClient } from '../rpc-client'
12
+ import type { ModalListTask } from './row'
13
+
14
+ type ConfirmCardProps = {
15
+ Dialog: TuiPluginApi['ui']['Dialog']
16
+ task: ModalListTask
17
+ rpc: Pick<RpcClient, 'tasksCancel'>
18
+ onConfirm: () => void
19
+ onCancel: () => void
20
+ onDismissAfterError: () => void
21
+ }
22
+
23
+ type ConfirmState =
24
+ | {
25
+ kind: 'confirm'
26
+ focusedAction: 'confirm' | 'cancel'
27
+ busy: boolean
28
+ }
29
+ | {
30
+ kind: 'error'
31
+ message: string
32
+ }
33
+
34
+ function errorMessage(error: unknown): string {
35
+ if (error instanceof Error && error.message.trim().length > 0) {
36
+ return error.message.trim()
37
+ }
38
+
39
+ return 'Unknown RPC error.'
40
+ }
41
+
42
+ function matchesKey(key: KeyEvent, expected: string): boolean {
43
+ return (
44
+ key.name === expected || key.sequence === expected || key.raw === expected
45
+ )
46
+ }
47
+
48
+ function isActivateKey(key: KeyEvent): boolean {
49
+ return (
50
+ matchesKey(key, 'return') ||
51
+ matchesKey(key, 'enter') ||
52
+ matchesKey(key, 'space') ||
53
+ key.sequence === ' ' ||
54
+ key.raw === ' '
55
+ )
56
+ }
57
+
58
+ function isLeftKey(key: KeyEvent): boolean {
59
+ return matchesKey(key, 'left')
60
+ }
61
+
62
+ function isRightKey(key: KeyEvent): boolean {
63
+ return matchesKey(key, 'right')
64
+ }
65
+
66
+ function updateText(
67
+ node: TextRenderable | undefined,
68
+ content: string,
69
+ attributes = TextAttributes.NONE,
70
+ ) {
71
+ if (!node) {
72
+ return
73
+ }
74
+
75
+ node.content = content
76
+ node.attributes = attributes
77
+ node.visible = content.length > 0
78
+ }
79
+
80
+ export function ConfirmCard(props: ConfirmCardProps) {
81
+ const Dialog = props.Dialog
82
+ const renderer = useRenderer()
83
+ const title = `Cancel Copilot delegation ${props.task.taskId}?`
84
+ const state: { value: ConfirmState } = {
85
+ value: {
86
+ kind: 'confirm',
87
+ focusedAction: 'confirm',
88
+ busy: false,
89
+ },
90
+ }
91
+
92
+ let confirmText: TextRenderable | undefined
93
+ let cancelText: TextRenderable | undefined
94
+ let errorText: TextRenderable | undefined
95
+ let dismissText: TextRenderable | undefined
96
+
97
+ const renderView = () => {
98
+ const currentState = state.value
99
+
100
+ if (currentState.kind === 'error') {
101
+ updateText(confirmText, '')
102
+ updateText(cancelText, '')
103
+ updateText(errorText, `Cancel failed: ${currentState.message}`)
104
+ updateText(dismissText, 'Dismiss', TextAttributes.INVERSE)
105
+ renderer.requestRender()
106
+ return
107
+ }
108
+
109
+ updateText(
110
+ confirmText,
111
+ 'Cancel Task',
112
+ currentState.focusedAction === 'confirm'
113
+ ? TextAttributes.INVERSE
114
+ : TextAttributes.NONE,
115
+ )
116
+ updateText(
117
+ cancelText,
118
+ 'Keep Running',
119
+ currentState.focusedAction === 'cancel'
120
+ ? TextAttributes.INVERSE
121
+ : TextAttributes.NONE,
122
+ )
123
+ updateText(errorText, '')
124
+ updateText(dismissText, '')
125
+ renderer.requestRender()
126
+ }
127
+
128
+ const closeToList = () => {
129
+ if (state.value.kind === 'error') {
130
+ props.onDismissAfterError()
131
+ return
132
+ }
133
+
134
+ if (state.value.busy) {
135
+ return
136
+ }
137
+
138
+ props.onCancel()
139
+ }
140
+
141
+ const confirmCancel = async () => {
142
+ if (state.value.kind !== 'confirm' || state.value.busy) {
143
+ return
144
+ }
145
+
146
+ state.value = { ...state.value, busy: true }
147
+
148
+ try {
149
+ const response = await props.rpc.tasksCancel(props.task.taskId)
150
+
151
+ if (response.cancelled) {
152
+ props.onConfirm()
153
+ return
154
+ }
155
+
156
+ if (response.error === 'no such task') {
157
+ props.onConfirm()
158
+ return
159
+ }
160
+
161
+ state.value = {
162
+ kind: 'error',
163
+ message: response.error ?? 'Unknown RPC error.',
164
+ }
165
+ } catch (error) {
166
+ state.value = { kind: 'error', message: errorMessage(error) }
167
+ }
168
+
169
+ renderView()
170
+ }
171
+
172
+ const activateFocusedAction = () => {
173
+ if (state.value.kind === 'error') {
174
+ props.onDismissAfterError()
175
+ return
176
+ }
177
+
178
+ if (state.value.focusedAction === 'confirm') {
179
+ void confirmCancel()
180
+ return
181
+ }
182
+
183
+ props.onCancel()
184
+ }
185
+
186
+ const focusConfirm = () => {
187
+ if (state.value.kind !== 'confirm' || state.value.busy) {
188
+ return
189
+ }
190
+
191
+ state.value = { ...state.value, focusedAction: 'confirm' }
192
+ renderView()
193
+ }
194
+
195
+ const focusCancel = () => {
196
+ if (state.value.kind !== 'confirm' || state.value.busy) {
197
+ return
198
+ }
199
+
200
+ state.value = { ...state.value, focusedAction: 'cancel' }
201
+ renderView()
202
+ }
203
+
204
+ const handleKeyPress = (key: KeyEvent) => {
205
+ if (matchesKey(key, 'escape')) {
206
+ key.preventDefault()
207
+ closeToList()
208
+ return
209
+ }
210
+
211
+ if (state.value.kind === 'confirm' && !state.value.busy) {
212
+ if (isLeftKey(key)) {
213
+ key.preventDefault()
214
+ focusConfirm()
215
+ return
216
+ }
217
+
218
+ if (isRightKey(key)) {
219
+ key.preventDefault()
220
+ focusCancel()
221
+ return
222
+ }
223
+ }
224
+
225
+ if (isActivateKey(key)) {
226
+ key.preventDefault()
227
+ activateFocusedAction()
228
+ }
229
+ }
230
+
231
+ renderer.keyInput.on('keypress', handleKeyPress)
232
+
233
+ onCleanup(() => {
234
+ renderer.keyInput.off('keypress', handleKeyPress)
235
+ })
236
+
237
+ return (
238
+ <Dialog onClose={closeToList} size="medium">
239
+ <box flexDirection="column" gap={1} padding={1}>
240
+ <text>{title}</text>
241
+ {/* biome-ignore lint/a11y/noStaticElementInteractions: OpenTUI text nodes handle terminal mouse events directly. */}
242
+ <text
243
+ ref={confirmText}
244
+ attributes={TextAttributes.INVERSE}
245
+ onMouseUp={() => {
246
+ focusConfirm()
247
+ void confirmCancel()
248
+ }}
249
+ >
250
+ Cancel Task
251
+ </text>
252
+ {/* biome-ignore lint/a11y/noStaticElementInteractions: OpenTUI text nodes handle terminal mouse events directly. */}
253
+ <text
254
+ ref={cancelText}
255
+ onMouseUp={() => {
256
+ focusCancel()
257
+ closeToList()
258
+ }}
259
+ >
260
+ Keep Running
261
+ </text>
262
+ <text ref={errorText} visible={false} />
263
+ {/* biome-ignore lint/a11y/noStaticElementInteractions: OpenTUI text nodes handle terminal mouse events directly. */}
264
+ <text
265
+ ref={dismissText}
266
+ visible={false}
267
+ onMouseUp={() => {
268
+ props.onDismissAfterError()
269
+ }}
270
+ />
271
+ </box>
272
+ </Dialog>
273
+ )
274
+ }
275
+
276
+ export default ConfirmCard
@@ -0,0 +1,389 @@
1
+ /** @jsxImportSource @opentui/solid */
2
+
3
+ import type { TuiPluginApi } from '@opencode-ai/plugin/tui'
4
+ import {
5
+ type BoxRenderable,
6
+ type KeyEvent,
7
+ TextAttributes,
8
+ type TextRenderable,
9
+ } from '@opentui/core'
10
+ import { useRenderer } from '@opentui/solid'
11
+ import { onCleanup } from 'solid-js'
12
+ import type { RpcClient } from '../rpc-client'
13
+ import { formatRowText, type ModalListTask } from './row'
14
+
15
+ const LOADING_HEADER = 'Loading delegations…'
16
+ const EMPTY_STATE_TITLE = 'No Copilot delegations are running.'
17
+ const EMPTY_STATE_HINT =
18
+ 'Start one with the copilot_delegate tool, then reopen /copilot-status.'
19
+ const ERROR_STATE_TITLE = 'The Copilot delegate TUI plugin is not responding.'
20
+ const ERROR_STATE_HINT =
21
+ 'Try reloading the plugin (or run /copilot-status again).'
22
+
23
+ export type ModalListClock = {
24
+ now(): number
25
+ setInterval(fn: () => void, delayMs: number): unknown
26
+ clearInterval(handle: unknown): void
27
+ }
28
+
29
+ type ModalListProps = {
30
+ Dialog: TuiPluginApi['ui']['Dialog']
31
+ rpc: Pick<RpcClient, 'tasksList'>
32
+ onClose: () => void
33
+ onCancelTask?: (task: ModalListTask) => void
34
+ initialTaskId?: string
35
+ clock?: ModalListClock
36
+ }
37
+
38
+ type ViewState =
39
+ | { kind: 'loading' }
40
+ | { kind: 'ready'; tasks: ModalListTask[] }
41
+ | { kind: 'error'; message: string }
42
+
43
+ const defaultClock: ModalListClock = {
44
+ now: () => Date.now(),
45
+ setInterval: (fn, delayMs) => globalThis.setInterval(fn, delayMs),
46
+ clearInterval: (handle) => {
47
+ globalThis.clearInterval(handle as ReturnType<typeof setInterval>)
48
+ },
49
+ }
50
+
51
+ function errorMessage(error: unknown): string {
52
+ if (error instanceof Error && error.message.trim().length > 0) {
53
+ return error.message.trim()
54
+ }
55
+
56
+ return 'Unknown RPC error.'
57
+ }
58
+
59
+ function updateText(
60
+ node: TextRenderable | undefined,
61
+ content: string,
62
+ attributes = TextAttributes.NONE,
63
+ ) {
64
+ if (!node) {
65
+ return
66
+ }
67
+
68
+ node.content = content
69
+ node.attributes = attributes
70
+ }
71
+
72
+ function updateBoxVisibility(
73
+ node: BoxRenderable | undefined,
74
+ visible: boolean,
75
+ ) {
76
+ if (!node) {
77
+ return
78
+ }
79
+
80
+ node.visible = visible
81
+ }
82
+
83
+ function matchesKey(key: KeyEvent, expected: string): boolean {
84
+ return (
85
+ key.name === expected || key.sequence === expected || key.raw === expected
86
+ )
87
+ }
88
+
89
+ type ModalRefs = {
90
+ headerText?: TextRenderable
91
+ footerText?: TextRenderable
92
+ emptyBox?: BoxRenderable
93
+ emptyLineOne?: TextRenderable
94
+ emptyLineTwo?: TextRenderable
95
+ errorBox?: BoxRenderable
96
+ errorLineOne?: TextRenderable
97
+ errorLineTwo?: TextRenderable
98
+ errorLineThree?: TextRenderable
99
+ rowsBox?: BoxRenderable
100
+ rowTexts: Array<TextRenderable | undefined>
101
+ }
102
+
103
+ function renderLoadingState(refs: ModalRefs) {
104
+ updateText(refs.headerText, LOADING_HEADER)
105
+ updateText(refs.footerText, 'Esc close')
106
+ updateBoxVisibility(refs.emptyBox, false)
107
+ updateBoxVisibility(refs.errorBox, false)
108
+ updateBoxVisibility(refs.rowsBox, false)
109
+ }
110
+
111
+ function renderErrorState(refs: ModalRefs, message: string) {
112
+ updateText(refs.headerText, 'Status unavailable.')
113
+ updateText(refs.footerText, 'Esc close')
114
+ updateBoxVisibility(refs.emptyBox, false)
115
+ updateBoxVisibility(refs.errorBox, true)
116
+ updateBoxVisibility(refs.rowsBox, false)
117
+ updateText(refs.errorLineOne, ERROR_STATE_TITLE)
118
+ updateText(refs.errorLineTwo, message)
119
+ updateText(refs.errorLineThree, ERROR_STATE_HINT)
120
+ }
121
+
122
+ function renderEmptyView(refs: ModalRefs) {
123
+ updateText(refs.headerText, '0 running · 0 recent')
124
+ updateText(refs.footerText, 'Esc close')
125
+ updateBoxVisibility(refs.errorBox, false)
126
+ updateBoxVisibility(refs.emptyBox, true)
127
+ updateBoxVisibility(refs.rowsBox, false)
128
+ updateText(refs.emptyLineOne, EMPTY_STATE_TITLE)
129
+ updateText(refs.emptyLineTwo, EMPTY_STATE_HINT)
130
+ }
131
+
132
+ function renderTaskRows(
133
+ refs: ModalRefs,
134
+ tasks: ModalListTask[],
135
+ focusedIndex: number,
136
+ nowMs: number,
137
+ ) {
138
+ updateText(refs.headerText, `${tasks.length} running · 0 recent`)
139
+ updateText(refs.footerText, '↑↓ navigate · c cancel · Esc close')
140
+ updateBoxVisibility(refs.errorBox, false)
141
+ updateBoxVisibility(refs.emptyBox, false)
142
+ updateBoxVisibility(refs.rowsBox, true)
143
+
144
+ for (const [index, rowText] of refs.rowTexts.entries()) {
145
+ const task = tasks[index]
146
+
147
+ if (!rowText) {
148
+ continue
149
+ }
150
+
151
+ rowText.visible = task !== undefined
152
+ rowText.content = task ? formatRowText(task, nowMs) : ''
153
+ rowText.attributes =
154
+ task && index === focusedIndex
155
+ ? TextAttributes.INVERSE
156
+ : TextAttributes.NONE
157
+ }
158
+ }
159
+
160
+ function isReadyState(
161
+ state: ViewState,
162
+ ): state is Extract<ViewState, { kind: 'ready' }> {
163
+ return state.kind === 'ready'
164
+ }
165
+
166
+ function previousIndex(current: number, total: number): number {
167
+ return current === 0 ? total - 1 : current - 1
168
+ }
169
+
170
+ function nextIndex(current: number, total: number): number {
171
+ return current === total - 1 ? 0 : current + 1
172
+ }
173
+
174
+ function isUpKey(key: KeyEvent): boolean {
175
+ return matchesKey(key, 'up') || matchesKey(key, 'k')
176
+ }
177
+
178
+ function isDownKey(key: KeyEvent): boolean {
179
+ return matchesKey(key, 'down') || matchesKey(key, 'j')
180
+ }
181
+
182
+ export function ModalList(props: ModalListProps) {
183
+ const Dialog = props.Dialog
184
+ const renderer = useRenderer()
185
+ const clock = props.clock ?? defaultClock
186
+ const now = { value: clock.now() }
187
+ const state: { value: ViewState } = { value: { kind: 'loading' } }
188
+ const focusedIndex = { value: 0 }
189
+ let disposed = false
190
+
191
+ let headerText: TextRenderable | undefined
192
+ let footerText: TextRenderable | undefined
193
+ let emptyBox: BoxRenderable | undefined
194
+ let emptyLineOne: TextRenderable | undefined
195
+ let emptyLineTwo: TextRenderable | undefined
196
+ let errorBox: BoxRenderable | undefined
197
+ let errorLineOne: TextRenderable | undefined
198
+ let errorLineTwo: TextRenderable | undefined
199
+ let errorLineThree: TextRenderable | undefined
200
+ let rowsBox: BoxRenderable | undefined
201
+ const rowTexts: Array<TextRenderable | undefined> = []
202
+
203
+ const refs = (): ModalRefs => ({
204
+ headerText,
205
+ footerText,
206
+ emptyBox,
207
+ emptyLineOne,
208
+ emptyLineTwo,
209
+ errorBox,
210
+ errorLineOne,
211
+ errorLineTwo,
212
+ errorLineThree,
213
+ rowsBox,
214
+ rowTexts,
215
+ })
216
+
217
+ const renderView = () => {
218
+ if (disposed) {
219
+ return
220
+ }
221
+
222
+ const currentState = state.value
223
+ const currentRefs = refs()
224
+
225
+ if (currentState.kind === 'loading') {
226
+ renderLoadingState(currentRefs)
227
+ renderer.requestRender()
228
+ return
229
+ }
230
+
231
+ if (currentState.kind === 'error') {
232
+ renderErrorState(currentRefs, currentState.message)
233
+ renderer.requestRender()
234
+ return
235
+ }
236
+
237
+ const tasks = currentState.tasks
238
+
239
+ if (tasks.length === 0) {
240
+ renderEmptyView(currentRefs)
241
+ renderer.requestRender()
242
+ return
243
+ }
244
+
245
+ renderTaskRows(currentRefs, tasks, focusedIndex.value, now.value)
246
+
247
+ renderer.requestRender()
248
+ }
249
+
250
+ const loadTasks = async () => {
251
+ try {
252
+ const response = await props.rpc.tasksList()
253
+
254
+ if (disposed) {
255
+ return
256
+ }
257
+
258
+ const initialIndex = props.initialTaskId
259
+ ? response.tasks.findIndex(
260
+ (task) => task.taskId === props.initialTaskId,
261
+ )
262
+ : -1
263
+
264
+ if (initialIndex >= 0) {
265
+ focusedIndex.value = initialIndex
266
+ } else if (response.tasks.length === 0) {
267
+ focusedIndex.value = 0
268
+ } else {
269
+ focusedIndex.value = Math.min(
270
+ focusedIndex.value,
271
+ response.tasks.length - 1,
272
+ )
273
+ }
274
+
275
+ state.value = { kind: 'ready', tasks: response.tasks }
276
+ } catch (error) {
277
+ if (disposed) {
278
+ return
279
+ }
280
+
281
+ state.value = { kind: 'error', message: errorMessage(error) }
282
+ }
283
+
284
+ renderView()
285
+ }
286
+
287
+ const intervalHandle = clock.setInterval(() => {
288
+ now.value = clock.now()
289
+
290
+ if (state.value.kind === 'ready' && state.value.tasks.length > 0) {
291
+ renderView()
292
+ }
293
+ }, 1_000)
294
+
295
+ void loadTasks()
296
+
297
+ const handleKeyPress = (key: KeyEvent) => {
298
+ if (matchesKey(key, 'escape')) {
299
+ key.preventDefault()
300
+ props.onClose()
301
+ return
302
+ }
303
+
304
+ if (!isReadyState(state.value) || state.value.tasks.length === 0) {
305
+ return
306
+ }
307
+
308
+ if (isUpKey(key)) {
309
+ key.preventDefault()
310
+ focusedIndex.value = previousIndex(
311
+ focusedIndex.value,
312
+ state.value.tasks.length,
313
+ )
314
+ renderView()
315
+ return
316
+ }
317
+
318
+ if (isDownKey(key)) {
319
+ key.preventDefault()
320
+ focusedIndex.value = nextIndex(
321
+ focusedIndex.value,
322
+ state.value.tasks.length,
323
+ )
324
+ renderView()
325
+ return
326
+ }
327
+
328
+ if (matchesKey(key, 'c')) {
329
+ key.preventDefault()
330
+ props.onCancelTask?.(state.value.tasks[focusedIndex.value])
331
+ }
332
+ }
333
+
334
+ renderer.keyInput.on('keypress', handleKeyPress)
335
+
336
+ onCleanup(() => {
337
+ disposed = true
338
+ renderer.keyInput.off('keypress', handleKeyPress)
339
+ clock.clearInterval(intervalHandle)
340
+ })
341
+
342
+ return (
343
+ <Dialog onClose={props.onClose} size="medium">
344
+ <box flexDirection="column" gap={1} padding={1}>
345
+ <text ref={headerText}>Loading delegations…</text>
346
+
347
+ <box
348
+ ref={emptyBox}
349
+ flexDirection="column"
350
+ flexGrow={1}
351
+ justifyContent="center"
352
+ visible={false}
353
+ >
354
+ <text ref={emptyLineOne} />
355
+ <text ref={emptyLineTwo} />
356
+ </box>
357
+
358
+ <box
359
+ ref={errorBox}
360
+ flexDirection="column"
361
+ flexGrow={1}
362
+ justifyContent="center"
363
+ visible={false}
364
+ >
365
+ <text ref={errorLineOne} />
366
+ <text ref={errorLineTwo} />
367
+ <text ref={errorLineThree} />
368
+ </box>
369
+
370
+ <box ref={rowsBox} flexDirection="column" flexGrow={1} visible={false}>
371
+ <text ref={(node) => (rowTexts[0] = node)} />
372
+ <text ref={(node) => (rowTexts[1] = node)} />
373
+ <text ref={(node) => (rowTexts[2] = node)} />
374
+ <text ref={(node) => (rowTexts[3] = node)} />
375
+ <text ref={(node) => (rowTexts[4] = node)} />
376
+ <text ref={(node) => (rowTexts[5] = node)} />
377
+ <text ref={(node) => (rowTexts[6] = node)} />
378
+ <text ref={(node) => (rowTexts[7] = node)} />
379
+ <text ref={(node) => (rowTexts[8] = node)} />
380
+ <text ref={(node) => (rowTexts[9] = node)} />
381
+ </box>
382
+
383
+ <text ref={footerText}>Esc close</text>
384
+ </box>
385
+ </Dialog>
386
+ )
387
+ }
388
+
389
+ export default ModalList
@@ -0,0 +1,41 @@
1
+ import type { TasksListResponse } from '../../runtime/rpc-contract'
2
+
3
+ export type ModalListTask = TasksListResponse['tasks'][number]
4
+
5
+ export function displayValue(value: string): string {
6
+ const trimmed = value.trim()
7
+
8
+ return trimmed.length > 0 ? trimmed : 'default'
9
+ }
10
+
11
+ export function formatElapsed(elapsedMs: number): string {
12
+ const totalSeconds = Math.max(0, Math.floor(elapsedMs / 1_000))
13
+
14
+ if (totalSeconds >= 3_600) {
15
+ const hours = Math.floor(totalSeconds / 3_600)
16
+ const minutes = Math.floor((totalSeconds % 3_600) / 60)
17
+ return `${hours}h ${minutes}m`
18
+ }
19
+
20
+ if (totalSeconds >= 60) {
21
+ const minutes = Math.floor(totalSeconds / 60)
22
+ const seconds = totalSeconds % 60
23
+ return `${minutes}m ${seconds}s`
24
+ }
25
+
26
+ return `${totalSeconds}s`
27
+ }
28
+
29
+ function formatStatus(status: ModalListTask['status']): string {
30
+ return status === 'running' ? `● ${status}` : status
31
+ }
32
+
33
+ export function formatToolCalls(toolCallCount: number): string {
34
+ return `${toolCallCount} ${toolCallCount === 1 ? 'call' : 'calls'}`
35
+ }
36
+
37
+ export function formatRowText(task: ModalListTask, nowMs: number): string {
38
+ const elapsed = Math.max(0, nowMs - task.startedAt)
39
+
40
+ return `${formatStatus(task.status)} ${task.taskId} ${displayValue(task.agent)} ${displayValue(task.model)} ${formatElapsed(elapsed)} ${formatToolCalls(task.toolCallCount)}`
41
+ }