@memrosetta/cli 0.4.4 → 0.4.5
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/dist/index.js +6 -2
- package/dist/sync-JSEQ4AM3.js +401 -0
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -56,7 +56,11 @@ Global Options:
|
|
|
56
56
|
--version, -v Show version
|
|
57
57
|
|
|
58
58
|
Sync Subcommands:
|
|
59
|
-
memrosetta sync enable --server <url> [key-source]
|
|
59
|
+
memrosetta sync enable --server <url> [--user <id>] [key-source]
|
|
60
|
+
--user <id> Logical user id shared across devices
|
|
61
|
+
(defaults to OS username). Different
|
|
62
|
+
devices of the same person MUST share
|
|
63
|
+
the same --user to see each other.
|
|
60
64
|
Key sources (mutually exclusive, exactly one):
|
|
61
65
|
--key <value> Direct (visible in shell history)
|
|
62
66
|
--key-stdin Read from stdin (echo key | memrosetta ...)
|
|
@@ -189,7 +193,7 @@ async function main() {
|
|
|
189
193
|
break;
|
|
190
194
|
}
|
|
191
195
|
case "sync": {
|
|
192
|
-
const mod = await import("./sync-
|
|
196
|
+
const mod = await import("./sync-JSEQ4AM3.js");
|
|
193
197
|
await mod.run(commandOptions);
|
|
194
198
|
break;
|
|
195
199
|
}
|
|
@@ -0,0 +1,401 @@
|
|
|
1
|
+
import {
|
|
2
|
+
hasFlag,
|
|
3
|
+
optionalOption,
|
|
4
|
+
requireOption
|
|
5
|
+
} from "./chunk-NU5ZJJXP.js";
|
|
6
|
+
import {
|
|
7
|
+
output,
|
|
8
|
+
outputError
|
|
9
|
+
} from "./chunk-ET6TNQOJ.js";
|
|
10
|
+
import {
|
|
11
|
+
getConfig,
|
|
12
|
+
getDefaultDbPath,
|
|
13
|
+
writeConfig
|
|
14
|
+
} from "./chunk-SEPYQK3J.js";
|
|
15
|
+
|
|
16
|
+
// src/commands/sync.ts
|
|
17
|
+
import { randomUUID } from "crypto";
|
|
18
|
+
import { readFileSync } from "fs";
|
|
19
|
+
import { userInfo, platform } from "os";
|
|
20
|
+
import { createInterface } from "readline";
|
|
21
|
+
var ENV_API_KEY = "MEMROSETTA_SYNC_API_KEY";
|
|
22
|
+
var KEY_SOURCE_HINT = [
|
|
23
|
+
"API key required. Use exactly one of:",
|
|
24
|
+
" --key <value> (direct, visible in history)",
|
|
25
|
+
" --key-stdin (pipe from stdin)",
|
|
26
|
+
" --key-file <path> (read from file)",
|
|
27
|
+
` ${ENV_API_KEY}=<value> (environment variable)`,
|
|
28
|
+
"",
|
|
29
|
+
"On POSIX TTYs an interactive hidden prompt is also available.",
|
|
30
|
+
"See: memrosetta sync enable --help"
|
|
31
|
+
].join("\n");
|
|
32
|
+
function parseSubcommand(args) {
|
|
33
|
+
const first = args[0];
|
|
34
|
+
if (!first || first.startsWith("--")) return null;
|
|
35
|
+
if (first === "enable" || first === "disable" || first === "status" || first === "now" || first === "device-id") {
|
|
36
|
+
return first;
|
|
37
|
+
}
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
var CONTROL_CHAR_REGEX = /[\x00-\x1F\x7F]/;
|
|
41
|
+
function validateApiKey(key, sourceLabel) {
|
|
42
|
+
const trimmed = key.trim();
|
|
43
|
+
if (trimmed.length === 0) {
|
|
44
|
+
throw new Error(`API key from ${sourceLabel} is empty.`);
|
|
45
|
+
}
|
|
46
|
+
if (CONTROL_CHAR_REGEX.test(trimmed)) {
|
|
47
|
+
throw new Error(
|
|
48
|
+
`API key from ${sourceLabel} contains control characters. Try --key-file or MEMROSETTA_SYNC_API_KEY instead.`
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
return trimmed;
|
|
52
|
+
}
|
|
53
|
+
function readKeyFile(path) {
|
|
54
|
+
try {
|
|
55
|
+
return readFileSync(path, "utf-8");
|
|
56
|
+
} catch (err) {
|
|
57
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
58
|
+
throw new Error(`Could not read --key-file '${path}': ${msg}`);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
async function resolveApiKey(args) {
|
|
62
|
+
const directKey = optionalOption(args, "--key");
|
|
63
|
+
const keyFile = optionalOption(args, "--key-file");
|
|
64
|
+
const useStdin = hasFlag(args, "--key-stdin");
|
|
65
|
+
const explicitCount = [directKey !== void 0, keyFile !== void 0, useStdin].filter(Boolean).length;
|
|
66
|
+
if (explicitCount > 1) {
|
|
67
|
+
throw new Error(
|
|
68
|
+
"Specify only one of --key, --key-stdin, --key-file."
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
if (directKey !== void 0) {
|
|
72
|
+
return validateApiKey(directKey, "--key");
|
|
73
|
+
}
|
|
74
|
+
if (useStdin) {
|
|
75
|
+
const raw = await readStdinKey();
|
|
76
|
+
if (!raw) {
|
|
77
|
+
throw new Error(
|
|
78
|
+
"--key-stdin produced no input. On Windows PowerShell, prefer --key-file or MEMROSETTA_SYNC_API_KEY."
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
return validateApiKey(raw, "--key-stdin");
|
|
82
|
+
}
|
|
83
|
+
if (keyFile !== void 0) {
|
|
84
|
+
return validateApiKey(readKeyFile(keyFile), `--key-file ${keyFile}`);
|
|
85
|
+
}
|
|
86
|
+
const envKey = process.env[ENV_API_KEY];
|
|
87
|
+
if (envKey !== void 0 && envKey.length > 0) {
|
|
88
|
+
return validateApiKey(envKey, ENV_API_KEY);
|
|
89
|
+
}
|
|
90
|
+
if (platform() !== "win32" && process.stdin.isTTY) {
|
|
91
|
+
const raw = await readHiddenInput("API key: ");
|
|
92
|
+
return validateApiKey(raw, "hidden prompt");
|
|
93
|
+
}
|
|
94
|
+
throw new Error(KEY_SOURCE_HINT);
|
|
95
|
+
}
|
|
96
|
+
async function readHiddenInput(prompt) {
|
|
97
|
+
const stdin = process.stdin;
|
|
98
|
+
const stdout = process.stdout;
|
|
99
|
+
if (!stdin.isTTY) {
|
|
100
|
+
throw new Error("Interactive input requires a TTY. Use --key-stdin to pipe the key instead.");
|
|
101
|
+
}
|
|
102
|
+
stdout.write(prompt);
|
|
103
|
+
const originalWrite = stdout.write.bind(stdout);
|
|
104
|
+
let muted = true;
|
|
105
|
+
stdout.write = (chunk, ...rest) => {
|
|
106
|
+
if (!muted) {
|
|
107
|
+
return originalWrite(chunk, ...rest);
|
|
108
|
+
}
|
|
109
|
+
const str = typeof chunk === "string" ? chunk : chunk?.toString?.("utf-8") ?? "";
|
|
110
|
+
if (str === "\n" || str === "\r\n" || str === "\r") {
|
|
111
|
+
return originalWrite(chunk, ...rest);
|
|
112
|
+
}
|
|
113
|
+
return true;
|
|
114
|
+
};
|
|
115
|
+
const rl = createInterface({
|
|
116
|
+
input: stdin,
|
|
117
|
+
output: stdout,
|
|
118
|
+
terminal: true
|
|
119
|
+
});
|
|
120
|
+
try {
|
|
121
|
+
const answer = await new Promise((resolve, reject) => {
|
|
122
|
+
rl.once("close", () => {
|
|
123
|
+
reject(new Error("Aborted"));
|
|
124
|
+
});
|
|
125
|
+
rl.question("", (value) => {
|
|
126
|
+
resolve(value);
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
return answer;
|
|
130
|
+
} finally {
|
|
131
|
+
muted = false;
|
|
132
|
+
stdout.write = originalWrite;
|
|
133
|
+
rl.close();
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
async function readStdinKey() {
|
|
137
|
+
const chunks = [];
|
|
138
|
+
for await (const chunk of process.stdin) {
|
|
139
|
+
chunks.push(chunk);
|
|
140
|
+
}
|
|
141
|
+
return Buffer.concat(chunks).toString("utf-8").trim();
|
|
142
|
+
}
|
|
143
|
+
async function testConnection(serverUrl, apiKey) {
|
|
144
|
+
const url = `${serverUrl.replace(/\/$/, "")}/sync/health`;
|
|
145
|
+
try {
|
|
146
|
+
const res = await fetch(url, {
|
|
147
|
+
headers: { Authorization: `Bearer ${apiKey}` }
|
|
148
|
+
});
|
|
149
|
+
if (!res.ok) {
|
|
150
|
+
throw new Error(`HTTP ${res.status} ${res.statusText}`);
|
|
151
|
+
}
|
|
152
|
+
} catch (err) {
|
|
153
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
154
|
+
throw new Error(`Sync server health check failed: ${msg}`);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
async function withSyncClient(dbPath, config, fn) {
|
|
158
|
+
const Database = (await import("better-sqlite3")).default;
|
|
159
|
+
const { SyncClient, ensureSyncSchema } = await import("@memrosetta/sync-client");
|
|
160
|
+
if (!config.syncServerUrl || !config.syncApiKey || !config.syncDeviceId) {
|
|
161
|
+
throw new Error("Sync is not configured. Run: memrosetta sync enable --server <url>");
|
|
162
|
+
}
|
|
163
|
+
if (CONTROL_CHAR_REGEX.test(config.syncApiKey)) {
|
|
164
|
+
throw new Error(
|
|
165
|
+
`Stored API key is invalid (contains control characters from a previous terminal input). Re-run with one of:
|
|
166
|
+
memrosetta sync enable --server ${config.syncServerUrl} --key <api-key>
|
|
167
|
+
memrosetta sync enable --server ${config.syncServerUrl} --key-file path/to/key
|
|
168
|
+
$env:${ENV_API_KEY}='<api-key>'; memrosetta sync enable --server ${config.syncServerUrl}`
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
const db = new Database(dbPath);
|
|
172
|
+
try {
|
|
173
|
+
ensureSyncSchema(db);
|
|
174
|
+
const client = new SyncClient(db, {
|
|
175
|
+
serverUrl: config.syncServerUrl,
|
|
176
|
+
apiKey: config.syncApiKey,
|
|
177
|
+
deviceId: config.syncDeviceId,
|
|
178
|
+
userId: config.syncUserId ?? userInfo().username
|
|
179
|
+
});
|
|
180
|
+
return await fn(client, db);
|
|
181
|
+
} finally {
|
|
182
|
+
db.close();
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
async function runEnable(options) {
|
|
186
|
+
const { args, format } = options;
|
|
187
|
+
let serverUrl;
|
|
188
|
+
try {
|
|
189
|
+
serverUrl = requireOption(args, "--server", "server URL");
|
|
190
|
+
} catch (err) {
|
|
191
|
+
outputError(err instanceof Error ? err.message : String(err), format);
|
|
192
|
+
process.exitCode = 1;
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
let apiKey;
|
|
196
|
+
try {
|
|
197
|
+
apiKey = await resolveApiKey(args);
|
|
198
|
+
} catch (err) {
|
|
199
|
+
outputError(err instanceof Error ? err.message : String(err), format);
|
|
200
|
+
process.exitCode = 1;
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
const skipTest = hasFlag(args, "--no-test");
|
|
204
|
+
if (!skipTest) {
|
|
205
|
+
try {
|
|
206
|
+
await testConnection(serverUrl, apiKey);
|
|
207
|
+
} catch (err) {
|
|
208
|
+
outputError(
|
|
209
|
+
`${err instanceof Error ? err.message : String(err)}
|
|
210
|
+
Use --no-test to skip the health check.`,
|
|
211
|
+
format
|
|
212
|
+
);
|
|
213
|
+
process.exitCode = 1;
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
const existing = getConfig();
|
|
218
|
+
const deviceId = existing.syncDeviceId ?? `device-${randomUUID().slice(0, 8)}`;
|
|
219
|
+
const userOverride = optionalOption(args, "--user");
|
|
220
|
+
const syncUserId = userOverride ?? existing.syncUserId ?? userInfo().username;
|
|
221
|
+
writeConfig({
|
|
222
|
+
...existing,
|
|
223
|
+
syncEnabled: true,
|
|
224
|
+
syncServerUrl: serverUrl,
|
|
225
|
+
syncApiKey: apiKey,
|
|
226
|
+
syncDeviceId: deviceId,
|
|
227
|
+
syncUserId
|
|
228
|
+
});
|
|
229
|
+
if (format === "text") {
|
|
230
|
+
process.stdout.write("Sync enabled.\n");
|
|
231
|
+
process.stdout.write(` Server: ${serverUrl}
|
|
232
|
+
`);
|
|
233
|
+
process.stdout.write(` UserId: ${syncUserId}
|
|
234
|
+
`);
|
|
235
|
+
process.stdout.write(` DeviceId: ${deviceId}
|
|
236
|
+
`);
|
|
237
|
+
if (skipTest) {
|
|
238
|
+
process.stdout.write(" (health check skipped)\n");
|
|
239
|
+
}
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
output(
|
|
243
|
+
{ enabled: true, serverUrl, userId: syncUserId, deviceId, healthCheckSkipped: skipTest },
|
|
244
|
+
format
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
function runDisable(options) {
|
|
248
|
+
const { format } = options;
|
|
249
|
+
const existing = getConfig();
|
|
250
|
+
writeConfig({
|
|
251
|
+
...existing,
|
|
252
|
+
syncEnabled: false
|
|
253
|
+
});
|
|
254
|
+
if (format === "text") {
|
|
255
|
+
process.stdout.write("Sync disabled. (server URL and API key preserved for re-enable)\n");
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
output({ enabled: false }, format);
|
|
259
|
+
}
|
|
260
|
+
async function runStatus(options) {
|
|
261
|
+
const { format, db } = options;
|
|
262
|
+
const config = getConfig();
|
|
263
|
+
const dbPath = db ?? config.dbPath ?? getDefaultDbPath();
|
|
264
|
+
if (!config.syncEnabled) {
|
|
265
|
+
if (format === "text") {
|
|
266
|
+
process.stdout.write("Sync: disabled\n");
|
|
267
|
+
if (config.syncServerUrl) {
|
|
268
|
+
process.stdout.write(` Server: ${config.syncServerUrl}
|
|
269
|
+
`);
|
|
270
|
+
}
|
|
271
|
+
if (config.syncUserId) {
|
|
272
|
+
process.stdout.write(` UserId: ${config.syncUserId}
|
|
273
|
+
`);
|
|
274
|
+
}
|
|
275
|
+
if (config.syncDeviceId) {
|
|
276
|
+
process.stdout.write(` DeviceId: ${config.syncDeviceId}
|
|
277
|
+
`);
|
|
278
|
+
}
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
output(
|
|
282
|
+
{
|
|
283
|
+
enabled: false,
|
|
284
|
+
serverUrl: config.syncServerUrl ?? null,
|
|
285
|
+
userId: config.syncUserId ?? null,
|
|
286
|
+
deviceId: config.syncDeviceId ?? null
|
|
287
|
+
},
|
|
288
|
+
format
|
|
289
|
+
);
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
try {
|
|
293
|
+
const status = await withSyncClient(dbPath, config, async (client) => client.getStatus());
|
|
294
|
+
if (format === "text") {
|
|
295
|
+
process.stdout.write("Sync: enabled\n");
|
|
296
|
+
process.stdout.write(` Server: ${status.serverUrl}
|
|
297
|
+
`);
|
|
298
|
+
process.stdout.write(` UserId: ${status.userId}
|
|
299
|
+
`);
|
|
300
|
+
process.stdout.write(` DeviceId: ${status.deviceId}
|
|
301
|
+
`);
|
|
302
|
+
process.stdout.write(` Pending ops: ${status.pendingOps}
|
|
303
|
+
`);
|
|
304
|
+
process.stdout.write(` Current cursor: ${status.cursor}
|
|
305
|
+
`);
|
|
306
|
+
process.stdout.write(
|
|
307
|
+
` Last push: ${status.lastPush.successAt ?? "never"}` + (status.lastPush.attemptAt && status.lastPush.attemptAt !== status.lastPush.successAt ? ` (last attempt: ${status.lastPush.attemptAt})` : "") + "\n"
|
|
308
|
+
);
|
|
309
|
+
process.stdout.write(
|
|
310
|
+
` Last pull: ${status.lastPull.successAt ?? "never"}` + (status.lastPull.attemptAt && status.lastPull.attemptAt !== status.lastPull.successAt ? ` (last attempt: ${status.lastPull.attemptAt})` : "") + "\n"
|
|
311
|
+
);
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
output(status, format);
|
|
315
|
+
} catch (err) {
|
|
316
|
+
outputError(err instanceof Error ? err.message : String(err), format);
|
|
317
|
+
process.exitCode = 1;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
async function runNow(options) {
|
|
321
|
+
const { args, format, db } = options;
|
|
322
|
+
const config = getConfig();
|
|
323
|
+
const dbPath = db ?? config.dbPath ?? getDefaultDbPath();
|
|
324
|
+
if (!config.syncEnabled) {
|
|
325
|
+
outputError("Sync is disabled. Run: memrosetta sync enable --server <url>", format);
|
|
326
|
+
process.exitCode = 1;
|
|
327
|
+
return;
|
|
328
|
+
}
|
|
329
|
+
const pushOnly = hasFlag(args, "--push-only");
|
|
330
|
+
const pullOnly = hasFlag(args, "--pull-only");
|
|
331
|
+
try {
|
|
332
|
+
const result = await withSyncClient(dbPath, config, async (client) => {
|
|
333
|
+
let pushed = 0;
|
|
334
|
+
let pulled = 0;
|
|
335
|
+
if (!pullOnly) {
|
|
336
|
+
const pushResult = await client.push();
|
|
337
|
+
pushed = pushResult.pushed;
|
|
338
|
+
}
|
|
339
|
+
if (!pushOnly) {
|
|
340
|
+
pulled = await client.pull();
|
|
341
|
+
}
|
|
342
|
+
return { pushed, pulled };
|
|
343
|
+
});
|
|
344
|
+
if (format === "text") {
|
|
345
|
+
process.stdout.write(`Sync complete. pushed=${result.pushed} pulled=${result.pulled}
|
|
346
|
+
`);
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
349
|
+
output(result, format);
|
|
350
|
+
} catch (err) {
|
|
351
|
+
outputError(err instanceof Error ? err.message : String(err), format);
|
|
352
|
+
process.exitCode = 1;
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
function runDeviceId(options) {
|
|
356
|
+
const { format } = options;
|
|
357
|
+
const config = getConfig();
|
|
358
|
+
if (!config.syncDeviceId) {
|
|
359
|
+
outputError("No deviceId set. Run: memrosetta sync enable --server <url>", format);
|
|
360
|
+
process.exitCode = 1;
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
363
|
+
if (format === "text") {
|
|
364
|
+
process.stdout.write(`${config.syncDeviceId}
|
|
365
|
+
`);
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
output({ deviceId: config.syncDeviceId }, format);
|
|
369
|
+
}
|
|
370
|
+
async function run(options) {
|
|
371
|
+
const sub = parseSubcommand(options.args);
|
|
372
|
+
if (!sub) {
|
|
373
|
+
outputError(
|
|
374
|
+
"Usage: memrosetta sync <enable|disable|status|now|device-id>\n\n enable --server <url> [--key <key> | --key-stdin] [--no-test]\n disable\n status\n now [--push-only | --pull-only]\n device-id\n",
|
|
375
|
+
options.format
|
|
376
|
+
);
|
|
377
|
+
process.exitCode = 1;
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
const rest = { ...options, args: options.args.slice(1) };
|
|
381
|
+
switch (sub) {
|
|
382
|
+
case "enable":
|
|
383
|
+
await runEnable(rest);
|
|
384
|
+
return;
|
|
385
|
+
case "disable":
|
|
386
|
+
runDisable(rest);
|
|
387
|
+
return;
|
|
388
|
+
case "status":
|
|
389
|
+
await runStatus(rest);
|
|
390
|
+
return;
|
|
391
|
+
case "now":
|
|
392
|
+
await runNow(rest);
|
|
393
|
+
return;
|
|
394
|
+
case "device-id":
|
|
395
|
+
runDeviceId(rest);
|
|
396
|
+
return;
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
export {
|
|
400
|
+
run
|
|
401
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@memrosetta/cli",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.5",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"bin": {
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"better-sqlite3": "^11.0.0",
|
|
18
18
|
"@memrosetta/core": "0.4.0",
|
|
19
19
|
"@memrosetta/embeddings": "0.3.0",
|
|
20
|
-
"@memrosetta/sync-client": "0.1.
|
|
20
|
+
"@memrosetta/sync-client": "0.1.2",
|
|
21
21
|
"@memrosetta/types": "0.4.0"
|
|
22
22
|
},
|
|
23
23
|
"optionalDependencies": {
|