@memonest/sdk 0.0.12

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,86 @@
1
+ ## @memonest/sdk
2
+
3
+ A tiny TypeScript/JavaScript client for the Memonest API.
4
+ It makes it easy to **store, search, and auto-extract memories about your users** with just a few lines of code.
5
+
6
+ ### Installation
7
+
8
+ ```bash
9
+ npm install @memonest/sdk
10
+ # or
11
+ yarn add @memonest/sdk
12
+ # or
13
+ pnpm add @memonest/sdk
14
+ ```
15
+
16
+ ### Quick start
17
+
18
+ ```ts
19
+ import MemoryClient from "@memonest/sdk";
20
+
21
+ const client = new MemoryClient({
22
+ // If omitted, the client will fall back to process.env.MEMONEST_API_KEY (in Node)
23
+ apiKey: process.env.MEMONEST_API_KEY,
24
+ });
25
+
26
+ // Add a memory
27
+ await client.add({
28
+ userId: "user_123",
29
+ content: "Meeting notes from Q1 planning",
30
+ });
31
+
32
+ // Find memories
33
+ const result = await client.find({
34
+ userId: "user_123",
35
+ query: "planning notes",
36
+ // optional:
37
+ // limit: 10,
38
+ // relevanceThreshold: 0.7,
39
+ });
40
+
41
+ console.log(result); // { memories: [...] }
42
+ ```
43
+
44
+ ### API Overview
45
+
46
+ The client exposes a **small, ergonomic surface** on top of the HTTP API:
47
+
48
+ - **`new MemoryClient(options)`**
49
+ - `apiKey?: string` – Your Memonest API key. If omitted (Node), falls back to `process.env.MEMONEST_API_KEY` (and also supports `process.env.YOURAIMEMORY_API_KEY` for backward compatibility).
50
+ - `baseUrl?: string` – Override the API base URL (defaults to `https://api.getmemonest.xyz`).
51
+ - `fetchFn?: typeof fetch` – Custom fetch implementation (for tests, Node polyfills, etc.).
52
+
53
+ - **High-level helpers**
54
+ - `client.add({ userId, content, metadata? })`
55
+ Store a single memory for a user.
56
+ - `client.find({ userId, query, limit?, relevanceThreshold? })`
57
+ Search for the most relevant memories for a user.
58
+ - `client.auto({ userId, messages, maxMemories?, importanceThreshold? })`
59
+ Send a conversation transcript and let the API extract and store memories automatically.
60
+ - `client.list({ userId, limit?, offset? })`
61
+ List recent memories for a user.
62
+ - `client.deleteMemory(memoryId)`
63
+ Delete a memory by its ID.
64
+
65
+ - **Low-level access**
66
+ - `client.memories.auto(...)`
67
+ - `client.memories.search(...)`
68
+ - `client.memories.store(...)`
69
+ - `client.memories.list(...)`
70
+ - `client.memories.delete(...)`
71
+
72
+ These map 1:1 to the underlying `/v1/memories` HTTP endpoints and accept the full typed request payloads from `@aimemory/shared`.
73
+
74
+ ### Why use Memonest?
75
+
76
+ - **Long‑term memory for your AI apps**
77
+ Store user profile details, preferences, tasks, and episodic events so your AI can stay consistent over time.
78
+
79
+ - **Smart retrieval**
80
+ Hybrid search (vectors + keywords), recency, importance, usage frequency, and memory type signals are combined to surface the most relevant memories.
81
+
82
+ - **Automatic extraction**
83
+ Send raw conversation logs and let the API extract structured memories for you.
84
+
85
+ - **Simple integration**
86
+ Just a few lines of code with `client.add` and `client.find` – no need to manage embeddings, indexes, or ranking logic yourself.
@@ -0,0 +1,91 @@
1
+ import { AutoMemorySchema, ListMemoriesSchema, SearchMemorySchema, StoreMemorySchema } from "@memonest/shared";
2
+ import type { z } from "zod";
3
+ export type AutoMemoryRequest = z.infer<typeof AutoMemorySchema>;
4
+ export type SearchMemoryRequest = z.infer<typeof SearchMemorySchema>;
5
+ export type StoreMemoryRequest = z.infer<typeof StoreMemorySchema>;
6
+ export type ListMemoriesRequest = z.infer<typeof ListMemoriesSchema>;
7
+ export interface MemoryClientOptions {
8
+ apiKey?: string;
9
+ /**
10
+ * Base URL of the Memonest API (no trailing slash).
11
+ * Defaults to https://api.getmemonest.xyz
12
+ */
13
+ baseUrl?: string;
14
+ /**
15
+ * Optional custom fetch implementation (for testing or environments without global fetch).
16
+ */
17
+ fetchFn?: typeof fetch;
18
+ }
19
+ export interface MemoryClientResponseError extends Error {
20
+ status: number;
21
+ body: unknown;
22
+ }
23
+ export interface AddMemoryParams {
24
+ userId: string;
25
+ content: string;
26
+ metadata?: Record<string, unknown>;
27
+ }
28
+ export interface AutoMemoryParams {
29
+ userId: string;
30
+ messages: {
31
+ role: "user" | "assistant" | "system";
32
+ content: string;
33
+ }[];
34
+ maxMemories?: number;
35
+ importanceThreshold?: number;
36
+ }
37
+ export interface FindMemoriesParams {
38
+ userId: string;
39
+ query: string;
40
+ limit?: number;
41
+ relevanceThreshold?: number;
42
+ }
43
+ export interface ListSimpleParams {
44
+ userId: string;
45
+ limit?: number;
46
+ offset?: number;
47
+ }
48
+ export declare class MemoryClient {
49
+ private readonly apiKey;
50
+ private readonly baseUrl;
51
+ private readonly fetchFn;
52
+ constructor(options: MemoryClientOptions);
53
+ /**
54
+ * Access to memories-related operations.
55
+ */
56
+ readonly memories: {
57
+ auto: (req: AutoMemoryRequest) => Promise<any>;
58
+ search: (req: SearchMemoryRequest) => Promise<any>;
59
+ store: (req: StoreMemoryRequest) => Promise<any>;
60
+ list: (req: ListMemoriesRequest) => Promise<any>;
61
+ delete: (memoryId: string) => Promise<any>;
62
+ };
63
+ /**
64
+ * High-level convenience methods
65
+ */
66
+ /**
67
+ * Store a single memory for a user.
68
+ */
69
+ add(params: AddMemoryParams): Promise<any>;
70
+ /**
71
+ * Automatically extract and store memories from a conversation.
72
+ */
73
+ auto(params: AutoMemoryParams): Promise<any>;
74
+ /**
75
+ * Find relevant memories for a user.
76
+ */
77
+ find(params: FindMemoriesParams): Promise<any>;
78
+ /**
79
+ * List recent memories for a user.
80
+ */
81
+ list(params: ListSimpleParams): Promise<any>;
82
+ /**
83
+ * Delete a memory by id.
84
+ */
85
+ deleteMemory(memoryId: string): Promise<any>;
86
+ private postJson;
87
+ private getJson;
88
+ private delete;
89
+ private handleResponse;
90
+ }
91
+ export default MemoryClient;
package/dist/index.js ADDED
@@ -0,0 +1,130 @@
1
+ export class MemoryClient {
2
+ constructor(options) {
3
+ /**
4
+ * Access to memories-related operations.
5
+ */
6
+ this.memories = {
7
+ auto: (req) => this.postJson("/v1/memories/auto", req),
8
+ search: (req) => this.postJson("/v1/memories/search", req),
9
+ store: (req) => this.postJson("/v1/memories", req),
10
+ list: (req) => this.getJson("/v1/memories", {
11
+ user_id: req.user_id,
12
+ limit: req.limit ?? 20,
13
+ offset: req.offset ?? 0,
14
+ }),
15
+ delete: (memoryId) => this.delete(`/v1/memories/${encodeURIComponent(memoryId)}`),
16
+ };
17
+ const processEnv = typeof globalThis !== "undefined" && globalThis.process?.env
18
+ ? globalThis.process.env
19
+ : undefined;
20
+ const envApiKey = processEnv?.MEMONEST_API_KEY ?? processEnv?.YOURAIMEMORY_API_KEY;
21
+ const apiKey = options.apiKey ?? envApiKey;
22
+ if (!apiKey) {
23
+ throw new Error("MemoryClient: apiKey is required (or set MEMONEST_API_KEY / YOURAIMEMORY_API_KEY)");
24
+ }
25
+ this.apiKey = apiKey;
26
+ this.baseUrl = (options.baseUrl ?? "https://api.getmemonest.xyz").replace(/\/+$/, "");
27
+ this.fetchFn = options.fetchFn ?? fetch;
28
+ }
29
+ /**
30
+ * High-level convenience methods
31
+ */
32
+ /**
33
+ * Store a single memory for a user.
34
+ */
35
+ async add(params) {
36
+ const body = {
37
+ user_id: params.userId,
38
+ content: params.content,
39
+ metadata: params.metadata,
40
+ };
41
+ return this.memories.store(body);
42
+ }
43
+ /**
44
+ * Automatically extract and store memories from a conversation.
45
+ */
46
+ async auto(params) {
47
+ const body = {
48
+ user_id: params.userId,
49
+ messages: params.messages,
50
+ max_memories: params.maxMemories ?? 5,
51
+ importance_threshold: params.importanceThreshold ?? 0.5,
52
+ };
53
+ return this.memories.auto(body);
54
+ }
55
+ /**
56
+ * Find relevant memories for a user.
57
+ */
58
+ async find(params) {
59
+ const body = {
60
+ user_id: params.userId,
61
+ query: params.query,
62
+ limit: params.limit ?? 10,
63
+ relevance_threshold: params.relevanceThreshold ?? 0.7,
64
+ };
65
+ return this.memories.search(body);
66
+ }
67
+ /**
68
+ * List recent memories for a user.
69
+ */
70
+ async list(params) {
71
+ const req = {
72
+ user_id: params.userId,
73
+ limit: params.limit ?? 20,
74
+ offset: params.offset ?? 0,
75
+ };
76
+ return this.memories.list(req);
77
+ }
78
+ /**
79
+ * Delete a memory by id.
80
+ */
81
+ async deleteMemory(memoryId) {
82
+ return this.memories.delete(memoryId);
83
+ }
84
+ async postJson(path, body) {
85
+ const res = await this.fetchFn(this.baseUrl + path, {
86
+ method: "POST",
87
+ headers: {
88
+ "Content-Type": "application/json",
89
+ Authorization: `Bearer ${this.apiKey}`,
90
+ },
91
+ body: JSON.stringify(body),
92
+ });
93
+ return this.handleResponse(res);
94
+ }
95
+ async getJson(path, query) {
96
+ const url = new URL(this.baseUrl + path);
97
+ for (const [key, value] of Object.entries(query)) {
98
+ url.searchParams.set(key, String(value));
99
+ }
100
+ const res = await this.fetchFn(url.toString(), {
101
+ method: "GET",
102
+ headers: {
103
+ Authorization: `Bearer ${this.apiKey}`,
104
+ },
105
+ });
106
+ return this.handleResponse(res);
107
+ }
108
+ async delete(path) {
109
+ const res = await this.fetchFn(this.baseUrl + path, {
110
+ method: "DELETE",
111
+ headers: {
112
+ Authorization: `Bearer ${this.apiKey}`,
113
+ },
114
+ });
115
+ return this.handleResponse(res);
116
+ }
117
+ async handleResponse(res) {
118
+ const contentType = res.headers.get("content-type") ?? "";
119
+ const isJson = contentType.includes("application/json");
120
+ const body = isJson ? await res.json().catch(() => undefined) : await res.text().catch(() => undefined);
121
+ if (!res.ok) {
122
+ const err = Object.assign(new Error(`Memonest API request failed with status ${res.status}${body && typeof body === "object" && "error" in body
123
+ ? `: ${body.error}`
124
+ : ""}`), { status: res.status, body });
125
+ throw err;
126
+ }
127
+ return body;
128
+ }
129
+ }
130
+ export default MemoryClient;
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@memonest/sdk",
3
+ "version": "0.0.12",
4
+ "private": false,
5
+ "main": "./dist/index.cjs",
6
+ "module": "./dist/index.mjs",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "import": "./dist/index.mjs",
11
+ "require": "./dist/index.cjs",
12
+ "types": "./dist/index.d.ts"
13
+ }
14
+ },
15
+ "scripts": {
16
+ "build": "tsc -p tsconfig.json",
17
+ "lint": "tsc -p tsconfig.json --noEmit"
18
+ },
19
+ "dependencies": {
20
+ "@memonest/shared": "file:../shared",
21
+ "zod": "^3.25.76"
22
+ },
23
+ "devDependencies": {
24
+ "typescript": "^5.7.0"
25
+ },
26
+ "engines": {
27
+ "node": ">=18.0.0"
28
+ }
29
+ }
package/src/index.ts ADDED
@@ -0,0 +1,211 @@
1
+ import {
2
+ AutoMemorySchema,
3
+ ListMemoriesSchema,
4
+ SearchMemorySchema,
5
+ StoreMemorySchema,
6
+ } from "@memonest/shared";
7
+ import type { z } from "zod";
8
+
9
+ export type AutoMemoryRequest = z.infer<typeof AutoMemorySchema>;
10
+ export type SearchMemoryRequest = z.infer<typeof SearchMemorySchema>;
11
+ export type StoreMemoryRequest = z.infer<typeof StoreMemorySchema>;
12
+ export type ListMemoriesRequest = z.infer<typeof ListMemoriesSchema>;
13
+
14
+ export interface MemoryClientOptions {
15
+ apiKey?: string;
16
+ baseUrl?: string;
17
+ fetchFn?: typeof fetch;
18
+ }
19
+
20
+ export interface MemoryClientResponseError extends Error {
21
+ status: number;
22
+ body: unknown;
23
+ }
24
+
25
+ export interface AddMemoryParams {
26
+ userId: string;
27
+ content: string;
28
+ metadata?: Record<string, unknown>;
29
+ }
30
+
31
+ export interface AutoMemoryParams {
32
+ userId: string;
33
+ messages: { role: "user" | "assistant" | "system"; content: string }[];
34
+ maxMemories?: number;
35
+ importanceThreshold?: number;
36
+ }
37
+
38
+ export interface FindMemoriesParams {
39
+ userId: string;
40
+ query: string;
41
+ limit?: number;
42
+ relevanceThreshold?: number;
43
+ }
44
+
45
+ export interface ListSimpleParams {
46
+ userId: string;
47
+ limit?: number;
48
+ offset?: number;
49
+ }
50
+
51
+ export class MemoryClient {
52
+ private readonly apiKey: string;
53
+ private readonly baseUrl: string;
54
+ private readonly fetchFn: typeof fetch;
55
+
56
+ constructor(options: MemoryClientOptions) {
57
+ const processEnv =
58
+ typeof globalThis !== "undefined" && (globalThis as any).process?.env
59
+ ? ((globalThis as any).process.env as Record<string, string | undefined>)
60
+ : undefined;
61
+
62
+ const envApiKey =
63
+ processEnv?.MEMONEST_API_KEY ?? processEnv?.YOURAIMEMORY_API_KEY;
64
+ const apiKey = options.apiKey ?? envApiKey;
65
+ if (!apiKey) {
66
+ throw new Error(
67
+ "MemoryClient: apiKey is required (or set MEMONEST_API_KEY / YOURAIMEMORY_API_KEY)"
68
+ );
69
+ }
70
+ this.apiKey = apiKey;
71
+ this.baseUrl = (options.baseUrl ?? "https://api.getmemonest.xyz").replace(/\/+$/, "");
72
+ this.fetchFn = options.fetchFn ?? fetch;
73
+ }
74
+
75
+ /**
76
+ * Access to memories-related operations.
77
+ */
78
+ public readonly memories = {
79
+ auto: (req: AutoMemoryRequest) => this.postJson("/v1/memories/auto", req),
80
+ search: (req: SearchMemoryRequest) => this.postJson("/v1/memories/search", req),
81
+ store: (req: StoreMemoryRequest) => this.postJson("/v1/memories", req),
82
+ list: (req: ListMemoriesRequest) =>
83
+ this.getJson("/v1/memories", {
84
+ user_id: req.user_id,
85
+ limit: req.limit ?? 20,
86
+ offset: req.offset ?? 0,
87
+ }),
88
+ delete: (memoryId: string) => this.delete(`/v1/memories/${encodeURIComponent(memoryId)}`),
89
+ };
90
+
91
+ /**
92
+ * High-level convenience methods
93
+ */
94
+
95
+ /**
96
+ * Store a single memory for a user.
97
+ */
98
+ async add(params: AddMemoryParams): Promise<any> {
99
+ const body: StoreMemoryRequest = {
100
+ user_id: params.userId,
101
+ content: params.content,
102
+ metadata: params.metadata,
103
+ };
104
+ return this.memories.store(body);
105
+ }
106
+
107
+ /**
108
+ * Automatically extract and store memories from a conversation.
109
+ */
110
+ async auto(params: AutoMemoryParams): Promise<any> {
111
+ const body: AutoMemoryRequest = {
112
+ user_id: params.userId,
113
+ messages: params.messages,
114
+ max_memories: params.maxMemories ?? 5,
115
+ importance_threshold: params.importanceThreshold ?? 0.5,
116
+ };
117
+ return this.memories.auto(body);
118
+ }
119
+
120
+ /**
121
+ * Find relevant memories for a user.
122
+ */
123
+ async find(params: FindMemoriesParams): Promise<any> {
124
+ const body: SearchMemoryRequest = {
125
+ user_id: params.userId,
126
+ query: params.query,
127
+ limit: params.limit ?? 10,
128
+ relevance_threshold: params.relevanceThreshold ?? 0.7,
129
+ };
130
+ return this.memories.search(body);
131
+ }
132
+
133
+ /**
134
+ * List recent memories for a user.
135
+ */
136
+ async list(params: ListSimpleParams): Promise<any> {
137
+ const req: ListMemoriesRequest = {
138
+ user_id: params.userId,
139
+ limit: params.limit ?? 20,
140
+ offset: params.offset ?? 0,
141
+ };
142
+ return this.memories.list(req);
143
+ }
144
+
145
+ /**
146
+ * Delete a memory by id.
147
+ */
148
+ async deleteMemory(memoryId: string): Promise<any> {
149
+ return this.memories.delete(memoryId);
150
+ }
151
+
152
+ private async postJson(path: string, body: unknown): Promise<any> {
153
+ const res = await this.fetchFn(this.baseUrl + path, {
154
+ method: "POST",
155
+ headers: {
156
+ "Content-Type": "application/json",
157
+ Authorization: `Bearer ${this.apiKey}`,
158
+ },
159
+ body: JSON.stringify(body),
160
+ });
161
+ return this.handleResponse(res);
162
+ }
163
+
164
+ private async getJson(path: string, query: Record<string, string | number>): Promise<any> {
165
+ const url = new URL(this.baseUrl + path);
166
+ for (const [key, value] of Object.entries(query)) {
167
+ url.searchParams.set(key, String(value));
168
+ }
169
+ const res = await this.fetchFn(url.toString(), {
170
+ method: "GET",
171
+ headers: {
172
+ Authorization: `Bearer ${this.apiKey}`,
173
+ },
174
+ });
175
+ return this.handleResponse(res);
176
+ }
177
+
178
+ private async delete(path: string): Promise<any> {
179
+ const res = await this.fetchFn(this.baseUrl + path, {
180
+ method: "DELETE",
181
+ headers: {
182
+ Authorization: `Bearer ${this.apiKey}`,
183
+ },
184
+ });
185
+ return this.handleResponse(res);
186
+ }
187
+
188
+ private async handleResponse(res: Response): Promise<any> {
189
+ const contentType = res.headers.get("content-type") ?? "";
190
+ const isJson = contentType.includes("application/json");
191
+ const body = isJson ? await res.json().catch(() => undefined) : await res.text().catch(() => undefined);
192
+
193
+ if (!res.ok) {
194
+ const err: MemoryClientResponseError = Object.assign(
195
+ new Error(
196
+ `Memonest API request failed with status ${res.status}${
197
+ body && typeof body === "object" && "error" in (body as any)
198
+ ? `: ${(body as any).error}`
199
+ : ""
200
+ }`
201
+ ),
202
+ { status: res.status, body }
203
+ );
204
+ throw err;
205
+ }
206
+
207
+ return body;
208
+ }
209
+ }
210
+
211
+ export default MemoryClient;
package/tsconfig.json ADDED
@@ -0,0 +1,16 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2020",
4
+ "module": "ESNext",
5
+ "moduleResolution": "bundler",
6
+ "declaration": true,
7
+ "declarationMap": false,
8
+ "outDir": "./dist",
9
+ "rootDir": "./src",
10
+ "strict": true,
11
+ "esModuleInterop": true,
12
+ "skipLibCheck": true,
13
+ "forceConsistentCasingInFileNames": true
14
+ },
15
+ "include": ["src"]
16
+ }