MCPcopy Create free account
hub / github.com/mudler/LocalAI / forwardStream

Function forwardStream

core/services/cloudproxy/proxy.go:66–88  ·  view source on GitHub ↗

forwardStream relays the upstream SSE response to the client, flushing per read so events arrive in real time. Response/output PII redaction is out of scope for now, so the stream is forwarded unmodified.

(c echo.Context, body io.Reader)

Source from the content-addressed store, hash-verified

64// redaction is out of scope for now, so the stream is forwarded
65// unmodified.
66func forwardStream(c echo.Context, body io.Reader) error {
67 c.Response().Header().Set("Content-Type", "text/event-stream")
68 c.Response().Header().Set("Cache-Control", "no-cache")
69 c.Response().Header().Set("Connection", "keep-alive")
70 c.Response().WriteHeader(http.StatusOK)
71
72 buf := make([]byte, 32*1024)
73 for {
74 n, rErr := body.Read(buf)
75 if n > 0 {
76 if _, wErr := c.Response().Writer.Write(buf[:n]); wErr != nil {
77 return nil
78 }
79 c.Response().Flush()
80 }
81 if rErr != nil {
82 if rErr != io.EOF {
83 xlog.Debug("cloudproxy: stream read error", "error", rErr)
84 }
85 return nil
86 }
87 }
88}

Callers 1

ForwardViaBackendFunction · 0.85

Calls 6

HeaderMethod · 0.80
SetMethod · 0.65
WriteHeaderMethod · 0.45
ReadMethod · 0.45
WriteMethod · 0.45
FlushMethod · 0.45

Tested by

no test coverage detected