@ai-matrx/agents 0.8.0 → 0.9.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.
@@ -219,8 +219,8 @@ async function readSingleAgentRow(client, agentId) {
219
219
  }
220
220
  return null;
221
221
  }
222
- async function writeAgentFavorite(client, agentId, isFavorite) {
223
- const { error } = await client.schema("agent").from("definition").update({ is_favorite: isFavorite }).eq("id", agentId);
222
+ async function writeAgentFavorite(client, agentId, isFavorite2) {
223
+ const { error } = await client.schema("agent").from("definition").update({ is_favorite: isFavorite2 }).eq("id", agentId);
224
224
  if (error) {
225
225
  throw new AgentCatalogReadError(
226
226
  "agent.definition.is_favorite",
@@ -337,6 +337,51 @@ function writeCachedDefaultRow(storage, resolved) {
337
337
  } catch {
338
338
  }
339
339
  }
340
+ function parseCachedDefaultRow(mandateKey, raw) {
341
+ try {
342
+ if (!raw) return null;
343
+ const parsed = JSON.parse(raw);
344
+ if (!isRecord2(parsed)) return null;
345
+ const holderId = parsed["holderId"];
346
+ const holderName = parsed["holderName"];
347
+ if (typeof holderId !== "string" || typeof holderName !== "string") {
348
+ return null;
349
+ }
350
+ const description = parsed["holderDescription"];
351
+ const provenance = parsed["provenance"];
352
+ const at = parsed["at"];
353
+ return {
354
+ mandateKey,
355
+ holderId,
356
+ holderName,
357
+ holderDescription: typeof description === "string" ? description : null,
358
+ provenance: typeof provenance === "string" ? provenance : "system",
359
+ at: typeof at === "number" ? at : 0
360
+ };
361
+ } catch {
362
+ return null;
363
+ }
364
+ }
365
+ async function readCachedDefaultRowAsync(storage, mandateKey) {
366
+ let raw;
367
+ try {
368
+ raw = await storage.getItem(
369
+ CACHE_PREFIX + mandateKey
370
+ );
371
+ } catch {
372
+ return null;
373
+ }
374
+ return parseCachedDefaultRow(mandateKey, raw);
375
+ }
376
+ async function writeCachedDefaultRowAsync(storage, resolved) {
377
+ try {
378
+ await storage.setItem(
379
+ CACHE_PREFIX + resolved.mandateKey,
380
+ JSON.stringify(resolved)
381
+ );
382
+ } catch {
383
+ }
384
+ }
340
385
  function defaultRowFromResolution(resolved) {
341
386
  return {
342
387
  id: mandateAgentId(resolved.mandateKey),
@@ -376,7 +421,7 @@ function detectDefaultRowDrift(cached, live) {
376
421
  }
377
422
  async function resolveDefaultRow(args) {
378
423
  const { mandateKey, transport, client, storage, lookupRow } = args;
379
- const cached = readCachedDefaultRow(storage, mandateKey);
424
+ const cached = await readCachedDefaultRowAsync(storage, mandateKey);
380
425
  const verdict = await askTheServer(transport, mandateKey);
381
426
  if (verdict.holderType !== "agent") {
382
427
  throw new MandateDefaultRowError({
@@ -413,7 +458,7 @@ async function resolveDefaultRow(args) {
413
458
  at: Date.now()
414
459
  };
415
460
  const drift = detectDefaultRowDrift(cached, resolved);
416
- writeCachedDefaultRow(storage, resolved);
461
+ await writeCachedDefaultRowAsync(storage, resolved);
417
462
  return { resolved, row: defaultRowFromResolution(resolved), drift };
418
463
  }
419
464
 
@@ -753,7 +798,7 @@ function createAgentCatalog(config) {
753
798
  mergeRows(result.rows);
754
799
  return result.ids;
755
800
  }
756
- async function setFavorite(agentId, isFavorite) {
801
+ async function setFavorite(agentId, isFavorite2) {
757
802
  const existing = state.byId[agentId];
758
803
  if (!existing) {
759
804
  errorSink({
@@ -762,15 +807,15 @@ function createAgentCatalog(config) {
762
807
  });
763
808
  return;
764
809
  }
765
- mergeRows([{ ...existing, isFavorite }]);
810
+ mergeRows([{ ...existing, isFavorite: isFavorite2 }]);
766
811
  try {
767
- await writeAgentFavorite(client, agentId, isFavorite);
812
+ await writeAgentFavorite(client, agentId, isFavorite2);
768
813
  } catch (e) {
769
814
  mergeRows([existing]);
770
815
  errorSink({
771
816
  code: "favorite_write_failed",
772
817
  message: `the favorite could not be saved: ${errorMessage(e)}`,
773
- context: { agentId, isFavorite }
818
+ context: { agentId, isFavorite: isFavorite2 }
774
819
  });
775
820
  notifier?.({
776
821
  level: "error",
@@ -1049,7 +1094,19 @@ function memoize3(fn) {
1049
1094
  return last;
1050
1095
  };
1051
1096
  }
1097
+ function compareAgentIds(a, b) {
1098
+ const idA = a.id ?? "";
1099
+ const idB = b.id ?? "";
1100
+ if (idA < idB) return -1;
1101
+ if (idA > idB) return 1;
1102
+ return 0;
1103
+ }
1052
1104
  function applyAgentSortComparator(a, b, sortBy) {
1105
+ const primary = compareAgentSortKey(a, b, sortBy);
1106
+ if (!Number.isFinite(primary) || primary === 0) return compareAgentIds(a, b);
1107
+ return primary;
1108
+ }
1109
+ function compareAgentSortKey(a, b, sortBy) {
1053
1110
  switch (sortBy) {
1054
1111
  case "name-asc":
1055
1112
  return (a.name ?? "").localeCompare(b.name ?? "");
@@ -1297,22 +1354,46 @@ var makeSelectFilteredBuiltinAgentsCount = () => {
1297
1354
  function agentConsumerHasActiveFilters(consumer, defaults) {
1298
1355
  return consumer.tab !== defaults.tab || consumer.sortBy !== defaults.sortBy || consumer.searchTerm !== "" || consumer.includedCats.length > 0 || consumer.includedTags.length > 0 || consumer.favFilter !== defaults.favFilter || consumer.archFilter !== defaults.archFilter || consumer.accessFilter !== defaults.accessFilter || consumer.favoritesFirst !== defaults.favoritesFirst;
1299
1356
  }
1357
+ function agentMatchesArchiveFilter(agent, archFilter) {
1358
+ if (archFilter === "active") return !agent.isArchived;
1359
+ if (archFilter === "archived") return Boolean(agent.isArchived);
1360
+ return true;
1361
+ }
1362
+ function countUserRows(agents, archFilter, predicate) {
1363
+ let n = 0;
1364
+ for (const a of agents) {
1365
+ if (a.agentType !== "user") continue;
1366
+ if (!agentMatchesArchiveFilter(a, archFilter)) continue;
1367
+ if (predicate(a)) n += 1;
1368
+ }
1369
+ return n;
1370
+ }
1371
+ var isOwned = (a) => a.isOwner === true;
1372
+ var isShared = (a) => a.isOwner === false && a.accessLevel != null;
1373
+ var isAny = () => true;
1374
+ var isFavorite = (a) => Boolean(a.isFavorite);
1375
+ var selectAgentTabCounts = memoize2(
1376
+ (agents, archFilter) => ({
1377
+ mine: countUserRows(agents, archFilter, isOwned),
1378
+ shared: countUserRows(agents, archFilter, isShared),
1379
+ all: countUserRows(agents, archFilter, isAny),
1380
+ system: selectBuiltinTypeAgents(agents).length
1381
+ })
1382
+ );
1300
1383
  var selectTotalUserAgentsCount = memoize1(
1301
- (agents) => selectUserTypeAgents(agents).length
1384
+ (agents) => countUserRows(agents, "active", isAny)
1302
1385
  );
1303
1386
  var selectTotalOwnedAgentsCount = memoize1(
1304
- (agents) => selectUserTypeAgents(agents).filter((a) => a.isOwner === true).length
1387
+ (agents) => countUserRows(agents, "active", isOwned)
1305
1388
  );
1306
1389
  var selectTotalSharedAgentsCount = memoize1(
1307
- (agents) => selectUserTypeAgents(agents).filter(
1308
- (a) => a.isOwner === false && a.accessLevel != null
1309
- ).length
1390
+ (agents) => countUserRows(agents, "active", isShared)
1310
1391
  );
1311
1392
  var selectTotalBuiltinAgentsCount = memoize1(
1312
1393
  (agents) => selectBuiltinTypeAgents(agents).length
1313
1394
  );
1314
1395
  var selectTotalFavoriteAgentsCount = memoize1(
1315
- (agents) => selectUserTypeAgents(agents).filter((a) => a.isFavorite).length
1396
+ (agents) => countUserRows(agents, "active", isFavorite)
1316
1397
  );
1317
1398
  export {
1318
1399
  AGENT_CARDS_LIMIT_DESKTOP,
@@ -1329,10 +1410,12 @@ export {
1329
1410
  _resetCatalogGlobalState,
1330
1411
  agentConsumerHasActiveFilters,
1331
1412
  agentListEmptyLabel,
1413
+ agentMatchesArchiveFilter,
1332
1414
  agentMatchesSearch,
1333
1415
  applyAgentSortComparator,
1334
1416
  asAgentId,
1335
1417
  assertAgentCatalogSchema,
1418
+ compareAgentIds,
1336
1419
  computeAgentSearchScore,
1337
1420
  createAgentCatalog,
1338
1421
  createMemoryStorage,
@@ -1364,9 +1447,11 @@ export {
1364
1447
  parseMandateResolution,
1365
1448
  readAgentCatalogRows,
1366
1449
  readCachedDefaultRow,
1450
+ readCachedDefaultRowAsync,
1367
1451
  readSingleAgentRow,
1368
1452
  resolveDefaultRow,
1369
1453
  searchAgentsOnServer,
1454
+ selectAgentTabCounts,
1370
1455
  selectAllAgentCategories,
1371
1456
  selectAllAgentTags,
1372
1457
  selectAllSystemAgentCategories,
@@ -1382,6 +1467,7 @@ export {
1382
1467
  sortFilteredAgents,
1383
1468
  toAgentSummary,
1384
1469
  writeAgentFavorite,
1385
- writeCachedDefaultRow
1470
+ writeCachedDefaultRow,
1471
+ writeCachedDefaultRowAsync
1386
1472
  };
1387
1473
  //# sourceMappingURL=index.js.map