@moguiyu/dsh-tavily 0.1.6 → 0.1.7

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/client.js CHANGED
@@ -7,9 +7,9 @@ window.__ModuleLoader__.load({
7
7
  const inject = ['slots']
8
8
 
9
9
  const STRATEGIES = [
10
- { id: 'rotate', label: 'Rotate each key', hint: 'Keys are tried in turn (round-robin); on 401/429 the next key is used.' },
11
- { id: 'low-usage-first', label: 'Lowest usage first', hint: 'On save, keys are re-ordered by current Tavily usage (least used first).' },
12
- { id: 'high-usage-first', label: 'Highest usage first', hint: 'On save, keys are re-ordered by current Tavily usage (most used first).' }
10
+ { id: 'rotate', label: 'Round-robin', hint: 'Use keys in turn; on HTTP 401/429 the next key is tried automatically.' },
11
+ { id: 'low-usage-first', label: 'Lowest usage first', hint: 'Re-orders keys by current Tavily usage, least-used first. The first key becomes primary.' },
12
+ { id: 'high-usage-first', label: 'Highest usage first', hint: 'Re-orders keys by current Tavily usage, most-used first. The first key becomes primary.' }
13
13
  ]
14
14
 
15
15
  const ICONS = {
@@ -422,7 +422,7 @@ window.__ModuleLoader__.load({
422
422
  notice !== null && react.createElement('span', { style: { fontSize: 13, color: notice.ok ? 'var(--dsw-alias-state-success-primary)' : 'var(--dsw-alias-state-error-primary)' } }, notice.ok ? notice.ok : String(notice.error))
423
423
  ),
424
424
  react.createElement('p', { style: { margin: 0, fontSize: 12, color: 'var(--dsw-alias-label-tertiary)' } },
425
- (STRATEGIES.find((option) => option.id === strategy) || STRATEGIES[0]).hint + ' Saved immediately when selected; the first key then becomes primary for web_search.'
425
+ (STRATEGIES.find((option) => option.id === strategy) || STRATEGIES[0]).hint + ' Saved immediately when selected.'
426
426
  )
427
427
  ),
428
428
  usageError !== null && react.createElement('p', { style: { margin: 0, fontSize: 12, color: 'var(--dsw-alias-state-error-primary)' } }, String(usageError)),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moguiyu/dsh-tavily",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
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/client.js CHANGED
@@ -12,9 +12,9 @@ export function factory(require) {
12
12
  const inject = ['slots']
13
13
 
14
14
  const STRATEGIES = [
15
- { id: 'rotate', label: 'Rotate each key', hint: 'Keys are tried in turn (round-robin); on 401/429 the next key is used.' },
16
- { id: 'low-usage-first', label: 'Lowest usage first', hint: 'On save, keys are re-ordered by current Tavily usage (least used first).' },
17
- { id: 'high-usage-first', label: 'Highest usage first', hint: 'On save, keys are re-ordered by current Tavily usage (most used first).' }
15
+ { id: 'rotate', label: 'Round-robin', hint: 'Use keys in turn; on HTTP 401/429 the next key is tried automatically.' },
16
+ { id: 'low-usage-first', label: 'Lowest usage first', hint: 'Re-orders keys by current Tavily usage, least-used first. The first key becomes primary.' },
17
+ { id: 'high-usage-first', label: 'Highest usage first', hint: 'Re-orders keys by current Tavily usage, most-used first. The first key becomes primary.' }
18
18
  ]
19
19
 
20
20
  const ICONS = {
@@ -427,7 +427,7 @@ export function factory(require) {
427
427
  notice !== null && react.createElement('span', { style: { fontSize: 13, color: notice.ok ? 'var(--dsw-alias-state-success-primary)' : 'var(--dsw-alias-state-error-primary)' } }, notice.ok ? notice.ok : String(notice.error))
428
428
  ),
429
429
  react.createElement('p', { style: { margin: 0, fontSize: 12, color: 'var(--dsw-alias-label-tertiary)' } },
430
- (STRATEGIES.find((option) => option.id === strategy) || STRATEGIES[0]).hint + ' Saved immediately when selected; the first key then becomes primary for web_search.'
430
+ (STRATEGIES.find((option) => option.id === strategy) || STRATEGIES[0]).hint + ' Saved immediately when selected.'
431
431
  )
432
432
  ),
433
433
  usageError !== null && react.createElement('p', { style: { margin: 0, fontSize: 12, color: 'var(--dsw-alias-state-error-primary)' } }, String(usageError)),
package/src/index.js CHANGED
@@ -306,7 +306,7 @@ function readBody(req) {
306
306
  })
307
307
  }
308
308
 
309
- async function fetchUsageFor(key) {
309
+ async function fetchUsageDetailsFor(key) {
310
310
  try {
311
311
  const controller = new AbortController()
312
312
  const timer = setTimeout(() => controller.abort(), 15000)
@@ -323,14 +323,32 @@ async function fetchUsageFor(key) {
323
323
  const json = await response.json()
324
324
  const account = json !== null && typeof json === 'object' ? json.account : undefined
325
325
  const keyUsage = json !== null && typeof json === 'object' ? json.key : undefined
326
- if (account !== undefined && typeof account.plan_usage === 'number') return account.plan_usage
327
- if (keyUsage !== undefined && typeof keyUsage.usage === 'number') return keyUsage.usage
328
- return null
326
+ const usage = keyUsage !== undefined && typeof keyUsage.usage === 'number'
327
+ ? keyUsage.usage
328
+ : (account !== undefined && typeof account.plan_usage === 'number' ? account.plan_usage : null)
329
+ const planUsage = account !== undefined && typeof account.plan_usage === 'number'
330
+ ? account.plan_usage
331
+ : (keyUsage !== undefined && typeof keyUsage.usage === 'number' ? keyUsage.usage : null)
332
+ const planLimit = account !== undefined && typeof account.plan_limit === 'number'
333
+ ? account.plan_limit
334
+ : (keyUsage !== undefined && typeof keyUsage.limit === 'number' ? keyUsage.limit
335
+ : (keyUsage !== undefined && typeof keyUsage.plan_limit === 'number' ? keyUsage.plan_limit : null))
336
+ const currentPlan = account !== undefined && typeof account.plan === 'string'
337
+ ? account.plan
338
+ : (keyUsage !== undefined && typeof keyUsage.plan === 'string' ? keyUsage.plan : null)
339
+ if (usage === null && planUsage === null) return null
340
+ return { usage, planUsage, planLimit, currentPlan }
329
341
  } catch {
330
342
  return null
331
343
  }
332
344
  }
333
345
 
346
+ async function fetchUsageFor(key) {
347
+ const details = await fetchUsageDetailsFor(key)
348
+ if (details === null) return null
349
+ return details.usage !== null ? details.usage : details.planUsage
350
+ }
351
+
334
352
  async function collectStoredKeys(credentials) {
335
353
  const out = []
336
354
  const seen = new Set()
@@ -422,9 +440,9 @@ function applyBackend(ctx) {
422
440
  const perKey = []
423
441
  for (const key of keys) {
424
442
  try {
425
- const usage = await fetchUsageFor(key)
443
+ const usage = await fetchUsageDetailsFor(key)
426
444
  perKey.push(usage !== null
427
- ? { ok: true, usage, planUsage: usage, planLimit: null, currentPlan: null }
445
+ ? { ok: true, ...usage }
428
446
  : { ok: false, error: 'usage unavailable' })
429
447
  } catch (error) {
430
448
  perKey.push({ ok: false, error: String(error && error.message ? error.message : error) })
@@ -438,8 +456,8 @@ function applyBackend(ctx) {
438
456
  keys: keys.length,
439
457
  okKeys: okRows.length,
440
458
  usage: okRows.reduce((sum, row) => sum + (row.usage || 0), 0),
441
- planUsage: null,
442
- planLimit: null,
459
+ planUsage: okRows.reduce((sum, row) => sum + (row.planUsage || 0), 0),
460
+ planLimit: okRows.every((row) => row.planLimit !== null) ? okRows.reduce((sum, row) => sum + row.planLimit, 0) : null,
443
461
  },
444
462
  })
445
463
  } catch (error) {