@fayz-ai/core 0.12.0 → 0.15.0

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.
Files changed (76) hide show
  1. package/dist/access/engine.d.ts +31 -1
  2. package/dist/access/engine.d.ts.map +1 -1
  3. package/dist/access/index.d.ts +1 -1
  4. package/dist/access/index.d.ts.map +1 -1
  5. package/dist/access/index.js +24 -3
  6. package/dist/access/index.js.map +1 -1
  7. package/dist/{chunk-KFBEKFII.js → chunk-BCI743RH.js} +37 -4
  8. package/dist/chunk-BCI743RH.js.map +1 -0
  9. package/dist/{chunk-IKZR3WSK.js → chunk-KJUD6JA5.js} +3 -3
  10. package/dist/{chunk-IKZR3WSK.js.map → chunk-KJUD6JA5.js.map} +1 -1
  11. package/dist/{chunk-S5FOFKR4.js → chunk-NBKFYGVD.js} +88 -24
  12. package/dist/chunk-NBKFYGVD.js.map +1 -0
  13. package/dist/{chunk-7GJTLBG3.js → chunk-OHH26S4N.js} +3 -3
  14. package/dist/{chunk-7GJTLBG3.js.map → chunk-OHH26S4N.js.map} +1 -1
  15. package/dist/chunk-RCMFU3RZ.js +13 -0
  16. package/dist/chunk-RCMFU3RZ.js.map +1 -0
  17. package/dist/{chunk-RRYFXUQX.js → chunk-UMIZDILP.js} +7 -3
  18. package/dist/chunk-UMIZDILP.js.map +1 -0
  19. package/dist/{chunk-U3252GZO.js → chunk-ZYHS7T5T.js} +129 -7
  20. package/dist/chunk-ZYHS7T5T.js.map +1 -0
  21. package/dist/data/archetype.d.ts +5 -0
  22. package/dist/data/archetype.d.ts.map +1 -1
  23. package/dist/data/cached.d.ts +17 -2
  24. package/dist/data/cached.d.ts.map +1 -1
  25. package/dist/data/index.js +2 -2
  26. package/dist/data/offline.d.ts +10 -1
  27. package/dist/data/offline.d.ts.map +1 -1
  28. package/dist/data/resolve.d.ts.map +1 -1
  29. package/dist/data/supabase.d.ts +5 -0
  30. package/dist/data/supabase.d.ts.map +1 -1
  31. package/dist/data/unit-scope.d.ts +48 -0
  32. package/dist/data/unit-scope.d.ts.map +1 -0
  33. package/dist/events/index.d.ts +2 -0
  34. package/dist/events/index.d.ts.map +1 -1
  35. package/dist/events/log.d.ts.map +1 -1
  36. package/dist/events/useDataVersion.d.ts +28 -0
  37. package/dist/events/useDataVersion.d.ts.map +1 -0
  38. package/dist/i18n/index.d.ts.map +1 -1
  39. package/dist/i18n/index.js +1 -1
  40. package/dist/i18n/shell-translations.en.d.ts.map +1 -1
  41. package/dist/i18n/shell-translations.pt-br.d.ts.map +1 -1
  42. package/dist/index.d.ts +7 -1
  43. package/dist/index.d.ts.map +1 -1
  44. package/dist/index.js +190 -38
  45. package/dist/index.js.map +1 -1
  46. package/dist/integrations/index.js +2 -2
  47. package/dist/lib/unit.d.ts +20 -0
  48. package/dist/lib/unit.d.ts.map +1 -0
  49. package/dist/manifest/index.d.ts +45 -0
  50. package/dist/manifest/index.d.ts.map +1 -1
  51. package/dist/manifest/integration-surface.d.ts +16 -0
  52. package/dist/manifest/integration-surface.d.ts.map +1 -0
  53. package/dist/plugin/index.js +1 -1
  54. package/dist/plugin/runtime.d.ts.map +1 -1
  55. package/dist/{shell-translations.pt-br-ANQ64XTT.js → shell-translations.pt-br-MHKHD3IX.js} +102 -6
  56. package/dist/shell-translations.pt-br-MHKHD3IX.js.map +1 -0
  57. package/dist/testing/index.js +3 -3
  58. package/dist/types/agents.d.ts +97 -0
  59. package/dist/types/agents.d.ts.map +1 -0
  60. package/dist/types/auth.d.ts +9 -0
  61. package/dist/types/auth.d.ts.map +1 -1
  62. package/dist/types/crud.d.ts +22 -0
  63. package/dist/types/crud.d.ts.map +1 -1
  64. package/dist/types/index.d.ts +2 -0
  65. package/dist/types/index.d.ts.map +1 -1
  66. package/dist/types/index.js +1 -1
  67. package/dist/types/org.d.ts +10 -0
  68. package/dist/types/org.d.ts.map +1 -1
  69. package/dist/types/plugins.d.ts +24 -0
  70. package/dist/types/plugins.d.ts.map +1 -1
  71. package/package.json +5 -5
  72. package/dist/chunk-KFBEKFII.js.map +0 -1
  73. package/dist/chunk-RRYFXUQX.js.map +0 -1
  74. package/dist/chunk-S5FOFKR4.js.map +0 -1
  75. package/dist/chunk-U3252GZO.js.map +0 -1
  76. package/dist/shell-translations.pt-br-ANQ64XTT.js.map +0 -1
