@sovant/sdk 1.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/dist/index.js ADDED
@@ -0,0 +1,463 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ AuthenticationError: () => AuthenticationError,
24
+ NetworkError: () => NetworkError,
25
+ NotFoundError: () => NotFoundError,
26
+ RateLimitError: () => RateLimitError,
27
+ SovantClient: () => SovantClient,
28
+ SovantError: () => SovantError,
29
+ ValidationError: () => ValidationError,
30
+ default: () => index_default
31
+ });
32
+ module.exports = __toCommonJS(index_exports);
33
+
34
+ // src/errors.ts
35
+ var SovantError = class extends Error {
36
+ constructor(message, statusCode, details) {
37
+ super(message);
38
+ this.name = "SovantError";
39
+ this.statusCode = statusCode;
40
+ this.details = details;
41
+ }
42
+ };
43
+ var AuthenticationError = class extends SovantError {
44
+ constructor(message = "Authentication failed") {
45
+ super(message, 401);
46
+ this.name = "AuthenticationError";
47
+ }
48
+ };
49
+ var RateLimitError = class extends SovantError {
50
+ constructor(message = "Rate limit exceeded", retryAfter) {
51
+ super(message, 429);
52
+ this.name = "RateLimitError";
53
+ this.retryAfter = retryAfter;
54
+ }
55
+ };
56
+ var ValidationError = class extends SovantError {
57
+ constructor(message = "Validation failed", errors) {
58
+ super(message, 400);
59
+ this.name = "ValidationError";
60
+ this.errors = errors;
61
+ }
62
+ };
63
+ var NotFoundError = class extends SovantError {
64
+ constructor(message = "Resource not found") {
65
+ super(message, 404);
66
+ this.name = "NotFoundError";
67
+ }
68
+ };
69
+ var NetworkError = class extends SovantError {
70
+ constructor(message = "Network request failed") {
71
+ super(message);
72
+ this.name = "NetworkError";
73
+ }
74
+ };
75
+
76
+ // src/client.ts
77
+ var BaseClient = class {
78
+ constructor(config) {
79
+ this.config = {
80
+ apiKey: config.apiKey,
81
+ baseUrl: config.baseUrl || "https://api.sovant.ai/v1",
82
+ timeout: config.timeout || 3e4,
83
+ maxRetries: config.maxRetries || 3,
84
+ retryDelay: config.retryDelay || 1e3
85
+ };
86
+ if (!this.config.apiKey) {
87
+ throw new AuthenticationError("API key is required");
88
+ }
89
+ if (!this.config.baseUrl.startsWith("https://")) {
90
+ throw new SovantError("API base URL must use HTTPS for security");
91
+ }
92
+ }
93
+ async request(method, path, data, retryCount = 0) {
94
+ const url = `${this.config.baseUrl}${path}`;
95
+ const controller = new AbortController();
96
+ const timeoutId = setTimeout(() => controller.abort(), this.config.timeout);
97
+ try {
98
+ const response = await fetch(url, {
99
+ method,
100
+ headers: {
101
+ Authorization: `Bearer ${this.config.apiKey}`,
102
+ "Content-Type": "application/json",
103
+ "X-SDK-Version": "1.0.0",
104
+ "X-SDK-Language": "javascript"
105
+ },
106
+ body: data ? JSON.stringify(data) : void 0,
107
+ signal: controller.signal
108
+ });
109
+ clearTimeout(timeoutId);
110
+ if (!response.ok) {
111
+ await this.handleErrorResponse(response);
112
+ }
113
+ const responseData = await response.json();
114
+ return responseData;
115
+ } catch (error) {
116
+ clearTimeout(timeoutId);
117
+ if (error instanceof SovantError) {
118
+ throw error;
119
+ }
120
+ if (error instanceof Error) {
121
+ if (error.name === "AbortError") {
122
+ throw new NetworkError("Request timeout");
123
+ }
124
+ if (retryCount < this.config.maxRetries) {
125
+ await this.delay(this.config.retryDelay * Math.pow(2, retryCount));
126
+ return this.request(method, path, data, retryCount + 1);
127
+ }
128
+ throw new NetworkError(error.message);
129
+ }
130
+ throw new SovantError("Unknown error occurred");
131
+ }
132
+ }
133
+ async handleErrorResponse(response) {
134
+ let errorData;
135
+ try {
136
+ errorData = await response.json();
137
+ } catch {
138
+ errorData = {
139
+ error: "unknown_error",
140
+ message: `HTTP ${response.status} ${response.statusText}`,
141
+ status_code: response.status
142
+ };
143
+ }
144
+ switch (response.status) {
145
+ case 401:
146
+ throw new AuthenticationError(errorData.message);
147
+ case 404:
148
+ throw new NotFoundError(errorData.message);
149
+ case 429:
150
+ const retryAfter = response.headers.get("Retry-After");
151
+ throw new RateLimitError(
152
+ errorData.message,
153
+ retryAfter ? parseInt(retryAfter, 10) : void 0
154
+ );
155
+ case 400:
156
+ case 422:
157
+ throw new ValidationError(errorData.message, errorData.details);
158
+ default:
159
+ throw new SovantError(
160
+ errorData.message,
161
+ response.status,
162
+ errorData.details
163
+ );
164
+ }
165
+ }
166
+ delay(ms) {
167
+ return new Promise((resolve) => setTimeout(resolve, ms));
168
+ }
169
+ buildQueryString(params) {
170
+ const searchParams = new URLSearchParams();
171
+ Object.entries(params).forEach(([key, value]) => {
172
+ if (value !== void 0 && value !== null) {
173
+ if (Array.isArray(value)) {
174
+ value.forEach((v) => searchParams.append(key, String(v)));
175
+ } else {
176
+ searchParams.append(key, String(value));
177
+ }
178
+ }
179
+ });
180
+ const queryString = searchParams.toString();
181
+ return queryString ? `?${queryString}` : "";
182
+ }
183
+ };
184
+
185
+ // src/resources/memories.ts
186
+ var Memories = class extends BaseClient {
187
+ /**
188
+ * Create a new memory
189
+ */
190
+ async create(data) {
191
+ return this.request("POST", "/memory", data);
192
+ }
193
+ /**
194
+ * Get a memory by ID
195
+ */
196
+ async get(id) {
197
+ return this.request("GET", `/memory/${id}`);
198
+ }
199
+ /**
200
+ * Update a memory
201
+ */
202
+ async update(id, data) {
203
+ return this.request("PUT", `/memory/${id}`, data);
204
+ }
205
+ /**
206
+ * Delete a memory
207
+ */
208
+ async delete(id) {
209
+ await this.request("DELETE", `/memory/${id}`);
210
+ }
211
+ /**
212
+ * List memories with pagination
213
+ */
214
+ async list(options) {
215
+ const queryString = this.buildQueryString(options || {});
216
+ return this.request(
217
+ "GET",
218
+ `/memory${queryString}`
219
+ );
220
+ }
221
+ /**
222
+ * Search memories
223
+ */
224
+ async search(options) {
225
+ const queryString = this.buildQueryString({
226
+ q: options.query,
227
+ limit: options.limit,
228
+ offset: options.offset,
229
+ type: options.type,
230
+ tags: options.tags,
231
+ created_after: options.created_after,
232
+ created_before: options.created_before,
233
+ search_type: options.search_type,
234
+ include_archived: options.include_archived,
235
+ sort_by: options.sort_by,
236
+ sort_order: options.sort_order
237
+ });
238
+ const response = await this.request(
239
+ "GET",
240
+ `/memory/search${queryString}`
241
+ );
242
+ return response.results;
243
+ }
244
+ /**
245
+ * Batch create memories
246
+ */
247
+ async createBatch(memories) {
248
+ return this.request("POST", "/memory/batch", {
249
+ memories
250
+ });
251
+ }
252
+ /**
253
+ * Batch delete memories
254
+ */
255
+ async deleteBatch(ids) {
256
+ return this.request("POST", "/memory/batch/delete", {
257
+ ids
258
+ });
259
+ }
260
+ /**
261
+ * Get memory insights and analytics
262
+ */
263
+ async getInsights(options) {
264
+ const queryString = this.buildQueryString(options || {});
265
+ return this.request("GET", `/memory/insights${queryString}`);
266
+ }
267
+ /**
268
+ * Archive a memory
269
+ */
270
+ async archive(id) {
271
+ return this.update(id, { is_archived: true });
272
+ }
273
+ /**
274
+ * Unarchive a memory
275
+ */
276
+ async unarchive(id) {
277
+ return this.update(id, { is_archived: false });
278
+ }
279
+ /**
280
+ * Pin a memory
281
+ */
282
+ async pin(id) {
283
+ return this.update(id, { is_pinned: true });
284
+ }
285
+ /**
286
+ * Unpin a memory
287
+ */
288
+ async unpin(id) {
289
+ return this.update(id, { is_pinned: false });
290
+ }
291
+ /**
292
+ * Get related memories
293
+ */
294
+ async getRelated(id, options) {
295
+ const queryString = this.buildQueryString(options || {});
296
+ const response = await this.request(
297
+ "GET",
298
+ `/memory/${id}/related${queryString}`
299
+ );
300
+ return response.results;
301
+ }
302
+ };
303
+
304
+ // src/resources/threads.ts
305
+ var Threads = class extends BaseClient {
306
+ /**
307
+ * Create a new thread
308
+ */
309
+ async create(data) {
310
+ return this.request("POST", "/threads", data);
311
+ }
312
+ /**
313
+ * Get a thread by ID
314
+ */
315
+ async get(id, includeMemories = false) {
316
+ const queryString = includeMemories ? "?include_memories=true" : "";
317
+ return this.request(
318
+ "GET",
319
+ `/threads/${id}${queryString}`
320
+ );
321
+ }
322
+ /**
323
+ * Update a thread
324
+ */
325
+ async update(id, data) {
326
+ return this.request("PUT", `/threads/${id}`, data);
327
+ }
328
+ /**
329
+ * Delete a thread
330
+ */
331
+ async delete(id, deleteMemories = false) {
332
+ const queryString = deleteMemories ? "?delete_memories=true" : "";
333
+ await this.request("DELETE", `/threads/${id}${queryString}`);
334
+ }
335
+ /**
336
+ * List threads with pagination
337
+ */
338
+ async list(options) {
339
+ const queryString = this.buildQueryString(options || {});
340
+ return this.request(
341
+ "GET",
342
+ `/threads${queryString}`
343
+ );
344
+ }
345
+ /**
346
+ * Add memories to a thread
347
+ */
348
+ async addMemories(id, memoryIds) {
349
+ return this.request("POST", `/threads/${id}/memories`, {
350
+ add: memoryIds
351
+ });
352
+ }
353
+ /**
354
+ * Remove memories from a thread
355
+ */
356
+ async removeMemories(id, memoryIds) {
357
+ return this.request("POST", `/threads/${id}/memories`, {
358
+ remove: memoryIds
359
+ });
360
+ }
361
+ /**
362
+ * Get memories in a thread
363
+ */
364
+ async getMemories(id, options) {
365
+ const queryString = this.buildQueryString(options || {});
366
+ return this.request(
367
+ "GET",
368
+ `/threads/${id}/memories${queryString}`
369
+ );
370
+ }
371
+ /**
372
+ * Get thread statistics
373
+ */
374
+ async getStats(id) {
375
+ return this.request("GET", `/threads/${id}/stats`);
376
+ }
377
+ /**
378
+ * Archive a thread
379
+ */
380
+ async archive(id) {
381
+ return this.update(id, { status: "archived" });
382
+ }
383
+ /**
384
+ * Unarchive a thread
385
+ */
386
+ async unarchive(id) {
387
+ return this.update(id, { status: "active" });
388
+ }
389
+ /**
390
+ * Complete a thread
391
+ */
392
+ async complete(id) {
393
+ return this.update(id, { status: "completed" });
394
+ }
395
+ /**
396
+ * Search threads
397
+ */
398
+ async search(options) {
399
+ const queryString = this.buildQueryString({
400
+ q: options.query,
401
+ limit: options.limit,
402
+ offset: options.offset,
403
+ status: options.status,
404
+ tags: options.tags
405
+ });
406
+ return this.request(
407
+ "GET",
408
+ `/threads/search${queryString}`
409
+ );
410
+ }
411
+ /**
412
+ * Merge multiple threads into one
413
+ */
414
+ async merge(targetId, sourceIds) {
415
+ return this.request("POST", `/threads/${targetId}/merge`, {
416
+ source_thread_ids: sourceIds
417
+ });
418
+ }
419
+ /**
420
+ * Clone a thread
421
+ */
422
+ async clone(id, options) {
423
+ return this.request("POST", `/threads/${id}/clone`, options);
424
+ }
425
+ };
426
+
427
+ // src/index.ts
428
+ var SovantClient = class {
429
+ constructor(apiKeyOrConfig) {
430
+ if (typeof apiKeyOrConfig === "string") {
431
+ this.config = { apiKey: apiKeyOrConfig };
432
+ } else {
433
+ this.config = apiKeyOrConfig;
434
+ }
435
+ this.memories = new Memories(this.config);
436
+ this.threads = new Threads(this.config);
437
+ }
438
+ /**
439
+ * Test API connection
440
+ */
441
+ async ping() {
442
+ const client = new BaseClient(this.config);
443
+ return client.request("GET", "/health");
444
+ }
445
+ /**
446
+ * Get current API usage and quota
447
+ */
448
+ async getUsage() {
449
+ const client = new BaseClient(this.config);
450
+ return client.request("GET", "/usage");
451
+ }
452
+ };
453
+ var index_default = SovantClient;
454
+ // Annotate the CommonJS export names for ESM import in node:
455
+ 0 && (module.exports = {
456
+ AuthenticationError,
457
+ NetworkError,
458
+ NotFoundError,
459
+ RateLimitError,
460
+ SovantClient,
461
+ SovantError,
462
+ ValidationError
463
+ });