Skip to content

Commit

Permalink
Beta74 (#73)
Browse files Browse the repository at this point in the history
* beta1

* beta2

* beta3

* beta4

* beta5

* beta6

* beta7

* beta8

* beta9

* beta10

* beta11

* beta12

* beta13

* beta14

* beta15

* beta16

* beta16

* beta19

* beta20

* beta21

* beta22

* beta23

* beta24

* beta25

* beta27

* beta28

* beta29

* beta30

* beta31

* beta33

* beta34

* beta35

* beta36

* beta37

* beta38

* beta39

* beta40

* beta41

* beta42

* beta43

* beta44

* beta45

* beta45

* beta46

* beat48

* beta49

* beta50

* beta51

* beta52

* beta53

* beta54

* beta55

* beta57

* beta58

* beta59

* beta61

* beta62

* beta63

* beta63

* beta64

* beta65

* beta66

* beta67

* beta70

* beta71

* beta72

* beta72

* beta74
  • Loading branch information
Hoshinonyaruko authored Apr 19, 2024
1 parent a2f056e commit 1f68210
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 15 deletions.
22 changes: 16 additions & 6 deletions applogic/gensokyo.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"math/rand"
"net/http"
"net/url"
"strconv"
"strings"

Expand Down Expand Up @@ -359,14 +360,23 @@ func (app *App) GensokyoHandler(w http.ResponseWriter, r *http.Request) {
port := config.GetPort()
portStr := fmtf.Sprintf(":%d", port)

var url string
//如果promptstr不等于空,添加到参数中
// 初始化URL
baseURL := "http://127.0.0.1" + portStr + "/conversation"

// 使用net/url包来构建和编码URL
urlParams := url.Values{}
if promptstr != "" {
url = "http://127.0.0.1" + portStr + "/conversation?prompt=" + promptstr
} else {
url = "http://127.0.0.1" + portStr + "/conversation"
urlParams.Add("prompt", promptstr)
}

// 将查询参数编码后附加到基本URL上
fullURL := baseURL
if len(urlParams) > 0 {
fullURL += "?" + urlParams.Encode()
}

fmtf.Printf("Generated URL:%v", fullURL)

// 请求模型还是使用原文请求
requestmsg := message.Message.(string)

Expand Down Expand Up @@ -399,7 +409,7 @@ func (app *App) GensokyoHandler(w http.ResponseWriter, r *http.Request) {
return
}

resp, err := http.Post(url, "application/json", bytes.NewBuffer(requestBody))
resp, err := http.Post(fullURL, "application/json", bytes.NewBuffer(requestBody))
if err != nil {
fmtf.Printf("Error sending request to conversation interface: %v\n", err)
return
Expand Down
11 changes: 10 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ func GetWenxinAccessToken() string {
func GetWenxinApiPath(options ...string) string {
mu.Lock()
defer mu.Unlock()

return getWenxinApiPathInternal(options...)
}

Expand Down Expand Up @@ -1133,3 +1132,13 @@ func GetWSServerToken() string {
}
return ""
}

// 获取PathToken
func GetPathToken() string {
mu.Lock()
defer mu.Unlock()
if instance != nil {
return instance.Settings.PathToken
}
return ""
}
1 change: 1 addition & 0 deletions structs/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ type Settings struct {
UseSse bool `yaml:"useSse"`
Port int `yaml:"port"`
HttpPath string `yaml:"path"`
PathToken string `yaml:"pathToken"`
SystemPrompt []string `yaml:"systemPrompt"`
IPWhiteList []string `yaml:"iPWhiteList"`
ApiType int `yaml:"apiType"`
Expand Down
78 changes: 70 additions & 8 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"math/rand"
"net/http"
"net/url"
"os"
"regexp"
"strconv"
Expand Down Expand Up @@ -164,7 +165,22 @@ func SendGroupMessage(groupID int64, userID int64, message string, selfid string
baseURL := config.GetHttpPath() // 假设config.getHttpPath()返回基础URL

// 构建完整的URL
url := baseURL + "/send_group_msg"
baseURL = baseURL + "/send_group_msg"

// 获取PathToken并检查其是否为空
pathToken := config.GetPathToken()
// 使用net/url包构建URL
u, err := url.Parse(baseURL)
if err != nil {
panic("URL parsing failed: " + err.Error())
}

// 添加access_token参数
query := u.Query()
if pathToken != "" {
query.Set("access_token", pathToken)
}
u.RawQuery = query.Encode()

if config.GetSensitiveModeType() == 1 {
message = acnode.CheckWordOUT(message)
Expand All @@ -182,7 +198,7 @@ func SendGroupMessage(groupID int64, userID int64, message string, selfid string
}

// 发送POST请求
resp, err := http.Post(url, "application/json", bytes.NewBuffer(requestBody))
resp, err := http.Post(u.String(), "application/json", bytes.NewBuffer(requestBody))
if err != nil {
return fmtf.Errorf("failed to send POST request: %w", err)
}
Expand Down Expand Up @@ -234,7 +250,22 @@ func SendPrivateMessage(UserID int64, message string, selfid string) error {
baseURL := config.GetHttpPath() // 假设config.getHttpPath()返回基础URL

// 构建完整的URL
url := baseURL + "/send_private_msg"
baseURL = baseURL + "/send_private_msg"

// 获取PathToken并检查其是否为空
pathToken := config.GetPathToken()
// 使用net/url包构建URL
u, err := url.Parse(baseURL)
if err != nil {
panic("URL parsing failed: " + err.Error())
}

// 添加access_token参数
query := u.Query()
if pathToken != "" {
query.Set("access_token", pathToken)
}
u.RawQuery = query.Encode()

if config.GetSensitiveModeType() == 1 {
message = acnode.CheckWordOUT(message)
Expand All @@ -251,7 +282,7 @@ func SendPrivateMessage(UserID int64, message string, selfid string) error {
}

// 发送POST请求
resp, err := http.Post(url, "application/json", bytes.NewBuffer(requestBody))
resp, err := http.Post(u.String(), "application/json", bytes.NewBuffer(requestBody))
if err != nil {
return fmtf.Errorf("failed to send POST request: %w", err)
}
Expand Down Expand Up @@ -289,7 +320,23 @@ func SendPrivateMessageSSE(UserID int64, message structs.InterfaceBody) error {
baseURL := config.GetHttpPath() // 假设config.GetHttpPath()返回基础URL

// 构建完整的URL
url := baseURL + "/send_private_msg_sse"
baseURL = baseURL + "/send_private_msg_sse"

// 获取PathToken并检查其是否为空
pathToken := config.GetPathToken()
// 使用net/url包构建URL
u, err := url.Parse(baseURL)
if err != nil {
panic("URL parsing failed: " + err.Error())
}

// 添加access_token参数
query := u.Query()
if pathToken != "" {
query.Set("access_token", pathToken)
}
u.RawQuery = query.Encode()

// 调试用的
if config.GetPrintHanming() {
fmtf.Printf("流式信息替换前:%v", message.Content)
Expand Down Expand Up @@ -324,7 +371,7 @@ func SendPrivateMessageSSE(UserID int64, message structs.InterfaceBody) error {
}

// 发送POST请求
resp, err := http.Post(url, "application/json", bytes.NewBuffer(requestBody))
resp, err := http.Post(u.String(), "application/json", bytes.NewBuffer(requestBody))
if err != nil {
return fmtf.Errorf("failed to send POST request: %w", err)
}
Expand Down Expand Up @@ -806,7 +853,22 @@ func DeleteLatestMessage(messageType string, id int64, userid int64) error {
baseURL := config.GetHttpPath() // 假设config.GetHttpPath()返回基础URL

// 构建完整的URL
url := baseURL + "/delete_msg"
baseURL = baseURL + "/delete_msg"

// 获取PathToken并检查其是否为空
pathToken := config.GetPathToken()
// 使用net/url包构建URL
u, err := url.Parse(baseURL)
if err != nil {
panic("URL parsing failed: " + err.Error())
}

// 添加access_token参数
query := u.Query()
if pathToken != "" {
query.Set("access_token", pathToken)
}
u.RawQuery = query.Encode()

// 获取最新的有效消息ID
messageID, valid := GetLatestValidMessageID(userid)
Expand Down Expand Up @@ -839,5 +901,5 @@ func DeleteLatestMessage(messageType string, id int64, userid int64) error {
fmtf.Printf("发送撤回请求:%v", string(requestBodyBytes))

// 发送删除消息请求
return sendDeleteRequest(url, requestBodyBytes)
return sendDeleteRequest(u.String(), requestBodyBytes)
}

0 comments on commit 1f68210

Please sign in to comment.