scope-agent-workbench 0.2.0 → 0.2.3

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.
Files changed (88) hide show
  1. package/CHANGELOG.md +41 -0
  2. package/bridge/agent-email-relay.mjs +160 -160
  3. package/bridge/app-server-websocket-transport.mjs +219 -219
  4. package/bridge/archive/service.mjs +130 -130
  5. package/bridge/auth-profile-failover.mjs +58 -58
  6. package/bridge/auth-profile-manager.mjs +215 -215
  7. package/bridge/capacity-recovery.mjs +90 -90
  8. package/bridge/gpu-usage-history.mjs +322 -322
  9. package/bridge/local/history.mjs +20 -5
  10. package/bridge/plugins/aliyun-dlc/monitor.mjs +236 -236
  11. package/bridge/plugins/aliyun-dlc/probe.mjs +701 -701
  12. package/bridge/private-gateway.mjs +371 -371
  13. package/bridge/private-start.mjs +22 -22
  14. package/bridge/probes/app-server-handoff-probe.mjs +87 -87
  15. package/bridge/probes/app-server-writer-probe.mjs +69 -69
  16. package/bridge/probes/assistant-search-probe.mjs +321 -321
  17. package/bridge/probes/dialogue-probe.mjs +462 -462
  18. package/bridge/probes/history-probe.mjs +430 -345
  19. package/bridge/probes/new-session-preflight.mjs +14 -14
  20. package/bridge/probes/profile-cancel-probe.mjs +17 -17
  21. package/bridge/probes/profile-delete-probe.mjs +26 -26
  22. package/bridge/probes/profile-list-probe.mjs +114 -114
  23. package/bridge/probes/profile-write-probe.mjs +109 -109
  24. package/bridge/probes/raw-context-probe.mjs +51 -51
  25. package/bridge/probes/remote-probe.mjs +627 -626
  26. package/bridge/probes/submission-probe.mjs +2 -2
  27. package/bridge/probes/takeover-probe.mjs +326 -326
  28. package/bridge/probes/vscode-open-preflight.mjs +21 -21
  29. package/bridge/providers/claude-interactive.py +463 -341
  30. package/bridge/providers/cli-command.mjs +103 -103
  31. package/bridge/providers/conversations.mjs +231 -231
  32. package/bridge/providers/native-commands.py +38 -38
  33. package/bridge/providers/native-session-state.mjs +14 -14
  34. package/bridge/providers/native-session-state.py +96 -96
  35. package/bridge/providers/remote-job.py +130 -130
  36. package/bridge/remote-app-server-command.mjs +121 -121
  37. package/bridge/remote-directory-listing.mjs +148 -148
  38. package/bridge/rollout-summary.mjs +20 -2
  39. package/bridge/routes/dialogue.mjs +48 -3
  40. package/bridge/routes/models.mjs +312 -312
  41. package/bridge/server.mjs +3357 -3332
  42. package/bridge/session-attachments.mjs +150 -150
  43. package/bridge/session-catalog/agy-history.py +141 -2
  44. package/bridge/session-catalog/catalog.mjs +17 -0
  45. package/bridge/session-catalog/probe.py +1207 -331
  46. package/bridge/session-catalog/routes.mjs +6 -0
  47. package/bridge/start.mjs +205 -205
  48. package/bridge/state-persistence.mjs +9 -1
  49. package/bridge/tmux-session-control.mjs +29 -29
  50. package/bridge/vscode-opener.mjs +157 -157
  51. package/bridge/web-terminal.mjs +52 -52
  52. package/bridge/workbench/manager-context.mjs +51 -51
  53. package/bridge/workbench/service.mjs +301 -301
  54. package/dist/client/.vite/manifest.json +4 -4
  55. package/dist/client/assets/{index-Enu1x0PS.js → index-BMior55c.js} +2 -2
  56. package/dist/client/assets/{layout-segment-context-CmJzExWq.js → layout-segment-context-DjGudgXn.js} +1 -1
  57. package/dist/client/assets/page-CJqXlhDs.js +551 -0
  58. package/dist/client/assets/page-LYzRz6_S.css +1 -0
  59. package/dist/client/favicon.svg +6 -6
  60. package/dist/server/__vite_rsc_assets_manifest.js +8 -8
  61. package/dist/server/index.js +7 -7
  62. package/dist/server/ssr/__vite_rsc_assets_manifest.js +8 -8
  63. package/dist/server/ssr/assets/{page-CYYyCnKD.js → page-BB9Dri2u.js} +2416 -271
  64. package/dist/server/ssr/assets/page-LYzRz6_S.css +1 -0
  65. package/dist/server/ssr/index.js +1 -1
  66. package/dist/server/ssr/vinext-server.json +1 -1
  67. package/dist/server/vinext-server.json +1 -1
  68. package/dist/server/wrangler.json +1 -1
  69. package/lib/browser-snapshot-cache.js +53 -53
  70. package/lib/formula-preview.js +1 -1
  71. package/lib/incremental-snapshot.js +300 -300
  72. package/lib/markdown-math.js +151 -151
  73. package/lib/remote-links.js +95 -95
  74. package/lib/runtime-config.js +70 -70
  75. package/lib/session-filters.js +38 -38
  76. package/lib/ssh-config.js +26 -26
  77. package/lib/stable-order.js +39 -39
  78. package/lib/subagent-display-prefs.ts +130 -0
  79. package/package.json +1 -1
  80. package/scripts/agent-email-relay.mjs +104 -104
  81. package/scripts/email-notification-cli.mjs +28 -28
  82. package/scripts/enable-private-access.ps1 +109 -109
  83. package/scripts/fixtures/native-conversations.mjs +164 -6
  84. package/scripts/fixtures/workbench-conversations.mjs +475 -352
  85. package/scripts/preview-workbench.mjs +669 -669
  86. package/dist/client/assets/page-COBv6VWj.css +0 -1
  87. package/dist/client/assets/page-DVjU6VG_.js +0 -550
  88. package/dist/server/ssr/assets/page-COBv6VWj.css +0 -1
