Skip to content

Commit 2683db9

Browse files
committed
feat(search): find threads by conversation content
1 parent 90f3913 commit 2683db9

33 files changed

Lines changed: 1241 additions & 22 deletions

‎apps/mobile/src/features/home/HomeScreen.tsx‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import {
77
type EnvironmentProject,
88
type EnvironmentThreadShell,
99
} from "@t3tools/client-runtime/state/shell";
10+
import {
11+
threadSearchMatchKey,
12+
type EnvironmentThreadSearchMatch,
13+
} from "@t3tools/client-runtime/state/thread-search";
1014
import type {
1115
EnvironmentId,
1216
SidebarProjectGroupingMode,
@@ -27,6 +31,7 @@ import type { SavedRemoteConnection } from "../../lib/connection";
2731
import { scopedProjectKey } from "../../lib/scopedEntities";
2832
import { NATIVE_LIQUID_GLASS_SUPPORTED } from "../../native/native-glass";
2933
import { mobilePreferencesAtom, updateMobilePreferencesAtom } from "../../state/preferences";
34+
import { useThreadSearch } from "../../state/queries";
3035
import { useThreadListV2Enabled } from "../threads/use-thread-list-v2-enabled";
3136
import { environmentServerConfigsAtom } from "../../state/server";
3237
import type { PendingNewTask } from "../../state/use-pending-new-tasks";
@@ -189,6 +194,27 @@ export function HomeScreen(props: HomeScreenProps) {
189194
const listRef = useRef<LegendListRef | null>(null);
190195
const insets = useSafeAreaInsets();
191196
const accentColor = useThemeColor("--color-icon-muted");
197+
const searchEnvironmentIds = useMemo(
198+
() =>
199+
props.selectedEnvironmentId === null
200+
? props.environments.map((environment) => environment.environmentId)
201+
: [props.selectedEnvironmentId],
202+
[props.environments, props.selectedEnvironmentId],
203+
);
204+
const threadSearch = useThreadSearch(searchEnvironmentIds, props.searchQuery);
205+
const threadSearchMatchByKey = useMemo(() => {
206+
const matches = new Map<string, EnvironmentThreadSearchMatch>();
207+
for (const match of threadSearch.matches) {
208+
if (match.source === "user" || match.source === "assistant") {
209+
matches.set(threadSearchMatchKey(match), match);
210+
}
211+
}
212+
return matches;
213+
}, [threadSearch.matches]);
214+
const matchedThreadKeys = useMemo(
215+
() => new Set(threadSearch.matches.map(threadSearchMatchKey)),
216+
[threadSearch.matches],
217+
);
192218
const effectiveGroupDisplayStates = useMemo(() => {
193219
const next = new Map(groupDisplayStates);
194220
if (!AsyncResult.isSuccess(preferencesResult)) {
@@ -318,6 +344,7 @@ export function HomeScreen(props: HomeScreenProps) {
318344
pendingTasks: scopedPendingTasks,
319345
environmentId: props.selectedEnvironmentId,
320346
searchQuery: props.searchQuery,
347+
matchedThreadKeys,
321348
projectSortOrder: props.projectSortOrder,
322349
threadSortOrder: props.threadSortOrder,
323350
projectGroupingMode: props.projectGroupingMode,
@@ -328,6 +355,7 @@ export function HomeScreen(props: HomeScreenProps) {
328355
props.searchQuery,
329356
props.selectedEnvironmentId,
330357
props.threadSortOrder,
358+
matchedThreadKeys,
331359
scopedPendingTasks,
332360
scopedProjects,
333361
scopedThreads,
@@ -516,6 +544,7 @@ export function HomeScreen(props: HomeScreenProps) {
516544
environmentId: props.selectedEnvironmentId,
517545
projectRefs: v2ScopedProjectGroup === null ? null : v2ScopedProjectGroup.projectRefs,
518546
searchQuery: props.searchQuery,
547+
matchedThreadKeys,
519548
changeRequestStateByKey,
520549
settlementEnvironmentIds,
521550
snoozeEnvironmentIds,
@@ -533,6 +562,7 @@ export function HomeScreen(props: HomeScreenProps) {
533562
props.searchQuery,
534563
props.selectedEnvironmentId,
535564
props.threads,
565+
matchedThreadKeys,
536566
threadListV2Enabled,
537567
v2ScopedProjectGroup,
538568
]);
@@ -629,6 +659,13 @@ export function HomeScreen(props: HomeScreenProps) {
629659
? (props.savedConnectionsById[thread.environmentId]?.environmentLabel ?? null)
630660
: null
631661
}
662+
searchMatch={threadSearchMatchByKey.get(
663+
threadSearchMatchKey({
664+
environmentId: thread.environmentId,
665+
threadId: thread.id,
666+
}),
667+
)}
668+
searchQuery={props.searchQuery}
632669
onSelectThread={props.onSelectThread}
633670
onDeleteThread={handleDeleteThread}
634671
onArchiveThread={props.onArchiveThread}
@@ -660,7 +697,9 @@ export function HomeScreen(props: HomeScreenProps) {
660697
props.savedConnectionsById,
661698
serverConfigs,
662699
settlementEnvironmentIds,
700+
threadSearchMatchByKey,
663701
v2ProjectTitleByProjectKey,
702+
props.searchQuery,
664703
],
665704
);
666705
const v2KeyExtractor = useCallback((item: ThreadListV2ListItem) => item.key, []);
@@ -675,12 +714,14 @@ export function HomeScreen(props: HomeScreenProps) {
675714
projectTitleByProjectKey: v2ProjectTitleByProjectKey,
676715
serverConfigs,
677716
savedConnectionsById: props.savedConnectionsById,
717+
threadSearchMatchByKey,
678718
}),
679719
[
680720
projectByKey,
681721
projectCwdByKey,
682722
props.savedConnectionsById,
683723
serverConfigs,
724+
threadSearchMatchByKey,
684725
v2ProjectTitleByProjectKey,
685726
],
686727
);
@@ -740,6 +781,13 @@ export function HomeScreen(props: HomeScreenProps) {
740781
null
741782
}
742783
isLast={item.isLast}
784+
searchMatch={threadSearchMatchByKey.get(
785+
threadSearchMatchKey({
786+
environmentId: thread.environmentId,
787+
threadId: thread.id,
788+
}),
789+
)}
790+
searchQuery={props.searchQuery}
743791
onArchiveThread={props.onArchiveThread}
744792
onDeleteThread={props.onDeleteThread}
745793
onSelectThread={props.onSelectThread}
@@ -770,7 +818,9 @@ export function HomeScreen(props: HomeScreenProps) {
770818
props.onNewThreadInProject,
771819
props.onSelectPendingTask,
772820
props.onSelectThread,
821+
props.searchQuery,
773822
props.savedConnectionsById,
823+
threadSearchMatchByKey,
774824
updateGroupDisplay,
775825
],
776826
);

‎apps/mobile/src/features/home/homeThreadList.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type {
22
EnvironmentProject,
33
EnvironmentThreadShell,
44
} from "@t3tools/client-runtime/state/shell";
5+
import { threadSearchMatchKey } from "@t3tools/client-runtime/state/thread-search";
56
import { EnvironmentId, ProjectId, ProviderInstanceId, ThreadId } from "@t3tools/contracts";
67
import { describe, expect, it } from "vite-plus/test";
78

@@ -661,6 +662,33 @@ describe("buildHomeThreadGroups", () => {
661662
);
662663
});
663664

665+
it("includes a thread matched by message content", () => {
666+
const environmentId = EnvironmentId.make("environment-1");
667+
const project = makeProject({
668+
environmentId,
669+
id: ProjectId.make("project-1"),
670+
title: "T3 Code",
671+
});
672+
const thread = makeThread({
673+
environmentId,
674+
id: ThreadId.make("thread-content"),
675+
projectId: project.id,
676+
title: "Unrelated title",
677+
});
678+
679+
const groups = buildGroups([project], [thread], {
680+
searchQuery: "relay reconnect",
681+
matchedThreadKeys: new Set([
682+
threadSearchMatchKey({
683+
environmentId,
684+
threadId: thread.id,
685+
}),
686+
]),
687+
});
688+
689+
expect(groups[0]?.threads.map((candidate) => candidate.id)).toEqual(["thread-content"]);
690+
});
691+
664692
it("targets quick new threads at the group member with the newest thread", () => {
665693
const laptopEnv = EnvironmentId.make("environment-laptop");
666694
const desktopEnv = EnvironmentId.make("environment-desktop");

‎apps/mobile/src/features/home/homeThreadList.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
sortThreads,
1313
toSortableTimestamp,
1414
} from "@t3tools/client-runtime/state/thread-sort";
15+
import { threadSearchMatchKey } from "@t3tools/client-runtime/state/thread-search";
1516
import type {
1617
EnvironmentId,
1718
ScopedProjectRef,
@@ -254,6 +255,7 @@ export function buildHomeThreadGroups(input: {
254255
readonly pendingTasks?: ReadonlyArray<PendingNewTask>;
255256
readonly environmentId: EnvironmentId | null;
256257
readonly searchQuery: string;
258+
readonly matchedThreadKeys?: ReadonlySet<string>;
257259
readonly projectSortOrder: HomeProjectSortOrder;
258260
readonly threadSortOrder: SidebarThreadSortOrder;
259261
readonly projectGroupingMode: SidebarProjectGroupingMode;
@@ -350,7 +352,16 @@ export function buildHomeThreadGroups(input: {
350352
group.projects.some((project) => project.title.toLocaleLowerCase().includes(query));
351353
const matchingThreads = groupMatches
352354
? group.threads
353-
: group.threads.filter((thread) => thread.title.toLocaleLowerCase().includes(query));
355+
: group.threads.filter(
356+
(thread) =>
357+
thread.title.toLocaleLowerCase().includes(query) ||
358+
input.matchedThreadKeys?.has(
359+
threadSearchMatchKey({
360+
environmentId: thread.environmentId,
361+
threadId: thread.id,
362+
}),
363+
) === true,
364+
);
354365
const matchingPendingTasks = groupMatches
355366
? group.pendingTasks
356367
: group.pendingTasks.filter((pendingTask) =>

‎apps/mobile/src/features/threads/ThreadNavigationSidebar.tsx‎

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import type {
33
EnvironmentProject,
44
EnvironmentThreadShell,
55
} from "@t3tools/client-runtime/state/shell";
6+
import {
7+
threadSearchMatchKey,
8+
type EnvironmentThreadSearchMatch,
9+
} from "@t3tools/client-runtime/state/thread-search";
610
import { LegendList } from "@legendapp/list/react-native";
711
import type { MenuAction } from "@react-native-menu/menu";
812
import { useAtomValue } from "@effect/atom-react";
@@ -24,6 +28,7 @@ import { NativeStackScreenOptions } from "../../native/StackHeader";
2428
import { scopedProjectKey, scopedThreadKey } from "../../lib/scopedEntities";
2529
import { useThemeColor } from "../../lib/useThemeColor";
2630
import { useProjects, useThreadShells } from "../../state/entities";
31+
import { useThreadSearch } from "../../state/queries";
2732
import { useThreadListV2Enabled } from "./use-thread-list-v2-enabled";
2833
import { environmentServerConfigsAtom } from "../../state/server";
2934
import { usePendingNewTasks } from "../../state/use-pending-new-tasks";
@@ -209,6 +214,27 @@ function ThreadNavigationSidebarPane(
209214
);
210215
const { options, setSelectedEnvironmentId, setProjectSortOrder, setThreadSortOrder } =
211216
useHomeListOptions(availableEnvironmentIds);
217+
const searchEnvironmentIds = useMemo(
218+
() =>
219+
options.selectedEnvironmentId === null
220+
? environments.map((environment) => environment.environmentId)
221+
: [options.selectedEnvironmentId],
222+
[environments, options.selectedEnvironmentId],
223+
);
224+
const threadSearch = useThreadSearch(searchEnvironmentIds, props.searchQuery);
225+
const threadSearchMatchByKey = useMemo(() => {
226+
const matches = new Map<string, EnvironmentThreadSearchMatch>();
227+
for (const match of threadSearch.matches) {
228+
if (match.source === "user" || match.source === "assistant") {
229+
matches.set(threadSearchMatchKey(match), match);
230+
}
231+
}
232+
return matches;
233+
}, [threadSearch.matches]);
234+
const matchedThreadKeys = useMemo(
235+
() => new Set(threadSearch.matches.map(threadSearchMatchKey)),
236+
[threadSearch.matches],
237+
);
212238
const [selectedProjectKey, setSelectedProjectKey] = useState<string | null>(null);
213239
const projectScopes = useMemo(
214240
() =>
@@ -305,11 +331,19 @@ function ThreadNavigationSidebarPane(
305331
pendingTasks: scopedPendingTasks,
306332
environmentId: options.selectedEnvironmentId,
307333
searchQuery: props.searchQuery,
334+
matchedThreadKeys,
308335
projectSortOrder: options.projectSortOrder,
309336
threadSortOrder: options.threadSortOrder,
310337
projectGroupingMode: options.projectGroupingMode,
311338
}),
312-
[options, props.searchQuery, scopedPendingTasks, scopedProjects, scopedThreads],
339+
[
340+
matchedThreadKeys,
341+
options,
342+
props.searchQuery,
343+
scopedPendingTasks,
344+
scopedProjects,
345+
scopedThreads,
346+
],
313347
);
314348
const [groupDisplayStates, setGroupDisplayStates] = useState<
315349
ReadonlyMap<string, HomeGroupDisplayState>
@@ -432,6 +466,7 @@ function ThreadNavigationSidebarPane(
432466
environmentId: options.selectedEnvironmentId,
433467
projectRefs: selectedProjectScope === null ? null : selectedProjectScope.projectRefs,
434468
searchQuery: props.searchQuery,
469+
matchedThreadKeys,
435470
changeRequestStateByKey,
436471
settlementEnvironmentIds,
437472
snoozeEnvironmentIds,
@@ -445,6 +480,7 @@ function ThreadNavigationSidebarPane(
445480
snoozeWakeTick,
446481
options.selectedEnvironmentId,
447482
props.searchQuery,
483+
matchedThreadKeys,
448484
settledVisibleCount,
449485
settlementEnvironmentIds,
450486
snoozeEnvironmentIds,
@@ -687,6 +723,7 @@ function ThreadNavigationSidebarPane(
687723
projectTitleByProjectKey,
688724
savedConnectionsById,
689725
serverConfigs,
726+
threadSearchMatchByKey,
690727
}),
691728
[
692729
props.selectedThreadKey,
@@ -695,6 +732,7 @@ function ThreadNavigationSidebarPane(
695732
projectTitleByProjectKey,
696733
savedConnectionsById,
697734
serverConfigs,
735+
threadSearchMatchByKey,
698736
],
699737
);
700738
const sidebarItemsAreEqual = useCallback(
@@ -797,6 +835,13 @@ function ThreadNavigationSidebarPane(
797835
? (savedConnectionsById[thread.environmentId]?.environmentLabel ?? null)
798836
: null
799837
}
838+
searchMatch={threadSearchMatchByKey.get(
839+
threadSearchMatchKey({
840+
environmentId: thread.environmentId,
841+
threadId: thread.id,
842+
}),
843+
)}
844+
searchQuery={props.searchQuery}
800845
pane="sidebar"
801846
selected={
802847
scopedThreadKey(thread.environmentId, thread.id) === props.selectedThreadKey
@@ -876,6 +921,13 @@ function ThreadNavigationSidebarPane(
876921
null
877922
}
878923
isLast={item.isLast}
924+
searchMatch={threadSearchMatchByKey.get(
925+
threadSearchMatchKey({
926+
environmentId: thread.environmentId,
927+
threadId: thread.id,
928+
}),
929+
)}
930+
searchQuery={props.searchQuery}
879931
selected={
880932
scopedThreadKey(thread.environmentId, thread.id) === props.selectedThreadKey
881933
}
@@ -914,10 +966,12 @@ function ThreadNavigationSidebarPane(
914966
projectCwdByKey,
915967
projectTitleByProjectKey,
916968
props.onNewThreadInProject,
969+
props.searchQuery,
917970
props.selectedThreadKey,
918971
props.width,
919972
savedConnectionsById,
920973
serverConfigs,
974+
threadSearchMatchByKey,
921975
settleThread,
922976
settlementEnvironmentIds,
923977
showMoreSettled,

0 commit comments

Comments
 (0)