@omdp/dsh-key-fallback 1.0.1 → 1.0.2

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
@@ -14,35 +14,97 @@ window.__ModuleLoader__.load({
14
14
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' })
15
15
  var React = require('react')
16
16
 
17
- function fetchJson(url) {
18
- return fetch(url).then(function (r) { return r.json() })
17
+ function fetchJson(url, signal) {
18
+ return fetch(url, signal ? { signal: signal } : undefined).then(function (r) { return r.json() })
19
19
  }
20
20
 
21
+ // 卡片可见性驱动的"仅在用户观看时轮询":
22
+ // 不看(页面隐藏 / 卡片滚出视口 / 组件卸载)→ 停止轮询,零网络;
23
+ // 重新可见 → 立即刷新并恢复每 2s 轮询,数据最多晚 2s,正常及时。
21
24
  function KeyFallbackCard() {
22
25
  var s = React.useState({ loading: true, data: null })
23
26
  var state = s[0]
24
27
  var setState = s[1]
28
+ var rootRef = React.useRef(null)
29
+ var ioRef = React.useRef(null)
30
+
31
+ // 回调 ref:卡片根元素变化(loading ↔ 列表)时先解除旧元素再观察新元素,
32
+ // 避免旧元素因被移除触发 isIntersecting:false 而错误停止轮询。
33
+ var attachRoot = React.useCallback(function (el) {
34
+ var prev = rootRef.current
35
+ var io = ioRef.current
36
+ if (io && prev && prev !== el) io.unobserve(prev)
37
+ rootRef.current = el
38
+ if (io && el) io.observe(el)
39
+ }, [])
40
+
25
41
  var refresh = React.useCallback(function () {
26
42
  fetchJson('/dsh-key-fallback/pools')
27
43
  .then(function (data) { setState({ loading: false, data: (data && data.pools) || {} }) })
28
44
  .catch(function () { setState({ loading: false, data: null }) })
29
45
  }, [])
46
+
30
47
  React.useEffect(function () {
48
+ var alive = true
49
+ var timer = null
50
+ var pageVisible = !document.hidden
51
+ var cardVisible = true
52
+
53
+ function stop() {
54
+ if (timer !== null) { clearInterval(timer); timer = null }
55
+ }
56
+ function start() {
57
+ if (!alive || timer !== null || !pageVisible || !cardVisible) return
58
+ refresh()
59
+ timer = setInterval(refresh, 2000)
60
+ }
61
+ function sync() {
62
+ if (!alive) return
63
+ if (pageVisible && cardVisible) start()
64
+ else stop()
65
+ }
66
+ function onVisibility() {
67
+ pageVisible = !document.hidden
68
+ sync()
69
+ }
70
+
71
+ document.addEventListener('visibilitychange', onVisibility)
72
+
73
+ if (typeof IntersectionObserver === 'function') {
74
+ var io = new IntersectionObserver(function (entries) {
75
+ if (!alive) return
76
+ for (var i = 0; i < entries.length; i++) {
77
+ cardVisible = entries[i].isIntersecting
78
+ }
79
+ sync()
80
+ }, { threshold: 0.01 })
81
+ ioRef.current = io
82
+ var el = rootRef.current
83
+ if (el) io.observe(el)
84
+ }
85
+
31
86
  refresh()
32
- var t = setInterval(refresh, 2000)
33
- return function () { clearInterval(t) }
87
+ start()
88
+
89
+ return function () {
90
+ alive = false
91
+ stop()
92
+ document.removeEventListener('visibilitychange', onVisibility)
93
+ if (ioRef.current) { ioRef.current.disconnect(); ioRef.current = null }
94
+ ioRef.current = null
95
+ }
34
96
  }, [refresh])
35
97
 
36
98
  if (state.loading) {
37
- return React.createElement('div', { style: { fontSize: '12px', opacity: 0.6 } }, '加载中…')
99
+ return React.createElement('div', { ref: attachRoot, style: { fontSize: '12px', opacity: 0.6 } }, '加载中…')
38
100
  }
39
101
  var data = state.data || {}
40
102
  var providers = Object.keys(data)
41
103
  if (providers.length === 0) {
42
- return React.createElement('p', { style: { fontSize: '12px', opacity: 0.6, margin: 0 } },
104
+ return React.createElement('p', { ref: attachRoot, style: { fontSize: '12px', opacity: 0.6, margin: 0 } },
43
105
  '未在 settings.yaml 配置 keyFallback.providers')
44
106
  }
45
- return React.createElement('div', { style: { fontSize: '12px', display: 'flex', flexDirection: 'column', gap: '8px' } },
107
+ return React.createElement('div', { ref: attachRoot, style: { fontSize: '12px', display: 'flex', flexDirection: 'column', gap: '8px' } },
46
108
  providers.map(function (p) {
47
109
  var pd = data[p]
48
110
  return React.createElement('div', { key: p, style: { padding: '6px 0', borderTop: '1px solid var(--dsw-alias-border-l2, #eee)' } },
@@ -62,8 +124,9 @@ window.__ModuleLoader__.load({
62
124
  function apply(ctx) {
63
125
  var slots = ctx.get('slots')
64
126
  if (slots === undefined) return
65
- // settings.plugin.item 是 root-scope 的 keyed slot(注册只需 key);本卡是只读状态展示,
66
- // 不需要 settingsScope 绑定可编辑配置。与 resume-stream 的 slots.inject 姿势一致。
127
+ // settings.plugin.item 是 root-scope 的 keyed slot,按 settings 命名空间
128
+ // 分发:host 端 settings.register('key-fallback', …) 让该 key 进入
129
+ // served 集合后,本卡片才会被渲染(参考 auto-continue 的注册姿势)。
67
130
  slots.inject('settings.plugin.item', () => slots.register({
68
131
  name: 'settings.plugin.item',
69
132
  key: 'key-fallback',
package/lib/index.js CHANGED
@@ -8,6 +8,11 @@ const { load: loadYaml } = (() => {
8
8
  try { return require('js-yaml') }
9
9
  catch { return { load: (s) => { try { return JSON.parse(s) } catch { return {} } } } }
10
10
  })()
11
+ // schemastery 用于注册 settings 命名空间占位(served 集合来源)。
12
+ // bundle 环境解析失败时降级为 null,此时仅卡片显示受影响、轮换逻辑照常。
13
+ const zs = (() => {
14
+ try { return require('@deepseek-ai/schemastery') } catch { return null }
15
+ })()
11
16
 
12
17
  const name = 'key-fallback'
13
18
  const inject = ['llm', 'settings', 'webServer']
@@ -41,6 +46,27 @@ function readConfig() {
41
46
  }
42
47
 
43
48
  function apply(ctx) {
49
+ // 注册 settings 命名空间(含真实字段 schema):让客户端 Settings → 插件
50
+ // 的 keyed slot(settings.plugin.item, key='key-fallback')被 serve 并渲染状态卡。
51
+ // 状态卡只读;轮换逻辑仍直接读 settings.yaml(v1.0.0 原设计)。
52
+ // 注意:1.0.0 版写的是 `if (typeof zs !== 'undefined')` 死代码(zs 从未导入),
53
+ // 注册实际从未发生;这里用真实 require 修复。
54
+ if (zs) {
55
+ try {
56
+ ctx.inject(['settings'], (sctx) => {
57
+ sctx.settings.register('key-fallback', zs.object({
58
+ providers: zs.dict(zs.object({
59
+ env: zs.string(),
60
+ keys: zs.array(zs.string()),
61
+ cooldownMs: zs.natural(),
62
+ })),
63
+ }), { base: {} })
64
+ })
65
+ } catch (e) {
66
+ // 命名空间注册失败不影响核心轮换逻辑
67
+ }
68
+ }
69
+
44
70
  const pools = new Map()
45
71
  const attempts = new Map() // `${turn}:${step}` -> 已尝试次数(防止死循环)
46
72
  const lifetime = new AbortController()
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@omdp/dsh-key-fallback",
3
3
  "description": "Minimal API key fallback for DeepSeek Harness: on a retryable request error, silently retry the same call with the next key in the pool.",
4
- "version": "1.0.1",
4
+ "version": "1.0.2",
5
5
  "type": "commonjs",
6
6
  "main": "lib/index.js",
7
7
  "exports": {