media

package
v1.18.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 27, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package media provides native media handling for DevClaw.

Index

Constants

This section is empty.

Variables

View Source
var AllowedMimeTypes = map[MediaType][]string{
	MediaTypeImage: {
		"image/jpeg",
		"image/png",
		"image/gif",
		"image/webp",
	},
	MediaTypeAudio: {
		"audio/mpeg",
		"audio/mp3",
		"audio/ogg",
		"audio/wav",
		"audio/x-wav",
		"audio/webm",
		"audio/mp4",
		"audio/x-m4a",
		"video/ogg",
	},
	MediaTypeDocument: {
		"application/pdf",
		"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
		"text/plain",
		"text/csv",
		"application/json",
		"text/markdown",
	},
}

AllowedMimeTypes defines permitted MIME types for each media category.

Functions

func AllowedExtensions

func AllowedExtensions() []string

AllowedExtensions returns file extensions for allowed types.

func DetectMimeType

func DetectMimeType(data []byte, filename string) string

DetectMimeType uses http.DetectContentType and extension heuristics.

func IsAllowedExtension

func IsAllowedExtension(ext string) bool

IsAllowedExtension checks if a file extension is allowed.

func IsAudio

func IsAudio(mimeType string) bool

IsAudio returns true if the MIME type is audio.

func IsDocument

func IsDocument(mimeType string) bool

IsDocument returns true if the MIME type is a document.

func IsImage

func IsImage(mimeType string) bool

IsImage returns true if the MIME type is an image.

Types

type EnrichmentConfig

type EnrichmentConfig struct {
	AutoEnrichImages    bool `yaml:"auto_enrich_images" json:"auto_enrich_images"`
	AutoEnrichAudio     bool `yaml:"auto_enrich_audio" json:"auto_enrich_audio"`
	AutoEnrichDocuments bool `yaml:"auto_enrich_documents" json:"auto_enrich_documents"`
}

EnrichmentConfig configures automatic media enrichment.

func DefaultEnrichmentConfig

func DefaultEnrichmentConfig() EnrichmentConfig

DefaultEnrichmentConfig returns default enrichment configuration.

type EnrichmentResult

type EnrichmentResult struct {
	MediaID     string
	Type        MediaType
	Description string // Vision description
	Transcript  string // Audio transcript
	TextContent string // Document text
	Metadata    map[string]any
}

EnrichmentResult contains extracted content from media.

type ExtractDocFunc

type ExtractDocFunc func(ctx context.Context, data []byte, mimeType string) (string, error)

ExtractDocFunc extracts text from documents.

type FileSystemStore

type FileSystemStore struct {
	// contains filtered or unexported fields
}

FileSystemStore implements MediaStore using local filesystem.

func NewFileSystemStore

func NewFileSystemStore(cfg StoreConfig, logger *slog.Logger) *FileSystemStore

NewFileSystemStore creates a new filesystem-based media store.

func (*FileSystemStore) Delete

func (s *FileSystemStore) Delete(ctx context.Context, id string) error

Delete removes media by ID.

func (*FileSystemStore) DeleteExpired

func (s *FileSystemStore) DeleteExpired(ctx context.Context) (int, error)

DeleteExpired removes all temporary media past their expiration.

func (*FileSystemStore) EnsureDir

func (s *FileSystemStore) EnsureDir() error

EnsureDir creates the storage directories if they don't exist.

func (*FileSystemStore) Get

Get retrieves media data by ID.

func (*FileSystemStore) GetBytes

func (s *FileSystemStore) GetBytes(ctx context.Context, id string) ([]byte, *StoredMedia, error)

GetBytes retrieves media data as bytes.

func (*FileSystemStore) List

func (s *FileSystemStore) List(ctx context.Context, filter ListFilter) ([]*StoredMedia, error)

List returns media matching the filter.

func (*FileSystemStore) Save

Save stores media data and returns metadata.

func (*FileSystemStore) URL

func (s *FileSystemStore) URL(id string) string

URL returns a URL for accessing the media.

type LLMMediaPrep

type LLMMediaPrep struct {
	MediaID    string
	Type       MediaType
	Base64Data string
	MimeType   string
	Caption    string
	FileSize   int64
}

