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

useThreadFindPositionReader.ts

99 lines3.4 KB

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