Compare commits

..

No commits in common. "master" and "v2" have entirely different histories.
master ... v2

30 changed files with 342 additions and 1894 deletions

View File

@ -55,16 +55,6 @@ tools:
enabled: true
api_key: "dingsbbntrkeiyazcfdg"
api_secret: "ObqxwyR20r9rVNhju0sCPQyQA98_FZSc32W4vgxnGFH_b02HZr1BPCJsOAF816nu"
zltxOrderAfterSaleSupplier:
enabled: true
base_url: "https://revcl.1688sup.com/api/admin/afterSales/directs"
zltxOrderAfterSaleReseller:
enabled: true
base_url: "https://revcl.1688sup.com/api/admin/afterSales/reseller_pre_ai"
zltxOrderAfterSaleResellerBatch:
enabled: true
base_url: "https://revcl.1688sup.com/api/admin/afterSales/reseller_pre_ai"
default_prompt:
img_recognize:
@ -74,8 +64,4 @@ default_prompt:
提取出图片中对用户可能有用的关键信息(例如金额、日期、标题、编号、联系信息、商品名称等)。
若图片为文档类(如合同、发票、收据),请结构化输出关键字段(如客户名称、金额、开票日期等)。
'
user_prompt: '识别图片内容'
# 权限配置
permissionConfig:
permission_url: "https://api.user.1688sup.com/v1/menu/myCodes?systemId="
user_prompt: '识别图片内容'

View File

@ -7,7 +7,7 @@ ollama:
base_url: "http://127.0.0.1:11434"
model: "qwen3-coder:480b-cloud"
generate_model: "qwen3-coder:480b-cloud"
vl_model: "qwen2.5vl:7b"
vl_model: "qwen2.5vl:3b"
timeout: "120s"
level: "info"
format: "json"
@ -36,16 +36,16 @@ db:
tools:
zltxOrderDetail:
enabled: true
base_url: "https://gateway.dev.cdlsxd.cn/zltx_api/admin/direct/ai/%s"
add_url: "https://gateway.dev.cdlsxd.cn/zltx_api/admin/direct/log/%s/%s"
base_url: "https://revcl.1688sup.com/api/admin/direct/ai/%s"
add_url: "https://revcl.1688sup.com/api/admin/direct/log/%s/%s"
api_key: "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJ1c2VyQ2VudGVyIiwiZXhwIjoxNzU4MDkxOTU4LCJuYmYiOjE3NTgwOTAxNTgsImp0aSI6IjEiLCJQaG9uZSI6IjE4MDAwMDAwMDAwIiwiVXNlck5hbWUiOiJsc3hkIiwiUmVhbE5hbWUiOiLotoXnuqfnrqHnkIblkZgiLCJBY2NvdW50VHlwZSI6MSwiR3JvdXBDb2RlcyI6IlZDTF9DQVNISUVSLFZDTF9PUEVSQVRFLFZDTF9BRE1JTixWQ0xfQUFBLFZDTF9WQ0xfT1BFUkFULFZDTF9JTlZPSUNFLENSTV9BRE1JTixMSUFOTElBTl9BRE1JTixNQVJLRVRNQUcyX0FETUlOLFBIT05FQklMTF9BRE1JTixRSUFOWkhVX1NVUFBFUl9BRE0sTUFSS0VUSU5HU0FBU19TVVBFUkFETUlOLENBUkRfQ09ERSxDQVJEX1BST0NVUkVNRU5ULE1BUktFVElOR1NZU1RFTV9TVVBFUixTVEFUSVNUSUNBTFNZU1RFTV9BRE1JTixaTFRYX0FETUlOLFpMVFhfT1BFUkFURSIsIkRpbmdVc2VySWQiOiIxNjIwMjYxMjMwMjg5MzM4MzQifQ.Bjsx9f8yfcrV9EWxb0n6POwnXVOq9XPRD78JFZnnf1_VAVMN78W4W570SZL27PWuDnkD7E4oUg6RzeZwZgl7BZrNpNr-a-QpNC5qCptqrqXeNfVStmX7pxWA8GqnzI8ybkZgbhQ58Gje7DzdJtBq_8zte_LDaYhTYXdIc5EAG0AbCzAk22nPTl47nkMeHtmisXQVLEsdibl1hW3ViFJlXwfXvUrOENItmL1_mRYkggUB0MaTu2nHJOYM6PaOVGLHx-74eepnmK2rm6konFEb6ed-Ukc6gVR-nM9yWZaYLYNGNKJLwZoCX3tRuerq74n4kzQgWmUEJeaVI1yIGSw1zw"
zltxProduct:
enabled: true
base_url: "https://gateway.dev.cdlsxd.cn/zltx_api/admin/oursProduct"
add_url: "https://gateway.dev.cdlsxd.cn/zltx_api/admin/platformProduct/getProductsByOfficialProductId"
base_url: "https://revcl.1688sup.com/api/admin/oursProduct"
add_url: "https://revcl.1688sup.com/api/admin/platformProduct/getProductsByOfficialProductId"
api_key: "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJ1c2VyQ2VudGVyIiwiZXhwIjoxNzU2MTgyNTM1LCJuYmYiOjE3NTYxODA3MzUsImp0aSI6IjEiLCJQaG9uZSI6IjE4MDAwMDAwMDAwIiwiVXNlck5hbWUiOiJsc3hkIiwiUmVhbE5hbWUiOiLotoXnuqfnrqHnkIblkZgiLCJBY2NvdW50VHlwZSI6MSwiR3JvdXBDb2RlcyI6IlZDTF9DQVNISUVSLFZDTF9PUEVSQVRFLFZDTF9BRE1JTixWQ0xfQUFBLFZDTF9WQ0xfT1BFUkFULFZDTF9JTlZPSUNFLENSTV9BRE1JTixMSUFOTElBTl9BRE1JTixNQVJLRVRNQUcyX0FETUlOLFBIT05FQklMTF9BRE1JTixRSUFOWkhVX1NVUFBFUl9BRE0sTUFSS0VUSU5HU0FBU19TVVBFUkFETUlOLENBUkRfQ09ERSxDQVJEX1BST0NVUkVNRU5ULE1BUktFVElOR1NZU1RFTV9TVVBFUixTVEFUSVNUSUNBTFNZU1RFTV9BRE1JTixaTFRYX0FETUlOLFpMVFhfT1BFUkFURSIsIkRpbmdVc2VySWQiOiIxNjIwMjYxMjMwMjg5MzM4MzQifQ.N1xv1PYbcO8_jR5adaczc16YzGsr4z101gwEZdulkRaREBJNYTOnFrvRxTFx3RJTooXsqTqroE1MR84v_1WPX6BS6kKonA-kC1Jgot6yrt5rFWhGNGb2Cpr9rKIFCCQYmiGd3AUgDazEeaQ0_sodv3E-EXg9VfE1SX8nMcck9Yjnc8NCy7RTWaBIaSeOdZcEl-JfCD0S6GSx3oErp_hk-U9FKGwf60wAuDGTY1R0BP4BYpcEqS-C2LSnsSGyURi54Cuk5xH8r1WuF0Dm5bwAj5d7Hvs77-N_sUF-C5ONqyZJRAEhYLgcmN9RX_WQZfizdQJxizlTczdpzYfy-v-1eQ"
zltxOrderStatistics:
base_url: "https://gateway.dev.cdlsxd.cn/zltx_api/admin/direct/ai/search/"
base_url: "https://revcl.1688sup.com/api/admin/direct/ai/search/"
enabled: true
api_key: "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJ1c2VyQ2VudGVyIiwiZXhwIjoxNzU2MTgyNTM1LCJuYmYiOjE3NTYxODA3MzUsImp0aSI6IjEiLCJQaG9uZSI6IjE4MDAwMDAwMDAwIiwiVXNlck5hbWUiOiJsc3hkIiwiUmVhbE5hbWUiOiLotoXnuqfnrqHnkIblkZgiLCJBY2NvdW50VHlwZSI6MSwiR3JvdXBDb2RlcyI6IlZDTF9DQVNISUVSLFZDTF9PUEVSQVRFLFZDTF9BRE1JTixWQ0xfQUFBLFZDTF9WQ0xfT1BFUkFULFZDTF9JTlZPSUNFLENSTV9BRE1JTixMSUFOTElBTl9BRE1JTixNQVJLRVRNQUcyX0FETUlOLFBIT05FQklMTF9BRE1JTixRSUFOWkhVX1NVUFBFUl9BRE0sTUFSS0VUSU5HU0FBU19TVVBFUkFETUlOLENBUkRfQ09ERSxDQVJEX1BST0NVUkVNRU5ULE1BUktFVElOR1NZU1RFTV9TVVBFUixTVEFUSVNUSUNBTFNZU1RFTV9BRE1JTixaTFRYX0FETUlOLFpMVFhfT1BFUkFURSIsIkRpbmdVc2VySWQiOiIxNjIwMjYxMjMwMjg5MzM4MzQifQ.N1xv1PYbcO8_jR5adaczc16YzGsr4z101gwEZdulkRaREBJNYTOnFrvRxTFx3RJTooXsqTqroE1MR84v_1WPX6BS6kKonA-kC1Jgot6yrt5rFWhGNGb2Cpr9rKIFCCQYmiGd3AUgDazEeaQ0_sodv3E-EXg9VfE1SX8nMcck9Yjnc8NCy7RTWaBIaSeOdZcEl-JfCD0S6GSx3oErp_hk-U9FKGwf60wAuDGTY1R0BP4BYpcEqS-C2LSnsSGyURi54Cuk5xH8r1WuF0Dm5bwAj5d7Hvs77-N_sUF-C5ONqyZJRAEhYLgcmN9RX_WQZfizdQJxizlTczdpzYfy-v-1eQ"
knowledge:
@ -55,17 +55,6 @@ tools:
enabled: true
api_key: "dingsbbntrkeiyazcfdg"
api_secret: "ObqxwyR20r9rVNhju0sCPQyQA98_FZSc32W4vgxnGFH_b02HZr1BPCJsOAF816nu"
zltxOrderAfterSaleSupplier:
enabled: true
base_url: "https://gateway.dev.cdlsxd.cn/zltx_api/admin/afterSales/directs"
zltxOrderAfterSaleReseller:
enabled: true
base_url: "https://gateway.dev.cdlsxd.cn/zltx_api/admin/afterSales/reseller_pre_ai"
zltxOrderAfterSaleResellerBatch:
enabled: true
base_url: "https://gateway.dev.cdlsxd.cn/zltx_api/admin/afterSales/reseller_pre_ai"
default_prompt:
img_recognize:
@ -75,7 +64,4 @@ default_prompt:
提取出图片中对用户可能有用的关键信息(例如金额、日期、标题、编号、联系信息、商品名称等)。
若图片为文档类(如合同、发票、收据),请结构化输出关键字段(如客户名称、金额、开票日期等)。
'
user_prompt: '识别图片内容'
# 权限配置
permissionConfig:
permission_url: "http://api.test.user.1688sup.cn:8001/v1/menu/myCodes?systemId="
user_prompt: '识别图片内容'

View File

@ -16,7 +16,7 @@ CONFIG_FILE="config/config.yaml"
BRANCH="master"
if [ "$MODE" = "dev" ]; then
CONFIG_FILE="config/config_test.yaml"
BRANCH="test"
BRANCH="v2"
fi
git fetch origin

View File