@@ -1,4 +1,4 @@
1
- import { globalCache, getActiveTenantId, getSupabaseClientOptional, parseRangeFilter, matchesRange, applyRangeToQuery, isFilterObject, stableKey, DEFAULT_CACHE_TTL, createSupabaseProvider } from './chunk-KFBEKFII.js';
1
+ import { globalCache, getActiveTenantId, getSupabaseClientOptional, parseRangeFilter, matchesRange, applyUnitScope, applyRangeToQuery, isFilterObject, stableKey, DEFAULT_CACHE_TTL, getScopedResource, createSupabaseProvider, resolveUnitScope, clearGlobalCache } from './chunk-BCI743RH.js';
2
2
  import { declaredCustomFields, validateEntityRecordData } from './chunk-EWKX4VUM.js';
3
3
  import * as React from 'react';
4
4
 
@@ -303,14 +303,15 @@ var ARCHETYPE_CONFIG = {
303
303
  };
304
304
  var CUSTOM_FIELDS_COLUMN = "custom_fields";
305
305
  var CUSTOM_FIELD_FILTER_PREFIX = "custom.";
306
+ var UNIT_COLUMN = "unit_id";
306
307
  var ARCHETYPE_COLUMNS = {
307
- person: /* @__PURE__ */ new Set(["name", "email", "phone", "document_number", "avatar_url", "date_of_birth", "address", "city", "state", "country", "postal_code", "tags", "is_active", "notes", "metadata", CUSTOM_FIELDS_COLUMN]),
308
- category: /* @__PURE__ */ new Set(["name", "slug", "parent_id", "icon", "color", "sort_order", "is_active", "metadata", CUSTOM_FIELDS_COLUMN]),
309
- product: /* @__PURE__ */ new Set(["name", "description", "sku", "price", "cost", "currency", "unit", "image_url", "stock", "min_stock", "status", "is_active", "tags", "metadata", "category_id", CUSTOM_FIELDS_COLUMN]),
310
- service: /* @__PURE__ */ new Set(["name", "description", "price", "cost", "currency", "duration_minutes", "image_url", "status", "is_active", "tags", "metadata", "category_id", CUSTOM_FIELDS_COLUMN]),
308
+ person: /* @__PURE__ */ new Set(["name", "email", "phone", "document_number", "avatar_url", "date_of_birth", "address", "city", "state", "country", "postal_code", "tags", "is_active", "is_bookable", "notes", "metadata", CUSTOM_FIELDS_COLUMN, UNIT_COLUMN]),
309
+ category: /* @__PURE__ */ new Set(["name", "slug", "parent_id", "icon", "color", "sort_order", "is_active", "metadata", CUSTOM_FIELDS_COLUMN, UNIT_COLUMN]),
310
+ product: /* @__PURE__ */ new Set(["name", "description", "sku", "price", "cost", "currency", "unit", "image_url", "stock", "min_stock", "status", "is_active", "tags", "metadata", "category_id", CUSTOM_FIELDS_COLUMN, UNIT_COLUMN]),
311
+ service: /* @__PURE__ */ new Set(["name", "description", "price", "cost", "currency", "duration_minutes", "image_url", "status", "is_active", "tags", "metadata", "category_id", CUSTOM_FIELDS_COLUMN, UNIT_COLUMN]),
311
312
  location: /* @__PURE__ */ new Set(["name", "email", "phone", "address", "city", "state", "country", "postal_code", "is_headquarters", "is_active", "tags", "notes", "metadata", CUSTOM_FIELDS_COLUMN]),
312
313
  order: /* @__PURE__ */ new Set(["reference_number", "status", "party_id", "assignee_id", "location_id", "subtotal", "discount", "tax", "total", "currency", "due_at", "completed_at", "notes", "tags", "metadata", CUSTOM_FIELDS_COLUMN]),
313
- transaction: /* @__PURE__ */ new Set(["order_id", "party_id", "amount", "currency", "payment_method", "reference", "status", "transacted_at", "notes", "metadata", CUSTOM_FIELDS_COLUMN]),
314
+ transaction: /* @__PURE__ */ new Set(["order_id", "party_id", "amount", "currency", "payment_method", "reference", "status", "transacted_at", "notes", "metadata", CUSTOM_FIELDS_COLUMN, UNIT_COLUMN]),
314
315
  schedule: /* @__PURE__ */ new Set(["assignee_id", "location_id", "day_of_week", "specific_date", "starts_at", "ends_at", "is_active", "metadata", CUSTOM_FIELDS_COLUMN])
315
316
  };
