images

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2026 License: Apache-2.0 Imports: 24 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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).

View Source
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.

View Source
const URIScheme = "blitz-image:"

URIScheme prefixes references to stored images ("blitz-image:<sha256>").

Variables

View Source
var ErrNoClipboardImage = errors.New("no image on the clipboard")

ErrNoClipboardImage means the clipboard holds no image (or no clipboard tool is installed).

View Source
var ErrNotImage = errors.New("not a PNG, JPEG, GIF or WebP image")

ErrNotImage is returned for data that isn't a supported image.

View Source
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

func Expand(contents []*genai.Content, s *Store) []*genai.Content

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 HasRefs

func HasRefs(contents []*genai.Content) bool

HasRefs reports whether any content refers to a stored image.

func IsImagePath

func IsImagePath(p string) bool

IsImagePath reports whether a file name has a supported image extension.

func MentionedPaths

func MentionedPaths(prompt string) []string

MentionedPaths returns every @path in a prompt (images or not), in order and without duplicates; trailing punctuation isn't part of a bare path.

func Mentions

func Mentions(prompt string) []string

Mentions returns the image files referenced as @path in a prompt, in order and without duplicates. Other @paths are left for the model.

func Part

func Part(img *Image) *genai.Part

Part is the history reference for img.

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

func Prepare(name string, data []byte, o Options) (*Image, error)

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.

func (*Image) Summary

func (img *Image) Summary() string

Summary is a short human description: "shot.png 1280×720, 210 KB".

func (*Image) URI

func (img *Image) URI() string

URI is the reference stored in conversation history.

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.

func OpenStore

func OpenStore(dir string) (*Store, error)

OpenStore creates dir (mode 700) if needed.

func (*Store) Dir

func (s *Store) Dir() string

Dir returns the store's directory.

func (*Store) Get

func (s *Store) Get(uri string) ([]byte, string, error)

Get loads a stored image by the URI from Image.URI.

func (*Store) Prune

func (s *Store) Prune(maxAge time.Duration) (int, error)

Prune deletes images not used for maxAge and reports how many went.

func (*Store) Put

func (s *Store) Put(img *Image) error

Put saves img unless an identical image is already stored, in which case its age is reset so pruning keeps it.

Jump to

Keyboard shortcuts

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