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
@@ -80,6 +80,10 @@ type ImageMultimodalService struct {
// fallback in knowledgeService.resolveFileService.
fileSvc interfaces.FileService
storageResolver interfaces.StorageBackendResolver
// resourceCatalog resolves resource:// references to their owning storage
// backend so multimodal reads target the resource's real backend instead of
// the knowledge base's currently configured one.
resourceCatalog interfaces.ResourceCatalog
// spanTracker records this image's subspan under the parent attempt's
// multimodal stage. nil-safe — falls back to no-op via tracker().
@@ -99,6 +103,7 @@ func NewImageMultimodalService(
redisClient *redis.Client,
fileSvc interfaces.FileService,
storageResolver interfaces.StorageBackendResolver,
resourceCatalog interfaces.ResourceCatalog,
spanTracker SpanTracker,
) interfaces.TaskHandler {
return &ImageMultimodalService{
@@ -114,6 +119,7 @@ func NewImageMultimodalService(
redisClient: redisClient,
fileSvc: fileSvc,
storageResolver: storageResolver,
resourceCatalog: resourceCatalog,
spanTracker: spanTracker,
}
}
@@ -554,6 +560,18 @@ func (s *ImageMultimodalService) resolveFileServiceForPayload(ctx context.Contex
backendID, _, _ := types.ParseStorageBackendPath(payload.ImageURL)
provider := types.ParseProviderScheme(payload.ImageURL)
// A resource:// reference carries no provider/backend in the URL itself; the
// authoritative backend lives on the stored resource record. Using the KB's
// currently configured backend here would break reads when the resource was
// stored on a different backend (multi-backend / post-migration).
if _, isResourceRef := types.ParseResourcePath(payload.ImageURL); isResourceRef && s.resourceCatalog != nil {
if resource, resErr := s.resourceCatalog.Resolve(ctx, payload.ImageURL); resErr != nil {
logger.Warnf(ctx, "[ImageMultimodal] resolve resource reference failed: url=%s err=%v", payload.ImageURL, resErr)
} else if resource != nil {
backendID = resource.StorageBackendID
provider = strings.ToLower(strings.TrimSpace(resource.Provider))
}
}
if provider == "" {
kb, kbErr := s.kbService.GetKnowledgeBaseByIDOnly(ctx, payload.KnowledgeBaseID)
if kbErr != nil {
@@ -590,7 +608,8 @@ func (s *ImageMultimodalService) resolveFileServiceForPayload(ctx context.Contex
// file before falling back to the URL.
// - For plain http(s):// URLs it uses the SSRF-safe downloader.
func (s *ImageMultimodalService) readImageBytes(ctx context.Context, payload types.ImageMultimodalPayload) ([]byte, error) {
if types.ParseProviderScheme(payload.ImageURL) != "" {
_, isResourceRef := types.ParseResourcePath(payload.ImageURL)
if isResourceRef || types.ParseProviderScheme(payload.ImageURL) != "" {
fileSvc := s.resolveFileServiceForPayload(ctx, payload)
if fileSvc == nil {
return nil, fmt.Errorf("no file service available for %s", payload.ImageURL)