dsh-volcengine-provider 0.1.0 → 0.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/client.js CHANGED
@@ -28,6 +28,8 @@ const arkRemote = {
28
28
  // 2. 双套餐卡片:Agent 套餐与编程套餐各自独立选择账号、拉取模型目录、刷新额度。
29
29
  // 3. 网页登录:登录前先讲清副作用,登录中只展示状态与取消入口;
30
30
  // 官方 CLI 在系统默认浏览器里完成授权,页面既不接触授权码,也不接触 token。
31
+ // 4. 模型管理:套餐启用开关、逐模型启用开关、模型参数编辑与恢复默认。
32
+ // 套餐与模型的停用只影响路由注册,不会清掉账号选择与已保存的参数。
31
33
  //
32
34
  // 数据通道:所有交互都经由 remote.volcengineProvider.invoke({op: ...}) 单一接口往返,
33
35
  // 返回 {ok, value} 或 {ok:false, error}。协议里只可能出现凭据「引用名」,
@@ -103,6 +105,55 @@ const CATALOG_SOURCE_LABELS = {
103
105
  remote: '官方接口'
104
106
  };
105
107
 
108
+ /**
109
+ * Core 已有的思考档位候选。档位映射字典只接受这些键,其余一律按非法处理。
110
+ * 与 dsh-provider-config 规范里的标准档位保持一致。
111
+ */
112
+ const KNOWN_EFFORTS = ['off', 'minimal', 'low', 'medium', 'high', 'xhigh', 'max'];
113
+
114
+ /** 模型参数字段标签:保留技术字段名,方便和后端负载逐项对照。 */
115
+ const MODEL_FIELD_LABELS = {
116
+ contextWindow: '上下文长度 contextWindow',
117
+ maxTokens: '最大输出 token maxTokens',
118
+ temperature: '温度 temperature'
119
+ };
120
+
121
+ /**
122
+ * 思考模式文案。必须区分两种上游行为:
123
+ * 原生 effort(adaptive)——档位原样交给上游,由上游决定思考量;
124
+ * 固定预算(budget_tokens)——档位先映射成固定 token 预算。
125
+ */
126
+ const ADAPTIVE_MODE_OPTIONS = [
127
+ { value: 'inherit', label: '继承目录默认' },
128
+ { value: 'true', label: '原生 effort 模式(adaptive):档位交给上游决定思考量' },
129
+ { value: 'false', label: '固定预算模式(budget_tokens):档位映射为固定 token 预算' }
130
+ ];
131
+
132
+ /**
133
+ * 思考档位映射的说明。
134
+ *
135
+ * 语义边界必须讲清楚:reasoningEfforts=false 只是「不提供可选思考档位、按模型默认」,
136
+ * 底层可能根本不发 thinking 字段,不能说成强制关闭思考;
137
+ * 真正关闭思考是默认档位选 off、且映射里声明 off:null。
138
+ */
139
+ const EFFORTS_HINT = '键是界面档位,值是发往上游的档位字符串;只有 off 可写 null(真正的关闭思考是默认档位选 off 且映射声明 off:null);reasoningEfforts=false 只表示不提供可选思考档位,底层可能不发 thinking 字段,不等于强制关闭思考。';
140
+
141
+ /**
142
+ * 固定预算模式上游真正消费的档位。
143
+ * pi-ai 的预算模式只认 off/minimal/low/medium/high,xhigh 与 max 会被折叠成 high,
144
+ * 且不消费映射值本身,所以预算模式下必须由界面拦住这两类写法。
145
+ */
146
+ const BUDGET_EFFORTS = ['off', 'minimal', 'low', 'medium', 'high'];
147
+
148
+ /** 两种思考模式对档位映射的不同要求,切换模式时要说清楚差别。 */
149
+ const ADAPTIVE_MODE_HINTS = {
150
+ true: '原生 effort 模式:按 JSON 里声明的映射值原样发给上游。',
151
+ false: `固定预算模式:上游只消费 ${BUDGET_EFFORTS.join('/')},xhigh/max 会被折叠成 high,非 off 档位的映射值必须与档位同名;已有映射不会被自动删除,需要时请手工调整 JSON。`
152
+ };
153
+
154
+ /** 温度与输出 token 的已知约束:思考开启时上游可能忽略温度。 */
155
+ const TEMPERATURE_HINT = '开启思考时上游可能忽略温度;输出 token 还受思考预算与上下文长度约束。';
156
+
106
157
  /**
107
158
  * 启动网页登录前必须让用户看到的影响说明。
108
159
  * 这几条是登录的真实副作用,缺一条就可能让人以为会覆盖本机 CLI 身份或删掉凭据。
@@ -385,6 +436,249 @@ function draftFromAccount(account) {
385
436
  };
386
437
  }
387
438
 
439
+ // ── 模型管理(客户端侧校验与载荷组装) ────────────────────────────────────────
440
+
441
+ /**
442
+ * 读取套餐启用状态。旧快照没有 enabled 字段时按启用处理,避免升级后功能被静默关掉。
443
+ *
444
+ * @param {object} snapshot 整份快照
445
+ * @param {string} plan 套餐标识
446
+ * @returns {boolean} 是否启用
447
+ */
448
+ function providerEnabledOf(snapshot, plan) {
449
+ const value = snapshot?.enabled?.[plan];
450
+ if (value === undefined || value === null) return true;
451
+ return Boolean(value);
452
+ }
453
+
454
+ /**
455
+ * 把思考档位映射整理成一句可读说明。
456
+ *
457
+ * @param {unknown} efforts 档位映射、false 或 undefined
458
+ * @returns {string} 说明文本
459
+ */
460
+ function effortSummary(efforts) {
461
+ if (efforts === false) return '不提供可选思考档位';
462
+ if (!efforts || typeof efforts !== 'object') return '继承目录默认';
463
+ const entries = Object.entries(efforts);
464
+ if (!entries.length) return '继承目录默认';
465
+ return entries.map(([tier, value]) => (tier === 'off' && value === null ? 'off=关闭思考' : `${tier}=${String(value)}`)).join('、');
466
+ }
467
+
468
+ /**
469
+ * 描述某个模型当前的参数覆盖。
470
+ *
471
+ * @param {object} model 目录里的模型对象
472
+ * @returns {string} 说明文本
473
+ */
474
+ function describeModelSettings(model) {
475
+ const settings = model?.settings && typeof model.settings === 'object' ? model.settings : {};
476
+ const parts = [];
477
+ if (Number.isFinite(settings.contextWindow)) parts.push(`上下文长度 ${settings.contextWindow}`);
478
+ if (Number.isFinite(settings.maxTokens)) parts.push(`最大输出 ${settings.maxTokens}`);
479
+ if (Number.isFinite(settings.temperature)) parts.push(`温度 ${settings.temperature}`);
480
+ if (Object.hasOwn(settings, 'reasoningEfforts')) parts.push(`思考档位 ${effortSummary(settings.reasoningEfforts)}`);
481
+ if (Object.hasOwn(settings, 'defaultReasoning')) parts.push(`默认档位 ${settings.defaultReasoning === null ? '不强制' : String(settings.defaultReasoning)}`);
482
+ if (Object.hasOwn(settings, 'forceAdaptiveThinking')) parts.push(settings.forceAdaptiveThinking ? '原生 effort 模式' : '固定预算模式');
483
+ return parts.length ? parts.join(',') : '继承目录默认';
484
+ }
485
+
486
+ /**
487
+ * 描述某个模型最终生效的参数。
488
+ *
489
+ * @param {object} model 目录里的模型对象
490
+ * @returns {string} 说明文本,空串表示后端没有给出生效值
491
+ */
492
+ function describeEffective(model) {
493
+ const effective = model?.effective && typeof model.effective === 'object' ? model.effective : null;
494
+ if (!effective) return '';
495
+ const parts = [];
496
+ if (Number.isFinite(effective.contextWindow)) parts.push(`上下文长度 ${effective.contextWindow}`);
497
+ if (Number.isFinite(effective.maxTokens)) parts.push(`最大输出 ${effective.maxTokens}`);
498
+ if (Number.isFinite(effective.temperature)) parts.push(`温度 ${effective.temperature}`);
499
+ if (Object.hasOwn(effective, 'reasoningEfforts')) parts.push(`思考档位 ${effortSummary(effective.reasoningEfforts)}`);
500
+ if (Object.hasOwn(effective, 'defaultReasoning')) parts.push(`默认档位 ${effective.defaultReasoning === null ? '不强制' : String(effective.defaultReasoning)}`);
501
+ if (Object.hasOwn(effective, 'forceAdaptiveThinking')) parts.push(effective.forceAdaptiveThinking ? '原生 effort 模式' : '固定预算模式');
502
+ return parts.join(',');
503
+ }
504
+
505
+ /**
506
+ * 由模型对象生成参数草稿。草稿只保存原始覆盖值:留空即表示继承目录默认。
507
+ *
508
+ * @param {object} model 目录里的模型对象
509
+ * @returns {object} 草稿
510
+ */
511
+ function modelDraftFrom(model) {
512
+ const settings = model?.settings && typeof model.settings === 'object' ? model.settings : {};
513
+ const efforts = settings.reasoningEfforts;
514
+ let effortsMode = 'inherit';
515
+ let effortsText = '';
516
+ if (efforts === false) effortsMode = 'none';
517
+ else if (efforts && typeof efforts === 'object') {
518
+ effortsMode = 'map';
519
+ effortsText = JSON.stringify(efforts);
520
+ }
521
+ const defaultReasoning = settings.defaultReasoning === undefined
522
+ ? 'inherit'
523
+ : settings.defaultReasoning === null
524
+ ? 'none'
525
+ : String(settings.defaultReasoning);
526
+ const adaptive = settings.forceAdaptiveThinking === undefined
527
+ ? 'inherit'
528
+ : settings.forceAdaptiveThinking === true
529
+ ? 'true'
530
+ : 'false';
531
+ return {
532
+ contextWindow: settings.contextWindow === undefined ? '' : String(settings.contextWindow),
533
+ maxTokens: settings.maxTokens === undefined ? '' : String(settings.maxTokens),
534
+ temperature: settings.temperature === undefined ? '' : String(settings.temperature),
535
+ effortsMode,
536
+ effortsText,
537
+ defaultReasoning,
538
+ adaptive
539
+ };
540
+ }
541
+
542
+ /**
543
+ * 取目录里生效的档位映射(草稿未覆盖时使用)。
544
+ *
545
+ * @param {object} model 目录里的模型对象
546
+ * @returns {unknown} 档位映射、false 或 undefined
547
+ */
548
+ function inheritedEfforts(model) {
549
+ const effective = model?.effective?.reasoningEfforts;
550
+ if (effective !== undefined) return effective;
551
+ const defaults = model?.defaults?.reasoningEfforts;
552
+ if (defaults !== undefined) return defaults;
553
+ return undefined;
554
+ }
555
+
556
+ /**
557
+ * 列出当前草稿下可用的思考档位。
558
+ *
559
+ * 自定义映射优先;映射非法时回退到目录里的有效档位,保证界面仍可操作。
560
+ *
561
+ * @param {object} draft 参数草稿
562
+ * @param {object} model 目录里的模型对象
563
+ * @returns {Array<string>} 档位列表
564
+ */
565
+ function draftTiers(draft, model) {
566
+ const mode = draft?.effortsMode ?? 'inherit';
567
+ if (mode === 'none') return [];
568
+ if (mode === 'map') {
569
+ try {
570
+ const parsed = JSON.parse(String(draft?.effortsText ?? '').trim() || 'null');
571
+ if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) return Object.keys(parsed);
572
+ } catch {
573
+ // 非法 JSON:交由校验环节报错,这里回退到目录里的有效档位。
574
+ }
575
+ }
576
+ const inherited = inheritedEfforts(model);
577
+ if (inherited && typeof inherited === 'object') return Object.keys(inherited);
578
+ return [...KNOWN_EFFORTS];
579
+ }
580
+
581
+ /**
582
+ * 把参数草稿整理成后端负载,并做基础格式校验。
583
+ *
584
+ * 这里只做格式层面的兜底:后端仍会独立校验同一份负载,
585
+ * 前端校验的目的是不让明显非法的值发出去、并在界面上直接说明原因。
586
+ *
587
+ * @param {object} draft 参数草稿
588
+ * @param {object} model 目录里的模型对象
589
+ * @returns {{settings: object|null, error: string|null}} 负载或错误说明
590
+ */
591
+ function parseModelSettings(draft, model) {
592
+ const problems = [];
593
+ const settings = {};
594
+ const text = (key) => String(draft?.[key] ?? '').trim();
595
+
596
+ for (const key of ['contextWindow', 'maxTokens']) {
597
+ const raw = text(key);
598
+ if (!raw) continue; // 空值=继承目录默认,不写进负载。
599
+ const value = Number(raw);
600
+ if (!/^\d+$/.test(raw) || !Number.isSafeInteger(value) || value <= 0) {
601
+ problems.push(`${MODEL_FIELD_LABELS[key]} 须为正整数。`);
602
+ continue;
603
+ }
604
+ settings[key] = value;
605
+ }
606
+
607
+ const temperature = text('temperature');
608
+ if (temperature) {
609
+ const value = Number(temperature);
610
+ if (!Number.isFinite(value) || value < 0 || value > 1) problems.push('温度 temperature 须为 0 到 1 之间的数值。');
611
+ else settings.temperature = value;
612
+ }
613
+
614
+ const effortsMode = draft?.effortsMode ?? 'inherit';
615
+ // 生效中的档位映射:自定义映射用草稿里的值,继承时用目录里的有效值。
616
+ let effortsMap = null;
617
+ if (effortsMode === 'none') {
618
+ settings.reasoningEfforts = false;
619
+ } else if (effortsMode === 'map') {
620
+ const effortProblems = [];
621
+ let parsed = null;
622
+ try {
623
+ parsed = JSON.parse(text('effortsText') || 'null');
624
+ } catch {
625
+ effortProblems.push('思考档位映射必须是合法 JSON。');
626
+ }
627
+ if (!effortProblems.length) {
628
+ if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) effortProblems.push('思考档位映射必须是 JSON 对象。');
629
+ }
630
+ if (!effortProblems.length) {
631
+ const entries = Object.entries(parsed);
632
+ if (entries.some(([tier]) => !KNOWN_EFFORTS.includes(tier))) effortProblems.push(`思考档位只接受 Core 已有档位:${KNOWN_EFFORTS.join(' / ')}。`);
633
+ if (entries.some(([tier, value]) => value === null && tier !== 'off')) effortProblems.push('只有 off 档位允许为 null。');
634
+ if (entries.some(([tier, value]) => tier !== 'off' && (typeof value !== 'string' || !value.trim()))) effortProblems.push('除 off 外的档位必须映射到非空字符串。');
635
+ if (!entries.some(([tier]) => tier !== 'off')) effortProblems.push('至少要声明一个非 off 档位。');
636
+ }
637
+ if (effortProblems.length) problems.push(...effortProblems);
638
+ else {
639
+ settings.reasoningEfforts = parsed;
640
+ effortsMap = parsed;
641
+ }
642
+ } else {
643
+ const inherited = inheritedEfforts(model);
644
+ if (inherited && typeof inherited === 'object') effortsMap = inherited;
645
+ }
646
+
647
+ // 固定预算模式的上游只消费 off/minimal/low/medium/high,且不消费映射值本身:
648
+ // xhigh/max 会被折叠成 high,非同名的映射值也不会生效。
649
+ if ((draft?.adaptive ?? 'inherit') === 'false' && effortsMap) {
650
+ const badTiers = Object.keys(effortsMap).filter((tier) => !BUDGET_EFFORTS.includes(tier));
651
+ const badValues = Object.entries(effortsMap)
652
+ .filter(([tier, value]) => tier !== 'off' && value !== tier)
653
+ .map(([tier]) => tier);
654
+ const badOff = Object.entries(effortsMap)
655
+ .filter(([tier, value]) => tier === 'off' && value !== null)
656
+ .map(([tier]) => tier);
657
+ const suffix = effortsMode === 'map'
658
+ ? ';请手工调整 JSON 映射(不会自动删除已有映射)'
659
+ : ';当前档位映射来自目录默认,请改为自定义 JSON 映射';
660
+ if (badTiers.length || badValues.length) {
661
+ problems.push(`预算模式仅支持 ${BUDGET_EFFORTS.join('/')},非 off 映射值与档位同名${suffix}`);
662
+ }
663
+ if (badOff.length) problems.push(`预算模式下 off 档位只能为空值(null)${suffix}`);
664
+ }
665
+
666
+ const defaultReasoning = draft?.defaultReasoning ?? 'inherit';
667
+ if (defaultReasoning === 'none') {
668
+ settings.defaultReasoning = null;
669
+ } else if (defaultReasoning !== 'inherit') {
670
+ if (!draftTiers(draft, model).includes(defaultReasoning)) problems.push('默认思考档位必须是有效档位,且要与档位映射一致。');
671
+ else settings.defaultReasoning = defaultReasoning;
672
+ }
673
+
674
+ const adaptive = draft?.adaptive ?? 'inherit';
675
+ if (adaptive === 'true') settings.forceAdaptiveThinking = true;
676
+ else if (adaptive === 'false') settings.forceAdaptiveThinking = false;
677
+
678
+ if (problems.length) return { settings: null, error: problems.join(' ') };
679
+ return { settings, error: null };
680
+ }
681
+
388
682
  // ── 展示组件 ─────────────────────────────────────────────────────────────────
