Documentation
¶
Overview ¶
Package images prepares pictures for vision models and keeps them out of session files.
An image is normalised once (format checked, oversized pictures scaled down and re-encoded), written to a content-addressed Store and referred to in conversation history by a FileData part whose URI starts with URIScheme. Session files therefore hold a short reference instead of megabytes of base64; Expand swaps the references for the bytes just before each model request.
Index ¶
- Constants
- Variables
- func Expand(contents []*genai.Content, s *Store) []*genai.Content
- func HasRefs(contents []*genai.Content) bool
- func IsImagePath(p string) bool
- func MentionedPaths(prompt string) []string
- func Mentions(prompt string) []string
- func Part(img *Image) *genai.Part
- type Image
- type Options
- type Store
Constants ¶
const ( DefaultMaxDimension = 1568 DefaultMaxInput = 20 << 20 MaxEncodedBytes = 3_750_000 )
Defaults suit every supported provider: Anthropic recommends at most 1568 pixels on the long edge and caps images at 5 MB (3.75 MB before base64).
const ToolResultKey = "image_uri"
ToolResultKey marks a tool result that carries an image: its value is an Image.URI, and Expand places the picture right after the result.
const URIScheme = "blitz-image:"
URIScheme prefixes references to stored images ("blitz-image:<sha256>").
Variables ¶
var ErrNoClipboardImage = errors.New("no image on the clipboard")
ErrNoClipboardImage means the clipboard holds no image (or no clipboard tool is installed).
var ErrNotImage = errors.New("not a PNG, JPEG, GIF or WebP image")
ErrNotImage is returned for data that isn't a supported image.
var ReadClipboard = readClipboard
ReadClipboard returns the clipboard image as PNG bytes using the platform's own tools: osascript on macOS, wl-paste or xclip on Linux, and PowerShell on Windows. It is a variable so tests can replace it.
Functions ¶
func Expand ¶
Expand returns contents with stored-image references replaced by the image bytes (and tool-result images appended after their result). Inputs are never modified: they are usually the session's own events. A missing image, or a nil store, becomes a short note instead so the request still works.
func IsImagePath ¶
IsImagePath reports whether a file name has a supported image extension.
func MentionedPaths ¶
MentionedPaths returns every @path in a prompt (images or not), in order and without duplicates; trailing punctuation isn't part of a bare path.
Types ¶
type Image ¶
type Image struct {
Name string // file name for people (no directory)
Data []byte
MIME string
Width, Height int
SHA256 string // of Data
OriginalBytes int
Resized bool
// Size is len(Data), kept for an image whose data stays elsewhere (one
// held by the Blitz service).
Size int
}
Image is a prepared picture.
func Prepare ¶
Prepare validates data and scales it down when it is larger than the limits allow. The format is detected from the bytes, never the name.
type Options ¶
type Options struct {
MaxDimension int // longest edge after scaling (default 1568)
MaxInput int64 // largest file accepted (default 20 MB)
}
Options bound what Prepare accepts and produces.
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store keeps prepared images on disk, named by their SHA-256, readable only by the owner. Identical images are stored once.