dsh-expert-group 0.1.1 → 0.1.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/client.js CHANGED
@@ -2833,7 +2833,7 @@ window.__ModuleLoader__.load({
2833
2833
  { id: "pendingSettingsWrites", label: "在途 settings 写入", path: ["settings", "pendingWrites"], kind: "count" },
2834
2834
  { id: "settingsGeneration", label: "模型目录代次", path: ["settings", "generation"], kind: "count" },
2835
2835
  { id: "storageGeneration", label: "storage generation", path: ["storage", "generation"], kind: "count" },
2836
- { id: "handoffPending", label: "goal handoff 记录", path: ["handoff", "pending"], kind: "count" },
2836
+ { id: "handoffPending", label: "pending admission", path: ["handoff", "pending"], kind: "count" },
2837
2837
  { id: "notificationRoots", label: "已登记通知根会话", path: ["notifications", "roots"], kind: "count" },
2838
2838
  { id: "fanoutRestorePending", label: "fan-out 待恢复", path: ["fanout", "restorePending"], kind: "flag" },
2839
2839
  { id: "storageStatus", label: "storage 状态", path: ["storage", "status"], kind: "token" },
@@ -2841,7 +2841,7 @@ window.__ModuleLoader__.load({
2841
2841
 
2842
2842
  /** 宿主能力矩阵到客户端展示面的映射(状态语义统一为 available/degraded/absent/unknown)。 */
2843
2843
  const HOST_CAPABILITY_FIELDS = Object.freeze([
2844
- { key: "goals", id: "host.goals", label: "Goal handoff(宿主 goals)" },
2844
+ { key: "rootTurnWait", id: "host.rootTurnWait", label: "Root turn 等待(session-activity)" },
2845
2845
  { key: "connectionRpc", id: "host.connectionRpc", label: "Connection RPC 通道" },
2846
2846
  { key: "storageDomain", id: "host.storageDomain", label: "storage domain 绑定" },
2847
2847
  { key: "attachments", id: "host.attachments", label: "附件/图片规范化" },
@@ -2852,7 +2852,7 @@ window.__ModuleLoader__.load({
2852
2852
  function hostCapabilityStatusOf(status) {
2853
2853
  if (status === "available" || status === "ready") return "available";
2854
2854
  if (status === "incomplete" || status === "degraded" || status === "fail-closed" || status === "legacy" || status === "partial") return "degraded";
2855
- if (status === "absent" || status === "missing" || status === "none") return "absent";
2855
+ if (status === "absent" || status === "missing" || status === "none" || status === "unavailable") return "absent";
2856
2856
  return "unknown";
2857
2857
  }
2858
2858
 
@@ -3369,6 +3369,7 @@ window.__ModuleLoader__.load({
3369
3369
  const [conflict, setConflict] = react.useState(false);
3370
3370
  const [expandedRows, setExpandedRows] = react.useState(() => new Set());
3371
3371
  const [fetchState, setFetchState] = react.useState(null);
3372
+ const [candidateQuery, setCandidateQuery] = react.useState("");
3372
3373
  const [keyDraft, setKeyDraft] = react.useState("");
3373
3374
  // createCardSession 是可变会话;版本号把会话草稿变化桥接到 React 渲染。
3374
3375
  const [, setDraftRevision] = react.useState(0);
@@ -3456,6 +3457,7 @@ window.__ModuleLoader__.load({
3456
3457
  session.cancel();
3457
3458
  setExpandedRows(new Set());
3458
3459
  setFetchState(null);
3460
+ setCandidateQuery("");
3459
3461
  setError(null);
3460
3462
  setSaved(false);
3461
3463
  setConflict(false);
@@ -3520,8 +3522,13 @@ window.__ModuleLoader__.load({
3520
3522
  }
3521
3523
  };
3522
3524
  const fetchModels = async () => {
3525
+ // 手动意图必须在 await 之前同步置位:正在途中的自动探测回调据此放弃写草稿,
3526
+ // 否则自动结果可能晚于这次点击返回,把完整目录直接写进草稿绕过用户选择。
3527
+ discoveryIntent.manualIntent = true;
3523
3528
  setSaving(true);
3524
3529
  setError(null);
3530
+ setFetchState(null);
3531
+ setCandidateQuery("");
3525
3532
  try {
3526
3533
  const draft = session.draft;
3527
3534
  const baseURL = typeof draft.baseURL === "string" && draft.baseURL.length > 0 ? draft.baseURL : undefined;
@@ -3541,17 +3548,90 @@ window.__ModuleLoader__.load({
3541
3548
  setFetchState({ error: "该端点没有返回任何模型", candidates: null });
3542
3549
  return;
3543
3550
  }
3544
- setFetchState({ error: null, candidates: answer.models.map((model) => ({ ...model, picked: false })) });
3551
+ // 手动候选到达后先清掉"自动 fallback 写入且用户没改过"的行:这次手动发现
3552
+ // 取得候选选择权,自动填充的目录不得继续留在草稿里绕过用户勾选。
3553
+ // 用户在自动行上做过的修改(行内容已与快照不同)与其它手工行一律保留。
3554
+ const pruned = pruneUneditedAutoFallbackRows();
3555
+ if (pruned !== null) {
3556
+ session.edit((draft) => ({ ...draft, models: pruned }));
3557
+ setDraftRevision((value) => value + 1);
3558
+ setSaved(false);
3559
+ }
3560
+ // known 按清理后的草稿计算:被清掉的自动行不再算"已存在",手动候选里同 id 的条目
3561
+ // 因此会作为可勾选候选出现,而不是被当成已有行静默跳过。
3562
+ const known = new Set(session.modelRows
3563
+ .map((model) => (typeof model.id === "string" ? model.id.trim() : ""))
3564
+ .filter((id) => id.length > 0));
3565
+ setFetchState({
3566
+ error: null,
3567
+ candidates: answer.models.map((model) => ({
3568
+ ...model,
3569
+ // 手动发现的候选默认只勾选"草稿里还没有"的模型;自动 fallback 已先写入的行
3570
+ // 在手动发现命中后(除非用户改过)已被上面清掉,因此这里会重新作为候选出现,
3571
+ // 由用户显式勾选决定是否保留,而不是被自动目录静默带入。
3572
+ picked: typeof model.id === "string" && model.id.trim().length > 0
3573
+ && !known.has(model.id.trim()),
3574
+ })),
3575
+ });
3545
3576
  } finally {
3546
3577
  setSaving(false);
3547
3578
  }
3548
3579
  };
3580
+ /**
3581
+ * 自动 fallback 探测与手动「获取模型」之间的共享意图状态。
3582
+ *
3583
+ * 挂在稳定的 `session` 依赖的 useMemo 上:卡片重渲染不会重建它,因此晚到的自动探测
3584
+ * 回调看到的是**同一次**手动意图,而不是快照里的旧值。两个字段各司其职:
3585
+ * - `manualIntent`:用户点过「获取模型」就置位。自动探测晚于点击返回时必须放弃,
3586
+ * 绝不能把完整目录直接写进草稿(否则用户还没选模型就被隐式写入了目录)。
3587
+ * - `autoFallback`:最近一次自动探测写进草稿的行快照。手动候选到达后据此只清掉
3588
+ * "仍与该快照完全相同"的未编辑行,用户在自动 fallback 上做过的修改一律保留。
3589
+ */
3590
+ const discoveryIntent = react.useMemo(() => ({ manualIntent: false, autoFallback: null }), [session]);
3591
+ /** 记录自动 fallback 快照(同一批自动行只记一次,供之后的手动探测做"未编辑"判定)。 */
3592
+ const noteAutoFallback = (models) => {
3593
+ if (discoveryIntent.autoFallback !== null) return;
3594
+ discoveryIntent.autoFallback = Array.isArray(models) ? models.map((model) => ({ ...model })) : [];
3595
+ };
3596
+ /** 草稿行是否与自动 fallback 快照里同 id 的行逐字段相同(即"未编辑的自动填充行")。 */
3597
+ const isUneditedAutoFallbackRow = (row) => {
3598
+ const snapshot = discoveryIntent.autoFallback;
3599
+ if (!Array.isArray(snapshot) || !config.isPlainObject(row)) return false;
3600
+ const id = typeof row.id === "string" ? row.id.trim() : "";
3601
+ if (id.length === 0) return false;
3602
+ return snapshot.some((entry) => {
3603
+ if (!config.isPlainObject(entry)) return false;
3604
+ const entryId = typeof entry.id === "string" ? entry.id.trim() : "";
3605
+ return entryId === id && config.jsonEqual(entry, row);
3606
+ });
3607
+ };
3608
+ /**
3609
+ * 手动候选到达后清理草稿:只移除仍与自动 fallback 快照完全相同的未编辑行。
3610
+ * 返回新行数组;没有任何行被移除时返回 null,调用方据此跳过无谓的草稿改写。
3611
+ */
3612
+ const pruneUneditedAutoFallbackRows = () => {
3613
+ const snapshot = discoveryIntent.autoFallback;
3614
+ if (!Array.isArray(snapshot) || snapshot.length === 0) return null;
3615
+ const rows = session.modelRows;
3616
+ const kept = rows.filter((row) => !isUneditedAutoFallbackRow(row));
3617
+ return kept.length === rows.length ? null : kept;
3618
+ };
3549
3619
  react.useEffect(() => {
3550
3620
  if (!open || row.declared === true || namespace?.ns === "llm-deepseek") return undefined;
3551
3621
  let active = true;
3622
+ // 每一轮自动探测都重置快照:新的自动结果必须重新记录,否则手动清理会拿旧快照做判定。
3623
+ discoveryIntent.autoFallback = null;
3624
+ // 手动意图只屏蔽"当前打开周期":走到这里说明已通过守卫、确定要发起一次全新的自动探测,
3625
+ // 所以清掉上一周期遗留的手动标记,卡片折叠后再展开才能恢复自动目录增强。
3626
+ // 同一打开周期内晚于手动点击返回的自动请求,仍会观察到 true 并放弃写草稿。
3627
+ discoveryIntent.manualIntent = false;
3552
3628
  void host.operations.discoverModels(namespace.ns, { provider: row.provider }).then((answer) => {
3629
+ // 手动意图优先:用户已经点过「获取模型」时,这次自动结果不得写入草稿,
3630
+ // 由手动候选的选择流程独占草稿的模型目录。
3631
+ if (discoveryIntent.manualIntent) return;
3553
3632
  if (!active || answer.kind !== "found" || answer.models.length === 0 || session.modelRows.length > 0) return;
3554
3633
  session.setFallbackModels(answer.models);
3634
+ noteAutoFallback(session.modelRows);
3555
3635
  setDraftRevision((value) => value + 1);
3556
3636
  }).catch(() => {
3557
3637
  // 自动目录探测是增强能力,失败时保留 settings 中的模型与手工编辑入口。
@@ -3559,14 +3639,49 @@ window.__ModuleLoader__.load({
3559
3639
  return () => { active = false; };
3560
3640
  }, [host, namespace?.ns, open, row.declared, row.provider, session]);
3561
3641
 
3642
+ const toggleCandidate = (id) => {
3643
+ setFetchState((previous) => previous === null || !Array.isArray(previous.candidates)
3644
+ ? previous
3645
+ : {
3646
+ ...previous,
3647
+ candidates: previous.candidates.map((candidate) => candidate.id === id
3648
+ ? { ...candidate, picked: candidate.picked !== true }
3649
+ : candidate),
3650
+ });
3651
+ };
3652
+ /**
3653
+ * 弹窗「全选/取消全选」的唯一实现(弹窗按钮文案也走同一个纯函数判定)。
3654
+ *
3655
+ * - 可见项已全选 → 清空**全部**选择(含被搜索隐藏的候选),与 DSH 模型页一致;
3656
+ * - 否则只把可见项追加为已选,隐藏候选保留原选择。
3657
+ */
3658
+ const toggleVisibleCandidates = (ids) => {
3659
+ const wanted = new Set(Array.isArray(ids) ? ids : []);
3660
+ setFetchState((previous) => {
3661
+ if (previous === null || !Array.isArray(previous.candidates)) return previous;
3662
+ if (clearsAllCandidateSelection(previous.candidates, wanted)) {
3663
+ return { ...previous, candidates: previous.candidates.map((candidate) => ({ ...candidate, picked: false })) };
3664
+ }
3665
+ return {
3666
+ ...previous,
3667
+ candidates: previous.candidates.map((candidate) => wanted.has(candidate.id)
3668
+ ? { ...candidate, picked: true }
3669
+ : candidate),
3670
+ };
3671
+ });
3672
+ };
3673
+ const closeModelPicker = () => {
3674
+ setFetchState(null);
3675
+ setCandidateQuery("");
3676
+ };
3562
3677
  const adoptPicked = () => {
3563
- const candidates = fetchState?.candidates ?? [];
3564
- const picked = candidates.filter((candidate) => candidate.picked === true);
3678
+ const picked = (fetchState?.candidates ?? []).filter((candidate) => candidate.picked === true);
3679
+ if (picked.length === 0) return;
3565
3680
  edit((draft) => ({
3566
3681
  ...draft,
3567
- models: config.adoptModels(session.modelRows, picked.length > 0 ? picked : candidates),
3682
+ models: config.adoptModels(session.modelRows, picked),
3568
3683
  }));
3569
- setFetchState(null);
3684
+ closeModelPicker();
3570
3685
  };
3571
3686
 
3572
3687
  if (!config.providerEditable(row, namespace)) return null;
@@ -3586,13 +3701,13 @@ window.__ModuleLoader__.load({
3586
3701
  style: { background: "var(--dsw-alias-state-success-primary,#22c55e)" },
3587
3702
  })
3588
3703
  : null,
3589
- react.createElement("span", { className: "deg_mc_card_actions" }, removeAction,
3704
+ react.createElement("span", { className: "deg_mc_card_actions" },
3590
3705
  react.createElement("button", {
3591
3706
  type: "button",
3592
3707
  className: "deg_btn",
3593
3708
  "aria-label": card.displayName + " 编辑",
3594
3709
  onClick: () => { setOpen(true); setError(null); setSaved(false); setConflict(false); },
3595
- }, "编辑"))),
3710
+ }, "编辑"), removeAction)),
3596
3711
  row.error === undefined ? null : react.createElement("p", { className: "deg_mc_error", role: "alert" }, row.error));
3597
3712
  }
3598
3713
 
@@ -3696,44 +3811,12 @@ window.__ModuleLoader__.load({
3696
3811
  onClick: () => edit((draft) => ({ ...draft, models: config.removeModelRow(session.modelRows, model.index) })),
3697
3812
  }, "✕")), advancedSectionOf(model)));
3698
3813
 
3699
- const candidates = fetchState?.candidates ?? null;
3700
- const fetchList = candidates === null
3701
- ? null
3702
- : react.createElement("div", null,
3703
- react.createElement("p", { className: "deg_mc_hint" }, "勾选要采纳的模型(已存在的行不会被覆盖):"),
3704
- react.createElement("ul", { className: "deg_mc_fetch_list" },
3705
- candidates.map((candidate) => react.createElement("li", {
3706
- key: candidate.id,
3707
- className: "deg_mc_fetch_item",
3708
- }, react.createElement("label", { className: "deg_mc_effort" },
3709
- react.createElement("input", {
3710
- type: "checkbox",
3711
- checked: candidate.picked === true,
3712
- disabled,
3713
- "data-deg-candidate": candidate.id,
3714
- onChange: () => setFetchState((previous) => ({
3715
- ...previous,
3716
- candidates: previous.candidates.map((entry) => (entry.id === candidate.id
3717
- ? { ...entry, picked: entry.picked !== true }
3718
- : entry)),
3719
- })),
3720
- }),
3721
- react.createElement("span", null, candidate.id))))),
3722
- react.createElement("div", { className: "deg_mc_conflict_actions" },
3723
- react.createElement("button", { type: "button", className: "deg_btn deg_btn_primary", disabled, onClick: adoptPicked }, "采纳所选"),
3724
- react.createElement("button", { type: "button", className: "deg_btn", disabled, onClick: () => setFetchState(null) }, "取消")));
3814
+
3725
3815
 
3726
3816
  return react.createElement("li", { className: "deg_mc_card", "data-deg-provider": row.provider },
3727
3817
  react.createElement("div", { className: "deg_mc_card_head" },
3728
3818
  react.createElement("span", { className: "deg_mc_card_name" }, card.displayName),
3729
- card.provider === card.displayName ? null : react.createElement("span", { className: "deg_mc_card_route" }, card.provider),
3730
- react.createElement("span", { className: "deg_mc_card_actions" }, removeAction,
3731
- react.createElement("button", {
3732
- type: "button",
3733
- className: "deg_btn",
3734
- disabled: saving,
3735
- onClick: () => { setOpen(false); cancel(); },
3736
- }, "收起"))),
3819
+ card.provider === card.displayName ? null : react.createElement("span", { className: "deg_mc_card_route" }, card.provider)),
3737
3820
  row.error === undefined ? null : react.createElement("p", { className: "deg_mc_error", role: "alert" }, row.error),
3738
3821
  react.createElement("label", { className: "deg_mc_field" },
3739
3822
  react.createElement("span", { className: "deg_mc_field_label" }, "API Key"),
@@ -3827,8 +3910,8 @@ window.__ModuleLoader__.load({
3827
3910
  }, "添加模型"),
3828
3911
  fetchState?.error === null || fetchState?.error === undefined
3829
3912
  ? null
3830
- : react.createElement("p", { className: "deg_mc_error", role: "alert" }, fetchState.error),
3831
- fetchList),
3913
+ : react.createElement("p", { className: "deg_mc_error", role: "alert" }, fetchState.error)
3914
+ ),
3832
3915
  error === null ? null : react.createElement("p", { className: "deg_mc_error", role: "alert", "data-deg-card-error": row.provider }, error),
3833
3916
  conflict
3834
3917
  ? react.createElement("div", { className: "deg_mc_conflict" },
@@ -3846,23 +3929,118 @@ window.__ModuleLoader__.load({
3846
3929
  : null,
3847
3930
  react.createElement("div", { className: "deg_mc_card_footer" },
3848
3931
  react.createElement("span", { className: "deg_mc_status deg_settings_status", role: "status" }, saved ? "已保存" : ""),
3849
- react.createElement("button", { type: "button", className: "deg_btn", disabled: saving, onClick: cancel }, "取消"),
3932
+ react.createElement("button", { type: "button", className: "deg_btn", disabled: saving, onClick: () => { setOpen(false); cancel(); } }, "取消"),
3850
3933
  react.createElement("button", {
3851
3934
  type: "button",
3852
3935
  className: "deg_btn deg_btn_primary",
3853
3936
  disabled,
3854
3937
  "data-deg-apply": row.provider,
3855
3938
  onClick: () => { void apply(); },
3856
- }, saving ? "应用中…" : "应用")));
3939
+ }, saving ? "应用中…" : "应用")),
3940
+ fetchState !== null && Array.isArray(fetchState.candidates)
3941
+ ? react.createElement(ModelDiscoveryDialog, {
3942
+ candidates: fetchState.candidates,
3943
+ query: candidateQuery,
3944
+ onQueryChange: setCandidateQuery,
3945
+ onToggle: toggleCandidate,
3946
+ onToggleVisible: toggleVisibleCandidates,
3947
+ onClose: closeModelPicker,
3948
+ onAdopt: adoptPicked,
3949
+ })
3950
+ : null);
3857
3951
  }
3952
+ /**
3953
+ * 弹窗标题 id 的自增序号。
3954
+ *
3955
+ * 同一个设置页可以同时挂载多个候选弹窗(例如两张 provider 卡片各自探测),
3956
+ * 每个实例的 `aria-labelledby` 必须指向**自己**的标题;共用一个字面量 id 会让
3957
+ * 后挂载的弹窗把无障碍名称解析到别人的标题上。
3958
+ *
3959
+ * 用模块级计数器 + 组件内惰性初始化,而不是 `react.useId`:本插件的 react 面
3960
+ * 在嵌入宿主与测试替身里都可能缺少 `useId`,计数器只依赖 useState/useRef 这类
3961
+ * 一直存在的 API。
3962
+ */
3963
+ let modelDiscoveryTitleSeq = 0;
3964
+
3965
+ /**
3966
+ * 弹窗焦点环的候选控件:只保留真正可聚焦(启用且可见)的对话框控件。
3967
+ *
3968
+ * 判定只用声明式属性,不做布局测量(offsetParent 之类在嵌入宿主里不可靠):
3969
+ * - `disabled` / `aria-disabled="true"` 的控件浏览器会忽略 focus 调用;
3970
+ * - `hidden` / `aria-hidden="true"` 的控件不参与 Tab 顺序。
3971
+ * 两者都必须从焦点环里剔除——否则 handler 先 preventDefault 再 focus 一个被忽略的
3972
+ * 元素,焦点会原地卡死或漏到背景页面。
3973
+ *
3974
+ * @param {unknown} scope 弹窗根节点(任何提供 querySelectorAll 的元素)
3975
+ * @returns {Array<{focus?: Function}>} 按 DOM 顺序排列的可聚焦控件
3976
+ */
3977
+ function focusableDialogControls(scope) {
3978
+ const nodes = Array.from(scope?.querySelectorAll?.("[data-deg-dialog-action]") ?? []);
3979
+ return nodes.filter((node) => {
3980
+ if (node === null || typeof node !== "object") return false;
3981
+ if (node.disabled === true) return false;
3982
+ if (node.hidden === true) return false;
3983
+ if (typeof node.getAttribute !== "function") return true;
3984
+ if (node.getAttribute("aria-disabled") === "true") return false;
3985
+ if (node.getAttribute("aria-hidden") === "true") return false;
3986
+ return true;
3987
+ });
3988
+ }
3989
+
3990
+ /**
3991
+ * 候选弹窗「全选/取消全选」的纯函数语义(与 DSH 模型页的可见项切换一致)。
3992
+ *
3993
+ * 关键边界(不是"只动可见项"那么简单):
3994
+ * - 当前可见候选**已全部勾选**时,切换等价于清空**全部**选择(含被搜索隐藏的候选);
3995
+ * - 否则只把当前可见候选**追加**为已选,隐藏候选保留原有选择。
3996
+ *
3997
+ * 弹窗按钮文案与父卡片的实际改写都取自这一个判定,避免两处各自推断出不同语义。
3998
+ *
3999
+ * @param {Array<{id?: unknown, picked?: unknown}>} candidates 全部候选
4000
+ * @param {Set<unknown>} visibleIds 当前可见候选 id
4001
+ * @returns {boolean} true 表示"清空全部选择",false 表示"只追加可见项"
4002
+ */
4003
+ function clearsAllCandidateSelection(candidates, visibleIds) {
4004
+ const list = Array.isArray(candidates) ? candidates : [];
4005
+ const wanted = visibleIds instanceof Set ? visibleIds : new Set();
4006
+ const visible = list.filter((candidate) => wanted.has(candidate?.id));
4007
+ return visible.length > 0 && visible.every((candidate) => candidate.picked === true);
4008
+ }
4009
+
3858
4010
  /**
3859
4011
  * 自定义 provider 的可用模型选择弹窗:候选只进入当前草稿,不在探测成功时自动写 settings。
3860
4012
  *
3861
4013
  * 交互与 DSH 原生模型页保持一致:搜索当前候选、当前结果全选/取消全选、逐项勾选,
3862
4014
  * 最后由“添加所选”调用父卡片的 adoptModels 合并。弹窗关闭不会丢弃卡片里已有模型。
4015
+ *
4016
+ * 模态契约(与 DSH 内置 picker 的 Modal/useModalLayer 对齐):
4017
+ * - 关闭按钮、点击遮罩、Escape 三条路径都只**关闭**弹窗(走 onClose),不改草稿、不采纳;
4018
+ * - 弹窗内 Tab / Shift+Tab 只在**真正可聚焦**(启用且可见)的控件之间循环,不把焦点
4019
+ * 漏到背景页面,也不会把焦点交给 disabled 的「添加所选」或空结果时的「全选」;
4020
+ * - 关闭(卸载)后把焦点还给打开弹窗前的那个元素;
4021
+ * - 标题 id 每个实例唯一,多个弹窗同时挂载时各自指向自己的标题。
4022
+ *
4023
+ * 焦点相关调用全部做能力探测:嵌入宿主(或测试替身)没有 document/focus 时静默跳过,
4024
+ * 绝不因为焦点不可用而让弹窗不可关闭。
3863
4025
  */
3864
4026
  function ModelDiscoveryDialog({ candidates, query, onQueryChange, onToggle, onToggleVisible, onClose, onAdopt }) {
3865
4027
  const source = Array.isArray(candidates) ? candidates : [];
4028
+ // 标题 id 每个实例唯一(惰性初始化只在首次渲染执行一次),dialog 的 aria-labelledby
4029
+ // 因此始终指向本实例自己的 h3。
4030
+ const [titleId] = react.useState(() => {
4031
+ modelDiscoveryTitleSeq += 1;
4032
+ return "deg-model-discovery-title-" + String(modelDiscoveryTitleSeq);
4033
+ });
4034
+ const dialogRef = react.useRef(null);
4035
+ // 打开前的焦点归属:只在挂载时读一次,卸载时原样还回去。
4036
+ const restoreTargetRef = react.useRef(typeof document === "undefined" ? null : document.activeElement ?? null);
4037
+ react.useEffect(() => () => {
4038
+ // 关闭(卸载)时恢复先前焦点;元素可能已从页面移除,因此 focus 缺失时直接跳过。
4039
+ const target = restoreTargetRef.current;
4040
+ try {
4041
+ if (target !== null && typeof target?.focus === "function") target.focus();
4042
+ } catch { /* 焦点不可用时保持弹窗关闭语义,不抛出。 */ }
4043
+ }, []);
3866
4044
  const normalizedQuery = typeof query === "string" ? query.trim().toLowerCase() : "";
3867
4045
  const visible = normalizedQuery.length === 0
3868
4046
  ? source
@@ -3871,7 +4049,32 @@ window.__ModuleLoader__.load({
3871
4049
  const name = typeof candidate?.name === "string" ? candidate.name : "";
3872
4050
  return id.toLowerCase().includes(normalizedQuery) || name.toLowerCase().includes(normalizedQuery);
3873
4051
  });
3874
- const allVisiblePicked = visible.length > 0 && visible.every((candidate) => candidate.picked === true);
4052
+ const visibleIds = new Set(visible.map((candidate) => candidate?.id));
4053
+ // 按钮文案与父卡片的改写必须同源:这里只用纯函数判定"这次切换是否清空全部选择"。
4054
+ const clearsAll = clearsAllCandidateSelection(source, visibleIds);
4055
+ const pickedCount = source.filter((candidate) => candidate.picked === true).length;
4056
+ const handleKeyDown = (event) => {
4057
+ if (event?.key === "Escape") {
4058
+ // 关闭是模态层的独占语义:阻止默认行为并停止冒泡,避免外层再处理同一个 Escape。
4059
+ event.preventDefault?.();
4060
+ event.stopPropagation?.();
4061
+ onClose();
4062
+ return;
4063
+ }
4064
+ if (event?.key !== "Tab") return;
4065
+ // 焦点环:与提示词弹窗同一套 data 标记与循环算法(首尾相接,Shift+Tab 反向),
4066
+ // 但候选集只取**真正可聚焦**的控件:disabled 的「添加所选」与无结果时的「全选」
4067
+ // 必须跳过,否则 preventDefault 之后 focus 一个被浏览器忽略的元素会让焦点卡死。
4068
+ const controls = focusableDialogControls(dialogRef.current);
4069
+ // 没有任何可聚焦控件时不拦截 Tab:宁可让焦点按浏览器默认行为走,也不把焦点锁死。
4070
+ if (controls.length === 0) return;
4071
+ const current = controls.indexOf(typeof document === "undefined" ? null : document.activeElement);
4072
+ const nextIndex = event.shiftKey
4073
+ ? (current <= 0 ? controls.length - 1 : current - 1)
4074
+ : (current >= controls.length - 1 ? 0 : current + 1);
4075
+ event.preventDefault?.();
4076
+ try { controls[nextIndex]?.focus?.(); } catch { /* 无 DOM 焦点时保持可点击控件。 */ }
4077
+ };
3875
4078
  const list = visible.length === 0
3876
4079
  ? react.createElement("p", { className: "deg_mc_candidate_empty", role: "status" }, "没有匹配的模型")
3877
4080
  : react.createElement("ul", { className: "deg_mc_candidate_list" }, visible.map((candidate) => (
@@ -3881,23 +4084,34 @@ window.__ModuleLoader__.load({
3881
4084
  type: "checkbox",
3882
4085
  checked: candidate.picked === true,
3883
4086
  "data-deg-create-candidate": candidate.id,
4087
+ "data-deg-dialog-action": "candidate",
3884
4088
  "aria-label": "选择模型 " + candidate.id,
3885
4089
  onChange: () => onToggle(candidate.id),
3886
4090
  }),
3887
4091
  react.createElement("span", { className: "deg_mc_candidate_id" }, candidate.id))))));
3888
- return react.createElement("div", { className: "deg_mc_modal_backdrop" },
4092
+ return react.createElement("div", {
4093
+ className: "deg_mc_modal_backdrop",
4094
+ // 点遮罩只关闭:候选状态与草稿都不受影响(采纳只能由“添加所选”触发)。
4095
+ onClick: () => onClose(),
4096
+ },
3889
4097
  react.createElement("div", {
3890
4098
  className: "deg_mc_modal",
3891
4099
  role: "dialog",
3892
4100
  "aria-modal": "true",
3893
- "aria-labelledby": "deg-model-discovery-title",
4101
+ "aria-labelledby": titleId,
4102
+ ref: dialogRef,
4103
+ onKeyDown: handleKeyDown,
4104
+ // 点弹窗内部不应冒泡到遮罩,避免误关。
4105
+ onClick: (event) => event?.stopPropagation?.(),
3894
4106
  },
3895
4107
  react.createElement("div", { className: "deg_mc_modal_header" },
3896
- react.createElement("h3", { className: "deg_mc_modal_title", id: "deg-model-discovery-title" }, "选择要添加的模型"),
4108
+ react.createElement("h3", { className: "deg_mc_modal_title", id: titleId }, "选择要添加的模型"),
3897
4109
  react.createElement("button", {
3898
4110
  type: "button",
3899
4111
  className: "deg_mc_modal_close",
3900
4112
  "aria-label": "关闭",
4113
+ "data-deg-dialog-action": "close",
4114
+ autoFocus: true,
3901
4115
  onClick: onClose,
3902
4116
  }, "×")),
3903
4117
  react.createElement("p", { className: "deg_mc_modal_description" }, "以下是模型提供方的可用模型,勾选要添加的模型。"),
@@ -3908,18 +4122,31 @@ window.__ModuleLoader__.load({
3908
4122
  value: query,
3909
4123
  placeholder: "搜索模型",
3910
4124
  "aria-label": "搜索模型",
4125
+ "data-deg-dialog-action": "search",
3911
4126
  onChange: (event) => onQueryChange(event.target.value),
3912
4127
  }),
3913
4128
  react.createElement("button", {
3914
4129
  type: "button",
3915
4130
  className: "deg_btn",
3916
4131
  disabled: visible.length === 0,
4132
+ "data-deg-dialog-action": "toggle-visible",
3917
4133
  onClick: () => onToggleVisible(visible.map((candidate) => candidate.id)),
3918
- }, allVisiblePicked ? "取消全选" : "全选")),
4134
+ }, clearsAll ? "取消全选" : "全选")),
3919
4135
  list,
3920
4136
  react.createElement("div", { className: "deg_mc_modal_footer" },
3921
- react.createElement("button", { type: "button", className: "deg_btn", onClick: onClose }, "取消"),
3922
- react.createElement("button", { type: "button", className: "deg_btn deg_btn_primary", onClick: onAdopt }, "添加所选"))));
4137
+ react.createElement("button", {
4138
+ type: "button",
4139
+ className: "deg_btn",
4140
+ "data-deg-dialog-action": "cancel",
4141
+ onClick: onClose,
4142
+ }, "取消"),
4143
+ react.createElement("button", {
4144
+ type: "button",
4145
+ className: "deg_btn deg_btn_primary",
4146
+ disabled: pickedCount === 0,
4147
+ "data-deg-dialog-action": "adopt",
4148
+ onClick: onAdopt,
4149
+ }, "添加所选"))));
3923
4150
  }
