memorysync-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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 MemorySync
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,105 @@
1
+ # memorysync-sdk
2
+
3
+ Official JavaScript / TypeScript client for the MemorySync API.
4
+
5
+ ```bash
6
+ npm install memorysync-sdk
7
+ ```
8
+
9
+ ## Quick start
10
+
11
+ ```ts
12
+ import { MemorySyncClient } from "memorysync-sdk";
13
+
14
+ const ms = new MemorySyncClient({
15
+ apiKey: process.env.MEMORYSYNC_API_KEY!,
16
+ baseUrl: "https://api.memorysync.dev",
17
+ // optional:
18
+ projectId: "proj_xxxxxxxxxxxxxxxx",
19
+ endUserId: "user_42",
20
+ });
21
+
22
+ await ms.add({ text: "User prefers dark mode." });
23
+
24
+ const { memories } = await ms.query({ query: "ui preferences", k: 5 });
25
+ for (const m of memories) console.log(m.id, m.text);
26
+ ```
27
+
28
+ ## Configuration
29
+
30
+ | Field | Required | Description |
31
+ | ------------ | -------- | -------------------------------------------------------------------------------------------- |
32
+ | `apiKey` | yes | Sent as `X-API-Key`. Provision in your MemorySync dashboard. |
33
+ | `baseUrl` | yes | The deployment URL of your MemorySync instance. |
34
+ | `projectId` | no | Pin the client to a single project (`X-Project-ID`). Format: `proj_` + 16 hex chars. |
35
+ | `endUserId` | no | Identify which of *your* users this client speaks for (`X-End-User-ID`). |
36
+ | `timeoutMs` | no | Per-request timeout. Default `30000`. |
37
+ | `fetch` | no | Inject a custom fetch (tests, Node 16). Defaults to global `fetch` (Node 18+, all browsers). |
38
+
39
+ `endUserId` can also be passed per-call on `add()` to override the client default.
40
+
41
+ ## Methods
42
+
43
+ Every method is a thin wrapper over a real HTTP route. There are no hidden side effects.
44
+
45
+ | Method | Route |
46
+ | ------------------------------------- | -------------------------------------- |
47
+ | `add(req)` | `POST /memory/add` |
48
+ | `bulkAdd(items, { deduplicate? })` | `POST /memory/bulk-add` |
49
+ | `query(req)` | `POST /memory/query` |
50
+ | `get(memoryId)` | `GET /memory/{id}` |
51
+ | `update(memoryId, req)` | `PATCH /memory/{id}` |
52
+ | `forget(memoryIds, reason?)` | `DELETE /memory/forget` |
53
+ | `summarize(req)` | `POST /memory/summarize` |
54
+ | `compose(req)` | `POST /memory/compose` |
55
+ | `exportAll()` | `GET /memory/export` |
56
+ | `createRelation(fromId, req)` | `POST /memory/{id}/relations` |
57
+
58
+ ### `add` returns one of two shapes
59
+
60
+ `add()` is routed through MemorySync's extraction pipeline. Inputs that carry no
61
+ high-value content are intentionally skipped. The discriminator is the `status`
62
+ field on the skipped envelope:
63
+
64
+ ```ts
65
+ const result = await ms.add({ text: "User prefers dark mode." });
66
+ if ("status" in result && result.status === "skipped") {
67
+ // result.reason, result.candidatesExtracted, result.candidatesStored
68
+ } else {
69
+ // result is a MemoryRecord — result.id, result.text, result.createdAt, ...
70
+ }
71
+ ```
72
+
73
+ ## Errors
74
+
75
+ Every non-2xx response throws a typed subclass of `MemorySyncError`:
76
+
77
+ | Class | When |
78
+ | ----------------- | ------------------------------------------- |
79
+ | `AuthError` | `401` / `403` — bad key, missing scope. |
80
+ | `ValidationError` | `400` / `409` / `422`. |
81
+ | `NotFoundError` | `404` — record not visible to the caller. |
82
+ | `RateLimitError` | `429` — read `err.retryAfterSeconds`. |
83
+ | `ServerError` | `5xx`. |
84
+ | `MemorySyncError` | Network errors, timeouts, anything else. |
85
+
86
+ Every error carries `statusCode`, `response`, and the server-issued `requestId`
87
+ (when present) for support escalation.
88
+
89
+ ```ts
90
+ import { RateLimitError } from "memorysync-sdk";
91
+
92
+ try {
93
+ await ms.add({ text });
94
+ } catch (err) {
95
+ if (err instanceof RateLimitError) {
96
+ await new Promise((r) => setTimeout(r, err.retryAfterSeconds * 1000));
97
+ } else {
98
+ throw err;
99
+ }
100
+ }
101
+ ```
102
+
103
+ ## License
104
+
105
+ MIT
@@ -0,0 +1,188 @@
1
+ /**
2
+ * MemorySync SDK — JavaScript / TypeScript client.
3
+ *
4
+ * Wraps the public REST surface of MemorySync. Every method here maps 1:1
5
+ * to a route that exists in the backend; field names mirror the on-the-wire
6
+ * JSON exactly (snake_case is preserved on payloads, camelCase is exposed
7
+ * on the public TypeScript API).
8
+ */
9
+ interface MemorySyncConfig {
10
+ apiKey: string;
11
+ baseUrl: string;
12
+ projectId?: string;
13
+ endUserId?: string;
14
+ timeoutMs?: number;
15
+ fetch?: typeof fetch;
16
+ }
17
+ interface AddRequest {
18
+ text: string;
19
+ source?: string;
20
+ tags?: string[];
21
+ importance?: number;
22
+ sessionId?: string;
23
+ metadata?: Record<string, unknown>;
24
+ endUserId?: string;
25
+ }
26
+ interface MemoryRecord {
27
+ id: number;
28
+ text: string;
29
+ summary?: string | null;
30
+ tags?: string[] | null;
31
+ source?: string | null;
32
+ eventType?: string | null;
33
+ importance?: number | null;
34
+ metadata?: Record<string, unknown> | null;
35
+ isSummary: boolean;
36
+ createdAt: string;
37
+ updatedAt?: string | null;
38
+ score?: number | null;
39
+ }
40
+ interface AddSkippedResponse {
41
+ status: "skipped";
42
+ reason: string;
43
+ memoryIds: number[];
44
+ candidatesExtracted: number;
45
+ candidatesStored: number;
46
+ }
47
+ type AddResponse = MemoryRecord | AddSkippedResponse;
48
+ interface BulkAddItem {
49
+ text: string;
50
+ source?: string;
51
+ eventType?: string;
52
+ tags?: string[];
53
+ metadata?: Record<string, unknown>;
54
+ importance?: number;
55
+ endUserId?: string;
56
+ }
57
+ interface BulkAddItemResult {
58
+ index: number;
59
+ status: "created" | "skipped" | "rejected";
60
+ memoryIds: number[];
61
+ reason: string | null;
62
+ }
63
+ interface BulkAddResponse {
64
+ total: number;
65
+ created: number;
66
+ skipped: number;
67
+ rejected: number;
68
+ results: BulkAddItemResult[];
69
+ }
70
+ interface QueryFilters {
71
+ memoryType?: string;
72
+ source?: string;
73
+ tags?: string[];
74
+ since?: string;
75
+ until?: string;
76
+ includeSummaries?: boolean;
77
+ tier?: "hot" | "warm" | "cold";
78
+ }
79
+ interface QueryRequest {
80
+ query: string;
81
+ k?: number;
82
+ filters?: QueryFilters;
83
+ sessionId?: string;
84
+ traversalDepth?: number;
85
+ }
86
+ interface QueryResponse {
87
+ memories: MemoryRecord[];
88
+ context?: string | null;
89
+ latencyMs?: number | null;
90
+ sessionId?: string | null;
91
+ queryIntent?: string | null;
92
+ }
93
+ interface UpdateRequest {
94
+ tags?: string[];
95
+ importance?: number;
96
+ metadata?: Record<string, unknown>;
97
+ source?: string;
98
+ eventType?: string;
99
+ }
100
+ interface SummarizeRequest {
101
+ memoryIds: number[];
102
+ lossless?: boolean;
103
+ }
104
+ interface ComposeRequest {
105
+ promptTemplate: string;
106
+ recallK?: number;
107
+ maxTokens?: number;
108
+ }
109
+ interface ComposeResponse {
110
+ composedPrompt: string;
111
+ memoriesUsed: number;
112
+ tokenCount: number;
113
+ truncated: boolean;
114
+ }
115
+ type RelationshipType = "similar" | "derived_from" | "continuation" | "contradiction" | "summary_of" | "detail_of" | "caused_by" | "references" | "supports" | "extends";
116
+ interface RelationCreateRequest {
117
+ toMemoryId: number;
118
+ relationshipType: RelationshipType;
119
+ confidence?: number;
120
+ metadata?: Record<string, unknown>;
121
+ }
122
+ interface RelationRecord {
123
+ id: number;
124
+ fromMemoryId: number;
125
+ toMemoryId: number;
126
+ relationshipType: RelationshipType;
127
+ confidence: number;
128
+ metadata?: Record<string, unknown> | null;
129
+ createdAt: string;
130
+ }
131
+ interface ExportResponse {
132
+ userId: number;
133
+ memories: Array<Record<string, unknown>>;
134
+ generatedAt: string;
135
+ }
136
+ interface ErrorOpts {
137
+ statusCode?: number;
138
+ response?: unknown;
139
+ requestId?: string;
140
+ }
141
+ declare class MemorySyncError extends Error {
142
+ readonly statusCode?: number;
143
+ readonly response?: unknown;
144
+ readonly requestId?: string;
145
+ constructor(message: string, opts?: ErrorOpts);
146
+ }
147
+ declare class AuthError extends MemorySyncError {
148
+ constructor(message: string, opts?: ErrorOpts);
149
+ }
150
+ declare class ValidationError extends MemorySyncError {
151
+ constructor(message: string, opts?: ErrorOpts);
152
+ }
153
+ declare class NotFoundError extends MemorySyncError {
154
+ constructor(message: string, opts?: ErrorOpts);
155
+ }
156
+ declare class RateLimitError extends MemorySyncError {
157
+ readonly retryAfterSeconds: number;
158
+ constructor(message: string, retryAfterSeconds: number, opts?: ErrorOpts);
159
+ }
160
+ declare class ServerError extends MemorySyncError {
161
+ constructor(message: string, opts?: ErrorOpts);
162
+ }
163
+ declare class MemorySyncClient {
164
+ private readonly apiKey;
165
+ private readonly baseUrl;
166
+ private readonly projectId?;
167
+ private readonly endUserId?;
168
+ private readonly timeoutMs;
169
+ private readonly fetchImpl;
170
+ constructor(config: MemorySyncConfig);
171
+ private headers;
172
+ private request;
173
+ private throwForStatus;
174
+ add(req: AddRequest): Promise<AddResponse>;
175
+ bulkAdd(items: BulkAddItem[], opts?: {
176
+ deduplicate?: boolean;
177
+ }): Promise<BulkAddResponse>;
178
+ query(req: QueryRequest): Promise<QueryResponse>;
179
+ get(memoryId: number): Promise<MemoryRecord>;
180
+ update(memoryId: number, req: UpdateRequest): Promise<MemoryRecord>;
181
+ forget(memoryIds: number[], reason?: string): Promise<number[]>;
182
+ summarize(req: SummarizeRequest): Promise<MemoryRecord>;
183
+ compose(req: ComposeRequest): Promise<ComposeResponse>;
184
+ exportAll(): Promise<ExportResponse>;
185
+ createRelation(fromMemoryId: number, req: RelationCreateRequest): Promise<RelationRecord>;
186
+ }
187
+
188
+ export { type AddRequest, type AddResponse, type AddSkippedResponse, AuthError, type BulkAddItem, type BulkAddItemResult, type BulkAddResponse, type ComposeRequest, type ComposeResponse, type ExportResponse, type MemoryRecord, MemorySyncClient, type MemorySyncConfig, MemorySyncError, NotFoundError, type QueryFilters, type QueryRequest, type QueryResponse, RateLimitError, type RelationCreateRequest, type RelationRecord, type RelationshipType, ServerError, type SummarizeRequest, type UpdateRequest, ValidationError };
@@ -0,0 +1,188 @@
1
+ /**
2
+ * MemorySync SDK — JavaScript / TypeScript client.
3
+ *
4
+ * Wraps the public REST surface of MemorySync. Every method here maps 1:1
5
+ * to a route that exists in the backend; field names mirror the on-the-wire
6
+ * JSON exactly (snake_case is preserved on payloads, camelCase is exposed
7
+ * on the public TypeScript API).
8
+ */
9
+ interface MemorySyncConfig {
10
+ apiKey: string;
11
+ baseUrl: string;
12
+ projectId?: string;
13
+ endUserId?: string;
14
+ timeoutMs?: number;
15
+ fetch?: typeof fetch;
16
+ }
17
+ interface AddRequest {
18
+ text: string;
19
+ source?: string;
20
+ tags?: string[];
21
+ importance?: number;
22
+ sessionId?: string;
23
+ metadata?: Record<string, unknown>;
24
+ endUserId?: string;
25
+ }
26
+ interface MemoryRecord {
27
+ id: number;
28
+ text: string;
29
+ summary?: string | null;
30
+ tags?: string[] | null;
31
+ source?: string | null;
32
+ eventType?: string | null;
33
+ importance?: number | null;
34
+ metadata?: Record<string, unknown> | null;
35
+ isSummary: boolean;
36
+ createdAt: string;
37
+ updatedAt?: string | null;
38
+ score?: number | null;
39
+ }
40
+ interface AddSkippedResponse {
41
+ status: "skipped";
42
+ reason: string;
43
+ memoryIds: number[];
44
+ candidatesExtracted: number;
45
+ candidatesStored: number;
46
+ }
47
+ type AddResponse = MemoryRecord | AddSkippedResponse;
48
+ interface BulkAddItem {
49
+ text: string;
50
+ source?: string;
51
+ eventType?: string;
52
+ tags?: string[];
53
+ metadata?: Record<string, unknown>;
54
+ importance?: number;
55
+ endUserId?: string;
56
+ }
57
+ interface BulkAddItemResult {
58
+ index: number;
59
+ status: "created" | "skipped" | "rejected";
60
+ memoryIds: number[];
61
+ reason: string | null;
62
+ }
63
+ interface BulkAddResponse {
64
+ total: number;
65
+ created: number;
66
+ skipped: number;
67
+ rejected: number;
68
+ results: BulkAddItemResult[];
69
+ }
70
+ interface QueryFilters {
71
+ memoryType?: string;
72
+ source?: string;
73
+ tags?: string[];
74
+ since?: string;
75
+ until?: string;
76
+ includeSummaries?: boolean;
77
+ tier?: "hot" | "warm" | "cold";
78
+ }
79
+ interface QueryRequest {
80
+ query: string;
81
+ k?: number;
82
+ filters?: QueryFilters;
83
+ sessionId?: string;
84
+ traversalDepth?: number;
85
+ }
86
+ interface QueryResponse {
87
+ memories: MemoryRecord[];
88
+ context?: string | null;
89
+ latencyMs?: number | null;
90
+ sessionId?: string | null;
91
+ queryIntent?: string | null;
92
+ }
93
+ interface UpdateRequest {
94
+ tags?: string[];
95
+ importance?: number;
96
+ metadata?: Record<string, unknown>;
97
+ source?: string;
98
+ eventType?: string;
99
+ }
100
+ interface SummarizeRequest {
101
+ memoryIds: number[];
102
+ lossless?: boolean;
103
+ }
104
+ interface ComposeRequest {
105
+ promptTemplate: string;
106
+ recallK?: number;
107
+ maxTokens?: number;
108
+ }
109
+ interface ComposeResponse {
110
+ composedPrompt: string;
111
+ memoriesUsed: number;
112
+ tokenCount: number;
113
+ truncated: boolean;
114
+ }
115
+ type RelationshipType = "similar" | "derived_from" | "continuation" | "contradiction" | "summary_of" | "detail_of" | "caused_by" | "references" | "supports" | "extends";
116
+ interface RelationCreateRequest {
117
+ toMemoryId: number;
118
+ relationshipType: RelationshipType;
119
+ confidence?: number;
120
+ metadata?: Record<string, unknown>;
121
+ }
122
+ interface RelationRecord {
123
+ id: number;
124
+ fromMemoryId: number;
125
+ toMemoryId: number;
126
+ relationshipType: RelationshipType;
127
+ confidence: number;
128
+ metadata?: Record<string, unknown> | null;
129
+ createdAt: string;
130
+ }
131
+ interface ExportResponse {
132
+ userId: number;
133
+ memories: Array<Record<string, unknown>>;
134
+ generatedAt: string;
135
+ }
136
+ interface ErrorOpts {
137
+ statusCode?: number;
138
+ response?: unknown;
139
+ requestId?: string;
140
+ }
141
+ declare class MemorySyncError extends Error {
142
+ readonly statusCode?: number;
143
+ readonly response?: unknown;
144
+ readonly requestId?: string;
145
+ constructor(message: string, opts?: ErrorOpts);
146
+ }
147
+ declare class AuthError extends MemorySyncError {
148
+ constructor(message: string, opts?: ErrorOpts);
149
+ }
150
+ declare class ValidationError extends MemorySyncError {
151
+ constructor(message: string, opts?: ErrorOpts);
152
+ }
153
+ declare class NotFoundError extends MemorySyncError {
154
+ constructor(message: string, opts?: ErrorOpts);
155
+ }
156
+ declare class RateLimitError extends MemorySyncError {
157
+ readonly retryAfterSeconds: number;
158
+ constructor(message: string, retryAfterSeconds: number, opts?: ErrorOpts);
159
+ }
160
+ declare class ServerError extends MemorySyncError {
161
+ constructor(message: string, opts?: ErrorOpts);
162
+ }
163
+ declare class MemorySyncClient {
164
+ private readonly apiKey;
165
+ private readonly baseUrl;
166
+ private readonly projectId?;
167
+ private readonly endUserId?;
168
+ private readonly timeoutMs;
169
+ private readonly fetchImpl;
170
+ constructor(config: MemorySyncConfig);
171
+ private headers;
172
+ private request;
173
+ private throwForStatus;
174
+ add(req: AddRequest): Promise<AddResponse>;
175
+ bulkAdd(items: BulkAddItem[], opts?: {
176
+ deduplicate?: boolean;
177
+ }): Promise<BulkAddResponse>;
178
+ query(req: QueryRequest): Promise<QueryResponse>;
179
+ get(memoryId: number): Promise<MemoryRecord>;
180
+ update(memoryId: number, req: UpdateRequest): Promise<MemoryRecord>;
181
+ forget(memoryIds: number[], reason?: string): Promise<number[]>;
182
+ summarize(req: SummarizeRequest): Promise<MemoryRecord>;
183
+ compose(req: ComposeRequest): Promise<ComposeResponse>;
184
+ exportAll(): Promise<ExportResponse>;
185
+ createRelation(fromMemoryId: number, req: RelationCreateRequest): Promise<RelationRecord>;
186
+ }
187
+
188
+ export { type AddRequest, type AddResponse, type AddSkippedResponse, AuthError, type BulkAddItem, type BulkAddItemResult, type BulkAddResponse, type ComposeRequest, type ComposeResponse, type ExportResponse, type MemoryRecord, MemorySyncClient, type MemorySyncConfig, MemorySyncError, NotFoundError, type QueryFilters, type QueryRequest, type QueryResponse, RateLimitError, type RelationCreateRequest, type RelationRecord, type RelationshipType, ServerError, type SummarizeRequest, type UpdateRequest, ValidationError };