-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusage-plugin.ts
More file actions
55 lines (49 loc) · 1.84 KB
/
Copy pathusage-plugin.ts
File metadata and controls
55 lines (49 loc) · 1.84 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
// SPDX-License-Identifier: GPL-3.0-or-later
// opencode-usage TUI plugin.
//
// Registers a `/usage` slash command. Typing `/usage` in the TUI runs `onSelect`
// directly (the same mechanism behind built-in commands like /models and /help)
// and shows the report in a dialog, read from OpenCode's local database.
//
// IMPORTANT: TUI plugins are not auto-discovered from the plugin/ directory.
// This file must be referenced from `tui.json` (the installer does that):
// { "plugin": ["./usage-plugin.ts"] }
//
// The computation lives in ./usage.mjs (also usable as a CLI).
import type { TuiPlugin } from "@opencode-ai/plugin/tui"
import { buildReport, dbPath, loadMessages } from "./usage.mjs"
const tui: TuiPlugin = async (api) => {
// `api.command` is the supported way for a plugin to add a slash command.
const register = api.command?.register
if (!register) return
register(() => [
{
title: "Usage",
value: "usage.show",
description: "Show your OpenCode token + cost usage",
category: "Usage",
suggested: true,
slash: { name: "usage", aliases: ["cost", "tokens"] },
async onSelect(dialog) {
const stack = dialog ?? api.ui.dialog
let report: string
try {
const messages = await loadMessages()
report = buildReport(messages, { period: "summary", now: Date.now() })
} catch (err) {
const message = err instanceof Error ? err.message : String(err)
report = `Could not read OpenCode usage.\n\nDatabase: ${dbPath()}\nError: ${message}`
}
stack.setSize?.("large")
stack.replace(() =>
api.ui.DialogAlert({
title: "OpenCode Usage",
message: report,
onConfirm: () => stack.clear(),
}),
)
},
},
])
}
export default { id: "opencode-usage", tui }