- 14 new Code node drivers implementing the pipeline stages: normalize
candidates, build context SQL, merge context + hash, decide AI need,
prepare AI prompt, validate AI response, AI-disabled fallback, reuse
cached classification, compute score, select top 10, prepare memory/
envio records, build message/digest, compare legacy x V2, debug panel.
- Consolidated the two context queries (followups/tasks) into a single
UNION ALL query tagged by tipo, and added a "Merge - Unir Shadow"
synchronization node before comparing legacy x V2: n8n does not
guarantee relative execution order between sibling branches that only
share an early common ancestor, so cross-branch data was only safe to
read by name once a real Merge node forced both to complete first.
- Reordered score calculation before the memory upsert (score is needed
in the persisted row) and turned both Data Table writes into
side-effect branches that fan out from the main path instead of
gating it, so the Data Table node's own output shape never needs to
carry the full pipeline state forward.
- Fixed a duplicate-identifier bug in the build script: score.js's
require('./cooldown') survived inlining and collided with
cooldown.js's own computeCooldown function in the same Code node
scope. build-workflow.js now strips inter-module requires when
inlining, and a new test syntax-checks every generated jsCode string
plus asserts no leftover require() calls.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
44 lines
1.4 KiB
JavaScript
44 lines
1.4 KiB
JavaScript
// @requires config.js cache.js
|
|
const config = parseConfig($env);
|
|
const now = Date.now();
|
|
|
|
const items = $input.all().map((item) => item.json); // { ticket, contextHash }
|
|
const memoryRows = $('Data Table - Buscar Memoria').all().map((item) => item.json);
|
|
const memoryByTicket = {};
|
|
for (const row of memoryRows) memoryByTicket[String(row.ticket_id)] = row;
|
|
|
|
const withDecision = items.map((item) => {
|
|
const memory = memoryByTicket[String(item.ticket.id)] || null;
|
|
const analysis = needsReanalysis({ memory, currentHash: item.contextHash, now });
|
|
return { ticket: item.ticket, contextHash: item.contextHash, memory, needsAiReason: analysis.reason, needsAiRaw: analysis.needsAi };
|
|
});
|
|
|
|
const needingAi = withDecision
|
|
.filter((item) => item.needsAiRaw)
|
|
.sort((a, b) => (Number(b.ticket.score_criticidade) || 0) - (Number(a.ticket.score_criticidade) || 0));
|
|
|
|
const allowedIds = new Set(needingAi.slice(0, config.aiMaxCandidates).map((item) => item.ticket.id));
|
|
|
|
return withDecision.map((item) => {
|
|
let resolucao;
|
|
if (!item.needsAiRaw) {
|
|
resolucao = 'cache';
|
|
} else if (allowedIds.has(item.ticket.id)) {
|
|
resolucao = 'ia';
|
|
} else {
|
|
resolucao = 'fallback_cap';
|
|
}
|
|
return {
|
|
json: {
|
|
ticket: item.ticket,
|
|
contextHash: item.contextHash,
|
|
memory: item.memory,
|
|
needsAiReason: item.needsAiReason,
|
|
resolucao,
|
|
precisa_ia: resolucao === 'ia',
|
|
config,
|
|
now,
|
|
},
|
|
};
|
|
});
|