Skip to content

Commit 4b71a2a

Browse files
authored
feat(search): find threads by conversation content (pingdotgg#4959)
1 parent 197d348 commit 4b71a2a

34 files changed

Lines changed: 1345 additions & 72 deletions

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

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,28 +25,30 @@ export function HomeRouteScreen() {
2525
const { layout } = useAdaptiveWorkspaceLayout();
2626
const projects = useProjects();
2727
const threads = useThreadShells();
28-
const { state: catalogState } = useWorkspaceState();
28+
const { environments: workspaceEnvironments, state: catalogState } = useWorkspaceState();
2929
const { savedConnectionsById } = useSavedRemoteConnections();
3030
const navigation = useNavigation();
3131
const [searchQuery, setSearchQuery] = useState("");
3232
const { archiveThread, confirmDeleteThread, settleThread, unsettleThread } =
3333
useThreadListActions();
3434
const pendingTasks = usePendingNewTasks();
3535
const { openPendingTask, confirmDeletePendingTask } = usePendingTaskListActions();
36-
const environments = useMemo(
37-
() =>
38-
Arr.sort(
39-
Object.values(savedConnectionsById).map((connection) => ({
40-
environmentId: connection.environmentId,
41-
label: connection.environmentLabel,
42-
})),
43-
Order.mapInput(
44-
Order.String,
45-
(environment: { readonly label: string }) => environment.label,
46-
),
36+
const environments = useMemo(() => {
37+
const connectionStateByEnvironmentId = new Map(
38+
workspaceEnvironments.map(
39+
(environment) => [environment.environmentId, environment.connectionState] as const,
4740
),
48-
[savedConnectionsById],
49-
);
41+
);
42+
return Arr.sort(
43+
Object.values(savedConnectionsById).map((connection) => ({
44+
environmentId: connection.environmentId,
45+
label: connection.environmentLabel,
46+
connectionState:
47+
connectionStateByEnvironmentId.get(connection.environmentId) ?? "available",
48+
})),
49+
Order.mapInput(Order.String, (environment: { readonly label: string }) => environment.label),
50+
);
51+
}, [savedConnectionsById, workspaceEnvironments]);
5052
const availableEnvironmentIds = useMemo(
5153
() => new Set(environments.map((environment) => environment.environmentId)),
5254
[environments],

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

Lines changed: 97 additions & 27 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,
@@ -22,11 +26,12 @@ import { useThemeColor } from "../../lib/useThemeColor";
2226

2327
import { AppText as Text } from "../../components/AppText";
2428
import { EmptyState } from "../../components/EmptyState";
25-
import type { WorkspaceState } from "../../state/workspaceModel";
29+
import type { WorkspaceEnvironment, WorkspaceState } from "../../state/workspaceModel";
2630
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";
@@ -72,7 +77,9 @@ interface HomeScreenProps {
7277
readonly pendingTasks: ReadonlyArray<PendingNewTask>;
7378
readonly catalogState: WorkspaceState;
7479
readonly savedConnectionsById: Readonly<Record<string, SavedRemoteConnection>>;
75-
readonly environments: ReadonlyArray<HomeListFilterMenuEnvironment>;
80+
readonly environments: ReadonlyArray<
81+
HomeListFilterMenuEnvironment & Pick<WorkspaceEnvironment, "connectionState">
82+
>;
7683
readonly searchQuery: string;
7784
readonly selectedEnvironmentId: EnvironmentId | null;
7885
readonly selectedProjectKey: string | null;
@@ -189,6 +196,35 @@ export function HomeScreen(props: HomeScreenProps) {
189196
const listRef = useRef<LegendListRef | null>(null);
190197
const insets = useSafeAreaInsets();
191198
const accentColor = useThemeColor("--color-icon-muted");
199+
const searchEnvironmentIds = useMemo(
200+
() =>
201+
props.selectedEnvironmentId === null
202+
? props.environments
203+
.filter((environment) => environment.connectionState === "connected")
204+
.map((environment) => environment.environmentId)
205+
: props.environments.some(
206+
(environment) =>
207+
environment.environmentId === props.selectedEnvironmentId &&
208+
environment.connectionState === "connected",
209+
)
210+
? [props.selectedEnvironmentId]
211+
: [],
212+
[props.environments, props.selectedEnvironmentId],
213+
);
214+
const threadSearch = useThreadSearch(searchEnvironmentIds, props.searchQuery);
215+
const threadSearchMatchByKey = useMemo(() => {
216+
const matches = new Map<string, EnvironmentThreadSearchMatch>();
217+
for (const match of threadSearch.matches) {
218+
if (match.source === "user" || match.source === "assistant") {
219+
matches.set(threadSearchMatchKey(match), match);
220+
}
221+
}
222+
return matches;
223+
}, [threadSearch.matches]);
224+
const matchedThreadKeys = useMemo(
225+
() => new Set(threadSearch.matches.map(threadSearchMatchKey)),
226+
[threadSearch.matches],
227+
);
192228
const effectiveGroupDisplayStates = useMemo(() => {
193229
const next = new Map(groupDisplayStates);
194230
if (!AsyncResult.isSuccess(preferencesResult)) {
@@ -318,6 +354,7 @@ export function HomeScreen(props: HomeScreenProps) {
318354
pendingTasks: scopedPendingTasks,
319355
environmentId: props.selectedEnvironmentId,
320356
searchQuery: props.searchQuery,
357+
matchedThreadKeys,
321358
projectSortOrder: props.projectSortOrder,
322359
threadSortOrder: props.threadSortOrder,
323360
projectGroupingMode: props.projectGroupingMode,
@@ -328,6 +365,7 @@ export function HomeScreen(props: HomeScreenProps) {
328365
props.searchQuery,
329366
props.selectedEnvironmentId,
330367
props.threadSortOrder,
368+
matchedThreadKeys,
331369
scopedPendingTasks,
332370
scopedProjects,
333371
scopedThreads,
@@ -516,6 +554,7 @@ export function HomeScreen(props: HomeScreenProps) {
516554
environmentId: props.selectedEnvironmentId,
517555
projectRefs: v2ScopedProjectGroup === null ? null : v2ScopedProjectGroup.projectRefs,
518556
searchQuery: props.searchQuery,
557+
matchedThreadKeys,
519558
changeRequestStateByKey,
520559
settlementEnvironmentIds,
521560
snoozeEnvironmentIds,
@@ -533,6 +572,7 @@ export function HomeScreen(props: HomeScreenProps) {
533572
props.searchQuery,
534573
props.selectedEnvironmentId,
535574
props.threads,
575+
matchedThreadKeys,
536576
threadListV2Enabled,
537577
v2ScopedProjectGroup,
538578
]);
@@ -629,6 +669,13 @@ export function HomeScreen(props: HomeScreenProps) {
629669
? (props.savedConnectionsById[thread.environmentId]?.environmentLabel ?? null)
630670
: null
631671
}
672+
searchMatch={threadSearchMatchByKey.get(
673+
threadSearchMatchKey({
674+
environmentId: thread.environmentId,
675+
threadId: thread.id,
676+
}),
677+
)}
678+
searchQuery={props.searchQuery}
632679
onSelectThread={props.onSelectThread}
633680
onDeleteThread={handleDeleteThread}
634681
onArchiveThread={props.onArchiveThread}
@@ -660,7 +707,9 @@ export function HomeScreen(props: HomeScreenProps) {
660707
props.savedConnectionsById,
661708
serverConfigs,
662709
settlementEnvironmentIds,
710+
threadSearchMatchByKey,
663711
v2ProjectTitleByProjectKey,
712+
props.searchQuery,
664713
],
665714
);
666715
const v2KeyExtractor = useCallback((item: ThreadListV2ListItem) => item.key, []);
@@ -675,19 +724,28 @@ export function HomeScreen(props: HomeScreenProps) {
675724
projectTitleByProjectKey: v2ProjectTitleByProjectKey,
676725
serverConfigs,
677726
savedConnectionsById: props.savedConnectionsById,
727+
searchQuery: props.searchQuery,
728+
threadSearchMatchByKey,
678729
}),
679730
[
680731
projectByKey,
681732
projectCwdByKey,
733+
props.searchQuery,
682734
props.savedConnectionsById,
683735
serverConfigs,
736+
threadSearchMatchByKey,
684737
v2ProjectTitleByProjectKey,
685738
],
686739
);
687740

688741
const extraData = useMemo(
689-
() => ({ savedConnectionsById: props.savedConnectionsById, projectCwdByKey }),
690-
[props.savedConnectionsById, projectCwdByKey],
742+
() => ({
743+
projectCwdByKey,
744+
savedConnectionsById: props.savedConnectionsById,
745+
searchQuery: props.searchQuery,
746+
threadSearchMatchByKey,
747+
}),
748+
[projectCwdByKey, props.savedConnectionsById, props.searchQuery, threadSearchMatchByKey],
691749
);
692750

693751
const renderItem = useCallback(
@@ -740,6 +798,13 @@ export function HomeScreen(props: HomeScreenProps) {
740798
null
741799
}
742800
isLast={item.isLast}
801+
searchMatch={threadSearchMatchByKey.get(
802+
threadSearchMatchKey({
803+
environmentId: thread.environmentId,
804+
threadId: thread.id,
805+
}),
806+
)}
807+
searchQuery={props.searchQuery}
743808
onArchiveThread={props.onArchiveThread}
744809
onDeleteThread={props.onDeleteThread}
745810
onSelectThread={props.onSelectThread}
@@ -770,7 +835,9 @@ export function HomeScreen(props: HomeScreenProps) {
770835
props.onNewThreadInProject,
771836
props.onSelectPendingTask,
772837
props.onSelectThread,
838+
props.searchQuery,
773839
props.savedConnectionsById,
840+
threadSearchMatchByKey,
774841
updateGroupDisplay,
775842
],
776843
);
@@ -863,7 +930,7 @@ export function HomeScreen(props: HomeScreenProps) {
863930
const v2ListHeader = listHeader;
864931

865932
const listEmpty = !hasResults ? (
866-
hasSearchQuery ? (
933+
hasSearchQuery && threadSearch.isPending ? null : hasSearchQuery ? (
867934
<EmptyState title="No results" detail={`No threads matching "${props.searchQuery}".`} />
868935
) : selectedProjectScope !== null ? (
869936
<EmptyState
@@ -886,31 +953,34 @@ export function HomeScreen(props: HomeScreenProps) {
886953
// threads yet" over an inbox that is merely all-snoozed reads as data
887954
// loss.
888955
const v2SnoozedCount = threadListV2Layout.snoozedCount;
889-
const v2ListEmpty = hasSearchQuery ? (
890-
v2SnoozedCount > 0 ? (
891-
// The snoozed threads already passed this search filter: "No
892-
// results" would claim nothing matched when matches are merely
893-
// parked.
956+
const v2ListEmpty =
957+
hasSearchQuery && threadSearch.isPending && v2SnoozedCount === 0 ? null : hasSearchQuery ? (
958+
v2SnoozedCount > 0 ? (
959+
// The snoozed threads already passed this search filter: "No
960+
// results" would claim nothing matched when matches are merely
961+
// parked.
962+
<EmptyState
963+
title={
964+
v2SnoozedCount === 1 ? "1 matching thread snoozed" : `All matching threads snoozed`
965+
}
966+
detail={`Threads matching "${props.searchQuery}" are snoozed and return when their wake time passes.`}
967+
/>
968+
) : (
969+
<EmptyState title="No results" detail={`No threads matching "${props.searchQuery}".`} />
970+
)
971+
) : v2SnoozedCount > 0 ? (
972+
<EmptyState
973+
title={v2SnoozedCount === 1 ? "1 thread snoozed" : `${v2SnoozedCount} threads snoozed`}
974+
detail="Snoozed threads return when their wake time passes."
975+
/>
976+
) : v2ScopedProjectGroup !== null ? (
894977
<EmptyState
895-
title={v2SnoozedCount === 1 ? "1 matching thread snoozed" : `All matching threads snoozed`}
896-
detail={`Threads matching "${props.searchQuery}" are snoozed and return when their wake time passes.`}
978+
title={`No threads in ${v2ScopedProjectGroup.title}`}
979+
detail="Choose another project or create a new task."
897980
/>
898981
) : (
899-
<EmptyState title="No results" detail={`No threads matching "${props.searchQuery}".`} />
900-
)
901-
) : v2SnoozedCount > 0 ? (
902-
<EmptyState
903-
title={v2SnoozedCount === 1 ? "1 thread snoozed" : `${v2SnoozedCount} threads snoozed`}
904-
detail="Snoozed threads return when their wake time passes."
905-
/>
906-
) : v2ScopedProjectGroup !== null ? (
907-
<EmptyState
908-
title={`No threads in ${v2ScopedProjectGroup.title}`}
909-
detail="Choose another project or create a new task."
910-
/>
911-
) : (
912-
listEmpty
913-
);
982+
listEmpty
983+
);
914984

915985
if (threadListV2Enabled) {
916986
return (

‎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) =>

0 commit comments

Comments
 (0)