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

useThreadFindPositionReader.ts

108 lines3.8 KB

1import { useLayoutEffect } from "react";
2import type { RunId } from "@t3tools/contracts";
3import type { ToolCallVisibility } from "@t3tools/contracts/settings";
4import type { TimelineEntry } from "../../session-logic";
5import {
6 timelineTurnFoldRunIdsByEntryId,
7 type MessagesTimelineRow,
8 type TimelineLatestRun,
9} from "./MessagesTimeline.logic";
10import type { ThreadFindPositionReader } from "./threadFind";
11import { attachThreadFindTimeline } from "./threadFindStore";
12import { readThreadFindPosition } from "./threadFindPosition";
13​
14/** Hands thread find how to read the find position of this timeline's viewport. */
15export function useThreadFindPositionReader({
16 threadKey,
17 navigate,
18 timelineViewportElement,
19 findFoldRunIds,
20 timelineEntries,
21 latestRun,
22 runningRunId,
23 isWorking,
24 runlessWorkActive,
25 toolCallVisibility,
26 rows,
27 contentInsetEndAdjustment,
28}: {
29 readonly threadKey: string;
30 readonly navigate: () => void;
31 readonly timelineViewportElement: HTMLDivElement | null;
32 readonly findFoldRunIds: ReadonlyMap<string, RunId> | null;
33 readonly timelineEntries: ReadonlyArray<TimelineEntry>;
34 readonly latestRun: TimelineLatestRun | null;
35 readonly runningRunId: RunId | null;
36 readonly isWorking: boolean;
37 readonly runlessWorkActive: boolean;
38 readonly toolCallVisibility: ToolCallVisibility;
39 readonly rows: MessagesTimelineRow[];
40 readonly contentInsetEndAdjustment: number;
41}) {
42 // Layout effect so a find opened right after a thread switch reads this viewport.
43 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- attaches the timeline viewport's position reader to the thread find registry
44 useLayoutEffect(() => {
45 if (!timelineViewportElement) return;
46 const read: ThreadFindPositionReader = (query) => {
47 const foldRunIds =
48 findFoldRunIds ??
49 timelineTurnFoldRunIdsByEntryId({
50 timelineEntries,
51 latestRun,
52 runningRunId,
53 isWorking,
54 runlessWorkActive,
55 toolCallVisibility,
56 });
57 const entryById = new Map(timelineEntries.map((entry) => [entry.id, entry]));
58 return readThreadFindPosition(
59 timelineViewportElement,
60 query,
61 (rowId) => {
62 const rowIndex = rows.findIndex((row) => row.id === rowId);
63 for (const row of rows.slice(Math.max(0, rowIndex))) {
64 if (row.kind !== "turn-fold" && row.kind !== "attempt-fold") {
65 const entry = entryById.get(row.kind === "assistant-meta" ? row.message.id : row.id);
66 if (entry && (entry.kind === "message" || entry.kind === "proposed-plan"))
67 return entry.id;
68 continue;
69 }
70 const entry = timelineEntries.find((entry) => {
71 const runId =
72 row.kind === "turn-fold"
73 ? foldRunIds.get(entry.id)
74 : entry.kind === "message"
75 ? entry.message.runId
76 : entry.kind === "proposed-plan"
77 ? entry.proposedPlan.runId
78 : null;
79 return (
80 runId === row.runId &&
81 (entry.kind !== "message" || entry.message.role === "assistant") &&
82 (row.kind !== "attempt-fold" || entry.attempt?.id === row.attemptId)
83 );
84 });
85 if (entry && (entry.kind === "message" || entry.kind === "proposed-plan"))
86 return entry.id;
87 }
88 return undefined;
89 },
90 contentInsetEndAdjustment,
91 );
92 };
93 return attachThreadFindTimeline({ threadKey, readPosition: read, navigate });
94 }, [
95 contentInsetEndAdjustment,
96 findFoldRunIds,
97 isWorking,
98 navigate,
99 threadKey,
100 latestRun,
101 rows,
102 runlessWorkActive,
103 runningRunId,
104 timelineEntries,
105 timelineViewportElement,
106 toolCallVisibility,
107 ]);