mirror of
https://github.com/jasonxu114514/opencode2api.git
synced 2026-12-05 08:02:53 +00:00
- Remove the new/New prefix from constructors; rename the colliding types semantically (ModelCatalog, RuntimeController, Metrics, LogBroker, Redactor, proxyTransports, upstreamNodes, ...). - Unify upstream lane naming on v1 (/zen, OpenCode 1.x shape) and v2 (/inference and /zen/go, 2.x CLI shape); add models.IsV1Upstream and drop the duplicated lane detection. - Fix inaccurate, truncated and stale comments across all packages and document exported symbols plus non-obvious internals. - Fix anthropicThinking passing "enabled"/"adaptive" through as an effort; derive the level from the budget instead. - Delete dead code (identity.StableID, unused protocol decoders and stream parser wrappers). - Export EffectiveListeners, rename modelsDev* metadata constants, and describe the metadata source as the OpenCode endpoint. - Rename the forced-collapse log event to stream_collapse_failed and add an anonymous field. - Correct the README upstream.zen default and add upstream.anonymous.
44 lines
1.4 KiB
Go
44 lines
1.4 KiB
Go
// Package httpx provides shared HTTP response and transport helpers.
|
|
package httpx
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
)
|
|
|
|
// WriteJSON writes value as a JSON response with the given status, setting the
|
|
// Content-Type header first.
|
|
func WriteJSON(w http.ResponseWriter, status int, value any) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
WriteJSONStatus(w, status, value)
|
|
}
|
|
|
|
// WriteJSONStatus writes value as a JSON body with the given status without
|
|
// touching the headers, so a caller can set a Content-Type or Cache-Control of
|
|
// its own first. An encoding error cannot be reported: the status is already
|
|
// committed, and the body is simply cut short.
|
|
func WriteJSONStatus(w http.ResponseWriter, status int, value any) {
|
|
w.WriteHeader(status)
|
|
_ = json.NewEncoder(w).Encode(value)
|
|
}
|
|
|
|
// WriteSSE writes one server-sent event: an optional "id:" line when id is
|
|
// positive, an optional "event:" line when event is not empty, and the JSON
|
|
// encoding of value as the data. It returns an error only for the data: a
|
|
// value that cannot be encoded, or a data line that cannot be written.
|
|
func WriteSSE(w http.ResponseWriter, event string, id uint64, value any) error {
|
|
data, err := json.Marshal(value)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if id > 0 {
|
|
_, _ = fmt.Fprintf(w, "id: %d\n", id)
|
|
}
|
|
if event != "" {
|
|
_, _ = fmt.Fprintf(w, "event: %s\n", event)
|
|
}
|
|
_, err = fmt.Fprintf(w, "data: %s\n\n", data)
|
|
return err
|
|
}
|