Why my code not format chunked ### to bold <?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Services\ChatService;
use Parsedown;
use Illuminate\Support\Str;

class FormatController extends Controller
{
    protected $chatService;
    protected $parsedown;

    public function __construct(ChatService $chatService)
    {
        $this->chatService = $chatService;
        $this->parsedown = new Parsedown();
        $this->parsedown->setBreaksEnabled(true); // Enable line breaks
    }

    public function sendMessage(Request $request)
    {
        $conversationId = $request->input('conversationId');
        $messageText = $request->input('message');
        $systemInstruction = $request->input('sysInstruction', '');
        $temperature = $request->input('temperature', 0.7);

        // Directly trigger web search if user input suggests it
        if ($this->shouldTriggerWebSearch($messageText)) {
            return $this->performWebSearch($messageText);
        }

        $response = $this->chatService->sendMessage($conversationId, $messageText, $systemInstruction, $temperature);

        return response()->stream(function () use ($response) {
            $inCodeBlock = false;
            $codeLanguage = '';
            $buffer = '';

            $body = $response->wait()->getBody();

            while (!$body->eof()) {
                $data = $body->read(1024);
                $buffer .= $data;

                while (($newlinePos = strpos($buffer, "\n")) !== false) {
                    $line = substr($buffer, 0, $newlinePos);
                    $buffer = substr($buffer, $newlinePos + 1);

                    if (strpos($line, 'data: ') === 0) {
                        $jsonData = substr($line, 6);

                        if ($jsonData === '[DONE]') {
                            break;
                        }

                        $responseChunk = json_decode($jsonData, true);
                        if (isset($responseChunk['choices'][0]['delta']['content'])) {
                            $content = $responseChunk['choices'][0]['delta']['content'];

                            if (trim($content) === '```') {
                                // Toggle code block state
                                if ($inCodeBlock) {
                                    echo view('components.codeBlockEnd')->render();
                                    $inCodeBlock = false;
                                } else {
                                    $inCodeBlock = true;
                                    $codeLanguage = '';
                                }
                            } elseif ($inCodeBlock && empty($codeLanguage)) {
                                $codeLanguage = trim($content);
                                echo view('components.codeBlockStart', ['language' => $codeLanguage])->render();
                            } elseif ($inCodeBlock) {
                                echo e($content);
                            } else {
                                echo nl2br(e($content)); // Convert newlines to <br> for HTML output
                            }

                            ob_flush();
                            flush();
                        }
                    }
                }
            }

            if (!$inCodeBlock && !empty($buffer)) {
                echo $this->parsedown->text($buffer);
                ob_flush();
                flush();
            }
        }, 200, [
            'Content-Type' => 'text/html; charset=utf-8',
            'X-Accel-Buffering' => 'no',
            'Cache-Control' => 'no-cache',
        ]);
    }

    protected function shouldTriggerWebSearch($messageText)
    {
        $triggerPhrases = [
            "can you check",
            "can you look up",
            "can you find",
            "can you search",
            "can u check",
            "check for me",
            "look it up",
            "find it for me"
        ];

        foreach ($triggerPhrases as $phrase) {
            if (Str::contains(strtolower($messageText), $phrase)) {
                return true;
            }
        }

        return false;
    }

    protected function performWebSearch($query)
    {
        $response = app('App\Http\Controllers\Modules\WebSearchController')->searchWeb(new Request(['query' => $query]));
        return $response;
    }
}