-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathworker.js
More file actions
398 lines (347 loc) · 15.5 KB
/
worker.js
File metadata and controls
398 lines (347 loc) · 15.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
import { createProvider } from './providers/index.js';
import { executeTool } from './tools/index.js';
import { closeSession } from './tools/browser.js';
import { getMissingCredential } from './utils/config.js';
import { getWorkerPrompt } from './prompts/workers.js';
import { buildSkillPrompt } from './skills/loader.js';
import { getLogger } from './utils/logger.js';
import { truncateToolResult } from './utils/truncate.js';
/**
* WorkerAgent — runs a scoped agent loop in the background.
* Extracted from Agent._runLoop() with simplifications:
* - No conversation persistence
* - No intent detection or persona extraction
* - No completion gate
* - Checks cancellation before each iteration and tool execution
* - Reports progress via callbacks
*/
export class WorkerAgent {
/**
* @param {object} opts
* @param {object} opts.config - Full app config (opts.config.brain used for LLM)
* @param {string} opts.workerType - coding, browser, system, devops, research
* @param {string} opts.jobId - Job ID for logging
* @param {Array} opts.tools - Scoped tool definitions
* @param {string[]|null} opts.skillIds - Active skill IDs (for worker prompt)
* @param {string|null} opts.workerContext - Structured context (conversation history, persona, dependency results)
* @param {object} opts.callbacks - { onProgress, onComplete, onError }
* @param {AbortController} opts.abortController - For cancellation
*/
constructor({ config, workerType, jobId, tools, skillIds, workerContext, callbacks, abortController }) {
this.config = config;
this.workerType = workerType;
this.jobId = jobId;
this.tools = tools;
this.skillIds = skillIds || [];
this.workerContext = workerContext || null;
this.callbacks = callbacks || {};
this.abortController = abortController || new AbortController();
this._cancelled = false;
this._toolCallCount = 0;
this._llmCallCount = 0;
this._errors = [];
// Create provider from worker brain config
this.provider = createProvider(config);
// Build system prompt with combined skill expertise
const skillPrompt = buildSkillPrompt(this.skillIds);
this.systemPrompt = getWorkerPrompt(workerType, config, skillPrompt);
// Safety ceiling — not a real limit, just prevents infinite loops
// The real limit is the job timeout enforced by JobManager
this.maxIterations = 200;
const logger = getLogger();
logger.info(`[Worker ${jobId}] Created: type=${workerType}, provider=${config.brain.provider}/${config.brain.model}, tools=${tools.length}, skills=${this.skillIds.length > 0 ? this.skillIds.join(',') : 'none'}, context=${workerContext ? 'yes' : 'none'}`);
}
/** Cancel this worker. */
cancel() {
this._cancelled = true;
this.abortController.abort();
getLogger().info(`[Worker ${this.jobId}] Cancel signal sent — aborting ${this.workerType} worker`);
}
/** Run the worker loop with the given task. */
async run(task) {
const logger = getLogger();
logger.info(`[Worker ${this.jobId}] Starting task: "${task.slice(0, 150)}"`);
// Build first message: context sections + task
let firstMessage = '';
if (this.workerContext) {
firstMessage += this.workerContext + '\n\n---\n\n';
}
firstMessage += task;
const messages = [{ role: 'user', content: firstMessage }];
try {
const result = await this._runLoop(messages);
if (this._cancelled) {
logger.info(`[Worker ${this.jobId}] Run completed but worker was cancelled — skipping callbacks`);
return;
}
const parsed = this._parseResult(result);
logger.info(`[Worker ${this.jobId}] Run finished successfully — structured=${!!parsed.structured}, result: "${(result || '').slice(0, 150)}"`);
if (this.callbacks.onComplete) this.callbacks.onComplete(result, parsed);
} catch (err) {
if (this._cancelled) {
logger.info(`[Worker ${this.jobId}] Run threw error but worker was cancelled — ignoring: ${err.message}`);
return;
}
logger.error(`[Worker ${this.jobId}] Run failed: ${err.message}`);
if (this.callbacks.onError) this.callbacks.onError(err);
} finally {
// Clean up browser session for this worker (frees the Puppeteer page)
closeSession(this.jobId).catch(() => {});
logger.info(`[Worker ${this.jobId}] Browser session cleaned up`);
}
}
async _runLoop(messages) {
const logger = getLogger();
let consecutiveAllFailIterations = 0; // Track iterations where ALL tool calls fail
for (let depth = 0; depth < this.maxIterations; depth++) {
if (this._cancelled) {
logger.info(`[Worker ${this.jobId}] Cancelled before iteration ${depth + 1}`);
throw new Error('Worker cancelled');
}
logger.info(`[Worker ${this.jobId}] LLM call ${depth + 1} — sending ${messages.length} messages`);
const response = await this.provider.chat({
system: this.systemPrompt,
messages,
tools: this.tools,
signal: this.abortController.signal,
});
this._llmCallCount++;
logger.info(`[Worker ${this.jobId}] LLM response: stopReason=${response.stopReason}, text=${(response.text || '').length} chars, toolCalls=${(response.toolCalls || []).length}`);
// Report stats to the job after each LLM call
this._reportStats(response.text || null);
if (this._cancelled) {
logger.info(`[Worker ${this.jobId}] Cancelled after LLM response`);
throw new Error('Worker cancelled');
}
// End turn — return the text
if (response.stopReason === 'end_turn') {
logger.info(`[Worker ${this.jobId}] End turn — final response: "${(response.text || '').slice(0, 200)}"`);
return response.text || 'Task completed.';
}
// Tool use
if (response.stopReason === 'tool_use') {
messages.push({ role: 'assistant', content: response.rawContent });
// Log thinking text
if (response.text && response.text.trim()) {
logger.info(`[Worker ${this.jobId}] Thinking: "${response.text.slice(0, 200)}"`);
}
const toolResults = [];
for (const block of response.toolCalls) {
if (this._cancelled) {
logger.info(`[Worker ${this.jobId}] Cancelled before executing tool ${block.name}`);
throw new Error('Worker cancelled');
}
const summary = this._formatToolSummary(block.name, block.input);
logger.info(`[Worker ${this.jobId}] Executing tool: ${block.name} — ${summary}`);
logger.debug(`[Worker ${this.jobId}] Tool input: ${JSON.stringify(block.input).slice(0, 300)}`);
this._reportProgress(`🔧 ${summary}`);
this._toolCallCount++;
const result = await executeTool(block.name, block.input, {
config: this.config,
user: null, // workers don't have user context
personaManager: null,
onUpdate: this.callbacks.onUpdate || null, // Real bot onUpdate (returns message_id for coder.js smart output)
sendPhoto: this.callbacks.sendPhoto || null,
sessionId: this.jobId, // Per-worker browser session isolation
signal: this.abortController.signal, // For killing child processes on cancellation
});
// Track errors
if (result && typeof result === 'object' && result.error) {
this._errors.push({ tool: block.name, error: result.error });
}
const resultStr = this._truncateResult(block.name, result);
logger.info(`[Worker ${this.jobId}] Tool ${block.name} result: ${resultStr.slice(0, 200)}`);
toolResults.push({
type: 'tool_result',
tool_use_id: block.id,
content: resultStr,
});
}
// Track consecutive all-fail iterations (circuit breaker)
const allFailed = toolResults.every(tr => {
try { const parsed = JSON.parse(tr.content); return !!parsed.error; } catch { return false; }
});
if (allFailed) {
consecutiveAllFailIterations++;
logger.warn(`[Worker ${this.jobId}] All ${toolResults.length} tool calls failed (streak: ${consecutiveAllFailIterations})`);
if (consecutiveAllFailIterations >= 3) {
logger.warn(`[Worker ${this.jobId}] Circuit breaker: 3 consecutive all-fail iterations — forcing stop`);
messages.push({ role: 'user', content: toolResults });
messages.push({
role: 'user',
content: 'STOP: All your tool calls have failed 3 times in a row. Do NOT call any more tools. Summarize whatever you have found so far, or explain what went wrong.',
});
const bailResponse = await this.provider.chat({
system: this.systemPrompt,
messages,
tools: [], // No tools — force text response
signal: this.abortController.signal,
});
return bailResponse.text || 'All tool calls failed repeatedly. Could not complete the task.';
}
} else {
consecutiveAllFailIterations = 0;
}
messages.push({ role: 'user', content: toolResults });
continue;
}
// Unexpected stop reason
logger.warn(`[Worker ${this.jobId}] Unexpected stopReason: ${response.stopReason}`);
return response.text || 'Worker finished with unexpected response.';
}
// Safety ceiling hit (should basically never happen — job timeout is the real limit)
logger.warn(`[Worker ${this.jobId}] Hit safety ceiling (${this.maxIterations} iterations) — requesting final summary`);
this._reportProgress(`⏳ Summarizing results...`);
try {
messages.push({
role: 'user',
content: 'You have reached the iteration limit. Summarize everything you have found and accomplished so far. Return a complete, detailed summary of all results, data, and findings.',
});
const summaryResponse = await this.provider.chat({
system: this.systemPrompt,
messages,
tools: [], // No tools — force text-only response
signal: this.abortController.signal,
});
const summary = summaryResponse.text || '';
logger.info(`[Worker ${this.jobId}] Final summary: "${summary.slice(0, 200)}"`);
if (summary.length > 10) {
return summary;
}
} catch (err) {
logger.warn(`[Worker ${this.jobId}] Summary call failed: ${err.message}`);
}
// Fallback: extract any text the LLM produced during the loop
const lastAssistantText = this._extractLastAssistantText(messages);
if (lastAssistantText) {
logger.info(`[Worker ${this.jobId}] Falling back to last assistant text: "${lastAssistantText.slice(0, 200)}"`);
return lastAssistantText;
}
return 'Worker finished but could not produce a final summary.';
}
/** Extract the last meaningful assistant text from message history. */
_extractLastAssistantText(messages) {
for (let i = messages.length - 1; i >= 0; i--) {
const msg = messages[i];
if (msg.role !== 'assistant') continue;
if (typeof msg.content === 'string' && msg.content.trim()) {
return msg.content.trim();
}
if (Array.isArray(msg.content)) {
const texts = msg.content
.filter(b => b.type === 'text' && b.text?.trim())
.map(b => b.text.trim());
if (texts.length > 0) return texts.join('\n');
}
}
return null;
}
/**
* Parse the worker's final text into a structured WorkerResult.
* Attempts JSON parse from ```json fences, falls back to wrapping raw text.
*/
_parseResult(text) {
if (!text) {
return {
structured: false,
summary: 'Task completed.',
status: 'success',
details: '',
artifacts: [],
followUp: null,
toolsUsed: this._toolCallCount,
errors: this._errors,
};
}
const _str = (v) => typeof v === 'string' ? v : (v ? JSON.stringify(v, null, 2) : '');
/** Try to build a structured result from a parsed JSON object. */
const _fromParsed = (parsed) => {
if (!parsed?.summary || !parsed?.status) return null;
return {
structured: true,
summary: String(parsed.summary || ''),
status: String(parsed.status || 'success'),
details: _str(parsed.details),
artifacts: Array.isArray(parsed.artifacts) ? parsed.artifacts : [],
followUp: parsed.followUp ? String(parsed.followUp) : null,
toolsUsed: this._toolCallCount,
errors: this._errors,
};
};
// Try to extract JSON from ```json ... ``` fences
const fenceMatch = text.match(/```json\s*\n?([\s\S]*?)\n?\s*```/);
if (fenceMatch) {
try {
const result = _fromParsed(JSON.parse(fenceMatch[1]));
if (result) return result;
} catch { /* fall through */ }
}
// Try raw JSON parse (no fences)
try {
const result = _fromParsed(JSON.parse(text));
if (result) return result;
} catch { /* fall through */ }
// Fallback: wrap raw text
return {
structured: false,
summary: text.slice(0, 200),
status: 'success',
details: text,
artifacts: [],
followUp: null,
toolsUsed: this._toolCallCount,
errors: this._errors,
};
}
_reportProgress(text) {
if (this.callbacks.onProgress) {
try { this.callbacks.onProgress(text); } catch (err) {
getLogger().debug(`[Worker ${this.jobId}] onProgress callback error: ${err.message}`);
}
}
if (this.callbacks.onHeartbeat) {
try { this.callbacks.onHeartbeat(text); } catch (err) {
getLogger().debug(`[Worker ${this.jobId}] onHeartbeat callback error: ${err.message}`);
}
}
}
_reportStats(thinking) {
if (this.callbacks.onStats) {
try {
this.callbacks.onStats({
llmCalls: this._llmCallCount,
toolCalls: this._toolCallCount,
lastThinking: thinking || null,
});
} catch (err) {
getLogger().debug(`[Worker ${this.jobId}] onStats callback error: ${err.message}`);
}
}
}
_truncateResult(name, result) {
return truncateToolResult(name, result);
}
_formatToolSummary(name, input) {
const _short = (s, len = 80) => s && s.length > len ? s.slice(0, len) + '...' : s;
const _host = (url) => { try { return new URL(url).hostname; } catch { return url; } };
switch (name) {
case 'web_search': return `Searching: "${_short(input.query, 60)}"`;
case 'browse_website': return `Opening ${_host(input.url)}`;
case 'interact_with_page': return 'Interacting with page';
case 'extract_content': return 'Extracting content';
case 'screenshot_website': return `Screenshot of ${_host(input.url)}`;
case 'execute_command': return `Running: ${_short(input.command, 60)}`;
case 'read_file': return `Reading ${_short(input.path)}`;
case 'write_file': return `Writing ${_short(input.path)}`;
case 'git_clone': return `Cloning ${_short(input.repo)}`;
case 'git_checkout': return `Switching to ${input.branch}`;
case 'git_commit': return `Committing: "${_short(input.message, 50)}"`;
case 'git_push': return 'Pushing changes';
case 'github_create_pr': return `Creating PR: "${_short(input.title, 50)}"`;
case 'spawn_claude_code': return `Coding: ${_short(input.prompt, 60)}`;
case 'docker_exec': return `Running in ${_short(input.container)}`;
case 'docker_compose': return `Docker compose ${input.action}`;
default: return `${name}`;
}
}
}