Files
html/api/opus-arch-n8n-gen-v2.php
opus 6931d33a7d
Some checks failed
WEVAL NonReg / nonreg (push) Has been cancelled
V39 architect final: 7 remaining R&D caps wired (4/9/10/11/13/15/16) + doctrines 94-100
2026-04-17 19:26:56 +02:00

62 lines
3.0 KiB
PHP

<?php
// opus-arch-n8n-gen-v2.php - Cap 11 Enhanced (Doctrine 97)
header('Content-Type: application/json');
$action = $_GET['action'] ?? 'generate';
if ($action === 'generate') {
$description = $_POST['description'] ?? $_GET['description'] ?? '';
if (!$description) { echo json_encode(['ok'=>false,'error'=>'description required']); exit; }
// Simple NER: detect trigger/action/condition keywords
$triggers = [];
$actions = [];
$conditions = [];
$trigger_kw = ['when','quand','on','lorsque','si','arrive','receive','nouveau'];
$action_kw = ['send','envoie','create','cree','add','ajoute','update','post','notify','alert'];
$cond_kw = ['if','si','only if','seulement','except','sauf'];
foreach ($trigger_kw as $k) if (stripos($description, $k) !== false) $triggers[] = $k;
foreach ($action_kw as $k) if (stripos($description, $k) !== false) $actions[] = $k;
foreach ($cond_kw as $k) if (stripos($description, $k) !== false) $conditions[] = $k;
// Generate n8n workflow skeleton
$workflow = [
'name' => 'Auto-generated: ' . substr($description, 0, 60),
'nodes' => [
['id'=>'1', 'name'=>'Trigger', 'type'=>'webhook', 'parameters'=>['path'=>'auto-trigger-' . uniqid()]],
],
'connections' => [],
];
if (count($actions) > 0) {
$workflow['nodes'][] = ['id'=>'2', 'name'=>'Action Primary', 'type'=>'httpRequest', 'parameters'=>['method'=>'POST','url'=>'https://webhook.site/placeholder']];
$workflow['connections']['Trigger'] = ['main'=>[[['node'=>'Action Primary','type'=>'main','index'=>0]]]];
}
if (count($conditions) > 0) {
$workflow['nodes'][] = ['id'=>'3', 'name'=>'Condition', 'type'=>'if', 'parameters'=>['conditions'=>[]]];
}
echo json_encode(['ok'=>true, 'description'=>$description, 'detected'=>['triggers'=>$triggers, 'actions'=>$actions, 'conditions'=>$conditions], 'workflow'=>$workflow, 'note'=>'POST to n8n /workflows API to deploy']);
exit;
}
if ($action === 'deploy') {
$workflow = json_decode($_POST['workflow'] ?? '{}', true);
// Call n8n API to deploy
$secrets_lines = @file('/etc/weval/secrets.env');
$n8n_key = '';
foreach ($secrets_lines ?? [] as $line) {
if (preg_match('/^N8N_API_KEY=(.+)$/', trim($line), $m)) { $n8n_key = trim($m[1], '"'); break; }
}
if (!$n8n_key) { echo json_encode(['ok'=>false,'error'=>'n8n key missing']); exit; }
$ch = curl_init('http://127.0.0.1:5678/api/v1/workflows');
curl_setopt_array($ch, [CURLOPT_POST=>true, CURLOPT_POSTFIELDS=>json_encode($workflow), CURLOPT_RETURNTRANSFER=>true, CURLOPT_TIMEOUT=>10, CURLOPT_HTTPHEADER=>['Content-Type: application/json','X-N8N-API-KEY: '.$n8n_key]]);
$out = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
echo json_encode(['ok'=>$code<300, 'http_code'=>$code, 'response'=>substr($out, 0, 500)]);
exit;
}
echo json_encode(['ok'=>false, 'actions'=>['generate','deploy']]);