Files
Enterprise-Knowledge-Base/internal/application/repository/resource.go
T
wizardchen 6d14cc3536 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.
2026-07-15 17:02:43 +08:00

98 lines
3.0 KiB
Go

package repository
import (
"context"
"errors"
"time"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
type resourceRepository struct{ db *gorm.DB }
// NewResourceRepository creates the persistence adapter for resource metadata.
func NewResourceRepository(db *gorm.DB) interfaces.ResourceRepository {
return &resourceRepository{db: db}
}
func (r *resourceRepository) Create(ctx context.Context, resource *types.StoredResource) error {
return r.db.WithContext(ctx).Create(resource).Error
}
func (r *resourceRepository) GetByID(ctx context.Context, id string) (*types.StoredResource, error) {
var resource types.StoredResource
err := r.db.WithContext(ctx).Where("id = ? AND state = ?", id, types.ResourceStateActive).First(&resource).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
return &resource, err
}
func (r *resourceRepository) GetByHandle(ctx context.Context, handle string) (*types.StoredResource, error) {
var resource types.StoredResource
err := r.db.WithContext(ctx).
Where("handle = ? AND state = ?", handle, types.ResourceStateActive).
First(&resource).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
return &resource, err
}
func (r *resourceRepository) GetByTenantLocation(
ctx context.Context,
tenantID uint64,
locationHash string,
) (*types.StoredResource, error) {
var resource types.StoredResource
err := r.db.WithContext(ctx).
Where(
"tenant_id = ? AND location_hash = ? AND state = ?",
tenantID,
locationHash,
types.ResourceStateActive,
).
First(&resource).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
return &resource, err
}
func (r *resourceRepository) MarkDeleted(ctx context.Context, id string) error {
return r.db.WithContext(ctx).Model(&types.StoredResource{}).Where("id = ?", id).
Updates(map[string]interface{}{"state": types.ResourceStateDeleted, "deleted_at": time.Now()}).Error
}
func (r *resourceRepository) CreateBinding(ctx context.Context, binding *types.ResourceBinding) error {
return r.db.WithContext(ctx).Clauses(clause.OnConflict{DoNothing: true}).Create(binding).Error
}
func (r *resourceRepository) CreateGrant(ctx context.Context, grant *types.ResourceAccessGrant) error {
return r.db.WithContext(ctx).Create(grant).Error
}
func (r *resourceRepository) GetValidGrant(
ctx context.Context,
tokenHash string,
now time.Time,
) (*types.ResourceAccessGrant, error) {
var grant types.ResourceAccessGrant
err := r.db.WithContext(ctx).
Where("token_hash = ? AND revoked_at IS NULL AND expires_at > ?", tokenHash, now).
First(&grant).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
return &grant, err
}
func (r *resourceRepository) DeleteExpiredGrants(ctx context.Context, before time.Time) error {
return r.db.WithContext(ctx).
Where("expires_at <= ? OR revoked_at IS NOT NULL", before).
Delete(&types.ResourceAccessGrant{}).Error
}