Open History to see the last commit
1import { useAtomValue } from "@effect/atom-react";
2import {
3 type EnvironmentId,
4 type ThreadId,
5 hostPressureCulprit,
6 hostPressureRanking,
7} from "@t3tools/contracts";
8import { useGT } from "gt-react";
9import { TriangleAlertIcon } from "lucide-react";
10
11import { environmentPresentations } from "../../state/presentation";
12import { hostPressureAgentUsage } from "./SidebarHostPressureWarning";
13
14/** Selects primitives and the server's own agent object, so only culprit rows re-render. */
15function useThreadHostPressureNote(environmentId: EnvironmentId, threadId: ThreadId) {
16 const gt = useGT();
17 const agent = useAtomValue(environmentPresentations.serverConfigsAtom, (configs) =>
18 hostPressureCulprit(configs.get(environmentId)?.hostPressure, threadId),
19 );
20 const rankedBy = useAtomValue(environmentPresentations.serverConfigsAtom, (configs) => {
21 const pressure = configs.get(environmentId)?.hostPressure;
22 return pressure === undefined ? "memory" : hostPressureRanking(pressure);
23 });
24 const connected = useAtomValue(
25 environmentPresentations.presentationsAtom,
26 (presentations) => presentations.get(environmentId)?.connection.phase === "connected",
27 );
28 if (agent === null || !connected) return null;
29 const usage = hostPressureAgentUsage(gt, agent, rankedBy);
30 return rankedBy === "memory"
31 ? gt`Using ${{ usage }} while memory is slowing agents down. Stopping it helps the rest.`
32 : gt`Using ${{ usage }} while this machine is overloaded. Stopping it helps the rest.`;
33}
34
35/** Marks a thread whose agent is among the heaviest on a machine short on memory or CPU. */
36export function SidebarThreadHostPressureIcon(props: {
37 readonly environmentId: EnvironmentId;
38 readonly threadId: ThreadId;
39}) {
40 const note = useThreadHostPressureNote(props.environmentId, props.threadId);
41 if (note === null) return null;
42 return (
43 <TriangleAlertIcon
44 role="img"
45 aria-label={note}
46 data-testid={`sidebar-host-pressure-${props.threadId}`}
47 className="size-3 shrink-0 text-warning-foreground"
48 />
49 );
50}
51
52export function SidebarThreadHostPressureLine(props: {
53 readonly environmentId: EnvironmentId;
54 readonly threadId: ThreadId;
55}) {
56 const note = useThreadHostPressureNote(props.environmentId, props.threadId);
57 if (note === null) return null;
58 return (
59 <div className="flex min-w-0 items-start gap-2 text-warning">
60 <TriangleAlertIcon aria-hidden className="mt-0.5 size-3 shrink-0 stroke-current" />
61 <div className="min-w-0 flex-1 wrap-break-word">{note}</div>
62 </div>
63 );
64}