Files
ai/object/session.go
zeekay 39d9ec81ae ai serves its own routes: the second router is gone
The whole module builds on zip. This is the rest of it — routers/, the relay, and
the 2,823-line in-house router underneath.

ONE ROUTE TABLE. Every address was registered on a hand-written router held in a
package variable built at import time, and the zip app in front of it was a single
app.All("/v1/*", relay) forwarding through a net/http adaptor into that router. A
short "promoted" list re-registered three addresses specifically so the fleet
document could NAME them, because a glob tells a host a prefix and nothing under
it: three operations authenticated, reachable and answering in production with no
generated SDK, no MCP tool, no CLI command, and nothing saying who served them.
Every address is its own route now, so every one is described, and the boundary
the relay crossed — with the OTel context it had to stow and restore over it — is
gone with the relay.

Registration is one entry point, called with the app instead of writing onto a
global at import. The filters go on FIRST, and that is not cosmetic: the router
walks its stack in registration order, so a filter added after a route never runs
before it.

ONE IDENTITY. The filters read a per-pod in-memory session store; the handlers read
the request's verified IAM token. The store's own comment recorded the cost — it is
filled by a hook that does not run when the embedded binary serves these routes, so
it answered nil on every request and the filters billed and rate-limited by the raw
key instead. Filters now ask the same function the handlers ask.

Two filters went entirely, both doing somebody else's job:

  - Auto-signin accepted an MD5 of clientId:clientSecret, or HTTP Basic against the
    configured pair, and minted an IsAdmin identity into that store. IAM mints
    credentials. It was reachable only for /storage, which is why it survived.
  - Static served a landing folder, the admin bundle, and /storage from a path
    taken off the URL — with no cleaning, beside a sibling branch that checks for
    "/.." — then rewrote main.<hash>.js on the way out. Static content is ingress
    and the static plugin. Every route here is /v1.

A REFUSAL IS THE END OF THE REQUEST, and now says so. A filter used to write a
response and return, and the router inferred a refusal from something having been
written. Two places got that wrong: the demo-mode gate denied and then ran the
permission filter anyway, and the error envelope defaulted to 200 with a second
function beside it for callers who wanted a real status. One envelope, status
required.

Recording was two filters passing a username through a request parameter, because
a before-filter and an after-filter had no other way to share; it is one
middleware holding a local across the handler. The cookie rewriter went with the
store it patched. The prometheus filter counted /api throughput — a prefix this
service has never served — so its counter has always been zero.

The standalone binary serves the app on zip's own listener rather than a net/http
server wrapping it, and the ZAP transports take the same app as an http.Handler,
resolved per request: App.Fiber() hands out the current generation's router, and a
generation is a projection.

web/ keeps its React source and loses its Go.
2026-08-17 08:35:46 -07:00

195 lines
4.9 KiB
Go

// Copyright 2023-2025 Hanzo AI Inc. All Rights Reserved.
// Portions Copyright 2023 The OpenAgent Authors. 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 object
import (
"fmt"
"github.com/hanzoai/ai/util"
"github.com/hanzoai/dbx"
)
type Session struct {
Owner string `db:"pk" json:"owner"`
Name string `db:"pk" json:"name"`
CreatedTime string `json:"createdTime"`
SessionId StringList `json:"sessionId"`
}
func GetSessions(owner string) ([]*Session, error) {
sessions := []*Session{}
var err error
if owner != "" {
err = findAll(adapter.db, "session", &sessions, dbx.HashExp{"owner": owner}, "created_time DESC")
} else {
err = findAll(adapter.db, "session", &sessions, nil, "created_time DESC")
}
if err != nil {
return sessions, err
}
return sessions, nil
}
func GetPaginationSessions(owner string, offset, limit int, field, value, sortField, sortOrder string) ([]*Session, error) {
sessions := []*Session{}
session := GetDbQuery(owner, offset, limit, field, value, sortField, sortOrder)
err := queryFind(session, "session", &sessions)
if err != nil {
return sessions, err
}
return sessions, nil
}
func GetSessionCount(owner, field, value string) (int64, error) {
session := GetDbQuery(owner, -1, -1, field, value, "", "")
return queryCount(session, "session")
}
func GetSession(id string) (*Session, error) {
owner, name, err := util.GetOwnerAndNameFromIdWithError(id)
if err != nil {
return nil, err
}
session := Session{Owner: owner, Name: name}
get, err := getOne(adapter.db, "session", &session, pk2(session.Owner, session.Name))
if err != nil {
return &session, err
}
if !get {
return nil, nil
}
return &session, nil
}
func UpdateSession(id string, session *Session) (bool, error) {
owner, name, err := util.GetOwnerAndNameFromIdWithError(id)
if err != nil {
return false, err
}
if ss, err := GetSession(id); err != nil {
return false, err
} else if ss == nil {
return false, nil
}
session.Owner = owner
session.Name = name
err = adapter.db.Model(session).Update()
if err != nil {
return false, err
}
return true, nil
}
func removeExtraSessionIds(session *Session) {
if len(session.SessionId) > 100 {
session.SessionId = session.SessionId[(len(session.SessionId) - 100):]
}
}
func AddSession(session *Session) (bool, error) {
dbSession, err := GetSession(session.GetId())
if err != nil {
return false, err
}
if dbSession == nil {
session.CreatedTime = util.GetCurrentTime()
err = insertRow(adapter.db, session)
affected := int64(1)
if err != nil {
affected = 0
}
if err != nil {
return false, err
}
return affected != 0, nil
} else {
m := make(map[string]struct{})
for _, v := range dbSession.SessionId {
m[v] = struct{}{}
}
for _, v := range session.SessionId {
if _, exists := m[v]; !exists {
dbSession.SessionId = append(dbSession.SessionId, v)
}
}
removeExtraSessionIds(dbSession)
return UpdateSession(dbSession.GetId(), dbSession)
}
}
func DeleteSession(id string) (bool, error) {
owner, name, err := util.GetOwnerAndNameFromIdWithError(id)
if err != nil {
return false, err
}
session, err := GetSession(id)
if err != nil {
return false, err
}
if session != nil {
}
affected, err := deleteByPK(adapter.db, "session", pk2(owner, name))
if err != nil {
return false, err
}
return affected != 0, nil
}
func DeleteSessionId(id string, sessionId string) (bool, error) {
session, err := GetSession(id)
if err != nil {
return false, err
}
if session == nil {
return false, nil
}
session.SessionId = util.DeleteVal(session.SessionId, sessionId)
if len(session.SessionId) == 0 {
owner, name, err := util.GetOwnerAndNameFromIdWithError(id)
if err != nil {
return false, err
}
affected, err := deleteByPK(adapter.db, "session", pk2(owner, name))
if err != nil {
return false, err
}
return affected != 0, nil
} else {
return UpdateSession(id, session)
}
}
func (session *Session) GetId() string {
return fmt.Sprintf("%s/%s", session.Owner, session.Name)
}
func IsSessionDuplicated(id string, sessionId string) (bool, error) {
session, err := GetSession(id)
if err != nil {
return false, err
}
if session == nil {
return false, nil
} else {
if len(session.SessionId) > 1 {
return true, nil
} else if len(session.SessionId) < 1 {
return false, nil
} else {
return session.SessionId[0] != sessionId, nil
}
}
}