MCPcopy Index your code
hub / github.com/SMNETSTUDIO/You2API / handleStreamingResponse

Function handleStreamingResponse

api/main.go:285–330  ·  view source on GitHub ↗
(w http.ResponseWriter, youReq *http.Request)

Source from the content-addressed store, hash-verified

283}
284
285func handleStreamingResponse(w http.ResponseWriter, youReq *http.Request) {
286 client := &http.Client{}
287 resp, err := client.Do(youReq)
288 if err != nil {
289 http.Error(w, err.Error(), http.StatusInternalServerError)
290 return
291 }
292 defer resp.Body.Close()
293
294 w.Header().Set("Content-Type", "text/event-stream")
295 w.Header().Set("Cache-Control", "no-cache")
296 w.Header().Set("Connection", "keep-alive")
297
298 scanner := bufio.NewScanner(resp.Body)
299 for scanner.Scan() {
300 line := scanner.Text()
301
302 if strings.HasPrefix(line, "event: youChatToken") {
303 scanner.Scan()
304 data := scanner.Text()
305
306 var token YouChatResponse
307 json.Unmarshal([]byte(strings.TrimPrefix(data, "data: ")), &token)
308
309 openAIResp := OpenAIStreamResponse{
310 ID: "chatcmpl-" + fmt.Sprintf("%d", time.Now().Unix()),
311 Object: "chat.completion.chunk",
312 Created: time.Now().Unix(),
313 Model: reverseMapModelName(mapModelName(originalModel)),
314 Choices: []Choice{
315 {
316 Delta: Delta{
317 Content: token.YouChatToken,
318 },
319 Index: 0,
320 FinishReason: "",
321 },
322 },
323 }
324
325 respBytes, _ := json.Marshal(openAIResp)
326 fmt.Fprintf(w, "data: %s\n\n", string(respBytes))
327 w.(http.Flusher).Flush()
328 }
329 }
330}

Callers 1

HandlerFunction · 0.85

Calls 2

reverseMapModelNameFunction · 0.85
mapModelNameFunction · 0.85

Tested by

no test coverage detected