LLMMediaPrep contains prepared media for LLM consumption.

type ListFilter

type ListFilter struct {
	Channel   string
	SessionID string
	Type      MediaType
	Temporary *bool
	Limit     int
	Offset    int
}

ListFilter filters media listings.

type MediaService

type MediaService struct {
	// contains filtered or unexported fields
}

MediaService provides channel-agnostic media handling.

func NewMediaService

func NewMediaService(store MediaStore, channelMgr *channels.Manager, cfg ServiceConfig, logger *slog.Logger, opts ...MediaServiceOption) *MediaService

NewMediaService creates a new media service.

func (*MediaService) Config

func (s *MediaService) Config() ServiceConfig

Config returns the service configuration.

func (*MediaService) Delete

func (s *MediaService) Delete(ctx context.Context, id string) error

Delete removes media by ID.

func (*MediaService) Enrich

func (s *MediaService) Enrich(ctx context.Context, mediaID string) (*EnrichmentResult, error)

Enrich extracts content from media.

func (*MediaService) Get

func (s *MediaService) Get(ctx context.Context, id string) ([]byte, *StoredMedia, error)

Get retrieves media by ID.

func (*MediaService) List

func (s *MediaService) List(ctx context.Context, filter ListFilter) ([]*StoredMedia, error)

List returns media matching the filter.

func (*MediaService) PrepareForLLM

func (s *MediaService) PrepareForLLM(ctx context.Context, mediaID string) (*LLMMediaPrep, error)

PrepareForLLM prepares media for LLM consumption.

func (*MediaService) ResolveMediaSource

func (s *MediaService) ResolveMediaSource(ctx context.Context, source map[string]any) ([]byte, string, string, error)

ResolveMediaSource resolves media from different sources (media_id, file_path, url).

func (*MediaService) SendToChannel

func (s *MediaService) SendToChannel(ctx context.Context, channelName, to string, mediaID string, caption string) error

SendToChannel sends media through the channel manager.

func (*MediaService) StartCleanup

func (s *MediaService) StartCleanup(ctx context.Context)

StartCleanup runs periodic cleanup of expired temporary media.

func (*MediaService) URL

func (s *MediaService) URL(id string) string

URL returns the URL for a media ID.

func (*MediaService) Upload

func (s *MediaService) Upload(ctx context.Context, req UploadRequest) (*StoredMedia, error)

Upload processes and stores uploaded media.

type MediaServiceOption

type MediaServiceOption func(*MediaService)

MediaServiceOption configures the media service.

func WithDocumentExtraction

func WithDocumentExtraction(fn ExtractDocFunc) MediaServiceOption

WithDocumentExtraction sets the document extraction function.

func WithEnrichmentConfig

func WithEnrichmentConfig(cfg EnrichmentConfig) MediaServiceOption

WithEnrichmentConfig sets the enrichment configuration.

func WithTranscription

func WithTranscription(fn TranscribeFunc) MediaServiceOption

WithTranscription sets the transcription function.

func WithURLValidator

func WithURLValidator(v URLValidator) MediaServiceOption

WithURLValidator sets the URL validator for SSRF protection.

func WithVision

func WithVision(fn VisionFunc) MediaServiceOption

WithVision sets the vision function.

type MediaStore

type MediaStore interface {
	// Save stores media data and returns metadata.
	Save(ctx context.Context, req SaveRequest) (*StoredMedia, error)

	// Get retrieves media data by ID.
	Get(ctx context.Context, id string) (io.ReadCloser, *StoredMedia, error)

	// GetBytes retrieves media data as bytes.
	GetBytes(ctx context.Context, id string) ([]byte, *StoredMedia, error)

	// Delete removes media by ID.
	Delete(ctx context.Context, id string) error

	// List returns media matching the filter.
	List(ctx context.Context, filter ListFilter) ([]*StoredMedia, error)

	// DeleteExpired removes all temporary media past their expiration.
	DeleteExpired(ctx context.Context) (int, error)

	// URL returns a URL for accessing the media.
	URL(id string) string
}

MediaStore defines the storage abstraction for media files.

type MediaType

