Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions src/tui.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/** @jsxImportSource @opentui/solid */
import { createSignal, createMemo, onCleanup, onMount, Show } from "solid-js";
import type { TuiPlugin } from "@opencode-ai/plugin/tui";
import { logDebugEvent } from "./debug-logger.js";

const BALANCE_URL = "https://api.deepseek.com/user/balance";
const REFRESH_INTERVAL_MS = 30_000;
Expand Down Expand Up @@ -67,29 +68,49 @@ const DeepseekBalance = (props: { palette: Palette }) => {
const fetchBalance = async () => {
const apiKey = process.env.DEEPSEEK_API_KEY;
if (!apiKey) {
logDebugEvent("fetch.error", { reason: "no_api_key" });
setErrorMsg("DEEPSEEK_API_KEY not set");
setLoading(false);
return;
}

try {
const started = Date.now();
const res = await fetch(BALANCE_URL, {
headers: {
Authorization: `Bearer ${apiKey}`,
Accept: "application/json",
},
});
if (!res.ok) throw new Error(`HTTP ${res.status}`);

if (!res.ok) {
const status = res.status;
logDebugEvent("fetch.error", { status, ms: Date.now() - started });
setErrorMsg(`DeepSeek API ${status}`);
setLoading(false);
return;
}

const data = (await res.json()) as BalanceResponse;
const info = data.balance_infos?.[0];
if (info) {
logDebugEvent("fetch.ok", {
currency: info.currency,
total: info.total_balance,
ms: Date.now() - started,
});
setBalance(info);
setIsAvailable(data.is_available);
setErrorMsg("");
} else {
logDebugEvent("fetch.error", { reason: "empty_balance_infos" });
setErrorMsg("No balance data");
}
} catch {
} catch (err) {
logDebugEvent("fetch.error", {
reason: "network",
message: err instanceof Error ? err.message : String(err),
});
if (!balance()) {
setErrorMsg("DeepSeek API unreachable");
}
Expand Down Expand Up @@ -132,6 +153,7 @@ const DeepseekBalance = (props: { palette: Palette }) => {
});

onMount(() => {
logDebugEvent("lifecycle", { event: "mount" });
void fetchBalance();
const timer = setInterval(() => void fetchBalance(), REFRESH_INTERVAL_MS);
onCleanup(() => clearInterval(timer));
Expand Down Expand Up @@ -171,6 +193,8 @@ const SidebarBalance = (props: { theme: Record<string, unknown> }) => {
};

const tui: TuiPlugin = (api) => {
logDebugEvent("lifecycle", { event: "init", hasApiKey: !!process.env.DEEPSEEK_API_KEY });

api.slots.register({
order: SIDEBAR_ORDER,
slots: {
Expand Down
Loading