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

CommandPaletteThreadItems.tsx

150 lines5.9 KB

1import { scopeThreadRef } from "@t3tools/client-runtime/environment";
2import { threadSearchMatchKey } from "@t3tools/client-runtime/state/thread-search";
3import type { EnvironmentId, ThreadId } from "@t3tools/contracts";
4import { useAtomValue } from "@effect/atom-react";
5import { useNavigate } from "@tanstack/react-router";
6import { MessageSquareIcon } from "lucide-react";
7​
8import { useClientSettings } from "../hooks/useSettings";
9import {
10 applyProviderInstanceSettings,
11 deriveProviderInstanceEntries,
12 type ProviderInstanceEntry,
13} from "../providerInstances";
14import { useThreadShells } from "../state/entities";
15import {
16 useEnvironmentProviders,
17 useEnvironments,
18 usePrimaryEnvironmentId,
19} from "../state/environments";
20import { useThreadSearch } from "../state/queries";
21import { primaryServerProvidersAtom } from "../state/server";
22import { buildThreadRouteParams } from "../threadRoutes";
23import { buildThreadActionItems, ITEM_ICON_CLASS } from "./CommandPalette.logic";
24import { useEnvironmentLookups, useProjectLookups } from "./CommandPaletteLookups";
25import { ThreadCommandSubtitle } from "./ThreadCommandSubtitle";
26import {
27 ThreadRowLeadingStatus,
28 ThreadRowTrailingStatus,
29 ThreadSearchPullRequestNumber,
30} from "./ThreadStatusIndicators";
31​
32function useProviderEntryByEnvironmentAndInstanceId() {
33 const { environments } = useEnvironments();
34 const primaryEnvironmentId = usePrimaryEnvironmentId();
35 const providers = useAtomValue(primaryServerProvidersAtom);
36 const providersByEnvironment = useEnvironmentProviders();
37 return new Map<string, ProviderInstanceEntry>(
38 environments.flatMap((environment) => {
39 const serverConfig = environment.serverConfig;
40 const derived = deriveProviderInstanceEntries(
41 providersByEnvironment.get(environment.environmentId) ??
42 (environment.environmentId === primaryEnvironmentId ? providers : []),
43 );
44 const entries = serverConfig
45 ? applyProviderInstanceSettings(derived, serverConfig.settings, null)
46 : derived;
47 return entries.map(
48 (entry) => [`${environment.environmentId}:${entry.instanceId}`, entry] as const,
49 );
50 }),
51 );
53​
54function useThreadSearchWithContentMatches(query: string) {
55 const { environments } = useEnvironments();
56 const environmentIds: ReadonlyArray<EnvironmentId> = environments
57 .filter((environment) => environment.connection.phase === "connected")
58 .map((environment) => environment.environmentId);
59 const threadSearch = useThreadSearch(environmentIds, query);
60 const contentMatchByKey = new Map(
61 threadSearch.matches.flatMap((match) =>
62 match.source === "user" || match.source === "assistant"
63 ? [[threadSearchMatchKey(match), match] as const]
64 : [],
65 ),
66 );
67 return { threadSearch, contentMatchByKey };
69​
70/** Every thread as a palette item, with message matches for a non-empty `threadSearchQuery`. */
71export function useCommandPaletteThreadItems(input: {
72 readonly activeThreadId: ThreadId | undefined;
73 readonly threadSearchQuery: string;
74}) {
75 const { activeThreadId, threadSearchQuery } = input;
76 const navigate = useNavigate();
77 const threads = useThreadShells();
78 const sidebarThreadSortOrder = useClientSettings((settings) => settings.sidebarThreadSortOrder);
79 const { projectEnvironmentLocationById } = useEnvironmentLookups();
80 const providerEntryByEnvironmentAndInstanceId = useProviderEntryByEnvironmentAndInstanceId();
81 const { threadSearch, contentMatchByKey } = useThreadSearchWithContentMatches(threadSearchQuery);
82 const { projectByKey, projectTitleById } = useProjectLookups();
83​
84 const threadItems = buildThreadActionItems({
85 threads,
86 ...(activeThreadId ? { activeThreadId } : {}),
87 projectTitleById,
88 sortOrder: sidebarThreadSortOrder,
89 icon: <MessageSquareIcon className={ITEM_ICON_CLASS} />,
90 renderLeadingContent: (thread) => <ThreadRowLeadingStatus thread={thread} />,
91 renderTrailingContent: (thread) => (
92 <>
93 <ThreadSearchPullRequestNumber
94 thread={thread}
95 query={threadSearchQuery}
96 settled={thread.settledOverride === "settled"}
97 />
98 <ThreadRowTrailingStatus thread={thread} />
99 </>
100 ),
101 renderDescription: (thread, { projectTitle }) => {
102 const modelInstanceId =
103 thread.runtime?.providerInstanceId ?? thread.modelSelection.instanceId;
104 const providerEntry =
105 providerEntryByEnvironmentAndInstanceId.get(`${thread.environmentId}:${modelInstanceId}`) ??
106 null;
107 return (
108 <ThreadCommandSubtitle
109 project={projectByKey.get(`${thread.environmentId}:${thread.projectId}`) ?? null}
110 projectTitle={projectTitle ?? null}
111 environmentLabel={
112 projectEnvironmentLocationById.get(thread.environmentId)?.label ?? "Remote"
113 }
114 branch={thread.branch}
115 worktreePath={thread.worktreePath}
116 isCurrent={thread.id === activeThreadId}
117 driverKind={providerEntry?.driverKind ?? null}
118 providerDisplayName={
119 thread.runtime?.providerName ?? providerEntry?.displayName ?? modelInstanceId
120 }
121 acpRegistryAgentId={providerEntry?.acpRegistryAgentId}
122 acpRegistryIconUrl={providerEntry?.acpRegistryIconUrl}
123 />
124 );
125 },
126 getContentMatch: (thread) => {
127 const match = contentMatchByKey.get(
128 threadSearchMatchKey({
129 environmentId: thread.environmentId,
130 threadId: thread.id,
131 }),
132 );
133 return match && (match.source === "user" || match.source === "assistant")
134 ? {
135 source: match.source,
136 snippet: match.snippet,
137 query: threadSearch.query,
138 }
139 : undefined;
140 },
141 runThread: async (thread) => {
142 await navigate({
143 to: "/$environmentId/$threadId",
144 params: buildThreadRouteParams(scopeThreadRef(thread.environmentId, thread.id)),
145 });
146 },
147 });
148​
149 return { threadItems, isSearchPending: threadSearch.isPending };