48 lines
1.5 KiB
Go
48 lines
1.5 KiB
Go
package consumer
|
||
|
||
import (
|
||
"context"
|
||
|
||
"github.com/gogf/gf/v2/encoding/gjson"
|
||
"github.com/gogf/gf/v2/errors/gerror"
|
||
"github.com/gogf/gf/v2/frame/g"
|
||
"github.com/gogf/gf/v2/os/glog"
|
||
)
|
||
|
||
// recreateChatIfNeeded 检测并重建Chat配置
|
||
// 调用customerservice的recreateRAGFlow接口,由customerservice负责MongoDB操作
|
||
func recreateChatIfNeeded(ctx context.Context, tenantId, accountName, platform string) error {
|
||
glog.Infof(ctx, "开始自动重建Chat - accountName: %s, platform: %s, tenantId: %s", accountName, platform, tenantId)
|
||
|
||
// 1. 从配置获取customerservice地址(避免consul.GetInstanceAddr在Stream context中panic)
|
||
customerserviceAddr := g.Cfg().MustGet(ctx, "customerservice.address", "customerservice:3000").String()
|
||
|
||
// 2. 调用customerservice的recreateRAGFlow接口
|
||
url := "http://" + customerserviceAddr + "/customer/service/account/recreateRAGFlow"
|
||
reqBody := g.Map{
|
||
"accountName": accountName,
|
||
"platform": platform,
|
||
}
|
||
|
||
glog.Infof(ctx, "调用customerservice recreateRAGFlow接口 - URL: %s, 请求: %+v", url, reqBody)
|
||
|
||
client := g.Client()
|
||
resp, err := client.Post(ctx, url, reqBody)
|
||
if err != nil {
|
||
return gerror.Wrapf(err, "调用recreateRAGFlow接口失败")
|
||
}
|
||
defer resp.Close()
|
||
|
||
// 3. 解析响应
|
||
respBody := resp.ReadAllString()
|
||
result := gjson.New(respBody)
|
||
|
||
if result.Get("code").Int() != 0 {
|
||
errMsg := result.Get("message").String()
|
||
return gerror.Newf("recreateRAGFlow失败: %s", errMsg)
|
||
}
|
||
|
||
glog.Infof(ctx, "Chat自动重建完成 - accountName: %s", accountName)
|
||
return nil
|
||
}
|