|
1 | 1 | import { useAtomValue } from "@effect/atom-react"; |
2 | | -import { AuthFilesystemReadScope, type ScopedThreadRef } from "@t3tools/contracts"; |
| 2 | +import { |
| 3 | + AuthFilesystemReadScope, |
| 4 | + type OrchestrationV2ThreadProjection, |
| 5 | + type ScopedThreadRef, |
| 6 | +} from "@t3tools/contracts"; |
3 | 7 | import { formatFilePathPosition, splitFilePathPosition } from "@t3tools/shared/fileLinks"; |
4 | 8 | import { |
5 | 9 | resolveMarkdownFileLinkTarget, |
6 | 10 | resolveThreadFilePathCandidate, |
7 | 11 | } from "@t3tools/shared/markdownLinks"; |
| 12 | +import { isAbsolutePath, resolveWorkspaceFilePath } from "@t3tools/shared/path"; |
8 | 13 | import { collectThreadFilePaths, createThreadFilePathIndex } from "@t3tools/shared/threadFilePaths"; |
9 | 14 | import { Atom } from "effect/reactivity"; |
10 | | -import { createContext, useContext, useLayoutEffect, useMemo, useRef, useState } from "react"; |
| 15 | +import { |
| 16 | + createContext, |
| 17 | + useContext, |
| 18 | + useEffect, |
| 19 | + useLayoutEffect, |
| 20 | + useMemo, |
| 21 | + useRef, |
| 22 | + useState, |
| 23 | +} from "react"; |
11 | 24 |
|
12 | 25 | import { projectEnvironment } from "../../state/projects"; |
13 | 26 | import { useEnvironmentScope, usePreparedConnection } from "../../state/session"; |
| 27 | +import { environmentThreadDetails } from "../../state/threads"; |
| 28 | +import { useAtomCommand } from "../../state/use-atom-command"; |
14 | 29 | import { useThreadVisibleTurnItems } from "../../state/use-thread-detail"; |
15 | 30 |
|
16 | 31 | /** Encode a literal selected path once before giving it back to markdown parsers. */ |
@@ -48,6 +63,50 @@ export function useThreadFileLinks(ref: ScopedThreadRef, cwd: string | null) { |
48 | 63 | const enabled = canRead && connection._tag === "Some"; |
49 | 64 | const { environmentId, threadId } = ref; |
50 | 65 | useAtomValue(projectEnvironment.fileMetadataThread(ref)); |
| 66 | + const checkpoints = useAtomValue( |
| 67 | + environmentThreadDetails.threadAtom(ref), |
| 68 | + (thread) => thread?.projection.checkpoints, |
| 69 | + ); |
| 70 | + const invalidate = useAtomCommand(projectEnvironment.invalidateFileMetadata, { |
| 71 | + reportFailure: false, |
| 72 | + }); |
| 73 | + const previous = useRef<{ |
| 74 | + environmentId: ScopedThreadRef["environmentId"]; |
| 75 | + threadId: ScopedThreadRef["threadId"]; |
| 76 | + cwd: string | null; |
| 77 | + checkpoints: OrchestrationV2ThreadProjection["checkpoints"]; |
| 78 | + } | null>(null); |
| 79 | + useEffect(() => { |
| 80 | + if (!checkpoints) { |
| 81 | + previous.current = null; |
| 82 | + return; |
| 83 | + } |
| 84 | + const before = previous.current; |
| 85 | + previous.current = { environmentId, threadId, cwd, checkpoints }; |
| 86 | + if ( |
| 87 | + !before || |
| 88 | + before.environmentId !== environmentId || |
| 89 | + before.threadId !== threadId || |
| 90 | + before.cwd !== cwd |
| 91 | + ) |
| 92 | + return; |
| 93 | + const oldById = new Map(before.checkpoints.map((checkpoint) => [checkpoint.id, checkpoint])); |
| 94 | + const paths = new Set<string>(); |
| 95 | + const addPath = (path: string) => { |
| 96 | + if (cwd !== null || isAbsolutePath(path)) |
| 97 | + paths.add(cwd === null ? path : resolveWorkspaceFilePath(path, cwd)); |
| 98 | + }; |
| 99 | + for (const checkpoint of checkpoints) { |
| 100 | + const old = oldById.get(checkpoint.id); |
| 101 | + oldById.delete(checkpoint.id); |
| 102 | + if (old?.files === checkpoint.files && old?.status === checkpoint.status) continue; |
| 103 | + for (const file of checkpoint.files) addPath(file.path); |
| 104 | + for (const file of old?.files ?? []) addPath(file.path); |
| 105 | + } |
| 106 | + for (const checkpoint of oldById.values()) |
| 107 | + for (const file of checkpoint.files) addPath(file.path); |
| 108 | + if (paths.size > 0) void invalidate({ environmentId, input: { paths: [...paths] } }); |
| 109 | + }, [environmentId, threadId, cwd, checkpoints, invalidate]); |
51 | 110 | const generation = useRef(0); |
52 | 111 | useLayoutEffect( |
53 | 112 | () => () => { |
|
0 commit comments