xmemory 1.0.1 → 1.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 +129 -3
- package/dist/client.d.ts +15 -6
- package/dist/client.js +23 -7
- package/dist/types.d.ts +19 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,8 +1,134 @@
|
|
|
1
|
-
#
|
|
1
|
+
# xmemory
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
TypeScript/JavaScript client library for the [Xmemory](https://xmemory.ai) API.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
## Quick start
|
|
6
|
+
|
|
7
|
+
```typescript
|
|
8
|
+
import { xmemoryInstance } from "xmemory";
|
|
9
|
+
|
|
10
|
+
const mem = await xmemoryInstance({
|
|
11
|
+
url: "https://api.xmemory.ai", // or set XMEM_API_URL env var
|
|
12
|
+
token: "<your-token>", // or set XMEM_AUTH_TOKEN env var
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
mem.instanceId = "<your-instance-id>";
|
|
16
|
+
|
|
17
|
+
await mem.write("Alice is a software engineer who loves TypeScript.");
|
|
18
|
+
const result = await mem.read("What does Alice do?");
|
|
19
|
+
console.log(result.reader_result?.answer);
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Installation
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
npm install xmemory
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Configuration
|
|
29
|
+
|
|
30
|
+
| Parameter | Env var | Default | Description |
|
|
31
|
+
|-------------|-------------------|---------------------------|---------------------------------------|
|
|
32
|
+
| `url` | `XMEM_API_URL` | `http://0.0.0.0:8000` | Base URL of the Xmemory API |
|
|
33
|
+
| `token` | `XMEM_AUTH_TOKEN` | `undefined` | Bearer token for authentication |
|
|
34
|
+
| `timeoutMs` | — | `60000` | Default request timeout in milliseconds |
|
|
35
|
+
|
|
36
|
+
## Creating a client
|
|
37
|
+
|
|
38
|
+
```typescript
|
|
39
|
+
import { XmemoryClient, xmemoryInstance } from "xmemory";
|
|
40
|
+
|
|
41
|
+
// Option 1: factory function (runs a health check automatically)
|
|
42
|
+
const mem = await xmemoryInstance({ url: "https://api.xmemory.ai", token: "..." });
|
|
43
|
+
|
|
44
|
+
// Option 2: static create method (also runs a health check)
|
|
45
|
+
const mem = await XmemoryClient.create({ url: "https://api.xmemory.ai", token: "..." });
|
|
46
|
+
|
|
47
|
+
// Option 3: constructor (no health check)
|
|
48
|
+
const mem = new XmemoryClient({ url: "https://api.xmemory.ai", token: "..." });
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Methods
|
|
52
|
+
|
|
53
|
+
### `createInstance(schemaText, schemaType, timeoutMs?) → Promise<boolean>`
|
|
54
|
+
|
|
55
|
+
Create a new instance with the given schema. On success the new `instanceId`
|
|
56
|
+
is saved automatically and used for subsequent calls.
|
|
57
|
+
|
|
58
|
+
```typescript
|
|
59
|
+
import { SchemaType } from "xmemory";
|
|
60
|
+
|
|
61
|
+
const ok = await mem.createInstance(schemaYml, SchemaType.YML);
|
|
62
|
+
const ok = await mem.createInstance(schemaJson, SchemaType.JSON);
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### `write(text, options?) → Promise<WriteResponse>`
|
|
66
|
+
|
|
67
|
+
Extract structured objects from `text` and store them in the instance.
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
const resp = await mem.write("Bob joined the team on Monday as a designer.");
|
|
71
|
+
console.log(resp.status); // "ok" or "error"
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Options: `{ timeoutMs?, extractionLogic? }` where `extractionLogic` is `"fast"`, `"regular"`, or `"deep"` (default: `"deep"`).
|
|
75
|
+
|
|
76
|
+
### `writeAsync(text, options?) → Promise<AsyncWriteResponse>`
|
|
77
|
+
|
|
78
|
+
Start an asynchronous write and return immediately with a `write_id` for tracking.
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
const resp = await mem.writeAsync("Carol is a manager based in Berlin.", {
|
|
82
|
+
extractionLogic: "deep",
|
|
83
|
+
});
|
|
84
|
+
console.log(resp.write_id); // use this to check status
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
Options: `{ timeoutMs?, extractionLogic?, extractWriteId? }`.
|
|
88
|
+
|
|
89
|
+
### `writeStatus(writeId, options?) → Promise<WriteStatusResponse>`
|
|
90
|
+
|
|
91
|
+
Check the status of an async write operation.
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
const status = await mem.writeStatus(resp.write_id);
|
|
95
|
+
console.log(status.write_status); // "queued" | "processing" | "completed" | "failed" | "not_found"
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### `read(query, options?) → Promise<ReadResponse>`
|
|
99
|
+
|
|
100
|
+
Query the instance and get a natural-language answer.
|
|
101
|
+
|
|
102
|
+
```typescript
|
|
103
|
+
const resp = await mem.read("Who is on the team?");
|
|
104
|
+
console.log(resp.reader_result?.answer);
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
Options: `{ timeoutMs? }`.
|
|
108
|
+
|
|
109
|
+
## Error handling
|
|
110
|
+
|
|
111
|
+
All errors raise `XmemoryAPIError`.
|
|
112
|
+
|
|
113
|
+
```typescript
|
|
114
|
+
import { XmemoryAPIError, xmemoryInstance } from "xmemory";
|
|
115
|
+
|
|
116
|
+
const mem = await xmemoryInstance({ url: "https://api.xmemory.ai", token: "..." });
|
|
117
|
+
|
|
118
|
+
try {
|
|
119
|
+
const resp = await mem.read("something");
|
|
120
|
+
} catch (e) {
|
|
121
|
+
if (e instanceof XmemoryAPIError) {
|
|
122
|
+
console.error(`API error (HTTP ${e.status}): ${e.message}`);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
---
|
|
128
|
+
|
|
129
|
+
## Mastra integration
|
|
130
|
+
|
|
131
|
+
You can also use `xmemory` as an MCP server within [Mastra.ai](https://mastra.ai).
|
|
6
132
|
|
|
7
133
|
First, create a local Mastra instance:
|
|
8
134
|
|
package/dist/client.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* xmemory HTTP client for Node.js.
|
|
3
3
|
*/
|
|
4
|
-
import type { ReadResponse, SchemaTypeValue, WriteResponse } from "./types.js";
|
|
4
|
+
import type { AsyncWriteResponse, ExtractionLogic, ReadResponse, SchemaTypeValue, WriteResponse, WriteStatusResponse } from "./types.js";
|
|
5
5
|
export { SchemaType } from "./types.js";
|
|
6
|
-
export type {
|
|
6
|
+
export type { AsyncWriteResponse, CreateInstanceResponse, ExtractionLogic, ReadResponse, ReaderResult, SchemaTypeValue, WriteQueueStatus, WriteResponse, WriteStatusResponse, } from "./types.js";
|
|
7
7
|
export declare class XmemoryAPIError extends Error {
|
|
8
8
|
readonly status?: number | undefined;
|
|
9
9
|
constructor(message: string, status?: number | undefined);
|
|
@@ -11,7 +11,7 @@ export declare class XmemoryAPIError extends Error {
|
|
|
11
11
|
export interface XmemoryInstanceOptions {
|
|
12
12
|
url?: string;
|
|
13
13
|
token?: string;
|
|
14
|
-
|
|
14
|
+
timeoutMs?: number;
|
|
15
15
|
}
|
|
16
16
|
export declare class XmemoryClient {
|
|
17
17
|
readonly baseUrl: string;
|
|
@@ -22,12 +22,21 @@ export declare class XmemoryClient {
|
|
|
22
22
|
static create(options?: XmemoryInstanceOptions): Promise<XmemoryClient>;
|
|
23
23
|
private checkHealth;
|
|
24
24
|
private requireInstanceId;
|
|
25
|
-
createInstance(schemaText: string, schemaType: SchemaTypeValue,
|
|
25
|
+
createInstance(schemaText: string, schemaType: SchemaTypeValue, timeoutMs?: number): Promise<boolean>;
|
|
26
26
|
write(text: string, options?: {
|
|
27
|
-
|
|
27
|
+
timeoutMs?: number;
|
|
28
|
+
extractionLogic?: ExtractionLogic;
|
|
28
29
|
}): Promise<WriteResponse>;
|
|
30
|
+
writeAsync(text: string, options?: {
|
|
31
|
+
timeoutMs?: number;
|
|
32
|
+
extractionLogic?: ExtractionLogic;
|
|
33
|
+
extractWriteId?: string;
|
|
34
|
+
}): Promise<AsyncWriteResponse>;
|
|
35
|
+
writeStatus(writeId: string, options?: {
|
|
36
|
+
timeoutMs?: number;
|
|
37
|
+
}): Promise<WriteStatusResponse>;
|
|
29
38
|
read(query: string, options?: {
|
|
30
|
-
|
|
39
|
+
timeoutMs?: number;
|
|
31
40
|
}): Promise<ReadResponse>;
|
|
32
41
|
get instance_id(): string | null;
|
|
33
42
|
}
|
package/dist/client.js
CHANGED
|
@@ -73,7 +73,7 @@ export class XmemoryClient {
|
|
|
73
73
|
instanceId = null;
|
|
74
74
|
constructor(options = {}) {
|
|
75
75
|
this.baseUrl = options.url ?? "";
|
|
76
|
-
this.timeoutMs =
|
|
76
|
+
this.timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
77
77
|
this.token = options.token;
|
|
78
78
|
}
|
|
79
79
|
static async create(options = {}) {
|
|
@@ -100,11 +100,10 @@ export class XmemoryClient {
|
|
|
100
100
|
}
|
|
101
101
|
return this.instanceId;
|
|
102
102
|
}
|
|
103
|
-
async createInstance(schemaText, schemaType,
|
|
103
|
+
async createInstance(schemaText, schemaType, timeoutMs) {
|
|
104
104
|
const path = "/instance/create";
|
|
105
105
|
const body = schemaType === 0 ? { yml_schema: schemaText } : { json_schema: schemaText };
|
|
106
|
-
const
|
|
107
|
-
const response = await postJson(this.baseUrl, path, body, this.token, timeoutMs);
|
|
106
|
+
const response = await postJson(this.baseUrl, path, body, this.token, timeoutMs ?? this.timeoutMs);
|
|
108
107
|
if (response.status === "ok" && response.instance_id) {
|
|
109
108
|
this.instanceId = response.instance_id;
|
|
110
109
|
}
|
|
@@ -112,16 +111,33 @@ export class XmemoryClient {
|
|
|
112
111
|
}
|
|
113
112
|
async write(text, options) {
|
|
114
113
|
const iid = this.requireInstanceId("write");
|
|
115
|
-
const timeoutMs = options?.
|
|
114
|
+
const timeoutMs = options?.timeoutMs ?? this.timeoutMs;
|
|
116
115
|
return postJson(this.baseUrl, "/write", {
|
|
117
116
|
instance_id: iid,
|
|
118
117
|
text,
|
|
119
|
-
extraction_logic: "deep",
|
|
118
|
+
extraction_logic: options?.extractionLogic ?? "deep",
|
|
120
119
|
}, this.token, timeoutMs);
|
|
121
120
|
}
|
|
121
|
+
async writeAsync(text, options) {
|
|
122
|
+
const iid = this.requireInstanceId("writeAsync");
|
|
123
|
+
const timeoutMs = options?.timeoutMs ?? this.timeoutMs;
|
|
124
|
+
const body = {
|
|
125
|
+
instance_id: iid,
|
|
126
|
+
text,
|
|
127
|
+
extraction_logic: options?.extractionLogic ?? "deep",
|
|
128
|
+
};
|
|
129
|
+
if (options?.extractWriteId != null) {
|
|
130
|
+
body["extract_write_id"] = options.extractWriteId;
|
|
131
|
+
}
|
|
132
|
+
return postJson(this.baseUrl, "/write_async", body, this.token, timeoutMs);
|
|
133
|
+
}
|
|
134
|
+
async writeStatus(writeId, options) {
|
|
135
|
+
const timeoutMs = options?.timeoutMs ?? this.timeoutMs;
|
|
136
|
+
return postJson(this.baseUrl, "/write_status", { write_id: writeId }, this.token, timeoutMs);
|
|
137
|
+
}
|
|
122
138
|
async read(query, options) {
|
|
123
139
|
const iid = this.requireInstanceId("read");
|
|
124
|
-
const timeoutMs = options?.
|
|
140
|
+
const timeoutMs = options?.timeoutMs ?? this.timeoutMs;
|
|
125
141
|
return postJson(this.baseUrl, "/read", {
|
|
126
142
|
instance_id: iid,
|
|
127
143
|
query,
|
package/dist/types.d.ts
CHANGED
|
@@ -14,10 +14,11 @@ export interface CreateInstanceResponse {
|
|
|
14
14
|
instance_id?: string | null;
|
|
15
15
|
error_message?: string | null;
|
|
16
16
|
}
|
|
17
|
+
export type ExtractionLogic = "fast" | "regular" | "deep";
|
|
17
18
|
export interface WriteRequest {
|
|
18
19
|
instance_id: string;
|
|
19
20
|
text: string;
|
|
20
|
-
extraction_logic?:
|
|
21
|
+
extraction_logic?: ExtractionLogic;
|
|
21
22
|
}
|
|
22
23
|
export interface WriteResponse {
|
|
23
24
|
status: "ok" | "error";
|
|
@@ -37,3 +38,20 @@ export interface ReadResponse {
|
|
|
37
38
|
reader_result?: ReaderResult | null;
|
|
38
39
|
error_message?: string | null;
|
|
39
40
|
}
|
|
41
|
+
export interface AsyncWriteResponse {
|
|
42
|
+
status: "ok" | "error";
|
|
43
|
+
write_id?: string | null;
|
|
44
|
+
error_message?: string | null;
|
|
45
|
+
}
|
|
46
|
+
export type WriteQueueStatus = "queued" | "processing" | "completed" | "failed" | "not_found";
|
|
47
|
+
export interface WriteStatusRequest {
|
|
48
|
+
write_id: string;
|
|
49
|
+
}
|
|
50
|
+
export interface WriteStatusResponse {
|
|
51
|
+
status: "ok" | "error";
|
|
52
|
+
write_id: string;
|
|
53
|
+
write_status: WriteQueueStatus;
|
|
54
|
+
error_detail?: string | null;
|
|
55
|
+
completed_at?: string | null;
|
|
56
|
+
error_message?: string | null;
|
|
57
|
+
}
|