xmemory 1.0.0 → 1.0.1
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/client.d.ts +37 -0
- package/dist/client.js +140 -0
- package/dist/types.d.ts +39 -0
- package/dist/types.js +7 -0
- package/package.json +18 -8
- package/cli.js +0 -6
- package/index.js +0 -11
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* xmemory HTTP client for Node.js.
|
|
3
|
+
*/
|
|
4
|
+
import type { ReadResponse, SchemaTypeValue, WriteResponse } from "./types.js";
|
|
5
|
+
export { SchemaType } from "./types.js";
|
|
6
|
+
export type { SchemaTypeValue, CreateInstanceResponse, WriteResponse, ReadResponse, ReaderResult } from "./types.js";
|
|
7
|
+
export declare class XmemoryAPIError extends Error {
|
|
8
|
+
readonly status?: number | undefined;
|
|
9
|
+
constructor(message: string, status?: number | undefined);
|
|
10
|
+
}
|
|
11
|
+
export interface XmemoryInstanceOptions {
|
|
12
|
+
url?: string;
|
|
13
|
+
token?: string;
|
|
14
|
+
timeout?: number;
|
|
15
|
+
}
|
|
16
|
+
export declare class XmemoryClient {
|
|
17
|
+
readonly baseUrl: string;
|
|
18
|
+
readonly timeoutMs: number;
|
|
19
|
+
readonly token: string | undefined;
|
|
20
|
+
instanceId: string | null;
|
|
21
|
+
constructor(options?: XmemoryInstanceOptions);
|
|
22
|
+
static create(options?: XmemoryInstanceOptions): Promise<XmemoryClient>;
|
|
23
|
+
private checkHealth;
|
|
24
|
+
private requireInstanceId;
|
|
25
|
+
createInstance(schemaText: string, schemaType: SchemaTypeValue, timeout?: number): Promise<boolean>;
|
|
26
|
+
write(text: string, options?: {
|
|
27
|
+
timeout?: number;
|
|
28
|
+
}): Promise<WriteResponse>;
|
|
29
|
+
read(query: string, options?: {
|
|
30
|
+
timeout?: number;
|
|
31
|
+
}): Promise<ReadResponse>;
|
|
32
|
+
get instance_id(): string | null;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Create an xmemory client.
|
|
36
|
+
*/
|
|
37
|
+
export declare function xmemoryInstance(options?: XmemoryInstanceOptions): Promise<XmemoryClient>;
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* xmemory HTTP client for Node.js.
|
|
3
|
+
*/
|
|
4
|
+
export { SchemaType } from "./types.js";
|
|
5
|
+
const DEFAULT_BASE_URL = "http://0.0.0.0:8000";
|
|
6
|
+
const DEFAULT_TIMEOUT_MS = 60_000;
|
|
7
|
+
export class XmemoryAPIError extends Error {
|
|
8
|
+
status;
|
|
9
|
+
constructor(message, status) {
|
|
10
|
+
super(message);
|
|
11
|
+
this.status = status;
|
|
12
|
+
this.name = "XmemoryAPIError";
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
async function getEnv(name) {
|
|
16
|
+
if (typeof process !== "undefined" && process.env) {
|
|
17
|
+
return process.env[name];
|
|
18
|
+
}
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
async function fetchWithTimeout(url, options) {
|
|
22
|
+
const { timeoutMs = DEFAULT_TIMEOUT_MS, ...fetchOptions } = options;
|
|
23
|
+
const controller = new AbortController();
|
|
24
|
+
const id = setTimeout(() => controller.abort(), timeoutMs);
|
|
25
|
+
try {
|
|
26
|
+
const res = await fetch(url, {
|
|
27
|
+
...fetchOptions,
|
|
28
|
+
signal: controller.signal,
|
|
29
|
+
});
|
|
30
|
+
return res;
|
|
31
|
+
}
|
|
32
|
+
finally {
|
|
33
|
+
clearTimeout(id);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
async function postJson(baseUrl, path, body, token, timeoutMs) {
|
|
37
|
+
const url = `${baseUrl.replace(/\/$/, "")}${path}`;
|
|
38
|
+
const headers = {
|
|
39
|
+
Accept: "application/json",
|
|
40
|
+
"Content-Type": "application/json",
|
|
41
|
+
};
|
|
42
|
+
if (token) {
|
|
43
|
+
headers["Authorization"] = `Bearer ${token}`;
|
|
44
|
+
}
|
|
45
|
+
const res = await fetchWithTimeout(url, {
|
|
46
|
+
method: "POST",
|
|
47
|
+
headers,
|
|
48
|
+
body: JSON.stringify(body),
|
|
49
|
+
timeoutMs,
|
|
50
|
+
});
|
|
51
|
+
const raw = await res.text();
|
|
52
|
+
let payload;
|
|
53
|
+
try {
|
|
54
|
+
payload = raw ? JSON.parse(raw) : {};
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
throw new XmemoryAPIError(`Invalid JSON from server (${res.status})`, res.status);
|
|
58
|
+
}
|
|
59
|
+
if (typeof payload === "object" && payload !== null && payload.status === "error") {
|
|
60
|
+
const err = payload;
|
|
61
|
+
const msg = err.error_message || err.error || String(payload);
|
|
62
|
+
throw new XmemoryAPIError(`${path} failed: ${msg}`, res.status);
|
|
63
|
+
}
|
|
64
|
+
if (!res.ok) {
|
|
65
|
+
throw new XmemoryAPIError(`HTTP ${res.status}: ${raw.slice(0, 200)}`, res.status);
|
|
66
|
+
}
|
|
67
|
+
return payload;
|
|
68
|
+
}
|
|
69
|
+
export class XmemoryClient {
|
|
70
|
+
baseUrl;
|
|
71
|
+
timeoutMs;
|
|
72
|
+
token;
|
|
73
|
+
instanceId = null;
|
|
74
|
+
constructor(options = {}) {
|
|
75
|
+
this.baseUrl = options.url ?? "";
|
|
76
|
+
this.timeoutMs = (options.timeout ?? 60) * 1000;
|
|
77
|
+
this.token = options.token;
|
|
78
|
+
}
|
|
79
|
+
static async create(options = {}) {
|
|
80
|
+
const url = options.url ?? (await getEnv("XMEM_API_URL")) ?? DEFAULT_BASE_URL;
|
|
81
|
+
const token = options.token ?? (await getEnv("XMEM_AUTH_TOKEN"));
|
|
82
|
+
const client = new XmemoryClient({ ...options, url, token });
|
|
83
|
+
await client.checkHealth();
|
|
84
|
+
return client;
|
|
85
|
+
}
|
|
86
|
+
async checkHealth() {
|
|
87
|
+
const url = `${this.baseUrl.replace(/\/$/, "")}/api/healthz`;
|
|
88
|
+
const res = await fetchWithTimeout(url, {
|
|
89
|
+
method: "GET",
|
|
90
|
+
headers: { Accept: "application/json" },
|
|
91
|
+
timeoutMs: this.timeoutMs,
|
|
92
|
+
});
|
|
93
|
+
if (!res.ok) {
|
|
94
|
+
throw new XmemoryAPIError(`Health check failed: ${res.status} at ${url}`, res.status);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
requireInstanceId(op) {
|
|
98
|
+
if (!this.instanceId) {
|
|
99
|
+
throw new XmemoryAPIError(`instance_id is required for ${op}() but none was provided or saved.`);
|
|
100
|
+
}
|
|
101
|
+
return this.instanceId;
|
|
102
|
+
}
|
|
103
|
+
async createInstance(schemaText, schemaType, timeout) {
|
|
104
|
+
const path = "/instance/create";
|
|
105
|
+
const body = schemaType === 0 ? { yml_schema: schemaText } : { json_schema: schemaText };
|
|
106
|
+
const timeoutMs = timeout != null ? timeout * 1000 : this.timeoutMs;
|
|
107
|
+
const response = await postJson(this.baseUrl, path, body, this.token, timeoutMs);
|
|
108
|
+
if (response.status === "ok" && response.instance_id) {
|
|
109
|
+
this.instanceId = response.instance_id;
|
|
110
|
+
}
|
|
111
|
+
return response.status === "ok";
|
|
112
|
+
}
|
|
113
|
+
async write(text, options) {
|
|
114
|
+
const iid = this.requireInstanceId("write");
|
|
115
|
+
const timeoutMs = options?.timeout != null ? options.timeout * 1000 : this.timeoutMs;
|
|
116
|
+
return postJson(this.baseUrl, "/write", {
|
|
117
|
+
instance_id: iid,
|
|
118
|
+
text,
|
|
119
|
+
extraction_logic: "deep",
|
|
120
|
+
}, this.token, timeoutMs);
|
|
121
|
+
}
|
|
122
|
+
async read(query, options) {
|
|
123
|
+
const iid = this.requireInstanceId("read");
|
|
124
|
+
const timeoutMs = options?.timeout != null ? options.timeout * 1000 : this.timeoutMs;
|
|
125
|
+
return postJson(this.baseUrl, "/read", {
|
|
126
|
+
instance_id: iid,
|
|
127
|
+
query,
|
|
128
|
+
mode: "single-answer",
|
|
129
|
+
}, this.token, timeoutMs);
|
|
130
|
+
}
|
|
131
|
+
get instance_id() {
|
|
132
|
+
return this.instanceId;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Create an xmemory client.
|
|
137
|
+
*/
|
|
138
|
+
export async function xmemoryInstance(options = {}) {
|
|
139
|
+
return XmemoryClient.create(options);
|
|
140
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Types for the xmemory API.
|
|
3
|
+
*/
|
|
4
|
+
export declare const SchemaType: {
|
|
5
|
+
readonly YML: 0;
|
|
6
|
+
readonly JSON: 1;
|
|
7
|
+
};
|
|
8
|
+
export type SchemaTypeValue = (typeof SchemaType)[keyof typeof SchemaType];
|
|
9
|
+
export interface CreateInstanceYMLRequest {
|
|
10
|
+
yml_schema: string;
|
|
11
|
+
}
|
|
12
|
+
export interface CreateInstanceResponse {
|
|
13
|
+
status: "ok" | "error";
|
|
14
|
+
instance_id?: string | null;
|
|
15
|
+
error_message?: string | null;
|
|
16
|
+
}
|
|
17
|
+
export interface WriteRequest {
|
|
18
|
+
instance_id: string;
|
|
19
|
+
text: string;
|
|
20
|
+
extraction_logic?: "fast" | "regular" | "deep";
|
|
21
|
+
}
|
|
22
|
+
export interface WriteResponse {
|
|
23
|
+
status: "ok" | "error";
|
|
24
|
+
error_message?: string | null;
|
|
25
|
+
}
|
|
26
|
+
export interface ReadRequest {
|
|
27
|
+
instance_id: string;
|
|
28
|
+
query: string;
|
|
29
|
+
mode?: "single-answer" | "raw-tables" | "xresponse";
|
|
30
|
+
}
|
|
31
|
+
export interface ReaderResult {
|
|
32
|
+
answer?: string;
|
|
33
|
+
[key: string]: unknown;
|
|
34
|
+
}
|
|
35
|
+
export interface ReadResponse {
|
|
36
|
+
status: "ok" | "error";
|
|
37
|
+
reader_result?: ReaderResult | null;
|
|
38
|
+
error_message?: string | null;
|
|
39
|
+
}
|
package/dist/types.js
ADDED
package/package.json
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "xmemory",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "xmemory",
|
|
5
|
-
"keywords": [
|
|
5
|
+
"keywords": [
|
|
6
|
+
"xmemory"
|
|
7
|
+
],
|
|
6
8
|
"homepage": "https://github.com/xmemory-ai/xmemory-npm",
|
|
7
9
|
"bugs": {
|
|
8
10
|
"url": "https://github.com/xmemory-ai/xmemory-npm/issues"
|
|
@@ -13,12 +15,20 @@
|
|
|
13
15
|
},
|
|
14
16
|
"license": "MIT",
|
|
15
17
|
"author": "Dima Korolev",
|
|
16
|
-
"type": "
|
|
17
|
-
"main": "
|
|
18
|
-
"
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
"type": "module",
|
|
19
|
+
"main": "dist/client.js",
|
|
20
|
+
"types": "dist/client.d.ts",
|
|
21
|
+
"files": [
|
|
22
|
+
"dist"
|
|
23
|
+
],
|
|
21
24
|
"scripts": {
|
|
22
|
-
"
|
|
25
|
+
"build": "tsc",
|
|
26
|
+
"test": "tsc --noEmit && node --import tsx test.ts",
|
|
27
|
+
"prepublishOnly": "npm run build"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@types/node": "^25.4.0",
|
|
31
|
+
"tsx": "^4.21.0",
|
|
32
|
+
"typescript": "^5.6.0"
|
|
23
33
|
}
|
|
24
34
|
}
|
package/cli.js
DELETED
package/index.js
DELETED