perf: AI机器人支持多会话

This commit is contained in:
kuaifan 2025-02-10 15:43:02 +09:00
parent 30d88761b4
commit 0272933f70
5 changed files with 67 additions and 18 deletions

View File

@ -541,6 +541,8 @@ class DialogController extends AbstractController
//
if ($session_id > 0) {
$builder->whereSessionId($session_id);
} elseif ($dialog->session_id > 0) {
$builder->whereSessionId($dialog->session_id);
}
if ($msg_type) {
if ($msg_type === 'tag') {

View File

@ -19,6 +19,7 @@ use Illuminate\Support\Facades\DB;
* @property int $id
* @property string|null $type 对话类型
* @property string|null $group_type 聊天室类型
* @property int|null $session_id 会话ID最新
* @property string|null $name 对话名称
* @property string $avatar 头像(群)
* @property int|null $owner_id 群主用户ID
@ -48,6 +49,7 @@ use Illuminate\Support\Facades\DB;
* @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialog whereLinkId($value)
* @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialog whereName($value)
* @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialog whereOwnerId($value)
* @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialog whereSessionId($value)
* @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialog whereTopMsgId($value)
* @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialog whereTopUserid($value)
* @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialog whereType($value)

View File

@ -7,7 +7,6 @@ namespace App\Models;
*
* @property int $id
* @property int $dialog_id 对话ID
* @property int $userid 用户ID
* @property string $title 会话标题
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
@ -27,7 +26,6 @@ namespace App\Models;
* @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialogSession whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialogSession whereTitle($value)
* @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialogSession whereUpdatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialogSession whereUserid($value)
* @mixin \Eloquent
*/
class WebSocketDialogSession extends AbstractModel
@ -50,12 +48,4 @@ class WebSocketDialogSession extends AbstractModel
{
return $this->belongsTo(WebSocketDialog::class, 'dialog_id');
}
/**
* 获取关联的用户
*/
public function user()
{
return $this->belongsTo(User::class, 'userid');
}
}

View File

@ -0,0 +1,34 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class WebSocketDialogsAddSessionId extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('web_socket_dialogs', function (Blueprint $table) {
if (!Schema::hasColumn('web_socket_dialogs', 'session_id')) {
$table->bigInteger('session_id')->index()->nullable()->default(0)->after('group_type')->comment('会话ID最新');
}
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('web_socket_dialogs', function (Blueprint $table) {
$table->dropColumn('session_id');
});
}
}

View File

@ -1,5 +1,9 @@
<?php
use App\Models\WebSocketDialog;
use App\Models\WebSocketDialogMsg;
use App\Models\WebSocketDialogSession;
use App\Module\Base;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
@ -13,14 +17,31 @@ class CreateWebSocketDialogSessionsTable extends Migration
*/
public function up()
{
if (!Schema::hasTable('web_socket_dialog_sessions')) {
Schema::create('web_socket_dialog_sessions', function (Blueprint $table) {
$table->id();
$table->bigInteger('dialog_id')->unsigned()->index()->comment('对话ID');
$table->bigInteger('userid')->unsigned()->index()->comment('用户ID');
$table->string('title', 255)->default('')->comment('会话标题');
$table->timestamps();
});
if (Schema::hasTable('web_socket_dialog_sessions')) {
return;
}
Schema::create('web_socket_dialog_sessions', function (Blueprint $table) {
$table->id();
$table->bigInteger('dialog_id')->unsigned()->index()->comment('对话ID');
$table->string('title', 255)->default('')->comment('会话标题');
$table->timestamps();
});
$list = WebSocketDialog::select(['web_socket_dialogs.*', 'u.email'])
->join('web_socket_dialog_users as du', 'web_socket_dialogs.id', '=', 'du.dialog_id')
->join('users as u', 'du.userid', '=', 'u.userid')
->where('u.email', 'like', 'ai-%@bot.system')
->where('web_socket_dialogs.type', 'user')
->get();
foreach ($list as $item) {
$title = WebSocketDialogMsg::whereDialogId($item->id)->where('key', '!=', '')->orderBy('id')->value('key');
$session = WebSocketDialogSession::create([
'dialog_id' => $item->id,
'title' => $title ? Base::cutStr($title, 100) : 'Unknown',
]);
$session->save();
$item->session_id = $session->id;
$item->save();
WebSocketDialogMsg::whereDialogId($item->id)->update(['session_id' => $session->id]);
}
}