Add status progress for batch tool operations

This commit is contained in:
2026-05-29 13:40:17 +08:00
parent 5625d94106
commit 80f155dfe8
14 changed files with 307 additions and 65 deletions

View File

@@ -2,6 +2,7 @@ package main
import (
"encoding/json"
"fmt"
"net/http"
)
@@ -9,3 +10,27 @@ func writeJSON(w http.ResponseWriter, v any) {
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(v)
}
func newJSONStream(w http.ResponseWriter) func(any) {
w.Header().Set("Content-Type", "application/x-ndjson; charset=utf-8")
w.Header().Set("Cache-Control", "no-cache")
flusher, _ := w.(http.Flusher)
return func(v any) {
data, err := json.Marshal(v)
if err != nil {
data = []byte(fmt.Sprintf(`{"type":"error","message":%q}`, err.Error()))
}
_, _ = w.Write(data)
_, _ = w.Write([]byte("\n"))
if flusher != nil {
flusher.Flush()
}
}
}
func pluralS(n int) string {
if n == 1 {
return ""
}
return "s"
}