@wipcomputer/memory-crystal 0.7.32 → 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/bridge.d.ts +7 -0
- package/dist/bridge.js +14 -0
- package/dist/bulk-copy.d.ts +17 -0
- package/dist/bulk-copy.js +90 -0
- package/dist/cc-hook.d.ts +8 -0
- package/dist/cc-hook.js +368 -0
- package/dist/cc-poller.d.ts +1 -0
- package/dist/cc-poller.js +550 -0
- package/dist/chunk-25LXQJ4Z.js +110 -0
- package/dist/chunk-2DRXIRQW.js +97 -0
- package/dist/chunk-2GBYLMEF.js +1385 -0
- package/dist/chunk-2ZNH5F6E.js +1281 -0
- package/dist/chunk-3G3SFYYI.js +288 -0
- package/dist/chunk-3RG5ZIWI.js +10 -0
- package/dist/chunk-3S6TI23B.js +97 -0
- package/dist/chunk-3VFIJYS4.js +818 -0
- package/dist/chunk-437F27T6.js +97 -0
- package/dist/chunk-52QE3YI3.js +1169 -0
- package/dist/chunk-57RP3DIN.js +1205 -0
- package/dist/chunk-5HSZ4W2P.js +62 -0
- package/dist/chunk-5I7GMRDN.js +146 -0
- package/dist/chunk-645IPXW3.js +290 -0
- package/dist/chunk-7A7ELD4C.js +1205 -0
- package/dist/chunk-7FYY4GZM.js +1205 -0
- package/dist/chunk-7IUE7ODU.js +254 -0
- package/dist/chunk-7RMLKZIS.js +108 -0
- package/dist/chunk-AA3OPP4Z.js +432 -0
- package/dist/chunk-AEWLSYPH.js +72 -0
- package/dist/chunk-ASSZDR6I.js +108 -0
- package/dist/chunk-AYRJVWUC.js +1205 -0
- package/dist/chunk-CCYI5O3D.js +148 -0
- package/dist/chunk-CGIDSAJB.js +288 -0
- package/dist/chunk-D3I3ZSE2.js +411 -0
- package/dist/chunk-D3MACYZ4.js +108 -0
- package/dist/chunk-DACSKLY6.js +219 -0
- package/dist/chunk-DFQ72B7M.js +248 -0
- package/dist/chunk-DW5B4BL7.js +108 -0
- package/dist/chunk-EKSACBTJ.js +1070 -0
- package/dist/chunk-EXEZZADG.js +248 -0
- package/dist/chunk-F3Y7EL7K.js +83 -0
- package/dist/chunk-FBQWSDPC.js +1328 -0
- package/dist/chunk-FHRZNOMW.js +1205 -0
- package/dist/chunk-IM7N24MT.js +129 -0
- package/dist/chunk-IPNYIXFK.js +1178 -0
- package/dist/chunk-J7MRSZIO.js +167 -0
- package/dist/chunk-JITKI2OI.js +106 -0
- package/dist/chunk-JWZXYVET.js +1068 -0
- package/dist/chunk-KCQUXVYT.js +108 -0
- package/dist/chunk-KOQ43OX6.js +1281 -0
- package/dist/chunk-KYVWO6ZM.js +1069 -0
- package/dist/chunk-L3VHARQH.js +413 -0
- package/dist/chunk-LBWDS6BE.js +288 -0
- package/dist/chunk-LOVAHSQV.js +411 -0
- package/dist/chunk-LQOYCAGG.js +446 -0
- package/dist/chunk-LWAIPJ2W.js +146 -0
- package/dist/chunk-M5DHKW7M.js +127 -0
- package/dist/chunk-MBKCIJHM.js +1328 -0
- package/dist/chunk-MK42FMEG.js +147 -0
- package/dist/chunk-MOBMYHKL.js +1205 -0
- package/dist/chunk-MPLTNMRG.js +67 -0
- package/dist/chunk-NIJCVN3O.js +147 -0
- package/dist/chunk-NX647OM3.js +310 -0
- package/dist/chunk-NZCFSZQ7.js +1205 -0
- package/dist/chunk-O2UITJGH.js +465 -0
- package/dist/chunk-OCRA44AZ.js +108 -0
- package/dist/chunk-P3KJR66H.js +117 -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-PNKVD2UK.js +26 -0
- package/dist/chunk-PSQZURHO.js +229 -0
- package/dist/chunk-SGL6ISBJ.js +1061 -0
- package/dist/chunk-SJABZZT5.js +97 -0
- package/dist/chunk-TD3P3K32.js +1199 -0
- package/dist/chunk-TMDZJJKV.js +288 -0
- package/dist/chunk-UNHVZB5G.js +411 -0
- package/dist/chunk-VAFTWSTE.js +1061 -0
- package/dist/chunk-VNFXFQBB.js +217 -0
- package/dist/chunk-X3GVFKSJ.js +1205 -0
- package/dist/chunk-XZ3S56RQ.js +1061 -0
- package/dist/chunk-Y72C7F6O.js +148 -0
- package/dist/chunk-YLICP577.js +1205 -0
- package/dist/chunk-YX6AXLVK.js +159 -0
- package/dist/chunk-ZCQYHTNU.js +146 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +1160 -0
- package/dist/cloud-crystal.js +6 -0
- package/dist/core.d.ts +252 -0
- package/dist/core.js +12 -0
- package/dist/crypto.d.ts +20 -0
- package/dist/crypto.js +27 -0
- package/dist/crystal-capture.sh +29 -0
- package/dist/crystal-serve.d.ts +4 -0
- package/dist/crystal-serve.js +252 -0
- package/dist/dev-update-SZ2Z4WCQ.js +6 -0
- package/dist/discover.d.ts +30 -0
- package/dist/discover.js +177 -0
- package/dist/doctor.d.ts +9 -0
- package/dist/doctor.js +342 -0
- package/dist/dream-weaver.d.ts +8 -0
- package/dist/dream-weaver.js +56 -0
- package/dist/file-sync.d.ts +48 -0
- package/dist/file-sync.js +18 -0
- package/dist/installer.d.ts +61 -0
- package/dist/installer.js +772 -0
- package/dist/ldm-backup.sh +116 -0
- package/dist/ldm.d.ts +50 -0
- package/dist/ldm.js +32 -0
- package/dist/llm-XXLYPIOF.js +16 -0
- package/dist/mcp-server.d.ts +1 -0
- package/dist/mcp-server.js +277 -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 +159 -0
- package/dist/mlx-setup-XKU67WCT.js +289 -0
- package/dist/oc-backfill.d.ts +19 -0
- package/dist/oc-backfill.js +74 -0
- package/dist/openclaw.d.ts +5 -0
- package/dist/openclaw.js +434 -0
- package/dist/pair.d.ts +4 -0
- package/dist/pair.js +75 -0
- package/dist/poller.d.ts +1 -0
- package/dist/poller.js +634 -0
- package/dist/role.d.ts +24 -0
- package/dist/role.js +13 -0
- package/dist/search-pipeline-4K4OJSSS.js +255 -0
- package/dist/search-pipeline-4PRS6LI7.js +280 -0
- package/dist/search-pipeline-7UJMXPLO.js +280 -0
- package/dist/search-pipeline-CBV25NX7.js +99 -0
- package/dist/search-pipeline-DQTRLGBH.js +74 -0
- package/dist/search-pipeline-HNG37REH.js +282 -0
- package/dist/search-pipeline-IZFPLBUB.js +280 -0
- package/dist/search-pipeline-MID6F26Q.js +73 -0
- package/dist/search-pipeline-N52JZFNN.js +282 -0
- package/dist/search-pipeline-OPB2PRQQ.js +280 -0
- package/dist/search-pipeline-VXTE5HAD.js +262 -0
- package/dist/search-pipeline-XHFKADRG.js +73 -0
- package/dist/staging.d.ts +29 -0
- package/dist/staging.js +21 -0
- package/dist/summarize.d.ts +19 -0
- package/dist/summarize.js +10 -0
- package/dist/worker-demo.js +186 -0
- package/dist/worker-mcp.js +404 -0
- package/dist/worker.js +137 -0
- package/package.json +15 -1
- package/.env.example +0 -20
- package/.publish-skill.json +0 -1
- package/CHANGELOG.md +0 -1372
- package/README-ENTERPRISE.md +0 -226
- package/RELAY.md +0 -199
- package/wrangler-demo.toml +0 -8
- package/wrangler-mcp.toml +0 -24
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ensureLdm,
|
|
3
|
+
init_ldm,
|
|
4
|
+
ldmPaths
|
|
5
|
+
} from "./chunk-PSQZURHO.js";
|
|
6
|
+
|
|
7
|
+
// src/staging.ts
|
|
8
|
+
init_ldm();
|
|
9
|
+
import {
|
|
10
|
+
existsSync,
|
|
11
|
+
mkdirSync,
|
|
12
|
+
readdirSync,
|
|
13
|
+
renameSync,
|
|
14
|
+
unlinkSync,
|
|
15
|
+
writeFileSync
|
|
16
|
+
} from "fs";
|
|
17
|
+
import { join } from "path";
|
|
18
|
+
import { execSync } from "child_process";
|
|
19
|
+
var HOME = process.env.HOME || "";
|
|
20
|
+
var STAGING_ROOT = join(HOME, ".ldm", "staging");
|
|
21
|
+
function stagingPaths(agentId) {
|
|
22
|
+
const root = join(STAGING_ROOT, agentId);
|
|
23
|
+
return {
|
|
24
|
+
root,
|
|
25
|
+
transcripts: join(root, "transcripts"),
|
|
26
|
+
readyFile: join(root, "READY")
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
function ensureStaging(agentId) {
|
|
30
|
+
const paths = stagingPaths(agentId);
|
|
31
|
+
mkdirSync(paths.transcripts, { recursive: true });
|
|
32
|
+
return paths;
|
|
33
|
+
}
|
|
34
|
+
function markReady(agentId) {
|
|
35
|
+
const paths = stagingPaths(agentId);
|
|
36
|
+
writeFileSync(paths.readyFile, JSON.stringify({
|
|
37
|
+
markedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
38
|
+
agentId
|
|
39
|
+
}));
|
|
40
|
+
}
|
|
41
|
+
function isNewAgent(agentId) {
|
|
42
|
+
const paths = ldmPaths(agentId);
|
|
43
|
+
return !existsSync(paths.agentRoot);
|
|
44
|
+
}
|
|
45
|
+
function hasStagedData(agentId) {
|
|
46
|
+
const paths = stagingPaths(agentId);
|
|
47
|
+
return existsSync(paths.readyFile);
|
|
48
|
+
}
|
|
49
|
+
function listStagedAgents() {
|
|
50
|
+
if (!existsSync(STAGING_ROOT)) return [];
|
|
51
|
+
const agents = [];
|
|
52
|
+
for (const entry of readdirSync(STAGING_ROOT, { withFileTypes: true })) {
|
|
53
|
+
if (!entry.isDirectory()) continue;
|
|
54
|
+
if (existsSync(join(STAGING_ROOT, entry.name, "READY"))) {
|
|
55
|
+
agents.push(entry.name);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return agents;
|
|
59
|
+
}
|
|
60
|
+
async function processStagedAgent(agentId) {
|
|
61
|
+
const startTime = Date.now();
|
|
62
|
+
const staging = stagingPaths(agentId);
|
|
63
|
+
if (!existsSync(staging.readyFile)) {
|
|
64
|
+
throw new Error(`No READY file for agent ${agentId}`);
|
|
65
|
+
}
|
|
66
|
+
const agentPaths = ensureLdm(agentId);
|
|
67
|
+
const stagedFiles = existsSync(staging.transcripts) ? readdirSync(staging.transcripts).filter((f) => f.endsWith(".jsonl")) : [];
|
|
68
|
+
if (stagedFiles.length === 0) {
|
|
69
|
+
unlinkSync(staging.readyFile);
|
|
70
|
+
return {
|
|
71
|
+
agentId,
|
|
72
|
+
transcriptsProcessed: 0,
|
|
73
|
+
backfillChunks: 0,
|
|
74
|
+
dreamWeaverRan: false,
|
|
75
|
+
durationMs: Date.now() - startTime
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
for (const file of stagedFiles) {
|
|
79
|
+
const src = join(staging.transcripts, file);
|
|
80
|
+
const dest = join(agentPaths.transcripts, file);
|
|
81
|
+
try {
|
|
82
|
+
renameSync(src, dest);
|
|
83
|
+
} catch {
|
|
84
|
+
const { copyFileSync } = await import("fs");
|
|
85
|
+
copyFileSync(src, dest);
|
|
86
|
+
unlinkSync(src);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
let backfillChunks = 0;
|
|
90
|
+
let dreamWeaverRan = false;
|
|
91
|
+
try {
|
|
92
|
+
const output = execSync(
|
|
93
|
+
`crystal backfill --agent ${agentId}`,
|
|
94
|
+
{ encoding: "utf-8", timeout: 6e5 }
|
|
95
|
+
);
|
|
96
|
+
const match = output.match(/(\d+) chunks embedded/);
|
|
97
|
+
if (match) backfillChunks = parseInt(match[1], 10);
|
|
98
|
+
} catch (err) {
|
|
99
|
+
process.stderr.write(`[staging] backfill failed for ${agentId}: ${err.message}
|
|
100
|
+
`);
|
|
101
|
+
}
|
|
102
|
+
try {
|
|
103
|
+
execSync(
|
|
104
|
+
`crystal dream-weave --agent ${agentId} --mode full`,
|
|
105
|
+
{ encoding: "utf-8", timeout: 6e5 }
|
|
106
|
+
);
|
|
107
|
+
dreamWeaverRan = true;
|
|
108
|
+
} catch (err) {
|
|
109
|
+
process.stderr.write(`[staging] dream-weave failed for ${agentId}: ${err.message}
|
|
110
|
+
`);
|
|
111
|
+
}
|
|
112
|
+
try {
|
|
113
|
+
unlinkSync(staging.readyFile);
|
|
114
|
+
} catch {
|
|
115
|
+
}
|
|
116
|
+
return {
|
|
117
|
+
agentId,
|
|
118
|
+
transcriptsProcessed: stagedFiles.length,
|
|
119
|
+
backfillChunks,
|
|
120
|
+
dreamWeaverRan,
|
|
121
|
+
durationMs: Date.now() - startTime
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
async function processAllStaged() {
|
|
125
|
+
const agents = listStagedAgents();
|
|
126
|
+
const results = [];
|
|
127
|
+
for (const agentId of agents) {
|
|
128
|
+
try {
|
|
129
|
+
const result = await processStagedAgent(agentId);
|
|
130
|
+
results.push(result);
|
|
131
|
+
} catch (err) {
|
|
132
|
+
process.stderr.write(`[staging] failed to process ${agentId}: ${err.message}
|
|
133
|
+
`);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return results;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export {
|
|
140
|
+
stagingPaths,
|
|
141
|
+
ensureStaging,
|
|
142
|
+
markReady,
|
|
143
|
+
isNewAgent,
|
|
144
|
+
hasStagedData,
|
|
145
|
+
listStagedAgents,
|
|
146
|
+
processStagedAgent,
|
|
147
|
+
processAllStaged
|
|
148
|
+
};
|
|
@@ -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
|
+
};
|