@mnexium/sdk 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.
- package/LICENSE +21 -0
- package/README.md +401 -0
- package/dist/index.d.mts +944 -0
- package/dist/index.d.ts +944 -0
- package/dist/index.js +1110 -0
- package/dist/index.mjs +1074 -0
- package/package.json +67 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Mnexium, Inc.
|
|
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,401 @@
|
|
|
1
|
+
# @mnexium/sdk
|
|
2
|
+
|
|
3
|
+
Official Mnexium SDK for JavaScript/TypeScript. Add persistent memory, conversation history, user profiles, and agent state to your AI apps.
|
|
4
|
+
|
|
5
|
+
Works with **OpenAI**, **Anthropic**, and **Google Gemini**. Bring your own API key.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @mnexium/sdk
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Quick Start
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { Mnexium } from '@mnexium/sdk';
|
|
17
|
+
|
|
18
|
+
const mnx = new Mnexium({
|
|
19
|
+
apiKey: 'mnx_...', // Optional - auto-provisions trial key if omitted
|
|
20
|
+
openai: { apiKey: process.env.OPENAI_API_KEY },
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
// Get a Subject handle (no network call)
|
|
24
|
+
const alice = mnx.subject('user_123');
|
|
25
|
+
// Or auto-generate an ID:
|
|
26
|
+
const anon = mnx.subject();
|
|
27
|
+
|
|
28
|
+
// Simple one-off message
|
|
29
|
+
const response = await alice.process('Hello!');
|
|
30
|
+
console.log(response.content);
|
|
31
|
+
|
|
32
|
+
// Multi-turn chat with history
|
|
33
|
+
const chat = alice.createChat({ history: true, learn: true, recall: true });
|
|
34
|
+
await chat.process('My favorite color is blue');
|
|
35
|
+
await chat.process('What is my favorite color?'); // Remembers!
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Core Concepts
|
|
39
|
+
|
|
40
|
+
### Subject
|
|
41
|
+
|
|
42
|
+
A **Subject** is a logical identity (user, agent, org, device) that owns memory, profile, and state. Creating a Subject is instant — no network call.
|
|
43
|
+
|
|
44
|
+
```typescript
|
|
45
|
+
const alice = mnx.subject('user_123');
|
|
46
|
+
|
|
47
|
+
// Subject owns all resources
|
|
48
|
+
await alice.memories.search('hobbies');
|
|
49
|
+
await alice.memories.list();
|
|
50
|
+
await alice.profile.get();
|
|
51
|
+
await alice.state.get('preferences');
|
|
52
|
+
await alice.claims.get('favorite_color');
|
|
53
|
+
await alice.chats.list({ limit: 10 });
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Chat
|
|
57
|
+
|
|
58
|
+
A **Chat** is a conversation thread with a stable `chatId`. Chats belong to a Subject.
|
|
59
|
+
|
|
60
|
+
```typescript
|
|
61
|
+
const chat = alice.createChat({
|
|
62
|
+
model: 'gpt-4o-mini',
|
|
63
|
+
history: true, // Include previous messages
|
|
64
|
+
learn: true, // Extract memories from conversation
|
|
65
|
+
recall: true, // Inject relevant memories into context
|
|
66
|
+
profile: true, // Include user profile in context
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
await chat.process('Hello!');
|
|
70
|
+
await chat.process('What did I just say?'); // Has history
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### Streaming
|
|
74
|
+
|
|
75
|
+
Real-time streaming responses with async iterators:
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
const stream = await chat.process({ content: 'Tell me a story', stream: true });
|
|
79
|
+
|
|
80
|
+
for await (const chunk of stream) {
|
|
81
|
+
process.stdout.write(chunk.content);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// Metadata available after stream completes
|
|
85
|
+
console.log(stream.totalContent); // Full accumulated text
|
|
86
|
+
console.log(stream.usage); // Token counts
|
|
87
|
+
console.log(stream.chatId); // Chat ID
|
|
88
|
+
|
|
89
|
+
// Or collect the full response at once
|
|
90
|
+
const stream2 = await chat.process({ content: 'Summarize', stream: true });
|
|
91
|
+
const text = await stream2.text();
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### Multi-Provider Support
|
|
95
|
+
|
|
96
|
+
The SDK auto-detects the provider from the model name:
|
|
97
|
+
|
|
98
|
+
```typescript
|
|
99
|
+
const mnx = new Mnexium({
|
|
100
|
+
openai: { apiKey: process.env.OPENAI_API_KEY },
|
|
101
|
+
anthropic: { apiKey: process.env.ANTHROPIC_API_KEY },
|
|
102
|
+
google: { apiKey: process.env.GOOGLE_API_KEY },
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
const alice = mnx.subject('user_123');
|
|
106
|
+
const chat = alice.createChat({ learn: true, recall: true });
|
|
107
|
+
|
|
108
|
+
// Switch models freely — memories are shared across providers
|
|
109
|
+
await chat.process({ content: 'I love hiking', model: 'gpt-4o-mini' });
|
|
110
|
+
await chat.process({ content: 'What do I love?', model: 'claude-sonnet-4-20250514' });
|
|
111
|
+
await chat.process({ content: 'Tell me my hobbies', model: 'gemini-2.0-flash' });
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## API Reference
|
|
115
|
+
|
|
116
|
+
### Memories
|
|
117
|
+
|
|
118
|
+
```typescript
|
|
119
|
+
// Add a memory
|
|
120
|
+
const mem = await alice.memories.add('User prefers dark mode', { source: 'settings' });
|
|
121
|
+
|
|
122
|
+
// List memories
|
|
123
|
+
const memories = await alice.memories.list({ limit: 50 });
|
|
124
|
+
|
|
125
|
+
// Semantic search
|
|
126
|
+
const results = await alice.memories.search('preferences', { limit: 5 });
|
|
127
|
+
|
|
128
|
+
// Get a specific memory
|
|
129
|
+
const memory = await alice.memories.get('mem_abc123');
|
|
130
|
+
|
|
131
|
+
// Update a memory
|
|
132
|
+
await alice.memories.update('mem_abc123', { text: 'Updated text' });
|
|
133
|
+
|
|
134
|
+
// Delete a memory
|
|
135
|
+
await alice.memories.delete('mem_abc123');
|
|
136
|
+
|
|
137
|
+
// List superseded memories
|
|
138
|
+
const old = await alice.memories.superseded();
|
|
139
|
+
|
|
140
|
+
// Restore a superseded memory
|
|
141
|
+
await alice.memories.restore('mem_abc123');
|
|
142
|
+
|
|
143
|
+
// Query recall events (which memories were used in a chat)
|
|
144
|
+
const recalls = await alice.memories.recalls({ chatId: 'chat_xyz' });
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
### Real-time Memory Events
|
|
148
|
+
|
|
149
|
+
Subscribe to memory changes via SSE:
|
|
150
|
+
|
|
151
|
+
```typescript
|
|
152
|
+
const events = alice.memories.subscribe();
|
|
153
|
+
|
|
154
|
+
for await (const event of events) {
|
|
155
|
+
switch (event.type) {
|
|
156
|
+
case 'memory.created':
|
|
157
|
+
console.log('New memory:', event.data);
|
|
158
|
+
break;
|
|
159
|
+
case 'memory.superseded':
|
|
160
|
+
console.log('Memory replaced:', event.data);
|
|
161
|
+
break;
|
|
162
|
+
case 'memory.deleted':
|
|
163
|
+
console.log('Memory removed:', event.data);
|
|
164
|
+
break;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// Close the connection
|
|
169
|
+
events.close();
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
Event types: `connected`, `memory.created`, `memory.updated`, `memory.deleted`, `memory.superseded`, `profile.updated`, `heartbeat`
|
|
173
|
+
|
|
174
|
+
### Claims (Structured Facts)
|
|
175
|
+
|
|
176
|
+
```typescript
|
|
177
|
+
// Set a claim
|
|
178
|
+
await alice.claims.set('favorite_color', 'blue', { confidence: 0.95 });
|
|
179
|
+
|
|
180
|
+
// Get a specific slot
|
|
181
|
+
const color = await alice.claims.get('favorite_color');
|
|
182
|
+
|
|
183
|
+
// List all claim slots
|
|
184
|
+
const slots = await alice.claims.list();
|
|
185
|
+
|
|
186
|
+
// Get current truth (all active values)
|
|
187
|
+
const truth = await alice.claims.truth();
|
|
188
|
+
|
|
189
|
+
// Get claim history
|
|
190
|
+
const history = await alice.claims.history();
|
|
191
|
+
|
|
192
|
+
// Retract a claim
|
|
193
|
+
await alice.claims.retract('clm_abc123');
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
### Profile
|
|
197
|
+
|
|
198
|
+
```typescript
|
|
199
|
+
// Get profile
|
|
200
|
+
const profile = await alice.profile.get();
|
|
201
|
+
|
|
202
|
+
// Update profile fields
|
|
203
|
+
await alice.profile.update([
|
|
204
|
+
{ field_key: 'display_name', value: 'Alice' },
|
|
205
|
+
{ field_key: 'timezone', value: 'America/New_York' },
|
|
206
|
+
]);
|
|
207
|
+
|
|
208
|
+
// Delete a profile field
|
|
209
|
+
await alice.profile.deleteField('timezone');
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
### Agent State
|
|
213
|
+
|
|
214
|
+
Persist key-value state for workflows, wizards, and agent continuity:
|
|
215
|
+
|
|
216
|
+
```typescript
|
|
217
|
+
// Set state (with optional TTL)
|
|
218
|
+
await alice.state.set('current_task', { step: 3 }, { ttlSeconds: 3600 });
|
|
219
|
+
|
|
220
|
+
// Get state
|
|
221
|
+
const task = await alice.state.get('current_task');
|
|
222
|
+
|
|
223
|
+
// Delete state
|
|
224
|
+
await alice.state.delete('current_task');
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
### Chat History
|
|
228
|
+
|
|
229
|
+
```typescript
|
|
230
|
+
// List recent chats
|
|
231
|
+
const chats = await alice.chats.list({ limit: 10 });
|
|
232
|
+
|
|
233
|
+
// Read messages from a specific chat
|
|
234
|
+
const messages = await alice.chats.read('chat_abc123');
|
|
235
|
+
|
|
236
|
+
// Delete a chat
|
|
237
|
+
await alice.chats.delete('chat_abc123');
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
### System Prompts
|
|
241
|
+
|
|
242
|
+
```typescript
|
|
243
|
+
// Create a prompt
|
|
244
|
+
const prompt = await mnx.prompts.create({
|
|
245
|
+
name: 'Customer Support',
|
|
246
|
+
promptText: 'You are a helpful customer support agent...',
|
|
247
|
+
isDefault: true,
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
// List all prompts
|
|
251
|
+
const prompts = await mnx.prompts.list();
|
|
252
|
+
|
|
253
|
+
// Update a prompt
|
|
254
|
+
await mnx.prompts.update(prompt.id, { promptText: 'Updated instructions...' });
|
|
255
|
+
|
|
256
|
+
// Preview which prompts will be injected
|
|
257
|
+
const resolved = await mnx.prompts.resolve({ subjectId: 'user_123' });
|
|
258
|
+
|
|
259
|
+
// Use in chat
|
|
260
|
+
const chat = alice.createChat({ systemPrompt: prompt.id });
|
|
261
|
+
|
|
262
|
+
// Delete a prompt
|
|
263
|
+
await mnx.prompts.delete(prompt.id);
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
## Configuration
|
|
267
|
+
|
|
268
|
+
```typescript
|
|
269
|
+
const mnx = new Mnexium({
|
|
270
|
+
apiKey: 'mnx_...', // Optional — auto-provisions trial key
|
|
271
|
+
baseUrl: 'https://mnexium.com/api/v1', // API base URL
|
|
272
|
+
timeout: 30000, // Request timeout (ms)
|
|
273
|
+
maxRetries: 2, // Retry count for failed requests
|
|
274
|
+
openai: { apiKey: '...' }, // OpenAI config
|
|
275
|
+
anthropic: { apiKey: '...' }, // Anthropic config
|
|
276
|
+
google: { apiKey: '...' }, // Google config
|
|
277
|
+
defaults: { // Default options for all process() calls
|
|
278
|
+
model: 'gpt-4o-mini',
|
|
279
|
+
learn: true,
|
|
280
|
+
recall: false,
|
|
281
|
+
profile: false,
|
|
282
|
+
history: true,
|
|
283
|
+
},
|
|
284
|
+
});
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
### Mnx Parameters
|
|
288
|
+
|
|
289
|
+
Every `process()` and `chat.process()` call supports these options:
|
|
290
|
+
|
|
291
|
+
| Parameter | Type | Default | Description |
|
|
292
|
+
|-----------|------|---------|-------------|
|
|
293
|
+
| `model` | string | `'gpt-4o-mini'` | Model to use |
|
|
294
|
+
| `learn` | boolean/`'force'` | `true` | Extract memories from conversation |
|
|
295
|
+
| `recall` | boolean | `false` | Inject relevant memories into context |
|
|
296
|
+
| `profile` | boolean | `false` | Include user profile in context |
|
|
297
|
+
| `history` | boolean | `true` | Prepend previous messages from this chat |
|
|
298
|
+
| `log` | boolean | `true` | Save messages to chat history |
|
|
299
|
+
| `summarize` | boolean/string | `false` | `'light'`, `'balanced'`, or `'aggressive'` |
|
|
300
|
+
| `systemPrompt` | boolean/string | `true` | `true` (auto), `false` (skip), or prompt ID |
|
|
301
|
+
| `stream` | boolean | `false` | Enable streaming response |
|
|
302
|
+
| `metadata` | object | — | Custom metadata attached to saved logs |
|
|
303
|
+
|
|
304
|
+
## Trial Keys
|
|
305
|
+
|
|
306
|
+
If you don't provide an API key, Mnexium auto-provisions a trial key:
|
|
307
|
+
|
|
308
|
+
```typescript
|
|
309
|
+
const mnx = new Mnexium({
|
|
310
|
+
openai: { apiKey: process.env.OPENAI_API_KEY },
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
const alice = mnx.subject('user_123');
|
|
314
|
+
await alice.process('Hello!');
|
|
315
|
+
|
|
316
|
+
// Get the provisioned key to save it
|
|
317
|
+
const trial = mnx.getTrialInfo();
|
|
318
|
+
console.log('Save this key:', trial?.key);
|
|
319
|
+
```
|
|
320
|
+
|
|
321
|
+
Claim your trial key at [mnexium.com/claim](https://mnexium.com/claim).
|
|
322
|
+
|
|
323
|
+
## Error Handling
|
|
324
|
+
|
|
325
|
+
```typescript
|
|
326
|
+
import { Mnexium, AuthenticationError, RateLimitError, APIError } from '@mnexium/sdk';
|
|
327
|
+
|
|
328
|
+
try {
|
|
329
|
+
await alice.process('Hello!');
|
|
330
|
+
} catch (error) {
|
|
331
|
+
if (error instanceof AuthenticationError) {
|
|
332
|
+
console.error('Invalid API key');
|
|
333
|
+
} else if (error instanceof RateLimitError) {
|
|
334
|
+
console.error(`Rate limited. Current: ${error.current}, Limit: ${error.limit}`);
|
|
335
|
+
} else if (error instanceof APIError) {
|
|
336
|
+
console.error(`API error ${error.status}: ${error.message}`);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
```
|
|
340
|
+
|
|
341
|
+
## Raw Response
|
|
342
|
+
|
|
343
|
+
Access the full API response for advanced use:
|
|
344
|
+
|
|
345
|
+
```typescript
|
|
346
|
+
const response = await chat.process('Hello!');
|
|
347
|
+
console.log(response.raw); // Full API response object
|
|
348
|
+
console.log(response.chatId); // Chat ID
|
|
349
|
+
console.log(response.usage); // { promptTokens, completionTokens, totalTokens }
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
## Examples
|
|
353
|
+
|
|
354
|
+
See the [examples/](./examples/) directory for runnable demos:
|
|
355
|
+
|
|
356
|
+
| Example | Description |
|
|
357
|
+
|---------|-------------|
|
|
358
|
+
| `npm run hello` | Hello world — chat, history, recall |
|
|
359
|
+
| `npm run streaming` | Real-time streaming responses |
|
|
360
|
+
| `npm run events` | Real-time memory event subscriptions |
|
|
361
|
+
| `npm run memories` | Add, list, search, delete memories |
|
|
362
|
+
| `npm run claims` | Claims extraction and manual setting |
|
|
363
|
+
| `npm run state` | Agent state with TTL |
|
|
364
|
+
| `npm run profile` | User profiles |
|
|
365
|
+
| `npm run prompts` | System prompt management |
|
|
366
|
+
| `npm run multi` | Multi-provider (OpenAI, Claude, Gemini) |
|
|
367
|
+
| `npm run full` | Full API demo |
|
|
368
|
+
|
|
369
|
+
## TypeScript
|
|
370
|
+
|
|
371
|
+
Full TypeScript support with exported types:
|
|
372
|
+
|
|
373
|
+
```typescript
|
|
374
|
+
import type {
|
|
375
|
+
MnexiumConfig,
|
|
376
|
+
ChatOptions,
|
|
377
|
+
ProcessOptions,
|
|
378
|
+
ProcessResponse,
|
|
379
|
+
ChatHistoryItem,
|
|
380
|
+
StreamChunk,
|
|
381
|
+
MemoryEvent,
|
|
382
|
+
} from '@mnexium/sdk';
|
|
383
|
+
|
|
384
|
+
import { StreamResponse, EventStream } from '@mnexium/sdk';
|
|
385
|
+
```
|
|
386
|
+
|
|
387
|
+
## Supported Models
|
|
388
|
+
|
|
389
|
+
- **OpenAI:** gpt-4o, gpt-4o-mini, gpt-4-turbo, gpt-4, gpt-3.5-turbo, o1, o1-mini, o3
|
|
390
|
+
- **Anthropic:** claude-3-opus, claude-3-sonnet, claude-3-haiku, claude-3-5-sonnet, claude-sonnet-4
|
|
391
|
+
- **Google Gemini:** gemini-2.0-flash-lite, gemini-2.5-flash, gemini-1.5-pro, gemini-1.5-flash
|
|
392
|
+
|
|
393
|
+
## Links
|
|
394
|
+
|
|
395
|
+
- [Documentation](https://mnexium.com/docs)
|
|
396
|
+
- [Blog](https://mnexium.com/blogs)
|
|
397
|
+
- [Sign Up](https://mnexium.com)
|
|
398
|
+
|
|
399
|
+
## License
|
|
400
|
+
|
|
401
|
+
MIT
|