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

threadDetailsItems.ts

148 lines6.0 KB

1import type { ClientSettings } from "@t3tools/contracts";
2import { arrayMove } from "@dnd-kit/sortable";
3import { msg } from "gt-react";
4​
5// Ids are stored in client settings, so never rename one.
6export const THREAD_DETAILS_ITEMS = [
7 { id: "environment", label: msg("Environment and worktree"), defaultVisible: true },
8 { id: "openIn", label: msg("Open in editor"), defaultVisible: true },
9 { id: "scripts", label: msg("Project scripts"), defaultVisible: true },
10 { id: "branch", label: msg("Branch and pull request"), defaultVisible: true },
11 { id: "gitActions", label: msg("Git actions"), defaultVisible: true },
12 {
13 id: "changes",
14 label: msg({ context: "thread details item: version control changes" })`Changes`,
15 defaultVisible: true,
16 },
17 {
18 id: "servers",
19 label: msg({ context: "thread details item: running dev servers" })`Servers`,
20 defaultVisible: true,
21 },
22 { id: "automations", label: msg("Automations"), defaultVisible: true },
23 {
24 id: "lineage",
25 label: msg({ context: "thread details item: parent and child agents" })`Lineage`,
26 defaultVisible: true,
27 },
28 { id: "sideChats", label: msg("Side chats"), defaultVisible: true },
29 { id: "tasks", label: msg("Agent tasks"), defaultVisible: false },
30 { id: "usage", label: msg("Usage limits"), defaultVisible: false },
31 { id: "context", label: msg("Context window"), defaultVisible: false },
32 {
33 id: "machineLoad",
34 label: msg({ context: "thread details item: CPU, memory and disk use" })`Machine load`,
35 defaultVisible: false,
36 },
37] as const;
38​
39export type ThreadDetailsItemId = (typeof THREAD_DETAILS_ITEMS)[number]["id"];
40export type ThreadDetailsVisibility = Readonly<Record<ThreadDetailsItemId, boolean>>;
41​
42type ThreadDetailsItem = (typeof THREAD_DETAILS_ITEMS)[number];
43type ThreadDetailsItemSettings = Pick<
44 ClientSettings,
45 "threadDetailsItems" | "threadDetailsUsageEnabled"
46>;
47​
48// People who turned on the retired Labs toggle keep Usage and Context until they change them here.
49function defaultVisible(item: ThreadDetailsItem, settings: ThreadDetailsItemSettings) {
50 return item.id === "usage" || item.id === "context"
51 ? settings.threadDetailsUsageEnabled
52 : item.defaultVisible;
54​
55export function resolveThreadDetailsVisibility(
56 settings: ThreadDetailsItemSettings,
57): ThreadDetailsVisibility {
58 return Object.fromEntries(
59 THREAD_DETAILS_ITEMS.map((item) => [
60 item.id,
61 settings.threadDetailsItems[item.id] ?? defaultVisible(item, settings),
62 ]),
63 ) as Record<ThreadDetailsItemId, boolean>;
65​
66/** Stores only choices that differ from the default, so a later default change reaches everyone else. */
67export function setThreadDetailsItem(
68 settings: ThreadDetailsItemSettings,
69 item: ThreadDetailsItem,
70 visible: boolean,
71): ClientSettings["threadDetailsItems"] {
72 const { [item.id]: _previous, ...rest } = settings.threadDetailsItems;
73 return visible === defaultVisible(item, settings) ? rest : { ...rest, [item.id]: visible };
75​
76export function isDefaultThreadDetailsVisibility(visibility: ThreadDetailsVisibility) {
77 return THREAD_DETAILS_ITEMS.every((item) => visibility[item.id] === item.defaultVisible);
79​
80// The card's movable blocks. A block in a group shares one section with group neighbors next to it.
81// Ids are stored in client settings, so never rename one.
82export const THREAD_DETAILS_BLOCKS = [
83 { id: "environment", group: "workspace", label: msg("Environment and worktree") },
84 { id: "openIn", group: "workspace", label: msg("Open in editor") },
85 { id: "scripts", group: "workspace", label: msg("Project scripts") },
86 { id: "branch", group: "versionControl", label: msg("Branch and pull request") },
87 { id: "git", group: "versionControl", label: msg("Git actions") },
88 { id: "repositories", group: "versionControl", label: msg("Repositories") },
89 {
90 id: "servers",
91 group: null,
92 label: msg({ context: "thread details item: running dev servers" })`Servers`,
93 },
94 { id: "automations", group: null, label: msg("Automations") },
95 { id: "relationships", group: null, label: msg("Lineage and side chats") },
96 { id: "tasks", group: null, label: msg("Agent tasks") },
97 { id: "usage", group: null, label: msg("Usage limits") },
98 { id: "context", group: null, label: msg("Context window") },
99 {
100 id: "machineLoad",
101 group: null,
102 label: msg({ context: "thread details item: CPU, memory and disk use" })`Machine load`,
103 },
104] as const;
105​
106export type ThreadDetailsBlockId = (typeof THREAD_DETAILS_BLOCKS)[number]["id"];
107​
108const DEFAULT_BLOCK_ORDER = THREAD_DETAILS_BLOCKS.map((block) => block.id);
109​
110/** Saved order first; a block it never mentions, such as one added later, keeps its default neighbor. */
111export function resolveThreadDetailsOrder(
112 settings: Pick<ClientSettings, "threadDetailsOrder">,
113): ReadonlyArray<ThreadDetailsBlockId> {
114 const order = [
115 ...new Set(
116 settings.threadDetailsOrder.filter((id): id is ThreadDetailsBlockId =>
117 DEFAULT_BLOCK_ORDER.includes(id as ThreadDetailsBlockId),
118 ),
119 ),
120 ];
121 DEFAULT_BLOCK_ORDER.forEach((id, index) => {
122 if (order.includes(id)) return;
123 const previous = DEFAULT_BLOCK_ORDER[index - 1];
124 order.splice(previous === undefined ? 0 : order.indexOf(previous) + 1, 0, id);
125 });
126 return order;
128​
129/** Moves a block to where `target` is; an order equal to the default saves as `[]`. */
130export function moveThreadDetailsBlock(
131 order: ReadonlyArray<ThreadDetailsBlockId>,
132 id: ThreadDetailsBlockId,
133 target: ThreadDetailsBlockId,
134): ClientSettings["threadDetailsOrder"] {
135 const next = arrayMove([...order], order.indexOf(id), order.indexOf(target));
136 return isDefaultThreadDetailsOrder(next) ? [] : next;
138​
139export function isDefaultThreadDetailsOrder(order: ReadonlyArray<ThreadDetailsBlockId>) {
140 return order.join() === DEFAULT_BLOCK_ORDER.join();
142​
143/** Also clears the retired Labs flag, so Usage and Context return to off. */
144export const RESET_THREAD_DETAILS_ITEMS = {
145 threadDetailsItems: {},
146 threadDetailsOrder: [],
147 threadDetailsUsageEnabled: false,
148} satisfies Partial<ClientSettings>;