@omdp/dsh-key-fallback 1.0.4 → 1.0.6

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
@@ -121,12 +121,29 @@ window.__ModuleLoader__.load({
121
121
  )
122
122
  }
123
123
 
124
+ function FallbackSection() {
125
+ // 同一个 KeyFallbackCard 样式,只是放在 section 页面容器中。
126
+ return React.createElement('div', { style: { padding: '12px 0' } },
127
+ React.createElement('h3', { style: { margin: '0 0 8px 0', fontSize: '14px', fontWeight: 600 } }, 'API Key 回退 — 池状态'),
128
+ React.createElement(KeyFallbackCard))
129
+ }
130
+
124
131
  function apply(ctx) {
125
- // settings.plugin.item 是 root-scope 的 keyed slot,按 settings 命名空间
126
- // 分发:host 端 installSettingsSection 注册 key-fallback 命名空间,使它
127
- // 进入 served 集合后,本卡片才会被 ConfigurablePluginsTab 渲染。
128
- // 参考 dshmarket:注册通过嵌套 settingsScope inject(保持模块级无
129
- // settingsScope 硬依赖——旧宿主没有该服务时不至于拖垮整个插件)。
132
+ var slots = ctx.get('slots')
133
+ if (slots === undefined) return
134
+ // 1) 独立设置页:左侧菜单出现“API Key 回退”项(永远可见,不依赖 served)。
135
+ // 与 dshmarket/connector 的 settings.section 同构。
136
+ slots.inject('settings.section', function () {
137
+ return slots.register({
138
+ name: 'settings.section',
139
+ id: 'key-fallback',
140
+ order: 62,
141
+ label: 'API Key 回退',
142
+ }, FallbackSection)
143
+ })
144
+ // 2) 插件配置标签下的状态卡(served 时显示;上述独立页的补充)。
145
+ // 我们的 host 已用 installSettingsSection 注册 key-fallback 命名空间使它可 serve;
146
+ // 这里复用同一 KeyFallbackCard。
130
147
  var scopedInject = ctx.inject
131
148
  if (typeof scopedInject === 'function') {
132
149
  scopedInject(['settingsScope'], function (scoped) {
package/lib/index.js CHANGED
@@ -1,25 +1,19 @@
1
- 'use strict'
2
- // @omdp/dsh-key-fallback — Host half
1
+ // @omdp/dsh-key-fallback — Host half (ESM)
3
2
  // 极简 API Key 回退:请求报错 → 静默换下一个 key 重试同一请求。
4
- const { readFileSync, existsSync } = require('node:fs')
5
- const { homedir } = require('node:os')
6
- const { join } = require('node:path')
7
- const { load: loadYaml } = (() => {
8
- try { return require('js-yaml') }
9
- catch { return { load: (s) => { try { return JSON.parse(s) } catch { return {} } } } }
10
- })()
11
- // dsh-settings 官方助手:installSettingsSection 挂在 scoped fiber 上,注册命名空间
12
- // 后 client 的 settings.plugin.item 键槽才能把它 serve 并渲染状态卡。
13
- // 参考 dshmarket(同款姿势,实证可显示)。schemastery 用于构造命名空间 schema。
14
- const dshSettings = (() => {
15
- try { return require('@deepseek-ai/dsh-settings') } catch { return null }
16
- })()
17
- const zs = (() => {
18
- try { return require('@deepseek-ai/schemastery') } catch { return null }
19
- })()
3
+ //
4
+ // 注意:必须 ESM + 静态 import(与 dshmarket / vision-bridge 一致)。
5
+ // DSH 的 bundle loader 对 CommonJS 的 require() 是受限的(先前实测
6
+ // dshSettings=false zs=false),只有 ESM import 能解析 DSH 内部包
7
+ // (@deepseek-ai/dsh-settings / @deepseek-ai/schemastery)。
8
+ import { readFileSync, existsSync } from 'node:fs'
9
+ import { homedir } from 'node:os'
10
+ import { join } from 'node:path'
11
+ import { load as loadYaml } from 'js-yaml'
12
+ import * as dshSettings from '@deepseek-ai/dsh-settings'
13
+ import * as zs from '@deepseek-ai/schemastery'
20
14
 
21
- const name = 'key-fallback'
22
- const inject = ['llm', 'settings', 'webServer']
15
+ export const name = 'key-fallback'
16
+ export const inject = ['llm', 'settings', 'webServer']
23
17
  const API_BASE = '/dsh-key-fallback'
24
18
  const SETTINGS_FILE = join(homedir(), '.dsh', 'settings.yaml')
25
19
  // 与客户端 slots.register key 保持一致的命名空间名。
@@ -51,32 +45,25 @@ function readConfig() {
51
45
  } catch (e) { return {} }
52
46
  }
53
47
 
54
- function apply(ctx) {
48
+ export function apply(ctx) {
55
49
  // 注册 settings 命名空间(官方助手 installSettingsSection,与 dshmarket 同款):
56
50
  // 让客户端 Settings → 插件的 keyed slot(settings.plugin.item, key='key-fallback')
57
51
  // 被 serve 并渲染状态卡。状态卡只读;轮换逻辑仍直接读 settings.yaml(原设计)。
58
- // 诊断日志:确认注册链路是否真的执行(dsh-err.log 可查)。
59
- if (dshSettings && zs) {
60
- try {
61
- const ns = dshSettings.settingsNamespace(SETTINGS_NS)
62
- const schema = zs.object({
63
- providers: zs.dict(zs.object({
64
- env: zs.string(),
65
- keys: zs.array(zs.string()),
66
- cooldownMs: zs.natural(),
67
- })),
68
- })
69
- dshSettings.installSettingsSection(ctx, ns, schema, {}, {
70
- setSource: () => {},
71
- onChange: () => {},
72
- })
73
- console.log('[key-fallback] settings namespace registered: ' + SETTINGS_NS)
74
- } catch (e) {
75
- console.log('[key-fallback] settings register FAILED: ' + (e && e.message ? e.message : e))
76
- // 命名空间注册失败不影响核心轮换逻辑
77
- }
78
- } else {
79
- console.log('[key-fallback] settings register SKIPPED (dshSettings=' + (!!dshSettings) + ' zs=' + (!!zs) + ')')
52
+ try {
53
+ const ns = dshSettings.settingsNamespace(SETTINGS_NS)
54
+ const schema = zs.object({
55
+ providers: zs.dict(zs.object({
56
+ env: zs.string(),
57
+ keys: zs.array(zs.string()),
58
+ cooldownMs: zs.natural(),
59
+ })),
60
+ })
61
+ dshSettings.installSettingsSection(ctx, ns, schema, {}, {
62
+ setSource: () => {},
63
+ onChange: () => {},
64
+ })
65
+ } catch (e) {
66
+ // 命名空间注册失败不影响核心轮换逻辑
80
67
  }
81
68
 
82
69
  const pools = new Map()
@@ -205,5 +192,3 @@ function apply(ctx) {
205
192
 
206
193
  ctx.effect(() => () => { lifetime.abort() })
207
194
  }
208
-
209
- module.exports = { name, inject, apply }
package/package.json CHANGED
@@ -1,8 +1,8 @@
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.4",
5
- "type": "commonjs",
4
+ "version": "1.0.6",
5
+ "type": "module",
6
6
  "main": "lib/index.js",
7
7
  "exports": {
8
8
  ".": { "default": "./lib/index.js" },