-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtui.tsx
More file actions
422 lines (366 loc) · 12 KB
/
Copy pathtui.tsx
File metadata and controls
422 lines (366 loc) · 12 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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/** @jsxImportSource @opentui/solid */
import type { TuiPlugin, TuiPluginModule, TuiThemeCurrent } from "@opencode-ai/plugin/tui"
import { spawn } from "node:child_process"
import { createEffect, createSignal, For, Match, onCleanup, Show, Switch } from "solid-js"
type PluginOptions = {
codexBinary?: string
refreshMs?: number
}
type RateLimitWindow = {
usedPercent?: number
windowDurationMins?: number | null
resetsAt?: number | null
}
type RateLimitCredits = {
hasCredits?: boolean
unlimited?: boolean
balance?: string | null
}
type RateLimitSnapshot = {
limitId?: string | null
limitName?: string | null
primary?: RateLimitWindow | null
secondary?: RateLimitWindow | null
credits?: RateLimitCredits | null
planType?: string | null
}
type RateLimitResponse = {
rateLimits?: RateLimitSnapshot | null
rateLimitsByLimitId?: Record<string, RateLimitSnapshot> | null
}
type RateLimitData = {
fetchedAt: number
snapshots: RateLimitSnapshot[]
}
type RateLimitState =
| {
status: "loading"
data?: RateLimitData
}
| {
status: "ready"
data: RateLimitData
}
| {
status: "error"
message: string
data?: RateLimitData
}
const id = "opencode-plugin-codex-usage"
const MIN_REFRESH_MS = 15000
const DEFAULT_REFRESH_MS = 30000
function errorMessage(error: unknown) {
const message = error instanceof Error ? error.message : String(error)
if (/token_invalidated|invalidated|unauthorized|\b401\b/i.test(message)) {
return "Codex login expired. Run `codex login` and restart OpenCode."
}
if (error instanceof Error) {
if (error.message.includes("ENOENT")) return "codex CLI not found"
return error.message
}
return message
}
function getRefreshMs(options: PluginOptions | undefined) {
if (typeof options?.refreshMs !== "number" || !Number.isFinite(options.refreshMs)) return DEFAULT_REFRESH_MS
return Math.max(MIN_REFRESH_MS, Math.floor(options.refreshMs))
}
function durationLabel(window: RateLimitWindow | null | undefined, fallback: string) {
const minutes = window?.windowDurationMins
if (!minutes) return fallback
if (minutes === 10080) return "Weekly"
if (minutes === 43200) return "Monthly"
if (minutes % 1440 === 0) return `${minutes / 1440}d`
if (minutes % 60 === 0) return `${minutes / 60}h`
return `${minutes}m`
}
function percentLeft(window: RateLimitWindow | null | undefined) {
const used = window?.usedPercent ?? 0
return Math.max(0, Math.min(100, Math.round(100 - used)))
}
function remainingColor(remaining: number, theme: TuiThemeCurrent) {
if (remaining < 15) return theme.error
if (remaining < 50) return theme.warning
return theme.success
}
function resetLabel(timestamp: number | null | undefined) {
if (!timestamp) return "reset unavailable"
const date = new Date(timestamp * 1000)
if (Number.isNaN(date.getTime())) return "reset unavailable"
const time = new Intl.DateTimeFormat(undefined, {
hour: "2-digit",
minute: "2-digit",
hour12: false,
}).format(date)
const day = new Intl.DateTimeFormat(undefined, { day: "numeric" }).format(date)
const month = new Intl.DateTimeFormat(undefined, { month: "short" }).format(date)
return `${time} ${day} ${month}`
}
function rowLabel(window: RateLimitWindow | null | undefined, fallback: string) {
return `${durationLabel(window, fallback)}:`
}
function titleCase(part: string) {
if (!part) return part
if (/^gpt$/i.test(part)) return "GPT"
if (/^codex$/i.test(part)) return "Codex"
if (/^[0-9.]+$/.test(part)) return part
return part.charAt(0).toUpperCase() + part.slice(1)
}
function snapshotName(snapshot: RateLimitSnapshot) {
const raw = snapshot.limitName?.trim() || snapshot.limitId?.trim() || "codex"
return raw
.replace(/_/g, "-")
.split("-")
.map(titleCase)
.join("-")
}
function snapshotOrder(snapshot: RateLimitSnapshot) {
if ((snapshot.limitId || "codex").toLowerCase() === "codex") return ""
return snapshotName(snapshot).toLowerCase()
}
function sessionUsesCodex(api: Parameters<TuiPlugin>[0], sessionID: string) {
const messages = api.state.session.messages(sessionID)
for (let index = messages.length - 1; index >= 0; index -= 1) {
const message = messages[index]
const providerID = ("providerID" in message ? message.providerID : message.model.providerID).toLowerCase()
if (providerID.includes("openai") || providerID.includes("codex")) return true
return false
}
return false
}
function normalizeSnapshots(result: RateLimitResponse) {
const snapshots = Object.values(result.rateLimitsByLimitId || {})
if (snapshots.length > 0) {
return snapshots.sort((a, b) => snapshotOrder(a).localeCompare(snapshotOrder(b)))
}
return result.rateLimits ? [result.rateLimits] : []
}
async function fetchRateLimits(codexBinary: string) {
return new Promise<RateLimitData>((resolve, reject) => {
const child = spawn(codexBinary, ["app-server", "--listen", "stdio://"], {
stdio: ["pipe", "pipe", "pipe"],
})
let stdout = ""
let stderr = ""
let settled = false
const timeout = setTimeout(() => finish(new Error("timed out while reading Codex usage")), 15000)
const finish = (error?: Error, result?: RateLimitData) => {
if (settled) return
settled = true
clearTimeout(timeout)
child.kill()
if (error) {
reject(error)
return
}
if (result) {
resolve(result)
return
}
reject(new Error(stderr || "Codex returned no usage data"))
}
const send = (message: Record<string, unknown>) => {
child.stdin.write(`${JSON.stringify(message)}\n`)
}
child.on("error", (error) => finish(error instanceof Error ? error : new Error(String(error))))
child.stderr.on("data", (chunk) => {
stderr += chunk.toString()
})
child.stdout.on("data", (chunk) => {
stdout += chunk.toString()
const lines = stdout.split(/\r?\n/)
stdout = lines.pop() ?? ""
for (const line of lines) {
if (!line.trim()) continue
try {
const message = JSON.parse(line) as {
id?: number
result?: RateLimitResponse
error?: { message?: string }
}
if (message.id === 1) {
send({ method: "initialized", params: {} })
send({ method: "account/rateLimits/read", id: 2 })
continue
}
if (message.id !== 2) continue
if (message.error?.message) {
finish(new Error(message.error.message))
return
}
finish(undefined, {
fetchedAt: Date.now(),
snapshots: normalizeSnapshots(message.result || {}),
})
return
} catch (error) {
finish(error instanceof Error ? error : new Error(String(error)))
return
}
}
})
child.on("exit", (code) => {
if (!settled && code !== 0) finish(new Error(stderr || `codex exited with code ${code}`))
})
send({
method: "initialize",
id: 1,
params: {
clientInfo: {
name: "opencode_codex_usage",
title: "OpenCode Codex Usage",
version: "0.1.0",
},
},
})
})
}
function SnapshotView(props: {
snapshot: RateLimitSnapshot
theme: () => TuiThemeCurrent
}) {
const rows = () =>
[
props.snapshot.primary
? {
key: "primary",
label: rowLabel(props.snapshot.primary, "5h"),
window: props.snapshot.primary,
}
: undefined,
props.snapshot.secondary
? {
key: "secondary",
label: rowLabel(props.snapshot.secondary, "Weekly"),
window: props.snapshot.secondary,
}
: undefined,
].filter((item): item is { key: string; label: string; window: RateLimitWindow } => !!item)
const isPrimaryBucket = () => (props.snapshot.limitId || "codex").toLowerCase() === "codex"
const heading = () => (isPrimaryBucket() ? "Overall limit left" : `${snapshotName(props.snapshot)} limit left`)
return (
<box flexDirection="column" gap={0} marginTop={isPrimaryBucket() ? 0 : 1}>
<text fg={props.theme().textMuted}>{heading()}:</text>
<For each={rows()}>
{(row) => {
const remaining = percentLeft(row.window)
const color = remainingColor(remaining, props.theme())
return (
<box flexDirection="column" gap={0}>
<box flexDirection="row" gap={0}>
<text fg={props.theme().textMuted}>{row.label}</text>
<text fg={color}> {remaining}%</text>
<text fg={props.theme().textMuted}> (resets {resetLabel(row.window.resetsAt)})</text>
</box>
</box>
)
}}
</For>
</box>
)
}
function View(props: { api: Parameters<TuiPlugin>[0]; options: PluginOptions | undefined; sessionID: string }) {
const [state, setState] = createSignal<RateLimitState>({ status: "loading" })
const [collapsed, setCollapsed] = createSignal(!sessionUsesCodex(props.api, props.sessionID))
const theme = () => props.api.theme.current
const codexBinary = props.options?.codexBinary || "codex"
const refreshMs = getRefreshMs(props.options)
const toggleCollapsed = () => setCollapsed((value) => !value)
let disposed = false
let running = false
let queued = false
const refresh = async () => {
if (running) {
queued = true
return
}
running = true
try {
const data = await fetchRateLimits(codexBinary)
if (!disposed) setState({ status: "ready", data })
} catch (error) {
if (!disposed) {
const previous = state().data
setState({
status: "error",
message: errorMessage(error),
...(previous ? { data: previous } : {}),
})
}
} finally {
running = false
if (queued && !disposed) {
queued = false
void refresh()
}
}
}
createEffect(() => {
props.sessionID
setCollapsed(!sessionUsesCodex(props.api, props.sessionID))
void refresh()
})
const stopIdle = props.api.event.on("session.idle", (event) => {
if (event.properties.sessionID === props.sessionID) void refresh()
})
const interval = setInterval(() => {
void refresh()
}, refreshMs)
onCleanup(() => {
disposed = true
clearInterval(interval)
stopIdle()
})
const snapshots = () => state().data?.snapshots || []
return (
<box flexDirection="column" gap={0}>
<box
focusable
onMouseDown={toggleCollapsed}
onKeyDown={(event) => {
if (event.name === "return" || event.name === "space") {
event.preventDefault()
toggleCollapsed()
}
}}
>
<text fg={theme().text}>
<b>{collapsed() ? "▶" : "▼"} Codex Usage</b>
</text>
</box>
<Show when={!collapsed()}>
<Switch>
<Match when={state().status === "error" && !state().data}>
<text fg={theme().warning}>{state().message}</text>
</Match>
<Match when={state().status === "loading" && !state().data}>
<text fg={theme().textMuted}>Loading Codex usage...</text>
</Match>
<Match when={snapshots().length === 0}>
<text fg={theme().textMuted}>No Codex usage data available.</text>
</Match>
<Match when={snapshots().length > 0}>
<For each={snapshots()}>{(snapshot) => <SnapshotView snapshot={snapshot} theme={theme} />}</For>
</Match>
</Switch>
<Show when={state().status === "error" && state().data}>
<text fg={theme().warning}>refresh failed: {state().message}</text>
</Show>
</Show>
</box>
)
}
const tui: TuiPlugin = async (api, options) => {
api.slots.register({
order: 150,
slots: {
sidebar_content(_ctx, props) {
return <View api={api} options={options as PluginOptions | undefined} sessionID={props.session_id} />
},
},
})
}
const plugin: TuiPluginModule & { id: string } = {
id,
tui,
}
export default plugin