feat(smoke): 增加 AI 今日总结接口与首页缓存透出
This commit is contained in:
@@ -91,3 +91,46 @@ func yesterdayDate() time.Time {
|
||||
y := now.AddDate(0, 0, -1)
|
||||
return time.Date(y.Year(), y.Month(), y.Day(), 0, 0, 0, 0, time.Local)
|
||||
}
|
||||
|
||||
func todayDate() time.Time {
|
||||
now := time.Now().In(time.Local)
|
||||
return time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local)
|
||||
}
|
||||
|
||||
func (h *SmokeHandler) GetAIDailySummary(c *gin.Context) {
|
||||
user := middleware.MustCurrentUser(c)
|
||||
|
||||
summaryDate := todayDate()
|
||||
if dateStr := c.Query("date"); dateStr != "" {
|
||||
parsed, err := time.ParseInLocation(dateLayout, dateStr, time.Local)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "date 格式错误,应为 YYYY-MM-DD"))
|
||||
return
|
||||
}
|
||||
summaryDate = parsed
|
||||
}
|
||||
|
||||
record, err := h.smokeAIAdviceService.GetOrGenerateDailySummary(c.Request.Context(), user, summaryDate, smokeservice.DefaultSummaryPromptVersion)
|
||||
if err != nil {
|
||||
switch {
|
||||
case errors.Is(err, smokeservice.ErrAIAdviceLocked):
|
||||
c.JSON(http.StatusForbidden, model.Error(http.StatusForbidden, "需要会员或观看广告解锁后才可生成总结"))
|
||||
return
|
||||
case errors.Is(err, smokeservice.ErrAIServiceDisabled):
|
||||
c.JSON(http.StatusServiceUnavailable, model.Error(http.StatusServiceUnavailable, "AI 服务暂不可用,请联系管理员"))
|
||||
return
|
||||
case errors.Is(err, smokeservice.ErrNoSmokeLogsToday):
|
||||
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "今天还没有抽烟记录,无法生成总结"))
|
||||
return
|
||||
default:
|
||||
c.JSON(http.StatusInternalServerError, model.Error(http.StatusInternalServerError, "生成总结失败,请稍后重试"))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, model.Success(gin.H{
|
||||
"date": record.AdviceDate.Format(dateLayout),
|
||||
"content": record.Advice,
|
||||
"model": record.Model,
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ type homeDashboardResponse struct {
|
||||
CampaignCard homeCampaignCard `json:"campaign_card"`
|
||||
Timer homeTimerBlock `json:"timer"`
|
||||
Summary homeSummaryBlock `json:"summary"`
|
||||
DailySummary *homeDailySummaryBlock `json:"daily_summary,omitempty"`
|
||||
Motivation smokeservice.SmokeMotivation `json:"motivation"`
|
||||
QuickActions []homeQuickAction `json:"quick_actions"`
|
||||
DataSources homeDataSources `json:"data_sources"`
|
||||
@@ -50,14 +51,17 @@ type homeCampaignCard struct {
|
||||
}
|
||||
|
||||
type homeTimerBlock struct {
|
||||
Label string `json:"label"`
|
||||
LastSmokeAt string `json:"last_smoke_at"`
|
||||
SecondsSinceLast int `json:"seconds_since_last"`
|
||||
NextSuggestedAt string `json:"next_suggested_at"`
|
||||
NextSuggestedClock string `json:"next_suggested_clock"`
|
||||
NotBeforeAt string `json:"not_before_at"`
|
||||
SuggestionSource string `json:"suggestion_source"`
|
||||
SuggestionAlgorithm string `json:"suggestion_algorithm"`
|
||||
Label string `json:"label"`
|
||||
LastSmokeAt string `json:"last_smoke_at"`
|
||||
SecondsSinceLast int `json:"seconds_since_last"`
|
||||
NextSuggestedAt string `json:"next_suggested_at"`
|
||||
NextSuggestedClock string `json:"next_suggested_clock"`
|
||||
NotBeforeAt string `json:"not_before_at"`
|
||||
SuggestionSource string `json:"suggestion_source"`
|
||||
SuggestionAlgorithm string `json:"suggestion_algorithm"`
|
||||
AITimeNodes []string `json:"ai_time_nodes,omitempty"`
|
||||
AIAdvice string `json:"ai_advice,omitempty"`
|
||||
AIModel string `json:"ai_model,omitempty"`
|
||||
}
|
||||
|
||||
type homeSummaryBlock struct {
|
||||
@@ -80,6 +84,13 @@ type homeDataSources struct {
|
||||
PlanDate string `json:"plan_date"`
|
||||
}
|
||||
|
||||
type homeDailySummaryBlock struct {
|
||||
Date string `json:"date"`
|
||||
Content string `json:"content"`
|
||||
Model string `json:"model,omitempty"`
|
||||
Status string `json:"status"` // available | locked | no_data | empty
|
||||
}
|
||||
|
||||
func (h *SmokeHandler) Home(c *gin.Context) {
|
||||
user := middleware.MustCurrentUser(c)
|
||||
|
||||
@@ -149,6 +160,35 @@ func (h *SmokeHandler) Home(c *gin.Context) {
|
||||
SuggestionAlgorithm: defaultSuggestion.Algorithm,
|
||||
}
|
||||
|
||||
// 尝试读取 AI 缓存,有则覆盖 timer 的建议时间(默认算法作为保底)
|
||||
if aiSuggestion, ok, _ := h.smokeAINextService.GetCached(ctx, user, planDate, "v1"); ok {
|
||||
timerBlock.SuggestionSource = "ai"
|
||||
timerBlock.SuggestionAlgorithm = "ai_next_smoke_v1"
|
||||
timerBlock.AITimeNodes = aiSuggestion.TimeNodes
|
||||
timerBlock.AIAdvice = aiSuggestion.Advice
|
||||
timerBlock.AIModel = aiSuggestion.Model
|
||||
if aiSuggestion.SuggestedAt != "" {
|
||||
timerBlock.NextSuggestedAt = aiSuggestion.SuggestedAt
|
||||
if t, err := time.Parse(time.RFC3339, aiSuggestion.SuggestedAt); err == nil {
|
||||
timerBlock.NextSuggestedClock = t.In(time.Local).Format("15:04")
|
||||
}
|
||||
}
|
||||
if aiSuggestion.NotBeforeAt != "" {
|
||||
timerBlock.NotBeforeAt = aiSuggestion.NotBeforeAt
|
||||
}
|
||||
}
|
||||
|
||||
// 尝试读取今日 AI 总结缓存
|
||||
var dailySummaryBlock *homeDailySummaryBlock
|
||||
if summaryRecord, err := h.smokeAIAdviceService.GetCachedByType(ctx, int(user.ID), smokeservice.SmokeAIAdviceTypeDailySummary, planDate, smokeservice.DefaultSummaryPromptVersion); err == nil && summaryRecord != nil {
|
||||
dailySummaryBlock = &homeDailySummaryBlock{
|
||||
Date: summaryRecord.AdviceDate.Format(dateLayout),
|
||||
Content: summaryRecord.Advice,
|
||||
Model: summaryRecord.Model,
|
||||
Status: "available",
|
||||
}
|
||||
}
|
||||
|
||||
response := homeDashboardResponse{
|
||||
Greeting: greetingBlock(user.NickName, user.AvatarURL, now),
|
||||
Profile: profileView,
|
||||
@@ -173,7 +213,8 @@ func (h *SmokeHandler) Home(c *gin.Context) {
|
||||
ExceededYesterday: homeSummary.ExceededYesterday,
|
||||
ProfileCompleted: profileView.IsCompleted,
|
||||
},
|
||||
Motivation: motivation,
|
||||
DailySummary: dailySummaryBlock,
|
||||
Motivation: motivation,
|
||||
QuickActions: []homeQuickAction{
|
||||
{Type: "log_smoke", Title: "记录抽烟", Primary: false},
|
||||
{Type: "resist", Title: "想抽忍住了", Primary: true},
|
||||
|
||||
Reference in New Issue
Block a user