feat(usage): add OpenCode usage reporting #6040
Closedgmmeyer(GitHub profile, opens in a new tab)wants to merge 3 commits into
main fromrefs/pull/6040/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
- apps/server/src/usage/usageOpenCode.ts lines 208 to 211Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +94,21 @@94+ row.modelId.length === 095+ ) {96+ return null;97+ }98+99+ const uncachedInputTokens = nonNegativeInt(row.inputTokens);100+ const cachedInputTokens = nonNegativeInt(row.cacheReadTokens);101+ const cacheCreationTokens = nonNegativeInt(row.cacheWriteTokens);102+ const generatedOutputTokens = nonNegativeInt(row.outputTokens);103+ const reasoningTokens = nonNegativeInt(row.reasoningTokens);104+105+ // OpenCode reports generated text and reasoning as disjoint counts. The106+ // shared contract treats reasoning as a subset of output, so combine them in107+ // outputTokens while retaining the reasoning slice for the token-mix UI.108+ const outputTokens = generatedOutputTokens + reasoningTokens;109+ if (110+ uncachedInputTokens + cachedInputTokens + cacheCreationTokens + outputTokens === 0 &&111+ finiteNonNegative(row.costUsd) === null112+ ) {113+ return null;114+ }
- apps/server/src/usage/UsageService.ts line 246Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -219,0 +225,21 @@225+ const openCodeDatabaseOverride = process.env.OPENCODE_DB?.trim() || undefined;226+ const disableOpenCodeChannelDatabase = process.env.OPENCODE_DISABLE_CHANNEL_DB?.trim();227+ const shouldDiscoverOpenCodeDatabases =228+ !openCodeDatabaseOverride &&229+ !["1", "true"].includes(disableOpenCodeChannelDatabase?.toLowerCase() ?? "");230+ const openCodeDirectoryEntries = shouldDiscoverOpenCodeDatabases231+ ? yield* fileSystem232+ .readDirectory(openCodeDataDir)233+ .pipe(Effect.catchCause(() => Effect.succeed([] as string[])))234+ : [];235+ const openCodeDatabasePaths = resolveOpenCodeDatabasePaths({236+ dataDir: openCodeDataDir,237+ databaseOverride: openCodeDatabaseOverride,238+ disableChannelDatabase: disableOpenCodeChannelDatabase,239+ directoryEntries: openCodeDirectoryEntries,240+ path,241+ });242+ const openCodeResolvedHomePath =243+ openCodeDatabaseOverride !== undefined244+ ? (openCodeDatabasePaths[0] ?? ":memory:")245+ : openCodeDataDir;
- packages/contracts/src/usage.ts line 24OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -23,2 +23,2 @@2323 */2424 export const USAGE_CONTRACT_VERSION = 4 as const;apps/server/src/usage/usageOpenCode.ts lines 40 to 40Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +12,21 @@12+// Kept non-literal so the Node-targeted bundle leaves Bun's runtime module13+// external without asking its resolver to load a module Node cannot provide.14+const BUN_SQLITE_MODULE_ID: string = "bun:sqlite";15+16+const OPEN_CODE_STABLE_DATABASE = "opencode.db";17+const OPEN_CODE_CHANNEL_DATABASE = /^opencode-[a-zA-Z0-9._-]+\.db$/;18+19+const OPEN_CODE_USAGE_QUERY = `20+ SELECT21+ id AS messageId,22+ session_id AS sessionId,23+ time_created AS timestampMs,24+ json_extract(data, '$.providerID') AS providerId,25+ json_extract(data, '$.modelID') AS modelId,26+ json_extract(data, '$.tokens.input') AS inputTokens,27+ json_extract(data, '$.tokens.output') AS outputTokens,28+ json_extract(data, '$.tokens.reasoning') AS reasoningTokens,29+ json_extract(data, '$.tokens.cache.read') AS cacheReadTokens,30+ json_extract(data, '$.tokens.cache.write') AS cacheWriteTokens,31+ json_extract(data, '$.cost') AS costUsd32+ FROM message
t3dotgg(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.


Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID:
44678b53-e708-4389-b6aa-3a97bdafbf47You can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share