dsh-workspace-api-key 0.2.1 → 0.2.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/README.md CHANGED
@@ -31,12 +31,12 @@ Host half (`lib/index.js`):
31
31
  - matches that directory against `workspaceRegistry.list()` after normalizing both sides (absolute, trailing separator removed, lower-cased on Windows);
32
32
  - derives a stable ref per level — `DEEPSEEK_API_KEY_WS_<first 16 hex chars of sha1(normalized path), upper-cased>` for a workspace and `DEEPSEEK_API_KEY_SS_<first 16 hex chars of sha1(session id), upper-cased>` for a session (a session id contains `-` and cannot be a ref name) — writes the key through the credentials service, and serves that ref's value;
33
33
  - rewrites only refs a provider actually reads: the default ref, every adapter's configured `apiKeyEnv`, and its own generated refs. Every other ref passes through untouched;
34
- - builds the session list from the `sessionController` service (`list()`), so the panel shows the same titles and model projections the sidebar does, and degrades to per-workspace only when that service is absent;
35
- - reports its own version and a session probe (`available`, `count`, `withoutCwd`, `samples`, `error`, plus sample workspace paths) in `/wsk-api/state`, so the panel can say *why* no sessions are listed: a stale host half (only a page reload instead of an app restart), a missing `sessionController`, a throwing `list()`, or a `cwd` that matches no workspace path;
34
+ - builds the session list from the `sessionController` service (`list()`, unwrapping its `{items: […]}` envelope) and falls back to the live `sessions` service (`ctx.get('sessions').list()`), so the panel still has sessions when the controller is missing, throws, or returns an empty list — subagent sessions are filtered out of that fallback;
35
+ - reports its own version and a session probe (`available`, `shape`, `source`, `count`, `withoutCwd`, `samples`, `error`, plus sample workspace paths) in `/wsk-api/state`, so the panel can say *why* no sessions are listed: a stale host half (only a page reload instead of an app restart), a missing `sessionController`, a throwing `list()`, an unexpected envelope shape, or a `cwd` that matches no workspace path;
36
36
  - keeps its own bookkeeping (which level maps to which ref, invalid flags, the provider a key was recorded for) in `$DSH_HOME/storages/dsh-workspace-api-key.json`;
37
37
  - observes failures through the `agent/request-error` waterfall without ever returning `{kind: 'retry'}` — it only records the verdict and calls `next()`.
38
38
 
39
- The browser half (`lib/client.js`) registers into the `sidebar.panellist` slot (`order: 1`), the `main` slot (key `workspace-api-key`) and the `shell.overlay` slot (the entering-a-session warning), the same slots `dsh-skill-mcp-panel` and `dsh-429-guard` use. It is plain `React.createElement` with no build step and no bundled dependencies. The overlay asks `/wsk-api/check` about the session that currently owns the main view, asks at most once per session, and its buttons only ever navigate to the panel — the panel is where a key can actually be changed.
39
+ The browser half (`lib/client.js`) registers into the `sidebar.panellist` slot (`order: 1`), the `main` slot (key `workspace-api-key`) and the `shell.overlay` slot (the entering-a-session warning), the same slots `dsh-skill-mcp-panel` and `dsh-429-guard` use. It is plain `React.createElement` with no build step and no bundled dependencies. It merges the host rows with the browser-side `sessions` store (`ctx.get('sessions').list.getSnapshot()`, which is what the sidebar itself renders), so session titles come from `displayTitle` and sessions stay visible even when the host cannot list them. The overlay asks `/wsk-api/check?sessionId=…&path=…` about the session that currently owns the main view, asks at most once per session, and its buttons only ever navigate to the panel — the panel is where a key can actually be changed.
40
40
 
41
41
  ### HTTP API (loopback only)
42
42
 
@@ -71,22 +71,22 @@ Then add `"dsh-workspace-api-key"` to `dsh.profile.bundles` in that profile's `p
71
71
  ## Tests
72
72
 
73
73
  ```sh
74
- node test/host.test.mjs # 111 assertions
75
- node test/client.test.mjs # 106 assertions
74
+ node test/host.test.mjs # 125 assertions
75
+ node test/client.test.mjs # 121 assertions
76
76
  ```
77
77
 
78
- Neither suite needs a running DSH process. The host suite drives `apply()` against a mock context (fake `credentials`, `workspaceRegistry`, `agents`, `sessionController`, `webServer`, stubbed `fetch`) and covers ref derivation, the session → workspace → default cascade, workspace matching, redirecting a second adapter's `apiKeyEnv`, clearing back to the default, 401/402/429/5xx classification, the loopback and origin checks, and state persistence. The client suite loads `lib/client.js` through a minimal `window.__ModuleLoader__` plus a small React runtime, renders the panel and the overlay, and asserts slot registration, the first render, session expand/collapse, per-session saving and testing, the ungrouped-directory card, the stale and mismatch prompts, the "go configure" hand-off, graceful degradation without a session list, and back-navigation when `ctx.get('layout')` only becomes available after the plugin was applied.
78
+ Neither suite needs a running DSH process. The host suite drives `apply()` against a mock context (fake `credentials`, `workspaceRegistry`, `agents`, `sessionController`, `sessions`, `webServer`, stubbed `fetch`) and covers ref derivation, the session → workspace → default cascade, workspace matching, redirecting a second adapter's `apiKeyEnv`, clearing back to the default, 401/402/429/5xx classification, the loopback and origin checks, state persistence, and the session probe including the `{items}` envelope and the live-session fallback. The client suite loads `lib/client.js` through a minimal `window.__ModuleLoader__` plus a small React runtime, renders the panel and the overlay, and asserts slot registration, the first render, session expand/collapse, per-session saving and testing, sessions recovered from the browser-side store when the host has none (with `displayTitle` winning and subagent sessions hidden), the ungrouped-directory card, the stale and mismatch prompts, the "go configure" hand-off, graceful degradation without a session list, and back-navigation when `ctx.get('layout')` only becomes available after the plugin was applied.
79
79
 
80
80
  ## Requirements and limits
81
81
 
