codexmate 0.0.21 → 0.0.23

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 (115) hide show
  1. package/README.md +390 -284
  2. package/README.zh.md +322 -0
  3. package/cli/agents-files.js +224 -162
  4. package/cli/archive-helpers.js +446 -446
  5. package/cli/auth-profiles.js +359 -359
  6. package/cli/builtin-proxy.js +1044 -580
  7. package/cli/claude-proxy.js +998 -998
  8. package/cli/config-bootstrap.js +384 -384
  9. package/cli/config-health.js +338 -338
  10. package/cli/openai-bridge.js +950 -0
  11. package/cli/openclaw-config.js +629 -629
  12. package/cli/session-usage.concurrent.js +28 -0
  13. package/cli/session-usage.js +112 -0
  14. package/cli/session-usage.models.js +176 -0
  15. package/cli/skills.js +1141 -1141
  16. package/cli/zip-commands.js +510 -510
  17. package/cli.js +13214 -13129
  18. package/lib/cli-file-utils.js +151 -151
  19. package/lib/cli-models-utils.js +419 -419
  20. package/lib/cli-network-utils.js +164 -164
  21. package/lib/cli-path-utils.js +69 -69
  22. package/lib/cli-session-utils.js +121 -121
  23. package/lib/cli-sessions.js +386 -386
  24. package/lib/cli-utils.js +155 -155
  25. package/lib/download-artifacts.js +77 -77
  26. package/lib/mcp-stdio.js +440 -440
  27. package/lib/task-orchestrator.js +869 -869
  28. package/lib/text-diff.js +303 -303
  29. package/lib/workflow-engine.js +340 -340
  30. package/package.json +74 -74
  31. package/res/json5.min.js +1 -1
  32. package/res/logo.png +0 -0
  33. package/res/vue.global.prod.js +13 -13
  34. package/web-ui/app.js +626 -530
  35. package/web-ui/index.html +34 -33
  36. package/web-ui/logic.agents-diff.mjs +386 -386
  37. package/web-ui/logic.claude.mjs +168 -168
  38. package/web-ui/logic.mjs +5 -5
  39. package/web-ui/logic.runtime.mjs +128 -124
  40. package/web-ui/logic.sessions.mjs +614 -581
  41. package/web-ui/modules/api.mjs +90 -90
  42. package/web-ui/modules/app.computed.dashboard.mjs +126 -113
  43. package/web-ui/modules/app.computed.index.mjs +17 -15
  44. package/web-ui/modules/app.computed.main-tabs.mjs +202 -195
  45. package/web-ui/modules/app.computed.session.mjs +653 -507
  46. package/web-ui/modules/app.constants.mjs +15 -15
  47. package/web-ui/modules/app.methods.agents.mjs +544 -493
  48. package/web-ui/modules/app.methods.claude-config.mjs +174 -174
  49. package/web-ui/modules/app.methods.codex-config.mjs +795 -640
  50. package/web-ui/modules/app.methods.index.mjs +92 -88
  51. package/web-ui/modules/app.methods.install.mjs +177 -149
  52. package/web-ui/modules/app.methods.navigation.mjs +662 -619
  53. package/web-ui/modules/app.methods.openclaw-core.mjs +814 -814
  54. package/web-ui/modules/app.methods.openclaw-editing.mjs +372 -372
  55. package/web-ui/modules/app.methods.openclaw-persist.mjs +369 -369
  56. package/web-ui/modules/app.methods.providers.mjs +404 -363
  57. package/web-ui/modules/app.methods.runtime.mjs +323 -323
  58. package/web-ui/modules/app.methods.session-actions.mjs +537 -520
  59. package/web-ui/modules/app.methods.session-browser.mjs +691 -626
  60. package/web-ui/modules/app.methods.session-timeline.mjs +448 -448
  61. package/web-ui/modules/app.methods.session-trash.mjs +422 -422
  62. package/web-ui/modules/app.methods.startup-claude.mjs +417 -412
  63. package/web-ui/modules/app.methods.task-orchestration.mjs +556 -471
  64. package/web-ui/modules/config-mode.computed.mjs +126 -126
  65. package/web-ui/modules/config-template-confirm-pref.mjs +33 -0
  66. package/web-ui/modules/i18n.mjs +1823 -0
  67. package/web-ui/modules/plugins.computed.mjs +3 -0
  68. package/web-ui/modules/plugins.methods.mjs +3 -0
  69. package/web-ui/modules/plugins.storage.mjs +11 -0
  70. package/web-ui/modules/sessions-filters-url.mjs +85 -0
  71. package/web-ui/modules/skills.computed.mjs +107 -107
  72. package/web-ui/modules/skills.methods.mjs +481 -481
  73. package/web-ui/partials/index/layout-footer.html +13 -13
  74. package/web-ui/partials/index/layout-header.html +461 -402
  75. package/web-ui/partials/index/modal-config-template-agents.html +175 -125
  76. package/web-ui/partials/index/modal-confirm-toast.html +32 -32
  77. package/web-ui/partials/index/modal-health-check.html +72 -72
  78. package/web-ui/partials/index/modal-openclaw-config.html +280 -280
  79. package/web-ui/partials/index/modal-skills.html +200 -184
  80. package/web-ui/partials/index/modals-basic.html +165 -156
  81. package/web-ui/partials/index/panel-config-claude.html +159 -126
  82. package/web-ui/partials/index/panel-config-codex.html +255 -237
  83. package/web-ui/partials/index/panel-config-openclaw.html +78 -78
  84. package/web-ui/partials/index/panel-docs.html +147 -130
  85. package/web-ui/partials/index/panel-market.html +174 -174
  86. package/web-ui/partials/index/panel-orchestration.html +388 -397
  87. package/web-ui/partials/index/panel-plugins.html +273 -0
  88. package/web-ui/partials/index/panel-sessions.html +298 -292
  89. package/web-ui/partials/index/panel-settings.html +258 -190
  90. package/web-ui/partials/index/panel-usage.html +353 -213
  91. package/web-ui/session-helpers.mjs +573 -559
  92. package/web-ui/source-bundle.cjs +233 -233
  93. package/web-ui/styles/base-theme.css +264 -271
  94. package/web-ui/styles/controls-forms.css +369 -360
  95. package/web-ui/styles/docs-panel.css +247 -182
  96. package/web-ui/styles/feedback.css +108 -108
  97. package/web-ui/styles/health-check-dialog.css +144 -144
  98. package/web-ui/styles/layout-shell.css +602 -376
  99. package/web-ui/styles/modals-core.css +464 -464
  100. package/web-ui/styles/navigation-panels.css +390 -348
  101. package/web-ui/styles/openclaw-structured.css +266 -266
  102. package/web-ui/styles/plugins-panel.css +523 -0
  103. package/web-ui/styles/responsive.css +456 -450
  104. package/web-ui/styles/sessions-list.css +400 -400
  105. package/web-ui/styles/sessions-preview.css +411 -411
  106. package/web-ui/styles/sessions-toolbar-trash.css +268 -243
  107. package/web-ui/styles/sessions-usage.css +879 -628
  108. package/web-ui/styles/settings-panel.css +166 -0
  109. package/web-ui/styles/skills-list.css +303 -296
  110. package/web-ui/styles/skills-market.css +406 -335
  111. package/web-ui/styles/task-orchestration.css +822 -776
  112. package/web-ui/styles/titles-cards.css +408 -408
  113. package/web-ui/styles.css +20 -18
  114. package/web-ui.html +17 -17
  115. package/README.en.md +0 -349
