dsh-model-select-plus 0.1.0 → 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/README.md CHANGED
@@ -27,14 +27,14 @@ npm i dsh-model-select-plus
27
27
  `GET /plugins/dsh-model-select-plus/api/catalog` 与 `POST /plugins/dsh-model-select-plus/api/select`。
28
28
  - Client 侧(exports `./client`)由 `dsh.client` 注入加载,替换 `conversation.input.model` 占位(`priority: -1`)。
29
29
 
30
- 两个 API 都调用宿主的 `ctx.apiProxy.sessions.models / selectModel`——这是运行期 apiproxy 的真实目录与选择处理器,复用 `session.models` / `session.selectModel`,选中后会正确设置会话 Agent 的实时选择引用并保存默认模型。
30
+ 两个 API 都调用宿主的 `ctx.sessionController`——这是 `0.1.2` 里取代已删除 `apiProxy.sessions` 的 Remote 服务,`modelCatalog()` 列出当前可路由的模型目录,`selectModel()` 经 `llm.resolveCallConfig` 校验后设置会话 Agent 的实时选择引用并保存默认模型。
31
31
 
32
32
  ## 数据路径
33
33
 
34
34
  ```
35
35
  浏览器 (client bundle)
36
36
  └─ fetch /plugins/dsh-model-select-plus/api/{catalog,select}
37
- └─ host plugin (ctx.webServer) → ctx.apiProxy.sessions.{models,selectModel}
37
+ └─ host plugin (ctx.webServer) → ctx.sessionController.{modelCatalog,selectModel}
38
38
  ```
39
39
 
40
40
  (持久化插件避免动态插件的 `harness`/`host` 内建,采用与 `plugin-dashboard` 相同的 webserver+fetch 模式。)
package/lib/client.cjs CHANGED
@@ -147,6 +147,9 @@ var module = { exports: {} };
147
147
  setData({ status: "error", groups: [], failures: [], current: null, error: String(err && err.message || err) });
148
148
  });
149
149
  };
150
+ import_react.default.useEffect(() => {
151
+ if (sessionId !== void 0) load();
152
+ }, [sessionId]);
150
153
  const openMenu = () => {
151
154
  setOpen(true);
152
155
  load();
package/lib/host.js CHANGED
@@ -6,21 +6,28 @@
6
6
  * `fetch`. Persistent plugins avoid the dynamic-plugin `harness`/`host`
7
7
  * builtins, so this follows the `plugin-dashboard` pattern (webServer routes).
8
8
  *
9
- * Both handlers call the running harness's own `apiProxy.sessions` API:
9
+ * Both handlers call the running harness's own `sessionController` Remote
10
+ * service (the `0.1.2` replacement for the removed `apiProxy.sessions`):
10
11
  * - GET /plugins/dsh-model-select-plus/api/catalog?sessionId=<id>
11
- * → apiProxy.sessions.models({ rpcId, payload:{ sessionId } })
12
- * → { groups, failures, current, routable }
12
+ * → ctx.sessionController.modelCatalog()
13
+ * → { default, routableProviders, groups, failures }, then folded with the
14
+ * session's durable modelSelection projection to produce the client
15
+ * shape { groups, failures, current, routable }.
13
16
  * - POST /plugins/dsh-model-select-plus/api/select
14
17
  * body { sessionId, provider, model, reasoningEffort? }
15
- * → apiProxy.sessions.selectModel(...) → { ok, selected } | { ok:false, message }
18
+ * → ctx.sessionController.selectModel(...) → { selected } | RemoteError
16
19
  *
17
- * `selectModel` validates via llm.resolveCallConfig, sets the session Agent's
18
- * live selection ref and saves the deployment default — the exact shipped path.
20
+ * `modelCatalog()` describes every currently routable provider/model without
21
+ * requiring a Session; `selectModel()` validates via llm.resolveCallConfig,
22
+ * sets the session Agent's live selection ref and saves the deployment default
23
+ * — the exact shipped path. The per-session `current`/`routable` client fields
24
+ * are derived here from the durable `modelSelection` projection (the client
25
+ * face of the same fold the first-party ModelDirectory reads).
19
26
  *
20
27
  * @param ctx - the mounted host Cordis context.
21
28
  */
22
29
  export const name = "dsh-model-select-plus";
23
- export const inject = ["webServer"];
30
+ export const inject = ["webServer", "sessionController", "sessionProjections", "sessions"];
24
31
 
25
32
  const PREFIX = "/plugins/dsh-model-select-plus/api";
26
33
 
@@ -45,55 +52,85 @@ function readBody(req) {
45
52
  });
