Skip to content

Commit c958d25

Browse files
committed
✨ feat(精修): 支持对单个文件进行精修
1 parent 60176c4 commit c958d25

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

bind.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ func BindRoutes(server *fiber.App) {
1212
apiGroup.Get("/file", route.FileRoute)
1313
apiGroup.Post("/save", route.FileUpdateRoute)
1414
apiGroup.Get("/load", route.FileListRoute)
15+
apiGroup.Get("/change", route.ChangeFileRoute)
1516
}

route/file.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package route
22

33
import (
4+
"encoding/json"
45
"io/fs"
56
"os"
67
"path/filepath"
78
"strings"
89
"ui/global"
910
"ui/tool"
1011

12+
"github.com/dingdinglz/openai"
1113
"github.com/gofiber/fiber/v2"
1214
)
1315

@@ -32,6 +34,7 @@ func FileUpdateRoute(c *fiber.Ctx) error {
3234
return c.SendStatus(403)
3335
}
3436
os.WriteFile(filepath.Join(global.RootPath, "data", id, file), []byte(content), os.ModePerm)
37+
updateHistoryMessage(id, file, content)
3538
return c.JSON(map[string]interface{}{
3639
"message": "ok",
3740
})
@@ -67,3 +70,21 @@ func FileListRoute(c *fiber.Ctx) error {
6770
})
6871
return c.JSON(res)
6972
}
73+
74+
func updateHistoryMessage(id string, name string, content string) {
75+
workFile := filepath.Join(global.RootPath, "data", id, "messages.json")
76+
if !tool.FileExist(workFile) {
77+
return
78+
}
79+
historyMessages := []openai.Message{}
80+
j, _ := os.ReadFile(workFile)
81+
json.Unmarshal(j, &historyMessages)
82+
for i := 0; i < len(historyMessages); i++ {
83+
if historyMessages[i].Role == "user" && historyMessages[i].Content == name {
84+
historyMessages[i+1].Content = content
85+
break
86+
}
87+
}
88+
res, _ := json.Marshal(historyMessages)
89+
os.WriteFile(workFile, res, os.ModePerm)
90+
}

route/generate.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ func SendSSEMessage(w *bufio.Writer, message string) {
4040
fmt.Fprintf(w, "data: %s\n\n", message)
4141
w.Flush()
4242
}
43+
4344
func GenerateRoute(ctx *fiber.Ctx) error {
4445
ctx.Set("Content-Type", "text/event-stream")
4546
ctx.Set("Cache-Control", "no-cache")
@@ -121,3 +122,46 @@ func GenerateRoute(ctx *fiber.Ctx) error {
121122
}))
122123
return nil
123124
}
125+
126+
func ChangeFileRoute(ctx *fiber.Ctx) error {
127+
ctx.Set("Content-Type", "text/event-stream")
128+
ctx.Set("Cache-Control", "no-cache")
129+
ctx.Set("Connection", "keep-alive")
130+
ctx.Set("Transfer-Encoding", "chunked")
131+
id := ctx.Query("id")
132+
name := ctx.Query("name")
133+
prompt := ctx.Query("prompt")
134+
ctx.Status(fiber.StatusOK).Context().SetBodyStreamWriter(fasthttp.StreamWriter(func(w *bufio.Writer) {
135+
if id == "" || prompt == "" || name == "" {
136+
SendSSEMessage(w, MakeGenerateMessage("error", "不能为空!"))
137+
return
138+
}
139+
workPath := filepath.Join(global.RootPath, "data", id)
140+
if !tool.FileExist(filepath.Join(workPath, "messages.json")) {
141+
SendSSEMessage(w, MakeGenerateMessage("error", "历史记录缺失!"))
142+
return
143+
}
144+
historyContent, _ := os.ReadFile(filepath.Join(workPath, "messages.json"))
145+
historyMessages := []openai.Message{}
146+
json.Unmarshal(historyContent, &historyMessages)
147+
historyMessages = append(historyMessages, openai.Message{
148+
Role: "user",
149+
Content: "下面你需要重新生成" + name + ",要求是" + prompt + "\n注意,你需要和之前的生成规则保持一致,即仅返回html的内容即可",
150+
})
151+
fileContent := ""
152+
e := global.OpenaiClient.ChatStream(config.ConfigVar.Model.Model, historyMessages, func(s string) {
153+
fileContent += s
154+
SendSSEMessage(w, MakeGenerateMessage("update", fileContent))
155+
})
156+
if e != nil {
157+
SendSSEMessage(w, MakeGenerateMessage("error", e.Error()))
158+
return
159+
}
160+
fileContent = tool.StringBetween(fileContent, "```html", "```")
161+
os.WriteFile(filepath.Join(workPath, name), []byte(fileContent), os.ModePerm)
162+
updateHistoryMessage(id, name, fileContent)
163+
SendSSEMessage(w, MakeGenerateMessage("update", "生成完成!如遇任何问题请联系dinglz"))
164+
SendSSEMessage(w, MakeGenerateMessage("end", "生成完成"))
165+
}))
166+
return nil
167+
}

0 commit comments

Comments
 (0)