@ai-matrx/records 0.96.14 → 0.97.1
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 +21 -0
- package/dist/{client-D7bvxEZW.d.ts → client-MVuEbxKl.d.ts} +6 -0
- package/dist/{client-DKgbZUNt.d.cts → client-pFV_8AhM.d.cts} +6 -0
- package/dist/core/index.cjs +637 -3
- package/dist/core/index.cjs.map +1 -1
- package/dist/core/index.d.cts +3 -2
- package/dist/core/index.d.ts +3 -2
- package/dist/core/index.js +637 -3
- package/dist/core/index.js.map +1 -1
- package/dist/data/index.cjs +634 -0
- package/dist/data/index.cjs.map +1 -1
- package/dist/data/index.d.cts +2 -1
- package/dist/data/index.d.ts +2 -1
- package/dist/data/index.js +634 -0
- package/dist/data/index.js.map +1 -1
- package/dist/{declareTable-VoNRi_j_.d.cts → declareTable-Bekj6UwK.d.cts} +1 -1
- package/dist/{declareTable-D_CiMnXM.d.ts → declareTable-C7zVyyLA.d.ts} +1 -1
- package/dist/doors-C43_5HjC.d.ts +487 -0
- package/dist/doors-CJPHsohF.d.cts +487 -0
- package/dist/questions/index.d.cts +2 -1
- package/dist/questions/index.d.ts +2 -1
- package/dist/react/index.cjs +637 -3
- package/dist/react/index.cjs.map +1 -1
- package/dist/react/index.d.cts +338 -5
- package/dist/react/index.d.ts +338 -5
- package/dist/react/index.js +637 -3
- package/dist/react/index.js.map +1 -1
- package/dist/scopes/index.cjs +729 -0
- package/dist/scopes/index.cjs.map +1 -0
- package/dist/scopes/index.d.cts +33 -0
- package/dist/scopes/index.d.ts +33 -0
- package/dist/scopes/index.js +706 -0
- package/dist/scopes/index.js.map +1 -0
- package/dist/setup/index.cjs +634 -0
- package/dist/setup/index.cjs.map +1 -1
- package/dist/setup/index.d.cts +2 -1
- package/dist/setup/index.d.ts +2 -1
- package/dist/setup/index.js +634 -0
- package/dist/setup/index.js.map +1 -1
- package/dist/typed-table/index.cjs +634 -0
- package/dist/typed-table/index.cjs.map +1 -1
- package/dist/typed-table/index.d.cts +3 -2
- package/dist/typed-table/index.d.ts +3 -2
- package/dist/typed-table/index.js +634 -0
- package/dist/typed-table/index.js.map +1 -1
- package/dist/views/index.d.cts +2 -1
- package/dist/views/index.d.ts +2 -1
- package/package.json +13 -3
package/dist/core/index.js
CHANGED
|
@@ -5157,6 +5157,639 @@ function onTableStructureChanged(listener) {
|
|
|
5157
5157
|
return () => tableListeners.delete(listener);
|
|
5158
5158
|
}
|
|
5159
5159
|
|
|
5160
|
+
// src/scopes/codec.ts
|
|
5161
|
+
var str = (v, fallback = "") => typeof v === "string" ? v : fallback;
|
|
5162
|
+
var strOrNull = (v) => typeof v === "string" ? v : null;
|
|
5163
|
+
var num = (v, fallback = 0) => {
|
|
5164
|
+
const n = typeof v === "number" ? v : typeof v === "string" ? Number(v) : NaN;
|
|
5165
|
+
return Number.isFinite(n) ? n : fallback;
|
|
5166
|
+
};
|
|
5167
|
+
function scopeTypeFromStore(row) {
|
|
5168
|
+
const singular = row.label_singular ?? row.name ?? "";
|
|
5169
|
+
return {
|
|
5170
|
+
id: row.id,
|
|
5171
|
+
organization_id: row.organization_id,
|
|
5172
|
+
label_singular: singular,
|
|
5173
|
+
label_plural: row.label_plural ?? singular,
|
|
5174
|
+
icon: row.icon ?? "folder",
|
|
5175
|
+
color: row.color ?? "",
|
|
5176
|
+
slug: row.slug ?? null,
|
|
5177
|
+
description: row.description ?? "",
|
|
5178
|
+
sort_order: num(row.sort_order),
|
|
5179
|
+
max_assignments_per_entity: row.max_assignments_per_entity ?? null,
|
|
5180
|
+
default_variable_keys: Array.isArray(row.default_variable_keys) ? row.default_variable_keys : [],
|
|
5181
|
+
created_by: row.created_by ?? null,
|
|
5182
|
+
created_at: row.created_at ?? "",
|
|
5183
|
+
updated_at: row.updated_at ?? ""
|
|
5184
|
+
};
|
|
5185
|
+
}
|
|
5186
|
+
function scopeTypeLabelFromStore(t) {
|
|
5187
|
+
const singular = t.label_singular ?? t.name ?? "";
|
|
5188
|
+
return { id: t.id, label_singular: singular, label_plural: t.label_plural ?? singular, icon: t.icon ?? null, color: t.color ?? null };
|
|
5189
|
+
}
|
|
5190
|
+
function settingFromStore(value) {
|
|
5191
|
+
if (!Array.isArray(value)) return value;
|
|
5192
|
+
return value.map((el) => {
|
|
5193
|
+
if (typeof el !== "string") return el;
|
|
5194
|
+
const t = el.trim();
|
|
5195
|
+
if (!(t.startsWith("{") && t.endsWith("}")) && !(t.startsWith("[") && t.endsWith("]"))) return el;
|
|
5196
|
+
try {
|
|
5197
|
+
return JSON.parse(t);
|
|
5198
|
+
} catch {
|
|
5199
|
+
return el;
|
|
5200
|
+
}
|
|
5201
|
+
});
|
|
5202
|
+
}
|
|
5203
|
+
function scopeFromStore(row) {
|
|
5204
|
+
const settings = {};
|
|
5205
|
+
for (const [k, v] of Object.entries(row.settings ?? {})) settings[k] = settingFromStore(v);
|
|
5206
|
+
return {
|
|
5207
|
+
id: row.id,
|
|
5208
|
+
scope_type_id: row.scope_type_id,
|
|
5209
|
+
organization_id: row.organization_id,
|
|
5210
|
+
name: row.name ?? "",
|
|
5211
|
+
description: row.description ?? "",
|
|
5212
|
+
slug: row.slug ?? null,
|
|
5213
|
+
sort_order: num(row.sort_order),
|
|
5214
|
+
parent_scope_id: row.parent_scope_id ?? null,
|
|
5215
|
+
settings,
|
|
5216
|
+
created_by: row.created_by ?? null,
|
|
5217
|
+
created_at: row.created_at ?? "",
|
|
5218
|
+
updated_at: row.updated_at ?? ""
|
|
5219
|
+
};
|
|
5220
|
+
}
|
|
5221
|
+
function scopeWithTypeFromStore(row) {
|
|
5222
|
+
return { ...scopeFromStore(row), scope_type: row.scope_type ? scopeTypeLabelFromStore(row.scope_type) : null };
|
|
5223
|
+
}
|
|
5224
|
+
function archivedScopeTypeFromStore(row) {
|
|
5225
|
+
const singular = row.label_singular ?? "";
|
|
5226
|
+
return {
|
|
5227
|
+
id: row.id,
|
|
5228
|
+
organization_id: row.organization_id,
|
|
5229
|
+
label_singular: singular,
|
|
5230
|
+
label_plural: row.label_plural ?? singular,
|
|
5231
|
+
icon: row.icon ?? "folder",
|
|
5232
|
+
color: row.color ?? "",
|
|
5233
|
+
archived_at: row.deleted_at ?? "",
|
|
5234
|
+
created_by: row.created_by ?? null,
|
|
5235
|
+
archived_scope_count: row.archived_scope_count ?? 0
|
|
5236
|
+
};
|
|
5237
|
+
}
|
|
5238
|
+
var FILE_KERNEL_ID = "11111111-0000-4000-8000-000000000006";
|
|
5239
|
+
var KINDS = /* @__PURE__ */ new Set([
|
|
5240
|
+
"string",
|
|
5241
|
+
"markdown",
|
|
5242
|
+
"email",
|
|
5243
|
+
"url",
|
|
5244
|
+
"phone",
|
|
5245
|
+
"color",
|
|
5246
|
+
"number",
|
|
5247
|
+
"percent",
|
|
5248
|
+
"currency",
|
|
5249
|
+
"boolean",
|
|
5250
|
+
"date",
|
|
5251
|
+
"datetime",
|
|
5252
|
+
"time",
|
|
5253
|
+
"object",
|
|
5254
|
+
"array",
|
|
5255
|
+
"reference",
|
|
5256
|
+
"document"
|
|
5257
|
+
]);
|
|
5258
|
+
function contextFieldKind(doc, carried) {
|
|
5259
|
+
if (carried?.as_text && carried.value_type && KINDS.has(carried.value_type)) return carried.value_type;
|
|
5260
|
+
const behavior = String(doc.type ?? "text");
|
|
5261
|
+
const fmt = doc.format ? String(doc.format) : "";
|
|
5262
|
+
const shown = doc.display_format && typeof doc.display_format === "object" ? String(doc.display_format.id ?? "") : "";
|
|
5263
|
+
const kind = String(doc.config?.kind ?? "");
|
|
5264
|
+
const multi = Boolean(doc.multi);
|
|
5265
|
+
if (behavior === "boolean") return "boolean";
|
|
5266
|
+
if (behavior === "relation") return doc.relation_target === FILE_KERNEL_ID && !multi ? "document" : "reference";
|
|
5267
|
+
if (behavior === "range") {
|
|
5268
|
+
if (kind === "date") return "date";
|
|
5269
|
+
if (kind === "datetime") return "datetime";
|
|
5270
|
+
if (fmt === "percent" || fmt === "currency") return fmt;
|
|
5271
|
+
return "number";
|
|
5272
|
+
}
|
|
5273
|
+
if (behavior === "text") {
|
|
5274
|
+
if (multi) return "array";
|
|
5275
|
+
if (fmt === "json") return "object";
|
|
5276
|
+
for (const word of [fmt, shown]) {
|
|
5277
|
+
if (word === "time" || word === "email" || word === "url" || word === "phone" || word === "color" || word === "markdown") return word;
|
|
5278
|
+
}
|
|
5279
|
+
}
|
|
5280
|
+
return "string";
|
|
5281
|
+
}
|
|
5282
|
+
var POLICIES = /* @__PURE__ */ new Set(["include", "on_request", "exclude"]);
|
|
5283
|
+
var SENSITIVITIES = /* @__PURE__ */ new Set(["public", "internal", "confidential", "restricted"]);
|
|
5284
|
+
function contextFieldFromStore(row) {
|
|
5285
|
+
const d = row.data ?? {};
|
|
5286
|
+
const key = str(d.key);
|
|
5287
|
+
const policy = str(d.context_policy, "include");
|
|
5288
|
+
const sensitivity = str(d.sensitivity, "internal");
|
|
5289
|
+
return {
|
|
5290
|
+
id: row.id,
|
|
5291
|
+
scope_type_id: row.scope_type_id,
|
|
5292
|
+
organization_id: row.organization_id,
|
|
5293
|
+
key,
|
|
5294
|
+
label: str(d.label, key),
|
|
5295
|
+
description: str(d.description),
|
|
5296
|
+
kind: contextFieldKind(d, row.carried),
|
|
5297
|
+
type: str(d.type, "text"),
|
|
5298
|
+
multi: Boolean(d.multi),
|
|
5299
|
+
format: strOrNull(d.format),
|
|
5300
|
+
config: d.config ?? {},
|
|
5301
|
+
relation_target: strOrNull(d.relation_target),
|
|
5302
|
+
// The store's sort counts the scope's own two columns first (custom._ctx_store_item: + 2).
|
|
5303
|
+
sort: Math.max(0, num(d.sort) - 2),
|
|
5304
|
+
context_policy: POLICIES.has(policy) ? policy : "include",
|
|
5305
|
+
sensitivity: SENSITIVITIES.has(sensitivity) ? sensitivity : "internal",
|
|
5306
|
+
source: str(d.source, "manual"),
|
|
5307
|
+
status: str(d.status, "active"),
|
|
5308
|
+
status_note: strOrNull(d.status_note),
|
|
5309
|
+
category: strOrNull(d.category),
|
|
5310
|
+
tags: Array.isArray(d.tags) ? d.tags : [],
|
|
5311
|
+
max_items: typeof d.max_items === "number" ? d.max_items : 1,
|
|
5312
|
+
custom_component: d.custom_component ?? null,
|
|
5313
|
+
reference_source: d.reference_source ?? null,
|
|
5314
|
+
allowed_scope_type_ids: Array.isArray(d.allowed_scope_type_ids) ? d.allowed_scope_type_ids : null,
|
|
5315
|
+
allowed_reference_types: Array.isArray(d.allowed_reference_types) ? d.allowed_reference_types : null,
|
|
5316
|
+
review_interval_days: typeof d.review_interval_days === "number" ? d.review_interval_days : null,
|
|
5317
|
+
depends_on: Array.isArray(d.depends_on) ? d.depends_on : [],
|
|
5318
|
+
version: num(row.version, 1),
|
|
5319
|
+
created_by: row.created_by ?? null,
|
|
5320
|
+
created_at: row.created_at ?? "",
|
|
5321
|
+
updated_at: row.updated_at ?? ""
|
|
5322
|
+
};
|
|
5323
|
+
}
|
|
5324
|
+
var POLICY_TO_DOOR = { include: "always", on_request: "on_demand", exclude: "never" };
|
|
5325
|
+
var SENSITIVITY_TO_DOOR = {
|
|
5326
|
+
public: "public",
|
|
5327
|
+
internal: "internal",
|
|
5328
|
+
confidential: "restricted",
|
|
5329
|
+
restricted: "privileged"
|
|
5330
|
+
};
|
|
5331
|
+
var UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
5332
|
+
function referencesOf(row) {
|
|
5333
|
+
const v = row.value;
|
|
5334
|
+
const list = Array.isArray(v) ? v : v == null ? [] : [v];
|
|
5335
|
+
const labels = row.labels ?? {};
|
|
5336
|
+
const files = row.files ?? {};
|
|
5337
|
+
const toFile = row.field?.relation_target === FILE_KERNEL_ID;
|
|
5338
|
+
const out = [];
|
|
5339
|
+
for (const el of list) {
|
|
5340
|
+
let ref = null;
|
|
5341
|
+
if (typeof el === "string" && UUID.test(el)) ref = { id: el, type: toFile ? "file" : "scope" };
|
|
5342
|
+
else if (el && typeof el === "object" && typeof el.id === "string") {
|
|
5343
|
+
const token = String(el.token ?? "");
|
|
5344
|
+
const own = el.label;
|
|
5345
|
+
ref = { id: el.id, type: token === "dataset" ? "table" : token || (toFile ? "file" : "scope") };
|
|
5346
|
+
if (typeof own === "string" && own) ref.label = own;
|
|
5347
|
+
}
|
|
5348
|
+
if (!ref) continue;
|
|
5349
|
+
const label = labels[ref.id];
|
|
5350
|
+
if (label) ref.label = label;
|
|
5351
|
+
const fileId = files[ref.id];
|
|
5352
|
+
if (fileId) ref.file_id = fileId;
|
|
5353
|
+
out.push(ref);
|
|
5354
|
+
}
|
|
5355
|
+
return out;
|
|
5356
|
+
}
|
|
5357
|
+
function contextValueFromStore(row) {
|
|
5358
|
+
const kind = contextFieldKind(row.field ?? {}, row.field?.carried);
|
|
5359
|
+
const carriedText = Boolean(row.field?.carried?.as_text);
|
|
5360
|
+
const isRef = !carriedText && (kind === "reference" || kind === "document");
|
|
5361
|
+
return {
|
|
5362
|
+
scope_id: row.scope_id,
|
|
5363
|
+
field_id: row.context_item_id,
|
|
5364
|
+
key: row.key,
|
|
5365
|
+
kind,
|
|
5366
|
+
value: row.value ?? null,
|
|
5367
|
+
references: isRef ? referencesOf(row) : [],
|
|
5368
|
+
version: num(row.version, 1),
|
|
5369
|
+
set_at: row.set_at ?? null,
|
|
5370
|
+
source_type: row.source_type ?? null,
|
|
5371
|
+
authored_by: row.authored_by ?? null,
|
|
5372
|
+
whole_value: row.whole_value ?? null,
|
|
5373
|
+
incomplete: null
|
|
5374
|
+
};
|
|
5375
|
+
}
|
|
5376
|
+
async function sha256Hex(text2) {
|
|
5377
|
+
const subtle = globalThis.crypto?.subtle;
|
|
5378
|
+
if (!subtle) return null;
|
|
5379
|
+
const digest = await subtle.digest("SHA-256", new TextEncoder().encode(text2));
|
|
5380
|
+
return Array.from(new Uint8Array(digest), (b) => b.toString(16).padStart(2, "0")).join("");
|
|
5381
|
+
}
|
|
5382
|
+
async function completeWholeValues(values, read) {
|
|
5383
|
+
const texts = /* @__PURE__ */ new Map();
|
|
5384
|
+
await Promise.all(
|
|
5385
|
+
values.map(async (cell) => {
|
|
5386
|
+
const w = cell.whole_value;
|
|
5387
|
+
if (!w || w.in_value || typeof cell.value !== "string") return;
|
|
5388
|
+
const head = cell.value;
|
|
5389
|
+
const chars = typeof w.chars === "number" ? w.chars : null;
|
|
5390
|
+
const incomplete = (fileId, why) => {
|
|
5391
|
+
cell.incomplete = { head, chars, file_id: fileId, why };
|
|
5392
|
+
};
|
|
5393
|
+
if (!w.file_id) return incomplete(null, "The rest of this text is still being saved.");
|
|
5394
|
+
if (!read) return incomplete(w.file_id, "This place cannot open the file that holds the whole text.");
|
|
5395
|
+
if (!texts.has(w.file_id)) texts.set(w.file_id, read(w.file_id));
|
|
5396
|
+
try {
|
|
5397
|
+
const text2 = await texts.get(w.file_id);
|
|
5398
|
+
if (chars !== null && [...text2].length !== chars) {
|
|
5399
|
+
return incomplete(w.file_id, `The file holds ${[...text2].length} characters, not ${chars}.`);
|
|
5400
|
+
}
|
|
5401
|
+
if (w.sha256) {
|
|
5402
|
+
const sha = await sha256Hex(text2);
|
|
5403
|
+
if (sha === null) return incomplete(w.file_id, "This browser cannot check the file.");
|
|
5404
|
+
if (sha !== w.sha256) return incomplete(w.file_id, "The file's content is not the text the cell points at.");
|
|
5405
|
+
}
|
|
5406
|
+
cell.value = text2;
|
|
5407
|
+
} catch (thrown) {
|
|
5408
|
+
incomplete(w.file_id, thrown instanceof Error ? thrown.message : String(thrown));
|
|
5409
|
+
}
|
|
5410
|
+
})
|
|
5411
|
+
);
|
|
5412
|
+
}
|
|
5413
|
+
function definedOnly(fields) {
|
|
5414
|
+
const out = {};
|
|
5415
|
+
for (const [k, v] of Object.entries(fields)) if (v !== void 0) out[k] = v;
|
|
5416
|
+
return out;
|
|
5417
|
+
}
|
|
5418
|
+
function fieldSpecForDoor(spec) {
|
|
5419
|
+
return definedOnly({
|
|
5420
|
+
key: spec.key,
|
|
5421
|
+
display_name: spec.label,
|
|
5422
|
+
value_type: spec.kind,
|
|
5423
|
+
description: spec.description,
|
|
5424
|
+
category: spec.category,
|
|
5425
|
+
fetch_hint: spec.context_policy ? POLICY_TO_DOOR[spec.context_policy] : void 0,
|
|
5426
|
+
sensitivity: spec.sensitivity ? SENSITIVITY_TO_DOOR[spec.sensitivity] : void 0,
|
|
5427
|
+
tags: spec.tags,
|
|
5428
|
+
sort_order: spec.sort,
|
|
5429
|
+
status: spec.status,
|
|
5430
|
+
status_note: spec.status_note,
|
|
5431
|
+
custom_component: spec.custom_component,
|
|
5432
|
+
review_interval_days: spec.review_interval_days,
|
|
5433
|
+
allowed_reference_types: spec.allowed_reference_types,
|
|
5434
|
+
max_items: spec.max_items,
|
|
5435
|
+
allowed_scope_type_ids: spec.allowed_scope_type_ids,
|
|
5436
|
+
reference_source: spec.reference_source
|
|
5437
|
+
});
|
|
5438
|
+
}
|
|
5439
|
+
function referenceFence(references) {
|
|
5440
|
+
const kinds = [...new Set(references.map((r) => r.type))];
|
|
5441
|
+
const type = kinds.length === 1 ? kinds[0] : "mixed";
|
|
5442
|
+
const items = references.map((r) => {
|
|
5443
|
+
if (r.type === "file" && r.file_id) return r.label ? { file_id: r.file_id, label: r.label } : { file_id: r.file_id };
|
|
5444
|
+
const item2 = { id: r.id };
|
|
5445
|
+
if (r.label) item2.label = r.label;
|
|
5446
|
+
if (kinds.length > 1) item2.type = r.type;
|
|
5447
|
+
return item2;
|
|
5448
|
+
});
|
|
5449
|
+
return "```matrx\n" + JSON.stringify({ matrx_version: 1, kind: "reference", type, items }, null, 2) + "\n```";
|
|
5450
|
+
}
|
|
5451
|
+
function valuePayloadForDoor(write) {
|
|
5452
|
+
const base = definedOnly({
|
|
5453
|
+
scope_id: write.scope_id,
|
|
5454
|
+
context_item_id: write.field_id,
|
|
5455
|
+
source_type: write.source_type,
|
|
5456
|
+
change_summary: write.change_summary
|
|
5457
|
+
});
|
|
5458
|
+
const v = write.value;
|
|
5459
|
+
if (write.kind === "reference" || write.kind === "document") {
|
|
5460
|
+
if (write.references) return { ...base, value_text: write.references.length ? referenceFence(write.references) : null };
|
|
5461
|
+
if (write.kind === "document" && typeof v === "string") return { ...base, value_document_url: v || null };
|
|
5462
|
+
return { ...base, value_text: typeof v === "string" ? v || null : null };
|
|
5463
|
+
}
|
|
5464
|
+
if (v === null || v === void 0 || v === "") {
|
|
5465
|
+
return { ...base, value_text: null };
|
|
5466
|
+
}
|
|
5467
|
+
switch (write.kind) {
|
|
5468
|
+
case "number":
|
|
5469
|
+
case "percent":
|
|
5470
|
+
return typeof v === "number" ? { ...base, value_number: v } : { ...base, value_text: String(v) };
|
|
5471
|
+
case "currency":
|
|
5472
|
+
return typeof v === "object" ? { ...base, value_json: v } : typeof v === "number" ? { ...base, value_number: v } : { ...base, value_text: String(v) };
|
|
5473
|
+
case "boolean":
|
|
5474
|
+
return typeof v === "boolean" ? { ...base, value_boolean: v } : { ...base, value_text: String(v) };
|
|
5475
|
+
case "date":
|
|
5476
|
+
return { ...base, value_date: String(v) };
|
|
5477
|
+
case "datetime":
|
|
5478
|
+
return { ...base, value_timestamp: String(v) };
|
|
5479
|
+
case "time":
|
|
5480
|
+
return { ...base, value_time: String(v) };
|
|
5481
|
+
case "object":
|
|
5482
|
+
case "array":
|
|
5483
|
+
return typeof v === "object" ? { ...base, value_json: v } : { ...base, value_text: String(v) };
|
|
5484
|
+
default:
|
|
5485
|
+
return typeof v === "object" ? { ...base, value_json: v } : { ...base, value_text: String(v) };
|
|
5486
|
+
}
|
|
5487
|
+
}
|
|
5488
|
+
|
|
5489
|
+
// src/scopes/rules.ts
|
|
5490
|
+
function toSlug(name) {
|
|
5491
|
+
return name.toLowerCase().trim().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "");
|
|
5492
|
+
}
|
|
5493
|
+
function isValidSlug(s) {
|
|
5494
|
+
return /^[a-z0-9]+(?:[-_][a-z0-9]+)*$/.test(s);
|
|
5495
|
+
}
|
|
5496
|
+
var RESERVED_SCOPE_SLUGS = /* @__PURE__ */ new Set(["context-items", "scopes", "edit", "manage", "new", "settings"]);
|
|
5497
|
+
function isReservedSlug(s) {
|
|
5498
|
+
return RESERVED_SCOPE_SLUGS.has(s.toLowerCase().replace(/_/g, "-"));
|
|
5499
|
+
}
|
|
5500
|
+
function resolveSlug(supplied, fallbackName) {
|
|
5501
|
+
const slug = supplied?.trim() || toSlug(fallbackName);
|
|
5502
|
+
if (!slug || !isValidSlug(slug)) {
|
|
5503
|
+
return {
|
|
5504
|
+
ok: false,
|
|
5505
|
+
error: { code: "invalid_argument", message: "Use letters, numbers and single hyphens for the short name." }
|
|
5506
|
+
};
|
|
5507
|
+
}
|
|
5508
|
+
if (isReservedSlug(slug)) {
|
|
5509
|
+
return { ok: false, error: { code: "invalid_argument", message: `"${slug}" is a page name here. Pick another short name.` } };
|
|
5510
|
+
}
|
|
5511
|
+
return { ok: true, data: slug };
|
|
5512
|
+
}
|
|
5513
|
+
|
|
5514
|
+
// src/scopes/doors.ts
|
|
5515
|
+
var SCOPES_PER_CALL = 500;
|
|
5516
|
+
var SCOPE_TYPES_PER_FIELDS_CALL = 100;
|
|
5517
|
+
var SCOPES_PER_VALUES_CALL = 200;
|
|
5518
|
+
var TYPE_SCOPES_PAGE = 200;
|
|
5519
|
+
var ARCHIVED_SCOPES_PAGE = 200;
|
|
5520
|
+
var uniq = (ids) => [...new Set(ids.filter(Boolean))];
|
|
5521
|
+
async function inBatches(ids, size, call) {
|
|
5522
|
+
const unique = uniq(ids);
|
|
5523
|
+
const batches = [];
|
|
5524
|
+
for (let i = 0; i < unique.length; i += size) batches.push(unique.slice(i, i + size));
|
|
5525
|
+
const answers = await Promise.all(batches.map(call));
|
|
5526
|
+
const out = [];
|
|
5527
|
+
for (const a of answers) {
|
|
5528
|
+
if (!a.ok) return a;
|
|
5529
|
+
out.push(...a.data ?? []);
|
|
5530
|
+
}
|
|
5531
|
+
return { ok: true, data: out };
|
|
5532
|
+
}
|
|
5533
|
+
function noRow(door) {
|
|
5534
|
+
return { ok: false, error: { code: "internal", message: `${door} answered without the row it wrote.`, diagnostic: `@ai-matrx/records/scopes: ${door}` } };
|
|
5535
|
+
}
|
|
5536
|
+
var VALUE_DOOR_CODES = {
|
|
5537
|
+
unauthorized: "door",
|
|
5538
|
+
forbidden: "door",
|
|
5539
|
+
forbidden_org: "door",
|
|
5540
|
+
not_found: "not_found",
|
|
5541
|
+
invalid_argument: "invalid_argument",
|
|
5542
|
+
conflict: "stale_write",
|
|
5543
|
+
unavailable: "not_supported"
|
|
5544
|
+
};
|
|
5545
|
+
function createScopeDoors(call) {
|
|
5546
|
+
async function write(door, args) {
|
|
5547
|
+
const res = await call(door, args, `scopes.${door}`);
|
|
5548
|
+
if (!res.ok) return res;
|
|
5549
|
+
const row = res.data?.row;
|
|
5550
|
+
if (!row || typeof row !== "object") return noRow(door);
|
|
5551
|
+
return { ok: true, data: row };
|
|
5552
|
+
}
|
|
5553
|
+
async function archiveOrRestore(door, args, id) {
|
|
5554
|
+
const res = await call(door, args, `scopes.${door}`);
|
|
5555
|
+
return res.ok ? { ok: true, data: { id } } : res;
|
|
5556
|
+
}
|
|
5557
|
+
async function fieldById(scopeTypeId, fieldId, door) {
|
|
5558
|
+
const fields = await doors.fields([scopeTypeId]);
|
|
5559
|
+
if (!fields.ok) return fields;
|
|
5560
|
+
const field = fields.data.find((f) => f.id === fieldId);
|
|
5561
|
+
return field ? { ok: true, data: field } : noRow(door);
|
|
5562
|
+
}
|
|
5563
|
+
const doors = {
|
|
5564
|
+
async tree(organizationIds) {
|
|
5565
|
+
const ids = uniq(organizationIds);
|
|
5566
|
+
if (ids.length === 0) return { ok: true, data: [] };
|
|
5567
|
+
const res = await call("context_tree", { p_organization_ids: ids }, "scopes.tree");
|
|
5568
|
+
if (!res.ok) return res;
|
|
5569
|
+
const byType = /* @__PURE__ */ new Map();
|
|
5570
|
+
for (const s of res.data?.scopes ?? []) {
|
|
5571
|
+
const scope = scopeFromStore(s);
|
|
5572
|
+
const list = byType.get(scope.scope_type_id) ?? [];
|
|
5573
|
+
list.push(scope);
|
|
5574
|
+
byType.set(scope.scope_type_id, list);
|
|
5575
|
+
}
|
|
5576
|
+
return { ok: true, data: (res.data?.types ?? []).map((t) => ({ ...scopeTypeFromStore(t), scopes: byType.get(t.id) ?? [] })) };
|
|
5577
|
+
},
|
|
5578
|
+
async types(organizationIds, options) {
|
|
5579
|
+
const withCounts = options?.withCounts ?? true;
|
|
5580
|
+
const ids = uniq(organizationIds);
|
|
5581
|
+
if (ids.length === 0) return { ok: true, data: { types: [], counts: withCounts ? {} : null } };
|
|
5582
|
+
const res = await call("context_tree_types", { p_organization_ids: ids, p_with_counts: withCounts }, "scopes.types");
|
|
5583
|
+
if (!res.ok) return res;
|
|
5584
|
+
const rows2 = res.data?.types ?? [];
|
|
5585
|
+
const counts = withCounts ? {} : null;
|
|
5586
|
+
if (counts) for (const r of rows2) counts[r.id] = Number(r.scope_count ?? 0);
|
|
5587
|
+
return { ok: true, data: { types: rows2.map(scopeTypeFromStore), counts } };
|
|
5588
|
+
},
|
|
5589
|
+
async typeScopesPage(scopeTypeId, offset = 0, limit = TYPE_SCOPES_PAGE) {
|
|
5590
|
+
const res = await call(
|
|
5591
|
+
"context_tree_type_scopes",
|
|
5592
|
+
{ p_scope_type_id: scopeTypeId, p_offset: offset, p_limit: limit },
|
|
5593
|
+
"scopes.typeScopesPage"
|
|
5594
|
+
);
|
|
5595
|
+
if (!res.ok) return res;
|
|
5596
|
+
return {
|
|
5597
|
+
ok: true,
|
|
5598
|
+
data: { scopes: (res.data?.scopes ?? []).map(scopeFromStore), total: Number(res.data?.total ?? 0), next_offset: res.data?.next_offset ?? null }
|
|
5599
|
+
};
|
|
5600
|
+
},
|
|
5601
|
+
async search(organizationIds, query, limit = 100) {
|
|
5602
|
+
const ids = uniq(organizationIds);
|
|
5603
|
+
const q = query.trim();
|
|
5604
|
+
if (ids.length === 0 || q === "") return { ok: true, data: { scopes: [], total: 0 } };
|
|
5605
|
+
const res = await call(
|
|
5606
|
+
"context_tree_search",
|
|
5607
|
+
{ p_organization_ids: ids, p_query: q, p_limit: limit },
|
|
5608
|
+
"scopes.search"
|
|
5609
|
+
);
|
|
5610
|
+
if (!res.ok) return res;
|
|
5611
|
+
return { ok: true, data: { scopes: (res.data?.scopes ?? []).map(scopeFromStore), total: Number(res.data?.total ?? 0) } };
|
|
5612
|
+
},
|
|
5613
|
+
async scopes(ids) {
|
|
5614
|
+
const res = await inBatches(ids, SCOPES_PER_CALL, (batch) => call("context_scopes", { p_scope_ids: batch }, "scopes.scopes"));
|
|
5615
|
+
return res.ok ? { ok: true, data: res.data.map(scopeWithTypeFromStore) } : res;
|
|
5616
|
+
},
|
|
5617
|
+
async fields(scopeTypeIds) {
|
|
5618
|
+
const res = await inBatches(
|
|
5619
|
+
scopeTypeIds,
|
|
5620
|
+
SCOPE_TYPES_PER_FIELDS_CALL,
|
|
5621
|
+
(batch) => call("context_items", { p_scope_type_ids: batch }, "scopes.fields")
|
|
5622
|
+
);
|
|
5623
|
+
return res.ok ? { ok: true, data: res.data.map(contextFieldFromStore) } : res;
|
|
5624
|
+
},
|
|
5625
|
+
async values(scopeIds, options) {
|
|
5626
|
+
const res = await inBatches(
|
|
5627
|
+
scopeIds,
|
|
5628
|
+
SCOPES_PER_VALUES_CALL,
|
|
5629
|
+
(batch) => call("context_values", { p_scope_ids: batch }, "scopes.values")
|
|
5630
|
+
);
|
|
5631
|
+
if (!res.ok) return res;
|
|
5632
|
+
const cells = res.data.map(contextValueFromStore);
|
|
5633
|
+
await completeWholeValues(cells, options?.readFileText ?? null);
|
|
5634
|
+
return { ok: true, data: cells };
|
|
5635
|
+
},
|
|
5636
|
+
async archivedTypes(organizationId) {
|
|
5637
|
+
const res = await call("context_archived_types", { p_organization_id: organizationId }, "scopes.archivedTypes");
|
|
5638
|
+
return res.ok ? { ok: true, data: (res.data ?? []).map(archivedScopeTypeFromStore) } : res;
|
|
5639
|
+
},
|
|
5640
|
+
async archivedScopesOfType(organizationId, scopeTypeId, wanted) {
|
|
5641
|
+
const out = [];
|
|
5642
|
+
for (let offset = 0; out.length < wanted.size; offset += ARCHIVED_SCOPES_PAGE) {
|
|
5643
|
+
const res = await call(
|
|
5644
|
+
"read_records_archived",
|
|
5645
|
+
{ p_organization_id: organizationId, p_table_id: scopeTypeId, p_lane: "org", p_by_id: false, p_limit: ARCHIVED_SCOPES_PAGE, p_offset: offset },
|
|
5646
|
+
"scopes.archivedScopesOfType"
|
|
5647
|
+
);
|
|
5648
|
+
if (!res.ok) return res;
|
|
5649
|
+
const rows2 = res.data ?? [];
|
|
5650
|
+
for (const r of rows2) {
|
|
5651
|
+
if (!wanted.has(r.id)) continue;
|
|
5652
|
+
out.push({
|
|
5653
|
+
id: r.id,
|
|
5654
|
+
name: typeof r.document?.name === "string" ? r.document.name : null,
|
|
5655
|
+
slug: typeof r.document?.slug === "string" ? r.document.slug : null,
|
|
5656
|
+
archived_at: r.archived_at ?? null
|
|
5657
|
+
});
|
|
5658
|
+
}
|
|
5659
|
+
if (rows2.length < ARCHIVED_SCOPES_PAGE) break;
|
|
5660
|
+
}
|
|
5661
|
+
return { ok: true, data: out };
|
|
5662
|
+
},
|
|
5663
|
+
async systemItems() {
|
|
5664
|
+
const res = await call("context_system_items", {}, "scopes.systemItems");
|
|
5665
|
+
return res.ok ? { ok: true, data: res.data ?? [] } : res;
|
|
5666
|
+
},
|
|
5667
|
+
async templates() {
|
|
5668
|
+
const res = await call("context_templates", {}, "scopes.templates");
|
|
5669
|
+
return res.ok ? { ok: true, data: res.data ?? [] } : res;
|
|
5670
|
+
},
|
|
5671
|
+
async createType(organizationId, spec) {
|
|
5672
|
+
const slug = resolveSlug(spec.slug, spec.label_plural || spec.label_singular || "");
|
|
5673
|
+
if (!slug.ok) return slug;
|
|
5674
|
+
const res = await write("context_type_write", {
|
|
5675
|
+
p_organization_id: organizationId,
|
|
5676
|
+
p_type_id: null,
|
|
5677
|
+
p_spec: definedOnly({
|
|
5678
|
+
label_singular: spec.label_singular,
|
|
5679
|
+
label_plural: spec.label_plural,
|
|
5680
|
+
icon: spec.icon ?? "folder",
|
|
5681
|
+
description: spec.description ?? "",
|
|
5682
|
+
sort_order: spec.sort_order ?? 0,
|
|
5683
|
+
max_assignments: spec.max_assignments_per_entity ?? void 0,
|
|
5684
|
+
default_variable_keys: spec.default_variable_keys ?? [],
|
|
5685
|
+
color: spec.color,
|
|
5686
|
+
slug: slug.data
|
|
5687
|
+
})
|
|
5688
|
+
});
|
|
5689
|
+
return res.ok ? { ok: true, data: scopeTypeFromStore(res.data) } : res;
|
|
5690
|
+
},
|
|
5691
|
+
async updateType(typeId, spec) {
|
|
5692
|
+
let slug;
|
|
5693
|
+
if (spec.slug !== void 0) {
|
|
5694
|
+
const resolved = resolveSlug(spec.slug, spec.label_plural ?? spec.label_singular ?? "");
|
|
5695
|
+
if (!resolved.ok) return resolved;
|
|
5696
|
+
slug = resolved.data;
|
|
5697
|
+
}
|
|
5698
|
+
const res = await write("context_type_write", {
|
|
5699
|
+
p_organization_id: null,
|
|
5700
|
+
p_type_id: typeId,
|
|
5701
|
+
p_spec: definedOnly({
|
|
5702
|
+
label_singular: spec.label_singular,
|
|
5703
|
+
label_plural: spec.label_plural,
|
|
5704
|
+
icon: spec.icon,
|
|
5705
|
+
description: spec.description,
|
|
5706
|
+
sort_order: spec.sort_order,
|
|
5707
|
+
max_assignments: spec.max_assignments_per_entity,
|
|
5708
|
+
color: spec.color,
|
|
5709
|
+
slug
|
|
5710
|
+
})
|
|
5711
|
+
});
|
|
5712
|
+
return res.ok ? { ok: true, data: scopeTypeFromStore(res.data) } : res;
|
|
5713
|
+
},
|
|
5714
|
+
archiveType: (typeId) => archiveOrRestore("context_type_archive", { p_type_id: typeId }, typeId),
|
|
5715
|
+
restoreType: (typeId) => archiveOrRestore("context_type_restore", { p_type_id: typeId }, typeId),
|
|
5716
|
+
async createScope(organizationId, typeId, spec) {
|
|
5717
|
+
const slug = resolveSlug(spec.slug, spec.name ?? "");
|
|
5718
|
+
if (!slug.ok) return slug;
|
|
5719
|
+
const res = await write("context_scope_write", {
|
|
5720
|
+
p_organization_id: organizationId,
|
|
5721
|
+
p_scope_id: null,
|
|
5722
|
+
p_type_id: typeId,
|
|
5723
|
+
p_spec: definedOnly({
|
|
5724
|
+
name: spec.name,
|
|
5725
|
+
parent_scope_id: spec.parent_scope_id,
|
|
5726
|
+
description: spec.description ?? "",
|
|
5727
|
+
settings: spec.settings ?? {},
|
|
5728
|
+
slug: slug.data,
|
|
5729
|
+
sort_order: spec.sort_order
|
|
5730
|
+
})
|
|
5731
|
+
});
|
|
5732
|
+
return res.ok ? { ok: true, data: scopeFromStore(res.data) } : res;
|
|
5733
|
+
},
|
|
5734
|
+
async updateScope(scopeId, spec) {
|
|
5735
|
+
let slug;
|
|
5736
|
+
if (spec.slug !== void 0) {
|
|
5737
|
+
const resolved = resolveSlug(spec.slug, spec.name ?? "");
|
|
5738
|
+
if (!resolved.ok) return resolved;
|
|
5739
|
+
slug = resolved.data;
|
|
5740
|
+
}
|
|
5741
|
+
const res = await write("context_scope_write", {
|
|
5742
|
+
p_organization_id: null,
|
|
5743
|
+
p_scope_id: scopeId,
|
|
5744
|
+
p_type_id: null,
|
|
5745
|
+
p_spec: definedOnly({ name: spec.name, description: spec.description, settings: spec.settings, slug, sort_order: spec.sort_order })
|
|
5746
|
+
});
|
|
5747
|
+
return res.ok ? { ok: true, data: scopeFromStore(res.data) } : res;
|
|
5748
|
+
},
|
|
5749
|
+
archiveScope: (scopeId) => archiveOrRestore("context_scope_archive", { p_scope_id: scopeId }, scopeId),
|
|
5750
|
+
restoreScope: (scopeId) => archiveOrRestore("context_scope_restore", { p_scope_id: scopeId }, scopeId),
|
|
5751
|
+
async createField(scopeTypeId, spec) {
|
|
5752
|
+
const res = await write("context_item_write", {
|
|
5753
|
+
p_item_id: null,
|
|
5754
|
+
p_scope_type_id: scopeTypeId,
|
|
5755
|
+
p_spec: fieldSpecForDoor({ kind: "string", description: "", context_policy: "on_request", sensitivity: "internal", tags: [], ...spec })
|
|
5756
|
+
});
|
|
5757
|
+
if (!res.ok) return res;
|
|
5758
|
+
return fieldById(scopeTypeId, String(res.data.id), "context_item_write");
|
|
5759
|
+
},
|
|
5760
|
+
async updateField(fieldId, scopeTypeId, spec) {
|
|
5761
|
+
const res = await write("context_item_write", { p_item_id: fieldId, p_scope_type_id: null, p_spec: fieldSpecForDoor(spec) });
|
|
5762
|
+
if (!res.ok) return res;
|
|
5763
|
+
return fieldById(scopeTypeId, fieldId, "context_item_write");
|
|
5764
|
+
},
|
|
5765
|
+
archiveField: (fieldId) => archiveOrRestore("context_item_archive", { p_item_id: fieldId }, fieldId),
|
|
5766
|
+
restoreField: (fieldId) => archiveOrRestore("context_item_restore", { p_item_id: fieldId }, fieldId),
|
|
5767
|
+
async writeValue(cell) {
|
|
5768
|
+
const res = await call(
|
|
5769
|
+
"context_value_write",
|
|
5770
|
+
{ p_payload: { source_type: "ai_enriched", ...valuePayloadForDoor(cell) } },
|
|
5771
|
+
"scopes.writeValue"
|
|
5772
|
+
);
|
|
5773
|
+
if (!res.ok) return res;
|
|
5774
|
+
const envelope = res.data ?? {};
|
|
5775
|
+
if (!envelope.ok) {
|
|
5776
|
+
const code = VALUE_DOOR_CODES[envelope.error?.code ?? ""] ?? "internal";
|
|
5777
|
+
return { ok: false, error: { code, message: envelope.error?.message ?? "The value was not saved." } };
|
|
5778
|
+
}
|
|
5779
|
+
return {
|
|
5780
|
+
ok: true,
|
|
5781
|
+
data: {
|
|
5782
|
+
scope_id: cell.scope_id,
|
|
5783
|
+
field_id: cell.field_id,
|
|
5784
|
+
version: Number(envelope.data?.version ?? 1),
|
|
5785
|
+
source_type: String(envelope.data?.source_type ?? cell.source_type ?? "ai_enriched")
|
|
5786
|
+
}
|
|
5787
|
+
};
|
|
5788
|
+
}
|
|
5789
|
+
};
|
|
5790
|
+
return doors;
|
|
5791
|
+
}
|
|
5792
|
+
|
|
5160
5793
|
// src/core/keysetCursor.ts
|
|
5161
5794
|
function utf8ToBinary(text2) {
|
|
5162
5795
|
let out = "";
|
|
@@ -6453,6 +7086,7 @@ function createRecordsClient(given) {
|
|
|
6453
7086
|
const unavailableReason = (error) => `${error.message}${error.hint ? ` ${error.hint}` : ""}`;
|
|
6454
7087
|
const client = {
|
|
6455
7088
|
config,
|
|
7089
|
+
scopes: createScopeDoors(callDoor),
|
|
6456
7090
|
async recordWrite({ table_id, data }) {
|
|
6457
7091
|
const result = await callDoor(
|
|
6458
7092
|
DOORS.recordWrite,
|
|
@@ -9980,15 +10614,15 @@ function labelOf(field) {
|
|
|
9980
10614
|
function ruleRefusal(field, rule, item2, values) {
|
|
9981
10615
|
const label = labelOf(field);
|
|
9982
10616
|
const base = { sqlstate: "23514", predicted_by: "custom.validate_values", field_key: field.key };
|
|
9983
|
-
const
|
|
10617
|
+
const num2 = typeof item2 === "number" ? item2 : null;
|
|
9984
10618
|
switch (rule.kind) {
|
|
9985
10619
|
case "min":
|
|
9986
|
-
if (
|
|
10620
|
+
if (num2 !== null && num2 < Number(rule.value)) {
|
|
9987
10621
|
return { ...base, message: `${label} has to be at least ${rule.value}`, hint: "FLD-3: an attached validation Rule, not a behavior." };
|
|
9988
10622
|
}
|
|
9989
10623
|
return null;
|
|
9990
10624
|
case "max":
|
|
9991
|
-
if (
|
|
10625
|
+
if (num2 !== null && num2 > Number(rule.value)) {
|
|
9992
10626
|
return { ...base, message: `${label} cannot be more than ${rule.value}`, hint: "FLD-3: an attached validation Rule, not a behavior." };
|
|
9993
10627
|
}
|
|
9994
10628
|
return null;
|