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
| 1 | import type { EnvironmentId, OrchestrationV2Run, RunId } from "@t3tools/contracts"; |
| 2 | import { useSyncExternalStore } from "react"; |
| 3 | |
| 4 | import type { Thread } from "../../types"; |
| 5 | import { useMediaQuery } from "../../hooks/useMediaQuery"; |
| 6 | import { useServerConfigs } from "../../state/entities"; |
| 7 | import { orchestrationEnvironment } from "../../state/orchestration"; |
| 8 | import { useEnvironmentQuery } from "../../state/query"; |
| 9 | import { announcedRun } from "@t3tools/client-runtime/state/thread-activity-announcement"; |
| 10 | |
| 11 | // Dismissed suggestions, keyed by environment and run, shared by every mounted composer and kept |
| 12 | // across remounts. Run ids derive from thread ids, which can repeat across environments. |
| 13 | let dismissedKeys: ReadonlySet<string> = new Set(); |
| 14 | const listeners = new Set<() => void>(); |
| 15 | const subscribeDismissed = (listener: () => void) => { |
| 16 | listeners.add(listener); |
| 17 | return () => listeners.delete(listener); |
| 18 | }; |
| 19 | const readDismissed = () => dismissedKeys; |
| 20 | const dismissKey = (environmentId: EnvironmentId, runId: RunId) => `${environmentId}\u0000${runId}`; |
| 21 | |
| 22 | /** |
| 23 | * The suggestion while the run that produced it is the thread's latest completed run. A message |
| 24 | * promoted to Steer leaves its queued run cancelled before it started, still the latest run, so |
| 25 | * the comparison skips to the last run that ran. Detail can lag the shell, so it skips only once |
| 26 | * the runs include that cancelled run. |
| 27 | */ |
| 28 | export function offeredPromptSuggestion<Suggestion extends { readonly runId: RunId }>( |
| 29 | suggestion: Suggestion | null | undefined, |
| 30 | latestRun: Thread["latestRun"] | undefined, |
| 31 | runs: ReadonlyArray<OrchestrationV2Run>, |
| 32 | ): Suggestion | null { |
| 33 | const run = announcedRun(latestRun ?? null, runs); |
| 34 | const skippedUnseenRun = |
| 35 | latestRun != null && |
| 36 | run?.runId !== latestRun.runId && |
| 37 | !runs.some( |
| 38 | (candidate) => |
| 39 | candidate.id === latestRun.runId && |
| 40 | candidate.status === "cancelled" && |
| 41 | candidate.startedAt === null, |
| 42 | ); |
| 43 | return suggestion && |
| 44 | !skippedUnseenRun && |
| 45 | run?.runId === suggestion.runId && |
| 46 | run.status === "completed" |
| 47 | ? suggestion |
| 48 | : null; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * The provider's predicted next prompt, while the run that produced it is the thread's latest |
| 53 | * completed run and Escape has not dismissed it. |
| 54 | */ |
| 55 | export function usePromptSuggestion( |
| 56 | environmentId: EnvironmentId, |
| 57 | thread: Thread | undefined, |
| 58 | runs: ReadonlyArray<OrchestrationV2Run>, |
| 59 | ): { readonly suggestion: string | null; readonly dismiss: () => void } { |
| 60 | // Older servers decode the missing setting as false, so true also proves the method exists. |
| 61 | const enabled = useServerConfigs().get(environmentId)?.settings.promptSuggestionsEnabled === true; |
| 62 | // Tab is the only way to accept one, so skip devices with no mouse or trackpad at all. |
| 63 | const hasFinePointer = useMediaQuery("(any-pointer: fine)"); |
| 64 | const { data } = useEnvironmentQuery( |
| 65 | // A local draft has no run, and so no suggestion, until its first send. |
| 66 | enabled && hasFinePointer && thread?.latestRun |
| 67 | ? orchestrationEnvironment.v2.promptSuggestion({ |
| 68 | environmentId, |
| 69 | input: { threadId: thread.id }, |
| 70 | }) |
| 71 | : null, |
| 72 | ); |
| 73 | const dismissed = useSyncExternalStore(subscribeDismissed, readDismissed); |
| 74 | const offered = offeredPromptSuggestion(data, thread?.latestRun, runs); |
| 75 | return { |
| 76 | suggestion: |
| 77 | offered && !dismissed.has(dismissKey(environmentId, offered.runId)) ? offered.text : null, |
| 78 | dismiss: () => { |
| 79 | if (offered === null) return; |
| 80 | dismissedKeys = new Set(dismissedKeys).add(dismissKey(environmentId, offered.runId)); |
| 81 | for (const listener of listeners) listener(); |
| 82 | }, |
| 83 | }; |
| 84 | } |