增加代码提示代理功能(全部代理)

This commit is contained in:
luyoyu 2023-09-21 00:56:48 +08:00
parent 465426e4e1
commit fd1dea3ab3
2 changed files with 8 additions and 1 deletions

View File

@ -45,5 +45,5 @@ var (
diyMsg = ";//授权已经过期,重新获取-> https://www.sharecopilot.top" diyMsg = ";//授权已经过期,重新获取-> https://www.sharecopilot.top"
completionUrl = "https://copilot-proxy.githubusercontent.com/v1/engines/copilot-codex/completions" completionUrl = "https://copilot-proxy.githubusercontent.com/v1/engines/copilot-codex/completions"
telemetryUrl = "https://copilot-telemetry.githubusercontent.com/telemetry" telemetryUrl = "https://copilot-telemetry.githubusercontent.com/telemetry"
isModMsg = false isModMsg = true
) )

View File

@ -112,6 +112,7 @@ func registerProxyRoute(r *gin.Engine, routePath, targetURL string) {
// 修改代码提示内容 // 修改代码提示内容
func modProxyResp(c *gin.Context, proxy *httputil.ReverseProxy) { func modProxyResp(c *gin.Context, proxy *httputil.ReverseProxy) {
// 在代理响应之前修改响应
proxy.ModifyResponse = func(response *http.Response) error { proxy.ModifyResponse = func(response *http.Response) error {
responseBuffer := new(bytes.Buffer) responseBuffer := new(bytes.Buffer)
_, readErr := responseBuffer.ReadFrom(response.Body) _, readErr := responseBuffer.ReadFrom(response.Body)
@ -119,8 +120,11 @@ func modProxyResp(c *gin.Context, proxy *httputil.ReverseProxy) {
return readErr return readErr
} }
responseData := responseBuffer.Bytes() responseData := responseBuffer.Bytes()
// 定义正则表达式模式
pattern := `(.*?)data:` pattern := `(.*?)data:`
// 编译正则表达式
regex := regexp.MustCompile(pattern) regex := regexp.MustCompile(pattern)
// 查找匹配项
match := regex.FindStringSubmatch(string(responseData)) match := regex.FindStringSubmatch(string(responseData))
var replacedData = "" var replacedData = ""
if len(match) >= 2 { if len(match) >= 2 {
@ -128,9 +132,12 @@ func modProxyResp(c *gin.Context, proxy *httputil.ReverseProxy) {
} else { } else {
fmt.Println("No match found.") fmt.Println("No match found.")
} }
// 将字符串转换为[]rune类型的字符数组
runes := []rune(diyMsg) runes := []rune(diyMsg)
newStr := "" newStr := ""
// 遍历字符数组,对每个字符进行处理
for _, r := range runes { for _, r := range runes {
// 在字符前后添加内容,生成新的字符串
newStr += fmt.Sprintf("data: {\"id\":\"cmpl-7xy1GLgssjHEubVrPyt534VRYVF0t\",\"model\":\"cushman-ml\",\"created\":1694526422,\"choices\":[{\"text\":\"%c\",\"index\":0,\"finish_reason\":null,\"logprobs\":null}]}\n", r) newStr += fmt.Sprintf("data: {\"id\":\"cmpl-7xy1GLgssjHEubVrPyt534VRYVF0t\",\"model\":\"cushman-ml\",\"created\":1694526422,\"choices\":[{\"text\":\"%c\",\"index\":0,\"finish_reason\":null,\"logprobs\":null}]}\n", r)
} }
newStr = replacedData + newStr + "data: [DONE]\n" newStr = replacedData + newStr + "data: [DONE]\n"