codexmate 0.0.32 → 0.0.33

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 (147) hide show
  1. package/README.md +147 -363
  2. package/README.zh.md +147 -371
  3. package/cli/agents-files.js +224 -224
  4. package/cli/archive-helpers.js +446 -446
  5. package/cli/auth-profiles.js +375 -375
  6. package/cli/builtin-proxy.js +1725 -1725
  7. package/cli/claude-proxy.js +1022 -1022
  8. package/cli/config-bootstrap.js +402 -402
  9. package/cli/config-health.js +454 -454
  10. package/cli/doctor-core.js +903 -903
  11. package/cli/import-skills-url.js +356 -356
  12. package/cli/local-bridge.js +551 -324
  13. package/cli/openai-bridge.js +1653 -1653
  14. package/cli/openclaw-config.js +629 -629
  15. package/cli/session-convert-args.js +69 -69
  16. package/cli/session-convert-io.js +82 -82
  17. package/cli/session-convert.js +150 -150
  18. package/cli/session-usage.concurrent.js +28 -28
  19. package/cli/session-usage.js +118 -118
  20. package/cli/session-usage.models.js +176 -176
  21. package/cli/skills.js +1141 -1141
  22. package/cli/update.js +162 -0
  23. package/cli/zip-commands.js +510 -510
  24. package/cli.js +16023 -15829
  25. package/lib/automation.js +404 -404
  26. package/lib/cli-file-utils.js +151 -151
  27. package/lib/cli-models-utils.js +440 -440
  28. package/lib/cli-network-utils.js +190 -190
  29. package/lib/cli-path-utils.js +85 -85
  30. package/lib/cli-session-utils.js +121 -121
  31. package/lib/cli-sessions.js +427 -426
  32. package/lib/cli-utils.js +155 -155
  33. package/lib/cli-webhook.js +126 -126
  34. package/lib/download-artifacts.js +92 -92
  35. package/lib/mcp-stdio.js +453 -453
  36. package/lib/task-orchestrator.js +869 -869
  37. package/lib/text-diff.js +303 -303
  38. package/lib/win-tray.js +119 -0
  39. package/lib/workflow-engine.js +340 -340
  40. package/package.json +76 -76
  41. package/plugins/README.md +20 -20
  42. package/plugins/README.zh-CN.md +20 -20
  43. package/plugins/prompt-templates/comment-polish/index.mjs +25 -25
  44. package/plugins/prompt-templates/computed.mjs +253 -253
  45. package/plugins/prompt-templates/index.mjs +8 -8
  46. package/plugins/prompt-templates/manifest.mjs +15 -15
  47. package/plugins/prompt-templates/methods.mjs +553 -553
  48. package/plugins/prompt-templates/overview.mjs +91 -91
  49. package/plugins/prompt-templates/ownership.mjs +19 -19
  50. package/plugins/prompt-templates/rule-ack/index.mjs +21 -21
  51. package/plugins/prompt-templates/storage.mjs +64 -64
  52. package/plugins/registry.mjs +16 -16
  53. package/web-ui/app.js +650 -647
  54. package/web-ui/index.html +36 -36
  55. package/web-ui/logic.agents-diff.mjs +386 -386
  56. package/web-ui/logic.claude.mjs +168 -168
  57. package/web-ui/logic.codex.mjs +69 -69
  58. package/web-ui/logic.mjs +5 -5
  59. package/web-ui/logic.runtime.mjs +128 -128
  60. package/web-ui/logic.session-convert.mjs +70 -70
  61. package/web-ui/logic.sessions.mjs +781 -781
  62. package/web-ui/modules/api.mjs +90 -90
  63. package/web-ui/modules/app.computed.dashboard.mjs +248 -248
  64. package/web-ui/modules/app.computed.index.mjs +17 -17
  65. package/web-ui/modules/app.computed.main-tabs.mjs +205 -205
  66. package/web-ui/modules/app.computed.session.mjs +735 -693
  67. package/web-ui/modules/app.constants.mjs +15 -15
  68. package/web-ui/modules/app.methods.agents.mjs +651 -651
  69. package/web-ui/modules/app.methods.claude-config.mjs +269 -200
  70. package/web-ui/modules/app.methods.codex-config.mjs +861 -861
  71. package/web-ui/modules/app.methods.index.mjs +94 -94
  72. package/web-ui/modules/app.methods.install.mjs +205 -205
  73. package/web-ui/modules/app.methods.navigation.mjs +774 -774
  74. package/web-ui/modules/app.methods.openclaw-core.mjs +814 -814
  75. package/web-ui/modules/app.methods.openclaw-editing.mjs +372 -372
  76. package/web-ui/modules/app.methods.openclaw-persist.mjs +369 -369
  77. package/web-ui/modules/app.methods.providers.mjs +575 -529
  78. package/web-ui/modules/app.methods.runtime.mjs +345 -345
  79. package/web-ui/modules/app.methods.session-actions.mjs +591 -591
  80. package/web-ui/modules/app.methods.session-browser.mjs +1012 -1012
  81. package/web-ui/modules/app.methods.session-timeline.mjs +479 -479
  82. package/web-ui/modules/app.methods.session-trash.mjs +438 -438
  83. package/web-ui/modules/app.methods.startup-claude.mjs +538 -537
  84. package/web-ui/modules/app.methods.task-orchestration.mjs +556 -556
  85. package/web-ui/modules/app.methods.webhook.mjs +79 -79
  86. package/web-ui/modules/config-mode.computed.mjs +124 -124
  87. package/web-ui/modules/config-template-confirm-pref.mjs +33 -33
  88. package/web-ui/modules/i18n.dict.mjs +3192 -3177
  89. package/web-ui/modules/i18n.mjs +62 -62
  90. package/web-ui/modules/plugins.computed.mjs +3 -3
  91. package/web-ui/modules/plugins.methods.mjs +3 -3
  92. package/web-ui/modules/plugins.storage.mjs +11 -11
  93. package/web-ui/modules/provider-url-display.mjs +17 -17
  94. package/web-ui/modules/sessions-filters-url.mjs +85 -85
  95. package/web-ui/modules/skills.computed.mjs +107 -107
  96. package/web-ui/modules/skills.methods.mjs +482 -482
  97. package/web-ui/partials/index/layout-footer.html +13 -13
  98. package/web-ui/partials/index/layout-header.html +499 -503
  99. package/web-ui/partials/index/modal-config-template-agents.html +185 -185
  100. package/web-ui/partials/index/modal-confirm-toast.html +32 -32
  101. package/web-ui/partials/index/modal-health-check.html +45 -45
  102. package/web-ui/partials/index/modal-openclaw-config.html +280 -280
  103. package/web-ui/partials/index/modal-skills.html +200 -200
  104. package/web-ui/partials/index/modals-basic.html +173 -162
  105. package/web-ui/partials/index/panel-config-claude.html +166 -136
  106. package/web-ui/partials/index/panel-config-codex.html +190 -196
  107. package/web-ui/partials/index/panel-config-codex.html.bak +337 -0
  108. package/web-ui/partials/index/panel-config-openclaw.html +83 -83
  109. package/web-ui/partials/index/panel-dashboard.html +186 -219
  110. package/web-ui/partials/index/panel-docs.html +114 -114
  111. package/web-ui/partials/index/panel-market.html +177 -177
  112. package/web-ui/partials/index/panel-orchestration.html +391 -391
  113. package/web-ui/partials/index/panel-plugins.html +253 -253
  114. package/web-ui/partials/index/panel-sessions.html +319 -313
  115. package/web-ui/partials/index/panel-settings.html +190 -190
  116. package/web-ui/partials/index/panel-trash.html +82 -82
  117. package/web-ui/partials/index/panel-usage.html +137 -137
  118. package/web-ui/res/json5.min.js +1 -1
  119. package/web-ui/res/vue.global.prod.js +13 -13
  120. package/web-ui/session-helpers.mjs +591 -591
  121. package/web-ui/source-bundle.cjs +233 -233
  122. package/web-ui/styles/base-theme.css +281 -281
  123. package/web-ui/styles/bridge-pool.css +197 -197
  124. package/web-ui/styles/controls-forms.css +433 -433
  125. package/web-ui/styles/dashboard.css +406 -406
  126. package/web-ui/styles/docs-panel.css +245 -245
  127. package/web-ui/styles/feedback.css +108 -108
  128. package/web-ui/styles/health-check-dialog.css +144 -144
  129. package/web-ui/styles/layout-shell.css +628 -638
  130. package/web-ui/styles/modals-core.css +499 -466
  131. package/web-ui/styles/navigation-panels.css +391 -391
  132. package/web-ui/styles/openclaw-structured.css +266 -266
  133. package/web-ui/styles/plugins-panel.css +564 -564
  134. package/web-ui/styles/responsive.css +392 -392
  135. package/web-ui/styles/sessions-list.css +683 -647
  136. package/web-ui/styles/sessions-preview.css +407 -407
  137. package/web-ui/styles/sessions-toolbar-trash.css +518 -518
  138. package/web-ui/styles/sessions-usage.css +588 -588
  139. package/web-ui/styles/settings-panel.css +349 -349
  140. package/web-ui/styles/skills-list.css +305 -305
  141. package/web-ui/styles/skills-market.css +429 -429
  142. package/web-ui/styles/task-orchestration.css +822 -822
  143. package/web-ui/styles/titles-cards.css +472 -472
  144. package/web-ui/styles/trash-panel.css +90 -90
  145. package/web-ui/styles/webhook.css +81 -81
  146. package/web-ui/styles.css +24 -24
  147. package/web-ui.html +17 -17
