From 9757a6573dbd6e404cff998ffa1ffc1b7ebef1c2 Mon Sep 17 00:00:00 2001 From: Christopher Hertel Date: Thu, 9 Oct 2025 01:17:06 +0200 Subject: [PATCH] Fix demo by PHPStan findings + pipeline --- .github/workflows/code-quality.yaml | 4 ++++ demo/src/Audio/Chat.php | 2 +- demo/src/Blog/Command/QueryCommand.php | 2 +- demo/src/Blog/Command/StreamCommand.php | 3 +++ demo/src/Mcp/Prompts/CurrentTimePrompt.php | 3 +++ .../ResourceTemplates/CurrentTimeResourceTemplate.php | 3 +++ demo/src/Mcp/Resources/CurrentTimeResource.php | 3 +++ demo/src/Video/TwigComponent.php | 2 +- demo/tests/Blog/ChatTest.php | 11 ++++++++++- demo/tests/Blog/Command/StreamCommandTest.php | 2 ++ 10 files changed, 31 insertions(+), 4 deletions(-) diff --git a/.github/workflows/code-quality.yaml b/.github/workflows/code-quality.yaml index 42ff9a0c4..058574ba0 100644 --- a/.github/workflows/code-quality.yaml +++ b/.github/workflows/code-quality.yaml @@ -64,6 +64,10 @@ jobs: run: | cd examples/ && $COMPOSER_UP && ../link && $PHPSTAN + - name: Run PHPStan on demo + run: | + cd demo/ && $COMPOSER_UP && ../link && $PHPSTAN + - name: Run PHPStan on packages run: | source .github/workflows/.utils.sh diff --git a/demo/src/Audio/Chat.php b/demo/src/Audio/Chat.php index 178dd32fa..b198d004c 100644 --- a/demo/src/Audio/Chat.php +++ b/demo/src/Audio/Chat.php @@ -39,7 +39,7 @@ public function say(string $base64audio): void $path = tempnam(sys_get_temp_dir(), 'audio-').'.wav'; file_put_contents($path, base64_decode($base64audio)); - $result = $this->platform->invoke(new Whisper(), Audio::fromFile($path)); + $result = $this->platform->invoke('whisper-1', Audio::fromFile($path)); $this->submitMessage($result->asText()); } diff --git a/demo/src/Blog/Command/QueryCommand.php b/demo/src/Blog/Command/QueryCommand.php index e6472c107..eecaae0b7 100644 --- a/demo/src/Blog/Command/QueryCommand.php +++ b/demo/src/Blog/Command/QueryCommand.php @@ -47,7 +47,7 @@ public function __invoke(SymfonyStyle $io): int $vector = $this->vectorizer->vectorize($search); $queryResponse = $collection->query( - queryEmbeddings: [$vector->getData()], + queryEmbeddings: [$vector->getData()], /** @phpstan-ignore-line until https://github.com/symfony/ai/issues/768 */ nResults: 4, ); diff --git a/demo/src/Blog/Command/StreamCommand.php b/demo/src/Blog/Command/StreamCommand.php index e6bac3fa6..ce92e0d81 100644 --- a/demo/src/Blog/Command/StreamCommand.php +++ b/demo/src/Blog/Command/StreamCommand.php @@ -14,6 +14,7 @@ use Symfony\AI\Agent\AgentInterface; use Symfony\AI\Platform\Message\Message; use Symfony\AI\Platform\Message\MessageBag; +use Symfony\AI\Platform\Result\StreamResult; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Style\SymfonyStyle; @@ -42,6 +43,8 @@ public function __invoke(SymfonyStyle $io): int $io->section('Agent Response:'); $result = $this->blogAgent->call($messages, ['stream' => true]); + \assert($result instanceof StreamResult); + foreach ($result->getContent() as $word) { $io->write($word); } diff --git a/demo/src/Mcp/Prompts/CurrentTimePrompt.php b/demo/src/Mcp/Prompts/CurrentTimePrompt.php index 549a28ce0..a8a6acb67 100644 --- a/demo/src/Mcp/Prompts/CurrentTimePrompt.php +++ b/demo/src/Mcp/Prompts/CurrentTimePrompt.php @@ -15,6 +15,9 @@ class CurrentTimePrompt { + /** + * @return array{role: 'user', content: string}[] + */ #[McpPrompt(name: 'time-analysis')] public function getTimeAnalysisPrompt(): array { diff --git a/demo/src/Mcp/ResourceTemplates/CurrentTimeResourceTemplate.php b/demo/src/Mcp/ResourceTemplates/CurrentTimeResourceTemplate.php index 177a7d04c..5ecb75574 100644 --- a/demo/src/Mcp/ResourceTemplates/CurrentTimeResourceTemplate.php +++ b/demo/src/Mcp/ResourceTemplates/CurrentTimeResourceTemplate.php @@ -15,6 +15,9 @@ class CurrentTimeResourceTemplate { + /** + * @return array{uri: string, mimeType: string, text: string} + */ #[McpResourceTemplate(uriTemplate: 'time://{timezone}', name: 'time-by-timezone')] public function getTimeByTimezone(string $timezone): array { diff --git a/demo/src/Mcp/Resources/CurrentTimeResource.php b/demo/src/Mcp/Resources/CurrentTimeResource.php index 27f1d8e71..e3a2dec0d 100644 --- a/demo/src/Mcp/Resources/CurrentTimeResource.php +++ b/demo/src/Mcp/Resources/CurrentTimeResource.php @@ -15,6 +15,9 @@ class CurrentTimeResource { + /** + * @return array{uri: string, mimeType: string, text: string} + */ #[McpResource(uri: 'time://current', name: 'current-time-resource')] public function getCurrentTimeResource(): array { diff --git a/demo/src/Video/TwigComponent.php b/demo/src/Video/TwigComponent.php index 448e70005..d8cc18820 100644 --- a/demo/src/Video/TwigComponent.php +++ b/demo/src/Video/TwigComponent.php @@ -47,7 +47,7 @@ public function submit(#[LiveArg] string $instruction, #[LiveArg] string $image) Message::ofUser($instruction, Image::fromDataUrl($image)) ); - $result = $this->platform->invoke(new Gpt(Gpt::GPT_4O_MINI), $messageBag, [ + $result = $this->platform->invoke('gpt-4o-mini', $messageBag, [ 'max_tokens' => 100, ]); diff --git a/demo/tests/Blog/ChatTest.php b/demo/tests/Blog/ChatTest.php index 811be0c69..97280d7ae 100644 --- a/demo/tests/Blog/ChatTest.php +++ b/demo/tests/Blog/ChatTest.php @@ -15,6 +15,7 @@ use PHPUnit\Framework\TestCase; use Symfony\AI\Agent\MockAgent; use Symfony\AI\Platform\Message\AssistantMessage; +use Symfony\AI\Platform\Message\Content\Text; use Symfony\AI\Platform\Message\MessageBag; use Symfony\AI\Platform\Message\SystemMessage; use Symfony\AI\Platform\Message\UserMessage; @@ -65,6 +66,7 @@ public function testSubmitMessageAddsUserMessageAndAgentResponse() // Check user message $userMessage = $messageList[1]; $this->assertInstanceOf(UserMessage::class, $userMessage); + $this->assertInstanceOf(Text::class, $userMessage->getContent()[0]); $this->assertSame('What is Symfony?', $userMessage->getContent()[0]->getText()); // Check assistant message @@ -169,9 +171,16 @@ public function testAgentReceivesFullConversationHistory() $this->assertCount(5, $messages); // Verify the conversation flow (with 5 messages) - $this->assertStringContainsString('helpful assistant', $messages[0]->getContent()); // system + $this->assertInstanceOf(SystemMessage::class, $messages[0]); + $this->assertStringContainsString('helpful assistant', $messages[0]->getContent()); + $this->assertInstanceOf(UserMessage::class, $messages[1]); + $this->assertCount(1, $messages[1]->getContent()); + $this->assertInstanceOf(Text::class, $messages[1]->getContent()[0]); $this->assertSame('What is Symfony?', $messages[1]->getContent()[0]->getText()); // user1 $this->assertSame('Symfony is a PHP web framework for building web applications and APIs.', $messages[2]->getContent()); // assistant1 + $this->assertInstanceOf(UserMessage::class, $messages[3]); + $this->assertCount(1, $messages[3]->getContent()); + $this->assertInstanceOf(Text::class, $messages[3]->getContent()[0]); $this->assertSame('Tell me more', $messages[3]->getContent()[0]->getText()); // user2 // The 5th message appears to be the previous assistant response or another system message } diff --git a/demo/tests/Blog/Command/StreamCommandTest.php b/demo/tests/Blog/Command/StreamCommandTest.php index 5b186a849..b93daf92f 100644 --- a/demo/tests/Blog/Command/StreamCommandTest.php +++ b/demo/tests/Blog/Command/StreamCommandTest.php @@ -41,10 +41,12 @@ public function getContent(): iterable public function getMetadata(): Metadata { + return new Metadata(); } public function getRawResult(): ?RawResultInterface { + return null; } public function setRawResult(RawResultInterface $rawResult): void