ai_scheduler/internal/biz/router_test.go

115 lines
3.8 KiB
Go

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"
"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)
}
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.ResponseData, 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.ResponseData, 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.ResponseData, 5)
defer close(ch)
err := routerBiz.handleTask(ch, nil, &entitys.Match{Index: "order_diagnosis", Parameters: `{"name":"优酷周卡","serial_number":"822979421979938817"}`}, &model.AiTask{Config: `{"tool": "zltxProduct", "param": {"type": "object", "optional": [], "required": ["order_number"], "properties": {"order_number": {"type": "string", "description": "订单编号/流水号"}}}}`})
t.Log(err)
}
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()
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)
return routerBiz
}
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
}
// 向上查找父目录
parent := filepath.Dir(dir)
if parent == dir {
break // 到达根目录,未找到
}
dir = parent
}
return "", fmt.Errorf("go.mod not found in current directory or parents")
}