This commit is contained in:
150
packages/worker/src/execution/coordinator.ts
Normal file
150
packages/worker/src/execution/coordinator.ts
Normal file
@@ -0,0 +1,150 @@
|
||||
import type { AgentMemberConfig, AgentResult, ExecutionDeps } from './parallel.js';
|
||||
|
||||
/**
|
||||
* Coordinator execution strategy: a lead agent plans and delegates,
|
||||
* worker agents execute subtasks, then the lead synthesizes results.
|
||||
* Best for complex tasks requiring decomposition and integration.
|
||||
*/
|
||||
export async function executeCoordinator(
|
||||
members: AgentMemberConfig[],
|
||||
taskInput: Record<string, unknown>,
|
||||
deps?: ExecutionDeps,
|
||||
): Promise<Record<string, unknown>> {
|
||||
const lead = members.find(m => m.member.roleInGroup === 'lead');
|
||||
const workers = members.filter(m => m.member.roleInGroup === 'worker');
|
||||
|
||||
if (!lead) {
|
||||
throw new Error('Coordinator strategy requires a lead agent');
|
||||
}
|
||||
|
||||
const task = (taskInput.task as string) ?? JSON.stringify(taskInput);
|
||||
|
||||
// ── Stub mode (backward compat) ──────────────────────────────────
|
||||
if (!deps) {
|
||||
const plan = {
|
||||
agentId: lead.agent.id,
|
||||
agentName: lead.agent.name,
|
||||
phase: 'planning',
|
||||
output: `[Stub] ${lead.agent.name} delegated to ${workers.length} workers`,
|
||||
subtasks: workers.map(w => ({
|
||||
assignedTo: w.agent.name,
|
||||
description: `[Stub] Subtask for ${w.agent.name}`,
|
||||
})),
|
||||
};
|
||||
|
||||
const workerResults = await Promise.all(
|
||||
workers.map(async ({ agent }) => ({
|
||||
agentId: agent.id,
|
||||
agentName: agent.name,
|
||||
phase: 'execution',
|
||||
model: agent.model,
|
||||
output: `[Stub] ${agent.name} completed delegated work`,
|
||||
})),
|
||||
);
|
||||
|
||||
const synthesis = {
|
||||
agentId: lead.agent.id,
|
||||
agentName: lead.agent.name,
|
||||
phase: 'synthesis',
|
||||
output: `[Stub] ${lead.agent.name} synthesized ${workerResults.length} worker outputs`,
|
||||
};
|
||||
|
||||
return {
|
||||
strategy: 'coordinator',
|
||||
leadAgent: lead.agent.name,
|
||||
workerCount: workers.length,
|
||||
plan,
|
||||
workerResults,
|
||||
synthesis,
|
||||
finalOutput: synthesis.output,
|
||||
};
|
||||
}
|
||||
|
||||
// ── Real execution: 3-phase pattern ──────────────────────────────
|
||||
|
||||
// Phase 1: Lead agent creates plan
|
||||
const leadPrompt = lead.agent.systemPrompt ?? `You are ${lead.agent.name}, a lead coordinator.`;
|
||||
const planResult = await deps.runAgent({
|
||||
model: lead.agent.model,
|
||||
systemPrompt: `${leadPrompt}\n\nYou are the lead coordinator. Analyze the task and create a clear plan for ${workers.length} worker agent(s). Output a structured plan with one subtask per worker.`,
|
||||
tools: deps.resolveTools(lead.agent.tools),
|
||||
messages: [{ role: 'user', content: task }],
|
||||
});
|
||||
|
||||
const plan = {
|
||||
agentId: lead.agent.id,
|
||||
agentName: lead.agent.name,
|
||||
phase: 'planning',
|
||||
output: planResult.content,
|
||||
usage: planResult.usage,
|
||||
};
|
||||
|
||||
// Phase 2: Workers execute in parallel, receiving the plan as context
|
||||
const workerResults: AgentResult[] = await Promise.all(
|
||||
workers.map(async ({ agent, member }) => {
|
||||
try {
|
||||
const workerPrompt = agent.systemPrompt ?? `You are ${agent.name}. Execute your assigned subtask.`;
|
||||
const result = await deps.runAgent({
|
||||
model: agent.model,
|
||||
systemPrompt: `${workerPrompt}\n\n## Coordinator's Plan\n${planResult.content}`,
|
||||
tools: deps.resolveTools(agent.tools),
|
||||
messages: [{ role: 'user', content: task }],
|
||||
});
|
||||
|
||||
return {
|
||||
agentId: agent.id,
|
||||
agentName: agent.name,
|
||||
role: member.roleInGroup,
|
||||
model: agent.model,
|
||||
output: result.content,
|
||||
toolsUsed: result.toolsUsed,
|
||||
usage: result.usage,
|
||||
};
|
||||
} catch (err: unknown) {
|
||||
return {
|
||||
agentId: agent.id,
|
||||
agentName: agent.name,
|
||||
role: member.roleInGroup,
|
||||
model: agent.model,
|
||||
output: '',
|
||||
error: err instanceof Error ? err.message : String(err),
|
||||
};
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
// Phase 3: Lead synthesizes worker results (include failure notices)
|
||||
const successOutputs = workerResults
|
||||
.filter(r => !r.error)
|
||||
.map(r => `## ${r.agentName}\n${r.output}`);
|
||||
const failureNotices = workerResults
|
||||
.filter(r => r.error)
|
||||
.map(r => `## ${r.agentName} (FAILED)\nError: ${r.error}`);
|
||||
const workerOutputs = [...successOutputs, ...failureNotices].join('\n\n');
|
||||
|
||||
const synthesisResult = await deps.runAgent({
|
||||
model: lead.agent.model,
|
||||
systemPrompt: `${leadPrompt}\n\nSynthesize the following worker outputs into a comprehensive final result.`,
|
||||
tools: deps.resolveTools(lead.agent.tools),
|
||||
messages: [{ role: 'user', content: workerOutputs || 'No worker outputs available.' }],
|
||||
});
|
||||
|
||||
const synthesis = {
|
||||
agentId: lead.agent.id,
|
||||
agentName: lead.agent.name,
|
||||
phase: 'synthesis',
|
||||
output: synthesisResult.content,
|
||||
usage: synthesisResult.usage,
|
||||
};
|
||||
|
||||
return {
|
||||
strategy: 'coordinator',
|
||||
leadAgent: lead.agent.name,
|
||||
workerCount: workers.length,
|
||||
plan,
|
||||
workerResults,
|
||||
synthesis,
|
||||
finalOutput: synthesis.output,
|
||||
errors: workerResults.filter(r => r.error).map(r => ({ agent: r.agentName, error: r.error })),
|
||||
};
|
||||
}
|
||||
99
packages/worker/src/execution/parallel.ts
Normal file
99
packages/worker/src/execution/parallel.ts
Normal file
@@ -0,0 +1,99 @@
|
||||
import type { ToolDefinition } from '@waggle/agent';
|
||||
|
||||
export interface AgentMemberConfig {
|
||||
member: { roleInGroup: string; executionOrder: number };
|
||||
agent: { id: string; name: string; model: string; systemPrompt: string | null; tools: string[] };
|
||||
}
|
||||
|
||||
/** Dependencies injected for real execution (omit for stub mode) */
|
||||
export interface ExecutionDeps {
|
||||
runAgent: (config: {
|
||||
model: string;
|
||||
systemPrompt: string;
|
||||
tools: ToolDefinition[];
|
||||
messages: Array<{ role: string; content: string }>;
|
||||
maxTurns?: number;
|
||||
}) => Promise<{ content: string; toolsUsed: string[]; usage: { inputTokens: number; outputTokens: number } }>;
|
||||
/** Resolve tool names to ToolDefinition[] from available tools */
|
||||
resolveTools: (toolNames: string[]) => ToolDefinition[];
|
||||
}
|
||||
|
||||
export interface AgentResult {
|
||||
agentId: string;
|
||||
agentName: string;
|
||||
role: string;
|
||||
model: string;
|
||||
output: string;
|
||||
toolsUsed?: string[];
|
||||
usage?: { inputTokens: number; outputTokens: number };
|
||||
error?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parallel execution strategy: all agents run concurrently, results are merged.
|
||||
* Best for independent subtasks that don't depend on each other.
|
||||
*/
|
||||
export async function executeParallel(
|
||||
members: AgentMemberConfig[],
|
||||
taskInput: Record<string, unknown>,
|
||||
deps?: ExecutionDeps,
|
||||
): Promise<Record<string, unknown>> {
|
||||
const task = (taskInput.task as string) ?? JSON.stringify(taskInput);
|
||||
|
||||
const results: AgentResult[] = await Promise.all(
|
||||
members.map(async ({ agent, member }) => {
|
||||
if (!deps) {
|
||||
// Stub mode (backward compat for existing tests without deps)
|
||||
return {
|
||||
agentId: agent.id,
|
||||
agentName: agent.name,
|
||||
role: member.roleInGroup,
|
||||
model: agent.model,
|
||||
output: `[Stub] ${agent.name} processed task with model ${agent.model}`,
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
const tools = deps.resolveTools(agent.tools);
|
||||
const systemPrompt = agent.systemPrompt ?? `You are ${agent.name}. Complete the assigned task thoroughly.`;
|
||||
|
||||
const result = await deps.runAgent({
|
||||
model: agent.model,
|
||||
systemPrompt,
|
||||
tools,
|
||||
messages: [{ role: 'user', content: task }],
|
||||
});
|
||||
|
||||
return {
|
||||
agentId: agent.id,
|
||||
agentName: agent.name,
|
||||
role: member.roleInGroup,
|
||||
model: agent.model,
|
||||
output: result.content,
|
||||
toolsUsed: result.toolsUsed,
|
||||
usage: result.usage,
|
||||
};
|
||||
} catch (err: unknown) {
|
||||
return {
|
||||
agentId: agent.id,
|
||||
agentName: agent.name,
|
||||
role: member.roleInGroup,
|
||||
model: agent.model,
|
||||
output: '',
|
||||
error: err instanceof Error ? err.message : String(err),
|
||||
};
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
return {
|
||||
strategy: 'parallel',
|
||||
agentCount: results.length,
|
||||
results,
|
||||
mergedOutput: results
|
||||
.filter(r => !r.error)
|
||||
.map(r => r.output)
|
||||
.join('\n\n---\n\n'),
|
||||
errors: results.filter(r => r.error).map(r => ({ agent: r.agentName, error: r.error })),
|
||||
};
|
||||
}
|
||||
85
packages/worker/src/execution/sequential.ts
Normal file
85
packages/worker/src/execution/sequential.ts
Normal file
@@ -0,0 +1,85 @@
|
||||
import type { AgentMemberConfig, AgentResult, ExecutionDeps } from './parallel.js';
|
||||
|
||||
/** Stub-mode result carries an extra `inputFrom` marker for backward-compat tests. */
|
||||
type StubAgentResult = AgentResult & { inputFrom: string };
|
||||
|
||||
/**
|
||||
* Sequential execution strategy: agents run one after another,
|
||||
* each receiving the previous agent's output as context.
|
||||
* Best for pipeline-style workflows (research -> draft -> review).
|
||||
*/
|
||||
export async function executeSequential(
|
||||
members: AgentMemberConfig[],
|
||||
taskInput: Record<string, unknown>,
|
||||
deps?: ExecutionDeps,
|
||||
): Promise<Record<string, unknown>> {
|
||||
const task = (taskInput.task as string) ?? JSON.stringify(taskInput);
|
||||
const results: AgentResult[] = [];
|
||||
let previousOutput: string | null = null;
|
||||
|
||||
for (const { agent, member } of members) {
|
||||
if (!deps) {
|
||||
// Stub mode (backward compat)
|
||||
const output: StubAgentResult = {
|
||||
agentId: agent.id,
|
||||
agentName: agent.name,
|
||||
role: member.roleInGroup,
|
||||
model: agent.model,
|
||||
output: `[Stub] ${agent.name} processed with input from previous step`,
|
||||
// Preserve inputFrom for backward compat with existing tests
|
||||
inputFrom: previousOutput === null ? 'taskInput' : (results[results.length - 1]?.agentName ?? 'taskInput'),
|
||||
};
|
||||
results.push(output);
|
||||
previousOutput = output.output;
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
const tools = deps.resolveTools(agent.tools);
|
||||
const basePrompt = agent.systemPrompt ?? `You are ${agent.name}. Complete the assigned task.`;
|
||||
|
||||
// Chain: inject previous agent's output as context
|
||||
const systemPrompt = previousOutput
|
||||
? `${basePrompt}\n\n## Previous Agent's Output\n${previousOutput}`
|
||||
: basePrompt;
|
||||
|
||||
const result = await deps.runAgent({
|
||||
model: agent.model,
|
||||
systemPrompt,
|
||||
tools,
|
||||
messages: [{ role: 'user', content: task }],
|
||||
});
|
||||
|
||||
const agentResult: AgentResult = {
|
||||
agentId: agent.id,
|
||||
agentName: agent.name,
|
||||
role: member.roleInGroup,
|
||||
model: agent.model,
|
||||
output: result.content,
|
||||
toolsUsed: result.toolsUsed,
|
||||
usage: result.usage,
|
||||
};
|
||||
results.push(agentResult);
|
||||
previousOutput = result.content;
|
||||
} catch (err: unknown) {
|
||||
// Error in chain stops execution — subsequent agents depend on this output
|
||||
results.push({
|
||||
agentId: agent.id,
|
||||
agentName: agent.name,
|
||||
role: member.roleInGroup,
|
||||
model: agent.model,
|
||||
output: '',
|
||||
error: err instanceof Error ? err.message : String(err),
|
||||
});
|
||||
break; // Stop chain on failure
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
strategy: 'sequential',
|
||||
agentCount: results.length,
|
||||
results,
|
||||
finalOutput: results.filter(r => !r.error).at(-1)?.output ?? null,
|
||||
chainBroken: results.some(r => r.error),
|
||||
};
|
||||
}
|
||||
40
packages/worker/src/handlers/chat-handler.ts
Normal file
40
packages/worker/src/handlers/chat-handler.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import type { Job } from 'bullmq';
|
||||
import type { JobData } from '../job-processor.js';
|
||||
import type { Db } from '../../../server/src/db/connection.js';
|
||||
import { runAgentLoop, createSystemTools } from '@waggle/agent';
|
||||
|
||||
const LITELLM_URL = process.env.LITELLM_URL ?? 'http://localhost:4000/v1';
|
||||
const LITELLM_API_KEY = process.env.LITELLM_API_KEY ?? process.env.LITELLM_MASTER_KEY ?? 'sk-waggle-dev';
|
||||
const DEFAULT_MODEL = process.env.DEFAULT_MODEL ?? 'claude-sonnet';
|
||||
|
||||
export async function chatHandler(job: Job<JobData>, _db: Db): Promise<Record<string, unknown>> {
|
||||
const { userId, input } = job.data;
|
||||
const message = (input as Record<string, unknown>).message as string ?? '';
|
||||
const model = (input as Record<string, unknown>).model as string ?? DEFAULT_MODEL;
|
||||
const workspaceDir = (input as Record<string, unknown>).workspaceDir as string ?? process.cwd();
|
||||
|
||||
const tools = createSystemTools(workspaceDir);
|
||||
|
||||
const systemPrompt = [
|
||||
'You are a Waggle AI agent. You help users with tasks using your tools.',
|
||||
'Use system tools (bash, read_file, write_file, edit_file, search_files, search_content) to interact with the workspace.',
|
||||
'Be helpful, concise, and proactive.',
|
||||
].join('\n');
|
||||
|
||||
const result = await runAgentLoop({
|
||||
litellmUrl: LITELLM_URL,
|
||||
litellmApiKey: LITELLM_API_KEY,
|
||||
model,
|
||||
systemPrompt,
|
||||
tools,
|
||||
messages: [{ role: 'user', content: message }],
|
||||
});
|
||||
|
||||
return {
|
||||
response: result.content,
|
||||
userId,
|
||||
model,
|
||||
toolsUsed: result.toolsUsed,
|
||||
tokensUsed: result.usage.inputTokens + result.usage.outputTokens,
|
||||
};
|
||||
}
|
||||
53
packages/worker/src/handlers/cron-handler.ts
Normal file
53
packages/worker/src/handlers/cron-handler.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import type { Job } from 'bullmq';
|
||||
import type { Db } from '../../../server/src/db/connection.js';
|
||||
import type { JobData, JobHandler } from '../job-processor.js';
|
||||
|
||||
const DELEGATABLE_JOB_TYPES = new Set(['chat', 'task', 'waggle', 'group']);
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
||||
}
|
||||
|
||||
export type CronDispatch = (job: Job<JobData>, db: Db) => Promise<Record<string, unknown>>;
|
||||
|
||||
/**
|
||||
* Execute a scheduled wrapper job through one of the worker's real handlers.
|
||||
* The explicit allowlist prevents cron jobs from recursively dispatching cron
|
||||
* or reaching an unregistered handler by passing arbitrary job types.
|
||||
*/
|
||||
export function createCronHandler(dispatch: CronDispatch): JobHandler {
|
||||
return async (job, db) => {
|
||||
const input = job.data.input;
|
||||
const delegatedJobType = typeof input.jobType === 'string' ? input.jobType.trim() : '';
|
||||
|
||||
if (!delegatedJobType) {
|
||||
throw new Error('cron job requires input.jobType (chat, task, waggle, or group)');
|
||||
}
|
||||
|
||||
if (!DELEGATABLE_JOB_TYPES.has(delegatedJobType)) {
|
||||
throw new Error(
|
||||
`cron job cannot delegate to "${delegatedJobType}"; supported types: ${[...DELEGATABLE_JOB_TYPES].join(', ')}`,
|
||||
);
|
||||
}
|
||||
|
||||
const delegatedInput = input.jobConfig ?? input.input;
|
||||
if (!isRecord(delegatedInput)) {
|
||||
throw new Error('cron job requires input.jobConfig or input.input as an object');
|
||||
}
|
||||
|
||||
const delegatedJob = {
|
||||
...job,
|
||||
data: {
|
||||
...job.data,
|
||||
jobType: delegatedJobType,
|
||||
input: delegatedInput,
|
||||
},
|
||||
} as Job<JobData>;
|
||||
|
||||
const result = await dispatch(delegatedJob, db);
|
||||
return {
|
||||
...result,
|
||||
cron: { delegatedJobType },
|
||||
};
|
||||
};
|
||||
}
|
||||
78
packages/worker/src/handlers/group-handler.ts
Normal file
78
packages/worker/src/handlers/group-handler.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
import type { Job } from 'bullmq';
|
||||
import type { JobData } from '../job-processor.js';
|
||||
import type { Db } from '../../../server/src/db/connection.js';
|
||||
import { agentGroups, agentGroupMembers, agents } from '../../../server/src/db/schema.js';
|
||||
import { eq } from 'drizzle-orm';
|
||||
import { executeParallel, type ExecutionDeps } from '../execution/parallel.js';
|
||||
import { executeSequential } from '../execution/sequential.js';
|
||||
import { executeCoordinator } from '../execution/coordinator.js';
|
||||
import { runAgentLoop, createSystemTools } from '@waggle/agent';
|
||||
|
||||
const LITELLM_URL = process.env.LITELLM_URL ?? 'http://localhost:4000/v1';
|
||||
const LITELLM_API_KEY = process.env.LITELLM_API_KEY ?? process.env.LITELLM_MASTER_KEY ?? 'sk-waggle-dev';
|
||||
|
||||
export async function groupHandler(job: Job<JobData>, db: Db): Promise<Record<string, unknown>> {
|
||||
const { input } = job.data;
|
||||
const groupId = (input as Record<string, unknown>).groupId as string;
|
||||
const taskInput = (input as Record<string, unknown>).taskInput as Record<string, unknown> ?? {};
|
||||
|
||||
if (!groupId) {
|
||||
throw new Error('groupHandler requires input.groupId');
|
||||
}
|
||||
|
||||
// Load group config
|
||||
const [group] = await db.select().from(agentGroups)
|
||||
.where(eq(agentGroups.id, groupId));
|
||||
|
||||
if (!group) {
|
||||
throw new Error(`Agent group not found: ${groupId}`);
|
||||
}
|
||||
|
||||
// Load members with their agent configs
|
||||
const members = await db.select({
|
||||
member: agentGroupMembers,
|
||||
agent: agents,
|
||||
})
|
||||
.from(agentGroupMembers)
|
||||
.innerJoin(agents, eq(agentGroupMembers.agentId, agents.id))
|
||||
.where(eq(agentGroupMembers.groupId, groupId));
|
||||
|
||||
if (members.length === 0) {
|
||||
throw new Error(`Agent group ${groupId} has no members`);
|
||||
}
|
||||
|
||||
// Sort by execution order
|
||||
members.sort((a: typeof members[number], b: typeof members[number]) => a.member.executionOrder - b.member.executionOrder);
|
||||
|
||||
// Build execution deps — wires strategies to real runAgentLoop
|
||||
const workspaceDir = (taskInput.workspaceDir as string) ?? process.cwd();
|
||||
const allTools = createSystemTools(workspaceDir);
|
||||
|
||||
const deps: ExecutionDeps = {
|
||||
runAgent: async (config) => runAgentLoop({
|
||||
litellmUrl: LITELLM_URL,
|
||||
litellmApiKey: LITELLM_API_KEY,
|
||||
model: config.model,
|
||||
systemPrompt: config.systemPrompt,
|
||||
tools: config.tools,
|
||||
messages: config.messages,
|
||||
maxTurns: config.maxTurns ?? 10,
|
||||
}),
|
||||
resolveTools: (toolNames) => {
|
||||
if (toolNames.length === 0) return allTools; // No filter = all tools
|
||||
return allTools.filter(t => toolNames.includes(t.name));
|
||||
},
|
||||
};
|
||||
|
||||
// Dispatch to strategy with real execution deps
|
||||
switch (group.strategy) {
|
||||
case 'parallel':
|
||||
return executeParallel(members, taskInput, deps);
|
||||
case 'sequential':
|
||||
return executeSequential(members, taskInput, deps);
|
||||
case 'coordinator':
|
||||
return executeCoordinator(members, taskInput, deps);
|
||||
default:
|
||||
throw new Error(`Unknown execution strategy: ${group.strategy}`);
|
||||
}
|
||||
}
|
||||
90
packages/worker/src/handlers/task-handler.ts
Normal file
90
packages/worker/src/handlers/task-handler.ts
Normal file
@@ -0,0 +1,90 @@
|
||||
/**
|
||||
* Task handler — runs agent loop with task context.
|
||||
* Replaces the stub implementation with real agent invocation.
|
||||
*/
|
||||
|
||||
import type { Job } from 'bullmq';
|
||||
import type { JobData } from '../job-processor.js';
|
||||
import type { Db } from '../../../server/src/db/connection.js';
|
||||
import { tasks } from '../../../server/src/db/schema.js';
|
||||
import { eq } from 'drizzle-orm';
|
||||
import { runAgentLoop, createSystemTools } from '@waggle/agent';
|
||||
|
||||
const LITELLM_URL = process.env.LITELLM_URL ?? 'http://localhost:4000/v1';
|
||||
const LITELLM_API_KEY = process.env.LITELLM_API_KEY ?? process.env.LITELLM_MASTER_KEY ?? 'sk-waggle-dev';
|
||||
const DEFAULT_MODEL = process.env.DEFAULT_MODEL ?? 'claude-sonnet';
|
||||
|
||||
export async function taskHandler(job: Job<JobData>, db: Db): Promise<Record<string, unknown>> {
|
||||
const { teamId, userId, input } = job.data;
|
||||
const taskId = (input as Record<string, unknown>).taskId as string | undefined;
|
||||
|
||||
if (!taskId) {
|
||||
throw new Error('taskHandler requires input.taskId');
|
||||
}
|
||||
|
||||
// Load task from DB to verify it exists and belongs to the team
|
||||
const [task] = await db.select().from(tasks)
|
||||
.where(eq(tasks.id, taskId));
|
||||
|
||||
if (!task) {
|
||||
throw new Error(`Task not found: ${taskId}`);
|
||||
}
|
||||
|
||||
if (task.teamId !== teamId) {
|
||||
throw new Error(`Task ${taskId} does not belong to team ${teamId}`);
|
||||
}
|
||||
|
||||
// Mark task as in-progress
|
||||
await db.update(tasks)
|
||||
.set({ status: 'in_progress', assignedTo: userId, updatedAt: new Date() })
|
||||
.where(eq(tasks.id, taskId));
|
||||
|
||||
// Build prompt from task context
|
||||
const systemPrompt = [
|
||||
'You are a Waggle AI agent executing an assigned task.',
|
||||
'Complete the task described below. Be thorough but concise.',
|
||||
'Use system tools (bash, read_file, write_file, edit_file, search_files, search_content) to interact with the workspace.',
|
||||
'',
|
||||
`Task: ${task.title}`,
|
||||
task.description ? `Details: ${task.description}` : '',
|
||||
task.priority ? `Priority: ${task.priority}` : '',
|
||||
].filter(Boolean).join('\n');
|
||||
|
||||
const workspaceDir = (input as Record<string, unknown>).workspaceDir as string ?? process.cwd();
|
||||
const model = (input as Record<string, unknown>).model as string ?? DEFAULT_MODEL;
|
||||
|
||||
try {
|
||||
const tools = createSystemTools(workspaceDir);
|
||||
|
||||
const result = await runAgentLoop({
|
||||
litellmUrl: LITELLM_URL,
|
||||
litellmApiKey: LITELLM_API_KEY,
|
||||
model,
|
||||
systemPrompt,
|
||||
tools,
|
||||
messages: [{ role: 'user', content: `Execute this task: ${task.title}${task.description ? '\n\n' + task.description : ''}` }],
|
||||
});
|
||||
|
||||
// Mark task as completed
|
||||
await db.update(tasks)
|
||||
.set({ status: 'completed', updatedAt: new Date() })
|
||||
.where(eq(tasks.id, taskId));
|
||||
|
||||
return {
|
||||
taskId,
|
||||
taskTitle: task.title,
|
||||
status: 'completed',
|
||||
result: result.content,
|
||||
toolsUsed: result.toolsUsed,
|
||||
tokensUsed: result.usage.inputTokens + result.usage.outputTokens,
|
||||
userId,
|
||||
};
|
||||
} catch (err) {
|
||||
// Reset task to open on failure (not stuck in in_progress)
|
||||
await db.update(tasks)
|
||||
.set({ status: 'open', updatedAt: new Date() })
|
||||
.where(eq(tasks.id, taskId));
|
||||
|
||||
throw err; // Let the worker mark the job as failed
|
||||
}
|
||||
}
|
||||
111
packages/worker/src/handlers/waggle-handler.ts
Normal file
111
packages/worker/src/handlers/waggle-handler.ts
Normal file
@@ -0,0 +1,111 @@
|
||||
/**
|
||||
* Waggle handler — routes Waggle Dance protocol messages through the dispatcher,
|
||||
* with fallback to hive query for legacy topic-based calls.
|
||||
*/
|
||||
|
||||
import type { Job } from 'bullmq';
|
||||
import type { JobData } from '../job-processor.js';
|
||||
import type { Db } from '../../../server/src/db/connection.js';
|
||||
import { teamEntities, tasks } from '../../../server/src/db/schema.js';
|
||||
import { sql } from 'drizzle-orm';
|
||||
import { WaggleDanceDispatcher } from '@waggle/waggle-dance';
|
||||
import type { WaggleMessage } from '@waggle/shared';
|
||||
import { CapabilityRouter, createSystemTools } from '@waggle/agent';
|
||||
|
||||
export interface WaggleHandlerDeps {
|
||||
enqueueWorker?: (input: {
|
||||
teamId: string;
|
||||
userId: string;
|
||||
task: string;
|
||||
role: string;
|
||||
context?: string;
|
||||
}) => Promise<string>;
|
||||
}
|
||||
|
||||
export function createWaggleHandler(deps: WaggleHandlerDeps = {}) {
|
||||
const capabilityRouter = new CapabilityRouter({
|
||||
toolNames: createSystemTools(process.cwd()).map(tool => tool.name),
|
||||
skills: [],
|
||||
plugins: [],
|
||||
mcpServers: [],
|
||||
subAgentRoles: ['researcher', 'writer', 'coder', 'analyst', 'reviewer', 'planner'],
|
||||
connectors: [],
|
||||
});
|
||||
|
||||
return async function waggleHandler(job: Job<JobData>, db: Db): Promise<Record<string, unknown>> {
|
||||
const { teamId, userId, input } = job.data;
|
||||
const inputObj = input as Record<string, unknown>;
|
||||
|
||||
// ── Protocol message routing (type + subtype present) ────────────
|
||||
if (inputObj.type && inputObj.subtype) {
|
||||
const dispatcher = new WaggleDanceDispatcher({
|
||||
searchMemory: async (query: string) => {
|
||||
const searchTerm = `%${query}%`;
|
||||
const entities = await db.select().from(teamEntities)
|
||||
.where(sql`${teamEntities.teamId} = ${teamId} AND ${teamEntities.name} ILIKE ${searchTerm}`)
|
||||
.limit(10);
|
||||
return entities.map((e: typeof entities[number]) => `[${e.entityType}] ${e.name}`).join('\n') || 'No matching knowledge found.';
|
||||
},
|
||||
resolveCapability: (query: string) => {
|
||||
return capabilityRouter.resolve(query).map(route => ({
|
||||
source: route.source,
|
||||
name: route.name,
|
||||
description: route.description,
|
||||
available: route.available,
|
||||
}));
|
||||
},
|
||||
spawnWorker: async (task: string, role: string, context?: string) => {
|
||||
if (!deps.enqueueWorker) {
|
||||
throw new Error('Worker delegation is unavailable in this runtime');
|
||||
}
|
||||
|
||||
const childJobId = await deps.enqueueWorker({ teamId, userId, task, role, ...(context ? { context } : {}) });
|
||||
return `Worker job ${childJobId} queued for: ${task} (${role})`;
|
||||
},
|
||||
});
|
||||
|
||||
// `inputObj` is untrusted job-data; the dispatcher revalidates the
|
||||
// type/subtype combo at runtime before acting on it.
|
||||
const result = await dispatcher.dispatch(inputObj as unknown as WaggleMessage);
|
||||
return {
|
||||
dispatched: true,
|
||||
type: inputObj.type,
|
||||
subtype: inputObj.subtype,
|
||||
...result,
|
||||
};
|
||||
}
|
||||
|
||||
// ── Legacy fallback: topic-based hive query ──────────────────────
|
||||
const topic = (inputObj.topic as string) ?? '';
|
||||
const searchTerm = `%${topic}%`;
|
||||
|
||||
// Query existing team knowledge
|
||||
const entities = await db.select().from(teamEntities)
|
||||
.where(sql`${teamEntities.teamId} = ${teamId} AND ${teamEntities.name} ILIKE ${searchTerm}`)
|
||||
.limit(5);
|
||||
|
||||
// Query related tasks
|
||||
const relatedTasks = await db.select().from(tasks)
|
||||
.where(sql`${tasks.teamId} = ${teamId} AND ${tasks.title} ILIKE ${searchTerm}`)
|
||||
.limit(5);
|
||||
|
||||
const gaps = [
|
||||
...(entities.length === 0 ? [`No team knowledge matched "${topic}".`] : []),
|
||||
...(relatedTasks.length === 0 ? [`No team tasks matched "${topic}".`] : []),
|
||||
];
|
||||
|
||||
return {
|
||||
topic,
|
||||
existingKnowledge: entities.length,
|
||||
entities: entities.map((e: typeof entities[number]) => ({ id: e.id, name: e.name, type: e.entityType })),
|
||||
relatedTasks: relatedTasks.length,
|
||||
tasks: relatedTasks.map((t: typeof relatedTasks[number]) => ({ id: t.id, title: t.title, status: t.status })),
|
||||
gaps,
|
||||
recommendation: entities.length === 0 && relatedTasks.length === 0
|
||||
? `Create a knowledge entry or task for "${topic}" so the team can build a shared record.`
|
||||
: `Review the ${entities.length} matching knowledge entr${entities.length === 1 ? 'y' : 'ies'} and ${relatedTasks.length} matching task${relatedTasks.length === 1 ? '' : 's'} for "${topic}".`,
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
export const waggleHandler = createWaggleHandler();
|
||||
106
packages/worker/src/index.ts
Normal file
106
packages/worker/src/index.ts
Normal file
@@ -0,0 +1,106 @@
|
||||
import { Worker } from 'bullmq';
|
||||
import { createDb } from '../../server/src/db/connection.js';
|
||||
import { agentJobs } from '../../server/src/db/schema.js';
|
||||
import { eq } from 'drizzle-orm';
|
||||
import { JobProcessor, type JobData } from './job-processor.js';
|
||||
import Redis from 'ioredis';
|
||||
import { chatHandler } from './handlers/chat-handler.js';
|
||||
import { taskHandler } from './handlers/task-handler.js';
|
||||
import { groupHandler } from './handlers/group-handler.js';
|
||||
import { createCronHandler } from './handlers/cron-handler.js';
|
||||
import { createWaggleHandler } from './handlers/waggle-handler.js';
|
||||
import { JobService } from '../../server/src/services/job-service.js';
|
||||
|
||||
const REDIS_URL = process.env.REDIS_URL ?? 'redis://localhost:6381';
|
||||
|
||||
function requireDatabaseUrl(): string {
|
||||
const url = process.env.DATABASE_URL;
|
||||
if (!url) throw new Error('DATABASE_URL environment variable is required');
|
||||
return url;
|
||||
}
|
||||
|
||||
export function createWorker(redisUrl = REDIS_URL, databaseUrl?: string, queueName = 'waggle-jobs') {
|
||||
const resolvedDbUrl = databaseUrl ?? requireDatabaseUrl();
|
||||
const db = createDb(resolvedDbUrl);
|
||||
const processor = new JobProcessor();
|
||||
const redisPub = new Redis(redisUrl);
|
||||
const jobService = new JobService(db, redisUrl, queueName);
|
||||
|
||||
// Register job handlers
|
||||
processor.register('chat', chatHandler);
|
||||
processor.register('task', taskHandler);
|
||||
processor.register('waggle', createWaggleHandler({
|
||||
enqueueWorker: async ({ teamId, userId, task, role, context }) => {
|
||||
const childJob = await jobService.createJob(teamId, userId, 'chat', {
|
||||
message: task,
|
||||
role,
|
||||
...(context ? { context } : {}),
|
||||
});
|
||||
return childJob.id;
|
||||
},
|
||||
}));
|
||||
processor.register('group', groupHandler);
|
||||
processor.register('cron', createCronHandler((job, handlerDb) => processor.process(job, handlerDb)));
|
||||
|
||||
const url = new URL(redisUrl);
|
||||
const worker = new Worker<JobData>(queueName, async (job) => {
|
||||
// Update status to running
|
||||
await db.update(agentJobs)
|
||||
.set({ status: 'running', startedAt: new Date() })
|
||||
.where(eq(agentJobs.id, job.data.jobId));
|
||||
|
||||
try {
|
||||
const result = await processor.process(job, db);
|
||||
|
||||
// Update status to completed
|
||||
await db.update(agentJobs)
|
||||
.set({ status: 'completed', completedAt: new Date(), output: result })
|
||||
.where(eq(agentJobs.id, job.data.jobId));
|
||||
|
||||
// Publish progress to Redis (includes teamId for gateway routing)
|
||||
await redisPub.publish(`job:${job.data.jobId}:progress`, JSON.stringify({
|
||||
status: 'completed',
|
||||
output: result,
|
||||
userId: job.data.userId,
|
||||
teamId: job.data.teamId,
|
||||
}));
|
||||
|
||||
return result;
|
||||
} catch (error: unknown) {
|
||||
const message = error instanceof Error ? error.message : String(error);
|
||||
await db.update(agentJobs)
|
||||
.set({ status: 'failed', completedAt: new Date(), output: { error: message } })
|
||||
.where(eq(agentJobs.id, job.data.jobId));
|
||||
throw error;
|
||||
}
|
||||
}, {
|
||||
connection: {
|
||||
host: url.hostname,
|
||||
port: parseInt(url.port || '6379'),
|
||||
},
|
||||
concurrency: 5,
|
||||
});
|
||||
|
||||
// Clean up shared Redis publisher when worker closes
|
||||
worker.on('closed', () => {
|
||||
redisPub.quit().catch(() => {});
|
||||
jobService.close().catch(() => {});
|
||||
});
|
||||
|
||||
return { worker, processor, db, redisPub, jobService };
|
||||
}
|
||||
|
||||
// Start if run directly
|
||||
const isDirectRun = process.argv[1]?.replace(/\\/g, '/').includes('worker/src/index');
|
||||
if (isDirectRun) {
|
||||
const { worker } = createWorker();
|
||||
console.log('Waggle agent worker started, waiting for jobs...');
|
||||
|
||||
worker.on('completed', (job) => {
|
||||
console.log(`Job ${job.id} completed`);
|
||||
});
|
||||
|
||||
worker.on('failed', (job, err) => {
|
||||
console.error(`Job ${job?.id} failed:`, err.message);
|
||||
});
|
||||
}
|
||||
28
packages/worker/src/job-processor.ts
Normal file
28
packages/worker/src/job-processor.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import type { Job } from 'bullmq';
|
||||
import type { Db } from '../../server/src/db/connection.js';
|
||||
|
||||
export interface JobData {
|
||||
jobId: string;
|
||||
teamId: string;
|
||||
userId: string;
|
||||
jobType: string;
|
||||
input: Record<string, unknown>;
|
||||
}
|
||||
|
||||
export type JobHandler = (job: Job<JobData>, db: Db) => Promise<Record<string, unknown>>;
|
||||
|
||||
export class JobProcessor {
|
||||
private handlers = new Map<string, JobHandler>();
|
||||
|
||||
register(jobType: string, handler: JobHandler): void {
|
||||
this.handlers.set(jobType, handler);
|
||||
}
|
||||
|
||||
async process(job: Job<JobData>, db: Db): Promise<Record<string, unknown>> {
|
||||
const handler = this.handlers.get(job.data.jobType);
|
||||
if (!handler) {
|
||||
throw new Error(`No handler registered for job type: ${job.data.jobType}`);
|
||||
}
|
||||
return handler(job, db);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user