From c2574b0f9c4c01aeb1037bc1bf0bfb8a2ef445a4 Mon Sep 17 00:00:00 2001 From: Cold <16419454+cold502@user.noreply.gitee.com> Date: Sun, 4 Jan 2026 14:13:14 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4common/config=E5=8C=85=20-=20?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E5=B7=B2=E8=BF=81=E7=A7=BB=E5=88=B0customers?= =?UTF-8?q?ervice?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/constants.go | 42 ------- config/directions.go | 236 ----------------------------------- config/welcome.go | 40 ------ config/welcome_messages.yaml | 53 -------- 4 files changed, 371 deletions(-) delete mode 100644 config/constants.go delete mode 100644 config/directions.go delete mode 100644 config/welcome.go delete mode 100644 config/welcome_messages.yaml diff --git a/config/constants.go b/config/constants.go deleted file mode 100644 index 8963e8d..0000000 --- a/config/constants.go +++ /dev/null @@ -1,42 +0,0 @@ -package config - -// ==================== 可配置常量 ==================== -// 修改以下值来调整系统行为 - -// -------------------- 追问配置 -------------------- - -// FollowUpDelay1 第一次追问延时(秒) -var FollowUpDelay1 = 30 - -// FollowUpDelay2 第二次追问延时(秒) -var FollowUpDelay2 = 60 - -// FollowUpDelay3 第三次追问延时(秒) -var FollowUpDelay3 = 180 - -// FollowUpContent1 第一次追问话术 -var FollowUpContent1 = "还有其他问题吗?" - -// FollowUpContent2 第二次追问话术 -var FollowUpContent2 = "如果需要帮助,随时告诉我~" - -// FollowUpContent3 第三次追问话术 -var FollowUpContent3 = "我一直在线,有问题随时找我~" - -// -------------------- 归档配置 -------------------- - -// ArchiveDelay 归档延时(秒),默认 1 小时 -var ArchiveDelay = 3600 - -// -------------------- 历史上下文配置 -------------------- - -// HistoryContextLimit 读取历史对话轮数(用于新 Session 上下文注入) -var HistoryContextLimit int64 = 5 - -// -------------------- Stream 消费配置 -------------------- - -// DefaultBatchSize 批量读取消息数量(削峰填谷) -var DefaultBatchSize int64 = 200 - -// DefaultBlockTimeout 阻塞超时时间(毫秒) -var DefaultBlockTimeout int64 = 2000 diff --git a/config/directions.go b/config/directions.go deleted file mode 100644 index e0e2c15..0000000 --- a/config/directions.go +++ /dev/null @@ -1,236 +0,0 @@ -// Package config 提供全局配置管理和Consul监听 -// -// 本包实现了基于Consul的配置热更新机制,所有服务导入common包即可自动获得配置监听能力 -package config - -import ( - "context" - "encoding/json" - "sync" - "time" - - "github.com/gogf/gf/v2/frame/g" - "github.com/gogf/gf/v2/os/glog" - "github.com/hashicorp/consul/api" -) - -// Direction 咨询方向配置 -type Direction struct { - Name string `json:"name"` // 方向名称(如:气血、减肥) - ChatId string `json:"chat_id"` // RAGFlow对话ID -} - -var ( - directionsCache []Direction // 本地缓存(内存读取,超快) - cacheMu sync.RWMutex // 读写锁(支持多goroutine并发读) - startOnce sync.Once // 确保只启动一次监听 - consulClient *api.Client // Consul客户端(复用连接) -) - -// init 包初始化函数(所有服务导入common包时自动执行) -// -// Fallback顺序:Consul → config.yml -func init() { - ctx := context.Background() - - // 检查Consul是否配置 - consulAddr := g.Cfg().MustGet(ctx, "consul.address").String() - if consulAddr == "" { - glog.Warning(ctx, "Consul未配置,使用本地配置") - loadDirectionsFromLocal(ctx) - return - } - - // 初始化Consul客户端 - config := api.DefaultConfig() - config.Address = consulAddr - client, err := api.NewClient(config) - if err != nil { - glog.Errorf(ctx, "Consul客户端初始化失败: %v,fallback到本地配置", err) - loadDirectionsFromLocal(ctx) - return - } - consulClient = client - - // 启动后台监听(单例,确保只启动一次) - startOnce.Do(func() { - go startConsulWatcher(ctx) - glog.Info(ctx, "Consul配置监听已启动") - }) -} - -// GetDirections 获取咨询方向配置(从内存缓存读取) -// -// 返回: -// -// []Direction: 方向列表 -// -// 特点: -// - 高性能:读内存缓存,无网络IO -// - 线程安全:使用读锁,支持并发读取 -// - 自动更新:后台监听Consul,配置变化时自动更新缓存 -// -// 使用示例: -// -// dirs := config.GetDirections() -// for _, dir := range dirs { -// fmt.Printf("%s -> %s\n", dir.Name, dir.ChatId) -// } -func GetDirections() []Direction { - cacheMu.RLock() - defer cacheMu.RUnlock() - - // 返回副本,避免外部修改缓存 - result := make([]Direction, len(directionsCache)) - copy(result, directionsCache) - return result -} - -// GetDirectionChatId 根据方向名称获取对应的ChatId -// -// 参数: -// -// name: 方向名称(如:"气血"、"减肥") -// -// 返回: -// -// chatId: 对应的RAGFlow对话ID,未找到返回空字符串 -// -// 使用示例: -// -// chatId := config.GetDirectionChatId("气血") -func GetDirectionChatId(name string) string { - cacheMu.RLock() - defer cacheMu.RUnlock() - - for _, dir := range directionsCache { - if dir.Name == name { - return dir.ChatId - } - } - return "" -} - -// startConsulWatcher 后台监听Consul配置变化(Blocking Query长连接) -// -// 工作原理: -// 1. 使用Consul Blocking Query API(长连接,只在变化时返回) -// 2. 收到变化通知后更新本地缓存 -// 3. 自动重连(网络异常时自动恢复) -// -// 资源消耗: -// - 一个长连接(保持5分钟) -// - 配置未变化时几乎不占用CPU和网络 -// - 对比轮询:节省99%资源 -// -// 注意: -// - 此函数在后台goroutine中运行 -// - 使用Blocking Query避免轮询 -func startConsulWatcher(ctx context.Context) { - const consulKey = "ragflow/directions" - kv := consulClient.KV() - var lastIndex uint64 - - // 初始化时先读取一次配置 - if err := loadDirectionsFromConsul(ctx, kv); err != nil { - glog.Warningf(ctx, "初始化加载Consul配置失败: %v", err) - } - - // 持续监听配置变化 - for { - // Consul Blocking Query(长连接模式) - // WaitIndex: 指定版本号,只在配置变化时返回 - // WaitTime: 最长等待时间(超时后返回,客户端重新请求) - pair, meta, err := kv.Get(consulKey, &api.QueryOptions{ - WaitIndex: lastIndex, - WaitTime: 5 * time.Minute, - }) - - if err != nil { - glog.Errorf(ctx, "Consul查询失败: %v", err) - time.Sleep(5 * time.Second) // 错误时等待5秒重试 - continue - } - - // 配置版本号变化,说明有更新 - if meta.LastIndex != lastIndex { - lastIndex = meta.LastIndex - - // 配置被删除 - if pair == nil { - glog.Warning(ctx, "Consul配置已删除: "+consulKey) - cacheMu.Lock() - directionsCache = []Direction{} - cacheMu.Unlock() - continue - } - - // 解析并更新缓存 - var dirs []Direction - if err := json.Unmarshal(pair.Value, &dirs); err != nil { - glog.Errorf(ctx, "解析Consul配置失败: %v", err) - continue - } - - cacheMu.Lock() - directionsCache = dirs - cacheMu.Unlock() - - glog.Infof(ctx, "Consul配置已更新: %d个方向", len(dirs)) - } - } -} - -// loadDirectionsFromConsul 从Consul加载配置(初始化时调用) -func loadDirectionsFromConsul(ctx context.Context, kv *api.KV) error { - const consulKey = "ragflow/directions" - - pair, _, err := kv.Get(consulKey, nil) - if err != nil { - // Consul查询失败,fallback到本地配置 - glog.Warningf(ctx, "Consul查询失败: %v,fallback到本地配置", err) - loadDirectionsFromLocal(ctx) - return err - } - - if pair == nil { - glog.Warning(ctx, "Consul中未找到配置: "+consulKey+",fallback到本地配置") - loadDirectionsFromLocal(ctx) - return nil - } - - var dirs []Direction - if err := json.Unmarshal(pair.Value, &dirs); err != nil { - glog.Errorf(ctx, "解析Consul配置失败: %v,fallback到本地配置", err) - loadDirectionsFromLocal(ctx) - return err - } - - cacheMu.Lock() - directionsCache = dirs - cacheMu.Unlock() - - glog.Infof(ctx, "已加载Consul配置: %d个方向", len(dirs)) - return nil -} - -// loadDirectionsFromLocal 从本地config.yml加载配置(fallback机制) -func loadDirectionsFromLocal(ctx context.Context) { - directionsConfig := g.Cfg().MustGet(ctx, "ragflow.directions") - if directionsConfig.IsEmpty() { - glog.Warning(ctx, "本地配置中也未找到 ragflow.directions") - return - } - - var dirs []Direction - if err := directionsConfig.Scan(&dirs); err != nil { - glog.Errorf(ctx, "解析本地配置失败: %v", err) - return - } - - cacheMu.Lock() - directionsCache = dirs - cacheMu.Unlock() - - glog.Infof(ctx, "已加载config.yml配置: %d个方向", len(dirs)) -} diff --git a/config/welcome.go b/config/welcome.go deleted file mode 100644 index 4e4bb76..0000000 --- a/config/welcome.go +++ /dev/null @@ -1,40 +0,0 @@ -package config - -import ( - "context" - "sync" - - "github.com/gogf/gf/v2/frame/g" - "github.com/gogf/gf/v2/os/glog" -) - -var ( - welcomeCache map[string]string - welcomeMu sync.RWMutex - welcomeOnce sync.Once -) - -// initWelcomeMessages 初始化欢迎话术配置 -func initWelcomeMessages(ctx context.Context) { - welcomeOnce.Do(func() { - // 从默认配置文件(config.yml)读取 welcomes 配置 - welcomeMap := g.Cfg().MustGet(ctx, "welcomes").MapStrStr() - - welcomeMu.Lock() - welcomeCache = welcomeMap - welcomeMu.Unlock() - - glog.Infof(ctx, "已加载欢迎话术配置: %d个方向", len(welcomeMap)) - }) -} - -// GetWelcomeMessage 根据方向名称获取欢迎话术 -func GetWelcomeMessage(direction string) string { - ctx := context.Background() - initWelcomeMessages(ctx) - - welcomeMu.RLock() - defer welcomeMu.RUnlock() - - return welcomeCache[direction] -} diff --git a/config/welcome_messages.yaml b/config/welcome_messages.yaml deleted file mode 100644 index 0286482..0000000 --- a/config/welcome_messages.yaml +++ /dev/null @@ -1,53 +0,0 @@ -# 各咨询方向的欢迎话术配置 -# 当用户在状态5选择方向后,会自动发送对应的欢迎语 - -welcomes: - 乳腺贴: | - 🙋‍♀️ 欢迎姐妹们来到药济堂!我们是一个拥有10年经验的大健康专业团队🏅,专注于保守调理乳腺问题,已经成功帮助超过1万位姐妹轻松调理乳腺健康❤️。 - 如果您有结节或增生的困扰,欢迎随时咨询! - 请回复下面的数字,让我帮助您分析结节情况: - - 结节 - 增生 - 点击👇获取更快速的服务! - - 肝病: | - 你好,我是黄医生,有什么肝脏方面的问题我可以帮助您吗?无论是肝病、乙肝、丙肝,还是肝硬化腹水、脂肪肝、酒精肝,我都会为您提供专业的建议。😷 - 肝病的类型很多,常见症状有乏力、食欲减退和肝区不适等。为了更好地帮您,我需要了解一些详细的信息,比如: - - 您现在有腹胀或腹水的情况吗? - 是第一次出现还是反复出现呢? - 目前是早期还是中晚期呢? - 有没有病毒性肝炎的病史呢? - 请您留一下联系方式,我可以发送您的报告,并给您详细解读肝病治疗方案和成功案例。📋 - - 车膜: | - 🎉亲爱的车主,欢迎来到6膜王! - 🚗 我们在车膜行业深耕十年,拥有2家千平米的门店和30+专业团队,致力于为您提供最优质的服务! - ✨ 无论是隐形车衣还是改色膜,我们都能精准适配您的需求。留下您的【车型➕VX】,我们的资深顾问会立即为您匹配专属方案、膜材讲解以及报价!❤️ - - 毛孔: | - 啊啊~亲爱的姐妹们,最近有很多小伙伴在问我关于毛孔的问题!我之前也是毛孔大到妆容卡粉,真的是烦恼不断。😩 但在经过一段时间的摸索后,我终于找到了合适的方法,效果真的很好,差不多一个月就改善了许多!(亲测有效!)💖 - 如果需要帮助,可以随时告诉我,我非常乐意分享我的经验给你们哦! - - 免税店: | - Hi,长春的宝子们~💕欢迎锁定小红提免税集合店! - 这里有你想要的一切:美妆、香水、包包、首饰和大牌护肤品,正品保真不踩雷,价格特别美丽哦✨ - 全城顺丰包邮,购物更方便!期待你们来逛快来发现更多惊喜吧!💖 - - 门店地址:长春市绿园区皓月大路吾悦广场1楼 1036号,等你来哦!🌟 - - 减肥: | - 你好呀,姐妹~你也有肉肉的困扰吗?我现在已经掉了二十多斤了,至今都没有反弹! - 想要方法的话可以直接回复"1",我分享给你哦~ - (💗未成年发育期、哺乳期的姐妹我就不推荐啦) - - 气血: | - 亲爱的,欢迎光临!🌸 - 如果你有月经不调或气血不足的问题,随时可以问我哦! - - 停经闭经 - 痛经难忍 - 量少 - 经期不准 - 💗只需回复数字,我们会为你提供专业建议! 🌟如需更多帮助,点击下方咨询专业老师,我们一起寻求解决方案~