-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtui.ts
More file actions
178 lines (156 loc) · 4.63 KB
/
Copy pathtui.ts
File metadata and controls
178 lines (156 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import type {
TuiPlugin,
TuiPluginApi,
TuiPluginModule,
} from "@opencode-ai/plugin/tui"
import type { JSX } from "@opentui/solid"
import clipboardy from "clipboardy"
import {
createLoopActionRunner,
createLoopDialogActions,
type LoopDialogAction,
} from "./tui-dialog-actions.js"
import {
LoopFeedbackDialog,
type LoopFeedbackDialogProps,
} from "./tui-dialog-view.js"
import {
LOOP_COPY_TITLE,
createLoopFeedbackModel,
isLoopFeedbackToast,
type LoopFeedbackInput,
} from "./tui-feedback-model.js"
export interface LoopDialogRenderInput extends LoopFeedbackDialogProps {
api: TuiPluginApi
}
export interface LoopTuiDependencies {
writeClipboard(text: string): Promise<void>
renderDialog?(input: LoopDialogRenderInput): JSX.Element
}
const defaultDependencies: Required<LoopTuiDependencies> = {
writeClipboard: (text) => clipboardy.write(text),
renderDialog(input) {
return LoopFeedbackDialog({
message: input.message,
variant: input.variant,
actions: input.actions,
theme: input.theme,
onActivate: input.onActivate,
onClose: input.onClose,
})
},
}
export function createLoopTuiPlugin(
input: LoopTuiDependencies = defaultDependencies
): TuiPlugin {
const dependencies: Required<LoopTuiDependencies> = {
...defaultDependencies,
...input,
}
return async (api) => {
let latestGeneration = 0
let ownedGeneration: number | undefined
const finishGeneration = (generation: number) => {
if (ownedGeneration !== generation) return
ownedGeneration = undefined
}
const closeGeneration = (generation: number) => {
if (ownedGeneration !== generation) return
try {
api.ui.dialog.clear()
} finally {
finishGeneration(generation)
}
}
const close = () => {
const generation = ownedGeneration
if (generation !== undefined) closeGeneration(generation)
}
const logDialogFailure = (error: unknown) => {
const message = error instanceof Error ? error.message : String(error)
try {
void api.client.app
.log({
service: "opencode-plugin-loop",
level: "warn",
message: "failed to open interactive Loop dialog",
extra: { error: message },
})
.catch(() => {})
} catch {
// Diagnostics must never interrupt the server-side toast fallback.
}
}
const notifySuccess = (description: string) => {
api.ui.toast({
title: LOOP_COPY_TITLE,
message: `${description} copied to clipboard.`,
variant: "success",
duration: 2500,
})
}
const notifyFailure = () => {
api.ui.toast({
title: LOOP_COPY_TITLE,
message: "Could not copy to the system clipboard.",
variant: "error",
duration: 4000,
})
}
const openFeedback = (feedback: LoopFeedbackInput) => {
const generation = ++latestGeneration
try {
const model = createLoopFeedbackModel(feedback)
if (ownedGeneration !== undefined) closeGeneration(ownedGeneration)
const actions = createLoopDialogActions(model.taskIds)
const runner = createLoopActionRunner({
writeClipboard: dependencies.writeClipboard,
notifySuccess,
notifyFailure,
closeIfCurrent: () => closeGeneration(generation),
})
ownedGeneration = generation
api.ui.dialog.setSize("medium")
api.ui.dialog.replace(
() =>
dependencies.renderDialog({
api,
message: model.message,
variant: model.variant,
actions,
theme: api.theme.current,
onActivate(action: LoopDialogAction) {
void runner.run(action, model.message)
},
onClose: () => closeGeneration(generation),
}),
() => finishGeneration(generation)
)
} catch (error) {
if (ownedGeneration === generation) {
try {
api.ui.dialog.clear()
} catch {
// Continue with local state cleanup and structured diagnostics.
}
finishGeneration(generation)
}
logDialogFailure(error)
}
}
const unsubscribe = api.event.on("tui.toast.show", (event) => {
if (!isLoopFeedbackToast(event)) return
openFeedback(event.properties)
})
api.lifecycle.onDispose(() => {
unsubscribe()
close()
})
}
}
export const LoopTuiPlugin = createLoopTuiPlugin()
const module: TuiPluginModule = {
id: "opencode-plugin-loop-tui",
tui: LoopTuiPlugin,
}
export default module