smritea-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/README.md ADDED
@@ -0,0 +1,209 @@
1
+ # smritea SDK — TypeScript / Node.js
2
+
3
+ TypeScript SDK for the [smritea](https://smritea.ai) AI memory system.
4
+
5
+ **[Get a free API key →](https://smritea.ai)**
6
+
7
+ ---
8
+
9
+ ## Installation
10
+
11
+ ```bash
12
+ npm install smritea-sdk
13
+ ```
14
+
15
+ Requires Node.js 18+. Ships as ESM + CJS with bundled type declarations.
16
+
17
+ ---
18
+
19
+ ## Get your API key
20
+
21
+ 1. Sign up at **[smritea.ai](https://smritea.ai)** — free account, no credit card required
22
+ 2. Create an app in the dashboard and copy your API key (`sk-...`) and App ID (`app_...`)
23
+ 3. Export them as environment variables:
24
+
25
+ ```bash
26
+ export SMRITEA_API_KEY="sk-..."
27
+ export SMRITEA_APP_ID="app_..."
28
+ ```
29
+
30
+ ---
31
+
32
+ ## Quickstart
33
+
34
+ ```typescript
35
+ import { SmriteaClient } from 'smritea-sdk';
36
+
37
+ const client = new SmriteaClient({
38
+ apiKey: process.env.SMRITEA_API_KEY!,
39
+ appId: process.env.SMRITEA_APP_ID!,
40
+ });
41
+
42
+ // Store something about a user
43
+ await client.add('Alice is a vegetarian and loves hiking', { userId: 'alice' });
44
+
45
+ // Retrieve it later in a different session
46
+ const results = await client.search("What are Alice's food preferences?", { userId: 'alice' });
47
+ for (const r of results) {
48
+ console.log(r.score?.toFixed(2), r.content);
49
+ }
50
+ ```
51
+
52
+ ---
53
+
54
+ ## Constructor
55
+
56
+ ```typescript
57
+ import { SmriteaClient } from 'smritea-sdk';
58
+
59
+ const client = new SmriteaClient({
60
+ apiKey: 'sk-...', // required
61
+ appId: 'app_...', // required
62
+ baseUrl: 'https://api.smritea.ai', // optional, default shown
63
+ });
64
+ ```
65
+
66
+ ---
67
+
68
+ ## Methods
69
+
70
+ All methods are async and return Promises.
71
+
72
+ ### `add` — Store a memory
73
+
74
+ ```typescript
75
+ const memory = await client.add('User prefers concise replies', {
76
+ userId: 'alice',
77
+ metadata: { source: 'chat' },
78
+ conversationId: 'conv_123',
79
+ });
80
+ console.log(memory.id); // mem_...
81
+ ```
82
+
83
+ `userId` is a shorthand — sets `actorId` and forces `actorType="user"`. For agent or system memories use `actorId` + `actorType` directly.
84
+
85
+ | Option | Default | Description |
86
+ |---|---|---|
87
+ | `userId` | `undefined` | Shorthand: actorId + actorType="user" |
88
+ | `actorId` | `undefined` | Explicit actor ID |
89
+ | `actorType` | `"user"` | `"user"` \| `"agent"` \| `"system"` |
90
+ | `actorName` | `undefined` | Display name |
91
+ | `metadata` | `undefined` | Arbitrary key-value object |
92
+ | `conversationId` | `undefined` | Conversation context |
93
+
94
+ ---
95
+
96
+ ### `search` — Semantic search
97
+
98
+ ```typescript
99
+ const results = await client.search('dietary restrictions', {
100
+ userId: 'alice',
101
+ limit: 5,
102
+ method: 'deep_search', // "quick_search" | "deep_search" | "context_aware_search"
103
+ threshold: 0.7, // min relevance score 0.0–1.0
104
+ });
105
+ results.forEach(r => console.log(r.score, r.content));
106
+ ```
107
+
108
+ Results are ordered by relevance (descending). Each result exposes `score` (0.0–1.0) and all `Memory` fields directly.
109
+
110
+ | Search method | When to use |
111
+ |---|---|
112
+ | `quick_search` | Low-latency, keyword + vector hybrid |
113
+ | `deep_search` | Higher recall, traverses the memory graph |
114
+ | `context_aware_search` | Reranks results using conversation context |
115
+
116
+ | Option | Default | Description |
117
+ |---|---|---|
118
+ | `userId` | `undefined` | Filter to this user's memories |
119
+ | `actorId` | `undefined` | Filter by actor ID |
120
+ | `actorType` | `undefined` | Filter by actor type |
121
+ | `limit` | app default | Max results to return |
122
+ | `method` | app default | Search strategy |
123
+ | `threshold` | `undefined` | Min relevance score 0.0–1.0 |
124
+ | `graphDepth` | `undefined` | Graph traversal depth override |
125
+ | `conversationId` | `undefined` | Conversation context |
126
+
127
+ ---
128
+
129
+ ### `get` — Retrieve a memory by ID
130
+
131
+ ```typescript
132
+ const memory = await client.get('mem_abc123');
133
+ console.log(memory.content, memory.createdAt);
134
+ // Throws SmriteaNotFoundError if the ID does not exist
135
+ ```
136
+
137
+ ---
138
+
139
+ ### `delete` — Delete a memory by ID
140
+
141
+ ```typescript
142
+ await client.delete('mem_abc123');
143
+ // Throws SmriteaNotFoundError if the ID does not exist
144
+ ```
145
+
146
+ ---
147
+
148
+ ### `get_all` — List all memories
149
+
150
+ > **Not yet implemented.** There is no `get_all` method in the TypeScript SDK.
151
+ > Use `search()` with a broad query as a workaround:
152
+
153
+ ```typescript
154
+ const results = await client.search('', { userId: 'alice', limit: 100 });
155
+ ```
156
+
157
+ ---
158
+
159
+ ## Error handling
160
+
161
+ ```typescript
162
+ import {
163
+ SmriteaAuthError,
164
+ SmriteaNotFoundError,
165
+ SmriteaRateLimitError,
166
+ SmriteaQuotaError,
167
+ SmriteaValidationError,
168
+ SmriteaError,
169
+ } from 'smritea-sdk';
170
+
171
+ try {
172
+ await client.delete('mem_unknown');
173
+ } catch (e) {
174
+ if (e instanceof SmriteaNotFoundError) console.log('Memory not found');
175
+ if (e instanceof SmriteaRateLimitError) console.log(`Retry after ${e.retryAfter}s`);
176
+ if (e instanceof SmriteaAuthError) console.log('Invalid API key');
177
+ if (e instanceof SmriteaQuotaError) console.log('Plan quota exceeded');
178
+ }
179
+ ```
180
+
181
+ | Exception | HTTP | When |
182
+ |---|---|---|
183
+ | `SmriteaAuthError` | 401 | Invalid or missing API key |
184
+ | `SmriteaValidationError` | 400 | Invalid request parameters |
185
+ | `SmriteaNotFoundError` | 404 | Memory ID does not exist |
186
+ | `SmriteaQuotaError` | 402 | Organisation quota exceeded |
187
+ | `SmriteaRateLimitError` | 429 | Rate limit hit — check `.retryAfter` |
188
+ | `SmriteaError` | other | Unexpected server error |
189
+
190
+ ---
191
+
192
+ ## `Memory` type reference
193
+
194
+ All fields use `camelCase`.
195
+
196
+ | Field | Type | Description |
197
+ |---|---|---|
198
+ | `id` | `string` | Memory ID (`mem_...`) |
199
+ | `appId` | `string` | App this memory belongs to |
200
+ | `content` | `string` | Memory text |
201
+ | `actorId` | `string` | Actor who owns this memory |
202
+ | `actorType` | `string` | `"user"` \| `"agent"` \| `"system"` |
203
+ | `actorName` | `string?` | Display name |
204
+ | `metadata` | `object?` | Arbitrary key-value pairs |
205
+ | `conversationId` | `string?` | Conversation context |
206
+ | `activeFrom` | `string` | ISO 8601 — when memory becomes valid |
207
+ | `activeTo` | `string?` | ISO 8601 — when memory expires |
208
+ | `createdAt` | `string` | ISO 8601 creation timestamp |
209
+ | `updatedAt` | `string` | ISO 8601 last update timestamp |
@@ -0,0 +1,291 @@
1
+ /**
2
+ * smritea SDK API
3
+ * Public SDK API for smritea AI memory system — programmatic memory operations via API key
4
+ *
5
+ * The version of the OpenAPI document: 1.0.0
6
+ * Contact: support@smritea.ai
7
+ *
8
+ * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
9
+ * https://openapi-generator.tech
10
+ * Do not edit the class manually.
11
+ */
12
+ /**
13
+ *
14
+ * @export
15
+ * @interface CommondtoRelativeStandingConfig
16
+ */
17
+ interface CommondtoRelativeStandingConfig {
18
+ /**
19
+ * DecayFactor modulates the temporal decay rate: effectiveRate = baseRate × decay_factor.
20
+ * 0 = no decay (pinned), 0.2 = light, 1.0 = standard, 3.0+ = aggressive.
21
+ * @type {number}
22
+ * @memberof CommondtoRelativeStandingConfig
23
+ */
24
+ decayFactor?: number;
25
+ /**
26
+ * DecayFunction selects the temporal decay algorithm.
27
+ * Defaults to "exponential". Options: exponential, gaussian, linear.
28
+ * @type {string}
29
+ * @memberof CommondtoRelativeStandingConfig
30
+ */
31
+ decayFunction?: CommondtoRelativeStandingConfigDecayFunctionEnum;
32
+ /**
33
+ * Importance is the memory importance score (0-1, default 1.0).
34
+ * Used as a ranking signal in RRF reranking — higher importance = better rank.
35
+ * @type {number}
36
+ * @memberof CommondtoRelativeStandingConfig
37
+ */
38
+ importance?: number;
39
+ }
40
+ /**
41
+ * @export
42
+ */
43
+ declare const CommondtoRelativeStandingConfigDecayFunctionEnum: {
44
+ readonly Exponential: "exponential";
45
+ readonly Gaussian: "gaussian";
46
+ readonly Linear: "linear";
47
+ };
48
+ type CommondtoRelativeStandingConfigDecayFunctionEnum = typeof CommondtoRelativeStandingConfigDecayFunctionEnum[keyof typeof CommondtoRelativeStandingConfigDecayFunctionEnum];
49
+
50
+ /**
51
+ * smritea SDK API
52
+ * Public SDK API for smritea AI memory system — programmatic memory operations via API key
53
+ *
54
+ * The version of the OpenAPI document: 1.0.0
55
+ * Contact: support@smritea.ai
56
+ *
57
+ * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
58
+ * https://openapi-generator.tech
59
+ * Do not edit the class manually.
60
+ */
61
+
62
+ /**
63
+ *
64
+ * @export
65
+ * @interface MemoryMemoryResponse
66
+ */
67
+ interface MemoryMemoryResponse {
68
+ /**
69
+ *
70
+ * @type {string}
71
+ * @memberof MemoryMemoryResponse
72
+ */
73
+ activeFrom?: string;
74
+ /**
75
+ *
76
+ * @type {string}
77
+ * @memberof MemoryMemoryResponse
78
+ */
79
+ activeTo?: string;
80
+ /**
81
+ *
82
+ * @type {string}
83
+ * @memberof MemoryMemoryResponse
84
+ */
85
+ actorId?: string;
86
+ /**
87
+ *
88
+ * @type {string}
89
+ * @memberof MemoryMemoryResponse
90
+ */
91
+ actorName?: string;
92
+ /**
93
+ *
94
+ * @type {string}
95
+ * @memberof MemoryMemoryResponse
96
+ */
97
+ actorType?: string;
98
+ /**
99
+ *
100
+ * @type {string}
101
+ * @memberof MemoryMemoryResponse
102
+ */
103
+ appId?: string;
104
+ /**
105
+ *
106
+ * @type {string}
107
+ * @memberof MemoryMemoryResponse
108
+ */
109
+ content?: string;
110
+ /**
111
+ *
112
+ * @type {string}
113
+ * @memberof MemoryMemoryResponse
114
+ */
115
+ conversationId?: string;
116
+ /**
117
+ *
118
+ * @type {string}
119
+ * @memberof MemoryMemoryResponse
120
+ */
121
+ conversationMessageId?: string;
122
+ /**
123
+ *
124
+ * @type {string}
125
+ * @memberof MemoryMemoryResponse
126
+ */
127
+ createdAt?: string;
128
+ /**
129
+ *
130
+ * @type {string}
131
+ * @memberof MemoryMemoryResponse
132
+ */
133
+ id?: string;
134
+ /**
135
+ *
136
+ * @type {object}
137
+ * @memberof MemoryMemoryResponse
138
+ */
139
+ metadata?: object;
140
+ /**
141
+ *
142
+ * @type {CommondtoRelativeStandingConfig}
143
+ * @memberof MemoryMemoryResponse
144
+ */
145
+ relativeStanding?: CommondtoRelativeStandingConfig;
146
+ /**
147
+ *
148
+ * @type {string}
149
+ * @memberof MemoryMemoryResponse
150
+ */
151
+ updatedAt?: string;
152
+ }
153
+
154
+ /**
155
+ * smritea SDK API
156
+ * Public SDK API for smritea AI memory system — programmatic memory operations via API key
157
+ *
158
+ * The version of the OpenAPI document: 1.0.0
159
+ * Contact: support@smritea.ai
160
+ *
161
+ * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
162
+ * https://openapi-generator.tech
163
+ * Do not edit the class manually.
164
+ */
165
+
166
+ /**
167
+ *
168
+ * @export
169
+ * @interface MemorySearchMemoryResponse
170
+ */
171
+ interface MemorySearchMemoryResponse {
172
+ /**
173
+ *
174
+ * @type {MemoryMemoryResponse}
175
+ * @memberof MemorySearchMemoryResponse
176
+ */
177
+ memory?: MemoryMemoryResponse;
178
+ /**
179
+ *
180
+ * @type {number}
181
+ * @memberof MemorySearchMemoryResponse
182
+ */
183
+ score?: number;
184
+ }
185
+
186
+ /**
187
+ * Public-facing types for the smritea TypeScript SDK.
188
+ *
189
+ * Memory and SearchResult are re-exported directly from the auto-generated SDK
190
+ * so that the type contract matches the API exactly. Field names are camelCase
191
+ * as produced by the openapi-generator TypeScript mapper.
192
+ */
193
+
194
+ interface AddMemoryOptions {
195
+ /** Shorthand: sets actor_id and actor_type="user". Takes precedence if actorId also set. */
196
+ userId?: string;
197
+ /** Explicit actor ID. Use when actor_type is not "user". */
198
+ actorId?: string;
199
+ actorType?: string;
200
+ actorName?: string;
201
+ metadata?: Record<string, unknown>;
202
+ conversationId?: string;
203
+ }
204
+ interface SearchOptions {
205
+ /** Shorthand: sets actor_id and actor_type="user" filter. */
206
+ userId?: string;
207
+ actorId?: string;
208
+ actorType?: string;
209
+ limit?: number;
210
+ method?: string;
211
+ threshold?: number;
212
+ graphDepth?: number;
213
+ conversationId?: string;
214
+ }
215
+ interface SmriteaClientConfig {
216
+ apiKey: string;
217
+ appId: string;
218
+ /** Override API base URL. Defaults to https://api.smritea.ai */
219
+ baseUrl?: string;
220
+ /**
221
+ * Automatic retries on HTTP 429. Uses Retry-After header when the server
222
+ * provides one, otherwise exponential backoff with jitter (capped at 30 s).
223
+ * Set to 0 to disable. Default: 2.
224
+ */
225
+ maxRetries?: number;
226
+ }
227
+
228
+ declare class SmriteaClient {
229
+ private readonly appId;
230
+ private readonly api;
231
+ private readonly maxRetries;
232
+ constructor(config: SmriteaClientConfig);
233
+ add(content: string, options?: AddMemoryOptions): Promise<MemoryMemoryResponse>;
234
+ search(query: string, options?: SearchOptions): Promise<MemorySearchMemoryResponse[]>;
235
+ get(memoryId: string): Promise<MemoryMemoryResponse>;
236
+ delete(memoryId: string): Promise<void>;
237
+ /**
238
+ * Execute fn, retrying on HTTP 429 up to maxRetries times.
239
+ *
240
+ * Sleep strategy per attempt:
241
+ * 1. Retry-After header value (seconds), capped at 30 s.
242
+ * 2. Exponential backoff (1 s, 2 s, 4 s, …) with ±25 % jitter, capped at 30 s.
243
+ *
244
+ * After all retries are exhausted the 429 is re-raised as SmriteaRateLimitError
245
+ * with retryAfter populated from the final response header if available.
246
+ */
247
+ private withRetry;
248
+ /** Calculate sleep duration in milliseconds for a retry attempt. */
249
+ private retryDelayMs;
250
+ /** Extract the Retry-After header value in seconds, or undefined. */
251
+ private parseRetryAfter;
252
+ private handleError;
253
+ }
254
+
255
+ /**
256
+ * Error classes for the smritea TypeScript SDK.
257
+ * Each maps to an HTTP status code returned by the API.
258
+ */
259
+ declare class SmriteaError extends Error {
260
+ statusCode?: number;
261
+ constructor(message: string, statusCode?: number);
262
+ }
263
+ /** HTTP 401 -- invalid or missing API key. */
264
+ declare class SmriteaAuthError extends SmriteaError {
265
+ constructor(message: string, statusCode?: number);
266
+ }
267
+ /** HTTP 404 -- memory not found. */
268
+ declare class SmriteaNotFoundError extends SmriteaError {
269
+ constructor(message: string, statusCode?: number);
270
+ }
271
+ /** HTTP 400 -- request validation failed. */
272
+ declare class SmriteaValidationError extends SmriteaError {
273
+ constructor(message: string, statusCode?: number);
274
+ }
275
+ /** HTTP 402 -- quota exceeded for this organization. */
276
+ declare class SmriteaQuotaError extends SmriteaError {
277
+ constructor(message: string, statusCode?: number);
278
+ }
279
+ /**
280
+ * HTTP 429 -- rate limit exceeded after all retries are exhausted.
281
+ *
282
+ * `retryAfter` is the value from the server's Retry-After header (seconds),
283
+ * if provided. This is informational — the SDK already waited this long
284
+ * during its automatic retry attempts.
285
+ */
286
+ declare class SmriteaRateLimitError extends SmriteaError {
287
+ retryAfter?: number;
288
+ constructor(message: string, statusCode?: number, retryAfter?: number);
289
+ }
290
+
291
+ export { type AddMemoryOptions, type MemoryMemoryResponse as Memory, type SearchOptions, type MemorySearchMemoryResponse as SearchResult, SmriteaAuthError, SmriteaClient, type SmriteaClientConfig, SmriteaError, SmriteaNotFoundError, SmriteaQuotaError, SmriteaRateLimitError, SmriteaValidationError };