@@ -0,0 +1,29 @@
|
||||
package stream
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
)
|
||||
|
||||
type Frame struct {
|
||||
Type string `json:"type"`
|
||||
Text string `json:"text,omitempty"`
|
||||
Message string `json:"message,omitempty"`
|
||||
Tool string `json:"tool,omitempty"`
|
||||
Stage string `json:"stage,omitempty"`
|
||||
Status string `json:"status,omitempty"`
|
||||
Data map[string]any `json:"data,omitempty"`
|
||||
Stats *Stats `json:"stats,omitempty"`
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
type EmitFunc func(Frame)
|
||||
|
||||
func WriteSSEJSON(w io.Writer, frame Frame) {
|
||||
data, err := json.Marshal(frame)
|
||||
if err != nil {
|
||||
data, _ = json.Marshal(Frame{Type: "error", Error: "序列化流事件失败"})
|
||||
}
|
||||
fmt.Fprintf(w, "data: %s\n\n", data)
|
||||
}
|
||||
Reference in New Issue
Block a user