@mnexium/chat 2.0.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Marius Ndini
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,425 @@
1
+ # @mnexium/chat
2
+
3
+ A drop-in chat widget that gives any web app a full AI chat experience backed by [Mnexium](https://mnexium.com). Works with **React**, **Express**, or **vanilla JavaScript**.
4
+
5
+ ![Mnexium Chat Demo](img/embed.png)
6
+
7
+ ## Features
8
+
9
+ - **Framework Flexible** - React component, Express middleware, or vanilla JS client
10
+ - **Floating Widget** - Modern glassmorphism design with smooth animations
11
+ - **Persistent Memory** - Conversations and user context persist across sessions
12
+ - **Streaming Responses** - Real-time streaming with typing effect
13
+ - **Multi-Provider Support** - Works with OpenAI, Anthropic Claude, and Google Gemini
14
+ - **Markdown Rendering** - Rich formatting for assistant messages
15
+ - **Zero Client-Side Keys** - All API keys stay on your server
16
+ - **Theming** - Light and dark themes with customizable primary color
17
+ - **Mobile Optimized** - Full-screen chat on mobile with body scroll lock
18
+
19
+ ## Installation
20
+
21
+ ```bash
22
+ npm install @mnexium/chat
23
+ ```
24
+
25
+ ## Package Exports
26
+
27
+ | Import | Description |
28
+ |--------|-------------|
29
+ | `@mnexium/chat` | React component with full UI |
30
+ | `@mnexium/chat/browser` | Browser bundle (script tag, no build required) |
31
+ | `@mnexium/chat/core` | Vanilla JS client (framework-agnostic) |
32
+ | `@mnexium/chat/server` | Server handlers (Next.js + Express) |
33
+
34
+ ## Quick Start (React + Next.js)
35
+
36
+ ### 1. Add the Chat Widget
37
+
38
+ ```tsx
39
+ import { MnexiumChat } from '@mnexium/chat';
40
+
41
+ export default function Layout({ children }) {
42
+ return (
43
+ <>
44
+ {children}
45
+ <MnexiumChat endpoint="/api/mnx" />
46
+ </>
47
+ );
48
+ }
49
+ ```
50
+
51
+ This adds a floating "Ask AI" button to the bottom-right of your page.
52
+
53
+ ### 2. Set Up Server Routes
54
+
55
+ **`app/api/mnx/_mnx.ts`**
56
+
57
+ ```ts
58
+ import { createHandlers } from '@mnexium/chat/server';
59
+
60
+ export const mnx = createHandlers({
61
+ model: process.env.MODEL ?? 'gpt-4o-mini',
62
+ cookiePrefix: 'mnx',
63
+ mnxOptions: {
64
+ history: true,
65
+ learn: true,
66
+ recall: true,
67
+ profile: true,
68
+ summarize: 'balanced',
69
+ },
70
+ });
71
+ ```
72
+
73
+ **`app/api/mnx/bootstrap/route.ts`**
74
+
75
+ ```ts
76
+ import { mnx } from '../_mnx';
77
+ export const GET = mnx.bootstrap;
78
+ ```
79
+
80
+ **`app/api/mnx/chat/route.ts`**
81
+
82
+ ```ts
83
+ import { mnx } from '../_mnx';
84
+ export const POST = mnx.chat;
85
+ ```
86
+
87
+ **`app/api/mnx/new-chat/route.ts`**
88
+
89
+ ```ts
90
+ import { mnx } from '../_mnx';
91
+ export const POST = mnx.newChat;
92
+ ```
93
+
94
+ **`app/api/mnx/conversations/[chatId]/route.ts`**
95
+
96
+ ```ts
97
+ import { mnx } from '../../_mnx';
98
+
99
+ export async function GET(
100
+ req: Request,
101
+ { params }: { params: { chatId: string } }
102
+ ) {
103
+ return mnx.conversation(req, params.chatId);
104
+ }
105
+ ```
106
+
107
+ ### 3. Configure Environment Variables
108
+
109
+ ```bash
110
+ # .env.local
111
+ MNX_API_KEY=mnx_live_...
112
+
113
+ # Include one or all of the following provider keys:
114
+ OPENAI_API_KEY=sk-...
115
+ ANTHROPIC_API_KEY=sk-ant-...
116
+ GOOGLE_API_KEY=...
117
+
118
+ # Optionally set the model (defaults to gpt-4o-mini)
119
+ # MODEL=gpt-4o-mini
120
+ # MODEL=claude-3-haiku-20240307
121
+ # MODEL=gemini-2.0-flash-lite
122
+ ```
123
+
124
+ ---
125
+
126
+ ## Plain HTML (No Build Required)
127
+
128
+ For non-React apps, use the browser bundle with a simple script tag. **Same UI as React!**
129
+
130
+ ### 1. Set Up Express Server
131
+
132
+ ```javascript
133
+ import express from 'express';
134
+ import { mountMnexiumRoutes } from '@mnexium/chat/server';
135
+
136
+ const app = express();
137
+ app.use(express.json());
138
+ app.use(express.static('public'));
139
+
140
+ // Serve the browser bundle
141
+ app.get('/mnexium-chat.js', (req, res) => {
142
+ res.sendFile(require.resolve('@mnexium/chat/browser'));
143
+ });
144
+
145
+ // Mount Mnexium API routes
146
+ mountMnexiumRoutes(app, '/api/mnx');
147
+
148
+ app.listen(3000);
149
+ ```
150
+
151
+ ### 2. Add Script Tag to HTML
152
+
153
+ ```html
154
+ <!-- Just add this script tag! -->
155
+ <script
156
+ src="/mnexium-chat.js"
157
+ data-endpoint="/api/mnx"
158
+ data-title="Ask AI"
159
+ data-button-label="Ask AI"
160
+ data-theme="dark"
161
+ data-history="true"
162
+ ></script>
163
+ ```
164
+
165
+ That's it! The floating chat button appears automatically.
166
+
167
+ ### Script Data Attributes
168
+
169
+ | Attribute | Description |
170
+ |-----------|-------------|
171
+ | `data-endpoint` | API endpoint (default: `/api/mnx`) |
172
+ | `data-title` | Chat window title |
173
+ | `data-button-label` | Floating button label |
174
+ | `data-position` | `bottom-right` or `bottom-left` |
175
+ | `data-primary-color` | Accent color (hex) |
176
+ | `data-text-color` | Button text color |
177
+ | `data-theme` | `light` or `dark` |
178
+ | `data-logo` | Logo image URL |
179
+ | `data-welcome-icon` | Welcome emoji |
180
+ | `data-welcome-message` | Welcome text |
181
+ | `data-default-open` | `true` to start open |
182
+ | `data-history` | `true` to load history |
183
+ | `data-eager-init` | `false` to delay init |
184
+
185
+ ### Programmatic Usage
186
+
187
+ ```html
188
+ <script src="/mnexium-chat.js" data-auto-init="false"></script>
189
+ <script>
190
+ // Render manually with options
191
+ MnexiumChat.render({
192
+ endpoint: '/api/mnx',
193
+ title: 'Support',
194
+ primaryColor: '#45b1eb',
195
+ theme: 'dark',
196
+ });
197
+ </script>
198
+ ```
199
+
200
+ ---
201
+
202
+ ## Vanilla JS Client (Build Your Own UI)
203
+
204
+ For custom UIs, use the core client directly:
205
+
206
+ ```javascript
207
+ import { MnexiumClient } from '@mnexium/chat/core';
208
+
209
+ const chat = new MnexiumClient({ endpoint: '/api/mnx', history: true });
210
+
211
+ chat.onMessage((messages) => renderMessages(messages));
212
+ chat.onStateChange((state) => updateUI(state));
213
+
214
+ await chat.init();
215
+ await chat.send('Hello!');
216
+ ```
217
+
218
+ ## Component Props
219
+
220
+ | Prop | Type | Default | Description |
221
+ |------|------|---------|-------------|
222
+ | `endpoint` | `string` | `'/api/mnx'` | Base URL for API routes |
223
+ | `title` | `string` | `'Ask AI'` | Chat window header title |
224
+ | `buttonLabel` | `string` | `'Ask AI'` | Floating button label |
225
+ | `placeholder` | `string` | `'Type a message...'` | Input placeholder |
226
+ | `position` | `'bottom-right' \| 'bottom-left'` | `'bottom-right'` | Widget position |
227
+ | `primaryColor` | `string` | `'#facc15'` | Accent color (use 6-char hex, e.g. `#45b1eb`) |
228
+ | `textColor` | `string` | `'#000'` | Text color for button and user messages |
229
+ | `theme` | `'light' \| 'dark'` | `'dark'` | Color theme |
230
+ | `defaultOpen` | `boolean` | `false` | Start with chat open |
231
+ | `eagerInit` | `boolean` | `true` | Initialize on page load (no "Initializing..." delay) |
232
+ | `logo` | `string` | - | URL to custom logo image |
233
+ | `welcomeIcon` | `string` | `'👋'` | Emoji shown in empty chat |
234
+ | `welcomeMessage` | `string` | `'How can I help you today?'` | Welcome message |
235
+ | `history` | `boolean` | `false` | Load previous conversation on open |
236
+
237
+ ## Server Handler Options
238
+
239
+ ### `createHandlers(config)` (Recommended)
240
+
241
+ Creates all handlers with shared configuration:
242
+
243
+ ```ts
244
+ const mnx = createHandlers({
245
+ model: 'gpt-4o-mini',
246
+ cookiePrefix: 'mnx',
247
+ mnxOptions: { ... },
248
+ });
249
+
250
+ // Returns: { bootstrap, chat, newChat, history, conversation }
251
+ ```
252
+
253
+ | Option | Type | Default | Description |
254
+ |--------|------|---------|-------------|
255
+ | `model` | `string` | `'gpt-4o-mini'` | LLM model to use |
256
+ | `cookiePrefix` | `string` | `'mnx'` | Prefix for session cookies |
257
+ | `mnxOptions.history` | `boolean` | `true` | Enable conversation history |
258
+ | `mnxOptions.learn` | `boolean \| 'force'` | `true` | Extract and store memories |
259
+ | `mnxOptions.recall` | `boolean` | `true` | Inject relevant memories |
260
+ | `mnxOptions.profile` | `boolean` | `true` | Include user profile |
261
+ | `mnxOptions.summarize` | `'light' \| 'balanced' \| 'aggressive' \| false` | `'balanced'` | Summarization mode |
262
+ | `mnxOptions.system_prompt` | `string` | - | System prompt text for the AI assistant |
263
+
264
+ ### Individual Handlers
265
+
266
+ You can also import handlers individually:
267
+
268
+ ```ts
269
+ import { bootstrapHandler, chatHandler, newChatHandler, historyHandler, conversationHandler } from '@mnexium/chat/server';
270
+ ```
271
+
272
+ ### Express Middleware
273
+
274
+ For Express apps, use `createExpressMiddleware` or `mountMnexiumRoutes`:
275
+
276
+ ```ts
277
+ import { createExpressMiddleware, mountMnexiumRoutes } from '@mnexium/chat/server';
278
+
279
+ // Option 1: Mount all routes at once
280
+ mountMnexiumRoutes(app, '/api/mnx');
281
+
282
+ // Option 2: Use individual middleware handlers
283
+ const mnx = createExpressMiddleware({ cookiePrefix: 'mnx' });
284
+ app.get('/api/mnx/bootstrap', mnx.bootstrap);
285
+ app.post('/api/mnx/chat', mnx.chat);
286
+ app.post('/api/mnx/new-chat', mnx.newChat);
287
+ app.get('/api/mnx/conversations/:chatId', mnx.conversation);
288
+ ```
289
+
290
+ ## Supported Models
291
+
292
+ **OpenAI:** `gpt-4o`, `gpt-4o-mini`, `gpt-4-turbo`, `gpt-4`, `gpt-3.5-turbo`, `o1`, `o1-mini`
293
+
294
+ **Anthropic:** `claude-3-opus`, `claude-3-sonnet`, `claude-3-haiku`, `claude-3-5-sonnet`, `claude-sonnet-4`
295
+
296
+ **Google Gemini:** `gemini-2.0-flash-lite`, `gemini-2.5-flash`, `gemini-1.5-pro`, `gemini-1.5-flash`
297
+
298
+ ## How It Works
299
+
300
+ 1. **On mount**, the client calls `GET /api/mnx/bootstrap`
301
+ 2. Server generates or retrieves `subject_id` and `chat_id` from cookies
302
+ 3. If a previous chat exists, it's loaded from Mnexium history
303
+ 4. **On send**, the client `POST`s to `/api/mnx/chat`
304
+ 5. Server forwards to Mnexium with memory/history flags
305
+ 6. Response streams back to client in real-time
306
+
307
+ All API keys remain server-side. The client never sees or constructs Mnexium requests.
308
+
309
+ ## Full Next.js Example
310
+
311
+ ```
312
+ app/
313
+ ├── api/
314
+ │ └── mnx/
315
+ │ ├── _mnx.ts # Shared config
316
+ │ ├── bootstrap/
317
+ │ │ └── route.ts
318
+ │ ├── chat/
319
+ │ │ └── route.ts
320
+ │ ├── new-chat/
321
+ │ │ └── route.ts
322
+ │ └── conversations/
323
+ │ └── [chatId]/
324
+ │ └── route.ts
325
+ └── layout.tsx
326
+ ```
327
+
328
+ **`app/layout.tsx`**
329
+
330
+ ```tsx
331
+ import { MnexiumChat } from '@mnexium/chat';
332
+
333
+ export default function RootLayout({ children }: { children: React.ReactNode }) {
334
+ return (
335
+ <html lang="en">
336
+ <body>
337
+ {children}
338
+ <MnexiumChat
339
+ endpoint="/api/mnx"
340
+ title="Ask AI"
341
+ buttonLabel="Ask AI"
342
+ position="bottom-right"
343
+ primaryColor="#45b1eb"
344
+ textColor="#fff"
345
+ theme="dark"
346
+ logo="/logo.png"
347
+ welcomeIcon="🤖"
348
+ welcomeMessage="Ask me anything!"
349
+ history={true}
350
+ />
351
+ </body>
352
+ </html>
353
+ );
354
+ }
355
+ ```
356
+
357
+ ## Full Express Example
358
+
359
+ ```
360
+ project/
361
+ ├── server.js
362
+ ├── public/
363
+ │ └── index.html
364
+ ├── package.json
365
+ └── .env
366
+ ```
367
+
368
+ **`server.js`**
369
+
370
+ ```javascript
371
+ import 'dotenv/config';
372
+ import express from 'express';
373
+ import { createRequire } from 'module';
374
+ import { mountMnexiumRoutes } from '@mnexium/chat/server';
375
+
376
+ const require = createRequire(import.meta.url);
377
+
378
+ const app = express();
379
+ app.use(express.json());
380
+ app.use(express.static('public'));
381
+
382
+ // Serve the browser bundle
383
+ app.get('/mnexium-chat.js', (req, res) => {
384
+ res.sendFile(require.resolve('@mnexium/chat/browser'));
385
+ });
386
+
387
+ // Mount API routes
388
+ mountMnexiumRoutes(app, '/api/mnx');
389
+
390
+ app.listen(3000, () => console.log('Server running on http://localhost:3000'));
391
+ ```
392
+
393
+ **`public/index.html`**
394
+
395
+ ```html
396
+ <!DOCTYPE html>
397
+ <html>
398
+ <head>
399
+ <title>My App</title>
400
+ </head>
401
+ <body>
402
+ <h1>Welcome</h1>
403
+
404
+ <!-- Mnexium Chat Widget -->
405
+ <script
406
+ src="/mnexium-chat.js"
407
+ data-endpoint="/api/mnx"
408
+ data-theme="dark"
409
+ ></script>
410
+ </body>
411
+ </html>
412
+ ```
413
+
414
+ **`.env`**
415
+
416
+ ```bash
417
+ MNX_API_KEY=mnx_live_...
418
+ OPENAI_API_KEY=sk-...
419
+ ```
420
+
421
+ See `examples/express-demo` for a complete working example.
422
+
423
+ ## License
424
+
425
+ MIT