@@ -1,471 +1,556 @@
1
- function createDefaultTaskOrchestrationState() {
2
- return {
3
- loading: false,
4
- planning: false,
5
- running: false,
6
- queueAdding: false,
7
- queueStarting: false,
8
- retrying: false,
9
- target: '',
10
- title: '',
11
- notes: '',
12
- followUpsText: '',
13
- workflowIdsText: '',
14
- selectedEngine: 'codex',
15
- allowWrite: false,
16
- dryRun: false,
17
- concurrency: 2,
18
- autoFixRounds: 1,
19
- plan: null,
20
- planIssues: [],
21
- planWarnings: [],
22
- overviewWarnings: [],
23
- workflows: [],
24
- queue: [],
25
- runs: [],
26
- selectedRunId: '',
27
- workspaceTab: 'queue',
28
- selectedRunDetail: null,
29
- selectedRunLoading: false,
30
- selectedRunError: '',
31
- detailRequestToken: 0,
32
- lastLoadedAt: '',
33
- lastError: ''
34
- };
35
- }
36
-
37
- function normalizeLines(text) {
38
- return String(text || '')
39
- .split(/\r?\n/g)
40
- .map((item) => item.trim())
41
- .filter(Boolean);
42
- }
43
-
44
- function normalizePositiveInteger(value, fallback, min = 1, max = 8) {
45
- const numeric = Number.parseInt(String(value), 10);
46
- if (!Number.isFinite(numeric)) {
47
- return fallback;
48
- }
49
- return Math.min(max, Math.max(min, Math.floor(numeric)));
50
- }
51
-
52
- function normalizeTaskStatusTone(status) {
53
- const normalized = String(status || '').trim().toLowerCase();
54
- if (normalized === 'success' || normalized === 'completed') return 'success';
55
- if (normalized === 'failed' || normalized === 'blocked' || normalized === 'cancelled') return 'error';
56
- if (normalized === 'running' || normalized === 'queued') return 'warn';
57
- return 'neutral';
58
- }
59
-
60
- function isActiveStatus(status) {
61
- const normalized = String(status || '').trim().toLowerCase();
62
- return normalized === 'running' || normalized === 'queued';
63
- }
64
-
65
- export function createTaskOrchestrationMethods(options = {}) {
66
- const { api } = options;
67
-
68
- return {
69
- ensureTaskOrchestrationState() {
70
- const current = this.taskOrchestration;
71
- if (current && typeof current === 'object' && !Array.isArray(current)) {
72
- const defaults = createDefaultTaskOrchestrationState();
73
- for (const [key, value] of Object.entries(defaults)) {
74
- if (typeof current[key] === 'undefined') {
75
- current[key] = value;
76
- }
77
- }
78
- return current;
79
- }
80
- this.taskOrchestration = createDefaultTaskOrchestrationState();
81
- return this.taskOrchestration;
82
- },
83
-
84
- buildTaskOrchestrationRequest() {
85
- const state = this.ensureTaskOrchestrationState();
86
- return {
87
- title: String(state.title || '').trim(),
88
- target: String(state.target || '').trim(),
89
- notes: String(state.notes || '').trim(),
90
- followUps: normalizeLines(state.followUpsText),
91
- workflowIds: normalizeLines(state.workflowIdsText),
92
- engine: String(state.selectedEngine || 'codex').trim().toLowerCase() === 'workflow' ? 'workflow' : 'codex',
93
- allowWrite: state.allowWrite === true,
94
- dryRun: state.dryRun === true,
95
- concurrency: normalizePositiveInteger(state.concurrency, 2, 1, 8),
96
- autoFixRounds: normalizePositiveInteger(state.autoFixRounds, 1, 0, 5)
97
- };
98
- },
99
-
100
- taskRunStatusTone(status) {
101
- return normalizeTaskStatusTone(status);
102
- },
103
-
104
- isTaskRunActive(status) {
105
- return isActiveStatus(status);
106
- },
107
-
108
- formatTaskNodeDependencies(node) {
109
- const dependsOn = Array.isArray(node && node.dependsOn) ? node.dependsOn : [];
110
- return dependsOn.length > 0 ? dependsOn.join(', ') : '';
111
- },
112
-
113
- formatTaskNodeLogs(logs) {
114
- if (!Array.isArray(logs) || logs.length === 0) {
115
- return '(no logs)';
116
- }
117
- return logs.map((item) => `${item && item.at ? item.at : ''} ${item && item.level ? item.level : ''} ${item && item.message ? item.message : ''}`.trim()).join('\n');
118
- },
119
-
120
- appendTaskWorkflowId(workflowId) {
121
- const state = this.ensureTaskOrchestrationState();
122
- const normalizedWorkflowId = typeof workflowId === 'string' ? workflowId.trim() : '';
123
- if (!normalizedWorkflowId) {
124
- return;
125
- }
126
- const nextWorkflowIds = normalizeLines(state.workflowIdsText);
127
- if (!nextWorkflowIds.includes(normalizedWorkflowId)) {
128
- nextWorkflowIds.push(normalizedWorkflowId);
129
- }
130
- state.selectedEngine = 'workflow';
131
- state.workflowIdsText = nextWorkflowIds.join('\n');
132
- },
133
-
134
- async loadTaskOrchestrationOverview(options = {}) {
135
- const state = this.ensureTaskOrchestrationState();
136
- if (state.loading && !options.forceRefresh) {
137
- return null;
138
- }
139
- const silent = !!options.silent;
140
- state.loading = true;
141
- state.lastError = '';
142
- try {
143
- const res = await api('task-overview', {
144
- queueLimit: 20,
145
- runLimit: 20
146
- });
147
- if (res && res.error) {
148
- state.lastError = res.error;
149
- if (!silent) {
150
- this.showMessage(res.error, 'error');
151
- }
152
- return res;
153
- }
154
- state.workflows = Array.isArray(res && res.workflows) ? res.workflows : [];
155
- state.queue = Array.isArray(res && res.queue) ? res.queue : [];
156
- state.runs = Array.isArray(res && res.runs) ? res.runs : [];
157
- state.overviewWarnings = Array.isArray(res && res.warnings) ? res.warnings : [];
158
- state.lastLoadedAt = new Date().toISOString();
159
- if (!state.selectedRunId && state.runs.length > 0) {
160
- state.selectedRunId = state.runs[0].runId || '';
161
- }
162
- const shouldRefreshSelectedDetail = !!state.selectedRunId
163
- && (options.includeDetail !== false
164
- || (state.selectedRunDetail && this.isTaskRunActive(state.selectedRunDetail && state.selectedRunDetail.run && state.selectedRunDetail.run.status)));
165
- if (shouldRefreshSelectedDetail) {
166
- await this.loadTaskRunDetail(state.selectedRunId, { silent: true });
167
- }
168
- this.syncTaskOrchestrationPolling();
169
- return res;
170
- } catch (error) {
171
- const message = error && error.message ? error.message : '任务编排概览加载失败';
172
- state.lastError = message;
173
- if (!silent) {
174
- this.showMessage(message, 'error');
175
- }
176
- return { error: message };
177
- } finally {
178
- state.loading = false;
179
- }
180
- },
181
-
182
- async previewTaskPlan(options = {}) {
183
- const state = this.ensureTaskOrchestrationState();
184
- if (state.planning) {
185
- return null;
186
- }
187
- state.planning = true;
188
- try {
189
- const res = await api('task-plan', this.buildTaskOrchestrationRequest());
190
- state.plan = res && res.plan ? res.plan : null;
191
- state.planIssues = Array.isArray(res && res.issues) ? res.issues : [];
192
- state.planWarnings = Array.isArray(res && res.warnings) ? res.warnings : [];
193
- if (res && res.error) {
194
- if (!options.silent) {
195
- this.showMessage(res.error, 'error');
196
- }
197
- return res;
198
- }
199
- if (!options.silent) {
200
- this.showMessage('任务计划已更新', 'success');
201
- }
202
- return res;
203
- } catch (error) {
204
- const message = error && error.message ? error.message : '任务计划生成失败';
205
- state.plan = null;
206
- state.planIssues = [];
207
- state.planWarnings = [];
208
- if (!options.silent) {
209
- this.showMessage(message, 'error');
210
- }
211
- return { error: message };
212
- } finally {
213
- state.planning = false;
214
- }
215
- },
216
-
217
- async runTaskOrchestration() {
218
- const state = this.ensureTaskOrchestrationState();
219
- if (state.running) {
220
- return null;
221
- }
222
- state.running = true;
223
- try {
224
- const res = await api('task-run', {
225
- ...this.buildTaskOrchestrationRequest(),
226
- detach: true
227
- });
228
- if (res && res.error) {
229
- this.showMessage(res.error, 'error');
230
- return res;
231
- }
232
- state.selectedRunId = res.runId || state.selectedRunId;
233
- await this.loadTaskOrchestrationOverview({ silent: true, includeDetail: false });
234
- if (state.selectedRunId) {
235
- await this.loadTaskRunDetail(state.selectedRunId, { silent: true });
236
- }
237
- this.syncTaskOrchestrationPolling();
238
- this.showMessage(`任务已启动: ${res.runId || res.taskId || 'unknown'}`, 'success');
239
- return res;
240
- } catch (error) {
241
- const message = error && error.message ? error.message : '任务启动失败';
242
- this.showMessage(message, 'error');
243
- return { error: message };
244
- } finally {
245
- state.running = false;
246
- }
247
- },
248
-
249
- async addTaskOrchestrationToQueue() {
250
- const state = this.ensureTaskOrchestrationState();
251
- if (state.queueAdding) {
252
- return null;
253
- }
254
- state.queueAdding = true;
255
- try {
256
- const res = await api('task-queue-add', this.buildTaskOrchestrationRequest());
257
- if (res && res.error) {
258
- this.showMessage(res.error, 'error');
259
- return res;
260
- }
261
- await this.loadTaskOrchestrationOverview({ silent: true, includeDetail: false });
262
- this.showMessage(`已加入队列: ${res && res.task ? res.task.taskId : ''}`.trim(), 'success');
263
- return res;
264
- } catch (error) {
265
- const message = error && error.message ? error.message : '加入队列失败';
266
- this.showMessage(message, 'error');
267
- return { error: message };
268
- } finally {
269
- state.queueAdding = false;
270
- }
271
- },
272
-
273
- async startTaskQueueRunner() {
274
- const state = this.ensureTaskOrchestrationState();
275
- if (state.queueStarting) {
276
- return null;
277
- }
278
- state.queueStarting = true;
279
- try {
280
- const res = await api('task-queue-start', { detach: true });
281
- if (res && res.error) {
282
- this.showMessage(res.error, 'error');
283
- return res;
284
- }
285
- await this.loadTaskOrchestrationOverview({ silent: true, includeDetail: false });
286
- this.syncTaskOrchestrationPolling();
287
- this.showMessage(res && res.alreadyRunning ? '队列执行器已在运行' : '队列执行器已启动', 'success');
288
- return res;
289
- } catch (error) {
290
- const message = error && error.message ? error.message : '启动队列失败';
291
- this.showMessage(message, 'error');
292
- return { error: message };
293
- } finally {
294
- state.queueStarting = false;
295
- }
296
- },
297
-
298
- async loadTaskRunDetail(runId, options = {}) {
299
- const state = this.ensureTaskOrchestrationState();
300
- const normalizedRunId = String(runId || '').trim();
301
- if (!normalizedRunId) {
302
- state.selectedRunDetail = null;
303
- state.selectedRunId = '';
304
- state.selectedRunError = '';
305
- this.syncTaskOrchestrationPolling();
306
- return null;
307
- }
308
- const requestToken = (state.detailRequestToken || 0) + 1;
309
- const previousRunId = state.selectedRunId;
310
- state.detailRequestToken = requestToken;
311
- state.selectedRunLoading = true;
312
- state.selectedRunId = normalizedRunId;
313
- if (options.switchToDetail === true || !options.silent) {
314
- state.workspaceTab = 'detail';
315
- }
316
- state.selectedRunError = '';
317
- if (previousRunId !== normalizedRunId) {
318
- state.selectedRunDetail = null;
319
- }
320
- try {
321
- const res = await api('task-run-detail', { runId: normalizedRunId });
322
- if (state.detailRequestToken !== requestToken) {
323
- return res;
324
- }
325
- if (res && res.error) {
326
- state.selectedRunDetail = null;
327
- state.selectedRunError = res.error;
328
- if (!options.silent) {
329
- this.showMessage(res.error, 'error');
330
- }
331
- return res;
332
- }
333
- state.selectedRunDetail = res;
334
- state.selectedRunError = '';
335
- this.syncTaskOrchestrationPolling();
336
- return res;
337
- } catch (error) {
338
- const message = error && error.message ? error.message : '加载任务详情失败';
339
- if (state.detailRequestToken === requestToken) {
340
- state.selectedRunDetail = null;
341
- state.selectedRunError = message;
342
- }
343
- if (!options.silent) {
344
- this.showMessage(message, 'error');
345
- }
346
- return { error: message };
347
- } finally {
348
- if (state.detailRequestToken === requestToken) {
349
- state.selectedRunLoading = false;
350
- }
351
- }
352
- },
353
-
354
- async selectTaskRun(runId) {
355
- return this.loadTaskRunDetail(runId, { silent: false, switchToDetail: true });
356
- },
357
-
358
- async retryTaskRunFromUi(runId) {
359
- const state = this.ensureTaskOrchestrationState();
360
- const normalizedRunId = String(runId || '').trim();
361
- if (!normalizedRunId || state.retrying) {
362
- return null;
363
- }
364
- state.retrying = true;
365
- try {
366
- const res = await api('task-retry', { runId: normalizedRunId, detach: true });
367
- if (res && res.error) {
368
- this.showMessage(res.error, 'error');
369
- return res;
370
- }
371
- state.selectedRunId = res.runId || state.selectedRunId;
372
- await this.loadTaskOrchestrationOverview({ silent: true, includeDetail: false });
373
- if (state.selectedRunId) {
374
- await this.loadTaskRunDetail(state.selectedRunId, { silent: true });
375
- }
376
- this.syncTaskOrchestrationPolling();
377
- this.showMessage(`已开始重试: ${res.runId || normalizedRunId}`, 'success');
378
- return res;
379
- } catch (error) {
380
- const message = error && error.message ? error.message : '重试任务失败';
381
- this.showMessage(message, 'error');
382
- return { error: message };
383
- } finally {
384
- state.retrying = false;
385
- }
386
- },
387
-
388
- async cancelTaskRunFromUi(target) {
389
- const normalizedTarget = String(target || '').trim();
390
- if (!normalizedTarget) {
391
- return null;
392
- }
393
- try {
394
- const res = await api('task-cancel', { target: normalizedTarget });
395
- if (res && res.error) {
396
- this.showMessage(res.error, 'error');
397
- return res;
398
- }
399
- await this.loadTaskOrchestrationOverview({ silent: true, includeDetail: false });
400
- if (this.taskOrchestration.selectedRunId) {
401
- await this.loadTaskRunDetail(this.taskOrchestration.selectedRunId, { silent: true });
402
- }
403
- this.syncTaskOrchestrationPolling();
404
- this.showMessage('已发出取消请求', 'success');
405
- return res;
406
- } catch (error) {
407
- const message = error && error.message ? error.message : '取消任务失败';
408
- this.showMessage(message, 'error');
409
- return { error: message };
410
- }
411
- },
412
-
413
- taskOrchestrationHasLiveActivity() {
414
- const state = this.ensureTaskOrchestrationState();
415
- const queue = Array.isArray(state.queue) ? state.queue : [];
416
- if (queue.some((item) => isActiveStatus(item && (item.status || item.runStatus)))) {
417
- return true;
418
- }
419
- const detail = state.selectedRunDetail;
420
- const selectedStatus = detail && detail.run ? detail.run.status : '';
421
- if (isActiveStatus(selectedStatus)) {
422
- return true;
423
- }
424
- const runs = Array.isArray(state.runs) ? state.runs : [];
425
- return runs.some((item) => isActiveStatus(item && item.status));
426
- },
427
-
428
- stopTaskOrchestrationPolling() {
429
- if (this._taskOrchestrationPollTimer) {
430
- clearTimeout(this._taskOrchestrationPollTimer);
431
- this._taskOrchestrationPollTimer = 0;
432
- }
433
- },
434
-
435
- syncTaskOrchestrationPolling() {
436
- this.stopTaskOrchestrationPolling();
437
- if (this.mainTab !== 'orchestration') {
438
- return;
439
- }
440
- if (!this.taskOrchestrationHasLiveActivity()) {
441
- return;
442
- }
443
- this._taskOrchestrationPollTimer = setTimeout(async () => {
444
- this._taskOrchestrationPollTimer = 0;
445
- if (this.mainTab !== 'orchestration') {
446
- return;
447
- }
448
- await this.loadTaskOrchestrationOverview({ silent: true, includeDetail: true });
449
- }, 4000);
450
- },
451
-
452
- resetTaskOrchestrationDraft() {
453
- const state = this.ensureTaskOrchestrationState();
454
- state.target = '';
455
- state.title = '';
456
- state.notes = '';
457
- state.followUpsText = '';
458
- state.workflowIdsText = '';
459
- state.selectedEngine = 'codex';
460
- state.allowWrite = false;
461
- state.dryRun = false;
462
- state.concurrency = 2;
463
- state.autoFixRounds = 1;
464
- state.plan = null;
465
- state.planIssues = [];
466
- state.planWarnings = [];
467
- state.lastError = '';
468
- this.syncTaskOrchestrationPolling();
469
- }
470
- };
471
- }
1
+ function createDefaultTaskOrchestrationState() {
2
+ return {
3
+ loading: false,
4
+ planning: false,
5
+ running: false,
6
+ queueAdding: false,
7
+ queueStarting: false,
8
+ retrying: false,
9
+ target: '',
10
+ title: '',
11
+ notes: '',
12
+ followUpsText: '',
13
+ workflowIdsText: '',
14
+ selectedEngine: 'codex',
15
+ runMode: 'write',
16
+ concurrency: 2,
17
+ autoFixRounds: 1,
18
+ plan: null,
19
+ planFingerprint: '',
20
+ planIssues: [],
21
+ planWarnings: [],
22
+ overviewWarnings: [],
23
+ workflows: [],
24
+ queue: [],
25
+ runs: [],
26
+ selectedRunId: '',
27
+ workspaceTab: 'queue',
28
+ selectedRunDetail: null,
29
+ selectedRunLoading: false,
30
+ selectedRunError: '',
31
+ detailRequestToken: 0,
32
+ lastLoadedAt: '',
33
+ lastError: ''
34
+ };
35
+ }
36
+
37
+ function normalizeLines(text) {
38
+ return String(text || '')
39
+ .split(/\r?\n/g)
40
+ .map((item) => item.trim())
41
+ .filter(Boolean);
42
+ }
43
+
44
+ function normalizePositiveInteger(value, fallback, min = 1, max = 8) {
45
+ const numeric = Number.parseInt(String(value), 10);
46
+ if (!Number.isFinite(numeric)) {
47
+ return fallback;
48
+ }
49
+ return Math.min(max, Math.max(min, Math.floor(numeric)));
50
+ }
51
+
52
+ function normalizeTaskStatusTone(status) {
53
+ const normalized = String(status || '').trim().toLowerCase();
54
+ if (normalized === 'success' || normalized === 'completed') return 'success';
55
+ if (normalized === 'failed' || normalized === 'blocked' || normalized === 'cancelled') return 'error';
56
+ if (normalized === 'running' || normalized === 'queued') return 'warn';
57
+ return 'neutral';
58
+ }
59
+
60
+ function isActiveStatus(status) {
61
+ const normalized = String(status || '').trim().toLowerCase();
62
+ return normalized === 'running' || normalized === 'queued';
63
+ }
64
+
65
+ function normalizeTaskRunMode(value) {
66
+ const normalized = String(value || '').trim().toLowerCase();
67
+ if (normalized === 'read') return 'read';
68
+ if (normalized === 'dry-run' || normalized === 'dryrun' || normalized === 'plan') return 'dry-run';
69
+ return 'write';
70
+ }
71
+
72
+ function buildRunModeFlags(runMode) {
73
+ const normalized = normalizeTaskRunMode(runMode);
74
+ return {
75
+ runMode: normalized,
76
+ allowWrite: normalized === 'write',
77
+ dryRun: normalized === 'dry-run'
78
+ };
79
+ }
80
+
81
+ export function createTaskOrchestrationMethods(options = {}) {
82
+ const { api } = options;
83
+
84
+ return {
85
+ ensureTaskOrchestrationState() {
86
+ const current = this.taskOrchestration;
87
+ if (current && typeof current === 'object' && !Array.isArray(current)) {
88
+ const defaults = createDefaultTaskOrchestrationState();
89
+ for (const [key, value] of Object.entries(defaults)) {
90
+ if (typeof current[key] === 'undefined') {
91
+ current[key] = value;
92
+ }
93
+ }
94
+ current.runMode = normalizeTaskRunMode(current.runMode);
95
+ return current;
96
+ }
97
+ this.taskOrchestration = createDefaultTaskOrchestrationState();
98
+ return this.taskOrchestration;
99
+ },
100
+
101
+ buildTaskOrchestrationRequest() {
102
+ const state = this.ensureTaskOrchestrationState();
103
+ const flags = buildRunModeFlags(state.runMode);
104
+ return {
105
+ title: String(state.title || '').trim(),
106
+ target: String(state.target || '').trim(),
107
+ notes: String(state.notes || '').trim(),
108
+ followUps: normalizeLines(state.followUpsText),
109
+ workflowIds: normalizeLines(state.workflowIdsText),
110
+ engine: String(state.selectedEngine || 'codex').trim().toLowerCase() === 'workflow' ? 'workflow' : 'codex',
111
+ allowWrite: flags.allowWrite,
112
+ dryRun: flags.dryRun,
113
+ concurrency: normalizePositiveInteger(state.concurrency, 2, 1, 8),
114
+ autoFixRounds: normalizePositiveInteger(state.autoFixRounds, 1, 0, 5)
115
+ };
116
+ },
117
+
118
+ buildTaskOrchestrationFingerprint() {
119
+ const req = this.buildTaskOrchestrationRequest();
120
+ return JSON.stringify({
121
+ title: req.title,
122
+ target: req.target,
123
+ notes: req.notes,
124
+ followUps: req.followUps,
125
+ workflowIds: req.workflowIds,
126
+ engine: req.engine,
127
+ allowWrite: req.allowWrite,
128
+ dryRun: req.dryRun,
129
+ concurrency: req.concurrency,
130
+ autoFixRounds: req.autoFixRounds
131
+ });
132
+ },
133
+
134
+ taskRunStatusTone(status) {
135
+ return normalizeTaskStatusTone(status);
136
+ },
137
+
138
+ isTaskRunActive(status) {
139
+ return isActiveStatus(status);
140
+ },
141
+
142
+ formatTaskNodeDependencies(node) {
143
+ const dependsOn = Array.isArray(node && node.dependsOn) ? node.dependsOn : [];
144
+ return dependsOn.length > 0 ? dependsOn.join(', ') : '无';
145
+ },
146
+
147
+ formatTaskNodeLogs(logs) {
148
+ if (!Array.isArray(logs) || logs.length === 0) {
149
+ return '(no logs)';
150
+ }
151
+ return logs.map((item) => `${item && item.at ? item.at : ''} ${item && item.level ? item.level : ''} ${item && item.message ? item.message : ''}`.trim()).join('\n');
152
+ },
153
+
154
+ appendTaskWorkflowId(workflowId) {
155
+ const state = this.ensureTaskOrchestrationState();
156
+ const normalizedWorkflowId = typeof workflowId === 'string' ? workflowId.trim() : '';
157
+ if (!normalizedWorkflowId) {
158
+ return;
159
+ }
160
+ const nextWorkflowIds = normalizeLines(state.workflowIdsText);
161
+ if (!nextWorkflowIds.includes(normalizedWorkflowId)) {
162
+ nextWorkflowIds.push(normalizedWorkflowId);
163
+ }
164
+ state.selectedEngine = 'workflow';
165
+ state.workflowIdsText = nextWorkflowIds.join('\n');
166
+ },
167
+
168
+ async loadTaskOrchestrationOverview(options = {}) {
169
+ const state = this.ensureTaskOrchestrationState();
170
+ if (state.loading && !options.forceRefresh) {
171
+ return null;
172
+ }
173
+ const silent = !!options.silent;
174
+ state.loading = true;
175
+ state.lastError = '';
176
+ try {
177
+ const res = await api('task-overview', {
178
+ queueLimit: 20,
179
+ runLimit: 20
180
+ });
181
+ if (res && res.error) {
182
+ state.lastError = res.error;
183
+ if (!silent) {
184
+ this.showMessage(res.error, 'error');
185
+ }
186
+ return res;
187
+ }
188
+ state.workflows = Array.isArray(res && res.workflows) ? res.workflows : [];
189
+ state.queue = Array.isArray(res && res.queue) ? res.queue : [];
190
+ state.runs = Array.isArray(res && res.runs) ? res.runs : [];
191
+ state.overviewWarnings = Array.isArray(res && res.warnings) ? res.warnings : [];
192
+ state.lastLoadedAt = new Date().toISOString();
193
+ if (!state.selectedRunId && state.runs.length > 0) {
194
+ state.selectedRunId = state.runs[0].runId || '';
195
+ }
196
+ const shouldRefreshSelectedDetail = !!state.selectedRunId
197
+ && (options.includeDetail !== false
198
+ || (state.selectedRunDetail && this.isTaskRunActive(state.selectedRunDetail && state.selectedRunDetail.run && state.selectedRunDetail.run.status)));
199
+ if (shouldRefreshSelectedDetail) {
200
+ await this.loadTaskRunDetail(state.selectedRunId, { silent: true });
201
+ }
202
+ this.syncTaskOrchestrationPolling();
203
+ return res;
204
+ } catch (error) {
205
+ const message = error && error.message ? error.message : '任务编排概览加载失败';
206
+ state.lastError = message;
207
+ if (!silent) {
208
+ this.showMessage(message, 'error');
209
+ }
210
+ return { error: message };
211
+ } finally {
212
+ state.loading = false;
213
+ }
214
+ },
215
+
216
+ async previewTaskPlan(options = {}) {
217
+ const state = this.ensureTaskOrchestrationState();
218
+ if (state.planning) {
219
+ return null;
220
+ }
221
+ state.planning = true;
222
+ try {
223
+ const res = await api('task-plan', this.buildTaskOrchestrationRequest());
224
+ state.plan = res && res.plan ? res.plan : null;
225
+ state.planIssues = Array.isArray(res && res.issues) ? res.issues : [];
226
+ state.planWarnings = Array.isArray(res && res.warnings) ? res.warnings : [];
227
+ state.planFingerprint = state.plan ? this.buildTaskOrchestrationFingerprint() : '';
228
+ if (res && res.error) {
229
+ if (!options.silent) {
230
+ this.showMessage(res.error, 'error');
231
+ }
232
+ return res;
233
+ }
234
+ if (!options.silent) {
235
+ this.showMessage('任务计划已更新', 'success');
236
+ }
237
+ return res;
238
+ } catch (error) {
239
+ const message = error && error.message ? error.message : '任务计划生成失败';
240
+ state.plan = null;
241
+ state.planIssues = [];
242
+ state.planWarnings = [];
243
+ if (!options.silent) {
244
+ this.showMessage(message, 'error');
245
+ }
246
+ return { error: message };
247
+ } finally {
248
+ state.planning = false;
249
+ }
250
+ },
251
+
252
+ async runTaskOrchestration() {
253
+ const state = this.ensureTaskOrchestrationState();
254
+ if (state.running) {
255
+ return null;
256
+ }
257
+ state.running = true;
258
+ try {
259
+ const res = await api('task-run', {
260
+ ...this.buildTaskOrchestrationRequest(),
261
+ detach: true
262
+ });
263
+ if (res && res.error) {
264
+ this.showMessage(res.error, 'error');
265
+ return res;
266
+ }
267
+ state.selectedRunId = res.runId || state.selectedRunId;
268
+ await this.loadTaskOrchestrationOverview({ silent: true, includeDetail: false });
269
+ if (state.selectedRunId) {
270
+ await this.loadTaskRunDetail(state.selectedRunId, { silent: true });
271
+ }
272
+ this.syncTaskOrchestrationPolling();
273
+ this.showMessage(`任务已启动: ${res.runId || res.taskId || 'unknown'}`, 'success');
274
+ return res;
275
+ } catch (error) {
276
+ const message = error && error.message ? error.message : '任务启动失败';
277
+ this.showMessage(message, 'error');
278
+ return { error: message };
279
+ } finally {
280
+ state.running = false;
281
+ }
282
+ },
283
+
284
+ async addTaskOrchestrationToQueue(options = {}) {
285
+ const state = this.ensureTaskOrchestrationState();
286
+ if (state.queueAdding) {
287
+ return null;
288
+ }
289
+ state.queueAdding = true;
290
+ try {
291
+ const res = await api('task-queue-add', this.buildTaskOrchestrationRequest());
292
+ if (res && res.error) {
293
+ if (!options.silent) {
294
+ this.showMessage(res.error, 'error');
295
+ }
296
+ return res;
297
+ }
298
+ if (!options.deferRefresh) {
299
+ await this.loadTaskOrchestrationOverview({ silent: true, includeDetail: false });
300
+ }
301
+ if (!options.silent) {
302
+ this.showMessage(`已加入队列: ${res && res.task ? res.task.taskId : ''}`.trim(), 'success');
303
+ }
304
+ return res;
305
+ } catch (error) {
306
+ const message = error && error.message ? error.message : '加入队列失败';
307
+ if (!options.silent) {
308
+ this.showMessage(message, 'error');
309
+ }
310
+ return { error: message };
311
+ } finally {
312
+ state.queueAdding = false;
313
+ }
314
+ },
315
+
316
+ async planAndRunTaskOrchestration() {
317
+ const state = this.ensureTaskOrchestrationState();
318
+ if (state.running || state.planning) {
319
+ return null;
320
+ }
321
+ if (!String(state.target || '').trim()) {
322
+ return null;
323
+ }
324
+ if (buildRunModeFlags(state.runMode).dryRun) {
325
+ return this.previewTaskPlan({ silent: false });
326
+ }
327
+ const fingerprint = this.buildTaskOrchestrationFingerprint();
328
+ const shouldPreview = !state.plan || state.planFingerprint !== fingerprint;
329
+ if (shouldPreview) {
330
+ const preview = await this.previewTaskPlan({ silent: true });
331
+ if (preview && preview.error) {
332
+ this.showMessage(preview.error, 'error');
333
+ return preview;
334
+ }
335
+ }
336
+ if (state.planIssues && state.planIssues.length) {
337
+ this.showMessage('计划存在问题,请先修复再执行', 'error');
338
+ return { error: 'Plan has blocking issues' };
339
+ }
340
+ return this.runTaskOrchestration();
341
+ },
342
+
343
+ async queueTaskOrchestrationAndStart() {
344
+ const state = this.ensureTaskOrchestrationState();
345
+ if (state.queueAdding || state.queueStarting || state.planning || state.running) {
346
+ return null;
347
+ }
348
+ if (!String(state.target || '').trim()) {
349
+ return null;
350
+ }
351
+ const queued = await this.addTaskOrchestrationToQueue({ silent: true, deferRefresh: true });
352
+ if (queued && queued.error) {
353
+ this.showMessage(queued.error, 'error');
354
+ return queued;
355
+ }
356
+ return this.startTaskQueueRunner();
357
+ },
358
+
359
+ async startTaskQueueRunner() {
360
+ const state = this.ensureTaskOrchestrationState();
361
+ if (state.queueStarting) {
362
+ return null;
363
+ }
364
+ state.queueStarting = true;
365
+ try {
366
+ const res = await api('task-queue-start', { detach: true });
367
+ if (res && res.error) {
368
+ this.showMessage(res.error, 'error');
369
+ return res;
370
+ }
371
+ await this.loadTaskOrchestrationOverview({ silent: true, includeDetail: false });
372
+ this.syncTaskOrchestrationPolling();
373
+ this.showMessage(res && res.alreadyRunning ? '队列执行器已在运行' : '队列执行器已启动', 'success');
374
+ return res;
375
+ } catch (error) {
376
+ const message = error && error.message ? error.message : '启动队列失败';
377
+ this.showMessage(message, 'error');
378
+ return { error: message };
379
+ } finally {
380
+ state.queueStarting = false;
381
+ }
382
+ },
383
+
384
+ async loadTaskRunDetail(runId, options = {}) {
385
+ const state = this.ensureTaskOrchestrationState();
386
+ const normalizedRunId = String(runId || '').trim();
387
+ if (!normalizedRunId) {
388
+ state.selectedRunDetail = null;
389
+ state.selectedRunId = '';
390
+ state.selectedRunError = '';
391
+ this.syncTaskOrchestrationPolling();
392
+ return null;
393
+ }
394
+ const requestToken = (state.detailRequestToken || 0) + 1;
395
+ const previousRunId = state.selectedRunId;
396
+ state.detailRequestToken = requestToken;
397
+ state.selectedRunLoading = true;
398
+ state.selectedRunId = normalizedRunId;
399
+ if (options.switchToDetail === true || !options.silent) {
400
+ state.workspaceTab = 'detail';
401
+ }
402
+ state.selectedRunError = '';
403
+ if (previousRunId !== normalizedRunId) {
404
+ state.selectedRunDetail = null;
405
+ }
406
+ try {
407
+ const res = await api('task-run-detail', { runId: normalizedRunId });
408
+ if (state.detailRequestToken !== requestToken) {
409
+ return res;
410
+ }
411
+ if (res && res.error) {
412
+ state.selectedRunDetail = null;
413
+ state.selectedRunError = res.error;
414
+ if (!options.silent) {
415
+ this.showMessage(res.error, 'error');
416
+ }
417
+ return res;
418
+ }
419
+ state.selectedRunDetail = res;
420
+ state.selectedRunError = '';
421
+ this.syncTaskOrchestrationPolling();
422
+ return res;
423
+ } catch (error) {
424
+ const message = error && error.message ? error.message : '加载任务详情失败';
425
+ if (state.detailRequestToken === requestToken) {
426
+ state.selectedRunDetail = null;
427
+ state.selectedRunError = message;
428
+ }
429
+ if (!options.silent) {
430
+ this.showMessage(message, 'error');
431
+ }
432
+ return { error: message };
433
+ } finally {
434
+ if (state.detailRequestToken === requestToken) {
435
+ state.selectedRunLoading = false;
436
+ }
437
+ }
438
+ },
439
+
440
+ async selectTaskRun(runId) {
441
+ return this.loadTaskRunDetail(runId, { silent: false, switchToDetail: true });
442
+ },
443
+
444
+ async retryTaskRunFromUi(runId) {
445
+ const state = this.ensureTaskOrchestrationState();
446
+ const normalizedRunId = String(runId || '').trim();
447
+ if (!normalizedRunId || state.retrying) {
448
+ return null;
449
+ }
450
+ state.retrying = true;
451
+ try {
452
+ const res = await api('task-retry', { runId: normalizedRunId, detach: true });
453
+ if (res && res.error) {
454
+ this.showMessage(res.error, 'error');
455
+ return res;
456
+ }
457
+ state.selectedRunId = res.runId || state.selectedRunId;
458
+ await this.loadTaskOrchestrationOverview({ silent: true, includeDetail: false });
459
+ if (state.selectedRunId) {
460
+ await this.loadTaskRunDetail(state.selectedRunId, { silent: true });
461
+ }
462
+ this.syncTaskOrchestrationPolling();
463
+ this.showMessage(`已开始重试: ${res.runId || normalizedRunId}`, 'success');
464
+ return res;
465
+ } catch (error) {
466
+ const message = error && error.message ? error.message : '重试任务失败';
467
+ this.showMessage(message, 'error');
468
+ return { error: message };
469
+ } finally {
470
+ state.retrying = false;
471
+ }
472
+ },
473
+
474
+ async cancelTaskRunFromUi(target) {
475
+ const normalizedTarget = String(target || '').trim();
476
+ if (!normalizedTarget) {
477
+ return null;
478
+ }
479
+ try {
480
+ const res = await api('task-cancel', { target: normalizedTarget });
481
+ if (res && res.error) {
482
+ this.showMessage(res.error, 'error');
483
+ return res;
484
+ }
485
+ await this.loadTaskOrchestrationOverview({ silent: true, includeDetail: false });
486
+ if (this.taskOrchestration.selectedRunId) {
487
+ await this.loadTaskRunDetail(this.taskOrchestration.selectedRunId, { silent: true });
488
+ }
489
+ this.syncTaskOrchestrationPolling();
490
+ this.showMessage('已发出取消请求', 'success');
491
+ return res;
492
+ } catch (error) {
493
+ const message = error && error.message ? error.message : '取消任务失败';
494
+ this.showMessage(message, 'error');
495
+ return { error: message };
496
+ }
497
+ },
498
+
499
+ taskOrchestrationHasLiveActivity() {
500
+ const state = this.ensureTaskOrchestrationState();
501
+ const queue = Array.isArray(state.queue) ? state.queue : [];
502
+ if (queue.some((item) => isActiveStatus(item && (item.status || item.runStatus)))) {
503
+ return true;
504
+ }
505
+ const detail = state.selectedRunDetail;
506
+ const selectedStatus = detail && detail.run ? detail.run.status : '';
507
+ if (isActiveStatus(selectedStatus)) {
508
+ return true;
509
+ }
510
+ const runs = Array.isArray(state.runs) ? state.runs : [];
511
+ return runs.some((item) => isActiveStatus(item && item.status));
512
+ },
513
+
514
+ stopTaskOrchestrationPolling() {
515
+ if (this._taskOrchestrationPollTimer) {
516
+ clearTimeout(this._taskOrchestrationPollTimer);
517
+ this._taskOrchestrationPollTimer = 0;
518
+ }
519
+ },
520
+
521
+ syncTaskOrchestrationPolling() {
522
+ this.stopTaskOrchestrationPolling();
523
+ if (this.mainTab !== 'orchestration') {
524
+ return;
525
+ }
526
+ if (!this.taskOrchestrationHasLiveActivity()) {
527
+ return;
528
+ }
529
+ this._taskOrchestrationPollTimer = setTimeout(async () => {
530
+ this._taskOrchestrationPollTimer = 0;
531
+ if (this.mainTab !== 'orchestration') {
532
+ return;
533
+ }
534
+ await this.loadTaskOrchestrationOverview({ silent: true, includeDetail: true });
535
+ }, 4000);
536
+ },
537
+
538
+ resetTaskOrchestrationDraft() {
539
+ const state = this.ensureTaskOrchestrationState();
540
+ state.target = '';
541
+ state.title = '';
542
+ state.notes = '';
543
+ state.followUpsText = '';
544
+ state.workflowIdsText = '';
545
+ state.selectedEngine = 'codex';
546
+ state.runMode = 'write';
547
+ state.concurrency = 2;
548
+ state.autoFixRounds = 1;
549
+ state.plan = null;
550
+ state.planIssues = [];
551
+ state.planWarnings = [];
552
+ state.lastError = '';
553
+ this.syncTaskOrchestrationPolling();
554
+ }
555
+ };
556
+ }