feat(smoke): 增加 AI 今日总结接口与首页缓存透出
This commit is contained in:
@@ -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