@@ -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+
4344func 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