mirror of
https://github.com/hanzoai/ai.git
synced 2026-11-20 14:13:23 +00:00
382 lines
9.7 KiB
Go
382 lines
9.7 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 controllers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"sort"
|
|
|
|
iam "github.com/hanzoai/ai/internal/iam"
|
|
"github.com/hanzoai/ai/object"
|
|
"github.com/hanzoai/ai/util"
|
|
)
|
|
|
|
// GetGlobalStores
|
|
// @Title GetGlobalStores
|
|
// @Tag Store API
|
|
// @Description get global stores
|
|
// @Success 200 {array} object.Store The Response object
|
|
// @router /get-global-stores [get]
|
|
// storeFor resolves the store an id names, for a caller entitled to write it.
|
|
//
|
|
// A request body says WHICH store to act on; it does not say what that store
|
|
// currently is. The default flag is the sharp case — it is read here, from the
|
|
// stored row, so a body cannot declare itself non-default to walk past the guard
|
|
// that keeps one default store in place.
|
|
//
|
|
// A store the caller's org does not reach answers the same way as one that is not
|
|
// there, which is deliberate: the two are the same fact from where the caller
|
|
// stands, and distinguishing them would confirm another tenant's stores by name.
|
|
func storeFor(user *iam.User, id string) (*object.Store, error) {
|
|
stored, err := object.GetStore(id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if stored == nil || !reaches(user, stored.Owner) {
|
|
return nil, fmt.Errorf("the store: %s does not exist", id)
|
|
}
|
|
return stored, nil
|
|
}
|
|
|
|
func (c *ApiController) GetGlobalStores() {
|
|
// Every tenant's store configuration in one answer is a platform read, so it
|
|
// asks for the platform predicate — the reserved org — rather than a tenant's
|
|
// own admin flag. Both branches ask; the listing is the same disclosure
|
|
// whether or not a page size came with it.
|
|
if !c.RequireSuperAdmin() {
|
|
return
|
|
}
|
|
|
|
name := c.Input().Get("name")
|
|
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 == "" {
|
|
stores, err := object.GetGlobalStores()
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
c.ResponseOk(stores)
|
|
} else {
|
|
limit := util.ParseInt(limit)
|
|
count, err := object.GetStoreCount(name, field, value)
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
paginator := util.NewPaginator(c.PageAsked(), limit, count)
|
|
stores, err := object.GetPaginationStores(paginator.Offset(), limit, name, field, value, sortField, sortOrder)
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
sort.SliceStable(stores, func(i, j int) bool {
|
|
return stores[i].IsDefault && !stores[j].IsDefault
|
|
})
|
|
|
|
err = object.PopulateStoreCounts(stores)
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
c.ResponseOk(stores, paginator.Nums())
|
|
}
|
|
}
|
|
|
|
// GetStores
|
|
// @Title GetStores
|
|
// @Tag Store API
|
|
// @Description get stores
|
|
// @Param owner query string true "The owner of the store"
|
|
// @Success 200 {array} object.Store The Response object
|
|
// @router /get-stores [get]
|
|
func (c *ApiController) GetStores() {
|
|
owner, allowed := c.GetScopedOwner()
|
|
if !allowed {
|
|
return
|
|
}
|
|
|
|
stores, err := object.GetStores(owner)
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
// Apply store isolation based on user's Homepage field
|
|
stores = FilterStoresByHomepage(stores, c.GetSessionUser())
|
|
|
|
c.ResponseOk(stores)
|
|
}
|
|
|
|
// GetStore
|
|
// @Title GetStore
|
|
// @Tag Store API
|
|
// @Description get store
|
|
// @Param id query string true "The id (owner/name) of the store"
|
|
// @Success 200 {object} object.Store The Response object
|
|
// @router /get-store [get]
|
|
func (c *ApiController) GetStore() {
|
|
id := c.Input().Get("id")
|
|
|
|
var store *object.Store
|
|
var err error
|
|
if id == "admin/_cloud_default_store_" {
|
|
store, err = object.GetDefaultStore("admin")
|
|
} else {
|
|
store, err = object.GetStore(id)
|
|
}
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
if store != nil {
|
|
host := c.Host()
|
|
origin := getOriginFromHost(host)
|
|
err = store.Populate(origin, c.GetAcceptLanguage())
|
|
if err != nil {
|
|
c.ResponseOk(store, err.Error())
|
|
return
|
|
}
|
|
}
|
|
|
|
c.ResponseOk(store)
|
|
}
|
|
|
|
// UpdateStore
|
|
// @Title UpdateStore
|
|
// @Tag Store API
|
|
// @Description update store
|
|
// @Param id query string true "The id (owner/name) of the store"
|
|
// @Param body body object.Store true "The details of the store"
|
|
// @Success 200 {object} controllers.Response The Response object
|
|
// @router /update-store [post]
|
|
func (c *ApiController) UpdateStore() {
|
|
caller, ok := c.RequireSignedInUser()
|
|
if !ok {
|
|
return
|
|
}
|
|
id := c.Input().Get("id")
|
|
|
|
var store object.Store
|
|
err := json.Unmarshal(c.Body(), &store)
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
oldStore, err := storeFor(caller, id)
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
store.Owner = oldStore.Owner
|
|
|
|
if oldStore.IsDefault && !store.IsDefault {
|
|
c.ResponseError(c.T("store:given that there must be one default store in Hanzo Cloud, you cannot set this store to non-default. You can directly set another store as default"))
|
|
return
|
|
}
|
|
|
|
success, err := object.UpdateStore(id, &store)
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
if !oldStore.IsDefault && store.IsDefault {
|
|
stores, err := object.GetGlobalStores()
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
for _, store2 := range stores {
|
|
if store2.GetId() != store.GetId() && store2.IsDefault {
|
|
store2.IsDefault = false
|
|
success, err = object.UpdateStore(store2.GetId(), store2)
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
c.ResponseOk(success)
|
|
}
|
|
|
|
// AddStore
|
|
// @Title AddStore
|
|
// @Tag Store API
|
|
// @Description add store
|
|
// @Param body body object.Store true "The details of the store"
|
|
// @Success 200 {object} controllers.Response The Response object
|
|
// @router /add-store [post]
|
|
func (c *ApiController) AddStore() {
|
|
var store object.Store
|
|
err := json.Unmarshal(c.Body(), &store)
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
// A store is created into the same org GetStores reads back, and no other. The
|
|
// owner arrived on the request body, so an admin of ANY org could file a store
|
|
// into the chat plane's own tenant — where it is reachable as a default store,
|
|
// and a default store names the model every chat answer runs and bills.
|
|
owner, allowed := c.GetScopedOwner()
|
|
if !allowed {
|
|
return
|
|
}
|
|
store.Owner = owner
|
|
|
|
err = object.SyncDefaultProvidersToStore(&store)
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
if store.ModelProvider == "" {
|
|
var modelProvider *object.Provider
|
|
modelProvider, err = object.GetDefaultModelProvider()
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
if modelProvider != nil {
|
|
store.ModelProvider = modelProvider.Name
|
|
}
|
|
}
|
|
|
|
if store.EmbeddingProvider == "" {
|
|
var embeddingProvider *object.Provider
|
|
embeddingProvider, err = object.GetDefaultEmbeddingProvider()
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
if embeddingProvider != nil {
|
|
store.EmbeddingProvider = embeddingProvider.Name
|
|
}
|
|
}
|
|
|
|
success, err := object.AddStore(&store)
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
c.ResponseOk(success)
|
|
}
|
|
|
|
// DeleteStore
|
|
// @Title DeleteStore
|
|
// @Tag Store API
|
|
// @Description delete store
|
|
// @Param body body object.Store true "The details of the store"
|
|
// @Success 200 {object} controllers.Response The Response object
|
|
// @router /delete-store [post]
|
|
func (c *ApiController) DeleteStore() {
|
|
caller, ok := c.RequireSignedInUser()
|
|
if !ok {
|
|
return
|
|
}
|
|
var store object.Store
|
|
err := json.Unmarshal(c.Body(), &store)
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
stored, err := storeFor(caller, store.GetId())
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
if stored.IsDefault {
|
|
c.ResponseError(c.T("store:Cannot delete the default store"))
|
|
return
|
|
}
|
|
|
|
success, err := object.DeleteStore(stored)
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
c.ResponseOk(success)
|
|
}
|
|
|
|
// RefreshStoreVectors
|
|
// @Title RefreshStoreVectors
|
|
// @Tag Store API
|
|
// @Description refresh store vectors
|
|
// @Param body body object.Store true "The details of the store"
|
|
// @Success 200 {object} controllers.Response The Response object
|
|
// @router /refresh-store-vectors [post]
|
|
func (c *ApiController) RefreshStoreVectors() {
|
|
var store object.Store
|
|
err := json.Unmarshal(c.Body(), &store)
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
ok, err := object.RefreshStoreVectors(&store, c.GetAcceptLanguage())
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
c.ResponseOk(ok)
|
|
}
|
|
|
|
// GetStoreNames ...
|
|
// @Title GetStoreNames
|
|
// @Tag Store API
|
|
// @Param owner query string true "owner"
|
|
// @Description get all store name and displayName
|
|
// @Success 200 {array} object.Store The Response object
|
|
// @router /get-store-names [get]
|
|
func (c *ApiController) GetStoreNames() {
|
|
owner, allowed := c.GetScopedOwner()
|
|
if !allowed {
|
|
return
|
|
}
|
|
storeNames, err := object.GetStoresByFields(owner, []string{"name", "display_name"}...)
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
// Apply store isolation based on user's Homepage field
|
|
storeNames = FilterStoresByHomepage(storeNames, c.GetSessionUser())
|
|
|
|
c.ResponseOk(storeNames)
|
|
}
|