46
53
  }
47
54
 
48
- export function apply(ctx) {
49
- const api = () => ctx.get("apiProxy");
55
+ /**
56
+ * Derive the client's per-session `current`/`routable` from the durable
57
+ * modelSelection projection (pending → lastUsed) falling back to the catalog's
58
+ * deployment default. Mirrors the first-party ModelDirectory fold
59
+ * (`current = projected.next ?? catalog.default`) so the client's "current
60
+ * model" trigger reflects the session's actual selection rather than guessing.
61
+ * A session that is not live (cold/persisted) still reports the deployment
62
+ * default so the trigger never shows an empty placeholder when the catalog is
63
+ * ready.
64
+ *
65
+ * @param ctx - host context (sessionProjections, sessions).
66
+ * @param sessionId - the session being previewed.
67
+ * @param catalog - `modelCatalog()` result for this host generation.
68
+ * @returns `{ current, routable }` where current is never null while the catalog is ready.
69
+ */
70
+ function currentSelection(ctx, sessionId, catalog) {
71
+ const session = ctx.sessions.get(sessionId);
72
+ let pending;
73
+ let lastUsed;
74
+ if (session !== undefined) {
75
+ const projections = ctx.sessionProjections.stateOf(session, "modelSelection");
76
+ if (projections !== undefined) {
77
+ pending = projections.pending;
78
+ lastUsed = projections.lastUsed;
79
+ }
80
+ }
81
+ // pending → lastUsed → deployment default (the same precedence the first-party
82
+ // client uses for `projected.next`, falling back to catalog.default).
83
+ const current = pending ?? lastUsed ?? catalog.default;
84
+ const routable = (catalog.routableProviders ?? []).includes(current.provider);
85
+ return { current: { ...current }, routable };
86
+ }
50
87
 
88
+ export function apply(ctx) {
51
89
  const handler = async (req, res) => {
52
90
  try {
53
91
  const url = new URL(req.url ?? "/", "http://localhost");
54
92
  const pathname = url.pathname;
55
- const proxy = api();
56
- if (proxy === undefined || proxy.sessions === undefined) {
57
- json(res, 500, { error: "apiProxy.sessions unavailable" });
58
- return;
59
- }
60
93
 
61
94
  if (req.method === "GET" && pathname === `${PREFIX}/catalog`) {
62
95
  const sessionId = url.searchParams.get("sessionId") ?? "";
63
- const r = await proxy.sessions.models({ rpcId: "mdsl-catalog", payload: { sessionId } });
64
- if (r.result.ok === true) {
65
- const v = r.result.value;
66
- json(res, 200, { groups: v.groups, failures: v.failures, current: v.current, routable: v.routable });
67
- } else {
68
- json(res, 200, { error: (r.result.error && r.result.error.message) || "加载模型目录失败" });
69
- }
96
+ const catalog = await ctx.sessionController.modelCatalog();
97
+ const current = currentSelection(ctx, sessionId, catalog);
98
+ json(res, 200, {
99
+ groups: catalog.groups,
100
+ failures: catalog.failures,
101
+ current: current.current,
102
+ routable: current.routable,
103
+ default: catalog.default,
104
+ routableProviders: catalog.routableProviders,
105
+ });
70
106
  return;
71
107
  }
72
108
 
73
109
  if (req.method === "POST" && pathname === `${PREFIX}/select`) {
74
110
  const body = JSON.parse((await readBody(req)) || "{}");
75
- const r = await proxy.sessions.selectModel({
76
- rpcId: "mdsl-select",
77
- payload: {
78
- sessionId: body.sessionId,
79
- provider: body.provider,
80
- model: body.model,
81
- ...(body.reasoningEffort === undefined || body.reasoningEffort === null
82
- ? {}
83
- : { reasoningEffort: body.reasoningEffort }),
84
- },
111
+ const value = await ctx.sessionController.selectModel({
112
+ sessionId: body.sessionId,
113
+ provider: body.provider,
114
+ model: body.model,
115
+ ...(body.reasoningEffort === undefined || body.reasoningEffort === null
116
+ ? {}
117
+ : { reasoningEffort: body.reasoningEffort }),
85
118
  });
86
- if (r.result.ok === true) {
87
- json(res, 200, { ok: true, selected: r.result.value.selected });
88
- } else {
89
- json(res, 200, { ok: false, message: (r.result.error && r.result.error.message) || "模型不可用" });
90
- }
119
+ json(res, 200, { ok: true, selected: value.selected });
91
120
  return;
92
121
  }
93
122
 
94
123
  json(res, 404, { error: "not found" });
95
124
  } catch (error) {
96
- json(res, 500, { error: error instanceof Error ? error.message : String(error) });
125
+ // selectModel throws a RemoteError on failure (e.g. model-unavailable);
126
+ // report it as a soft 200 `{ ok:false, message }` so the client renders
127
+ // the in-menu error instead of a hard network failure.
128
+ const remote = error && typeof error === "object" ? error : undefined;
129
+ const code = remote && remote.code ? remote.code : undefined;
130
+ const message = remote && remote.message
131
+ ? remote.message
132
+ : (error instanceof Error ? error.message : String(error));
133
+ json(res, 200, { ok: false, message, code });
97
134
  }
98
135
  };
99
136
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-model-select-plus",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "A richer model-selection popup for DeepSeek Harness: provider-grouped models with descriptions, inline reasoning-effort chips, search and pinned favorites — replaces the composer's default ModelSelect.",
5
5
  "type": "module",
6
6
  "main": "lib/host.js",
@@ -59,10 +59,10 @@
59
59
  "esbuild": "^0.25.0"
60
60
  },
61
61
  "peerDependencies": {
62
- "@deepseek-ai/cordis": "^4.0.1",
63
- "@deepseek-ai/dsh-host-webserver": "^0.1.1-rc.2",
64
- "@deepseek-ai/dsh-client-ui-renderer": "^0.1.1-rc.2",
65
- "@deepseek-ai/dsh-client-ui-slots": "^0.1.1-rc.2",
62
+ "@deepseek-ai/cordis": "^4.0.2",
63
+ "@deepseek-ai/dsh-host-webserver": "^0.1.2-alpha.3",
64
+ "@deepseek-ai/dsh-client-ui-renderer": "^0.1.2-alpha.3",
65
+ "@deepseek-ai/dsh-client-ui-slots": "^0.1.2-alpha.3",
66
66
  "react": "^18.3.1"
67
67
  },
68
68
  "peerDependenciesMeta": {
package/src/client.js CHANGED
@@ -137,6 +137,10 @@ function ModelSelectLite(props) {
137
137
  });
138
138
  };
139
139
 
140
+ // Load on mount (and when the session changes) so the trigger shows the
141
+ // current model immediately instead of the "选择模型" placeholder.
142
+ React.useEffect(() => { if (sessionId !== undefined) load(); }, [sessionId]);
143
+
140
144
  const openMenu = () => { setOpen(true); load(); };
141
145
  const closeMenu = () => { setOpen(false); setQuery(""); };
142
146
 
package/src/host.js CHANGED
@@ -6,21 +6,28 @@
6
6
  * `fetch`. Persistent plugins avoid the dynamic-plugin `harness`/`host`
7
7
  * builtins, so this follows the `plugin-dashboard` pattern (webServer routes).
8
8
  *
9
- * Both handlers call the running harness's own `apiProxy.sessions` API:
9
+ * Both handlers call the running harness's own `sessionController` Remote
10
+ * service (the `0.1.2` replacement for the removed `apiProxy.sessions`):
10
11
  * - GET /plugins/dsh-model-select-plus/api/catalog?sessionId=<id>
11
- * → apiProxy.sessions.models({ rpcId, payload:{ sessionId } })
12
- * → { groups, failures, current, routable }
12
+ * → ctx.sessionController.modelCatalog()
13
+ * → { default, routableProviders, groups, failures }, then folded with the
14
+ * session's durable modelSelection projection to produce the client
15
+ * shape { groups, failures, current, routable }.
13
16
  * - POST /plugins/dsh-model-select-plus/api/select
14
17
  * body { sessionId, provider, model, reasoningEffort? }
15
- * → apiProxy.sessions.selectModel(...) → { ok, selected } | { ok:false, message }
18
+ * → ctx.sessionController.selectModel(...) → { selected } | RemoteError
16
19
  *
17
- * `selectModel` validates via llm.resolveCallConfig, sets the session Agent's
18
- * live selection ref and saves the deployment default — the exact shipped path.
20
+ * `modelCatalog()` describes every currently routable provider/model without
21
+ * requiring a Session; `selectModel()` validates via llm.resolveCallConfig,
22
+ * sets the session Agent's live selection ref and saves the deployment default
23
+ * — the exact shipped path. The per-session `current`/`routable` client fields
24
+ * are derived here from the durable `modelSelection` projection (the client
25
+ * face of the same fold the first-party ModelDirectory reads).
19
26
  *
20
27
  * @param ctx - the mounted host Cordis context.
21
28
  */
22
29
  export const name = "dsh-model-select-plus";
23
- export const inject = ["webServer"];
30
+ export const inject = ["webServer", "sessionController", "sessionProjections", "sessions"];
24
31
 
25
32
  const PREFIX = "/plugins/dsh-model-select-plus/api";
26
33
 
@@ -45,55 +52,85 @@ function readBody(req) {
45
52
  });
46
53
  }
