Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 45 additions & 27 deletions backend/src/routes/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,33 +371,14 @@ router.post('/', async (req: AuthRequest, res, next) => {
? [...new Set([...tags, ...(enrichmentResult.detectedTags || [])])]
: (enrichmentResult.detectedTags || []);

// Obter complexity via IntelligentEnrichmentService - I/O bound (slow)
// Obter complexity via Heurística (Sync) - Fast default
let taskComplexity: 'simple' | 'medium' | 'complex' = 'medium';
let suggestedAgent: string | null = null;
try {
const aiEnrichment = await intelligentEnrichment.enrichTask(
title,
description || null,
due_date ? new Date(due_date) : null,
{
userId: req.userId!,
userProfile: undefined,
userGoals: [],
userTasks: [],
userMemories: []
}
);
taskComplexity = aiEnrichment.complexity || 'medium';
suggestedAgent = aiEnrichment.suggestedAgentId || null;
} catch (error) {
console.warn('[Tasks] Erro ao obter complexity da IA, usando padrão:', error);
// Fallback: usar heurística simples baseada em título/descrição
const text = `${title} ${description || ''}`.toLowerCase();
if (text.length < 50 || text.match(/\b(call|email|send|check|read|view)\b/i)) {
taskComplexity = 'simple';
} else if (text.match(/\b(plan|develop|create|analyze|research|strategy)\b/i)) {
taskComplexity = 'complex';
}
const text = `${title} ${description || ''}`.toLowerCase();

if (text.length < 50 || text.match(/\b(call|email|send|check|read|view)\b/i)) {
taskComplexity = 'simple';
} else if (text.match(/\b(plan|develop|create|analyze|research|strategy)\b/i)) {
taskComplexity = 'complex';
}

// Now start transaction for insertion and strict integrity check
Expand Down Expand Up @@ -440,7 +421,7 @@ router.post('/', async (req: AuthRequest, res, next) => {
ai_confidence || null,
selected_agent || null,
taskComplexity,
suggestedAgent ?? null
null
]
);

Expand All @@ -458,6 +439,43 @@ router.post('/', async (req: AuthRequest, res, next) => {
}

const task = taskResult.task;

// ⚡ OPTIMIZATION: Trigger AI Enrichment in BACKGROUND (Fire and Forget)
// This prevents holding the response for seconds while waiting for the LLM.
intelligentEnrichment.enrichTask(
title,
description || null,
due_date ? new Date(due_date) : null,
{
userId: req.userId!,
userProfile: undefined,
userGoals: [],
userTasks: [],
userMemories: []
}
).then(async (aiEnrichment) => {
try {
if (aiEnrichment.complexity || aiEnrichment.suggestedAgentId) {
await query(
`UPDATE tasks SET
complexity = COALESCE($1, complexity),
suggested_agent = COALESCE($2, suggested_agent)
WHERE id = $3`,
[
aiEnrichment.complexity || null,
aiEnrichment.suggestedAgentId || null,
task.id
]
);
}
} catch (bgError) {
console.error('[Tasks] Background enrichment update failed:', bgError);
}
}).catch(err => {
// Silent fail or log warning, as this is background enhancement
console.warn('[Tasks] Background enrichment failed:', err);
});

// enrichmentResult is already available from outer scope

sendCreated(res, {
Expand Down
Loading