feat: implement tenant default storage provider handling in Knowledge Base creation
This commit introduces functionality to utilize the tenant's default storage provider when creating a Knowledge Base. It includes updates to the frontend to load the default provider from settings and apply it during Knowledge Base initialization. Additionally, the backend has been enhanced to ensure that the storage provider is set correctly based on tenant configuration, improving consistency across the application. Tests have been added to verify the correct application of the default storage provider in various scenarios.
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/application/service/retriever"
|
||||
@@ -105,6 +106,7 @@ func (s *knowledgeBaseService) CreateKnowledgeBase(ctx context.Context,
|
||||
kb.CreatorID = uid
|
||||
}
|
||||
kb.EnsureDefaults()
|
||||
applyTenantDefaultStorageProvider(ctx, kb)
|
||||
|
||||
// Fold empty-string vector_store_id into nil so this path and the
|
||||
// retrieve-engine factory's pre-condition share a single representation.
|
||||
@@ -136,6 +138,23 @@ func (s *knowledgeBaseService) CreateKnowledgeBase(ctx context.Context,
|
||||
return kb, nil
|
||||
}
|
||||
|
||||
// applyTenantDefaultStorageProvider fills an empty KB storage provider from the
|
||||
// tenant's global default (Settings → Storage engine). Frontend should send the
|
||||
// same value; this keeps API clients and legacy UIs consistent.
|
||||
func applyTenantDefaultStorageProvider(ctx context.Context, kb *types.KnowledgeBase) {
|
||||
if kb == nil || strings.TrimSpace(kb.GetStorageProvider()) != "" {
|
||||
return
|
||||
}
|
||||
tenant, _ := ctx.Value(types.TenantInfoContextKey).(*types.Tenant)
|
||||
provider := "local"
|
||||
if tenant != nil && tenant.StorageEngineConfig != nil {
|
||||
if p := strings.ToLower(strings.TrimSpace(tenant.StorageEngineConfig.DefaultProvider)); p != "" {
|
||||
provider = p
|
||||
}
|
||||
}
|
||||
kb.SetStorageProvider(provider)
|
||||
}
|
||||
|
||||
// validateVectorStoreBinding routes through retriever.VerifyBinding so the
|
||||
// ownership + registry sentinel hierarchy stays the single source of truth.
|
||||
// The service layer's responsibility is to:
|
||||
|
||||
@@ -131,6 +131,33 @@ func ctxWithTenant(tenantID uint64) context.Context {
|
||||
return context.WithValue(context.Background(), types.TenantIDContextKey, tenantID)
|
||||
}
|
||||
|
||||
func ctxWithTenantStorage(tenantID uint64, defaultProvider string) context.Context {
|
||||
ctx := ctxWithTenant(tenantID)
|
||||
tenant := &types.Tenant{
|
||||
ID: tenantID,
|
||||
StorageEngineConfig: &types.StorageEngineConfig{
|
||||
DefaultProvider: defaultProvider,
|
||||
},
|
||||
}
|
||||
return context.WithValue(ctx, types.TenantInfoContextKey, tenant)
|
||||
}
|
||||
|
||||
func TestCreateKnowledgeBase_DefaultStorageProviderFromTenant(t *testing.T) {
|
||||
repo := newFakeKBRepo()
|
||||
svc := newPR3KBService(repo, &fakeRegistry{registered: map[string]struct{}{}}, &fakeOwnership{})
|
||||
|
||||
kb, err := svc.CreateKnowledgeBase(ctxWithTenantStorage(1, "minio"), &types.KnowledgeBase{Name: "kb"})
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "minio", kb.GetStorageProvider())
|
||||
|
||||
kbExplicit, err := svc.CreateKnowledgeBase(ctxWithTenantStorage(1, "minio"), &types.KnowledgeBase{
|
||||
Name: "kb2",
|
||||
StorageProviderConfig: &types.StorageProviderConfig{Provider: "cos"},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "cos", kbExplicit.GetStorageProvider())
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// CreateKnowledgeBase — vector_store_id binding validation matrix
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user