@celljs/ta-sync 4.24.0 → 4.25.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/host/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  // web-src/host.ts
2
- import { readFile as readFile7 } from "node:fs/promises";
3
- import { join as join9 } from "node:path";
2
+ import { readFile as readFile8 } from "node:fs/promises";
3
+ import { join as join10 } from "node:path";
4
4
 
5
5
  // web-src/migrate-protocol.ts
6
6
  var MIGRATE_CATEGORIES = [
@@ -677,6 +677,7 @@ var SyncCodec;
677
677
  import { mkdir, readdir as readdir2, readFile as readFile2, rename, rm, stat as stat2, writeFile } from "node:fs/promises";
678
678
  import { dirname as dirname2, join as join2, relative, sep } from "node:path";
679
679
  var SETTINGS = "settings.json";
680
+ var SECRET = "secret:";
680
681
  var SYNC_FILES = {
681
682
  "keybindings.json": "settings",
682
683
  "AGENTS.md": "instructions",
@@ -719,6 +720,9 @@ var SyncItems;
719
720
  if (id.startsWith("data:")) {
720
721
  return "data";
721
722
  }
723
+ if (id.startsWith(SECRET)) {
724
+ return "secrets";
725
+ }
722
726
  if (id.startsWith("file:")) {
723
727
  return SYNC_FILES[id.slice(5)];
724
728
  }
@@ -743,6 +747,10 @@ var SyncItems;
743
747
  return Array.isArray(settings.packages) ? settings.packages.filter((entry) => source(entry) !== undefined) : [];
744
748
  }
745
749
  SyncItems.portablePackages = portablePackages;
750
+ function secretId(name) {
751
+ return SECRET + name;
752
+ }
753
+ SyncItems.secretId = secretId;
746
754
  function dataId(name, key) {
747
755
  return `data:${name}:${key}`;
748
756
  }
@@ -843,6 +851,12 @@ class AgentDirPlace {
843
851
  }
844
852
  }
845
853
  }
854
+ if (resources.has("secrets") && this.side.secrets?.available()) {
855
+ for (const secret of await this.side.secrets.synced() ?? []) {
856
+ const value = { value: secret.value, about: secret.about };
857
+ add(SyncItems.item(SyncItems.secretId(secret.name), "json", SyncCodec.json(value), 0, value));
858
+ }
859
+ }
846
860
  if (resources.has("data")) {
847
861
  const keys = await this.side.dataKeys();
848
862
  this.tracked = new Set(keys.map(({ name, key }) => SyncItems.dataId(name, key)));
@@ -857,7 +871,14 @@ class AgentDirPlace {
857
871
  }
858
872
  return items;
859
873
  }
874
+ reaches(resource) {
875
+ return resource !== "secrets" || !!this.side.secrets?.available();
876
+ }
860
877
  async apply(id, type, content, aside) {
878
+ if (id.startsWith(SECRET)) {
879
+ await this.applySecret(id.slice(SECRET.length), content);
880
+ return;
881
+ }
861
882
  if (aside) {
862
883
  await this.asideOf(id, type);
863
884
  }
@@ -899,6 +920,9 @@ class AgentDirPlace {
899
920
  }
900
921
  }
901
922
  async keep(id, type, content, from) {
923
+ if (id.startsWith(SECRET)) {
924
+ return;
925
+ }
902
926
  const target = join2(this.folder(), `from-${from.replace(/[^\w.-]/g, "_")}`, this.asidePath(id));
903
927
  if (type === "tree") {
904
928
  await this.writeTree(target, SyncCodec.unpack(content));
@@ -940,6 +964,9 @@ class AgentDirPlace {
940
964
  await note(join2(this.agentDir, dir), true);
941
965
  }
942
966
  }
967
+ if (resources.has("conversations")) {
968
+ await note(join2(this.agentDir, "sessions"), true);
969
+ }
943
970
  if (resources.has("identity")) {
944
971
  for (const name of IDENTITY_FILES) {
945
972
  await note(join2(this.agentDir, name), false);
@@ -1031,6 +1058,20 @@ class AgentDirPlace {
1031
1058
  await this.writeFile(target, file.bytes);
1032
1059
  }
1033
1060
  }
1061
+ async applySecret(name, content) {
1062
+ const secrets = this.side.secrets;
1063
+ if (!secrets?.available()) {
1064
+ throw new Error("The vault is off on this computer.");
1065
+ }
1066
+ const parsed = content ? SyncCodec.parse(content) : undefined;
1067
+ const value = SyncMerge.isObject(parsed) && typeof parsed.value === "string" ? parsed.value : undefined;
1068
+ const about = SyncMerge.isObject(parsed) && SyncMerge.isObject(parsed.about) ? parsed.about : {};
1069
+ if (content && value === undefined) {
1070
+ throw new Error("A secret of the sync holds no value.");
1071
+ }
1072
+ const secret = value === undefined ? null : { name, value, about };
1073
+ await secrets.receive(name, secret);
1074
+ }
1034
1075
  async applyPackages(value) {
1035
1076
  const wanted = Array.isArray(value) ? value.map((entry) => SyncItems.source(entry)).filter((entry) => !!entry) : [];
1036
1077
  const file = await readText(join2(this.agentDir, SETTINGS));
@@ -1747,6 +1788,24 @@ var SyncServerTexts;
1747
1788
  });
1748
1789
  }
1749
1790
  SyncServerTexts.folderNewer = folderNewer;
