Files
opencode2api/internal/admin/rotation.go
T
Jason Xu 81ad2842c7 refactor: drop new prefixes, unify v1/v2 lane naming, audit comments
- 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.
2026-10-19 03:46:09 +08:00

74 lines
2.2 KiB
Go

package admin
import (
"fmt"
"net/http"
"opencode2api/internal/config"
"opencode2api/internal/httpx"
"slices"
)
// handleRotation reports the rotation state: both groups with their alias,
// order, current model, pause map and recent switches, plus the recent
// attempts and the first-output timeout.
func (a *Server) handleRotation(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "no-store")
httpx.WriteJSON(w, 200, a.manager.RotationSnapshot())
}
// handleSaveRotation stores new aliases and orders. Each order must be a
// permutation of the group's current order, so a list the WebUI rendered
// before a catalog refresh is rejected instead of dropping models.
func (a *Server) handleSaveRotation(w http.ResponseWriter, r *http.Request) {
var input config.RotationConfig
if err := decodeAdminJSON(w, r, &input); err != nil {
writeAdminError(w, 400, "invalid_request", err.Error())
return
}
_, err := a.manager.Update(func(cfg *config.Config) error {
if err := input.Validate(nil); err != nil {
return err
}
state := a.manager.RotationSnapshot()
for i, g := range state.Groups {
order := input.SOTA.Order
if i == 1 {
order = input.Sweet.Order
}
if len(order) != len(g.Order) {
return fmt.Errorf("模型列表已更新,请刷新后重新排序")
}
for _, v := range order {
if !slices.Contains(g.Order, v) {
return fmt.Errorf("只能调整组内模型的顺序")
}
}
}
cfg.Rotation = input
return nil
})
if err != nil {
writeAdminError(w, 400, "rotation_rejected", a.manager.Redact(err.Error()))
return
}
a.handleRotation(w, r)
}
// handleSelectRotation switches a group to another of its models manually,
// clearing that model's pause.
func (a *Server) handleSelectRotation(w http.ResponseWriter, r *http.Request) {
var input struct {
Group string `json:"group"`
Model string `json:"model"`
}
if err := decodeAdminJSON(w, r, &input); err != nil {
writeAdminError(w, 400, "invalid_request", err.Error())
return
}
if err := a.manager.SelectRotation(input.Group, input.Model); err != nil {
writeAdminError(w, 400, "rotation_rejected", a.manager.Redact(err.Error()))
return
}
a.handleRotation(w, r)
}