membase-ai 0.0.1 → 0.2.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 +42 -3
- package/dist/client.d.ts +185 -0
- package/dist/client.js +171 -0
- package/dist/errors.d.ts +66 -0
- package/dist/errors.js +80 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +2 -0
- package/package.json +27 -16
- package/index.d.ts +0 -1
- package/index.js +0 -1
package/README.md
CHANGED
|
@@ -1,10 +1,49 @@
|
|
|
1
1
|
# membase-ai
|
|
2
2
|
|
|
3
|
-
Membase — long-term memory for AI.
|
|
3
|
+
Membase — long-term memory for AI. The TypeScript client for the [Membase](https://membase.ai) API, hosted or local.
|
|
4
4
|
|
|
5
5
|
```bash
|
|
6
6
|
npm install membase-ai
|
|
7
|
+
export MEMBASE_API_KEY="mbk_…" # Connect › Developer keys in the Membase app
|
|
7
8
|
```
|
|
8
9
|
|
|
9
|
-
|
|
10
|
-
|
|
10
|
+
```ts
|
|
11
|
+
import { Membase } from "membase-ai";
|
|
12
|
+
|
|
13
|
+
const client = new Membase();
|
|
14
|
+
|
|
15
|
+
await client.add({ container: "mv-…", content: "Call notes with Acme: they want SSO before the pilot.", customId: "call-2026-09-24" });
|
|
16
|
+
const { results } = await client.search({ q: "what does Acme need before the pilot", limit: 5 });
|
|
17
|
+
console.log(results[0].content);
|
|
18
|
+
|
|
19
|
+
await client.profile({ q: "working hours" }); // who the user is (needs the profile tick on the key)
|
|
20
|
+
await client.containers.list();
|
|
21
|
+
await client.documents.list({ container: "mv-…" });
|
|
22
|
+
await client.memories.add({ content: "The user prefers dark mode.", static: true });
|
|
23
|
+
await client.documents.delete("srcitem_…", { confirm: true }); // Full access; confirm means the person agreed
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Every method is one operation of the Membase agent protocol; access level, reach and confirmation
|
|
27
|
+
rules are enforced server-side. Errors are one class per HTTP status (`PermissionDeniedError`,
|
|
28
|
+
`RateLimitError`, …) and carry the API's `code` and `traceId`. 429 and 5xx answers are retried
|
|
29
|
+
twice with backoff; the default timeout is 90 s, because the first search after a quiet spell
|
|
30
|
+
waits for the user's memory to wake.
|
|
31
|
+
|
|
32
|
+
Node 18+ (global `fetch`), Deno and Bun. Zero dependencies. Works against a self-hosted Membase
|
|
33
|
+
too: `new Membase({ baseUrl: "http://localhost:8080" })`.
|
|
34
|
+
|
|
35
|
+
## Local memory
|
|
36
|
+
|
|
37
|
+
Run the engine on your machine with the Python package and point the client at it — same
|
|
38
|
+
methods, same answers:
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
pip install 'membase-ai[local]'
|
|
42
|
+
membase --local serve # http://127.0.0.1:8787/v1, store under ~/.membase
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
const local = new Membase({ apiKey: "local", baseUrl: "http://127.0.0.1:8787" });
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
`membase-sdk` on npm is the earlier name of this package and keeps working as an alias.
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Membase client: one developer key, the account's memory.
|
|
3
|
+
*
|
|
4
|
+
* import { Membase } from "membase-ai";
|
|
5
|
+
*
|
|
6
|
+
* const client = new Membase(); // MEMBASE_API_KEY, optional MEMBASE_BASE_URL
|
|
7
|
+
* await client.add({ content: "Call notes …", container: "mv-…", customId: "call-1" });
|
|
8
|
+
* await client.search({ q: "what did we decide about the ledger" });
|
|
9
|
+
* await client.profile({ q: "working hours" });
|
|
10
|
+
*
|
|
11
|
+
* Every method is one operation of the agent protocol (the REST namespace under
|
|
12
|
+
* `https://api.app.membase.io/v1`); the access level, reach and confirmation rules are enforced
|
|
13
|
+
* server-side, so nothing here can do what the key cannot.
|
|
14
|
+
*/
|
|
15
|
+
export declare const VERSION = "0.2.0";
|
|
16
|
+
export declare const DEFAULT_BASE_URL = "https://api.app.membase.io";
|
|
17
|
+
/** A search is a turn inside the user's memory; the first one after a quiet spell can take up
|
|
18
|
+
* to a minute while the memory wakes, so the default is generous. */
|
|
19
|
+
export declare const DEFAULT_TIMEOUT_MS = 90000;
|
|
20
|
+
export declare const DEFAULT_MAX_RETRIES = 2;
|
|
21
|
+
type Fetch = typeof fetch;
|
|
22
|
+
export interface MembaseOptions {
|
|
23
|
+
/** Defaults to `process.env.MEMBASE_API_KEY`. Connect › Developer keys in the Membase app. */
|
|
24
|
+
apiKey?: string;
|
|
25
|
+
/** Defaults to `process.env.MEMBASE_BASE_URL`, then `https://api.app.membase.io`. */
|
|
26
|
+
baseUrl?: string;
|
|
27
|
+
/** Per-request timeout. Default 90 s. */
|
|
28
|
+
timeoutMs?: number;
|
|
29
|
+
/** Retries on 408/409/429/5xx and connection errors. Default 2. */
|
|
30
|
+
maxRetries?: number;
|
|
31
|
+
/** A `fetch` to use instead of the global one (tests, custom agents). */
|
|
32
|
+
fetch?: Fetch;
|
|
33
|
+
}
|
|
34
|
+
export interface Container {
|
|
35
|
+
id: string;
|
|
36
|
+
name: string;
|
|
37
|
+
description?: string;
|
|
38
|
+
[k: string]: unknown;
|
|
39
|
+
}
|
|
40
|
+
export interface ContainerList {
|
|
41
|
+
containers: Container[];
|
|
42
|
+
}
|
|
43
|
+
export interface SearchHit {
|
|
44
|
+
content: string;
|
|
45
|
+
score?: number;
|
|
46
|
+
container: string;
|
|
47
|
+
container_name?: string;
|
|
48
|
+
source?: string;
|
|
49
|
+
[k: string]: unknown;
|
|
50
|
+
}
|
|
51
|
+
export interface SearchResult {
|
|
52
|
+
query: string;
|
|
53
|
+
results: SearchHit[];
|
|
54
|
+
containers: Array<{
|
|
55
|
+
id: string;
|
|
56
|
+
name: string;
|
|
57
|
+
error?: string;
|
|
58
|
+
}>;
|
|
59
|
+
}
|
|
60
|
+
export interface Profile {
|
|
61
|
+
available?: boolean;
|
|
62
|
+
ready?: boolean;
|
|
63
|
+
static: string[];
|
|
64
|
+
dynamic: Array<{
|
|
65
|
+
id: number | string;
|
|
66
|
+
content: string;
|
|
67
|
+
updated_at?: string;
|
|
68
|
+
}>;
|
|
69
|
+
results?: SearchHit[];
|
|
70
|
+
}
|
|
71
|
+
export interface Document {
|
|
72
|
+
id: string;
|
|
73
|
+
container: string;
|
|
74
|
+
title?: string;
|
|
75
|
+
learned?: boolean;
|
|
76
|
+
[k: string]: unknown;
|
|
77
|
+
}
|
|
78
|
+
export interface DocumentList {
|
|
79
|
+
documents: Document[];
|
|
80
|
+
}
|
|
81
|
+
export interface AddDocumentResult {
|
|
82
|
+
document_id: string | null;
|
|
83
|
+
container: string;
|
|
84
|
+
status: string;
|
|
85
|
+
path?: string;
|
|
86
|
+
learning_run?: {
|
|
87
|
+
id: string;
|
|
88
|
+
status: string;
|
|
89
|
+
} | null;
|
|
90
|
+
[k: string]: unknown;
|
|
91
|
+
}
|
|
92
|
+
export interface ConfirmationRequired {
|
|
93
|
+
status: "confirmation_required";
|
|
94
|
+
verb: string;
|
|
95
|
+
how: string;
|
|
96
|
+
[k: string]: unknown;
|
|
97
|
+
}
|
|
98
|
+
export type Json = Record<string, unknown>;
|
|
99
|
+
export interface AddParams {
|
|
100
|
+
/** Which container reads it. */
|
|
101
|
+
container: string;
|
|
102
|
+
/** The document's full text. */
|
|
103
|
+
content?: string;
|
|
104
|
+
/** A public web address to fetch instead of `content`. */
|
|
105
|
+
url?: string;
|
|
106
|
+
title?: string;
|
|
107
|
+
/** Your own tags, as an object. */
|
|
108
|
+
metadata?: Record<string, unknown>;
|
|
109
|
+
/** Your own id; adding the same customId again is a no-op. */
|
|
110
|
+
customId?: string;
|
|
111
|
+
}
|
|
112
|
+
export interface SearchParams {
|
|
113
|
+
/** The question, in natural language. */
|
|
114
|
+
q: string;
|
|
115
|
+
/** One container id; omit to search every container in reach. */
|
|
116
|
+
container?: string;
|
|
117
|
+
/** How many passages in all, not per container (1–50). A search over every container
|
|
118
|
+
* splits this budget between them, best-of-each first; left unset it covers every
|
|
119
|
+
* container in reach, so none goes unheard. */
|
|
120
|
+
limit?: number;
|
|
121
|
+
}
|
|
122
|
+
export declare class Membase {
|
|
123
|
+
readonly baseUrl: string;
|
|
124
|
+
readonly maxRetries: number;
|
|
125
|
+
readonly timeoutMs: number;
|
|
126
|
+
private readonly apiKey;
|
|
127
|
+
private readonly fetchImpl;
|
|
128
|
+
readonly containers: {
|
|
129
|
+
/** The containers this key may use — every one, for the account's owner. */
|
|
130
|
+
list: () => Promise<ContainerList>;
|
|
131
|
+
};
|
|
132
|
+
readonly documents: {
|
|
133
|
+
/** The documents the containers in reach have read, newest first, each with `learned`. */
|
|
134
|
+
list: (params?: {
|
|
135
|
+
container?: string;
|
|
136
|
+
}) => Promise<DocumentList>;
|
|
137
|
+
/** One document, with whether its container has learned it yet. */
|
|
138
|
+
get: (documentId: string) => Promise<Document>;
|
|
139
|
+
/** Remove one document everywhere (the file goes to the Files trash). Needs Full access and
|
|
140
|
+
* `confirm: true`; without it the answer is `status: confirmation_required` with a `how`
|
|
141
|
+
* sentence to relay, not an error. */
|
|
142
|
+
delete: (documentId: string, params?: {
|
|
143
|
+
confirm?: boolean;
|
|
144
|
+
}) => Promise<Json | ConfirmationRequired>;
|
|
145
|
+
};
|
|
146
|
+
readonly memories: {
|
|
147
|
+
/** Save one fact. `static: true` is a standing fact about the user and goes to their profile;
|
|
148
|
+
* otherwise a note the container reads. `container` may be omitted when exactly one is in reach. */
|
|
149
|
+
add: (params: {
|
|
150
|
+
content: string;
|
|
151
|
+
container?: string;
|
|
152
|
+
static?: boolean;
|
|
153
|
+
title?: string;
|
|
154
|
+
}) => Promise<Json>;
|
|
155
|
+
/** Forget one learned fact. Same confirmation rule as `documents.delete`. */
|
|
156
|
+
forget: (memoryId: string, params?: {
|
|
157
|
+
container?: string;
|
|
158
|
+
confirm?: boolean;
|
|
159
|
+
}) => Promise<Json | ConfirmationRequired>;
|
|
160
|
+
};
|
|
161
|
+
constructor(opts?: MembaseOptions);
|
|
162
|
+
/** Hand a document (its text, or a public `url` to fetch) to a container. Returns at once with
|
|
163
|
+
* `status: queued` and a `document_id`; the container learns it in the background —
|
|
164
|
+
* `documents.get(id).learned` turns true when it has. The same `customId` again is a no-op,
|
|
165
|
+
* so retries are safe. */
|
|
166
|
+
add(params: AddParams): Promise<AddDocumentResult>;
|
|
167
|
+
/** Passages the containers hold on `q`, most relevant first, each naming its container.
|
|
168
|
+
* `container` omitted searches every container in reach. Retrieval, not an answer — `ask` is
|
|
169
|
+
* the agent's answer. `containers[]` marks any container that could not answer yet. */
|
|
170
|
+
search(params: SearchParams): Promise<SearchResult>;
|
|
171
|
+
/** Who the user is: `static` (standing facts), `dynamic` (recently changed facts) and, with
|
|
172
|
+
* `q`, `results` relevant to the topic. Needs a key minted with the profile tick. */
|
|
173
|
+
profile(params?: {
|
|
174
|
+
q?: string;
|
|
175
|
+
}): Promise<Profile>;
|
|
176
|
+
/** The exposed agent's answer to `message` (an agent-endpoint credential). */
|
|
177
|
+
ask(params: {
|
|
178
|
+
message: string;
|
|
179
|
+
model?: string;
|
|
180
|
+
}): Promise<Json>;
|
|
181
|
+
/** The user's standing rules for how their memory is used by this credential. */
|
|
182
|
+
rules(): Promise<Json>;
|
|
183
|
+
private request;
|
|
184
|
+
}
|
|
185
|
+
export {};
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Membase client: one developer key, the account's memory.
|
|
3
|
+
*
|
|
4
|
+
* import { Membase } from "membase-ai";
|
|
5
|
+
*
|
|
6
|
+
* const client = new Membase(); // MEMBASE_API_KEY, optional MEMBASE_BASE_URL
|
|
7
|
+
* await client.add({ content: "Call notes …", container: "mv-…", customId: "call-1" });
|
|
8
|
+
* await client.search({ q: "what did we decide about the ledger" });
|
|
9
|
+
* await client.profile({ q: "working hours" });
|
|
10
|
+
*
|
|
11
|
+
* Every method is one operation of the agent protocol (the REST namespace under
|
|
12
|
+
* `https://api.app.membase.io/v1`); the access level, reach and confirmation rules are enforced
|
|
13
|
+
* server-side, so nothing here can do what the key cannot.
|
|
14
|
+
*/
|
|
15
|
+
import { APIConnectionError, APITimeoutError, errorFor } from "./errors.js";
|
|
16
|
+
export const VERSION = "0.2.0";
|
|
17
|
+
export const DEFAULT_BASE_URL = "https://api.app.membase.io";
|
|
18
|
+
/** A search is a turn inside the user's memory; the first one after a quiet spell can take up
|
|
19
|
+
* to a minute while the memory wakes, so the default is generous. */
|
|
20
|
+
export const DEFAULT_TIMEOUT_MS = 90_000;
|
|
21
|
+
export const DEFAULT_MAX_RETRIES = 2;
|
|
22
|
+
const RETRY_STATUSES = new Set([408, 409, 429]);
|
|
23
|
+
export class Membase {
|
|
24
|
+
baseUrl;
|
|
25
|
+
maxRetries;
|
|
26
|
+
timeoutMs;
|
|
27
|
+
apiKey;
|
|
28
|
+
fetchImpl;
|
|
29
|
+
containers = {
|
|
30
|
+
/** The containers this key may use — every one, for the account's owner. */
|
|
31
|
+
list: () => this.request("GET", "/v1/containers"),
|
|
32
|
+
};
|
|
33
|
+
documents = {
|
|
34
|
+
/** The documents the containers in reach have read, newest first, each with `learned`. */
|
|
35
|
+
list: (params = {}) => this.request("GET", "/v1/documents", { query: { container: params.container } }),
|
|
36
|
+
/** One document, with whether its container has learned it yet. */
|
|
37
|
+
get: (documentId) => this.request("GET", `/v1/documents/${encodeURIComponent(documentId)}`),
|
|
38
|
+
/** Remove one document everywhere (the file goes to the Files trash). Needs Full access and
|
|
39
|
+
* `confirm: true`; without it the answer is `status: confirmation_required` with a `how`
|
|
40
|
+
* sentence to relay, not an error. */
|
|
41
|
+
delete: (documentId, params = {}) => this.request("DELETE", `/v1/documents/${encodeURIComponent(documentId)}`, {
|
|
42
|
+
query: { confirm: params.confirm ? "true" : "false" },
|
|
43
|
+
}),
|
|
44
|
+
};
|
|
45
|
+
memories = {
|
|
46
|
+
/** Save one fact. `static: true` is a standing fact about the user and goes to their profile;
|
|
47
|
+
* otherwise a note the container reads. `container` may be omitted when exactly one is in reach. */
|
|
48
|
+
add: (params) => this.request("POST", "/v1/memories", {
|
|
49
|
+
body: { content: params.content, container: params.container, static: params.static ?? false, title: params.title ?? "" },
|
|
50
|
+
}),
|
|
51
|
+
/** Forget one learned fact. Same confirmation rule as `documents.delete`. */
|
|
52
|
+
forget: (memoryId, params = {}) => this.request("DELETE", `/v1/memories/${encodeURIComponent(memoryId)}`, {
|
|
53
|
+
query: { container: params.container, confirm: params.confirm ? "true" : "false" },
|
|
54
|
+
}),
|
|
55
|
+
};
|
|
56
|
+
constructor(opts = {}) {
|
|
57
|
+
const env = globalThis.process?.env ?? {};
|
|
58
|
+
const apiKey = opts.apiKey ?? env.MEMBASE_API_KEY;
|
|
59
|
+
if (!apiKey) {
|
|
60
|
+
throw new Error("no API key: pass apiKey or set MEMBASE_API_KEY (Connect › Developer keys in the Membase app)");
|
|
61
|
+
}
|
|
62
|
+
this.apiKey = apiKey;
|
|
63
|
+
this.baseUrl = (opts.baseUrl ?? env.MEMBASE_BASE_URL ?? DEFAULT_BASE_URL).replace(/\/+$/, "");
|
|
64
|
+
this.timeoutMs = opts.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
65
|
+
this.maxRetries = Math.max(0, opts.maxRetries ?? DEFAULT_MAX_RETRIES);
|
|
66
|
+
this.fetchImpl = opts.fetch ?? fetch;
|
|
67
|
+
}
|
|
68
|
+
/** Hand a document (its text, or a public `url` to fetch) to a container. Returns at once with
|
|
69
|
+
* `status: queued` and a `document_id`; the container learns it in the background —
|
|
70
|
+
* `documents.get(id).learned` turns true when it has. The same `customId` again is a no-op,
|
|
71
|
+
* so retries are safe. */
|
|
72
|
+
add(params) {
|
|
73
|
+
return this.request("POST", "/v1/documents", {
|
|
74
|
+
body: {
|
|
75
|
+
container: params.container,
|
|
76
|
+
content: params.content,
|
|
77
|
+
url: params.url,
|
|
78
|
+
title: params.title ?? "",
|
|
79
|
+
metadata: params.metadata,
|
|
80
|
+
custom_id: params.customId ?? "",
|
|
81
|
+
},
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
/** Passages the containers hold on `q`, most relevant first, each naming its container.
|
|
85
|
+
* `container` omitted searches every container in reach. Retrieval, not an answer — `ask` is
|
|
86
|
+
* the agent's answer. `containers[]` marks any container that could not answer yet. */
|
|
87
|
+
search(params) {
|
|
88
|
+
return this.request("POST", "/v1/search", { body: { q: params.q, container: params.container, limit: params.limit } });
|
|
89
|
+
}
|
|
90
|
+
/** Who the user is: `static` (standing facts), `dynamic` (recently changed facts) and, with
|
|
91
|
+
* `q`, `results` relevant to the topic. Needs a key minted with the profile tick. */
|
|
92
|
+
profile(params = {}) {
|
|
93
|
+
return this.request("GET", "/v1/profile", { query: { q: params.q } });
|
|
94
|
+
}
|
|
95
|
+
/** The exposed agent's answer to `message` (an agent-endpoint credential). */
|
|
96
|
+
ask(params) {
|
|
97
|
+
return this.request("POST", "/v1/ask", { body: { message: params.message, model: params.model } });
|
|
98
|
+
}
|
|
99
|
+
/** The user's standing rules for how their memory is used by this credential. */
|
|
100
|
+
rules() {
|
|
101
|
+
return this.request("GET", "/v1/rules");
|
|
102
|
+
}
|
|
103
|
+
// -- plumbing --------------------------------------------------------------------------
|
|
104
|
+
async request(method, path, opts = {}) {
|
|
105
|
+
const url = new URL(this.baseUrl + path);
|
|
106
|
+
for (const [k, v] of Object.entries(opts.query ?? {}))
|
|
107
|
+
if (v !== undefined)
|
|
108
|
+
url.searchParams.set(k, String(v));
|
|
109
|
+
const headers = {
|
|
110
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
111
|
+
Accept: "application/json",
|
|
112
|
+
"User-Agent": `membase-ai-typescript/${VERSION}`,
|
|
113
|
+
};
|
|
114
|
+
let body;
|
|
115
|
+
if (opts.body !== undefined) {
|
|
116
|
+
headers["Content-Type"] = "application/json";
|
|
117
|
+
body = JSON.stringify(Object.fromEntries(Object.entries(opts.body).filter(([, v]) => v !== undefined)));
|
|
118
|
+
}
|
|
119
|
+
let attempt = 0;
|
|
120
|
+
for (;;) {
|
|
121
|
+
const controller = new AbortController();
|
|
122
|
+
const timer = setTimeout(() => controller.abort(), this.timeoutMs);
|
|
123
|
+
let res;
|
|
124
|
+
try {
|
|
125
|
+
res = await this.fetchImpl(url.toString(), { method, headers, body, signal: controller.signal });
|
|
126
|
+
}
|
|
127
|
+
catch (e) {
|
|
128
|
+
clearTimeout(timer);
|
|
129
|
+
if (attempt < this.maxRetries) {
|
|
130
|
+
attempt += 1;
|
|
131
|
+
await sleep(backoff(attempt));
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
const aborted = e?.name === "AbortError";
|
|
135
|
+
throw aborted
|
|
136
|
+
? new APITimeoutError(`${method} ${path} timed out after ${this.timeoutMs} ms`)
|
|
137
|
+
: new APIConnectionError(`${method} ${path}: ${e?.message ?? String(e)}`);
|
|
138
|
+
}
|
|
139
|
+
clearTimeout(timer);
|
|
140
|
+
if (res.ok) {
|
|
141
|
+
const text = await res.text();
|
|
142
|
+
return (text ? JSON.parse(text) : null);
|
|
143
|
+
}
|
|
144
|
+
if ((RETRY_STATUSES.has(res.status) || res.status >= 500) && attempt < this.maxRetries) {
|
|
145
|
+
attempt += 1;
|
|
146
|
+
await sleep(backoff(attempt, res.headers.get("Retry-After")));
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
let parsed;
|
|
150
|
+
const text = await res.text();
|
|
151
|
+
try {
|
|
152
|
+
parsed = text ? JSON.parse(text) : {};
|
|
153
|
+
}
|
|
154
|
+
catch {
|
|
155
|
+
parsed = { error: { message: text } };
|
|
156
|
+
}
|
|
157
|
+
throw errorFor(res.status, parsed);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
function backoff(attempt, retryAfter) {
|
|
162
|
+
if (retryAfter) {
|
|
163
|
+
const s = Number(retryAfter);
|
|
164
|
+
if (Number.isFinite(s))
|
|
165
|
+
return Math.min(s, 30) * 1000;
|
|
166
|
+
}
|
|
167
|
+
return Math.min(500 * 2 ** (attempt - 1), 8000) * (0.5 + Math.random());
|
|
168
|
+
}
|
|
169
|
+
function sleep(ms) {
|
|
170
|
+
return new Promise((r) => setTimeout(r, ms));
|
|
171
|
+
}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Errors, one class per HTTP status class, all carrying the platform's error envelope.
|
|
3
|
+
*
|
|
4
|
+
* The API answers every failure as `{"error": {"code", "message", "details", "retryable",
|
|
5
|
+
* "trace_id"}}`. The status decides the class (so a caller can `instanceof RateLimitError`),
|
|
6
|
+
* `code` says which rule refused (`unauthorized` for a container outside the key's reach,
|
|
7
|
+
* `capability_unavailable` when the account's memory cannot run a turn, …), and `traceId` is
|
|
8
|
+
* what to quote when reporting a problem.
|
|
9
|
+
*/
|
|
10
|
+
export interface ErrorEnvelope {
|
|
11
|
+
error?: {
|
|
12
|
+
code?: string;
|
|
13
|
+
message?: string;
|
|
14
|
+
details?: unknown;
|
|
15
|
+
retryable?: boolean;
|
|
16
|
+
trace_id?: string;
|
|
17
|
+
};
|
|
18
|
+
detail?: unknown;
|
|
19
|
+
}
|
|
20
|
+
export declare class MembaseError extends Error {
|
|
21
|
+
readonly status: number | undefined;
|
|
22
|
+
readonly body: unknown;
|
|
23
|
+
readonly code: string;
|
|
24
|
+
readonly details: unknown;
|
|
25
|
+
readonly retryable: boolean;
|
|
26
|
+
readonly traceId: string;
|
|
27
|
+
constructor(message: string, opts?: {
|
|
28
|
+
status?: number;
|
|
29
|
+
body?: unknown;
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
/** The request never got an answer (DNS, connection refused, reset). */
|
|
33
|
+
export declare class APIConnectionError extends MembaseError {
|
|
34
|
+
}
|
|
35
|
+
/** The request ran past the client's timeout. */
|
|
36
|
+
export declare class APITimeoutError extends APIConnectionError {
|
|
37
|
+
}
|
|
38
|
+
/** A non-2xx answer. Subclasses per status class below. */
|
|
39
|
+
export declare class APIStatusError extends MembaseError {
|
|
40
|
+
}
|
|
41
|
+
/** 400 — a malformed request: a missing `q`, both `content` and `url`, an ambiguous `container`. */
|
|
42
|
+
export declare class BadRequestError extends APIStatusError {
|
|
43
|
+
}
|
|
44
|
+
/** 401 — the request carried no bearer at all. */
|
|
45
|
+
export declare class AuthenticationError extends APIStatusError {
|
|
46
|
+
}
|
|
47
|
+
/** 403 (`code: unauthorized`) — an unknown, expired or revoked key; a container outside the key's reach; or a verb above its access level. */
|
|
48
|
+
export declare class PermissionDeniedError extends APIStatusError {
|
|
49
|
+
}
|
|
50
|
+
/** 404 — an unknown document or memory id. */
|
|
51
|
+
export declare class NotFoundError extends APIStatusError {
|
|
52
|
+
}
|
|
53
|
+
/** 409. */
|
|
54
|
+
export declare class ConflictError extends APIStatusError {
|
|
55
|
+
}
|
|
56
|
+
/** 422 — the account's memory cannot run a turn on this deployment (`capability_unavailable`). */
|
|
57
|
+
export declare class UnprocessableEntityError extends APIStatusError {
|
|
58
|
+
}
|
|
59
|
+
/** 429 — the account's turn budget is spent for now. Retried automatically; retry later. */
|
|
60
|
+
export declare class RateLimitError extends APIStatusError {
|
|
61
|
+
}
|
|
62
|
+
/** 5xx. */
|
|
63
|
+
export declare class InternalServerError extends APIStatusError {
|
|
64
|
+
}
|
|
65
|
+
/** The exception for a non-2xx answer. */
|
|
66
|
+
export declare function errorFor(status: number, body: unknown, fallback?: string): APIStatusError;
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Errors, one class per HTTP status class, all carrying the platform's error envelope.
|
|
3
|
+
*
|
|
4
|
+
* The API answers every failure as `{"error": {"code", "message", "details", "retryable",
|
|
5
|
+
* "trace_id"}}`. The status decides the class (so a caller can `instanceof RateLimitError`),
|
|
6
|
+
* `code` says which rule refused (`unauthorized` for a container outside the key's reach,
|
|
7
|
+
* `capability_unavailable` when the account's memory cannot run a turn, …), and `traceId` is
|
|
8
|
+
* what to quote when reporting a problem.
|
|
9
|
+
*/
|
|
10
|
+
export class MembaseError extends Error {
|
|
11
|
+
status;
|
|
12
|
+
body;
|
|
13
|
+
code;
|
|
14
|
+
details;
|
|
15
|
+
retryable;
|
|
16
|
+
traceId;
|
|
17
|
+
constructor(message, opts = {}) {
|
|
18
|
+
super(message);
|
|
19
|
+
this.name = new.target.name;
|
|
20
|
+
this.status = opts.status;
|
|
21
|
+
this.body = opts.body;
|
|
22
|
+
const err = opts.body?.error ?? {};
|
|
23
|
+
this.code = String(err.code ?? "");
|
|
24
|
+
this.details = err.details;
|
|
25
|
+
this.retryable = Boolean(err.retryable ?? false);
|
|
26
|
+
this.traceId = String(err.trace_id ?? "");
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/** The request never got an answer (DNS, connection refused, reset). */
|
|
30
|
+
export class APIConnectionError extends MembaseError {
|
|
31
|
+
}
|
|
32
|
+
/** The request ran past the client's timeout. */
|
|
33
|
+
export class APITimeoutError extends APIConnectionError {
|
|
34
|
+
}
|
|
35
|
+
/** A non-2xx answer. Subclasses per status class below. */
|
|
36
|
+
export class APIStatusError extends MembaseError {
|
|
37
|
+
}
|
|
38
|
+
/** 400 — a malformed request: a missing `q`, both `content` and `url`, an ambiguous `container`. */
|
|
39
|
+
export class BadRequestError extends APIStatusError {
|
|
40
|
+
}
|
|
41
|
+
/** 401 — the request carried no bearer at all. */
|
|
42
|
+
export class AuthenticationError extends APIStatusError {
|
|
43
|
+
}
|
|
44
|
+
/** 403 (`code: unauthorized`) — an unknown, expired or revoked key; a container outside the key's reach; or a verb above its access level. */
|
|
45
|
+
export class PermissionDeniedError extends APIStatusError {
|
|
46
|
+
}
|
|
47
|
+
/** 404 — an unknown document or memory id. */
|
|
48
|
+
export class NotFoundError extends APIStatusError {
|
|
49
|
+
}
|
|
50
|
+
/** 409. */
|
|
51
|
+
export class ConflictError extends APIStatusError {
|
|
52
|
+
}
|
|
53
|
+
/** 422 — the account's memory cannot run a turn on this deployment (`capability_unavailable`). */
|
|
54
|
+
export class UnprocessableEntityError extends APIStatusError {
|
|
55
|
+
}
|
|
56
|
+
/** 429 — the account's turn budget is spent for now. Retried automatically; retry later. */
|
|
57
|
+
export class RateLimitError extends APIStatusError {
|
|
58
|
+
}
|
|
59
|
+
/** 5xx. */
|
|
60
|
+
export class InternalServerError extends APIStatusError {
|
|
61
|
+
}
|
|
62
|
+
const BY_STATUS = {
|
|
63
|
+
400: BadRequestError,
|
|
64
|
+
401: AuthenticationError,
|
|
65
|
+
403: PermissionDeniedError,
|
|
66
|
+
404: NotFoundError,
|
|
67
|
+
409: ConflictError,
|
|
68
|
+
422: UnprocessableEntityError,
|
|
69
|
+
429: RateLimitError,
|
|
70
|
+
};
|
|
71
|
+
/** The exception for a non-2xx answer. */
|
|
72
|
+
export function errorFor(status, body, fallback = "") {
|
|
73
|
+
const env = (body ?? {});
|
|
74
|
+
let message = env.error?.message;
|
|
75
|
+
if (!message && env.detail !== undefined)
|
|
76
|
+
message = typeof env.detail === "string" ? env.detail : JSON.stringify(env.detail);
|
|
77
|
+
message = message || fallback || `HTTP ${status}`;
|
|
78
|
+
const Cls = BY_STATUS[status] ?? (status >= 500 ? InternalServerError : APIStatusError);
|
|
79
|
+
return new Cls(message, { status, body });
|
|
80
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { Membase, DEFAULT_BASE_URL, DEFAULT_TIMEOUT_MS, DEFAULT_MAX_RETRIES, VERSION } from "./client.js";
|
|
2
|
+
export type { MembaseOptions, AddParams, SearchParams, Container, ContainerList, SearchHit, SearchResult, Profile, Document, DocumentList, AddDocumentResult, ConfirmationRequired, Json, } from "./client.js";
|
|
3
|
+
export { MembaseError, APIConnectionError, APITimeoutError, APIStatusError, BadRequestError, AuthenticationError, PermissionDeniedError, NotFoundError, ConflictError, UnprocessableEntityError, RateLimitError, InternalServerError, } from "./errors.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export { Membase, DEFAULT_BASE_URL, DEFAULT_TIMEOUT_MS, DEFAULT_MAX_RETRIES, VERSION } from "./client.js";
|
|
2
|
+
export { MembaseError, APIConnectionError, APITimeoutError, APIStatusError, BadRequestError, AuthenticationError, PermissionDeniedError, NotFoundError, ConflictError, UnprocessableEntityError, RateLimitError, InternalServerError, } from "./errors.js";
|
package/package.json
CHANGED
|
@@ -1,35 +1,46 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "membase-ai",
|
|
3
|
-
"version": "0.0
|
|
4
|
-
"description": "Membase — long-term memory for AI.
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Membase — long-term memory for AI. The TypeScript client for hosted Membase, or local memory via `membase serve`.",
|
|
5
|
+
"license": "MIT",
|
|
5
6
|
"type": "module",
|
|
6
|
-
"main": "./index.js",
|
|
7
|
-
"types": "./index.d.ts",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
8
9
|
"exports": {
|
|
9
10
|
".": {
|
|
10
|
-
"types": "./index.d.ts",
|
|
11
|
-
"import": "./index.js"
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js"
|
|
12
13
|
}
|
|
13
14
|
},
|
|
14
15
|
"files": [
|
|
15
|
-
"
|
|
16
|
-
"index.d.ts",
|
|
16
|
+
"dist",
|
|
17
17
|
"README.md"
|
|
18
18
|
],
|
|
19
|
-
"
|
|
20
|
-
|
|
19
|
+
"sideEffects": false,
|
|
20
|
+
"engines": {
|
|
21
|
+
"node": ">=18"
|
|
22
|
+
},
|
|
23
|
+
"scripts": {
|
|
24
|
+
"build": "tsc -p tsconfig.build.json",
|
|
25
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
26
|
+
"test": "vitest run"
|
|
21
27
|
},
|
|
22
28
|
"keywords": [
|
|
23
29
|
"membase",
|
|
24
30
|
"memory",
|
|
25
|
-
"llm",
|
|
26
31
|
"ai",
|
|
27
|
-
"agents"
|
|
32
|
+
"agents",
|
|
33
|
+
"mcp"
|
|
28
34
|
],
|
|
29
35
|
"homepage": "https://membase.ai",
|
|
30
|
-
"
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
"
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@types/node": "^20.19.43",
|
|
38
|
+
"typescript": "^5.6.2",
|
|
39
|
+
"vitest": "^2.1.9"
|
|
40
|
+
},
|
|
41
|
+
"repository": {
|
|
42
|
+
"type": "git",
|
|
43
|
+
"url": "git+https://github.com/unibaseio/membase-ai.git",
|
|
44
|
+
"directory": "typescript"
|
|
34
45
|
}
|
|
35
46
|
}
|
package/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "membase-sdk";
|
package/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "membase-sdk";
|