From 6ae46cf7bbf2836fb119360339686742025d4768 Mon Sep 17 00:00:00 2001 From: kuaifan Date: Sat, 22 Feb 2025 17:49:11 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E6=94=AF=E6=8C=81=E5=9C=A8AI=E5=AF=B9?= =?UTF-8?q?=E8=AF=9D=E4=B8=AD=E7=9B=B4=E6=8E=A5=E5=BC=95=E7=94=A8=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1=E6=8F=90=E9=97=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Models/ProjectTask.php | 61 +++++++++++++++++++++++ app/Tasks/BotReceiveMsgTask.php | 86 ++++++++++++++++++--------------- 2 files changed, 108 insertions(+), 39 deletions(-) diff --git a/app/Models/ProjectTask.php b/app/Models/ProjectTask.php index 5f542ed5f..3d33ae4a2 100644 --- a/app/Models/ProjectTask.php +++ b/app/Models/ProjectTask.php @@ -12,6 +12,7 @@ use App\Tasks\PushTask; use App\Exceptions\ApiException; use App\Observers\ProjectTaskObserver; use Hhxsv5\LaravelS\Swoole\Task\Task; +use League\HTMLToMarkdown\HtmlConverter; use Illuminate\Database\Eloquent\SoftDeletes; /** @@ -1869,6 +1870,66 @@ class ProjectTask extends AbstractModel return true; } + /** + * 生成AI上下文 + * @return array + */ + public function AIContext() + { + $contexts = []; + if ($this->archived_at) { + $contexts[] = "任务状态:已归档"; + $contexts[] = "归档时间:" . $this->archived_at; + } elseif ($this->complete_at) { + $contexts[] = "任务状态:已完成"; + $contexts[] = "完成时间:" . $this->complete_at; + } elseif ($this->end_at && Carbon::parse($this->end_at)->lt(Carbon::now())) { + $contexts[] = "任务状态:已过期"; + $contexts[] = "任务截止时间:" . $this->end_at; + } else { + $contexts[] = "任务状态:进行中"; + if ($this->start_at) { + $contexts[] = "任务开始时间:" . $this->start_at; + } + if ($this->end_at) { + $contexts[] = "任务截止时间:" . $this->end_at; + } + } + if ($this->content) { + $taskDesc = $this->content?->getContentInfo(); + if ($taskDesc) { + $converter = new HtmlConverter(['strip_tags' => true]); + $descContent = Base::cutStr($converter->convert($taskDesc['content']), 2000); + $contexts[] = <<whereParentId($this->id)->get(); + if ($subTask->isNotEmpty()) { + $subTaskContent = $subTask->map(function($item) { + if ($item->complete_at) { + $status = " (已完成)"; + } elseif ($item->end_at && Carbon::parse($item->end_at)->lt(Carbon::now())) { + $status = " (已过期)"; + } else { + $status = " (进行中)"; + } + return " - {$item->name} {$status}"; + })->join("\n"); + if ($subTaskContent) { + $contexts[] = <<client['platform'], ['win', 'mac', 'web']) && !Base::judgeClientVersion("0.41.11", $this->client['version'])) { $errorContent = '当前客户端版本低(所需版本≥v0.41.11)。'; } + $attachments = Cache::get("bot:{$msg->id}:attachments"); + if ($attachments) { + $command = <<reply_id > 0) { $replyMsg = WebSocketDialogMsg::find($msg->reply_id); $replyCommand = ''; if ($replyMsg) { $replyCommand = $this->extractCommand($replyMsg); if ($replyCommand) { - $replyCommand = "" . Base::cutStr($replyCommand, 2000) . "\n\nThe content within the above tags is a citation.\n\n"; + $replyCommand = Base::cutStr($replyCommand, 2000); + $replyCommand = << + {$replyCommand} + + + 上述 quoted 标签内的内容为引用。 + + EOF; } } $command = $replyCommand . $command; @@ -563,6 +580,31 @@ class BotReceiveMsgTask extends AbstractTask $command = ":" . substr($command, 4); } } else { + $attachments = []; + if (preg_match_all("/(.*?)<\/span>/", $original, $match)) { + $taskIds = Base::newIntval($match[1]); + foreach ($taskIds as $index => $taskId) { + $taskInfo = ProjectTask::with(['content'])->whereId($taskId)->first(); + if ($taskInfo) { + $taskContext = implode("\n", $taskInfo->AIContext()); + $attachments[] = <<name}】 + {$taskContext} + EOF; + } else { + $attachments[] = <<id}:attachments", implode("\n\n", $attachments), 60); + } $command = trim(strip_tags($original)); } if (empty($command)) { @@ -625,45 +667,11 @@ class BotReceiveMsgTask extends AbstractTask $taskInfo = ProjectTask::with(['content'])->whereDialogId($dialog->id)->first(); if ($taskInfo) { $taskText = "当前我在任务【{$taskInfo->name}】中"; - if ($taskInfo->archived_at) { - $taskText .= ",此任务已经归档"; - } elseif ($taskInfo->complete_at) { - $taskText .= ",此任务已经完成"; - } elseif ($taskInfo->end_at && Carbon::parse($taskInfo->end_at)->lt(Carbon::now())) { - $taskText .= ",此任务已经过期"; + $taskContext = implode("\n", $taskInfo->AIContext()); + if ($taskContext) { + $taskText .= "\n{$taskContext}"; } $before_text[] = $taskText; - if ($taskInfo->content) { - $taskDesc = $taskInfo->content?->getContentInfo(); - if ($taskDesc) { - $converter = new HtmlConverter(['strip_tags' => true]); - $descContent = Base::cutStr($converter->convert($taskDesc['content']), 2000); - $before_text[] = <<whereParentId($taskInfo->id)->get(); - if ($subTask->isNotEmpty()) { - $subTaskContent = $subTask->map(function($item) { - $status = ""; - if ($item->complete_at) { - $status = " (已完成)"; - } elseif ($item->end_at && Carbon::parse($item->end_at)->lt(Carbon::now())) { - $status = " (已过期)"; - } - return " - {$item->name} {$status}"; - })->join("\n"); - if ($subTaskContent) { - $before_text[] = <<