@ai-matrx/records 0.98.10 → 0.98.11

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,22 @@ 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 alternatePatch(document, field_key, alternate_index, rank) {
5373
5373
  const block = document._values ?? {};
5374
5374
  const envelope = block[field_key];
5375
5375
  const alternates = Array.isArray(envelope?.alternates) ? envelope.alternates : [];
5376
- const chosen = alternates[alternate_index];
5376
+ const at = rank === void 0 ? alternate_index : alternates.findIndex((a) => a.rank === rank);
5377
+ alternate_index = at;
5378
+ const chosen = alternates[at];
5377
5379
  if (!envelope || !chosen) return { ok: false, message: "That other value is no longer on this record." };
5378
5380
  const kept = alternates.filter((_, i) => i !== alternate_index);
5379
5381
  const current = document[field_key];
5380
5382
  const next = [...kept];
5381
5383
  if (current !== null && current !== void 0) {
5382
5384
  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 });
5385
+ let rank2 = 1;
5386
+ while (taken.has(rank2)) rank2 += 1;
5387
+ next.push({ value: current, ...envelope.src ? { src: envelope.src } : {}, rank: rank2 });
5386
5388
  }
5387
5389
  const { ver: _ver, actor: _actor, at: _at, on_behalf_of: _on, absent: _absent, alternates: _alts, src: _src, ...rest } = envelope;
5388
5390
  const changed = { ...rest, ...chosen.src ? { src: chosen.src } : {} };
@@ -5396,14 +5398,20 @@ function alternatePatch(document, field_key, alternate_index) {
5396
5398
  }
5397
5399
  };
5398
5400
  }
5399
- async function chooseAlternate(client, { record_id, field_key, alternate_index }) {
5401
+ async function chooseAlternate(client, { record_id, field_key, alternate_index, rank }) {
5400
5402
  const read = await client.recordRead({ record_id, withVersion: true });
5401
5403
  if (!read.ok) return read;
5404
+ if (Object.keys(read.data.hidden ?? {}).length > 0) {
5405
+ 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.");
5406
+ }
5402
5407
  const version = read.data.version;
5403
5408
  if (version === null || version === void 0) {
5404
5409
  return refuse("stale_read", "Nothing was saved: this record's latest changes could not be checked.", "Reload, then try again.");
5405
5410
  }
5406
- const built = alternatePatch(read.data.document, field_key, alternate_index);
5411
+ const raw = await client.recordAsOf({ record_id, recorded_at: (/* @__PURE__ */ new Date()).toISOString() });
5412
+ if (!raw.ok) return raw;
5413
+ if (raw.data === null) return refuse("not_found", "That record is no longer here.", "Reload, then look again.");
5414
+ const built = alternatePatch(raw.data, field_key, alternate_index, rank);
5407
5415
  if (!built.ok) return refuse("not_found", built.message, "Reload, then look again.");
5408
5416
  return client.recordUpdate({ record_id, patch: built.patch, expectedVersion: version });
5409
5417
  }