- 添加 `FileReadTool`,支持文件内容读取与安全验证 - 引入 `hasToolMessages` 逻辑,优化工具历史上下文处理 - 修改工具选项逻辑,支持禁用工具时的动态调整 - 增加消息序列化逻辑,优化 Redis 序列管理与数据同步 - 扩展测试覆盖,验证序列化与工具调用场景 - 增强 Docker Compose 脚本,支持应用重置与日志清理 - 调整工具调用超时设置,提升运行时用户体验
60 lines
1.5 KiB
PHP
60 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\ChatSession;
|
|
use App\Models\Message;
|
|
use Illuminate\Support\Facades\Redis;
|
|
|
|
class MessageSequence
|
|
{
|
|
public function nextForSession(ChatSession $session): int
|
|
{
|
|
$key = $this->redisKey($session->session_id);
|
|
|
|
try {
|
|
$current = Redis::get($key);
|
|
|
|
if ($current === null) {
|
|
$seed = $this->seedFromDatabase($session);
|
|
Redis::setnx($key, $seed);
|
|
} elseif ((int) $current < $session->last_seq) {
|
|
Redis::set($key, (string) $session->last_seq);
|
|
}
|
|
|
|
return (int) Redis::incr($key);
|
|
} catch (\Throwable) {
|
|
return $session->last_seq + 1;
|
|
}
|
|
}
|
|
|
|
public function syncToAtLeast(string $sessionId, int $seq): void
|
|
{
|
|
$key = $this->redisKey($sessionId);
|
|
|
|
try {
|
|
$current = Redis::get($key);
|
|
if ($current === null || (int) $current < $seq) {
|
|
Redis::set($key, (string) $seq);
|
|
}
|
|
} catch (\Throwable) {
|
|
return;
|
|
}
|
|
}
|
|
|
|
private function seedFromDatabase(ChatSession $session): int
|
|
{
|
|
$maxPersistedSeq = (int) (Message::query()
|
|
->where('session_id', $session->session_id)
|
|
->max('seq') ?? 0);
|
|
|
|
return max($session->last_seq, $maxPersistedSeq);
|
|
}
|
|
|
|
private function redisKey(string $sessionId): string
|
|
{
|
|
return "chat_session:{$sessionId}:seq";
|
|
}
|
|
}
|
|
|