@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 222 lines 7.58 KB
Raw
1 import { useAtomValue } from "@effect/atom-react";
2
3 import type { EnvironmentId, ThreadId } from "@t3tools/contracts";
4 import { Atom } from "effect/reactivity";
5
6 import { scopedThreadKey } from "../../lib/scopedEntities";
7 import { appAtomRegistry } from "../../state/atom-registry";
8 import { clearReviewCommentTarget } from "./reviewCommentSelection";
9 import { m, type MessageResolver } from "../../i18n/messages";
10 import { buildReviewParsedDiff, type ReviewParsedDiff } from "./reviewModel";
11
12 const EMPTY_REVIEW_SECTION_FILE_IDS = Object.freeze<
13 Readonly<Record<string, ReadonlyArray<string> | undefined>>
14 >({});
15 const EMPTY_REVIEW_SELECTED_SECTION_ID_ATOM = Atom.make<string | null>(null).pipe(
16 Atom.keepAlive,
17 Atom.withLabel("mobile:review:selected-section-id:null"),
18 );
19 const EMPTY_REVIEW_SECTION_FILE_IDS_ATOM = Atom.make(EMPTY_REVIEW_SECTION_FILE_IDS).pipe(
20 Atom.keepAlive,
21 Atom.withLabel("mobile:review:section-file-ids:null"),
22 );
23
24 const reviewSelectedSectionIdByThreadKeyAtom = Atom.family((threadKey: string) =>
25 Atom.make<string | null>(null).pipe(
26 Atom.keepAlive,
27 Atom.withLabel(`mobile:review:selected-section-id:${threadKey}`),
28 ),
29 );
30
31 const reviewExpandedFileIdsByThreadKeyAtom = Atom.family((threadKey: string) =>
32 Atom.make(EMPTY_REVIEW_SECTION_FILE_IDS).pipe(
33 Atom.keepAlive,
34 Atom.withLabel(`mobile:review:expanded-file-ids:${threadKey}`),
35 ),
36 );
37
38 const reviewRevealedLargeFileIdsByThreadKeyAtom = Atom.family((threadKey: string) =>
39 Atom.make(EMPTY_REVIEW_SECTION_FILE_IDS).pipe(
40 Atom.keepAlive,
41 Atom.withLabel(`mobile:review:revealed-large-file-ids:${threadKey}`),
42 ),
43 );
44
45 const reviewViewedFileIdsByThreadKeyAtom = Atom.family((threadKey: string) =>
46 Atom.make(EMPTY_REVIEW_SECTION_FILE_IDS).pipe(
47 Atom.keepAlive,
48 Atom.withLabel(`mobile:review:viewed-file-ids:${threadKey}`),
49 ),
50 );
51
52 export const MAX_CACHED_REVIEW_DIFFS = 8;
53 // This bounds source string length, not the parsed or native heap size.
54 export const MAX_CACHED_REVIEW_SOURCE_CHARACTERS = 4 * 1024 * 1024;
55
56 interface CachedReviewParsedDiff {
57 readonly diff: string | null;
58 readonly locale: string;
59 readonly parsed: ReviewParsedDiff;
60 readonly sourceCharacterCount: number;
61 }
62
63 const reviewParsedDiffCacheAtom = Atom.make(() => ({
64 entries: new Map<string, CachedReviewParsedDiff>(),
65 sourceCharacterCount: 0,
66 })).pipe(Atom.keepAlive, Atom.withLabel("mobile:review:parsed-diffs"));
67
68 export interface ReviewCacheForThread {
69 readonly threadKey: string | null;
70 readonly selectedSectionId: string | null;
71 readonly expandedFileIdsBySection: Readonly<Record<string, ReadonlyArray<string> | undefined>>;
72 readonly revealedLargeFileIdsBySection: Readonly<
73 Record<string, ReadonlyArray<string> | undefined>
74 >;
75 readonly viewedFileIdsBySection: Readonly<Record<string, ReadonlyArray<string> | undefined>>;
76 }
77
78 function buildThreadKey(input: {
79 readonly environmentId?: EnvironmentId;
80 readonly threadId?: ThreadId;
81 }): string | null {
82 return input.environmentId && input.threadId
83 ? scopedThreadKey(input.environmentId, input.threadId)
84 : null;
85 }
86
87 function buildSectionCacheKey(threadKey: string, sectionId: string): string {
88 return `${threadKey}:${sectionId}`;
89 }
90
91 export function useReviewCacheForThread(input: {
92 readonly environmentId?: EnvironmentId;
93 readonly threadId?: ThreadId;
94 }): ReviewCacheForThread {
95 const threadKey = buildThreadKey(input);
96 const selectedSectionId = useAtomValue(
97 threadKey
98 ? reviewSelectedSectionIdByThreadKeyAtom(threadKey)
99 : EMPTY_REVIEW_SELECTED_SECTION_ID_ATOM,
100 );
101 const expandedFileIdsBySection = useAtomValue(
102 threadKey
103 ? reviewExpandedFileIdsByThreadKeyAtom(threadKey)
104 : EMPTY_REVIEW_SECTION_FILE_IDS_ATOM,
105 );
106 const revealedLargeFileIdsBySection = useAtomValue(
107 threadKey
108 ? reviewRevealedLargeFileIdsByThreadKeyAtom(threadKey)
109 : EMPTY_REVIEW_SECTION_FILE_IDS_ATOM,
110 );
111 const viewedFileIdsBySection = useAtomValue(
112 threadKey ? reviewViewedFileIdsByThreadKeyAtom(threadKey) : EMPTY_REVIEW_SECTION_FILE_IDS_ATOM,
113 );
114
115 return {
116 threadKey,
117 selectedSectionId,
118 expandedFileIdsBySection,
119 revealedLargeFileIdsBySection,
120 viewedFileIdsBySection,
121 };
122 }
123
124 export function setReviewSelectedSectionId(threadKey: string, sectionId: string | null): void {
125 // A line selection belongs to the section it was made in; switching away drops it, so
126 // switching back does not bring back a range over lines that may have changed.
127 clearReviewCommentTarget();
128 appAtomRegistry.set(reviewSelectedSectionIdByThreadKeyAtom(threadKey), sectionId);
129 }
130
131 export function updateReviewExpandedFileIds(
132 threadKey: string,
133 sectionId: string,
134 update: (current: ReadonlyArray<string> | undefined) => ReadonlyArray<string> | undefined,
135 ): void {
136 const atom = reviewExpandedFileIdsByThreadKeyAtom(threadKey);
137 const current = appAtomRegistry.get(atom);
138 const nextValue = update(current[sectionId]);
139 appAtomRegistry.set(atom, {
140 ...current,
141 [sectionId]: nextValue,
142 });
143 }
144
145 export function updateReviewViewedFileIds(
146 threadKey: string,
147 sectionId: string,
148 update: (current: ReadonlyArray<string> | undefined) => ReadonlyArray<string> | undefined,
149 ): void {
150 const atom = reviewViewedFileIdsByThreadKeyAtom(threadKey);
151 const current = appAtomRegistry.get(atom);
152 const nextValue = update(current[sectionId]);
153 appAtomRegistry.set(atom, {
154 ...current,
155 [sectionId]: nextValue,
156 });
157 }
158
159 export function getReviewParsedDiffSourceCharacterCount(input: {
160 readonly threadKey: string;
161 readonly sectionId: string;
162 readonly diff: string | null;
163 }): number {
164 const sourceCharacterCount = input.diff?.length ?? 0;
165 const cache = appAtomRegistry.get(reviewParsedDiffCacheAtom);
166 const cached = cache.entries.get(buildSectionCacheKey(input.threadKey, input.sectionId));
167 return cached && cached.diff === (input.diff?.trim() ?? null)
168 ? Math.max(sourceCharacterCount, cached.sourceCharacterCount)
169 : sourceCharacterCount;
170 }
171
172 export function getCachedReviewParsedDiff(input: {
173 readonly threadKey: string | null;
174 readonly sectionId: string | null;
175 readonly diff: string | null | undefined;
176 readonly resolve?: MessageResolver;
177 readonly locale: string;
178 }): ReviewParsedDiff {
179 const resolve = input.resolve ?? m;
180 if (!input.threadKey || !input.sectionId) {
181 return buildReviewParsedDiff(input.diff, input.sectionId ?? "mobile-review", resolve);
182 }
183
184 const cacheKey = buildSectionCacheKey(input.threadKey, input.sectionId);
185 const normalizedDiff = input.diff?.trim() ?? null;
186 const cache = appAtomRegistry.get(reviewParsedDiffCacheAtom);
187 const cached = cache.entries.get(cacheKey);
188 if (cached && cached.diff === normalizedDiff && cached.locale === input.locale) {
189 cache.entries.delete(cacheKey);
190 cache.entries.set(cacheKey, cached);
191 return cached.parsed;
192 }
193
194 const parsed = buildReviewParsedDiff(input.diff, input.sectionId, resolve);
195 if (cached) {
196 cache.entries.delete(cacheKey);
197 cache.sourceCharacterCount -= cached.sourceCharacterCount;
198 }
199 const sourceCharacterCount = input.diff?.length ?? 0;
200 if (sourceCharacterCount > MAX_CACHED_REVIEW_SOURCE_CHARACTERS) {
201 return parsed;
202 }
203
204 for (const [oldestKey, oldest] of cache.entries) {
205 if (
206 cache.entries.size < MAX_CACHED_REVIEW_DIFFS &&
207 cache.sourceCharacterCount + sourceCharacterCount <= MAX_CACHED_REVIEW_SOURCE_CHARACTERS
208 ) {
209 break;
210 }
211 cache.entries.delete(oldestKey);
212 cache.sourceCharacterCount -= oldest.sourceCharacterCount;
213 }
214 cache.entries.set(cacheKey, {
215 diff: normalizedDiff,
216 locale: input.locale,
217 parsed,
218 sourceCharacterCount,
219 });
220 cache.sourceCharacterCount += sourceCharacterCount;
221 return parsed;
222 }