main: 扩展 Agent Run 调度与队列功能
- 增加 Agent Run MVP-0,包括 RunDispatcher 和 AgentRunJob - 优化队列配置,支持 Redis 队列驱动,添加 Horizon 容器 - 更新 Docker 配置,细化角色分工,新增 Horizon 配置 - 增加测试任务 `TestJob`,扩展队列使用示例 - 更新 OpenAPI 规范,添加 Agent Run 相关接口及示例 - 编写文档,详细描述 Agent Run 流程与 MVP-0 功能 - 优化相关服务与文档,支持队列与异步运行
This commit is contained in:
126
tests/Feature/AgentRunTest.php
Normal file
126
tests/Feature/AgentRunTest.php
Normal file
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Jobs\AgentRunJob;
|
||||
use App\Models\Message;
|
||||
use App\Services\ChatService;
|
||||
use App\Services\RunDispatcher;
|
||||
use App\Services\RunLoop;
|
||||
use App\Services\OutputSink;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Queue;
|
||||
use Tests\TestCase;
|
||||
|
||||
class AgentRunTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_dispatch_and_run_creates_agent_reply_and_statuses(): void
|
||||
{
|
||||
Queue::fake();
|
||||
$service = app(ChatService::class);
|
||||
$dispatcher = app(RunDispatcher::class);
|
||||
|
||||
$session = $service->createSession('Run Session');
|
||||
$prompt = $service->appendMessage([
|
||||
'session_id' => $session->session_id,
|
||||
'role' => Message::ROLE_USER,
|
||||
'type' => 'user.prompt',
|
||||
'content' => 'hello agent',
|
||||
]);
|
||||
|
||||
$runId = $dispatcher->dispatchForPrompt($session->session_id, $prompt->message_id);
|
||||
|
||||
Queue::assertPushed(AgentRunJob::class, function ($job) use ($session, $runId) {
|
||||
return $job->sessionId === $session->session_id && $job->runId === $runId;
|
||||
});
|
||||
|
||||
// simulate worker execution
|
||||
(new AgentRunJob($session->session_id, $runId))->handle(
|
||||
app(RunLoop::class),
|
||||
app(OutputSink::class)
|
||||
);
|
||||
|
||||
$messages = Message::query()
|
||||
->where('session_id', $session->session_id)
|
||||
->orderBy('seq')
|
||||
->get();
|
||||
|
||||
$this->assertTrue($messages->contains(fn ($m) => $m->type === 'run.status' && ($m->payload['status'] ?? null) === 'RUNNING'));
|
||||
$this->assertTrue($messages->contains(fn ($m) => $m->type === 'agent.message' && ($m->payload['run_id'] ?? null) === $runId));
|
||||
$this->assertTrue($messages->contains(fn ($m) => $m->type === 'run.status' && ($m->payload['status'] ?? null) === 'DONE'));
|
||||
}
|
||||
|
||||
public function test_second_prompt_dispatches_new_run_after_first_completes(): void
|
||||
{
|
||||
Queue::fake();
|
||||
$service = app(ChatService::class);
|
||||
$dispatcher = app(RunDispatcher::class);
|
||||
|
||||
$session = $service->createSession('Sequential Runs');
|
||||
$firstPrompt = $service->appendMessage([
|
||||
'session_id' => $session->session_id,
|
||||
'role' => Message::ROLE_USER,
|
||||
'type' => 'user.prompt',
|
||||
'content' => 'first run',
|
||||
]);
|
||||
|
||||
$firstRunId = $dispatcher->dispatchForPrompt($session->session_id, $firstPrompt->message_id);
|
||||
|
||||
(new AgentRunJob($session->session_id, $firstRunId))->handle(
|
||||
app(RunLoop::class),
|
||||
app(OutputSink::class)
|
||||
);
|
||||
|
||||
$secondPrompt = $service->appendMessage([
|
||||
'session_id' => $session->session_id,
|
||||
'role' => Message::ROLE_USER,
|
||||
'type' => 'user.prompt',
|
||||
'content' => 'second run',
|
||||
]);
|
||||
|
||||
$secondRunId = $dispatcher->dispatchForPrompt($session->session_id, $secondPrompt->message_id);
|
||||
|
||||
$this->assertNotSame($firstRunId, $secondRunId);
|
||||
|
||||
Queue::assertPushed(AgentRunJob::class, 2);
|
||||
Queue::assertPushed(AgentRunJob::class, function ($job) use ($secondRunId, $session) {
|
||||
return $job->runId === $secondRunId && $job->sessionId === $session->session_id;
|
||||
});
|
||||
}
|
||||
|
||||
public function test_cancel_prevents_agent_reply_and_marks_canceled(): void
|
||||
{
|
||||
Queue::fake();
|
||||
$service = app(ChatService::class);
|
||||
$dispatcher = app(RunDispatcher::class);
|
||||
$loop = app(RunLoop::class);
|
||||
|
||||
$session = $service->createSession('Cancel Session');
|
||||
$prompt = $service->appendMessage([
|
||||
'session_id' => $session->session_id,
|
||||
'role' => Message::ROLE_USER,
|
||||
'type' => 'user.prompt',
|
||||
'content' => 'please cancel',
|
||||
]);
|
||||
|
||||
$runId = $dispatcher->dispatchForPrompt($session->session_id, $prompt->message_id);
|
||||
|
||||
$service->appendMessage([
|
||||
'session_id' => $session->session_id,
|
||||
'role' => Message::ROLE_USER,
|
||||
'type' => 'run.cancel.request',
|
||||
'payload' => ['run_id' => $runId],
|
||||
]);
|
||||
|
||||
$loop->run($session->session_id, $runId);
|
||||
|
||||
$messages = Message::query()
|
||||
->where('session_id', $session->session_id)
|
||||
->get();
|
||||
|
||||
$this->assertFalse($messages->contains(fn ($m) => $m->type === 'agent.message' && ($m->payload['run_id'] ?? null) === $runId));
|
||||
$this->assertTrue($messages->contains(fn ($m) => $m->type === 'run.status' && ($m->payload['status'] ?? null) === 'CANCELED'));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user