Documentation
¶
Overview ¶
Package attachmentprocessor scans span attributes for base64-encoded LLM attachments and replaces them with Braintrust attachment references after uploading the data to object storage.
Index ¶
- Variables
- func BuildHeuristic(formats []Format) *regexp.Regexp
- func NewTransformedSpan(delegate sdktrace.ReadOnlySpan, overrides map[attribute.Key]string) sdktrace.ReadOnlySpan
- type Format
- type NoopUploader
- type Processor
- type Reference
- type S3Uploader
- type UploadFunc
- type Uploader
- type UploaderConfig
Constants ¶
This section is empty.
Variables ¶
var Formats = []Format{
openAIFormat,
bedrockFormat,
anthropicFormat,
geminiFormat,
}
Formats lists all supported vendor attachment formats, checked in order during tree traversal.
Functions ¶
func BuildHeuristic ¶
BuildHeuristic compiles a single regex from all format heuristic fragments.
func NewTransformedSpan ¶
func NewTransformedSpan(delegate sdktrace.ReadOnlySpan, overrides map[attribute.Key]string) sdktrace.ReadOnlySpan
NewTransformedSpan creates a transformedSpan that overrides the given attribute keys with new values. All other attributes are preserved. Override entries for keys not present in the original span are appended to the attribute list rather than silently dropped.
Types ¶
type Format ¶
type Format struct {
// Name is a human-readable label for logging and test-coverage tracking.
Name string
// HeuristicFragment is a regex fragment for the combined fast-path
// heuristic. Duplicate fragments across formats are de-duplicated.
HeuristicFragment string
// Match returns true if this format should handle the given JSON node.
// parentKey is the key of this node within its parent (empty for root/array items).
Match func(parentKey string, node any) bool
// Replace builds the replacement value. It receives the matched node and
// an Uploader to enqueue the binary data. It returns the replacement value
// and the decoded binary data, or nil if replacement should be skipped.
Replace func(node any, upload UploadFunc) (replacement any, ok bool)
}
Format describes how to detect and replace a provider-specific base64 attachment structure. Each vendor is a self-contained unit; adding a new provider is a matter of appending an entry to Formats.
type NoopUploader ¶
type NoopUploader struct {
// contains filtered or unexported fields
}
NoopUploader is an uploader that accepts all jobs but does nothing. Useful for testing the processor in isolation.
func (*NoopUploader) Enqueue ¶
func (u *NoopUploader) Enqueue(_ Reference, _ []byte) bool
Enqueue accepts the job but does nothing.
func (*NoopUploader) ForceFlush ¶
func (u *NoopUploader) ForceFlush(_ time.Duration) bool
ForceFlush is a no-op that always succeeds.
func (*NoopUploader) IsShutdown ¶
func (u *NoopUploader) IsShutdown() bool
IsShutdown returns true if the uploader has been shut down.
func (*NoopUploader) Shutdown ¶
func (u *NoopUploader) Shutdown()
Shutdown marks the uploader as shut down.
type Processor ¶
type Processor struct {
// contains filtered or unexported fields
}
Processor scans JSON strings for base64 attachments across multiple LLM provider formats, uploads them, and returns modified JSON with attachment references.
func NewProcessor ¶
NewProcessor creates a processor with the given uploader, logger, and the default set of vendor formats.
func NewProcessorWithFormats ¶
NewProcessorWithFormats creates a processor with an explicit set of formats.
func (*Processor) ProcessAndUpload ¶
ProcessAndUpload scans json for base64 attachments, uploads them, and returns the modified JSON. Returns the original string unchanged when no attachments are found or if the uploader has shut down.
JSON parse errors on individual spans are silently skipped — they don't affect processing of subsequent spans. The uploader shuts itself down on upload failures (network errors, auth errors, etc.), which causes all future calls to bail out via IsShutdown().
type Reference ¶
type Reference struct {
Type string `json:"type"`
ContentType string `json:"content_type"`
Filename string `json:"filename"`
Key string `json:"key"`
}
Reference is the JSON-serialisable object that replaces inline base64 attachment data on a span. Its shape is the cross-SDK Braintrust attachment reference format.
func NewReference ¶
NewReference creates a reference with a freshly-generated UUID key.
type S3Uploader ¶
type S3Uploader struct {
// contains filtered or unexported fields
}
S3Uploader uploads attachments in the background via signed URLs.
func NewS3Uploader ¶
func NewS3Uploader(cfg UploaderConfig) *S3Uploader
NewS3Uploader creates and returns a new S3 uploader. The background worker starts lazily on the first Enqueue call.
func (*S3Uploader) Enqueue ¶
func (u *S3Uploader) Enqueue(ref Reference, data []byte) bool
Enqueue adds an upload job. Returns false if the uploader is shut down or the queue is full.
func (*S3Uploader) ForceFlush ¶
func (u *S3Uploader) ForceFlush(timeout time.Duration) bool
ForceFlush blocks until all currently-enqueued uploads complete or timeout expires.
func (*S3Uploader) IsShutdown ¶
func (u *S3Uploader) IsShutdown() bool
IsShutdown returns true if the uploader has been shut down.
func (*S3Uploader) Shutdown ¶
func (u *S3Uploader) Shutdown()
Shutdown stops the uploader, waiting up to a generous timeout for pending uploads. Safe to call multiple times.
type UploadFunc ¶
UploadFunc enqueues an attachment for background upload. Returns false if the uploader has shut down or the queue is full.
type Uploader ¶
type Uploader interface {
// Enqueue adds an upload job. Returns false if the uploader is shut down
// or the queue is full.
Enqueue(ref Reference, data []byte) bool
// ForceFlush blocks until all currently-enqueued uploads complete or
// timeout expires.
ForceFlush(timeout time.Duration) bool
// Shutdown stops the uploader, waiting up to a generous timeout for
// pending uploads.
Shutdown()
// IsShutdown returns true if the uploader has been shut down.
IsShutdown() bool
}
Uploader enqueues attachment data for background upload.
type UploaderConfig ¶
type UploaderConfig struct {
APIURL string
APIKey string
OrgID string // If empty, resolved via login endpoint.
HTTPClient *http.Client
Logger logger.Logger
MaxRetries int
InitialBackoff time.Duration
RequestTimeout time.Duration
QueueSize int
ShutdownTimeout time.Duration
LoginURL string // App URL for login endpoint (e.g. "https://www.braintrust.dev").
}
UploaderConfig holds configuration for the S3 uploader.