type MediaType string

MediaType categorizes stored media.

const (
	MediaTypeImage    MediaType = "image"
	MediaTypeAudio    MediaType = "audio"
	MediaTypeVideo    MediaType = "video"
	MediaTypeDocument MediaType = "document"
)

func CategorizeType

func CategorizeType(mimeType string) MediaType

CategorizeType maps MIME type to MediaType.

type SaveRequest

type SaveRequest struct {
	Data      []byte
	Filename  string
	MimeType  string
	Type      MediaType
	Channel   string
	SessionID string
	Temporary bool
	TTL       time.Duration
	Metadata  map[string]any
}

SaveRequest contains data for storing media.

type ServiceConfig

type ServiceConfig struct {
	Enabled         bool   `yaml:"enabled" json:"enabled"`
	MaxImageSize    int64  `yaml:"max_image_size" json:"max_image_size"`
	MaxAudioSize    int64  `yaml:"max_audio_size" json:"max_audio_size"`
	MaxDocSize      int64  `yaml:"max_doc_size" json:"max_doc_size"`
	MaxUploadSize   int64  `yaml:"max_upload_size" json:"max_upload_size"`
	TempTTL         string `yaml:"temp_ttl" json:"temp_ttl"`
	CleanupEnabled  bool   `yaml:"cleanup_enabled" json:"cleanup_enabled"`
	CleanupInterval string `yaml:"cleanup_interval" json:"cleanup_interval"`
}

ServiceConfig contains validation limits.

func DefaultServiceConfig

func DefaultServiceConfig() ServiceConfig

DefaultServiceConfig returns default configuration.

func (ServiceConfig) MaxSizeForType

func (c ServiceConfig) MaxSizeForType(t MediaType) int64

MaxSizeForType returns the maximum size for a media type.

type StoreConfig

type StoreConfig struct {
	BaseDir     string `yaml:"base_dir" json:"base_dir"`
	TempDir     string `yaml:"temp_dir" json:"temp_dir"`
	BaseURL     string `yaml:"base_url" json:"base_url"`
	MaxFileSize int64  `yaml:"max_file_size" json:"max_file_size"`
}

StoreConfig configures FileSystemStore.

func DefaultStoreConfig

func DefaultStoreConfig() StoreConfig

DefaultStoreConfig returns default configuration.

type StoredMedia

type StoredMedia struct {
	ID        string         `json:"id"`
	Filename  string         `json:"filename"`
	MimeType  string         `json:"mime_type"`
	Type      MediaType      `json:"type"`
	Size      int64          `json:"size"`
	Channel   string         `json:"channel"`
	SessionID string         `json:"session_id,omitempty"`
	Temporary bool           `json:"temporary"`
	CreatedAt time.Time      `json:"created_at"`
	ExpiresAt *time.Time     `json:"expires_at,omitempty"`
	Metadata  map[string]any `json:"metadata,omitempty"`
}

StoredMedia represents persisted media metadata.

type TranscribeFunc

type TranscribeFunc func(ctx context.Context, audioData []byte, filename string) (string, error)

TranscribeFunc transcribes audio using Whisper API.

type URLValidator

type URLValidator interface {
	IsAllowed(url string) error
}

URLValidator validates URLs for security (SSRF protection).

type UploadRequest

type UploadRequest struct {
	Data      []byte
	Filename  string
	MimeType  string
	Channel   string
	SessionID string
	Temporary bool
}

UploadRequest contains data for uploading media.

type ValidationResult

type ValidationResult struct {
	MimeType string
	Type     MediaType
	Size     int64
	Valid    bool
	Error    string
}

ValidationResult contains validation output.

type Validator

type Validator struct {
	// contains filtered or unexported fields
}

Validator handles media validation.

func NewValidator

func NewValidator(config ServiceConfig) *Validator

NewValidator creates a new validator.

func (*Validator) Validate

func (v *Validator) Validate(data []byte, filename, mimeType string) (*ValidationResult, error)

Validate checks MIME type, size, and content.

type VisionFunc

type VisionFunc func(ctx context.Context, imageData []byte, mimeType string) (string, error)

VisionFunc describes an image using vision API.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL