feat(resource): add stable resource registry with LLM-context alias compaction

Introduce resource://<handle> as the stable application identity for stored
objects, decoupling API responses, persisted rich text, and LLM prompts from
physical storage paths. Adds a resource catalog service (register/resolve/bind,
revocable expiring access grants), a request-local alias registry that compacts
resource references while in the LLM context and restores them (including across
streaming chunks), plus router, IM, chat-pipeline, and multimodal integration.

Includes schema migrations (resources, resource_bindings, resource_access_grants)
for sqlite, and hardens URL/output security handling.
This commit is contained in:
wizardchen
2026-07-15 16:31:21 +08:00
committed by lyingbug
parent 9733bd28bf
commit 6d14cc3536
32 changed files with 1790 additions and 59 deletions
+36 -8
View File
@@ -231,6 +231,8 @@ func BuildContainer(container *dig.Container) *dig.Container {
must(container.Provide(repository.NewWebSearchProviderRepository))
must(container.Provide(repository.NewVectorStoreRepository))
must(container.Provide(repository.NewStorageBackendRepository))
must(container.Provide(repository.NewResourceRepository))
must(container.Provide(service.NewResourceCatalog))
// TenantStoreOwnership adapter used by the retriever factory functions
// to verify that a resolved VectorStore belongs to the caller's tenant.
must(container.Provide(retriever.NewVectorStoreRepoOwnership))
@@ -247,7 +249,7 @@ func BuildContainer(container *dig.Container) *dig.Container {
return sr, nil
}))
must(container.Provide(service.NewVectorStoreService))
must(container.Provide(service.NewStorageBackendService))
must(container.Provide(service.NewStorageBackendServiceWithResources))
must(container.Provide(func(s *service.StorageBackendService) interfaces.StorageBackendService { return s }))
must(container.Provide(func(s *service.StorageBackendService) interfaces.StorageBackendResolver { return s }))
@@ -412,20 +414,38 @@ func BuildContainer(container *dig.Container) *dig.Container {
// disk bytes requires rebuilding the FileService from that tenant's storage
// config. The owning tenant is parsed from the URL's first path segment, which
// correctly handles cross-tenant shared resources (e.g. shared KB images).
func registerChatLocalImageResolver(tenantRepo interfaces.TenantRepository, storageResolver interfaces.StorageBackendResolver) {
func registerChatLocalImageResolver(
tenantRepo interfaces.TenantRepository,
storageResolver interfaces.StorageBackendResolver,
resourceCatalog interfaces.ResourceCatalog,
) {
chat.LocalImageResolver = func(storageURL string) ([]byte, bool) {
tenantID := secutils.ParseTenantIDFromStoragePath(storageURL)
ctx := context.Background()
physicalPath, resource, err := resourceCatalog.ResolvePath(ctx, storageURL)
if err != nil {
return nil, false
}
tenantID := secutils.ParseTenantIDFromStoragePath(physicalPath)
if resource != nil {
tenantID = resource.TenantID
}
if tenantID == 0 {
return nil, false
}
ctx := context.Background()
tenant, err := tenantRepo.GetTenantByID(ctx, tenantID)
if err != nil || tenant == nil {
return nil, false
}
baseDir := strings.TrimSpace(os.Getenv("LOCAL_STORAGE_BASE_DIR"))
backendID, inner, _ := types.ParseStorageBackendPath(storageURL)
provider := types.ParseProviderScheme(inner)
backendID, inner, scoped := types.ParseStorageBackendPath(physicalPath)
if resource != nil && resource.StorageBackendID != "" {
backendID = resource.StorageBackendID
}
providerPath := physicalPath
if scoped {
providerPath = inner
}
provider := types.ParseProviderScheme(providerPath)
if provider == "" {
provider = "local"
}
@@ -433,7 +453,7 @@ func registerChatLocalImageResolver(tenantRepo interfaces.TenantRepository, stor
if err != nil {
return nil, false
}
rc, err := fileSvc.GetFile(ctx, storageURL)
rc, err := fileSvc.GetFile(ctx, physicalPath)
if err != nil {
return nil, false
}
@@ -860,7 +880,15 @@ func syncSequences(db *gorm.DB) {
// Returns:
// - Configured file service implementation
// - Error if initialization fails
func initFileService(cfg *config.Config) (interfaces.FileService, error) {
func initFileService(cfg *config.Config, catalog interfaces.ResourceCatalog) (interfaces.FileService, error) {
inner, err := initRawFileService(cfg)
if err != nil {
return nil, err
}
return file.NewResourceCatalogFileService(inner, catalog), nil
}
func initRawFileService(_ *config.Config) (interfaces.FileService, error) {
storageType := strings.TrimSpace(os.Getenv("STORAGE_TYPE"))
if storageType == "" {
storageType = "local"