|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace App\Console\Commands; |
| 6 | + |
| 7 | +use App\Models\ConversationRecord; |
| 8 | +use App\Workflows\Conversation\ConversationStore; |
| 9 | +use App\Workflows\ServiceMode\Conversation\ConversationWorkflow; |
| 10 | +use DurableWorkflow\Bridge\Laravel\LaravelWorkflowClientInterface; |
| 11 | +use DurableWorkflow\WorkflowHandleInterface; |
| 12 | +use Illuminate\Console\Command; |
| 13 | +use Illuminate\Support\Str; |
| 14 | + |
| 15 | +final class ServiceModeConversation extends Command |
| 16 | +{ |
| 17 | + protected $signature = 'app:service-mode-conversation |
| 18 | + {--message=* : Scripted user message; repeat for several turns. Without it the command is interactive} |
| 19 | + {--decision=approve : Scripted answer to an approval request (approve or decline)} |
| 20 | + {--turns-per-run=3 : Turns before the run continues as new} |
| 21 | + {--workflow-id= : Use a specific workflow ID}'; |
| 22 | + |
| 23 | + protected $description = 'Run the conversation reference on a PHP service-mode worker'; |
| 24 | + |
| 25 | + private const WAIT_SECONDS = 60; |
| 26 | + |
| 27 | + public function __construct(private readonly LaravelWorkflowClientInterface $workflows) |
| 28 | + { |
| 29 | + parent::__construct(); |
| 30 | + } |
| 31 | + |
| 32 | + public function handle(): int |
| 33 | + { |
| 34 | + $workflowId = $this->option('workflow-id') ?: 'service-conversation-'.Str::lower((string) Str::ulid()); |
| 35 | + $workflow = $this->workflows->start( |
| 36 | + ConversationWorkflow::class, |
| 37 | + [[], max(1, (int) $this->option('turns-per-run'))], |
| 38 | + $workflowId, |
| 39 | + ); |
| 40 | + |
| 41 | + $this->info("Conversation {$workflowId} started. A service-mode worker processes it."); |
| 42 | + |
| 43 | + $scripted = $this->option('message'); |
| 44 | + $messages = $scripted !== [] ? $scripted : null; |
| 45 | + $turn = 0; |
| 46 | + |
| 47 | + while (true) { |
| 48 | + $text = $messages !== null ? array_shift($messages) : $this->ask('You ('.ConversationWorkflow::END_COMMAND.' to finish)'); |
| 49 | + |
| 50 | + if ($text === null || trim($text) === '' || trim($text) === ConversationWorkflow::END_COMMAND) { |
| 51 | + break; |
| 52 | + } |
| 53 | + |
| 54 | + if ($messages !== null) { |
| 55 | + $this->line("<comment>You:</comment> {$text}"); |
| 56 | + } |
| 57 | + |
| 58 | + $turn++; |
| 59 | + |
| 60 | + if (! $this->takeTurn($workflow, $workflowId, $turn, $text, $messages !== null)) { |
| 61 | + return self::FAILURE; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + $workflow->signal('message', [ConversationWorkflow::END_COMMAND]); |
| 66 | + $this->line(json_encode($workflow->result(self::WAIT_SECONDS, 0.25), JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR)); |
| 67 | + |
| 68 | + return self::SUCCESS; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Sends one message and answers any approval request until the turn's |
| 73 | + * reply is stored. |
| 74 | + * |
| 75 | + * The `status` query describes the current run, and a turn can end with |
| 76 | + * continue-as-new. The reply record is keyed by workflow ID and turn, so |
| 77 | + * it is the reliable sign that the turn finished. |
| 78 | + */ |
| 79 | + private function takeTurn(WorkflowHandleInterface $workflow, string $workflowId, int $turn, string $text, bool $scripted): bool |
| 80 | + { |
| 81 | + $reference = ConversationStore::replyReference($workflowId, $turn); |
| 82 | + $workflow->signal('message', [$text]); |
| 83 | + $answered = null; |
| 84 | + |
| 85 | + while (true) { |
| 86 | + $reply = null; |
| 87 | + $proposal = null; |
| 88 | + $ready = $this->waitUntil(function () use ($workflow, $reference, $answered, &$reply, &$proposal): bool { |
| 89 | + $reply = ConversationRecord::query()->find($reference); |
| 90 | + $proposal = $reply === null ? $this->status($workflow)['pending_proposal'] : null; |
| 91 | + |
| 92 | + return $reply !== null || ($proposal !== null && $proposal['id'] !== $answered); |
| 93 | + }); |
| 94 | + |
| 95 | + if (! $ready) { |
| 96 | + return false; |
| 97 | + } |
| 98 | + |
| 99 | + if ($reply !== null) { |
| 100 | + break; |
| 101 | + } |
| 102 | + |
| 103 | + $approved = $scripted |
| 104 | + ? $this->option('decision') === 'approve' |
| 105 | + : $this->confirm(sprintf('Approve "%s"?', $proposal['description'])); |
| 106 | + |
| 107 | + $this->line(sprintf('<comment>Reviewer:</comment> %s', $approved ? 'approve' : 'decline')); |
| 108 | + $workflow->signal('decision', [$proposal['id'], $approved, 'artisan']); |
| 109 | + $answered = $proposal['id']; |
| 110 | + } |
| 111 | + |
| 112 | + $this->line(sprintf('<info>Assistant:</info> %s', $reply->content)); |
| 113 | + |
| 114 | + return true; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * @return array<string, mixed> |
| 119 | + */ |
| 120 | + private function status(WorkflowHandleInterface $workflow): array |
| 121 | + { |
| 122 | + return $workflow->query('status'); |
| 123 | + } |
| 124 | + |
| 125 | + private function waitUntil(callable $condition): bool |
| 126 | + { |
| 127 | + $deadline = time() + self::WAIT_SECONDS; |
| 128 | + |
| 129 | + while (time() < $deadline) { |
| 130 | + if ($condition()) { |
| 131 | + return true; |
| 132 | + } |
| 133 | + |
| 134 | + usleep(250_000); |
| 135 | + } |
| 136 | + |
| 137 | + $this->error('Timed out waiting for the conversation. Is `php artisan durable-workflow:worker` running?'); |
| 138 | + |
| 139 | + return false; |
| 140 | + } |
| 141 | +} |
0 commit comments