chg: [opencode-greenpt-usage] port the TUI plugin to OpenCode 2

V1 TUI modules no longer load: replace ``{ id, tui }`` with
``Plugin.define({ id, setup })``, register the section on
``sidebar.content``, and expose commands through ``context.keymap.layer``.

Move balance, clock and usage state onto ``context.storage.store/memory``
so host subscribers rerender; plugin-created signals stayed stale in the
V2 renderer. Read V2 assistant messages (``type``,
``model.providerID``/``model.id``) and subscribe through ``context.data``.

Configure the package directory in ``cli.json`` instead of the source
file; document the V2 install. Typecheck, 49 unit tests and the live
GreenPT integration test pass, and the sidebar renders the balance.
This commit is contained in:
Stéphan Sainléger
2026-11-18 23:01:40 +01:00
parent 9c84473266
commit dadc93a22d
13 changed files with 4757 additions and 671 deletions

View File

@@ -17,9 +17,8 @@ const NOW = new Date(2026, 9, 19, 15, 30, 0).getTime()
function assistantMessage(overrides: Record<string, unknown> = {}) {
return {
id: "msg-1",
role: "assistant",
providerID: "greenpt",
modelID: "glm-5.3-flash",
type: "assistant",
model: { providerID: "greenpt", id: "glm-5.3-flash" },
cost: 0.01,
time: { created: NOW },
tokens: { input: 100, output: 50, reasoning: 10, cache: { read: 5, write: 2 } },
@@ -53,7 +52,12 @@ describe("recordMessage", () => {
it("ignores non-GreenPT providers", () => {
const days: UsageDays = {}
expect(recordMessage(days, assistantMessage({ providerID: "anthropic" }))).toBe(false)
expect(
recordMessage(
days,
assistantMessage({ model: { providerID: "anthropic", id: "claude-sonnet" } }),
),
).toBe(false)
expect(Object.keys(days).length).toBe(0)
})
@@ -78,7 +82,7 @@ describe("applyMessages", () => {
it("skips user messages and unknown shapes", () => {
const days: UsageDays = {}
applyMessages(days, [
{ id: "u1", role: "user" },
{ id: "u1", type: "user" },
assistantMessage(),
null,
undefined,
@@ -104,7 +108,7 @@ describe("totals", () => {
recordMessage(days, assistantMessage())
recordMessage(
days,
assistantMessage({ id: "msg-2", modelID: "kimi-k2", cost: 0.05 }),
assistantMessage({ id: "msg-2", model: { providerID: "greenpt", id: "kimi-k2" }, cost: 0.05 }),
)
const day = dayTotals(days, NOW)
expect(day.byModel.map((row) => row.model)).toEqual(["kimi-k2", "glm-5.3-flash"])