82
82
  - Verified against DSH Desktop 2.0.17 with `@deepseek-ai/dsh*` 0.2.0-rc.2 and `@deepseek-ai/cordis` 4.0.4. It has no runtime dependency and imports no `@deepseek-ai/*` package — only the host services (`credentials`, `workspaceRegistry`, `agents`, `sessionController`, `webServer`, `llm`, `settings`) and Node built-ins.
83
83
  - One key per level: a session has a single key whichever provider route reads it, and a key recorded for one provider is reported as a mismatch when the session runs on another. Model changes inside one provider are not treated as a mismatch, because a DeepSeek key is account-wide.
84
- - Sessions are matched to a workspace by their directory (`cwd`). Sessions that never stored a `cwd` are not listed by the host's own `list()`, so they appear in no row.
84
+ - Sessions are matched to a workspace by their directory (`cwd`). The host's own `list()` skips stored sessions that never got a `cwd`, and the live-session fallback only knows sessions that are currently loaded, so the panel may show fewer sessions than the sidebar. Sessions visible only in the browser-side store carry no `cwd` of their own beyond the one the sidebar knows.
85
85
  - The connection test speaks the Anthropic Messages shape (`POST <baseURL>/messages` with `x-api-key` and `anthropic-version: 2023-06-01`), which is what the DeepSeek route uses. A provider that speaks a different protocol can still have keys bound per level, but the test may report a failure for a key that works.
86
86
  - Keys are stored through the credentials service in `$DSH_HOME/.credentials.yaml` under `refs:`, in plaintext, exactly like the default key. They are not written into session logs, the plugin's state file, or the plugin directory.
87
87
  - Matching is textual after normalization. If a session's `cwd` is a symlink or junction to a registered workspace path, it will not match and the level above is used.
88
88
  - Uninstalling the plugin leaves the generated `*_WS_*` / `*_SS_*` refs in the credentials file; remove them from the Models page.
89
- - If the panel lists workspaces but no expandable sessions, read the diagnostics line at the bottom of the panel. A red "the host half is still an older version" banner means the DSH process was never restarted: the host half is loaded once at startup, so closing the window or reloading the page is not enough — fully quit DSH Desktop and open it again. Otherwise the diagnostics line shows how many sessions the host sees and, when none of them match, sample session `cwd` values next to sample workspace paths.
89
+ - If the panel lists workspaces but no expandable sessions, read the diagnostics line at the bottom of the panel. A red "the host half is still an older version" banner means the DSH process was never restarted: the host half is loaded once at startup, so closing the window or reloading the page is not enough — fully quit DSH Desktop and open it again. Otherwise the diagnostics line shows how many sessions the host sees (and from which source: `sessionController`, its live-session fallback, or the browser store), plus sample session `cwd` values next to sample workspace paths when none of them match.
90
90
 
91
91
  ## License
92
92
 
package/README.zh.md CHANGED
@@ -31,12 +31,12 @@ DSH 只从一个全局凭据 ref(默认 `DEEPSEEK_API_KEY`)解析 DeepSeek k
31
31
  - 把该目录与 `workspaceRegistry.list()` 做规范化后比对(绝对化、去掉尾部分隔符、Windows 折大小写);
32
32
  - 由每一层派生出稳定的 ref —— 工作区是 `DEEPSEEK_API_KEY_WS_<sha1(规范化路径) 前 16 位十六进制大写>`,会话是 `DEEPSEEK_API_KEY_SS_<sha1(会话 id) 前 16 位十六进制大写>`(会话 id 含 `-`,不能直接当 ref 名)——通过凭据服务写入 key,并返回这个 ref 的值;
33
33
  - 只改写 provider 真正会读的 ref:默认 ref、每个适配器配置的 `apiKeyEnv`,以及插件自己生成的 ref;其它 ref 原样透传;
34
- - 会话列表来自 `sessionController` 服务(`list()`),所以面板显示的标题与模型投影和侧栏一致;该服务缺失时自动降级为只能按工作区配置;
35
- - `/wsk-api/state` 会带上插件自身版本与会话探针(`available`、`count`、`withoutCwd`、`samples`、`error`,以及工作区路径样本),面板据此说明**为什么**没有列出会话:宿主半边是旧版本(只刷新了页面而没重启应用)、没有 `sessionController`、`list()` 抛错,还是会话的 `cwd` 与任何工作区路径都对不上;
34
+ - 会话列表来自 `sessionController` 服务(`list()`,会拆开它 `{items: […]}` 的信封),控件缺失 / 抛错 / 列表为空时再退到宿主 live 会话服务(`ctx.get('sessions').list()`),所以那种情况下面板仍能列出会话(该兜底会过滤掉子代理会话);
35
+ - `/wsk-api/state` 会带上插件自身版本与会话探针(`available`、`shape`、`source`、`count`、`withoutCwd`、`samples`、`error`,以及工作区路径样本),面板据此说明**为什么**没有列出会话:宿主半边是旧版本(只刷新了页面而没重启应用)、没有 `sessionController`、`list()` 抛错、信封形状不认识,还是会话的 `cwd` 与任何工作区路径都对不上;
36
36
  - 自己的账本(哪一层对应哪个 ref、失效标记、key 是给哪个服务商记的)落在 `$DSH_HOME/storages/dsh-workspace-api-key.json`;
37
37
  - 通过 `agent/request-error` waterfall 观察失败,但**从不**返回 `{kind: 'retry'}`——只记录判定结果并 `next()` 放行。
38
38
 
