Skip to content

Commit 77ec59b

Browse files
committed
fix(opencode): price Go usage and recognize free models
1 parent bb4afa5 commit 77ec59b

2 files changed

Lines changed: 95 additions & 2 deletions

File tree

‎packages/provider-opencode/src/server/usage.test.ts‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,90 @@ const openDatabase = (filename: string) =>
1919
);
2020

2121
describe("readOpenCodeUsage", () => {
22+
it.effect("preserves OpenCode Go pricing identity and recognizes free models", () =>
23+
Effect.gen(function* () {
24+
const fileSystem = yield* FileSystem.FileSystem;
25+
const path = yield* Path.Path;
26+
const dir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "usage-pricing-test-" });
27+
const db = yield* openDatabase(path.join(dir, "opencode.db"));
28+
yield* db.unsafe("CREATE TABLE message (id TEXT, session_id TEXT, data TEXT)");
29+
const model = "deepseek-v4.1-flash";
30+
const rateModel = `opencode_go/${model}`;
31+
const cases = [
32+
{
33+
metadata: { modelID: model, providerID: "opencode-go" },
34+
rateModel,
35+
cost: 0,
36+
reportedCostUsd: null,
37+
},
38+
{
39+
metadata: { model: { id: model, providerID: "opencode-go" } },
40+
rateModel,
41+
cost: 0.25,
42+
reportedCostUsd: 0.25,
43+
},
44+
{
45+
metadata: { model: { modelID: model, providerID: "opencode-go" } },
46+
rateModel,
47+
cost: 0,
48+
reportedCostUsd: null,
49+
},
50+
{
51+
metadata: { modelID: model, providerID: "other" },
52+
rateModel: undefined,
53+
cost: 0,
54+
reportedCostUsd: null,
55+
},
56+
{ metadata: { modelID: model }, rateModel: undefined, cost: 0, reportedCostUsd: null },
57+
{
58+
metadata: { modelID: rateModel, providerID: "opencode-go" },
59+
rateModel: undefined,
60+
cost: 0,
61+
reportedCostUsd: null,
62+
},
63+
{
64+
metadata: { modelID: `${model}-free`, providerID: "opencode" },
65+
rateModel: undefined,
66+
cost: 0,
67+
reportedCostUsd: 0,
68+
},
69+
];
70+
for (const [index, test] of cases.entries()) {
71+
const source = JSON.stringify({
72+
role: "assistant",
73+
...test.metadata,
74+
time: { created: 1780000000000 },
75+
cost: test.cost,
76+
tokens: { input: 100, output: 20, cache: { read: 30 } },
77+
});
78+
if (index === 2) {
79+
const legacy = path.join(dir, "storage", "message", "session-1");
80+
yield* fileSystem.makeDirectory(legacy, { recursive: true });
81+
yield* fileSystem.writeFileString(path.join(legacy, `msg-${index}.json`), source);
82+
} else {
83+
yield* db.unsafe("INSERT INTO message VALUES (?, ?, ?)", [
84+
`msg-${index}`,
85+
"session-1",
86+
source,
87+
]);
88+
}
89+
}
90+
const result = yield* readOpenCodeUsage(dir, 0);
91+
assert.isFalse(result.error);
92+
const records = result.files.flatMap((file) => file.records);
93+
assert.strictEqual(records.length, cases.length);
94+
for (const [index, test] of cases.entries()) {
95+
const record = records.find((record) => record.dedupeKey === `opencode:msg-${index}`);
96+
assert.strictEqual(
97+
record?.model,
98+
test.metadata.modelID ?? test.metadata.model?.id ?? test.metadata.model?.modelID,
99+
);
100+
assert.strictEqual(record?.rateModel, test.rateModel);
101+
assert.strictEqual(record?.reportedCostUsd, test.reportedCostUsd);
102+
}
103+
}).pipe(Effect.provide(NodeServices.layer)),
104+
);
105+
22106
it.effect("bounds raw history allocations while counting every eligible message once", () =>
23107
Effect.gen(function* () {
24108
const fileSystem = yield* FileSystem.FileSystem;

‎packages/provider-opencode/src/server/usage.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ function parseOpenCodeMessage(
4949
const cache = object(usage.cache);
5050
const modelReference = object(message.model);
5151
const model = text(modelReference.id) || text(modelReference.modelID) || text(message.modelID);
52+
const providerID = text(modelReference.providerID) || text(message.providerID);
5253
const timestampMs = object(message.time).created ?? fallback.timestampMs;
5354
if (!model || typeof timestampMs !== "number" || !Number.isFinite(timestampMs)) return null;
5455
const reasoningTokens = tokens(usage.reasoning);
@@ -66,10 +67,18 @@ function parseOpenCodeMessage(
6667
provider: "opencode",
6768
timestampMs,
6869
model,
70+
...(providerID === "opencode-go" && !model.includes("/")
71+
? { rateModel: `opencode_go/${model}` }
72+
: {}),
6973
sessionId: fallback.sessionId || text(message.sessionID),
7074
totals,
71-
// OpenCode uses zero for unknown rates, including paid subscriptions; let shared pricing estimate.
72-
reportedCostUsd: typeof cost === "number" && Number.isFinite(cost) && cost > 0 ? cost : null,
75+
// Zero means unknown for paid subscriptions, but is authoritative for explicit free models.
76+
reportedCostUsd:
77+
typeof cost === "number" &&
78+
Number.isFinite(cost) &&
79+
(cost > 0 || (cost === 0 && model.endsWith("-free")))
80+
? cost
81+
: null,
7382
speed: "standard",
7483
dedupeKey: id ? `opencode:${id}` : null,
7584
};

0 commit comments

Comments
 (0)