@ -6,13 +6,10 @@ import (
"ai_scheduler/internal/data/impl"
"ai_scheduler/internal/data/model"
"ai_scheduler/internal/entitys"
"ai_scheduler/internal/gateway"
"ai_scheduler/internal/pkg"
"ai_scheduler/tmpl/dataTemp"
"context"
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
"time"
@ -25,7 +22,7 @@ import (
)
type Do struct {
//Ctx *entitys.RequireData
Ctx *entitys.RequireData
sessionImpl *impl.SessionImpl
sysImpl *impl.SysImpl
taskImpl *impl.TaskImpl
@ -47,125 +44,78 @@ func NewDo(
}
}
func (d *Do) DataAuth(ctx context.Context, client *gateway.Client, requireData *entitys.RequireData) (err error) {
// 1. 验证客户端数据
if err = d.validateClientData(client, requireData); err != nil {
return err
func (d *Do) InitCtx(req *entitys.ChatSockRequest) *Do {
d.Ctx = &entitys.RequireData{
Req: req,
}
// 2. 加载系统信息
if err = d.loadSystemInfo(ctx, client, requireData); err != nil {
return fmt.Errorf("获取系统信息失败: %w", err)
}
// 3. 加载任务列表
if err = d.loadTaskList(ctx, client, requireData); err != nil {
return fmt.Errorf("获取任务列表失败: %w", err)
}
// 4. 加载聊天历史
if err = d.loadChatHistory(ctx, requireData); err != nil {
return fmt.Errorf("获取历史记录失败: %w", err)
}
// 5. 加载图片数据
if err = d.getImgData(requireData); err != nil {
return err
}
// 6. 加载用户权限
if _, err = d.LoadUserPermission(client, requireData); err != nil {
return fmt.Errorf("获取用户权限失败: %w", err)
}
return nil
return d
}
// 提取数据验证为单独函数
func (d *Do) validateClientData(client *gateway.Client, requireData *entitys.RequireData) error {
requireData.Session = client.GetSession()
if len(requireData.Session) == 0 {
return errors.SessionNotFound
func (d *Do) DataAuth(c *websocket.Conn) (err error) {
d.Ctx.Session = c.Query("x-session", "")
if len(d.Ctx.Session) == 0 {
err = errors.SessionNotFound
return
}
d.Ctx.Auth = c.Query("x-authorization", "")
if len(d.Ctx.Auth) == 0 {
err = errors.AuthNotFound
return
}
d.Ctx.Key = c.Query("x-app-key", "")
if len(d.Ctx.Key) == 0 {
err = errors.KeyNotFound
return
}
requireData.Auth = client.GetAuth()
if len(requireData.Auth) == 0 {
return errors.AuthNotFound
}
requireData.Key = client.GetKey()
if len(requireData.Key) == 0 {
return errors.KeyNotFound
}
return nil
}
// 获取系统信息的辅助函数
func (d *Do) loadSystemInfo(ctx context.Context, client *gateway.Client, requireData *entitys.RequireData) error {
if sysInfo := client.GetSysInfo(); sysInfo == nil {
sys, err := d.getSysInfo(requireData)
if err != nil {
return err
}
client.SetSysInfo(&sys)
requireData.Sys = sys
} else {
requireData.Sys = *sysInfo
}
return nil
}
// 获取任务列表的辅助函数
func (d *Do) loadTaskList(ctx context.Context, client *gateway.Client, requireData *entitys.RequireData) error {
if taskInfo := client.GetTasks(); len(taskInfo) == 0 {
tasks, err := d.getTasks(requireData.Sys.SysID)
if err != nil {
return err
}
requireData.Tasks = tasks
client.SetTasks(tasks)
} else {
requireData.Tasks = taskInfo
}
return nil
}
// 获取历史记录的辅助函数
func (d *Do) loadChatHistory(ctx context.Context, requireData *entitys.RequireData) error {
histories, err := d.getSessionChatHis(requireData)
d.Ctx.Sys, err = d.getSysInfo()
if err != nil {
return err
err = errors.SysErr("获取系统信息失败:%v", err.Error())
return
}
requireData.Histories = histories
return nil
d.Ctx.Histories, err = d.getSessionChatHis()
if err != nil {
err = errors.SysErr("获取历史记录失败:%v", err.Error())
return
}
d.Ctx.Tasks, err = d.getTasks(d.Ctx.Sys.SysID)
if err != nil {
err = errors.SysErr("获取任务列表失败:%v", err.Error())
return
}
if err = d.getImgData(); err != nil {
return
}
return
}
func (d *Do) MakeCh(c *websocket.Conn, requireData *entitys.RequireData) (ctx context.Context, deferFunc func()) {
requireData.Ch = make(chan entitys.Response)
func (d *Do) MakeCh(c *websocket.Conn) (ctx context.Context, deferFunc func()) {
d.Ctx.Ch = make(chan entitys.Response)
ctx, cancel := context.WithCancel(context.Background())
done := d.startMessageHandler(ctx, c, requireData)
done := d.startMessageHandler(ctx, c)
return ctx, func() {
close(requireData.Ch) //关闭主通道
<-done // 等待消息处理完成
close(d.Ctx.Ch) //关闭主通道
<-done // 等待消息处理完成
cancel()
}
}
func (d *Do) getImgData(requireData *entitys.RequireData) (err error) {
if len(requireData.Req.Img) == 0 {
func (d *Do) getImgData() (err error) {
if len(d.Ctx.Req.Img) == 0 {
return
}
imgs := strings.Split(requireData.Req.Img, ",")
imgs := strings.Split(d.Ctx.Req.Img, ",")
if len(imgs) == 0 {
return
}
for k, img := range imgs {
baseErr := "获取第" + strconv.Itoa(k+1) + "张图片失败:"
entitys.ResLog(requireData.Ch, "img_get_start", "正在获取第"+strconv.Itoa(k+1)+"张图片")
entitys.ResLog(d.Ctx.Ch, "img_get_start", "正在获取第"+strconv.Itoa(k+1)+"张图片")
if err = pkg.ValidateImageURL(img); err != nil {
entitys.ResLog(requireData.Ch, "", baseErr+"expected image content")
entitys.ResLog(d.Ctx.Ch, "", baseErr+"expected image content")
continue
}
req := l_request.Request{
@ -178,20 +128,20 @@ func (d *Do) getImgData(requireData *entitys.RequireData) (err error) {
}
res, _err := req.Send()
if _err != nil {
entitys.ResLog(requireData.Ch, "", baseErr+_err.Error())
entitys.ResLog(d.Ctx.Ch, "", baseErr+_err.Error())
continue
}
if _, ex := res.Headers["Content-Type"]; !ex {
entitys.ResLog(requireData.Ch, "", baseErr+"Content-Type不存在")
entitys.ResLog(d.Ctx.Ch, "", baseErr+"Content-Type不存在")
continue
}
if !strings.HasPrefix(res.Headers["Content-Type"], "image/") {
entitys.ResLog(requireData.Ch, "", baseErr+"expected image content")
entitys.ResLog(d.Ctx.Ch, "", baseErr+"expected image content")
continue
}
requireData.ImgByte = append(requireData.ImgByte, res.Content)
requireData.ImgUrls = append(requireData.ImgUrls, img)
entitys.ResLog(requireData.Ch, "img_get_end", "第"+strconv.Itoa(k+1)+"张图片获取成功")
d.Ctx.ImgByte = append(d.Ctx.ImgByte, res.Content)
d.Ctx.ImgUrls = append(d.Ctx.ImgUrls, img)
entitys.ResLog(d.Ctx.Ch, "img_get_end", "第"+strconv.Itoa(k+1)+"张图片获取成功")
}
return
@ -202,19 +152,19 @@ func (d *Do) getRequireData() (err error) {
return
}
func (d *Do) getSysInfo(requireData *entitys.RequireData) (sysInfo model.AiSy, err error) {
func (d *Do) getSysInfo() (sysInfo model.AiSy, err error) {
cond := builder.NewCond()
cond = cond.And(builder.Eq{"app_key": requireData.Key})
cond = cond.And(builder.Eq{"app_key": d.Ctx.Key})
cond = cond.And(builder.IsNull{"delete_at"})
cond = cond.And(builder.Eq{"status": 1})
err = d.sysImpl.GetOneBySearchToStrut(&cond, &sysInfo)
return
}
func (d *Do) getSessionChatHis(requireData *entitys.RequireData) (his []model.AiChatHi, err error) {
func (d *Do) getSessionChatHis() (his []model.AiChatHi, err error) {
cond := builder.NewCond()
cond = cond.And(builder.Eq{"session_id": requireData.Session})
cond = cond.And(builder.Eq{"session_id": d.Ctx.Session})
_, err = d.hisImpl.GetListToStruct(&cond, &dataTemp.ReqPageBo{Limit: d.conf.Sys.SessionLen}, &his, "his_id desc")
@ -236,7 +186,7 @@ func (d *Do) getTasks(sysId int32) (tasks []model.AiTask, err error) {
func (d *Do) startMessageHandler(
ctx context.Context,
c *websocket.Conn,
requireData *entitys.RequireData,
) <-chan struct{} {
done := make(chan struct{})
var chat []string
@ -250,10 +200,10 @@ func (d *Do) startMessageHandler(
)
if len(chat) > 0 {
AiRes := &model.AiChatHi{
SessionID: requireData.Session,
Ques: requireData.Req.Text,
SessionID: d.Ctx.Session,
Ques: d.Ctx.Req.Text,
Ans: strings.Join(chat, ""),
Files: requireData.Req.Img,
Files: d.Ctx.Req.Img,
}
d.hisImpl.AddWithData(AiRes)
hisLog.HisId = AiRes.HisID
@ -266,7 +216,7 @@ func (d *Do) startMessageHandler(
}()
for v := range requireData.Ch { // 自动检测通道关闭
for v := range d.Ctx.Ch { // 自动检测通道关闭
if err := sendWithTimeout(c, v, 2*time.Second); err != nil {
log.Errorf("Send error: %v", err)
return
@ -305,51 +255,3 @@ func sendWithTimeout(c *websocket.Conn, data entitys.Response, timeout time.Dura
return sendCtx.Err()
}
}
// 从统一登录平台获取用户权限
func (d *Do) LoadUserPermission(client *gateway.Client, requireData *entitys.RequireData) (codes []string, err error) {
if len(client.GetCodes()) > 0 {
return client.GetCodes(), nil
}
var (
request l_request.Request
)
// 构建请求URL
request.Url = d.conf.PermissionConfig.PermissionURL + strconv.Itoa(int(requireData.Sys.SysID))
request.Method = "GET"
request.Headers = map[string]string{
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36",
"Accept": "application/json, text/plain, */*",
"Authorization": "Bearer " + client.GetAuth(),
}
// 发送请求
res, err := request.Send()
if err != nil {
return
}
// 检查响应状态码
if res.StatusCode != http.StatusOK {
err = errors.SysErr("获取用户权限失败")
return
}
type resp struct {
Codes []string `json:"codes"`
}
// 解析响应体
var respBody resp
err = json.Unmarshal([]byte(res.Text), &respBody)
if err != nil {
return
}
// 设置客户端权限
client.SetCodes(respBody.Codes)
return respBody.Codes, nil
}

View File

@ -8,7 +8,6 @@ import (
"ai_scheduler/internal/data/impl"
"ai_scheduler/internal/data/model"
"ai_scheduler/internal/entitys"
"ai_scheduler/internal/gateway"
"ai_scheduler/internal/pkg"
"ai_scheduler/internal/pkg/l_request"
"ai_scheduler/internal/pkg/mapstructure"
@ -17,7 +16,6 @@ import (
"context"
"encoding/json"
"fmt"
"gorm.io/gorm/utils"
"strings"
)
@ -70,7 +68,7 @@ func (r *Handle) handleOtherTask(ctx context.Context, requireData *entitys.Requi
return
}
func (r *Handle) HandleMatch(ctx context.Context, client *gateway.Client, requireData *entitys.RequireData) (err error) {
func (r *Handle) HandleMatch(ctx context.Context, requireData *entitys.RequireData) (err error) {
if !requireData.Match.IsMatch {
if len(requireData.Match.Chat) != 0 {
@ -92,13 +90,6 @@ func (r *Handle) HandleMatch(ctx context.Context, client *gateway.Client, requir
if pointTask == nil || pointTask.Index == "other" {
return r.OtherTask(ctx, requireData)
}
// 校验用户权限
// if err = r.PermissionAuth(client, pointTask); err != nil {
// log.Errorf("权限验证失败: %s", err.Error())
// return
// }
switch constants.TaskType(pointTask.Type) {
case constants.TaskTypeApi:
return r.handleApiTask(ctx, requireData, pointTask)
@ -261,13 +252,3 @@ func (r *Handle) handleApiTask(ctx context.Context, requireData *entitys.Require
return
}
// 权限验证
func (r *Handle) PermissionAuth(client *gateway.Client, pointTask *model.AiTask) (err error) {
// 授权检查权限
if !utils.Contains(client.GetCodes(), pointTask.Index) {
return fmt.Errorf("用户权限不足: %s", pointTask.Name)
}
return nil
}

View File

@ -15,7 +15,7 @@ type LlmService interface {
// buildSystemPrompt 构建系统提示词
func buildSystemPrompt(prompt string) string {
if len(prompt) == 0 {
prompt = "[system] 你是一个智能路由系统,核心职责是 **精准解析用户意图并路由至对应任务模块**\n[rule]\n1.返回以下格式的JSON{ \"index\": \"工具索引index\", \"confidence\": 0.0-1.0,\"reasoning\": \"判断理由\"}\n2.严格返回字符串格式禁用markdown格式返回\n3.只返回json字符串不包含任何其他解释性文字\n4.当用户意图非常不清晰时使用,尝试进行追问具体希望查询内容,\n当前时间是" + time.Now().Format(time.DateTime)
prompt = "[system] 你是一个智能路由系统,核心职责是 **精准解析用户意图并路由至对应任务模块**\n[rule]\n1.返回以下格式的JSON{ \"index\": \"工具索引index\", \"confidence\": 0.0-1.0,\"reasoning\": \"判断理由\"}\n2.严格返回字符串格式禁用markdown格式返回\n3.只返回json字符串不包含任何其他解释性文字\n4.当用户意图非常不清晰时使用,尝试进行追问具体希望查询内容"
}
return prompt

View File

@ -2,11 +2,11 @@ package biz
import (
"ai_scheduler/internal/biz/do"
"ai_scheduler/internal/gateway"
"ai_scheduler/internal/entitys"
"github.com/gofiber/fiber/v2/log"
"github.com/gofiber/websocket/v2"
)
// AiRouterBiz 智能路由服务
@ -26,45 +26,28 @@ func NewAiRouterBiz(
}
}
// 路由处理WebSocket请求
//
// 参数:
// - client: 网关客户端
// - req: 聊天请求结构体
//
// 返回:
// - err: 错误信息
func (r *AiRouterBiz) RouteWithSocket(client *gateway.Client, req *entitys.ChatSockRequest) (err error) {
// 创建请求上下文数据
requireData := &entitys.RequireData{
Req: req,
}
// 获取WebSocket连接
conn := client.GetConn()
func (r *AiRouterBiz) RouteWithSocket(c *websocket.Conn, req *entitys.ChatSockRequest) (err error) {
//必要数据验证和获取
dos := r.do.InitCtx(req)
//初始化通道/上下文
ctx, clearFunc := r.do.MakeCh(conn, requireData)
defer func() {
if err != nil {
entitys.ResError(requireData.Ch, "", err.Error())
}
clearFunc()
}()
ctx, clearFunc := dos.MakeCh(c)
defer clearFunc()
//数据验证和收集
if err = r.do.DataAuth(ctx, client, requireData); err != nil {
if err = dos.DataAuth(c); err != nil {
log.Errorf("数据验证和收集失败: %s", err.Error())
return
}
//意图识别
if err = r.handle.Recognize(ctx, requireData); err != nil {
if err = r.handle.Recognize(ctx, dos.Ctx); err != nil {
log.Errorf("意图识别失败: %s", err.Error())
return
}
//向下传递
if err = r.handle.HandleMatch(ctx, client, requireData); err != nil {
if err = r.handle.HandleMatch(ctx, dos.Ctx); err != nil {
log.Errorf("任务处理失败: %s", err.Error())
return
}

View File

@ -1,122 +1,122 @@
package biz
// import (
// "ai_scheduler/internal/config"
// "ai_scheduler/internal/data/impl"
// "ai_scheduler/internal/data/model"
// "ai_scheduler/internal/entitys"
// "ai_scheduler/internal/pkg"
// "ai_scheduler/internal/pkg/utils_ollama"
// "ai_scheduler/internal/tools"
// "ai_scheduler/utils"
// "encoding/json"
// "flag"
// "fmt"
// "os"
// "path/filepath"
// "testing"
import (
"ai_scheduler/internal/config"
"ai_scheduler/internal/data/impl"
"ai_scheduler/internal/data/model"
"ai_scheduler/internal/entitys"
"ai_scheduler/internal/pkg"
"ai_scheduler/internal/pkg/utils_ollama"
"ai_scheduler/internal/tools"
"ai_scheduler/utils"
"encoding/json"
"flag"
"fmt"
"os"
"path/filepath"
"testing"
// "github.com/gofiber/fiber/v2/log"
// )
"github.com/gofiber/fiber/v2/log"
)
// func Test_task(t *testing.T) {
// var c entitys.TaskConfig
// config := `{"param": {"type": "object", "required": ["number"], "properties": {"number": {"type": "string", "description": "订单编号/流水号"}}}, "request": {"url": "http://www.baidu.com/${number}", "headers": {"Authorization": "${authorization}"}, "method": "GET"}}`
// err := json.Unmarshal([]byte(config), &c)
// t.Log(err)
// }
func Test_task(t *testing.T) {
var c entitys.TaskConfig
config := `{"param": {"type": "object", "required": ["number"], "properties": {"number": {"type": "string", "description": "订单编号/流水号"}}}, "request": {"url": "http://www.baidu.com/${number}", "headers": {"Authorization": "${authorization}"}, "method": "GET"}}`
err := json.Unmarshal([]byte(config), &c)
t.Log(err)
}
// type configData struct {
// Param map[string]interface{} `json:"param"`
// Do map[string]interface{} `json:"do"`
// }
type configData struct {
Param map[string]interface{} `json:"param"`
Do map[string]interface{} `json:"do"`
}
// func Test_Order(t *testing.T) {
// routerBiz := in()
// ch := make(chan entitys.Response, 5)
// defer close(ch)
// err := routerBiz.handleTask(ch, nil, &entitys.Match{Index: "order_diagnosis", Parameters: `{"order_number":"822895927188791297"}`}, &model.AiTask{Config: `{"tool": "zltxOrderDetail", "param": {"type": "object", "optional": [], "required": ["order_number"], "properties": {"order_number": {"type": "string", "description": "订单编号/流水号"}}}}`})
// select {
// case v := <-ch: // 尝试接收
// fmt.Println("接收到值:", v)
// default:
// fmt.Println("无数据可接收")
// }
// t.Log(err)
// }
func Test_Order(t *testing.T) {
routerBiz := in()
ch := make(chan entitys.Response, 5)
defer close(ch)
err := routerBiz.handleTask(ch, nil, &entitys.Match{Index: "order_diagnosis", Parameters: `{"order_number":"822895927188791297"}`}, &model.AiTask{Config: `{"tool": "zltxOrderDetail", "param": {"type": "object", "optional": [], "required": ["order_number"], "properties": {"order_number": {"type": "string", "description": "订单编号/流水号"}}}}`})
select {
case v := <-ch: // 尝试接收
fmt.Println("接收到值:", v)
default:
fmt.Println("无数据可接收")
}
t.Log(err)
}
// func Test_OrderLog(t *testing.T) {
// routerBiz := in()
// ch := make(chan entitys.Response, 5)
// defer close(ch)
// err := routerBiz.handleTask(ch, nil, &entitys.Match{Index: "order_diagnosis", Parameters: `{"order_number":"822979421673758721","serial_number":"822979421979938817"}`}, &model.AiTask{Config: `{"tool": "zltxOrderDirectLog", "param": {"type": "object", "optional": [], "required": ["order_number"], "properties": {"order_number": {"type": "string", "description": "订单编号/流水号"}}}}`})
// t.Log(err)
// }
func Test_OrderLog(t *testing.T) {
routerBiz := in()
ch := make(chan entitys.Response, 5)
defer close(ch)
err := routerBiz.handleTask(ch, nil, &entitys.Match{Index: "order_diagnosis", Parameters: `{"order_number":"822979421673758721","serial_number":"822979421979938817"}`}, &model.AiTask{Config: `{"tool": "zltxOrderDirectLog", "param": {"type": "object", "optional": [], "required": ["order_number"], "properties": {"order_number": {"type": "string", "description": "订单编号/流水号"}}}}`})
t.Log(err)
}
// func Test_ProductLog(t *testing.T) {
// routerBiz := in()
// ch := make(chan entitys.Response, 5)
// defer close(ch)
// err := routerBiz.handleTask(ch, nil, &entitys.Match{Index: "order_diagnosis", Parameters: `{"name":"利楚测试"}`}, &model.AiTask{Config: `{"tool": "zltxProduct", "param": {"type": "object", "optional": [], "required": ["order_number"], "properties": {"order_number": {"type": "string", "description": "订单编号/流水号"}}}}`})
// t.Log(err)
// }
func Test_ProductLog(t *testing.T) {
routerBiz := in()
ch := make(chan entitys.Response, 5)
defer close(ch)
err := routerBiz.handleTask(ch, nil, &entitys.Match{Index: "order_diagnosis", Parameters: `{"name":"利楚测试"}`}, &model.AiTask{Config: `{"tool": "zltxProduct", "param": {"type": "object", "optional": [], "required": ["order_number"], "properties": {"order_number": {"type": "string", "description": "订单编号/流水号"}}}}`})
t.Log(err)
}
// func Test_ZltxStatistics(t *testing.T) {
// routerBiz := in()
// ch := make(chan entitys.Response, 5)
// defer close(ch)
// err := routerBiz.handleTask(ch, nil, &entitys.Match{Index: "order_diagnosis", Parameters: `{"number":"13737882067"}`}, &model.AiTask{Config: `{"tool": "zltxOrderStatistics", "param": {"type": "object", "optional": [], "required": ["number"], "properties": {"number": {"type": "string", "description": "充值账号/分销商ID"}}}}`})
// t.Log(err)
// }
func Test_ZltxStatistics(t *testing.T) {
routerBiz := in()
ch := make(chan entitys.Response, 5)
defer close(ch)
err := routerBiz.handleTask(ch, nil, &entitys.Match{Index: "order_diagnosis", Parameters: `{"number":"13737882067"}`}, &model.AiTask{Config: `{"tool": "zltxOrderStatistics", "param": {"type": "object", "optional": [], "required": ["number"], "properties": {"number": {"type": "string", "description": "充值账号/分销商ID"}}}}`})
t.Log(err)
}
// func in() *AiRouterBiz {
func in() *AiRouterBiz {
// modDir, err := getModuleDir()
// if err != nil {
// panic("1")
// }
// configPath := flag.String("config", fmt.Sprintf("%s/config/config.yaml", modDir), "Path to configuration file")
// flag.Parse()
modDir, err := getModuleDir()
if err != nil {
panic("1")
}
configPath := flag.String("config", fmt.Sprintf("%s/config/config.yaml", modDir), "Path to configuration file")
flag.Parse()
// configConfig, err := config.LoadConfig(*configPath)
// if err != nil {
// panic("加载配置失败")
// }
// client, _, err := utils_ollama.NewClient(configConfig)
// allLogger := log.DefaultLogger()
// utilOllama := utils_ollama.NewUtilOllama(configConfig, allLogger)
// manager := tools.NewManager(configConfig, client)
configConfig, err := config.LoadConfig(*configPath)
if err != nil {
panic("加载配置失败")
}
client, _, err := utils_ollama.NewClient(configConfig)
allLogger := log.DefaultLogger()
utilOllama := utils_ollama.NewUtilOllama(configConfig, allLogger)
manager := tools.NewManager(configConfig, client)
// db, _ := utils.NewGormDb(configConfig)
// sessionImpl := impl.NewSessionImpl(db)
// sysImpl := impl.NewSysImpl(db)
// taskImpl := impl.NewTaskImpl(db)
// chatImpl := impl.NewChatImpl(db)
// safeChannelPool, _ := pkg.NewSafeChannelPool(configConfig)
// routerBiz := NewAiRouterBiz(manager, sessionImpl, sysImpl, taskImpl, chatImpl, configConfig, utilOllama, safeChannelPool, client)
db, _ := utils.NewGormDb(configConfig)
sessionImpl := impl.NewSessionImpl(db)
sysImpl := impl.NewSysImpl(db)
taskImpl := impl.NewTaskImpl(db)
chatImpl := impl.NewChatImpl(db)
safeChannelPool, _ := pkg.NewSafeChannelPool(configConfig)
routerBiz := NewAiRouterBiz(manager, sessionImpl, sysImpl, taskImpl, chatImpl, configConfig, utilOllama, safeChannelPool, client)
// return routerBiz
// }
return routerBiz
}
// func getModuleDir() (string, error) {
// dir, err := os.Getwd()
// if err != nil {
// return "", err
// }
func getModuleDir() (string, error) {
dir, err := os.Getwd()
if err != nil {
return "", err
}
// for {
// modPath := filepath.Join(dir, "go.mod")
// if _, err := os.Stat(modPath); err == nil {
// return dir, nil // 找到 go.mod
// }
for {
modPath := filepath.Join(dir, "go.mod")
if _, err := os.Stat(modPath); err == nil {
return dir, nil // 找到 go.mod
}
// // 向上查找父目录
// parent := filepath.Dir(dir)
// if parent == dir {
// break // 到达根目录,未找到
// }
// dir = parent
// }
// 向上查找父目录
parent := filepath.Dir(dir)
if parent == dir {
break // 到达根目录,未找到
}
dir = parent
}
// return "", fmt.Errorf("go.mod not found in current directory or parents")
// }
return "", fmt.Errorf("go.mod not found in current directory or parents")
}

View File

@ -1,18 +1,10 @@
package biz
import (
"ai_scheduler/internal/data/constants"
errors "ai_scheduler/internal/data/error"
"ai_scheduler/internal/data/impl"
"ai_scheduler/internal/data/model"
"ai_scheduler/internal/entitys"
"ai_scheduler/internal/pkg/l_request"
"context"
"encoding/json"
"net/http"
"strconv"
"gorm.io/gorm/utils"
"xorm.io/builder"
@ -32,63 +24,12 @@ func NewTaskBiz(conf *config.Config, taskRepo *impl.TaskImpl) *TaskBiz {
}
// taskList 功能列表
func (t *TaskBiz) TaskList(ctx context.Context, req *entitys.TaskRequest, auth string) (list []model.AiTask, err error) {
tasks := make([]model.AiTask, 0)
func (t *TaskBiz) TaskList(ctx context.Context, req *entitys.TaskRequest) (list []model.AiTask, err error) {
cond := builder.NewCond()
cond = cond.And(builder.Eq{"status": constants.Enable})
cond = cond.And(builder.Eq{"status": 1})
cond = cond.And(builder.Eq{"sys_id": req.SysId})
cond = cond.And(builder.Eq{"is_show": constants.IsSHOW})
err = t.taskRepo.GetRangeToMapStruct(&cond, &tasks)
codes, err := t.GetUserPermission(req, auth)
if err != nil {
return
}
// 检查用户是否有权限
for _, task := range tasks {
if utils.Contains(codes, task.Index) {
list = append(list, task)
}
}
err = t.taskRepo.GetRangeToMapStruct(&cond, &list)
return
}
// 从统一登录平台获取用户权限
func (t *TaskBiz) GetUserPermission(req *entitys.TaskRequest, auth string) (codes []string, err error) {
request := l_request.Request{
Method: "GET",
Url: t.conf.PermissionConfig.PermissionURL + strconv.Itoa(int(req.SysId)),
Headers: map[string]string{
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36",
"Accept": "application/json, text/plain, */*",
"Authorization": auth,
},
}
// 发送请求
res, err := request.Send()
if err != nil {
err = errors.SysErr("请求用户权限失败")
return
}
// 检查响应状态码
if res.StatusCode != http.StatusOK {
err = errors.SysErr("获取用户权限失败")
return
}
type resp struct {
Codes []string `json:"codes"`
}
// 解析响应体s
var respBody resp
err = json.Unmarshal([]byte(res.Text), &respBody)
if err != nil {
return
}
return respBody.Codes, nil
}

View File

@ -9,15 +9,14 @@ import (
// Config 应用配置
type Config struct {
Server ServerConfig `mapstructure:"server"`
Ollama OllamaConfig `mapstructure:"ollama"`
Sys SysConfig `mapstructure:"sys"`
Tools ToolsConfig `mapstructure:"tools"`
Logging LoggingConfig `mapstructure:"logging"`
Redis Redis `mapstructure:"redis"`
DB DB `mapstructure:"db"`
DefaultPrompt SysPrompt `mapstructure:"default_prompt"`
PermissionConfig PermissionConfig `mapstructure:"permissionConfig"`
Server ServerConfig `mapstructure:"server"`
Ollama OllamaConfig `mapstructure:"ollama"`
Sys SysConfig `mapstructure:"sys"`
Tools ToolsConfig `mapstructure:"tools"`
Logging LoggingConfig `mapstructure:"logging"`
Redis Redis `mapstructure:"redis"`
DB DB `mapstructure:"db"`
DefaultPrompt SysPrompt `mapstructure:"default_prompt"`
// LLM *LLM `mapstructure:"llm"`
}
@ -90,12 +89,6 @@ type ToolsConfig struct {
//通过账号获取订单统计信息
ZltxOrderStatistics ToolConfig `mapstructure:"zltxOrderStatistics"`
DingTalkBot ToolConfig `mapstructure:"dingTalkBot"`
// 上游订单售后
ZltxOrderAfterSaleSupplier ToolConfig `mapstructure:"zltxOrderAfterSaleSupplier"`
// 下游订单售后
ZltxOrderAfterSaleReseller ToolConfig `mapstructure:"zltxOrderAfterSaleReseller"`
// 下游批充订单售后
ZltxOrderAfterSaleResellerBatch ToolConfig `mapstructure:"zltxOrderAfterSaleResellerBatch"`
}
// ToolConfig 单个工具配置
@ -114,12 +107,6 @@ type LoggingConfig struct {
Format string `mapstructure:"format"`
}
// PermissionConfig 权限校验配置
type PermissionConfig struct {
// 获取权限的地址
PermissionURL string `mapstructure:"permission_url"`
}
// LoadConfig 加载配置
func LoadConfig(configPath string) (*Config, error) {
viper.SetConfigFile(configPath)

View File

@ -1,11 +0,0 @@
package constants
const (
IsSHOW = 1
NotShow = 2
)
const (
Enable = 1
Disable = 2
)

View File

@ -13,7 +13,6 @@ var (
AuthNotFound = &BusinessErr{code: 408, message: "身份验证失败"}
KeyNotFound = &BusinessErr{code: 409, message: "身份验证失败"}
SysNotFound = &BusinessErr{code: 410, message: "未找到系统信息"}
SysCodeNotFound = &BusinessErr{code: 411, message: "未找到系统编码"}
InvalidParam = &BusinessErr{code: InvalidParamCode, message: "无效参数"}
)

View File

@ -12,21 +12,17 @@ const TableNameAiTask = "ai_task"
// AiTask mapped from table <ai_task>
type AiTask struct {
TaskID int32 `gorm:"column:task_id;primaryKey;autoIncrement:true" json:"task_id"`
SysID int32 `gorm:"column:sys_id;not null" json:"sys_id"`
Name string `gorm:"column:name;not null" json:"name"`
Index string `gorm:"column:index;not null" json:"index"`
Desc string `gorm:"column:desc;not null" json:"desc"`
UseCase string `gorm:"column:use_case;not null;comment:适用场景" json:"use_case"` // 适用场景
TempPrompt string `gorm:"column:temp_prompt;not null;comment:提示词模板" json:"temp_prompt"` // 提示词模板
Type int32 `gorm:"column:type;not null;default:1;comment:类型1api,2:知识库" json:"type"` // 类型1api,2:知识库
Config string `gorm:"column:config" json:"config"`
TagType int32 `gorm:"column:tag_type;comment:标签类型1.AI日常 2.AI查询 3.AI执行" json:"tag_type"` // 标签类型1.AI日常 2.AI查询 3.AI执行
CreateAt time.Time `gorm:"column:create_at;default:CURRENT_TIMESTAMP" json:"create_at"`
UpdatedAt time.Time `gorm:"column:updated_at;default:CURRENT_TIMESTAMP" json:"updated_at"`
IsShow int32 `gorm:"column:is_show;not null;default:1;comment:是否展示1为展示2为不展示" json:"is_show"` // 是否展示1为展示2为不展示
Status int32 `gorm:"column:status;not null;default:1" json:"status"`
DeleteAt time.Time `gorm:"column:delete_at" json:"delete_at"`
TaskID int32 `gorm:"column:task_id;primaryKey" json:"task_id"`
SysID int32 `gorm:"column:sys_id;not null" json:"sys_id"`
Name string `gorm:"column:name;not null" json:"name"`
Index string `gorm:"column:index;not null" json:"index"`
Desc string `gorm:"column:desc;not null" json:"desc"`
Type int32 `gorm:"column:type;not null;comment:类型1api,2:知识库" json:"type"` // 类型1api,2:知识库
Config string `gorm:"column:config" json:"config"`
CreateAt time.Time `gorm:"column:create_at;default:CURRENT_TIMESTAMP" json:"create_at"`
UpdateAt time.Time `gorm:"column:update_at;default:CURRENT_TIMESTAMP" json:"update_at"`
Status int32 `gorm:"column:status;not null;default:1" json:"status"`
DeleteAt time.Time `gorm:"column:delete_at" json:"delete_at"`
}
// TableName AiTask's table name

View File

@ -2,6 +2,7 @@ package entitys
import (
"encoding/json"
"github.com/gofiber/websocket/v2"
)
@ -67,13 +68,6 @@ func ResLoading(ch chan Response, index string, content string) {
Type: ResponseLoading,
}
}
func ResError(ch chan Response, index string, content string) {
ch <- Response{
Index: index,
Content: content,
Type: ResponseErr,
}
}
type ResponseData struct {
Done bool

View File

@ -1,138 +0,0 @@
package gateway
import (
errors "ai_scheduler/internal/data/error"
"ai_scheduler/internal/data/model"
"encoding/hex"
"fmt"
"github.com/gofiber/websocket/v2"
"math/rand"
"time"
)
var (
ErrConnClosed = errors.SysErr("连接不存在或已关闭")
)
type Client struct {
id string // 客户端唯一ID
conn *websocket.Conn // WebSocket 连接
session string // 会话ID
key string // 应用密钥
auth string // 用户凭证token
codes []string // 用户权限code
sysInfo *model.AiSy // 系统信息
tasks []model.AiTask // 任务列表
sysCode string // 系统编码
}
func NewClient(conn *websocket.Conn) *Client {
return &Client{
id: generateClientID(),
conn: conn,
}
}
// GetID 获取客户端的唯一ID
func (c *Client) GetID() string {
return c.id
}
// GetConn 获取客户端的 WebSocket 连接
func (c *Client) GetConn() *websocket.Conn {
return c.conn
}
// GetSession 获取会话ID
func (c *Client) GetSession() string {
return c.session
}
// GetKey 获取应用密钥
func (c *Client) GetKey() string {
return c.key
}
// GetAuth 获取用户凭证token
func (c *Client) GetAuth() string {
return c.auth
}
// GetCodes 获取用户权限code
func (c *Client) GetCodes() []string {
return c.codes
}
// GetSysCode 获取系统编码
func (c *Client) GetSysCode() string {
return c.sysCode
}
// GetSysInfo 获取系统信息
func (c *Client) GetSysInfo() *model.AiSy {
return c.sysInfo
}
// SetSysInfo 设置系统信息
func (c *Client) SetSysInfo(sysInfo *model.AiSy) {
c.sysInfo = sysInfo
}
// GetTasks 获取任务列表
func (c *Client) GetTasks() []model.AiTask {
return c.tasks
}
// SetTasks 设置任务列表
func (c *Client) SetTasks(tasks []model.AiTask) {
c.tasks = tasks
}
// 设置用户权限code
func (c *Client) SetCodes(codes []string) {
c.codes = codes
}
// SendFunc 发送消息到客户端
func (c *Client) SendFunc(msg []byte) error {
if c.conn != nil {
return c.conn.WriteMessage(websocket.TextMessage, msg)
}
return ErrConnClosed
}
// 生成唯一的客户端ID
func generateClientID() string {
// 使用时间戳+随机数确保唯一性
timestamp := time.Now().UnixNano()
randomBytes := make([]byte, 4)
rand.Read(randomBytes)
randomStr := hex.EncodeToString(randomBytes)
return fmt.Sprintf("%d%s", timestamp, randomStr)
}
// 连接数据验证和收集
func (c *Client) DataAuth() (err error) {
c.session = c.conn.Query("x-session", "")
if len(c.session) == 0 {
err = errors.SessionNotFound
return
}
c.auth = c.conn.Query("x-authorization", "")
if len(c.auth) == 0 {
err = errors.AuthNotFound
return
}
c.key = c.conn.Query("x-app-key", "")
if len(c.key) == 0 {
err = errors.KeyNotFound
return
}
// 系统编码
c.sysCode = c.conn.Query("x-sys-code", "")
if len(c.sysCode) == 0 {
err = errors.SysCodeNotFound
return
}
return
}

View File

@ -5,6 +5,11 @@ import (
"sync"
)
type Client struct {
ID string
SendFunc func(data []byte) error
}
type Gateway struct {
mu sync.RWMutex
clients map[string]*Client // clientID -> Client
@ -21,7 +26,7 @@ func NewGateway() *Gateway {
func (g *Gateway) AddClient(c *Client) {
g.mu.Lock()
defer g.mu.Unlock()
g.clients[c.GetID()] = c
g.clients[c.ID] = c
}
func (g *Gateway) RemoveClient(clientID string) {

View File

@ -2,7 +2,6 @@ package util
import (
"encoding/json"
"strconv"
"strings"
)
@ -20,15 +19,3 @@ func EscapeJSONString(s string) string {
b, _ := json.Marshal(s)
return string(b[1 : len(b)-1])
}
// string 转 int
func StringToInt(s string) int {
i, _ := strconv.Atoi(s)
return i
}
// string 转 float64
func StringToFloat64(s string) float64 {
i, _ := strconv.ParseFloat(s, 64)
return i
}

View File

@ -48,9 +48,8 @@ type Envelope struct {
// bug_optimization_submit 工单回调
const (
ActionBugOptimizationSubmitProcess = "bug_optimization_submit_process" // 工单过程回调
ActionBugOptimizationSubmitDone = "bug_optimization_submit_done" // 工单完成回调
ActionBugOptimizationSubmitUpdate = "bug_optimization_submit_update" // 工单更新回调
ActionBugOptimizationSubmitDone = "bug_optimization_submit_done" // 工单完成回调
ActionBugOptimizationSubmitUpdate = "bug_optimization_submit_update" // 工单更新回调
)
// BugOptimizationSubmitDoneData 工单完成回调数据
@ -168,18 +167,6 @@ func (s *CallbackService) handleDingTalkCallback(c *fiber.Ctx, env Envelope) err
// 删除映射
s.botTool.DelTaskMapping(env.TaskID)
return c.JSON(fiber.Map{"code": 0, "message": "ok"})
case ActionBugOptimizationSubmitProcess:
type processData struct {
Process string `json:"process"`
}
var data processData
if err := json.Unmarshal(env.Data, &data); err != nil {
return errorcode.ParamErr("invalid json: %v", err)
}
s.sendStreamLoading(sessionID, data.Process)
return c.JSON(fiber.Map{"code": 0, "message": "ok"})
default:
return errorcode.ParamErr("unknown action: %s", env.Action)
@ -207,10 +194,6 @@ func (s *CallbackService) getDingtalkReceivers(ctx context.Context, receiverIds
// sendStreamLog 发送流式日志
func (s *CallbackService) sendStreamLog(sessionID string, content string) {
if content == "" {
return
}
streamLog := entitys.Response{
Index: constants.BotToolsBugOptimizationSubmit,
Content: content,
@ -222,10 +205,6 @@ func (s *CallbackService) sendStreamLog(sessionID string, content string) {
// sendStreamTxt 发送流式文本
func (s *CallbackService) sendStreamTxt(sessionID string, content string) {
if content == "" {
return
}
streamLog := entitys.Response{
Index: constants.BotToolsBugOptimizationSubmit,
Content: content,
@ -235,21 +214,6 @@ func (s *CallbackService) sendStreamTxt(sessionID string, content string) {
s.gateway.SendToUid(sessionID, streamLogBytes)
}
// sendStreamLoading 发送流式加载过程
func (s *CallbackService) sendStreamLoading(sessionID string, content string) {
if content == "" {
return
}
streamLog := entitys.Response{
Index: constants.BotToolsBugOptimizationSubmit,
Content: content,
Type: entitys.ResponseLoading,
}
streamLogBytes := pkg.JsonByteIgonErr(streamLog)
s.gateway.SendToUid(sessionID, streamLogBytes)
}
// handleBugOptimizationSubmitUpdate 处理 bug 优化提交更新回调
func (s *CallbackService) handleBugOptimizationSubmitUpdate(ctx context.Context, taskData json.RawMessage) (string, *errorcode.BusinessErr) {
var data BugOptimizationSubmitUpdateData

View File

@ -2,13 +2,16 @@ package services
import (
"ai_scheduler/internal/biz"
"ai_scheduler/internal/config"
"ai_scheduler/internal/data/constants"
"ai_scheduler/internal/entitys"
"ai_scheduler/internal/gateway"
"encoding/hex"
"encoding/json"
"fmt"
"log"
"math/rand"
"sync"
"time"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/websocket/v2"
@ -20,7 +23,6 @@ type ChatService struct {
Gw *gateway.Gateway
mu sync.Mutex
ChatHis *biz.ChatHistoryBiz
cfg *config.Config
}
// NewChatHandler 创建聊天处理器
@ -28,13 +30,11 @@ func NewChatService(
routerService *biz.AiRouterBiz,
chatHis *biz.ChatHistoryBiz,
gw *gateway.Gateway,
cfg *config.Config,
) *ChatService {
return &ChatService{
routerBiz: routerService,
Gw: gw,
ChatHis: chatHis,
cfg: cfg,
}
}
@ -60,52 +60,36 @@ func (h *ChatService) ChatFail(c *websocket.Conn, content string) {
_ = c.Close()
}
// Chat 处理WebSocket聊天连接
// 这是WebSocket处理的主入口函数
func generateClientID() string {
// 使用时间戳+随机数确保唯一性
timestamp := time.Now().UnixNano()
randomBytes := make([]byte, 4)
rand.Read(randomBytes)
randomStr := hex.EncodeToString(randomBytes)
return fmt.Sprintf("%d%s", timestamp, randomStr)
}
func (h *ChatService) Chat(c *websocket.Conn) {
// 创建新的客户端实例
h.mu.Lock()
client := gateway.NewClient(c)
clientID := generateClientID()
h.mu.Unlock()
// 将客户端添加到网关管理
client := &gateway.Client{
ID: clientID,
SendFunc: func(data []byte) error {
return c.WriteMessage(websocket.TextMessage, data)
},
}
h.Gw.AddClient(client)
log.Println("client connected:", client.GetID())
log.Println("client connected:", clientID)
log.Println("客户端已连接")
// 绑定会话ID
uid := c.Query("x-session")
if uid != "" {
if err := h.Gw.BindUid(client.GetID(), uid); err != nil {
log.Println("绑定UID错误:", err)
}
log.Printf("bind %s -> uid:%s\n", client.GetID(), uid)
}
// 验证并收集连接数据,后续对话中会使用
if err := client.DataAuth(); err != nil {
log.Println("数据验证错误:", err)
h.ChatFail(c, err.Error())
return
}
// 确保在函数返回时移除客户端并关闭连接
defer func() {
h.Gw.RemoveClient(client.GetID())
_ = c.Close()
log.Println("client disconnected:", client.GetID())
}()
// 循环读取客户端消息
for {
// 读取消息
messageType, message, err := c.ReadMessage()
if err != nil {
log.Println("读取错误:", err)
break
}
// 处理消息
msg, chatType := h.handleMessageToString(c, messageType, message)
if chatType == constants.ConnStatusClosed {
break
@ -115,31 +99,39 @@ func (h *ChatService) Chat(c *websocket.Conn) {
}
log.Printf("收到消息: %s", string(msg))
// 解析请求
var req entitys.ChatSockRequest
if err = json.Unmarshal(msg, &req); err != nil {
if err := json.Unmarshal(msg, &req); err != nil {
log.Println("JSON parse error:", err)
continue
}
// 路由处理请求
err = h.routerBiz.RouteWithSocket(client, &req)
//简单协议bind:<uid>
// if c.Headers("Sec-Websocket-Protocol") == "bind" && req.SessionID != "" {
// uid := c.Query("x-session")
// _ = h.Gw.BindUid(clientID, req.SessionID)
// log.Printf("bind %s -> uid:%s\n", clientID, uid)
// }
uid := c.Query("x-session")
if uid != "" {
_ = h.Gw.BindUid(clientID, uid)
log.Printf("bind %s -> uid:%s\n", clientID, uid)
}
err = h.routerBiz.RouteWithSocket(c, &req)
if err != nil {
log.Println("处理失败:", err)
entitys.MsgSend(c, entitys.Response{
Content: err.Error(),
Type: entitys.ResponseText,
})
}
}
h.Gw.RemoveClient(clientID)
_ = c.Close()
log.Println("client disconnected:", clientID)
}
// handleMessageToString 处理不同类型的WebSocket消息
// 参数:
// - c: WebSocket连接
// - msgType: 消息类型
// - msg: 消息内容
//
// 返回:
// - text: 处理后的文本内容
// - chatType: 连接状态
func (h *ChatService) handleMessageToString(c *websocket.Conn, msgType int, msg any) (text []byte, chatType constants.ConnStatus) {
switch msgType {
case websocket.TextMessage:

View File

@ -3,6 +3,7 @@ package services
import (
"ai_scheduler/internal/biz"
"ai_scheduler/internal/entitys"
"github.com/gofiber/fiber/v2"
)
@ -24,15 +25,7 @@ func (s *TaskService) Tasks(c *fiber.Ctx) error {
return err
}
auth := ""
if auths := c.GetReqHeaders()["Authorization"]; len(auths) > 0 {
auth = auths[0]
}
if auth == "" {
return fiber.ErrUnauthorized
}
result, err := s.taskBiz.TaskList(c.Context(), req, auth)
result, err := s.taskBiz.TaskList(c.Context(), req)
if err != nil {
return err

View File

@ -1,17 +1,19 @@
package tools
import (
"ai_scheduler/internal/config"
"ai_scheduler/internal/entitys"
"testing"
)
func TestKnowledgeBaseTool_Execute(t *testing.T) {
// kb := NewKnowledgeBaseTool(config.ToolConfig{})
// channel := make(chan entitys.ResponseData)
// err := kb.Execute(channel, nil, nil)
// if err != nil {
// t.Errorf("Execute() error = %v", err)
// }
kb := NewKnowledgeBaseTool(config.ToolConfig{})
channel := make(chan entitys.ResponseData)
err := kb.Execute(channel, nil, nil)
if err != nil {
t.Errorf("Execute() error = %v", err)
}
}

View File

@ -5,7 +5,6 @@ import (
"ai_scheduler/internal/data/constants"
"ai_scheduler/internal/entitys"
"ai_scheduler/internal/pkg/utils_ollama"
zltxtool "ai_scheduler/internal/tools/zltx"
"context"
"fmt"
@ -71,22 +70,10 @@ func NewManager(config *config.Config, llm *utils_ollama.Client) *Manager {
m.tools[knowledgeTool.Name()] = knowledgeTool
}
// 注册直连天下上游订单售后工具
if config.Tools.ZltxOrderAfterSaleSupplier.Enabled {
zltxOrderAfterSaleSupplierTool := zltxtool.NewOrderAfterSaleSupplierTool(config.Tools.ZltxOrderAfterSaleSupplier)
m.tools[zltxOrderAfterSaleSupplierTool.Name()] = zltxOrderAfterSaleSupplierTool
if config.Tools.Knowledge.Enabled {
knowledgeTool := NewKnowledgeBaseTool(config.Tools.Knowledge)
m.tools[knowledgeTool.Name()] = knowledgeTool
}
// 注册直连天下下游订单售后工具
if config.Tools.ZltxOrderAfterSaleReseller.Enabled {
zltxOrderAfterSaleResellerTool := zltxtool.NewOrderAfterSaleResellerTool(config.Tools.ZltxOrderAfterSaleReseller)
m.tools[zltxOrderAfterSaleResellerTool.Name()] = zltxOrderAfterSaleResellerTool
}
// 注册直连天下下游批充订单售后工具
if config.Tools.ZltxOrderAfterSaleResellerBatch.Enabled {
zltxOrderAfterSaleResellerBatchTool := zltxtool.NewOrderAfterSaleResellerBatchTool(config.Tools.ZltxOrderAfterSaleResellerBatch)
m.tools[zltxOrderAfterSaleResellerBatchTool.Name()] = zltxOrderAfterSaleResellerBatchTool
}
// 普通对话
chat := NewNormalChatTool(m.llm, config)
m.tools[chat.Name()] = chat

View File

@ -1,357 +0,0 @@
package zltx
import (
"ai_scheduler/internal/config"
"ai_scheduler/internal/entitys"
"ai_scheduler/internal/pkg/l_request"
"ai_scheduler/internal/pkg/util"
"context"
"encoding/json"
"fmt"
"sync"
"time"
)
type OrderAfterSaleResellerTool struct {
config config.ToolConfig
}
// NewOrderAfterSaleResellerTool 创建售后订单预检工具
func NewOrderAfterSaleResellerTool(config config.ToolConfig) *OrderAfterSaleResellerTool {
return &OrderAfterSaleResellerTool{config: config}
}
// Name 返回工具名称
func (t *OrderAfterSaleResellerTool) Name() string {
return "zltxOrderAfterSaleReseller"
}
// 未使用-仅实现接口
func (t *OrderAfterSaleResellerTool) Description() string {
return "直连天下下游分销商直充订单售后工具"
}
// 未使用-仅实现接口
func (t *OrderAfterSaleResellerTool) Definition() entitys.ToolDefinition {
return entitys.ToolDefinition{}
}
type OrderAfterSaleResellerRequest struct {
OrderNumber []string `json:"orderNumber"` // 订单号
Account []string `json:"account"` // 充值账号
SerialCreateTime string `json:"serialCreateTime"` // 流水创建时间
AfterType string `json:"afterType"` // 处理方式 1.退款 2.扣款
AfterSalesPrice string `json:"afterSalesPrice"` // 售后金额
AfterSalesReason string `json:"afterSalesReason"` // 售后原因
ResponsibleType string `json:"responsibleType"` // 费用承担者 1.供应商 2.商务 3.公司 4.无
ResponsiblePerson string `json:"responsiblePerson"` // 费用承担供应商
}
type OrderAfterSaleResellerResponse struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data []*OrderAfterSaleResellerData `json:"data"`
}
type OrderAfterSaleResellerData struct {
OrderType int `json:"orderType"`
OrderNumber string `json:"orderNumber"`
OrderAmount float64 `json:"orderAmount"`
OrderPrice float64 `json:"orderPrice"`
SignCompany int `json:"signCompany"`
OrderQuantity int `json:"orderQuantity"`
ResellerID int `json:"resellerId"`
ResellerName string `json:"resellerName"`
OurProductID int `json:"ourProductId"`
OurProductTitle string `json:"ourProductTitle"`
Account []string `json:"account"`
Platforms map[int]string `json:"platforms"`
AfterType int `json:"afterType"` // 处理方式 1.退款 2.扣款
Remark string `json:"remark"` // 售后原因
AfterAmount float64 `json:"afterAmount"` // 售后金额
ResponsibleType int `json:"responsibleType"` // 费用承担者 1.供应商 2.商务 3.公司 4.无
ResponsiblePerson string `json:"responsiblePerson"` // 费用承担供应商
IsExistsAfterSale bool `json:"isExistsAfterSale"` // 是否已存在售后
CreateTime int `json:"createTime"` // 流水创建时间
}
// 接口返回
type OrderAfterSaleResellerApiResponse struct {
Code int `json:"code"`
Error string `json:"error"`
Data OrderAfterSaleResellerApiData `json:"data"`
}
type OrderAfterSaleResellerApiData struct {
Data []OrderAfterSaleResellerApiBase `json:"data"`
ExtData map[string]OrderAfterSaleResellerApiExtItem `json:"extraData"`
}
type OrderAfterSaleResellerApiBase struct {
OrderType int `json:"orderType"`
OrderNumber string `json:"orderNumber"`
OrderAmount float64 `json:"orderAmount"`
OrderPrice float64 `json:"orderPrice"`
SignCompany int `json:"signCompany"`
OrderQuantity int `json:"orderQuantity"`
ResellerID int `json:"resellerId"`
ResellerName string `json:"resellerName"`
OurProductID int `json:"ourProductId"`
OurProductTitle string `json:"ourProductTitle"`
Account []string `json:"account"`
Platforms map[int]string `json:"platforms"`
}
type OrderAfterSaleResellerApiExtItem struct {
IsExistsAfterSale bool `json:"isExistsAfterSale"` // 是否已存在售后 - 未使用
SerialCreateTime int `json:"createTime"` // 流水创建时间
}
func (t *OrderAfterSaleResellerTool) Execute(ctx context.Context, requireData *entitys.RequireData) error {
var req OrderAfterSaleResellerRequest
if err := json.Unmarshal([]byte(requireData.Match.Parameters), &req); err != nil {
return fmt.Errorf("解析参数失败,请重试或联系管理员")
}
if len(req.OrderNumber) == 0 && len(req.Account) == 0 {
return fmt.Errorf("订单号 和 充值账号 不能同时为空")
}
// 时间格式不匹配,直接置为空
if req.SerialCreateTime != "" {
_, err := time.ParseInLocation(time.DateTime, req.SerialCreateTime, time.Local)
if err != nil {
entitys.ResLog(requireData.Ch, t.Name(), "时间格式不匹配,已置为空")
req.SerialCreateTime = ""
}
}
entitys.ResLog(requireData.Ch, t.Name(), "正在拉取售后订单信息")
return t.checkOrderAfterSaleReseller(req, requireData)
}
func (t *OrderAfterSaleResellerTool) checkOrderAfterSaleReseller(toolReq OrderAfterSaleResellerRequest, requireData *entitys.RequireData) error {
var serialStartTime, serialEndTime int64
if toolReq.SerialCreateTime != "" {
// 流水创建时间上下浮动10min
serialCreateTime, err := time.ParseInLocation(time.DateTime, toolReq.SerialCreateTime, time.Local)
if err != nil {
return err
}
serialStartTime = serialCreateTime.Unix() - 10*60
serialEndTime = serialCreateTime.Unix() + 10*60
} else {
// 未指定流水创建时间默认30天内
serialEndTime = time.Now().Unix()
serialStartTime = serialEndTime - 60*60*24*30 // 30天内
}
// 账号数量超过10直接截断
if len(toolReq.Account) > 10 {
entitys.ResLog(requireData.Ch, t.Name(), "账号数量超过10已被截断")
toolReq.Account = toolReq.Account[:10]
}
headers := map[string]string{
"Authorization": fmt.Sprintf("Bearer %s", requireData.Auth),
}
// 最终输出
var orderList []*OrderAfterSaleResellerData
var err error
// 多订单号
if len(toolReq.OrderNumber) > 0 {
body := map[string]any{
"order_numbers": toolReq.OrderNumber, // 订单号
}
orderList, err = t.getAfterSaleResellerList(headers, body, toolReq)
if err != nil {
return err
}
} else if len(toolReq.Account) > 0 {
// 多充值账号并发
orderListChan := make(chan []*OrderAfterSaleResellerData, len(toolReq.Account))
waitGroup := sync.WaitGroup{}
// 并发请求
for _, account := range toolReq.Account {
waitGroup.Add(1)
go func(account string) {
defer waitGroup.Done()
body := map[string]any{
"account": account, // 充值账号
"create_time": []int64{serialStartTime, serialEndTime}, // 流水创建时间区间
"order_type": 1, // 1.直充
}
orderListIn, errIn := t.getAfterSaleResellerList(headers, body, toolReq)
if errIn != nil {
return
}
orderListChan <- orderListIn
}(account)
}
waitGroup.Wait()
close(orderListChan)
// 合并结果
for orderListIn := range orderListChan {
orderList = append(orderList, orderListIn...)
}
} else {
return fmt.Errorf("订单号 和 充值账号 不能同时为空")
}
// 未查询到相应售后订单,请核实提供信息是否正确
if len(orderList) == 0 {
return fmt.Errorf("未查询到相应售后订单,请核实提供信息是否正确")
}
toolResp := OrderAfterSaleResellerResponse{
Code: 0,
Msg: "Success",
Data: orderList,
}
var jsonByte []byte
jsonByte, err = json.Marshal(toolResp)
if err != nil {
return err
}
entitys.ResLog(requireData.Ch, t.Name(), "售后订单信息拉取完成")
entitys.ResJson(requireData.Ch, t.Name(), string(jsonByte))
return nil
}
func (t *OrderAfterSaleResellerTool) getAfterSaleResellerList(headers map[string]string, body map[string]any, originInput OrderAfterSaleResellerRequest) ([]*OrderAfterSaleResellerData, error) {
req := l_request.Request{
Url: t.config.BaseURL,
Headers: headers,
Method: "POST",
Json: body,
}
res, err := req.Send()
if err != nil {
return nil, err
}
// 解析响应
var resp OrderAfterSaleResellerApiResponse
if err = json.Unmarshal(res.Content, &resp); err != nil {
return nil, err
}
if resp.Code != 200 {
return nil, fmt.Errorf("after sale reseller failed: %s", resp.Error)
}
orderList := make([]*OrderAfterSaleResellerData, 0, len(resp.Data.Data))
// 转换数据
for _, item := range resp.Data.Data {
// 处理方式
afterType := util.StringToInt(originInput.AfterType)
if afterType == 0 {
afterType = 1 // 默认退款
}
// 费用承担者
responsibleType := util.StringToInt(originInput.ResponsibleType)
if responsibleType == 0 {
responsibleType = 4 // 默认无
}
// 售后金额
afterSalesPrice := util.StringToFloat64(originInput.AfterSalesPrice)
if afterSalesPrice == 0 {
afterSalesPrice = item.OrderPrice
}
orderList = append(orderList, &OrderAfterSaleResellerData{
OrderType: item.OrderType,
OrderNumber: item.OrderNumber,
OrderAmount: item.OrderAmount,
OrderPrice: item.OrderPrice,
SignCompany: item.SignCompany,
OrderQuantity: item.OrderQuantity,
ResellerID: item.ResellerID,
ResellerName: item.ResellerName,
OurProductID: item.OurProductID,
OurProductTitle: item.OurProductTitle,
Account: item.Account,
Platforms: item.Platforms,
AfterType: afterType,
Remark: originInput.AfterSalesReason,
AfterAmount: afterSalesPrice,
ResponsibleType: responsibleType,
ResponsiblePerson: originInput.ResponsiblePerson,
})
}
// 追加扩展数据
for _, item := range orderList {
if extItem, ok := resp.Data.ExtData[item.OrderNumber]; ok {
item.IsExistsAfterSale = item.OrderType > 100 // 101 直充&已售后
item.CreateTime = extItem.SerialCreateTime
}
}
return orderList, nil
}
// func (t *OrderAfterSaleResellerTool) checkOrderAfterSaleResellerMock(req OrderAfterSaleResellerRequest, requireData *entitys.RequireData) error {
// resp := OrderAfterSaleResellerResponse{
// Code: 0,
// Msg: "success",
// Data: []*OrderAfterSaleResellerData{
// {
// OrderType: 1,
// OrderNumber: "846784115378364417",
// OrderAmount: 0.1,
// OrderPrice: 0.1,
// SignCompany: 1,
// OrderQuantity: 1,
// ResellerID: 23329,
// ResellerName: "分销商23329",
// OurProductID: 106,
// OurProductTitle: "爱奇艺黄金会员周卡",
// Account: []string{"15516353308"},
// Platforms: map[int]string{4: "爱奇艺"},
// CreateTime: 1723304000,
// AfterType: 1,
// Remark: "测试售后",
// AfterAmount: 50,
// ResponsibleType: 1,
// IsExistsAfterSale: false,
// },
// {
// OrderType: 101,
// OrderNumber: "846052057729867777",
// OrderAmount: 23,
// OrderPrice: 23,
// SignCompany: 1,
// OrderQuantity: 1,
// ResellerID: 25629,
// ResellerName: "二期财务分销商简称",
// OurProductID: 104,
// OurProductTitle: "优酷年卡",
// Account: []string{"18380416326"},
// Platforms: map[int]string{1: "爱瓦力"},
// CreateTime: 1723305000,
// AfterType: 2,
// Remark: "测试售后2",
// AfterAmount: 30,
// ResponsibleType: 2,
// IsExistsAfterSale: false,
// },
// },
// }
// if len(req.OrderNumber) == 1 {
// resp.Data = resp.Data[:1]
// }
// jsonByte, err := json.Marshal(resp)
// if err != nil {
// return err
// }
// entitys.ResLog(requireData.Ch, t.Name(), "售后订单信息拉取完成")
// entitys.ResJson(requireData.Ch, t.Name(), string(jsonByte))
// return nil
// }

View File

@ -1,206 +0,0 @@
package zltx
import (
"ai_scheduler/internal/config"
"ai_scheduler/internal/entitys"
"ai_scheduler/internal/pkg/l_request"
"ai_scheduler/internal/pkg/util"
"context"
"encoding/json"
"fmt"
)
type OrderAfterSaleResellerBatchTool struct {
config config.ToolConfig
}
// NewOrderAfterSaleResellerBatchTool 创建售后订单预检工具
func NewOrderAfterSaleResellerBatchTool(config config.ToolConfig) *OrderAfterSaleResellerBatchTool {
return &OrderAfterSaleResellerBatchTool{config: config}
}
// Name 返回工具名称
func (t *OrderAfterSaleResellerBatchTool) Name() string {
return "zltxOrderAfterSaleResellerBatch"
}
// 未使用-仅实现接口
func (t *OrderAfterSaleResellerBatchTool) Description() string {
return "直连天下下游分销商批充订单售后工具"
}
// 未使用-仅实现接口
func (t *OrderAfterSaleResellerBatchTool) Definition() entitys.ToolDefinition {
return entitys.ToolDefinition{}
}
type OrderAfterSaleResellerBatchRequest struct {
OrderNumber []string `json:"orderNumber"` // 订单号
AfterType string `json:"afterType"` // 处理方式 1.退款 2.扣款
AfterSalesPrice string `json:"afterSalesPrice"` // 售后金额
AfterSalesReason string `json:"afterSalesReason"` // 售后原因
ResponsibleType string `json:"responsibleType"` // 费用承担者 1.供应商 2.商务 3.公司 4.无
ResponsiblePerson string `json:"responsiblePerson"` // 费用承担供应商
}
type OrderAfterSaleResellerBatchResponse struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data []*OrderAfterSaleResellerBatchData `json:"data"`
}
type OrderAfterSaleResellerBatchData struct {
OrderType int `json:"orderType"`
OrderNumber string `json:"orderNumber"`
OrderAmount float64 `json:"orderAmount"`
OrderPrice float64 `json:"orderPrice"`
SignCompany int `json:"signCompany"`
OrderQuantity int `json:"orderQuantity"`
ResellerID int `json:"resellerId"`
ResellerName string `json:"resellerName"`
OurProductID int `json:"ourProductId"`
OurProductTitle string `json:"ourProductTitle"`
Account []string `json:"account"`
Platforms map[int]string `json:"platforms"`
AfterType int `json:"afterType"` // 处理方式 1.退款 2.扣款
Remark string `json:"remark"` // 售后原因
AfterAmount float64 `json:"afterAmount"` // 售后金额
ResponsibleType int `json:"responsibleType"` // 费用承担者 1.供应商 2.商务 3.公司 4.无
ResponsiblePerson string `json:"responsiblePerson"` // 费用承担供应商
IsExistsAfterSale bool `json:"isExistsAfterSale"` // 是否已存在售后
CreateTime int `json:"createTime"` // 创建时间
}
// 接口返回
type OrderAfterSaleResellerBatchApiResponse struct {
Code int `json:"code"`
Error string `json:"error"`
Data OrderAfterSaleResellerBatchApiData `json:"data"`
}
type OrderAfterSaleResellerBatchApiData struct {
Data []OrderAfterSaleResellerBatchApiBase `json:"data"`
ExtData map[string]OrderAfterSaleResellerBatchApiExtItem `json:"extraData"`
}
type OrderAfterSaleResellerBatchApiBase struct {
OrderType int `json:"orderType"`
OrderNumber string `json:"orderNumber"`
OrderAmount float64 `json:"orderAmount"`
OrderPrice float64 `json:"orderPrice"`
SignCompany int `json:"signCompany"`
OrderQuantity int `json:"orderQuantity"`
ResellerID int `json:"resellerId"`
ResellerName string `json:"resellerName"`
OurProductID int `json:"ourProductId"`
OurProductTitle string `json:"ourProductTitle"`
Account []string `json:"account"`
Platforms map[int]string `json:"platforms"`
}
type OrderAfterSaleResellerBatchApiExtItem struct {
IsExistsAfterSale bool `json:"isExistsAfterSale"` // 是否已存在售后 - 未使用
SerialCreateTime int `json:"createTime"` // 流水创建时间
}
func (t *OrderAfterSaleResellerBatchTool) Execute(ctx context.Context, requireData *entitys.RequireData) error {
var req OrderAfterSaleResellerBatchRequest
if err := json.Unmarshal([]byte(requireData.Match.Parameters), &req); err != nil {
return fmt.Errorf("解析参数失败,请重试或联系管理员")
}
if len(req.OrderNumber) == 0 {
return fmt.Errorf("批充订单号不能为空")
}
entitys.ResLog(requireData.Ch, t.Name(), "正在拉取售后订单信息")
return t.checkOrderAfterSaleResellerBatch(req, requireData)
}
func (t *OrderAfterSaleResellerBatchTool) checkOrderAfterSaleResellerBatch(toolReq OrderAfterSaleResellerBatchRequest, requireData *entitys.RequireData) error {
req := l_request.Request{
Url: t.config.BaseURL,
Headers: map[string]string{
"Authorization": fmt.Sprintf("Bearer %s", requireData.Auth),
},
Method: "POST",
Json: map[string]any{
"order_numbers": toolReq.OrderNumber, // 流水号
"order_type": 2, // 2.批充
},
}
res, err := req.Send()
if err != nil {
return err
}
// 解析响应
var resp OrderAfterSaleResellerBatchApiResponse
if err = json.Unmarshal(res.Content, &resp); err != nil {
return err
}
if resp.Code != 200 {
return fmt.Errorf("售后订单查询异常: %s", resp.Error)
}
if len(resp.Data.Data) == 0 {
return fmt.Errorf("未查询到相应售后订单,请核实订单号是否正确")
}
toolResp := OrderAfterSaleResellerBatchResponse{
Code: resp.Code,
Msg: resp.Error,
Data: make([]*OrderAfterSaleResellerBatchData, 0, len(resp.Data.Data)),
}
// 转换数据
for _, item := range resp.Data.Data {
// 处理方式
afterType := util.StringToInt(toolReq.AfterType)
if afterType == 0 {
afterType = 1 // 默认退款
}
// 费用承担者
responsibleType := util.StringToInt(toolReq.ResponsibleType)
if responsibleType == 0 {
responsibleType = 4 // 默认无
}
// 售后金额
afterSalesPrice := util.StringToFloat64(toolReq.AfterSalesPrice)
if afterSalesPrice == 0 {
afterSalesPrice = item.OrderPrice
}
toolResp.Data = append(toolResp.Data, &OrderAfterSaleResellerBatchData{
OrderType: item.OrderType,
OrderNumber: item.OrderNumber,
OrderAmount: item.OrderAmount,
OrderPrice: item.OrderPrice,
SignCompany: item.SignCompany,
OrderQuantity: item.OrderQuantity,
ResellerID: item.ResellerID,
ResellerName: item.ResellerName,
OurProductID: item.OurProductID,
OurProductTitle: item.OurProductTitle,
Account: item.Account,
Platforms: item.Platforms,
AfterType: afterType,
Remark: toolReq.AfterSalesReason,
AfterAmount: afterSalesPrice,
ResponsibleType: responsibleType,
ResponsiblePerson: toolReq.ResponsiblePerson,
})
}
// 追加扩展数据
for _, item := range toolResp.Data {
if extItem, ok := resp.Data.ExtData[item.OrderNumber]; ok {
item.IsExistsAfterSale = item.OrderType > 100 // 102 批充&已售后
item.CreateTime = extItem.SerialCreateTime
}
}
jsonByte, err := json.Marshal(toolResp)
if err != nil {
return err
}
entitys.ResLog(requireData.Ch, t.Name(), "售后订单信息拉取完成")
entitys.ResJson(requireData.Ch, t.Name(), string(jsonByte))
return nil
}

View File

@ -1,279 +0,0 @@
package zltx
import (
"ai_scheduler/internal/config"
"ai_scheduler/internal/entitys"
"ai_scheduler/internal/pkg/l_request"
"ai_scheduler/internal/pkg/util"
"context"
"encoding/json"
"fmt"
"sync"
"time"
)
type OrderAfterSaleSupplierTool struct {
config config.ToolConfig
}
// NewOrderAfterSaleSupplierTool 创建售后订单预检工具
func NewOrderAfterSaleSupplierTool(config config.ToolConfig) *OrderAfterSaleSupplierTool {
return &OrderAfterSaleSupplierTool{config: config}
}
// Name 返回工具名称
func (t *OrderAfterSaleSupplierTool) Name() string {
return "zltxOrderAfterSaleSupplier"
}
// 未使用-仅实现接口
func (t *OrderAfterSaleSupplierTool) Description() string {
return "直连天下上游供应商直充订单售后工具"
}
// 未使用-仅实现接口
func (t *OrderAfterSaleSupplierTool) Definition() entitys.ToolDefinition {
return entitys.ToolDefinition{}
}
type OrderAfterSaleSupplierRequest struct {
SerialNumber []string `json:"serialNumber"` // 流水号
Account []string `json:"account"` // 充值账号
SerialCreateTime string `json:"serialCreateTime"` // 流水创建时间
AfterSalesReason string `json:"afterSalesReason"` // 售后原因
AfterSalesPrice string `json:"afterSalesPrice"` // 售后金额
AfterType string `json:"afterType"` // 售后类型 1.加款 2.扣款
}
// 工具最终返回
type OrderAfterSaleSupplierResponse struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data []*OrderAfterSaleSupplierData `json:"data"`
}
type OrderAfterSaleSupplierData struct {
SerialNumber string `json:"serialNumber"` // 流水号
PlatformName string `json:"platformName"` // 供应商名称
SignCompany int `json:"signCompany"` // 签约主体
PlatformProductName string `json:"platformProductName"` // 商品名称
PlatformPrice float64 `json:"platformPrice"` // 上游价格
TerminalAccount string `json:"terminalAccount"` // 充值账号
Status int `json:"status"` // 充值状态
PlatformProductID int `json:"platformProductId"` // 上有商品id
PlatformID int `json:"platformId"` // 上游平台id
SignCompanyName string `json:"signCompanyName"` // 签约主体名称
Reason string `json:"reason"` // 售后原因
SalePrice float64 `json:"salePrice"` // 售后金额
SaleType int `json:"saleType"` // 处理方式 1.加款 2.扣款
ExecuteTime int `json:"executeTime"` // 流水创建时间
IsExistsAfterSale bool `json:"isExistsAfterSale"` // 是否已存在售后
}
// 接口返回
type OrderAfterSaleSupplierApiResponse struct {
Code int `json:"code"`
Error string `json:"error"`
Data OrderAfterSaleSupplierApiData `json:"data"`
}
type OrderAfterSaleSupplierApiData struct {
Data []OrderAfterSaleSupplierApiBase `json:"data"`
ExtData map[string]OrderAfterSaleSupplierApiExtItem `json:"extraData"`
}
type OrderAfterSaleSupplierApiBase struct {
SerialNumber string `json:"serialNumber"` // 流水号
PlatformName string `json:"platformName"` // 供应商名称
SignCompany int `json:"signCompany"` // 签约主体
PlatformProductName string `json:"platformProductName"` // 商品名称
PlatformPrice float64 `json:"platformPrice"` // 上游价格
TerminalAccount string `json:"terminalAccount"` // 充值账号
Status int `json:"status"` // 充值状态
PlatformProductID int `json:"platformProductId"` // 上有商品id
PlatformID int `json:"platformId"` // 上游平台id
SignCompanyName string `json:"signCompanyName"` // 签约主体名称
ExecuteTime int `json:"executeTime"` // 充值执行时间
}
type OrderAfterSaleSupplierApiExtItem struct {
IsExistsAfterSale bool `json:"existAfterSales"` // 是否已存在售后 - 未使用
SerialCreateTime int `json:"createTime"` // 流水创建时间
}
func (t *OrderAfterSaleSupplierTool) Execute(ctx context.Context, requireData *entitys.RequireData) error {
var req OrderAfterSaleSupplierRequest
if err := json.Unmarshal([]byte(requireData.Match.Parameters), &req); err != nil {
return fmt.Errorf("解析参数失败,请重试或联系管理员")
}
if len(req.SerialNumber) == 0 && len(req.Account) == 0 {
return fmt.Errorf("充值流水号 和 充值账号 不能同时为空")
}
// 时间格式不匹配,直接置为空
if req.SerialCreateTime != "" {
_, err := time.ParseInLocation(time.DateTime, req.SerialCreateTime, time.Local)
if err != nil {
entitys.ResLog(requireData.Ch, t.Name(), "时间格式不匹配,已置为空")
req.SerialCreateTime = ""
}
}
entitys.ResLog(requireData.Ch, t.Name(), "正在拉取售后订单信息")
return t.checkOrderAfterSaleSupplier(req, requireData)
}
func (t *OrderAfterSaleSupplierTool) checkOrderAfterSaleSupplier(toolReq OrderAfterSaleSupplierRequest, requireData *entitys.RequireData) error {
var serialStartTime, serialEndTime int64
if toolReq.SerialCreateTime != "" {
// 流水创建时间上下浮动10min
serialCreateTime, err := time.ParseInLocation(time.DateTime, toolReq.SerialCreateTime, time.Local)
if err != nil {
return err
}
serialStartTime = serialCreateTime.Unix() - 10*60
serialEndTime = serialCreateTime.Unix() + 10*60
} else {
// 未指定流水创建时间默认30天内
serialEndTime = time.Now().Unix()
serialStartTime = serialEndTime - 60*60*24*30 // 30天内
}
// 账号数量超过10直接截断
if len(toolReq.Account) > 10 {
entitys.ResLog(requireData.Ch, t.Name(), "账号数量超过10已被截断")
toolReq.Account = toolReq.Account[:10]
}
headers := map[string]string{
"Authorization": fmt.Sprintf("Bearer %s", requireData.Auth),
}
// 最终输出
var orderList []*OrderAfterSaleSupplierData
var err error
// 多流水号
if len(toolReq.SerialNumber) > 0 {
body := map[string]any{
"serial_numbers": toolReq.SerialNumber, // 流水号
}
orderList, err = t.getAfterSaleSupplierList(headers, body, toolReq)
if err != nil {
return err
}
} else if len(toolReq.Account) > 0 {
// 多充值账号并发
orderListChan := make(chan []*OrderAfterSaleSupplierData, len(toolReq.Account))
waitGroup := sync.WaitGroup{}
// 并发请求
for _, account := range toolReq.Account {
waitGroup.Add(1)
go func(account string) {
defer waitGroup.Done()
body := map[string]any{
"account": account, // 充值账号
"create_time": []int64{serialStartTime, serialEndTime}, // 流水创建时间区间
}
orderListIn, errIn := t.getAfterSaleSupplierList(headers, body, toolReq)
if errIn != nil {
return
}
orderListChan <- orderListIn
}(account)
}
// 等待所有请求完成
waitGroup.Wait()
close(orderListChan)
for orderListIn := range orderListChan {
orderList = append(orderList, orderListIn...)
}
} else {
return fmt.Errorf("充值流水号 和 充值账号 不能同时为空")
}
// 未查询到相应售后订单,请核实提供信息是否正确
if len(orderList) == 0 {
return fmt.Errorf("未查询到相应售后订单,请核实提供信息是否正确")
}
toolResp := OrderAfterSaleSupplierResponse{
Code: 0,
Msg: "Success",
Data: orderList,
}
var jsonByte []byte
jsonByte, err = json.Marshal(toolResp)
if err != nil {
return err
}
entitys.ResLog(requireData.Ch, t.Name(), "售后订单信息拉取完成")
entitys.ResJson(requireData.Ch, t.Name(), string(jsonByte))
return nil
}
func (t *OrderAfterSaleSupplierTool) getAfterSaleSupplierList(headers map[string]string, body map[string]any, originInput OrderAfterSaleSupplierRequest) ([]*OrderAfterSaleSupplierData, error) {
req := l_request.Request{
Url: t.config.BaseURL,
Headers: headers,
Method: "POST",
Json: body,
}
res, err := req.Send()
if err != nil {
return nil, err
}
// 解析响应
var resp OrderAfterSaleSupplierApiResponse
if err = json.Unmarshal(res.Content, &resp); err != nil {
return nil, err
}
if resp.Code != 200 {
return nil, fmt.Errorf("after sale supplier failed: %s", resp.Error)
}
orderList := make([]*OrderAfterSaleSupplierData, 0, len(resp.Data.Data))
// 转换数据
for _, item := range resp.Data.Data {
// 处理方式
afterType := util.StringToInt(originInput.AfterType)
if afterType == 0 {
afterType = 1 // 默认退款
}
// 售后金额
afterSalesPrice := util.StringToFloat64(originInput.AfterSalesPrice)
if afterSalesPrice == 0 {
afterSalesPrice = item.PlatformPrice
}
orderList = append(orderList, &OrderAfterSaleSupplierData{
SerialNumber: item.SerialNumber,
PlatformName: item.PlatformName,
SignCompany: item.SignCompany,
PlatformProductName: item.PlatformProductName,
PlatformPrice: item.PlatformPrice,
TerminalAccount: item.TerminalAccount,
Status: item.Status,
PlatformProductID: item.PlatformProductID,
PlatformID: item.PlatformID,
SignCompanyName: item.SignCompanyName,
Reason: originInput.AfterSalesReason,
SalePrice: afterSalesPrice,
SaleType: afterType,
})
}
// 追加扩展数据
for _, item := range orderList {
if extItem, ok := resp.Data.ExtData[item.SerialNumber]; ok {
item.IsExistsAfterSale = extItem.IsExistsAfterSale
item.ExecuteTime = extItem.SerialCreateTime
}
}
return orderList, nil
}

View File

@ -1,119 +0,0 @@
package tools
import (
"ai_scheduler/internal/config"
"ai_scheduler/internal/entitys"
"ai_scheduler/internal/pkg/l_request"
"context"
"encoding/json"
"fmt"
)
type ZltxOrderAfterSaleDetailTool struct {
config config.ToolConfig
}
// NewZltxOrderAfterSaleDetailTool 创建售后订单详情工具
func NewZltxOrderAfterSaleDetailTool(config config.ToolConfig) *ZltxOrderAfterSaleDetailTool {
return &ZltxOrderAfterSaleDetailTool{config: config}
}
// Definition 返回工具定义
func (this *ZltxOrderAfterSaleDetailTool) Definition() entitys.ToolDefinition {
return entitys.ToolDefinition{
Type: "function",
Function: entitys.FunctionDef{
Name: this.Name(),
Description: this.Description(),
Parameters: map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
"direct_order_number": map[string]interface{}{
"type": "string",
"description": "售后订单号",
},
},
"required": []string{"direct_order_number"},
},
},
}
}
type ZltxOrderAfterSaleDetailRequest struct {
DirectOrderNumber string `json:"direct_order_number"`
}
type ZltxOrderAfterSaleDetailResponse struct {
Code int `json:"code"`
Data struct {
AfterSaleOrder ZltxOrderAfterSaleDetailData `json:"afterSaleOrder"`
} `json:"data"`
Error string `json:"error"`
}
type ZltxOrderAfterSaleDetailData struct {
SerialNumber string `json:"serialNumber"`
PlatformName string `json:"platformName"`
SignCompany int `json:"signCompany"`
PlatformProductName string `json:"platformProductName"`
PlatformPrice float64 `json:"platformPrice"`
TerminalAccount string `json:"terminalAccount"`
Status int `json:"status"`
PlatformProductID int `json:"platformProductId"`
PlatformID int `json:"platformId"`
SignCompanyName string `json:"signCompanyName"`
ExecuteTime int `json:"executeTime"`
}
func (this *ZltxOrderAfterSaleDetailTool) Execute(ctx context.Context, requireData *entitys.RequireData) error {
var req ZltxOrderAfterSaleDetailRequest
if err := json.Unmarshal([]byte(requireData.Match.Parameters), &req); err != nil {
return err
}
if req.DirectOrderNumber == "" {
return fmt.Errorf("direct_order_number is required")
}
return this.getZltxOrderAfterSaleDetail(ctx, requireData, req.DirectOrderNumber)
}
func (this *ZltxOrderAfterSaleDetailTool) getZltxOrderAfterSaleDetail(ctx context.Context, requireData *entitys.RequireData, directOrderNumber string) error {
//查询订单详情
url := fmt.Sprintf("%s%s", this.config.BaseURL, directOrderNumber)
req := l_request.Request{
Url: url,
Headers: map[string]string{
"Authorization": fmt.Sprintf("Bearer %s", requireData.Auth),
},
Method: "GET",
}
res, err := req.Send()
var resData ZltxOrderAfterSaleDetailResponse
if err != nil {
return err
}
if err := json.Unmarshal(res.Content, &resData); err != nil {
return err
}
if resData.Code != 200 {
return fmt.Errorf("为获取到数据,请检查权限: %s", string(res.Content))
}
jsonByte, err := json.Marshal(resData)
if err != nil {
return err
}
entitys.ResJson(requireData.Ch, this.Name(), string(jsonByte))
return nil
}
func (this *ZltxOrderAfterSaleDetailTool) GetConfig() config.ToolConfig {
return this.config
}
// Name 工具名称
func (this *ZltxOrderAfterSaleDetailTool) Name() string {
return "zltx_order_after_sale_detail"
}
func (this *ZltxOrderAfterSaleDetailTool) Description() string {
return "查询直连天下上游售后订单详情"
}

View File

@ -1,125 +0,0 @@
package tools
import (
"ai_scheduler/internal/config"
"ai_scheduler/internal/entitys"
"ai_scheduler/internal/pkg/l_request"
"context"
"encoding/json"
"fmt"
)
type ZltxOrderAfterSalePreCheckTool struct {
config config.ToolConfig
}
// NewZltxOrderAfterSalePreCheckTool 创建售后订单预检工具
func NewZltxOrderAfterSalePreCheckTool(config config.ToolConfig) *ZltxOrderAfterSalePreCheckTool {
return &ZltxOrderAfterSalePreCheckTool{config: config}
}
// Name 返回工具名称
func (t *ZltxOrderAfterSalePreCheckTool) Name() string {
return "zltxOrderAfterSalePreCheck"
}
func (t *ZltxOrderAfterSalePreCheckTool) Description() string {
return "直连天下售后订单预检工具"
}
func (t *ZltxOrderAfterSalePreCheckTool) Definition() entitys.ToolDefinition {
return entitys.ToolDefinition{
Type: "function",
Function: entitys.FunctionDef{
Name: t.Name(),
Description: t.Description(),
Parameters: map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
"orderType": map[string]interface{}{
"type": "integer",
"description": "售后订单类型",
},
"orderNumber": map[string]interface{}{
"type": "string",
"description": "售后订单号",
},
},
"required": []string{"orderType", "orderNumber"},
},
},
}
}
type ZltxOrderAfterSalePreCheckRequest struct {
OrderType int `json:"orderType"`
OrderNumber string `json:"orderNumber"`
}
type ZltxOrderAfterSalePreCheckResponse struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data struct {
CheckResult bool `json:"checkResult"`
} `json:"data"`
}
type CheckResult struct {
OrderType int `json:"orderType"`
OrderNumber string `json:"orderNumber"`
OrderAmount float64 `json:"orderAmount"`
OrderPrice float64 `json:"orderPrice"`
SignCompany int `json:"signCompany"`
OrderQuantity int `json:"orderQuantity"`
ResellerID int `json:"resellerId"`
ResellerName string `json:"resellerName"`
OurProductID int `json:"ourProductId"`
OurProductTitle string `json:"ourProductTitle"`
Account []string `json:"account"`
Platforms struct {
Num4 string `json:"4"`
} `json:"platforms"`
}
func (t *ZltxOrderAfterSalePreCheckTool) Execute(ctx context.Context, requireData *entitys.RequireData) error {
var req ZltxOrderAfterSalePreCheckRequest
if err := json.Unmarshal([]byte(requireData.Match.Parameters), &req); err != nil {
return err
}
if req.OrderType == 0 || req.OrderNumber == "" {
return fmt.Errorf("orderType and orderNumber are required")
}
return t.checkZltxOrderAfterSalePreCheck(req.OrderType, req.OrderNumber, requireData)
}
func (t *ZltxOrderAfterSalePreCheckTool) checkZltxOrderAfterSalePreCheck(orderType int, orderNumber string, requireData *entitys.RequireData) error {
req := l_request.Request{
Url: t.config.BaseURL,
Headers: map[string]string{
"Authorization": fmt.Sprintf("Bearer %s", requireData.Auth),
},
Method: "POST",
Data: map[string]string{
"orderType": fmt.Sprintf("%d", orderType),
"orderNumber": orderNumber,
},
}
res, err := req.Send()
if err != nil {
return err
}
// 解析响应
var resp ZltxOrderAfterSalePreCheckResponse
if err := json.Unmarshal(res.Content, &resp); err != nil {
return err
}
if resp.Code != 0 {
return fmt.Errorf("check failed: %s", resp.Msg)
}
jsonByte, err := json.Marshal(resp)
if err != nil {
return err
}
entitys.ResJson(requireData.Ch, t.Name(), string(jsonByte))
return nil
}

View File

@ -77,7 +77,6 @@ func (w *BotTool) BugOptimizationSubmit(ctx context.Context, requireData *entity
// 记录 task_id 到 session_id 的映射
w.SetTaskMapping(body.TaskId, requireData.Session)
entitys.ResLog(requireData.Ch, requireData.Match.Index, "问题记录中")
entitys.ResLoading(requireData.Ch, requireData.Match.Index, "问题记录中...")
// 等待异步回调完成再结束
for {
@ -85,8 +84,8 @@ func (w *BotTool) BugOptimizationSubmit(ctx context.Context, requireData *entity
if !ok || sessionID != requireData.Session {
break
}
time.Sleep(time.Second * 2)
entitys.ResLoading(requireData.Ch, requireData.Match.Index, "问题记录中...")
time.Sleep(time.Second)
}
return

View File

@ -17,7 +17,6 @@ type BotTool struct {
llm *utils_ollama.Client
sessionImpl *impl.SessionImpl
taskMap map[string]string // task_id -> session_id
// zltxOrderAfterSaleTool tools.ZltxOrderAfterSaleTool
}
// NewBotTool 创建直连天下订单详情工具

View File

@ -1,72 +1,72 @@
package utils
// import (
// "fmt"
// "github.com/go-kratos/kratos/v2/log"
// "google.golang.org/protobuf/runtime/protoimpl"
// "gopkg.in/yaml.v3"
// "io/fs"
// "os"
// "path/filepath"
// "testing"
// baseconf "trans_hub/base_conf"
// "trans_hub/pkg"
// "trans_hub/pkg/mapstructure"
// )
import (
"fmt"
"github.com/go-kratos/kratos/v2/log"
"google.golang.org/protobuf/runtime/protoimpl"
"gopkg.in/yaml.v3"
"io/fs"
"os"
"path/filepath"
"testing"
baseconf "trans_hub/base_conf"
"trans_hub/pkg"
"trans_hub/pkg/mapstructure"
)
// const SPACE = "public"
// const PORT = 8848
// const User = ""
// const Pass = ""
// const IP = "192.168.110.93"
// const Group = "DEFAULT_GROUP"
// const DataId = "PG_BASE_CONFIG"
const SPACE = "public"
const PORT = 8848
const User = ""
const Pass = ""
const IP = "192.168.110.93"
const Group = "DEFAULT_GROUP"
const DataId = "PG_BASE_CONFIG"
// func TestConfig(t *testing.T) {
// type Nacos struct {
// state protoimpl.MessageState
// sizeCache protoimpl.SizeCache
// unknownFields protoimpl.UnknownFields
// Ip string `protobuf:"bytes,1,opt,name=ip,proto3" json:"ip,omitempty"`
// Port uint64 `protobuf:"varint,2,opt,name=port,proto3" json:"port,omitempty"`
// }
// type Conf struct {
// Nacos *Nacos `protobuf:"bytes,8,opt,name=nacos,proto3" json:"nacos,omitempty"`
// }
// var c Conf
// nc := &baseconf.Nacos{Ip: IP, Port: PORT, Space: SPACE, User: User, Password: Pass}
func TestConfig(t *testing.T) {
type Nacos struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Ip string `protobuf:"bytes,1,opt,name=ip,proto3" json:"ip,omitempty"`
Port uint64 `protobuf:"varint,2,opt,name=port,proto3" json:"port,omitempty"`
}
type Conf struct {
Nacos *Nacos `protobuf:"bytes,8,opt,name=nacos,proto3" json:"nacos,omitempty"`
}
var c Conf
nc := &baseconf.Nacos{Ip: IP, Port: PORT, Space: SPACE, User: User, Password: Pass}
// var s = ServerConfig(nc, Group, DataId)
// err := mapstructure.Decode(s, &c)
// t.Log(s, err)
// }
var s = ServerConfig(nc, Group, DataId)
err := mapstructure.Decode(s, &c)
t.Log(s, err)
}
// func TestMod(t *testing.T) {
// dir := pkg.GetRootPath()
// // 读取目录内容
// err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
// if err != nil {
// return err
// }
// if !d.IsDir() && filepath.Ext(path) == ".yaml" {
// data, err := os.ReadFile(path)
// if err != nil {
// return err
// }
// var result map[string]interface{}
// err = yaml.Unmarshal(data, &result) // 解析YAML到map中使用gopkg.v3的yaml包或其他你选择的版本例如encoding/yaml
// if err != nil {
// return err
// }
// fmt.Printf("File: %s\nContent: %+v\n", path, result)
// }
// return nil
// })
// if err != nil {
// log.Fatal(err)
// }
// }
func TestMod(t *testing.T) {
dir := pkg.GetRootPath()
// 读取目录内容
err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if !d.IsDir() && filepath.Ext(path) == ".yaml" {
data, err := os.ReadFile(path)
if err != nil {
return err
}
var result map[string]interface{}
err = yaml.Unmarshal(data, &result) // 解析YAML到map中使用gopkg.v3的yaml包或其他你选择的版本例如encoding/yaml
if err != nil {
return err
}
fmt.Printf("File: %s\nContent: %+v\n", path, result)
}
return nil
})
if err != nil {
log.Fatal(err)
}
}
// func TestYaml(t *testing.T) {
// t.Log(GetBaseYaml())
// }
func TestYaml(t *testing.T) {
t.Log(GetBaseYaml())
}