Files
ai/util/paginator.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

71 lines
2.1 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 util
import (
"math"
)
// Paginator turns the "p" page-number query parameter plus a page size and a
// known total count into the offset for an offset-based list query. It reads
// the same "p" parameter and computes the same offset as the historical
// list handlers.
type Paginator struct {
perPage int
nums int64
page int
asked int
}
// NewPaginator builds a Paginator over the requested page, a page size and a
// total count. A per of zero or less defaults to 10.
//
// The page arrives as a number rather than a request: reading "p" is the
// caller's job, and it was the only thing this ever wanted a request for.
func NewPaginator(page int, per int, nums int64) *Paginator {
if per <= 0 {
per = 10
}
return &Paginator{perPage: per, nums: nums, asked: page}
}
// Nums returns the total item count.
func (p *Paginator) Nums() int64 { return p.nums }
// Offset is the item offset of the requested page: (page - 1) * perPage.
func (p *Paginator) Offset() int { return (p.currentPage() - 1) * p.perPage }
// pageCount is the number of pages the count spans at the page size.
func (p *Paginator) pageCount() int {
return int(math.Ceil(float64(p.nums) / float64(p.perPage)))
}
// currentPage reads the requested page from the "p" query parameter and
// clamps it into [1, pageCount], caching the result.
func (p *Paginator) currentPage() int {
if p.page != 0 {
return p.page
}
page := p.asked
if n := p.pageCount(); page > n {
page = n
}
if page <= 0 {
page = 1
}
p.page = page
return p.page
}