@xdarkicex/openclaw-memory-libravdb 1.4.5 → 1.4.7

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.
Files changed (75) hide show
  1. package/HOOK.md +14 -0
  2. package/README.md +32 -2
  3. package/dist/cli.d.ts +39 -0
  4. package/dist/cli.js +208 -0
  5. package/dist/context-engine.d.ts +56 -0
  6. package/dist/context-engine.js +125 -0
  7. package/dist/dream-promotion.d.ts +47 -0
  8. package/dist/dream-promotion.js +363 -0
  9. package/dist/dream-routing.d.ts +6 -0
  10. package/dist/dream-routing.js +31 -0
  11. package/dist/durable-namespace.d.ts +6 -0
  12. package/dist/durable-namespace.js +24 -0
  13. package/dist/grpc-client.d.ts +23 -0
  14. package/dist/grpc-client.js +104 -0
  15. package/dist/index.d.ts +10 -0
  16. package/dist/index.js +40 -0
  17. package/dist/lifecycle-hooks.d.ts +4 -0
  18. package/dist/lifecycle-hooks.js +64 -0
  19. package/dist/markdown-hash.d.ts +3 -0
  20. package/dist/markdown-hash.js +82 -0
  21. package/dist/markdown-ingest.d.ts +43 -0
  22. package/dist/markdown-ingest.js +464 -0
  23. package/dist/memory-provider.d.ts +4 -0
  24. package/dist/memory-provider.js +13 -0
  25. package/dist/memory-runtime.d.ts +118 -0
  26. package/dist/memory-runtime.js +217 -0
  27. package/dist/plugin-runtime.d.ts +28 -0
  28. package/dist/plugin-runtime.js +127 -0
  29. package/dist/proto/intelligence_kernel/v1/kernel.proto +378 -0
  30. package/dist/recall-cache.d.ts +2 -0
  31. package/dist/recall-cache.js +30 -0
  32. package/dist/rpc-protobuf-codecs.d.ts +70 -0
  33. package/dist/rpc-protobuf-codecs.js +77 -0
  34. package/dist/rpc.d.ts +14 -0
  35. package/dist/rpc.js +121 -0
  36. package/dist/sidecar.d.ts +34 -0
  37. package/dist/sidecar.js +535 -0
  38. package/dist/types.d.ts +163 -0
  39. package/dist/types.js +1 -0
  40. package/docs/contributing.md +14 -13
  41. package/docs/install.md +7 -9
  42. package/docs/installation.md +23 -16
  43. package/docs/uninstall.md +1 -1
  44. package/index.js +2 -0
  45. package/openclaw.plugin.json +2 -2
  46. package/package.json +39 -16
  47. package/packaging/README.md +0 -71
  48. package/packaging/homebrew/libravdbd.rb.tmpl +0 -224
  49. package/packaging/launchd/com.xdarkicex.libravdbd.plist +0 -32
  50. package/packaging/systemd/libravdbd.service +0 -12
  51. package/src/cli.ts +0 -299
  52. package/src/comparison-experiments.ts +0 -128
  53. package/src/context-engine.ts +0 -1451
  54. package/src/continuity.ts +0 -93
  55. package/src/dream-promotion.ts +0 -492
  56. package/src/dream-routing.ts +0 -40
  57. package/src/durable-namespace.ts +0 -34
  58. package/src/index.ts +0 -47
  59. package/src/lifecycle-hooks.ts +0 -96
  60. package/src/markdown-hash.ts +0 -104
  61. package/src/markdown-ingest.ts +0 -627
  62. package/src/memory-provider.ts +0 -25
  63. package/src/memory-runtime.ts +0 -283
  64. package/src/openclaw-plugin-sdk.d.ts +0 -59
  65. package/src/plugin-runtime.ts +0 -116
  66. package/src/recall-cache.ts +0 -34
  67. package/src/recall-utils.ts +0 -131
  68. package/src/rpc.ts +0 -84
  69. package/src/scoring.ts +0 -632
  70. package/src/sidecar.ts +0 -486
  71. package/src/temporal.ts +0 -1010
  72. package/src/tokens.ts +0 -52
  73. package/src/types.ts +0 -277
  74. package/tsconfig.json +0 -20
  75. package/tsconfig.tests.json +0 -12
