Files
ai/controllers/model_route.go
zeekay 43c4b66ef5 the paginator takes a page, and the stream owns the connection
Three structural pieces of the move off web.

SSE. The chat-completions stream and the single-chunk reply now run inside
SendStreamWriter: zip hands the handler a writer and flushes each frame as it is
written, so a chunk reaches the client when it is produced. The billing capture
runs inside that same closure, so the usage it settles on is the usage of the
bytes the client actually received.

Pagination. NewPaginator wanted an *http.Request for exactly one thing: reading
"p". It takes the page as a number now, and the parameter name is read by the
caller — 22 call sites, and util no longer imports net/http for this. pageAsked
is a pure function so the name and the not-a-number cases are assertable without
a live request; its table moved with it.

Per-request values. Input.SetParam/GetData are Locals, which is where a request
carries something for a later filter to read.

909 compile errors down to 76. Left: 14 bare Request values, 28 ResponseWriter
in the remaining streamers, the session, and routers/ + app.go + the tests.
2026-08-16 22:00:37 -07:00

191 lines
4.8 KiB
Go

// Copyright 2023-2025 Hanzo AI Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package controllers
import (
"encoding/json"
"github.com/hanzoai/ai/object"
"github.com/hanzoai/ai/util"
)
// GetModelRoutes
// @Title GetModelRoutes
// @Tag ModelRoute API
// @Description get model routes for an owner
// @Param owner query string true "The owner (org) of the model routes"
// @Success 200 {array} object.ModelRoute The Response object
// @router /get-model-routes [get]
func (c *ApiController) GetModelRoutes() {
if !c.RequireSuperAdmin() {
return
}
owner := c.Input().Get("owner")
if owner == "" {
owner = "admin"
}
limit := c.Input().Get("pageSize")
page := c.Input().Get("p")
field := c.Input().Get("field")
value := c.Input().Get("value")
sortField := c.Input().Get("sortField")
sortOrder := c.Input().Get("sortOrder")
if limit == "" || page == "" {
routes, err := object.GetModelRoutes(owner)
if err != nil {
c.ResponseError(err.Error())
return
}
c.ResponseOk(routes)
} else {
limit := util.ParseInt(limit)
count, err := object.GetModelRouteCount(owner, field, value)
if err != nil {
c.ResponseError(err.Error())
return
}
paginator := util.NewPaginator(c.PageAsked(), limit, count)
routes, err := object.GetPaginationModelRoutes(owner, paginator.Offset(), limit, field, value, sortField, sortOrder)
if err != nil {
c.ResponseError(err.Error())
return
}
c.ResponseOk(routes, paginator.Nums())
}
}
// GetModelRoute
// @Title GetModelRoute
// @Tag ModelRoute API
// @Description get a specific model route
// @Param owner query string true "The owner (org)"
// @Param modelName query string true "The model name"
// @Success 200 {object} object.ModelRoute The Response object
// @router /get-model-route [get]
func (c *ApiController) GetModelRoute() {
if !c.RequireSuperAdmin() {
return
}
owner := c.Input().Get("owner")
modelName := c.Input().Get("modelName")
if owner == "" || modelName == "" {
c.ResponseError("owner and modelName are required")
return
}
route, err := object.GetModelRoute(owner, modelName)
if err != nil {
c.ResponseError(err.Error())
return
}
c.ResponseOk(route)
}
// AddModelRoute
// @Title AddModelRoute
// @Tag ModelRoute API
// @Description add a model route
// @Param body body object.ModelRoute true "The details of the model route"
// @Success 200 {object} controllers.Response The Response object
// @router /add-model-route [post]
func (c *ApiController) AddModelRoute() {
if !c.RequireSuperAdmin() {
return
}
var route object.ModelRoute
err := json.Unmarshal(c.Body(), &route)
if err != nil {
c.ResponseError(err.Error())
return
}
if route.Owner == "" {
route.Owner = "admin"
}
success, err := object.AddModelRoute(&route)
if err != nil {
c.ResponseError(err.Error())
return
}
c.ResponseOk(success)
}
// UpdateModelRoute
// @Title UpdateModelRoute
// @Tag ModelRoute API
// @Description update a model route
// @Param owner query string true "The owner (org)"
// @Param modelName query string true "The model name"
// @Param body body object.ModelRoute true "The details of the model route"
// @Success 200 {object} controllers.Response The Response object
// @router /update-model-route [post]
func (c *ApiController) UpdateModelRoute() {
if !c.RequireSuperAdmin() {
return
}
owner := c.Input().Get("owner")
modelName := c.Input().Get("modelName")
var route object.ModelRoute
err := json.Unmarshal(c.Body(), &route)
if err != nil {
c.ResponseError(err.Error())
return
}
success, err := object.UpdateModelRoute(owner, modelName, &route)
if err != nil {
c.ResponseError(err.Error())
return
}
c.ResponseOk(success)
}
// DeleteModelRoute
// @Title DeleteModelRoute
// @Tag ModelRoute API
// @Description delete a model route
// @Param body body object.ModelRoute true "The details of the model route"
// @Success 200 {object} controllers.Response The Response object
// @router /delete-model-route [post]
func (c *ApiController) DeleteModelRoute() {
if !c.RequireSuperAdmin() {
return
}
var route object.ModelRoute
err := json.Unmarshal(c.Body(), &route)
if err != nil {
c.ResponseError(err.Error())
return
}
success, err := object.DeleteModelRoute(&route)
if err != nil {
c.ResponseError(err.Error())
return
}
c.ResponseOk(success)
}