some changes

This commit is contained in:
2026-03-02 00:58:43 +03:00
parent b394c0be68
commit 3e0d3db47e
13 changed files with 486 additions and 292 deletions

37
openai/sse.go Normal file
View File

@@ -0,0 +1,37 @@
package openai
import (
"bufio"
"io"
"iter"
"strings"
)
// Server-sent event
func ReadSSE(r io.ReadCloser) iter.Seq[string] {
reader := bufio.NewReader(r)
return func(yield func(string) bool) {
for {
line, err := reader.ReadString('\n')
if err != nil {
return
}
if line == "" || line == "\n" {
continue
}
if strings.HasPrefix(line, "data: ") {
line = line[len("data: "):]
}
line = strings.TrimSpace(line)
line = strings.Trim(line, "\r")
line = strings.Trim(line, "\n")
if strings.HasPrefix(line, "[DONE]") {
return
}
if !yield(line) {
return
}
}
}
}