@teamclaws/teamclaw 2026.7.1 → 2026.8.1
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/README.md +8 -0
- package/dist/api.js +1 -0
- package/dist/index.js +325 -0
- package/dist/src/config.js +381 -0
- package/dist/src/controller/controller-capacity.js +14 -0
- package/dist/src/controller/controller-service.js +393 -0
- package/dist/src/controller/controller-tools.js +582 -0
- package/dist/src/controller/delivery-report.js +458 -0
- package/dist/src/controller/github-auth.js +564 -0
- package/dist/src/controller/http-server.js +6032 -0
- package/dist/src/controller/kickoff-orchestrator.js +220 -0
- package/dist/src/controller/managed-gateway-process.js +286 -0
- package/dist/src/controller/message-router.js +38 -0
- package/dist/src/controller/orchestration-manifest.js +173 -0
- package/dist/src/controller/preview-manager.js +586 -0
- package/dist/src/controller/prompt-injector.js +187 -0
- package/dist/src/controller/role-inference.js +35 -0
- package/dist/src/controller/task-router.js +67 -0
- package/dist/src/controller/tunnel-manager.js +548 -0
- package/dist/src/controller/websocket.js +42 -0
- package/dist/src/controller/worker-provisioning.js +1648 -0
- package/dist/src/discovery.js +86 -0
- package/dist/src/git-collaboration.js +694 -0
- package/dist/src/identity.js +131 -0
- package/dist/src/install-defaults.js +1 -0
- package/dist/src/interaction-contracts.js +531 -0
- package/dist/src/networking.js +91 -0
- package/dist/src/openclaw-workspace.js +903 -0
- package/dist/src/prompt-policy.js +373 -0
- package/dist/src/protocol.js +58 -0
- package/dist/src/roles.js +349 -0
- package/dist/src/state.js +183 -0
- package/dist/src/task-executor.js +1033 -0
- package/dist/src/teamauto/github-client.js +192 -0
- package/dist/src/teamauto/norms.js +85 -0
- package/dist/src/teamauto/repo-sync.js +201 -0
- package/dist/src/teamauto/scanner.js +179 -0
- package/dist/src/teamauto/schedule.js +123 -0
- package/dist/src/teamauto/scheduler.js +688 -0
- package/dist/src/teamauto/store.js +133 -0
- package/dist/src/types.js +243 -0
- package/dist/src/ui/app.js +4292 -0
- package/dist/src/ui/assets/teamclaw-app-icon.png +0 -0
- package/dist/src/ui/github-login.html +296 -0
- package/dist/src/ui/index.html +414 -0
- package/dist/src/ui/style.css +2611 -0
- package/dist/src/worker/http-handler.js +146 -0
- package/dist/src/worker/message-queue.js +24 -0
- package/dist/src/worker/prompt-injector.js +42 -0
- package/dist/src/worker/skill-installer.js +253 -0
- package/dist/src/worker/tools.js +541 -0
- package/dist/src/worker/worker-service.js +234 -0
- package/dist/src/workspace-browser.js +269 -0
- package/openclaw.plugin.json +1 -1
- package/package.json +9 -7
- package/src/controller/http-server.ts +26 -1
- package/src/controller/tunnel-manager.ts +139 -15
- package/src/ui/app.js +97 -20
- package/src/ui/index.html +16 -4
- package/src/ui/style.css +49 -0
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import os from "node:os";
|
|
2
|
+
import { generateId, createRegistrationRequest } from "./protocol.js";
|
|
3
|
+
import { getRole } from "./roles.js";
|
|
4
|
+
import { loadWorkerIdentity, saveWorkerIdentity, clearWorkerIdentity } from "./state.js";
|
|
5
|
+
import { MDnsBrowser } from "./discovery.js";
|
|
6
|
+
function getLocalIp(targetHost) {
|
|
7
|
+
if (!targetHost || targetHost === "localhost" || targetHost === "127.0.0.1") {
|
|
8
|
+
return "localhost";
|
|
9
|
+
}
|
|
10
|
+
const interfaces = os.networkInterfaces();
|
|
11
|
+
for (const name of Object.keys(interfaces)) {
|
|
12
|
+
const addrs = interfaces[name];
|
|
13
|
+
if (!addrs)
|
|
14
|
+
continue;
|
|
15
|
+
for (const addr of addrs) {
|
|
16
|
+
if (addr.family === "IPv4" && !addr.internal) {
|
|
17
|
+
return addr.address;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return "localhost";
|
|
22
|
+
}
|
|
23
|
+
export class IdentityManager {
|
|
24
|
+
config;
|
|
25
|
+
logger;
|
|
26
|
+
identity = null;
|
|
27
|
+
browser;
|
|
28
|
+
constructor(config, logger) {
|
|
29
|
+
this.config = config;
|
|
30
|
+
this.logger = logger;
|
|
31
|
+
this.browser = new MDnsBrowser(logger);
|
|
32
|
+
}
|
|
33
|
+
hasIdentity() {
|
|
34
|
+
return this.identity !== null;
|
|
35
|
+
}
|
|
36
|
+
getIdentity() {
|
|
37
|
+
return this.identity;
|
|
38
|
+
}
|
|
39
|
+
async discoverControllerUrl() {
|
|
40
|
+
if (this.config.controllerUrl) {
|
|
41
|
+
return this.config.controllerUrl;
|
|
42
|
+
}
|
|
43
|
+
const results = await this.browser.browse(this.config.teamName, 5000);
|
|
44
|
+
if (results.length > 0) {
|
|
45
|
+
const controller = results[0];
|
|
46
|
+
return `http://${controller.host}:${controller.port}`;
|
|
47
|
+
}
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
async register() {
|
|
51
|
+
const requestedWorkerId = process.env.TEAMCLAW_WORKER_ID?.trim() || undefined;
|
|
52
|
+
const launchToken = process.env.TEAMCLAW_LAUNCH_TOKEN?.trim() || undefined;
|
|
53
|
+
const existing = await loadWorkerIdentity();
|
|
54
|
+
if (existing) {
|
|
55
|
+
if (requestedWorkerId && existing.workerId !== requestedWorkerId) {
|
|
56
|
+
await clearWorkerIdentity();
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
this.identity = existing;
|
|
60
|
+
this.logger.info(`Identity: restored existing identity (workerId=${existing.workerId})`);
|
|
61
|
+
return existing;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
const restored = await loadWorkerIdentity();
|
|
65
|
+
if (restored) {
|
|
66
|
+
this.identity = restored;
|
|
67
|
+
this.logger.info(`Identity: restored existing identity (workerId=${restored.workerId})`);
|
|
68
|
+
return restored;
|
|
69
|
+
}
|
|
70
|
+
const controllerUrl = await this.discoverControllerUrl();
|
|
71
|
+
if (!controllerUrl) {
|
|
72
|
+
this.logger.warn("Identity: no controller found via mDNS or manual URL");
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
const roleDef = getRole(this.config.role);
|
|
76
|
+
const workerId = requestedWorkerId ?? generateId();
|
|
77
|
+
const controllerHost = new URL(controllerUrl).hostname;
|
|
78
|
+
// Prefer explicit override (set by provisioner for Docker/K8s workers),
|
|
79
|
+
// then detected IP (works for bridge-network containers and real hosts),
|
|
80
|
+
// then os.hostname() as last resort.
|
|
81
|
+
const advertisedHost = process.env.TEAMCLAW_ADVERTISE_HOST?.trim() ||
|
|
82
|
+
getLocalIp(controllerHost) ||
|
|
83
|
+
os.hostname();
|
|
84
|
+
const advertisedPort = process.env.TEAMCLAW_ADVERTISE_PORT?.trim()
|
|
85
|
+
? Number(process.env.TEAMCLAW_ADVERTISE_PORT.trim())
|
|
86
|
+
: this.config.port;
|
|
87
|
+
const workerUrl = `http://${advertisedHost}:${advertisedPort}`;
|
|
88
|
+
const registration = createRegistrationRequest(workerId, this.config.role, roleDef?.label ?? this.config.role, workerUrl, roleDef?.capabilities ?? [], launchToken);
|
|
89
|
+
try {
|
|
90
|
+
const res = await fetch(`${controllerUrl}/api/v1/workers/register`, {
|
|
91
|
+
method: "POST",
|
|
92
|
+
headers: { "Content-Type": "application/json" },
|
|
93
|
+
body: JSON.stringify(registration),
|
|
94
|
+
});
|
|
95
|
+
if (!res.ok) {
|
|
96
|
+
const text = await res.text();
|
|
97
|
+
this.logger.error(`Identity: registration failed (${res.status}): ${text}`);
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
const identity = {
|
|
101
|
+
workerId,
|
|
102
|
+
role: this.config.role,
|
|
103
|
+
controllerUrl,
|
|
104
|
+
registeredAt: Date.now(),
|
|
105
|
+
};
|
|
106
|
+
this.identity = identity;
|
|
107
|
+
await saveWorkerIdentity(identity);
|
|
108
|
+
this.logger.info(`Identity: registered as ${this.config.role} (workerId=${workerId}) at ${controllerUrl}`);
|
|
109
|
+
return identity;
|
|
110
|
+
}
|
|
111
|
+
catch (err) {
|
|
112
|
+
this.logger.error(`Identity: registration error: ${err instanceof Error ? err.message : String(err)}`);
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
async clear() {
|
|
117
|
+
if (this.identity) {
|
|
118
|
+
try {
|
|
119
|
+
await fetch(`${this.identity.controllerUrl}/api/v1/workers/${this.identity.workerId}`, {
|
|
120
|
+
method: "DELETE",
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
catch {
|
|
124
|
+
// ignore
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
this.identity = null;
|
|
128
|
+
await clearWorkerIdentity();
|
|
129
|
+
this.logger.info("Identity: cleared");
|
|
130
|
+
}
|
|
131
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const TEAMCLAW_PUBLISHED_RUNTIME_IMAGE = "ghcr.io/topcheer/teamclaw-openclaw:latest";
|
|
@@ -0,0 +1,531 @@
|
|
|
1
|
+
import { ROLES } from "./roles.js";
|
|
2
|
+
const CONTRACT_VERSION = "1.0";
|
|
3
|
+
const ROLE_IDS = new Set(ROLES.map((role) => role.id));
|
|
4
|
+
const RESULT_DELIVERABLE_KINDS = new Set([
|
|
5
|
+
"file",
|
|
6
|
+
"directory",
|
|
7
|
+
"command",
|
|
8
|
+
"artifact",
|
|
9
|
+
"note",
|
|
10
|
+
]);
|
|
11
|
+
const RESULT_FOLLOW_UP_TYPES = new Set([
|
|
12
|
+
"review",
|
|
13
|
+
"handoff",
|
|
14
|
+
"clarification",
|
|
15
|
+
"downstream-task",
|
|
16
|
+
]);
|
|
17
|
+
const MESSAGE_INTENTS = new Set([
|
|
18
|
+
"question",
|
|
19
|
+
"announcement",
|
|
20
|
+
"handoff",
|
|
21
|
+
"review-request",
|
|
22
|
+
"review-response",
|
|
23
|
+
"update",
|
|
24
|
+
"coordination",
|
|
25
|
+
]);
|
|
26
|
+
export function normalizeContractStringList(raw) {
|
|
27
|
+
if (!Array.isArray(raw)) {
|
|
28
|
+
return [];
|
|
29
|
+
}
|
|
30
|
+
return raw
|
|
31
|
+
.map((entry) => (typeof entry === "string" ? entry.trim() : ""))
|
|
32
|
+
.filter(Boolean);
|
|
33
|
+
}
|
|
34
|
+
export function normalizeOptionalContractText(raw) {
|
|
35
|
+
if (typeof raw !== "string") {
|
|
36
|
+
return undefined;
|
|
37
|
+
}
|
|
38
|
+
const normalized = raw.trim();
|
|
39
|
+
return normalized || undefined;
|
|
40
|
+
}
|
|
41
|
+
export function normalizeContractRole(raw) {
|
|
42
|
+
if (typeof raw !== "string") {
|
|
43
|
+
return undefined;
|
|
44
|
+
}
|
|
45
|
+
const normalized = raw.trim();
|
|
46
|
+
return normalized && ROLE_IDS.has(normalized) ? normalized : undefined;
|
|
47
|
+
}
|
|
48
|
+
export function summarizeContractText(text, maxChars = 180) {
|
|
49
|
+
const normalized = text.replace(/\s+/g, " ").trim();
|
|
50
|
+
if (!normalized) {
|
|
51
|
+
return "";
|
|
52
|
+
}
|
|
53
|
+
if (normalized.length <= maxChars) {
|
|
54
|
+
return normalized;
|
|
55
|
+
}
|
|
56
|
+
return `${normalized.slice(0, maxChars).trimEnd()}…`;
|
|
57
|
+
}
|
|
58
|
+
export function ensureTeamMessageContract(raw, fallback) {
|
|
59
|
+
return normalizeTeamMessageContract(raw) ?? buildBackfilledTeamMessageContract(fallback);
|
|
60
|
+
}
|
|
61
|
+
export function normalizeTeamMessageContract(raw) {
|
|
62
|
+
if (!raw || typeof raw !== "object") {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
const input = raw;
|
|
66
|
+
const summary = typeof input.summary === "string" ? input.summary.trim() : "";
|
|
67
|
+
if (!summary) {
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
const intent = normalizeMessageIntent(input.intent) ?? "update";
|
|
71
|
+
return {
|
|
72
|
+
version: typeof input.version === "string" && input.version.trim() ? input.version.trim() : CONTRACT_VERSION,
|
|
73
|
+
intent,
|
|
74
|
+
summary,
|
|
75
|
+
details: normalizeOptionalContractText(input.details),
|
|
76
|
+
requestedAction: normalizeOptionalContractText(input.requestedAction),
|
|
77
|
+
requestedRole: normalizeContractRole(input.requestedRole),
|
|
78
|
+
needsResponse: Boolean(input.needsResponse),
|
|
79
|
+
references: normalizeContractStringList(input.references),
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
export function normalizeWorkerProgressContract(raw) {
|
|
83
|
+
if (!raw || typeof raw !== "object") {
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
const input = raw;
|
|
87
|
+
const summary = typeof input.summary === "string" ? input.summary.trim() : "";
|
|
88
|
+
if (!summary) {
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
const status = normalizeProgressStatus(input.status) ?? "in_progress";
|
|
92
|
+
return {
|
|
93
|
+
version: typeof input.version === "string" && input.version.trim() ? input.version.trim() : CONTRACT_VERSION,
|
|
94
|
+
summary,
|
|
95
|
+
status,
|
|
96
|
+
currentStep: normalizeOptionalContractText(input.currentStep),
|
|
97
|
+
nextStep: normalizeOptionalContractText(input.nextStep),
|
|
98
|
+
blockers: normalizeContractStringList(input.blockers),
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
export function backfillWorkerProgressContract(progress, status) {
|
|
102
|
+
const normalized = progress.trim();
|
|
103
|
+
if (!normalized) {
|
|
104
|
+
return undefined;
|
|
105
|
+
}
|
|
106
|
+
return {
|
|
107
|
+
version: CONTRACT_VERSION,
|
|
108
|
+
summary: summarizeContractText(normalized),
|
|
109
|
+
status: normalizeProgressStatus(status) ?? "in_progress",
|
|
110
|
+
currentStep: normalized,
|
|
111
|
+
blockers: extractQuestionOrBulletLines(normalized, 3, /blocked|waiting|stuck|need/i),
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
export function renderWorkerProgressText(contract, fallbackProgress) {
|
|
115
|
+
if (!contract) {
|
|
116
|
+
return fallbackProgress?.trim() ?? "";
|
|
117
|
+
}
|
|
118
|
+
const lines = [contract.summary];
|
|
119
|
+
if (contract.currentStep) {
|
|
120
|
+
lines.push(`Current step: ${contract.currentStep}`);
|
|
121
|
+
}
|
|
122
|
+
if (contract.nextStep) {
|
|
123
|
+
lines.push(`Next step: ${contract.nextStep}`);
|
|
124
|
+
}
|
|
125
|
+
if (contract.blockers.length > 0) {
|
|
126
|
+
lines.push(`Blockers: ${contract.blockers.join("; ")}`);
|
|
127
|
+
}
|
|
128
|
+
return lines.filter(Boolean).join("\n");
|
|
129
|
+
}
|
|
130
|
+
export function normalizeTaskHandoffContract(raw, fallback) {
|
|
131
|
+
if (raw && typeof raw === "object") {
|
|
132
|
+
const input = raw;
|
|
133
|
+
const summary = typeof input.summary === "string" ? input.summary.trim() : "";
|
|
134
|
+
const reason = typeof input.reason === "string" ? input.reason.trim() : "";
|
|
135
|
+
if (summary && reason) {
|
|
136
|
+
return {
|
|
137
|
+
version: typeof input.version === "string" && input.version.trim() ? input.version.trim() : CONTRACT_VERSION,
|
|
138
|
+
summary,
|
|
139
|
+
reason,
|
|
140
|
+
targetRole: normalizeContractRole(input.targetRole) ?? fallback.targetRole,
|
|
141
|
+
expectedNextStep: normalizeOptionalContractText(input.expectedNextStep),
|
|
142
|
+
artifacts: normalizeContractStringList(input.artifacts),
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return {
|
|
147
|
+
version: CONTRACT_VERSION,
|
|
148
|
+
summary: fallback.summary?.trim() || buildDefaultHandoffSummary(fallback.targetRole, fallback.reason),
|
|
149
|
+
reason: fallback.reason.trim(),
|
|
150
|
+
targetRole: fallback.targetRole,
|
|
151
|
+
expectedNextStep: fallback.expectedNextStep?.trim() || undefined,
|
|
152
|
+
artifacts: (fallback.artifacts ?? []).map((entry) => entry.trim()).filter(Boolean),
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
export function normalizeWorkerTaskResultContract(raw) {
|
|
156
|
+
if (!raw || typeof raw !== "object") {
|
|
157
|
+
return null;
|
|
158
|
+
}
|
|
159
|
+
const input = raw;
|
|
160
|
+
const summary = typeof input.summary === "string" ? input.summary.trim() : "";
|
|
161
|
+
if (!summary) {
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
return {
|
|
165
|
+
version: typeof input.version === "string" && input.version.trim() ? input.version.trim() : CONTRACT_VERSION,
|
|
166
|
+
outcome: normalizeResultOutcome(input.outcome),
|
|
167
|
+
summary,
|
|
168
|
+
deliverables: normalizeResultDeliverables(input.deliverables),
|
|
169
|
+
keyPoints: normalizeContractStringList(input.keyPoints),
|
|
170
|
+
blockers: normalizeContractStringList(input.blockers),
|
|
171
|
+
followUps: normalizeResultFollowUps(input.followUps),
|
|
172
|
+
questions: normalizeContractStringList(input.questions),
|
|
173
|
+
notes: normalizeOptionalContractText(input.notes),
|
|
174
|
+
discoveredPatterns: normalizeContractStringList(input.discoveredPatterns),
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
export function backfillWorkerTaskResultContract(task, result, error) {
|
|
178
|
+
const normalizedResult = result.trim();
|
|
179
|
+
const normalizedError = error?.trim();
|
|
180
|
+
const summarySource = normalizedError
|
|
181
|
+
|| firstMeaningfulLine(normalizedResult)
|
|
182
|
+
|| task?.lastHandoff?.summary
|
|
183
|
+
|| task?.description
|
|
184
|
+
|| task?.title
|
|
185
|
+
|| "Worker result summary unavailable.";
|
|
186
|
+
const outcome = normalizedError
|
|
187
|
+
? "failed"
|
|
188
|
+
: task?.lastHandoff
|
|
189
|
+
? "blocked"
|
|
190
|
+
: "completed";
|
|
191
|
+
const keyPoints = extractQuestionOrBulletLines(normalizedResult, 5);
|
|
192
|
+
const questions = extractQuestionOrBulletLines(normalizedResult, 5, /\?$/);
|
|
193
|
+
const blockers = normalizedError
|
|
194
|
+
? [normalizedError]
|
|
195
|
+
: task?.lastHandoff
|
|
196
|
+
? [task.lastHandoff.reason]
|
|
197
|
+
: [];
|
|
198
|
+
const followUps = task?.lastHandoff
|
|
199
|
+
? [{
|
|
200
|
+
type: "handoff",
|
|
201
|
+
targetRole: task.lastHandoff.targetRole,
|
|
202
|
+
reason: task.lastHandoff.reason,
|
|
203
|
+
}]
|
|
204
|
+
: [];
|
|
205
|
+
return {
|
|
206
|
+
version: CONTRACT_VERSION,
|
|
207
|
+
outcome,
|
|
208
|
+
summary: summarizeContractText(summarySource),
|
|
209
|
+
deliverables: inferResultDeliverables(normalizedResult, normalizedError),
|
|
210
|
+
keyPoints,
|
|
211
|
+
blockers,
|
|
212
|
+
followUps,
|
|
213
|
+
questions,
|
|
214
|
+
notes: "Backfilled by TeamClaw because the worker did not submit a structured result contract.",
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
function buildBackfilledTeamMessageContract(fallback) {
|
|
218
|
+
const summary = fallback.summary?.trim() || summarizeContractText(fallback.content);
|
|
219
|
+
const intent = fallback.intent ?? inferMessageIntent(fallback.type, fallback.content);
|
|
220
|
+
const requestedAction = fallback.requestedAction?.trim() || buildDefaultRequestedAction(intent, fallback.toRole);
|
|
221
|
+
const references = [...(fallback.references ?? []), ...(fallback.taskId ? [fallback.taskId] : [])]
|
|
222
|
+
.map((entry) => entry.trim())
|
|
223
|
+
.filter(Boolean);
|
|
224
|
+
return {
|
|
225
|
+
version: CONTRACT_VERSION,
|
|
226
|
+
intent,
|
|
227
|
+
summary: summary || "TeamClaw message",
|
|
228
|
+
details: fallback.details?.trim() || deriveMessageDetails(summary, fallback.content),
|
|
229
|
+
requestedAction: requestedAction || undefined,
|
|
230
|
+
requestedRole: fallback.toRole,
|
|
231
|
+
needsResponse: typeof fallback.needsResponse === "boolean" ? fallback.needsResponse : intent === "question" || intent === "review-request" || intent === "handoff",
|
|
232
|
+
references: Array.from(new Set(references)),
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
function normalizeResultDeliverables(raw) {
|
|
236
|
+
if (!Array.isArray(raw)) {
|
|
237
|
+
return [];
|
|
238
|
+
}
|
|
239
|
+
return raw
|
|
240
|
+
.filter((entry) => !!entry && typeof entry === "object")
|
|
241
|
+
.map((entry) => {
|
|
242
|
+
const kind = typeof entry.kind === "string" && RESULT_DELIVERABLE_KINDS.has(entry.kind)
|
|
243
|
+
? entry.kind
|
|
244
|
+
: "note";
|
|
245
|
+
const value = typeof entry.value === "string" ? entry.value.trim() : "";
|
|
246
|
+
const summary = normalizeOptionalContractText(entry.summary);
|
|
247
|
+
const artifactType = typeof entry.artifactType === "string" ? entry.artifactType : undefined;
|
|
248
|
+
const previewCommand = normalizeOptionalContractText(entry.previewCommand);
|
|
249
|
+
const previewCwd = normalizeOptionalContractText(entry.previewCwd);
|
|
250
|
+
const previewReadyPath = normalizeOptionalContractText(entry.previewReadyPath);
|
|
251
|
+
const liveUrl = normalizeOptionalContractText(entry.liveUrl);
|
|
252
|
+
return value ? { kind, value, summary, artifactType, previewCommand, previewCwd, previewReadyPath, liveUrl } : null;
|
|
253
|
+
})
|
|
254
|
+
.filter((entry) => !!entry);
|
|
255
|
+
}
|
|
256
|
+
function normalizeResultFollowUps(raw) {
|
|
257
|
+
if (!Array.isArray(raw)) {
|
|
258
|
+
return [];
|
|
259
|
+
}
|
|
260
|
+
return raw
|
|
261
|
+
.filter((entry) => !!entry && typeof entry === "object")
|
|
262
|
+
.map((entry) => {
|
|
263
|
+
const type = typeof entry.type === "string" && RESULT_FOLLOW_UP_TYPES.has(entry.type)
|
|
264
|
+
? entry.type
|
|
265
|
+
: null;
|
|
266
|
+
const reason = typeof entry.reason === "string" ? entry.reason.trim() : "";
|
|
267
|
+
if (!type || !reason) {
|
|
268
|
+
return null;
|
|
269
|
+
}
|
|
270
|
+
return {
|
|
271
|
+
type,
|
|
272
|
+
targetRole: normalizeContractRole(entry.targetRole),
|
|
273
|
+
reason,
|
|
274
|
+
};
|
|
275
|
+
})
|
|
276
|
+
.filter((entry) => !!entry);
|
|
277
|
+
}
|
|
278
|
+
function normalizeResultOutcome(raw) {
|
|
279
|
+
if (raw === "blocked" || raw === "failed") {
|
|
280
|
+
return raw;
|
|
281
|
+
}
|
|
282
|
+
return "completed";
|
|
283
|
+
}
|
|
284
|
+
function normalizeProgressStatus(raw) {
|
|
285
|
+
if (raw === "in_progress" || raw === "review") {
|
|
286
|
+
return raw;
|
|
287
|
+
}
|
|
288
|
+
return null;
|
|
289
|
+
}
|
|
290
|
+
function normalizeMessageIntent(raw) {
|
|
291
|
+
if (typeof raw !== "string") {
|
|
292
|
+
return null;
|
|
293
|
+
}
|
|
294
|
+
const normalized = raw.trim();
|
|
295
|
+
return MESSAGE_INTENTS.has(normalized) ? normalized : null;
|
|
296
|
+
}
|
|
297
|
+
function inferMessageIntent(type, content) {
|
|
298
|
+
if (type === "review-request") {
|
|
299
|
+
return "review-request";
|
|
300
|
+
}
|
|
301
|
+
if (content.includes("?")) {
|
|
302
|
+
return "question";
|
|
303
|
+
}
|
|
304
|
+
return type === "broadcast" ? "announcement" : "update";
|
|
305
|
+
}
|
|
306
|
+
function buildDefaultRequestedAction(intent, toRole) {
|
|
307
|
+
switch (intent) {
|
|
308
|
+
case "question":
|
|
309
|
+
return "Answer the question so the sender can continue safely.";
|
|
310
|
+
case "review-request":
|
|
311
|
+
return toRole
|
|
312
|
+
? `Review the referenced work as ${toRole} and reply with findings.`
|
|
313
|
+
: "Review the referenced work and reply with findings.";
|
|
314
|
+
case "handoff":
|
|
315
|
+
return "Take over the next step described in the handoff summary.";
|
|
316
|
+
case "announcement":
|
|
317
|
+
return "Read the update and align your work if needed.";
|
|
318
|
+
default:
|
|
319
|
+
return "";
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
function buildDefaultHandoffSummary(targetRole, reason) {
|
|
323
|
+
if (targetRole) {
|
|
324
|
+
return `Hand off the current task to ${targetRole}: ${summarizeContractText(reason)}`;
|
|
325
|
+
}
|
|
326
|
+
return `Hand off the current task: ${summarizeContractText(reason)}`;
|
|
327
|
+
}
|
|
328
|
+
function deriveMessageDetails(summary, content) {
|
|
329
|
+
const normalized = content.trim();
|
|
330
|
+
if (!normalized || normalized === summary) {
|
|
331
|
+
return undefined;
|
|
332
|
+
}
|
|
333
|
+
return normalized;
|
|
334
|
+
}
|
|
335
|
+
function firstMeaningfulLine(text) {
|
|
336
|
+
for (const line of text.split(/\n+/)) {
|
|
337
|
+
const normalized = line.replace(/^[-*]\s*/, "").trim();
|
|
338
|
+
if (normalized) {
|
|
339
|
+
return normalized;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
return "";
|
|
343
|
+
}
|
|
344
|
+
function extractQuestionOrBulletLines(text, maxItems, matcher) {
|
|
345
|
+
const lines = text
|
|
346
|
+
.split(/\n+/)
|
|
347
|
+
.map((line) => line.replace(/^[-*]\s*/, "").trim())
|
|
348
|
+
.filter(Boolean)
|
|
349
|
+
.filter((line) => !matcher || matcher.test(line));
|
|
350
|
+
return Array.from(new Set(lines.map((line) => summarizeContractText(line, 220)))).slice(0, maxItems);
|
|
351
|
+
}
|
|
352
|
+
const WEB_HTML_FILENAMES = new Set(["index.html", "app.html", "main.html", "home.html"]);
|
|
353
|
+
/**
|
|
354
|
+
* Default preview command used when the worker does not provide one.
|
|
355
|
+
* This is a minimal static file server — the worker (LLM) is responsible
|
|
356
|
+
* for providing the real, framework-appropriate command via previewCommand
|
|
357
|
+
* in its result contract.
|
|
358
|
+
*/
|
|
359
|
+
function inferPreviewCommand(_cwd, _resultText, _filePaths) {
|
|
360
|
+
return "npx -y serve -l {PORT}";
|
|
361
|
+
}
|
|
362
|
+
function isWebHtmlPath(filePath) {
|
|
363
|
+
const segments = filePath.replace(/\\/gu, "/").split("/");
|
|
364
|
+
const filename = segments[segments.length - 1] ?? "";
|
|
365
|
+
if (!filename.toLowerCase().endsWith(".html") && !filename.toLowerCase().endsWith(".htm")) {
|
|
366
|
+
return false;
|
|
367
|
+
}
|
|
368
|
+
if (filename.startsWith(".") || filename.includes(".config.") || filename.includes(".test.") || filename.includes(".spec.")) {
|
|
369
|
+
return false;
|
|
370
|
+
}
|
|
371
|
+
return true;
|
|
372
|
+
}
|
|
373
|
+
function inferResultDeliverables(result, error) {
|
|
374
|
+
if (error) {
|
|
375
|
+
return [{
|
|
376
|
+
kind: "note",
|
|
377
|
+
value: error,
|
|
378
|
+
summary: "Execution error surfaced by the worker.",
|
|
379
|
+
}];
|
|
380
|
+
}
|
|
381
|
+
const deliverables = [];
|
|
382
|
+
let webAppInferred = false;
|
|
383
|
+
const pathMatches = Array.from(result.matchAll(/(?:^|[\s:(`])([A-Za-z0-9_./-]+\.[A-Za-z0-9_-]+)/g))
|
|
384
|
+
.map((match) => match[1])
|
|
385
|
+
.filter(Boolean);
|
|
386
|
+
const uniquePaths = Array.from(new Set(pathMatches)).slice(0, 5);
|
|
387
|
+
for (const filePath of uniquePaths) {
|
|
388
|
+
if (!webAppInferred && isWebHtmlPath(filePath)) {
|
|
389
|
+
const segments = filePath.replace(/\\/gu, "/").split("/");
|
|
390
|
+
const filename = segments[segments.length - 1] ?? "";
|
|
391
|
+
const dirname = segments.length > 1 ? segments.slice(0, -1).join("/") : ".";
|
|
392
|
+
const isTopLevelHtml = WEB_HTML_FILENAMES.has(filename);
|
|
393
|
+
if (isTopLevelHtml || filename.toLowerCase().endsWith(".html")) {
|
|
394
|
+
webAppInferred = true;
|
|
395
|
+
deliverables.push({
|
|
396
|
+
kind: "directory",
|
|
397
|
+
value: dirname,
|
|
398
|
+
summary: `Web application at ${dirname}`,
|
|
399
|
+
artifactType: "web-app",
|
|
400
|
+
previewCommand: inferPreviewCommand(dirname, result, uniquePaths),
|
|
401
|
+
previewCwd: dirname,
|
|
402
|
+
previewReadyPath: "/",
|
|
403
|
+
});
|
|
404
|
+
continue;
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
deliverables.push({
|
|
408
|
+
kind: "file",
|
|
409
|
+
value: filePath,
|
|
410
|
+
});
|
|
411
|
+
}
|
|
412
|
+
if (deliverables.length > 0) {
|
|
413
|
+
return deliverables;
|
|
414
|
+
}
|
|
415
|
+
if (!result.trim()) {
|
|
416
|
+
return [];
|
|
417
|
+
}
|
|
418
|
+
return [{
|
|
419
|
+
kind: "note",
|
|
420
|
+
value: summarizeContractText(result, 300),
|
|
421
|
+
summary: "Backfilled from the worker's final reply.",
|
|
422
|
+
}];
|
|
423
|
+
}
|
|
424
|
+
/**
|
|
425
|
+
* Enrich existing deliverables with preview fields (artifactType, previewCommand, etc.)
|
|
426
|
+
* by inferring from the raw result text.
|
|
427
|
+
*
|
|
428
|
+
* This function only enriches when the worker did NOT already provide these fields.
|
|
429
|
+
* When the worker (LLM) submits a proper result contract with previewCommand and
|
|
430
|
+
* artifactType, we trust it completely — it knows its own code better than we do.
|
|
431
|
+
*
|
|
432
|
+
* Returns the contract with enriched deliverables if any enrichment happened, or null
|
|
433
|
+
* if no enrichment was needed.
|
|
434
|
+
*/
|
|
435
|
+
export function enrichDeliverablesWithPreviewInference(contract, resultText) {
|
|
436
|
+
const { deliverables } = contract;
|
|
437
|
+
const existingWebApp = deliverables.find((d) => d.artifactType === "web-app");
|
|
438
|
+
if (existingWebApp) {
|
|
439
|
+
// Worker already provided a web-app with a real previewCommand — trust it.
|
|
440
|
+
if (existingWebApp.previewCommand?.trim()) {
|
|
441
|
+
return null;
|
|
442
|
+
}
|
|
443
|
+
// Worker declared web-app but no command — try to provide a fallback command.
|
|
444
|
+
const cwd = existingWebApp.previewCwd?.trim();
|
|
445
|
+
if (cwd && cwd !== "." && cwd !== "./") {
|
|
446
|
+
// Has a real cwd but no command — provide generic static serve fallback
|
|
447
|
+
existingWebApp.previewCommand = inferPreviewCommand(cwd, resultText, deliverables.map((d) => d.value ?? ""));
|
|
448
|
+
return { ...contract, deliverables };
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
// Strategy 1: Extract file paths from result text to detect HTML files
|
|
452
|
+
const allSources = [resultText, ...deliverables.map((d) => d.summary ?? "")];
|
|
453
|
+
const pathMatches = Array.from(allSources.join("\n").matchAll(/(?:^|[\s:(,`])([A-Za-z0-9_./-]+\.[A-Za-z0-9_-]+)/g))
|
|
454
|
+
.map((match) => match[1])
|
|
455
|
+
.filter(Boolean);
|
|
456
|
+
for (const filePath of Array.from(new Set(pathMatches)).slice(0, 5)) {
|
|
457
|
+
if (isWebHtmlPath(filePath)) {
|
|
458
|
+
const segments = filePath.replace(/\\/gu, "/").split("/");
|
|
459
|
+
const filename = segments[segments.length - 1] ?? "";
|
|
460
|
+
const dirname = segments.length > 1 ? segments.slice(0, -1).join("/") : ".";
|
|
461
|
+
if (!filename.toLowerCase().endsWith(".html") && !filename.toLowerCase().endsWith(".htm")) {
|
|
462
|
+
continue;
|
|
463
|
+
}
|
|
464
|
+
const allDeliverablePaths = deliverables.map((d) => d.value ?? "");
|
|
465
|
+
const matchingIndex = deliverables.findIndex((d) => d.kind === "directory"
|
|
466
|
+
&& filePath.replace(/\\/gu, "/").startsWith(d.value?.replace(/\\/gu, "/").replace(/\/$/u, "")));
|
|
467
|
+
if (matchingIndex >= 0) {
|
|
468
|
+
deliverables[matchingIndex] = {
|
|
469
|
+
...deliverables[matchingIndex],
|
|
470
|
+
artifactType: "web-app",
|
|
471
|
+
previewCommand: inferPreviewCommand(dirname, resultText, allDeliverablePaths),
|
|
472
|
+
previewCwd: dirname,
|
|
473
|
+
previewReadyPath: "/",
|
|
474
|
+
summary: deliverables[matchingIndex].summary || `Web application at ${dirname}`,
|
|
475
|
+
};
|
|
476
|
+
}
|
|
477
|
+
else {
|
|
478
|
+
deliverables.push({
|
|
479
|
+
kind: "directory",
|
|
480
|
+
value: dirname,
|
|
481
|
+
summary: `Web application at ${dirname}`,
|
|
482
|
+
artifactType: "web-app",
|
|
483
|
+
previewCommand: inferPreviewCommand(dirname, resultText, allDeliverablePaths),
|
|
484
|
+
previewCwd: dirname,
|
|
485
|
+
previewReadyPath: "/",
|
|
486
|
+
});
|
|
487
|
+
}
|
|
488
|
+
return { ...contract, deliverables };
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
// Strategy 2: If result text mentions HTML/web/blog/site keywords and there's a
|
|
492
|
+
// directory deliverable without artifactType, infer web-app from the directory itself.
|
|
493
|
+
const webKeywords = /\b(html|web\s*app|web\s*site|blog|index\.html|app\.html|homepage)\b/i;
|
|
494
|
+
const hasWebKeywords = webKeywords.test(resultText) || deliverables.some((d) => webKeywords.test(d.summary ?? ""));
|
|
495
|
+
if (hasWebKeywords) {
|
|
496
|
+
const dirDeliverable = deliverables.find((d) => d.kind === "directory" && !d.artifactType && d.value);
|
|
497
|
+
if (dirDeliverable) {
|
|
498
|
+
const cwd = dirDeliverable.value.replace(/\/$/u, "");
|
|
499
|
+
const allDeliverablePaths = deliverables.map((d) => d.value ?? "");
|
|
500
|
+
deliverables[deliverables.indexOf(dirDeliverable)] = {
|
|
501
|
+
...dirDeliverable,
|
|
502
|
+
artifactType: "web-app",
|
|
503
|
+
previewCommand: inferPreviewCommand(cwd, resultText, allDeliverablePaths),
|
|
504
|
+
previewCwd: cwd,
|
|
505
|
+
previewReadyPath: "/",
|
|
506
|
+
summary: dirDeliverable.summary || `Web application at ${cwd}`,
|
|
507
|
+
};
|
|
508
|
+
return { ...contract, deliverables };
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
// Strategy 3: Extract directory paths that contain HTML files from result text
|
|
512
|
+
// (e.g. "blog/index.html" → directory "blog"). This handles cases where deliverables
|
|
513
|
+
// are all "note" type but result text references HTML files in directories.
|
|
514
|
+
const dirHtmlPattern = /([A-Za-z0-9_.-]+\/)[A-Za-z0-9_.-]+\.html\b/g;
|
|
515
|
+
const dirMatches = Array.from(resultText.matchAll(dirHtmlPattern)).map((m) => m[1].replace(/\/$/u, ""));
|
|
516
|
+
const uniqueDirs = Array.from(new Set(dirMatches));
|
|
517
|
+
if (uniqueDirs.length > 0 && hasWebKeywords) {
|
|
518
|
+
const dirname = uniqueDirs[0];
|
|
519
|
+
deliverables.push({
|
|
520
|
+
kind: "directory",
|
|
521
|
+
value: dirname,
|
|
522
|
+
summary: `Web application at ${dirname}`,
|
|
523
|
+
artifactType: "web-app",
|
|
524
|
+
previewCommand: inferPreviewCommand(dirname, resultText, [dirname]),
|
|
525
|
+
previewCwd: dirname,
|
|
526
|
+
previewReadyPath: "/",
|
|
527
|
+
});
|
|
528
|
+
return { ...contract, deliverables };
|
|
529
|
+
}
|
|
530
|
+
return null;
|
|
531
|
+
}
|