@lumi0/sdk 0.0.2

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,147 @@
1
+ # Lumi0 TypeScript SDK
2
+
3
+ The official TypeScript SDK for storing, retrieving, and compressing persistent
4
+ AI memory with Lumi0.
5
+
6
+ ## Installation
7
+
8
+ ```bash
9
+ bun add @lumi0/sdk
10
+ ```
11
+
12
+ ```bash
13
+ npm install @lumi0/sdk
14
+ ```
15
+
16
+ ## Quick start
17
+
18
+ Create a client with a Lumi0 API key:
19
+
20
+ ```ts
21
+ import { Lumi0 } from "@lumi0/sdk";
22
+
23
+ const client = new Lumi0({
24
+ apiKey: process.env.LUMI0_API_KEY!,
25
+ });
26
+ ```
27
+
28
+ Store a memory, then search for relevant context:
29
+
30
+ ```ts
31
+ await client.store({
32
+ id: "user_123",
33
+ content: "The user prefers concise TypeScript examples.",
34
+ });
35
+
36
+ const memories = await client.search({
37
+ id: "user_123",
38
+ query: "What coding examples does the user prefer?",
39
+ limit: 5,
40
+ });
41
+ ```
42
+
43
+ ## API
44
+
45
+ ### `store`
46
+
47
+ Stores one memory for an identifier.
48
+
49
+ ```ts
50
+ await client.store({
51
+ id: "user_123",
52
+ content: "The user uses Bun and TypeScript.",
53
+ dedupeThreshold: 0.9,
54
+ });
55
+ ```
56
+
57
+ ### `batch`
58
+
59
+ Stores multiple memories in one request.
60
+
61
+ ```ts
62
+ await client.batch([
63
+ {
64
+ id: "user_123",
65
+ content: "The user works with Hono.",
66
+ },
67
+ {
68
+ id: "user_123",
69
+ content: "The user prefers practical implementation details.",
70
+ },
71
+ ]);
72
+ ```
73
+
74
+ ### `search`
75
+
76
+ Searches memories semantically.
77
+
78
+ ```ts
79
+ const memories = await client.search({
80
+ id: "user_123",
81
+ query: "Which backend framework does the user use?",
82
+ limit: 10,
83
+ minSimilarity: 0.7,
84
+ memoryType: "semantic",
85
+ });
86
+ ```
87
+
88
+ ### `get`
89
+
90
+ Retrieves a memory by identifier. Pass `version` to retrieve a specific version.
91
+
92
+ ```ts
93
+ const memory = await client.get({
94
+ id: "memory_123",
95
+ version: 2,
96
+ });
97
+ ```
98
+
99
+ ### `forget`
100
+
101
+ Deletes a memory for a user.
102
+
103
+ ```ts
104
+ await client.forget({
105
+ id: "memory_123",
106
+ userId: "user_123",
107
+ });
108
+ ```
109
+
110
+ ### `compress`
111
+
112
+ Reduces context while retaining information relevant to a query.
113
+
114
+ ```ts
115
+ const result = await client.compress({
116
+ content: JSON.stringify(memories),
117
+ query: "What is the user's preferred backend stack?",
118
+ mode: "adaptive",
119
+ budgetRatio: 0.5,
120
+ });
121
+
122
+ console.log(result.data.compressedText);
123
+ console.log(`${result.data.tokensSaved} tokens saved`);
124
+ ```
125
+
126
+ Use `"fixed"` mode with `budgetRatio` to target a specific retained-context
127
+ ratio. Use `"adaptive"` mode to let Lumi0 determine the appropriate amount of
128
+ context to retain for the query.
129
+
130
+ ## Configuration
131
+
132
+ ```ts
133
+ const client = new Lumi0({
134
+ apiKey: process.env.LUMI0_API_KEY!,
135
+ baseUrl: "https://api.lumi0.com/api/v1",
136
+ timeout: 30_000,
137
+ });
138
+ ```
139
+
140
+ `baseUrl` defaults to `https://api.lumi0.com/api/v1` and `timeout` defaults to
141
+ 30 seconds.
142
+
143
+ ## Errors
144
+
145
+ SDK requests reject with an `Error` when Lumi0 returns a non-success status.
146
+ The error message includes the HTTP status and the API-provided error message
147
+ when available.
@@ -0,0 +1,63 @@
1
+ //#region ../../../apps/api/src/shared/utils/types.d.ts
2
+ interface ForgetMemoryInput {
3
+ userId: string;
4
+ id: string;
5
+ }
6
+ //#endregion
7
+ //#region src/types.d.ts
8
+ type Lumi0Options = {
9
+ apiKey: string;
10
+ baseUrl?: string;
11
+ timeout?: number;
12
+ };
13
+ //#endregion
14
+ //#region src/client.d.ts
15
+ declare class Lumi0 {
16
+ private readonly http;
17
+ constructor(option: Lumi0Options);
18
+ store(input: StoreMemoryInput): Promise<unknown>;
19
+ search(input: SearchMemoryInput): Promise<unknown>;
20
+ get(input: GetMemoryInput): Promise<unknown>;
21
+ forget(input: ForgetMemoryInput): Promise<unknown>;
22
+ batch(input: StoreMemoryInput[]): Promise<unknown>;
23
+ compress(input: CompressMemoryInput): Promise<CompressMemoryApiResponse>;
24
+ }
25
+ interface StoreMemoryInput {
26
+ content: string;
27
+ dedupeThreshold?: number;
28
+ id: string;
29
+ }
30
+ interface SearchMemoryInput {
31
+ query: string;
32
+ limit?: number;
33
+ minSimilarity?: number;
34
+ memoryType?: string;
35
+ id: string;
36
+ }
37
+ interface GetMemoryInput {
38
+ id: string;
39
+ version?: number;
40
+ }
41
+ interface CompressMemoryInput {
42
+ content: string;
43
+ query?: string;
44
+ budgetRatio?: number;
45
+ mode: "fixed" | "adaptive";
46
+ }
47
+ interface CompressMemoryResult {
48
+ compressedText: string;
49
+ originalTokens: number;
50
+ keptTokens: number;
51
+ tokensSaved: number;
52
+ tokensSavedPct: number;
53
+ keepRatio: number;
54
+ mode: "fixed" | "adaptive";
55
+ policyName: string;
56
+ keptLineRatio: number;
57
+ }
58
+ interface CompressMemoryApiResponse {
59
+ success: boolean;
60
+ data: CompressMemoryResult;
61
+ }
62
+ //#endregion
63
+ export { CompressMemoryApiResponse, CompressMemoryInput, CompressMemoryResult, GetMemoryInput, Lumi0, SearchMemoryInput, StoreMemoryInput };
package/dist/index.mjs ADDED
@@ -0,0 +1,112 @@
1
+ //#region src/http.ts
2
+ var HttpClient = class {
3
+ options;
4
+ constructor(options) {
5
+ this.options = options;
6
+ }
7
+ async request(path, init = {}) {
8
+ const controller = new AbortController();
9
+ const timeout = setTimeout(() => controller.abort(), this.options.timeout ?? 3e4);
10
+ try {
11
+ const response = await fetch(`${this.options.baseUrl}${path}`, {
12
+ ...init,
13
+ signal: controller.signal,
14
+ headers: {
15
+ Authorization: `Bearer ${this.options.apiKey}`,
16
+ "Content-Type": "application/json",
17
+ Accept: "application/json",
18
+ "User-Agent": "lumi0-sdk/0.1.0",
19
+ ...this.options.headers,
20
+ ...init.headers
21
+ }
22
+ });
23
+ if (!response.ok) throw await this.parseError(response);
24
+ if (response.status === 204) return;
25
+ return await response.json();
26
+ } finally {
27
+ clearTimeout(timeout);
28
+ }
29
+ }
30
+ get(path) {
31
+ return this.request(path);
32
+ }
33
+ post(path, body) {
34
+ return this.request(path, {
35
+ method: "POST",
36
+ body: body ? JSON.stringify(body) : void 0
37
+ });
38
+ }
39
+ list(path, items) {
40
+ return this.request(path, {
41
+ method: "POST",
42
+ body: JSON.stringify(items)
43
+ });
44
+ }
45
+ put(path, body) {
46
+ return this.request(path, {
47
+ method: "PUT",
48
+ body: body ? JSON.stringify(body) : void 0
49
+ });
50
+ }
51
+ patch(path, body) {
52
+ return this.request(path, {
53
+ method: "PATCH",
54
+ body: body ? JSON.stringify(body) : void 0
55
+ });
56
+ }
57
+ delete(path, query) {
58
+ const params = new URLSearchParams();
59
+ if (query) {
60
+ for (const [key, value] of Object.entries(query)) if (value !== void 0) params.append(key, String(value));
61
+ }
62
+ const url = params.toString() ? `${path}?${params}` : path;
63
+ return this.request(url, { method: "DELETE" });
64
+ }
65
+ async parseError(response) {
66
+ let message = response.statusText;
67
+ try {
68
+ const json = await response.json();
69
+ message = json.message ?? json.error ?? response.statusText;
70
+ } catch {}
71
+ return /* @__PURE__ */ new Error(`${response.status} ${message}`);
72
+ }
73
+ };
74
+ //#endregion
75
+ //#region src/client.ts
76
+ var Lumi0 = class {
77
+ http;
78
+ constructor(option) {
79
+ const apiKey = option.apiKey;
80
+ this.http = new HttpClient({
81
+ baseUrl: option.baseUrl ?? "https://api.lumi0.com/api/v1",
82
+ apiKey,
83
+ timeout: option.timeout ?? 3e4
84
+ });
85
+ }
86
+ store(input) {
87
+ return this.http.post("/memory", {
88
+ ...input,
89
+ externalId: input.id
90
+ });
91
+ }
92
+ search(input) {
93
+ return this.http.post("/memory/search", {
94
+ ...input,
95
+ externalId: input.id
96
+ });
97
+ }
98
+ get(input) {
99
+ return this.http.post("/memory/get", input);
100
+ }
101
+ forget(input) {
102
+ return this.http.delete(`/memory/${input.id}`, { userId: input.userId });
103
+ }
104
+ batch(input) {
105
+ return this.http.post("/memory/batch", input.map((item) => ({ ...item })));
106
+ }
107
+ compress(input) {
108
+ return this.http.post("/compress", input);
109
+ }
110
+ };
111
+ //#endregion
112
+ export { Lumi0 };
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "@lumi0/sdk",
3
+ "version": "0.0.2",
4
+ "description": "Official TypeScript SDK for Lumi0",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/lumi0ai/lumi0-ts.git"
10
+ },
11
+ "homepage": "https://lumi0.com",
12
+ "keywords": [
13
+ "lumi0",
14
+ "ai",
15
+ "memory",
16
+ "ai-memory",
17
+ "llm",
18
+ "typescript",
19
+ "sdk"
20
+ ],
21
+ "files": [
22
+ "dist",
23
+ "README.md"
24
+ ],
25
+ "exports": {
26
+ ".": {
27
+ "types": "./dist/index.d.mts",
28
+ "import": "./dist/index.mjs"
29
+ }
30
+ },
31
+ "scripts": {
32
+ "build": "tsdown index.ts",
33
+ "dev": "bun --watch index.ts",
34
+ "typecheck": "tsc --noEmit",
35
+ "prepublishOnly": "bun run typecheck && bun run build"
36
+ },
37
+ "devDependencies": {
38
+ "@types/bun": "latest",
39
+ "tsdown": "^0.22.14",
40
+ "typescript": "^5"
41
+ },
42
+ "publishConfig": {
43
+ "access": "public"
44
+ }
45
+ }