cai-exos-systems/daveadmin-exos-demo:api/super.php
api/super.php
```text
<?php
declare(strict_types=1);
/**
* Exos Demo - TM Forum Super Mode proxy
* Receives: {message, account, prefer_boost?}
* Streams: SSE events shaped like the Dify proxy so the current UI can reuse
* its existing tool trace and reasoning panels.
*/
require_once dirname(__DIR__) . '/auth.php';
require_once dirname(__DIR__) . '/demo-config.php';
function superJsonResponse(int $status, array $payload): void
{
http_response_code($status);
header('Content-Type: application/json; charset=UTF-8');
echo json_encode($payload);
exit;
}
function superStreamEvent(string $event, array $data = []): void
{
echo 'data: ' . json_encode(array_merge(['event' => $event], $data), JSON_UNESCAPED_SLASHES) . "\n\n";
if (ob_get_level()) {
ob_flush();
}
flush();
}
function superStreamError(string $message): void
{
superStreamEvent('error', ['message' => $message]);
}
function superTrim(string $value, int $limit = 240): string
{
$value = trim((string) preg_replace('/\s+/', ' ', $value));
if ($value === '') {
return '';
}
return mb_strlen($value) > $limit
? mb_substr($value, 0, $limit - 3) . '...'
: $value;
}
function superFormatList(array $items, callable $formatter): string
{
if ($items === []) {
return '';
}
$lines = [];
foreach ($items as $item) {
$line = trim((string) $formatter($item));
if ($line !== '') {
$lines[] = '- ' . $line;
}
}
return $lines === [] ? '' : implode("\n", $lines);
}
function superBuildMessage(array $result): string
{
$structured = is_array($result['structured'] ?? null) ? $result['structured'] : [];
$sections = [];
$opening = trim((string) ($structured['opening_pitch'] ?? $result['response'] ?? ''));
if ($opening !== '') {
$sections[] = $opening;
}
$apiMap = is_array($structured['tmf_api_map'] ?? null) ? $structured['tmf_api_map'] : [];
$apiText = superFormatList($apiMap, static function (array $item): string {
$apiId = trim((string) ($item['api_id'] ?? 'TMF'));
$domain = trim((string) ($item['domain'] ?? 'BSS domain'));
$role = superTrim((string) ($item['role'] ?? ''), 180);
return trim($apiId . ' - ' . $domain . ($role !== '' ? ': ' . $role : ''));
});
if ($apiText !== '') {
$sections[] = "TM Forum API map:\n" . $apiText;
}
$flows = is_array($structured['native_agent_flows'] ?? null) ? $structured['native_agent_flows'] : [];
$flowText = superFormatList($flows, static function (array $item): string {
$flow = trim((string) ($item['flow'] ?? 'Agent flow'));
$goal = superTrim((string) ($item['goal'] ?? ''), 180);
$apis = array_values(array_filter(array_map('strval', (array) ($item['tmf_apis'] ?? []))));
$tools = array_values(array_filter(array_map('strval', (array) ($item['tools'] ?? []))));
$parts = [$flow];
if ($goal !== '') {
$parts[] = $goal;
}
if ($apis !== []) {
$parts[] = 'TMF APIs: ' . implode(', ', $apis);
}
if ($tools !== []) {
$parts[] = 'Tools: ' . implode(', ', $tools);
}
return implode(' | ', $parts);
});
if ($flowText !== '') {
$sections[] = "Native agent flows:\n" . $flowText;
}
$platformMoves = array_values(array_filter(array_map('strval', (array) ($structured['caveau_platform_leverage'] ?? []))));
$platformText = superFormatList($platformMoves, static fn(string $item): string => superTrim($item, 180));
if ($platformText !== '') {
$sections[] = "How CaveauAI sells this today:\n" . $platformText;
}
$improvements = array_values(array_filter(array_map('strval', (array) ($structured['improvements_before_finetune'] ?? []))));
$improvementText = superFormatList($improvements, static fn(string $item): string => superTrim($item, 180));
if ($improvementText !== '') {
$sections[] = "Best next improvements:\n" . $improvementText;
}
$route = is_array($result['route'] ?? null) ? $result['route'] : [];
$sections[] = sprintf(
'Runtime: %s | Model: %s | Boost requested: %s | Viper available: %s',
strtoupper((string) ($route['runtime'] ?? 'cuttlefish')),
(string) ($route['model'] ?? 'qwen3:8b'),
!empty($route['boost_requested']) ? 'yes' : 'no',
!empty($route['boost_available']) ? 'yes' : 'no'
);
$fineTune = trim((string) ($structured['fine_tune_status'] ?? ''));
if ($fineTune !== '') {
$sections[] = 'Fine-tune note: ' . $fineTune;
}
$citations = is_array($result['citations'] ?? null) ? $result['citations'] : [];
$citationText = superFormatList($citations, static function (array $item): string {
$tag = trim((string) ($item['tag'] ?? 'Doc'));
$title = trim((string) ($item['title'] ?? 'Retrieved source'));
return $tag . ' - ' . $title;
});
if ($citationText !== '') {
$sections[] = "Retrieved evidence:\n" . $citationText;
}
return implode("\n\n", array_filter($sections));
}
demoRequireApiLogin();
$demoUser = demoCurrentUser();
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
superJsonResponse(405, ['error' => 'Method not allowed']);
}
if (!demoOriginAllowed()) {
demoAuditLog('super_origin_blocked');
superJsonResponse(403, ['error' => 'Request origin not allowed']);
}
api/super.php the answer.', 'tool_input' => json_encode([ 'collections' => (array) ($result['collections_searched'] ?? []), ], JSON_UNESCAPED_SLASHES), 'observation' => sprintf( 'Collections: %s | Chunks used: %s', implode(', ', (array) ($result['collections_searched'] ?? [])), (string) ($result['chunks_used'] ?? '0') ), ]); foreach ((array) ($result['tool_trace'] ?? []) as $trace) { if (!is_array($trace)) { continue; } $tool = trim((string) ($trace['tool'] ?? 'tool')); $observation = []; foreach (['runtime', 'model', 'mode', 'chunks_used', 'boost_requested', 'boost_available'] as $key) { if (array_key_exists($key, $trace)) { $observation[$key] = $trace[$key]; } } superStreamEvent('agent_thought', [ 'tool' => $tool, 'thought' => 'Recording super-mode tool telemetry.', 'tool_input' => json_encode($trace, JSON_UNESCAPED_SLASHES), 'observation' => json_encode($observation, JSON_UNESCAPED_SLASHES), ]); } superStreamEvent('message', [ 'answer' => superBuildMessage($result), ]); superStreamEvent('message_end', [ 'conversation_id' => 'super-' . $account . '-' . time(), ]); demoAuditLog('super_completed', [ 'account' => $account, 'runtime' => $route['runtime'] ?? null, 'model' => $route['model'] ?? null, 'user' => $demoUser['username'] ?? null, ]); ```
api/super.php the answer.', 'tool_input' => json_encode([ 'collections' => (array) ($result['collections_searched'] ?? []), ], JSON_UNESCAPED_SLASHES), 'observation' => sprintf( 'Collections: %s | Chunks used: %s', implode(', ', (array) ($result['collections_searched'] ?? [])), (string) ($result['chunks_used'] ?? '0') ), ]); foreach ((array) ($result['tool_trace'] ?? []) as $trace) { if (!is_array($trace)) { continue; } $tool = trim((string) ($trace['tool'] ?? 'tool')); $observation = []; foreach (['runtime', 'model', 'mode', 'chunks_used', 'boost_requested', 'boost_available'] as $key) { if (array_key_exists($key, $trace)) { $observation[$key] = $trace[$key]; } } superStreamEvent('agent_thought', [ 'tool' => $tool, 'thought' => 'Recording super-mode tool telemetry.', 'tool_input' => json_encode($trace, JSON_UNESCAPED_SLASHES), 'observation' => json_encode($observation, JSON_UNESCAPED_SLASHES), ]); } superStreamEvent('message', [ 'answer' => superBuildMessage($result), ]); superStreamEvent('message_end', [ 'conversation_id' => 'super-' . $account . '-' . time(), ]); demoAuditLog('super_completed', [ 'account' => $account, 'runtime' => $route['runtime'] ?? null, 'model' => $route['model'] ?? null, 'user' => $demoUser['username'] ?? null, ]); ```