@@ -1,312 +1,312 @@
1
- import { LOCAL_HOST } from "../../lib/execution-host.js";
2
- /** Domain routes. Context is read per request so replaced snapshot state stays current. */
3
- export function modelRoutes(context) {
4
- return async (request, response, url) => {
5
- const {
6
- appServerManager,
7
- auditLifecycle,
8
- checkedProfileSlug,
9
- findAgent,
10
- hostRuntimeConfig,
11
- jsonResponse,
12
- listAuthProfiles,
13
- persistAppServerSessions,
14
- readInstalledModels,
15
- readJsonBody,
16
- selectTmuxModel,
17
- sessionProfileAssignment,
18
- sessionProfiles,
19
- sessionYolo,
20
- snapshot,
21
- } = context();
22
- if (url.pathname === "/api/models" && request.method === "GET") {
23
- try {
24
- const host = snapshot.hosts.find(
25
- (candidate) => candidate.alias === url.searchParams.get("host"),
26
- );
27
- if (!host?.reachable) {
28
- jsonResponse(response, 404, { error: "所选 SSH 主机当前不可连接" });
29
- return true;
30
- }
31
- const managedSession = appServerManager
32
- .sessionRecords()
33
- .find(
34
- (s) =>
35
- s.host === host.alias &&
36
- s.sessionId === url.searchParams.get("sessionId"),
37
- );
38
- const catalog = await readInstalledModels(
39
- host,
40
- url.searchParams.get("refresh") === "1",
41
- managedSession?.connectionKey || "active",
42
- );
43
- jsonResponse(response, 200, { host: host.alias, ...catalog });
44
- } catch (error) {
45
- jsonResponse(response, 502, {
46
- error: `读取模型目录失败:${error.message}`,
47
- });
48
- }
49
- return true;
50
- }
51
-
52
- if (
53
- url.pathname === "/api/session/profile" &&
54
- ["GET", "POST"].includes(request.method)
55
- ) {
56
- try {
57
- const body =
58
- request.method === "POST"
59
- ? await readJsonBody(request)
60
- : Object.fromEntries(url.searchParams);
61
- const host = snapshot.hosts.find((h) => h.alias === body.host);
62
- if (!host?.reachable) throw new Error("所选 SSH 主机当前不可连接");
63
- const session = appServerManager
64
- .sessionRecords()
65
- .find((s) => s.host === body.host && s.sessionId === body.sessionId);
66
- if (!session) throw new Error("请先将这个 Session 恢复到 App Server");
67
- if (request.method === "POST") {
68
- if (body.host === LOCAL_HOST) throw new Error("本机会话沿用本机 Codex 登录;远端 Profile 切换不适用于本机");
69
- const assignment =
70
- body.action === "cancel"
71
- ? await sessionProfiles.cancel(body.host, body.sessionId)
72
- : await sessionProfiles.assign(
73
- body.host,
74
- body.sessionId,
75
- checkedProfileSlug(body.slug),
76
- );
77
- await persistAppServerSessions();
78
- await auditLifecycle("session-profile-assigned", {
79
- host: body.host,
80
- sessionId: body.sessionId,
81
- profile: assignment?.desiredProfile || null,
82
- status: assignment?.status || "cancelled",
83
- remoteSessionSignalSent: false,
84
- });
85
- jsonResponse(response, 200, { assignment });
86
- } else {
87
- const profiles = await listAuthProfiles(body.host);
88
- let account = null;
89
- let accountError = null;
90
- let rateLimits = null;
91
- if (appServerManager.isConnected(body.host, body.sessionId)) {
92
- try {
93
- [{ account }, rateLimits] = await Promise.all([
94
- appServerManager.getAccount(
95
- body.host,
96
- session.executable,
97
- session.connectionKey,
98
- ),
99
- appServerManager
100
- .getRateLimits(
101
- body.host,
102
- session.executable,
103
- session.connectionKey,
104
- )
105
- .catch(() => null),
106
- ]);
107
- } catch (error) {
108
- accountError = error.message;
109
- }
110
- }
111
- jsonResponse(response, 200, {
112
- profiles: profiles.profiles,
113
- activeProfile: profiles.activeProfile,
114
- assignment: sessionProfileAssignment(body.host, body.sessionId),
115
- account,
116
- accountError,
117
- rateLimits,
118
- connectionKey: session.connectionKey,
119
- });
120
- }
121
- } catch (error) {
122
- jsonResponse(response, 409, { error: error.message });
123
- }
124
- return true;
125
- }
126
-
127
- if (url.pathname === "/api/session/model" && request.method === "POST") {
128
- try {
129
- const body = await readJsonBody(request);
130
- const agent = body.sessionId ? findAgent(body.host, body.sessionId)
131
- : snapshot.hosts.find((host) => host.alias === body.host)?.agents.find((item) => item.id === body.agentId && item.pane);
132
- const model =
133
- typeof body.model === "string" ? body.model.trim().slice(0, 200) : "";
134
- const effort =
135
- typeof body.effort === "string"
136
- ? body.effort.trim().slice(0, 40)
137
- : "";
138
- const yolo =
139
- typeof body.yolo === "boolean"
140
- ? body.yolo
141
- : sessionYolo(agent?.rollout, hostRuntimeConfig(body.host).yolo);
142
- if (!agent || (!agent.rollout?.id && !agent.pane) || !model) {
143
- jsonResponse(response, 404, {
144
- error: "找不到对应 Session 或模型为空",
145
- });
146
- return true;
147
- }
148
- if (
149
- agent.status === "working" &&
150
- !appServerManager.isConnected(body.host, body.sessionId)
151
- ) {
152
- jsonResponse(response, 409, {
153
- error: "该 Session 正在执行 turn,请在本轮结束后切换模型",
154
- });
155
- return true;
156
- }
157
- const host = snapshot.hosts.find(
158
- (candidate) => candidate.alias === agent.host,
159
- );
160
- const managedSession = appServerManager
161
- .sessionRecords()
162
- .find((s) => s.host === body.host && s.sessionId === body.sessionId);
163
- const { models } = await readInstalledModels(
164
- host,
165
- false,
166
- managedSession?.connectionKey || "active",
167
- );
168
- const selectedModel = models.find(
169
- (candidate) => candidate.id === model,
170
- );
171
- if (!selectedModel) {
172
- jsonResponse(response, 400, {
173
- error: `模型 ${model} 不在该主机当前账号的可用目录中`,
174
- });
175
- return true;
176
- }
177
- const supportedEfforts = selectedModel.supportedReasoningEfforts || [];
178
- if (supportedEfforts.length && !supportedEfforts.includes(effort)) {
179
- jsonResponse(response, 400, {
180
- error: `推理强度 ${effort || "(空)"} 不受模型 ${model} 支持`,
181
- });
182
- return true;
183
- }
184
- if (agent.rollout?.id && appServerManager.isConnected(agent.host, agent.rollout.id)) {
185
- const session = await appServerManager.setSessionModel(
186
- agent.host,
187
- agent.rollout.id,
188
- model,
189
- effort,
190
- yolo,
191
- );
192
- jsonResponse(response, 200, {
193
- ok: true,
194
- transport: "app-server",
195
- model: session.model,
196
- effort: session.effort,
197
- yolo: session.yolo,
198
- deferred: session.deferred,
199
- verified: true,
200
- });
201
- } else if (agent.pane) {
202
- if (typeof body.yolo === "boolean" && yolo !== sessionYolo(agent.rollout, true)) {
203
- jsonResponse(response, 409, {
204
- error:
205
- "tmux Session 的执行策略只能在恢复时修改;App Server 可立即切换",
206
- });
207
- return true;
208
- }
209
- const result = await selectTmuxModel(
210
- agent,
211
- selectedModel,
212
- models,
213
- effort,
214
- );
215
- jsonResponse(response, 200, {
216
- ok: true,
217
- transport: "tmux",
218
- ...result,
219
- });
220
- } else {
221
- jsonResponse(response, 409, {
222
- error:
223
- "该外部 Session 尚未接入 tmux/app-server,无法安全发送 /model",
224
- });
225
- }
226
- } catch (error) {
227
- jsonResponse(response, 502, {
228
- error: `切换模型失败:${error.message}`,
229
- });
230
- }
231
- return true;
232
- }
233
-
234
- if (url.pathname === "/api/host/model" && request.method === "POST") {
235
- try {
236
- const body = await readJsonBody(request);
237
- const host = snapshot.hosts.find(
238
- (candidate) => candidate.alias === body.host,
239
- );
240
- if (!host?.reachable) throw new Error("所选 SSH 主机当前不可连接");
241
- const { models } = await readInstalledModels(host, true);
242
- const selected = models.find((model) => model.id === body.model);
243
- if (!selected)
244
- throw new Error("模型不在该主机当前账号的可用目录中,请刷新模型列表");
245
- const effort =
246
- typeof body.effort === "string" ? body.effort.trim() : "";
247
- if (effort && !selected.supportedReasoningEfforts.includes(effort)) {
248
- throw new Error(`模型 ${selected.id} 不支持推理强度 ${effort}`);
249
- }
250
- const results = await appServerManager.setHostModel(
251
- host.alias,
252
- selected,
253
- effort,
254
- );
255
- const managed = new Set(results.map((result) => result.sessionId));
256
- for (const agent of host.agents || []) {
257
- const sessionId = agent.rollout?.id;
258
- if (!sessionId || agent.rollout.isSubagent || managed.has(sessionId))
259
- continue;
260
- managed.add(sessionId);
261
- if (!agent.pane || agent.status === "working") {
262
- results.push({
263
- sessionId,
264
- status: "skipped",
265
- reason: agent.pane
266
- ? "tmux 正在执行,本轮结束后可再次批量应用"
267
- : "外部 CLI 未接管",
268
- });
269
- continue;
270
- }
271
- try {
272
- const nextEffort =
273
- effort ||
274
- (selected.supportedReasoningEfforts.includes(
275
- agent.rollout.reasoningEffort,
276
- )
277
- ? agent.rollout.reasoningEffort
278
- : selected.defaultReasoningEffort);
279
- await selectTmuxModel(agent, selected, models, nextEffort);
280
- results.push({
281
- sessionId,
282
- transport: "tmux",
283
- status: "applied",
284
- effort: nextEffort,
285
- });
286
- } catch (error) {
287
- results.push({ sessionId, status: "failed", error: error.message });
288
- }
289
- }
290
- await persistAppServerSessions();
291
- await auditLifecycle("host-model-changed", {
292
- host: host.alias,
293
- model: selected.id,
294
- results,
295
- remoteSessionSignalSent: false,
296
- });
297
- jsonResponse(response, 200, {
298
- ok: results.every((result) => result.status !== "failed"),
299
- host: host.alias,
300
- model: selected.id,
301
- results,
302
- });
303
- } catch (error) {
304
- jsonResponse(response, 502, {
305
- error: `主机模型切换失败:${error.message}`,
306
- });
307
- }
308
- return true;
309
- }
310
- return false;
311
- };
312
- }
1
+ import { LOCAL_HOST } from "../../lib/execution-host.js";
2
+ /** Domain routes. Context is read per request so replaced snapshot state stays current. */
3
+ export function modelRoutes(context) {
4
+ return async (request, response, url) => {
5
+ const {
6
+ appServerManager,
7
+ auditLifecycle,
8
+ checkedProfileSlug,
9
+ findAgent,
10
+ hostRuntimeConfig,
11
+ jsonResponse,
12
+ listAuthProfiles,
13
+ persistAppServerSessions,
14
+ readInstalledModels,
15
+ readJsonBody,
16
+ selectTmuxModel,
17
+ sessionProfileAssignment,
18
+ sessionProfiles,
19
+ sessionYolo,
20
+ snapshot,
21
+ } = context();
22
+ if (url.pathname === "/api/models" && request.method === "GET") {
23
+ try {
24
+ const host = snapshot.hosts.find(
25
+ (candidate) => candidate.alias === url.searchParams.get("host"),
26
+ );
27
+ if (!host?.reachable) {
28
+ jsonResponse(response, 404, { error: "所选 SSH 主机当前不可连接" });
29
+ return true;
30
+ }
31
+ const managedSession = appServerManager
32
+ .sessionRecords()
33
+ .find(
34
+ (s) =>
35
+ s.host === host.alias &&
36
+ s.sessionId === url.searchParams.get("sessionId"),
37
+ );
38
+ const catalog = await readInstalledModels(
39
+ host,
40
+ url.searchParams.get("refresh") === "1",
41
+ managedSession?.connectionKey || "active",
42
+ );
43
+ jsonResponse(response, 200, { host: host.alias, ...catalog });
44
+ } catch (error) {
45
+ jsonResponse(response, 502, {
46
+ error: `读取模型目录失败:${error.message}`,
47
+ });
48
+ }
49
+ return true;
50
+ }
51
+
52
+ if (
53
+ url.pathname === "/api/session/profile" &&
54
+ ["GET", "POST"].includes(request.method)
55
+ ) {
56
+ try {
57
+ const body =
58
+ request.method === "POST"
59
+ ? await readJsonBody(request)
60
+ : Object.fromEntries(url.searchParams);
61
+ const host = snapshot.hosts.find((h) => h.alias === body.host);
62
+ if (!host?.reachable) throw new Error("所选 SSH 主机当前不可连接");
63
+ const session = appServerManager
64
+ .sessionRecords()
65
+ .find((s) => s.host === body.host && s.sessionId === body.sessionId);
66
+ if (!session) throw new Error("请先将这个 Session 恢复到 App Server");
67
+ if (request.method === "POST") {
68
+ if (body.host === LOCAL_HOST) throw new Error("本机会话沿用本机 Codex 登录;远端 Profile 切换不适用于本机");
69
+ const assignment =
70
+ body.action === "cancel"
71
+ ? await sessionProfiles.cancel(body.host, body.sessionId)
72
+ : await sessionProfiles.assign(
73
+ body.host,
74
+ body.sessionId,
75
+ checkedProfileSlug(body.slug),
76
+ );
77
+ await persistAppServerSessions();
78
+ await auditLifecycle("session-profile-assigned", {
79
+ host: body.host,
80
+ sessionId: body.sessionId,
81
+ profile: assignment?.desiredProfile || null,
82
+ status: assignment?.status || "cancelled",
83
+ remoteSessionSignalSent: false,
84
+ });
85
+ jsonResponse(response, 200, { assignment });
86
+ } else {
87
+ const profiles = await listAuthProfiles(body.host);
88
+ let account = null;
89
+ let accountError = null;
90
+ let rateLimits = null;
91
+ if (appServerManager.isConnected(body.host, body.sessionId)) {
92
+ try {
93
+ [{ account }, rateLimits] = await Promise.all([
94
+ appServerManager.getAccount(
95
+ body.host,
96
+ session.executable,
97
+ session.connectionKey,
98
+ ),
99
+ appServerManager
100
+ .getRateLimits(
101
+ body.host,
102
+ session.executable,
103
+ session.connectionKey,
104
+ )
105
+ .catch(() => null),
106
+ ]);
107
+ } catch (error) {
108
+ accountError = error.message;
109
+ }
110
+ }
111
+ jsonResponse(response, 200, {
112
+ profiles: profiles.profiles,
113
+ activeProfile: profiles.activeProfile,
114
+ assignment: sessionProfileAssignment(body.host, body.sessionId),
115
+ account,
116
+ accountError,
117
+ rateLimits,
118
+ connectionKey: session.connectionKey,
119
+ });
120
+ }
121
+ } catch (error) {
122
+ jsonResponse(response, 409, { error: error.message });
123
+ }
124
+ return true;
125
+ }
126
+
127
+ if (url.pathname === "/api/session/model" && request.method === "POST") {
128
+ try {
129
+ const body = await readJsonBody(request);
130
+ const agent = body.sessionId ? findAgent(body.host, body.sessionId)
131
+ : snapshot.hosts.find((host) => host.alias === body.host)?.agents.find((item) => item.id === body.agentId && item.pane);
132
+ const model =
133
+ typeof body.model === "string" ? body.model.trim().slice(0, 200) : "";
134
+ const effort =
135
+ typeof body.effort === "string"
136
+ ? body.effort.trim().slice(0, 40)
137
+ : "";
138
+ const yolo =
139
+ typeof body.yolo === "boolean"
140
+ ? body.yolo
141
+ : sessionYolo(agent?.rollout, hostRuntimeConfig(body.host).yolo);
142
+ if (!agent || (!agent.rollout?.id && !agent.pane) || !model) {
143
+ jsonResponse(response, 404, {
144
+ error: "找不到对应 Session 或模型为空",
145
+ });
146
+ return true;
147
+ }
148
+ if (
149
+ agent.status === "working" &&
150
+ !appServerManager.isConnected(body.host, body.sessionId)
151
+ ) {
152
+ jsonResponse(response, 409, {
153
+ error: "该 Session 正在执行 turn,请在本轮结束后切换模型",
154
+ });
155
+ return true;
156
+ }
157
+ const host = snapshot.hosts.find(
158
+ (candidate) => candidate.alias === agent.host,
159
+ );
160
+ const managedSession = appServerManager
161
+ .sessionRecords()
162
+ .find((s) => s.host === body.host && s.sessionId === body.sessionId);
163
+ const { models } = await readInstalledModels(
164
+ host,
165
+ false,
166
+ managedSession?.connectionKey || "active",
167
+ );
168
+ const selectedModel = models.find(
169
+ (candidate) => candidate.id === model,
170
+ );
171
+ if (!selectedModel) {
172
+ jsonResponse(response, 400, {
173
+ error: `模型 ${model} 不在该主机当前账号的可用目录中`,
174
+ });
175
+ return true;
176
+ }
177
+ const supportedEfforts = selectedModel.supportedReasoningEfforts || [];
178
+ if (supportedEfforts.length && !supportedEfforts.includes(effort)) {
179
+ jsonResponse(response, 400, {
180
+ error: `推理强度 ${effort || "(空)"} 不受模型 ${model} 支持`,
181
+ });
182
+ return true;
183
+ }
184
+ if (agent.rollout?.id && appServerManager.isConnected(agent.host, agent.rollout.id)) {
185
+ const session = await appServerManager.setSessionModel(
186
+ agent.host,
187
+ agent.rollout.id,
188
+ model,
189
+ effort,
190
+ yolo,
191
+ );
192
+ jsonResponse(response, 200, {
193
+ ok: true,
194
+ transport: "app-server",
195
+ model: session.model,
196
+ effort: session.effort,
197
+ yolo: session.yolo,
198
+ deferred: session.deferred,
199
+ verified: true,
200
+ });
201
+ } else if (agent.pane) {
202
+ if (typeof body.yolo === "boolean" && yolo !== sessionYolo(agent.rollout, true)) {
203
+ jsonResponse(response, 409, {
204
+ error:
205
+ "tmux Session 的执行策略只能在恢复时修改;App Server 可立即切换",
206
+ });
207
+ return true;
208
+ }
209
+ const result = await selectTmuxModel(
210
+ agent,
211
+ selectedModel,
212
+ models,
213
+ effort,
214
+ );
215
+ jsonResponse(response, 200, {
216
+ ok: true,
217
+ transport: "tmux",
218
+ ...result,
219
+ });
220
+ } else {
221
+ jsonResponse(response, 409, {
222
+ error:
223
+ "该外部 Session 尚未接入 tmux/app-server,无法安全发送 /model",
224
+ });
225
+ }
226
+ } catch (error) {
227
+ jsonResponse(response, 502, {
228
+ error: `切换模型失败:${error.message}`,
229
+ });
230
+ }
231
+ return true;
232
+ }
233
+
234
+ if (url.pathname === "/api/host/model" && request.method === "POST") {
235
+ try {
236
+ const body = await readJsonBody(request);
237
+ const host = snapshot.hosts.find(
238
+ (candidate) => candidate.alias === body.host,
239
+ );
240
+ if (!host?.reachable) throw new Error("所选 SSH 主机当前不可连接");
241
+ const { models } = await readInstalledModels(host, true);
242
+ const selected = models.find((model) => model.id === body.model);
243
+ if (!selected)
244
+ throw new Error("模型不在该主机当前账号的可用目录中,请刷新模型列表");
245
+ const effort =
246
+ typeof body.effort === "string" ? body.effort.trim() : "";
247
+ if (effort && !selected.supportedReasoningEfforts.includes(effort)) {
248
+ throw new Error(`模型 ${selected.id} 不支持推理强度 ${effort}`);
249
+ }
250
+ const results = await appServerManager.setHostModel(
251
+ host.alias,
252
+ selected,
253
+ effort,
254
+ );
255
+ const managed = new Set(results.map((result) => result.sessionId));
256
+ for (const agent of host.agents || []) {
257
+ const sessionId = agent.rollout?.id;
258
+ if (!sessionId || agent.rollout.isSubagent || managed.has(sessionId))
259
+ continue;
260
+ managed.add(sessionId);
261
+ if (!agent.pane || agent.status === "working") {
262
+ results.push({
263
+ sessionId,
264
+ status: "skipped",
265
+ reason: agent.pane
266
+ ? "tmux 正在执行,本轮结束后可再次批量应用"
267
+ : "外部 CLI 未接管",
268
+ });
269
+ continue;
270
+ }
271
+ try {
272
+ const nextEffort =
273
+ effort ||
274
+ (selected.supportedReasoningEfforts.includes(
275
+ agent.rollout.reasoningEffort,
276
+ )
277
+ ? agent.rollout.reasoningEffort
278
+ : selected.defaultReasoningEffort);
279
+ await selectTmuxModel(agent, selected, models, nextEffort);
280
+ results.push({
281
+ sessionId,
282
+ transport: "tmux",
283
+ status: "applied",
284
+ effort: nextEffort,
285
+ });
286
+ } catch (error) {
287
+ results.push({ sessionId, status: "failed", error: error.message });
288
+ }
289
+ }
290
+ await persistAppServerSessions();
291
+ await auditLifecycle("host-model-changed", {
292
+ host: host.alias,
293
+ model: selected.id,
294
+ results,
295
+ remoteSessionSignalSent: false,
296
+ });
297
+ jsonResponse(response, 200, {
298
+ ok: results.every((result) => result.status !== "failed"),
299
+ host: host.alias,
300
+ model: selected.id,
301
+ results,
302
+ });
303
+ } catch (error) {
304
+ jsonResponse(response, 502, {
305
+ error: `主机模型切换失败:${error.message}`,
306
+ });
307
+ }
308
+ return true;
309
+ }
310
+ return false;
311
+ };
312
+ }