SIMON Master Console Rebuild
True command center for SIMON + WEB360
This rebuild gives you a cleaner production spine: interactive tree, live viewer, guarded editor, dynamic graphics, architecture map, and note storage. It is designed to sit beside your existing
console.php
and become the stronger replacement path.
Dashboard
Tree
Viewer
System Map
File Registry
Notes
Files
15,026
Folders
377
Scanned Size
3.79 GB
PHP Files
791
Editable Text Files
12,310
File viewer
guarded to /htdocs
/api/chat.php
<?php declare(strict_types=1); header('Content-Type: application/json; charset=utf-8'); header('Cache-Control: no-store'); function json_out(array $data, int $code=200): void { http_response_code($code); echo json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); exit; } function read_json(): array { $raw = file_get_contents('php://input') ?: ''; $j = json_decode($raw, true); return is_array($j) ? $j : []; } function get_client_key(): string { $h = getallheaders(); if (!empty($h['X-OpenAI-Key'])) return trim((string)$h['X-OpenAI-Key']); if (!empty($h['x-openai-key'])) return trim((string)$h['x-openai-key']); return ''; } function openai_key(): string { $k = get_client_key(); if ($k !== '') return $k; $env = getenv('OPENAI_API_KEY') ?: ''; return trim((string)$env); } function curl_json(string $url, array $body, string $apiKey): array { $ch = curl_init($url); $payload = json_encode($body, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); curl_setopt_array($ch, [ CURLOPT_POST => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => [ 'Authorization: Bearer ' . $apiKey, 'Content-Type: application/json', ], CURLOPT_POSTFIELDS => $payload, CURLOPT_TIMEOUT => 60, ]); $resp = curl_exec($ch); $err = curl_error($ch); $code = (int)curl_getinfo($ch, CURLINFO_RESPONSE_CODE); curl_close($ch); if ($resp === false) return [ 'ok'=>false, 'code'=>0, 'error'=>'cURL error: '.$err ]; $j = json_decode($resp, true); if (!is_array($j)) return [ 'ok'=>false, 'code'=>$code, 'error'=>'Bad JSON from OpenAI', 'raw'=>mb_substr($resp,0,500) ]; if ($code < 200 || $code >= 300) { $msg = $j['error']['message'] ?? ('OpenAI HTTP ' . $code); return [ 'ok'=>false, 'code'=>$code, 'error'=>(string)$msg ]; } return [ 'ok'=>true, 'code'=>$code, 'data'=>$j ]; } $apiKey = openai_key(); if ($apiKey === '') { json_out([ 'ok' => false, 'error' => 'Server missing OPENAI_API_KEY (or client key disabled / not provided).' ], 500); } $in = read_json(); $q = trim((string)($in['q'] ?? '')); if ($q === '') json_out(['ok'=>false,'error'=>'Missing q'], 400); $model = trim((string)($in['model'] ?? 'gpt-4o-mini')); $personality = (string)($in['personality'] ?? 'Concise'); $focus = (string)($in['focus'] ?? 'General'); $memory = $in['memory'] ?? []; $system = "You are SIMON — a highly conversational assistant. " . "Be warm, witty when appropriate, and helpful. " . "If the user selects a Focus Area, prioritize that domain. " . "If the user selects a Personality, match it. " . "Keep answers practical. Ask at most 1 follow-up question only if truly required.\n\n" . "Personality: {$personality}\n" . "Focus: {$focus}\n"; $contextLines = []; if (is_array($memory)) { $tail = array_slice($memory, -12); foreach ($tail as $t) { if (!is_array($t)) continue; $mq = trim((string)($t['q'] ?? '')); $ma = trim((string)($t['a'] ?? '')); if ($mq !== '' && $ma !== '') { $contextLines[] = "User: {$mq}\nSIMON: {$ma}"; } } } $context = $contextLines ? ("\nRecent context:\n" . implode("\n\n", $contextLines) . "\n") : ""; $body = [ 'model' => $model, 'input' => [ ['role'=>'system','content'=> $system . $context], ['role'=>'user','content'=> $q], ], ]; $r = curl_json('https://api.openai.com/v1/responses', $body, $apiKey); if (!$r['ok']) { json_out(['ok'=>false,'error'=>$r['error']], 500); } $data = $r['data']; // best-effort extraction for Responses API "output_text" $answer = (string)($data['output_text'] ?? ''); if ($answer === '') { // fallback: walk output blocks $out = $data['output'] ?? []; if (is_array($out)) { foreach ($out as $o) { $content = $o['content'] ?? []; if (is_array($content)) { foreach ($content as $c) { if (($c['type'] ?? '') === 'output_text') { $answer .= (string)($c['text'] ?? ''); } } } } } } json_out([ 'ok' => true, 'answer' => trim($answer), ]);
Save file
Quick jump
open a path
Open