39
- 浏览器半边(`lib/client.js`)注册到 `sidebar.panellist` 槽(`order: 1`)、`main` 槽(key 为 `workspace-api-key`)与 `shell.overlay` 槽(点进会话时的提醒),和 `dsh-skill-mcp-panel`、`dsh-429-guard` 用的是同一套槽。全部是朴素的 `React.createElement`,没有构建步骤,也不打包任何依赖。覆盖层向 `/wsk-api/check` 查询当前占据主视图的会话,每个会话最多问一次,它的按钮只负责把你带到面板——改 key 只能由面板里的操作完成。
39
+ 浏览器半边(`lib/client.js`)注册到 `sidebar.panellist` 槽(`order: 1`)、`main` 槽(key 为 `workspace-api-key`)与 `shell.overlay` 槽(点进会话时的提醒),和 `dsh-skill-mcp-panel`、`dsh-429-guard` 用的是同一套槽。全部是朴素的 `React.createElement`,没有构建步骤,也不打包任何依赖。它会把宿主行与浏览器端 `sessions` 存储(`ctx.get('sessions').list.getSnapshot()`,也就是侧栏自己渲染的数据)合并起来,因此标题取自 `displayTitle`,宿主列不出会话时面板也照样有会话可列。覆盖层向 `/wsk-api/check?sessionId=…&path=…` 查询当前占据主视图的会话,每个会话最多问一次,它的按钮只负责把你带到面板——改 key 只能由面板里的操作完成。
40
40
 
41
41
  ### HTTP 接口(仅限回环)
42
42
 
@@ -71,22 +71,22 @@ pnpm add "dsh-workspace-api-key@github:COH2357/dsh-workspace-api-key"
71
71
  ## 测试
72
72
 
73
73
  ```sh
74
- node test/host.test.mjs # 111 条断言
75
- node test/client.test.mjs # 106 条断言
74
+ node test/host.test.mjs # 125 条断言
75
+ node test/client.test.mjs # 121 条断言
76
76
  ```
77
77
 
78
- 两个套件都不需要启动 DSH。宿主套件用一个假 ctx(假的 `credentials`、`workspaceRegistry`、`agents`、`sessionController`、`webServer`,以及打桩的 `fetch`)驱动 `apply()`,覆盖 ref 派生、会话→工作区→默认的级联、工作区匹配、把第二个适配器的 `apiKeyEnv` 一起重定向、清回默认、401/402/429/5xx 的判定、回环与来源校验、状态落盘。浏览器套件通过一个极小的 `window.__ModuleLoader__` 与自制 React 运行时加载 `lib/client.js`,渲染出面板与覆盖层,断言槽注册、首屏渲染、会话折叠/展开、会话级保存与测试、未分组目录卡片、失效与不匹配提醒、「去配置」的交接、没有会话列表时的降级,以及 `ctx.get('layout')` 在插件 apply 之后才可用时返回键仍然有效。
78
+ 两个套件都不需要启动 DSH。宿主套件用一个假 ctx(假的 `credentials`、`workspaceRegistry`、`agents`、`sessionController`、`sessions`、`webServer`,以及打桩的 `fetch`)驱动 `apply()`,覆盖 ref 派生、会话→工作区→默认的级联、工作区匹配、把第二个适配器的 `apiKeyEnv` 一起重定向、清回默认、401/402/429/5xx 的判定、回环与来源校验、状态落盘,以及会话探针(含 `{items}` 信封与 live 兜底)。浏览器套件通过一个极小的 `window.__ModuleLoader__` 与自制 React 运行时加载 `lib/client.js`,渲染出面板与覆盖层,断言槽注册、首屏渲染、会话折叠/展开、会话级保存与测试、宿主列不出会话时从浏览器端存储恢复会话(标题取 `displayTitle`、隐藏子代理会话)、未分组目录卡片、失效与不匹配提醒、「去配置」的交接、没有会话列表时的降级,以及 `ctx.get('layout')` 在插件 apply 之后才可用时返回键仍然有效。
79
79
 
80
80
  ## 要求与限制
81
81
 
82
82
  - 已在 DSH Desktop 2.0.17(`@deepseek-ai/dsh*` 0.2.0-rc.2、`@deepseek-ai/cordis` 4.0.4)上验证。插件没有运行时依赖,也不 import 任何 `@deepseek-ai/*` 包,只用宿主服务(`credentials`、`workspaceRegistry`、`agents`、`sessionController`、`webServer`、`llm`、`settings`)与 Node 内置模块。
83
83
  - 每一层只有一把 key:一个会话无论被哪个 provider 路由读取,用的都是同一把 key;key 记下的服务商与会话当前服务商不一致时会被报为「不匹配」,但同一服务商内换模型不算不匹配,因为 DeepSeek key 是账号级的。
84
- - 会话按目录(`cwd`)归入工作区。从未存过 `cwd` 的会话不会被宿主自己的 `list()` 列出,因此不会出现在任何行里。
84
+ - 会话按目录(`cwd`)归入工作区。宿主自己的 `list()` 会跳过从未存过 `cwd` 的历史会话,而 live 兜底只认识当前装载中的会话,所以面板列出的会话可能比侧栏少;只在浏览器端存储里可见的会话,除了侧栏知道的那个 `cwd` 之外没有别的路径信息。
85
85
  - 「测试连接」说的是 Anthropic Messages 协议(`POST <baseURL>/messages` 带 `x-api-key` 与 `anthropic-version: 2023-06-01`),也就是 DeepSeek 路由用的协议;换了别的协议的 provider 一样可以按层绑 key,只是测试可能把有效的 key 报成失败。
86
86
  - key 通过凭据服务存放在 `$DSH_HOME/.credentials.yaml` 的 `refs:` 里,明文,和默认 key 完全一样。它不会被写进会话日志、插件状态文件或插件目录。
87
87
  - 匹配是规范化之后的文本比较。如果某个会话的 `cwd` 是指向已注册工作区路径的符号链接或 junction,则匹配不上,会走上层层级。
88
88
  - 卸载插件不会删除已生成的 `*_WS_*` / `*_SS_*` ref,可在「设置 → 模型」里清掉。
89
- - 如果面板列出了工作区却没有可展开的会话,请看面板底部的诊断行。出现红色「宿主端插件还是旧版本」横幅,说明 DSH 进程没有重启过:宿主半边只在启动时加载,关窗口和刷新页面都不算——请完全退出 DSH Desktop 再打开。其余情况诊断行会写明宿主看到多少个会话;一个都对不上时,还会并排给出会话 `cwd` 样本与工作区路径样本,便于比对。
89
+ - 如果面板列出了工作区却没有可展开的会话,请看面板底部的诊断行。出现红色「宿主端插件还是旧版本」横幅,说明 DSH 进程没有重启过:宿主半边只在启动时加载,关窗口和刷新页面都不算——请完全退出 DSH Desktop 再打开。其余情况诊断行会写明宿主看到多少个会话、会话列表来自哪个来源(`sessionController`、live 兜底,还是浏览器端存储);一个都对不上时,还会并排给出会话 `cwd` 样本与工作区路径样本,便于比对。
90
90
 
