@ai-matrx/records 0.98.10 → 0.98.12

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 CHANGED
@@ -1,5 +1,17 @@
1
1
  # Changelog — @ai-matrx/records
2
2
 
3
+ ## 0.98.12
4
+
5
+ Automatic changed-only republish (docs/metadata drift since the last tag — see
6
+ `git diff npm/records/v0.98.11..npm/records/v0.98.12 -- apps/shared/records`).
7
+ No source changes intended and no consumer action required.
8
+
9
+ ## 0.98.11
10
+
11
+ Automatic changed-only republish (docs/metadata drift since the last tag — see
12
+ `git diff npm/records/v0.98.10..npm/records/v0.98.11 -- apps/shared/records`).
13
+ No source changes intended and no consumer action required.
14
+
3
15
  ## 0.98.10
4
16
 
5
17
  Automatic changed-only republish (docs/metadata drift since the last tag — see
@@ -5480,20 +5480,29 @@ function tableRenderSchema(facts) {
5480
5480
  function refuse(code, message, hint) {
5481
5481
  return { ok: false, error: { code, message, hint, ours: true } };
5482
5482
  }
5483
- function alternatePatch(document, field_key, alternate_index) {
5483
+ function displacedSource(envelope) {
5484
+ if (envelope.src) return { src: envelope.src };
5485
+ const by = envelope.actor === "agent" ? "an agent" : envelope.actor === "system" ? "the system" : envelope.actor ? "a person" : null;
5486
+ if (by === null) return {};
5487
+ const day = typeof envelope.at === "string" && envelope.at.length >= 10 ? ` on ${envelope.at.slice(0, 10)}` : "";
5488
+ return { src: { name: `Entered by ${by}${day}` } };
5489
+ }
5490
+ function alternatePatch(document, field_key, alternate_index, rank) {
5484
5491
  const block = document._values ?? {};
5485
5492
  const envelope = block[field_key];
5486
5493
  const alternates = Array.isArray(envelope?.alternates) ? envelope.alternates : [];
5487
- const chosen = alternates[alternate_index];
5494
+ const at = rank === void 0 ? alternate_index : alternates.findIndex((a) => a.rank === rank);
5495
+ alternate_index = at;
5496
+ const chosen = alternates[at];
5488
5497
  if (!envelope || !chosen) return { ok: false, message: "That other value is no longer on this record." };
5489
5498
  const kept = alternates.filter((_, i) => i !== alternate_index);
5490
5499
  const current = document[field_key];
5491
5500
  const next = [...kept];
5492
5501
  if (current !== null && current !== void 0) {
5493
5502
  const taken = new Set(kept.map((a) => a.rank));
5494
- let rank = 1;
5495
- while (taken.has(rank)) rank += 1;
5496
- next.push({ value: current, ...envelope.src ? { src: envelope.src } : {}, rank });
5503
+ let rank2 = 1;
5504
+ while (taken.has(rank2)) rank2 += 1;
5505
+ next.push({ value: current, ...displacedSource(envelope), rank: rank2 });
5497
5506
  }
5498
5507
  const { ver: _ver, actor: _actor, at: _at, on_behalf_of: _on, absent: _absent, alternates: _alts, src: _src, ...rest } = envelope;
5499
5508
  const changed = { ...rest, ...chosen.src ? { src: chosen.src } : {} };
@@ -5507,14 +5516,20 @@ function alternatePatch(document, field_key, alternate_index) {
5507
5516
  }
5508
5517
  };
5509
5518
  }
5510
- async function chooseAlternate(client, { record_id, field_key, alternate_index }) {
5519
+ async function chooseAlternate(client, { record_id, field_key, alternate_index, rank }) {
5511
5520
  const read = await client.recordRead({ record_id, withVersion: true });
5512
5521
  if (!read.ok) return read;
5522
+ if (Object.keys(read.data.hidden ?? {}).length > 0) {
5523
+ return refuse("refused_by_rule", "Some fields here are hidden from you, so this choice can't be saved safely.", "Ask someone who can see every field.");
5524
+ }
5513
5525
  const version = read.data.version;
5514
5526
  if (version === null || version === void 0) {
5515
5527
  return refuse("stale_read", "Nothing was saved: this record's latest changes could not be checked.", "Reload, then try again.");
5516
5528
  }
5517
- const built = alternatePatch(read.data.document, field_key, alternate_index);
5529
+ const raw = await client.recordAsOf({ record_id, recorded_at: (/* @__PURE__ */ new Date()).toISOString() });
5530
+ if (!raw.ok) return raw;
5531
+ if (raw.data === null) return refuse("not_found", "That record is no longer here.", "Reload, then look again.");
5532
+ const built = alternatePatch(raw.data, field_key, alternate_index, rank);
5518
5533
  if (!built.ok) return refuse("not_found", built.message, "Reload, then look again.");
5519
5534
  return client.recordUpdate({ record_id, patch: built.patch, expectedVersion: version });
5520
5535
  }