refactor: automation 中心落地 + 清理 delivery_rules/SystemMessage 死代码
把卡密自动发货从 engine.DeliveryService 收敛到统一的 automation.Center: WS 事件/计划任务/手动触发统一为 Task,trigger_key 持久化防重,付款发货、 评价赠品、超时求评价共用一套规则+动作编排。新增 internal/automation 包、 db.AutomationRules repository、00007 迁移。 同步删除被取代的并行死系统: - engine.DeliveryService + delivery_test.go(已无引用) - db.DeliveryRules/DeliveryRule + 3 个 Match 方法(生产零调用) - card_handlers 的 /delivery-rules CRUD(前端早已只走 automation-rules) - engine.SystemMessage + extractSystemMessage(dispatch 不再单独路由 message["3"]) - store.DeliveryRules 字段、Cards.IncrementDeliveryTimes - 00008 迁移 DROP delivery_rules/delivery_rule_variants + email_verifications/ captcha_codes/ai_item_cache/risk_control_logs + publish 批次 3 个死列 - isAPIPath 清理 ~15 个未挂载的前缀,stub_handlers.go 内联删除 - vite.config 删 /delivery-rules proxy 其它:orders.Upsert 注释改为诚实描述(version 列不参与并发控制); dispatch.cleanupDedupLocked 用 sort.Slice 取代 O(n²) 选择排序; 删自定义 min 用内置;deliveryRuleItemOwned 改名 itemOwnedByUser。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
5546494577
commit
57f9090dd2
29 files changed
+2469
-2382
No files matched your search
+20
-72
@@ -3,8 +3,11 @@ package engine
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"xianyu-go/internal/automation"
|
||||
)
|
||||
|
||||
// dispatch 是 ws.ReceiveLoop 的回调,对每条解密后的消息做:
|
||||
@@ -29,40 +32,27 @@ func (a *Account) dispatch(decrypted map[string]any) {
|
||||
|
||||
// handleMessage 分类并投递消息。
|
||||
func (a *Account) handleMessage(decrypted map[string]any) {
|
||||
// 第一优先级:系统卡片和平台通知进入自动化中心。
|
||||
// 这里不判断具体业务,只做“平台事件”事实解析;系统消息永远不进入 AI 回复范围。
|
||||
if task := automation.ExtractTaskFromWS(a.CookieID, a.currentCookieStr(), decrypted); task != nil {
|
||||
if a.handler != nil {
|
||||
if err := a.handler.HandleSystemEvent(context.Background(), *task); err != nil {
|
||||
a.logger.Error("处理系统自动化事件失败", "err", err, "trigger", task.TriggerType)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
chat := extractChatMessage(decrypted, a.CookieID, a.currentCookieStr())
|
||||
if chat != nil && chat.Text != "" {
|
||||
// 去重。
|
||||
if !a.markAndCheckDedup(decrypted, chat) {
|
||||
return
|
||||
}
|
||||
// 自动发货触发消息必须立即处理,不能进入普通聊天防抖。
|
||||
// 否则闲鱼紧随其后的系统提示(如“不想宝贝被砍价”)可能覆盖付款卡片,
|
||||
// 造成未发货且进入 AI 回复。
|
||||
if IsAutoDeliveryTrigger(chat.Text) {
|
||||
a.cancelDebouncedReply(chat.ChatID)
|
||||
if a.delivery != nil {
|
||||
if err := a.delivery.Handle(context.Background(), *chat); err != nil {
|
||||
a.logger.Error("处理自动发货失败", "err", err, "chat_id", chat.ChatID)
|
||||
}
|
||||
}
|
||||
if a.handler != nil {
|
||||
if err := a.handler.HandleChatMessage(context.Background(), *chat); err != nil {
|
||||
a.logger.Error("处理聊天消息失败", "err", err, "chat_id", chat.ChatID)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// 普通聊天消息走防抖,合并连续文本后再自动回复。
|
||||
// 用户消息走防抖,合并连续文本后再进入关键词/AI/默认回复链。
|
||||
a.scheduleDebouncedReply(*chat)
|
||||
return
|
||||
}
|
||||
sys := extractSystemMessage(decrypted, a.CookieID, a.currentCookieStr())
|
||||
if sys != nil && sys.RedReminder != "" && a.handler != nil {
|
||||
if err := a.handler.HandleSystemMessage(context.Background(), *sys); err != nil {
|
||||
a.logger.Error("处理系统消息失败", "err", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (a *Account) cancelDebouncedReply(chatID string) {
|
||||
@@ -119,7 +109,7 @@ func (a *Account) cleanupDedupLocked(now time.Time) {
|
||||
}
|
||||
}
|
||||
if len(a.processed) > ProcessedIDsMaxSize {
|
||||
// 删最旧一半。
|
||||
// 仍超上限:按时间升序排序后删最旧一半。
|
||||
type kv struct {
|
||||
id string
|
||||
t time.Time
|
||||
@@ -128,14 +118,7 @@ func (a *Account) cleanupDedupLocked(now time.Time) {
|
||||
for id, t := range a.processed {
|
||||
all = append(all, kv{id, t})
|
||||
}
|
||||
// 简单选择:按时间升序,删前一半。
|
||||
for i := 0; i < len(all); i++ {
|
||||
for j := i + 1; j < len(all); j++ {
|
||||
if all[j].t.Before(all[i].t) {
|
||||
all[i], all[j] = all[j], all[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
sort.Slice(all, func(i, j int) bool { return all[i].t.Before(all[j].t) })
|
||||
remove := len(all) / 2
|
||||
for i := 0; i < remove; i++ {
|
||||
delete(a.processed, all[i].id)
|
||||
@@ -169,14 +152,6 @@ func (a *Account) scheduleDebouncedReply(chat ChatMessage) {
|
||||
lastMsg := cur.lastMsg
|
||||
a.debounceMu.Unlock()
|
||||
|
||||
if a.delivery != nil {
|
||||
if err := a.delivery.Handle(context.Background(), lastMsg); err != nil {
|
||||
a.logger.Error("处理自动发货失败", "err", err, "chat_id", chat.ChatID)
|
||||
}
|
||||
if IsAutoDeliveryTrigger(lastMsg.Text) {
|
||||
return
|
||||
}
|
||||
}
|
||||
if a.reply != nil {
|
||||
if err := a.reply.Handle(context.Background(), lastMsg); err != nil {
|
||||
a.logger.Error("处理自动回复失败", "err", err, "chat_id", chat.ChatID)
|
||||
@@ -264,18 +239,18 @@ func extractChatMessage(decrypted map[string]any, accountID, cookieStr string) *
|
||||
}
|
||||
}
|
||||
|
||||
// isNonUserChatNotice 判断闲鱼 IM 中不应进入自动回复/自动发货的系统提示或交易卡片。
|
||||
// isNonUserChatNotice 判断闲鱼 IM 中不应进入自动回复的系统提示或交易卡片。
|
||||
// 典型样本:
|
||||
// - contentType=14:“有蚂蚁森林能量可领”“不想宝贝被砍价?设置不砍价回复”“退款成功”
|
||||
// - contentType=26:交易卡片,如“我已拍下,待付款”“我发起了退款申请”
|
||||
// 例外:付款待发货卡片要进入自动发货,因此保留。
|
||||
// 付款待发货卡片已经在 handleMessage 前半段进入 automation.Center,这里不能再进入聊天回复链。
|
||||
func isNonUserChatNotice(m1, m10 map[string]any, reminder string) bool {
|
||||
contentType := messageContentType(m1, m10)
|
||||
switch contentType {
|
||||
case "14":
|
||||
return true
|
||||
case "26":
|
||||
return !IsAutoDeliveryTrigger(reminder)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -309,26 +284,6 @@ func messageContentType(m1, m10 map[string]any) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
// extractSystemMessage 提取订单状态等系统消息(message["3"])。
|
||||
func extractSystemMessage(decrypted map[string]any, accountID, cookieStr string) *SystemMessage {
|
||||
m3, ok := decrypted["3"].(map[string]any)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
red, _ := m3["redReminder"].(string)
|
||||
uid, _ := m3["userId"].(string)
|
||||
if red == "" && uid == "" {
|
||||
return nil
|
||||
}
|
||||
return &SystemMessage{
|
||||
AccountID: accountID,
|
||||
CookieStr: cookieStr,
|
||||
RedReminder: red,
|
||||
UserID: uid,
|
||||
Raw: decrypted,
|
||||
}
|
||||
}
|
||||
|
||||
// extractItemID 从 reminderUrl 中正则提取 itemId=xxx。
|
||||
func extractItemID(url string) string {
|
||||
const key = "itemId="
|
||||
@@ -429,10 +384,3 @@ func sleepCtx(ctx context.Context, d time.Duration) error {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func min(a, b int) int {
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
Reference in new issue
Block a user