@luma-ai/react 0.1.0

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.
@@ -0,0 +1,121 @@
1
+ ---
2
+ name: luma-ai-react
3
+ description: Add or change the Luma AI site assistant in a React app with @luma-ai/react (Next.js App Router, TanStack Start/Router, React Router 7/8, or client-only SPAs). Use when wiring LumaProvider, the createLumaSessionHandler session route, dataSources, actions (defineAction/useLumaAction), useLuma/useLumaContext, privacy markers, or debugging "[@luma-ai/react]" console errors.
4
+ ---
5
+
6
+ # @luma-ai/react
7
+
8
+ Full docs (Persian, same version as installed): `node_modules/@luma-ai/react/docs/index.md`.
9
+ Online: https://dash.lumai.ir/llms.txt (append `.md` to any https://dash.lumai.ir/docs/react/... URL).
10
+
11
+ ## 1. Pick the connection mode
12
+
13
+ - **Server mode (default choice).** The app has a backend (Next.js, TanStack Start, React Router framework mode). The API key stays in a server env var.
14
+ - **Keyless mode.** Only for apps with no backend (Vite SPA, static hosting). It needs the owner to enable keyless access and allow the exact origin in the Luma dashboard.
15
+
16
+ Never put `LUMA_API_KEY` in client code or in a `NEXT_PUBLIC_`/`VITE_` variable.
17
+
18
+ ## 2. Server mode
19
+
20
+ ```ts
21
+ // app/api/luma/session/route.ts (Next.js)
22
+ import { createLumaSessionHandler } from "@luma-ai/react/server";
23
+
24
+ export const POST = createLumaSessionHandler({
25
+ apiKey: process.env.LUMA_API_KEY!,
26
+ assistantId: process.env.LUMA_ASSISTANT_ID!,
27
+ // actions: [addToCart], // declare actions here in server mode
28
+ // authorize: (req) => limiter(req) // add a per-visitor rate limit in production
29
+ });
30
+ ```
31
+
32
+ TanStack Start: `server: { handlers: { POST: ({ request }) => handler(request) } }` on a file route.
33
+ React Router framework mode: `export const action = ({ request }) => handler(request)`.
34
+
35
+ Provider, once, near the root. Import from the framework entry so navigation is client-side:
36
+
37
+ | Framework | Import |
38
+ | --- | --- |
39
+ | Next.js App Router | `@luma-ai/react/next` (works directly in `app/layout.tsx`) |
40
+ | TanStack Router/Start | `@luma-ai/react/tanstack` (root route) |
41
+ | React Router 7/8 | `@luma-ai/react/react-router` (root route, inside the router) |
42
+ | No router | `@luma-ai/react` |
43
+
44
+ ```tsx
45
+ <LumaProvider sessionEndpoint="/api/luma/session">{children}</LumaProvider>
46
+ ```
47
+
48
+ ## 3. Keyless mode
49
+
50
+ ```tsx
51
+ import { LumaProvider } from "@luma-ai/react"; // or /react-router, /tanstack
52
+ <LumaProvider assistantId="<uuid>" actions={[addToCart]}>{children}</LumaProvider>
53
+ ```
54
+
55
+ ## 4. Live data
56
+
57
+ ```tsx
58
+ <LumaProvider
59
+ sessionEndpoint="/api/luma/session"
60
+ dataSources={{
61
+ site: { name: "My shop", currency: "تومان", menus: [{ title: "Shipping", url: "/shipping" }] },
62
+ // Resolves to { items: LumaProduct[], hasMore?: boolean }
63
+ searchProducts: async ({ query, maxPrice, inStock, page }, { signal }) =>
64
+ (await fetch(`/api/products?q=${encodeURIComponent(query)}&page=${page}`, { signal })).json(),
65
+ // Resolves to LumaProduct | null
66
+ getProduct: async ({ productId }, { signal }) =>
67
+ (await fetch(`/api/products/${productId}`, { signal })).json(),
68
+ }}
69
+ />
70
+ ```
71
+
72
+ `LumaProduct` requires `productId`, `title`, `url` (same-origin), `price`, `inStock`. Everything runs in the browser. Pass `signal` to `fetch`. A missing function means that capability is not offered.
73
+
74
+ ## 5. Actions
75
+
76
+ ```ts
77
+ // lib/luma-actions.ts: shared by server and client
78
+ import { z } from "zod";
79
+ import { defineAction } from "@luma-ai/react/server";
80
+ export const addToCart = defineAction({
81
+ name: "addToCart", // /^[A-Za-z][\w-]{0,63}$/
82
+ title: "افزودن به سبد خرید", // Persian label on the approval card
83
+ description: "Add the product the visitor is viewing to their cart.",
84
+ input: z.object({ productId: z.string(), quantity: z.number().int().min(1).max(5) }),
85
+ });
86
+ ```
87
+
88
+ ```tsx
89
+ // in the component that owns the state
90
+ useLumaAction(addToCart, async ({ productId, quantity }) => {
91
+ await cart.add(productId, quantity);
92
+ return { cartItems: cart.count }; // JSON result reported to the assistant
93
+ });
94
+ ```
95
+
96
+ Declare the action in `createLumaSessionHandler({ actions })` (server) or `<LumaProvider actions>` (keyless). The visitor approves each run unless `confirm: false`. Keep `confirm: true` for anything financial or irreversible.
97
+
98
+ ## 6. Hooks and privacy
99
+
100
+ - `useLuma()` returns `{ isOpen, status, open, close, toggle, ask(text), newChat }`.
101
+ - `useLumaContext("product", { id, selectedSize })` shares small JSON page state (8 KB total). No secrets.
102
+ - Use `<LumaPrivate>`, `{...lumaPrivate()}` or `privateSelectors` for regions the assistant must never read. Use `restrictedPaths={["/admin"]}` for pages it must not inspect or open. Use `{...lumaLabel("…")}` to name unlabeled sections for guides.
103
+
104
+ ## Common mistakes
105
+
106
+ - Wrong: `<LumaProvider assistantId=… apiKey=…>`. There is no client `apiKey` prop. Use server mode.
107
+ - Wrong: importing `LumaProvider` from `@luma-ai/react` in a Next.js app. Pages then reload on navigation. Use `@luma-ai/react/next`.
108
+ - Wrong: defining `defineAction(...)` inside a component. Define it at module level.
109
+ - Wrong: registering `useLumaAction` without declaring the action. The console warns `LUMA_ACTION_NOT_DECLARED`.
110
+ - Wrong: an auth middleware that redirects `/api/luma/session`. The console reports `LUMA_INVALID_SESSION_RESPONSE`. Exclude the route.
111
+
112
+ ## Steps only the owner can do
113
+
114
+ Ask the user. Do not invent values:
115
+ - the assistant ID (UUID);
116
+ - creating an API key at https://dash.lumai.ir/developers/api/keys;
117
+ - for keyless mode, enabling keyless access and adding the exact origin in the dashboard (assistant → overview).
118
+
119
+ ## Debugging
120
+
121
+ Errors are logged as `[@luma-ai/react] CODE: hint`. Session route errors also return `{ error, code }` and log on the server. Look up the code in `node_modules/@luma-ai/react/docs/troubleshooting.md`.