@assistant-ui/store 0.2.9 → 0.2.11

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.
Files changed (38) hide show
  1. package/README.md +36 -65
  2. package/dist/AuiIf.d.ts +31 -0
  3. package/dist/AuiIf.d.ts.map +1 -1
  4. package/dist/AuiIf.js +22 -0
  5. package/dist/AuiIf.js.map +1 -1
  6. package/dist/Derived.d.ts +1 -3
  7. package/dist/Derived.d.ts.map +1 -1
  8. package/dist/RenderChildrenWithAccessor.d.ts.map +1 -1
  9. package/dist/RenderChildrenWithAccessor.js +11 -7
  10. package/dist/RenderChildrenWithAccessor.js.map +1 -1
  11. package/dist/useAui.d.ts +59 -0
  12. package/dist/useAui.d.ts.map +1 -1
  13. package/dist/useAui.js +39 -21
  14. package/dist/useAui.js.map +1 -1
  15. package/dist/useAuiEvent.d.ts +41 -0
  16. package/dist/useAuiEvent.d.ts.map +1 -1
  17. package/dist/useAuiEvent.js +41 -0
  18. package/dist/useAuiEvent.js.map +1 -1
  19. package/dist/useAuiState.d.ts +25 -8
  20. package/dist/useAuiState.d.ts.map +1 -1
  21. package/dist/useAuiState.js +25 -8
  22. package/dist/useAuiState.js.map +1 -1
  23. package/dist/utils/react-assistant-context.d.ts +18 -5
  24. package/dist/utils/react-assistant-context.d.ts.map +1 -1
  25. package/dist/utils/react-assistant-context.js +16 -5
  26. package/dist/utils/react-assistant-context.js.map +1 -1
  27. package/dist/utils/splitClients.d.ts.map +1 -1
  28. package/dist/utils/splitClients.js.map +1 -1
  29. package/package.json +5 -5
  30. package/src/AuiIf.ts +35 -1
  31. package/src/Derived.ts +1 -1
  32. package/src/RenderChildrenWithAccessor.tsx +10 -8
  33. package/src/__tests__/RenderChildrenWithAccessor.test.tsx +136 -0
  34. package/src/useAui.ts +101 -32
  35. package/src/useAuiEvent.ts +41 -0
  36. package/src/useAuiState.ts +25 -8
  37. package/src/utils/react-assistant-context.tsx +18 -5
  38. package/src/utils/splitClients.ts +4 -2
@@ -4,18 +4,35 @@ import { useAui } from "./useAui";
4
4
  import { getProxiedAssistantState } from "./utils/proxied-assistant-state";
5
5
 
6
6
  /**
7
- * Hook to access a slice of the assistant state with automatic subscription
7
+ * Subscribes to a slice of {@link AssistantState} and re-renders the
8
+ * component whenever that slice changes.
8
9
  *
9
- * @param selector - Function to select a slice of the state
10
- * @returns The selected state slice
10
+ * The `selector` is called on every store update; its return value is
11
+ * compared by `Object.is`, and the component re-renders only when the
12
+ * selected slice changes. Returning the entire state object is not
13
+ * supported and throws at runtime — select a specific field instead, or
14
+ * compose multiple `useAuiState` calls. Returning a new object or array
15
+ * literal, including spreading `s.thread` into a new object, causes a
16
+ * re-render on every store update; either select primitives or return a
17
+ * memoized reference.
18
+ *
19
+ * @param selector - Pure function that derives a value from the current
20
+ * assistant state. Should be cheap and referentially stable for equal
21
+ * inputs (plain field reads, primitives, or memoized values).
22
+ * @returns The currently selected slice.
11
23
  *
12
24
  * @example
13
- * ```typescript
14
- * const aui = useAui({
15
- * foo: RootScope({ ... }),
16
- * });
25
+ * ```tsx
26
+ * // Disable a button while a run is in flight.
27
+ * const isRunning = useAuiState((s) => s.thread.isRunning);
28
+ * ```
17
29
  *
18
- * const bar = useAuiState((s) => s.foo.bar);
30
+ * @example
31
+ * ```tsx
32
+ * // Prefer multiple selectors over an inline object literal, which would
33
+ * // create a new reference on every render.
34
+ * const text = useAuiState((s) => s.composer.text);
35
+ * const canSend = useAuiState((s) => s.composer.canSend);
19
36
  * ```
20
37
  */
21
38
  export const useAuiState = <T>(selector: (state: AssistantState) => T): T => {
@@ -85,20 +85,33 @@ export const useAssistantContextValue = (): AssistantClient => {
85
85
  };
86
86
 
87
87
  /**
88
- * Provider component for AssistantClient
88
+ * Supplies an `AssistantClient` to the React tree.
89
+ *
90
+ * Place near the root of any subtree that uses {@link useAui} or the
91
+ * primitives built on it. Components rendered outside an `AuiProvider`
92
+ * receive a default client whose scope accessors throw on use, so
93
+ * missing-provider mistakes surface at the point of use.
94
+ *
95
+ * When mounting a runtime built with one of the runtime hooks, use
96
+ * {@link AssistantRuntimeProvider} — it installs an `AuiProvider`
97
+ * internally — rather than wiring `AuiProvider` yourself.
89
98
  *
90
99
  * @example
91
- * ```typescript
92
- * <AuiProvider value={aui}>
93
- * <YourApp />
94
- * </AuiProvider>
100
+ * ```tsx
101
+ * function ScopedAssistant({ children, scopes }) {
102
+ * const aui = useAui(scopes);
103
+ *
104
+ * return <AuiProvider value={aui}>{children}</AuiProvider>;
105
+ * }
95
106
  * ```
96
107
  */
97
108
  export const AuiProvider = ({
98
109
  value,
99
110
  children,
100
111
  }: {
112
+ /** Assistant client to expose to descendants. */
101
113
  value: AssistantClient;
114
+ /** Subtree that may read from the client. */
102
115
  children: React.ReactNode;
103
116
  }): React.ReactElement => {
104
117
  return (
@@ -6,7 +6,7 @@ import type {
6
6
  } from "../types/client";
7
7
  import { getTransformScopes } from "../attachTransformScopes";
8
8
  import type { useAui } from "../useAui";
9
- import { tapMemo } from "@assistant-ui/tap";
9
+ import { tapMemo, type ResourceElement } from "@assistant-ui/tap";
10
10
 
11
11
  export type RootClients = Partial<
12
12
  Record<ClientNames, ClientElement<ClientNames>>
@@ -35,7 +35,9 @@ function splitClients(clients: useAui.Props, baseClient: AssistantClient) {
35
35
  if (visited.has(clientElement.type)) continue;
36
36
  visited.add(clientElement.type);
37
37
 
38
- const transform = getTransformScopes(clientElement.type);
38
+ const transform = getTransformScopes(
39
+ clientElement.type as (props: any) => ResourceElement<any>,
40
+ );
39
41
  if (transform) {
40
42
  transform(scopes, baseClient);
41
43
  changed = true;