feat: 添加 AI 助手生成任务功能

- 在 ProjectController 中新增 ai_generate 接口,支持根据用户输入生成任务标题和详细描述
- 在 AI 模块中实现 generateTask 方法,处理任务生成逻辑
- 更新前端 TaskAdd 组件,添加 AI 生成按钮,集成任务生成请求
- 优化 TEditor 和 TEditorTask 组件,支持设置内容格式
- 增强样式以提升用户体验
This commit is contained in:
kuaifan
2025-09-23 13:11:33 +08:00
parent 1c4bae2d91
commit 8ddc507bd5
7 changed files with 385 additions and 12 deletions

View File

@@ -14,6 +14,7 @@ use App\Models\User;
use App\Module\Base;
use App\Module\Timer;
use Swoole\Coroutine;
use App\Module\AI;
use App\Models\Deleted;
use App\Models\Project;
use App\Module\TimeRange;
@@ -2590,6 +2591,66 @@ class ProjectController extends AbstractController
return Base::retSuccess('移动成功', $data);
}
/**
* @api {post} api/project/task/ai_generate 40. 使用 AI 助手生成任务
*
* @apiDescription 需要token身份使用AI根据用户输入和上下文信息生成任务标题和详细描述
* @apiVersion 1.0.0
* @apiGroup project
* @apiName task__ai_generate
*
* @apiParam {String} content 用户输入的任务描述(必填)
* @apiParam {String} [current_title] 当前已有的任务标题(用于优化改进)
* @apiParam {String} [current_content] 当前已有的任务内容HTML格式用于优化改进
* @apiParam {String} [template_name] 选中的任务模板名称
* @apiParam {String} [template_content] 选中的任务模板内容HTML格式
* @apiParam {Boolean} [has_owner] 是否已设置负责人
* @apiParam {Boolean} [has_time_plan] 是否已设置计划时间
* @apiParam {String} [priority_level] 任务优先级等级名称
*
* @apiSuccess {Number} ret 返回状态码1正确、0错误
* @apiSuccess {String} msg 返回信息(错误描述)
* @apiSuccess {Object} data 返回数据
* @apiSuccess {String} data.title AI 生成的任务标题
* @apiSuccess {String} data.content AI 生成的任务内容HTML 格式)
* @apiSuccess {Array} data.subtasks 当任务较复杂时生成的子任务名称列表
*/
public function task__ai_generate()
{
User::auth();
// 获取用户输入的任务描述
$content = Request::input('content');
if (empty($content)) {
return Base::retError('任务描述不能为空');
}
// 获取上下文信息
$context = [
'current_title' => Request::input('current_title', ''),
'current_content' => Request::input('current_content', ''),
'template_name' => Request::input('template_name', ''),
'template_content' => Request::input('template_content', ''),
'has_owner' => boolval(Request::input('has_owner', false)),
'has_time_plan' => boolval(Request::input('has_time_plan', false)),
'priority_level' => Request::input('priority_level', ''),
];
// 如果当前内容是HTML格式转换为markdown
if (!empty($context['current_content'])) {
$context['current_content'] = Base::html2markdown($context['current_content']);
}
if (!empty($context['template_content'])) {
$context['template_content'] = Base::html2markdown($context['template_content']);
}
$result = AI::generateTask($content, $context);
if (Base::isError($result)) {
return Base::retError('生成任务失败', $result);
}
return Base::retSuccess('生成任务成功', $result['data']);
}
/**
* @api {get} api/project/flow/list 40. 工作流列表
*