@moguiyu/dsh-tavily 0.1.8 → 0.1.10

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 (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.js +11 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moguiyu/dsh-tavily",
3
- "version": "0.1.8",
3
+ "version": "0.1.10",
4
4
  "description": "Tavily Search for DSH: multiple API keys, rotation/failover, usage gauge, settings card, and web_search toggle in one plugin.",
5
5
  "type": "module",
6
6
  "main": "./src/index.js",
package/src/index.js CHANGED
@@ -12,6 +12,9 @@ export const name = 'dsh-tavily'
12
12
 
13
13
  export const inject = ['tools', 'webServer', 'credentials']
14
14
 
15
+ const USAGE_TTL_MS = 60000
16
+ const usageCache = new Map()
17
+
15
18
  /** Clamp a number into [min, max]; `fallback` when not finite. */
16
19
  export function clampInt(value, min, max, fallback) {
17
20
  const n = Number(value)
@@ -307,6 +310,8 @@ function readBody(req) {
307
310
  }
308
311
 
309
312
  async function fetchUsageDetailsFor(key) {
313
+ const cached = usageCache.get(key)
314
+ if (cached !== undefined && Date.now() - cached.at < USAGE_TTL_MS) return cached.details
310
315
  try {
311
316
  const controller = new AbortController()
312
317
  const timer = setTimeout(() => controller.abort(), 15000)
@@ -337,7 +342,9 @@ async function fetchUsageDetailsFor(key) {
337
342
  ? account.plan
338
343
  : (keyUsage !== undefined && typeof keyUsage.plan === 'string' ? keyUsage.plan : null)
339
344
  if (usage === null && planUsage === null) return null
340
- return { usage, planUsage, planLimit, currentPlan }
345
+ const details = { usage, planUsage, planLimit, currentPlan }
346
+ usageCache.set(key, { at: Date.now(), details })
347
+ return details
341
348
  } catch {
342
349
  return null
343
350
  }
@@ -346,6 +353,9 @@ async function fetchUsageDetailsFor(key) {
346
353
  async function fetchUsageFor(key) {
347
354
  const details = await fetchUsageDetailsFor(key)
348
355
  if (details === null) return null
356
+ if (details.planLimit !== null && details.planLimit > 0 && details.planUsage !== null) {
357
+ return (details.planUsage / details.planLimit) * 100
358
+ }
349
359
  return details.usage !== null ? details.usage : details.planUsage
350
360
  }
351
361