@smartmemory/sdk-js 0.5.1
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/README.md +266 -0
- package/dist/core-Ds7Kn35C.js +2476 -0
- package/dist/core.js +20 -0
- package/dist/fetch.js +43 -0
- package/dist/index.js +102 -0
- package/dist/progress.js +191 -0
- package/dist/react.js +77 -0
- package/package.json +68 -0
package/README.md
ADDED
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
# @smartmemory/sdk-js
|
|
2
|
+
|
|
3
|
+
Unified JavaScript SDK for [SmartMemory](https://smartmemory.ai) — consolidates authentication and API client logic across all SmartMemory frontend applications.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **SSO auth (Clerk)**: Cookie-based session bootstrap via `/auth/me`; redirect to Clerk IdP for login
|
|
8
|
+
- **Automatic token refresh**: Single-flight deduplication prevents race conditions
|
|
9
|
+
- **80+ API methods**: Full coverage of SmartMemory's REST API across 10 domain modules
|
|
10
|
+
- **React bindings**: Provider, hooks, and route protection components
|
|
11
|
+
- **Zero runtime dependencies**: Framework-agnostic core with optional React layer
|
|
12
|
+
- **Tiny bundle**: 5.6 KB gzipped (main entry)
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install @smartmemory/sdk-js
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Quick Start
|
|
21
|
+
|
|
22
|
+
### SSO Mode (All Apps — Clerk-backed)
|
|
23
|
+
|
|
24
|
+
```javascript
|
|
25
|
+
import { SmartMemoryClient } from '@smartmemory/sdk-js';
|
|
26
|
+
|
|
27
|
+
const client = new SmartMemoryClient({
|
|
28
|
+
mode: 'sso',
|
|
29
|
+
apiBaseUrl: 'http://localhost:9001',
|
|
30
|
+
webAppUrl: 'http://localhost:5173',
|
|
31
|
+
endpoints: { refresh: '/auth/refresh' }
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
// In SSO mode, auth is bootstrapped via /auth/me using the sm_access_token cookie
|
|
35
|
+
// set by the Clerk-hosted login flow. No local login form or token URL params.
|
|
36
|
+
await client.auth.bootstrapSession(); // calls GET /auth/me, credentials: 'include'
|
|
37
|
+
|
|
38
|
+
// If unauthenticated, redirect to the Clerk IdP:
|
|
39
|
+
if (!client.auth.isAuthenticated()) {
|
|
40
|
+
window.location.href = client.auth.getLoginUrl(window.location.href);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Use the API (cookie auth is automatic)
|
|
44
|
+
const memories = await client.memories.list({ limit: 10 });
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### React
|
|
48
|
+
|
|
49
|
+
```jsx
|
|
50
|
+
import { SmartMemoryProvider, useAuth, useSmartMemory } from '@smartmemory/sdk-js/react';
|
|
51
|
+
|
|
52
|
+
function App() {
|
|
53
|
+
return (
|
|
54
|
+
<SmartMemoryProvider
|
|
55
|
+
mode="sso"
|
|
56
|
+
apiBaseUrl="http://localhost:9001"
|
|
57
|
+
webAppUrl="http://localhost:5173"
|
|
58
|
+
endpoints={{ refresh: '/auth/refresh' }}
|
|
59
|
+
>
|
|
60
|
+
<Dashboard />
|
|
61
|
+
</SmartMemoryProvider>
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function Dashboard() {
|
|
66
|
+
const { isAuthenticated, user, logout } = useAuth();
|
|
67
|
+
const client = useSmartMemory();
|
|
68
|
+
|
|
69
|
+
if (!isAuthenticated) return <LoginRedirect />;
|
|
70
|
+
|
|
71
|
+
return (
|
|
72
|
+
<div>
|
|
73
|
+
<p>Welcome, {user.name}</p>
|
|
74
|
+
<button onClick={logout}>Logout</button>
|
|
75
|
+
</div>
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## Entry Points
|
|
81
|
+
|
|
82
|
+
| Import | Contents |
|
|
83
|
+
|--------|----------|
|
|
84
|
+
| `@smartmemory/sdk-js` | SmartMemoryClient, AuthCore, TokenManager, all domain APIs, APIError |
|
|
85
|
+
| `@smartmemory/sdk-js/core` | Everything above + BaseAPI, RefreshManager, SSOManager (internal access) |
|
|
86
|
+
| `@smartmemory/sdk-js/react` | SmartMemoryProvider, useAuth, useAuthState, useAuthActions, useSmartMemory, AuthWrapper |
|
|
87
|
+
| `@smartmemory/sdk-js/fetch` | createAuthFetch, installInterceptor |
|
|
88
|
+
|
|
89
|
+
## Configuration
|
|
90
|
+
|
|
91
|
+
```javascript
|
|
92
|
+
const client = new SmartMemoryClient({
|
|
93
|
+
// Required
|
|
94
|
+
mode: 'custom' | 'sso',
|
|
95
|
+
apiBaseUrl: 'http://localhost:9001',
|
|
96
|
+
|
|
97
|
+
// SSO mode only
|
|
98
|
+
webAppUrl: 'http://localhost:5173',
|
|
99
|
+
allowedRedirectHosts: ['studio.smartmemory.ai'],
|
|
100
|
+
|
|
101
|
+
// Optional
|
|
102
|
+
endpoints: {
|
|
103
|
+
refresh: '/auth/refresh', // token refresh
|
|
104
|
+
logout: '/auth/logout' // optional override
|
|
105
|
+
},
|
|
106
|
+
storage: 'localStorage', // 'localStorage' | 'sessionStorage' | 'memory'
|
|
107
|
+
tokenKeys: { // custom storage key names
|
|
108
|
+
access: 'smart_memory_auth_token',
|
|
109
|
+
refresh: 'smart_memory_refresh_token',
|
|
110
|
+
user: 'smart_memory_user',
|
|
111
|
+
tenant: 'smart_memory_tenant_id',
|
|
112
|
+
team: 'smart_memory_team_id'
|
|
113
|
+
},
|
|
114
|
+
onTokenRefresh: (newToken) => {} // callback after successful refresh
|
|
115
|
+
});
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## Domain APIs
|
|
119
|
+
|
|
120
|
+
### Memories
|
|
121
|
+
|
|
122
|
+
```javascript
|
|
123
|
+
client.memories.create({ content: 'text', memoryType: 'semantic' });
|
|
124
|
+
client.memories.get(id);
|
|
125
|
+
client.memories.update(id, { content: 'updated' });
|
|
126
|
+
client.memories.delete(id);
|
|
127
|
+
client.memories.list({ limit: 50, offset: 0, type: 'semantic' });
|
|
128
|
+
client.memories.search('query', { topK: 5, enableHybrid: true });
|
|
129
|
+
client.memories.searchAdvanced('query', { algorithm: 'query_traversal' });
|
|
130
|
+
client.memories.ingest('content', { extractorName: 'llm' });
|
|
131
|
+
client.memories.getSummary();
|
|
132
|
+
client.memories.link(sourceId, targetId, 'RELATED');
|
|
133
|
+
client.memories.enrich(itemId, ['routine1']);
|
|
134
|
+
client.memories.getHistory(itemId);
|
|
135
|
+
client.memories.timeTravel(timestamp);
|
|
136
|
+
client.memories.rollback(itemId, { toVersion: 3 });
|
|
137
|
+
client.memories.runClustering(0.1, false);
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### Decisions
|
|
141
|
+
|
|
142
|
+
```javascript
|
|
143
|
+
client.decisions.list({ status: 'pending' });
|
|
144
|
+
client.decisions.listPending(50);
|
|
145
|
+
client.decisions.getProofTree(decisionId, 5);
|
|
146
|
+
client.decisions.getFuzzyConfidence(decisionId);
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
### Graph
|
|
150
|
+
|
|
151
|
+
```javascript
|
|
152
|
+
client.graph.getNeighbors(itemId);
|
|
153
|
+
client.graph.addEdge(sourceId, targetId, 'RELATED', { weight: 0.5 });
|
|
154
|
+
client.graph.getHealth();
|
|
155
|
+
client.graph.getInferenceRules();
|
|
156
|
+
client.graph.runInference(['transitivity']);
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
### Teams
|
|
160
|
+
|
|
161
|
+
```javascript
|
|
162
|
+
client.teams.list();
|
|
163
|
+
client.teams.create({ name: 'Engineering' });
|
|
164
|
+
client.teams.get(teamId);
|
|
165
|
+
client.teams.update(teamId, { name: 'New Name' });
|
|
166
|
+
client.teams.delete(teamId);
|
|
167
|
+
client.teams.getMembers(teamId);
|
|
168
|
+
client.teams.addMember(teamId, userId, 'admin');
|
|
169
|
+
client.teams.updateMember(teamId, userId, 'member');
|
|
170
|
+
client.teams.removeMember(teamId, userId);
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
### Other APIs
|
|
174
|
+
|
|
175
|
+
```javascript
|
|
176
|
+
// Profiles & LLM Keys
|
|
177
|
+
client.profiles.list();
|
|
178
|
+
client.profiles.get('default');
|
|
179
|
+
client.profiles.getLLMKeys();
|
|
180
|
+
client.profiles.updateLLMKeys({ openai: 'sk-...' });
|
|
181
|
+
|
|
182
|
+
// Subscriptions
|
|
183
|
+
client.subscriptions.getCurrent();
|
|
184
|
+
client.subscriptions.upgrade('pro');
|
|
185
|
+
client.subscriptions.createCheckoutSession('pro', 'monthly');
|
|
186
|
+
|
|
187
|
+
// Usage
|
|
188
|
+
client.usage.getDashboard();
|
|
189
|
+
client.usage.getCurrent();
|
|
190
|
+
client.usage.getTiers();
|
|
191
|
+
|
|
192
|
+
// Agents
|
|
193
|
+
client.agents.list();
|
|
194
|
+
client.agents.create({ name: 'Researcher' });
|
|
195
|
+
|
|
196
|
+
// Insights
|
|
197
|
+
client.insights.getHealth();
|
|
198
|
+
client.insights.getReflection();
|
|
199
|
+
client.insights.getMaintenanceStatus();
|
|
200
|
+
client.insights.getPlugins();
|
|
201
|
+
|
|
202
|
+
// Auth API (signup, password reset, API keys)
|
|
203
|
+
client.authAPI.signup({ email, password, fullName });
|
|
204
|
+
client.authAPI.getCurrentUser();
|
|
205
|
+
client.authAPI.requestPasswordReset(email);
|
|
206
|
+
client.authAPI.createAPIKey(name, scopes);
|
|
207
|
+
client.authAPI.listAPIKeys();
|
|
208
|
+
client.authAPI.revokeAPIKey(keyId);
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
## Fetch Utilities
|
|
212
|
+
|
|
213
|
+
```javascript
|
|
214
|
+
import { createAuthFetch, installInterceptor } from '@smartmemory/sdk-js/fetch';
|
|
215
|
+
|
|
216
|
+
// Wrap individual fetch calls
|
|
217
|
+
const authFetch = createAuthFetch(client.auth);
|
|
218
|
+
const response = await authFetch('http://api.example.com/data');
|
|
219
|
+
|
|
220
|
+
// Or intercept all fetch calls globally
|
|
221
|
+
const uninstall = installInterceptor(client.auth, {
|
|
222
|
+
urlPatterns: ['localhost:9001']
|
|
223
|
+
});
|
|
224
|
+
// ... all matching fetch calls now include auth headers
|
|
225
|
+
uninstall(); // restore original fetch
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
## Migration from AuthService.js
|
|
229
|
+
|
|
230
|
+
Replace the per-app AuthService pattern:
|
|
231
|
+
|
|
232
|
+
```diff
|
|
233
|
+
- import { authService } from '../services/AuthService';
|
|
234
|
+
- import { authFetch } from '../services/AuthService';
|
|
235
|
+
+ import { SmartMemoryClient } from '@smartmemory/sdk-js';
|
|
236
|
+
+
|
|
237
|
+
+ const client = new SmartMemoryClient({
|
|
238
|
+
+ mode: 'sso',
|
|
239
|
+
+ apiBaseUrl: import.meta.env.VITE_API_URL,
|
|
240
|
+
+ webAppUrl: import.meta.env.VITE_WEB_APP_URL,
|
|
241
|
+
+ endpoints: { refresh: '/auth/refresh' }
|
|
242
|
+
+ });
|
|
243
|
+
|
|
244
|
+
- authService.getLoginUrl()
|
|
245
|
+
+ client.auth.getLoginUrl()
|
|
246
|
+
|
|
247
|
+
- authService.storeCallbackTokens(searchParams)
|
|
248
|
+
+ client.auth.storeCallbackTokens(searchParams)
|
|
249
|
+
|
|
250
|
+
- authFetch('/memory/list')
|
|
251
|
+
+ client.memories.list()
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
## Development
|
|
255
|
+
|
|
256
|
+
```bash
|
|
257
|
+
npm install
|
|
258
|
+
npm test # run tests
|
|
259
|
+
npm run test:watch # watch mode
|
|
260
|
+
npm run test:coverage # with coverage
|
|
261
|
+
npm run build # production build
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
## License
|
|
265
|
+
|
|
266
|
+
MIT
|