@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.d.cts CHANGED
@@ -410,15 +410,17 @@ interface ChooseAlternateArgs {
410
410
  field_key: string;
411
411
  /** Position in the field's `alternates` list as the record holds it. */
412
412
  alternate_index: number;
413
+ /** When given, the alternate holding this rank is chosen instead — the list a screen drew may be ordered differently. */
414
+ rank?: number;
413
415
  }
414
416
  /** The patch that makes alternate `alternate_index` of `field_key` the value. Pure; exported for tests. */
415
- declare function alternatePatch(document: RecordDocument, field_key: string, alternate_index: number): {
417
+ declare function alternatePatch(document: RecordDocument, field_key: string, alternate_index: number, rank?: number): {
416
418
  ok: true;
417
419
  patch: Record<string, unknown>;
418
420
  } | {
419
421
  ok: false;
420
422
  message: string;
421
423
  };
422
- declare function chooseAlternate(client: Pick<RecordsClient, "recordRead" | "recordUpdate">, { record_id, field_key, alternate_index }: ChooseAlternateArgs): Promise<RecordsResult<number>>;
424
+ declare function chooseAlternate(client: Pick<RecordsClient, "recordRead" | "recordAsOf" | "recordUpdate">, { record_id, field_key, alternate_index, rank }: ChooseAlternateArgs): Promise<RecordsResult<number>>;
423
425
 
424
426
  export { CaptureFileRef, CaptureOutcome, type CaptureQueue, type CaptureQueueCounts, type CaptureQueueOptions, type CaptureQueuePorts, type CaptureQueueStore, type CaptureSyncReport, type ChooseAlternateArgs, CoercibleField, Delivery, Freshness, type MergeField, MergeFieldModifier, type MergeFieldProvenance, type MergeFieldResolution, MergeFieldSemanticType, MergeFieldSource, type MergeFieldTransform, type MissingBehavior, OverridePolicy, type QueuedCapture, type QueuedCaptureFile, type QueuedCaptureState, REFUSALS_WORTH_REPEATING, RecordDocument, RecordsResult, type ResolutionTriple, Timestamp, type TypedDate, type TypedMatchScore, type TypedNumber, type TypedTime, Uuid, alternatePatch, autonumberJoin, autonumberPrefixOf, autonumberSearchOf, autonumberWords, chooseAlternate, filterWithAutonumber, indexedDbStore, isAutonumberField, isDecided, localeDateOrder, memoryStore, mintClientKey, openCaptureQueue, rankTypedMatches, readTypedDate, readTypedNumber, readTypedTime, twoDigitYear, typedDateText, typedMatchScore, typedTimeText };
package/dist/index.d.ts CHANGED
@@ -410,15 +410,17 @@ interface ChooseAlternateArgs {
410
410
  field_key: string;
411
411
  /** Position in the field's `alternates` list as the record holds it. */
412
412
  alternate_index: number;
413
+ /** When given, the alternate holding this rank is chosen instead — the list a screen drew may be ordered differently. */
414
+ rank?: number;
413
415
  }
414
416
  /** The patch that makes alternate `alternate_index` of `field_key` the value. Pure; exported for tests. */
415
- declare function alternatePatch(document: RecordDocument, field_key: string, alternate_index: number): {
417
+ declare function alternatePatch(document: RecordDocument, field_key: string, alternate_index: number, rank?: number): {
416
418
  ok: true;
417
419
  patch: Record<string, unknown>;
418
420
  } | {
419
421
  ok: false;
420
422
  message: string;
421
423
  };
422
- declare function chooseAlternate(client: Pick<RecordsClient, "recordRead" | "recordUpdate">, { record_id, field_key, alternate_index }: ChooseAlternateArgs): Promise<RecordsResult<number>>;
424
+ declare function chooseAlternate(client: Pick<RecordsClient, "recordRead" | "recordAsOf" | "recordUpdate">, { record_id, field_key, alternate_index, rank }: ChooseAlternateArgs): Promise<RecordsResult<number>>;
423
425
 
424
426
  export { CaptureFileRef, CaptureOutcome, type CaptureQueue, type CaptureQueueCounts, type CaptureQueueOptions, type CaptureQueuePorts, type CaptureQueueStore, type CaptureSyncReport, type ChooseAlternateArgs, CoercibleField, Delivery, Freshness, type MergeField, MergeFieldModifier, type MergeFieldProvenance, type MergeFieldResolution, MergeFieldSemanticType, MergeFieldSource, type MergeFieldTransform, type MissingBehavior, OverridePolicy, type QueuedCapture, type QueuedCaptureFile, type QueuedCaptureState, REFUSALS_WORTH_REPEATING, RecordDocument, RecordsResult, type ResolutionTriple, Timestamp, type TypedDate, type TypedMatchScore, type TypedNumber, type TypedTime, Uuid, alternatePatch, autonumberJoin, autonumberPrefixOf, autonumberSearchOf, autonumberWords, chooseAlternate, filterWithAutonumber, indexedDbStore, isAutonumberField, isDecided, localeDateOrder, memoryStore, mintClientKey, openCaptureQueue, rankTypedMatches, readTypedDate, readTypedNumber, readTypedTime, twoDigitYear, typedDateText, typedMatchScore, typedTimeText };
package/dist/index.js CHANGED
@@ -5114,20 +5114,29 @@ function tableRenderSchema(facts) {
5114
5114
  function refuse(code, message, hint) {
5115
5115
  return { ok: false, error: { code, message, hint, ours: true } };
5116
5116
  }
5117
- function alternatePatch(document, field_key, alternate_index) {
5117
+ function displacedSource(envelope) {
5118
+ if (envelope.src) return { src: envelope.src };
5119
+ const by = envelope.actor === "agent" ? "an agent" : envelope.actor === "system" ? "the system" : envelope.actor ? "a person" : null;
5120
+ if (by === null) return {};
5121
+ const day = typeof envelope.at === "string" && envelope.at.length >= 10 ? ` on ${envelope.at.slice(0, 10)}` : "";
5122
+ return { src: { name: `Entered by ${by}${day}` } };
5123
+ }
5124
+ function alternatePatch(document, field_key, alternate_index, rank) {
5118
5125
  const block = document._values ?? {};
5119
5126
  const envelope = block[field_key];
5120
5127
  const alternates = Array.isArray(envelope?.alternates) ? envelope.alternates : [];
5121
- const chosen = alternates[alternate_index];
5128
+ const at = rank === void 0 ? alternate_index : alternates.findIndex((a) => a.rank === rank);
5129
+ alternate_index = at;
5130
+ const chosen = alternates[at];
5122
5131
  if (!envelope || !chosen) return { ok: false, message: "That other value is no longer on this record." };
5123
5132
  const kept = alternates.filter((_, i) => i !== alternate_index);
5124
5133
  const current = document[field_key];
5125
5134
  const next = [...kept];
5126
5135
  if (current !== null && current !== void 0) {
5127
5136
  const taken = new Set(kept.map((a) => a.rank));
5128
- let rank = 1;
5129
- while (taken.has(rank)) rank += 1;
5130
- next.push({ value: current, ...envelope.src ? { src: envelope.src } : {}, rank });
5137
+ let rank2 = 1;
5138
+ while (taken.has(rank2)) rank2 += 1;
5139
+ next.push({ value: current, ...displacedSource(envelope), rank: rank2 });
5131
5140
  }
5132
5141
  const { ver: _ver, actor: _actor, at: _at, on_behalf_of: _on, absent: _absent, alternates: _alts, src: _src, ...rest } = envelope;
5133
5142
  const changed = { ...rest, ...chosen.src ? { src: chosen.src } : {} };
@@ -5141,14 +5150,20 @@ function alternatePatch(document, field_key, alternate_index) {
5141
5150
  }
5142
5151
  };
5143
5152
  }
5144
- async function chooseAlternate(client, { record_id, field_key, alternate_index }) {
5153
+ async function chooseAlternate(client, { record_id, field_key, alternate_index, rank }) {
5145
5154
  const read = await client.recordRead({ record_id, withVersion: true });
5146
5155
  if (!read.ok) return read;
5156
+ if (Object.keys(read.data.hidden ?? {}).length > 0) {
5157
+ 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.");
5158
+ }
5147
5159
  const version = read.data.version;
5148
5160
  if (version === null || version === void 0) {
5149
5161
  return refuse("stale_read", "Nothing was saved: this record's latest changes could not be checked.", "Reload, then try again.");
5150
5162
  }
5151
- const built = alternatePatch(read.data.document, field_key, alternate_index);
5163
+ const raw = await client.recordAsOf({ record_id, recorded_at: (/* @__PURE__ */ new Date()).toISOString() });
5164
+ if (!raw.ok) return raw;
5165
+ if (raw.data === null) return refuse("not_found", "That record is no longer here.", "Reload, then look again.");
5166
+ const built = alternatePatch(raw.data, field_key, alternate_index, rank);
5152
5167
  if (!built.ok) return refuse("not_found", built.message, "Reload, then look again.");
5153
5168
  return client.recordUpdate({ record_id, patch: built.patch, expectedVersion: version });
5154
5169
  }