forked from anomalyco/opencode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtui-plugin.ts
More file actions
350 lines (342 loc) · 8.98 KB
/
tui-plugin.ts
File metadata and controls
350 lines (342 loc) · 8.98 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import { createOpencodeClient } from "@opencode-ai/sdk/v2"
import { RGBA, type CliRenderer } from "@opentui/core"
import type { HostPluginApi } from "../../src/cli/cmd/tui/plugin/slots"
import { createTuiResolvedConfig } from "./tui-runtime"
type Count = {
event_add: number
event_drop: number
route_add: number
route_drop: number
command_add: number
command_drop: number
}
type AttentionOpts = Partial<Omit<HostPluginApi["attention"], "soundboard">> & {
soundboard?: Partial<HostPluginApi["attention"]["soundboard"]>
}
function themeCurrent(): HostPluginApi["theme"]["current"] {
const a = RGBA.fromInts(0, 120, 240)
const b = RGBA.fromInts(120, 120, 120)
const c = RGBA.fromInts(230, 230, 230)
const d = RGBA.fromInts(120, 30, 30)
const e = RGBA.fromInts(140, 100, 40)
const f = RGBA.fromInts(20, 140, 80)
const g = RGBA.fromInts(20, 80, 160)
const h = RGBA.fromInts(40, 40, 40)
const i = RGBA.fromInts(60, 60, 60)
const j = RGBA.fromInts(80, 80, 80)
return {
primary: a,
secondary: b,
accent: a,
error: d,
warning: e,
success: f,
info: g,
text: c,
textMuted: b,
selectedListItemText: h,
background: h,
backgroundPanel: h,
backgroundElement: i,
backgroundMenu: i,
border: j,
borderActive: c,
borderSubtle: i,
diffAdded: f,
diffRemoved: d,
diffContext: b,
diffHunkHeader: b,
diffHighlightAdded: f,
diffHighlightRemoved: d,
diffAddedBg: h,
diffRemovedBg: h,
diffContextBg: h,
diffLineNumber: b,
diffAddedLineNumberBg: h,
diffRemovedLineNumberBg: h,
markdownText: c,
markdownHeading: c,
markdownLink: a,
markdownLinkText: g,
markdownCode: f,
markdownBlockQuote: e,
markdownEmph: e,
markdownStrong: c,
markdownHorizontalRule: b,
markdownListItem: a,
markdownListEnumeration: g,
markdownImage: a,
markdownImageText: g,
markdownCodeBlock: c,
syntaxComment: b,
syntaxKeyword: a,
syntaxFunction: g,
syntaxVariable: c,
syntaxString: f,
syntaxNumber: e,
syntaxType: a,
syntaxOperator: a,
syntaxPunctuation: c,
thinkingOpacity: 0.6,
}
}
type Opts = {
client?: HostPluginApi["client"] | (() => HostPluginApi["client"])
renderer?: HostPluginApi["renderer"]
attention?: AttentionOpts
event?: HostPluginApi["event"]
count?: Count
keymap?: HostPluginApi["keymap"]
tuiConfig?: Partial<HostPluginApi["tuiConfig"]>
app?: Partial<HostPluginApi["app"]>
state?: {
ready?: HostPluginApi["state"]["ready"]
config?: HostPluginApi["state"]["config"]
provider?: HostPluginApi["state"]["provider"]
path?: HostPluginApi["state"]["path"]
vcs?: HostPluginApi["state"]["vcs"]
session?: Partial<HostPluginApi["state"]["session"]>
part?: HostPluginApi["state"]["part"]
lsp?: HostPluginApi["state"]["lsp"]
mcp?: HostPluginApi["state"]["mcp"]
}
theme?: {
selected?: string
has?: HostPluginApi["theme"]["has"]
set?: HostPluginApi["theme"]["set"]
install?: HostPluginApi["theme"]["install"]
mode?: HostPluginApi["theme"]["mode"]
ready?: boolean
current?: HostPluginApi["theme"]["current"]
}
}
function tuiConfig(input?: Partial<HostPluginApi["tuiConfig"]>): HostPluginApi["tuiConfig"] {
return {
...createTuiResolvedConfig(),
...input,
}
}
export function createTuiPluginApi(opts: Opts = {}): HostPluginApi {
const kv: Record<string, unknown> = {}
const count = opts.count
const ctrl = new AbortController()
const own = createOpencodeClient({
baseUrl: "http://localhost:4096",
})
const fallback = () => own
const read =
typeof opts.client === "function"
? opts.client
: opts.client
? () => opts.client as HostPluginApi["client"]
: fallback
const client = () => read()
let depth = 0
let size: "medium" | "large" | "xlarge" = "medium"
const has = opts.theme?.has ?? (() => false)
let selected = opts.theme?.selected ?? "opencode"
const set =
opts.theme?.set ??
((name: string) => {
if (!has(name)) return false
selected = name
return true
})
const renderer: CliRenderer = opts.renderer ?? {
...Object.create(null),
once(this: CliRenderer) {
return this
},
}
const keymap =
opts.keymap ??
({
acquireResource(_key: symbol, setup: () => () => void) {
const dispose = setup()
return () => {
dispose()
}
},
registerLayer() {
if (count) count.command_add += 1
return () => {
if (!count) return
count.command_drop += 1
}
},
runCommand() {
return { ok: true } as const
},
} as unknown as HostPluginApi["keymap"])
function kvGet(name: string): unknown
function kvGet<Value>(name: string, fallback: Value): Value
function kvGet(name: string, fallback?: unknown) {
const value = kv[name]
if (value === undefined) return fallback
return value
}
return {
app: {
get version() {
return opts.app?.version ?? "0.0.0-test"
},
},
attention: {
async notify(input) {
return opts.attention?.notify?.(input) ?? { ok: false, notification: false, sound: false }
},
soundboard: {
registerPack: (pack) => opts.attention?.soundboard?.registerPack?.(pack) ?? (() => {}),
activate: (id, options) => opts.attention?.soundboard?.activate?.(id, options) ?? false,
current: () => opts.attention?.soundboard?.current?.() ?? "opencode.default",
list: () => opts.attention?.soundboard?.list?.() ?? [],
},
},
keys: {
formatSequence: () => "",
formatBindings: () => undefined,
},
get client() {
return client()
},
event: opts.event ?? {
on: () => {
if (count) count.event_add += 1
return () => {
if (!count) return
count.event_drop += 1
}
},
},
renderer,
slots: {
register: () => "fixture-slot",
},
plugins: {
list: () => [],
activate: async () => false,
deactivate: async () => false,
add: async () => false,
install: async () => ({
ok: false,
message: "not implemented in fixture",
}),
},
lifecycle: {
signal: ctrl.signal,
onDispose() {
return () => {}
},
},
keymap,
route: {
register: () => {
if (count) count.route_add += 1
return () => {
if (!count) return
count.route_drop += 1
}
},
navigate: () => {},
get current() {
return { name: "home" }
},
},
ui: {
Dialog: () => null,
DialogAlert: () => null,
DialogConfirm: () => null,
DialogPrompt: () => null,
DialogSelect: () => null,
Slot: () => null,
Prompt: () => null,
toast: () => {},
dialog: {
replace: () => {
depth = 1
},
clear: () => {
depth = 0
size = "medium"
},
setSize: (next) => {
size = next
},
get size() {
return size
},
get depth() {
return depth
},
get open() {
return depth > 0
},
},
},
tuiConfig: tuiConfig(opts.tuiConfig),
kv: {
get: kvGet,
set(name, value) {
kv[name] = value
},
get ready() {
return true
},
},
state: {
get ready() {
return opts.state?.ready ?? true
},
get config() {
return opts.state?.config ?? {}
},
get provider() {
return opts.state?.provider ?? []
},
get path() {
return opts.state?.path ?? { home: "", state: "", config: "", worktree: "", directory: "" }
},
get vcs() {
return opts.state?.vcs
},
session: {
count: opts.state?.session?.count ?? (() => 0),
get: opts.state?.session?.get ?? (() => undefined),
diff: opts.state?.session?.diff ?? (() => []),
todo: opts.state?.session?.todo ?? (() => []),
messages: opts.state?.session?.messages ?? (() => []),
status: opts.state?.session?.status ?? (() => undefined),
permission: opts.state?.session?.permission ?? (() => []),
question: opts.state?.session?.question ?? (() => []),
},
part: opts.state?.part ?? (() => []),
lsp: opts.state?.lsp ?? (() => []),
mcp: opts.state?.mcp ?? (() => []),
},
theme: {
get current() {
return opts.theme?.current ?? themeCurrent()
},
get selected() {
return selected
},
has(name) {
return has(name)
},
set(name) {
return set(name)
},
async install(file) {
if (opts.theme?.install) return opts.theme.install(file)
throw new Error("base theme.install should not run")
},
mode() {
if (opts.theme?.mode) return opts.theme.mode()
return "dark"
},
get ready() {
return opts.theme?.ready ?? true
},
},
}
}