← Back to all documents

cai-exos-systems/daveadmin-exos-demo:api/chat.php.bak

gitea 448 words Source ↗
api/chat.php.bak ```text <?php /** * Exos Demo — Dify Chat Proxy * Receives: {agent, message, conversation_id, account} * Streams: SSE from Dify /v1/chat-messages */ require_once dirname(__DIR__) . '/auth.php'; demoRequireApiLogin(); $demoUser = demoCurrentUser(); // Only POST if ($_SERVER['REQUEST_METHOD'] !== 'POST') { http_response_code(405); header('Content-Type: application/json; charset=UTF-8'); echo json_encode(['error' => 'Method not allowed']); exit; } $body = json_decode(file_get_contents('php://input'), true); if (!$body) { http_response_code(400); header('Content-Type: application/json; charset=UTF-8'); echo json_encode(['error' => 'Invalid JSON']); exit; } $agent = $body['agent'] ?? 'billing'; $message = trim($body['message'] ?? ''); $convId = $body['conversation_id'] ?? ''; $account = $body['account'] ?? '1'; $temperature = isset($body['temperature']) ? (float)$body['temperature'] : null; $maxTokens = isset($body['max_tokens']) ? (int)$body['max_tokens'] : null; $topP = isset($body['top_p']) ? (float)$body['top_p'] : null; if (!$message) { http_response_code(400); header('Content-Type: application/json; charset=UTF-8'); echo json_encode(['error' => 'Message required']); exit; } // Map agent → API key $keyMap = [ 'billing' => demoEnv('DIFY_API_KEY_BILLING', ''), 'product' => demoEnv('DIFY_API_KEY_PRODUCT', ''), 'order' => demoEnv('DIFY_API_KEY_ORDER', ''), 'care' => demoEnv('DIFY_API_KEY_CARE', ''), ]; $apiKey = $keyMap[$agent] ?? $keyMap['billing']; if (!$apiKey) { http_response_code(500); header('Content-Type: application/json; charset=UTF-8'); echo json_encode(['error' => 'Agent API key not configured']); exit; } $difyBase = rtrim((string) demoEnv('DIFY_BASE_URL', 'https://dify.bluenotelogic.com'), '/'); // Account labels for context injection $accountNames = [ '1' => 'Sure Telecom', '2' => 'Telesur Suriname', '3' => 'CW Seychelles (Cable & Wireless)', '4' => 'Digicel Pacific', '5' => 'Vodacom Mozambique', ]; $accountName = $accountNames[$account] ?? 'Sure Telecom'; // Inject account context on first message (no conversation_id yet) $userMessage = $message; if (!$convId) { $userMessage = "[Demo account: {$accountName} (Account ID: {$account})]\n{$message}"; } // Merge model params into inputs (forwarded as Dify input variables) $modelParams = array_filter([ 'temperature' => $temperature, 'max_tokens' => $maxTokens, 'top_p' => $topP, ], fn($v) => $v !== null); // Build Dify request $payload = [ 'inputs' => array_merge(['account' => $account, 'account_name' => $accountName], $modelParams), 'query' => $userMessage, 'response_mode' => 'streaming', 'user' => (string) ($demoUser['username'] ?? 'exos-demo'), ]; if ($convId) { $payload['conversation_id'] = $convId; } // Stream SSE headers header('Content-Type: text/event-stream'); header('Cache-Control: no-cache'); header('X-Accel-Buffering: no'); // Forward streaming response via cURL $ch = curl_init("{$difyBase}/v1/chat-messages"); curl_setopt_array($ch, [ CURLOPT_POST => true, CURLOPT_POSTFIELDS => json_encode($payload), CURLOPT_HTTPHEADER => [ 'Content-Type: application/json', 'Authorization: Bearer ' . $apiKey, 'Accept: text/event-stream', ], CURLOPT_WRITEFUNCTION => function ($curl, $data) { echo $data; if (ob_get_level()) ob_flush(); flush(); return strlen($data); }, CURLOPT_FOLLOWLOCATION => true, CURLOPT_TIMEOUT => 120, CURLOPT_SSL_VERIFYPEER => true, ]); $result = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($result === false || $httpCode >= 400) { echo "data: " . json_encode(['event' => 'error', 'data' => ['message' => "Upstream error {$httpCode}"]]) . "\n\n"; if (ob_get_level()) ob_flush(); flush(); } ```