Skip to content
Merged
Show file tree
Hide file tree
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
55 changes: 55 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { existsSync, readFileSync } from "node:fs";
import { join } from "node:path";

export interface UsageStatsConfig {
/** Enable the dashboard web server on plugin startup (default: true) */
dashboardEnabled: boolean;
/** Port for the dashboard web server (default: 3333) */
dashboardPort: number;
}

const DEFAULTS: UsageStatsConfig = {
dashboardEnabled: true,
dashboardPort: 3333,
};

const CONFIG_DIR = join(process.env.HOME || "~", ".config", "opencode");
const CONFIG_CANDIDATES = [
join(CONFIG_DIR, "usage-stats.jsonc"),
join(CONFIG_DIR, "usage-stats.json"),
];

function stripJsoncComments(text: string): string {
return text.replace(/\/\/.*$/gm, "").replace(/\/\*[\s\S]*?\*\//g, "");
}

export function loadConfig(): UsageStatsConfig {
for (const path of CONFIG_CANDIDATES) {
if (!existsSync(path)) continue;

try {
const raw = readFileSync(path, "utf-8");
const parsed = JSON.parse(stripJsoncComments(raw));
return {
dashboardEnabled:
typeof parsed.dashboardEnabled === "boolean"
? parsed.dashboardEnabled
: DEFAULTS.dashboardEnabled,
dashboardPort:
typeof parsed.dashboardPort === "number"
? parsed.dashboardPort
: DEFAULTS.dashboardPort,
};
} catch {
// Malformed config — fall through to defaults
}
}

// Environment variable overrides (backward compat)
return {
dashboardEnabled: process.env.OPENCODE_USAGE_STATS_DASHBOARD !== "false",
dashboardPort: process.env.OPENCODE_USAGE_STATS_PORT
? parseInt(process.env.OPENCODE_USAGE_STATS_PORT, 10)
: DEFAULTS.dashboardPort,
};
}
16 changes: 15 additions & 1 deletion src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { join } from "node:path";
import type { Plugin } from "@opencode-ai/plugin";
import { loadConfig } from "./config";
import { SessionContext } from "./context/session-context";
import { createDashboard } from "./dashboard/index";
import type { Repos } from "./db/repos";
import { createSqliteRepos } from "./db/sqlite-repository";
import { createSqliteRepos, gcOldData } from "./db/sqlite-repository";
import { createChatParamsHandler } from "./handlers/chat-params";
import { createEventHandler } from "./handlers/event";
import { createToolExecuteAfterHandler } from "./handlers/tool-execute";
Expand All @@ -18,13 +20,25 @@ interface UsageStatsPluginDeps {
function createUsageStatsPlugin(deps: UsageStatsPluginDeps): Plugin {
return async (ctx) => {
const repos = deps.createRepos(DB_PATH);
const config = loadConfig();

const today = new Date().toISOString().slice(0, 10);
const sevenDaysAgo = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000)
.toISOString()
.slice(0, 10);
repos.dailyUsage.recompute(sevenDaysAgo, today);

if (config.dashboardEnabled) {
const dashboard = createDashboard({
createReadRepos: (p) => createSqliteRepos(p, { readonly: true }),
createWriteRepos: (p) => createSqliteRepos(p),
gcOldData,
});
dashboard.start(config.dashboardPort, DB_PATH).catch(() => {
// Silently ignore dashboard start failures to not break the plugin
});
}

const context = new SessionContext(ctx.project?.id ?? null);
const chatParamsHandler = createChatParamsHandler(context);
const toolExecuteAfterHandler = createToolExecuteAfterHandler(
Expand Down