Files
html/api/ambre-tool-translate-code.php
Opus d7871f7f73 feat(wevia-godmode-v3): 17 generators auto-intent router + 7 new premium APIs
NEW GENERATORS (V3 GODMODE):
- ambre-tool-3d.php: Three.js r128 scenes interactives (OrbitControls + anim loop + fog)
- ambre-tool-dataviz.php: Dashboards Plotly.js (3-4 charts + KPI cards + responsive grid)
- ambre-tool-site.php: Landing pages SaaS COMPLETES 10 sections (header/hero/features/pricing/FAQ/footer)
- ambre-tool-sql.php: NL -> SQL multi-dialect (PG/MySQL/SQLite) avec explanation + indexes suggested
- ambre-tool-brainstorm.php: Multi-IA PARALLELE 5 providers (cerebras+groq+sambanova+gemini+cloudflare) + synthese
- ambre-tool-image-gen.php: Text2Image avec cascade sovereign + fallback ambre-image
- ambre-tool-translate-code.php: Code translator multi-langages (Python/JS/TS/Go/Rust/Java/Ruby)

ROUTER V3:
- 17 generators catalogues (4 docs + 7 GODMODE + 6 utilities)
- detectIntent() NL regex français/anglais
- extractPayload() nettoyage intelligent
- Rendering adapte par kind: docx/xlsx/pptx/react (preview panel), 3d (three.js iframe), image (inline img), code (pre+copy btn), json (summary card OR brainstorm providers_used), inline (calc), audio (player)

SAFETY PUBLIC:
- Zero secret WEVAL divulgue dans prompts
- Zero acces vault/credentials/serveurs internes
- Sovereign cascade uniquement (0€ LLM cost)
- Tous prompts contraints 'info generique safe'

TESTED LIVE:
- SQL generator PostgreSQL validated (json_agg + INNER JOIN + GROUP BY)
- DOCX 7 sections + XLSX 3 sheets + PPTX 10 slides + REACT standalone (all previously tested 1d24e243c commit)

17 intents auto-detectes dans wevia.html public widget.
WEVIA public maintenant aussi capable qu'un copilot grand public tout en restant safe sur secrets WEVAL.
2026-04-24 21:44:55 +02:00

92 lines
3.5 KiB
PHP

<?php
/**
* ambre-tool-translate-code.php — Translate code JS->Python, Python->JS, etc.
*/
header('Content-Type: application/json');
if ($_SERVER['REQUEST_METHOD'] !== 'POST') { echo json_encode(['ok'=>false,'error'=>'POST only']); exit; }
$input = json_decode(file_get_contents('php://input'), true);
$code = trim($input['code'] ?? '');
$from_lang = $input['from'] ?? 'auto';
$to_lang = $input['to'] ?? 'python';
// If topic was sent instead (from intent router), parse "translate X from Y to Z"
if (!$code && isset($input['topic'])) {
$topic = $input['topic'];
// Try extract code block if present
if (preg_match('/```(?:\w+)?\s*\n(.*?)\n```/s', $topic, $m)) {
$code = $m[1];
} else {
$code = $topic; // treat topic as code
}
// Detect to_lang
foreach (['python','javascript','typescript','go','rust','java','csharp','php','ruby','kotlin','swift'] as $lang) {
if (stripos($topic, $lang) !== false) { $to_lang = $lang; break; }
}
}
if (strlen($code) < 5) { echo json_encode(['ok'=>false,'error'=>'code too short']); exit; }
$code = substr($code, 0, 4000);
$prompt = "Expert polyglot programmer. Traduis ce code" . ($from_lang !== 'auto' ? " de $from_lang" : "") . " vers $to_lang:\n\n"
. "```\n$code\n```\n\n"
. "Retourne UNIQUEMENT un JSON:\n"
. "{\n"
. " \"from\": \"langue detectee\",\n"
. " \"to\": \"$to_lang\",\n"
. " \"code\": \"<code traduit complet>\",\n"
. " \"notes\": \"Notes sur differences idiomatiques importantes\",\n"
. " \"dependencies\": [\"package1\", \"package2\"]\n"
. "}\n\n"
. "IMPORTANT:\n"
. "- Code traduit IDIOMATIQUE dans la langue cible (pas traduction literale)\n"
. "- Utiliser les conventions modernes (ES2024/Python3.12/Go1.22/etc)\n"
. "- Preserver les commentaires si presents, traduits en FR\n"
. "- JSON valide uniquement, aucun texte avant/apres\n"
. "- dependencies = liste des libs a installer";
$ch = curl_init('http://127.0.0.1:4000/v1/chat/completions');
curl_setopt_array($ch, [
CURLOPT_POST => true, CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => json_encode([
'model' => 'auto',
'messages' => [['role'=>'user', 'content'=>$prompt]],
'max_tokens' => 3500, 'temperature' => 0.3
]),
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
CURLOPT_TIMEOUT => 90,
]);
$resp = curl_exec($ch);
$http = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($http !== 200) { echo json_encode(['ok'=>false,'error'=>"LLM HTTP $http"]); exit; }
$data = json_decode($resp, true);
$content_raw = $data['choices'][0]['message']['content'] ?? '';
if (preg_match('/```(?:json)?\s*\n?(.*?)\n?```/s', $content_raw, $m)) { $content_raw = $m[1]; }
$jstart = strpos($content_raw, '{');
if ($jstart !== false) {
$depth = 0; $jend = -1;
for ($i = $jstart; $i < strlen($content_raw); $i++) {
if ($content_raw[$i] === '{') $depth++;
elseif ($content_raw[$i] === '}') { $depth--; if ($depth === 0) { $jend = $i; break; } }
}
if ($jend > $jstart) $content_raw = substr($content_raw, $jstart, $jend - $jstart + 1);
}
$result = json_decode($content_raw, true);
if (!$result || !isset($result['code'])) {
echo json_encode(['ok'=>false,'error'=>'invalid JSON','raw'=>substr($content_raw,0,300)]); exit;
}
echo json_encode([
'ok' => true,
'from' => $result['from'] ?? $from_lang,
'to' => $result['to'] ?? $to_lang,
'code' => $result['code'],
'notes' => $result['notes'] ?? '',
'dependencies' => $result['dependencies'] ?? [],
'result' => $result['code'], // for inline render
]);