|
| 1 | +# @aipexstudio/aipex-react |
| 2 | + |
| 3 | +React UI toolkit for building AIPex-powered chat and extension experiences. |
| 4 | + |
| 5 | +This package depends on `@aipexstudio/aipex-core` only (no browser-specific runtime code). It provides: |
| 6 | + |
| 7 | +- **Chat UI**: `<Chatbot />` and related components, slots, and themes |
| 8 | +- **Headless primitives**: `useChat`, `ChatAdapter` (convert core `AgentEvent` streams into UI messages) |
| 9 | +- **Settings UI + persistence**: `<SettingsPage />`, `useChatConfig` (storage via `KeyValueStorage`) |
| 10 | +- **Extension building blocks**: `<ContentScript />`, `<Omni />`, intervention cards, fake mouse cursor |
| 11 | +- **Optional submodules**: i18n provider and theme provider (via package subpath exports) |
| 12 | + |
| 13 | +## Design notes |
| 14 | + |
| 15 | +- **Core-first**: UI consumes `AgentEvent` from `@aipexstudio/aipex-core` and does not assume a runtime. |
| 16 | +- **Storage abstraction**: persistence is done through `KeyValueStorage` (localStorage by default; can be |
| 17 | + swapped for `chrome.storage`, IndexedDB, etc). |
| 18 | +- **Streaming UX**: the adapter + hooks are built around incremental updates and tool lifecycle states. |
| 19 | +- **Customization**: compose your UI with component overrides and slots, and style via CSS variables. |
| 20 | + |
| 21 | +## Installation |
| 22 | + |
| 23 | +```bash |
| 24 | +npm install @aipexstudio/aipex-react @aipexstudio/aipex-core |
| 25 | +# or |
| 26 | +pnpm add @aipexstudio/aipex-react @aipexstudio/aipex-core |
| 27 | +``` |
| 28 | + |
| 29 | +Peer dependencies: |
| 30 | + |
| 31 | +- `react` |
| 32 | +- `react-dom` |
| 33 | + |
| 34 | +## Quick start (drop-in Chatbot) |
| 35 | + |
| 36 | +```tsx |
| 37 | +import { google } from "@ai-sdk/google"; |
| 38 | +import { AIPex, aisdk } from "@aipexstudio/aipex-core"; |
| 39 | +import { Chatbot } from "@aipexstudio/aipex-react"; |
| 40 | + |
| 41 | +const agent = AIPex.create({ |
| 42 | + instructions: "You are a helpful assistant.", |
| 43 | + model: aisdk(google("gemini-2.5-flash")), |
| 44 | +}); |
| 45 | + |
| 46 | +export function App() { |
| 47 | + return <Chatbot agent={agent} />; |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +## Usage |
| 52 | + |
| 53 | +### 1) Headless chat (`useChat`) |
| 54 | + |
| 55 | +`useChat` is a rendering-free hook for building your own UI. |
| 56 | + |
| 57 | +```tsx |
| 58 | +import { useChat } from "@aipexstudio/aipex-react"; |
| 59 | + |
| 60 | +export function MyChat({ agent }: { agent: any }) { |
| 61 | + const { messages, status, sendMessage, interrupt, reset, regenerate } = useChat(agent); |
| 62 | + |
| 63 | + return ( |
| 64 | + <div> |
| 65 | + <div>Status: {status}</div> |
| 66 | + <pre>{JSON.stringify(messages, null, 2)}</pre> |
| 67 | + <button onClick={() => sendMessage("Hello!")}>Send</button> |
| 68 | + <button onClick={() => regenerate()}>Regenerate</button> |
| 69 | + <button onClick={() => interrupt()}>Interrupt</button> |
| 70 | + <button onClick={() => reset()}>Reset</button> |
| 71 | + </div> |
| 72 | + ); |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +### 2) Themes (Chatbot CSS variables) |
| 77 | + |
| 78 | +The Chatbot uses CSS variables to keep theming predictable and framework-friendly. |
| 79 | +Built-in themes: |
| 80 | + |
| 81 | +- `defaultTheme` |
| 82 | +- `darkTheme` |
| 83 | +- `minimalTheme` |
| 84 | +- `colorfulTheme` |
| 85 | + |
| 86 | +You can also build your own theme: |
| 87 | + |
| 88 | +```tsx |
| 89 | +import { Chatbot, createTheme } from "@aipexstudio/aipex-react"; |
| 90 | + |
| 91 | +const myTheme = createTheme({ |
| 92 | + className: "my-chat", |
| 93 | + variables: { |
| 94 | + "--chatbot-primary": "hsl(262 83% 58%)", |
| 95 | + "--chatbot-radius": "12px", |
| 96 | + }, |
| 97 | +}); |
| 98 | + |
| 99 | +export function App({ agent }: { agent: any }) { |
| 100 | + return <Chatbot agent={agent} theme={myTheme} />; |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +### 3) Customize components and slots |
| 105 | + |
| 106 | +`Chatbot` supports: |
| 107 | + |
| 108 | +- **Component overrides** (replace structural components) |
| 109 | +- **Slots** (inject custom UI fragments like tool display, message actions, etc.) |
| 110 | + |
| 111 | +```tsx |
| 112 | +import { Chatbot } from "@aipexstudio/aipex-react"; |
| 113 | + |
| 114 | +export function App({ agent }: { agent: any }) { |
| 115 | + return ( |
| 116 | + <Chatbot |
| 117 | + agent={agent} |
| 118 | + slots={{ |
| 119 | + messageActions: ({ message }) => <button onClick={() => console.log(message.id)}>Log</button>, |
| 120 | + }} |
| 121 | + /> |
| 122 | + ); |
| 123 | +} |
| 124 | +``` |
| 125 | + |
| 126 | +### 4) Settings persistence (`useChatConfig`) and `<SettingsPage />` |
| 127 | + |
| 128 | +`useChatConfig` loads/saves `AppSettings` using a `KeyValueStorage` adapter. |
| 129 | + |
| 130 | +- Default storage: localStorage-backed adapter |
| 131 | +- Extension storage: pass any `KeyValueStorage` (e.g. `ChromeStorageAdapter` from `@aipexstudio/browser-runtime`) |
| 132 | + |
| 133 | +```tsx |
| 134 | +import { SettingsPage } from "@aipexstudio/aipex-react"; |
| 135 | +import { chromeStorageAdapter } from "@aipexstudio/browser-runtime"; |
| 136 | + |
| 137 | +export function Options() { |
| 138 | + return <SettingsPage storageAdapter={chromeStorageAdapter} />; |
| 139 | +} |
| 140 | +``` |
| 141 | + |
| 142 | +### 5) i18n and theme providers (subpath exports) |
| 143 | + |
| 144 | +This package exposes i18n + theme contexts via subpath exports: |
| 145 | + |
| 146 | +- `@aipexstudio/aipex-react/i18n/context` |
| 147 | +- `@aipexstudio/aipex-react/theme/context` |
| 148 | + |
| 149 | +```tsx |
| 150 | +import { ChromeStorageAdapter } from "@aipexstudio/browser-runtime"; |
| 151 | +import { I18nProvider } from "@aipexstudio/aipex-react/i18n/context"; |
| 152 | +import { ThemeProvider } from "@aipexstudio/aipex-react/theme/context"; |
| 153 | +import type { Language } from "@aipexstudio/aipex-react/i18n/types"; |
| 154 | +import type { Theme } from "@aipexstudio/aipex-react/theme/types"; |
| 155 | +import { Chatbot } from "@aipexstudio/aipex-react"; |
| 156 | + |
| 157 | +const i18nStorage = new ChromeStorageAdapter<Language>(); |
| 158 | +const themeStorage = new ChromeStorageAdapter<Theme>(); |
| 159 | + |
| 160 | +export function App({ agent }: { agent: any }) { |
| 161 | + return ( |
| 162 | + <I18nProvider storageAdapter={i18nStorage}> |
| 163 | + <ThemeProvider storageAdapter={themeStorage}> |
| 164 | + <Chatbot agent={agent} /> |
| 165 | + </ThemeProvider> |
| 166 | + </I18nProvider> |
| 167 | + ); |
| 168 | +} |
| 169 | +``` |
| 170 | + |
| 171 | +### 6) Content script UI (`<ContentScript />`) and plugins |
| 172 | + |
| 173 | +`ContentScript` is an extensible component for browser extension content scripts. It: |
| 174 | + |
| 175 | +- mounts an Omni-like UI (default or custom) |
| 176 | +- hosts plugins with a shared state + event bus |
| 177 | +- integrates with `chrome.runtime`-like message passing (when available) |
| 178 | + |
| 179 | +```tsx |
| 180 | +import { ContentScript, type ContentScriptPlugin } from "@aipexstudio/aipex-react"; |
| 181 | + |
| 182 | +const loggerPlugin: ContentScriptPlugin = { |
| 183 | + name: "logger", |
| 184 | + setup: (ctx) => { |
| 185 | + ctx.state.loggerReady = true; |
| 186 | + }, |
| 187 | + onMessage: async (message, ctx) => { |
| 188 | + console.log("[content-script message]", message); |
| 189 | + ctx.emit("debug:message", message); |
| 190 | + }, |
| 191 | + onEvent: (event, data) => { |
| 192 | + if (event === "debug:message") { |
| 193 | + console.log("[content-script event]", data); |
| 194 | + } |
| 195 | + }, |
| 196 | +}; |
| 197 | + |
| 198 | +export function InjectedUI() { |
| 199 | + return <ContentScript plugins={[loggerPlugin]} initialOpen={false} />; |
| 200 | +} |
| 201 | +``` |
| 202 | + |
| 203 | +## API reference |
| 204 | + |
| 205 | +### Chatbot components |
| 206 | + |
| 207 | +- `Chatbot(props)` |
| 208 | + - `agent: AIPex | undefined` |
| 209 | + - `config?: ChatConfig` |
| 210 | + - `handlers?: ChatbotEventHandlers` |
| 211 | + - `components?: ChatbotComponents` |
| 212 | + - `slots?: ChatbotSlots` |
| 213 | + - `theme?: ChatbotTheme` |
| 214 | + - `className?: string` |
| 215 | + - `initialSettings?: Partial<AppSettings>` |
| 216 | + - `storageAdapter?: KeyValueStorage<unknown>` (defaults to localStorage) |
| 217 | + - `models?: Array<{ name: string; value: string }>` |
| 218 | + - `placeholderTexts?: string[]` |
| 219 | + - `title?: string` |
| 220 | + |
| 221 | +- `ChatbotProvider(props)` is the same core provider but expects `children` and does not include `title/models`. |
| 222 | + |
| 223 | +### Slots (`ChatbotSlots`) |
| 224 | + |
| 225 | +Customize specific parts of the Chatbot UI: |
| 226 | + |
| 227 | +- `messageActions(props)` |
| 228 | +- `inputToolbar(props)` |
| 229 | +- `modelSelector(props)` |
| 230 | +- `contextTags(props)` |
| 231 | +- `toolDisplay(props)` |
| 232 | +- `headerContent()` |
| 233 | +- `footerContent()` |
| 234 | +- `emptyState(props)` |
| 235 | +- `loadingIndicator()` |
| 236 | +- `afterMessages()` |
| 237 | + |
| 238 | +### Hooks and adapter |
| 239 | + |
| 240 | +- `useChat(agent, options?)` → `{ messages, status, sessionId, sendMessage, continueConversation, interrupt, reset, regenerate, setMessages }` |
| 241 | +- `useChatConfig(options?)` → `{ settings, isLoading, updateSetting, updateSettings, resetSettings, reloadSettings }` |
| 242 | +- `ChatAdapter` / `createChatAdapter(options?)` |
| 243 | + |
| 244 | +### Settings |
| 245 | + |
| 246 | +- `SettingsPage(props)` |
| 247 | + - `storageAdapter: KeyValueStorage<unknown>` |
| 248 | + - `storageKey?: string` |
| 249 | + - `className?: string` |
| 250 | + - `onSave?: (settings: AppSettings) => void` |
| 251 | + - `onTestConnection?: (settings: AppSettings) => Promise<boolean>` |
| 252 | + |
| 253 | +### Chatbot themes |
| 254 | + |
| 255 | +- Presets: `defaultTheme`, `darkTheme`, `minimalTheme`, `colorfulTheme` |
| 256 | +- Variables: `defaultThemeVariables`, `darkThemeVariables` |
| 257 | +- Helpers: `createTheme(overrides)`, `mergeThemes(base, overrides)` |
| 258 | + |
| 259 | +### i18n (subpath exports) |
| 260 | + |
| 261 | +Import from `@aipexstudio/aipex-react/i18n/context`: |
| 262 | + |
| 263 | +- `I18nProvider({ storageAdapter, children })` |
| 264 | +- `useTranslation()` → `{ language, t(key, params?), changeLanguage }` |
| 265 | + |
| 266 | +### Global theme (subpath exports) |
| 267 | + |
| 268 | +Import from `@aipexstudio/aipex-react/theme/context`: |
| 269 | + |
| 270 | +- `ThemeProvider({ storageAdapter, scope?, children })` |
| 271 | +- `useTheme()` → `{ theme, effectiveTheme, changeTheme }` |
| 272 | + |
| 273 | +### Content script + plugins |
| 274 | + |
| 275 | +- `ContentScript(props)` |
| 276 | + - `initialOpen?: boolean` |
| 277 | + - `onOpen?: () => void` |
| 278 | + - `onClose?: () => void` |
| 279 | + - `plugins?: ContentScriptPlugin[]` |
| 280 | + - `messageHandlers?: MessageHandlers` |
| 281 | + - `runtime?: RuntimeApi` (defaults to `chrome.runtime`-like global if available) |
| 282 | + - `container?: HTMLElement` |
| 283 | + - `shadowRoot?: ShadowRoot` |
| 284 | +- `initContentScript(props, options?)` → cleanup function |
| 285 | + |
| 286 | +Built-in runtime message actions handled by `ContentScript`: |
| 287 | + |
| 288 | +- `message.action === "aipex_open_omni"` |
| 289 | +- `message.action === "aipex_close_omni"` |
| 290 | + |
| 291 | +### Misc components |
| 292 | + |
| 293 | +- `Omni` (command palette) |
| 294 | +- `FakeMouse` (+ controller/types) |
| 295 | +- Intervention components: `InterventionCard`, `SelectionCard`, `MonitorCard`, `VoiceCard`, `InterventionModeToggle` |
| 296 | + |
| 297 | +## Development (monorepo) |
| 298 | + |
| 299 | +From the repository root: |
| 300 | + |
| 301 | +```bash |
| 302 | +pnpm --filter @aipexstudio/aipex-react build |
| 303 | +pnpm --filter @aipexstudio/aipex-react typecheck |
| 304 | +pnpm --filter @aipexstudio/aipex-react test |
| 305 | +``` |
| 306 | + |
| 307 | +## License |
| 308 | + |
| 309 | +MIT |
| 310 | + |
0 commit comments