|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace App\Stream; |
| 13 | + |
| 14 | +use Symfony\AI\Platform\Message\MessageInterface; |
| 15 | +use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
| 16 | +use Symfony\Component\HttpFoundation\EventStreamResponse; |
| 17 | +use Symfony\Component\HttpFoundation\Request; |
| 18 | +use Symfony\Component\HttpFoundation\ServerEvent; |
| 19 | +use Symfony\Component\HttpFoundation\Session\Session; |
| 20 | +use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; |
| 21 | +use Symfony\UX\LiveComponent\Attribute\AsLiveComponent; |
| 22 | +use Symfony\UX\LiveComponent\Attribute\LiveAction; |
| 23 | +use Symfony\UX\LiveComponent\Attribute\LiveProp; |
| 24 | +use Symfony\UX\LiveComponent\DefaultActionTrait; |
| 25 | + |
| 26 | +#[AsLiveComponent('stream')] |
| 27 | +final class TwigComponent extends AbstractController |
| 28 | +{ |
| 29 | + use DefaultActionTrait; |
| 30 | + |
| 31 | + #[LiveProp(writable: true)] |
| 32 | + public ?string $message = null; |
| 33 | + public bool $stream = false; |
| 34 | + |
| 35 | + public function __construct( |
| 36 | + private readonly Chat $chat, |
| 37 | + ) { |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * @return MessageInterface[] |
| 42 | + */ |
| 43 | + public function getMessages(): array |
| 44 | + { |
| 45 | + return $this->chat->loadMessages()->withoutSystemMessage()->getMessages(); |
| 46 | + } |
| 47 | + |
| 48 | + #[LiveAction] |
| 49 | + public function submit(): void |
| 50 | + { |
| 51 | + if (!$this->message) { |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + $this->chat->submitMessage($this->message); |
| 56 | + $this->message = null; |
| 57 | + $this->stream = true; |
| 58 | + } |
| 59 | + |
| 60 | + #[LiveAction] |
| 61 | + public function reset(): void |
| 62 | + { |
| 63 | + $this->chat->reset(); |
| 64 | + } |
| 65 | + |
| 66 | + public function streamContent(Request $request): EventStreamResponse |
| 67 | + { |
| 68 | + $messages = $this->chat->loadMessages(); |
| 69 | + |
| 70 | + $actualSession = $request->getSession(); |
| 71 | + |
| 72 | + // Overriding session will prevent the framework calling save() on the actual session. |
| 73 | + // This fixes "Failed to start the session because headers have already been sent" error. |
| 74 | + $request->setSession(new Session(new MockArraySessionStorage())); |
| 75 | + |
| 76 | + return new EventStreamResponse(function () use ($request, $actualSession, $messages) { |
| 77 | + $request->setSession($actualSession); |
| 78 | + $response = $this->chat->getAssistantResponse($messages); |
| 79 | + |
| 80 | + $thinking = true; |
| 81 | + foreach ($response as $chunk) { |
| 82 | + // Remove "Thinking..." when we receive something |
| 83 | + if ($thinking && trim($chunk)) { |
| 84 | + $thinking = false; |
| 85 | + yield new ServerEvent(explode("\n", $this->renderBlockView('_stream.html.twig', 'start'))); |
| 86 | + } |
| 87 | + |
| 88 | + yield new ServerEvent(explode("\n", $this->renderBlockView('_stream.html.twig', 'partial', ['part' => $chunk]))); |
| 89 | + } |
| 90 | + |
| 91 | + yield new ServerEvent(explode("\n", $this->renderBlockView('_stream.html.twig', 'end', ['message' => $response->getReturn()]))); |
| 92 | + }); |
| 93 | + } |
| 94 | +} |
0 commit comments