316
317
  function camelToSnake(str) {
@@ -338,11 +339,14 @@ function createArchetypeProvider(config) {
338
339
  function coreClient() {
339
340
  return getClient2();
340
341
  }
342
+ function unitScope() {
343
+ return resolveUnitScope(config);
344
+ }
341
345
  function splitFields(data) {
342
346
  const archetypeData = {};
343
347
  const projectData = {};
344
348
  for (const [key, value] of Object.entries(data)) {
345
- if (["id", "createdAt", "updatedAt", "createdBy", "tenantId", fkColumn].includes(key)) continue;
349
+ if (["id", "createdAt", "updatedAt", "createdBy", "ownerId", "tenantId", fkColumn].includes(key)) continue;
346
350
  const snakeKey = camelToSnake(key);
347
351
  if (archetypeColumns.has(snakeKey)) {
348
352
  archetypeData[snakeKey] = value;
@@ -364,6 +368,7 @@ function createArchetypeProvider(config) {
364
368
  if (!tenantId) return { data: [], total: 0 };
365
369
  const countOpts = query.countMode === "none" ? void 0 : { count: "exact" };
366
370
  let q = isPure ? coreClient().from(ac.table).select("*", countOpts).eq("tenant_id", tenantId) : getClient2().from(viewName).select("*", countOpts).eq("tenant_id", tenantId);
371
+ q = applyUnitScope(q, unitScope());
367
372
  if (isPure && HAS_KIND.has(config.archetype)) {
368
373
  q = q.eq("kind", config.archetypeKind);
369
374
  }
@@ -413,6 +418,10 @@ function createArchetypeProvider(config) {
413
418
  if (HAS_KIND.has(config.archetype)) {
414
419
  archetypeData.kind = config.archetypeKind;
415
420
  }
421
+ const scope = unitScope();
422
+ if (scope && archetypeData[scope.column] === void 0) {
423
+ archetypeData[scope.column] = scope.id;
424
+ }
416
425
  const { data: archetypeRow, error: archetypeError } = await coreClient().from(ac.table).insert(archetypeData).select().single();
417
426
  if (archetypeError) throw archetypeError;
418
427
  const archetypeId = archetypeRow.id;
@@ -465,8 +474,9 @@ function createArchetypeProvider(config) {
465
474
  function cachePrefixFor(tenantId, table) {
466
475
  return `${tenantId ?? "_"}:${table}`;
467
476
  }
468
- function cacheKeyFor(tenantId, table, query) {
469
- return `${cachePrefixFor(tenantId, table)}:${stableKey(query)}`;
477
+ function cacheKeyFor(tenantId, table, query, unitId) {
478
+ const keyed = unitId ? { ...query, __unit: unitId } : query;
479
+ return `${cachePrefixFor(tenantId, table)}:${stableKey(keyed)}`;
470
480
  }
471
481
  function withCache(provider, options) {
472
482
  function prefix() {
@@ -474,7 +484,7 @@ function withCache(provider, options) {
474
484
  }
475
485
  return {
476
486
  async list(query) {
477
- const key = cacheKeyFor(options.tenantId(), options.table, query);
487
+ const key = cacheKeyFor(options.tenantId(), options.table, query, options.unitId?.());
478
488
  const cached = globalCache.get(key);
479
489
  if (cached) return { ...cached, cached: true };
480
490
  const result = await provider.list(query);
@@ -1034,8 +1044,8 @@ var REPLICA_MAX_ROWS = 5e3;
1034
1044
  var REPLICA_FIRST_DELAY_MS = 1500;
1035
1045
  var REPLICA_REFRESH_DELAY_MS = 3e3;
1036
1046
  var REPLICA_MIN_INTERVAL_MS = 6e4;
1037
- function replicaKeyFor(tenantId, table) {
1038
- return `replica:${cachePrefixFor(tenantId, table)}`;
1047
+ function replicaKeyFor(tenantId, table, unitId) {
1048
+ return `replica:${cachePrefixFor(tenantId, table)}${unitId ? `@${unitId}` : ""}`;
1039
1049
  }
1040
1050
  function withOffline(provider, options) {
1041
1051
  if (options.policy === "online") return provider;
@@ -1044,14 +1054,14 @@ function withOffline(provider, options) {
1044
1054
  async function fromPersisted(query) {
1045
1055
  const l2 = getPersistentCache();
1046
1056
  if (!l2?.available) return void 0;
1047
- const entry = await l2.getStale(cacheKeyFor(options.tenantId(), options.table, query));
1057
+ const entry = await l2.getStale(cacheKeyFor(options.tenantId(), options.table, query, options.unitId?.()));
1048
1058
  if (!entry) return void 0;
1049
1059
  return { ...entry.data, stale: true, staleAt: entry.storedAt };
1050
1060
  }
1051
1061
  async function fromReplica(query) {
1052
1062
  const l2 = getPersistentCache();
1053
1063
  if (!l2?.available) return void 0;
1054
- const snapshot = await l2.getReplica(replicaKeyFor(options.tenantId(), options.table));
1064
+ const snapshot = await l2.getReplica(replicaKeyFor(options.tenantId(), options.table, options.unitId?.()));
1055
1065
  if (!snapshot) return void 0;
1056
1066
  const result = await localList(snapshot.rows, query, options.entityDef);
1057
1067
  return { ...result, stale: true, staleAt: snapshot.syncedAt };
@@ -1062,7 +1072,7 @@ function withOffline(provider, options) {
1062
1072
  const result = await provider.list(query);
1063
1073
  const l2 = getPersistentCache();
1064
1074
  if (options.policy === "cache" && l2?.available) {
1065
- l2.put(cacheKeyFor(options.tenantId(), options.table, query), result, Date.now() + ttl);
1075
+ l2.put(cacheKeyFor(options.tenantId(), options.table, query, options.unitId?.()), result, Date.now() + ttl);
1066
1076
  }
1067
1077
  return result;
1068
1078
  } catch (err) {
@@ -1118,13 +1128,13 @@ async function syncReplica(provider, options) {
1118
1128
  if (result.data.length < pageSize) break;
1119
1129
  }
1120
1130
  const snapshot = rows.slice(0, maxRows);
1121
- l2.putReplica(replicaKeyFor(options.tenantId, options.table), snapshot);
1131
+ l2.putReplica(replicaKeyFor(options.tenantId, options.table, options.unitId), snapshot);
1122
1132
  await l2.flush();
1123
1133
  return snapshot.length;
1124
1134
  }
1125
1135
  var replicaSyncs = /* @__PURE__ */ new Map();
1126
1136
  function ensureReplicaSync(provider, options) {
1127
- const key = replicaKeyFor(options.tenantId(), options.table);
1137
+ const key = replicaKeyFor(options.tenantId(), options.table, options.unitId?.());
1128
1138
  if (replicaSyncs.has(key)) return;
1129
1139
  const state = {
1130
1140
  // Tenant read at sync time, not at registration: the first provider is often
@@ -1132,6 +1142,7 @@ function ensureReplicaSync(provider, options) {
1132
1142
  run: () => syncReplica(provider, {
1133
1143
  table: options.table,
1134
1144
  tenantId: options.tenantId(),
1145
+ unitId: options.unitId?.(),
1135
1146
  pageSize: options.replicaPageSize,
1136
1147
  maxRows: options.replicaMaxRows
1137
1148
  }),
@@ -1149,7 +1160,7 @@ function ensureReplicaSync(provider, options) {
1149
1160
  scheduleReplicaSync(options, REPLICA_FIRST_DELAY_MS);
1150
1161
  }
1151
1162
  function scheduleReplicaSync(options, delayMs) {
1152
- const state = replicaSyncs.get(replicaKeyFor(options.tenantId(), options.table));
1163
+ const state = replicaSyncs.get(replicaKeyFor(options.tenantId(), options.table, options.unitId?.()));
1153
1164
  if (!state || state.timer) return;
1154
1165
  state.timer = setTimeout(() => {
1155
1166
  state.timer = null;
@@ -1247,6 +1258,47 @@ function mergeCustomFields(current, patch) {
1247
1258
  }
1248
1259
  return merged;
1249
1260
  }
1261
+ var _activeUnitId;
1262
+ var _unitMode = false;
1263
+ var listeners = /* @__PURE__ */ new Set();
1264
+ function notify() {
1265
+ clearGlobalCache();
1266
+ for (const l of listeners) l();
1267
+ }
1268
+ function setActiveUnitId(id) {
1269
+ if (id === _activeUnitId) return;
1270
+ _activeUnitId = id;
1271
+ notify();
1272
+ }
1273
+ function getActiveUnitId() {
1274
+ return _unitMode ? _activeUnitId : void 0;
1275
+ }
1276
+ function setUnitMode(on) {
1277
+ if (on === _unitMode) return;
1278
+ _unitMode = on;
1279
+ notify();
1280
+ }
1281
+ function isUnitMode() {
1282
+ return _unitMode;
1283
+ }
1284
+ function subscribeActiveUnit(listener) {
1285
+ listeners.add(listener);
1286
+ return () => {
1287
+ listeners.delete(listener);
1288
+ };
1289
+ }
1290
+ function useActiveUnitId() {
1291
+ return React.useSyncExternalStore(subscribeActiveUnit, getActiveUnitId, getActiveUnitId);
1292
+ }
1293
+ function useUnitMode() {
1294
+ return React.useSyncExternalStore(subscribeActiveUnit, isUnitMode, isUnitMode);
1295
+ }
1296
+ function resetActiveUnit() {
1297
+ if (_activeUnitId === void 0 && !_unitMode) return;
1298
+ _activeUnitId = void 0;
1299
+ _unitMode = false;
1300
+ notify();
1301
+ }
1250
1302
 
1251
1303
  // src/data/custom-fields.ts
1252
1304
  var KEY = "customFields";
@@ -1311,10 +1363,20 @@ function resolveDataProvider(entityDef, mockData, options = {}) {
1311
1363
  const client = getSupabaseClientOptional();
1312
1364
  if (client && entityDef.data?.table) {
1313
1365
  const tenantId = () => getActiveTenantId();
1366
+ const registered = getScopedResource(entityDef.data.table);
1367
+ const unitScoped = entityDef.data.unitScoped ?? Boolean(registered);
1368
+ const unitId = unitScoped ? () => getActiveUnitId() : void 0;
1369
+ const unitConfig = {
1370
+ unitScoped,
1371
+ unitColumn: entityDef.data.unitIdColumn ?? registered?.unitColumn,
1372
+ includeSharedRows: entityDef.data.includeSharedRows,
1373
+ unitId
1374
+ };
1314
1375
  const cacheOptions = {
1315
1376
  table: entityDef.data.archetypeKind ? `${entityDef.data.table}#${entityDef.data.archetypeKind}` : entityDef.data.table,
1316
1377
  tenantId,
1317
- ttl: entityDef.data.cacheTTL
1378
+ ttl: entityDef.data.cacheTTL,
1379
+ unitId
1318
1380
  };
1319
1381
  const offlinePolicy = entityDef.data.offline ?? "online";
1320
1382
  const cached = (p) => {
@@ -1327,7 +1389,8 @@ function resolveDataProvider(entityDef, mockData, options = {}) {
1327
1389
  archetypeKind: entityDef.data.archetypeKind,
1328
1390
  projectTable: entityDef.data.table,
1329
1391
  tenantId,
1330
- searchColumns: entityDef.data.searchColumns ?? entityDef.fields.filter((field) => field.searchable).map((field) => field.key)
1392
+ searchColumns: entityDef.data.searchColumns ?? entityDef.fields.filter((field) => field.searchable).map((field) => field.key),
1393
+ ...unitConfig
1331
1394
  }), entityDef.data.archetype));
1332
1395
  }
1333
1396
  return cached(createSupabaseProvider(entityDef.data.table, {
@@ -1338,7 +1401,8 @@ function resolveDataProvider(entityDef, mockData, options = {}) {
1338
1401
  selectColumns: entityDef.data.selectColumns,
1339
1402
  columnMap: entityDef.data.columnMap,
1340
1403
  filters: entityDef.data.filters,
1341
- defaults: entityDef.data.defaults
1404
+ defaults: entityDef.data.defaults,
1405
+ ...unitConfig
1342
1406
  }));
1343
1407
  }
1344
1408
  if (entityDef.data?.table) {
@@ -1613,6 +1677,6 @@ async function bulkRemove(provider, ids, options) {
1613
1677
  return { removed, failed };
1614
1678
  }
1615
1679
 
1616
- export { CustomFieldsError, DATA_CHANGED_EVENT, DataError, FIELDS_TABLE, attachPersistentCache, bulkCreate, bulkRemove, cacheKeyFor, cachePrefixFor, clearCustomFieldsCache, countByTenant, createArchetypeProvider, createEventBus, createFayzApiProvider, createMockProvider, dataCall, dataQuery, emitDataChanged, eventBus, getPersistentCache, invalidateCount, localList, matchesDataChange, mergeCustomFields, registerDataTelemetry, replicaKeyFor, resolveCustomFields, resolveDataProvider, syncReplica, toDataError, useDataChanged, validateCustomFields, withCache, withOffline };
1617
- //# sourceMappingURL=chunk-S5FOFKR4.js.map
1618
- //# sourceMappingURL=chunk-S5FOFKR4.js.map
1680
+ export { CustomFieldsError, DATA_CHANGED_EVENT, DataError, FIELDS_TABLE, attachPersistentCache, bulkCreate, bulkRemove, cacheKeyFor, cachePrefixFor, clearCustomFieldsCache, countByTenant, createArchetypeProvider, createEventBus, createFayzApiProvider, createMockProvider, dataCall, dataQuery, emitDataChanged, eventBus, getActiveUnitId, getPersistentCache, invalidateCount, isUnitMode, localList, matchesDataChange, mergeCustomFields, registerDataTelemetry, replicaKeyFor, resetActiveUnit, resolveCustomFields, resolveDataProvider, setActiveUnitId, setUnitMode, subscribeActiveUnit, syncReplica, toDataError, useActiveUnitId, useDataChanged, useUnitMode, validateCustomFields, withCache, withOffline };
1681
+ //# sourceMappingURL=chunk-NBKFYGVD.js.map
1682
+ //# sourceMappingURL=chunk-NBKFYGVD.js.map