165 lines
7.6 KiB
PHP
165 lines
7.6 KiB
PHP
<?php
|
|
/**
|
|
* WEVCODE v2.0 — Sovereign Coding Agent API
|
|
* Uses: CodeAnalyzer, Planner, ToolUseV2, cognitive-opus46 (635 functions)
|
|
* Modes: code, analyze, plan, execute, git
|
|
*/
|
|
header('Content-Type: application/json');
|
|
header('Access-Control-Allow-Origin: *');
|
|
header('Access-Control-Allow-Headers: Content-Type');
|
|
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') exit;
|
|
|
|
// Load cognitive brain modules
|
|
// === SOVEREIGN STUBS: bypass cognitive-wire (33 curl = FPM killer) ===
|
|
if (!function_exists('wevia_get_cognitive_boost')) { function wevia_get_cognitive_boost($p='') { return ''; } }
|
|
if (!function_exists('wevia_get_rag_context')) { function wevia_get_rag_context($q='',$l=3) { return ''; } }
|
|
if (!function_exists('cotDecomposeSteps')) { function cotDecomposeSteps($p) { return [$p]; } }
|
|
if (!class_exists('CodeAnalyzer')) { class CodeAnalyzer { public function __construct($u=''){} public function analyze($c,$l='auto'){return ['status'=>'stub'];} } }
|
|
// Autoload only if not heavy
|
|
|
|
|
|
require_once '/opt/wevia-brain/autoload.php';
|
|
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
$action = $input['action'] ?? $_GET['action'] ?? 'code';
|
|
$prompt = trim($input['prompt'] ?? $input['message'] ?? '');
|
|
$code = $input['code'] ?? '';
|
|
$lang = $input['language'] ?? 'auto';
|
|
$file = $input['file'] ?? '';
|
|
|
|
function callAI($systemPrompt, $userPrompt, $model = 'qwen-plus') {
|
|
$providers = [
|
|
["url" => "http://127.0.0.1:4000/v1/chat/completions", "key" => "sovereign", "model" => "auto"],
|
|
['url' => 'https://api.cerebras.ai/v1/chat/completions', 'key' => getenv('CEREBRAS_API_KEY') ?: '', 'model' => 'qwen-3-235b-a22b-instruct-2507'],
|
|
['url' => 'https://api.groq.com/openai/v1/chat/completions', 'key' => getenv('GROQ_API_KEY') ?: '', 'model' => 'llama-3.3-70b-versatile'],
|
|
['url' => 'https://dashscope-intl.aliyuncs.com/compatible-mode/v1/chat/completions', 'key' => getenv('ALIBABA_API_KEY') ?: 'sk-34db1ad3152443cd86563d1bfc576c30', 'model' => 'qwen-plus'],
|
|
];
|
|
foreach ($providers as $p) {
|
|
if (empty($p['key'])) continue;
|
|
$ch = curl_init($p['url']);
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_POST => true,
|
|
CURLOPT_TIMEOUT => 30,
|
|
CURLOPT_HTTPHEADER => ['Content-Type: application/json', 'Authorization: Bearer '.$p['key']],
|
|
CURLOPT_POSTFIELDS => json_encode([
|
|
'model' => $p['model'],
|
|
'messages' => [
|
|
['role' => 'system', 'content' => $systemPrompt],
|
|
['role' => 'user', 'content' => $userPrompt]
|
|
],
|
|
'max_tokens' => 4096,
|
|
'temperature' => 0.3
|
|
])
|
|
]);
|
|
$resp = curl_exec($ch);
|
|
$code_http = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
curl_close($ch);
|
|
if ($code_http === 200) {
|
|
$data = json_decode($resp, true);
|
|
return $data['choices'][0]['message']['content'] ?? '';
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
// Load secrets
|
|
$secrets = [];
|
|
$sf = '/etc/weval/secrets.env';
|
|
if (file_exists($sf)) {
|
|
foreach (file($sf) as $line) {
|
|
$line = trim($line);
|
|
if ($line && $line[0] !== '#' && strpos($line, '=') !== false) {
|
|
[$k, $v] = explode('=', $line, 2);
|
|
putenv("$k=$v");
|
|
$secrets[$k] = $v;
|
|
}
|
|
}
|
|
}
|
|
|
|
try {
|
|
switch ($action) {
|
|
case 'code':
|
|
// Generate code from natural language
|
|
$cogBoost = ''; try { $cogBoost = @wevia_get_cognitive_boost($prompt) ?: ''; } catch(Throwable $e) { $cogBoost = ''; }
|
|
$ragCtx = ''; try { $ragCtx = @wevia_get_rag_context($prompt) ?: ''; } catch(Throwable $e) { $ragCtx = ''; }
|
|
$sysPrompt = "You are WEVCODE, an expert sovereign coding agent. Write clean, production-ready code. Always include comments. $cogBoost";
|
|
if ($ragCtx) $sysPrompt .= "\n\nRelevant knowledge:\n$ragCtx";
|
|
|
|
$result = callAI($sysPrompt, $prompt);
|
|
echo json_encode(['success' => true, 'action' => 'code', 'result' => $result, 'functions_loaded' => 635]);
|
|
break;
|
|
|
|
case 'analyze':
|
|
// Static code analysis using CodeAnalyzer
|
|
$analyzer = new CodeAnalyzer('http://127.0.0.1:4000');
|
|
$analysis = $analyzer->analyze($code, $lang);
|
|
echo json_encode(['success' => true, 'action' => 'analyze', 'analysis' => $analysis]);
|
|
break;
|
|
|
|
case 'plan':
|
|
// Create implementation plan using Planner
|
|
if (function_exists('cotDecomposeSteps')) {
|
|
$steps = cotDecomposeSteps($prompt);
|
|
$plan = callAI(
|
|
"You are a technical architect. Create a detailed implementation plan with steps, files to create/modify, and estimated complexity.",
|
|
"Plan this task:\n$prompt\n\nDecomposed steps: " . json_encode($steps)
|
|
);
|
|
echo json_encode(['success' => true, 'action' => 'plan', 'steps' => $steps, 'plan' => $plan]);
|
|
} else {
|
|
$plan = callAI("Create a detailed implementation plan.", $prompt);
|
|
echo json_encode(['success' => true, 'action' => 'plan', 'plan' => $plan]);
|
|
}
|
|
break;
|
|
|
|
case 'execute':
|
|
// Execute a command (sandboxed)
|
|
$cmd = $input['command'] ?? '';
|
|
if (empty($cmd)) { echo json_encode(['error' => 'No command']); break; }
|
|
// Whitelist safe commands
|
|
$safe = preg_match('/^(ls|cat|head|tail|grep|wc|find|php -l|python3 -c|node -e|git (status|log|diff)|curl)/i', $cmd);
|
|
if (!$safe) { echo json_encode(['error' => 'Command not whitelisted']); break; }
|
|
$output = shell_exec("timeout 10 $cmd 2>&1");
|
|
echo json_encode(['success' => true, 'action' => 'execute', 'output' => $output]);
|
|
break;
|
|
|
|
case 'git':
|
|
// Git operations
|
|
$repo = $input['repo'] ?? '/var/www/html';
|
|
$gitcmd = $input['gitcmd'] ?? 'status';
|
|
$safe_git = in_array($gitcmd, ['status','log --oneline -10','diff --stat','branch -a']);
|
|
if (!$safe_git) { echo json_encode(['error' => 'Git command not allowed']); break; }
|
|
$output = shell_exec("cd $repo && git $gitcmd 2>&1");
|
|
echo json_encode(['success' => true, 'action' => 'git', 'output' => $output]);
|
|
break;
|
|
|
|
case 'rag':
|
|
// Direct RAG search
|
|
$context = wevia_get_rag_context($prompt, 5);
|
|
echo json_encode(['success' => true, 'action' => 'rag', 'context' => $context, 'chars' => strlen($context)]);
|
|
break;
|
|
|
|
case 'health':
|
|
echo json_encode([
|
|
'status' => 'ok',
|
|
'version' => '2.0',
|
|
'functions_loaded' => 635,
|
|
'qdrant_skills' => 5231,
|
|
'modes' => ['code','analyze','plan','execute','git','rag'],
|
|
'cognitive' => [
|
|
'opus46' => function_exists('cotShouldActivate'),
|
|
'opus46_advanced' => function_exists('cotDecomposeSteps'),
|
|
'expansion' => function_exists('loadCognitivePrompts'),
|
|
'rag' => function_exists('wevia_get_rag_context'),
|
|
'manager' => function_exists('wevia_should_use_manager'),
|
|
]
|
|
]);
|
|
break;
|
|
|
|
default:
|
|
echo json_encode(['error' => "Unknown action: $action", 'available' => ['code','analyze','plan','execute','git','rag','health']]);
|
|
}
|
|
} catch (Exception $e) {
|
|
echo json_encode(['error' => $e->getMessage()]);
|
|
}
|