| ... | ... | @@ -1,15 +1,15 @@ |
| 1 | 1 | import { useAtomValue } from "@effect/atom-react"; |
| 2 | | import type { EnvironmentId, UsageModelPriceOverride } from "@t3tools/contracts"; |
| 3 | | import { useState } from "react"; |
| 2 | import type { EnvironmentId } from "@t3tools/contracts"; |
| 3 | import { ChevronDownIcon, PlusIcon, RotateCcwIcon, XIcon } from "lucide-react"; |
| 4 | import * as Option from "effect/Option"; |
| 5 | import { AsyncResult, Atom } from "effect/unstable/reactivity"; |
| 6 | import { useRef, useState } from "react"; |
| 4 | 7 | |
| 5 | 8 | import { isElectron } from "../../env"; |
| 6 | | import { |
| 7 | | type EnvironmentPresentation, |
| 8 | | useEnvironments, |
| 9 | | usePrimaryEnvironmentId, |
| 10 | | } from "../../state/environments"; |
| 9 | import { cn } from "../../lib/utils"; |
| 10 | import { environmentPresentations } from "../../state/presentation"; |
| 11 | 11 | import { serverEnvironment } from "../../state/server"; |
| 12 | | import { useEnvironmentSessionState } from "../../state/session"; |
| 12 | import { environmentSession } from "../../state/session"; |
| 13 | 13 | import { useAtomCommand } from "../../state/use-atom-command"; |
| 14 | 14 | import type { EnvironmentUsageStatus } from "../../state/usage"; |
| 15 | 15 | import { |
| ... | ... | @@ -20,6 +20,7 @@ import { Button } from "../ui/button"; |
| 20 | 20 | import { |
| 21 | 21 | Dialog, |
| 22 | 22 | DialogDescription, |
| 23 | DialogFooter, |
| 23 | 24 | DialogHeader, |
| 24 | 25 | DialogPanel, |
| 25 | 26 | DialogPopup, |
| ... | ... | @@ -27,292 +28,527 @@ import { |
| 27 | 28 | } from "../ui/dialog"; |
| 28 | 29 | import { Input } from "../ui/input"; |
| 29 | 30 | import { Label } from "../ui/label"; |
| 30 | | import { Select, SelectItem, SelectPopup, SelectTrigger, SelectValue } from "../ui/select"; |
| 31 | | import { parseUsagePriceForm, USAGE_PRICE_FIELDS, usagePriceForm } from "./usagePriceForm"; |
| 31 | import { Menu, MenuCheckboxItem, MenuPopup, MenuSeparator, MenuTrigger } from "../ui/menu"; |
| 32 | import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "../ui/table"; |
| 33 | import { Tooltip, TooltipTrigger, TooltipPopup } from "../ui/tooltip"; |
| 34 | import { USAGE_PRICE_FIELDS } from "./usagePriceForm"; |
| 35 | import { |
| 36 | isEmptyUsagePriceDraft, |
| 37 | usagePriceCell, |
| 38 | usagePriceTableChanges, |
| 39 | usagePriceTableErrors, |
| 40 | type UsagePriceDraft, |
| 41 | type UsagePriceField, |
| 42 | } from "./usagePriceTable"; |
| 43 | import { |
| 44 | writeUsagePrices, |
| 45 | type UsagePriceTarget, |
| 46 | type UsagePriceWriteResult, |
| 47 | } from "./usagePriceTargets"; |
| 32 | 48 | |
| 33 | | export function UsagePriceOverrides({ |
| 34 | | usage, |
| 35 | | }: { |
| 36 | | readonly usage: readonly EnvironmentUsageStatus[]; |
| 37 | | }) { |
| 38 | | const [open, setOpen] = useState(false); |
| 39 | | return ( |
| 40 | | <> |
| 41 | | <Button variant="outline" size="sm" onClick={() => setOpen(true)}> |
| 42 | | Model prices |
| 43 | | </Button> |
| 44 | | {open ? <ModelPricesDialog usage={usage} onOpenChange={setOpen} /> : null} |
| 45 | | </> |
| 46 | | ); |
| 47 | | } |
| 49 | const priceTargetsAtom = Atom.make((get): readonly UsagePriceTarget[] => |
| 50 | [...get(environmentPresentations.presentationsAtom)].map(([environmentId, environment]) => { |
| 51 | const settings = get(serverEnvironment.settingsValueAtom(environmentId)); |
| 52 | const session = get(environmentSession.sessionStateAtom(environmentId)); |
| 53 | const sessionAccess = { |
| 54 | session: Option.getOrNull(AsyncResult.value(session)), |
| 55 | isPending: session.waiting, |
| 56 | hasError: session._tag === "Failure", |
| 57 | }; |
| 58 | const isPrimary = environment.entry.target._tag === "PrimaryConnectionTarget"; |
| 59 | const access = isPrimary |
| 60 | ? resolvePrimaryOperateAccess({ ...sessionAccess, isPrimary, hasDesktopBridge: isElectron }) |
| 61 | : resolveRemoteOperateAccess(sessionAccess); |
| 62 | return { |
| 63 | environmentId, |
| 64 | label: environment.entry.target.label, |
| 65 | prices: settings?.usagePriceOverrides ?? null, |
| 66 | unavailable: |
| 67 | environment.connection.phase !== "connected" |
| 68 | ? "Offline" |
| 69 | : settings === null |
| 70 | ? "Prices not loaded" |
| 71 | : environment.serverConfig?.environment.capabilities.usagePriceOverrides !== true |
| 72 | ? "Update server to edit prices" |
| 73 | : access === "pending" |
| 74 | ? "Checking permissions…" |
| 75 | : access === "denied" |
| 76 | ? "Read-only access" |
| 77 | : null, |
| 78 | }; |
| 79 | }), |
| 80 | ); |
| 81 | |
| 82 | type SaveAttempt = { |
| 83 | readonly drafts: readonly UsagePriceDraft[]; |
| 84 | readonly destinations: readonly { |
| 85 | readonly environmentId: EnvironmentId; |
| 86 | readonly label: string; |
| 87 | }[]; |
| 88 | readonly results: ReadonlyMap< |
| 89 | EnvironmentId, |
| 90 | UsagePriceWriteResult | { readonly status: "saving" } |
| 91 | >; |
| 92 | }; |
| 48 | 93 | |
| 49 | | function ModelPricesDialog({ |
| 94 | export function UsagePriceOverrides({ |
| 50 | 95 | usage, |
| 96 | initialSelectedEnvironmentIds, |
| 51 | 97 | onOpenChange, |
| 52 | 98 | }: { |
| 53 | 99 | readonly usage: readonly EnvironmentUsageStatus[]; |
| 100 | readonly initialSelectedEnvironmentIds: ReadonlySet<EnvironmentId> | null; |
| 54 | 101 | readonly onOpenChange: (open: boolean) => void; |
| 55 | 102 | }) { |
| 56 | | const { environments } = useEnvironments(); |
| 57 | | const primaryEnvironmentId = usePrimaryEnvironmentId(); |
| 58 | | const [selectedId, setSelectedId] = useState<EnvironmentId | null>(primaryEnvironmentId); |
| 59 | | const environment = |
| 60 | | environments.find((entry) => entry.environmentId === selectedId) ?? environments[0]; |
| 61 | | const summary = usage.find( |
| 62 | | (entry) => entry.environmentId === environment?.environmentId, |
| 63 | | )?.summary; |
| 64 | | const models = [...new Set(summary?.buckets.map((bucket) => bucket.model) ?? [])].sort(); |
| 103 | const environments = useAtomValue(priceTargetsAtom); |
| 104 | const [selectedIds, setSelectedIds] = useState(initialSelectedEnvironmentIds); |
| 105 | const selected = environments.filter( |
| 106 | (environment) => selectedIds === null || selectedIds.has(environment.environmentId), |
| 107 | ); |
| 108 | const [drafts, setDrafts] = useState<readonly UsagePriceDraft[]>([]); |
| 109 | const [pending, setPending] = useState(false); |
| 110 | const [attempt, setAttempt] = useState<SaveAttempt | null>(null); |
| 111 | const focusRowRef = useRef<string | null>(null); |
| 112 | const nextRowId = useRef(0); |
| 113 | const updateSettings = useAtomCommand(serverEnvironment.updateSettings, { reportFailure: false }); |
| 114 | const customModels = [ |
| 115 | ...new Set(selected.flatMap((environment) => Object.keys(environment.prices ?? {}))), |
| 116 | ].sort(); |
| 117 | const models = [ |
| 118 | ...new Set([ |
| 119 | ...customModels, |
| 120 | ...usage |
| 121 | .filter((environment) => |
| 122 | selected.some((target) => target.environmentId === environment.environmentId), |
| 123 | ) |
| 124 | .flatMap((environment) => environment.summary?.buckets.map((bucket) => bucket.model) ?? []), |
| 125 | ]), |
| 126 | ].sort(); |
| 127 | // Keep new rows in place while successful environments publish their updated settings. |
| 128 | const newModels = new Set( |
| 129 | drafts.filter((draft) => draft.isNew).map((draft) => draft.model.trim()), |
| 130 | ); |
| 131 | const rows: readonly UsagePriceDraft[] = [ |
| 132 | ...customModels |
| 133 | .filter((model) => attempt === null || !newModels.has(model)) |
| 134 | .map( |
| 135 | (model) => |
| 136 | drafts.find((draft) => draft.id === `model:${model}`) ?? { |
| 137 | id: `model:${model}`, |
| 138 | model, |
| 139 | isNew: false, |
| 140 | values: {}, |
| 141 | }, |
| 142 | ), |
| 143 | ...drafts.filter((draft) => draft.isNew), |
| 144 | ]; |
| 145 | const stagedDrafts = drafts.filter((draft) => !isEmptyUsagePriceDraft(draft)); |
| 146 | const errors = usagePriceTableErrors(selected, stagedDrafts); |
| 147 | for (const draft of stagedDrafts) { |
| 148 | if (!draft.isNew) continue; |
| 149 | if (draft.model.trim() === "") errors.set(draft.id, "Enter a model ID."); |
| 150 | else if ( |
| 151 | attempt === null && |
| 152 | (customModels.includes(draft.model.trim()) || |
| 153 | drafts.some((other) => other.id !== draft.id && other.model.trim() === draft.model.trim())) |
| 154 | ) |
| 155 | errors.set(draft.id, "This model already has a row. Edit its prices there."); |
| 156 | } |
| 157 | const failedDestinations = |
| 158 | attempt?.destinations.filter( |
| 159 | (destination) => attempt.results.get(destination.environmentId)?.status === "failed", |
| 160 | ) ?? []; |
| 161 | const locked = pending || failedDestinations.length > 0; |
| 162 | const hasChanges = stagedDrafts.length > 0; |
| 163 | const destinationLabel = |
| 164 | selected.length === 1 ? selected[0]!.label : `${selected.length} environments`; |
| 165 | const selectionLabel = selectedIds === null ? "All environments" : destinationLabel; |
| 166 | const discard = () => { |
| 167 | setDrafts([]); |
| 168 | setAttempt(null); |
| 169 | }; |
| 170 | const selectEnvironments = (ids: ReadonlySet<EnvironmentId> | null) => { |
| 171 | setSelectedIds(ids); |
| 172 | setAttempt(null); |
| 173 | }; |
| 174 | const updateDraft = (draft: UsagePriceDraft) => { |
| 175 | setDrafts((previous) => |
| 176 | previous.some((entry) => entry.id === draft.id) |
| 177 | ? previous.map((entry) => (entry.id === draft.id ? draft : entry)) |
| 178 | : [...previous, draft], |
| 179 | ); |
| 180 | setAttempt(null); |
| 181 | }; |
| 182 | const editCell = (row: UsagePriceDraft, field: UsagePriceField, value: string) => { |
| 183 | const values = { ...row.values, [field]: value }; |
| 184 | const original = usagePriceCell(selected, row.model, field); |
| 185 | if ( |
| 186 | !row.isNew && |
| 187 | original.placeholder !== "Mixed" && |
| 188 | original.placeholder !== "Unavailable" && |
| 189 | value === original.value |
| 190 | ) |
| 191 | delete values[field]; |
| 192 | if (!row.isNew && Object.keys(values).length === 0) |
| 193 | setDrafts((previous) => previous.filter((entry) => entry.id !== row.id)); |
| 194 | else updateDraft({ ...row, values }); |
| 195 | }; |
| 196 | const save = async (retry = false) => { |
| 197 | if (pending) return; |
| 198 | const destinations = retry ? failedDestinations : selected; |
| 199 | const edits = retry && attempt ? attempt.drafts : stagedDrafts; |
| 200 | if (destinations.length === 0 || edits.length === 0) return; |
| 201 | const targets = destinations.map( |
| 202 | (destination) => |
| 203 | environments.find( |
| 204 | (environment) => environment.environmentId === destination.environmentId, |
| 205 | ) ?? { ...destination, prices: null, unavailable: "Environment removed" }, |
| 206 | ); |
| 207 | const changes = new Map( |
| 208 | targets.map((target) => [target.environmentId, usagePriceTableChanges(target, edits)]), |
| 209 | ); |
| 210 | setPending(true); |
| 211 | setAttempt((previous) => ({ |
| 212 | drafts: edits, |
| 213 | destinations: retry && previous ? previous.destinations : targets, |
| 214 | results: new Map([ |
| 215 | ...(retry && previous ? previous.results : []), |
| 216 | ...targets.map((target) => [target.environmentId, { status: "saving" } as const] as const), |
| 217 | ]), |
| 218 | })); |
| 219 | let failed = false; |
| 220 | await writeUsagePrices({ |
| 221 | targets: targets.map((target) => ({ |
| 222 | ...target, |
| 223 | unavailable: |
| 224 | target.unavailable ?? [...changes.get(target.environmentId)!.errors.values()][0] ?? null, |
| 225 | })), |
| 226 | changes: new Map([...changes].map(([id, plan]) => [id, plan.changes])), |
| 227 | write: updateSettings, |
| 228 | onResult: (environmentId, result) => { |
| 229 | if (result.status === "failed") failed = true; |
| 230 | setAttempt((previous) => |
| 231 | previous === null |
| 232 | ? null |
| 233 | : { ...previous, results: new Map(previous.results).set(environmentId, result) }, |
| 234 | ); |
| 235 | }, |
| 236 | }); |
| 237 | setPending(false); |
| 238 | if (!failed) setDrafts([]); |
| 239 | }; |
| 65 | 240 | |
| 66 | 241 | return ( |
| 67 | | <Dialog open onOpenChange={onOpenChange}> |
| 68 | | <DialogPopup className="max-w-xl"> |
| 242 | <Dialog |
| 243 | open |
| 244 | onOpenChange={(open) => { |
| 245 | if (!pending) onOpenChange(open); |
| 246 | }} |
| 247 | > |
| 248 | <DialogPopup className="max-w-3xl"> |
| 69 | 249 | <DialogHeader> |
| 70 | | <DialogTitle>Model prices</DialogTitle> |
| 250 | <DialogTitle>Custom model prices</DialogTitle> |
| 71 | 251 | <DialogDescription> |
| 72 | | Override estimated costs using USD per million tokens. Prices apply to all saved history |
| 73 | | on the selected environment. |
| 252 | Prices apply to all past and future usage on the environments you select. |
| 74 | 253 | </DialogDescription> |
| 75 | 254 | </DialogHeader> |
| 76 | | <DialogPanel className="grid gap-5 pb-6"> |
| 77 | | {environment ? ( |
| 78 | | <> |
| 79 | | <div className="grid gap-1.5"> |
| 80 | | <Label htmlFor="usage-prices-environment">Environment</Label> |
| 81 | | <Select |
| 82 | | value={environment.environmentId} |
| 83 | | onValueChange={(value) => { |
| 84 | | const next = environments.find((entry) => entry.environmentId === value); |
| 85 | | if (next) setSelectedId(next.environmentId); |
| 86 | | }} |
| 255 | <DialogPanel className="grid gap-4"> |
| 256 | <div className="flex flex-wrap items-center justify-between gap-2"> |
| 257 | <div className="flex min-w-0 items-center gap-2"> |
| 258 | <Label id="usage-prices-apply-label" className="shrink-0"> |
| 259 | Apply to |
| 260 | </Label> |
| 261 | <Menu> |
| 262 | <MenuTrigger |
| 263 | render={<Button variant="outline" size="sm" className="min-w-0" />} |
| 264 | aria-labelledby="usage-prices-apply-label usage-prices-selection" |
| 265 | disabled={pending || hasChanges} |
| 87 | 266 | > |
| 88 | | <SelectTrigger id="usage-prices-environment"> |
| 89 | | <SelectValue>{environment.label}</SelectValue> |
| 90 | | </SelectTrigger> |
| 91 | | <SelectPopup> |
| 92 | | {environments.map((entry) => ( |
| 93 | | <SelectItem key={entry.environmentId} value={entry.environmentId}> |
| 94 | | {entry.label} |
| 95 | | </SelectItem> |
| 267 | <span id="usage-prices-selection" className="truncate"> |
| 268 | {selectionLabel} |
| 269 | </span> |
| 270 | <ChevronDownIcon className="size-3.5 shrink-0" aria-hidden /> |
| 271 | </MenuTrigger> |
| 272 | <MenuPopup align="start" className="w-80 max-w-[calc(100vw-2rem)]"> |
| 273 | <MenuCheckboxItem |
| 274 | checked={selectedIds === null} |
| 275 | closeOnClick={false} |
| 276 | onCheckedChange={(checked) => selectEnvironments(checked ? null : new Set())} |
| 277 | > |
| 278 | All environments |
| 279 | </MenuCheckboxItem> |
| 280 | <MenuSeparator /> |
| 281 | {environments.map((environment) => ( |
| 282 | <MenuCheckboxItem |
| 283 | key={environment.environmentId} |
| 284 | checked={selectedIds === null || selectedIds.has(environment.environmentId)} |
| 285 | closeOnClick={false} |
| 286 | onCheckedChange={(checked) => { |
| 287 | const next = new Set(selected.map((target) => target.environmentId)); |
| 288 | if (checked) next.add(environment.environmentId); |
| 289 | else next.delete(environment.environmentId); |
| 290 | selectEnvironments(next.size === environments.length ? null : next); |
| 291 | }} |
| 292 | > |
| 293 | <span className="flex min-w-0 items-center gap-3"> |
| 294 | <span className="min-w-0 flex-1 truncate">{environment.label}</span> |
| 295 | {environment.unavailable ? ( |
| 296 | <span className="text-xs text-muted-foreground"> |
| 297 | {environment.unavailable} |
| 298 | </span> |
| 299 | ) : null} |
| 300 | </span> |
| 301 | </MenuCheckboxItem> |
| 302 | ))} |
| 303 | </MenuPopup> |
| 304 | </Menu> |
| 305 | </div> |
| 306 | <span className="text-xs text-muted-foreground">USD / million tokens</span> |
| 307 | </div> |
| 308 | {selected.length === 0 ? ( |
| 309 | <p className="text-sm text-muted-foreground"> |
| 310 | {environments.length === 0 |
| 311 | ? "Connect an environment to set model prices." |
| 312 | : "Select an environment to see and change its model prices."} |
| 313 | </p> |
| 314 | ) : ( |
| 315 | <> |
| 316 | <div className="min-w-0 overflow-hidden rounded-lg border border-border"> |
| 317 | <Table className="min-w-160 table-fixed" aria-label="Custom model prices"> |
| 318 | <colgroup> |
| 319 | <col className="w-[34%]" /> |
| 320 | {USAGE_PRICE_FIELDS.map((field) => ( |
| 321 | <col key={field.key} /> |
| 96 | 322 | ))} |
| 97 | | </SelectPopup> |
| 98 | | </Select> |
| 323 | <col className="w-10" /> |
| 324 | </colgroup> |
| 325 | <TableHeader> |
| 326 | <TableRow className="hover:bg-transparent"> |
| 327 | <TableHead className="pl-3">Model ID</TableHead> |
| 328 | {USAGE_PRICE_FIELDS.map((field) => ( |
| 329 | <TableHead key={field.key}>{field.label}</TableHead> |
| 330 | ))} |
| 331 | <TableHead className="px-1"> |
| 332 | <Button |
| 333 | size="icon-xs" |
| 334 | variant="ghost" |
| 335 | aria-label="Add model price" |
| 336 | disabled={locked} |
| 337 | onClick={() => { |
| 338 | const id = `new:${nextRowId.current++}`; |
| 339 | focusRowRef.current = id; |
| 340 | updateDraft({ id, model: "", isNew: true, values: {} }); |
| 341 | }} |
| 342 | > |
| 343 | <PlusIcon aria-hidden /> |
| 344 | </Button> |
| 345 | </TableHead> |
| 346 | </TableRow> |
| 347 | </TableHeader> |
| 348 | <TableBody> |
| 349 | {rows.length === 0 ? ( |
| 350 | <TableRow className="hover:bg-transparent"> |
| 351 | <TableCell |
| 352 | colSpan={6} |
| 353 | className="py-8 text-center whitespace-normal text-muted-foreground" |
| 354 | > |
| 355 | {selected.some((environment) => environment.prices === null) |
| 356 | ? "Some environment prices are unavailable." |
| 357 | : "No custom prices. Add a row to override automatic pricing."} |
| 358 | </TableCell> |
| 359 | </TableRow> |
| 360 | ) : ( |
| 361 | rows.map((row) => ( |
| 362 | <TableRow |
| 363 | key={row.id} |
| 364 | data-row-id={row.id} |
| 365 | className="hover:bg-transparent" |
| 366 | > |
| 367 | <TableCell className="pl-3 whitespace-normal"> |
| 368 | {row.isNew ? ( |
| 369 | <Input |
| 370 | size="compact" |
| 371 | value={row.model} |
| 372 | ref={(node) => { |
| 373 | if (node && focusRowRef.current === row.id) { |
| 374 | node.focus(); |
| 375 | focusRowRef.current = null; |
| 376 | } |
| 377 | }} |
| 378 | aria-label="New model ID" |
| 379 | aria-invalid={ |
| 380 | (row.model.trim() !== "" && errors.has(row.id)) || undefined |
| 381 | } |
| 382 | list="usage-price-models" |
| 383 | placeholder="Model ID" |
| 384 | autoComplete="off" |
| 385 | spellCheck={false} |
| 386 | disabled={locked} |
| 387 | onChange={(event) => |
| 388 | updateDraft({ ...row, model: event.target.value }) |
| 389 | } |
| 390 | /> |
| 391 | ) : ( |
| 392 | <span |
| 393 | className={cn( |
| 394 | "block break-all", |
| 395 | row.removed && "text-muted-foreground line-through", |
| 396 | )} |
| 397 | > |
| 398 | {row.model} |
| 399 | </span> |
| 400 | )} |
| 401 | {errors.has(row.id) && |
| 402 | (row.model.trim() !== "" || |
| 403 | Object.values(row.values).some((value) => value !== "")) ? ( |
| 404 | <p role="alert" className="mt-1 text-xs text-destructive"> |
| 405 | {errors.get(row.id)} |
| 406 | </p> |
| 407 | ) : null} |
| 408 | </TableCell> |
| 409 | {row.removed ? ( |
| 410 | <TableCell colSpan={4} className="text-muted-foreground"> |
| 411 | Automatic pricing after saving |
| 412 | </TableCell> |
| 413 | ) : ( |
| 414 | USAGE_PRICE_FIELDS.map((field) => { |
| 415 | const cell = usagePriceCell(selected, row.model, field.key); |
| 416 | return ( |
| 417 | <TableCell key={field.key} className="px-1"> |
| 418 | <Input |
| 419 | size="compact" |
| 420 | inputMode="decimal" |
| 421 | aria-label={`${field.label} price for ${row.model || "new model"}`} |
| 422 | value={row.values[field.key] ?? (row.isNew ? "" : cell.value)} |
| 423 | placeholder={ |
| 424 | row.isNew |
| 425 | ? field.optional |
| 426 | ? "Input rate" |
| 427 | : "0.00" |
| 428 | : cell.placeholder |
| 429 | } |
| 430 | autoComplete="off" |
| 431 | disabled={locked} |
| 432 | className="tabular-nums" |
| 433 | onChange={(event) => |
| 434 | editCell(row, field.key, event.target.value) |
| 435 | } |
| 436 | /> |
| 437 | </TableCell> |
| 438 | ); |
| 439 | }) |
| 440 | )} |
| 441 | <TableCell className="px-1"> |
| 442 | <Tooltip> |
| 443 | <TooltipTrigger |
| 444 | render={<Button size="icon-xs" variant="ghost" />} |
| 445 | disabled={locked} |
| 446 | aria-label={ |
| 447 | row.removed |
| 448 | ? `Undo reset for ${row.model}` |
| 449 | : row.isNew |
| 450 | ? "Remove new model" |
| 451 | : `Reset price for ${row.model} to automatic` |
| 452 | } |
| 453 | onClick={() => { |
| 454 | if (row.isNew) |
| 455 | setDrafts((previous) => |
| 456 | previous.filter((entry) => entry.id !== row.id), |
| 457 | ); |
| 458 | else if (row.removed) { |
| 459 | if (Object.keys(row.values).length === 0) |
| 460 | setDrafts((previous) => |
| 461 | previous.filter((entry) => entry.id !== row.id), |
| 462 | ); |
| 463 | else updateDraft({ ...row, removed: false }); |
| 464 | } else updateDraft({ ...row, removed: true }); |
| 465 | }} |
| 466 | > |
| 467 | {row.isNew ? <XIcon aria-hidden /> : <RotateCcwIcon aria-hidden />} |
| 468 | </TooltipTrigger> |
| 469 | <TooltipPopup> |
| 470 | {row.removed |
| 471 | ? "Undo reset" |
| 472 | : row.isNew |
| 473 | ? "Remove row" |
| 474 | : "Reset to automatic"} |
| 475 | </TooltipPopup> |
| 476 | </Tooltip> |
| 477 | </TableCell> |
| 478 | </TableRow> |
| 479 | )) |
| 480 | )} |
| 481 | </TableBody> |
| 482 | </Table> |
| 99 | 483 | </div> |
| 100 | | <EnvironmentModelPrices |
| 101 | | key={environment.environmentId} |
| 102 | | environment={environment} |
| 103 | | models={models} |
| 104 | | /> |
| 484 | <datalist id="usage-price-models"> |
| 485 | {models |
| 486 | .filter((model) => !customModels.includes(model)) |
| 487 | .map((model) => ( |
| 488 | <option key={model} value={model} /> |
| 489 | ))} |
| 490 | </datalist> |
| 491 | <p className="text-xs text-muted-foreground"> |
| 492 | Blank cache rates use the input price. Enter 0 for free tokens. |
| 493 | {selected.length > 1 |
| 494 | ? " Mixed cells keep each environment’s rate until you edit them." |
| 495 | : ""} |
| 496 | </p> |
| 105 | 497 | </> |
| 106 | | ) : ( |
| 107 | | <p className="text-sm text-muted-foreground"> |
| 108 | | Connect an environment to set model prices. |
| 109 | | </p> |
| 110 | 498 | )} |
| 111 | | </DialogPanel> |
| 112 | | </DialogPopup> |
| 113 | | </Dialog> |
| 114 | | ); |
| 115 | | } |
| 116 | | |
| 117 | | function EnvironmentModelPrices({ |
| 118 | | environment, |
| 119 | | models, |
| 120 | | }: { |
| 121 | | readonly environment: EnvironmentPresentation; |
| 122 | | readonly models: readonly string[]; |
| 123 | | }) { |
| 124 | | const settings = useAtomValue(serverEnvironment.settingsValueAtom(environment.environmentId)); |
| 125 | | const session = useEnvironmentSessionState(environment.environmentId); |
| 126 | | const updateSettings = useAtomCommand(serverEnvironment.updateSettings, { reportFailure: false }); |
| 127 | | const [form, setForm] = useState(usagePriceForm()); |
| 128 | | const [editingModel, setEditingModel] = useState<string | null>(null); |
| 129 | | const [pending, setPending] = useState(false); |
| 130 | | const [error, setError] = useState<string | null>(null); |
| 131 | | const prices = settings?.usagePriceOverrides ?? {}; |
| 132 | | const isPrimary = environment.entry.target._tag === "PrimaryConnectionTarget"; |
| 133 | | const access = isPrimary |
| 134 | | ? resolvePrimaryOperateAccess({ |
| 135 | | isPrimary, |
| 136 | | hasDesktopBridge: isElectron, |
| 137 | | session: session.data, |
| 138 | | isPending: session.isPending, |
| 139 | | hasError: session.hasError, |
| 140 | | }) |
| 141 | | : resolveRemoteOperateAccess({ |
| 142 | | session: session.data, |
| 143 | | isPending: session.isPending, |
| 144 | | hasError: session.hasError, |
| 145 | | }); |
| 146 | | const supportsOverrides = |
| 147 | | environment.serverConfig?.environment.capabilities.usagePriceOverrides === true; |
| 148 | | const unavailable = |
| 149 | | environment.connection.phase !== "connected" |
| 150 | | ? "Connect this environment to change its model prices." |
| 151 | | : settings === null |
| 152 | | ? "Loading model prices..." |
| 153 | | : !supportsOverrides |
| 154 | | ? "Update this environment's server to set model prices." |
| 155 | | : access === "pending" |
| 156 | | ? "Checking permissions..." |
| 157 | | : access === "denied" |
| 158 | | ? "This session can view model prices but cannot change them." |
| 159 | | : null; |
| 160 | | const readOnly = unavailable !== null || pending; |
| 161 | | const parsed = parseUsagePriceForm(form); |
| 162 | | const duplicate = editingModel === null && parsed !== null && Object.hasOwn(prices, parsed.model); |
| 163 | | |
| 164 | | const reset = () => { |
| 165 | | setEditingModel(null); |
| 166 | | setForm(usagePriceForm()); |
| 167 | | setError(null); |
| 168 | | }; |
| 169 | | const save = async (model: string, price: UsageModelPriceOverride | null) => { |
| 170 | | if (readOnly) return; |
| 171 | | setPending(true); |
| 172 | | setError(null); |
| 173 | | try { |
| 174 | | const result = await updateSettings({ |
| 175 | | environmentId: environment.environmentId, |
| 176 | | input: { patch: { usagePriceOverrides: { [model]: price } } }, |
| 177 | | }); |
| 178 | | if (result._tag === "Failure") { |
| 179 | | setError("Could not save model prices. Try again."); |
| 180 | | } else if (price !== null || editingModel === model) { |
| 181 | | reset(); |
| 182 | | } |
| 183 | | } finally { |
| 184 | | setPending(false); |
| 185 | | } |
| 186 | | }; |
| 187 | | |
| 188 | | return ( |
| 189 | | <> |
| 190 | | {Object.keys(prices).length > 0 ? ( |
| 191 | | <ul className="divide-y divide-border rounded-lg border border-border"> |
| 192 | | {Object.entries(prices) |
| 193 | | .sort(([left], [right]) => left.localeCompare(right)) |
| 194 | | .map(([model, price]) => ( |
| 195 | | <li key={model} className="flex flex-wrap items-center gap-3 p-3"> |
| 196 | | <div className="min-w-0 flex-1"> |
| 197 | | <p className="break-all text-sm font-medium">{model}</p> |
| 198 | | <p className="text-xs text-muted-foreground"> |
| 199 | | Input ${price.inputCostPerMillionTokens} · Output $ |
| 200 | | {price.outputCostPerMillionTokens} |
| 201 | | <br /> |
| 202 | | Cache read $ |
| 203 | | {price.cacheReadCostPerMillionTokens ?? price.inputCostPerMillionTokens} |
| 204 | | {" · "}Cache write $ |
| 205 | | {price.cacheWriteCostPerMillionTokens ?? price.inputCostPerMillionTokens} |
| 206 | | </p> |
| 207 | | </div> |
| 208 | | <Button |
| 209 | | variant="ghost" |
| 210 | | size="xs" |
| 211 | | disabled={readOnly} |
| 212 | | aria-label={`Edit price for ${model}`} |
| 213 | | onClick={() => { |
| 214 | | setEditingModel(model); |
| 215 | | setForm(usagePriceForm(model, price)); |
| 216 | | setError(null); |
| 217 | | }} |
| 218 | | > |
| 219 | | Edit |
| 220 | | </Button> |
| 221 | | <Button |
| 222 | | variant="ghost" |
| 223 | | size="xs" |
| 224 | | disabled={readOnly} |
| 225 | | aria-label={`Remove price for ${model}`} |
| 226 | | onClick={() => void save(model, null)} |
| 227 | | > |
| 228 | | Remove |
| 229 | | </Button> |
| 230 | | </li> |
| 231 | | ))} |
| 232 | | </ul> |
| 233 | | ) : ( |
| 234 | | <p className="text-sm text-muted-foreground"> |
| 235 | | No custom prices. Public model pricing is used when available. |
| 236 | | </p> |
| 237 | | )} |
| 238 | | {unavailable ? <p className="text-sm text-muted-foreground">{unavailable}</p> : null} |
| 239 | | <form |
| 240 | | className="grid gap-4" |
| 241 | | onSubmit={(event) => { |
| 242 | | event.preventDefault(); |
| 243 | | if (parsed && !duplicate) void save(parsed.model, parsed.price); |
| 244 | | }} |
| 245 | | > |
| 246 | | <fieldset disabled={readOnly} className="grid min-w-0 gap-4"> |
| 247 | | <legend className="mb-3 text-sm font-medium"> |
| 248 | | {editingModel === null ? "Add model price" : "Edit model price"} |
| 249 | | </legend> |
| 250 | | <div className="grid gap-1.5"> |
| 251 | | <Label htmlFor="usage-price-model">Model ID</Label> |
| 252 | | <Input |
| 253 | | id="usage-price-model" |
| 254 | | list="usage-price-models" |
| 255 | | value={form.model} |
| 256 | | disabled={editingModel !== null} |
| 257 | | placeholder="Exact model ID from usage" |
| 258 | | autoComplete="off" |
| 259 | | spellCheck={false} |
| 260 | | required |
| 261 | | onChange={(event) => setForm({ ...form, model: event.target.value })} |
| 262 | | /> |
| 263 | | <datalist id="usage-price-models"> |
| 264 | | {models.map((model) => ( |
| 265 | | <option key={model} value={model} /> |
| 266 | | ))} |
| 267 | | </datalist> |
| 268 | | {duplicate ? ( |
| 269 | | <p role="alert" className="text-xs text-destructive"> |
| 270 | | This model already has a price. Use Edit to change it. |
| 271 | | </p> |
| 272 | | ) : null} |
| 273 | | </div> |
| 274 | | <div className="grid grid-cols-2 gap-3"> |
| 275 | | {USAGE_PRICE_FIELDS.map((field) => ( |
| 276 | | <div key={field.key} className="grid gap-1.5"> |
| 277 | | <Label htmlFor={`usage-price-${field.key}`}> |
| 278 | | {field.label} |
| 279 | | {field.optional ? " (optional)" : ""} |
| 280 | | </Label> |
| 281 | | <Input |
| 282 | | id={`usage-price-${field.key}`} |
| 283 | | type="number" |
| 284 | | min="0" |
| 285 | | step="any" |
| 286 | | inputMode="decimal" |
| 287 | | required={!field.optional} |
| 288 | | value={form[field.key]} |
| 289 | | placeholder={field.optional ? "Use input rate" : "0.00"} |
| 290 | | onChange={(event) => setForm({ ...form, [field.key]: event.target.value })} |
| 291 | | /> |
| 292 | | </div> |
| 293 | | ))} |
| 294 | | </div> |
| 295 | | <p className="text-xs text-muted-foreground"> |
| 296 | | Blank cache rates use the input price. Enter 0 for free tokens. Model IDs match exactly. |
| 297 | | Remove a price to restore automatic pricing. |
| 298 | | </p> |
| 299 | | {error ? ( |
| 300 | | <p role="alert" className="text-sm text-destructive"> |
| 301 | | {error} |
| 302 | | </p> |
| 499 | {attempt ? ( |
| 500 | <div role="status" className="grid gap-1 text-xs"> |
| 501 | {attempt.destinations.map((destination) => { |
| 502 | const result = attempt.results.get(destination.environmentId); |
| 503 | return ( |
| 504 | <div key={destination.environmentId} className="flex justify-between gap-3"> |
| 505 | <span className="truncate text-muted-foreground">{destination.label}</span> |
| 506 | <span |
| 507 | className={ |
| 508 | result?.status === "failed" |
| 509 | ? "text-right text-destructive" |
| 510 | : "text-muted-foreground" |
| 511 | } |
| 512 | > |
| 513 | {result?.status === "failed" |
| 514 | ? `Not saved · ${result.error}` |
| 515 | : result?.status === "saved" |
| 516 | ? "Saved" |
| 517 | : "Saving…"} |
| 518 | </span> |
| 519 | </div> |
| 520 | ); |
| 521 | })} |
| 522 | </div> |
| 303 | 523 | ) : null} |
| 304 | | <div className="flex justify-end gap-2"> |
| 305 | | {editingModel !== null ? ( |
| 306 | | <Button variant="outline" onClick={reset}> |
| 307 | | Cancel |
| 524 | </DialogPanel> |
| 525 | <DialogFooter variant="bare" className="items-center sm:justify-between"> |
| 526 | <span className="text-xs text-muted-foreground"> |
| 527 | {hasChanges ? `Changes apply to ${destinationLabel}` : ""} |
| 528 | </span> |
| 529 | <div className="flex w-full flex-wrap justify-end gap-2 sm:w-auto"> |
| 530 | {hasChanges ? ( |
| 531 | <Button variant="ghost" disabled={pending} onClick={discard}> |
| 532 | {failedDestinations.length > 0 ? "Discard pending changes" : "Discard changes"} |
| 308 | 533 | </Button> |
| 309 | 534 | ) : null} |
| 310 | | <Button type="submit" disabled={parsed === null || duplicate || readOnly}> |
| 311 | | {pending ? "Saving..." : editingModel === null ? "Add price" : "Save price"} |
| 535 | <Button |
| 536 | disabled={ |
| 537 | pending || |
| 538 | (!hasChanges && failedDestinations.length === 0) || |
| 539 | (failedDestinations.length === 0 && (errors.size > 0 || selected.length === 0)) |
| 540 | } |
| 541 | onClick={() => void save(failedDestinations.length > 0)} |
| 542 | > |
| 543 | {pending |
| 544 | ? "Saving…" |
| 545 | : failedDestinations.length > 0 |
| 546 | ? "Retry failed saves" |
| 547 | : "Save changes"} |
| 312 | 548 | </Button> |
| 313 | 549 | </div> |
| 314 | | </fieldset> |
| 315 | | </form> |
| 316 | | </> |
| 550 | </DialogFooter> |
| 551 | </DialogPopup> |
| 552 | </Dialog> |
| 317 | 553 | ); |
| 318 | 554 | } |