package/src/sidecar.ts DELETED
@@ -1,486 +0,0 @@
1
- import fs from "node:fs";
2
- import net from "node:net";
3
- import os from "node:os";
4
- import path from "node:path";
5
-
6
- import type { LoggerLike, PluginConfig, SidecarHandle, SidecarSocket } from "./types.js";
7
-
8
- type CloseHandler = () => void;
9
- type DataHandler = (chunk: string) => void;
10
- type ErrorHandler = (error: Error) => void;
11
-
12
- export interface SidecarRuntime {
13
- resolveEndpoint(cfg: PluginConfig): string | Promise<string>;
14
- createSocket(endpoint: string): SidecarSocket;
15
- scheduleRestart(delayMs: number, restart: () => void): void;
16
- }
17
-
18
- class PlaceholderSocket implements SidecarSocket {
19
- private readonly onData = new Set<DataHandler>();
20
- private readonly onClose = new Set<CloseHandler>();
21
- private readonly connectOnce = new Set<CloseHandler>();
22
- private readonly errorOnce = new Set<ErrorHandler>();
23
-
24
- constructor() {
25
- queueMicrotask(() => {
26
- for (const handler of this.connectOnce) {
27
- handler();
28
- }
29
- this.connectOnce.clear();
30
- });
31
- }
32
-
33
- setEncoding(_encoding: string): void {}
34
-
35
- on(event: "data" | "close", handler: DataHandler | CloseHandler): void {
36
- if (event === "data") {
37
- this.onData.add(handler as DataHandler);
38
- return;
39
- }
40
- this.onClose.add(handler as CloseHandler);
41
- }
42
-
43
- once(event: "connect" | "error", handler: CloseHandler | ErrorHandler): void {
44
- if (event === "connect") {
45
- this.connectOnce.add(handler as CloseHandler);
46
- return;
47
- }
48
- this.errorOnce.add(handler as ErrorHandler);
49
- }
50
-
51
- write(chunk: string): void {
52
- try {
53
- const msg = JSON.parse(chunk);
54
- const response = JSON.stringify({
55
- jsonrpc: "2.0",
56
- id: msg.id,
57
- result: msg.method === "health" ? { ok: true } : {},
58
- });
59
- for (const handler of this.onData) {
60
- handler(`${response}\n`);
61
- }
62
- } catch (error) {
63
- const err = error instanceof Error ? error : new Error(String(error));
64
- for (const handler of this.errorOnce) {
65
- handler(err);
66
- }
67
- this.errorOnce.clear();
68
- }
69
- }
70
-
71
- destroy(): void {
72
- for (const handler of this.onClose) {
73
- handler();
74
- }
75
- }
76
- }
77
-
78
- class SupervisorSocket implements SidecarSocket {
79
- private readonly onData = new Set<DataHandler>();
80
- private readonly onClose = new Set<CloseHandler>();
81
- private readonly connectOnce = new Set<CloseHandler>();
82
- private readonly errorOnce = new Set<ErrorHandler>();
83
- private current?: SidecarSocket;
84
- private encoding = "utf8";
85
- private generation = 0;
86
-
87
- bind(socket: SidecarSocket): void {
88
- this.current = socket;
89
- this.generation += 1;
90
- const generation = this.generation;
91
-
92
- socket.setEncoding(this.encoding);
93
- socket.on("data", (chunk) => {
94
- if (generation !== this.generation) {
95
- return;
96
- }
97
- for (const handler of this.onData) {
98
- handler(chunk);
99
- }
100
- });
101
- socket.on("close", () => {
102
- if (generation !== this.generation) {
103
- return;
104
- }
105
- for (const handler of this.onClose) {
106
- handler();
107
- }
108
- });
109
-
110
- for (const handler of this.connectOnce) {
111
- handler();
112
- }
113
- this.connectOnce.clear();
114
- }
115
-
116
- setEncoding(encoding: string): void {
117
- this.encoding = encoding;
118
- this.current?.setEncoding(encoding);
119
- }
120
-
121
- on(event: "data" | "close", handler: DataHandler | CloseHandler): void {
122
- if (event === "data") {
123
- this.onData.add(handler as DataHandler);
124
- return;
125
- }
126
- this.onClose.add(handler as CloseHandler);
127
- }
128
-
129
- once(event: "connect" | "error", handler: CloseHandler | ErrorHandler): void {
130
- if (event === "connect") {
131
- if (this.current) {
132
- (handler as CloseHandler)();
133
- return;
134
- }
135
- this.connectOnce.add(handler as CloseHandler);
136
- return;
137
- }
138
- this.errorOnce.add(handler as ErrorHandler);
139
- }
140
-
141
- write(chunk: string): void {
142
- if (!this.current) {
143
- throw new Error("Sidecar socket unavailable");
144
- }
145
- this.current.write(chunk);
146
- }
147
-
148
- destroy(): void {
149
- this.current?.destroy();
150
- }
151
- }
152
-
153
- class SidecarSupervisor implements SidecarHandle {
154
- private retries = 0;
155
- private degraded = false;
156
- private shuttingDown = false;
157
- public socket: SidecarSocket;
158
-
159
- constructor(
160
- private readonly cfg: PluginConfig,
161
- private readonly logger: LoggerLike,
162
- private readonly runtime: SidecarRuntime,
163
- ) {
164
- this.socket = new SupervisorSocket();
165
- }
166
-
167
- async start(): Promise<SidecarSocket> {
168
- const endpoint = await this.runtime.resolveEndpoint(this.cfg);
169
- const socket = await this.connectEndpoint(endpoint);
170
- if (this.socket instanceof SupervisorSocket) {
171
- this.socket.bind(socket);
172
- } else {
173
- this.socket = socket;
174
- }
175
- return socket;
176
- }
177
-
178
- isDegraded(): boolean {
179
- return this.degraded;
180
- }
181
-
182
- async shutdown(): Promise<void> {
183
- this.shuttingDown = true;
184
- this.socket.destroy();
185
- }
186
-
187
- private async connectEndpoint(endpoint: string): Promise<SidecarSocket> {
188
- const socket = this.runtime.createSocket(endpoint);
189
- socket.on("close", () => {
190
- void this.handleExit(1);
191
- });
192
-
193
- if (isTcpEndpoint(endpoint)) {
194
- this.logger.info?.(`[libravdb] using TCP endpoint ${endpoint}`);
195
- } else {
196
- this.logger.info?.(`[libravdb] using Unix socket ${endpoint}`);
197
- }
198
-
199
- return await new Promise<SidecarSocket>((resolve, reject) => {
200
- socket.once("connect", () => resolve(socket));
201
- socket.once("error", (error) => reject(formatConnectionError(endpoint, error)));
202
- });
203
- }
204
-
205
- private async handleExit(code: number | null): Promise<void> {
206
- if (this.shuttingDown) {
207
- return;
208
- }
209
- if (code === 0) {
210
- return;
211
- }
212
-
213
- const maxRetries = this.cfg.maxRetries ?? 3;
214
- if (this.retries >= maxRetries) {
215
- this.logger.error("[libravdb] sidecar retries exhausted; degraded mode");
216
- this.degraded = true;
217
- return;
218
- }
219
-
220
- const backoffMs = computeBackoffMs(this.retries);
221
- this.retries += 1;
222
- this.runtime.scheduleRestart(backoffMs, () => {
223
- void this.start().catch((error) => {
224
- const message = error instanceof Error ? error.message : String(error);
225
- this.logger.error(`[libravdb] sidecar reconnect failed: ${message}`);
226
- });
227
- });
228
- }
229
- }
230
-
231
- export async function startSidecar(
232
- cfg: PluginConfig,
233
- logger: LoggerLike = console,
234
- runtime: SidecarRuntime = createDefaultRuntime(),
235
- ): Promise<SidecarHandle> {
236
- const supervisor = new SidecarSupervisor(cfg, logger, runtime);
237
- await supervisor.start();
238
- return supervisor;
239
- }
240
-
241
- export function computeBackoffMs(retries: number): number {
242
- return Math.min(500 * Math.pow(2, retries), 16000);
243
- }
244
-
245
- export function isTcpEndpoint(endpoint: string): boolean {
246
- return endpoint.startsWith("tcp:");
247
- }
248
-
249
- export function resolveEndpoint(cfg: PluginConfig): string {
250
- const endpoint = resolveConfiguredEndpoint(cfg);
251
- return endpoint.replace(/^unix:/, "");
252
- }
253
-
254
- export function resolveConfiguredEndpoint(cfg: PluginConfig): string {
255
- const value = cfg.sidecarPath?.trim();
256
- if (!value || value === "auto") {
257
- return defaultEndpoint();
258
- }
259
- if (!isConfiguredEndpoint(value)) {
260
- throw new Error(
261
- `LibraVDB sidecarPath must be a daemon endpoint like unix:/path/to/libravdb.sock or tcp:127.0.0.1:37421. Executable paths are no longer supported.`,
262
- );
263
- }
264
- return value;
265
- }
266
-
267
- export function daemonProvisioningHint(): string {
268
- return "If you installed the npm package, install and start libravdbd separately; the package does not provision the daemon binary, ONNX Runtime, or model assets.";
269
- }
270
-
271
- export function defaultEndpoint(platform = process.platform, homeDir = os.homedir()): string {
272
- // Honour the daemon's own env var first (set by Homebrew LaunchAgent / systemd unit).
273
- const envEndpoint = process.env.LIBRAVDB_RPC_ENDPOINT?.trim();
274
- if (envEndpoint && isConfiguredEndpoint(envEndpoint)) {
275
- return envEndpoint;
276
- }
277
-
278
- if (platform === "win32") {
279
- return "tcp:127.0.0.1:37421";
280
- }
281
-
282
- const sockName = "libravdb.sock";
283
- const candidateDirs = [
284
- // User-local (npm plugin convention)
285
- homeDir?.trim() ? path.join(homeDir, ".clawdb", "run") : null,
286
- // Homebrew (Apple Silicon) — matches the Homebrew formula LaunchAgent
287
- "/opt/homebrew/var/clawdb/run",
288
- // Homebrew (Intel Mac) / manual Linux installs
289
- "/usr/local/var/clawdb/run",
290
- ].filter((d): d is string => d !== null);
291
-
292
- for (const dir of candidateDirs) {
293
- const sockPath = path.join(dir, sockName);
294
- try {
295
- if (fs.existsSync(sockPath)) {
296
- return `unix:${sockPath}`;
297
- }
298
- } catch {
299
- // Permission error or similar — skip this candidate.
300
- }
301
- }
302
-
303
- // Fallback to the original user-local path so error messages stay familiar.
304
- const baseDir = homeDir?.trim()
305
- ? path.join(homeDir, ".clawdb", "run")
306
- : path.join(".", ".clawdb", "run");
307
- return `unix:${path.join(baseDir, sockName)}`;
308
- }
309
-
310
- export function buildSidecarEnv(cfg: PluginConfig): Record<string, string> {
311
- const env: Record<string, string> = {};
312
-
313
- if (cfg.dbPath) {
314
- env.LIBRAVDB_DB_PATH = cfg.dbPath;
315
- }
316
- if (cfg.embeddingRuntimePath) {
317
- env.LIBRAVDB_ONNX_RUNTIME = cfg.embeddingRuntimePath;
318
- }
319
- if (cfg.embeddingBackend) {
320
- env.LIBRAVDB_EMBEDDING_BACKEND = cfg.embeddingBackend;
321
- }
322
- if (cfg.embeddingProfile) {
323
- env.LIBRAVDB_EMBEDDING_PROFILE = cfg.embeddingProfile;
324
- }
325
- if (cfg.fallbackProfile) {
326
- env.LIBRAVDB_FALLBACK_PROFILE = cfg.fallbackProfile;
327
- }
328
- if (cfg.embeddingModelPath) {
329
- env.LIBRAVDB_EMBEDDING_MODEL = cfg.embeddingModelPath;
330
- }
331
- if (cfg.embeddingTokenizerPath) {
332
- env.LIBRAVDB_EMBEDDING_TOKENIZER = cfg.embeddingTokenizerPath;
333
- }
334
- if (typeof cfg.embeddingDimensions === "number" && cfg.embeddingDimensions > 0) {
335
- env.LIBRAVDB_EMBEDDING_DIMENSIONS = String(cfg.embeddingDimensions);
336
- }
337
- if (typeof cfg.embeddingNormalize === "boolean") {
338
- env.LIBRAVDB_EMBEDDING_NORMALIZE = String(cfg.embeddingNormalize);
339
- }
340
- if (cfg.summarizerBackend) {
341
- env.LIBRAVDB_SUMMARIZER_BACKEND = cfg.summarizerBackend;
342
- }
343
- if (cfg.summarizerProfile) {
344
- env.LIBRAVDB_SUMMARIZER_PROFILE = cfg.summarizerProfile;
345
- }
346
- if (cfg.summarizerRuntimePath) {
347
- env.LIBRAVDB_SUMMARIZER_RUNTIME = cfg.summarizerRuntimePath;
348
- }
349
- if (cfg.summarizerModelPath) {
350
- env.LIBRAVDB_SUMMARIZER_MODEL_PATH = cfg.summarizerModelPath;
351
- }
352
- if (cfg.summarizerTokenizerPath) {
353
- env.LIBRAVDB_SUMMARIZER_TOKENIZER = cfg.summarizerTokenizerPath;
354
- }
355
- if (cfg.summarizerModel) {
356
- env.LIBRAVDB_SUMMARIZER_MODEL = cfg.summarizerModel;
357
- }
358
- if (cfg.summarizerEndpoint) {
359
- env.LIBRAVDB_SUMMARIZER_ENDPOINT = cfg.summarizerEndpoint;
360
- }
361
- if (cfg.ollamaUrl && !env.LIBRAVDB_SUMMARIZER_ENDPOINT) {
362
- env.LIBRAVDB_SUMMARIZER_ENDPOINT = cfg.ollamaUrl;
363
- }
364
- if (cfg.compactModel && !env.LIBRAVDB_SUMMARIZER_MODEL) {
365
- env.LIBRAVDB_SUMMARIZER_MODEL = cfg.compactModel;
366
- }
367
- if (cfg.gatingWeights?.w1c != null) {
368
- env.LIBRAVDB_GATING_W1C = String(cfg.gatingWeights.w1c);
369
- }
370
- if (cfg.gatingWeights?.w2c != null) {
371
- env.LIBRAVDB_GATING_W2C = String(cfg.gatingWeights.w2c);
372
- }
373
- if (cfg.gatingWeights?.w3c != null) {
374
- env.LIBRAVDB_GATING_W3C = String(cfg.gatingWeights.w3c);
375
- }
376
- if (cfg.gatingWeights?.w1t != null) {
377
- env.LIBRAVDB_GATING_W1T = String(cfg.gatingWeights.w1t);
378
- }
379
- if (cfg.gatingWeights?.w2t != null) {
380
- env.LIBRAVDB_GATING_W2T = String(cfg.gatingWeights.w2t);
381
- }
382
- if (cfg.gatingWeights?.w3t != null) {
383
- env.LIBRAVDB_GATING_W3T = String(cfg.gatingWeights.w3t);
384
- }
385
- if (typeof cfg.gatingTechNorm === "number" && cfg.gatingTechNorm > 0) {
386
- env.LIBRAVDB_GATING_TECH_NORM = String(cfg.gatingTechNorm);
387
- }
388
- if (typeof cfg.ingestionGateThreshold === "number" && cfg.ingestionGateThreshold >= 0) {
389
- env.LIBRAVDB_GATING_THRESHOLD = String(cfg.ingestionGateThreshold);
390
- }
391
- if (typeof cfg.gatingCentroidK === "number" && cfg.gatingCentroidK > 0) {
392
- env.LIBRAVDB_GATING_CENTROID_K = String(cfg.gatingCentroidK);
393
- }
394
- if (typeof cfg.lifecycleJournalMaxEntries === "number" && cfg.lifecycleJournalMaxEntries > 0) {
395
- env.LIBRAVDB_LIFECYCLE_JOURNAL_MAX_ENTRIES = String(cfg.lifecycleJournalMaxEntries);
396
- }
397
-
398
- return env;
399
- }
400
-
401
- function createDefaultRuntime(): SidecarRuntime {
402
- return {
403
- resolveEndpoint(cfg) {
404
- return resolveEndpoint(cfg);
405
- },
406
- createSocket(endpoint) {
407
- if (isTcpEndpoint(endpoint)) {
408
- const address = endpoint.slice("tcp:".length);
409
- const separator = address.lastIndexOf(":");
410
- if (separator <= 0) {
411
- throw new Error(`Invalid TCP sidecar endpoint: ${endpoint}`);
412
- }
413
- return net.connect({
414
- host: address.slice(0, separator),
415
- port: Number(address.slice(separator + 1)),
416
- }) as unknown as SidecarSocket;
417
- }
418
- return net.connect(endpoint) as unknown as SidecarSocket;
419
- },
420
- scheduleRestart(delayMs, restart) {
421
- setTimeout(restart, delayMs);
422
- },
423
- };
424
- }
425
-
426
- function formatConnectionError(endpoint: string, error: Error): Error {
427
- const code = typeof (error as NodeJS.ErrnoException).code === "string"
428
- ? (error as NodeJS.ErrnoException).code
429
- : "";
430
- if (code === "ENOENT" || code === "ECONNREFUSED") {
431
- return new Error(
432
- `LibraVDB daemon unavailable at ${describeEndpoint(endpoint)}. ${daemonProvisioningHint()} Or set sidecarPath to a running daemon endpoint.`,
433
- );
434
- }
435
- return error;
436
- }
437
-
438
- function describeEndpoint(endpoint: string): string {
439
- if (isTcpEndpoint(endpoint)) {
440
- return endpoint;
441
- }
442
- return `unix:${endpoint}`;
443
- }
444
-
445
- function isConfiguredEndpoint(value?: string): boolean {
446
- return value?.startsWith("tcp:") === true || value?.startsWith("unix:") === true;
447
- }
448
-
449
- export { PlaceholderSocket };
450
-
451
- export async function probeSidecarEndpoint(cfg: PluginConfig): Promise<string | null> {
452
- const endpoint = resolveConfiguredEndpoint(cfg);
453
- try {
454
- await new Promise<void>((resolve, reject) => {
455
- if (isTcpEndpoint(endpoint)) {
456
- const address = endpoint.slice("tcp:".length);
457
- const separator = address.lastIndexOf(":");
458
- if (separator <= 0) {
459
- reject(new Error("invalid tcp endpoint"));
460
- return;
461
- }
462
- const host = address.slice(0, separator);
463
- const port = Number(address.slice(separator + 1));
464
- const socket = net.connect({ host, port }, () => {
465
- socket.destroy();
466
- resolve();
467
- });
468
- socket.setTimeout(500);
469
- socket.on("error", reject);
470
- socket.on("timeout", reject);
471
- } else {
472
- const socketPath = endpoint.replace(/^unix:/, "");
473
- const socket = net.connect(socketPath, () => {
474
- socket.destroy();
475
- resolve();
476
- });
477
- socket.setTimeout(500);
478
- socket.on("error", reject);
479
- socket.on("timeout", reject);
480
- }
481
- });
482
- return endpoint;
483
- } catch {
484
- return null;
485
- }
486
- }