Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

UsagePriceOverrides.tsx

554 lines23.6 KB

1import { useAtomValue } from "@effect/atom-react";
2import type { EnvironmentId } from "@t3tools/contracts";
3import { ChevronDownIcon, PlusIcon, RotateCcwIcon, XIcon } from "lucide-react";
4import * as Option from "effect/Option";
5import { AsyncResult, Atom } from "effect/unstable/reactivity";
6import { useRef, useState } from "react";
7​
8import { isElectron } from "../../env";
9import { cn } from "../../lib/utils";
10import { environmentPresentations } from "../../state/presentation";
11import { serverEnvironment } from "../../state/server";
12import { environmentSession } from "../../state/session";
13import { useAtomCommand } from "../../state/use-atom-command";
14import type { EnvironmentUsageStatus } from "../../state/usage";
15import {
16 resolvePrimaryOperateAccess,
17 resolveRemoteOperateAccess,
18} from "../settings/ProviderSettingsPanel.logic";
19import { Button } from "../ui/button";
20import {
21 Dialog,
22 DialogDescription,
23 DialogFooter,
24 DialogHeader,
25 DialogPanel,
26 DialogPopup,
27 DialogTitle,
28} from "../ui/dialog";
29import { Input } from "../ui/input";
30import { Label } from "../ui/label";
31import { Menu, MenuCheckboxItem, MenuPopup, MenuSeparator, MenuTrigger } from "../ui/menu";
32import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "../ui/table";
33import { Tooltip, TooltipTrigger, TooltipPopup } from "../ui/tooltip";
34import { USAGE_PRICE_FIELDS } from "./usagePriceForm";
35import {
36 isEmptyUsagePriceDraft,
37 usagePriceCell,
38 usagePriceTableChanges,
39 usagePriceTableErrors,
40 type UsagePriceDraft,
41 type UsagePriceField,
42} from "./usagePriceTable";
43import {
44 writeUsagePrices,
45 type UsagePriceTarget,
46 type UsagePriceWriteResult,
47} from "./usagePriceTargets";
48​
49const 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​
82type 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};
93​
94export function UsagePriceOverrides({
95 usage,
96 initialSelectedEnvironmentIds,
97 onOpenChange,
98}: {
99 readonly usage: readonly EnvironmentUsageStatus[];
100 readonly initialSelectedEnvironmentIds: ReadonlySet<EnvironmentId> | null;
101 readonly onOpenChange: (open: boolean) => void;
102}) {
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 };
240​
241 return (
242 <Dialog
243 open
244 onOpenChange={(open) => {
245 if (!pending) onOpenChange(open);
246 }}
247 >
248 <DialogPopup className="max-w-3xl">
249 <DialogHeader>
250 <DialogTitle>Custom model prices</DialogTitle>
251 <DialogDescription>
252 Prices apply to all past and future usage on the environments you select.
253 </DialogDescription>
254 </DialogHeader>
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}
266 >
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} />
322 ))}
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>
483 </div>
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>
497 </>
498 )}
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>
523 ) : null}
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"}
533 </Button>
534 ) : null}
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"}
548 </Button>
549 </div>
550 </DialogFooter>
551 </DialogPopup>
552 </Dialog>
553 );