@wipcomputer/memory-crystal 0.7.30 → 0.7.33
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/SKILL.md +1 -1
- package/cloud/wrangler.toml +30 -0
- package/dist/bulk-copy.js +1 -1
- package/dist/cc-hook.js +3 -3
- package/dist/cc-poller.js +2 -2
- package/dist/chunk-2GBYLMEF.js +1385 -0
- package/dist/chunk-437F27T6.js +97 -0
- package/dist/chunk-5I7GMRDN.js +146 -0
- package/dist/chunk-CGIDSAJB.js +288 -0
- package/dist/chunk-D3MACYZ4.js +108 -0
- package/dist/chunk-DFQ72B7M.js +248 -0
- package/dist/chunk-NX647OM3.js +310 -0
- package/dist/cli.js +62 -7
- package/dist/core.d.ts +22 -2
- package/dist/core.js +1 -1
- package/dist/crypto.js +2 -2
- package/dist/crystal-serve.js +3 -3
- package/dist/doctor.js +12 -4
- package/dist/dream-weaver.js +2 -2
- package/dist/file-sync.js +3 -3
- package/dist/installer.js +99 -3
- package/dist/ldm.js +1 -1
- package/dist/llm-XXLYPIOF.js +16 -0
- package/dist/mcp-server.js +17 -5
- package/dist/migrate.js +1 -1
- package/dist/mirror-sync.js +4 -4
- package/dist/mlx-setup-XKU67WCT.js +289 -0
- package/dist/openclaw.js +16 -5
- package/dist/pair.js +2 -2
- package/dist/poller.js +5 -5
- package/dist/role.js +2 -2
- package/dist/search-pipeline-CBV25NX7.js +99 -0
- package/dist/staging.js +2 -2
- package/package.json +15 -1
- package/.env.example +0 -20
- package/.publish-skill.json +0 -1
- package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/.env.example +0 -20
- package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/.publish-skill.json +0 -1
- package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/CHANGELOG.md +0 -1297
- package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/CLA.md +0 -19
- package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/LICENSE +0 -52
- package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/README-ENTERPRISE.md +0 -226
- package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/README.md +0 -151
- package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/RELAY.md +0 -199
- package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/SKILL.md +0 -462
- package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/TECHNICAL.md +0 -656
- package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/_trash/RELEASE-NOTES-v0-7-23.md +0 -48
- package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/_trash/RELEASE-NOTES-v0-7-25.md +0 -24
- package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/_trash/RELEASE-NOTES-v0-7-26.md +0 -7
- package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/_trash/RELEASE-NOTES-v0-7-28.md +0 -31
- package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/_trash/RELEASE-NOTES-v0-7-29.md +0 -28
- package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/_trash/RELEASE-NOTES-v0-7-4.md +0 -64
- package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/_trash/RELEASE-NOTES-v0-7-5.md +0 -19
- package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/cloud/README.md +0 -116
- package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/cloud/docs/gpt-system-instructions.md +0 -69
- package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/cloud/migrations/0001_init.sql +0 -52
- package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/migrations/0001_init.sql +0 -51
- package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/migrations/0002_cloud_storage.sql +0 -49
- package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/openclaw.plugin.json +0 -11
- package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/package-lock.json +0 -4169
- package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/package.json +0 -61
- package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/scripts/crystal-capture.sh +0 -29
- package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/scripts/deploy-cloud.sh +0 -153
- package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/scripts/ldm-backup.sh +0 -116
- package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/scripts/migrate-lance-to-sqlite.mjs +0 -218
- package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/skills/memory/SKILL.md +0 -438
- package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/wrangler-demo.toml +0 -8
- package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/wrangler-mcp.toml +0 -24
- package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/.env.example +0 -20
- package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/.publish-skill.json +0 -1
- package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/CHANGELOG.md +0 -1297
- package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/CLA.md +0 -19
- package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/LICENSE +0 -52
- package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/README-ENTERPRISE.md +0 -226
- package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/README.md +0 -151
- package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/RELAY.md +0 -199
- package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/RELEASE-NOTES-v0.7.30.md +0 -29
- package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/SKILL.md +0 -462
- package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/TECHNICAL.md +0 -656
- package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/_trash/RELEASE-NOTES-v0-7-23.md +0 -48
- package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/_trash/RELEASE-NOTES-v0-7-25.md +0 -24
- package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/_trash/RELEASE-NOTES-v0-7-26.md +0 -7
- package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/_trash/RELEASE-NOTES-v0-7-28.md +0 -31
- package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/_trash/RELEASE-NOTES-v0-7-29.md +0 -28
- package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/_trash/RELEASE-NOTES-v0-7-4.md +0 -64
- package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/_trash/RELEASE-NOTES-v0-7-5.md +0 -19
- package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/cloud/README.md +0 -116
- package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/cloud/docs/gpt-system-instructions.md +0 -69
- package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/cloud/migrations/0001_init.sql +0 -52
- package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/migrations/0001_init.sql +0 -51
- package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/migrations/0002_cloud_storage.sql +0 -49
- package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/openclaw.plugin.json +0 -11
- package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/package-lock.json +0 -4169
- package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/package.json +0 -61
- package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/scripts/crystal-capture.sh +0 -29
- package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/scripts/deploy-cloud.sh +0 -153
- package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/scripts/ldm-backup.sh +0 -116
- package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/scripts/migrate-lance-to-sqlite.mjs +0 -218
- package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/skills/memory/SKILL.md +0 -438
- package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/wrangler-demo.toml +0 -8
- package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/wrangler-mcp.toml +0 -24
- package/CHANGELOG.md +0 -1329
- package/README-ENTERPRISE.md +0 -226
- package/RELAY.md +0 -199
- package/_trash/RELEASE-NOTES-v0-7-23.md +0 -48
- package/_trash/RELEASE-NOTES-v0-7-25.md +0 -24
- package/_trash/RELEASE-NOTES-v0-7-26.md +0 -7
- package/_trash/RELEASE-NOTES-v0-7-28.md +0 -31
- package/_trash/RELEASE-NOTES-v0-7-29.md +0 -28
- package/_trash/RELEASE-NOTES-v0-7-4.md +0 -64
- package/_trash/RELEASE-NOTES-v0-7-5.md +0 -19
- package/_trash/RELEASE-NOTES-v0.7.30.md +0 -29
- package/wrangler-demo.toml +0 -8
- package/wrangler-mcp.toml +0 -24
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getAgentId,
|
|
3
|
+
ldmPaths,
|
|
4
|
+
resolveSecretPath,
|
|
5
|
+
resolveStatePath,
|
|
6
|
+
stateWritePath
|
|
7
|
+
} from "./chunk-DFQ72B7M.js";
|
|
8
|
+
|
|
9
|
+
// src/role.ts
|
|
10
|
+
import { existsSync, readFileSync, writeFileSync } from "fs";
|
|
11
|
+
var STATE_FILE = "crystal-role.json";
|
|
12
|
+
function loadRoleState() {
|
|
13
|
+
try {
|
|
14
|
+
const path = resolveStatePath(STATE_FILE);
|
|
15
|
+
if (existsSync(path)) {
|
|
16
|
+
return JSON.parse(readFileSync(path, "utf-8"));
|
|
17
|
+
}
|
|
18
|
+
} catch {
|
|
19
|
+
}
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
function saveRoleState(state) {
|
|
23
|
+
const writePath = stateWritePath(STATE_FILE);
|
|
24
|
+
writeFileSync(writePath, JSON.stringify(state, null, 2) + "\n");
|
|
25
|
+
}
|
|
26
|
+
function hasLocalEmbeddingProvider() {
|
|
27
|
+
if (process.env.OPENAI_API_KEY) return true;
|
|
28
|
+
if (process.env.GOOGLE_API_KEY && process.env.CRYSTAL_EMBEDDING_PROVIDER === "google") return true;
|
|
29
|
+
if (process.env.CRYSTAL_EMBEDDING_PROVIDER === "ollama") return true;
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
function hasRelayKey() {
|
|
33
|
+
const keyPath = resolveSecretPath("crystal-relay-key");
|
|
34
|
+
return existsSync(keyPath);
|
|
35
|
+
}
|
|
36
|
+
function detectRole() {
|
|
37
|
+
const agentId = getAgentId();
|
|
38
|
+
const paths = ldmPaths(agentId);
|
|
39
|
+
const relayUrl = process.env.CRYSTAL_RELAY_URL || null;
|
|
40
|
+
const relayToken = !!process.env.CRYSTAL_RELAY_TOKEN;
|
|
41
|
+
const relayKeyExists = hasRelayKey();
|
|
42
|
+
const localEmbeddings = hasLocalEmbeddingProvider();
|
|
43
|
+
const localDb = existsSync(paths.crystalDb);
|
|
44
|
+
const state = loadRoleState();
|
|
45
|
+
if (state && state.override) {
|
|
46
|
+
return {
|
|
47
|
+
role: state.role,
|
|
48
|
+
source: "state-file",
|
|
49
|
+
relayUrl,
|
|
50
|
+
relayToken,
|
|
51
|
+
relayKeyExists,
|
|
52
|
+
agentId,
|
|
53
|
+
hasLocalEmbeddings: localEmbeddings,
|
|
54
|
+
hasLocalDb: localDb
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
let role = "core";
|
|
58
|
+
if ((relayUrl || relayKeyExists) && !localEmbeddings) {
|
|
59
|
+
role = "node";
|
|
60
|
+
} else if ((relayUrl || relayKeyExists) && localEmbeddings) {
|
|
61
|
+
role = "core";
|
|
62
|
+
}
|
|
63
|
+
return {
|
|
64
|
+
role,
|
|
65
|
+
source: "auto-detected",
|
|
66
|
+
relayUrl,
|
|
67
|
+
relayToken,
|
|
68
|
+
relayKeyExists,
|
|
69
|
+
agentId,
|
|
70
|
+
hasLocalEmbeddings: localEmbeddings,
|
|
71
|
+
hasLocalDb: localDb
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
function promoteToCore() {
|
|
75
|
+
saveRoleState({
|
|
76
|
+
role: "core",
|
|
77
|
+
override: true,
|
|
78
|
+
agentId: getAgentId(),
|
|
79
|
+
setAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
function demoteToNode(relayUrl) {
|
|
83
|
+
saveRoleState({
|
|
84
|
+
role: "node",
|
|
85
|
+
override: true,
|
|
86
|
+
relayUrl,
|
|
87
|
+
agentId: getAgentId(),
|
|
88
|
+
setAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export {
|
|
93
|
+
loadRoleState,
|
|
94
|
+
detectRole,
|
|
95
|
+
promoteToCore,
|
|
96
|
+
demoteToNode
|
|
97
|
+
};
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ensureLdm,
|
|
3
|
+
ldmPaths
|
|
4
|
+
} from "./chunk-DFQ72B7M.js";
|
|
5
|
+
|
|
6
|
+
// src/staging.ts
|
|
7
|
+
import {
|
|
8
|
+
existsSync,
|
|
9
|
+
mkdirSync,
|
|
10
|
+
readdirSync,
|
|
11
|
+
renameSync,
|
|
12
|
+
unlinkSync,
|
|
13
|
+
writeFileSync
|
|
14
|
+
} from "fs";
|
|
15
|
+
import { join } from "path";
|
|
16
|
+
import { execSync } from "child_process";
|
|
17
|
+
var HOME = process.env.HOME || "";
|
|
18
|
+
var STAGING_ROOT = join(HOME, ".ldm", "staging");
|
|
19
|
+
function stagingPaths(agentId) {
|
|
20
|
+
const root = join(STAGING_ROOT, agentId);
|
|
21
|
+
return {
|
|
22
|
+
root,
|
|
23
|
+
transcripts: join(root, "transcripts"),
|
|
24
|
+
readyFile: join(root, "READY")
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function ensureStaging(agentId) {
|
|
28
|
+
const paths = stagingPaths(agentId);
|
|
29
|
+
mkdirSync(paths.transcripts, { recursive: true });
|
|
30
|
+
return paths;
|
|
31
|
+
}
|
|
32
|
+
function markReady(agentId) {
|
|
33
|
+
const paths = stagingPaths(agentId);
|
|
34
|
+
writeFileSync(paths.readyFile, JSON.stringify({
|
|
35
|
+
markedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
36
|
+
agentId
|
|
37
|
+
}));
|
|
38
|
+
}
|
|
39
|
+
function isNewAgent(agentId) {
|
|
40
|
+
const paths = ldmPaths(agentId);
|
|
41
|
+
return !existsSync(paths.agentRoot);
|
|
42
|
+
}
|
|
43
|
+
function hasStagedData(agentId) {
|
|
44
|
+
const paths = stagingPaths(agentId);
|
|
45
|
+
return existsSync(paths.readyFile);
|
|
46
|
+
}
|
|
47
|
+
function listStagedAgents() {
|
|
48
|
+
if (!existsSync(STAGING_ROOT)) return [];
|
|
49
|
+
const agents = [];
|
|
50
|
+
for (const entry of readdirSync(STAGING_ROOT, { withFileTypes: true })) {
|
|
51
|
+
if (!entry.isDirectory()) continue;
|
|
52
|
+
if (existsSync(join(STAGING_ROOT, entry.name, "READY"))) {
|
|
53
|
+
agents.push(entry.name);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return agents;
|
|
57
|
+
}
|
|
58
|
+
async function processStagedAgent(agentId) {
|
|
59
|
+
const startTime = Date.now();
|
|
60
|
+
const staging = stagingPaths(agentId);
|
|
61
|
+
if (!existsSync(staging.readyFile)) {
|
|
62
|
+
throw new Error(`No READY file for agent ${agentId}`);
|
|
63
|
+
}
|
|
64
|
+
const agentPaths = ensureLdm(agentId);
|
|
65
|
+
const stagedFiles = existsSync(staging.transcripts) ? readdirSync(staging.transcripts).filter((f) => f.endsWith(".jsonl")) : [];
|
|
66
|
+
if (stagedFiles.length === 0) {
|
|
67
|
+
unlinkSync(staging.readyFile);
|
|
68
|
+
return {
|
|
69
|
+
agentId,
|
|
70
|
+
transcriptsProcessed: 0,
|
|
71
|
+
backfillChunks: 0,
|
|
72
|
+
dreamWeaverRan: false,
|
|
73
|
+
durationMs: Date.now() - startTime
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
for (const file of stagedFiles) {
|
|
77
|
+
const src = join(staging.transcripts, file);
|
|
78
|
+
const dest = join(agentPaths.transcripts, file);
|
|
79
|
+
try {
|
|
80
|
+
renameSync(src, dest);
|
|
81
|
+
} catch {
|
|
82
|
+
const { copyFileSync } = await import("fs");
|
|
83
|
+
copyFileSync(src, dest);
|
|
84
|
+
unlinkSync(src);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
let backfillChunks = 0;
|
|
88
|
+
let dreamWeaverRan = false;
|
|
89
|
+
try {
|
|
90
|
+
const output = execSync(
|
|
91
|
+
`crystal backfill --agent ${agentId}`,
|
|
92
|
+
{ encoding: "utf-8", timeout: 6e5 }
|
|
93
|
+
);
|
|
94
|
+
const match = output.match(/(\d+) chunks embedded/);
|
|
95
|
+
if (match) backfillChunks = parseInt(match[1], 10);
|
|
96
|
+
} catch (err) {
|
|
97
|
+
process.stderr.write(`[staging] backfill failed for ${agentId}: ${err.message}
|
|
98
|
+
`);
|
|
99
|
+
}
|
|
100
|
+
try {
|
|
101
|
+
execSync(
|
|
102
|
+
`crystal dream-weave --agent ${agentId} --mode full`,
|
|
103
|
+
{ encoding: "utf-8", timeout: 6e5 }
|
|
104
|
+
);
|
|
105
|
+
dreamWeaverRan = true;
|
|
106
|
+
} catch (err) {
|
|
107
|
+
process.stderr.write(`[staging] dream-weave failed for ${agentId}: ${err.message}
|
|
108
|
+
`);
|
|
109
|
+
}
|
|
110
|
+
try {
|
|
111
|
+
unlinkSync(staging.readyFile);
|
|
112
|
+
} catch {
|
|
113
|
+
}
|
|
114
|
+
return {
|
|
115
|
+
agentId,
|
|
116
|
+
transcriptsProcessed: stagedFiles.length,
|
|
117
|
+
backfillChunks,
|
|
118
|
+
dreamWeaverRan,
|
|
119
|
+
durationMs: Date.now() - startTime
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
async function processAllStaged() {
|
|
123
|
+
const agents = listStagedAgents();
|
|
124
|
+
const results = [];
|
|
125
|
+
for (const agentId of agents) {
|
|
126
|
+
try {
|
|
127
|
+
const result = await processStagedAgent(agentId);
|
|
128
|
+
results.push(result);
|
|
129
|
+
} catch (err) {
|
|
130
|
+
process.stderr.write(`[staging] failed to process ${agentId}: ${err.message}
|
|
131
|
+
`);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return results;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export {
|
|
138
|
+
stagingPaths,
|
|
139
|
+
ensureStaging,
|
|
140
|
+
markReady,
|
|
141
|
+
isNewAgent,
|
|
142
|
+
hasStagedData,
|
|
143
|
+
listStagedAgents,
|
|
144
|
+
processStagedAgent,
|
|
145
|
+
processAllStaged
|
|
146
|
+
};
|
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
import {
|
|
2
|
+
decrypt,
|
|
3
|
+
decryptJSON,
|
|
4
|
+
encrypt,
|
|
5
|
+
encryptJSON,
|
|
6
|
+
loadRelayKey
|
|
7
|
+
} from "./chunk-D3MACYZ4.js";
|
|
8
|
+
import {
|
|
9
|
+
ldmPaths,
|
|
10
|
+
resolveStatePath,
|
|
11
|
+
stateWritePath
|
|
12
|
+
} from "./chunk-DFQ72B7M.js";
|
|
13
|
+
|
|
14
|
+
// src/file-sync.ts
|
|
15
|
+
import { createHash } from "crypto";
|
|
16
|
+
import {
|
|
17
|
+
existsSync,
|
|
18
|
+
mkdirSync,
|
|
19
|
+
readFileSync,
|
|
20
|
+
writeFileSync,
|
|
21
|
+
unlinkSync,
|
|
22
|
+
readdirSync,
|
|
23
|
+
statSync
|
|
24
|
+
} from "fs";
|
|
25
|
+
import { join, relative, dirname } from "path";
|
|
26
|
+
var RELAY_URL = process.env.CRYSTAL_RELAY_URL || "";
|
|
27
|
+
var RELAY_TOKEN = process.env.CRYSTAL_RELAY_TOKEN || "";
|
|
28
|
+
var EXCLUDE_PATTERNS = [
|
|
29
|
+
"memory/crystal.db",
|
|
30
|
+
// DB syncs via delta chunks, not file copy
|
|
31
|
+
"memory/crystal.db-wal",
|
|
32
|
+
"memory/crystal.db-shm",
|
|
33
|
+
"memory/crystal.db.bak",
|
|
34
|
+
"memory/crystal.db.tmp",
|
|
35
|
+
"memory/lance/",
|
|
36
|
+
// LanceDB (deprecated, not synced)
|
|
37
|
+
"state/",
|
|
38
|
+
// Local state files (watermarks, etc.)
|
|
39
|
+
"secrets/",
|
|
40
|
+
// Encryption keys, tokens
|
|
41
|
+
"staging/",
|
|
42
|
+
// Staging pipeline (Core-only)
|
|
43
|
+
"bin/",
|
|
44
|
+
// Local scripts
|
|
45
|
+
".DS_Store"
|
|
46
|
+
];
|
|
47
|
+
function shouldExclude(relativePath) {
|
|
48
|
+
for (const pattern of EXCLUDE_PATTERNS) {
|
|
49
|
+
if (relativePath === pattern || relativePath.startsWith(pattern)) return true;
|
|
50
|
+
}
|
|
51
|
+
const parts = relativePath.split("/");
|
|
52
|
+
for (const part of parts) {
|
|
53
|
+
if (part.startsWith(".") && part !== ".ldm") return true;
|
|
54
|
+
}
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
function scanDir(baseDir, currentDir, entries) {
|
|
58
|
+
if (!existsSync(currentDir)) return;
|
|
59
|
+
const items = readdirSync(currentDir);
|
|
60
|
+
for (const item of items) {
|
|
61
|
+
const fullPath = join(currentDir, item);
|
|
62
|
+
const relPath = relative(baseDir, fullPath);
|
|
63
|
+
if (shouldExclude(relPath)) continue;
|
|
64
|
+
let stat;
|
|
65
|
+
try {
|
|
66
|
+
stat = statSync(fullPath);
|
|
67
|
+
} catch {
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
if (stat.isDirectory()) {
|
|
71
|
+
scanDir(baseDir, fullPath, entries);
|
|
72
|
+
} else if (stat.isFile()) {
|
|
73
|
+
if (stat.size > 50 * 1024 * 1024) continue;
|
|
74
|
+
const content = readFileSync(fullPath);
|
|
75
|
+
const sha256 = createHash("sha256").update(content).digest("hex");
|
|
76
|
+
entries.push({ path: relPath, sha256, size: stat.size });
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
function generateManifest() {
|
|
81
|
+
const paths = ldmPaths();
|
|
82
|
+
const entries = [];
|
|
83
|
+
scanDir(paths.root, paths.root, entries);
|
|
84
|
+
return {
|
|
85
|
+
version: 1,
|
|
86
|
+
generatedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
87
|
+
fileCount: entries.length,
|
|
88
|
+
entries
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
function compareManifest(coreManifest) {
|
|
92
|
+
const paths = ldmPaths();
|
|
93
|
+
const upsert = [];
|
|
94
|
+
const toDelete = [];
|
|
95
|
+
const localManifest = generateManifest();
|
|
96
|
+
const localMap = new Map(localManifest.entries.map((e) => [e.path, e]));
|
|
97
|
+
const coreMap = new Map(coreManifest.entries.map((e) => [e.path, e]));
|
|
98
|
+
for (const entry of coreManifest.entries) {
|
|
99
|
+
const local = localMap.get(entry.path);
|
|
100
|
+
if (!local || local.sha256 !== entry.sha256) {
|
|
101
|
+
upsert.push({ path: entry.path, sha256: entry.sha256 });
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
for (const entry of localManifest.entries) {
|
|
105
|
+
if (!coreMap.has(entry.path)) {
|
|
106
|
+
toDelete.push(entry.path);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return { upsert, delete: toDelete };
|
|
110
|
+
}
|
|
111
|
+
function loadFileSyncState() {
|
|
112
|
+
const statePath = resolveStatePath("file-sync-state.json");
|
|
113
|
+
try {
|
|
114
|
+
if (existsSync(statePath)) {
|
|
115
|
+
return JSON.parse(readFileSync(statePath, "utf-8"));
|
|
116
|
+
}
|
|
117
|
+
} catch {
|
|
118
|
+
}
|
|
119
|
+
return { lastSync: null, lastManifestHash: null, filesTransferred: 0, filesDeleted: 0 };
|
|
120
|
+
}
|
|
121
|
+
function saveFileSyncState(state) {
|
|
122
|
+
const writePath = stateWritePath("file-sync-state.json");
|
|
123
|
+
writeFileSync(writePath, JSON.stringify(state, null, 2));
|
|
124
|
+
}
|
|
125
|
+
async function pushFileSync() {
|
|
126
|
+
if (!RELAY_URL || !RELAY_TOKEN) {
|
|
127
|
+
throw new Error("CRYSTAL_RELAY_URL and CRYSTAL_RELAY_TOKEN must be set");
|
|
128
|
+
}
|
|
129
|
+
const relayKey = loadRelayKey();
|
|
130
|
+
const paths = ldmPaths();
|
|
131
|
+
const manifest = generateManifest();
|
|
132
|
+
const manifestJson = JSON.stringify(manifest.entries.map((e) => `${e.path}:${e.sha256}`));
|
|
133
|
+
const manifestHash = createHash("sha256").update(manifestJson).digest("hex");
|
|
134
|
+
const state = loadFileSyncState();
|
|
135
|
+
if (state.lastManifestHash === manifestHash) {
|
|
136
|
+
return { manifest: 0, files: 0 };
|
|
137
|
+
}
|
|
138
|
+
const encryptedManifest = encryptJSON(manifest, relayKey);
|
|
139
|
+
const manifestResp = await fetch(`${RELAY_URL}/drop/files`, {
|
|
140
|
+
method: "POST",
|
|
141
|
+
headers: {
|
|
142
|
+
"Authorization": `Bearer ${RELAY_TOKEN}`,
|
|
143
|
+
"Content-Type": "application/octet-stream",
|
|
144
|
+
"X-File-Type": "manifest"
|
|
145
|
+
},
|
|
146
|
+
body: JSON.stringify(encryptedManifest)
|
|
147
|
+
});
|
|
148
|
+
if (!manifestResp.ok) {
|
|
149
|
+
throw new Error(`Manifest push failed: ${manifestResp.status} ${await manifestResp.text()}`);
|
|
150
|
+
}
|
|
151
|
+
let filesPushed = 0;
|
|
152
|
+
for (const entry of manifest.entries) {
|
|
153
|
+
const fullPath = join(paths.root, entry.path);
|
|
154
|
+
if (!existsSync(fullPath)) continue;
|
|
155
|
+
const content = readFileSync(fullPath);
|
|
156
|
+
const encrypted = encrypt(content, relayKey);
|
|
157
|
+
const filePayload = JSON.stringify({
|
|
158
|
+
path: entry.path,
|
|
159
|
+
sha256: entry.sha256,
|
|
160
|
+
size: entry.size,
|
|
161
|
+
data: encrypted
|
|
162
|
+
});
|
|
163
|
+
const fileResp = await fetch(`${RELAY_URL}/drop/files`, {
|
|
164
|
+
method: "POST",
|
|
165
|
+
headers: {
|
|
166
|
+
"Authorization": `Bearer ${RELAY_TOKEN}`,
|
|
167
|
+
"Content-Type": "application/octet-stream",
|
|
168
|
+
"X-File-Type": "file"
|
|
169
|
+
},
|
|
170
|
+
body: filePayload
|
|
171
|
+
});
|
|
172
|
+
if (fileResp.ok) filesPushed++;
|
|
173
|
+
}
|
|
174
|
+
state.lastSync = (/* @__PURE__ */ new Date()).toISOString();
|
|
175
|
+
state.lastManifestHash = manifestHash;
|
|
176
|
+
state.filesTransferred += filesPushed;
|
|
177
|
+
saveFileSyncState(state);
|
|
178
|
+
return { manifest: manifest.fileCount, files: filesPushed };
|
|
179
|
+
}
|
|
180
|
+
async function pullFileSync() {
|
|
181
|
+
if (!RELAY_URL || !RELAY_TOKEN) {
|
|
182
|
+
throw new Error("CRYSTAL_RELAY_URL and CRYSTAL_RELAY_TOKEN must be set");
|
|
183
|
+
}
|
|
184
|
+
const relayKey = loadRelayKey();
|
|
185
|
+
const paths = ldmPaths();
|
|
186
|
+
const listResp = await fetch(`${RELAY_URL}/pickup/files`, {
|
|
187
|
+
headers: { "Authorization": `Bearer ${RELAY_TOKEN}` }
|
|
188
|
+
});
|
|
189
|
+
if (!listResp.ok) {
|
|
190
|
+
throw new Error(`File sync list failed: ${listResp.status} ${await listResp.text()}`);
|
|
191
|
+
}
|
|
192
|
+
const listData = await listResp.json();
|
|
193
|
+
if (listData.count === 0) {
|
|
194
|
+
return { imported: 0, deleted: 0 };
|
|
195
|
+
}
|
|
196
|
+
let coreManifest = null;
|
|
197
|
+
const fileBlobs = [];
|
|
198
|
+
for (const blob of listData.blobs) {
|
|
199
|
+
try {
|
|
200
|
+
const blobResp = await fetch(`${RELAY_URL}/pickup/files/${blob.id}`, {
|
|
201
|
+
headers: { "Authorization": `Bearer ${RELAY_TOKEN}` }
|
|
202
|
+
});
|
|
203
|
+
if (!blobResp.ok) continue;
|
|
204
|
+
const text = await blobResp.text();
|
|
205
|
+
const parsed = JSON.parse(text);
|
|
206
|
+
if (parsed.v !== void 0 && parsed.nonce !== void 0) {
|
|
207
|
+
try {
|
|
208
|
+
const manifest = decryptJSON(parsed, relayKey);
|
|
209
|
+
if (manifest.version && manifest.entries) {
|
|
210
|
+
coreManifest = manifest;
|
|
211
|
+
}
|
|
212
|
+
} catch {
|
|
213
|
+
}
|
|
214
|
+
} else if (parsed.path && parsed.data) {
|
|
215
|
+
fileBlobs.push({
|
|
216
|
+
id: blob.id,
|
|
217
|
+
path: parsed.path,
|
|
218
|
+
sha256: parsed.sha256,
|
|
219
|
+
data: parsed.data
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
await fetch(`${RELAY_URL}/confirm/files/${blob.id}`, {
|
|
223
|
+
method: "DELETE",
|
|
224
|
+
headers: { "Authorization": `Bearer ${RELAY_TOKEN}` }
|
|
225
|
+
});
|
|
226
|
+
} catch (err) {
|
|
227
|
+
process.stderr.write(`[file-sync] error processing blob ${blob.id}: ${err.message}
|
|
228
|
+
`);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
if (!coreManifest) {
|
|
232
|
+
process.stderr.write("[file-sync] no manifest found in relay\n");
|
|
233
|
+
return { imported: 0, deleted: 0 };
|
|
234
|
+
}
|
|
235
|
+
const delta = compareManifest(coreManifest);
|
|
236
|
+
const fileBlobMap = new Map(fileBlobs.map((f) => [f.path, f]));
|
|
237
|
+
let imported = 0;
|
|
238
|
+
for (const entry of delta.upsert) {
|
|
239
|
+
const fileBlob = fileBlobMap.get(entry.path);
|
|
240
|
+
if (!fileBlob) {
|
|
241
|
+
continue;
|
|
242
|
+
}
|
|
243
|
+
try {
|
|
244
|
+
const content = decrypt(fileBlob.data, relayKey);
|
|
245
|
+
const actualHash = createHash("sha256").update(content).digest("hex");
|
|
246
|
+
if (actualHash !== entry.sha256) {
|
|
247
|
+
process.stderr.write(`[file-sync] hash mismatch for ${entry.path}, skipping
|
|
248
|
+
`);
|
|
249
|
+
continue;
|
|
250
|
+
}
|
|
251
|
+
const destPath = join(paths.root, entry.path);
|
|
252
|
+
mkdirSync(dirname(destPath), { recursive: true });
|
|
253
|
+
writeFileSync(destPath, content);
|
|
254
|
+
imported++;
|
|
255
|
+
} catch (err) {
|
|
256
|
+
process.stderr.write(`[file-sync] failed to write ${entry.path}: ${err.message}
|
|
257
|
+
`);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
let deleted = 0;
|
|
261
|
+
for (const path of delta.delete) {
|
|
262
|
+
const destPath = join(paths.root, path);
|
|
263
|
+
if (existsSync(destPath)) {
|
|
264
|
+
try {
|
|
265
|
+
unlinkSync(destPath);
|
|
266
|
+
deleted++;
|
|
267
|
+
} catch (err) {
|
|
268
|
+
process.stderr.write(`[file-sync] failed to delete ${path}: ${err.message}
|
|
269
|
+
`);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
const state = loadFileSyncState();
|
|
274
|
+
state.lastSync = (/* @__PURE__ */ new Date()).toISOString();
|
|
275
|
+
state.filesTransferred += imported;
|
|
276
|
+
state.filesDeleted += deleted;
|
|
277
|
+
saveFileSyncState(state);
|
|
278
|
+
return { imported, deleted };
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
export {
|
|
282
|
+
generateManifest,
|
|
283
|
+
compareManifest,
|
|
284
|
+
loadFileSyncState,
|
|
285
|
+
saveFileSyncState,
|
|
286
|
+
pushFileSync,
|
|
287
|
+
pullFileSync
|
|
288
|
+
};
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import {
|
|
2
|
+
resolveSecretPath
|
|
3
|
+
} from "./chunk-DFQ72B7M.js";
|
|
4
|
+
|
|
5
|
+
// src/crypto.ts
|
|
6
|
+
import { readFileSync, existsSync } from "fs";
|
|
7
|
+
import { createCipheriv, createDecipheriv, createHmac, randomBytes, hkdfSync } from "crypto";
|
|
8
|
+
import { createHash } from "crypto";
|
|
9
|
+
var KEY_PATH = process.env.CRYSTAL_RELAY_KEY_PATH || resolveSecretPath("crystal-relay-key");
|
|
10
|
+
function loadRelayKey() {
|
|
11
|
+
if (!existsSync(KEY_PATH)) {
|
|
12
|
+
throw new Error(
|
|
13
|
+
`Relay key not found at ${KEY_PATH}
|
|
14
|
+
Generate one: mkdir -p ~/.ldm/secrets && openssl rand -base64 32 > ~/.ldm/secrets/crystal-relay-key && chmod 600 ~/.ldm/secrets/crystal-relay-key
|
|
15
|
+
Or run: crystal pair`
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
const raw = readFileSync(KEY_PATH, "utf-8").trim();
|
|
19
|
+
const key = Buffer.from(raw, "base64");
|
|
20
|
+
if (key.length !== 32) {
|
|
21
|
+
throw new Error(`Relay key must be 32 bytes (256 bits). Got ${key.length} bytes. Regenerate with: openssl rand -base64 32`);
|
|
22
|
+
}
|
|
23
|
+
return key;
|
|
24
|
+
}
|
|
25
|
+
function deriveSigningKey(masterKey) {
|
|
26
|
+
return Buffer.from(hkdfSync("sha256", masterKey, "", "crystal-relay-sign", 32));
|
|
27
|
+
}
|
|
28
|
+
function encrypt(plaintext, masterKey) {
|
|
29
|
+
const nonce = randomBytes(12);
|
|
30
|
+
const cipher = createCipheriv("aes-256-gcm", masterKey, nonce);
|
|
31
|
+
const ciphertext = Buffer.concat([cipher.update(plaintext), cipher.final()]);
|
|
32
|
+
const tag = cipher.getAuthTag();
|
|
33
|
+
const signingKey = deriveSigningKey(masterKey);
|
|
34
|
+
const hmacData = Buffer.concat([nonce, ciphertext, tag]);
|
|
35
|
+
const hmac = createHmac("sha256", signingKey).update(hmacData).digest("hex");
|
|
36
|
+
return {
|
|
37
|
+
v: 1,
|
|
38
|
+
nonce: nonce.toString("base64"),
|
|
39
|
+
ciphertext: ciphertext.toString("base64"),
|
|
40
|
+
tag: tag.toString("base64"),
|
|
41
|
+
hmac
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
function decrypt(payload, masterKey) {
|
|
45
|
+
if (payload.v !== 1) {
|
|
46
|
+
throw new Error(`Unknown payload version: ${payload.v}`);
|
|
47
|
+
}
|
|
48
|
+
const nonce = Buffer.from(payload.nonce, "base64");
|
|
49
|
+
const ciphertext = Buffer.from(payload.ciphertext, "base64");
|
|
50
|
+
const tag = Buffer.from(payload.tag, "base64");
|
|
51
|
+
const signingKey = deriveSigningKey(masterKey);
|
|
52
|
+
const hmacData = Buffer.concat([nonce, ciphertext, tag]);
|
|
53
|
+
const expectedHmac = createHmac("sha256", signingKey).update(hmacData).digest("hex");
|
|
54
|
+
if (payload.hmac !== expectedHmac) {
|
|
55
|
+
throw new Error("HMAC verification failed \u2014 blob rejected (tampered or wrong key)");
|
|
56
|
+
}
|
|
57
|
+
const decipher = createDecipheriv("aes-256-gcm", masterKey, nonce);
|
|
58
|
+
decipher.setAuthTag(tag);
|
|
59
|
+
return Buffer.concat([decipher.update(ciphertext), decipher.final()]);
|
|
60
|
+
}
|
|
61
|
+
function encryptJSON(data, masterKey) {
|
|
62
|
+
const plaintext = Buffer.from(JSON.stringify(data), "utf-8");
|
|
63
|
+
return encrypt(plaintext, masterKey);
|
|
64
|
+
}
|
|
65
|
+
function decryptJSON(payload, masterKey) {
|
|
66
|
+
const plaintext = decrypt(payload, masterKey);
|
|
67
|
+
return JSON.parse(plaintext.toString("utf-8"));
|
|
68
|
+
}
|
|
69
|
+
function encryptFile(filePath, masterKey) {
|
|
70
|
+
const plaintext = readFileSync(filePath);
|
|
71
|
+
return encrypt(plaintext, masterKey);
|
|
72
|
+
}
|
|
73
|
+
var RELAY_KEY_PATH = KEY_PATH;
|
|
74
|
+
function generateRelayKey() {
|
|
75
|
+
return randomBytes(32);
|
|
76
|
+
}
|
|
77
|
+
function encodePairingString(key) {
|
|
78
|
+
if (key.length !== 32) throw new Error("Key must be 32 bytes");
|
|
79
|
+
return `mc1:${key.toString("base64")}`;
|
|
80
|
+
}
|
|
81
|
+
function decodePairingString(str) {
|
|
82
|
+
const trimmed = str.trim();
|
|
83
|
+
if (!trimmed.startsWith("mc1:")) {
|
|
84
|
+
throw new Error("Invalid pairing string (expected mc1: prefix)");
|
|
85
|
+
}
|
|
86
|
+
const key = Buffer.from(trimmed.slice(4), "base64");
|
|
87
|
+
if (key.length !== 32) {
|
|
88
|
+
throw new Error(`Invalid key length: expected 32 bytes, got ${key.length}`);
|
|
89
|
+
}
|
|
90
|
+
return key;
|
|
91
|
+
}
|
|
92
|
+
function hashBuffer(data) {
|
|
93
|
+
return createHash("sha256").update(data).digest("hex");
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export {
|
|
97
|
+
loadRelayKey,
|
|
98
|
+
encrypt,
|
|
99
|
+
decrypt,
|
|
100
|
+
encryptJSON,
|
|
101
|
+
decryptJSON,
|
|
102
|
+
encryptFile,
|
|
103
|
+
RELAY_KEY_PATH,
|
|
104
|
+
generateRelayKey,
|
|
105
|
+
encodePairingString,
|
|
106
|
+
decodePairingString,
|
|
107
|
+
hashBuffer
|
|
108
|
+
};
|