47
54
 
48
- export function apply(ctx) {
49
- const api = () => ctx.get("apiProxy");
55
+ /**
56
+ * Derive the client's per-session `current`/`routable` from the durable
57
+ * modelSelection projection (pending → lastUsed) falling back to the catalog's
58
+ * deployment default. Mirrors the first-party ModelDirectory fold
59
+ * (`current = projected.next ?? catalog.default`) so the client's "current
60
+ * model" trigger reflects the session's actual selection rather than guessing.
61
+ * A session that is not live (cold/persisted) still reports the deployment
62
+ * default so the trigger never shows an empty placeholder when the catalog is
63
+ * ready.
64
+ *
65
+ * @param ctx - host context (sessionProjections, sessions).
66
+ * @param sessionId - the session being previewed.
67
+ * @param catalog - `modelCatalog()` result for this host generation.
68
+ * @returns `{ current, routable }` where current is never null while the catalog is ready.
69
+ */
70
+ function currentSelection(ctx, sessionId, catalog) {
71
+ const session = ctx.sessions.get(sessionId);
72
+ let pending;
73
+ let lastUsed;
74
+ if (session !== undefined) {
75
+ const projections = ctx.sessionProjections.stateOf(session, "modelSelection");
76
+ if (projections !== undefined) {
77
+ pending = projections.pending;
78
+ lastUsed = projections.lastUsed;
79
+ }
80
+ }
81
+ // pending → lastUsed → deployment default (the same precedence the first-party
82
+ // client uses for `projected.next`, falling back to catalog.default).
83
+ const current = pending ?? lastUsed ?? catalog.default;
84
+ const routable = (catalog.routableProviders ?? []).includes(current.provider);
85
+ return { current: { ...current }, routable };
86
+ }
50
87
 
88
+ export function apply(ctx) {
51
89
  const handler = async (req, res) => {
52
90
  try {
53
91
  const url = new URL(req.url ?? "/", "http://localhost");
54
92
  const pathname = url.pathname;
55
- const proxy = api();
56
- if (proxy === undefined || proxy.sessions === undefined) {
57
- json(res, 500, { error: "apiProxy.sessions unavailable" });
58
- return;
59
- }
60
93
 
61
94
  if (req.method === "GET" && pathname === `${PREFIX}/catalog`) {
62
95
  const sessionId = url.searchParams.get("sessionId") ?? "";
63
- const r = await proxy.sessions.models({ rpcId: "mdsl-catalog", payload: { sessionId } });
64
- if (r.result.ok === true) {
65
- const v = r.result.value;
66
- json(res, 200, { groups: v.groups, failures: v.failures, current: v.current, routable: v.routable });
67
- } else {
68
- json(res, 200, { error: (r.result.error && r.result.error.message) || "加载模型目录失败" });
69
- }
96
+ const catalog = await ctx.sessionController.modelCatalog();
97
+ const current = currentSelection(ctx, sessionId, catalog);
98
+ json(res, 200, {
99
+ groups: catalog.groups,
100
+ failures: catalog.failures,
101
+ current: current.current,
102
+ routable: current.routable,
103
+ default: catalog.default,
104
+ routableProviders: catalog.routableProviders,
105
+ });
70
106
  return;
71
107
  }
72
108
 
73
109
  if (req.method === "POST" && pathname === `${PREFIX}/select`) {
74
110
  const body = JSON.parse((await readBody(req)) || "{}");
75
- const r = await proxy.sessions.selectModel({
76
- rpcId: "mdsl-select",
77
- payload: {
78
- sessionId: body.sessionId,
79
- provider: body.provider,
80
- model: body.model,
81
- ...(body.reasoningEffort === undefined || body.reasoningEffort === null
82
- ? {}
83
- : { reasoningEffort: body.reasoningEffort }),
84
- },
111
+ const value = await ctx.sessionController.selectModel({
112
+ sessionId: body.sessionId,
113
+ provider: body.provider,
114
+ model: body.model,
115
+ ...(body.reasoningEffort === undefined || body.reasoningEffort === null
116
+ ? {}
117
+ : { reasoningEffort: body.reasoningEffort }),
85
118
  });
86
- if (r.result.ok === true) {
87
- json(res, 200, { ok: true, selected: r.result.value.selected });
88
- } else {
89
- json(res, 200, { ok: false, message: (r.result.error && r.result.error.message) || "模型不可用" });
90
- }
119
+ json(res, 200, { ok: true, selected: value.selected });
91
120
  return;
92
121
  }
93
122
 
94
123
  json(res, 404, { error: "not found" });
95
124
  } catch (error) {
96
- json(res, 500, { error: error instanceof Error ? error.message : String(error) });
125
+ // selectModel throws a RemoteError on failure (e.g. model-unavailable);
126
+ // report it as a soft 200 `{ ok:false, message }` so the client renders
127
+ // the in-menu error instead of a hard network failure.
128
+ const remote = error && typeof error === "object" ? error : undefined;
129
+ const code = remote && remote.code ? remote.code : undefined;
130
+ const message = remote && remote.message
131
+ ? remote.message
132
+ : (error instanceof Error ? error.message : String(error));
133
+ json(res, 200, { ok: false, message, code });
97
134
  }
98
135
  };
99
136