@@ -11,10 +11,7 @@ import { matchComposerThreadItems } from "@t3tools/client-runtime/composerThread |
11 | 11 | import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/models"; |
12 | 12 |
|
13 | 13 | const EMPTY_THREAD_SHELLS: ReadonlyArray<EnvironmentThreadShell> = []; |
14 | | -import { |
15 | | - COMPOSER_CONTEXT_MAX_RECORDS, |
16 | | - PROVIDER_WORKSPACE_SNAPSHOT_TTL_MS, |
17 | | -} from "@t3tools/contracts"; |
| 14 | +import { COMPOSER_CONTEXT_MAX_RECORDS } from "@t3tools/contracts"; |
18 | 15 | import { formatComposerContextReference } from "@t3tools/shared/composerContextReferences"; |
19 | 16 | import { pullRequestComposerContext, threadComposerContext } from "../../lib/composerContext"; |
20 | 17 | import { uuidv4 } from "../../lib/uuid"; |
@@ -36,20 +33,20 @@ import { |
36 | 33 | scoreQueryMatch, |
37 | 34 | } from "@t3tools/shared/searchRanking"; |
38 | 35 | import { |
| 36 | + createProviderWorkspaceSnapshotRefreshAtomFamily, |
39 | 37 | dedupeProviderSkillsByName, |
40 | 38 | getProviderSkillsForSlashMenu, |
41 | 39 | getProviderSlashCommandsForSlashMenu, |
42 | 40 | isProviderSkillUserInvocable, |
43 | | - hasCompleteProviderWorkspaceSnapshot, |
44 | | - hasCurrentProviderWorkspaceSnapshot, |
45 | 41 | resolveProviderSkillsForCwd, |
46 | 42 | resolveProviderSlashCommandsForCwd, |
47 | 43 | } from "@t3tools/client-runtime/providerSkills"; |
| 44 | +import { useAtomRefresh } from "@effect/atom-react"; |
| 45 | +import { Atom } from "effect/reactivity"; |
48 | 46 | import { useEffect, useRef, useState } from "react"; |
49 | 47 |
|
50 | 48 | import type { ComposerEditorSelection } from "../../components/ComposerEditor"; |
51 | 49 | import { serverEnvironment } from "../../state/server"; |
52 | | -import { useAtomCommand } from "../../state/use-atom-command"; |
53 | 50 | import { |
54 | 51 | useComposerPathSearch, |
55 | 52 | useComposerPullRequestSearch, |
@@ -58,9 +55,14 @@ import { |
58 | 55 | import type { ComposerCommandItem } from "./ComposerCommandPopover"; |
59 | 56 | import { matchesSlashSkillQuery } from "./composerSlashSkillSearch"; |
60 | 57 |
|
61 | | -const WORKSPACE_SNAPSHOT_RETRY_COOLDOWN_MS = 10_000; |
62 | | -// A cwd that never scans, such as a deleted worktree, would otherwise retry forever. |
63 | | -const WORKSPACE_SNAPSHOT_MAX_ATTEMPTS = 3; |
| 58 | +const providerWorkspaceSnapshotRefresh = createProviderWorkspaceSnapshotRefreshAtomFamily({ |
| 59 | + providers: serverEnvironment.providersValueAtom, |
| 60 | + refreshProviders: serverEnvironment.refreshProviders, |
| 61 | +}); |
| 62 | + |
| 63 | +const idleWorkspaceSnapshotRefreshAtom: Atom.Atom<unknown> = Atom.make<void>(undefined).pipe( |
| 64 | + Atom.withLabel("mobile:provider-workspace-snapshot-refresh:idle"), |
| 65 | +); |
64 | 66 |
|
65 | 67 | function composerSelectionAtEnd(draftMessage: string): ComposerEditorSelection { |
66 | 68 | return { start: draftMessage.length, end: draftMessage.length }; |
@@ -214,9 +216,6 @@ export function useComposerCommandMenu({ |
214 | 216 | // Thread mentions rank by recency as of when the composer mounted. |
215 | 217 | const [now] = useState(() => Date.now()); |
216 | 218 | const previousOwnerKeyRef = useRef(ownerKey); |
217 | | - const onSelectionChange = (nextSelection: ComposerEditorSelection) => { |
218 | | - setSelection(nextSelection); |
219 | | - }; |
220 | 219 | useEffect(() => { |
221 | 220 | const inserted = ownerKey ? readComposerDraftSelection(ownerKey, draftMessage) : null; |
222 | 221 | if (inserted) { |
@@ -245,109 +244,21 @@ export function useComposerCommandMenu({ |
245 | 244 | const skills = selectedProviderStatus |
246 | 245 | ? resolveProviderSkillsForCwd(selectedProviderStatus, projectCwd) |
247 | 246 | : []; |
248 | | - const refreshProviders = useAtomCommand(serverEnvironment.refreshProviders, { |
249 | | - reportFailure: false, |
250 | | - }); |
251 | 247 | const selectedProviderInstanceId = selectedProviderStatus?.instanceId; |
252 | | - const hasWorkspaceSnapshot = hasCompleteProviderWorkspaceSnapshot( |
253 | | - selectedProviderStatus, |
254 | | - projectCwd, |
255 | | - ); |
256 | | - // Record client request time to enforce TTL even when the client clock runs ahead of the server. |
257 | | - const workspaceRefreshKeyRef = useRef<{ key: string; requestedAt: number } | null>(null); |
258 | | - const failedRefreshesRef = useRef<{ key: string; count: number } | null>(null); |
259 | | - const [workspaceRefreshRetry, setWorkspaceRefreshRetry] = useState<{ |
260 | | - key: string; |
261 | | - notBefore: number; |
262 | | - } | null>(null); |
263 | | - const workspaceRefreshScopeKey = |
| 248 | + const refreshWorkspaceSnapshot = useAtomRefresh( |
264 | 249 | environmentId && projectCwd && selectedProviderInstanceId |
265 | | - ? `${environmentId}:${selectedProviderInstanceId}:${projectCwd}` |
266 | | - : null; |
267 | | - const workspaceSlashCommandsPending = |
268 | | - selectedProviderStatus?.workspaceSnapshots?.some( |
269 | | - (snapshot) => snapshot.cwd === projectCwd && snapshot.slashCommandsPending === true, |
270 | | - ) ?? false; |
271 | | - useEffect(() => { |
272 | | - if ( |
273 | | - !workspaceSlashCommandsPending || |
274 | | - !workspaceRefreshRetry || |
275 | | - workspaceRefreshRetry.key !== workspaceRefreshScopeKey |
276 | | - ) |
277 | | - return; |
278 | | - const timeout = setTimeout( |
279 | | - () => { |
280 | | - setWorkspaceRefreshRetry((current) => (current === workspaceRefreshRetry ? null : current)); |
281 | | - }, |
282 | | - Math.max(0, workspaceRefreshRetry.notBefore - Date.now()), |
283 | | - ); |
284 | | - return () => clearTimeout(timeout); |
285 | | - }, [workspaceRefreshRetry, workspaceRefreshScopeKey, workspaceSlashCommandsPending]); |
286 | | - const hadWorkspaceSnapshotRef = useRef(false); |
287 | | - useEffect(() => { |
288 | | - if (hadWorkspaceSnapshotRef.current && !hasWorkspaceSnapshot) { |
289 | | - workspaceRefreshKeyRef.current = null; |
290 | | - setWorkspaceRefreshRetry(null); |
291 | | - } |
292 | | - hadWorkspaceSnapshotRef.current = hasWorkspaceSnapshot; |
293 | | - }, [hasWorkspaceSnapshot]); |
294 | | - useEffect(() => { |
295 | | - if (!environmentId || !projectCwd || !selectedProviderInstanceId) return; |
296 | | - const key = `${environmentId}:${selectedProviderInstanceId}:${projectCwd}`; |
297 | | - const now = Date.now(); |
298 | | - const lastRequest = workspaceRefreshKeyRef.current; |
299 | | - if ( |
300 | | - lastRequest?.key === key && |
301 | | - now - lastRequest.requestedAt < PROVIDER_WORKSPACE_SNAPSHOT_TTL_MS |
302 | | - ) |
303 | | - return; |
304 | | - if (hasCurrentProviderWorkspaceSnapshot(selectedProviderStatus, projectCwd, now)) { |
305 | | - // oxlint-disable-next-line react/set-state-in-effect -- A current provider workspace snapshot clears the pending refresh retry. |
306 | | - setWorkspaceRefreshRetry(null); |
307 | | - return; |
308 | | - } |
309 | | - const retry = workspaceRefreshRetry; |
310 | | - if (retry?.key === key && now < retry.notBefore) return; |
311 | | - const request = { key, requestedAt: now }; |
312 | | - workspaceRefreshKeyRef.current = request; |
313 | | - const retryLater = () => { |
314 | | - if (workspaceRefreshKeyRef.current !== request) return; |
315 | | - const failed = failedRefreshesRef.current; |
316 | | - const count = failed?.key === key ? failed.count + 1 : 1; |
317 | | - failedRefreshesRef.current = { key, count }; |
318 | | - // Keeping the request marker lets the TTL hold off the next scan. |
319 | | - if (count >= WORKSPACE_SNAPSHOT_MAX_ATTEMPTS) return; |
320 | | - workspaceRefreshKeyRef.current = null; |
321 | | - setWorkspaceRefreshRetry({ |
322 | | - key, |
323 | | - notBefore: Date.now() + WORKSPACE_SNAPSHOT_RETRY_COOLDOWN_MS, |
324 | | - }); |
325 | | - }; |
326 | | - void refreshProviders({ |
327 | | - environmentId, |
328 | | - input: { instanceId: selectedProviderInstanceId, cwd: projectCwd }, |
329 | | - }).then((result) => { |
330 | | - const refreshed = |
331 | | - result._tag === "Success" && |
332 | | - hasCompleteProviderWorkspaceSnapshot( |
333 | | - result.value.providers.find( |
334 | | - (provider) => provider.instanceId === selectedProviderInstanceId, |
335 | | - ), |
336 | | - projectCwd, |
337 | | - ); |
338 | | - if (!refreshed) retryLater(); |
339 | | - else failedRefreshesRef.current = null; |
340 | | - }, retryLater); |
341 | | - }, [ |
342 | | - // oxlint-disable-next-line react/exhaustive-effect-dependencies -- Typing in the draft re-checks whether the provider workspace snapshot needs refreshing. |
343 | | - draftMessage, |
344 | | - environmentId, |
345 | | - projectCwd, |
346 | | - refreshProviders, |
347 | | - selectedProviderInstanceId, |
348 | | - selectedProviderStatus, |
349 | | - workspaceRefreshRetry, |
350 | | - ]); |
| 250 | + ? providerWorkspaceSnapshotRefresh({ |
| 251 | + environmentId, |
| 252 | + instanceId: selectedProviderInstanceId, |
| 253 | + cwd: projectCwd, |
| 254 | + }) |
| 255 | + : idleWorkspaceSnapshotRefreshAtom, |
| 256 | + ); |
| 257 | + // Selection changes on every keystroke, which is when a stale snapshot is rechecked. |
| 258 | + const onSelectionChange = (nextSelection: ComposerEditorSelection) => { |
| 259 | + setSelection(nextSelection); |
| 260 | + refreshWorkspaceSnapshot(); |
| 261 | + }; |
351 | 262 |
|
352 | 263 | const trigger = (() => { |
353 | 264 | if (!enabled || selection.start !== selection.end) { |
|