@wirecat/cli-core 0.703.0 → 0.704.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/CHANGELOG.md +49 -0
- package/dist/background/daily-archive-timer.d.ts +1 -1
- package/dist/documents/file-mirror-model.d.ts +88 -0
- package/dist/documents/file-mirror-model.js +1 -0
- package/dist/documents/index.d.ts +5 -2
- package/dist/documents/index.js +2 -0
- package/dist/documents/push-model.d.ts +3 -0
- package/dist/embeddings/onnx-runtime.d.ts +1 -1
- package/dist/embeddings/onnx-runtime.js +42 -61
- package/dist/errors/reasons.js +8 -0
- package/dist/ports/wirecat-tools.js +7 -0
- package/dist/sends/upload.js +0 -1
- package/dist/services/documents/mirror-watch.d.ts +12 -4
- package/dist/services/documents/mirror-watch.js +2 -0
- package/dist/services/documents/mirrors.d.ts +55 -35
- package/dist/services/documents/mirrors.js +63 -66
- package/dist/services/documents/push.d.ts +56 -4
- package/dist/services/documents/push.js +131 -24
- package/dist/services/files/mirror.d.ts +145 -0
- package/dist/services/files/mirror.js +603 -0
- package/dist/services/files/versions.d.ts +50 -0
- package/dist/services/files/versions.js +142 -0
- package/dist/services/metadata.d.ts +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
+
import { appendFileSync, createReadStream, createWriteStream, existsSync, mkdirSync, readFileSync } from "node:fs";
|
|
3
|
+
import { readdir, rename, rm, stat, utimes } from "node:fs/promises";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { Transform } from "node:stream";
|
|
6
|
+
import { pipeline } from "node:stream/promises";
|
|
7
|
+
import { CliError } from "@wirecat/cli-base";
|
|
8
|
+
import { assertNoLink, replaceLocal, safePath } from "../documents/push.js";
|
|
9
|
+
const RETAIN_MS = 30 * 24 * 60 * 60 * 1000;
|
|
10
|
+
/**
|
|
11
|
+
* A mirror's local version store: `<dir>/objects/<sha256>` holds each kept content once, `<dir>/log.jsonl` is
|
|
12
|
+
* append-only. The store lives under the profile's state folder, never inside the mirrored folder. Retention, one
|
|
13
|
+
* rule: a content stays while an event newer than 30 days names it, and the last content of a path whose newest
|
|
14
|
+
* event is a deletion stays for good. A content no event names stays 30 days after it was last kept, so a crash
|
|
15
|
+
* between keeping a file and logging the change loses nothing.
|
|
16
|
+
*/
|
|
17
|
+
export const fileVersions = (dir, now = Date.now) => {
|
|
18
|
+
const objects = join(dir, "objects");
|
|
19
|
+
const logPath = join(dir, "log.jsonl");
|
|
20
|
+
const events = () => {
|
|
21
|
+
try {
|
|
22
|
+
return readFileSync(logPath, "utf8")
|
|
23
|
+
.split("\n")
|
|
24
|
+
.filter(Boolean)
|
|
25
|
+
.map((line) => JSON.parse(line));
|
|
26
|
+
}
|
|
27
|
+
catch (error) {
|
|
28
|
+
if (error.code === "ENOENT")
|
|
29
|
+
return [];
|
|
30
|
+
throw error;
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
const has = (sha256) => existsSync(join(objects, sha256));
|
|
34
|
+
return {
|
|
35
|
+
keep: async (absolute) => {
|
|
36
|
+
mkdirSync(objects, { recursive: true, mode: 0o700 });
|
|
37
|
+
const temp = join(objects, `.${randomUUID()}.tmp`);
|
|
38
|
+
const hash = createHash("sha256");
|
|
39
|
+
try {
|
|
40
|
+
// Hashed while copied: the file may change under the sync, and the store is keyed by what it really holds.
|
|
41
|
+
await pipeline(createReadStream(absolute), new Transform({
|
|
42
|
+
transform(chunk, _encoding, done) {
|
|
43
|
+
hash.update(chunk);
|
|
44
|
+
done(null, chunk);
|
|
45
|
+
},
|
|
46
|
+
}), createWriteStream(temp, { flags: "wx", mode: 0o600 }));
|
|
47
|
+
const sha256 = hash.digest("hex");
|
|
48
|
+
const object = join(objects, sha256);
|
|
49
|
+
if (!has(sha256))
|
|
50
|
+
await rename(temp, object);
|
|
51
|
+
const at = now() / 1000;
|
|
52
|
+
await utimes(object, at, at);
|
|
53
|
+
return sha256;
|
|
54
|
+
}
|
|
55
|
+
finally {
|
|
56
|
+
await rm(temp, { force: true });
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
log: (event) => {
|
|
60
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
61
|
+
appendFileSync(logPath, `${JSON.stringify({ at: new Date(now()).toISOString(), ...event })}\n`, { mode: 0o600 });
|
|
62
|
+
},
|
|
63
|
+
list: (path) => {
|
|
64
|
+
const found = events().filter((event) => event.path === path || event.to === path);
|
|
65
|
+
return found.map((event) => ({
|
|
66
|
+
...event,
|
|
67
|
+
available: event.old !== undefined && existsSync(join(objects, event.old)),
|
|
68
|
+
}));
|
|
69
|
+
},
|
|
70
|
+
write: async (sha256, into, over) => {
|
|
71
|
+
if (!has(sha256))
|
|
72
|
+
return false;
|
|
73
|
+
const copy = (temp) => pipeline(createReadStream(join(objects, sha256)), createWriteStream(temp, { flags: "wx" }));
|
|
74
|
+
return (await replaceLocal(into, over, copy, { sha256 })) !== undefined;
|
|
75
|
+
},
|
|
76
|
+
prune: async () => {
|
|
77
|
+
const all = events();
|
|
78
|
+
const keep = new Set();
|
|
79
|
+
const last = new Map();
|
|
80
|
+
for (const event of all) {
|
|
81
|
+
last.set(event.path, event);
|
|
82
|
+
if (Date.parse(event.at) > now() - RETAIN_MS)
|
|
83
|
+
for (const hash of [event.old, event.new])
|
|
84
|
+
if (hash)
|
|
85
|
+
keep.add(hash);
|
|
86
|
+
}
|
|
87
|
+
for (const event of last.values())
|
|
88
|
+
if (event.event === "deleted" && event.old)
|
|
89
|
+
keep.add(event.old);
|
|
90
|
+
let names;
|
|
91
|
+
try {
|
|
92
|
+
names = await readdir(objects);
|
|
93
|
+
}
|
|
94
|
+
catch {
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
for (const name of names) {
|
|
98
|
+
if (name.startsWith(".") || keep.has(name))
|
|
99
|
+
continue;
|
|
100
|
+
const object = join(objects, name);
|
|
101
|
+
const kept = await stat(object).catch((error) => {
|
|
102
|
+
if (error.code === "ENOENT")
|
|
103
|
+
return undefined;
|
|
104
|
+
throw error;
|
|
105
|
+
});
|
|
106
|
+
if (kept && kept.mtimeMs <= now() - RETAIN_MS)
|
|
107
|
+
await rm(object, { force: true });
|
|
108
|
+
}
|
|
109
|
+
},
|
|
110
|
+
};
|
|
111
|
+
};
|
|
112
|
+
/**
|
|
113
|
+
* Puts a kept version of `path` back into the mirrored folder; the file there now is kept first. The next sync sends
|
|
114
|
+
* it like any local change. False when the store no longer has the version or the file changed meanwhile. Runs under
|
|
115
|
+
* the mirrors' sync lock, so a sync never reads the folder halfway through.
|
|
116
|
+
*/
|
|
117
|
+
export const restoreFileVersion = async (root, path, sha256, versions, lock) => {
|
|
118
|
+
if (!safePath(path))
|
|
119
|
+
throw new CliError("validation_error", `${path} is not a path inside the mirror`);
|
|
120
|
+
const held = await lock();
|
|
121
|
+
try {
|
|
122
|
+
await assertNoLink(root, path);
|
|
123
|
+
const absolute = join(root, path);
|
|
124
|
+
const current = existsSync(absolute) ? await versions.keep(absolute) : undefined;
|
|
125
|
+
if (current === sha256)
|
|
126
|
+
return true;
|
|
127
|
+
held.assertHeld();
|
|
128
|
+
if (!(await versions.write(sha256, absolute, current)))
|
|
129
|
+
return false;
|
|
130
|
+
versions.log({
|
|
131
|
+
path,
|
|
132
|
+
event: current ? "changed" : "created",
|
|
133
|
+
direction: "restored",
|
|
134
|
+
...(current ? { old: current } : {}),
|
|
135
|
+
new: sha256,
|
|
136
|
+
});
|
|
137
|
+
return true;
|
|
138
|
+
}
|
|
139
|
+
finally {
|
|
140
|
+
await held.release();
|
|
141
|
+
}
|
|
142
|
+
};
|
|
@@ -14,12 +14,12 @@ export declare const metadataService: (deps: ServiceDeps) => {
|
|
|
14
14
|
dryRun?: boolean;
|
|
15
15
|
}) => Promise<{
|
|
16
16
|
items: ({
|
|
17
|
-
error?: undefined;
|
|
18
17
|
chatId: string;
|
|
19
18
|
tags: import("../domain/channel-tags.js").ChannelTagMatch[];
|
|
20
19
|
algorithm: string;
|
|
21
20
|
fetchedAt: string | null;
|
|
22
21
|
source: string;
|
|
22
|
+
error?: undefined;
|
|
23
23
|
} | {
|
|
24
24
|
tags?: undefined;
|
|
25
25
|
algorithm?: undefined;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wirecat/cli-core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.704.0",
|
|
4
4
|
"description": "The messenger-neutral half of a messaging CLI: domain model, message locators, rendering, name resolution, the SQLite seam, the command skeleton and shared read commands, the send guard, run records and the message store",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "Viacheslav Ptsarev",
|