91
91
  ## 许可
92
92
 
package/lib/client.js CHANGED
@@ -20,6 +20,19 @@ window.__ModuleLoader__.load({
20
20
  // refresh —— 面板挂载后在这里登记自己的刷新函数
21
21
  const bridge = { focus: null, refresh: null }
22
22
 
23
+ // apply 阶段拿到的 ctx。会话列表服务(browser 侧 "sessions")由
24
+ // dsh-api-session-controller 的客户端半边在 effect 里 provide,可能晚于
25
+ // 本插件注册,所以只在渲染/点击时惰性取,不在 apply 阶段捕获结果。
26
+ const host = { ctx: null }
27
+ function sessionsService(fallback) {
28
+ if (fallback !== undefined && fallback !== null) return fallback
29
+ try {
30
+ return host.ctx?.get?.('sessions')
31
+ } catch {
32
+ return undefined
33
+ }
34
+ }
35
+
23
36
  // ─────────────────────────────── 样式 ───────────────────────────────
24
37
 
25
38
  const CSS = `
@@ -179,6 +192,153 @@ window.__ModuleLoader__.load({
179
192
  return title ?? (session.blank === true ? '(空会话)' : String(session.sessionId).slice(0, 12))
180
193
  }
181
194
 
195
+ // ─────────────────── 会话树:宿主行 + 浏览器端列表 ───────────────────
196
+ // 宿主 /state 里的会话行只有「带 cwd 的会话」才能被挂到目录下,而且不含标题
197
+ // (标题是浏览器端的 projection)。这里用浏览器端 sessions 服务补齐:
198
+ // · 标题(displayTitle)
199
+ // · 宿主漏掉的会话(例如 cwd 规范化差异导致的挂载失败)
200
+ // 合并后仍然保持宿主的 key 信息(level / configured / invalid / mismatch)。
201
+
202
+ function normKey(p) {
203
+ if (typeof p !== 'string' || p === '') return undefined
204
+ const trimmed = p.replace(/[\\/]+$/, '')
205
+ const base = trimmed === '' ? p : trimmed
206
+ return base.replace(/\//g, '\\').toLowerCase()
207
+ }
208
+
209
+ function dirName(p) {
210
+ if (typeof p !== 'string' || p === '') return ''
211
+ const parts = p.replace(/[\\/]+$/, '').split(/[\\/]/)
212
+ return parts[parts.length - 1] ?? p
213
+ }
214
+
215
+ /** 浏览器端 sessions 服务快照 → 顶层会话条目(子代理会话不算)。 */
216
+ function clientSessionEntries(sessions) {
217
+ const snap = sessions?.list?.getSnapshot?.()
218
+ if (snap === null || snap === undefined) return []
219
+ const ids = Array.isArray(snap.ids) ? snap.ids : Object.keys(snap.byId ?? {})
220
+ const entries = []
221
+ for (const id of ids) {
222
+ const item = snap.byId?.[id]
223
+ if (item === null || item === undefined) continue
224
+ // 子代理会话不作为可配置项列出(它们跟随父会话)。
225
+ if (item.parentId !== undefined || item.origin === 'subagent') continue
226
+ entries.push({
227
+ sessionId: typeof item.id === 'string' && item.id !== '' ? item.id : id,
228
+ cwd: typeof item.cwd === 'string' && item.cwd !== '' ? item.cwd : undefined,
229
+ title: typeof item.displayTitle === 'string' && item.displayTitle !== ''
230
+ ? item.displayTitle
231
+ : typeof item.title === 'string' && item.title !== '' ? item.title : undefined,
232
+ updatedAt: typeof item.updatedAt === 'number' ? item.updatedAt : 0,
233
+ running: item.running === true,
234
+ })
235
+ }
236
+ return entries
237
+ }
238
+
239
+ /**
240
+ * 用浏览器端的会话列表重建「工作区/目录 → 会话」的树。
241
+ * 宿主行(含 key 层级)按 sessionId 合并进来;宿主没给出的会话用
242
+ * /state 里的覆盖记录补出层级,保证按钮不会因为宿主列表为空而消失。
243
+ */
244
+ function mergeSessions(state, sessions) {
245
+ const entries = clientSessionEntries(sessions)
246
+ if (state === null || state === undefined || entries.length === 0) return state
247
+
248
+ const hostRows = new Map()
249
+ for (const group of [...(state.workspaces ?? []), ...(state.ungrouped ?? [])]) {
250
+ for (const row of (Array.isArray(group?.sessions) ? group.sessions : [])) {
251
+ if (row?.sessionId !== undefined) hostRows.set(row.sessionId, row)
252
+ }
253
+ }
254
+ const overrideBySession = new Map()
255
+ for (const item of (Array.isArray(state.sessionOverrides) ? state.sessionOverrides : [])) {
256
+ if (item?.sessionId !== undefined) overrideBySession.set(item.sessionId, item)
257
+ }
258
+ const workspaceByPath = new Map()
259
+ for (const ws of (state.workspaces ?? [])) {
260
+ const key = normKey(ws?.path)
261
+ if (key !== undefined) workspaceByPath.set(key, ws)
262
+ }
263
+
264
+ const buckets = new Map()
265
+ const extras = new Map()
266
+ for (const entry of entries) {
267
+ const host = hostRows.get(entry.sessionId)
268
+ const cwd = typeof host?.cwd === 'string' && host.cwd !== '' ? host.cwd : entry.cwd
269
+ const pathKey = normKey(cwd)
270
+ const ws = pathKey !== undefined ? workspaceByPath.get(pathKey) : undefined
271
+ let row = host
272
+ if (row === undefined) {
273
+ const own = overrideBySession.get(entry.sessionId)
274
+ const level = own !== undefined ? 'session' : ws?.configured === true ? 'workspace' : 'default'
275
+ row = {
276
+ sessionId: entry.sessionId,
277
+ title: entry.title,
278
+ cwd,
279
+ updatedAt: entry.updatedAt,
280
+ running: entry.running,
281
+ blank: false,
282
+ parentSessionId: undefined,
283
+ provider: own?.provider ?? ws?.overrideProvider,
284
+ model: own?.model ?? ws?.overrideModel,
285
+ ref: own?.ref,
286
+ configured: own !== undefined,
287
+ keyMasked: own?.keyMasked,
288
+ overrideProvider: own?.provider,
289
+ overrideModel: own?.model,
290
+ level,
291
+ effectiveRef: level === 'session' ? own?.ref : level === 'workspace' ? ws?.ref : undefined,
292
+ effectiveKeyMasked: level === 'session'
293
+ ? own?.keyMasked
294
+ : level === 'workspace' ? ws?.keyMasked : state.defaultRef?.keyMasked,
295
+ effectiveProvider: own?.provider ?? ws?.overrideProvider,
296
+ effectiveModel: own?.model ?? ws?.overrideModel,
297
+ effectiveTitle: undefined,
298
+ invalid: (level === 'session' ? own?.invalid : level === 'workspace' ? ws?.invalid : null) ?? null,
299
+ mismatch: null,
300
+ }
301
+ } else {
302
+ const patch = {}
303
+ if ((row.title === undefined || row.title === '') && entry.title !== undefined) patch.title = entry.title
304
+ if ((row.cwd === undefined || row.cwd === '') && cwd !== undefined) patch.cwd = cwd
305
+ if (Object.keys(patch).length > 0) row = { ...row, ...patch }
306
+ }
307
+ const bucketKey = pathKey ?? ''
308
+ const target = ws !== undefined ? buckets : extras
309
+ const list = target.get(ws !== undefined ? ws.path : bucketKey) ?? []
310
+ list.push(row)
311
+ target.set(ws !== undefined ? ws.path : bucketKey, list)
312
+ }
313
+
314
+ const sorted = (rows) => rows.slice().sort((a, b) => (b.updatedAt ?? 0) - (a.updatedAt ?? 0))
315
+ const workspaces = (state.workspaces ?? []).map((ws) => (
316
+ { ...ws, sessions: sorted(buckets.get(ws.path) ?? []) }
317
+ ))
318
+ const ungrouped = []
319
+ for (const [key, rows] of extras) {
320
+ const known = (state.ungrouped ?? []).find((group) => normKey(group?.path) === key)
321
+ const path = known?.path ?? rows[0]?.cwd ?? undefined
322
+ ungrouped.push({
323
+ ...(known ?? {
324
+ id: undefined,
325
+ path,
326
+ title: path !== undefined && path !== '' ? dirName(path) : '(没有目录)',
327
+ kind: 'directory',
328
+ configured: false,
329
+ exists: true,
330
+ }),
331
+ sessions: sorted(rows),
332
+ })
333
+ }
334
+ for (const group of (state.ungrouped ?? [])) {
335
+ if (!ungrouped.some((item) => item.path === group.path)) {
336
+ ungrouped.push({ ...group, sessions: sorted(buckets.get(group.path) ?? []) })
337
+ }
338
+ }
339
+ return { ...state, workspaces, ungrouped, sessionSource: 'client' }
340
+ }
341
+
182
342
  function modelText(session) {
183
343
  if (typeof session.provider !== 'string' || session.provider === '') return '模型未知'
184
344
  return `${session.provider} / ${session.model ?? '默认模型'}`
@@ -322,6 +482,16 @@ window.__ModuleLoader__.load({
322
482
 
323
483
  useEffect(() => { refresh() }, [refresh])
324
484
 
485
+ // 浏览器端的会话列表(含标题、cwd)会随时变化:变了就重渲染。
486
+ const sessionStore = sessionsService(props?.sessions)
487
+ const [sessionTick, setSessionTick] = useState(0)
488
+ useEffect(() => {
489
+ const store = sessionStore?.list
490
+ if (store === undefined || store === null || typeof store.subscribe !== 'function') return undefined
491
+ const off = store.subscribe(() => setSessionTick((tick) => tick + 1))
492
+ return () => { if (typeof off === 'function') off() }
493
+ }, [sessionStore])
494
+
325
495
  // 让覆盖层能立刻刷新面板(点「去配置」时面板可能已经挂载着)。
326
496
  useEffect(() => {
327
497
  bridge.refresh = refresh
@@ -418,12 +588,15 @@ window.__ModuleLoader__.load({
418
588
  }
419
589
  }
420
590
 
421
- const workspaces = state?.workspaces ?? []
591
+ // 把浏览器端的会话列表并进宿主状态:宿主列表为空时(例如 list() 信封形状
592
+ // 不匹配)界面仍然能列出会话,并且会话标题来自浏览器端 projection。
593
+ const view = mergeSessions(state, sessionStore)
594
+ const workspaces = view?.workspaces ?? []
422
595
  const grouped = workspaces.filter((ws) => ws.kind === 'workspace')
423
596
  const orphans = workspaces.filter((ws) => ws.kind !== 'workspace')
424
- const ungrouped = state?.ungrouped ?? []
425
- const defaultRef = state?.defaultRef
426
- const providers = state?.providers ?? []
597
+ const ungrouped = view?.ungrouped ?? []
598
+ const defaultRef = view?.defaultRef
599
+ const providers = view?.providers ?? []
427
600
 
428
601
  // ── 诊断 ──────────────────────────────────────────────────────────────
429
602
  // 面板里「看不到可展开的会话」有两个完全不同的原因,修法也不同:
@@ -432,6 +605,7 @@ window.__ModuleLoader__.load({
432
605
  // 这里把两者都显式画出来,用户截一张图就能定位。
433
606
  const probe = state?.sessionProbe
434
607
  const hostIsStale = state !== null && state.storeVersion === undefined
608
+ const clientCount = clientSessionEntries(sessionStore).length
435
609
  const sessionTotal = [...grouped, ...ungrouped].reduce(
436
610
  (sum, ws) => sum + (Array.isArray(ws.sessions) ? ws.sessions.length : 0), 0)
437
611
 
@@ -444,10 +618,13 @@ window.__ModuleLoader__.load({
444
618
  ? '未知'
445
619
  : probe.available !== true
446
620
  ? '不可用'
447
- : `${probe.count} 个`
621
+ : `${probe.count} 个${probe.shape !== undefined ? `(${probe.shape})` : ''}`
622
+ + (probe.source === 'sessions' ? '(live 兜底)' : '')
448
623
  const listError = probe?.error !== undefined ? ` · list 报错:${probe.error}` : ''
449
624
  const noCwd = probe?.withoutCwd > 0 ? ` · ${probe.withoutCwd} 个没有 cwd` : ''
450
- return `插件 v${hostVersion} · 状态存储 v${state.storeVersion ?? '—'} · 会话列表:${sessions}${listError}${noCwd}`
625
+ return `插件 v${hostVersion} · 状态存储 v${state.storeVersion ?? '—'}`
626
+ + ` · 宿主会话列表:${sessions}${listError}${noCwd}`
627
+ + ` · 浏览器会话列表:${clientCount} 个`
451
628
  + ` · 工作区 ${grouped.length} + 未分组 ${ungrouped.length} · 挂在目录下的会话 ${sessionTotal}`
452
629
  }
453
630
 
@@ -519,7 +696,9 @@ window.__ModuleLoader__.load({
519
696
  type: 'button', className: 'wsk-btn',
520
697
  onClick: () => setEditing(null),
521
698
  }, '取消')),
522
- h('div', { className: 'wsk-hint' }, `凭据引用:${target.ref ?? '—'}`))
699
+ h('div', { className: 'wsk-hint' }, target.ref === undefined || target.ref === null
700
+ ? '凭据引用:保存时由宿主按这个会话/目录的 ID 生成,保存后这里会显示出来'
701
+ : `凭据引用:${target.ref}`))
523
702
  }
524
703
 
525
704
  const renderSession = (ws, session) => {
@@ -748,20 +927,20 @@ window.__ModuleLoader__.load({
748
927
  '请「完全退出」DSH Desktop 再重新打开(关闭窗口 / 刷新页面都不够:宿主代码只在启动时加载)。'),
749
928
  h('div', { className: 'wsk-hint' }, diagText()))
750
929
  : null,
751
- state !== null && state.sessionListAvailable === false
930
+ state !== null && state.sessionListAvailable === false && clientCount === 0
752
931
  ? h('div', { className: 'wsk-msg info', style: { marginBottom: 12 } },
753
932
  '当前 DSH 版本没有提供会话列表服务,只能按工作区配置 key。',
754
933
  h('div', { className: 'wsk-hint' }, diagText()))
755
934
  : null,
756
- !hostIsStale && probe?.available === true && probe.count === 0
935
+ !hostIsStale && probe?.available === true && probe.count === 0 && clientCount === 0
757
936
  ? h('div', { className: 'wsk-msg info', style: { marginBottom: 12 } },
758
- '宿主报告 0 个会话,所以没有可展开的列表。',
937
+ '宿主报告 0 个会话,浏览器端也没有拿到会话列表,所以没有可展开的列表。',
759
938
  h('div', { className: 'wsk-hint' },
760
939
  '只有「会话记录里带 cwd」的会话才能挂到工作区/未分组目录下。'),
761
940
  h('div', { className: 'wsk-hint' }, diagText()),
762
941
  diagSamples())
763
942
  : null,
764
- !hostIsStale && (probe?.count ?? 0) > 0 && sessionTotal === 0
943
+ !hostIsStale && sessionTotal === 0 && clientCount === 0 && (probe?.count ?? 0) > 0
765
944
  ? h('div', { className: 'wsk-msg info', style: { marginBottom: 12 } },
766
945
  '拿到会话了,但没有一个能对上工作区/目录(路径不匹配)。',
767
946
  h('div', { className: 'wsk-hint' }, diagText()),
@@ -836,25 +1015,31 @@ window.__ModuleLoader__.load({
836
1015
  if (sessions === undefined || sessions === null) return undefined
837
1016
 
838
1017
  let disposed = false
839
- const currentSessionId = () => {
1018
+ const currentSession = () => {
840
1019
  try {
841
1020
  const snapshot = sessions.list?.getSnapshot?.()
842
1021
  const byId = snapshot?.byId ?? {}
843
1022
  for (const [id, item] of Object.entries(byId)) {
844
- if (Number(item?.retainedBy?.mainView) > 0) return id
1023
+ if (Number(item?.retainedBy?.mainView) > 0) return { id, cwd: item?.cwd }
845
1024
  }
846
1025
  } catch {
847
1026
  /* 快照不可用时静默跳过 */
848
1027
  }
849
1028
  return undefined
850
1029
  }
1030
+ const currentSessionId = () => currentSession()?.id
851
1031
 
852
1032
  const evaluate = async () => {
853
- const sessionId = currentSessionId()
1033
+ const current = currentSession()
1034
+ const sessionId = current?.id
854
1035
  if (sessionId === undefined || asked.current.has(sessionId)) return
855
1036
  asked.current.add(sessionId)
856
1037
  try {
857
- const res = await api(`/check?sessionId=${encodeURIComponent(sessionId)}`)
1038
+ // 一并带上 cwd:宿主拿到路径后,即使会话列表为空也能定位到工作区。
1039
+ const cwd = typeof current?.cwd === 'string' && current.cwd !== '' ? current.cwd : undefined
1040
+ const query = `/check?sessionId=${encodeURIComponent(sessionId)}`
1041
+ + (cwd === undefined ? '' : `&path=${encodeURIComponent(cwd)}`)
1042
+ const res = await api(query)
858
1043
  if (disposed) return
859
1044
  if (res.needsAttention === true) setPrompt(res)
860
1045
  } catch {
@@ -949,6 +1134,7 @@ window.__ModuleLoader__.load({
949
1134
  ensureStyles()
950
1135
  const slots = ctx.get('slots')
951
1136
  if (slots === undefined || slots === null) return
1137
+ host.ctx = ctx
952
1138
 
953
1139
  // 惰性取 layout:点击时才读,避免服务尚未就绪时把 undefined 缓存下来。
954
1140
  const back = () => {
@@ -975,7 +1161,7 @@ window.__ModuleLoader__.load({
975
1161
  slots.register({
976
1162
  name: 'main',
977
1163
  key: PANEL_ID,
978
- inject: () => ({ back }),
1164
+ inject: () => ({ back, sessions: ctx.get('sessions') }),
979
1165
  }, WorkspaceKeyPage)), 'workspace-api-key: main page')
980
1166
 
981
1167
  ctx.effect(() => slots.inject('shell.overlay', () =>
package/lib/index.js CHANGED
@@ -202,9 +202,9 @@ function effectiveOverride(ctx, state, { sessionId, cwd }) {
202
202
  return { level: 'default', ref: undefined, entry: undefined }
203
203
  }
204
204
 
205
- // ───────────────── 会话列表(宿主 sessionController) ─────────────────
205
+ // ───────────────── 会话列表(宿主 sessionController + live 兜底) ─────────────────
206
206
 
207
- /** 宿主是否挂载了会话列表 API(决定界面能否列出会话)。 */
207
+ /** 宿主是否挂载了会话列表 API(仅诊断用;界面看 sessionProbe.available)。 */
208
208
  function sessionListAvailable(ctx) {
209
209
  try {
210
210
  const controller = ctx.get('sessionController')
@@ -221,33 +221,80 @@ function sessionListAvailable(ctx) {
221
221
  */
222
222
  async function sessionProbe(ctx) {
223
223
  let controller
224
+ let controllerError
224
225
  try {
225
226
  controller = ctx.get('sessionController')
226
227
  } catch (err) {
227
- return { available: false, rows: [], count: 0, error: String(err?.message ?? err) }
228
+ controllerError = String(err?.message ?? err)
228
229
  }
229
- if (controller === undefined) return { available: false, rows: [], count: 0 }
230
- if (typeof controller.list !== 'function') {
231
- return { available: false, rows: [], count: 0, error: 'sessionController.list is not a function' }
230
+ if (controller !== undefined && typeof controller.list !== 'function') {
231
+ controllerError = 'sessionController.list is not a function'
232
232
  }
233
- try {
234
- const rows = await controller.list()
235
- const list = Array.isArray(rows) ? rows : []
236
- const samples = []
237
- let withoutCwd = 0
238
- for (const row of list) {
239
- const cwd = typeof row?.cwd === 'string' && row.cwd !== '' ? row.cwd : undefined
240
- if (cwd === undefined) {
241
- withoutCwd += 1
242
- continue
233
+ let list = []
234
+ let shape
235
+ let probeError = controllerError
236
+ if (controller !== undefined && typeof controller.list === 'function') {
237
+ try {
238
+ // 宿主 SessionController.list(request, signal) 返回的是 { items: [...] } 信封,
239
+ // 不是数组(远端 remote.session.list 也是同一形状)。早期版本只认数组,
240
+ // 于是「信封」被当成空列表 —— 界面因此看不到任何会话。
241
+ // 这里同时容忍裸数组与小写 value.items(不同 DSH 版本的形状差异)。
242
+ const raw = await controller.list()
243
+ shape = Array.isArray(raw)
244
+ ? 'array'
245
+ : raw === null || raw === undefined
246
+ ? String(raw)
247
+ : typeof raw === 'object'
248
+ ? 'object'
249
+ : typeof raw
250
+ list = Array.isArray(raw)
251
+ ? raw
252
+ : Array.isArray(raw?.items)
253
+ ? raw.items
254
+ : Array.isArray(raw?.value?.items)
255
+ ? raw.value.items
256
+ : []
257
+ if (list.length === 0 && shape === 'object') {
258
+ probeError = `sessionController.list() 返回了 object(keys: ${Object.keys(raw).slice(0, 8).join(',') || '—'}),既不是数组也没有 items`
243
259
  }
244
- if (samples.length < 5 && !samples.includes(cwd)) samples.push(cwd)
260
+ } catch (err) {
261
+ log('sessionController.list failed:', String(err?.message ?? err))
262
+ probeError = String(err?.message ?? err)
263
+ shape = 'throw'
245
264
  }
246
- log('sessionController.list ok:', list.length, 'rows,', withoutCwd, 'without cwd')
247
- return { available: true, rows: list, count: list.length, withoutCwd, samples }
248
- } catch (err) {
249
- log('sessionController.list failed:', String(err?.message ?? err))
250
- return { available: true, rows: [], count: 0, error: String(err?.message ?? err) }
265
+ }
266
+ // sessionController 给不出任何会话时(服务缺失、list 抛错、列表为空……),
267
+ // 用宿主 live 会话服务兜底,让界面至少能列出「当前装载中的会话」。
268
+ let rows = list
269
+ let source = rows.length > 0 ? 'sessionController' : undefined
270
+ if (rows.length === 0) {
271
+ const live = liveSessionRows(ctx)
272
+ if (live.length > 0) {
273
+ rows = live
274
+ source = 'sessions'
275
+ log('sessionController gave no rows, falling back to sessions service:', live.length, 'rows')
276
+ }
277
+ }
278
+ const samples = []
279
+ let withoutCwd = 0
280
+ for (const row of rows) {
281
+ const cwd = typeof row?.cwd === 'string' && row.cwd !== '' ? row.cwd : undefined
282
+ if (cwd === undefined) {
283
+ withoutCwd += 1
284
+ continue
285
+ }
286
+ if (samples.length < 5 && !samples.includes(cwd)) samples.push(cwd)
287
+ }
288
+ log('sessionProbe:', rows.length, 'rows from', source ?? 'none', ',', withoutCwd, 'without cwd, shape', shape)
289
+ return {
290
+ available: source !== undefined,
291
+ rows,
292
+ count: rows.length,
293
+ withoutCwd,
294
+ samples,
295
+ shape,
296
+ source,
297
+ error: probeError,
251
298
  }
252
299
  }
253
300
 
@@ -281,6 +328,69 @@ function titleOf(row) {
281
328
  return undefined
282
329
  }
283
330
 
331
+ // ───────────────── 兜底:宿主 live 会话服务(ctx.get('sessions')) ─────────────────
332
+ // sessionController.list() 依赖 sessionQuery / 持久化绑定,某些情况下会返回空列表;
333
+ // 而 dsh-session 的 sessions 服务始终有「当前装载中的会话」。用它兜底能让
334
+ // /check(进入会话时的提醒)与 /state 在那种情况下仍然工作。
335
+
336
+ /** 单个 live 会话的 cwd 与当前模型选择。 */
337
+ function liveSessionInfo(ctx, sessionId) {
338
+ if (sessionId === undefined) return undefined
339
+ try {
340
+ const session = ctx.get('sessions')?.get?.(sessionId)
341
+ if (session === undefined || session === null) return undefined
342
+ const cwd = typeof session.header?.cwd === 'string' && session.header.cwd !== ''
343
+ ? session.header.cwd
344
+ : undefined
345
+ let config
346
+ try {
347
+ config = session.requestHeader?.()?.config
348
+ } catch {
349
+ config = undefined
350
+ }
351
+ const provider = typeof config?.provider === 'string' && config.provider !== '' ? config.provider : undefined
352
+ const model = typeof config?.model === 'string' && config.model !== '' ? config.model : undefined
353
+ const selection = provider === undefined && model === undefined ? undefined : { provider, model }
354
+ if (cwd === undefined && selection === undefined) return undefined
355
+ return { cwd, selection }
356
+ } catch {
357
+ return undefined
358
+ }
359
+ }
360
+
361
+ /** 用 live 会话服务凑出与 sessionController 行兼容的摘要(仅在没有其它来源时使用)。 */
362
+ function liveSessionRows(ctx) {
363
+ let sessions = []
364
+ try {
365
+ const service = ctx.get('sessions')
366
+ if (service === undefined || typeof service.list !== 'function') return []
367
+ sessions = service.list() ?? []
368
+ } catch {
369
+ return []
370
+ }
371
+ const rows = []
372
+ for (const session of sessions) {
373
+ const sessionId = session?.id ?? session?.header?.id
374
+ if (typeof sessionId !== 'string' || sessionId === '') continue
375
+ // 子代理会话不作为可配置项列出(与 sessionController 的 remote 列表不同,
376
+ // live 会话服务包含子代理会话)。
377
+ const origin = session?.header?.origin
378
+ if (origin === 'subagent' || session?.header?.parentSession !== undefined) continue
379
+ const info = liveSessionInfo(ctx, sessionId)
380
+ const values = {}
381
+ if (info?.selection !== undefined) values.modelSelection = { ...info.selection }
382
+ rows.push({
383
+ sessionId,
384
+ cwd: info?.cwd,
385
+ updatedAt: 0,
386
+ running: session?.getSnapshot?.()?.running === true,
387
+ blank: false,
388
+ projections: Object.keys(values).length === 0 ? undefined : { values },
389
+ })
390
+ }
391
+ return rows
392
+ }
393
+
284
394
  /** 所有 provider 路由名(用于「这把 key 是给哪个 provider 的」选择项)。 */
285
395
  function providerList(ctx) {
286
396
  const names = new Set(['deepseek-official'])
@@ -1025,6 +1135,8 @@ async function snapshot(ctx, state) {
1025
1135
  withoutCwd: probe.withoutCwd ?? 0,
1026
1136
  error: probe.error,
1027
1137
  samples: probe.samples ?? [],
1138
+ shape: probe.shape,
1139
+ source: probe.source,
1028
1140
  },
1029
1141
  workspacePathSample: workspaces.slice(0, 5).map((ws) => ws.path),
1030
1142
  defaultRef: {
@@ -1039,7 +1151,8 @@ async function snapshot(ctx, state) {
1039
1151
  interceptableRefs: [...interceptableRefs(ctx, state)],
1040
1152
  workspaceCount: workspaceIndex(ctx).size,
1041
1153
  credentialsAvailable: credentials !== undefined,
1042
- sessionListAvailable: sessionListAvailable(ctx),
1154
+ sessionListAvailable: probe.available,
1155
+ sessionControllerAvailable: sessionListAvailable(ctx),
1043
1156
  statePath: stateFilePath(),
1044
1157
  workspaces,
1045
1158
  ungrouped,
@@ -1100,6 +1213,13 @@ async function checkSession(ctx, state, params) {
1100
1213
  title = titleOf(row)
1101
1214
  }
1102
1215
  }
1216
+ // 会话没出现在宿主列表里时(sessionController 空列表、冷会话未装载等),
1217
+ // 用 live 会话服务补 cwd 与模型选择 —— 否则进入会话时的提醒会静默失效。
1218
+ if (sid !== undefined && (cwd === undefined || selection === undefined)) {
1219
+ const live = liveSessionInfo(ctx, sid)
1220
+ if (cwd === undefined && live?.cwd !== undefined) cwd = live.cwd
1221
+ if (selection === undefined && live?.selection !== undefined) selection = live.selection
1222
+ }
1103
1223
  if (cwd === undefined) cwd = activeCwd(ctx)
1104
1224
  const eff = effectiveOverride(ctx, state, { sessionId: sid, cwd })
1105
1225
  const hit = eff.ref !== undefined ? await resolveKey(ctx, eff.ref) : await resolveKey(ctx, 'DEEPSEEK_API_KEY')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-workspace-api-key",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "description": "A DeepSeek Harness plugin that binds a separate DeepSeek API key to each workspace, or to a single session, so the spend of one key stays with the sessions you choose.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -15,6 +15,7 @@
15
15
  "homepage": "https://github.com/COH2357/dsh-workspace-api-key#readme",
16
16
  "keywords": [
17
17
  "dsh",
18
+ "dsh-plugin",
18
19
  "deepseek-harness",
19
20
  "plugin",
20
21
  "api-key",