@@ -1,454 +1,454 @@
1
- const { isValidHttpUrl, normalizeBaseUrl } = require('../lib/cli-utils');
2
- const { buildModelProbeSpecs } = require('../lib/cli-models-utils');
3
- const { probeJsonPost } = require('../lib/cli-network-utils');
4
-
5
- const DEFAULT_TIMEOUT_MS = 6000;
6
- const JSON_RESPONSE_MAX_BYTES = 256 * 1024;
7
-
8
- function buildRemoteHealthMessage(issueCode, statusCode, detail) {
9
- if (!issueCode) {
10
- return '远程模型探测通过:endpoint、鉴权与模型均可用';
11
- }
12
-
13
- if (issueCode === 'remote-model-probe-unreachable') {
14
- return '远程模型接口不可达,请检查 endpoint、网络或 DNS';
15
- }
16
-
17
- if (issueCode === 'remote-model-probe-auth-failed') {
18
- return '远程模型探测鉴权失败(401/403),请检查 API Key、endpoint 与模型权限';
19
- }
20
-
21
- if (issueCode === 'remote-model-probe-not-found') {
22
- return '远程模型探测返回 404,请检查 base_url、接口路径或模型名';
23
- }
24
-
25
- if (issueCode === 'remote-model-probe-error') {
26
- return detail || '远程模型接口返回错误,请检查模型名与账号权限';
27
- }
28
-
29
- if (issueCode === 'remote-model-probe-http-error' && statusCode) {
30
- return `远程模型探测返回 HTTP ${statusCode},请检查 endpoint 与模型`;
31
- }
32
-
33
- return '远程模型探测失败,请检查配置与远端服务状态';
34
- }
35
-
36
- function extractPayloadErrorMessage(payload) {
37
- if (!payload || typeof payload !== 'object') {
38
- return '';
39
- }
40
- if (typeof payload.error === 'string' && payload.error.trim()) {
41
- return payload.error.trim();
42
- }
43
- if (!payload.error || typeof payload.error !== 'object') {
44
- return '';
45
- }
46
- if (typeof payload.error.message === 'string' && payload.error.message.trim()) {
47
- return payload.error.message.trim();
48
- }
49
- if (typeof payload.error.code === 'string' && payload.error.code.trim()) {
50
- return payload.error.code.trim();
51
- }
52
- return '';
53
- }
54
-
55
- async function runRemoteHealthCheck(providerName, provider, modelName, options = {}) {
56
- const issues = [];
57
- const baseUrl = normalizeBaseUrl(provider && provider.base_url ? provider.base_url : '');
58
- const summary = {
59
- type: 'remote-health-check',
60
- provider: typeof providerName === 'string' ? providerName.trim() : '',
61
- endpoint: baseUrl,
62
- ok: false,
63
- statusCode: null,
64
- message: '',
65
- checks: {}
66
- };
67
-
68
- if (!baseUrl) {
69
- issues.push({
70
- code: 'remote-skip-base-url',
71
- message: '无法进行远程探测:base_url 为空',
72
- suggestion: '补全 base_url 或关闭远程探测'
73
- });
74
- summary.message = '无法进行远程探测:base_url 为空';
75
- return { issues, remote: summary };
76
- }
77
-
78
- if (!isValidHttpUrl(baseUrl)) {
79
- issues.push({
80
- code: 'remote-skip-base-url',
81
- message: '无法进行远程探测:base_url 无效',
82
- suggestion: '补全 base_url 或关闭远程探测'
83
- });
84
- summary.message = '无法进行远程探测:base_url 无效';
85
- return { issues, remote: summary };
86
- }
87
-
88
- const modelProbeSpecs = buildModelProbeSpecs(provider, modelName, baseUrl);
89
- if (!modelProbeSpecs.length) {
90
- issues.push({
91
- code: 'remote-skip-model',
92
- message: '无法进行远程探测:当前模型未设置',
93
- suggestion: '补全 model 后重试'
94
- });
95
- summary.message = '无法进行远程探测:当前模型未设置';
96
- return { issues, remote: summary };
97
- }
98
-
99
- const requiresAuth = provider && provider.requires_openai_auth !== false;
100
- const apiKey = typeof provider.preferred_auth_method === 'string'
101
- ? provider.preferred_auth_method.trim()
102
- : '';
103
- const authValue = requiresAuth ? apiKey : (apiKey || '');
104
- const timeoutMs = Number.isFinite(options.timeoutMs)
105
- ? Math.max(1000, Number(options.timeoutMs))
106
- : DEFAULT_TIMEOUT_MS;
107
- const runProbeJsonPost = typeof options.probeJsonPost === 'function' ? options.probeJsonPost : probeJsonPost;
108
-
109
- let modelProbeSpec = modelProbeSpecs[0];
110
- let modelProbe = null;
111
- for (let index = 0; index < modelProbeSpecs.length; index += 1) {
112
- const candidate = modelProbeSpecs[index];
113
- const probeResult = await runProbeJsonPost(candidate.url, candidate.body, {
114
- apiKey: authValue,
115
- timeoutMs,
116
- maxBytes: JSON_RESPONSE_MAX_BYTES
117
- });
118
- modelProbeSpec = candidate;
119
- modelProbe = probeResult;
120
- const shouldTryNextCandidate = index < modelProbeSpecs.length - 1
121
- && (!probeResult.ok || probeResult.status === 404);
122
- if (!shouldTryNextCandidate) {
123
- break;
124
- }
125
- }
126
-
127
- summary.checks.modelProbe = {
128
- url: modelProbeSpec.url,
129
- ok: !!modelProbe.ok,
130
- status: Number.isFinite(modelProbe.status) ? modelProbe.status : 0,
131
- durationMs: Number.isFinite(modelProbe.durationMs) ? modelProbe.durationMs : 0
132
- };
133
-
134
- if (!modelProbe.ok) {
135
- issues.push({
136
- code: 'remote-model-probe-unreachable',
137
- message: `模型可用性探测失败:${modelProbe.error || '无法连接'}`,
138
- suggestion: '检查 endpoint、网络或模型接口是否可用'
139
- });
140
- } else if (modelProbe.status === 401 || modelProbe.status === 403) {
141
- issues.push({
142
- code: 'remote-model-probe-auth-failed',
143
- message: '模型可用性探测鉴权失败(401/403)',
144
- suggestion: '检查 API Key 或认证方式'
145
- });
146
- } else if (modelProbe.status === 404) {
147
- issues.push({
148
- code: 'remote-model-probe-not-found',
149
- message: '模型可用性探测返回 404',
150
- suggestion: '检查 base_url、接口路径或模型名'
151
- });
152
- } else if (modelProbe.status >= 400) {
153
- issues.push({
154
- code: 'remote-model-probe-http-error',
155
- message: `模型可用性探测返回异常状态: ${modelProbe.status}`,
156
- suggestion: '检查 endpoint、模型名或服务状态'
157
- });
158
- } else {
159
- let payload = null;
160
- try {
161
- payload = modelProbe.body ? JSON.parse(modelProbe.body) : null;
162
- } catch (e) {
163
- payload = null;
164
- }
165
- const payloadError = extractPayloadErrorMessage(payload);
166
- if (payloadError) {
167
- issues.push({
168
- code: 'remote-model-probe-error',
169
- message: `模型可用性探测失败:${payloadError}`,
170
- suggestion: '检查模型名与权限'
171
- });
172
- }
173
- }
174
-
175
- const primaryIssue = issues[0] || null;
176
- summary.ok = issues.length === 0;
177
- summary.statusCode = Number.isFinite(modelProbe.status) && modelProbe.status > 0
178
- ? modelProbe.status
179
- : null;
180
- summary.message = buildRemoteHealthMessage(
181
- primaryIssue ? primaryIssue.code : '',
182
- summary.statusCode,
183
- primaryIssue ? primaryIssue.message : ''
184
- );
185
-
186
- return { issues, remote: summary };
187
- }
188
-
189
- async function buildConfigHealthReport(params = {}, deps = {}) {
190
- const issues = [];
191
- const {
192
- readConfigOrVirtualDefault,
193
- readModels
194
- } = deps;
195
-
196
- if (typeof readConfigOrVirtualDefault !== 'function') {
197
- throw new Error('buildConfigHealthReport 缺少 readConfigOrVirtualDefault 依赖');
198
- }
199
- if (typeof readModels !== 'function') {
200
- throw new Error('buildConfigHealthReport 缺少 readModels 依赖');
201
- }
202
-
203
- const status = readConfigOrVirtualDefault();
204
- const config = status.config || {};
205
-
206
- if (status.isVirtual) {
207
- const parseFailed = status.errorType === 'parse';
208
- const readFailed = status.errorType === 'read';
209
- issues.push({
210
- code: parseFailed ? 'config-parse-failed' : (readFailed ? 'config-read-failed' : 'config-missing'),
211
- message: status.reason || (parseFailed
212
- ? 'config.toml 解析失败'
213
- : (readFailed ? '读取 config.toml 失败' : '未检测到 config.toml')),
214
- suggestion: parseFailed
215
- ? '修复 config.toml 语法错误后重试'
216
- : (readFailed ? '检查文件权限后重试' : '在模板编辑器中确认应用配置,生成可用的 config.toml')
217
- });
218
- if (parseFailed || readFailed) {
219
- return {
220
- ok: false,
221
- issues,
222
- summary: {
223
- currentProvider: '',
224
- currentModel: ''
225
- },
226
- remote: null
227
- };
228
- }
229
- }
230
-
231
- const providerName = typeof config.model_provider === 'string' ? config.model_provider.trim() : '';
232
- const modelName = typeof config.model === 'string' ? config.model.trim() : '';
233
- if (!providerName) {
234
- issues.push({
235
- code: 'provider-missing',
236
- message: '当前 provider 未设置',
237
- suggestion: '在模板中设置 model_provider'
238
- });
239
- }
240
-
241
- if (!modelName) {
242
- issues.push({
243
- code: 'model-missing',
244
- message: '当前模型未设置',
245
- suggestion: '在模板中设置 model'
246
- });
247
- }
248
-
249
- const providers = config.model_providers && typeof config.model_providers === 'object'
250
- ? config.model_providers
251
- : {};
252
- const provider = providerName ? providers[providerName] : null;
253
- if (providerName && !provider) {
254
- issues.push({
255
- code: 'provider-not-found',
256
- message: `当前 provider 未在配置中找到: ${providerName}`,
257
- suggestion: '检查 model_providers 是否包含该 provider 配置块'
258
- });
259
- }
260
-
261
- if (provider && typeof provider === 'object') {
262
- const baseUrl = typeof provider.base_url === 'string' ? provider.base_url.trim() : '';
263
- if (!isValidHttpUrl(baseUrl)) {
264
- issues.push({
265
- code: 'base-url-invalid',
266
- message: '当前 provider 的 base_url 无效',
267
- suggestion: '请设置为 http/https 的完整 URL'
268
- });
269
- }
270
-
271
- const requiresAuth = provider.requires_openai_auth;
272
- if (requiresAuth !== false) {
273
- const apiKey = typeof provider.preferred_auth_method === 'string'
274
- ? provider.preferred_auth_method.trim()
275
- : '';
276
- if (!apiKey) {
277
- issues.push({
278
- code: 'api-key-missing',
279
- message: '当前 provider 未配置 API Key',
280
- suggestion: '在模板中设置 preferred_auth_method'
281
- });
282
- }
283
- }
284
- }
285
-
286
- if (modelName) {
287
- const models = readModels();
288
- if (!models.includes(modelName)) {
289
- issues.push({
290
- code: 'model-unavailable',
291
- message: `模型未在可用列表中找到: ${modelName}`,
292
- suggestion: '在模型列表中添加该模型或切换到已有模型'
293
- });
294
- }
295
- }
296
-
297
- let remote = null;
298
- if (params.remote) {
299
- if (!provider) {
300
- issues.push({
301
- code: 'remote-skip-provider',
302
- message: '无法进行远程探测:provider 未找到',
303
- suggestion: '检查 model_provider 配置或关闭远程探测'
304
- });
305
- remote = {
306
- type: 'remote-health-check',
307
- provider: providerName,
308
- endpoint: '',
309
- ok: false,
310
- statusCode: null,
311
- message: '无法进行远程探测:provider 未找到',
312
- checks: {}
313
- };
314
- } else {
315
- const remoteReport = await runRemoteHealthCheck(providerName, provider, modelName, {
316
- timeoutMs: Number.isFinite(params.timeoutMs) ? Number(params.timeoutMs) : undefined,
317
- probeJsonPost: deps.probeJsonPost
318
- });
319
- issues.push(...remoteReport.issues);
320
- remote = remoteReport.remote;
321
- }
322
- }
323
-
324
- return {
325
- ok: issues.length === 0,
326
- issues,
327
- summary: {
328
- currentProvider: providerName,
329
- currentModel: modelName
330
- },
331
- remote
332
- };
333
- }
334
-
335
- async function buildAllProvidersHealthReport(params = {}, deps = {}) {
336
- const {
337
- readConfigOrVirtualDefault,
338
- readCurrentModels,
339
- probeJsonPost: probeJsonPostDep
340
- } = deps;
341
-
342
- if (typeof readConfigOrVirtualDefault !== 'function') {
343
- throw new Error('buildAllProvidersHealthReport 缺少 readConfigOrVirtualDefault 依赖');
344
- }
345
-
346
- const status = readConfigOrVirtualDefault();
347
- const config = status.config || {};
348
- const providerEntries = config.model_providers && typeof config.model_providers === 'object'
349
- ? Object.entries(config.model_providers)
350
- : [];
351
- const currentProvider = typeof config.model_provider === 'string' ? config.model_provider.trim() : '';
352
- const currentModels = typeof readCurrentModels === 'function' ? readCurrentModels() : {};
353
-
354
- if (!providerEntries.length) {
355
- return {
356
- ok: true,
357
- currentProvider,
358
- providers: [],
359
- summary: { total: 0, green: 0, yellow: 0, red: 0 }
360
- };
361
- }
362
-
363
- const remote = !!(params && params.remote);
364
- const timeoutMs = Number.isFinite(params && params.timeoutMs) ? Number(params.timeoutMs) : DEFAULT_TIMEOUT_MS;
365
-
366
- const entries = providerEntries.map(([name, provider]) => {
367
- const baseUrl = typeof provider.base_url === 'string' ? provider.base_url.trim() : '';
368
- const requiresAuth = provider.requires_openai_auth !== false;
369
- const apiKey = typeof provider.preferred_auth_method === 'string'
370
- ? provider.preferred_auth_method.trim() : '';
371
- const modelName = currentModels[name] || (name === currentProvider ? (config.model || '').trim() : '');
372
-
373
- const checks = {
374
- baseUrlValid: isValidHttpUrl(baseUrl),
375
- apiKeyPresent: !requiresAuth || !!apiKey
376
- };
377
-
378
- const issues = [];
379
- if (!baseUrl) {
380
- issues.push({ code: 'base-url-empty', message: `${name} 的 base_url 为空`, suggestion: '设置 base_url' });
381
- } else if (!checks.baseUrlValid) {
382
- issues.push({ code: 'base-url-invalid', message: `${name} 的 base_url 无效`, suggestion: '设置为 http/https 完整 URL' });
383
- }
384
- if (!checks.apiKeyPresent) {
385
- issues.push({ code: 'api-key-missing', message: `${name} 未配置 API Key`, suggestion: '设置 preferred_auth_method' });
386
- }
387
-
388
- return { name, provider, baseUrl, modelName, checks, issues };
389
- });
390
-
391
- if (remote) {
392
- const remotePromises = entries.map(async (entry) => {
393
- if (!entry.checks.baseUrlValid || !entry.modelName) {
394
- entry.remote = null;
395
- return;
396
- }
397
- try {
398
- const report = await runRemoteHealthCheck(entry.name, entry.provider, entry.modelName, {
399
- timeoutMs,
400
- probeJsonPost: probeJsonPostDep
401
- });
402
- entry.remote = report.remote;
403
- entry.issues.push(...report.issues);
404
- } catch (e) {
405
- entry.remote = null;
406
- entry.issues.push({
407
- code: 'remote-probe-error',
408
- message: `${entry.name} 远程探测异常: ${e.message}`,
409
- suggestion: '检查网络连接与 endpoint'
410
- });
411
- }
412
- });
413
- await Promise.allSettled(remotePromises);
414
- }
415
-
416
- const providers = entries.map((entry) => {
417
- const hasConfigIssue = !entry.checks.baseUrlValid || !entry.checks.apiKeyPresent;
418
- const remoteFailed = entry.remote && !entry.remote.ok;
419
- let statusValue = 'green';
420
- if (remoteFailed) {
421
- statusValue = 'red';
422
- } else if (hasConfigIssue) {
423
- statusValue = 'yellow';
424
- }
425
- return {
426
- provider: entry.name,
427
- isCurrent: entry.name === currentProvider,
428
- status: statusValue,
429
- checks: entry.checks,
430
- remote: entry.remote || null,
431
- issues: entry.issues
432
- };
433
- });
434
-
435
- const summary = {
436
- total: providers.length,
437
- green: providers.filter((p) => p.status === 'green').length,
438
- yellow: providers.filter((p) => p.status === 'yellow').length,
439
- red: providers.filter((p) => p.status === 'red').length
440
- };
441
-
442
- return {
443
- ok: summary.red === 0 && summary.yellow === 0,
444
- currentProvider,
445
- providers,
446
- summary
447
- };
448
- }
449
-
450
- module.exports = {
451
- runRemoteHealthCheck,
452
- buildConfigHealthReport,
453
- buildAllProvidersHealthReport
454
- };
1
+ const { isValidHttpUrl, normalizeBaseUrl } = require('../lib/cli-utils');
2
+ const { buildModelProbeSpecs } = require('../lib/cli-models-utils');
3
+ const { probeJsonPost } = require('../lib/cli-network-utils');
4
+
5
+ const DEFAULT_TIMEOUT_MS = 6000;
6
+ const JSON_RESPONSE_MAX_BYTES = 256 * 1024;
7
+
8
+ function buildRemoteHealthMessage(issueCode, statusCode, detail) {
9
+ if (!issueCode) {
10
+ return '远程模型探测通过:endpoint、鉴权与模型均可用';
11
+ }
12
+
13
+ if (issueCode === 'remote-model-probe-unreachable') {
14
+ return '远程模型接口不可达,请检查 endpoint、网络或 DNS';
15
+ }
16
+
17
+ if (issueCode === 'remote-model-probe-auth-failed') {
18
+ return '远程模型探测鉴权失败(401/403),请检查 API Key、endpoint 与模型权限';
19
+ }
20
+
21
+ if (issueCode === 'remote-model-probe-not-found') {
22
+ return '远程模型探测返回 404,请检查 base_url、接口路径或模型名';
23
+ }
24
+
25
+ if (issueCode === 'remote-model-probe-error') {
26
+ return detail || '远程模型接口返回错误,请检查模型名与账号权限';
27
+ }
28
+
29
+ if (issueCode === 'remote-model-probe-http-error' && statusCode) {
30
+ return `远程模型探测返回 HTTP ${statusCode},请检查 endpoint 与模型`;
31
+ }
32
+
33
+ return '远程模型探测失败,请检查配置与远端服务状态';
34
+ }
35
+
36
+ function extractPayloadErrorMessage(payload) {
37
+ if (!payload || typeof payload !== 'object') {
38
+ return '';
39
+ }
40
+ if (typeof payload.error === 'string' && payload.error.trim()) {
41
+ return payload.error.trim();
42
+ }
43
+ if (!payload.error || typeof payload.error !== 'object') {
44
+ return '';
45
+ }
46
+ if (typeof payload.error.message === 'string' && payload.error.message.trim()) {
47
+ return payload.error.message.trim();
48
+ }
49
+ if (typeof payload.error.code === 'string' && payload.error.code.trim()) {
50
+ return payload.error.code.trim();
51
+ }
52
+ return '';
53
+ }
54
+
55
+ async function runRemoteHealthCheck(providerName, provider, modelName, options = {}) {
56
+ const issues = [];
57
+ const baseUrl = normalizeBaseUrl(provider && provider.base_url ? provider.base_url : '');
58
+ const summary = {
59
+ type: 'remote-health-check',
60
+ provider: typeof providerName === 'string' ? providerName.trim() : '',
61
+ endpoint: baseUrl,
62
+ ok: false,
63
+ statusCode: null,
64
+ message: '',
65
+ checks: {}
66
+ };
67
+
68
+ if (!baseUrl) {
69
+ issues.push({
70
+ code: 'remote-skip-base-url',
71
+ message: '无法进行远程探测:base_url 为空',
72
+ suggestion: '补全 base_url 或关闭远程探测'
73
+ });
74
+ summary.message = '无法进行远程探测:base_url 为空';
75
+ return { issues, remote: summary };
76
+ }
77
+
78
+ if (!isValidHttpUrl(baseUrl)) {
79
+ issues.push({
80
+ code: 'remote-skip-base-url',
81
+ message: '无法进行远程探测:base_url 无效',
82
+ suggestion: '补全 base_url 或关闭远程探测'
83
+ });
84
+ summary.message = '无法进行远程探测:base_url 无效';
85
+ return { issues, remote: summary };
86
+ }
87
+
88
+ const modelProbeSpecs = buildModelProbeSpecs(provider, modelName, baseUrl);
89
+ if (!modelProbeSpecs.length) {
90
+ issues.push({
91
+ code: 'remote-skip-model',
92
+ message: '无法进行远程探测:当前模型未设置',
93
+ suggestion: '补全 model 后重试'
94
+ });
95
+ summary.message = '无法进行远程探测:当前模型未设置';
96
+ return { issues, remote: summary };
97
+ }
98
+
99
+ const requiresAuth = provider && provider.requires_openai_auth !== false;
100
+ const apiKey = typeof provider.preferred_auth_method === 'string'
101
+ ? provider.preferred_auth_method.trim()
102
+ : '';
103
+ const authValue = requiresAuth ? apiKey : (apiKey || '');
104
+ const timeoutMs = Number.isFinite(options.timeoutMs)
105
+ ? Math.max(1000, Number(options.timeoutMs))
106
+ : DEFAULT_TIMEOUT_MS;
107
+ const runProbeJsonPost = typeof options.probeJsonPost === 'function' ? options.probeJsonPost : probeJsonPost;
108
+
109
+ let modelProbeSpec = modelProbeSpecs[0];
110
+ let modelProbe = null;
111
+ for (let index = 0; index < modelProbeSpecs.length; index += 1) {
112
+ const candidate = modelProbeSpecs[index];
113
+ const probeResult = await runProbeJsonPost(candidate.url, candidate.body, {
114
+ apiKey: authValue,
115
+ timeoutMs,
116
+ maxBytes: JSON_RESPONSE_MAX_BYTES
117
+ });
118
+ modelProbeSpec = candidate;
119
+ modelProbe = probeResult;
120
+ const shouldTryNextCandidate = index < modelProbeSpecs.length - 1
121
+ && (!probeResult.ok || probeResult.status === 404);
122
+ if (!shouldTryNextCandidate) {
123
+ break;
124
+ }
125
+ }
126
+
127
+ summary.checks.modelProbe = {
128
+ url: modelProbeSpec.url,
129
+ ok: !!modelProbe.ok,
130
+ status: Number.isFinite(modelProbe.status) ? modelProbe.status : 0,
131
+ durationMs: Number.isFinite(modelProbe.durationMs) ? modelProbe.durationMs : 0
132
+ };
133
+
134
+ if (!modelProbe.ok) {
135
+ issues.push({
136
+ code: 'remote-model-probe-unreachable',
137
+ message: `模型可用性探测失败:${modelProbe.error || '无法连接'}`,
138
+ suggestion: '检查 endpoint、网络或模型接口是否可用'
139
+ });
140
+ } else if (modelProbe.status === 401 || modelProbe.status === 403) {
141
+ issues.push({
142
+ code: 'remote-model-probe-auth-failed',
143
+ message: '模型可用性探测鉴权失败(401/403)',
144
+ suggestion: '检查 API Key 或认证方式'
145
+ });
146
+ } else if (modelProbe.status === 404) {
147
+ issues.push({
148
+ code: 'remote-model-probe-not-found',
149
+ message: '模型可用性探测返回 404',
150
+ suggestion: '检查 base_url、接口路径或模型名'
151
+ });
152
+ } else if (modelProbe.status >= 400) {
153
+ issues.push({
154
+ code: 'remote-model-probe-http-error',
155
+ message: `模型可用性探测返回异常状态: ${modelProbe.status}`,
156
+ suggestion: '检查 endpoint、模型名或服务状态'
157
+ });
158
+ } else {
159
+ let payload = null;
160
+ try {
161
+ payload = modelProbe.body ? JSON.parse(modelProbe.body) : null;
162
+ } catch (e) {
163
+ payload = null;
164
+ }
165
+ const payloadError = extractPayloadErrorMessage(payload);
166
+ if (payloadError) {
167
+ issues.push({
168
+ code: 'remote-model-probe-error',
169
+ message: `模型可用性探测失败:${payloadError}`,
170
+ suggestion: '检查模型名与权限'
171
+ });
172
+ }
173
+ }
174
+
175
+ const primaryIssue = issues[0] || null;
176
+ summary.ok = issues.length === 0;
177
+ summary.statusCode = Number.isFinite(modelProbe.status) && modelProbe.status > 0
178
+ ? modelProbe.status
179
+ : null;
180
+ summary.message = buildRemoteHealthMessage(
181
+ primaryIssue ? primaryIssue.code : '',
182
+ summary.statusCode,
183
+ primaryIssue ? primaryIssue.message : ''
184
+ );
185
+
186
+ return { issues, remote: summary };
187
+ }
188
+
189
+ async function buildConfigHealthReport(params = {}, deps = {}) {
190
+ const issues = [];
191
+ const {
192
+ readConfigOrVirtualDefault,
193
+ readModels
194
+ } = deps;
195
+
196
+ if (typeof readConfigOrVirtualDefault !== 'function') {
197
+ throw new Error('buildConfigHealthReport 缺少 readConfigOrVirtualDefault 依赖');
198
+ }
199
+ if (typeof readModels !== 'function') {
200
+ throw new Error('buildConfigHealthReport 缺少 readModels 依赖');
201
+ }
202
+
203
+ const status = readConfigOrVirtualDefault();
204
+ const config = status.config || {};
205
+
206
+ if (status.isVirtual) {
207
+ const parseFailed = status.errorType === 'parse';
208
+ const readFailed = status.errorType === 'read';
209
+ issues.push({
210
+ code: parseFailed ? 'config-parse-failed' : (readFailed ? 'config-read-failed' : 'config-missing'),
211
+ message: status.reason || (parseFailed
212
+ ? 'config.toml 解析失败'
213
+ : (readFailed ? '读取 config.toml 失败' : '未检测到 config.toml')),
214
+ suggestion: parseFailed
215
+ ? '修复 config.toml 语法错误后重试'
216
+ : (readFailed ? '检查文件权限后重试' : '在模板编辑器中确认应用配置,生成可用的 config.toml')
217
+ });
218
+ if (parseFailed || readFailed) {
219
+ return {
220
+ ok: false,
221
+ issues,
222
+ summary: {
223
+ currentProvider: '',
224
+ currentModel: ''
225
+ },
226
+ remote: null
227
+ };
228
+ }
229
+ }
230
+
231
+ const providerName = typeof config.model_provider === 'string' ? config.model_provider.trim() : '';
232
+ const modelName = typeof config.model === 'string' ? config.model.trim() : '';
233
+ if (!providerName) {
234
+ issues.push({
235
+ code: 'provider-missing',
236
+ message: '当前 provider 未设置',
237
+ suggestion: '在模板中设置 model_provider'
238
+ });
239
+ }
240
+
241
+ if (!modelName) {
242
+ issues.push({
243
+ code: 'model-missing',
244
+ message: '当前模型未设置',
245
+ suggestion: '在模板中设置 model'
246
+ });
247
+ }
248
+
249
+ const providers = config.model_providers && typeof config.model_providers === 'object'
250
+ ? config.model_providers
251
+ : {};
252
+ const provider = providerName ? providers[providerName] : null;
253
+ if (providerName && !provider) {
254
+ issues.push({
255
+ code: 'provider-not-found',
256
+ message: `当前 provider 未在配置中找到: ${providerName}`,
257
+ suggestion: '检查 model_providers 是否包含该 provider 配置块'
258
+ });
259
+ }
260
+
261
+ if (provider && typeof provider === 'object') {
262
+ const baseUrl = typeof provider.base_url === 'string' ? provider.base_url.trim() : '';
263
+ if (!isValidHttpUrl(baseUrl)) {
264
+ issues.push({
265
+ code: 'base-url-invalid',
266
+ message: '当前 provider 的 base_url 无效',
267
+ suggestion: '请设置为 http/https 的完整 URL'
268
+ });
269
+ }
270
+
271
+ const requiresAuth = provider.requires_openai_auth;
272
+ if (requiresAuth !== false) {
273
+ const apiKey = typeof provider.preferred_auth_method === 'string'
274
+ ? provider.preferred_auth_method.trim()
275
+ : '';
276
+ if (!apiKey) {
277
+ issues.push({
278
+ code: 'api-key-missing',
279
+ message: '当前 provider 未配置 API Key',
280
+ suggestion: '在模板中设置 preferred_auth_method'
281
+ });
282
+ }
283
+ }
284
+ }
285
+
286
+ if (modelName) {
287
+ const models = readModels();
288
+ if (!models.includes(modelName)) {
289
+ issues.push({
290
+ code: 'model-unavailable',
291
+ message: `模型未在可用列表中找到: ${modelName}`,
292
+ suggestion: '在模型列表中添加该模型或切换到已有模型'
293
+ });
294
+ }
295
+ }
296
+
297
+ let remote = null;
298
+ if (params.remote) {
299
+ if (!provider) {
300
+ issues.push({
301
+ code: 'remote-skip-provider',
302
+ message: '无法进行远程探测:provider 未找到',
303
+ suggestion: '检查 model_provider 配置或关闭远程探测'
304
+ });
305
+ remote = {
306
+ type: 'remote-health-check',
307
+ provider: providerName,
308
+ endpoint: '',
309
+ ok: false,
310
+ statusCode: null,
311
+ message: '无法进行远程探测:provider 未找到',
312
+ checks: {}
313
+ };
314
+ } else {
315
+ const remoteReport = await runRemoteHealthCheck(providerName, provider, modelName, {
316
+ timeoutMs: Number.isFinite(params.timeoutMs) ? Number(params.timeoutMs) : undefined,
317
+ probeJsonPost: deps.probeJsonPost
318
+ });
319
+ issues.push(...remoteReport.issues);
320
+ remote = remoteReport.remote;
321
+ }
322
+ }
323
+
324
+ return {
325
+ ok: issues.length === 0,
326
+ issues,
327
+ summary: {
328
+ currentProvider: providerName,
329
+ currentModel: modelName
330
+ },
331
+ remote
332
+ };
333
+ }
334
+
335
+ async function buildAllProvidersHealthReport(params = {}, deps = {}) {
336
+ const {
337
+ readConfigOrVirtualDefault,
338
+ readCurrentModels,
339
+ probeJsonPost: probeJsonPostDep
340
+ } = deps;
341
+
342
+ if (typeof readConfigOrVirtualDefault !== 'function') {
343
+ throw new Error('buildAllProvidersHealthReport 缺少 readConfigOrVirtualDefault 依赖');
344
+ }
345
+
346
+ const status = readConfigOrVirtualDefault();
347
+ const config = status.config || {};
348
+ const providerEntries = config.model_providers && typeof config.model_providers === 'object'
349
+ ? Object.entries(config.model_providers)
350
+ : [];
351
+ const currentProvider = typeof config.model_provider === 'string' ? config.model_provider.trim() : '';
352
+ const currentModels = typeof readCurrentModels === 'function' ? readCurrentModels() : {};
353
+
354
+ if (!providerEntries.length) {
355
+ return {
356
+ ok: true,
357
+ currentProvider,
358
+ providers: [],
359
+ summary: { total: 0, green: 0, yellow: 0, red: 0 }
360
+ };
361
+ }
362
+
363
+ const remote = !!(params && params.remote);
364
+ const timeoutMs = Number.isFinite(params && params.timeoutMs) ? Number(params.timeoutMs) : DEFAULT_TIMEOUT_MS;
365
+
366
+ const entries = providerEntries.map(([name, provider]) => {
367
+ const baseUrl = typeof provider.base_url === 'string' ? provider.base_url.trim() : '';
368
+ const requiresAuth = provider.requires_openai_auth !== false;
369
+ const apiKey = typeof provider.preferred_auth_method === 'string'
370
+ ? provider.preferred_auth_method.trim() : '';
371
+ const modelName = currentModels[name] || (name === currentProvider ? (config.model || '').trim() : '');
372
+
373
+ const checks = {
374
+ baseUrlValid: isValidHttpUrl(baseUrl),
375
+ apiKeyPresent: !requiresAuth || !!apiKey
376
+ };
377
+
378
+ const issues = [];
379
+ if (!baseUrl) {
380
+ issues.push({ code: 'base-url-empty', message: `${name} 的 base_url 为空`, suggestion: '设置 base_url' });
381
+ } else if (!checks.baseUrlValid) {
382
+ issues.push({ code: 'base-url-invalid', message: `${name} 的 base_url 无效`, suggestion: '设置为 http/https 完整 URL' });
383
+ }
384
+ if (!checks.apiKeyPresent) {
385
+ issues.push({ code: 'api-key-missing', message: `${name} 未配置 API Key`, suggestion: '设置 preferred_auth_method' });
386
+ }
387
+
388
+ return { name, provider, baseUrl, modelName, checks, issues };
389
+ });
390
+
391
+ if (remote) {
392
+ const remotePromises = entries.map(async (entry) => {
393
+ if (!entry.checks.baseUrlValid || !entry.modelName) {
394
+ entry.remote = null;
395
+ return;
396
+ }
397
+ try {
398
+ const report = await runRemoteHealthCheck(entry.name, entry.provider, entry.modelName, {
399
+ timeoutMs,
400
+ probeJsonPost: probeJsonPostDep
401
+ });
402
+ entry.remote = report.remote;
403
+ entry.issues.push(...report.issues);
404
+ } catch (e) {
405
+ entry.remote = null;
406
+ entry.issues.push({
407
+ code: 'remote-probe-error',
408
+ message: `${entry.name} 远程探测异常: ${e.message}`,
409
+ suggestion: '检查网络连接与 endpoint'
410
+ });
411
+ }
412
+ });
413
+ await Promise.allSettled(remotePromises);
414
+ }
415
+
416
+ const providers = entries.map((entry) => {
417
+ const hasConfigIssue = !entry.checks.baseUrlValid || !entry.checks.apiKeyPresent;
418
+ const remoteFailed = entry.remote && !entry.remote.ok;
419
+ let statusValue = 'green';
420
+ if (remoteFailed) {
421
+ statusValue = 'red';
422
+ } else if (hasConfigIssue) {
423
+ statusValue = 'yellow';
424
+ }
425
+ return {
426
+ provider: entry.name,
427
+ isCurrent: entry.name === currentProvider,
428
+ status: statusValue,
429
+ checks: entry.checks,
430
+ remote: entry.remote || null,
431
+ issues: entry.issues
432
+ };
433
+ });
434
+
435
+ const summary = {
436
+ total: providers.length,
437
+ green: providers.filter((p) => p.status === 'green').length,
438
+ yellow: providers.filter((p) => p.status === 'yellow').length,
439
+ red: providers.filter((p) => p.status === 'red').length
440
+ };
441
+
442
+ return {
443
+ ok: summary.red === 0 && summary.yellow === 0,
444
+ currentProvider,
445
+ providers,
446
+ summary
447
+ };
448
+ }
449
+
450
+ module.exports = {
451
+ runRemoteHealthCheck,
452
+ buildConfigHealthReport,
453
+ buildAllProvidersHealthReport
454
+ };