结构修改
This commit is contained in:
parent
2edea5f0fb
commit
ad66615c2e
|
@ -75,50 +75,6 @@ func (r *AiRouterBiz) Route(ctx context.Context, req *entitys.ChatRequest) (*ent
|
||||||
|
|
||||||
func (r *AiRouterBiz) RouteWithSocket(c *websocket.Conn, req *entitys.ChatSockRequest) (err error) {
|
func (r *AiRouterBiz) RouteWithSocket(c *websocket.Conn, req *entitys.ChatSockRequest) (err error) {
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
|
||||||
defer cancel()
|
|
||||||
//ch := r.channelPool.Get()
|
|
||||||
ch := make(chan entitys.ResponseData)
|
|
||||||
done := make(chan struct{})
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
defer close(done)
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case v, ok := <-ch:
|
|
||||||
if !ok {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
// 带超时的发送,避免阻塞
|
|
||||||
if err := sendWithTimeout(c, v, 2*time.Second); err != nil {
|
|
||||||
log.Errorf("Send error: %v", err)
|
|
||||||
cancel() // 通知主流程退出
|
|
||||||
return
|
|
||||||
}
|
|
||||||
case <-ctx.Done():
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
defer func() {
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
_ = entitys.MsgSend(c, entitys.ResponseData{
|
|
||||||
Done: false,
|
|
||||||
Content: err.Error(),
|
|
||||||
Type: entitys.ResponseErr,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
_ = entitys.MsgSend(c, entitys.ResponseData{
|
|
||||||
Done: true,
|
|
||||||
Content: "",
|
|
||||||
Type: entitys.ResponseEnd,
|
|
||||||
})
|
|
||||||
//r.channelPool.Put(ch)
|
|
||||||
close(ch)
|
|
||||||
}()
|
|
||||||
|
|
||||||
session := c.Headers("X-Session", "")
|
session := c.Headers("X-Session", "")
|
||||||
if len(session) == 0 {
|
if len(session) == 0 {
|
||||||
return errors.SessionNotFound
|
return errors.SessionNotFound
|
||||||
|
@ -132,6 +88,77 @@ func (r *AiRouterBiz) RouteWithSocket(c *websocket.Conn, req *entitys.ChatSockRe
|
||||||
return errors.KeyNotFound
|
return errors.KeyNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var chat = make([]string, 0)
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
//ch := r.channelPool.Get()
|
||||||
|
ch := make(chan entitys.Response)
|
||||||
|
done := make(chan struct{})
|
||||||
|
go func() {
|
||||||
|
defer func() {
|
||||||
|
close(done)
|
||||||
|
if len(chat) > 0 {
|
||||||
|
|
||||||
|
}
|
||||||
|
var his = []*model.AiChatHi{
|
||||||
|
{
|
||||||
|
SessionID: session,
|
||||||
|
Role: "user",
|
||||||
|
Content: req.Text,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
if len(chat) > 0 {
|
||||||
|
his = append(his, &model.AiChatHi{
|
||||||
|
SessionID: session,
|
||||||
|
Role: "assistant",
|
||||||
|
Content: strings.Join(chat, ""),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
for _, hi := range his {
|
||||||
|
r.hisImpl.Add(hi)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case v, ok := <-ch:
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// 带超时的发送,避免阻塞
|
||||||
|
if err = sendWithTimeout(c, v, 2*time.Second); err != nil {
|
||||||
|
log.Errorf("Send error: %v", err)
|
||||||
|
cancel() // 通知主流程退出
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if v.Type == entitys.ResponseText || v.Type == entitys.ResponseStream {
|
||||||
|
chat = append(chat, v.Content)
|
||||||
|
}
|
||||||
|
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
if err != nil {
|
||||||
|
_ = entitys.MsgSend(c, entitys.Response{
|
||||||
|
|
||||||
|
Content: err.Error(),
|
||||||
|
Type: entitys.ResponseErr,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
_ = entitys.MsgSend(c, entitys.Response{
|
||||||
|
|
||||||
|
Content: "",
|
||||||
|
Type: entitys.ResponseEnd,
|
||||||
|
})
|
||||||
|
|
||||||
|
//r.channelPool.Put(ch)
|
||||||
|
close(ch)
|
||||||
|
}()
|
||||||
|
|
||||||
sysInfo, err := r.getSysInfo(key)
|
sysInfo, err := r.getSysInfo(key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.SysNotFound
|
return errors.SysNotFound
|
||||||
|
@ -151,8 +178,8 @@ func (r *AiRouterBiz) RouteWithSocket(c *websocket.Conn, req *entitys.ChatSockRe
|
||||||
|
|
||||||
AgentClient := r.utilAgent.Get()
|
AgentClient := r.utilAgent.Get()
|
||||||
|
|
||||||
ch <- entitys.ResponseData{
|
ch <- entitys.Response{
|
||||||
Done: false,
|
Index: "",
|
||||||
Content: "准备意图识别",
|
Content: "准备意图识别",
|
||||||
Type: entitys.ResponseLog,
|
Type: entitys.ResponseLog,
|
||||||
}
|
}
|
||||||
|
@ -165,46 +192,21 @@ func (r *AiRouterBiz) RouteWithSocket(c *websocket.Conn, req *entitys.ChatSockRe
|
||||||
resMsg := match.Choices[0].Content
|
resMsg := match.Choices[0].Content
|
||||||
|
|
||||||
r.utilAgent.Put(AgentClient)
|
r.utilAgent.Put(AgentClient)
|
||||||
ch <- entitys.ResponseData{
|
ch <- entitys.Response{
|
||||||
Done: false,
|
Index: "",
|
||||||
Content: resMsg,
|
Content: resMsg,
|
||||||
Type: entitys.ResponseLog,
|
Type: entitys.ResponseLog,
|
||||||
}
|
}
|
||||||
ch <- entitys.ResponseData{
|
ch <- entitys.Response{
|
||||||
Done: false,
|
Index: "",
|
||||||
Content: "意图识别结束",
|
Content: "意图识别结束",
|
||||||
Type: entitys.ResponseLog,
|
Type: entitys.ResponseLog,
|
||||||
}
|
}
|
||||||
//for i := 1; i < 10; i++ {
|
|
||||||
// ch <- entitys.ResponseData{
|
|
||||||
// Done: false,
|
|
||||||
// Content: fmt.Sprintf("%d", i),
|
|
||||||
// Type: entitys.ResponseLog,
|
|
||||||
// }
|
|
||||||
// time.Sleep(1 * time.Second)
|
|
||||||
//}
|
|
||||||
//return
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("LLM error: %v", err)
|
log.Errorf("LLM error: %v", err)
|
||||||
return errors.SystemError
|
return errors.SystemError
|
||||||
}
|
}
|
||||||
|
|
||||||
//msg, err := r.ollama.ToolSelect(ctx, r.getPromptOllama(sysInfo, history, req.Text), []api.Tool{})
|
|
||||||
//if err != nil {
|
|
||||||
// return
|
|
||||||
//}
|
|
||||||
//resMsg := msg.Message.Content
|
|
||||||
select {
|
|
||||||
case ch <- entitys.ResponseData{
|
|
||||||
Done: false,
|
|
||||||
Content: resMsg,
|
|
||||||
Type: entitys.ResponseLog,
|
|
||||||
}:
|
|
||||||
case <-ctx.Done():
|
|
||||||
return ctx.Err()
|
|
||||||
}
|
|
||||||
|
|
||||||
var matchJson entitys.Match
|
var matchJson entitys.Match
|
||||||
if err := json.Unmarshal([]byte(resMsg), &matchJson); err != nil {
|
if err := json.Unmarshal([]byte(resMsg), &matchJson); err != nil {
|
||||||
return errors.SystemError
|
return errors.SystemError
|
||||||
|
@ -218,7 +220,7 @@ func (r *AiRouterBiz) RouteWithSocket(c *websocket.Conn, req *entitys.ChatSockRe
|
||||||
}
|
}
|
||||||
|
|
||||||
// 辅助函数:带超时的 WebSocket 发送
|
// 辅助函数:带超时的 WebSocket 发送
|
||||||
func sendWithTimeout(c *websocket.Conn, data entitys.ResponseData, timeout time.Duration) error {
|
func sendWithTimeout(c *websocket.Conn, data entitys.Response, timeout time.Duration) error {
|
||||||
sendCtx, cancel := context.WithTimeout(context.Background(), timeout)
|
sendCtx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
|
@ -234,9 +236,9 @@ func sendWithTimeout(c *websocket.Conn, data entitys.ResponseData, timeout time.
|
||||||
return sendCtx.Err()
|
return sendCtx.Err()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
func (r *AiRouterBiz) handleOtherTask(c *websocket.Conn, ch chan entitys.ResponseData, matchJson *entitys.Match) (err error) {
|
func (r *AiRouterBiz) handleOtherTask(c *websocket.Conn, ch chan entitys.Response, matchJson *entitys.Match) (err error) {
|
||||||
ch <- entitys.ResponseData{
|
ch <- entitys.Response{
|
||||||
Done: false,
|
Index: "",
|
||||||
Content: matchJson.Reasoning,
|
Content: matchJson.Reasoning,
|
||||||
Type: entitys.ResponseText,
|
Type: entitys.ResponseText,
|
||||||
}
|
}
|
||||||
|
@ -244,11 +246,11 @@ func (r *AiRouterBiz) handleOtherTask(c *websocket.Conn, ch chan entitys.Respons
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *AiRouterBiz) handleMatch(ctx context.Context, c *websocket.Conn, ch chan entitys.ResponseData, matchJson *entitys.Match, tasks []model.AiTask, sysInfo model.AiSy) (err error) {
|
func (r *AiRouterBiz) handleMatch(ctx context.Context, c *websocket.Conn, ch chan entitys.Response, matchJson *entitys.Match, tasks []model.AiTask, sysInfo model.AiSy) (err error) {
|
||||||
|
|
||||||
if !matchJson.IsMatch {
|
if !matchJson.IsMatch {
|
||||||
ch <- entitys.ResponseData{
|
ch <- entitys.Response{
|
||||||
Done: false,
|
Index: "",
|
||||||
Content: matchJson.Reasoning,
|
Content: matchJson.Reasoning,
|
||||||
Type: entitys.ResponseText,
|
Type: entitys.ResponseText,
|
||||||
}
|
}
|
||||||
|
@ -277,7 +279,7 @@ func (r *AiRouterBiz) handleMatch(ctx context.Context, c *websocket.Conn, ch cha
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *AiRouterBiz) handleTask(channel chan entitys.ResponseData, c *websocket.Conn, matchJson *entitys.Match, task *model.AiTask) (err error) {
|
func (r *AiRouterBiz) handleTask(channel chan entitys.Response, c *websocket.Conn, matchJson *entitys.Match, task *model.AiTask) (err error) {
|
||||||
|
|
||||||
var configData entitys.ConfigDataTool
|
var configData entitys.ConfigDataTool
|
||||||
err = json.Unmarshal([]byte(task.Config), &configData)
|
err = json.Unmarshal([]byte(task.Config), &configData)
|
||||||
|
@ -293,7 +295,7 @@ func (r *AiRouterBiz) handleTask(channel chan entitys.ResponseData, c *websocket
|
||||||
}
|
}
|
||||||
|
|
||||||
// 知识库
|
// 知识库
|
||||||
func (r *AiRouterBiz) handleKnowle(channel chan entitys.ResponseData, c *websocket.Conn, matchJson *entitys.Match, task *model.AiTask, sysInfo model.AiSy) (err error) {
|
func (r *AiRouterBiz) handleKnowle(channel chan entitys.Response, c *websocket.Conn, matchJson *entitys.Match, task *model.AiTask, sysInfo model.AiSy) (err error) {
|
||||||
|
|
||||||
var (
|
var (
|
||||||
configData entitys.ConfigDataTool
|
configData entitys.ConfigDataTool
|
||||||
|
@ -376,7 +378,7 @@ func (r *AiRouterBiz) handleKnowle(channel chan entitys.ResponseData, c *websock
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *AiRouterBiz) handleApiTask(channels chan entitys.ResponseData, c *websocket.Conn, matchJson *entitys.Match, task *model.AiTask) (err error) {
|
func (r *AiRouterBiz) handleApiTask(channels chan entitys.Response, c *websocket.Conn, matchJson *entitys.Match, task *model.AiTask) (err error) {
|
||||||
var (
|
var (
|
||||||
request l_request.Request
|
request l_request.Request
|
||||||
auth = c.Headers("X-Authorization", "")
|
auth = c.Headers("X-Authorization", "")
|
||||||
|
|
|
@ -6,27 +6,33 @@ import (
|
||||||
"github.com/gofiber/websocket/v2"
|
"github.com/gofiber/websocket/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Response string
|
type ResponseType string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
ResponseJson Response = "json"
|
ResponseJson ResponseType = "json"
|
||||||
ResponseLoading Response = "loading"
|
ResponseLoading ResponseType = "loading"
|
||||||
ResponseEnd Response = "end"
|
ResponseEnd ResponseType = "end"
|
||||||
ResponseStream Response = "stream"
|
ResponseStream ResponseType = "stream"
|
||||||
ResponseText Response = "txt"
|
ResponseText ResponseType = "txt"
|
||||||
ResponseImg Response = "img"
|
ResponseImg ResponseType = "img"
|
||||||
ResponseFile Response = "file"
|
ResponseFile ResponseType = "file"
|
||||||
ResponseErr Response = "error"
|
ResponseErr ResponseType = "error"
|
||||||
ResponseLog Response = "log"
|
ResponseLog ResponseType = "log"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ResponseData struct {
|
type ResponseData struct {
|
||||||
Done bool
|
Done bool
|
||||||
Content string
|
Content string
|
||||||
Type Response
|
Type ResponseType
|
||||||
}
|
}
|
||||||
|
|
||||||
func MsgSet(msgType Response, msg string, done bool) []byte {
|
type Response struct {
|
||||||
|
Content string
|
||||||
|
Type ResponseType
|
||||||
|
Index string
|
||||||
|
}
|
||||||
|
|
||||||
|
func MsgSet(msgType ResponseType, msg string, done bool) []byte {
|
||||||
jsonByte, err := json.Marshal(ResponseData{
|
jsonByte, err := json.Marshal(ResponseData{
|
||||||
Done: done,
|
Done: done,
|
||||||
Content: msg,
|
Content: msg,
|
||||||
|
@ -39,7 +45,7 @@ func MsgSet(msgType Response, msg string, done bool) []byte {
|
||||||
return jsonByte
|
return jsonByte
|
||||||
}
|
}
|
||||||
|
|
||||||
func MsgSend(c *websocket.Conn, msg ResponseData) error {
|
func MsgSend(c *websocket.Conn, msg Response) error {
|
||||||
jsonByte, _ := json.Marshal(msg)
|
jsonByte, _ := json.Marshal(msg)
|
||||||
|
|
||||||
return c.WriteMessage(websocket.TextMessage, jsonByte)
|
return c.WriteMessage(websocket.TextMessage, jsonByte)
|
||||||
|
|
|
@ -67,7 +67,7 @@ type Tool interface {
|
||||||
Name() string
|
Name() string
|
||||||
Description() string
|
Description() string
|
||||||
Definition() ToolDefinition
|
Definition() ToolDefinition
|
||||||
Execute(channel chan ResponseData, c *websocket.Conn, args json.RawMessage) error
|
Execute(channel chan Response, c *websocket.Conn, args json.RawMessage) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type ConfigDataHttp struct {
|
type ConfigDataHttp struct {
|
||||||
|
|
|
@ -60,7 +60,7 @@ func (c *Client) ToolSelect(ctx context.Context, messages []api.Message, tools [
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) ChatStream(ctx context.Context, ch chan entitys.ResponseData, messages []api.Message) (err error) {
|
func (c *Client) ChatStream(ctx context.Context, ch chan entitys.Response, messages []api.Message, index string) (err error) {
|
||||||
// 构建聊天请求
|
// 构建聊天请求
|
||||||
req := &api.ChatRequest{
|
req := &api.ChatRequest{
|
||||||
Model: c.config.Model,
|
Model: c.config.Model,
|
||||||
|
@ -74,8 +74,8 @@ func (c *Client) ChatStream(ctx context.Context, ch chan entitys.ResponseData, m
|
||||||
defer w.Done()
|
defer w.Done()
|
||||||
err = c.client.Chat(ctx, req, func(resp api.ChatResponse) error {
|
err = c.client.Chat(ctx, req, func(resp api.ChatResponse) error {
|
||||||
if resp.Message.Content != "" {
|
if resp.Message.Content != "" {
|
||||||
ch <- entitys.ResponseData{
|
ch <- entitys.Response{
|
||||||
Done: false,
|
Index: index,
|
||||||
Content: resp.Message.Content,
|
Content: resp.Message.Content,
|
||||||
Type: entitys.ResponseStream,
|
Type: entitys.ResponseStream,
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,10 +7,11 @@ import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gofiber/fiber/v2/log"
|
|
||||||
"github.com/gofiber/websocket/v2"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/gofiber/fiber/v2/log"
|
||||||
|
"github.com/gofiber/websocket/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
// 知识库工具
|
// 知识库工具
|
||||||
|
@ -59,7 +60,7 @@ func (k *KnowledgeBaseTool) Definition() entitys.ToolDefinition {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Execute 执行知识库查询
|
// Execute 执行知识库查询
|
||||||
func (k *KnowledgeBaseTool) Execute(channel chan entitys.ResponseData, c *websocket.Conn, args json.RawMessage) error {
|
func (k *KnowledgeBaseTool) Execute(channel chan entitys.Response, c *websocket.Conn, args json.RawMessage) error {
|
||||||
|
|
||||||
var params KnowledgeBaseRequest
|
var params KnowledgeBaseRequest
|
||||||
if err := json.Unmarshal(args, ¶ms); err != nil {
|
if err := json.Unmarshal(args, ¶ms); err != nil {
|
||||||
|
@ -93,14 +94,14 @@ type MsgContent struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 解析知识库响应内容,并把通过channel结果返回
|
// 解析知识库响应内容,并把通过channel结果返回
|
||||||
func msgContentParse(input string, channel chan entitys.ResponseData) (msgContent MsgContent, err error) {
|
func (this *KnowledgeBaseTool) msgContentParse(input string, channel chan entitys.Response) (msgContent MsgContent, err error) {
|
||||||
err = json.Unmarshal([]byte(input), &msgContent)
|
err = json.Unmarshal([]byte(input), &msgContent)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = fmt.Errorf("unmarshal input failed: %w", err)
|
err = fmt.Errorf("unmarshal input failed: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
channel <- entitys.ResponseData{
|
channel <- entitys.Response{
|
||||||
Done: msgContent.Done,
|
Index: this.Name(),
|
||||||
Content: msgContent.Content,
|
Content: msgContent.Content,
|
||||||
Type: entitys.ResponseStream,
|
Type: entitys.ResponseStream,
|
||||||
}
|
}
|
||||||
|
@ -109,7 +110,7 @@ func msgContentParse(input string, channel chan entitys.ResponseData) (msgConten
|
||||||
}
|
}
|
||||||
|
|
||||||
// 请求知识库聊天
|
// 请求知识库聊天
|
||||||
func (this *KnowledgeBaseTool) chat(channel chan entitys.ResponseData, c *websocket.Conn, param KnowledgeBaseRequest) (err error) {
|
func (this *KnowledgeBaseTool) chat(channel chan entitys.Response, c *websocket.Conn, param KnowledgeBaseRequest) (err error) {
|
||||||
|
|
||||||
req := l_request.Request{
|
req := l_request.Request{
|
||||||
Method: "post",
|
Method: "post",
|
||||||
|
@ -136,7 +137,7 @@ func (this *KnowledgeBaseTool) chat(channel chan entitys.ResponseData, c *websoc
|
||||||
}
|
}
|
||||||
defer rsp.Body.Close()
|
defer rsp.Body.Close()
|
||||||
|
|
||||||
err = connectAndReadSSE(rsp, channel)
|
err = this.connectAndReadSSE(rsp, channel)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -145,7 +146,7 @@ func (this *KnowledgeBaseTool) chat(channel chan entitys.ResponseData, c *websoc
|
||||||
}
|
}
|
||||||
|
|
||||||
// 连接 SSE 并读取数据
|
// 连接 SSE 并读取数据
|
||||||
func connectAndReadSSE(resp *http.Response, channel chan entitys.ResponseData) error {
|
func (this *KnowledgeBaseTool) connectAndReadSSE(resp *http.Response, channel chan entitys.Response) error {
|
||||||
|
|
||||||
// 验证响应状态和格式
|
// 验证响应状态和格式
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
@ -165,7 +166,7 @@ func connectAndReadSSE(resp *http.Response, channel chan entitys.ResponseData) e
|
||||||
if line == "" {
|
if line == "" {
|
||||||
// 空行表示一条消息结束,处理当前消息
|
// 空行表示一条消息结束,处理当前消息
|
||||||
if currentMsg.Data != "" || currentMsg.Event != "" || currentMsg.ID != "" {
|
if currentMsg.Data != "" || currentMsg.Event != "" || currentMsg.ID != "" {
|
||||||
_, err := msgContentParse(currentMsg.Data, channel)
|
_, err := this.msgContentParse(currentMsg.Data, channel)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("msgContentParse failed: %w", err)
|
return fmt.Errorf("msgContentParse failed: %w", err)
|
||||||
}
|
}
|
||||||
|
@ -200,7 +201,7 @@ func connectAndReadSSE(resp *http.Response, channel chan entitys.ResponseData) e
|
||||||
|
|
||||||
// 处理最后一条未结束的消息(无结尾空行)
|
// 处理最后一条未结束的消息(无结尾空行)
|
||||||
if currentMsg.Data != "" || currentMsg.Event != "" || currentMsg.ID != "" {
|
if currentMsg.Data != "" || currentMsg.Event != "" || currentMsg.ID != "" {
|
||||||
_, err := msgContentParse(currentMsg.Data, channel)
|
_, err := this.msgContentParse(currentMsg.Data, channel)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("msgContentParse failed: %w", err)
|
return fmt.Errorf("msgContentParse failed: %w", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -100,7 +100,7 @@ func (m *Manager) GetToolDefinitions(caller constants.Caller) []entitys.ToolDefi
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExecuteTool 执行工具
|
// ExecuteTool 执行工具
|
||||||
func (m *Manager) ExecuteTool(channel chan entitys.ResponseData, c *websocket.Conn, name string, args json.RawMessage) error {
|
func (m *Manager) ExecuteTool(channel chan entitys.Response, c *websocket.Conn, name string, args json.RawMessage) error {
|
||||||
tool, exists := m.GetTool(name)
|
tool, exists := m.GetTool(name)
|
||||||
if !exists {
|
if !exists {
|
||||||
return fmt.Errorf("tool not found: %s", name)
|
return fmt.Errorf("tool not found: %s", name)
|
||||||
|
|
|
@ -81,7 +81,7 @@ type ZltxOrderDetailData struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Execute 执行直连天下订单详情查询
|
// Execute 执行直连天下订单详情查询
|
||||||
func (w *ZltxOrderDetailTool) Execute(channel chan entitys.ResponseData, c *websocket.Conn, args json.RawMessage) error {
|
func (w *ZltxOrderDetailTool) Execute(channel chan entitys.Response, c *websocket.Conn, args json.RawMessage) error {
|
||||||
var req ZltxOrderDetailRequest
|
var req ZltxOrderDetailRequest
|
||||||
if err := json.Unmarshal(args, &req); err != nil {
|
if err := json.Unmarshal(args, &req); err != nil {
|
||||||
return fmt.Errorf("invalid zltxOrderDetail request: %w", err)
|
return fmt.Errorf("invalid zltxOrderDetail request: %w", err)
|
||||||
|
@ -96,7 +96,7 @@ func (w *ZltxOrderDetailTool) Execute(channel chan entitys.ResponseData, c *webs
|
||||||
}
|
}
|
||||||
|
|
||||||
// getMockZltxOrderDetail 获取模拟直连天下订单详情数据
|
// getMockZltxOrderDetail 获取模拟直连天下订单详情数据
|
||||||
func (w *ZltxOrderDetailTool) getZltxOrderDetail(ch chan entitys.ResponseData, c *websocket.Conn, number string) (err error) {
|
func (w *ZltxOrderDetailTool) getZltxOrderDetail(ch chan entitys.Response, c *websocket.Conn, number string) (err error) {
|
||||||
//查询订单详情
|
//查询订单详情
|
||||||
var auth string
|
var auth string
|
||||||
if c != nil {
|
if c != nil {
|
||||||
|
@ -129,14 +129,14 @@ func (w *ZltxOrderDetailTool) getZltxOrderDetail(ch chan entitys.ResponseData, c
|
||||||
if err = json.Unmarshal(res.Content, &resData); err != nil {
|
if err = json.Unmarshal(res.Content, &resData); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ch <- entitys.ResponseData{
|
ch <- entitys.Response{
|
||||||
Done: false,
|
Index: w.Name(),
|
||||||
Content: res.Text,
|
Content: res.Text,
|
||||||
Type: entitys.ResponseJson,
|
Type: entitys.ResponseJson,
|
||||||
}
|
}
|
||||||
if resData.Data.Direct != nil && resData.Data.Direct["needAi"].(bool) {
|
if resData.Data.Direct != nil && resData.Data.Direct["needAi"].(bool) {
|
||||||
ch <- entitys.ResponseData{
|
ch <- entitys.Response{
|
||||||
Done: false,
|
Index: w.Name(),
|
||||||
Content: "正在分析订单日志",
|
Content: "正在分析订单日志",
|
||||||
Type: entitys.ResponseLoading,
|
Type: entitys.ResponseLoading,
|
||||||
}
|
}
|
||||||
|
@ -173,7 +173,7 @@ func (w *ZltxOrderDetailTool) getZltxOrderDetail(ch chan entitys.ResponseData, c
|
||||||
Role: "user",
|
Role: "user",
|
||||||
Content: fmt.Sprintf("订单日志:%s", string(dataJson)),
|
Content: fmt.Sprintf("订单日志:%s", string(dataJson)),
|
||||||
},
|
},
|
||||||
})
|
}, w.Name())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("订单日志解析失败:%s", err)
|
return fmt.Errorf("订单日志解析失败:%s", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -67,7 +67,7 @@ type ZltxOrderDirectLogData struct {
|
||||||
Data map[string]interface{} `json:"data"`
|
Data map[string]interface{} `json:"data"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *ZltxOrderLogTool) Execute(channel chan entitys.ResponseData, c *websocket.Conn, args json.RawMessage) error {
|
func (t *ZltxOrderLogTool) Execute(channel chan entitys.Response, c *websocket.Conn, args json.RawMessage) error {
|
||||||
var req ZltxOrderLogRequest
|
var req ZltxOrderLogRequest
|
||||||
if err := json.Unmarshal(args, &req); err != nil {
|
if err := json.Unmarshal(args, &req); err != nil {
|
||||||
return fmt.Errorf("invalid zltxOrderLog request: %w", err)
|
return fmt.Errorf("invalid zltxOrderLog request: %w", err)
|
||||||
|
@ -78,7 +78,7 @@ func (t *ZltxOrderLogTool) Execute(channel chan entitys.ResponseData, c *websock
|
||||||
return t.getZltxOrderLog(channel, c, req.OrderNumber, req.SerialNumber)
|
return t.getZltxOrderLog(channel, c, req.OrderNumber, req.SerialNumber)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *ZltxOrderLogTool) getZltxOrderLog(channel chan entitys.ResponseData, c *websocket.Conn, orderNumber, serialNumber string) (err error) {
|
func (t *ZltxOrderLogTool) getZltxOrderLog(channel chan entitys.Response, c *websocket.Conn, orderNumber, serialNumber string) (err error) {
|
||||||
//查询订单详情
|
//查询订单详情
|
||||||
var auth string
|
var auth string
|
||||||
if c != nil {
|
if c != nil {
|
||||||
|
@ -106,16 +106,11 @@ func (t *ZltxOrderLogTool) getZltxOrderLog(channel chan entitys.ResponseData, c
|
||||||
if err = json.Unmarshal(res.Content, &resData); err != nil {
|
if err = json.Unmarshal(res.Content, &resData); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if c != nil {
|
channel <- entitys.Response{
|
||||||
_ = c.WriteMessage(websocket.TextMessage, res.Content)
|
Index: t.Name(),
|
||||||
return
|
|
||||||
} else {
|
|
||||||
channel <- entitys.ResponseData{
|
|
||||||
Done: false,
|
|
||||||
Content: res.Text,
|
Content: res.Text,
|
||||||
Type: entitys.ResponseJson,
|
Type: entitys.ResponseJson,
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -52,7 +52,7 @@ type ZltxProductRequest struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (z ZltxProductTool) Execute(channel chan entitys.ResponseData, c *websocket.Conn, args json.RawMessage) error {
|
func (z ZltxProductTool) Execute(channel chan entitys.Response, c *websocket.Conn, args json.RawMessage) error {
|
||||||
var req ZltxProductRequest
|
var req ZltxProductRequest
|
||||||
if err := json.Unmarshal(args, &req); err != nil {
|
if err := json.Unmarshal(args, &req); err != nil {
|
||||||
return fmt.Errorf("invalid zltxProduct request: %w", err)
|
return fmt.Errorf("invalid zltxProduct request: %w", err)
|
||||||
|
@ -132,7 +132,7 @@ type ZltxProductData struct {
|
||||||
PlatformProductList interface{} `json:"platform_product_list"`
|
PlatformProductList interface{} `json:"platform_product_list"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (z ZltxProductTool) getZltxProduct(channel chan entitys.ResponseData, c *websocket.Conn, id string, name string) error {
|
func (z ZltxProductTool) getZltxProduct(channel chan entitys.Response, c *websocket.Conn, id string, name string) error {
|
||||||
var auth string
|
var auth string
|
||||||
if c != nil {
|
if c != nil {
|
||||||
auth = c.Headers("X-Authorization", "")
|
auth = c.Headers("X-Authorization", "")
|
||||||
|
@ -196,8 +196,8 @@ func (z ZltxProductTool) getZltxProduct(channel chan entitys.ResponseData, c *we
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
channel <- entitys.ResponseData{
|
channel <- entitys.Response{
|
||||||
Done: false,
|
Index: z.Name(),
|
||||||
Content: string(marshal),
|
Content: string(marshal),
|
||||||
Type: entitys.ResponseJson,
|
Type: entitys.ResponseJson,
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,7 +46,7 @@ type ZltxOrderStatisticsRequest struct {
|
||||||
Number string `json:"number"`
|
Number string `json:"number"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (z ZltxOrderStatisticsTool) Execute(channel chan entitys.ResponseData, c *websocket.Conn, args json.RawMessage) error {
|
func (z ZltxOrderStatisticsTool) Execute(channel chan entitys.Response, c *websocket.Conn, args json.RawMessage) error {
|
||||||
var req ZltxOrderStatisticsRequest
|
var req ZltxOrderStatisticsRequest
|
||||||
if err := json.Unmarshal(args, &req); err != nil {
|
if err := json.Unmarshal(args, &req); err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -74,7 +74,7 @@ type ZltxOrderStatisticsData struct {
|
||||||
Total int `json:"total"`
|
Total int `json:"total"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (z ZltxOrderStatisticsTool) getZltxOrderStatistics(channel chan entitys.ResponseData, c *websocket.Conn, number string) error {
|
func (z ZltxOrderStatisticsTool) getZltxOrderStatistics(channel chan entitys.Response, c *websocket.Conn, number string) error {
|
||||||
//查询订单详情
|
//查询订单详情
|
||||||
var auth string
|
var auth string
|
||||||
if c != nil {
|
if c != nil {
|
||||||
|
@ -102,16 +102,11 @@ func (z ZltxOrderStatisticsTool) getZltxOrderStatistics(channel chan entitys.Res
|
||||||
if resData.Code != 200 {
|
if resData.Code != 200 {
|
||||||
return fmt.Errorf("zltx order statistics error: %s", resData.Error)
|
return fmt.Errorf("zltx order statistics error: %s", resData.Error)
|
||||||
}
|
}
|
||||||
if c != nil {
|
channel <- entitys.Response{
|
||||||
_ = c.WriteMessage(websocket.TextMessage, res.Content)
|
Index: z.Name(),
|
||||||
return nil
|
|
||||||
} else {
|
|
||||||
channel <- entitys.ResponseData{
|
|
||||||
Done: false,
|
|
||||||
Content: res.Text,
|
Content: res.Text,
|
||||||
Type: entitys.ResponseJson,
|
Type: entitys.ResponseJson,
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue