You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
2.6 KiB
85 lines
2.6 KiB
package allhelp
|
|
|
|
import (
|
|
"yunyan/comm"
|
|
"yunyan/pb"
|
|
"yunyan/utils"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// SubmitChatSummary 提交每日聊天内容,异步生成AI总结
|
|
// @Summary 提交每日聊天总结任务
|
|
// @Description 提交某一天的聊天消息列表,异步调用AI生成总结;同一(uid,日期)重复提交会覆盖重算
|
|
// @Tags Allhelp
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param user body pb.AllhelpSubmitChatSummaryReq true "请求数据"
|
|
// @Success 200 {object} comm.HttpResult{data=pb.AllhelpSubmitChatSummaryResp} "响应数据"
|
|
// @Router /api/home/allhelp_submitchatsummary [post]
|
|
func (this *apiComp) SubmitChatSummary(session comm.IUserSession, req *pb.AllhelpSubmitChatSummaryReq) (resp *pb.AllhelpSubmitChatSummaryResp, errdata *pb.ErrorData) {
|
|
uid := session.GetUserId()
|
|
if uid == "" {
|
|
errdata = &pb.ErrorData{Code: pb.ErrorCode_ReqParameterError, Message: "用户未登录"}
|
|
return
|
|
}
|
|
if !isValidDate(req.SummaryDate) {
|
|
errdata = &pb.ErrorData{Code: pb.ErrorCode_ReqParameterError, Message: "summary_date 必须为 YYYY-MM-DD"}
|
|
return
|
|
}
|
|
if len(req.Msgs) == 0 {
|
|
errdata = &pb.ErrorData{Code: pb.ErrorCode_ReqParameterError, Message: "聊天内容不能为空"}
|
|
return
|
|
}
|
|
|
|
rec := &pb.DBChatSummary{
|
|
Uid: uid,
|
|
SummaryDate: req.SummaryDate,
|
|
ChatContent: joinChatMsgs(req.Msgs),
|
|
ChatMsgs: utils.ToString(req.Msgs),
|
|
Summary: "",
|
|
State: pb.ChatSummaryState_ChatSummaryState_Pending,
|
|
ErrorMsg: "",
|
|
FinishTime: 0,
|
|
}
|
|
if err := this.module.model.upsertChatSummary(rec); err != nil {
|
|
this.module.Errorln("SubmitChatSummary upsert error:", err)
|
|
errdata = &pb.ErrorData{Code: pb.ErrorCode_DBError, Message: "入库失败"}
|
|
return
|
|
}
|
|
if err := this.module.summary.Enqueue(rec.Id); err != nil {
|
|
this.module.Errorln("SubmitChatSummary enqueue error:", err)
|
|
errdata = &pb.ErrorData{Code: pb.ErrorCode_DBError, Message: "入队失败"}
|
|
return
|
|
}
|
|
resp = &pb.AllhelpSubmitChatSummaryResp{Record: rec}
|
|
return
|
|
}
|
|
|
|
// isValidDate 检查 YYYY-MM-DD
|
|
func isValidDate(s string) bool {
|
|
_, err := time.Parse("2006-01-02", s)
|
|
return err == nil
|
|
}
|
|
|
|
// joinChatMsgs 将结构化消息拼接为 AI 可读的文本
|
|
func joinChatMsgs(msgs []*pb.ChatMsg) string {
|
|
var b strings.Builder
|
|
for _, m := range msgs {
|
|
if m == nil {
|
|
continue
|
|
}
|
|
speaker := m.Speaker
|
|
if speaker == "" {
|
|
speaker = "user"
|
|
}
|
|
if m.Time > 0 {
|
|
b.WriteString(fmt.Sprintf("[%s %s] %s\n", time.Unix(m.Time, 0).Format("15:04"), speaker, m.Content))
|
|
} else {
|
|
b.WriteString(fmt.Sprintf("[%s] %s\n", speaker, m.Content))
|
|
}
|
|
}
|
|
return b.String()
|
|
}
|
|
|