- 增加 Agent Run MVP-0,包括 RunDispatcher 和 AgentRunJob - 优化队列配置,支持 Redis 队列驱动,添加 Horizon 容器 - 更新 Docker 配置,细化角色分工,新增 Horizon 配置 - 增加测试任务 `TestJob`,扩展队列使用示例 - 更新 OpenAPI 规范,添加 Agent Run 相关接口及示例 - 编写文档,详细描述 Agent Run 流程与 MVP-0 功能 - 优化相关服务与文档,支持队列与异步运行
150 lines
5.5 KiB
PHP
150 lines
5.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Http\Resources\MessageResource;
|
|
use App\Services\ChatService;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Response;
|
|
use Illuminate\Support\Facades\Redis;
|
|
use Symfony\Component\HttpFoundation\StreamedResponse;
|
|
|
|
class ChatSessionSseController extends Controller
|
|
{
|
|
public function __construct(private readonly ChatService $service)
|
|
{
|
|
}
|
|
|
|
public function stream(Request $request, string $sessionId): Response|StreamedResponse
|
|
{
|
|
$this->service->getSession($sessionId); // ensure exists
|
|
|
|
$lastEventId = $request->header('Last-Event-ID');
|
|
$afterSeq = is_numeric($lastEventId) ? (int) $lastEventId : (int) $request->query('after_seq', 0);
|
|
$limit = (int) $request->query('limit', 200);
|
|
$limit = $limit > 0 && $limit <= 500 ? $limit : 200;
|
|
|
|
$useBacklogOnly = app()->runningUnitTests()
|
|
|| app()->environment('testing')
|
|
|| defined('PHPUNIT_COMPOSER_INSTALL')
|
|
|| ! class_exists(\Redis::class);
|
|
|
|
if ($useBacklogOnly) {
|
|
$lastSentSeq = $afterSeq;
|
|
ob_start();
|
|
$this->sendBacklog($sessionId, $lastSentSeq, $limit, false);
|
|
$content = ob_get_clean() ?: '';
|
|
|
|
return response($content, 200, [
|
|
'Content-Type' => 'text/event-stream',
|
|
'Cache-Control' => 'no-cache',
|
|
'X-Accel-Buffering' => 'no',
|
|
]);
|
|
}
|
|
|
|
$response = new StreamedResponse(function () use ($sessionId, $afterSeq, $limit) {
|
|
$lastSentSeq = $afterSeq;
|
|
|
|
$this->sendBacklog($sessionId, $lastSentSeq, $limit);
|
|
|
|
try {
|
|
$redis = Redis::connection()->client();
|
|
if (method_exists($redis, 'setOption')) {
|
|
$redis->setOption(\Redis::OPT_READ_TIMEOUT, 5);
|
|
}
|
|
|
|
$channel = "session:{$sessionId}:messages";
|
|
$lastPing = time();
|
|
logger()->info('sse open');
|
|
if (method_exists($redis, 'pubSubLoop')) {
|
|
$pubSub = $redis->pubSubLoop();
|
|
$pubSub->subscribe($channel);
|
|
|
|
foreach ($pubSub as $message) {
|
|
if ($message->kind === 'subscribe') {
|
|
continue;
|
|
}
|
|
|
|
if (connection_aborted()) {
|
|
$pubSub->unsubscribe();
|
|
break;
|
|
}
|
|
|
|
$payloadId = $message->payload ?? null;
|
|
if ($payloadId) {
|
|
$msg = $this->service->getMessage($sessionId, $payloadId);
|
|
if ($msg && $msg->seq > $lastSentSeq) {
|
|
$this->emitMessage($msg);
|
|
$lastSentSeq = $msg->seq;
|
|
}
|
|
}
|
|
|
|
if (time() - $lastPing >= 180) {
|
|
logger()->info('ping: sent'.$sessionId);
|
|
echo ": ping\n\n";
|
|
@ob_flush();
|
|
@flush();
|
|
$lastPing = time();
|
|
}
|
|
}
|
|
logger()->info('close: sent'.$sessionId);
|
|
unset($pubSub);
|
|
} else {
|
|
// Fallback for Redis drivers without pubSubLoop (older phpredis)
|
|
$redis->subscribe([$channel], function ($redisInstance, $chan, $payload) use (&$lastSentSeq, $sessionId) {
|
|
if (connection_aborted()) {
|
|
$redisInstance->unsubscribe([$chan]);
|
|
return;
|
|
}
|
|
|
|
if (! $payload) {
|
|
return;
|
|
}
|
|
|
|
$msg = $this->service->getMessage($sessionId, $payload);
|
|
if ($msg && $msg->seq > $lastSentSeq) {
|
|
$this->emitMessage($msg);
|
|
$lastSentSeq = $msg->seq;
|
|
}
|
|
});
|
|
}
|
|
} catch (\RedisException $exception) {
|
|
logger()->warning('SSE redis subscription failed', [
|
|
'session_id' => $sessionId,
|
|
'message' => $exception->getMessage(),
|
|
]);
|
|
echo ": redis-error\n\n";
|
|
@ob_flush();
|
|
@flush();
|
|
}
|
|
});
|
|
|
|
$response->headers->set('Content-Type', 'text/event-stream');
|
|
$response->headers->set('Cache-Control', 'no-cache');
|
|
$response->headers->set('X-Accel-Buffering', 'no');
|
|
|
|
return $response;
|
|
}
|
|
|
|
private function sendBacklog(string $sessionId, int &$lastSentSeq, int $limit, bool $flush = true): void
|
|
{
|
|
$backlog = $this->service->listMessagesBySeq($sessionId, $lastSentSeq, $limit);
|
|
foreach ($backlog as $message) {
|
|
$this->emitMessage($message, $flush);
|
|
$lastSentSeq = $message->seq;
|
|
}
|
|
}
|
|
|
|
private function emitMessage($message, bool $flush = true): void
|
|
{
|
|
$payload = (new MessageResource($message))->resolve();
|
|
echo 'id: '.$message->seq."\n";
|
|
echo "event: message\n";
|
|
echo 'data: '.json_encode($payload, JSON_UNESCAPED_UNICODE)."\n\n";
|
|
if ($flush) {
|
|
@ob_flush();
|
|
@flush();
|
|
}
|
|
}
|
|
}
|