Files
smartmate/backend/api/course.go
Losita d3cec2a5b9 Version: 0.7.0.dev.260319
 feat(agent): 新增智能排程 Agent 全链路 + ReAct 精排引擎

  🏗️ 智能排程 Graph 编排(阶段 1 基础链路)
  - 新增 scheduleplan 包:state / tool / prompt / nodes / runner / graph 六件套
  - 实现 plan → preview → materialize → apply → reflect → finalize 完整图编排
  - 通过函数注入解耦 agent 层与 service 层,避免循环依赖
  - 路由层新增 schedule_plan 动作,复用现有 SSE + 持久化链路

  🧠 ReAct 精排引擎(阶段 1.5 语义化微调)
  - 粗排后构建"混合日程"(既有课程 + 建议任务),统一为 HybridScheduleEntry
  - LLM 开启深度思考,通过 Swap / Move / TimeAvailable / GetAvailableSlots 四个 Tool 在内存中优化任务时间
  - reasoning_content 实时流式推送前端,用户可见 AI 思考过程
  - 精排结果仅预览不落库,向后兼容(未注入依赖时走原有 materialize 路径)

  📝 文档
  - 新增 ReAct 精排引擎决策记录

  ⚠️ 已知问题:深度思考模式耗时较长,超时策略待优化
2026-03-19 23:18:56 +08:00

70 lines
1.9 KiB
Go

package api
import (
"context"
"errors"
"net/http"
"time"
"github.com/LoveLosita/smartflow/backend/model"
"github.com/LoveLosita/smartflow/backend/respond"
"github.com/LoveLosita/smartflow/backend/service"
"github.com/gin-gonic/gin"
)
type CourseHandler struct {
// 伸出手:准备接住 Service
service *service.CourseService
}
// NewCourseHandler 创建 CourseHandler 实例
func NewCourseHandler(service *service.CourseService) *CourseHandler {
return &CourseHandler{
service: service,
}
}
func (sa *CourseHandler) CheckUserCourse(c *gin.Context) {
//1.从请求中获取课程信息
var req model.UserCheckCourseRequest
err := c.ShouldBindJSON(&req)
if err != nil {
c.JSON(http.StatusBadRequest, respond.WrongParamType)
return
}
//2.调用 service 层的 CheckSingleCourse 方法进行校验
result := service.CheckSingleCourse(req)
//3.根据校验结果返回响应
if result {
c.JSON(http.StatusOK, respond.Ok)
} else {
c.JSON(http.StatusBadRequest, respond.WrongCourseInfo)
}
}
func (sa *CourseHandler) AddUserCourses(c *gin.Context) {
//1.从请求中获取课程信息
var req model.UserImportCoursesRequest
err := c.ShouldBindJSON(&req)
if err != nil {
c.JSON(http.StatusBadRequest, respond.WrongParamType)
return
}
//2.从上下文获取用户ID
userIDInterface := c.GetInt("user_id")
//3.调用 service 层的 AddUserCoursesIntoSchedule 方法添加课程
// 创建一个带 1 秒超时的上下文
ctx, cancel := context.WithTimeout(c.Request.Context(), 10*time.Second)
defer cancel() // 记得释放资源
conflicts, err := sa.service.AddUserCourses(ctx, req, userIDInterface)
if err != nil {
if errors.Is(err, respond.ScheduleConflict) {
c.JSON(http.StatusConflict, respond.RespWithData(respond.ScheduleConflict, conflicts))
}
respond.DealWithError(c, err)
return
}
//4.返回成功响应
c.JSON(http.StatusOK, respond.Ok)
}