389
683
 
390
684
  /**
@@ -455,13 +749,127 @@ function QuotaWindows({ quota }) {
455
749
  );
456
750
  }
457
751
 
752
+ /**
753
+ * 模型参数编辑面板。
754
+ *
755
+ * 空数字=继承目录默认;档位映射可整段关闭(false)或自定义 JSON;
756
+ * 默认档位分「继承 / 不强制 / 有效档位」三态;思考模式分「继承 / 原生 effort / 固定预算」。
757
+ *
758
+ * @param {object} props 组件属性
759
+ * @returns {object} 元素树
760
+ */
761
+ function ModelSettingsEditor({ model, draft, error, disabled, onChange, onSave, onCancel }) {
762
+ const stackedField = { ...style.field, flex: '0 0 auto', minWidth: 0 };
763
+ const effortsMode = draft?.effortsMode ?? 'inherit';
764
+ const tiers = draftTiers(draft, model);
765
+ const numberField = (key, label) => h('label', { style: style.field },
766
+ h('span', { style: style.label }, label),
767
+ h('input', {
768
+ type: 'text', inputMode: 'numeric', style: style.input, value: draft?.[key] ?? '', disabled,
769
+ 'aria-label': label, placeholder: '留空表示继承目录默认', autoComplete: 'off', spellCheck: false,
770
+ onChange: (event) => onChange({ [key]: event.target.value })
771
+ }));
772
+ return h('div', { style: { display: 'flex', flexDirection: 'column', gap: 8, padding: '8px 10px', border: '1px solid var(--dsw-alias-border-l2, rgba(128,128,128,.3))', borderRadius: 'var(--dsw-radius-sm, 6px)' } },
773
+ h('div', { style: { fontWeight: 600, fontSize: 13 } }, `模型参数:${model?.name || model?.id}`),
774
+ h('div', { style: { display: 'flex', flexWrap: 'wrap', gap: 10 } },
775
+ numberField('contextWindow', MODEL_FIELD_LABELS.contextWindow),
776
+ numberField('maxTokens', MODEL_FIELD_LABELS.maxTokens),
777
+ numberField('temperature', MODEL_FIELD_LABELS.temperature)),
778
+ h('p', { style: style.muted }, TEMPERATURE_HINT),
779
+ h('label', { style: stackedField },
780
+ h('span', { style: style.label }, '思考档位映射 reasoningEfforts'),
781
+ h('select', {
782
+ style: style.input, value: effortsMode, disabled, 'aria-label': '思考档位映射 reasoningEfforts',
783
+ onChange: (event) => onChange({ effortsMode: event.target.value })
784
+ },
785
+ h('option', { value: 'inherit' }, '继承目录默认'),
786
+ h('option', { value: 'none' }, '不提供可选思考档位(false)'),
787
+ h('option', { value: 'map' }, '自定义 JSON 映射'))),
788
+ effortsMode === 'none'
789
+ ? h('p', { style: style.muted }, 'reasoningEfforts=false 表示不提供可选思考档位(按模型默认);底层可能不发 thinking 字段,不等于强制关闭思考。真正关闭思考需要把默认档位设为 off 且映射声明 off:null。')
790
+ : null,
791
+ effortsMode === 'map'
792
+ ? h('label', { style: stackedField },
793
+ h('span', { style: style.label }, 'JSON 映射'),
794
+ h('textarea', {
795
+ style: { ...style.input, minHeight: 64 }, value: draft?.effortsText ?? '', disabled,
796
+ 'aria-label': 'JSON 映射', spellCheck: false, autoComplete: 'off',
797
+ onChange: (event) => onChange({ effortsText: event.target.value })
798
+ }))
799
+ : null,
800
+ effortsMode === 'map' ? h('p', { style: style.muted }, EFFORTS_HINT) : null,
801
+ h('label', { style: stackedField },
802
+ h('span', { style: style.label }, '默认思考档位 defaultReasoning'),
803
+ h('select', {
804
+ style: style.input, value: draft?.defaultReasoning ?? 'inherit', disabled,
805
+ 'aria-label': '默认思考档位 defaultReasoning',
806
+ onChange: (event) => onChange({ defaultReasoning: event.target.value })
807
+ },
808
+ h('option', { value: 'inherit' }, '继承目录默认'),
809
+ h('option', { value: 'none' }, '不强制默认档位(null)'),
810
+ tiers.map((tier) => h('option', { key: tier, value: tier }, tier)))),
811
+ h('label', { style: stackedField },
812
+ h('span', { style: style.label }, '思考模式 forceAdaptiveThinking'),
813
+ h('select', {
814
+ style: style.input, value: draft?.adaptive ?? 'inherit', disabled,
815
+ 'aria-label': '思考模式 forceAdaptiveThinking',
816
+ onChange: (event) => onChange({ adaptive: event.target.value })
817
+ },
818
+ ADAPTIVE_MODE_OPTIONS.map((option) => h('option', { key: option.value, value: option.value }, option.label)))),
819
+ ADAPTIVE_MODE_HINTS[draft?.adaptive] ? h('p', { style: style.muted }, ADAPTIVE_MODE_HINTS[draft.adaptive]) : null,
820
+ error ? h('p', { role: 'alert', style: style.error }, error) : null,
821
+ h('div', { style: style.row },
822
+ h('button', { type: 'button', style: style.primary, disabled, onClick: onSave }, '保存参数'),
823
+ h('button', { type: 'button', style: style.btn, disabled, onClick: onCancel }, '取消'))
824
+ );
825
+ }
826
+
827
+ /**
828
+ * 目录里的单个模型:启用开关 + 参数入口。
829
+ *
830
+ * 停用的模型同样可以编辑参数;重新启用时后端会保留已保存的参数。
831
+ *
832
+ * @param {object} props 组件属性
833
+ * @returns {object} 元素树
834
+ */
835
+ function ModelRow({ model, disabled, editor, draftError, onToggle, onOpenEditor, onCloseEditor, onChangeDraft, onSave, onReset }) {
836
+ const enabled = model?.enabled !== false;
837
+ const editing = editor?.modelId === model?.id;
838
+ const effective = describeEffective(model);
839
+ return h('div', { style: { display: 'flex', flexDirection: 'column', gap: 6, padding: '8px 10px', border: '1px solid var(--dsw-alias-border-l1, rgba(128,128,128,.22))', borderRadius: 'var(--dsw-radius-sm, 6px)' } },
840
+ h('div', { style: style.row },
841
+ h('input', {
842
+ type: 'checkbox', checked: enabled, disabled,
843
+ 'aria-label': `启用模型 ${model?.id ?? ''}`,
844
+ onChange: (event) => onToggle(model, Boolean(event.target.checked))
845
+ }),
846
+ h('span', { style: { fontWeight: 500 } }, model?.name || model?.id),
847
+ h('span', { style: style.chip }, model?.id),
848
+ model?.capabilityUnverified ? h('span', { style: style.chip }, '能力未核实') : null,
849
+ enabled ? null : h('span', { style: style.chip }, '已停用'),
850
+ h('button', { type: 'button', style: style.btn, disabled, 'aria-label': `模型参数 ${model?.id ?? ''}`, onClick: () => (editing ? onCloseEditor() : onOpenEditor(model)) }, editing ? '收起参数' : '参数'),
851
+ h('button', { type: 'button', style: style.btn, disabled, 'aria-label': `恢复默认 ${model?.id ?? ''}`, onClick: () => onReset(model) }, '恢复默认')),
852
+ h('p', { style: style.muted }, `当前参数:${describeModelSettings(model)}`),
853
+ effective ? h('p', { style: style.muted }, `生效值:${effective}`) : null,
854
+ editing
855
+ ? h(ModelSettingsEditor, {
856
+ model, draft: editor.draft, error: draftError, disabled,
857
+ onChange: onChangeDraft, onSave: () => onSave(model), onCancel: onCloseEditor
858
+ })
859
+ : null
860
+ );
861
+ }
862
+
458
863
  /**
459
864
  * 一张套餐卡片。
460
865
  *
866
+ * 套餐启用状态、账号选择、模型开关与模型参数彼此独立:
867
+ * 停用套餐只影响路由注册,不会清掉账号选择,也不会删掉已保存的模型参数。
868
+ *
461
869
  * @param {object} props 组件属性
462
870
  * @returns {object} 元素树
463
871
  */
