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:
@@ -387,6 +387,7 @@ import { MessagePlugin, DialogPlugin } from 'tdesign-vue-next'
|
|||||||
import { createKnowledgeBase, getKnowledgeBaseById, listKnowledgeFiles, updateKnowledgeBase, rebuildKBIndex } from '@/api/knowledge-base'
|
import { createKnowledgeBase, getKnowledgeBaseById, listKnowledgeFiles, updateKnowledgeBase, rebuildKBIndex } from '@/api/knowledge-base'
|
||||||
import { updateKBConfig, type KBModelConfigRequest } from '@/api/initialization'
|
import { updateKBConfig, type KBModelConfigRequest } from '@/api/initialization'
|
||||||
import { listModels } from '@/api/model'
|
import { listModels } from '@/api/model'
|
||||||
|
import { getStorageEngineConfig } from '@/api/system'
|
||||||
import { useUIStore } from '@/stores/ui'
|
import { useUIStore } from '@/stores/ui'
|
||||||
import { useAuthStore } from '@/stores/auth'
|
import { useAuthStore } from '@/stores/auth'
|
||||||
import KBModelConfig from './settings/KBModelConfig.vue'
|
import KBModelConfig from './settings/KBModelConfig.vue'
|
||||||
@@ -449,6 +450,8 @@ const loading = ref(false)
|
|||||||
const allModels = ref<any[]>([])
|
const allModels = ref<any[]>([])
|
||||||
const hasFiles = ref(false)
|
const hasFiles = ref(false)
|
||||||
const initialStorageProvider = ref<string>('')
|
const initialStorageProvider = ref<string>('')
|
||||||
|
/** Tenant-wide default from Settings → Storage engine (used when creating a KB). */
|
||||||
|
const tenantDefaultStorageProvider = ref('local')
|
||||||
const initialIndexingStrategy = ref<any>(null)
|
const initialIndexingStrategy = ref<any>(null)
|
||||||
const dsCount = ref(0)
|
const dsCount = ref(0)
|
||||||
// Identifier of the user who created this KB. Empty for older rows
|
// Identifier of the user who created this KB. Empty for older rows
|
||||||
@@ -870,10 +873,26 @@ const handleAddWikiModel = () => {
|
|||||||
|
|
||||||
const handleStorageProviderUpdate = (value: string) => {
|
const handleStorageProviderUpdate = (value: string) => {
|
||||||
if (formData.value) {
|
if (formData.value) {
|
||||||
formData.value.storageProvider = value || 'local'
|
formData.value.storageProvider = value || tenantDefaultStorageProvider.value || 'local'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function loadTenantDefaultStorageProvider() {
|
||||||
|
try {
|
||||||
|
const res = await getStorageEngineConfig()
|
||||||
|
tenantDefaultStorageProvider.value = res?.data?.default_provider || 'local'
|
||||||
|
} catch {
|
||||||
|
tenantDefaultStorageProvider.value = 'local'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Resolved storage provider for create payload (never silently default to local before tenant config loads). */
|
||||||
|
function resolvedStorageProvider(): string {
|
||||||
|
const explicit = formData.value?.storageProvider?.trim()
|
||||||
|
if (explicit) return explicit
|
||||||
|
return tenantDefaultStorageProvider.value || 'local'
|
||||||
|
}
|
||||||
|
|
||||||
const handleVectorStoreIdUpdate = (id: string) => {
|
const handleVectorStoreIdUpdate = (id: string) => {
|
||||||
if (formData.value) {
|
if (formData.value) {
|
||||||
// Empty string here means "use system default" (env-store fallback).
|
// Empty string here means "use system default" (env-store fallback).
|
||||||
@@ -1005,11 +1024,12 @@ const buildSubmitData = () => {
|
|||||||
|
|
||||||
// 存储引擎:仅传 provider,参数从全局设置读取
|
// 存储引擎:仅传 provider,参数从全局设置读取
|
||||||
// Write to storage_provider_config (authoritative) + storage_config (legacy dual-write)
|
// Write to storage_provider_config (authoritative) + storage_config (legacy dual-write)
|
||||||
|
const storageProvider = resolvedStorageProvider()
|
||||||
data.storage_provider_config = {
|
data.storage_provider_config = {
|
||||||
provider: formData.value.storageProvider || 'local'
|
provider: storageProvider
|
||||||
}
|
}
|
||||||
data.storage_config = {
|
data.storage_config = {
|
||||||
provider: formData.value.storageProvider || 'local'
|
provider: storageProvider
|
||||||
}
|
}
|
||||||
|
|
||||||
// 添加知识图谱配置 — now synced via indexingStrategy.graphEnabled
|
// 添加知识图谱配置 — now synced via indexingStrategy.graphEnabled
|
||||||
@@ -1258,6 +1278,7 @@ const resetState = () => {
|
|||||||
formData.value = null
|
formData.value = null
|
||||||
hasFiles.value = false
|
hasFiles.value = false
|
||||||
initialStorageProvider.value = ''
|
initialStorageProvider.value = ''
|
||||||
|
tenantDefaultStorageProvider.value = 'local'
|
||||||
initialIndexingStrategy.value = null
|
initialIndexingStrategy.value = null
|
||||||
saving.value = false
|
saving.value = false
|
||||||
loading.value = false
|
loading.value = false
|
||||||
@@ -1284,15 +1305,16 @@ watch(() => props.visible, async (newVal) => {
|
|||||||
currentSection.value = uiStore.kbEditorInitialSection
|
currentSection.value = uiStore.kbEditorInitialSection
|
||||||
}
|
}
|
||||||
|
|
||||||
// 加载模型列表
|
// 加载模型列表与租户默认存储引擎(创建 KB 时即使用,不依赖是否打开「存储引擎」Tab)
|
||||||
await loadAllModels()
|
await Promise.all([loadAllModels(), loadTenantDefaultStorageProvider()])
|
||||||
|
|
||||||
// 根据模式加载数据
|
// 根据模式加载数据
|
||||||
if (props.mode === 'edit' && props.kbId) {
|
if (props.mode === 'edit' && props.kbId) {
|
||||||
await loadKBData()
|
await loadKBData()
|
||||||
} else {
|
} else {
|
||||||
// 创建模式:初始化空表单
|
// 创建模式:初始化空表单,并预填租户默认存储引擎
|
||||||
formData.value = initFormData(props.initialType || 'document')
|
formData.value = initFormData(props.initialType || 'document')
|
||||||
|
formData.value.storageProvider = tenantDefaultStorageProvider.value
|
||||||
hasFiles.value = false
|
hasFiles.value = false
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -68,7 +68,8 @@ const emit = defineEmits<{
|
|||||||
}>()
|
}>()
|
||||||
|
|
||||||
const uiStore = useUIStore()
|
const uiStore = useUIStore()
|
||||||
const localProvider = ref(props.storageProvider || 'local')
|
// Keep empty until tenant default_provider is loaded — do not pre-fill 'local'.
|
||||||
|
const localProvider = ref(props.storageProvider)
|
||||||
const loading = ref(true)
|
const loading = ref(true)
|
||||||
const engineStatus = ref<StorageEngineStatusItem[]>([])
|
const engineStatus = ref<StorageEngineStatusItem[]>([])
|
||||||
const defaultProvider = ref('local')
|
const defaultProvider = ref('local')
|
||||||
@@ -188,9 +189,12 @@ async function load() {
|
|||||||
defaultProvider.value = configRes?.data?.default_provider || 'local'
|
defaultProvider.value = configRes?.data?.default_provider || 'local'
|
||||||
const d = configRes?.data
|
const d = configRes?.data
|
||||||
hasAnyConfig.value = !!(d?.local?.path_prefix || d?.minio?.bucket_name || d?.cos?.bucket_name || d?.tos?.bucket_name || d?.s3?.bucket_name || d?.oss?.bucket_name || d?.ks3?.bucket_name || d?.obs?.bucket_name)
|
hasAnyConfig.value = !!(d?.local?.path_prefix || d?.minio?.bucket_name || d?.cos?.bucket_name || d?.tos?.bucket_name || d?.s3?.bucket_name || d?.oss?.bucket_name || d?.ks3?.bucket_name || d?.obs?.bucket_name)
|
||||||
if (!localProvider.value || localProvider.value === '') {
|
const parentUnset = !props.storageProvider
|
||||||
|
if (parentUnset) {
|
||||||
localProvider.value = defaultProvider.value
|
localProvider.value = defaultProvider.value
|
||||||
emit('update:storageProvider', localProvider.value)
|
emit('update:storageProvider', localProvider.value)
|
||||||
|
} else {
|
||||||
|
localProvider.value = props.storageProvider
|
||||||
}
|
}
|
||||||
ensureAllowedProvider()
|
ensureAllowedProvider()
|
||||||
} catch {
|
} catch {
|
||||||
@@ -200,9 +204,13 @@ async function load() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Sync only when parent sets an explicit provider (edit mode). Create mode leaves
|
||||||
|
// storageProvider empty until load() applies tenant default_provider.
|
||||||
watch(() => props.storageProvider, (v) => {
|
watch(() => props.storageProvider, (v) => {
|
||||||
localProvider.value = v || defaultProvider.value || 'local'
|
if (v) {
|
||||||
}, { immediate: true })
|
localProvider.value = v
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
onMounted(load)
|
onMounted(load)
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Tencent/WeKnora/internal/application/service/retriever"
|
"github.com/Tencent/WeKnora/internal/application/service/retriever"
|
||||||
@@ -105,6 +106,7 @@ func (s *knowledgeBaseService) CreateKnowledgeBase(ctx context.Context,
|
|||||||
kb.CreatorID = uid
|
kb.CreatorID = uid
|
||||||
}
|
}
|
||||||
kb.EnsureDefaults()
|
kb.EnsureDefaults()
|
||||||
|
applyTenantDefaultStorageProvider(ctx, kb)
|
||||||
|
|
||||||
// Fold empty-string vector_store_id into nil so this path and the
|
// Fold empty-string vector_store_id into nil so this path and the
|
||||||
// retrieve-engine factory's pre-condition share a single representation.
|
// retrieve-engine factory's pre-condition share a single representation.
|
||||||
@@ -136,6 +138,23 @@ func (s *knowledgeBaseService) CreateKnowledgeBase(ctx context.Context,
|
|||||||
return kb, nil
|
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
|
// validateVectorStoreBinding routes through retriever.VerifyBinding so the
|
||||||
// ownership + registry sentinel hierarchy stays the single source of truth.
|
// ownership + registry sentinel hierarchy stays the single source of truth.
|
||||||
// The service layer's responsibility is to:
|
// 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)
|
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
|
// CreateKnowledgeBase — vector_store_id binding validation matrix
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|||||||
Reference in New Issue
Block a user