Files
nav-carte/worker/enrich.js
T
JulesandClaude Opus 5.5 27982b019b feat(aep-15): enrich.js branche R6 et R3 et propose un type
- base de R6 : la table des organisations (numéro, nom, adresse, statut),
  écartées comprises, hors fiches en attente ; illisible → R6 non fait ;
- R6 sur l'adresse avant le modèle ; page récupérée, adresse finale dans
  la table de redirections ; un seul appel au modèle, JSON étendu
  (objet, porteurs, désignations, type et postures proposés) ;
- R6 complet puis aiguillerLot sur le lot du passage ;
- tout dans ai_raw_output : rien d'appliqué, tout à trier, type et
  postures jamais dans leurs colonnes ; doublon → ai_doublon (repli
  ai_processed si la colonne refuse la valeur) ;
- reprise : une fiche récupérée mais pas enrichie rappelle le modèle avec
  sa page ;
- un « nouveau » ne dit pas plus sûr que son extraction.
Tests : branchement en mode à blanc (18), lot 1 entier par enrich.js.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-21 18:57:46 +01:00

737 lines
34 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env node
/**
* AEP — Worker enrichissement IA (B5-M2)
* Lancé via systemd timer toutes les 15 minutes (deploy/aep-worker/aep-worker.timer)
* Pipeline : fetch pending → scrape léger (fetch natif) → Bifrost (LLM) → update NocoDB → ntfy
*
* Écarts à la version NAV V2 d'origine (PIPE-IA-DOC.md, fusionné pas réécrit) :
* - LLM : Bifrost (passerelle OpenAI-compatible en prod) au lieu de Mistral direct.
* - Scrape : fetch natif Node 22 (timeout 8s, 500 Ko max, texte tronqué 4000 car.)
* au lieu de crawl4ai/Python (disque VPS tendu, décision MOE 27/09).
* - Notification : ntfy (https://ntfy.sh/$NTFY_TOPIC) au lieu de Resend (abandonné 15/07).
* Le message ne contient JAMAIS l'email ni le texte libre du contributeur.
* - Le formulaire assoupli (B5-M1) écrit un `nom` placeholder ("[à qualifier] host") et
* éventuellement "Type : non précisé" en tête de description_user. Le worker propose un
* vrai nom et un type — voir §Sortie JSON ci-dessous et PIPE-IA-DOC.md.
* - Seuil « email à 5 fiches pending » PAS réactivé (décision MOE : volume faible, une
* notif par fiche traitée suffit — cf. B5-proposer-pipe.md §Points d'attention).
*
* Lot 15 (21/11/2026) — les modules B17 branchés, sans rien de neuf en base :
* 1. la base de R6 (table des organisations, écartées comprises) est lue une fois par passage ;
* 2. R6 première passe sur l'adresse seule, avant le modèle (lib/fiches.js::passeAdresse) ;
* 3. la page est récupérée par lib/recupererPage.js (adresses internes refusées, redirections
* contrôlées) ; son adresse finale remplit la table de redirections de R6 ;
* 4. un seul appel au modèle, dont le JSON étend l'ancien (objet, porteurs, désignations,
* type d'organisation et postures proposés) ;
* 5. R6 complet puis R3 (aiguillerLot) sur le lot du passage ;
* 6. tout s'écrit dans le JSON de ai_raw_output : aucune fusion appliquée, aucun objet publié,
* type et postures jamais écrits dans leurs colonnes. Un doublon : moderation_status ai_doublon.
* Une fiche déjà récupérée mais pas enrichie (coupure) rappelle le modèle AVEC sa page.
*/
import { spawnSync, execSync } from 'child_process';
import { existsSync, writeFileSync, unlinkSync, readFileSync, mkdirSync } from 'fs';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';
import { messageFicheEnrichie, hoteDuLien } from './lib/messageNtfy.js';
import { recupererPage, controlerAdresse, tableRedirections } from './lib/recupererPage.js';
import { extractionDepuisModele } from './lib/extraction.js';
import {
CHAMPS_BASE, CHAMPS_ECRITS, VERSION_SORTIE, baseDepuisFiches, passeAdresse, resoudreLot,
propositionTaxonomie, statutApresEnrichissement, relireScrape, lienPrincipal,
} from './lib/fiches.js';
const __dirname = dirname(fileURLToPath(import.meta.url));
// ─── CONFIG DEPUIS .env ───────────────────────────────────────────────────────
const NOCODB_URL = process.env.NOCODB_URL || 'http://localhost:8070';
const NOCODB_TOKEN = process.env.NOCODB_TOKEN;
const NOCODB_BASE = process.env.NOCODB_BASE;
const NOCODB_TABLE_ORGAS = process.env.NOCODB_TABLE_ORGAS;
const NOCODB_TABLE_STATS = process.env.NOCODB_TABLE_STATS;
const BIFROST_URL = process.env.BIFROST_URL || 'http://127.0.0.1:8080';
const BIFROST_VK = process.env.BIFROST_VK;
const WORKER_MODEL = process.env.WORKER_MODEL || 'groq/openai/gpt-oss-20b'; // llama-3.1-8b-instant retiré par Groq (28/09)
const NTFY_TOPIC = process.env.NTFY_TOPIC;
const BUDGET_MAX_EUR = parseFloat(process.env.BUDGET_MAX_EUR || '20');
const WORKER_LIMIT = parseInt(process.env.WORKER_LIMIT || '5');
const LOCK_FILE = process.env.WORKER_LOCK_FILE || '/tmp/aep-worker.lock';
// Prix par 1M tokens (USD) — par défaut 0 (Groq gratuit dans le tier RAPIDE Bifrost).
// Réglable si le worker bascule un jour sur un modèle payant.
const WORKER_PRICE_IN = parseFloat(process.env.WORKER_PRICE_IN_USD_PER_M || '0') / 1_000_000;
const WORKER_PRICE_OUT = parseFloat(process.env.WORKER_PRICE_OUT_USD_PER_M || '0') / 1_000_000;
const USD_TO_EUR = 0.93;
// Scrape léger (délai, plafond d'octets et User-Agent : lib/recupererPage.js)
const SCRAPE_TEXT_MAX_CHARS = 4_000;
// ─── CLI ──────────────────────────────────────────────────────────────────────
const DRY_RUN = process.argv.includes('--dry-run');
// DRY_RUN_LIVE=1 : en dry-run, tente quand même de vrais appels réseau (scrape + Bifrost)
// pour un test manuel avec accès réseau — ne touche JAMAIS NocoDB en dry-run, dans tous les cas.
const DRY_RUN_LIVE = process.env.DRY_RUN_LIVE === '1';
// DRY_RUN_FIXTURE : un tableau de fiches (comme avant), ou un scénario
// { lignes, base, modele: { [Id]: sortie | null }, pages: { [url]: page | null } } (lot 15).
const DRY_RUN_FIXTURE = process.env.DRY_RUN_FIXTURE || join(__dirname, 'fixtures', 'dry-run-rows.json');
// ─── TAXONOMIE VALIDE (apostrophe typographique U+2019 comme NocoDB) ─────────
const VALID_FONCTIONS = [
'Juridique', 'Technique', 'Économique', 'Administratif', 'Chantier',
'Comptabilité', 'Développement', 'Formation', 'Gestion d’agence', 'Santé mentale'
];
const VALID_SUBMISSION_TYPES = ['ecosysteme', 'reseau', 'job', 'outil'];
// ─── MAPPING NORMALISATION TAGS ───────────────────────────────────────────────
const TAG_MAP = [
[['juridique', 'droit', 'litige', 'contrat', 'déontologie', 'décennale', 'médiation', 'pi ', 'propriété intellectuelle', 'ccag', 'marchés publics droit'], 'Juridique'],
[['technique', 're2020', 'thermique', 'structure', 'bim', 'dtu', 'acoustique', 'matériaux', 'simulation', 'opr', 'réserves', 'acv', 'pcd', 'eurocodes', 'pmc'], 'Technique'],
[['économique', 'prix', 'tarif', 'honoraire', 'devis', 'roi', 'financement', 'subvention', 'cee', 'maprimerenov', 'anah', 'business plan', 'pricing'], 'Économique'],
[['administratif', 'permis', 'plu', 'plui', 'erp', 'autorisation travaux', 'abf', 'patrimoine', 'marchés publics procédure', 'cctp', 'dpgf', 'concours', 'urbanisme'], 'Administratif'],
[['chantier', 'coordination chantier', 'det', 'suivi travaux', 'sps', 'sécurité chantier', 'planning chantier', 'entreprise', 'sous-traitance', 'réception travaux'], 'Chantier'],
[['comptabilité', 'fiscal', 'tva', 'bnc', 'bic', 'expert-comptable', 'bilan', 'trésorerie', 'transmission agence', 'création agence', 'micro'], 'Comptabilité'],
[['développement', 'prospection', 'commercial', 'client', 'réseau', 'candidature', 'consultation', 'acquisition', 'marketing', 'notoriété', 'ao '], 'Développement'],
[['formation', 'école', 'mooc', 'organisme', 'formation continue', 'cpf', 'dpc', 'cfaa'], 'Formation'],
[['gestion d’agence', 'gestion d\'agence', 'rh', 'recrutement', 'emploi', 'salaire', 'ccn', 'convention collective', 'idcc', 'temps de travail', 'management'], 'Gestion d’agence'],
[['santé mentale', 'burn-out', 'épuisement', 'souffrance', 'bien-être', 'harcèlement', 'stress', 'psychologique', 'équilibre'], 'Santé mentale'],
];
function normalizeTag(raw) {
const t = String(raw).toLowerCase().trim();
const exact = VALID_FONCTIONS.find(v => v.toLowerCase() === t);
if (exact) return exact;
for (const [patterns, normalized] of TAG_MAP) {
if (patterns.some(p => t.includes(p))) return normalized;
}
return null;
}
// ─── UTILITAIRES LOG ─────────────────────────────────────────────────────────
function log(...args) {
const ts = new Date().toISOString();
console.log(`[${ts}]`, ...args);
}
// ─── LOCK ANTI-OVERLAP ────────────────────────────────────────────────────────
function acquireLock() {
if (existsSync(LOCK_FILE)) {
const content = execSync(`cat ${LOCK_FILE}`).toString().trim();
const pid = parseInt(content);
try {
execSync(`kill -0 ${pid} 2>/dev/null`);
return false; // Process encore vivant
} catch {
log('Lock orphelin détecté, suppression');
unlinkSync(LOCK_FILE);
}
}
writeFileSync(LOCK_FILE, process.pid.toString());
return true;
}
function releaseLock() {
try { unlinkSync(LOCK_FILE); } catch {}
}
// ─── NOCODB API ──────────────────────────────────────────────────────────────
async function nocodbGet(path) {
const res = await fetch(`${NOCODB_URL}/api/v1/db/data/noco/${NOCODB_BASE}/${path}`, {
headers: { 'xc-token': NOCODB_TOKEN }
});
if (!res.ok) throw new Error(`NocoDB GET ${path} → ${res.status}: ${await res.text()}`);
return res.json();
}
async function nocodbPatch(tableId, rowId, data) {
const res = await fetch(`${NOCODB_URL}/api/v1/db/data/noco/${NOCODB_BASE}/${tableId}/${rowId}`, {
method: 'PATCH',
headers: { 'xc-token': NOCODB_TOKEN, 'Content-Type': 'application/json' },
body: JSON.stringify(data)
});
if (!res.ok) throw new Error(`NocoDB PATCH ${tableId}/${rowId} → ${res.status}: ${await res.text()}`);
return res.json();
}
async function nocodbPost(tableId, data) {
const res = await fetch(`${NOCODB_URL}/api/v1/db/data/noco/${NOCODB_BASE}/${tableId}`, {
method: 'POST',
headers: { 'xc-token': NOCODB_TOKEN, 'Content-Type': 'application/json' },
body: JSON.stringify(data)
});
if (!res.ok) throw new Error(`NocoDB POST ${tableId} → ${res.status}: ${await res.text()}`);
return res.json();
}
/** Écrit une mise à jour de fiche — no-op loggé en dry-run (jamais de PATCH réel). */
async function patchRow(rowId, data) {
if (DRY_RUN) {
log(`[dry-run] PATCH fiche ${rowId} :`, JSON.stringify(data));
return;
}
return nocodbPatch(NOCODB_TABLE_ORGAS, rowId, data);
}
// ─── BUDGET CIRCUIT BREAKER ──────────────────────────────────────────────────
async function getBudgetMoisCourant() {
if (DRY_RUN) return 0;
const now = new Date();
const year = now.getFullYear();
const month = now.getMonth(); // 0-indexed
// NocoDB ne supporte pas bien le filtre datetime — on récupère tout et filtre en JS
try {
const data = await nocodbGet(`${NOCODB_TABLE_STATS}?limit=1000&sort=-timestamp`);
const total = (data.list || []).reduce((sum, row) => {
const ts = new Date(row.timestamp || row.CreatedAt || 0);
if (ts.getFullYear() === year && ts.getMonth() === month) {
return sum + (parseFloat(row.cout_eur) || 0);
}
return sum;
}, 0);
return total;
} catch (e) {
log('Erreur lecture budget:', e.message);
return 0;
}
}
async function logUsage(usage, model, endpoint, orgaId) {
const tokensIn = usage?.prompt_tokens || 0;
const tokensOut = usage?.completion_tokens || 0;
const coutEur = ((tokensIn * WORKER_PRICE_IN) + (tokensOut * WORKER_PRICE_OUT)) * USD_TO_EUR;
if (DRY_RUN) {
log(`[dry-run] Usage (non loggé) : ${tokensIn}in + ${tokensOut}out = €${coutEur.toFixed(6)} (${model})`);
return coutEur;
}
await nocodbPost(NOCODB_TABLE_STATS, {
model,
endpoint,
tokens_in: tokensIn,
tokens_out: tokensOut,
cout_eur: parseFloat(coutEur.toFixed(6)),
timestamp: new Date().toISOString(),
orga_id: orgaId || null
});
log(`Usage log: ${tokensIn}in + ${tokensOut}out = €${coutEur.toFixed(6)} (${model})`);
return coutEur;
}
// ─── FETCH FICHES PENDING ────────────────────────────────────────────────────
async function fetchPendingRows() {
const data = await nocodbGet(
`${NOCODB_TABLE_ORGAS}?where=(moderation_status,eq,pending)~and(ai_processed,eq,false)&limit=${WORKER_LIMIT}&sort=submitted_at`
);
return data.list || [];
}
let scenario = null;
function chargerScenario() {
if (scenario) return scenario;
if (!existsSync(DRY_RUN_FIXTURE)) {
throw new Error(`Fixture dry-run introuvable : ${DRY_RUN_FIXTURE}`);
}
const raw = JSON.parse(readFileSync(DRY_RUN_FIXTURE, 'utf-8'));
scenario = Array.isArray(raw) ? { lignes: raw } : (raw.lignes ? raw : { lignes: [raw] });
return scenario;
}
function loadFixtureRows() {
return chargerScenario().lignes;
}
// ─── BASE DE R6 (lot 15) : la table des organisations, écartées comprises ────
// Numéro, nom, adresse, statut seulement. Paginée ; au plus BASE_MAX_PAGES × 1000 lignes.
const BASE_MAX_PAGES = 20;
async function lireLignesBase() {
if (DRY_RUN) {
const b = chargerScenario().base;
if (b === 'illisible') throw new Error('[mock dry-run] base illisible');
return b || [];
}
const lignes = [];
for (let page = 0; page < BASE_MAX_PAGES; page++) {
const data = await nocodbGet(`${NOCODB_TABLE_ORGAS}?fields=${CHAMPS_BASE.join(',')}&limit=1000&offset=${page * 1000}&sort=Id`);
lignes.push(...(data.list || []));
if (data.pageInfo?.isLastPage !== false || !(data.list || []).length) return lignes;
}
log(`Base R6 : plus de ${BASE_MAX_PAGES * 1000} lignes, lecture arrêtée (dédoublonnage partiel)`);
return lignes;
}
// ─── LIENS — extraits de url + description_user (format B5-M1 "Liens :\n...") ─
function extractLinks(row) {
const text = `${row.url || ''}\n${row.description_user || row.description || ''}`;
const matches = text.match(/https?:\/\/[^\s)"'<>]+/g) || [];
const cleaned = matches.map(u => u.replace(/[.,;:!?]+$/, ''));
return [...new Set(cleaned)];
}
// ─── SCRAPE LÉGER (fetch natif, pas de crawl4ai) ─────────────────────────────
// Lot 15 : la récupération passe par lib/recupererPage.js, qui refuse les adresses internes
// avant l'appel et après chaque redirection (suivie à la main) ; 8 s, 500 Ko, comme avant.
function extractMeta(html, key) {
const re1 = new RegExp(`<meta[^>]*(?:name|property)=["']${key}["'][^>]*content=["']([^"']*)["']`, 'i');
const re2 = new RegExp(`<meta[^>]*content=["']([^"']*)["'][^>]*(?:name|property)=["']${key}["']`, 'i');
return (html.match(re1) || html.match(re2))?.[1]?.trim() || null;
}
function extractOgTags(html) {
const og = {};
const re1 = /<meta[^>]*property=["']og:([a-zA-Z:_-]+)["'][^>]*content=["']([^"']*)["']/gi;
const re2 = /<meta[^>]*content=["']([^"']*)["'][^>]*property=["']og:([a-zA-Z:_-]+)["']/gi;
let m;
while ((m = re1.exec(html))) og[m[1]] = m[2];
while ((m = re2.exec(html))) if (!(m[2] in og)) og[m[2]] = m[1];
return og;
}
function decodeEntities(s) {
return s
.replace(/&nbsp;/g, ' ')
.replace(/&amp;/g, '&')
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>')
.replace(/&quot;/g, '"')
.replace(/&#0?39;/g, "'");
}
function extractVisibleText(html) {
let s = html
.replace(/<!--[\s\S]*?-->/g, ' ')
.replace(/<script[\s\S]*?<\/script>/gi, ' ')
.replace(/<style[\s\S]*?<\/style>/gi, ' ')
.replace(/<nav[\s\S]*?<\/nav>/gi, ' ')
.replace(/<footer[\s\S]*?<\/footer>/gi, ' ')
.replace(/<head[\s\S]*?<\/head>/gi, ' ');
s = s.replace(/<[^>]+>/g, ' ');
s = decodeEntities(s);
s = s.replace(/\s+/g, ' ').trim();
return s.slice(0, SCRAPE_TEXT_MAX_CHARS);
}
function extractTitle(html) {
return html.match(/<title[^>]*>([^<]*)<\/title>/i)?.[1]?.trim() || null;
}
async function scrapeLight(url) {
log(`Scraping léger: ${hoteDuLien(url) || 'adresse illisible'}`);
const { html, urlFinale, chaine } = await recupererPage(url);
return {
title: extractTitle(html),
metaDescription: extractMeta(html, 'description'),
og: extractOgTags(html),
text: extractVisibleText(html),
// Lot 15 : de quoi remplir la table de redirections de R6 (et la reprise après coupure).
url_finale: urlFinale,
redirections: chaine,
};
}
const MOCK_SCRAPE_RESULT = {
title: '[mock dry-run] Titre de la page',
metaDescription: '[mock dry-run] Meta description factice, aucun réseau contacté.',
og: { site_name: '[mock dry-run]' },
text: '[mock dry-run] Extrait de texte factice utilisé quand DRY_RUN_LIVE n’est pas activé.',
};
/** Page simulée en dry-run : le scénario d'abord, sinon le mock. Le contrôle d'adresse hors réseau s'applique. */
function pageSimulee(url) {
const c = controlerAdresse(url);
if (!c.ok) throw new Error(`adresse refusée : ${c.motif}`);
const pages = chargerScenario().pages || {};
if (url in pages) {
if (pages[url] === null) throw new Error('[mock dry-run] page injoignable');
const p = pages[url];
return { ...MOCK_SCRAPE_RESULT, ...p, url_finale: p.url_finale ?? c.url.href, redirections: p.redirections ?? [c.url.href, ...(p.url_finale ? [p.url_finale] : [])] };
}
return { ...MOCK_SCRAPE_RESULT, url_finale: c.url.href, redirections: [c.url.href] };
}
// ─── APPEL LLM VIA BIFROST ────────────────────────────────────────────────────
const SYSTEM_PROMPT = `Tu es un assistant qui aide à qualifier des ressources soumises pour une cartographie collaborative de l'écosystème professionnel de l'architecture en France (AEP).
RÈGLES ABSOLUES :
1. Tu ne dois JAMAIS inventer d'informations non présentes dans les sources fournies.
2. Si une information est absente ou incertaine, retourne \`null\` pour ce champ (ou une liste vide).
3. Tu dois retourner UNIQUEMENT un objet JSON valide, sans texte avant ou après.
4. "description" : neutre, factuelle, en français, max 300 caractères, sans jugement de valeur.
5. "nom" : un nom de fiche court et identifiable, sans le préfixe "[à qualifier]".
6. "tags" : 1 à 5 valeurs, uniquement parmi la liste autorisée.
7. "objet", "porteurs" et "designations" décrivent ce que le lien désigne (pas le site en général), d'après le contenu de la page. Le texte du contributeur dit pourquoi la ressource lui sert : n'en tire l'objet ou un porteur que s'il les nomme explicitement.
8. "type_organisation_suggere" et "postures_suggerees" : seulement les valeurs autorisées ci-dessous ; dans le doute, null et [].
TAGS AUTORISÉS : "Juridique" | "Technique" | "Économique" | "Administratif" | "Chantier" | "Comptabilité" | "Développement" | "Formation" | "Gestion d’agence" | "Santé mentale"
TYPE_SUGGERE AUTORISÉ (une seule valeur ou null) : "ecosysteme" | "reseau" | "job" | "outil"
TYPE_ORGANISATION_SUGGERE AUTORISÉ (une seule valeur ou null) : "institution" | "association" | "syndicat" | "agence" | "formation" | "media" | "autre"
POSTURES_SUGGEREES AUTORISÉES (0 à 3 valeurs) : "regenerative" | "bas-carbone" | "cooperative" | "patrimoine" | "ecoconstruction" | "low-tech" | "participatif"
FORMAT DE SORTIE JSON :
{
"nom": "string | null",
"description": "string (max 300 chars, français, neutre, factuel) | null",
"type_suggere": "ecosysteme" | "reseau" | "job" | "outil" | null,
"ville": "string | null",
"tags": ["string", "..."],
"type_organisation_suggere": "institution" | "association" | "syndicat" | "agence" | "formation" | "media" | "autre" | null,
"postures_suggerees": ["regenerative" | "bas-carbone" | "cooperative" | "patrimoine" | "ecoconstruction" | "low-tech" | "participatif"],
"objet": "nom de la chose que le lien désigne" | null,
"variantes": ["autres noms de la même chose"],
"porteurs": ["personnes ou organisations qui portent cette chose"],
"point_de_vue": "porteur" (publié par le porteur lui-même) | "tiers" (publié par quelqu'un d'autre) | null,
"designations": [
{
"nom": "une chose désignée par le lien",
"auteurs": [], "date": "AAAA-MM-JJ" | null,
"indices": {
"indexable": true si c'est un texte à lire et indexer (livre, article, PDF, vidéo transcrite),
"agit": true si c'est une organisation, un collectif ou un lieu qui agit aujourd'hui,
"peut_cesser": true si elle peut cesser d'exister,
"s_utilise": true si c'est un outil qu'on utilise ou fabrique,
"methode": true si c'est une méthode, une charte, un jeu de tableaux ou de patterns,
"personne": true si c'est une personne,
"concept": true si c'est une idée nommée
},
"licence": null, "depot": null
}
],
"confiance": "haute" | "moyenne" | "faible"
}
Le champ "confiance" reflète ta certitude globale :
- "haute" : contenu scrapé riche, informations claires.
- "moyenne" : contenu partiel, ou texte du contributeur seul mais clair.
- "faible" : aucun contenu scrapé et texte du contributeur vague, inférences importantes.`;
function buildUserPrompt(row, scrapeData, autresLiens) {
return `RESSOURCE À QUALIFIER :
Nom actuel (placeholder à remplacer) : ${row.nom}
Lien principal : ${row.url || 'non fourni'}
${autresLiens.length ? `Autres liens mentionnés par le contributeur : ${autresLiens.join(', ')}` : ''}
Texte du contributeur (pourquoi c'est pertinent pour lui) : ${row.description_user || row.description || 'non fourni'}
CONTENU EXTRAIT DU SITE (scraping léger) :
Titre : ${scrapeData?.title || 'non disponible'}
Meta description : ${scrapeData?.metaDescription || 'non disponible'}
Open Graph : ${scrapeData && Object.keys(scrapeData.og || {}).length ? JSON.stringify(scrapeData.og) : 'non disponible'}
Texte visible (extrait) : ${scrapeData?.text || 'Site non accessible ou lien non fourni.'}
---
Qualifie cette ressource selon les règles du system prompt. Retourne uniquement le JSON.`;
}
async function callBifrostWithRetry(row, scrapeData, autresLiens, maxRetries = 2) {
const userPrompt = buildUserPrompt(row, scrapeData, autresLiens);
for (let attempt = 0; attempt <= maxRetries; attempt++) {
if (attempt > 0) {
log(`Retry ${attempt}/${maxRetries} pour fiche ${row.Id}`);
await new Promise(r => setTimeout(r, 2000 * attempt));
}
try {
const res = await fetch(`${BIFROST_URL}/v1/chat/completions`, {
method: 'POST',
headers: {
'x-bf-vk': BIFROST_VK,
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: WORKER_MODEL,
temperature: 0.2,
// Lot 15 : 800 → 2000. La sortie étend l'ancienne (désignations, porteurs, type proposé),
// et gpt-oss prend ses jetons de raisonnement sur max_tokens (PIPE-IA-DOC §11.3).
max_tokens: 2000,
response_format: { type: 'json_object' },
messages: [
{ role: 'system', content: SYSTEM_PROMPT },
{ role: 'user', content: userPrompt }
]
}),
signal: AbortSignal.timeout(60_000)
});
if (!res.ok) {
const err = await res.text();
throw new Error(`Bifrost API ${res.status}: ${err}`);
}
const data = await res.json();
const content = data.choices?.[0]?.message?.content;
if (!content) throw new Error('Réponse Bifrost vide');
const parsed = JSON.parse(content);
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) throw new Error('Réponse Bifrost hors format');
parsed._usage = data.usage;
parsed._fin = data.choices?.[0]?.finish_reason ?? null;
parsed._raw = content;
return parsed;
} catch (e) {
log(`Erreur Bifrost (tentative ${attempt + 1}): ${e.message}`);
if (attempt === maxRetries) return null;
}
}
return null;
}
const MOCK_BIFROST_RESULT = {
nom: '[mock dry-run] Nom suggéré',
description: '[mock dry-run] Description factice générée sans appel réseau.',
type_suggere: 'ecosysteme',
ville: null,
tags: ['Développement'],
type_organisation_suggere: 'association',
postures_suggerees: ['cooperative'],
objet: '[mock dry-run] Objet',
variantes: [],
porteurs: ['[mock dry-run] Porteur'],
point_de_vue: 'porteur',
designations: [{ nom: '[mock dry-run] Objet', indices: { agit: true, peut_cesser: true } }],
confiance: 'faible',
_usage: { prompt_tokens: 0, completion_tokens: 0 },
_raw: '{"mock":"dry-run"}',
};
/** Sortie du modèle en dry-run : celle du scénario pour ce numéro (null = échec simulé), sinon le mock. */
function modeleSimule(row) {
const parFiche = chargerScenario().modele || {};
if (!(String(row.Id) in parFiche)) {
// Un objet et un porteur par fiche : le même mock pour deux fiches ferait un faux doublon.
const objet = `[mock dry-run] Objet ${row.Id}`;
return { ...MOCK_BIFROST_RESULT, objet, porteurs: [`[mock dry-run] Porteur ${row.Id}`], designations: [{ nom: objet, indices: { agit: true, peut_cesser: true } }] };
}
const sortie = parFiche[String(row.Id)];
return sortie === null ? null : { ...sortie, _usage: { prompt_tokens: 0, completion_tokens: 0 }, _raw: JSON.stringify(sortie) };
}
// ─── NOTIFICATION NTFY (jamais l'email ni le texte du contributeur) ──────────
async function notifyNtfy(title, message) {
if (DRY_RUN) {
log(`[dry-run] ntfy (non envoyé) — ${title} :`, message.replace(/\n/g, ' | '));
return;
}
if (!NTFY_TOPIC) {
log('NTFY_TOPIC absent, notification skippée');
return;
}
try {
const res = await fetch(`https://ntfy.sh/${NTFY_TOPIC}`, {
method: 'POST',
headers: { Title: title, Priority: '3', Tags: 'inbox_tray' },
body: message,
});
if (!res.ok) log('ntfy erreur:', await res.text());
else log('ntfy envoyé:', title);
} catch (e) {
log('ntfy exception:', e.message);
}
}
// ─── MAIN ─────────────────────────────────────────────────────────────────────
async function run() {
if (!DRY_RUN && !acquireLock()) {
log('Worker déjà en cours, skip');
process.exit(0);
}
const startTime = Date.now();
log(`=== Worker AEP enrichissement démarré${DRY_RUN ? ' (--dry-run)' : ''} ===`);
try {
if (!DRY_RUN) {
if (!NOCODB_TOKEN || !NOCODB_BASE || !NOCODB_TABLE_ORGAS || !BIFROST_VK) {
throw new Error('Variables .env manquantes (NOCODB_TOKEN, NOCODB_BASE, NOCODB_TABLE_ORGAS, BIFROST_VK)');
}
}
const budgetMois = await getBudgetMoisCourant();
log(`Budget mois courant: €${budgetMois.toFixed(4)} / €${BUDGET_MAX_EUR}`);
if (budgetMois >= BUDGET_MAX_EUR) {
log('Budget épuisé pour ce mois. Worker en pause.');
await notifyNtfy('[AEP] Budget IA épuisé', `Le budget de ${BUDGET_MAX_EUR}€ a été atteint. Worker en pause jusqu'au 1er du mois prochain.`);
return;
}
const rows = DRY_RUN ? loadFixtureRows() : await fetchPendingRows();
log(`${rows.length} fiche(s) à traiter${DRY_RUN ? ' (fixture)' : ''}`);
if (rows.length === 0) {
log('Rien à traiter.');
return;
}
// Lot 15 : la base de R6, lue une fois par passage. Illisible → R6 « non fait », l'enrichissement continue.
let base = null;
try {
const exclure = new Set(rows.map(r => r.Id));
base = baseDepuisFiches(await lireLignesBase(), { exclure });
log(`Base R6 : ${base.length} fiche(s) de comparaison`);
} catch (e) {
log(`Base R6 illisible (${e.message}) : dédoublonnage non fait sur ce passage`);
}
const useNetwork = !DRY_RUN || DRY_RUN_LIVE;
const enrichies = [];
// ── 1. Par fiche : R6 sur l'adresse, page, modèle ──────────────────────────
for (const row of rows) {
log(`--- Traitement fiche ${row.Id} ---`);
const budgetCheck = await getBudgetMoisCourant();
if (budgetCheck >= BUDGET_MAX_EUR) {
log('Budget atteint mid-pipeline, arrêt.');
break;
}
const liens = extractLinks(row);
const primaryUrl = lienPrincipal(row);
const autresLiens = liens.filter(l => l !== primaryUrl);
const passe1 = base ? passeAdresse(row, base) : null;
if (passe1) log(`R6 sur l'adresse : ${passe1.statut} (${passe1.confiance})`);
// Scraping (uniquement le lien principal — un seul champ scrape_content en base)
let scrapeData = null;
let refusPage = null;
const shouldScrape = primaryUrl && (row.scrape_status === 'pending' || !row.scrape_status);
if (shouldScrape) {
try {
scrapeData = useNetwork ? await scrapeLight(primaryUrl) : pageSimulee(primaryUrl);
await patchRow(row.Id, {
scrape_status: 'scraped',
scrape_content: JSON.stringify(scrapeData),
});
} catch (e) {
log(`Scrape échoué: ${e.message}`);
if (/^adresse refusée/.test(e.message)) refusPage = e.message;
await patchRow(row.Id, { scrape_status: 'failed' });
}
} else if (primaryUrl && row.scrape_status === 'scraped') {
// Reprise (inventaire §8, piège 4) : page déjà récupérée, fiche pas enrichie → la relire.
scrapeData = relireScrape(row.scrape_content);
log(scrapeData ? 'Reprise : page déjà récupérée, relue depuis scrape_content' : 'Reprise : scrape_content illisible, modèle sans la page');
} else if (!primaryUrl) {
await patchRow(row.Id, { scrape_status: 'no_link' });
}
// Appel LLM (Bifrost) — un seul par fiche
if (DRY_RUN) log(`[dry-run] invite (non envoyée) : page ${scrapeData ? `fournie (titre « ${scrapeData.title ?? 'aucun'} »)` : 'absente'}`);
const enriched = useNetwork
? await callBifrostWithRetry(row, scrapeData, autresLiens)
: modeleSimule(row);
if (!enriched) {
log(`Échec Bifrost sur fiche ${row.Id}, flag ai_error`);
await patchRow(row.Id, {
moderation_status: 'ai_error',
ai_processed: true,
ai_raw_output: JSON.stringify({ version: VERSION_SORTIE, erreur: 'modele', r6: { passe_adresse: passe1 } }),
});
continue;
}
await logUsage(enriched._usage, WORKER_MODEL, 'enrichissement', row.Id);
const extraction = extractionDepuisModele(enriched, { avecPage: Boolean(scrapeData) });
const redirections = tableRedirections(scrapeData?.redirections);
enrichies.push({ row, enriched, extraction, redirections, passe1, scrapeData, refusPage });
}
// ── 2. R6 complet puis R3, sur le lot du passage ───────────────────────────
let parFiche = new Map();
if (base && enrichies.length) {
try {
parFiche = resoudreLot(enrichies, base);
} catch (e) {
log(`R6/R3 en erreur (${e.message}) : propositions non faites sur ce passage`);
console.error(e.stack);
}
}
// ── 3. Écriture : les colonnes d'avant, le reste dans ai_raw_output ────────
let processedCount = 0;
for (const { row, enriched, extraction, passe1, scrapeData, refusPage } of enrichies) {
const rowStart = Date.now();
const resolu = parFiche.get(row.Id) ?? null;
// Normalisation tags
const rawTags = Array.isArray(enriched.tags) ? enriched.tags : (Array.isArray(enriched.tags_fonction) ? enriched.tags_fonction : []);
const normalizedTags = [...new Set(rawTags.map(normalizeTag).filter(Boolean))];
const updateData = {
description_enrichie: typeof enriched.description === 'string' ? enriched.description : null,
tags_fonction: normalizedTags.join(','),
moderation_status: statutApresEnrichissement(resolu?.r6),
ai_processed: true,
ai_raw_output: JSON.stringify({
output: enriched,
confiance: enriched.confiance,
version: VERSION_SORTIE,
proposition: propositionTaxonomie(enriched),
extraction,
page: scrapeData ? { url_finale: scrapeData.url_finale ?? null, redirections: scrapeData.redirections ?? [] } : (refusPage ? { refus: refusPage } : null),
r6: base ? { passe_adresse: passe1, ...(resolu?.r6 ?? { statut: 'non_fait', motif: 'R6/R3 en erreur sur ce passage' }) } : { statut: 'non_fait', motif: 'base illisible sur ce passage' },
r3: resolu?.r3 ?? null,
}),
};
// Nom : ne remplacer que le placeholder posé par le formulaire assoupli (B5-M1).
if (typeof enriched.nom === 'string' && enriched.nom && typeof row.nom === 'string' && row.nom.startsWith('[à qualifier]')) {
updateData.nom = enriched.nom;
}
if (typeof enriched.ville === 'string' && enriched.ville && !row.localisation_ville) {
updateData.localisation_ville = enriched.ville;
}
// submission_type : ne réajuster que si l'utilisateur n'avait pas choisi de chip
// (marqueur posé par utils/submitLibre.ts::toOrgaPayload — "Type : non précisé").
const typeNonPrecise = typeof row.description_user === 'string'
&& row.description_user.startsWith('Type : non précisé');
if (typeNonPrecise && VALID_SUBMISSION_TYPES.includes(enriched.type_suggere)) {
updateData.submission_type = enriched.type_suggere;
}
for (const champ of Object.keys(updateData)) {
if (!CHAMPS_ECRITS.has(champ)) throw new Error(`champ ${champ} hors de la liste écrite par le worker`);
}
try {
await patchRow(row.Id, updateData);
} catch (e) {
// ai_doublon est une valeur nouvelle de moderation_status : si la colonne la refuse,
// la fiche passe ai_processed (le doublon reste dit dans ai_raw_output) plutôt que de
// rester pending et d'être reprise toutes les quinze minutes.
if (updateData.moderation_status === 'ai_processed') throw e;
log(`PATCH refusé avec ${updateData.moderation_status} (${e.message}) : nouvel essai en ai_processed`);
await patchRow(row.Id, { ...updateData, moderation_status: 'ai_processed' });
}
// Lot 15a : jamais de texte de contributeur sur ntfy (sans lien : « sans titre » et le numéro).
await notifyNtfy('[AEP] Fiche enrichie', messageFicheEnrichie(row, updateData, enriched));
const elapsed = ((Date.now() - rowStart) / 1000).toFixed(1);
log(`Fiche ${row.Id} traitée en ${elapsed}s — confiance: ${enriched.confiance || 'nc'} — R6 : ${resolu?.r6?.statut ?? 'non fait'}`);
processedCount++;
}
log(`=== Run terminé: ${processedCount}/${rows.length} fiches traitées en ${((Date.now() - startTime) / 1000).toFixed(1)}s ===`);
} catch (e) {
log('ERREUR WORKER:', e.message);
console.error(e.stack);
process.exitCode = 1;
} finally {
if (!DRY_RUN) releaseLock();
}
}
run();