'No notes provided.']); exit; } $notes = $input['notes']; $prompt = << [ ['role' => 'system', 'content' => $prompt], ], ]); if (!empty($resp['success'])) { $text = LocalAIApi::extractText($resp); $json_output = LocalAIApi::decodeJsonFromResponse($resp); if ($json_output) { echo json_encode($json_output); } else { // If the model didn't return valid JSON, try to wrap its text output in a JSON structure. echo json_encode(['support_needs_summary' => $text]); } } else { throw new Exception($resp['error'] ?? 'Unknown AI error'); } } catch (Exception $e) { http_response_code(500); echo json_encode(['error' => 'AI Service Error: ' . $e->getMessage()]); }