@lynn123411/dsh-a6api 1.1.0 → 1.2.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.
package/lib/index.js CHANGED
@@ -1032,13 +1032,14 @@ async function probeSingleModel(baseURL, apiKey, userId, accessToken, modelName)
1032
1032
  error: requestOk ? void 0 : requestError
1033
1033
  };
1034
1034
  }
1035
- async function getKnownMerchantsFromLogs(userId, accessToken, modelNames = []) {
1035
+ async function getKnownMerchantsFromLogs(userId, accessToken, modelNames = [], logs) {
1036
1036
  if (!userId && !accessToken || modelNames.length === 0) return {};
1037
1037
  const result = {};
1038
1038
  try {
1039
- const logs = await fetchRecentLogs(userId, accessToken, 50);
1039
+ const items = logs !== void 0 ? logs : await fetchRecentLogs(userId, accessToken, 50);
1040
+ const sorted = items.slice().sort((a, b) => (Number(b.created_at) || 0) - (Number(a.created_at) || 0));
1040
1041
  const modelToLog = /* @__PURE__ */ new Map();
1041
- for (const log of logs) {
1042
+ for (const log of sorted) {
1042
1043
  const mName = log.model_name;
1043
1044
  const chId = Number(log.channel);
1044
1045
  if (mName && chId && !modelToLog.has(mName.toLowerCase())) {
@@ -1437,23 +1438,43 @@ function apply(ctx) {
1437
1438
  ])
1438
1439
  ];
1439
1440
  }
1441
+ const allLogs = await fetchRecentLogs(config.userId, token, 100);
1442
+ allLogs.sort((a, b) => (Number(b.created_at) || 0) - (Number(a.created_at) || 0));
1440
1443
  if (config.userId || token) {
1441
1444
  const missing = modelIds.filter((m) => {
1442
1445
  const entry = merchantCardCache.get(m.toLowerCase());
1443
1446
  return !entry || Date.now() - entry.at >= MERCHANT_CARD_TTL_MS;
1444
1447
  });
1445
1448
  if (missing.length > 0) {
1446
- const found = await getKnownMerchantsFromLogs(config.userId, token, missing);
1449
+ let found = {};
1450
+ try {
1451
+ found = await Promise.race([
1452
+ getKnownMerchantsFromLogs(config.userId, token, missing, allLogs),
1453
+ new Promise((resolve) => setTimeout(() => resolve({}), 1e4))
1454
+ ]);
1455
+ } catch {
1456
+ found = {};
1457
+ }
1447
1458
  for (const [mName, card] of Object.entries(found)) {
1448
1459
  merchantCardCache.set(mName.toLowerCase(), { card, at: Date.now() });
1449
1460
  }
1450
1461
  }
1451
1462
  }
1463
+ const lastRoutedMap = /* @__PURE__ */ new Map();
1464
+ for (const log of allLogs) {
1465
+ const mName = log.model_name;
1466
+ const chId = Number(log.channel);
1467
+ const ts = Number(log.created_at) || 0;
1468
+ if (mName && chId > 0 && ts > 0 && !lastRoutedMap.has(mName.toLowerCase())) {
1469
+ lastRoutedMap.set(mName.toLowerCase(), ts);
1470
+ }
1471
+ }
1452
1472
  const dshSet = new Set(dshConfiguredModels);
1453
1473
  const models = modelIds.map((mId) => {
1454
1474
  const meta = resolveModelMeta(mId);
1455
1475
  const cacheEntry = merchantCardCache.get(mId.toLowerCase());
1456
1476
  const cachedCard = cacheEntry && Date.now() - cacheEntry.at < MERCHANT_CARD_TTL_MS ? cacheEntry.card : void 0;
1477
+ const routedAt = lastRoutedMap.get(mId.toLowerCase());
1457
1478
  return {
1458
1479
  model_name: mId,
1459
1480
  brand: meta.brand,
@@ -1463,10 +1484,12 @@ function apply(ctx) {
1463
1484
  hasReasoning: Boolean(meta.reasoningEfforts || meta.thinkingFormat),
1464
1485
  inDsh: dshSet.has(mId),
1465
1486
  merchant: cachedCard,
1466
- probeStatus: cachedCard ? "success" : "idle"
1487
+ probeStatus: cachedCard ? "success" : "idle",
1488
+ lastRoutedAt: routedAt,
1489
+ lastRoutedText: routedAt ? formatRelativeTime(routedAt) : void 0
1467
1490
  };
1468
1491
  });
1469
- const recentLogs = await fetchRecentLogs(config.userId, token, 20);
1492
+ const recentLogs = allLogs.slice(0, 20);
1470
1493
  const response = {
1471
1494
  config: maskConfig(config),
1472
1495
  balance,