Open History to see the last commit
1import type { EnvironmentId, OrchestrationV2Run, RunId } from "@t3tools/contracts";
2import { useSyncExternalStore } from "react";
3
4import type { Thread } from "../../types";
5import { useMediaQuery } from "../../hooks/useMediaQuery";
6import { useServerConfigs } from "../../state/entities";
7import { orchestrationEnvironment } from "../../state/orchestration";
8import { useEnvironmentQuery } from "../../state/query";
9import { 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.
13let dismissedKeys: ReadonlySet<string> = new Set();
14const listeners = new Set<() => void>();
15const subscribeDismissed = (listener: () => void) => {
16 listeners.add(listener);
17 return () => listeners.delete(listener);
18};
19const readDismissed = () => dismissedKeys;
20const 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 */
28export 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 */
55export 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}