The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import type { |
| 2 | EnvironmentId, |
| 3 | ServerSettingsPatch, |
| 4 | UsageModelPriceOverride, |
| 5 | } from "@t3tools/contracts"; |
| 6 | import { msg } from "gt-react"; |
| 7 | |
| 8 | import { m } from "~/i18n/messages"; |
| 9 | |
| 10 | export interface UsagePriceTarget { |
| 11 | readonly environmentId: EnvironmentId; |
| 12 | readonly label: string; |
| 13 | readonly prices: Readonly<Record<string, UsageModelPriceOverride>> | null; |
| 14 | readonly aliases: Readonly<Record<string, string>> | null; |
| 15 | readonly unavailable: string | null; |
| 16 | } |
| 17 | |
| 18 | /** `null` removes the model's custom price or mapping. */ |
| 19 | export type UsagePriceChange = |
| 20 | | { readonly model: string; readonly price: UsageModelPriceOverride | null } |
| 21 | | { readonly model: string; readonly alias: string | null }; |
| 22 | |
| 23 | export type UsagePriceWriteResult = |
| 24 | | { readonly status: "saved" } |
| 25 | | { readonly status: "failed"; readonly error: string }; |
| 26 | |
| 27 | /** Destinations settle independently; retries pass only failures. */ |
| 28 | export async function writeUsagePrices(input: { |
| 29 | readonly targets: readonly UsagePriceTarget[]; |
| 30 | readonly changes: ReadonlyMap<EnvironmentId, readonly UsagePriceChange[]>; |
| 31 | readonly write: (input: { |
| 32 | environmentId: EnvironmentId; |
| 33 | input: { patch: ServerSettingsPatch }; |
| 34 | }) => Promise<{ readonly _tag: "Success" | "Failure" }>; |
| 35 | readonly onResult: (environmentId: EnvironmentId, result: UsagePriceWriteResult) => void; |
| 36 | }) { |
| 37 | await Promise.all( |
| 38 | input.targets.map(async (target) => { |
| 39 | let result: UsagePriceWriteResult; |
| 40 | const changes = input.changes.get(target.environmentId) ?? []; |
| 41 | const prices = changes.flatMap((change) => |
| 42 | "price" in change ? [[change.model, change.price] as const] : [], |
| 43 | ); |
| 44 | const aliases = changes.flatMap((change) => |
| 45 | "alias" in change ? [[change.model, change.alias] as const] : [], |
| 46 | ); |
| 47 | if (target.unavailable !== null) { |
| 48 | result = { status: "failed", error: target.unavailable }; |
| 49 | } else if (aliases.length > 0 && target.aliases === null) { |
| 50 | result = { status: "failed", error: m(msg("Update server to map models")) }; |
| 51 | } else { |
| 52 | try { |
| 53 | const saved = |
| 54 | changes.length === 0 |
| 55 | ? { _tag: "Success" as const } |
| 56 | : await input.write({ |
| 57 | environmentId: target.environmentId, |
| 58 | input: { |
| 59 | patch: { |
| 60 | ...(prices.length > 0 |
| 61 | ? { usagePriceOverrides: Object.fromEntries(prices) } |
| 62 | : {}), |
| 63 | ...(aliases.length > 0 |
| 64 | ? { usageModelAliases: Object.fromEntries(aliases) } |
| 65 | : {}), |
| 66 | }, |
| 67 | }, |
| 68 | }); |
| 69 | result = |
| 70 | saved._tag === "Success" |
| 71 | ? { status: "saved" } |
| 72 | : { status: "failed", error: m(msg("Could not save. Try again.")) }; |
| 73 | } catch { |
| 74 | result = { status: "failed", error: m(msg("Could not save. Try again.")) }; |
| 75 | } |
| 76 | } |
| 77 | input.onResult(target.environmentId, result); |
| 78 | }), |
| 79 | ); |
| 80 | } |