@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/dist/index.cjs CHANGED
@@ -5369,20 +5369,29 @@ function tableRenderSchema(facts) {
5369
5369
  function refuse(code, message, hint) {
5370
5370
  return { ok: false, error: { code, message, hint, ours: true } };
5371
5371
  }
5372
- function alternatePatch(document, field_key, alternate_index) {
5372
+ function displacedSource(envelope) {
5373
+ if (envelope.src) return { src: envelope.src };
5374
+ const by = envelope.actor === "agent" ? "an agent" : envelope.actor === "system" ? "the system" : envelope.actor ? "a person" : null;
5375
+ if (by === null) return {};
5376
+ const day = typeof envelope.at === "string" && envelope.at.length >= 10 ? ` on ${envelope.at.slice(0, 10)}` : "";
5377
+ return { src: { name: `Entered by ${by}${day}` } };
5378
+ }
5379
+ function alternatePatch(document, field_key, alternate_index, rank) {
5373
5380
  const block = document._values ?? {};
5374
5381
  const envelope = block[field_key];
5375
5382
  const alternates = Array.isArray(envelope?.alternates) ? envelope.alternates : [];
5376
- const chosen = alternates[alternate_index];
5383
+ const at = rank === void 0 ? alternate_index : alternates.findIndex((a) => a.rank === rank);
5384
+ alternate_index = at;
5385
+ const chosen = alternates[at];
5377
5386
  if (!envelope || !chosen) return { ok: false, message: "That other value is no longer on this record." };
5378
5387
  const kept = alternates.filter((_, i) => i !== alternate_index);
5379
5388
  const current = document[field_key];
5380
5389
  const next = [...kept];
5381
5390
  if (current !== null && current !== void 0) {
5382
5391
  const taken = new Set(kept.map((a) => a.rank));
5383
- let rank = 1;
5384
- while (taken.has(rank)) rank += 1;
5385
- next.push({ value: current, ...envelope.src ? { src: envelope.src } : {}, rank });
5392
+ let rank2 = 1;
5393
+ while (taken.has(rank2)) rank2 += 1;
5394
+ next.push({ value: current, ...displacedSource(envelope), rank: rank2 });
5386
5395
  }
5387
5396
  const { ver: _ver, actor: _actor, at: _at, on_behalf_of: _on, absent: _absent, alternates: _alts, src: _src, ...rest } = envelope;
5388
5397
  const changed = { ...rest, ...chosen.src ? { src: chosen.src } : {} };
@@ -5396,14 +5405,20 @@ function alternatePatch(document, field_key, alternate_index) {
5396
5405
  }
5397
5406
  };
5398
5407
  }
5399
- async function chooseAlternate(client, { record_id, field_key, alternate_index }) {
5408
+ async function chooseAlternate(client, { record_id, field_key, alternate_index, rank }) {
5400
5409
  const read = await client.recordRead({ record_id, withVersion: true });
5401
5410
  if (!read.ok) return read;
5411
+ if (Object.keys(read.data.hidden ?? {}).length > 0) {
5412
+ 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.");
5413
+ }
5402
5414
  const version = read.data.version;
5403
5415
  if (version === null || version === void 0) {
5404
5416
  return refuse("stale_read", "Nothing was saved: this record's latest changes could not be checked.", "Reload, then try again.");
5405
5417
  }
5406
- const built = alternatePatch(read.data.document, field_key, alternate_index);
5418
+ const raw = await client.recordAsOf({ record_id, recorded_at: (/* @__PURE__ */ new Date()).toISOString() });
5419
+ if (!raw.ok) return raw;
5420
+ if (raw.data === null) return refuse("not_found", "That record is no longer here.", "Reload, then look again.");
5421
+ const built = alternatePatch(raw.data, field_key, alternate_index, rank);
5407
5422
  if (!built.ok) return refuse("not_found", built.message, "Reload, then look again.");
5408
5423
  return client.recordUpdate({ record_id, patch: built.patch, expectedVersion: version });
5409
5424
  }