Skip to content

Commit b41c0c9

Browse files
kagura-chenMeinianda-L
authored andcommitted
fix(acp): respect provider currency in usage_update instead of hardcoding USD
The usage_update ACP event hardcodes currency: "USD" regardless of the provider's actual currency configuration. This causes non-USD providers (e.g. EUR-priced custom providers) to report incorrect currency labels. Add an optional `currency` field to the ProviderCost schema (defaults to "USD" when omitted for backward compatibility). Thread the configured currency through to both usage_update call sites (usage.ts layer and service.ts ACP client) using a cached lookup pattern matching the existing contextLimit implementation. Fixes anomalyco#38667 Signed-off-by: kagura-agent <[email protected]>
1 parent b471c2b commit b41c0c9

3 files changed

Lines changed: 76 additions & 2 deletions

File tree

‎packages/opencode/src/acp/service.ts‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,7 @@ function makeDirectoryService(sdk: OpencodeClient) {
616616

617617
function makeUsageService(sdk: OpencodeClient) {
618618
const limits = new Map<string, Promise<number | undefined>>()
619+
const currencies = new Map<string, Promise<string>>()
619620
const contextLimit: UsageService.Interface["contextLimit"] = Effect.fn("ACP.promptUsage.contextLimit")(
620621
function* (params) {
621622
const key = `${params.directory}\u0000${params.providerID}\u0000${params.modelID}`
@@ -636,6 +637,26 @@ function makeUsageService(sdk: OpencodeClient) {
636637
},
637638
)
638639

640+
const modelCurrency = Effect.fn("ACP.promptUsage.modelCurrency")(
641+
function* (params: { readonly directory: string; readonly providerID: ProviderV2.ID; readonly modelID: ModelV2.ID }) {
642+
const key = `${params.directory}\u0000${params.providerID}\u0000${params.modelID}`
643+
const current = currencies.get(key)
644+
if (current) return yield* Effect.promise(() => current)
645+
646+
const next = sdk.config
647+
.providers({ directory: params.directory }, { throwOnError: true })
648+
.then((response) => {
649+
const providers = Object.fromEntries(
650+
(response.data?.providers ?? []).map((provider) => [provider.id, provider]),
651+
) as Record<ProviderV2.ID, Provider.Info>
652+
return UsageService.findCurrency(providers, params.providerID, params.modelID)
653+
})
654+
.catch(() => "USD")
655+
currencies.set(key, next)
656+
return yield* Effect.promise(() => next)
657+
},
658+
)
659+
639660
const sendUpdate: UsageService.Interface["sendUpdate"] = Effect.fn("ACP.promptUsage.sendUpdate")(function* (params) {
640661
const messages = yield* request(
641662
() =>
@@ -665,6 +686,12 @@ function makeUsageService(sdk: OpencodeClient) {
665686
})
666687
if (!size) return
667688

689+
const currency = yield* modelCurrency({
690+
directory: params.directory,
691+
providerID: ProviderV2.ID.make(message.providerID),
692+
modelID: ModelV2.ID.make(message.modelID),
693+
})
694+
668695
yield* Effect.promise(() =>
669696
params.connection
670697
.sessionUpdate({
@@ -673,7 +700,7 @@ function makeUsageService(sdk: OpencodeClient) {
673700
sessionUpdate: "usage_update",
674701
used: UsageService.contextTokens(message),
675702
size,
676-
cost: { amount: UsageService.totalSessionCost(messages), currency: "USD" },
703+
cost: { amount: UsageService.totalSessionCost(messages), currency },
677704
},
678705
})
679706
.catch(() => {}),

‎packages/opencode/src/acp/usage.ts‎

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,14 @@ export function findContextLimit(
122122
return providers[providerID]?.models[modelID]?.limit.context
123123
}
124124

125+
export function findCurrency(
126+
providers: Record<ProviderV2.ID, Provider.Info>,
127+
providerID: ProviderV2.ID,
128+
modelID: ModelV2.ID,
129+
): string {
130+
return providers[providerID]?.models[modelID]?.cost.currency ?? "USD"
131+
}
132+
125133
export const contextLimitLoaderLayer = Layer.effect(
126134
ContextLimitLoader,
127135
Effect.gen(function* () {
@@ -180,6 +188,38 @@ const layer = Layer.effect(
180188
return yield* yield* cachedLimit(input)
181189
})
182190

191+
const currencies = yield* SynchronizedRef.make(new Map<string, Effect.Effect<string>>())
192+
193+
const cachedCurrency = Effect.fnUntraced(function* (input: {
194+
readonly directory: string
195+
readonly providerID: ProviderV2.ID
196+
readonly modelID: ModelV2.ID
197+
}) {
198+
return yield* SynchronizedRef.modifyEffect(
199+
currencies,
200+
Effect.fnUntraced(function* (items) {
201+
const key = `${input.directory}\u0000${input.providerID}\u0000${input.modelID}`
202+
const current = items.get(key)
203+
if (current) return [current, items] as const
204+
const next = yield* Effect.cached(
205+
contextLimitLoader.providers(input.directory).pipe(
206+
Effect.map((providers) => findCurrency(providers, input.providerID, input.modelID)),
207+
Effect.catch(() => Effect.succeed("USD")),
208+
),
209+
)
210+
return [next, new Map(items).set(key, next)] as const
211+
}),
212+
)
213+
})
214+
215+
const modelCurrency = Effect.fn("ACPUsage.modelCurrency")(function* (input: {
216+
readonly directory: string
217+
readonly providerID: ProviderV2.ID
218+
readonly modelID: ModelV2.ID
219+
}) {
220+
return yield* yield* cachedCurrency(input)
221+
})
222+
183223
const sendUpdate = Effect.fn("ACPUsage.sendUpdate")(function* (input: {
184224
readonly connection: UsageConnection
185225
readonly sessionID: string
@@ -205,6 +245,12 @@ const layer = Layer.effect(
205245
})
206246
if (!size) return
207247

248+
const currency = yield* modelCurrency({
249+
directory: input.directory,
250+
providerID: ProviderV2.ID.make(message.providerID),
251+
modelID: ModelV2.ID.make(message.modelID),
252+
})
253+
208254
yield* Effect.promise(() =>
209255
input.connection
210256
.sessionUpdate({
@@ -213,7 +259,7 @@ const layer = Layer.effect(
213259
sessionUpdate: "usage_update",
214260
used: contextTokens(message),
215261
size,
216-
cost: { amount: totalSessionCost(messages), currency: "USD" },
262+
cost: { amount: totalSessionCost(messages), currency },
217263
},
218264
})
219265
.catch(() => {}),

‎packages/opencode/src/provider/provider.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,6 +1059,7 @@ const ProviderCost = Schema.Struct({
10591059
input: Schema.Finite,
10601060
output: Schema.Finite,
10611061
cache: ProviderCacheCost,
1062+
currency: optional(Schema.String),
10621063
tiers: optional(Schema.Array(ProviderCostTier)),
10631064
experimentalOver200K: optional(
10641065
Schema.Struct({

0 commit comments

Comments
 (0)