3924
4151
 
3925
4152
  /**
@@ -3965,7 +4192,11 @@ window.__ModuleLoader__.load({
3965
4192
  const disabled = card.readOnly || saving;
3966
4193
  // 已提交后只允许重试 API Key:route 与凭据引用已经落盘,继续编辑会导致
3967
4194
  // "旧 profile + 新 route 的凭据"错配。其余字段与模型编辑统一禁用。
3968
- const profileDisabled = card.profileLocked || saving;
4195
+ //
4196
+ // fetching 一并锁定 profile:候选是用**点击那一刻**的 endpoint 参数探测出来的,
4197
+ // 请求在途时改 Base URL / 协议 / 模型行(或收起卡片丢弃草稿),就会让旧参数的
4198
+ // 结果被采纳进新配置。已有 provider 卡片用 saving 覆盖同一段窗口,这里保持同语义。
4199
+ const profileDisabled = card.profileLocked || saving || fetching;
3969
4200
  // 只同步已占用 route 词表;revision 由状态机一次性固定(rebase 内部只在 undefined 时初始化)。
3970
4201
  react.useEffect(() => {
3971
4202
  draft.rebase({
@@ -4054,16 +4285,23 @@ window.__ModuleLoader__.load({
4054
4285
  : candidate),
4055
4286
  });
4056
4287
  };
4288
+ /**
4289
+ * 弹窗「全选/取消全选」的唯一实现(弹窗按钮文案也走同一个纯函数判定)。
4290
+ *
4291
+ * - 可见项已全选 → 清空**全部**选择(含被搜索隐藏的候选),与 DSH 模型页一致;
4292
+ * - 否则只把可见项追加为已选,隐藏候选保留原选择。
4293
+ */
4057
4294
  const toggleVisibleCandidates = (ids) => {
4058
4295
  const wanted = new Set(Array.isArray(ids) ? ids : []);
4059
4296
  setFetchState((previous) => {
4060
4297
  if (previous === null || !Array.isArray(previous.candidates)) return previous;
4061
- const visible = previous.candidates.filter((candidate) => wanted.has(candidate.id));
4062
- const allPicked = visible.length > 0 && visible.every((candidate) => candidate.picked === true);
4298
+ if (clearsAllCandidateSelection(previous.candidates, wanted)) {
4299
+ return { ...previous, candidates: previous.candidates.map((candidate) => ({ ...candidate, picked: false })) };
4300
+ }
4063
4301
  return {
4064
4302
  ...previous,
4065
4303
  candidates: previous.candidates.map((candidate) => wanted.has(candidate.id)
4066
- ? { ...candidate, picked: !allPicked }
4304
+ ? { ...candidate, picked: true }
4067
4305
  : candidate),
4068
4306
  };
4069
4307
  });
@@ -4368,7 +4606,9 @@ window.__ModuleLoader__.load({
4368
4606
  react.createElement("button", {
4369
4607
  type: "button",
4370
4608
  className: "deg_btn",
4371
- disabled: saving,
4609
+ // 发现请求在途时同样禁止取消:取消会丢弃草稿并收起卡片,而响应仍会回来,
4610
+ // 与 profile 锁定一起构成"在途期间不改变 endpoint 与草稿归属"的同一条边界。
4611
+ disabled: saving || fetching,
4372
4612
  "data-deg-create-cancel": "true",
4373
4613
  onClick: () => { onClose(draft.settingsCommitted); },
4374
4614
  }, "取消"),