1791
+ function placeLocked() {
1792
+ return TaLanguageUtil.say({
1793
+ "zh-CN": "同步正在进行:先停止同步,再换地方。",
1794
+ "zh-TW": "同步正在進行:先停止同步,再換地方。",
1795
+ "en-US": "Sync is on: stop it first, then choose another place.",
1796
+ "ja-JP": "同期中です。先に同期を停止してから場所を変えてください。"
1797
+ });
1798
+ }
1799
+ SyncServerTexts.placeLocked = placeLocked;
1800
+ function placeInUse() {
1801
+ return TaLanguageUtil.say({
1802
+ "zh-CN": "它正在使用,不能移除。",
1803
+ "zh-TW": "它正在使用,不能移除。",
1804
+ "en-US": "It is in use: it cannot be removed.",
1805
+ "ja-JP": "使用中のため削除できません。"
1806
+ });
1807
+ }
1808
+ SyncServerTexts.placeInUse = placeInUse;
1750
1809
  function official() {
1751
1810
  return TaLanguageUtil.say({
1752
1811
  "zh-CN": "官方中继",
@@ -2466,7 +2525,17 @@ class SyncAccount {
2466
2525
  var SYNC_KIND = "ta-sync";
2467
2526
  var SYNC_PACKAGE = "@celljs/ta-sync";
2468
2527
  var SYNC_VIEW = `${SYNC_PACKAGE}/devices`;
2469
- var SYNC_RESOURCES = ["settings", "instructions", "skills", "models", "extensions", "data", "identity"];
2528
+ var SYNC_RESOURCES = [
2529
+ "settings",
2530
+ "instructions",
2531
+ "skills",
2532
+ "models",
2533
+ "extensions",
2534
+ "data",
2535
+ "conversations",
2536
+ "secrets",
2537
+ "identity"
2538
+ ];
2470
2539
  var DEFAULT_RESOURCES = {
2471
2540
  settings: true,
2472
2541
  instructions: true,
@@ -2474,8 +2543,21 @@ var DEFAULT_RESOURCES = {
2474
2543
  models: false,
2475
2544
  extensions: true,
2476
2545
  data: true,
2546
+ conversations: true,
2547
+ secrets: true,
2477
2548
  identity: false
2478
2549
  };
2550
+ var SyncWhere;
2551
+ ((SyncWhere) => {
2552
+ function conversations(where, store) {
2553
+ return where === "folder" || where === "store" && store !== "git";
2554
+ }
2555
+ SyncWhere.conversations = conversations;
2556
+ function of(service) {
2557
+ return service.type === "ta-relay" ? { where: "relay" } : service.type === "folder" ? { where: "folder" } : { where: "store", store: service.type };
2558
+ }
2559
+ SyncWhere.of = of;
2560
+ })(SyncWhere ||= {});
2479
2561
  var SyncKeys;
2480
2562
  ((SyncKeys) => {
2481
2563
  SyncKeys.RESOURCES = "resources";
@@ -2515,7 +2597,7 @@ var SyncDeclarations;
2515
2597
  })(SyncDeclarations ||= {});
2516
2598
 
2517
2599
  // web-src/sync-service.ts
2518
- import { rm as rm4 } from "node:fs/promises";
2600
+ import { rm as rm5 } from "node:fs/promises";
2519
2601
 
2520
2602
  // ../ta-relay/esm/node/relays-file.js
2521
2603
  import { accessSync, chmodSync, constants, mkdirSync, readdirSync, readFileSync, renameSync, statSync, writeFileSync } from "node:fs";
@@ -2555,6 +2637,8 @@ var RelayCloseCode;
2555
2637
  RelayCloseCode2[RelayCloseCode2["CLOSED_BY_HOST"] = 4410] = "CLOSED_BY_HOST";
2556
2638
  RelayCloseCode2[RelayCloseCode2["FULL"] = 4429] = "FULL";
2557
2639
  RelayCloseCode2[RelayCloseCode2["TIMEOUT"] = 4408] = "TIMEOUT";
2640
+ RelayCloseCode2[RelayCloseCode2["BLOCKED"] = 4451] = "BLOCKED";
2641
+ RelayCloseCode2[RelayCloseCode2["SIGN_IN"] = 4452] = "SIGN_IN";
2558
2642
  })(RelayCloseCode || (RelayCloseCode = {}));
2559
2643
  var RELAY_HUB_MAX_MEMBERS = 101;
2560
2644
  var RELAY_HUB_MAX_VALUE = 96 * 1024;
@@ -2584,10 +2668,22 @@ var RELAY_LEGACY_INFO = {
2584
2668
  };
2585
2669
  var RelayInfoUtil;
2586
2670
  (function(RelayInfoUtil2) {
2587
- function of(persistent, media2) {
2588
- return { ...RELAY_LEGACY_INFO, persistent, ...media2 ? { media: media2 } : {} };
2671
+ function of(persistent, media2, content) {
2672
+ return { ...RELAY_LEGACY_INFO, persistent, ...media2 ? { media: media2 } : {}, ...content ? { content } : {} };
2589
2673
  }
2590
2674
  RelayInfoUtil2.of = of;
2675
+ function origin(value) {
2676
+ if (typeof value !== "string" || !value.trim()) {
2677
+ return;
2678
+ }
2679
+ try {
2680
+ const url = new URL(value.trim());
2681
+ return /^https?:$/.test(url.protocol) && !url.username && !url.password ? url.origin : undefined;
2682
+ } catch (_a) {
2683
+ return;
2684
+ }
2685
+ }
2686
+ RelayInfoUtil2.origin = origin;
2591
2687
  function media(days, megabytes) {
2592
2688
  const set = days !== undefined && days !== null && String(days).trim() !== "";
2593
2689
  const kept = Math.min(Math.floor(set ? Number(days) || 0 : RELAY_MEDIA_DEFAULT_DAYS), RELAY_MEDIA_MAX.days);
@@ -2653,12 +2749,16 @@ var RelayInfoUtil;
2653
2749
  }
2654
2750
  }
2655
2751
  const kept = info.media && media(info.media.days, (Number(info.media.bytes) || 0) / (1024 * 1024));
2752
+ const content = origin(info.content);
2753
+ const needs = Array.isArray(info.needsAccount) ? info.needsAccount.filter((each) => typeof each === "string").slice(0, 8) : [];
2656
2754
  return {
2657
2755
  v: 1,
2658
2756
  capabilities: info.capabilities.filter((capability) => known.includes(capability)),
2659
2757
  limits,
2660
2758
  persistent: info.persistent !== false,
2661
- ...kept ? { media: kept } : {}
2759
+ ...kept ? { media: kept } : {},
2760
+ ...content ? { content } : {},
2761
+ ...needs.length ? { needsAccount: needs } : {}
2662
2762
  };
2663
2763
  }
2664
2764
  RelayInfoUtil2.parse = parse;
@@ -3028,8 +3128,10 @@ import { createCipheriv as createCipheriv2, createDecipheriv as createDecipheriv
3028
3128
  import { watch } from "node:fs";
3029
3129
  import { mkdir as mkdir4, readdir as readdir5, readFile as readFile4, rename as rename4, rm as rm3, stat as stat5, writeFile as writeFile3 } from "node:fs/promises";
3030
3130
  import { join as join6, sep as sep3 } from "node:path";
3131
+ import { gunzipSync as gunzipSync2, gzipSync as gzipSync2 } from "node:zlib";
3031
3132
  var CHECK = "ta-sync folder v1";
3032
3133
  var DIR = "ta-sync";
3134
+ var CONVERSATIONS = "conversations";
3033
3135
  var FOLDER_PIECE = 4 * 1024 * 1024;
3034
3136
  var FolderSyncUtil;
3035
3137
  ((FolderSyncUtil) => {
@@ -3240,14 +3342,16 @@ class StoreSpace {
3240
3342
  }
3241
3343
  return devices;
3242
3344
  }
3243
- async signature() {
3345
+ async signature(conversations = false) {
3244
3346
  const parts = [];
3245
3347
  for (const device of await this.dirs(`${this.root}/devices`)) {
3246
3348
  if (device === this.record.device) {
3247
3349
  continue;
3248
3350
  }
3249
- for (const file of await this.store.list(`${this.root}/devices/${device}/items`)) {
3250
- parts.push(`${device}/${file.name}:${file.tag}`);
3351
+ for (const dir of ["items", ...conversations ? [CONVERSATIONS] : []]) {
3352
+ for (const file of await this.store.list(`${this.root}/devices/${device}/${dir}`)) {
3353
+ parts.push(`${device}/${dir}/${file.name}:${file.tag}`);
3354
+ }
3251
3355
  }
3252
3356
  }
3253
3357
  return parts.join(`
@@ -3260,12 +3364,64 @@ class StoreSpace {
3260
3364
  };
3261
3365
  }
3262
3366
  return this.store.watch((path) => {
3263
- if (path !== undefined && (!path.startsWith(`${this.root}/`) || path.includes(`devices/${this.record.device}/`) || path.split("/").pop()?.startsWith("."))) {
3367
+ if (path !== undefined && (!path.startsWith(`${this.root}/`) || path.includes(`devices/${this.record.device}/`) || path.startsWith(`${this.root}/${CONVERSATIONS}/`) || path.split("/").pop()?.startsWith("."))) {
3264
3368
  return;
3265
3369
  }
3266
3370
  listener();
3267
3371
  });
3268
3372
  }
3373
+ async conversations() {
3374
+ const latest = new Map;
3375
+ for (const device of await this.dirs(`${this.root}/devices`)) {
3376
+ const dir = `${this.root}/devices/${device}/${CONVERSATIONS}`;
3377
+ for (const file of await this.store.list(dir)) {
3378
+ if (file.dir) {
3379
+ continue;
3380
+ }
3381
+ const sealed = await this.cached(`${dir}/${file.name}`, file.tag, file.name);
3382
+ if (!sealed || typeof sealed.key !== "string" || typeof sealed.v !== "number" || this.name(`conversation:${sealed.key}`) !== file.name) {
3383
+ continue;
3384
+ }
3385
+ const before = latest.get(sealed.key);
3386
+ if (!before || sealed.v > before.v || sealed.v === before.v && device > before.device) {
3387
+ latest.set(sealed.key, { entry: sealed.value, v: sealed.v, device });
3388
+ }
3389
+ }
3390
+ }
3391
+ return latest;
3392
+ }
3393
+ async putConversation(file, entry, version) {
3394
+ const current = (await this.conversations()).get(file);
3395
+ if ((current?.v ?? 0) !== version) {
3396
+ return false;
3397
+ }
3398
+ const name = this.name(`conversation:${file}`);
3399
+ const sealed = { key: file, value: entry, v: version + 1 };
3400
+ await this.store.write(`${this.root}/devices/${this.record.device}/${CONVERSATIONS}/${name}`, this.seal(name, sealed));
3401
+ return true;
3402
+ }
3403
+ chunkName(hash) {
3404
+ return this.name(`chunk:${hash}`);
3405
+ }
3406
+ async chunks() {
3407
+ return new Set((await this.store.list(`${this.root}/${CONVERSATIONS}`)).filter((file) => !file.dir).map((file) => file.name));
3408
+ }
3409
+ async putChunk(hash, bytes) {
3410
+ const name = this.chunkName(hash);
3411
+ await this.store.write(`${this.root}/${CONVERSATIONS}/${name}`, this.sealBytes(name, gzipSync2(bytes)));
3412
+ }
3413
+ async getChunk(hash) {
3414
+ const name = this.chunkName(hash);
3415
+ try {
3416
+ const bytes = await this.store.read(`${this.root}/${CONVERSATIONS}/${name}`);
3417
+ return bytes ? new Uint8Array(gunzipSync2(this.openBytes(Buffer.from(bytes), name))) : undefined;
3418
+ } catch {
3419
+ return;
3420
+ }
3421
+ }
3422
+ async removeChunk(name) {
3423
+ await this.store.remove(`${this.root}/${CONVERSATIONS}/${name}`);
3424
+ }
3269
3425
  async items() {
3270
3426
  const latest = new Map;
3271
3427
  for (const device of await this.dirs(`${this.root}/devices`)) {
@@ -3316,17 +3472,23 @@ class StoreSpace {
3316
3472
  ${key}`).digest("base64url").slice(0, 32);
3317
3473
  }
3318
3474
  seal(aad, value) {
3475
+ return this.sealBytes(aad, Buffer.from(JSON.stringify(value)));
3476
+ }
3477
+ open(sealed, aad) {
3478
+ return JSON.parse(this.openBytes(sealed, aad).toString("utf8"));
3479
+ }
3480
+ sealBytes(aad, bytes) {
3319
3481
  const iv = randomBytes4(12);
3320
3482
  const cipher = createCipheriv2("aes-256-gcm", this.key, iv);
3321
3483
  cipher.setAAD(Buffer.from(aad));
3322
- const body = Buffer.concat([cipher.update(JSON.stringify(value)), cipher.final()]);
3484
+ const body = Buffer.concat([cipher.update(bytes), cipher.final()]);
3323
3485
  return Buffer.concat([iv, cipher.getAuthTag(), body]);
3324
3486
  }
3325
- open(sealed, aad) {
3487
+ openBytes(sealed, aad) {
3326
3488
  const decipher = createDecipheriv2("aes-256-gcm", this.key, sealed.subarray(0, 12));
3327
3489
  decipher.setAAD(Buffer.from(aad));
3328
3490
  decipher.setAuthTag(sealed.subarray(12, 28));
3329
- return JSON.parse(Buffer.concat([decipher.update(sealed.subarray(28)), decipher.final()]).toString("utf8"));
3491
+ return Buffer.concat([decipher.update(sealed.subarray(28)), decipher.final()]);
3330
3492
  }
3331
3493
  async cached(path, tag, aad) {
3332
3494
  const known = this.opened.get(path);
@@ -3361,14 +3523,301 @@ class FolderSpace extends StoreSpace {
3361
3523
  }
3362
3524
 
3363
3525
  // web-src/sync-service-space.ts
3364
- import { mkdir as mkdir6, readFile as readFile6, rename as rename6, writeFile as writeFile5 } from "node:fs/promises";
3526
+ import { mkdir as mkdir7, readFile as readFile7, rename as rename7, writeFile as writeFile6 } from "node:fs/promises";
3365
3527
  import { platform as platform3 } from "node:os";
3366
- import { join as join8 } from "node:path";
3528
+ import { join as join9 } from "node:path";
3367
3529
 
3368
3530
  // web-src/sync-service-base.ts
3369
- import { mkdir as mkdir5, readFile as readFile5, rename as rename5, writeFile as writeFile4 } from "node:fs/promises";
3370
- import { hostname as hostname3, platform as platform2 } from "node:os";
3371
- import { join as join7 } from "node:path";
3531
+ import { mkdir as mkdir6, readFile as readFile6, rename as rename6, writeFile as writeFile5 } from "node:fs/promises";
3532
+ import { homedir as homedir5, hostname as hostname3, platform as platform2 } from "node:os";
3533
+ import { join as join8 } from "node:path";
3534
+
3535
+ // web-src/sync-conversations.ts
3536
+ import { createHash as createHash2 } from "node:crypto";
3537
+ import { mkdir as mkdir5, readdir as readdir6, readFile as readFile5, rename as rename5, rm as rm4, stat as stat6, writeFile as writeFile4 } from "node:fs/promises";
3538
+ import { dirname as dirname5, join as join7 } from "node:path";
3539
+ var CONVERSATION_CHUNK = 1024 * 1024;
3540
+ var MAX_FILE2 = 256 * 1024 * 1024;
3541
+
3542
+ class SyncConversations {
3543
+ space;
3544
+ options;
3545
+ root;
3546
+ constructor(space, options) {
3547
+ this.space = space;
3548
+ this.options = options;
3549
+ this.root = join7(options.agentDir, "sessions");
3550
+ }
3551
+ get chunk() {
3552
+ return this.options.chunk ?? CONVERSATION_CHUNK;
3553
+ }
3554
+ async round(base, report) {
3555
+ const remote = await this.space.conversations();
3556
+ const there = await this.space.chunks();
3557
+ const seen = new Set;
3558
+ const local = await this.scan(seen);
3559
+ const names = new Set([...local.keys(), ...remote.keys(), ...Object.keys(base)]);
3560
+ const kept = new Set;
3561
+ for (const name of [...names].sort()) {
3562
+ if (seen.has(name) && !local.has(name)) {
3563
+ this.keep(kept, remote.get(name)?.entry.chunks);
3564
+ continue;
3565
+ }
3566
+ try {
3567
+ await this.one(name, local.get(name), remote.get(name), base, there, kept, report);
3568
+ } catch (error) {
3569
+ report.skipped.push({ id: name, reason: error instanceof Error ? error.message : String(error) });
3570
+ }
3571
+ }
3572
+ await this.collect(kept, there);
3573
+ }
3574
+ async one(name, local, remote, base, there, kept, report) {
3575
+ const had = base[name];
3576
+ const alive = remote && !remote.entry.deleted ? remote : undefined;
3577
+ if (!local) {
3578
+ if (!alive) {
3579
+ delete base[name];
3580
+ } else if (had?.h && had.h === alive.entry.h) {
3581
+ await this.space.putConversation(name, this.mark(), remote?.v ?? 0);
3582
+ delete base[name];
3583
+ report.sent++;
3584
+ } else if (await this.pull(name, undefined, alive, base, there)) {
3585
+ report.received++;
3586
+ }
3587
+ this.keep(kept, alive?.entry.chunks);
3588
+ return;
3589
+ }
3590
+ if (!alive) {
3591
+ if (remote && had && local.h === had.h) {
3592
+ await this.options.keepAside(`conversations/${name}`, await readFile5(local.path));
3593
+ await rm4(local.path, { force: true });
3594
+ delete base[name];
3595
+ report.received++;
3596
+ return;
3597
+ }
3598
+ if (await this.push(name, local, remote, base, there)) {
3599
+ report.sent++;
3600
+ }
3601
+ this.keep(kept, local.chunks);
3602
+ return;
3603
+ }
3604
+ this.keep(kept, alive.entry.chunks);
3605
+ this.keep(kept, local.chunks);
3606
+ if (local.h === alive.entry.h) {
3607
+ base[name] = this.baseOf(local);
3608
+ if (alive.device === this.options.device) {
3609
+ await this.upload(local, there);
3610
+ }
3611
+ return;
3612
+ }
3613
+ const pull = had?.h === local.h ? true : had?.h === alive.entry.h ? false : alive.entry.size > local.size;
3614
+ if (pull) {
3615
+ if (await this.pull(name, local, alive, base, there)) {
3616
+ report.received++;
3617
+ }
3618
+ return;
3619
+ }
3620
+ if (!this.startsWith(local.body, alive.entry)) {
3621
+ const theirs = await this.body(alive.entry);
3622
+ if (!theirs) {
3623
+ return;
3624
+ }
3625
+ await this.options.keepAside(`conversations/from-${alive.entry.by.replace(/[^\w.-]/g, "_")}/${name}`, this.file(this.headLine(alive.entry), theirs));
3626
+ }
3627
+ if (await this.push(name, local, alive, base, there)) {
3628
+ report.sent++;
3629
+ }
3630
+ }
3631
+ async push(name, local, remote, base, there) {
3632
+ await this.upload(local, there);
3633
+ const entry = {
3634
+ head: local.head,
3635
+ from: { home: this.options.home, agentDir: this.options.agentDir },
3636
+ size: local.size,
3637
+ h: local.h,
3638
+ chunks: local.chunks,
3639
+ at: Math.round(local.mtime),
3640
+ by: this.options.name
3641
+ };
3642
+ if (!await this.space.putConversation(name, entry, remote?.v ?? 0)) {
3643
+ return false;
3644
+ }
3645
+ base[name] = this.baseOf(local);
3646
+ return true;
3647
+ }
3648
+ async upload(local, there) {
3649
+ for (const [index, hash] of local.chunks.entries()) {
3650
+ const name = this.space.chunkName(hash);
3651
+ if (!there.has(name)) {
3652
+ await this.space.putChunk(hash, local.body.subarray(index * this.chunk, (index + 1) * this.chunk));
3653
+ there.add(name);
3654
+ }
3655
+ }
3656
+ }
3657
+ async pull(name, local, remote, base, there) {
3658
+ if (remote.entry.chunks.some((hash) => !there.has(this.space.chunkName(hash)))) {
3659
+ return false;
3660
+ }
3661
+ const body = await this.body(remote.entry);
3662
+ if (!body) {
3663
+ return false;
3664
+ }
3665
+ let path;
3666
+ let headLine;
3667
+ if (local) {
3668
+ const now = await stat6(local.path).catch(() => {
3669
+ return;
3670
+ });
3671
+ if (!now || now.size !== local.fileSize || now.mtimeMs !== local.mtime || await SyncConversations.open(local.path)) {
3672
+ return false;
3673
+ }
3674
+ if (!startsWithBytes(body, local.body)) {
3675
+ await this.options.keepAside(`conversations/${name}`, await readFile5(local.path));
3676
+ }
3677
+ path = local.path;
3678
+ headLine = local.headLine;
3679
+ } else {
3680
+ if (!/^[\w.-]+\.jsonl$/.test(name)) {
3681
+ throw new Error("Not a conversation.");
3682
+ }
3683
+ const cwd = typeof remote.entry.head.cwd === "string" ? this.cwdOf(remote.entry.head.cwd, remote.entry.from) : "";
3684
+ if (!cwd) {
3685
+ throw new Error("Not a conversation of pi.");
3686
+ }
3687
+ path = join7(this.root, MigrateUtil.sessionDir(cwd), name);
3688
+ headLine = JSON.stringify({ ...remote.entry.head, cwd });
3689
+ }
3690
+ await mkdir5(dirname5(path), { recursive: true });
3691
+ const temporary = `${path}.ta-sync-${process.pid}`;
3692
+ await writeFile4(temporary, this.file(headLine, body));
3693
+ await rename5(temporary, path);
3694
+ const written = await stat6(path);
3695
+ base[name] = { h: remote.entry.h, size: remote.entry.size, chunks: remote.entry.chunks, path, fileSize: written.size, mtime: written.mtimeMs };
3696
+ return true;
3697
+ }
3698
+ keep(kept, chunks) {
3699
+ for (const hash of chunks ?? []) {
3700
+ kept.add(this.space.chunkName(hash));
3701
+ }
3702
+ }
3703
+ async collect(kept, there) {
3704
+ for (const name of there) {
3705
+ if (!kept.has(name)) {
3706
+ await this.space.removeChunk(name).catch(() => {
3707
+ return;
3708
+ });
3709
+ }
3710
+ }
3711
+ }
3712
+ async body(entry) {
3713
+ const parts = [];
3714
+ for (const hash of entry.chunks) {
3715
+ const part = await this.space.getChunk(hash);
3716
+ if (!part || hashOf(part) !== hash) {
3717
+ return;
3718
+ }
3719
+ parts.push(part);
3720
+ }
3721
+ const body = new Uint8Array(Buffer.concat(parts));
3722
+ return body.length === entry.size && hashOf(body) === entry.h ? body : undefined;
3723
+ }
3724
+ startsWith(bytes, entry) {
3725
+ if (entry.size > bytes.length) {
3726
+ return false;
3727
+ }
3728
+ return entry.chunks.every((hash, index) => {
3729
+ const end = Math.min((index + 1) * this.chunk, entry.size);
3730
+ return hashOf(bytes.subarray(index * this.chunk, end)) === hash;
3731
+ });
3732
+ }
3733
+ async scan(seen) {
3734
+ const found = new Map;
3735
+ for (const dir of await readdir6(this.root, { withFileTypes: true }).catch(() => [])) {
3736
+ if (!dir.isDirectory()) {
3737
+ continue;
3738
+ }
3739
+ for (const file of await readdir6(join7(this.root, dir.name), { withFileTypes: true }).catch(() => [])) {
3740
+ if (!file.isFile() || !file.name.endsWith(".jsonl") || found.has(file.name)) {
3741
+ continue;
3742
+ }
3743
+ seen.add(file.name);
3744
+ const path = join7(this.root, dir.name, file.name);
3745
+ const info = await stat6(path).catch(() => {
3746
+ return;
3747
+ });
3748
+ if (!info || info.size > MAX_FILE2) {
3749
+ continue;
3750
+ }
3751
+ const conversation = await this.read(path, info.size, info.mtimeMs);
3752
+ if (conversation) {
3753
+ found.set(file.name, conversation);
3754
+ }
3755
+ }
3756
+ }
3757
+ return found;
3758
+ }
3759
+ async read(path, fileSize, mtime) {
3760
+ const bytes = new Uint8Array(await readFile5(path).catch(() => Buffer.alloc(0)));
3761
+ const end = bytes.indexOf(10);
3762
+ if (end < 0) {
3763
+ return;
3764
+ }
3765
+ const headLine = new TextDecoder().decode(bytes.subarray(0, end));
3766
+ let head;
3767
+ try {
3768
+ head = JSON.parse(headLine);
3769
+ } catch {
3770
+ return;
3771
+ }
3772
+ if (!head || typeof head !== "object" || head.type !== "session") {
3773
+ return;
3774
+ }
3775
+ const body = bytes.subarray(end + 1);
3776
+ const chunks = [];
3777
+ for (let start = 0;start < body.length; start += this.chunk) {
3778
+ chunks.push(hashOf(body.subarray(start, start + this.chunk)));
3779
+ }
3780
+ return { path, fileSize, mtime, headLine, head, body, h: hashOf(body), size: body.length, chunks };
3781
+ }
3782
+ static async open(path) {
3783
+ try {
3784
+ const lock = JSON.parse(await readFile5(`${path}.lock`, "utf8"));
3785
+ if (typeof lock.pid !== "number") {
3786
+ return false;
3787
+ }
3788
+ process.kill(lock.pid, 0);
3789
+ return true;
3790
+ } catch (error) {
3791
+ return error?.code === "EPERM";
3792
+ }
3793
+ }
3794
+ cwdOf(cwd, from) {
3795
+ return TaPathMap.path(cwd, [
3796
+ [from.agentDir, this.options.agentDir],
3797
+ [from.home, this.options.home]
3798
+ ]);
3799
+ }
3800
+ headLine(entry) {
3801
+ return JSON.stringify(entry.head);
3802
+ }
3803
+ file(headLine, body) {
3804
+ return new Uint8Array(Buffer.concat([Buffer.from(`${headLine}
3805
+ `), body]));
3806
+ }
3807
+ mark() {
3808
+ const now = (this.options.now ?? Date.now)();
3809
+ return { head: {}, from: { home: "", agentDir: "" }, size: 0, h: "", chunks: [], at: now, by: this.options.name, deleted: true };
3810
+ }
3811
+ baseOf(local) {
3812
+ return { h: local.h, size: local.size, chunks: local.chunks, path: local.path, fileSize: local.fileSize, mtime: local.mtime };
3813
+ }
3814
+ }
3815
+ function hashOf(bytes) {
3816
+ return createHash2("sha256").update(bytes).digest("hex");
3817
+ }
3818
+ function startsWithBytes(bytes, start) {
3819
+ return start.length <= bytes.length && Buffer.from(bytes.buffer, bytes.byteOffset, start.length).equals(Buffer.from(start));
3820
+ }
3372
3821
 
3373
3822
  // web-src/sync-engine.ts
3374
3823
  var ATTEMPTS = 3;
@@ -3766,8 +4215,8 @@ class SyncServiceBase {
3766
4215
  constructor(options) {
3767
4216
  this.options = options;
3768
4217
  this.device = options.device ?? SyncServiceBase.deviceName();
3769
- this.baseFile = join7(options.agentDir, "sync", "base.json");
3770
- this.folderFile = join7(options.agentDir, "sync", "folder.json");
4218
+ this.baseFile = join8(options.agentDir, "sync", "base.json");
4219
+ this.folderFile = join8(options.agentDir, "sync", "folder.json");
3771
4220
  }
3772
4221
  static deviceName() {
3773
4222
  return hostname3().replace(/\.local$/, "") || platform2();
@@ -3833,7 +4282,7 @@ class SyncServiceBase {
3833
4282
  target = { id: joined.id, room: this.roomOf(joined.id), self: joined.members.find((member) => member.self)?.member };
3834
4283
  }
3835
4284
  const now = await this.resources();
3836
- const resources = new Set(SYNC_RESOURCES.filter((resource) => now[resource]));
4285
+ const resources = this.effective(now, folder);
3837
4286
  const loaded = await this.loadBase();
3838
4287
  const base = loaded.room === target.id ? loaded : { room: target.id, items: {} };
3839
4288
  this.options.place.begin();
@@ -3846,6 +4295,17 @@ class SyncServiceBase {
3846
4295
  report.sent += shared.sent;
3847
4296
  report.received += shared.received;
3848
4297
  }
4298
+ if (folder && this.space && resources.has("conversations")) {
4299
+ base.conversations ??= {};
4300
+ const place = this.options.place;
4301
+ await new SyncConversations(this.space.space, {
4302
+ agentDir: this.options.agentDir,
4303
+ home: homedir5(),
4304
+ name: this.device,
4305
+ device: folder.device,
4306
+ keepAside: (path, bytes) => place.keepAside(path, bytes)
4307
+ }).round(base.conversations, report);
4308
+ }
3849
4309
  await this.saveBase(base);
3850
4310
  if (target.self) {
3851
4311
  const entry = { name: this.device, platform: platform2(), at: report.at };
@@ -3854,7 +4314,7 @@ class SyncServiceBase {
3854
4314
  });
3855
4315
  }
3856
4316
  if (folder && this.space) {
3857
- this.spaceSignature = await this.space.space.signature().catch(() => this.spaceSignature);
4317
+ this.spaceSignature = await this.space.space.signature(resources.has("conversations")).catch(() => this.spaceSignature);
3858
4318
  }
3859
4319
  this.signature = await this.options.place.signature(resources);
3860
4320
  this.lastFull = Date.now();
@@ -3868,14 +4328,21 @@ class SyncServiceBase {
3868
4328
  if (this.running || this.stopped) {
3869
4329
  return;
3870
4330
  }
3871
- const resources = new Set(SYNC_RESOURCES.filter((resource) => this.resourcesNow[resource]));
4331
+ const resources = this.effective(this.resourcesNow, this.space?.record);
3872
4332
  const signature = await this.options.place.signature(resources).catch(() => this.signature);
3873
- const space = this.space ? await this.space.space.signature().catch(() => this.spaceSignature) : this.spaceSignature;
4333
+ const space = this.space ? await this.space.space.signature(resources.has("conversations")).catch(() => this.spaceSignature) : this.spaceSignature;
3874
4334
  const every = this.options.everyMs ?? 10 * 60000;
3875
4335
  if (signature !== this.signature || space !== this.spaceSignature || Date.now() - this.lastFull > every) {
3876
4336
  this.schedule(0);
3877
4337
  }
3878
4338
  }
4339
+ effective(resources, folder) {
4340
+ const on = new Set(SYNC_RESOURCES.filter((resource) => resources[resource] && (this.options.place.reaches?.(resource) ?? true)));
4341
+ if (!folder || !SyncWhere.conversations(folder.service ? "store" : "folder", folder.type)) {
4342
+ on.delete("conversations");
4343
+ }
4344
+ return on;
4345
+ }
3879
4346
  roomOf(id) {
3880
4347
  const rooms = this.options.rooms;
3881
4348
  return {
@@ -3941,16 +4408,16 @@ class SyncServiceBase {
3941
4408
  }
3942
4409
  async loadBase() {
3943
4410
  try {
3944
- const data = JSON.parse(await readFile5(this.baseFile, "utf8"));
4411
+ const data = JSON.parse(await readFile6(this.baseFile, "utf8"));
3945
4412
  return SyncMerge.isObject(data) && typeof data.room === "string" && SyncMerge.isObject(data.items) ? data : { room: "", items: {} };
3946
4413
  } catch {
3947
4414
  return { room: "", items: {} };
3948
4415
  }
3949
4416
  }
3950
4417
  async saveBase(base) {
3951
- await mkdir5(join7(this.options.agentDir, "sync"), { recursive: true });
3952
- await writeFile4(`${this.baseFile}.tmp`, JSON.stringify(base));
3953
- await rename5(`${this.baseFile}.tmp`, this.baseFile);
4418
+ await mkdir6(join8(this.options.agentDir, "sync"), { recursive: true });
4419
+ await writeFile5(`${this.baseFile}.tmp`, JSON.stringify(base));
4420
+ await rename6(`${this.baseFile}.tmp`, this.baseFile);
3954
4421
  }
3955
4422
  }
3956
4423
 
@@ -4040,16 +4507,16 @@ class SyncServiceSpace extends SyncServiceBase {
4040
4507
  }
4041
4508
  async folderRecord() {
4042
4509
  try {
4043
- const record = JSON.parse(await readFile6(this.folderFile, "utf8"));
4510
+ const record = JSON.parse(await readFile7(this.folderFile, "utf8"));
4044
4511
  return record && typeof record.id === "string" && typeof record.key === "string" && typeof record.path === "string" && typeof record.device === "string" ? record : undefined;
4045
4512
  } catch {
4046
4513
  return;
4047
4514
  }
4048
4515
  }
4049
4516
  async saveFolder(record) {
4050
- await mkdir6(join8(this.options.agentDir, "sync"), { recursive: true });
4051
- await writeFile5(`${this.folderFile}.tmp`, JSON.stringify(record), { mode: 384 });
4052
- await rename6(`${this.folderFile}.tmp`, this.folderFile);
4517
+ await mkdir7(join9(this.options.agentDir, "sync"), { recursive: true });
4518
+ await writeFile6(`${this.folderFile}.tmp`, JSON.stringify(record), { mode: 384 });
4519
+ await rename7(`${this.folderFile}.tmp`, this.folderFile);
4053
4520
  }
4054
4521
  async openSpace(record) {
4055
4522
  const where = record.service ? `store:${record.service}` : `folder:${record.path}`;
@@ -4138,6 +4605,11 @@ class SyncService extends SyncServiceSpace {
4138
4605
  stop();
4139
4606
  }
4140
4607
  }
4608
+ secretsChanged() {
4609
+ if (this.resourcesNow.secrets) {
4610
+ this.schedule();
4611
+ }
4612
+ }
4141
4613
  dataChanged(name, key) {
4142
4614
  if (name === SYNC_PACKAGE ? key === SyncKeys.RESOURCES : this.options.place.tracks(name, key)) {
4143
4615
  this.schedule();
@@ -4301,7 +4773,7 @@ class SyncService extends SyncServiceSpace {
4301
4773
  if (folder) {
4302
4774
  this.space?.stop();
4303
4775
  this.space = undefined;
4304
- await rm4(this.folderFile, { force: true });
4776
+ await rm5(this.folderFile, { force: true });
4305
4777
  }
4306
4778
  const room = await this.room();
4307
4779
  if (room) {
@@ -4341,6 +4813,10 @@ class SyncService extends SyncServiceSpace {
4341
4813
  this.schedule(0);
4342
4814
  return this.status();
4343
4815
  }
4816
+ async placeChanged() {
4817
+ await this.publish();
4818
+ return this.status();
4819
+ }
4344
4820
  async syncNow() {
4345
4821
  clearTimeout(this.timer);
4346
4822
  await this.run();
@@ -4362,13 +4838,14 @@ function hostSide(host) {
4362
4838
  getData: (name, key) => host.getExtensionData(name, key),
4363
4839
  setData: (name, key, value) => host.setExtensionData(name, key, value),
4364
4840
  ...typeof host.rooms.exportIdentity === "function" ? { identity: host.rooms } : {},
4841
+ ...typeof host.credentials?.synced === "function" ? { secrets: host.credentials } : {},
4365
4842
  dataKeys: async () => {
4366
4843
  const keys = Object.entries(SyncDeclarations.SHELL).flatMap(([name, list]) => list.map((key) => ({ name, key })));
4367
4844
  for (const pkg of await host.listPackages().catch(() => [])) {
4368
4845
  if (!pkg.enabled) {
4369
4846
  continue;
4370
4847
  }
4371
- const manifest = await readFile7(join9(pkg.root, "package.json"), "utf8").then((text) => JSON.parse(text), () => {
4848
+ const manifest = await readFile8(join10(pkg.root, "package.json"), "utf8").then((text) => JSON.parse(text), () => {
4372
4849
  return;
4373
4850
  });
4374
4851
  keys.push(...SyncDeclarations.keys(manifest).map((key) => ({ name: pkg.name, key })));
@@ -4401,6 +4878,7 @@ function activate(host) {
4401
4878
  storage: { get: (key) => host.getExtensionData(SYNC_PACKAGE, key), set: (key, value) => host.setExtensionData(SYNC_PACKAGE, key, value) }
4402
4879
  });
4403
4880
  const stopData = host.onExtensionData((name, key, cwd) => cwd === undefined && service.dataChanged(name, key));
4881
+ const stopSecrets = typeof host.credentials?.onChanged === "function" ? host.credentials.onChanged(() => service.secretsChanged()) : undefined;
4404
4882
  service.start();
4405
4883
  const OFF = "account.off";
4406
4884
  const off = (value) => host.setExtensionData(SYNC_PACKAGE, OFF, value || null).catch(() => {
@@ -4414,7 +4892,64 @@ function activate(host) {
4414
4892
  off: async () => await host.getExtensionData(SYNC_PACKAGE, OFF) === true
4415
4893
  }) : undefined;
4416
4894
  account?.start();
4895
+ const places = async () => {
4896
+ const relays = host.relays;
4897
+ const listed = relays?.overview ? await relays.overview() : undefined;
4898
+ const services = listed?.relays ?? await relays?.list().catch(() => []) ?? [];
4899
+ const sync = listed?.use.sync ?? (await relays?.serviceFor("sync").catch(() => {
4900
+ return;
4901
+ }))?.id ?? "";
4902
+ return {
4903
+ use: sync,
4904
+ services: services.filter((service2) => !service2.only || service2.only.includes("sync")).map(({ id, type, name, url, path, where, builtin, unavailable }) => ({
4905
+ id,
4906
+ type,
4907
+ builtin,
4908
+ ...name ? { name } : {},
4909
+ ...url ? { url } : {},
4910
+ ...path ? { path } : {},
4911
+ ...where ? { where } : {},
4912
+ ...unavailable ? { unavailable } : {}
4913
+ })),
4914
+ drives: listed?.drives ?? [],
4915
+ types: (listed?.types ?? []).map(({ type, title, hint, icon, options }) => ({
4916
+ type,
4917
+ title,
4918
+ options,
4919
+ ...hint ? { hint } : {},
4920
+ ...icon ? { icon } : {}
4921
+ }))
4922
+ };
4923
+ };
4924
+ const changePlace = async (change) => {
4925
+ const relays = host.relays;
4926
+ if (!relays) {
4927
+ throw new Error(SyncServerTexts.storeUnavailable(""));
4928
+ }
4929
+ if ((await service.status()).state !== "off") {
4930
+ throw new Error(SyncServerTexts.placeLocked());
4931
+ }
4932
+ const chosen = await change(relays);
4933
+ if (chosen !== undefined) {
4934
+ await relays.choose("sync", chosen);
4935
+ }
4936
+ await service.placeChanged();
4937
+ return places();
4938
+ };
4417
4939
  return {
4940
+ places,
4941
+ choosePlace: (id) => changePlace(async () => typeof id === "string" && id ? id : null),
4942
+ addFolderPlace: (path, name) => changePlace((relays) => relays.addFolder(String(path ?? ""), typeof name === "string" && name ? name : undefined)),
4943
+ addStorePlace: (type, options, name) => changePlace((relays) => relays.addStore(String(type ?? ""), options && typeof options === "object" ? options : {}, typeof name === "string" && name ? name : undefined)),
4944
+ removePlace: async (id) => {
4945
+ const listed = await places();
4946
+ if (listed.use === id) {
4947
+ throw new Error(SyncServerTexts.placeInUse());
4948
+ }
4949
+ await host.relays?.remove?.(String(id ?? ""));
4950
+ await service.placeChanged();
4951
+ return places();
4952
+ },
4418
4953
  status: () => service.status(),
4419
4954
  enable: async () => {
4420
4955
  await off(false);
@@ -4449,6 +4984,7 @@ function activate(host) {
4449
4984
  migrateRelocate: (from, to) => migrate.relocate(String(from ?? ""), String(to ?? "")),
4450
4985
  dispose: () => {
4451
4986
  stopData();
4987
+ stopSecrets?.();
4452
4988
  account?.dispose();
4453
4989
  service.dispose();
4454
4990
  migrate.dispose();