memory-crystal 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/.env.example +20 -0
- package/CHANGELOG.md +6 -0
- package/LETTERS.md +22 -0
- package/LICENSE +21 -0
- package/README-ENTERPRISE.md +162 -0
- package/README-old.md +275 -0
- package/README.md +91 -0
- package/RELAY.md +88 -0
- package/TECHNICAL.md +379 -0
- package/ai/dev-updates/2026-02-25--cc-air--phase2-architecture-pivot.md +70 -0
- package/ai/dev-updates/2026-02-25--cc-air--phase2-worker-build.md +72 -0
- package/ai/dev-updates/2026-02-26--10-25-16--cc-mini--phase2-implementation.md +49 -0
- package/ai/dev-updates/2026-02-27--20-30-00--cc-mini--readme-overhaul-and-public-deploy.md +69 -0
- package/ai/notes/2026-02-26--cc-air--notes.md +412 -0
- package/ai/notes/2026-02-27--cc-mini--grok-feedback.md +44 -0
- package/ai/notes/2026-02-27--cc-mini--lesa-feedback.md +45 -0
- package/ai/notes/RESEARCH.md +1185 -0
- package/ai/notes/salience-research/README.md +29 -0
- package/ai/notes/salience-research/eurosla-salience-review.md +64 -0
- package/ai/notes/salience-research/full-research-summary.md +269 -0
- package/ai/notes/salience-research/salience-levels-diagram.png +0 -0
- package/ai/plan/2026-02-27--cc-mini--qr-pairing-spec.md +203 -0
- package/ai/plan/_archive/PLAN.md +194 -0
- package/ai/plan/_archive/PRD.md +1014 -0
- package/ai/plan/cc-plans-duplicates-from-dot-claude/2026-02-26--cc-mini--phase2-implementation-plan.md +245 -0
- package/ai/plan/dev-conventions-note.md +70 -0
- package/ai/plan/ldm-os-install-and-boot-architecture.md +285 -0
- package/ai/plan/memory-crystal-phase2-plan.md +192 -0
- package/ai/plan/memory-system-lay-of-the-land.md +214 -0
- package/ai/plan/phase2-ephemeral-relay.md +238 -0
- package/ai/plan/readme-first.md +68 -0
- package/ai/plan/roadmap.md +159 -0
- package/ai/todos/PUNCHLIST.md +44 -0
- package/ai/todos/README.md +31 -0
- package/ai/todos/inboxes/cc-air/2026-02-26--cc-air--post-relay-todos.md +85 -0
- package/ai/todos/inboxes/cc-mini/2026-02-26--cc-mini--phase2-status.md +100 -0
- package/ai/todos/inboxes/cc-mini/_archive/TODO.md +25 -0
- package/ai/todos/inboxes/parker/2026-02-25--cc-air--setup-checklist.md +139 -0
- package/ai/todos/inboxes/parker/2026-02-26--cc-mini--phase2-your-moves.md +72 -0
- package/dist/cc-hook.d.ts +1 -0
- package/dist/cc-hook.js +349 -0
- package/dist/chunk-3VFIJYS4.js +818 -0
- package/dist/chunk-52QE3YI3.js +1169 -0
- package/dist/chunk-AA3OPP4Z.js +432 -0
- package/dist/chunk-D3I3ZSE2.js +411 -0
- package/dist/chunk-EKSACBTJ.js +1070 -0
- package/dist/chunk-F3Y7EL7K.js +83 -0
- package/dist/chunk-JWZXYVET.js +1068 -0
- package/dist/chunk-KYVWO6ZM.js +1069 -0
- package/dist/chunk-L3VHARQH.js +413 -0
- package/dist/chunk-LOVAHSQV.js +411 -0
- package/dist/chunk-LQOYCAGG.js +446 -0
- package/dist/chunk-MK42FMEG.js +147 -0
- package/dist/chunk-NIJCVN3O.js +147 -0
- package/dist/chunk-O2UITJGH.js +465 -0
- package/dist/chunk-PEK6JH65.js +432 -0
- package/dist/chunk-PJ6FFKEX.js +77 -0
- package/dist/chunk-PLUBBZYR.js +800 -0
- package/dist/chunk-SGL6ISBJ.js +1061 -0
- package/dist/chunk-UNHVZB5G.js +411 -0
- package/dist/chunk-VAFTWSTE.js +1061 -0
- package/dist/chunk-XZ3S56RQ.js +1061 -0
- package/dist/chunk-Y72C7F6O.js +148 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +325 -0
- package/dist/core.d.ts +188 -0
- package/dist/core.js +12 -0
- package/dist/crypto.d.ts +16 -0
- package/dist/crypto.js +18 -0
- package/dist/dev-update-SZ2Z4WCQ.js +6 -0
- package/dist/ldm.d.ts +17 -0
- package/dist/ldm.js +12 -0
- package/dist/mcp-server.d.ts +1 -0
- package/dist/mcp-server.js +250 -0
- package/dist/migrate.d.ts +1 -0
- package/dist/migrate.js +89 -0
- package/dist/mirror-sync.d.ts +1 -0
- package/dist/mirror-sync.js +130 -0
- package/dist/openclaw.d.ts +5 -0
- package/dist/openclaw.js +349 -0
- package/dist/poller.d.ts +1 -0
- package/dist/poller.js +272 -0
- package/dist/summarize.d.ts +19 -0
- package/dist/summarize.js +10 -0
- package/dist/worker.js +137 -0
- package/openclaw.plugin.json +11 -0
- package/package.json +40 -0
- package/scripts/migrate-lance-to-sqlite.mjs +217 -0
- package/skills/memory/SKILL.md +61 -0
- package/src/cc-hook.ts +447 -0
- package/src/cli.ts +356 -0
- package/src/core.ts +1472 -0
- package/src/crypto.ts +113 -0
- package/src/dev-update.ts +178 -0
- package/src/ldm.ts +117 -0
- package/src/mcp-server.ts +274 -0
- package/src/migrate.ts +104 -0
- package/src/mirror-sync.ts +175 -0
- package/src/openclaw.ts +250 -0
- package/src/poller.ts +345 -0
- package/src/summarize.ts +210 -0
- package/src/worker.ts +208 -0
- package/tsconfig.json +18 -0
- package/wrangler.toml +20 -0
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
// src/dev-update.ts
|
|
2
|
+
import { execSync } from "child_process";
|
|
3
|
+
import { existsSync, mkdirSync, writeFileSync, readFileSync } from "fs";
|
|
4
|
+
import { join, basename } from "path";
|
|
5
|
+
var HOME = process.env.HOME || "/Users/lesa";
|
|
6
|
+
var STAFF_DIR = join(HOME, "Documents", "wipcomputer--mac-mini-01", "staff");
|
|
7
|
+
var CC_REPOS = join(STAFF_DIR, "Parker", "Claude Code - Mini", "repos");
|
|
8
|
+
var LESA_REPOS = join(STAFF_DIR, "L\u0113sa", "repos");
|
|
9
|
+
var DEV_UPDATES_DIR = join(CC_REPOS, "wip-dev-updates");
|
|
10
|
+
var LAST_RUN_PATH = join(HOME, ".openclaw", "memory", "dev-update-last-run.json");
|
|
11
|
+
function loadLastRun() {
|
|
12
|
+
try {
|
|
13
|
+
if (existsSync(LAST_RUN_PATH)) {
|
|
14
|
+
return JSON.parse(readFileSync(LAST_RUN_PATH, "utf-8"));
|
|
15
|
+
}
|
|
16
|
+
} catch {
|
|
17
|
+
}
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
function saveLastRun(run) {
|
|
21
|
+
const dir = join(HOME, ".openclaw", "memory");
|
|
22
|
+
if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
|
|
23
|
+
writeFileSync(LAST_RUN_PATH, JSON.stringify(run, null, 2));
|
|
24
|
+
}
|
|
25
|
+
function git(repoPath, cmd) {
|
|
26
|
+
try {
|
|
27
|
+
return execSync(`git -C "${repoPath}" ${cmd}`, {
|
|
28
|
+
encoding: "utf-8",
|
|
29
|
+
timeout: 1e4,
|
|
30
|
+
stdio: ["pipe", "pipe", "pipe"]
|
|
31
|
+
}).trim();
|
|
32
|
+
} catch {
|
|
33
|
+
return "";
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
function scanRepo(repoPath, since) {
|
|
37
|
+
if (!existsSync(join(repoPath, ".git"))) return null;
|
|
38
|
+
const name = basename(repoPath);
|
|
39
|
+
if (name === "_third-party-repos" || name === "wip-dev-updates") return null;
|
|
40
|
+
const recentCommits = git(repoPath, `log --oneline --since="${since}"`);
|
|
41
|
+
const uncommitted = git(repoPath, "status --porcelain");
|
|
42
|
+
if (!recentCommits && !uncommitted) return null;
|
|
43
|
+
const lines = [];
|
|
44
|
+
lines.push(`# ${name}`);
|
|
45
|
+
lines.push("");
|
|
46
|
+
if (recentCommits) {
|
|
47
|
+
lines.push("## Recent Commits");
|
|
48
|
+
lines.push("");
|
|
49
|
+
lines.push("```");
|
|
50
|
+
lines.push(...recentCommits.split("\n").slice(0, 10));
|
|
51
|
+
lines.push("```");
|
|
52
|
+
lines.push("");
|
|
53
|
+
}
|
|
54
|
+
if (uncommitted) {
|
|
55
|
+
lines.push("## Uncommitted Changes");
|
|
56
|
+
lines.push("");
|
|
57
|
+
lines.push("```");
|
|
58
|
+
lines.push(...uncommitted.split("\n").slice(0, 20));
|
|
59
|
+
lines.push("```");
|
|
60
|
+
lines.push("");
|
|
61
|
+
}
|
|
62
|
+
if (recentCommits) {
|
|
63
|
+
const diffStat = git(repoPath, `diff --stat "HEAD@{${since}}" HEAD`);
|
|
64
|
+
if (diffStat) {
|
|
65
|
+
lines.push("## Files Changed");
|
|
66
|
+
lines.push("");
|
|
67
|
+
lines.push("```");
|
|
68
|
+
lines.push(...diffStat.split("\n").slice(-15));
|
|
69
|
+
lines.push("```");
|
|
70
|
+
lines.push("");
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
const branch = git(repoPath, "branch --show-current") || "unknown";
|
|
74
|
+
lines.push(`**Branch:** ${branch}`);
|
|
75
|
+
lines.push("");
|
|
76
|
+
return lines.join("\n");
|
|
77
|
+
}
|
|
78
|
+
function runDevUpdate(author) {
|
|
79
|
+
const lastRun = loadLastRun();
|
|
80
|
+
if (lastRun) {
|
|
81
|
+
const elapsed = Date.now() - new Date(lastRun.timestamp).getTime();
|
|
82
|
+
if (elapsed < 60 * 60 * 1e3) {
|
|
83
|
+
return { reposUpdated: 0, files: [] };
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
let since = "6 hours ago";
|
|
87
|
+
if (lastRun?.timestamp) {
|
|
88
|
+
const lastDate = new Date(lastRun.timestamp);
|
|
89
|
+
const hoursAgo = Math.ceil((Date.now() - lastDate.getTime()) / (1e3 * 60 * 60));
|
|
90
|
+
since = `${Math.max(hoursAgo, 1)} hours ago`;
|
|
91
|
+
}
|
|
92
|
+
const now = /* @__PURE__ */ new Date();
|
|
93
|
+
const ts = [
|
|
94
|
+
String(now.getMonth() + 1).padStart(2, "0"),
|
|
95
|
+
String(now.getDate()).padStart(2, "0"),
|
|
96
|
+
String(now.getFullYear())
|
|
97
|
+
].join("-") + "--" + [
|
|
98
|
+
String(now.getHours()).padStart(2, "0"),
|
|
99
|
+
String(now.getMinutes()).padStart(2, "0"),
|
|
100
|
+
String(now.getSeconds()).padStart(2, "0")
|
|
101
|
+
].join("-");
|
|
102
|
+
const files = [];
|
|
103
|
+
const repoDirs = [CC_REPOS, LESA_REPOS];
|
|
104
|
+
for (const parentDir of repoDirs) {
|
|
105
|
+
if (!existsSync(parentDir)) continue;
|
|
106
|
+
let entries;
|
|
107
|
+
try {
|
|
108
|
+
entries = execSync(`ls "${parentDir}"`, { encoding: "utf-8" }).trim().split("\n");
|
|
109
|
+
} catch {
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
for (const entry of entries) {
|
|
113
|
+
const repoPath = join(parentDir, entry);
|
|
114
|
+
const content = scanRepo(repoPath, since);
|
|
115
|
+
if (!content) continue;
|
|
116
|
+
const repoName = basename(repoPath);
|
|
117
|
+
const outDir = join(DEV_UPDATES_DIR, repoName);
|
|
118
|
+
const outFile = join(outDir, `${author}-dev-update-${ts}.md`);
|
|
119
|
+
mkdirSync(outDir, { recursive: true });
|
|
120
|
+
const header = `*Auto-generated dev update by ${author} at ${now.toISOString().slice(0, 16).replace("T", " ")}*
|
|
121
|
+
|
|
122
|
+
`;
|
|
123
|
+
writeFileSync(outFile, content.replace(/^# .+\n/, `$&
|
|
124
|
+
${header}`));
|
|
125
|
+
files.push(`${repoName}/${author}-dev-update-${ts}.md`);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
if (files.length > 0 && existsSync(join(DEV_UPDATES_DIR, ".git"))) {
|
|
129
|
+
try {
|
|
130
|
+
execSync(
|
|
131
|
+
`cd "${DEV_UPDATES_DIR}" && git add -A && git commit -m "${author} auto-dev-update ${ts}: ${files.length} repo(s)" --no-verify && git push --quiet`,
|
|
132
|
+
{ encoding: "utf-8", timeout: 3e4, stdio: "pipe" }
|
|
133
|
+
);
|
|
134
|
+
} catch {
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
saveLastRun({
|
|
138
|
+
timestamp: now.toISOString(),
|
|
139
|
+
author,
|
|
140
|
+
reposUpdated: files.length
|
|
141
|
+
});
|
|
142
|
+
return { reposUpdated: files.length, files };
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export {
|
|
146
|
+
runDevUpdate
|
|
147
|
+
};
|
|
@@ -0,0 +1,465 @@
|
|
|
1
|
+
// src/core.ts
|
|
2
|
+
import * as lancedb from "@lancedb/lancedb";
|
|
3
|
+
import Database from "better-sqlite3";
|
|
4
|
+
import { readFileSync, existsSync, mkdirSync } from "fs";
|
|
5
|
+
import { execSync } from "child_process";
|
|
6
|
+
import { join } from "path";
|
|
7
|
+
import http from "http";
|
|
8
|
+
import https from "https";
|
|
9
|
+
async function embedOpenAI(texts, apiKey, model) {
|
|
10
|
+
return new Promise((resolve, reject) => {
|
|
11
|
+
const body = JSON.stringify({ input: texts, model });
|
|
12
|
+
const req = https.request({
|
|
13
|
+
hostname: "api.openai.com",
|
|
14
|
+
path: "/v1/embeddings",
|
|
15
|
+
method: "POST",
|
|
16
|
+
headers: {
|
|
17
|
+
"Content-Type": "application/json",
|
|
18
|
+
"Authorization": `Bearer ${apiKey}`,
|
|
19
|
+
"Content-Length": Buffer.byteLength(body)
|
|
20
|
+
},
|
|
21
|
+
timeout: 3e4
|
|
22
|
+
}, (res) => {
|
|
23
|
+
let data = "";
|
|
24
|
+
res.on("data", (chunk) => data += chunk);
|
|
25
|
+
res.on("end", () => {
|
|
26
|
+
if (res.statusCode !== 200) {
|
|
27
|
+
reject(new Error(`OpenAI API error ${res.statusCode}: ${data.slice(0, 200)}`));
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
const parsed = JSON.parse(data);
|
|
31
|
+
resolve(parsed.data.map((d) => d.embedding));
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
req.on("error", reject);
|
|
35
|
+
req.on("timeout", () => {
|
|
36
|
+
req.destroy();
|
|
37
|
+
reject(new Error("OpenAI timeout"));
|
|
38
|
+
});
|
|
39
|
+
req.write(body);
|
|
40
|
+
req.end();
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
async function embedOllama(texts, host, model) {
|
|
44
|
+
const results = [];
|
|
45
|
+
for (const text of texts) {
|
|
46
|
+
const result = await new Promise((resolve, reject) => {
|
|
47
|
+
const url = new URL("/api/embeddings", host);
|
|
48
|
+
const body = JSON.stringify({ model, prompt: text });
|
|
49
|
+
const req = http.request({
|
|
50
|
+
hostname: url.hostname,
|
|
51
|
+
port: url.port,
|
|
52
|
+
path: url.pathname,
|
|
53
|
+
method: "POST",
|
|
54
|
+
headers: {
|
|
55
|
+
"Content-Type": "application/json",
|
|
56
|
+
"Content-Length": Buffer.byteLength(body)
|
|
57
|
+
},
|
|
58
|
+
timeout: 15e3
|
|
59
|
+
}, (res) => {
|
|
60
|
+
let data = "";
|
|
61
|
+
res.on("data", (chunk) => data += chunk);
|
|
62
|
+
res.on("end", () => {
|
|
63
|
+
if (res.statusCode !== 200) {
|
|
64
|
+
reject(new Error(`Ollama error ${res.statusCode}: ${data.slice(0, 200)}`));
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
resolve(JSON.parse(data).embedding);
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
req.on("error", reject);
|
|
71
|
+
req.on("timeout", () => {
|
|
72
|
+
req.destroy();
|
|
73
|
+
reject(new Error("Ollama timeout"));
|
|
74
|
+
});
|
|
75
|
+
req.write(body);
|
|
76
|
+
req.end();
|
|
77
|
+
});
|
|
78
|
+
results.push(result);
|
|
79
|
+
}
|
|
80
|
+
return results;
|
|
81
|
+
}
|
|
82
|
+
async function embedGoogle(texts, apiKey, model) {
|
|
83
|
+
return new Promise((resolve, reject) => {
|
|
84
|
+
const body = JSON.stringify({
|
|
85
|
+
requests: texts.map((text) => ({ model: `models/${model}`, content: { parts: [{ text }] } }))
|
|
86
|
+
});
|
|
87
|
+
const req = https.request({
|
|
88
|
+
hostname: "generativelanguage.googleapis.com",
|
|
89
|
+
path: `/v1beta/models/${model}:batchEmbedContents?key=${apiKey}`,
|
|
90
|
+
method: "POST",
|
|
91
|
+
headers: {
|
|
92
|
+
"Content-Type": "application/json",
|
|
93
|
+
"Content-Length": Buffer.byteLength(body)
|
|
94
|
+
},
|
|
95
|
+
timeout: 3e4
|
|
96
|
+
}, (res) => {
|
|
97
|
+
let data = "";
|
|
98
|
+
res.on("data", (chunk) => data += chunk);
|
|
99
|
+
res.on("end", () => {
|
|
100
|
+
if (res.statusCode !== 200) {
|
|
101
|
+
reject(new Error(`Google API error ${res.statusCode}: ${data.slice(0, 200)}`));
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
const parsed = JSON.parse(data);
|
|
105
|
+
resolve(parsed.embeddings.map((e) => e.values));
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
req.on("error", reject);
|
|
109
|
+
req.on("timeout", () => {
|
|
110
|
+
req.destroy();
|
|
111
|
+
reject(new Error("Google timeout"));
|
|
112
|
+
});
|
|
113
|
+
req.write(body);
|
|
114
|
+
req.end();
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
var Crystal = class {
|
|
118
|
+
config;
|
|
119
|
+
lanceDb = null;
|
|
120
|
+
sqliteDb = null;
|
|
121
|
+
chunksTable = null;
|
|
122
|
+
constructor(config) {
|
|
123
|
+
this.config = config;
|
|
124
|
+
if (!existsSync(config.dataDir)) {
|
|
125
|
+
mkdirSync(config.dataDir, { recursive: true });
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
// ── Initialization ──
|
|
129
|
+
async init() {
|
|
130
|
+
const lanceDir = join(this.config.dataDir, "lance");
|
|
131
|
+
const sqlitePath = join(this.config.dataDir, "crystal.db");
|
|
132
|
+
if (!existsSync(lanceDir)) mkdirSync(lanceDir, { recursive: true });
|
|
133
|
+
this.lanceDb = await lancedb.connect(lanceDir);
|
|
134
|
+
this.sqliteDb = new Database(sqlitePath);
|
|
135
|
+
this.sqliteDb.pragma("journal_mode = WAL");
|
|
136
|
+
this.initSqliteTables();
|
|
137
|
+
await this.initLanceTables();
|
|
138
|
+
}
|
|
139
|
+
initSqliteTables() {
|
|
140
|
+
const db = this.sqliteDb;
|
|
141
|
+
db.exec(`
|
|
142
|
+
CREATE TABLE IF NOT EXISTS sources (
|
|
143
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
144
|
+
type TEXT NOT NULL,
|
|
145
|
+
uri TEXT NOT NULL,
|
|
146
|
+
title TEXT,
|
|
147
|
+
agent_id TEXT NOT NULL,
|
|
148
|
+
metadata TEXT DEFAULT '{}',
|
|
149
|
+
ingested_at TEXT NOT NULL,
|
|
150
|
+
chunk_count INTEGER DEFAULT 0
|
|
151
|
+
);
|
|
152
|
+
|
|
153
|
+
CREATE TABLE IF NOT EXISTS capture_state (
|
|
154
|
+
agent_id TEXT NOT NULL,
|
|
155
|
+
source_id TEXT NOT NULL,
|
|
156
|
+
last_message_count INTEGER DEFAULT 0,
|
|
157
|
+
capture_count INTEGER DEFAULT 0,
|
|
158
|
+
last_capture_at TEXT,
|
|
159
|
+
PRIMARY KEY (agent_id, source_id)
|
|
160
|
+
);
|
|
161
|
+
|
|
162
|
+
CREATE TABLE IF NOT EXISTS memories (
|
|
163
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
164
|
+
text TEXT NOT NULL,
|
|
165
|
+
category TEXT NOT NULL DEFAULT 'fact',
|
|
166
|
+
confidence REAL NOT NULL DEFAULT 1.0,
|
|
167
|
+
source_ids TEXT DEFAULT '[]',
|
|
168
|
+
status TEXT NOT NULL DEFAULT 'active',
|
|
169
|
+
created_at TEXT NOT NULL,
|
|
170
|
+
updated_at TEXT NOT NULL
|
|
171
|
+
);
|
|
172
|
+
|
|
173
|
+
CREATE TABLE IF NOT EXISTS entities (
|
|
174
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
175
|
+
name TEXT NOT NULL UNIQUE,
|
|
176
|
+
type TEXT NOT NULL DEFAULT 'concept',
|
|
177
|
+
description TEXT,
|
|
178
|
+
properties TEXT DEFAULT '{}',
|
|
179
|
+
created_at TEXT NOT NULL,
|
|
180
|
+
updated_at TEXT NOT NULL
|
|
181
|
+
);
|
|
182
|
+
|
|
183
|
+
CREATE TABLE IF NOT EXISTS relationships (
|
|
184
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
185
|
+
source_id INTEGER NOT NULL REFERENCES entities(id),
|
|
186
|
+
target_id INTEGER NOT NULL REFERENCES entities(id),
|
|
187
|
+
type TEXT NOT NULL,
|
|
188
|
+
description TEXT,
|
|
189
|
+
weight REAL DEFAULT 1.0,
|
|
190
|
+
valid_from TEXT NOT NULL,
|
|
191
|
+
valid_until TEXT,
|
|
192
|
+
created_at TEXT NOT NULL
|
|
193
|
+
);
|
|
194
|
+
|
|
195
|
+
CREATE INDEX IF NOT EXISTS idx_sources_agent ON sources(agent_id);
|
|
196
|
+
CREATE INDEX IF NOT EXISTS idx_memories_status ON memories(status);
|
|
197
|
+
CREATE INDEX IF NOT EXISTS idx_entities_name ON entities(name);
|
|
198
|
+
CREATE INDEX IF NOT EXISTS idx_relationships_source ON relationships(source_id);
|
|
199
|
+
CREATE INDEX IF NOT EXISTS idx_relationships_target ON relationships(target_id);
|
|
200
|
+
`);
|
|
201
|
+
}
|
|
202
|
+
async initLanceTables() {
|
|
203
|
+
const db = this.lanceDb;
|
|
204
|
+
const tableNames = await db.tableNames();
|
|
205
|
+
if (tableNames.includes("chunks")) {
|
|
206
|
+
this.chunksTable = await db.openTable("chunks");
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
// ── Embedding ──
|
|
210
|
+
async embed(texts) {
|
|
211
|
+
if (texts.length === 0) return [];
|
|
212
|
+
const cfg = this.config;
|
|
213
|
+
switch (cfg.embeddingProvider) {
|
|
214
|
+
case "openai":
|
|
215
|
+
if (!cfg.openaiApiKey) throw new Error("OpenAI API key required");
|
|
216
|
+
return embedOpenAI(texts, cfg.openaiApiKey, cfg.openaiModel || "text-embedding-3-small");
|
|
217
|
+
case "ollama":
|
|
218
|
+
return embedOllama(texts, cfg.ollamaHost || "http://localhost:11434", cfg.ollamaModel || "nomic-embed-text");
|
|
219
|
+
case "google":
|
|
220
|
+
if (!cfg.googleApiKey) throw new Error("Google API key required");
|
|
221
|
+
return embedGoogle(texts, cfg.googleApiKey, cfg.googleModel || "text-embedding-004");
|
|
222
|
+
default:
|
|
223
|
+
throw new Error(`Unknown embedding provider: ${cfg.embeddingProvider}`);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
// ── Chunking ──
|
|
227
|
+
chunkText(text, targetTokens = 400, overlapTokens = 80) {
|
|
228
|
+
const targetChars = targetTokens * 4;
|
|
229
|
+
const overlapChars = overlapTokens * 4;
|
|
230
|
+
const chunks = [];
|
|
231
|
+
let start = 0;
|
|
232
|
+
while (start < text.length) {
|
|
233
|
+
let end = Math.min(start + targetChars, text.length);
|
|
234
|
+
if (end < text.length) {
|
|
235
|
+
const minBreak = start + Math.floor(targetChars * 0.5);
|
|
236
|
+
const paraBreak = text.lastIndexOf("\n\n", end);
|
|
237
|
+
if (paraBreak > minBreak) {
|
|
238
|
+
end = paraBreak;
|
|
239
|
+
} else {
|
|
240
|
+
const sentBreak = text.lastIndexOf(". ", end);
|
|
241
|
+
if (sentBreak > minBreak) {
|
|
242
|
+
end = sentBreak + 1;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
const chunk = text.slice(start, end).trim();
|
|
247
|
+
if (chunk.length > 0) chunks.push(chunk);
|
|
248
|
+
if (end >= text.length) break;
|
|
249
|
+
start = end - overlapChars;
|
|
250
|
+
if (start <= (chunks.length > 0 ? end - targetChars : 0)) {
|
|
251
|
+
start = end;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
return chunks;
|
|
255
|
+
}
|
|
256
|
+
// ── Ingest ──
|
|
257
|
+
async ingest(chunks) {
|
|
258
|
+
if (chunks.length === 0) return 0;
|
|
259
|
+
const texts = chunks.map((c) => c.text);
|
|
260
|
+
const embeddings = await this.embed(texts);
|
|
261
|
+
const records = chunks.map((chunk, i) => ({
|
|
262
|
+
text: chunk.text,
|
|
263
|
+
vector: embeddings[i],
|
|
264
|
+
role: chunk.role,
|
|
265
|
+
source_type: chunk.source_type,
|
|
266
|
+
source_id: chunk.source_id,
|
|
267
|
+
agent_id: chunk.agent_id,
|
|
268
|
+
token_count: chunk.token_count,
|
|
269
|
+
created_at: chunk.created_at || (/* @__PURE__ */ new Date()).toISOString()
|
|
270
|
+
}));
|
|
271
|
+
if (!this.chunksTable) {
|
|
272
|
+
this.chunksTable = await this.lanceDb.createTable("chunks", records);
|
|
273
|
+
} else {
|
|
274
|
+
await this.chunksTable.add(records);
|
|
275
|
+
}
|
|
276
|
+
return records.length;
|
|
277
|
+
}
|
|
278
|
+
// ── Recency helpers ──
|
|
279
|
+
recencyWeight(ageDays) {
|
|
280
|
+
return Math.max(0.5, 1 - ageDays * 0.01);
|
|
281
|
+
}
|
|
282
|
+
freshnessLabel(ageDays) {
|
|
283
|
+
if (ageDays < 3) return "fresh";
|
|
284
|
+
if (ageDays < 7) return "recent";
|
|
285
|
+
if (ageDays < 14) return "aging";
|
|
286
|
+
return "stale";
|
|
287
|
+
}
|
|
288
|
+
// ── Search ──
|
|
289
|
+
async search(query, limit = 5, filter) {
|
|
290
|
+
if (!this.chunksTable) return [];
|
|
291
|
+
const [embedding] = await this.embed([query]);
|
|
292
|
+
const fetchLimit = Math.max(limit * 3, 30);
|
|
293
|
+
let queryBuilder = this.chunksTable.vectorSearch(embedding).distanceType("cosine").limit(fetchLimit);
|
|
294
|
+
if (filter?.agent_id) {
|
|
295
|
+
queryBuilder = queryBuilder.where(`agent_id = '${filter.agent_id}'`);
|
|
296
|
+
}
|
|
297
|
+
if (filter?.source_type) {
|
|
298
|
+
queryBuilder = queryBuilder.where(`source_type = '${filter.source_type}'`);
|
|
299
|
+
}
|
|
300
|
+
const results = await queryBuilder.toArray();
|
|
301
|
+
const now = Date.now();
|
|
302
|
+
return results.map((row) => {
|
|
303
|
+
const cosine = row._distance != null ? 1 - row._distance : 0;
|
|
304
|
+
const createdAt = row.created_at || "";
|
|
305
|
+
const ageDays = createdAt ? (now - new Date(createdAt).getTime()) / (1e3 * 60 * 60 * 24) : 0;
|
|
306
|
+
const weight = createdAt ? this.recencyWeight(ageDays) : 1;
|
|
307
|
+
return {
|
|
308
|
+
text: row.text,
|
|
309
|
+
role: row.role,
|
|
310
|
+
score: cosine * weight,
|
|
311
|
+
source_type: row.source_type,
|
|
312
|
+
source_id: row.source_id,
|
|
313
|
+
agent_id: row.agent_id,
|
|
314
|
+
created_at: createdAt,
|
|
315
|
+
freshness: createdAt ? this.freshnessLabel(ageDays) : void 0
|
|
316
|
+
};
|
|
317
|
+
}).sort((a, b) => b.score - a.score).slice(0, limit);
|
|
318
|
+
}
|
|
319
|
+
// ── Remember (explicit fact storage) ──
|
|
320
|
+
async remember(text, category = "fact") {
|
|
321
|
+
const db = this.sqliteDb;
|
|
322
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
323
|
+
const stmt = db.prepare(`
|
|
324
|
+
INSERT INTO memories (text, category, confidence, source_ids, status, created_at, updated_at)
|
|
325
|
+
VALUES (?, ?, 1.0, '[]', 'active', ?, ?)
|
|
326
|
+
`);
|
|
327
|
+
const result = stmt.run(text, category, now, now);
|
|
328
|
+
await this.ingest([{
|
|
329
|
+
text,
|
|
330
|
+
role: "system",
|
|
331
|
+
source_type: "manual",
|
|
332
|
+
source_id: `memory:${result.lastInsertRowid}`,
|
|
333
|
+
agent_id: "system",
|
|
334
|
+
token_count: Math.ceil(text.length / 4),
|
|
335
|
+
created_at: now
|
|
336
|
+
}]);
|
|
337
|
+
return result.lastInsertRowid;
|
|
338
|
+
}
|
|
339
|
+
// ── Forget (deprecate a memory) ──
|
|
340
|
+
forget(memoryId) {
|
|
341
|
+
const db = this.sqliteDb;
|
|
342
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
343
|
+
const result = db.prepare(`
|
|
344
|
+
UPDATE memories SET status = 'deprecated', updated_at = ? WHERE id = ? AND status = 'active'
|
|
345
|
+
`).run(now, memoryId);
|
|
346
|
+
return result.changes > 0;
|
|
347
|
+
}
|
|
348
|
+
// ── Status ──
|
|
349
|
+
async status() {
|
|
350
|
+
const db = this.sqliteDb;
|
|
351
|
+
let chunks = 0;
|
|
352
|
+
let oldestChunk = null;
|
|
353
|
+
let newestChunk = null;
|
|
354
|
+
const agents = [];
|
|
355
|
+
if (this.chunksTable) {
|
|
356
|
+
chunks = await this.chunksTable.countRows();
|
|
357
|
+
try {
|
|
358
|
+
const sample = await this.chunksTable.search([]).limit(1).toArray();
|
|
359
|
+
} catch {
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
const memories = db.prepare("SELECT COUNT(*) as count FROM memories WHERE status = ?").get("active")?.count || 0;
|
|
363
|
+
const sources = db.prepare("SELECT COUNT(*) as count FROM sources").get()?.count || 0;
|
|
364
|
+
const sourceAgentRows = db.prepare("SELECT DISTINCT agent_id FROM sources").all();
|
|
365
|
+
const captureAgentRows = db.prepare("SELECT DISTINCT agent_id FROM capture_state").all();
|
|
366
|
+
const allAgents = [.../* @__PURE__ */ new Set([
|
|
367
|
+
...sourceAgentRows.map((r) => r.agent_id),
|
|
368
|
+
...captureAgentRows.map((r) => r.agent_id)
|
|
369
|
+
])];
|
|
370
|
+
agents.push(...allAgents);
|
|
371
|
+
const captureInfo = db.prepare(
|
|
372
|
+
"SELECT COUNT(*) as count, MAX(last_capture_at) as latest FROM capture_state"
|
|
373
|
+
).get();
|
|
374
|
+
return {
|
|
375
|
+
chunks,
|
|
376
|
+
memories,
|
|
377
|
+
sources,
|
|
378
|
+
agents,
|
|
379
|
+
oldestChunk,
|
|
380
|
+
newestChunk,
|
|
381
|
+
embeddingProvider: this.config.embeddingProvider,
|
|
382
|
+
dataDir: this.config.dataDir,
|
|
383
|
+
capturedSessions: captureInfo?.count || 0,
|
|
384
|
+
latestCapture: captureInfo?.latest || null
|
|
385
|
+
};
|
|
386
|
+
}
|
|
387
|
+
// ── Capture State (for incremental ingestion) ──
|
|
388
|
+
getCaptureState(agentId, sourceId) {
|
|
389
|
+
const db = this.sqliteDb;
|
|
390
|
+
const row = db.prepare("SELECT last_message_count, capture_count FROM capture_state WHERE agent_id = ? AND source_id = ?").get(agentId, sourceId);
|
|
391
|
+
if (!row) return { lastMessageCount: 0, captureCount: 0 };
|
|
392
|
+
return {
|
|
393
|
+
lastMessageCount: row.last_message_count,
|
|
394
|
+
captureCount: row.capture_count
|
|
395
|
+
};
|
|
396
|
+
}
|
|
397
|
+
setCaptureState(agentId, sourceId, messageCount, captureCount) {
|
|
398
|
+
const db = this.sqliteDb;
|
|
399
|
+
db.prepare(`
|
|
400
|
+
INSERT OR REPLACE INTO capture_state (agent_id, source_id, last_message_count, capture_count, last_capture_at)
|
|
401
|
+
VALUES (?, ?, ?, ?, ?)
|
|
402
|
+
`).run(agentId, sourceId, messageCount, captureCount, (/* @__PURE__ */ new Date()).toISOString());
|
|
403
|
+
}
|
|
404
|
+
// ── Cleanup ──
|
|
405
|
+
close() {
|
|
406
|
+
this.sqliteDb?.close();
|
|
407
|
+
}
|
|
408
|
+
};
|
|
409
|
+
function resolveConfig(overrides) {
|
|
410
|
+
const openclawHome = process.env.OPENCLAW_HOME || join(process.env.HOME || "/Users/lesa", ".openclaw");
|
|
411
|
+
const dataDir = overrides?.dataDir || join(openclawHome, "memory-crystal");
|
|
412
|
+
loadEnvFile(join(dataDir, ".env"));
|
|
413
|
+
const openaiApiKey = overrides?.openaiApiKey || process.env.OPENAI_API_KEY || opRead(openclawHome, "OpenAI API", "api key");
|
|
414
|
+
const googleApiKey = overrides?.googleApiKey || process.env.GOOGLE_API_KEY || opRead(openclawHome, "Google AI", "api key");
|
|
415
|
+
const remoteToken = overrides?.remoteToken || process.env.CRYSTAL_REMOTE_TOKEN || opRead(openclawHome, "Memory Crystal Remote", "token");
|
|
416
|
+
return {
|
|
417
|
+
dataDir,
|
|
418
|
+
embeddingProvider: overrides?.embeddingProvider || process.env.CRYSTAL_EMBEDDING_PROVIDER || "openai",
|
|
419
|
+
openaiApiKey,
|
|
420
|
+
openaiModel: overrides?.openaiModel || process.env.CRYSTAL_OPENAI_MODEL || "text-embedding-3-small",
|
|
421
|
+
ollamaHost: overrides?.ollamaHost || process.env.CRYSTAL_OLLAMA_HOST || "http://localhost:11434",
|
|
422
|
+
ollamaModel: overrides?.ollamaModel || process.env.CRYSTAL_OLLAMA_MODEL || "nomic-embed-text",
|
|
423
|
+
googleApiKey,
|
|
424
|
+
googleModel: overrides?.googleModel || process.env.CRYSTAL_GOOGLE_MODEL || "text-embedding-004",
|
|
425
|
+
remoteUrl: overrides?.remoteUrl || process.env.CRYSTAL_REMOTE_URL,
|
|
426
|
+
remoteToken
|
|
427
|
+
};
|
|
428
|
+
}
|
|
429
|
+
function loadEnvFile(path) {
|
|
430
|
+
if (!existsSync(path)) return;
|
|
431
|
+
const content = readFileSync(path, "utf8");
|
|
432
|
+
for (const line of content.split("\n")) {
|
|
433
|
+
const trimmed = line.trim();
|
|
434
|
+
if (!trimmed || trimmed.startsWith("#")) continue;
|
|
435
|
+
const eqIdx = trimmed.indexOf("=");
|
|
436
|
+
if (eqIdx === -1) continue;
|
|
437
|
+
const key = trimmed.slice(0, eqIdx).trim();
|
|
438
|
+
let value = trimmed.slice(eqIdx + 1).trim();
|
|
439
|
+
if (value.startsWith('"') && value.endsWith('"') || value.startsWith("'") && value.endsWith("'")) {
|
|
440
|
+
value = value.slice(1, -1);
|
|
441
|
+
}
|
|
442
|
+
if (key && !process.env[key]) {
|
|
443
|
+
process.env[key] = value;
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
function opRead(openclawHome, item, field) {
|
|
448
|
+
try {
|
|
449
|
+
const saTokenPath = join(openclawHome, "secrets", "op-sa-token");
|
|
450
|
+
if (!existsSync(saTokenPath)) return void 0;
|
|
451
|
+
const saToken = readFileSync(saTokenPath, "utf8").trim();
|
|
452
|
+
return execSync(`op read "op://Agent Secrets/${item}/${field}" 2>/dev/null`, {
|
|
453
|
+
encoding: "utf8",
|
|
454
|
+
env: { ...process.env, OP_SERVICE_ACCOUNT_TOKEN: saToken },
|
|
455
|
+
timeout: 1e4
|
|
456
|
+
}).trim() || void 0;
|
|
457
|
+
} catch {
|
|
458
|
+
return void 0;
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
export {
|
|
463
|
+
Crystal,
|
|
464
|
+
resolveConfig
|
|
465
|
+
};
|