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:
@@ -2,18 +2,51 @@ package interfaces
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
)
|
||||
|
||||
// ResourceCleaner defines the resource cleaner interface
|
||||
// ResourceCleaner owns process-lifetime cleanup callbacks (database clients,
|
||||
// worker pools, etc.). It is unrelated to persisted StoredResource records.
|
||||
type ResourceCleaner interface {
|
||||
// Register registers a resource cleanup function
|
||||
Register(cleanup types.CleanupFunc)
|
||||
|
||||
// RegisterWithName registers a resource cleanup function with a name
|
||||
RegisterWithName(name string, cleanup types.CleanupFunc)
|
||||
|
||||
// Cleanup executes all resource cleanup functions
|
||||
Cleanup(ctx context.Context) []error
|
||||
}
|
||||
|
||||
// ResourceRepository persists stable resource identities, ownership bindings,
|
||||
// and short-lived access grants.
|
||||
type ResourceRepository interface {
|
||||
Create(ctx context.Context, resource *types.StoredResource) error
|
||||
GetByID(ctx context.Context, id string) (*types.StoredResource, error)
|
||||
GetByHandle(ctx context.Context, handle string) (*types.StoredResource, error)
|
||||
GetByTenantLocation(ctx context.Context, tenantID uint64, locationHash string) (*types.StoredResource, error)
|
||||
MarkDeleted(ctx context.Context, id string) error
|
||||
CreateBinding(ctx context.Context, binding *types.ResourceBinding) error
|
||||
CreateGrant(ctx context.Context, grant *types.ResourceAccessGrant) error
|
||||
GetValidGrant(ctx context.Context, tokenHash string, now time.Time) (*types.ResourceAccessGrant, error)
|
||||
DeleteExpiredGrants(ctx context.Context, before time.Time) error
|
||||
}
|
||||
|
||||
// ResourceRegistration describes one physical object at registration time.
|
||||
type ResourceRegistration struct {
|
||||
Kind string
|
||||
MimeType string
|
||||
OriginalName string
|
||||
Size int64
|
||||
ContentHash string
|
||||
Temporary bool
|
||||
}
|
||||
|
||||
// ResourceCatalog maps public resource references to internal storage
|
||||
// locations and manages their access capabilities.
|
||||
type ResourceCatalog interface {
|
||||
Register(ctx context.Context, tenantID uint64, physicalPath string, meta ResourceRegistration) (string, error)
|
||||
Resolve(ctx context.Context, reference string) (*types.StoredResource, error)
|
||||
ResolvePath(ctx context.Context, value string) (string, *types.StoredResource, error)
|
||||
Bind(ctx context.Context, reference, ownerType, ownerID, relation string) error
|
||||
MarkDeleted(ctx context.Context, reference string) error
|
||||
CreateAccessGrant(ctx context.Context, reference string, ttl time.Duration) (string, error)
|
||||
ResolveAccessGrant(ctx context.Context, token string) (*types.StoredResource, error)
|
||||
}
|
||||
|
||||
@@ -384,7 +384,7 @@ func ParseProviderScheme(filePath string) string {
|
||||
if _, inner, ok := ParseStorageBackendPath(filePath); ok {
|
||||
filePath = inner
|
||||
}
|
||||
for _, provider := range []string{"local", "minio", "cos", "tos", "s3", "oss", "ks3", "obs"} {
|
||||
for _, provider := range []string{"local", "minio", "cos", "tos", "s3", "oss", "ks3", "obs", "dummy"} {
|
||||
if strings.HasPrefix(filePath, provider+"://") {
|
||||
return provider
|
||||
}
|
||||
|
||||
@@ -0,0 +1,131 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// Stable resource reference and lifecycle constants.
|
||||
const (
|
||||
ResourceScheme = "resource://"
|
||||
ResourceHandleLength = 22
|
||||
ResourceStateActive = "active"
|
||||
ResourceStateDeleted = "deleted"
|
||||
ResourceLifecyclePersistent = "persistent"
|
||||
ResourceLifecycleTemporary = "temporary"
|
||||
)
|
||||
|
||||
// StoredResource is the stable application identity of one stored object. PhysicalPath
|
||||
// is deliberately internal: API responses, persisted rich text and LLM prompts
|
||||
// use resource://<handle> instead.
|
||||
type StoredResource struct {
|
||||
ID string `json:"id" gorm:"type:varchar(36);primaryKey"`
|
||||
Handle string `json:"handle" gorm:"type:varchar(22);not null;uniqueIndex"`
|
||||
TenantID uint64 `json:"tenant_id" gorm:"not null;index"`
|
||||
StorageBackendID string `json:"storage_backend_id,omitempty" gorm:"type:varchar(36);index"`
|
||||
Provider string `json:"provider" gorm:"type:varchar(32);not null"`
|
||||
PhysicalPath string `json:"-" gorm:"type:text;not null"`
|
||||
LocationHash string `json:"-" gorm:"type:varchar(64);not null"`
|
||||
Kind string `json:"kind" gorm:"type:varchar(32);not null;default:'file'"`
|
||||
MimeType string `json:"mime_type,omitempty" gorm:"type:varchar(255);not null;default:''"`
|
||||
OriginalName string `json:"original_name,omitempty" gorm:"type:varchar(1024);not null;default:''"`
|
||||
Size int64 `json:"size" gorm:"not null;default:0"`
|
||||
ContentHash string `json:"content_hash,omitempty" gorm:"type:varchar(64);not null;default:''"`
|
||||
Lifecycle string `json:"lifecycle" gorm:"type:varchar(16);not null;default:'persistent'"`
|
||||
ExpiresAt *time.Time `json:"expires_at,omitempty"`
|
||||
State string `json:"state" gorm:"type:varchar(16);not null;default:'active'"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
DeletedAt gorm.DeletedAt `json:"deleted_at" gorm:"index"`
|
||||
}
|
||||
|
||||
// TableName returns the resource registry table name.
|
||||
func (StoredResource) TableName() string { return "resources" }
|
||||
|
||||
// BeforeCreate fills resource defaults before persistence.
|
||||
func (r *StoredResource) BeforeCreate(_ *gorm.DB) error {
|
||||
if r.ID == "" {
|
||||
r.ID = uuid.NewString()
|
||||
}
|
||||
if r.Kind == "" {
|
||||
r.Kind = "file"
|
||||
}
|
||||
if r.Lifecycle == "" {
|
||||
r.Lifecycle = ResourceLifecyclePersistent
|
||||
}
|
||||
if r.State == "" {
|
||||
r.State = ResourceStateActive
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ResourceBinding connects a resource to a domain object that owns or uses it.
|
||||
type ResourceBinding struct {
|
||||
ID string `json:"id" gorm:"type:varchar(36);primaryKey"`
|
||||
ResourceID string `json:"resource_id" gorm:"type:varchar(36);not null;index"`
|
||||
TenantID uint64 `json:"tenant_id" gorm:"not null;index"`
|
||||
OwnerType string `json:"owner_type" gorm:"type:varchar(32);not null"`
|
||||
OwnerID string `json:"owner_id" gorm:"type:varchar(64);not null"`
|
||||
Relation string `json:"relation" gorm:"type:varchar(32);not null;default:'attachment'"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
// TableName returns the resource binding table name.
|
||||
func (ResourceBinding) TableName() string { return "resource_bindings" }
|
||||
|
||||
// BeforeCreate assigns an ID before persistence.
|
||||
func (b *ResourceBinding) BeforeCreate(_ *gorm.DB) error {
|
||||
if b.ID == "" {
|
||||
b.ID = uuid.NewString()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ResourceAccessGrant is a revocable, expiring read capability.
|
||||
type ResourceAccessGrant struct {
|
||||
ID string `json:"id" gorm:"type:varchar(36);primaryKey"`
|
||||
TokenHash string `json:"-" gorm:"type:varchar(64);not null;uniqueIndex"`
|
||||
ResourceID string `json:"resource_id" gorm:"type:varchar(36);not null;index"`
|
||||
AccessScope string `json:"access_scope" gorm:"type:varchar(16);not null;default:'read'"`
|
||||
ExpiresAt time.Time `json:"expires_at" gorm:"not null;index"`
|
||||
RevokedAt *time.Time `json:"revoked_at,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
// TableName returns the resource access grant table name.
|
||||
func (ResourceAccessGrant) TableName() string { return "resource_access_grants" }
|
||||
|
||||
// BeforeCreate assigns an ID before persistence.
|
||||
func (g *ResourceAccessGrant) BeforeCreate(_ *gorm.DB) error {
|
||||
if g.ID == "" {
|
||||
g.ID = uuid.NewString()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// BuildResourcePath creates a stable application resource reference.
|
||||
func BuildResourcePath(handle string) string {
|
||||
return ResourceScheme + strings.TrimSpace(handle)
|
||||
}
|
||||
|
||||
// ParseResourcePath validates and extracts a canonical resource handle.
|
||||
func ParseResourcePath(value string) (string, bool) {
|
||||
value = strings.TrimSpace(value)
|
||||
if !strings.HasPrefix(value, ResourceScheme) {
|
||||
return "", false
|
||||
}
|
||||
handle := strings.TrimPrefix(value, ResourceScheme)
|
||||
if len(handle) != ResourceHandleLength {
|
||||
return "", false
|
||||
}
|
||||
for _, char := range handle {
|
||||
if (char < 'a' || char > 'z') && (char < 'A' || char > 'Z') &&
|
||||
(char < '0' || char > '9') && char != '_' && char != '-' {
|
||||
return "", false
|
||||
}
|
||||
}
|
||||
return handle, true
|
||||
}
|
||||
Reference in New Issue
Block a user