codexmate 0.0.21 → 0.0.22

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 (114) hide show
  1. package/README.md +389 -284
  2. package/README.zh.md +321 -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 +575 -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 +198 -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 +161 -149
  52. package/web-ui/modules/app.methods.navigation.mjs +619 -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 +626 -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 +405 -412
  63. package/web-ui/modules/app.methods.task-orchestration.mjs +471 -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 +1609 -0
  67. package/web-ui/modules/plugins.computed.mjs +220 -0
  68. package/web-ui/modules/plugins.methods.mjs +620 -0
  69. package/web-ui/modules/plugins.storage.mjs +37 -0
  70. package/web-ui/modules/skills.computed.mjs +107 -107
  71. package/web-ui/modules/skills.methods.mjs +481 -481
  72. package/web-ui/partials/index/layout-footer.html +13 -13
  73. package/web-ui/partials/index/layout-header.html +461 -402
  74. package/web-ui/partials/index/modal-config-template-agents.html +175 -125
  75. package/web-ui/partials/index/modal-confirm-toast.html +32 -32
  76. package/web-ui/partials/index/modal-health-check.html +72 -72
  77. package/web-ui/partials/index/modal-openclaw-config.html +280 -280
  78. package/web-ui/partials/index/modal-skills.html +200 -184
  79. package/web-ui/partials/index/modals-basic.html +165 -156
  80. package/web-ui/partials/index/panel-config-claude.html +138 -126
  81. package/web-ui/partials/index/panel-config-codex.html +234 -237
  82. package/web-ui/partials/index/panel-config-openclaw.html +78 -78
  83. package/web-ui/partials/index/panel-docs.html +147 -130
  84. package/web-ui/partials/index/panel-market.html +174 -174
  85. package/web-ui/partials/index/panel-orchestration.html +397 -397
  86. package/web-ui/partials/index/panel-plugins.html +243 -0
  87. package/web-ui/partials/index/panel-sessions.html +292 -292
  88. package/web-ui/partials/index/panel-settings.html +258 -190
  89. package/web-ui/partials/index/panel-usage.html +353 -213
  90. package/web-ui/session-helpers.mjs +573 -559
  91. package/web-ui/source-bundle.cjs +233 -233
  92. package/web-ui/styles/base-theme.css +264 -271
  93. package/web-ui/styles/controls-forms.css +362 -360
  94. package/web-ui/styles/docs-panel.css +247 -182
  95. package/web-ui/styles/feedback.css +108 -108
  96. package/web-ui/styles/health-check-dialog.css +144 -144
  97. package/web-ui/styles/layout-shell.css +596 -376
  98. package/web-ui/styles/modals-core.css +464 -464
  99. package/web-ui/styles/navigation-panels.css +382 -348
  100. package/web-ui/styles/openclaw-structured.css +266 -266
  101. package/web-ui/styles/plugins-panel.css +518 -0
  102. package/web-ui/styles/responsive.css +456 -450
  103. package/web-ui/styles/sessions-list.css +400 -400
  104. package/web-ui/styles/sessions-preview.css +411 -411
  105. package/web-ui/styles/sessions-toolbar-trash.css +268 -243
  106. package/web-ui/styles/sessions-usage.css +851 -628
  107. package/web-ui/styles/settings-panel.css +166 -0
  108. package/web-ui/styles/skills-list.css +303 -296
  109. package/web-ui/styles/skills-market.css +396 -335
  110. package/web-ui/styles/task-orchestration.css +776 -776
  111. package/web-ui/styles/titles-cards.css +408 -408
  112. package/web-ui/styles.css +20 -18
  113. package/web-ui.html +17 -17
  114. package/README.en.md +0 -349
@@ -1,471 +1,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
- 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
+ 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
+ }