|
1 | | -# React + TypeScript + Vite |
2 | | - |
3 | | -This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules. |
4 | | - |
5 | | -Currently, two official plugins are available: |
6 | | - |
7 | | -- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react/README.md) uses [Babel](https://babeljs.io/) for Fast Refresh |
8 | | -- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh |
9 | | - |
10 | | -## Expanding the ESLint configuration |
11 | | - |
12 | | -If you are developing a production application, we recommend updating the configuration to enable type-aware lint rules: |
13 | | - |
14 | | -```js |
15 | | -export default tseslint.config({ |
16 | | - extends: [ |
17 | | - // Remove ...tseslint.configs.recommended and replace with this |
18 | | - ...tseslint.configs.recommendedTypeChecked, |
19 | | - // Alternatively, use this for stricter rules |
20 | | - ...tseslint.configs.strictTypeChecked, |
21 | | - // Optionally, add this for stylistic rules |
22 | | - ...tseslint.configs.stylisticTypeChecked, |
23 | | - ], |
24 | | - languageOptions: { |
25 | | - // other options... |
26 | | - parserOptions: { |
27 | | - project: ["./tsconfig.node.json", "./tsconfig.app.json"], |
28 | | - tsconfigRootDir: import.meta.dirname, |
29 | | - }, |
30 | | - }, |
31 | | -}); |
| 1 | +# Browser Workflow Demo |
| 2 | + |
| 3 | +Run LlamaIndex workflows directly in the browser with React and Vite. |
| 4 | +This demo renders workflow output and showcases the streaming APIs without any server component. |
| 5 | + |
| 6 | +## Highlights |
| 7 | +- Fully client-side workflow execution with the `WorkflowContext` stream API |
| 8 | +- Event chaining example using `startEvent` and `stopEvent` to emit UI updates |
| 9 | + |
| 10 | +## Prerequisites |
| 11 | +- Node.js 20+ |
| 12 | +- npm, pnpm, or yarn (examples below use npm) |
| 13 | + |
| 14 | +## Getting Started |
| 15 | +```bash |
| 16 | +npm install |
| 17 | +npm run dev |
| 18 | +``` |
| 19 | +The dev server starts on http://localhost:5173 by default. |
| 20 | + |
| 21 | +To create a production build, run: |
| 22 | +```bash |
| 23 | +npm run build |
| 24 | +npm run preview |
32 | 25 | ``` |
33 | 26 |
|
34 | | -You can also install [eslint-plugin-react-x](https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin-react-x) and [eslint-plugin-react-dom](https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin-react-dom) for React-specific lint rules: |
35 | | - |
36 | | -```js |
37 | | -// eslint.config.js |
38 | | -import reactX from "eslint-plugin-react-x"; |
39 | | -import reactDom from "eslint-plugin-react-dom"; |
40 | | - |
41 | | -export default tseslint.config({ |
42 | | - plugins: { |
43 | | - // Add the react-x and react-dom plugins |
44 | | - "react-x": reactX, |
45 | | - "react-dom": reactDom, |
46 | | - }, |
47 | | - rules: { |
48 | | - // other rules... |
49 | | - // Enable its recommended typescript rules |
50 | | - ...reactX.configs["recommended-typescript"].rules, |
51 | | - ...reactDom.configs.recommended.rules, |
52 | | - }, |
| 27 | +## How It Works |
| 28 | +The demo defines two events and a simple workflow in `src/App.tsx`: |
| 29 | +```ts |
| 30 | +const startEvent = workflowEvent(); |
| 31 | +const stopEvent = workflowEvent<string>(); |
| 32 | + |
| 33 | +workflow.handle([startEvent], (context) => { |
| 34 | + setTimeout(() => { |
| 35 | + context.sendEvent(stopEvent.with("Hello, World!")); |
| 36 | + }, 1000); |
53 | 37 | }); |
| 38 | + |
| 39 | +const context = workflow.createContext(); |
| 40 | +context.sendEvent(startEvent.with()); |
| 41 | +const events = await context.stream.until(stopEvent).toArray(); |
54 | 42 | ``` |
| 43 | +- `startEvent` triggers when the component loads. |
| 44 | +- The handler fires an async side effect, then emits `stopEvent`. |
| 45 | +- `context.stream.until(stopEvent)` collects the events and the component renders the payload. |
| 46 | + |
| 47 | +## Customize the Workflow |
| 48 | +- Edit `src/App.tsx` to add additional events, stateful middleware, or validation. |
| 49 | +- Import React hooks to stream multiple updates or feed results into components. |
| 50 | + |
| 51 | +## Project Structure |
| 52 | +- `src/` – React components and workflow definition |
| 53 | +- `public/` – Static assets served by Vite |
| 54 | +- `vite.config.ts` – Vite configuration with the React SWC plugin |
| 55 | + |
0 commit comments