Skip to content

Commit

Permalink
[Go] Update flow streaming protocol to SSE
Browse files Browse the repository at this point in the history
  • Loading branch information
hugoaguirre committed Nov 15, 2024
1 parent 4e53051 commit ebb72b8
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions go/genkit/servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,12 +380,12 @@ func nonDurableFlowHandler(f flow) func(http.ResponseWriter, *http.Request) erro
return err
}
var callback streamingCallback[json.RawMessage]
if stream {
if r.Header.Get("Accept") == "text/event-stream" || stream {
w.Header().Set("Content-Type", "text/plain")
w.Header().Set("Transfer-Encoding", "chunked")
// Stream results are newline-separated JSON.
callback = func(ctx context.Context, msg json.RawMessage) error {
_, err := fmt.Fprintf(w, "%s\n", msg)
_, err := fmt.Fprintf(w, "data: %s\n\n", msg)
if err != nil {
return err
}
Expand All @@ -402,7 +402,7 @@ func nonDurableFlowHandler(f flow) func(http.ResponseWriter, *http.Request) erro
}
// Responses for non-streaming, non-durable flows are passed back
// with the flow result stored in a field called "result."
_, err = fmt.Fprintf(w, `{"result": %s}\n`, out)
_, err = fmt.Fprintf(w, `data: {"result": %s}\n`, out)
return err
}
}
Expand Down

0 comments on commit ebb72b8

Please sign in to comment.