feat: implement file upload handler and related functionality
- Added file upload handler to process both multipart and raw file uploads. - Implemented parsing logic for upload requests, including handling file metadata. - Introduced SaveFileDecodedInput structure for handling decoded file uploads. - Created unit tests for file upload parsing and validation. feat: add metadata retry configuration and functionality - Introduced MetadataRetryConfig to the application configuration. - Implemented MetadataRetryer to handle retrying metadata extraction for thoughts. - Added new tool for retrying failed metadata extractions. - Updated thought metadata structure to include status and timestamps for metadata processing. fix: enhance metadata normalization and error handling - Updated metadata normalization functions to track status and errors. - Improved handling of metadata extraction failures during thought updates and captures. - Ensured that metadata status is correctly set during various operations. refactor: streamline file saving logic in FilesTool - Refactored Save method in FilesTool to utilize new SaveDecoded method. - Simplified project and thought ID resolution logic during file saving.
This commit is contained in:
@@ -3,15 +3,19 @@ package metadata
|
||||
import (
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.warky.dev/wdevs/amcs/internal/config"
|
||||
thoughttypes "git.warky.dev/wdevs/amcs/internal/types"
|
||||
)
|
||||
|
||||
const (
|
||||
DefaultType = "observation"
|
||||
DefaultTopicFallback = "uncategorized"
|
||||
maxTopics = 10
|
||||
DefaultType = "observation"
|
||||
DefaultTopicFallback = "uncategorized"
|
||||
MetadataStatusComplete = "complete"
|
||||
MetadataStatusPending = "pending"
|
||||
MetadataStatusFailed = "failed"
|
||||
maxTopics = 10
|
||||
)
|
||||
|
||||
var allowedTypes = map[string]struct{}{
|
||||
@@ -36,18 +40,23 @@ func Fallback(capture config.CaptureConfig) thoughttypes.ThoughtMetadata {
|
||||
Type: normalizeType(capture.MetadataDefaults.Type),
|
||||
Source: normalizeSource(capture.Source),
|
||||
Attachments: []thoughttypes.ThoughtAttachment{},
|
||||
MetadataStatus: MetadataStatusComplete,
|
||||
}
|
||||
}
|
||||
|
||||
func Normalize(in thoughttypes.ThoughtMetadata, capture config.CaptureConfig) thoughttypes.ThoughtMetadata {
|
||||
out := thoughttypes.ThoughtMetadata{
|
||||
People: normalizeList(in.People, 0),
|
||||
ActionItems: normalizeList(in.ActionItems, 0),
|
||||
DatesMentioned: normalizeList(in.DatesMentioned, 0),
|
||||
Topics: normalizeList(in.Topics, maxTopics),
|
||||
Type: normalizeType(in.Type),
|
||||
Source: normalizeSource(in.Source),
|
||||
Attachments: normalizeAttachments(in.Attachments),
|
||||
People: normalizeList(in.People, 0),
|
||||
ActionItems: normalizeList(in.ActionItems, 0),
|
||||
DatesMentioned: normalizeList(in.DatesMentioned, 0),
|
||||
Topics: normalizeList(in.Topics, maxTopics),
|
||||
Type: normalizeType(in.Type),
|
||||
Source: normalizeSource(in.Source),
|
||||
Attachments: normalizeAttachments(in.Attachments),
|
||||
MetadataStatus: normalizeMetadataStatus(in.MetadataStatus),
|
||||
MetadataUpdatedAt: strings.TrimSpace(in.MetadataUpdatedAt),
|
||||
MetadataLastAttemptedAt: strings.TrimSpace(in.MetadataLastAttemptedAt),
|
||||
MetadataError: strings.TrimSpace(in.MetadataError),
|
||||
}
|
||||
|
||||
if len(out.Topics) == 0 {
|
||||
@@ -59,10 +68,48 @@ func Normalize(in thoughttypes.ThoughtMetadata, capture config.CaptureConfig) th
|
||||
if out.Source == "" {
|
||||
out.Source = Fallback(capture).Source
|
||||
}
|
||||
if out.MetadataStatus == "" {
|
||||
out.MetadataStatus = MetadataStatusComplete
|
||||
}
|
||||
if out.MetadataStatus == MetadataStatusComplete {
|
||||
out.MetadataError = ""
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
func MarkMetadataPending(base thoughttypes.ThoughtMetadata, capture config.CaptureConfig, attempt time.Time, err error) thoughttypes.ThoughtMetadata {
|
||||
out := Normalize(base, capture)
|
||||
out.MetadataStatus = MetadataStatusPending
|
||||
out.MetadataLastAttemptedAt = attempt.UTC().Format(time.RFC3339)
|
||||
if err != nil {
|
||||
out.MetadataError = strings.TrimSpace(err.Error())
|
||||
}
|
||||
out.MetadataUpdatedAt = strings.TrimSpace(base.MetadataUpdatedAt)
|
||||
return out
|
||||
}
|
||||
|
||||
func MarkMetadataFailed(base thoughttypes.ThoughtMetadata, capture config.CaptureConfig, attempt time.Time, err error) thoughttypes.ThoughtMetadata {
|
||||
out := Normalize(base, capture)
|
||||
out.MetadataStatus = MetadataStatusFailed
|
||||
out.MetadataLastAttemptedAt = attempt.UTC().Format(time.RFC3339)
|
||||
if err != nil {
|
||||
out.MetadataError = strings.TrimSpace(err.Error())
|
||||
}
|
||||
out.MetadataUpdatedAt = strings.TrimSpace(base.MetadataUpdatedAt)
|
||||
return out
|
||||
}
|
||||
|
||||
func MarkMetadataComplete(base thoughttypes.ThoughtMetadata, capture config.CaptureConfig, updatedAt time.Time) thoughttypes.ThoughtMetadata {
|
||||
out := Normalize(base, capture)
|
||||
out.MetadataStatus = MetadataStatusComplete
|
||||
timestamp := updatedAt.UTC().Format(time.RFC3339)
|
||||
out.MetadataUpdatedAt = timestamp
|
||||
out.MetadataLastAttemptedAt = timestamp
|
||||
out.MetadataError = ""
|
||||
return out
|
||||
}
|
||||
|
||||
func normalizeList(values []string, limit int) []string {
|
||||
seen := make(map[string]struct{}, len(values))
|
||||
result := make([]string, 0, len(values))
|
||||
@@ -100,6 +147,19 @@ func normalizeType(value string) string {
|
||||
return DefaultType
|
||||
}
|
||||
|
||||
func normalizeMetadataStatus(value string) string {
|
||||
switch strings.ToLower(strings.TrimSpace(value)) {
|
||||
case "", MetadataStatusComplete:
|
||||
return MetadataStatusComplete
|
||||
case MetadataStatusPending:
|
||||
return MetadataStatusPending
|
||||
case MetadataStatusFailed:
|
||||
return MetadataStatusFailed
|
||||
default:
|
||||
return MetadataStatusComplete
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeSource(value string) string {
|
||||
normalized := strings.TrimSpace(value)
|
||||
if normalized == "" {
|
||||
|
||||
Reference in New Issue
Block a user