|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 5 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 6 | + */ |
| 7 | + |
| 8 | +declare(strict_types=1); |
| 9 | + |
| 10 | +namespace OCA\Testing\TaskProcessing; |
| 11 | + |
| 12 | +use OCA\Testing\AppInfo\Application; |
| 13 | +use OCP\AppFramework\Services\IAppConfig; |
| 14 | +use OCP\TaskProcessing\Exception\ProcessingException; |
| 15 | +use OCP\TaskProcessing\ISynchronousProvider; |
| 16 | +use OCP\TaskProcessing\TaskTypes\TextToTextChat; |
| 17 | +use RuntimeException; |
| 18 | + |
| 19 | +class FakeTextToTextChatProvider implements ISynchronousProvider { |
| 20 | + |
| 21 | + public function __construct( |
| 22 | + protected IAppConfig $appConfig, |
| 23 | + ) { |
| 24 | + } |
| 25 | + |
| 26 | + public function getId(): string { |
| 27 | + return Application::APP_ID . '-text2textchat'; |
| 28 | + } |
| 29 | + |
| 30 | + public function getName(): string { |
| 31 | + return 'Fake text2text chat task processing provider'; |
| 32 | + } |
| 33 | + |
| 34 | + public function getTaskTypeId(): string { |
| 35 | + return TextToTextChat::ID; |
| 36 | + } |
| 37 | + |
| 38 | + public function getExpectedRuntime(): int { |
| 39 | + return 1; |
| 40 | + } |
| 41 | + |
| 42 | + public function getInputShapeEnumValues(): array { |
| 43 | + return []; |
| 44 | + } |
| 45 | + |
| 46 | + public function getInputShapeDefaults(): array { |
| 47 | + return []; |
| 48 | + } |
| 49 | + |
| 50 | + public function getOptionalInputShape(): array { |
| 51 | + return []; |
| 52 | + } |
| 53 | + |
| 54 | + public function getOptionalInputShapeEnumValues(): array { |
| 55 | + return []; |
| 56 | + } |
| 57 | + |
| 58 | + public function getOptionalInputShapeDefaults(): array { |
| 59 | + return []; |
| 60 | + } |
| 61 | + |
| 62 | + public function getOptionalOutputShape(): array { |
| 63 | + return []; |
| 64 | + } |
| 65 | + |
| 66 | + public function getOutputShapeEnumValues(): array { |
| 67 | + return []; |
| 68 | + } |
| 69 | + |
| 70 | + public function getOptionalOutputShapeEnumValues(): array { |
| 71 | + return []; |
| 72 | + } |
| 73 | + |
| 74 | + public function process(?string $userId, array $input, callable $reportProgress): array { |
| 75 | + if ($this->appConfig->getAppValueBool('fail-' . $this->getId())) { |
| 76 | + throw new ProcessingException('Failing as set by AppConfig'); |
| 77 | + } |
| 78 | + |
| 79 | + if (!isset($input['system_prompt']) || !is_string($input['system_prompt'])) { |
| 80 | + throw new RuntimeException('Invalid system prompt'); |
| 81 | + } |
| 82 | + |
| 83 | + if (!isset($input['input']) || !is_string($input['input'])) { |
| 84 | + throw new RuntimeException('Invalid input message'); |
| 85 | + } |
| 86 | + |
| 87 | + if (!isset($input['history']) || !is_array($input['history'])) { |
| 88 | + throw new RuntimeException('Invalid message history'); |
| 89 | + } |
| 90 | + |
| 91 | + return [ |
| 92 | + 'output' => 'This is a fake response message: ' |
| 93 | + . "\n\n- System prompt: " . $input['system_prompt'] |
| 94 | + . "\n- Input message: " . $input['input'] |
| 95 | + . "\n- Message history:\n" . count($input['history']) . ' messages', |
| 96 | + ]; |
| 97 | + } |
| 98 | +} |
0 commit comments