<?php
namespace App\AI\SymfonyAiExtend\Processor; use App\AI\SymfonyAiExtend\Toolbox\The; use Symfony\AI\Agent\Output; use Symfony\AI\Agent\OutputProcessorInterface; class MainOutputProcessor implements OutputProcessorInterface { private $userResponses = []; private $toolCalls = []; private $content = []; private $shouldStopLoop = false; public function __construct( ) { } public function getUserResponses(): array { return $this->userResponses; } public function getToolCalls(): array { return $this->toolCalls; } public function getContent(): array { return $this->content; } public function shouldStopLoop(): bool { return $this->shouldStopLoop; } public function processOutput(Output $output): void { if (str_starts_with($output->getModel(), 'gpt-')) { $result = $output->getResult()->getRawResult()->getData(); if ($result && $result['output']) { foreach ($result['output'] as $message) { if ($message['type'] == 'message') { foreach ($message['content'] as $content) { if ($content['text']) { $this->userResponses[] = $content['text']; } } } } } $this->shouldStopLoop = !!$this->userResponses; } if (str_starts_with($output->getModel(), 'claude-')) { $result = $output->getResult()->getRawResult()->getData(); $this->content = $result['content']; $texts = array_filter($this->content, function ($content) { return $content['type'] == 'text'; }); $toolCalls = array_filter($this->content, function ($content) { return $content['type'] == 'tool_use'; }); if ($texts && !$toolCalls) { $this->userResponses = array_filter(array_map(function ($text) { return $text['text']; }, $texts)); $this->shouldStopLoop = true; } } if (str_starts_with($output->getModel(), 'gemini-')) { $result = $output->getResult()->getRawResult()->getData(); $this->content = $result['steps']; $texts = array_filter($this->content, function ($step) { return $step['type'] == 'model_output'; }); $toolCalls = array_filter($this->content, function ($step) { return $step['type'] == 'function_call'; }); if ($texts) { foreach ($texts as $text) { if ($text['content']) { foreach ($text['content'] as $content) { if ($content['text']) { $this->userResponses[] = $content['text']; } } } } } if ($result['status']=='completed') { $this->shouldStopLoop = true; } } } }