-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathopencode2-tui.ts
More file actions
188 lines (162 loc) · 6.07 KB
/
Copy pathopencode2-tui.ts
File metadata and controls
188 lines (162 loc) · 6.07 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
import { appendFileSync } from "node:fs";
import { Plugin } from "@opencode/plugin/tui";
import { createEffect, createMemo, createSignal, onCleanup } from "solid-js";
import {
createQuotaMonitor,
type QuotaMonitor,
type QuotaMonitorOptions,
} from "./lib/codex-usage-monitor.js";
import { ProbeSnapshotSchema, type ProbeSnapshot } from "./lib/codex-usage-probe.js";
import { resolveSignalPath } from "./lib/codex-usage-signal.js";
import { CODEX_USAGE_RPC } from "./lib/opencode2-rpc.js";
import {
resolvePollMs,
resolveToastDurationMs,
resolveToastThreshold,
} from "./lib/quota-settings.js";
type QuotaProbe = () => Promise<ProbeSnapshot>;
type MonitorFactory = (options: QuotaMonitorOptions) => QuotaMonitor;
export const createOpenCode2TuiPlugin = (
probe?: QuotaProbe,
createMonitor: MonitorFactory = createQuotaMonitor,
) =>
Plugin.define({
id: "opencode-codex-usage",
setup(ctx) {
const debugPath = process.env.OPENCODE_CODEX_QUOTA_DEBUG_FILE?.trim();
let debugErrorReported = false;
const recordPolling = (event: string) => {
if (!debugPath) return;
try {
appendFileSync(debugPath, `${new Date().toISOString()} ${event}\n`, { mode: 0o600 });
} catch (error: unknown) {
if (debugErrorReported) return;
debugErrorReported = true;
const detail = error instanceof Error ? error.message : String(error);
console.error(`[opencode-codex-usage] polling diagnostic write failed: ${detail}`);
}
};
const quotaProbe: QuotaProbe =
probe ??
(async () => {
const snapshot = await ctx.client.rpc(CODEX_USAGE_RPC).usage({});
return ProbeSnapshotSchema.parse(snapshot);
});
const monitorOptions = {
probe: quotaProbe,
notify: (toast) => ctx.ui.toast.show(toast),
logError: (message, detail) => {
console.error(`[opencode-codex-usage] ${message}${detail ? `: ${detail}` : ""}`);
},
pollMs: resolvePollMs(),
threshold: resolveToastThreshold(),
durationMs: resolveToastDurationMs(),
signalPath: resolveSignalPath(),
} satisfies QuotaMonitorOptions;
const monitor = createMonitor({ ...monitorOptions, onDiagnostic: recordPolling });
// Explicit checks have no timers and survive pauses of the automatic monitor.
const manualMonitor = createMonitor(monitorOptions);
let disposed = false;
const releaseSlot = ctx.ui.slot({
append: "app",
render: () => {
// Reading the cached selection reactively catches model switches as well as navigation.
const selected = createMemo(() => {
const route = ctx.ui.router.current();
if (route.type !== "session") return undefined;
const session = ctx.data.session.get(route.sessionID);
if (!session) return undefined;
return {
sessionID: route.sessionID,
providerID: session.model?.providerID,
modelID: session.model?.id,
location: { directory: session.location.directory },
};
});
const [revision, invalidate] = createSignal(0);
onCleanup(
ctx.data.listen(({ details }) => {
if (
"location" in details &&
details.location?.directory &&
details.location.directory !== selected()?.location.directory
)
return;
switch (details.type) {
case "credential.updated":
case "credential.switched":
case "integration.updated":
case "provider.updated":
case "model.updated":
case "config.updated":
case "location.shutdown":
case "server.connected":
invalidate((value) => value + 1);
}
}),
);
let polling = false;
const setPolling = (enabled: boolean) => {
if (polling === enabled) return;
polling = enabled;
if (enabled) monitor.start();
else monitor.stop({ resetStatus: false });
};
onCleanup(() => setPolling(false));
createEffect(() => {
revision();
const selection = selected();
if (!selection || disposed) {
setPolling(false);
return;
}
if (selection.providerID && selection.providerID !== "openai") {
setPolling(false);
return;
}
let current = true;
onCleanup(() => {
current = false;
});
void ctx.client
.rpc(CODEX_USAGE_RPC)
.pollingEligible({ sessionID: selection.sessionID }, { location: selection.location })
.then((eligible) => {
if (!current || disposed) return;
setPolling(eligible);
})
.catch((error: unknown) => {
if (!current || disposed) return;
setPolling(false);
const detail = error instanceof Error ? error.message : String(error);
console.error(`[opencode-codex-usage] polling eligibility failed: ${detail}`);
});
});
ctx.keymap.layer(() => ({
mode: "global",
commands: [
{
id: "codex_usage",
title: "Codex usage",
description: "Show current Codex quota",
group: "Codex",
bind: "ctrl+x u",
palette: true,
slash: { name: "codex-usage" },
run: () => manualMonitor.refresh({ force: true, showFailure: true }),
},
],
}));
return null;
},
});
return () => {
if (disposed) return;
disposed = true;
monitor.stop();
manualMonitor.stop();
releaseSlot();
};
},
});
export default createOpenCode2TuiPlugin();