runTextToTextTask($taskInput, $userId); $converter = new GithubFlavoredMarkdownConverter(); $htmlContent = $converter->convert($markdownContent)->getContent(); $htmlStream = $this->stringToStream($htmlContent); $docxContent = $this->remoteService->convertTo('document.html', $htmlStream, $targetFormat, true); return $docxContent; } public function generateSpreadSheetDocument(?string $userId, string $description, string $targetFormat = TextToSpreadsheetProvider::DEFAULT_TARGET_FORMAT) { $prompt = self::SPREADSHEET_PROMPT; $taskInput = $prompt . "\n\n" . $description; $csvContent = $this->runTextToTextTask($taskInput, $userId); $csvStream = $this->stringToStream($csvContent); $xlsxContent = $this->remoteService->convertTo('document.csv', $csvStream, $targetFormat, true); return $xlsxContent; } private function runTextToTextTask(string $input, ?string $userId): string { $task = new Task( TextToText::ID, ['input' => $input], Application::APPNAME, $userId, ); try { $this->taskProcessingManager->scheduleTask($task); } catch (PreConditionNotMetException|UnauthorizedException|ValidationException|Exception $e) { throw new RuntimeException($e->getMessage(), $e->getCode(), $e); } while (true) { try { $task = $this->taskProcessingManager->getTask($task->getId()); sleep(2); } catch (NotFoundException|Exception $e) { throw new RuntimeException($e->getMessage(), $e->getCode(), $e); } if (in_array($task->getStatus(), [Task::STATUS_SUCCESSFUL, Task::STATUS_FAILED, Task::STATUS_CANCELLED])) { break; } } if ($task->getStatus() !== Task::STATUS_SUCCESSFUL) { throw new RuntimeException('LLM backend Task with id ' . $task->getId() . ' failed or was cancelled'); } $output = $task->getOutput(); if (!isset($output['output'])) { throw new RuntimeException('LLM backend Task with id ' . $task->getId() . ' does not have output key'); } /** @var string $taskOutputString */ $taskOutputString = $output['output']; return $taskOutputString; } private function stringToStream(string $text) { $stream = fopen('php://memory', 'r+'); fwrite($stream, $text); rewind($stream); return $stream; } }