feat(kol): store KOL prompt content in database with object storage fallback
Add prompt_content columns to kol_prompts and kol_prompt_revisions so prompt bodies live alongside their metadata, eliminating an extra round trip to object storage for the common read path while keeping the old asset key as a fallback for legacy rows. Reads go through a singleflight-deduped, cache-backed loader that prefers the database column, falls back to the asset key, and tolerates missing objects via a new objectstorage.ErrObjectNotFound returned by both the Aliyun OSS and MinIO clients on 404/NoSuchKey responses. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -141,6 +141,9 @@ func (c *aliyunClient) GetBytes(ctx context.Context, objectKey string) ([]byte,
|
||||
zap.String("object_key", objectKey),
|
||||
zap.Error(err),
|
||||
)
|
||||
if isAliyunObjectNotFound(err) {
|
||||
return nil, fmt.Errorf("%w: %s", ErrObjectNotFound, objectKey)
|
||||
}
|
||||
return nil, fmt.Errorf("get aliyun object: %w", err)
|
||||
}
|
||||
defer reader.Close()
|
||||
@@ -158,6 +161,16 @@ func (c *aliyunClient) GetBytes(ctx context.Context, objectKey string) ([]byte,
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func isAliyunObjectNotFound(err error) bool {
|
||||
serviceErr, ok := err.(oss.ServiceError)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
return serviceErr.StatusCode == 404 ||
|
||||
serviceErr.Code == "NoSuchKey" ||
|
||||
serviceErr.Code == "NoSuchObject"
|
||||
}
|
||||
|
||||
func (c *aliyunClient) Exists(ctx context.Context, objectKey string) (bool, error) {
|
||||
if err := c.Validate(); err != nil {
|
||||
return false, err
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
)
|
||||
|
||||
var ErrNotConfigured = errors.New("object storage is not configured")
|
||||
var ErrObjectNotFound = errors.New("object storage object not found")
|
||||
|
||||
type Client interface {
|
||||
Validate() error
|
||||
|
||||
@@ -119,6 +119,9 @@ func (c *minioClient) GetBytes(ctx context.Context, objectKey string) ([]byte, e
|
||||
zap.String("object_key", objectKey),
|
||||
zap.Error(err),
|
||||
)
|
||||
if isMinIOObjectNotFound(err) {
|
||||
return nil, fmt.Errorf("%w: %s", ErrObjectNotFound, objectKey)
|
||||
}
|
||||
return nil, fmt.Errorf("get minio object: %w", err)
|
||||
}
|
||||
defer reader.Close()
|
||||
@@ -130,11 +133,21 @@ func (c *minioClient) GetBytes(ctx context.Context, objectKey string) ([]byte, e
|
||||
zap.String("object_key", objectKey),
|
||||
zap.Error(err),
|
||||
)
|
||||
if isMinIOObjectNotFound(err) {
|
||||
return nil, fmt.Errorf("%w: %s", ErrObjectNotFound, objectKey)
|
||||
}
|
||||
return nil, fmt.Errorf("read minio object: %w", err)
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func isMinIOObjectNotFound(err error) bool {
|
||||
responseErr := minio.ToErrorResponse(err)
|
||||
return responseErr.StatusCode == 404 ||
|
||||
responseErr.Code == "NoSuchKey" ||
|
||||
responseErr.Code == "NoSuchObject"
|
||||
}
|
||||
|
||||
func (c *minioClient) Exists(ctx context.Context, objectKey string) (bool, error) {
|
||||
if err := c.Validate(); err != nil {
|
||||
return false, err
|
||||
|
||||
Reference in New Issue
Block a user