@teamclaws/teamclaw 2026.8.1 → 2026.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cli.mjs +46 -15
- package/dist/src/config.js +33 -2
- package/dist/src/controller/clarification-routes.js +315 -0
- package/dist/src/controller/controller-intake.js +895 -0
- package/dist/src/controller/controller-planning.js +813 -0
- package/dist/src/controller/controller-runs.js +175 -0
- package/dist/src/controller/controller-service.js +271 -40
- package/dist/src/controller/controller-tools.js +11 -6
- package/dist/src/controller/http-server.js +256 -6004
- package/dist/src/controller/kickoff-orchestrator.js +32 -6
- package/dist/src/controller/message-delivery.js +28 -0
- package/dist/src/controller/message-routes.js +114 -0
- package/dist/src/controller/orchestration-manifest.js +6 -28
- package/dist/src/controller/preview-proxy.js +98 -0
- package/dist/src/controller/profile-switch.js +289 -0
- package/dist/src/controller/project-registry.js +294 -0
- package/dist/src/controller/prompt-injector.js +19 -9
- package/dist/src/controller/repo-routes.js +63 -0
- package/dist/src/controller/role-inference.js +231 -20
- package/dist/src/controller/route-table.js +1353 -0
- package/dist/src/controller/task-assignment.js +508 -0
- package/dist/src/controller/task-cleanup.js +258 -0
- package/dist/src/controller/task-execution.js +240 -0
- package/dist/src/controller/task-results.js +461 -0
- package/dist/src/controller/task-router.js +74 -4
- package/dist/src/controller/task-routes.js +703 -0
- package/dist/src/controller/teamauto-routes.js +124 -0
- package/dist/src/controller/ui-assets.js +68 -0
- package/dist/src/controller/worker-provisioning.js +30 -4
- package/dist/src/controller/worker-routes.js +188 -0
- package/dist/src/identity.js +3 -2
- package/dist/src/interaction-contracts.js +7 -7
- package/dist/src/openclaw-workspace.js +40 -18
- package/dist/src/profile-loader.js +486 -0
- package/dist/src/profiles/banking.json +178 -0
- package/dist/src/profiles/crossborder-ecom.json +253 -0
- package/dist/src/profiles/insurance.json +157 -0
- package/dist/src/profiles/knowledge/hs-structure.md +97 -0
- package/dist/src/profiles/knowledge/incoterms-2020.md +188 -0
- package/dist/src/profiles/knowledge/vat-principles.md +85 -0
- package/dist/src/profiles/schema.json +201 -0
- package/dist/src/profiles/securities.json +137 -0
- package/dist/src/profiles/software-rd.json +264 -0
- package/dist/src/profiles/trading-co.json +200 -0
- package/dist/src/prompt-policy.js +26 -13
- package/dist/src/role-ids.js +29 -0
- package/dist/src/roles.js +54 -13
- package/dist/src/session-roles.js +14 -0
- package/dist/src/state.js +188 -10
- package/dist/src/task-executor.js +7 -17
- package/dist/src/teamauto/github-client.js +18 -0
- package/dist/src/teamauto/repo-sync.js +228 -6
- package/dist/src/teamauto/scanner.js +40 -12
- package/dist/src/teamauto/scheduler.js +231 -12
- package/dist/src/types.js +62 -15
- package/dist/src/ui/app.js +592 -24
- package/dist/src/ui/index.html +44 -11
- package/dist/src/ui/style.css +194 -0
- package/dist/src/worker/prompt-injector.js +44 -8
- package/dist/src/worker/skill-installer.js +3 -1
- package/dist/src/worker/worker-service.js +76 -3
- package/openclaw.plugin.json +28 -1
- package/package.json +3 -2
- package/src/config.ts +33 -2
- package/src/controller/clarification-routes.ts +404 -0
- package/src/controller/controller-intake.ts +1113 -0
- package/src/controller/controller-planning.ts +1029 -0
- package/src/controller/controller-runs.ts +239 -0
- package/src/controller/controller-service.ts +295 -41
- package/src/controller/controller-tools.ts +11 -7
- package/src/controller/http-server.ts +294 -6971
- package/src/controller/kickoff-orchestrator.ts +70 -7
- package/src/controller/message-delivery.ts +49 -0
- package/src/controller/message-routes.ts +137 -0
- package/src/controller/orchestration-manifest.ts +6 -29
- package/src/controller/preview-proxy.ts +127 -0
- package/src/controller/profile-switch.ts +366 -0
- package/src/controller/project-registry.ts +361 -0
- package/src/controller/prompt-injector.ts +23 -8
- package/src/controller/repo-routes.ts +91 -0
- package/src/controller/role-inference.ts +260 -22
- package/src/controller/route-table.ts +1616 -0
- package/src/controller/task-assignment.ts +618 -0
- package/src/controller/task-cleanup.ts +344 -0
- package/src/controller/task-execution.ts +356 -0
- package/src/controller/task-results.ts +544 -0
- package/src/controller/task-router.ts +98 -6
- package/src/controller/task-routes.ts +812 -0
- package/src/controller/teamauto-routes.ts +121 -0
- package/src/controller/ui-assets.ts +70 -0
- package/src/controller/websocket.ts +13 -1
- package/src/controller/worker-provisioning.ts +32 -4
- package/src/controller/worker-routes.ts +241 -0
- package/src/identity.ts +3 -2
- package/src/interaction-contracts.ts +7 -7
- package/src/openclaw-workspace.ts +44 -18
- package/src/profile-loader.ts +650 -0
- package/src/profiles/banking.json +178 -0
- package/src/profiles/crossborder-ecom.json +253 -0
- package/src/profiles/insurance.json +157 -0
- package/src/profiles/knowledge/hs-structure.md +97 -0
- package/src/profiles/knowledge/incoterms-2020.md +188 -0
- package/src/profiles/knowledge/vat-principles.md +85 -0
- package/src/profiles/schema.json +201 -0
- package/src/profiles/securities.json +137 -0
- package/src/profiles/software-rd.json +264 -0
- package/src/profiles/trading-co.json +200 -0
- package/src/prompt-policy.ts +27 -13
- package/src/role-ids.ts +31 -0
- package/src/roles.ts +60 -14
- package/src/session-roles.ts +14 -0
- package/src/state.ts +236 -9
- package/src/task-executor.ts +7 -18
- package/src/teamauto/github-client.ts +21 -0
- package/src/teamauto/repo-sync.ts +246 -6
- package/src/teamauto/scanner.ts +47 -13
- package/src/teamauto/scheduler.ts +259 -16
- package/src/teamauto/store.ts +12 -0
- package/src/types.ts +133 -16
- package/src/ui/app.js +592 -24
- package/src/ui/index.html +44 -11
- package/src/ui/style.css +194 -0
- package/src/worker/prompt-injector.ts +48 -8
- package/src/worker/skill-installer.ts +3 -1
- package/src/worker/worker-service.ts +83 -3
package/cli.mjs
CHANGED
|
@@ -55,19 +55,47 @@ const AGENT_MODE_OPTIONS = [
|
|
|
55
55
|
},
|
|
56
56
|
];
|
|
57
57
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
58
|
+
// Installer role options are derived from the packaged role registry
|
|
59
|
+
// (dist/src/roles.js, compiled from src/src/roles.ts) instead of a
|
|
60
|
+
// hand-maintained copy, so profile-driven role sets flow into the wizard
|
|
61
|
+
// without editing this file. dist/ always ships inside the npm package; in a
|
|
62
|
+
// dev checkout it appears after `npm --prefix src run build`. The dynamic
|
|
63
|
+
// import keeps pre-build tooling (lint, source-level tests) working: this
|
|
64
|
+
// file parses without dist present and only the interactive/install paths
|
|
65
|
+
// require it.
|
|
66
|
+
let ROLE_OPTIONS = [];
|
|
67
|
+
let DEFAULT_WORKER_ROLE = "";
|
|
68
|
+
|
|
69
|
+
async function loadRoleOptions() {
|
|
70
|
+
try {
|
|
71
|
+
const rolesModule = await import("./dist/src/roles.js");
|
|
72
|
+
const roles = Array.isArray(rolesModule.ROLES) ? rolesModule.ROLES : [];
|
|
73
|
+
ROLE_OPTIONS = roles
|
|
74
|
+
.filter((role) => role && typeof role.id === "string" && role.id.trim())
|
|
75
|
+
.map((role) => ({
|
|
76
|
+
value: role.id,
|
|
77
|
+
label: typeof role.label === "string" && role.label.trim() ? role.label : role.id,
|
|
78
|
+
}));
|
|
79
|
+
const registryDefault = rolesModule.DEFAULT_ROLE;
|
|
80
|
+
DEFAULT_WORKER_ROLE = typeof registryDefault === "string" && ROLE_OPTIONS.some((option) => option.value === registryDefault)
|
|
81
|
+
? registryDefault
|
|
82
|
+
: "";
|
|
83
|
+
} catch {
|
|
84
|
+
// dist not built yet (dev checkout). Keep the registry empty so callers
|
|
85
|
+
// fail with the actionable guidance in ensureRoleOptionsLoaded instead
|
|
86
|
+
// of presenting a stale role list.
|
|
87
|
+
ROLE_OPTIONS = [];
|
|
88
|
+
DEFAULT_WORKER_ROLE = "";
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function ensureRoleOptionsLoaded() {
|
|
93
|
+
if (ROLE_OPTIONS.length === 0) {
|
|
94
|
+
throw new Error(
|
|
95
|
+
"TeamClaw role registry unavailable (./dist/src/roles.js). Run `npm --prefix src run build` in the repository, or reinstall the @teamclaws/teamclaw package, then retry.",
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
71
99
|
|
|
72
100
|
const INSTALL_MODE_OPTIONS = [
|
|
73
101
|
{
|
|
@@ -185,6 +213,7 @@ function parseArgs(argv) {
|
|
|
185
213
|
}
|
|
186
214
|
if (arg === "--worker-role") {
|
|
187
215
|
const value = argv[index + 1];
|
|
216
|
+
ensureRoleOptionsLoaded();
|
|
188
217
|
const validRoles = new Set(ROLE_OPTIONS.map((option) => option.value));
|
|
189
218
|
if (!value || !validRoles.has(value)) {
|
|
190
219
|
throw new Error(`--worker-role requires one of: ${ROLE_OPTIONS.map((option) => option.value).join(", ")}`);
|
|
@@ -1318,7 +1347,7 @@ async function collectInstallChoices(configPath, config, prompter, options) {
|
|
|
1318
1347
|
defaultValue: options.workerRole || (
|
|
1319
1348
|
typeof existingTeamClaw.role === "string" && existingTeamClaw.role.trim()
|
|
1320
1349
|
? existingTeamClaw.role.trim()
|
|
1321
|
-
:
|
|
1350
|
+
: DEFAULT_WORKER_ROLE || ROLE_OPTIONS[0].value
|
|
1322
1351
|
),
|
|
1323
1352
|
});
|
|
1324
1353
|
const workerPort = await prompter.number({
|
|
@@ -1410,7 +1439,7 @@ async function collectInstallChoices(configPath, config, prompter, options) {
|
|
|
1410
1439
|
|
|
1411
1440
|
const provisioningRoles = await promptOptionalRoleList(
|
|
1412
1441
|
prompter,
|
|
1413
|
-
|
|
1442
|
+
`Preferred on-demand roles (comma-separated, leave empty to keep workerProvisioningRoles empty and let startup readiness default to a warm ${DEFAULT_WORKER_ROLE} worker)`,
|
|
1414
1443
|
resolveDefaultProvisioningRoles(existingTeamClaw),
|
|
1415
1444
|
);
|
|
1416
1445
|
const maxPerRole = await prompter.number({
|
|
@@ -1950,6 +1979,7 @@ async function runInstall(options) {
|
|
|
1950
1979
|
if (!options.yes && (!process.stdin.isTTY || !process.stdout.isTTY)) {
|
|
1951
1980
|
throw new Error("Interactive install requires a TTY. Re-run with --yes or in a terminal.");
|
|
1952
1981
|
}
|
|
1982
|
+
ensureRoleOptionsLoaded();
|
|
1953
1983
|
|
|
1954
1984
|
const prompter = new Prompter({ yes: options.yes });
|
|
1955
1985
|
try {
|
|
@@ -2073,6 +2103,7 @@ async function runInstall(options) {
|
|
|
2073
2103
|
}
|
|
2074
2104
|
|
|
2075
2105
|
async function main() {
|
|
2106
|
+
await loadRoleOptions();
|
|
2076
2107
|
const { command, options } = parseArgs(process.argv.slice(2));
|
|
2077
2108
|
if (command === "help") {
|
|
2078
2109
|
printHelp();
|
package/dist/src/config.js
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import { parsePluginConfig } from "./types.js";
|
|
2
|
-
|
|
2
|
+
// Phase 1.3: the enum is driven by the active TeamProfile (software-rd
|
|
3
|
+
// baseline) instead of importing the roles.ts registry directly. The
|
|
4
|
+
// manifest generator evaluates this file without imports and injects
|
|
5
|
+
// equivalent getActiveRoleIds/SOFTWARE_RD_PROFILE_ID bindings.
|
|
6
|
+
import { getActiveRoleIds, SOFTWARE_RD_PROFILE_ID } from "./profile-loader.js";
|
|
3
7
|
import { TEAMCLAW_PUBLISHED_RUNTIME_IMAGE } from "./install-defaults.js";
|
|
4
8
|
function buildConfigSchema() {
|
|
5
9
|
return {
|
|
@@ -17,6 +21,11 @@ function buildConfigSchema() {
|
|
|
17
21
|
default: 9527,
|
|
18
22
|
description: "HTTP server port for this instance",
|
|
19
23
|
},
|
|
24
|
+
listenHost: {
|
|
25
|
+
type: "string",
|
|
26
|
+
default: "",
|
|
27
|
+
description: "HTTP listen host override (empty = 127.0.0.1 on hosts, 0.0.0.0 in containers); set 0.0.0.0 for external workers reachable by a remote controller",
|
|
28
|
+
},
|
|
20
29
|
role: {
|
|
21
30
|
type: "string",
|
|
22
31
|
default: "developer",
|
|
@@ -32,6 +41,11 @@ function buildConfigSchema() {
|
|
|
32
41
|
default: "TeamClaw",
|
|
33
42
|
description: "Team name for mDNS identification",
|
|
34
43
|
},
|
|
44
|
+
teamProfile: {
|
|
45
|
+
type: "string",
|
|
46
|
+
default: SOFTWARE_RD_PROFILE_ID,
|
|
47
|
+
description: "Team profile package id (software-rd baseline); seeds first initialization only - the persisted team state profile is the runtime truth",
|
|
48
|
+
},
|
|
35
49
|
heartbeatIntervalMs: {
|
|
36
50
|
type: "number",
|
|
37
51
|
default: 10000,
|
|
@@ -42,6 +56,11 @@ function buildConfigSchema() {
|
|
|
42
56
|
default: 600000,
|
|
43
57
|
description: "Inactivity threshold in milliseconds before TeamClaw probes a stalled role task instead of failing it",
|
|
44
58
|
},
|
|
59
|
+
teamSwitchDrainTimeoutMs: {
|
|
60
|
+
type: "number",
|
|
61
|
+
default: 30000,
|
|
62
|
+
description: "How long a team profile switch waits for in-flight tasks to finish before force-collecting them (design 2.3 graceful drain)",
|
|
63
|
+
},
|
|
45
64
|
maxConcurrentTasks: {
|
|
46
65
|
type: "integer",
|
|
47
66
|
default: 5,
|
|
@@ -118,7 +137,7 @@ function buildConfigSchema() {
|
|
|
118
137
|
description: "Preferred on-demand roles; task-required roles can still launch automatically. Empty means no preferred startup role list, so startup readiness falls back to a warm developer worker",
|
|
119
138
|
items: {
|
|
120
139
|
type: "string",
|
|
121
|
-
enum:
|
|
140
|
+
enum: getActiveRoleIds(),
|
|
122
141
|
},
|
|
123
142
|
},
|
|
124
143
|
workerProvisioningMinPerRole: {
|
|
@@ -234,14 +253,26 @@ function buildConfigSchema() {
|
|
|
234
253
|
uiHints: {
|
|
235
254
|
mode: { label: "Mode", help: "controller manages the team, worker executes tasks" },
|
|
236
255
|
port: { label: "Port", help: "HTTP server port for this instance" },
|
|
256
|
+
listenHost: {
|
|
257
|
+
label: "Listen Host",
|
|
258
|
+
help: "HTTP bind address override; empty defaults to 127.0.0.1 on hosts and 0.0.0.0 in containers. Use 0.0.0.0 when a remote controller must reach this worker",
|
|
259
|
+
},
|
|
237
260
|
role: { label: "Role", help: "Worker role (worker mode only)" },
|
|
238
261
|
controllerUrl: { label: "Controller URL", help: "Manual fallback if mDNS discovery fails" },
|
|
239
262
|
teamName: { label: "Team Name", help: "Team identifier for mDNS" },
|
|
263
|
+
teamProfile: {
|
|
264
|
+
label: "Team Profile",
|
|
265
|
+
help: "Team profile package id; applies to the first team initialization only (Phase 2 adds profile switching)",
|
|
266
|
+
},
|
|
240
267
|
heartbeatIntervalMs: { label: "Heartbeat Interval", help: "In milliseconds, minimum 1000" },
|
|
241
268
|
taskTimeoutMs: {
|
|
242
269
|
label: "Task Stall Threshold",
|
|
243
270
|
help: "In milliseconds, how long TeamClaw waits without visible progress before probing the task instead of failing it",
|
|
244
271
|
},
|
|
272
|
+
teamSwitchDrainTimeoutMs: {
|
|
273
|
+
label: "Profile Switch Drain Timeout",
|
|
274
|
+
help: "In milliseconds, how long a team profile switch waits for in-flight tasks to finish before force-collecting them",
|
|
275
|
+
},
|
|
245
276
|
maxConcurrentTasks: {
|
|
246
277
|
label: "Max Concurrent Tasks",
|
|
247
278
|
hint: "number",
|
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
import { generateId, parseJsonBody, sendError, sendJson } from "../protocol.js";
|
|
2
|
+
import { ensureTeamMessageContract } from "../interaction-contracts.js";
|
|
3
|
+
import { validateActiveRoleId } from "../profile-loader.js";
|
|
4
|
+
import { normalizeClarificationQuestionSchema } from "./orchestration-manifest.js";
|
|
5
|
+
import { cancelTaskExecution, recordTaskExecutionEvent, resetTaskForFreshAttempt, serializeTask, } from "./task-execution.js";
|
|
6
|
+
import { assignTaskToWorker } from "./task-assignment.js";
|
|
7
|
+
import { routeDirectMessage } from "./message-delivery.js";
|
|
8
|
+
import { reconcileControllerClarifications, runControllerIntake, } from "./controller-intake.js";
|
|
9
|
+
// POST /api/v1/clarifications
|
|
10
|
+
export async function handleClarificationCreateRequest(deps, req, res) {
|
|
11
|
+
const body = await parseJsonBody(req);
|
|
12
|
+
const taskId = typeof body.taskId === "string" ? body.taskId : "";
|
|
13
|
+
const requestedBy = typeof body.requestedBy === "string" ? body.requestedBy : "";
|
|
14
|
+
const requestedByWorkerId = typeof body.requestedByWorkerId === "string" ? body.requestedByWorkerId : undefined;
|
|
15
|
+
const requestedByRole = validateActiveRoleId(body.requestedByRole);
|
|
16
|
+
const question = typeof body.question === "string" ? body.question.trim() : "";
|
|
17
|
+
const blockingReason = typeof body.blockingReason === "string" ? body.blockingReason.trim() : "";
|
|
18
|
+
const context = typeof body.context === "string" && body.context.trim() ? body.context.trim() : undefined;
|
|
19
|
+
const questionSchema = normalizeClarificationQuestionSchema(body.questionSchema);
|
|
20
|
+
if (!taskId || !question || !blockingReason) {
|
|
21
|
+
sendError(res, 400, "taskId, question, and blockingReason are required");
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
const requested = await requestTaskClarification({
|
|
25
|
+
taskId,
|
|
26
|
+
requestedBy,
|
|
27
|
+
requestedByWorkerId,
|
|
28
|
+
requestedByRole,
|
|
29
|
+
question,
|
|
30
|
+
questionSchema,
|
|
31
|
+
blockingReason,
|
|
32
|
+
context,
|
|
33
|
+
}, deps);
|
|
34
|
+
if (requested.status === "missing-task") {
|
|
35
|
+
sendError(res, 404, "Task not found");
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
if (requested.status === "conflict") {
|
|
39
|
+
sendError(res, 409, "Cannot request clarification for a completed task");
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
if (requested.status === "already-pending") {
|
|
43
|
+
sendJson(res, 200, {
|
|
44
|
+
clarification: requested.clarification,
|
|
45
|
+
task: serializeTask(requested.task),
|
|
46
|
+
status: "already-pending",
|
|
47
|
+
});
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
sendJson(res, 201, {
|
|
51
|
+
clarification: requested.clarification,
|
|
52
|
+
task: serializeTask(requested.task),
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
// GET /api/v1/clarifications
|
|
56
|
+
export async function handleClarificationListRequest(deps, res) {
|
|
57
|
+
const state = reconcileControllerClarifications(deps);
|
|
58
|
+
const clarifications = state
|
|
59
|
+
? Object.values(state.clarifications).sort((left, right) => right.createdAt - left.createdAt)
|
|
60
|
+
: [];
|
|
61
|
+
sendJson(res, 200, {
|
|
62
|
+
clarifications,
|
|
63
|
+
pendingCount: clarifications.filter((item) => item.status === "pending").length,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
// POST /api/v1/clarifications/:id/answer
|
|
67
|
+
export async function handleClarificationAnswerRequest(deps, req, res, pathname) {
|
|
68
|
+
const { getTeamState, taskRouter, updateTeamState, wsServer } = deps;
|
|
69
|
+
const clarificationId = pathname.split("/")[4];
|
|
70
|
+
const body = await parseJsonBody(req);
|
|
71
|
+
const answerValue = typeof body.answerValue === "string" ? body.answerValue.trim() : undefined;
|
|
72
|
+
const answerValues = Array.isArray(body.answerValues)
|
|
73
|
+
? body.answerValues.map((entry) => String(entry || "").trim()).filter(Boolean)
|
|
74
|
+
: undefined;
|
|
75
|
+
const answerNumber = typeof body.answerNumber === "number" && Number.isFinite(body.answerNumber)
|
|
76
|
+
? body.answerNumber
|
|
77
|
+
: undefined;
|
|
78
|
+
const answerComment = typeof body.answerComment === "string" && body.answerComment.trim()
|
|
79
|
+
? body.answerComment.trim()
|
|
80
|
+
: undefined;
|
|
81
|
+
const answeredBy = typeof body.answeredBy === "string" && body.answeredBy.trim()
|
|
82
|
+
? body.answeredBy.trim()
|
|
83
|
+
: "human";
|
|
84
|
+
const currentState = getTeamState();
|
|
85
|
+
const currentClarification = currentState?.clarifications[clarificationId];
|
|
86
|
+
if (!currentClarification) {
|
|
87
|
+
sendError(res, 404, "Clarification request not found");
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
const answer = buildStructuredClarificationAnswer(currentClarification, {
|
|
91
|
+
answer: typeof body.answer === "string" ? body.answer.trim() : "",
|
|
92
|
+
answerValue,
|
|
93
|
+
answerValues,
|
|
94
|
+
answerNumber,
|
|
95
|
+
answerComment,
|
|
96
|
+
});
|
|
97
|
+
if (!answer) {
|
|
98
|
+
sendError(res, 400, "answer is required");
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
if (currentClarification.status === "answered") {
|
|
102
|
+
sendError(res, 409, "Clarification request already answered");
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
const now = Date.now();
|
|
106
|
+
const state = updateTeamState((s) => {
|
|
107
|
+
const clarification = s.clarifications[clarificationId];
|
|
108
|
+
if (!clarification) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
clarification.status = "answered";
|
|
112
|
+
clarification.answer = answer;
|
|
113
|
+
clarification.answerValue = answerValue;
|
|
114
|
+
clarification.answerValues = answerValues;
|
|
115
|
+
clarification.answerNumber = answerNumber;
|
|
116
|
+
clarification.answerComment = answerComment;
|
|
117
|
+
clarification.answeredBy = answeredBy;
|
|
118
|
+
clarification.answeredAt = now;
|
|
119
|
+
clarification.updatedAt = now;
|
|
120
|
+
const task = s.tasks[clarification.taskId];
|
|
121
|
+
if (!task) {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
task.status = "pending";
|
|
125
|
+
task.progress = `Clarification answered by ${answeredBy}: ${answer}`;
|
|
126
|
+
task.clarificationRequestId = undefined;
|
|
127
|
+
resetTaskForFreshAttempt(task);
|
|
128
|
+
task.updatedAt = now;
|
|
129
|
+
});
|
|
130
|
+
const clarification = state.clarifications[clarificationId];
|
|
131
|
+
const task = clarification?.taskId ? state.tasks[clarification.taskId] : undefined;
|
|
132
|
+
let responseMessage;
|
|
133
|
+
if (clarification?.requestedByRole && task) {
|
|
134
|
+
responseMessage = {
|
|
135
|
+
id: generateId(),
|
|
136
|
+
from: answeredBy,
|
|
137
|
+
toRole: clarification.requestedByRole,
|
|
138
|
+
type: "direct",
|
|
139
|
+
content: `Clarification answer for task ${task.id}: ${answer}`,
|
|
140
|
+
contract: ensureTeamMessageContract(null, {
|
|
141
|
+
type: "direct",
|
|
142
|
+
content: `Clarification answer for task ${task.id}: ${answer}`,
|
|
143
|
+
toRole: clarification.requestedByRole,
|
|
144
|
+
taskId: task.id,
|
|
145
|
+
summary: `Clarification answered for task ${task.id}`,
|
|
146
|
+
details: answer,
|
|
147
|
+
requestedAction: "Resume the task using this clarification.",
|
|
148
|
+
needsResponse: false,
|
|
149
|
+
intent: "update",
|
|
150
|
+
}),
|
|
151
|
+
taskId: task.id,
|
|
152
|
+
createdAt: now,
|
|
153
|
+
};
|
|
154
|
+
updateTeamState((s) => {
|
|
155
|
+
s.messages.push(responseMessage);
|
|
156
|
+
});
|
|
157
|
+
await routeDirectMessage(responseMessage, deps);
|
|
158
|
+
wsServer.broadcastUpdate({ type: "message:new", data: responseMessage });
|
|
159
|
+
}
|
|
160
|
+
let resumedTask = task;
|
|
161
|
+
let resumedWorker = null;
|
|
162
|
+
let continuedControllerRun = null;
|
|
163
|
+
if (task) {
|
|
164
|
+
const latestState = getTeamState();
|
|
165
|
+
if (clarification?.requestedByWorkerId && latestState.workers[clarification.requestedByWorkerId]?.status === "idle") {
|
|
166
|
+
resumedWorker = latestState.workers[clarification.requestedByWorkerId];
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
resumedWorker = taskRouter.routeTask(task, latestState.workers);
|
|
170
|
+
}
|
|
171
|
+
if (resumedWorker) {
|
|
172
|
+
resumedTask = await assignTaskToWorker(task.id, resumedWorker, deps, {
|
|
173
|
+
assignedRole: task.assignedRole,
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
else if (clarification?.controllerRunId) {
|
|
178
|
+
const targetSessionKey = clarification.controllerSessionKey
|
|
179
|
+
|| state.controllerRuns[clarification.controllerRunId]?.sessionKey;
|
|
180
|
+
if (targetSessionKey) {
|
|
181
|
+
continuedControllerRun = {
|
|
182
|
+
sessionKey: targetSessionKey,
|
|
183
|
+
controllerRunId: clarification.controllerRunId,
|
|
184
|
+
queued: true,
|
|
185
|
+
};
|
|
186
|
+
void runControllerIntake(buildControllerClarificationAnswerMessage(clarification, answer, answeredBy), targetSessionKey, deps).catch((err) => {
|
|
187
|
+
deps.logger.warn(`Controller: failed to continue intake after clarification ${clarification.id}: ${String(err)}`);
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
wsServer.broadcastUpdate({ type: "clarification:answered", data: clarification });
|
|
192
|
+
if (resumedTask) {
|
|
193
|
+
recordTaskExecutionEvent(resumedTask.id, {
|
|
194
|
+
type: "lifecycle",
|
|
195
|
+
phase: "clarification_answered",
|
|
196
|
+
source: "controller",
|
|
197
|
+
message: `Clarification answered by ${answeredBy}: ${answer}`,
|
|
198
|
+
role: clarification?.requestedByRole,
|
|
199
|
+
workerId: clarification?.requestedByWorkerId,
|
|
200
|
+
}, deps);
|
|
201
|
+
wsServer.broadcastUpdate({ type: "task:updated", data: serializeTask(resumedTask) });
|
|
202
|
+
}
|
|
203
|
+
sendJson(res, 200, {
|
|
204
|
+
clarification,
|
|
205
|
+
task: serializeTask(resumedTask),
|
|
206
|
+
resumedWorker,
|
|
207
|
+
controllerRun: continuedControllerRun,
|
|
208
|
+
message: responseMessage,
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
export function buildControllerClarificationAnswerMessage(clarification, answer, answeredBy) {
|
|
212
|
+
return [
|
|
213
|
+
"The human has answered a pending controller clarification.",
|
|
214
|
+
`Question: ${clarification.question}`,
|
|
215
|
+
`Answer: ${answer}`,
|
|
216
|
+
`Answered by: ${answeredBy}`,
|
|
217
|
+
"Use this answer to continue the same requirement and update the plan/tasks accordingly.",
|
|
218
|
+
].join("\n");
|
|
219
|
+
}
|
|
220
|
+
export function buildStructuredClarificationAnswer(clarification, payload) {
|
|
221
|
+
if (typeof payload.answer === "string" && payload.answer.trim()) {
|
|
222
|
+
return payload.answer.trim();
|
|
223
|
+
}
|
|
224
|
+
const schema = clarification.questionSchema;
|
|
225
|
+
const optionLabel = (value) => {
|
|
226
|
+
const match = schema?.options?.find((entry) => entry.value === value);
|
|
227
|
+
return match?.label || value;
|
|
228
|
+
};
|
|
229
|
+
const parts = [];
|
|
230
|
+
if (schema?.kind === "single-select" && payload.answerValue) {
|
|
231
|
+
parts.push(optionLabel(payload.answerValue));
|
|
232
|
+
}
|
|
233
|
+
else if (schema?.kind === "multi-select" && Array.isArray(payload.answerValues) && payload.answerValues.length > 0) {
|
|
234
|
+
parts.push(payload.answerValues.map((entry) => optionLabel(entry)).join(", "));
|
|
235
|
+
}
|
|
236
|
+
else if (schema?.kind === "number" && typeof payload.answerNumber === "number" && Number.isFinite(payload.answerNumber)) {
|
|
237
|
+
parts.push(`${payload.answerNumber}${schema.unit ? ` ${schema.unit}` : ""}`);
|
|
238
|
+
}
|
|
239
|
+
else if (payload.answerValue) {
|
|
240
|
+
parts.push(payload.answerValue);
|
|
241
|
+
}
|
|
242
|
+
if (payload.answerComment && payload.answerComment.trim()) {
|
|
243
|
+
parts.push(parts.length > 0 ? `Additional context: ${payload.answerComment.trim()}` : payload.answerComment.trim());
|
|
244
|
+
}
|
|
245
|
+
return parts.join("\n").trim();
|
|
246
|
+
}
|
|
247
|
+
export async function requestTaskClarification(params, deps) {
|
|
248
|
+
const { getTeamState, updateTeamState, wsServer } = deps;
|
|
249
|
+
const currentState = getTeamState();
|
|
250
|
+
const currentTask = currentState?.tasks[params.taskId];
|
|
251
|
+
if (!currentTask) {
|
|
252
|
+
return { status: "missing-task" };
|
|
253
|
+
}
|
|
254
|
+
if (currentTask.clarificationRequestId) {
|
|
255
|
+
const existing = currentState?.clarifications[currentTask.clarificationRequestId];
|
|
256
|
+
if (existing?.status === "pending") {
|
|
257
|
+
return { status: "already-pending", clarification: existing, task: currentTask };
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
if (currentTask.status === "completed" || currentTask.status === "failed") {
|
|
261
|
+
return { status: "conflict", task: currentTask };
|
|
262
|
+
}
|
|
263
|
+
const previousWorkerId = currentTask.assignedWorkerId;
|
|
264
|
+
const clarificationId = generateId();
|
|
265
|
+
const now = Date.now();
|
|
266
|
+
const clarification = {
|
|
267
|
+
id: clarificationId,
|
|
268
|
+
taskId: params.taskId,
|
|
269
|
+
requestedBy: params.requestedBy,
|
|
270
|
+
requestedByWorkerId: params.requestedByWorkerId,
|
|
271
|
+
requestedByRole: params.requestedByRole,
|
|
272
|
+
question: params.question,
|
|
273
|
+
questionSchema: params.questionSchema,
|
|
274
|
+
blockingReason: params.blockingReason,
|
|
275
|
+
context: params.context,
|
|
276
|
+
status: "pending",
|
|
277
|
+
createdAt: now,
|
|
278
|
+
updatedAt: now,
|
|
279
|
+
};
|
|
280
|
+
const state = updateTeamState((s) => {
|
|
281
|
+
s.clarifications[clarificationId] = clarification;
|
|
282
|
+
const task = s.tasks[params.taskId];
|
|
283
|
+
if (!task) {
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
const assignedWorkerId = task.assignedWorkerId;
|
|
287
|
+
task.status = "blocked";
|
|
288
|
+
task.progress = `Awaiting clarification: ${params.question}`;
|
|
289
|
+
task.clarificationRequestId = clarificationId;
|
|
290
|
+
task.assignedWorkerId = undefined;
|
|
291
|
+
task.updatedAt = now;
|
|
292
|
+
if (assignedWorkerId && s.workers[assignedWorkerId]) {
|
|
293
|
+
const assignedWorker = s.workers[assignedWorkerId];
|
|
294
|
+
if (assignedWorker.status !== "offline") {
|
|
295
|
+
assignedWorker.status = "idle";
|
|
296
|
+
}
|
|
297
|
+
assignedWorker.currentTaskId = undefined;
|
|
298
|
+
}
|
|
299
|
+
});
|
|
300
|
+
await cancelTaskExecution(params.taskId, previousWorkerId, "clarification request", deps);
|
|
301
|
+
const updatedTask = state.tasks[params.taskId];
|
|
302
|
+
wsServer.broadcastUpdate({ type: "clarification:requested", data: clarification });
|
|
303
|
+
if (updatedTask) {
|
|
304
|
+
recordTaskExecutionEvent(params.taskId, {
|
|
305
|
+
type: "lifecycle",
|
|
306
|
+
phase: "clarification_requested",
|
|
307
|
+
source: "controller",
|
|
308
|
+
message: `Clarification requested: ${params.question}`,
|
|
309
|
+
role: clarification.requestedByRole,
|
|
310
|
+
workerId: clarification.requestedByWorkerId,
|
|
311
|
+
}, deps);
|
|
312
|
+
wsServer.broadcastUpdate({ type: "task:updated", data: serializeTask(updatedTask) });
|
|
313
|
+
}
|
|
314
|
+
return { status: "created", clarification, task: updatedTask };
|
|
315
|
+
}
|