$v, 'prompt'=>$prompt, 'ts'=>date('c'), 'score'=>null, 'tests'=>0]; file_put_contents($db, json_encode($versions, JSON_PRETTY_PRINT)); echo json_encode(['ok'=>true, 'intent'=>$intent, 'version'=>$v]); exit; } if ($action === 'score') { $intent = $_POST['intent'] ?? ''; $v = (int)$_POST['version']; $score = (float)$_POST['score']; foreach ($versions[$intent] ?? [] as &$pv) { if ($pv['v'] == $v) { $tests = $pv['tests'] ?? 0; $pv['score'] = ((($pv['score'] ?? 0) * $tests) + $score) / ($tests + 1); $pv['tests'] = $tests + 1; } } file_put_contents($db, json_encode($versions, JSON_PRETTY_PRINT)); echo json_encode(['ok'=>true]); exit; } if ($action === 'best') { $intent = $_GET['intent'] ?? ''; $list = $versions[$intent] ?? []; usort($list, fn($a,$b) => ($b['score']??0) <=> ($a['score']??0)); echo json_encode(['ok'=>true, 'best'=>$list[0] ?? null]); exit; } if ($action === 'evolve') { // Mutation: create new version from best with small tweak $intent = $_POST['intent'] ?? ''; $list = $versions[$intent] ?? []; if (!$list) { echo json_encode(['ok'=>false, 'error'=>'no parent']); exit; } usort($list, fn($a,$b) => ($b['score']??0) <=> ($a['score']??0)); $parent = $list[0]; $mutations = ['soyez concis', 'répondez étape par étape', 'vérifiez vos hypothèses', 'donnez 3 options', 'évitez le jargon']; $new_p = $parent['prompt'] . "\n\nAdjust: " . $mutations[array_rand($mutations)]; $v = count($versions[$intent]) + 1; $versions[$intent][] = ['v'=>$v, 'prompt'=>$new_p, 'ts'=>date('c'), 'score'=>null, 'tests'=>0, 'parent'=>$parent['v']]; file_put_contents($db, json_encode($versions, JSON_PRETTY_PRINT)); echo json_encode(['ok'=>true, 'new_version'=>$v, 'parent'=>$parent['v']]); exit; } echo json_encode(['ok'=>true, 'versions'=>$versions, 'actions'=>['add','score','best','evolve']]);