@alexkroman1/aai-ui 9.2.0 → 10.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -22,7 +22,7 @@ still React, still the same theme tokens.
22
22
  `client()` mounts the default chat shell with your sidebar, or replaces the
23
23
  whole UI with a custom component:
24
24
 
25
- ```tsx no-check
25
+ ```tsx
26
26
  import "@alexkroman1/aai-ui/styles.css";
27
27
  import { client, useAgentState, useTheme } from "@alexkroman1/aai-ui";
28
28
 
@@ -0,0 +1,73 @@
1
+ /**
2
+ * Finding a run again when the page has lost its id.
3
+ *
4
+ * A run is durable and a page is not — which `useWorkflowRun`'s doc says, and
5
+ * which was only half true of the hooks above it: the run id lived in plain
6
+ * `useState`, so a refresh (or a same-tab navigation, or a crashed tab) left a
7
+ * live run with nothing anywhere able to name it. The run really did continue;
8
+ * the person really could not get back to it.
9
+ *
10
+ * `StartOptions.key` is the handle that survives that, and it always was — a
11
+ * caller's own name for a run, indexed by the agent, read back with
12
+ * `find(workflow, key)`. What was missing is the two lines that ASK. This is
13
+ * them, plus the four decisions they turn out to carry.
14
+ *
15
+ * ## It is a MOUNT-time act, not "whenever there is no run"
16
+ *
17
+ * The tempting spelling is "if we hold no run id, look one up", and it breaks
18
+ * `reset()`: a form put back to its initial state holds no run id, so the next
19
+ * pass would re-adopt the very run the person had just dismissed — a Clear
20
+ * button that clears nothing. So the lookup runs once per mount (and again only
21
+ * if the KEY changes, which is a different person's run), and every later
22
+ * absence of a run id is taken at face value.
23
+ *
24
+ * ## The lookup NEVER wins a race against a submit
25
+ *
26
+ * A person who reloads and immediately submits has started the run they want,
27
+ * and an answer that was already in flight names an older one. The caller
28
+ * therefore adopts through `current ?? found`: the recovered id fills an empty
29
+ * slot and never replaces a full one.
30
+ *
31
+ * ## A failed lookup is REPORTED
32
+ *
33
+ * The alternative is a page that quietly shows an empty form to somebody whose
34
+ * run is live, who then starts a second one — the duplicated work the key
35
+ * exists to prevent, and on a workflow app that is real money. A person who has
36
+ * never run anything pays a banner they can ignore. Same trade as
37
+ * `useWorkflows`, for the same reason: an empty answer here is a confident
38
+ * false statement.
39
+ *
40
+ * ## It is OPT-IN
41
+ *
42
+ * A `key` on its own still means only "record this with the run", which is what
43
+ * a voice agent's `ctx.workflows.start({ key })` means and what a page passing
44
+ * an account id may well want. Adopting a run is a decision about the PAGE, so
45
+ * it is `recover: true` and the two together read as what they do.
46
+ */
47
+ import type { WorkflowApi } from "./workflow-client.ts";
48
+ /** What {@link useRecoveredRun} needs. */
49
+ export type RecoverRunOptions = {
50
+ /** The workflow whose runs are indexed under `key`. */
51
+ workflow: string;
52
+ /** The caller's handle on its own run, or undefined for a page with none. */
53
+ key: string | undefined;
54
+ /** Whether the caller asked for this at all — `recover` at the call site. */
55
+ enabled: boolean;
56
+ /** The stable getter from `useWorkflowApiRef`. */
57
+ getClient: () => WorkflowApi;
58
+ /** Adopt this run. Called at most once, and never with an empty answer. */
59
+ onFound: (runId: string) => void;
60
+ /** The lookup failed, and the page has to say so. */
61
+ onError: (message: string) => void;
62
+ };
63
+ /**
64
+ * Look up the newest run for a key, once, as the component mounts.
65
+ *
66
+ * @param opts - See {@link RecoverRunOptions}.
67
+ * @returns Whether the lookup is still out. A caller folds it into its own
68
+ * `pending`, because a form offering Submit while a live run is arriving is a
69
+ * form inviting a second one.
70
+ *
71
+ * @internal
72
+ */
73
+ export declare function useRecoveredRun(opts: RecoverRunOptions): boolean;