From fd1dea3ab31cb13b1b940bb674c763ddfb309009 Mon Sep 17 00:00:00 2001 From: luyoyu <8829470+chuangxxt@user.noreply.gitee.com> Date: Thu, 21 Sep 2023 00:56:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BB=A3=E7=A0=81=E6=8F=90?= =?UTF-8?q?=E7=A4=BA=E4=BB=A3=E7=90=86=E5=8A=9F=E8=83=BD(=E5=85=A8?= =?UTF-8?q?=E9=83=A8=E4=BB=A3=E7=90=86)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/global.go | 2 +- src/server.go | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/global.go b/src/global.go index fb859cf..e40ff5b 100644 --- a/src/global.go +++ b/src/global.go @@ -45,5 +45,5 @@ var ( diyMsg = ";//授权已经过期,重新获取-> https://www.sharecopilot.top" completionUrl = "https://copilot-proxy.githubusercontent.com/v1/engines/copilot-codex/completions" telemetryUrl = "https://copilot-telemetry.githubusercontent.com/telemetry" - isModMsg = false + isModMsg = true ) diff --git a/src/server.go b/src/server.go index f9ba540..474f54e 100644 --- a/src/server.go +++ b/src/server.go @@ -112,6 +112,7 @@ func registerProxyRoute(r *gin.Engine, routePath, targetURL string) { // 修改代码提示内容 func modProxyResp(c *gin.Context, proxy *httputil.ReverseProxy) { + // 在代理响应之前修改响应 proxy.ModifyResponse = func(response *http.Response) error { responseBuffer := new(bytes.Buffer) _, readErr := responseBuffer.ReadFrom(response.Body) @@ -119,8 +120,11 @@ func modProxyResp(c *gin.Context, proxy *httputil.ReverseProxy) { return readErr } responseData := responseBuffer.Bytes() + // 定义正则表达式模式 pattern := `(.*?)data:` + // 编译正则表达式 regex := regexp.MustCompile(pattern) + // 查找匹配项 match := regex.FindStringSubmatch(string(responseData)) var replacedData = "" if len(match) >= 2 { @@ -128,9 +132,12 @@ func modProxyResp(c *gin.Context, proxy *httputil.ReverseProxy) { } else { fmt.Println("No match found.") } + // 将字符串转换为[]rune类型的字符数组 runes := []rune(diyMsg) newStr := "" + // 遍历字符数组,对每个字符进行处理 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 = replacedData + newStr + "data: [DONE]\n"