vision

package
v0.68.2 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2026 License: AGPL-3.0 Imports: 23 Imported by: 0

Documentation

Overview

Package vision turns verified image bytes into bounded text.

Index

Constants

View Source
const (
	// MaxRendererPayloadBytes is the hard provider image payload ceiling.
	MaxRendererPayloadBytes = 5 * 1024 * 1024
	// MaxImageInputBytes aliases the canonical session-ingress ceiling. Together
	// with MaxImagePixels, it protects model and Xberg paths from compressed-byte
	// and decompression-bomb inputs.
	MaxImageInputBytes = ai.MaxImageInputBytes
	MaxImagePixels     = 50_000_000
)
View Source
const (
	// BaselineVLMTimeout leaves five seconds of Enricher's 15 second message
	// budget for Xberg after a slow model attempt.
	BaselineVLMTimeout = 10 * time.Second

	// DescribeTimeout is deliberately longer than BaselineVLMTimeout: baseline
	// runs inside a message-enrichment budget with an Xberg fallback waiting,
	// while an agent's explicit image question has no fallback and no other
	// deadline to protect.
	DescribeTimeout = 60 * time.Second
)

Variables

View Source
var ErrNoVisionModel = errors.New("no vision model configured")

ErrNoVisionModel reports that this deployment has no usable vision model, so there is nothing for Describe to call.

Functions

func ExtractWithXberg

func ExtractWithXberg(ctx context.Context, path string) (string, error)

ExtractWithXberg shells out to the Xberg CLI to extract text from a file. It returns an error when the binary is missing or extraction fails.

func NormalizeXbergBaseline

func NormalizeXbergBaseline(text string) string

NormalizeXbergBaseline makes local OCR conform to the same durable contract without inventing scene understanding that Xberg does not provide.

func PrepareRendererPayloadContext

func PrepareRendererPayloadContext(ctx context.Context, data []byte, cfg image.Config, mime string) ([]byte, string, error)

PrepareRendererPayloadContext keeps original pixels and dimensions untouched whenever they fit the renderer payload ceiling. Both baseline rendering and active provider hydration use this adaptive payload preparation. Only a hard payload overflow causes reduction; no fixed dimension ceiling or tiling is applied. The standard-library decode/encode calls themselves are not interruptible, so it checks ctx around each potentially expensive step.

func ValidateBudget

func ValidateBudget(data []byte) (image.Config, error)

ValidateBudget rejects unsafe image bytes before full decode for callers that do not have a declared MIME to compare.

func ValidateImage

func ValidateImage(data []byte, declaredMIME string) (image.Config, string, error)

ValidateImage validates the security budget and requires the declared MIME to match the decoder's actual format. Canonical media never trusts an extension or provider-supplied content type.

Types

type BaselineRenderer

type BaselineRenderer interface {
	Baseline(context.Context, Request) (ai.ImageBaseline, error)
}

BaselineRenderer is the narrow contract session-media enrichment needs. Implementations must honor ctx promptly. Enricher protects its message deadline from non-cooperative renderers, but at most two abandoned calls may continue in the background until their implementation returns.

type Options

type Options struct {
	Model  ai.Model
	APIKey string
	Build  StreamBuilder
}

Options configures a Service.

type Request

type Request struct {
	Data     []byte
	MimeType string
}

Request is one image to render as stable baseline text.

type Service

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

Service owns one resolved baseline provider. Canonical enrichment resolves a fresh Service for each image-bearing message so deployment settings are not runner-snapshot state.

func New

func New(opts Options) *Service

New creates a service. A missing or invalid model is intentionally an Xberg-only service rather than a construction error.

func NewFromSnapshot

func NewFromSnapshot(snap *config.Snapshot, build StreamBuilder) *Service

NewFromSnapshot builds a service from one current application snapshot.

func (*Service) Baseline

func (s *Service) Baseline(ctx context.Context, req Request) (ai.ImageBaseline, error)

Baseline produces the sole canonical generic OCR + scene representation. A ready result is returned only after an explicit clean stop and exact contract validation. Model failures fall back to Xberg using the verified same bytes.

func (*Service) CanDescribeImages added in v0.66.0

func (s *Service) CanDescribeImages() bool

CanDescribeImages reports whether the resolved vision model may receive image bytes. Assigning a model to the vision tier is the operator's declaration that it reads images, so a model with no declared inputs passes; only an explicit text-only declaration fails closed.

func (*Service) Describe added in v0.65.0

func (s *Service) Describe(ctx context.Context, req Request, instruction string) (string, error)

Describe answers one caller-supplied question about an image. Unlike Baseline it does not fall back to Xberg: a custom question has no meaningful answer from raw OCR text, and silently returning a transcript would look like an answer while being none. An empty instruction defaults to transcription.

func (*Service) ModelConfigured

func (s *Service) ModelConfigured() bool

type StreamBuilder

type StreamBuilder func(api, apiKey, baseURL string) (providers.StreamFunc, error)

StreamBuilder constructs a provider stream for one API/credential pair.

Jump to

Keyboard shortcuts

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