464
- function PlanCard({ plan, snapshot, rawQuota, busy, onSelect, onModels, onQuota }) {
872
+ function PlanCard({ plan, snapshot, rawQuota, busy, onSelect, onModels, onQuota, onToggleProvider, onToggleModel, onSaveModelSettings, onResetModelSettings }) {
465
873
  const accounts = Array.isArray(snapshot?.accounts) ? snapshot.accounts : [];
466
874
  const activeId = snapshot?.active?.[plan] ?? null;
467
875
  const account = accounts.find((item) => item?.id === activeId) ?? null;
@@ -473,10 +881,77 @@ function PlanCard({ plan, snapshot, rawQuota, busy, onSelect, onModels, onQuota
473
881
  const disabled = busy !== null;
474
882
  const hasInference = !!(account && account?.hasApiKeys?.[plan]);
475
883
  const hasQuotaKeys = !!(account && account?.hasQuotaKeys);
884
+ // 旧快照没有 enabled 字段时按启用处理。
885
+ const providerEnabled = providerEnabledOf(snapshot, plan);
886
+ // 旧快照的目录没有 models 明细:安全地当作「没有列表」展示。
887
+ const models = Array.isArray(catalog?.models) ? catalog.models : [];
888
+ const enabledCount = Number.isFinite(catalog?.enabledCount)
889
+ ? catalog.enabledCount
890
+ : models.filter((item) => item?.enabled !== false).length;
891
+
892
+ // 参数编辑状态:同一时刻只展开一个模型,避免把卡片撑得过长。
893
+ const [editor, setEditor] = react.useState(null);
894
+ const [modelsOpen, setModelsOpen] = react.useState(false);
895
+ const [draftError, setDraftError] = react.useState(null);
896
+
897
+ const editedModel = editor ? models.find((item) => item?.id === editor.modelId) ?? null : null;
898
+ const editedSignature = JSON.stringify(editedModel?.settings ?? null);
899
+
900
+ // 保存或恢复默认后,后端会给出新的覆盖值;此时把草稿对齐到最新状态。
901
+ react.useEffect(() => {
902
+ if (!editor || !editedModel) return;
903
+ setEditor((previous) => {
904
+ if (!previous || previous.modelId !== editedModel.id) return previous;
905
+ if (previous.synced === editedSignature) return previous;
906
+ return { ...previous, draft: modelDraftFrom(editedModel), synced: editedSignature };
907
+ });
908
+ }, [editor?.modelId, editedSignature]);
909
+
910
+ const openEditor = (model) => {
911
+ setEditor({ modelId: model.id, draft: modelDraftFrom(model), synced: JSON.stringify(model?.settings ?? null) });
912
+ setDraftError(null);
913
+ };
914
+
915
+ const closeEditor = () => {
916
+ setEditor(null);
917
+ setDraftError(null);
918
+ };
919
+
920
+ const changeDraft = (patch) => {
921
+ setEditor((previous) => (previous ? { ...previous, draft: { ...previous.draft, ...patch } } : previous));
922
+ };
923
+
924
+ // 校验不通过时不发 RPC:只在界面上说明原因。
925
+ const saveEditor = (model) => {
926
+ if (!editor || !activeId) return;
927
+ const parsed = parseModelSettings(editor.draft, model);
928
+ if (parsed.error) {
929
+ setDraftError(parsed.error);
930
+ return;
931
+ }
932
+ setDraftError(null);
933
+ onSaveModelSettings(plan, activeId, model.id, parsed.settings);
934
+ };
935
+
936
+ const resetModel = (model) => {
937
+ if (!activeId) return;
938
+ onResetModelSettings(plan, activeId, model.id);
939
+ };
476
940
 
477
941
  return h('section', { style: style.card },
478
942
  h('h3', { style: style.cardTitle }, PLAN_TITLES[plan]),
479
943
  h('p', { style: style.muted }, PLAN_HINTS[plan]),
944
+ h('div', { style: style.row },
945
+ h('label', { style: { display: 'flex', alignItems: 'center', gap: 6, fontSize: 13 } },
946
+ h('input', {
947
+ type: 'checkbox', checked: providerEnabled, disabled,
948
+ 'aria-label': `启用${PLAN_TITLES[plan]}`,
949
+ onChange: (event) => onToggleProvider(plan, Boolean(event.target.checked))
950
+ }),
951
+ h('span', null, '启用该套餐')),
952
+ providerEnabled
953
+ ? h('span', { style: style.chip }, '套餐已启用')
954
+ : h('span', { style: style.chip }, '已停用:不注册模型路由(账号选择与模型参数保留)')),
480
955
  h('div', { style: style.row },
481
956
  h('label', { style: style.label }, '使用账号'),
482
957
  h('select', {
@@ -508,6 +983,30 @@ function PlanCard({ plan, snapshot, rawQuota, busy, onSelect, onModels, onQuota
508
983
  ? h('p', { style: style.muted }, `模型目录:${catalog.count} 个模型(来源 ${CATALOG_SOURCE_LABELS[catalog.source] ?? catalog.source}${catalog.updatedAt ? `,更新于 ${formatTime(catalog.updatedAt)}` : ''})。`)
509
984
  : h('p', { style: style.muted }, '模型目录:尚未拉取。'),
510
985
  catalog?.error ? h('p', { role: 'alert', style: style.error }, describeError(catalog.error)) : null,
986
+ models.length
987
+ ? h('details', { open: modelsOpen },
988
+ h('summary', {
989
+ style: { cursor: 'pointer', fontSize: 13 },
990
+ onClick: (event) => {
991
+ // 自己接管开合,避免浏览器原生开合与受控状态打架。
992
+ if (event?.preventDefault) event.preventDefault();
993
+ setModelsOpen((previous) => !previous);
994
+ }
995
+ }, `模型列表(已启用 ${enabledCount} / 共 ${models.length})`),
996
+ h('div', { style: { display: 'flex', flexDirection: 'column', gap: 8, marginTop: 8 } },
997
+ models.map((model, index) => h(ModelRow, {
998
+ key: model?.id ?? `model-${index}`,
999
+ model, disabled, editor, draftError,
1000
+ onToggle: (item, enabled) => onToggleModel(plan, activeId, item.id, enabled),
1001
+ onOpenEditor: openEditor,
1002
+ onCloseEditor: closeEditor,
1003
+ onChangeDraft: changeDraft,
1004
+ onSave: saveEditor,
1005
+ onReset: resetModel
1006
+ }))))
1007
+ : h('p', { style: style.muted }, catalog
1008
+ ? '该目录暂无模型明细(旧缓存可能没有模型列表,重新拉取模型后可用)。'
1009
+ : '模型目录:尚未拉取。'),
511
1010
  h(QuotaWindows, { quota })
512
1011
  );
513
1012
  }
@@ -734,6 +1233,54 @@ function VolcengineProviderSection({ client }) {
734
1233
  }
735
1234
  });
736
1235
 
1236
+ // 套餐启停:只改启用状态,不动账号选择,也不动模型参数。
1237
+ const toggleProvider = (plan, enabled) => run(`provider:${plan}`, async () => {
1238
+ if (!snapshot?.revision) return;
1239
+ try {
1240
+ const value = await client.invoke({ op: 'setProviderEnabled', plan, enabled, revision: snapshot.revision });
1241
+ applySnapshot(value);
1242
+ setNotice({ ok: true, text: `${PLAN_TITLES[plan]}已${enabled ? '启用' : '停用'}(账号选择与模型参数保留)。` });
1243
+ } catch (error) {
1244
+ await reportSaveError(error, `${enabled ? '启用' : '停用'}${PLAN_TITLES[plan]}失败`);
1245
+ }
1246
+ });
1247
+
1248
+ // 逐模型启停:停用只影响模型是否进入目录,参数照旧保留。
1249
+ const toggleModel = (plan, accountId, modelId, enabled) => run(`model:${plan}:${modelId}`, async () => {
1250
+ if (!snapshot?.revision || !accountId) return;
1251
+ try {
1252
+ const value = await client.invoke({ op: 'setModelEnabled', accountId, plan, modelId, enabled, revision: snapshot.revision });
1253
+ applySnapshot(value);
1254
+ setNotice({ ok: true, text: `模型 ${modelId} 已${enabled ? '启用' : '停用'}。` });
1255
+ } catch (error) {
1256
+ await reportSaveError(error, `切换模型 ${modelId} 失败`);
1257
+ }
1258
+ });
1259
+
1260
+ // 保存模型参数:整份覆盖,空值表示继承目录默认。
1261
+ const saveModelSettings = (plan, accountId, modelId, settings) => run(`settings:${plan}:${modelId}`, async () => {
1262
+ if (!snapshot?.revision || !accountId) return;
1263
+ try {
1264
+ const value = await client.invoke({ op: 'saveModelSettings', accountId, plan, modelId, settings, revision: snapshot.revision });
1265
+ applySnapshot(value);
1266
+ setNotice({ ok: true, text: `模型 ${modelId} 的参数已保存。` });
1267
+ } catch (error) {
1268
+ await reportSaveError(error, `保存模型 ${modelId} 的参数失败`);
1269
+ }
1270
+ });
1271
+
1272
+ // 恢复默认:清掉该模型的覆盖值,回到目录默认(含启用状态)。
1273
+ const resetModelSettings = (plan, accountId, modelId) => run(`reset:${plan}:${modelId}`, async () => {
1274
+ if (!snapshot?.revision || !accountId) return;
1275
+ try {
1276
+ const value = await client.invoke({ op: 'resetModelSettings', accountId, plan, modelId, revision: snapshot.revision });
1277
+ applySnapshot(value);
1278
+ setNotice({ ok: true, text: `模型 ${modelId} 的参数已恢复默认。` });
1279
+ } catch (error) {
1280
+ await reportSaveError(error, `恢复模型 ${modelId} 的默认参数失败`);
1281
+ }
1282
+ });
1283
+
737
1284
  const startLogin = (accountId) => run('login', async () => {
738
1285
  if (!snapshot?.revision) return;
739
1286
  try {
@@ -848,7 +1395,7 @@ function VolcengineProviderSection({ client }) {
848
1395
 
849
1396
  return h('div', { style: style.root },
850
1397
  h('header', null,
851
- h('h2', { style: style.title }, 'dsh-volcengine-provider'),
1398
+ h('h2', { style: style.title }, '火山方舟 Provider'),
852
1399
  h('p', { style: style.muted }, '管理火山方舟 Agent 套餐与编程套餐的账号、模型目录与额度。页面只保存凭据引用名。')),
853
1400
  loadError ? h('p', { role: 'alert', style: style.error }, `读取状态失败:${loadError}`) : null,
854
1401
  snapshot?.routingError ? h('p', { role: 'alert', style: style.error }, describeError(snapshot.routingError)) : null,
@@ -864,11 +1411,15 @@ function VolcengineProviderSection({ client }) {
864
1411
  : h('div', { style: { display: 'flex', flexDirection: 'column', gap: 16 } },
865
1412
  h(PlanCard, {
866
1413
  plan: 'agent-plan', snapshot, rawQuota: quotas['agent-plan'], busy,
867
- onSelect: selectAccount, onModels: pullModels, onQuota: pullQuota
1414
+ onSelect: selectAccount, onModels: pullModels, onQuota: pullQuota,
1415
+ onToggleProvider: toggleProvider, onToggleModel: toggleModel,
1416
+ onSaveModelSettings: saveModelSettings, onResetModelSettings: resetModelSettings
868
1417
  }),
869
1418
  h(PlanCard, {
870
1419
  plan: 'coding-plan', snapshot, rawQuota: quotas['coding-plan'], busy,
871
- onSelect: selectAccount, onModels: pullModels, onQuota: pullQuota
1420
+ onSelect: selectAccount, onModels: pullModels, onQuota: pullQuota,
1421
+ onToggleProvider: toggleProvider, onToggleModel: toggleModel,
1422
+ onSaveModelSettings: saveModelSettings, onResetModelSettings: resetModelSettings
872
1423
  }),
873
1424
  h(AccountPanel, {
874
1425
  snapshot, editor, confirmDelete, loginConfirm, login, busy,
@@ -978,7 +1529,7 @@ function apply(ctx) {
978
1529
  name: 'settings.section',
979
1530
  id: 'volcengine-provider',
980
1531
  order: 24,
981
- label: () => 'dsh-volcengine-provider',
1532
+ label: () => '火山方舟 Provider',
982
1533
  inject: () => ({ client })
983
1534
  }, VolcengineProviderSection));
984
1535
  } catch (error) {