This commit is contained in:
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();
|
||||
Reference in New Issue
Block a user