imagegen

package
v0.16.521 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2026 License: AGPL-3.0 Imports: 27 Imported by: 0 modules, 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 imagegen makes pictures with agent CLIs (Codex by default): a Codex job runs `codex exec` and watches the folder Codex saves each picture to (~/.codex/generated_images/<thread>/), so every picture is known the moment it is drawn, not when the run ends. Codex's --json stream says nothing of pictures; the folder does.

A job belongs to whoever asked: a chat (an agent's generate_images call), a Sinopia design. Its pictures are copied into the job's own folder (and into the asker's folder when one is named), where the page shows them from.

Index

Constants

View Source
const (
	// MaxCount is the most pictures one job draws.
	MaxCount = 8
	// RunTimeout bounds one job.
	RunTimeout = 12 * time.Minute

	// MaxRunning is how many jobs draw at once on a machine.
	MaxRunning = 4
)
View Source
const (
	// ToolGenerate and ToolWait are the agents' MCP tools.
	ToolGenerate = "generate_images"
	ToolWait     = "wait_images"
	// WaitMax is how long a call waits for the pictures; CodexWaitMax is the most for a Codex caller, which ends a
	// tool call at a minute.
	WaitMax      = 5 * time.Minute
	CodexWaitMax = 55 * time.Second
)

Variables

View Source
var (
	ErrBadRequest = errors.New("bad request")
	ErrBusy       = errors.New("busy")
)

ErrBadRequest is a request that cannot be drawn; ErrBusy, one that waits for the jobs already drawing.

View Source
var BusyPoll = 2 * time.Second

BusyPoll is how often a call that found every slot drawing tries again.

View Source
var ErrNoMedia = errors.New("Higgsfield is not connected: the person connects «Higgsfield (MCP)» in Settings → Connections")

ErrNoMedia: a request for Higgsfield on a machine where it is not connected.

Functions

func Brief

func Brief(r Request) string

Brief is what Codex is told for a request.

func Report

func Report(s Snapshot) string

Report is what the agent is told of a job. Its first line carries the job's id, the chat's card finds the job by.

Types

type Image

type Image struct {
	N    int    `json:"n"`
	Path string `json:"path"`
	// Copy is the picture in the asker's folder ("" when none was asked or the copy failed).
	Copy string `json:"copy,omitempty"`
}

Image is one picture drawn.

type Media

type Media interface {
	// Available says whether the provider is connected on this machine.
	Available() bool
	// Draw makes one picture and gives the path of its file.
	Draw(ctx context.Context, d MediaDraw) (string, error)
}

Media draws pictures with a media provider of the person's (Higgsfield: their plan's credits), one picture a call. It is the same service the agents' generate_images uses with a provider, so its price checks and daily cap hold here too.

type MediaDraw

type MediaDraw struct {
	// Model is the provider's model ("" for its default).
	Model  string
	Prompt string
	// Ratio is the frame, "1:1", "16:9", … ("" for the model's own).
	Ratio string
	// Reference is a picture file the model starts from ("" for none): the picture being changed, the marks on it.
	Reference string
}

MediaDraw is one picture asked of a media provider.

type Mode

type Mode string

Mode says how a job changes its source.

const (
	// ModeText changes the picture as the words say.
	ModeText Mode = "text"
	// ModeMark follows what is drawn on the picture.
	ModeMark Mode = "mark"
	// ModeNote changes the area in a frame.
	ModeNote Mode = "note"
	// ModeErase removes what is painted over.
	ModeErase Mode = "erase"
	// ModeCut removes the background.
	ModeCut Mode = "cut"
	// ModeSize recomposes the picture to another format.
	ModeSize Mode = "size"
)

type Provider

type Provider string

Provider selects the CLI that draws the pictures. Antigravity is experimental.

const (
	Codex       Provider = "codex"
	Antigravity Provider = "antigravity"
	// Higgsfield draws through the person's media provider (media.go): it spends their credits.
	Higgsfield Provider = "higgsfield"
)

type Request

type Request struct {
	Provider Provider `json:"provider,omitempty"`
	// Model is the provider's model (Higgsfield only; "" for its default).
	Model  string `json:"model,omitempty"`
	Prompt string `json:"prompt"`
	Count  int    `json:"count"`
	Shape  Shape  `json:"shape,omitempty"`
	// Transparent asks for a transparent background.
	Transparent bool `json:"transparent,omitempty"`
	// Conv is the chat that asked ("" for none); Owner says who else did (e.g. "design:<id>").
	Conv  string `json:"conv,omitempty"`
	Owner string `json:"owner,omitempty"`
	// CopyTo is a folder the pictures are also copied into ("" for none), as <Name>-<n>.png.
	CopyTo string `json:"copyTo,omitempty"`
	// SaveOK says, right before each copy, whether CopyTo (its links followed) may still receive the pictures; a
	// request with CopyTo and no SaveOK copies nothing.
	SaveOK func(real string) bool `json:"-"`
	Name   string                 `json:"name,omitempty"`
	// Source is a picture to change instead of drawing a new one ("" for a new one): see edit.go. Mode says how, Format
	// is the frame of ModeSize, Overlay a PNG with what the person drew on it. SourceOK says, when the job starts,
	// whether this source may be read; a request with a Source and no SourceOK is refused.
	Source   string            `json:"source,omitempty"`
	Mode     Mode              `json:"mode,omitempty"`
	Format   string            `json:"format,omitempty"`
	Overlay  []byte            `json:"overlay,omitempty"`
	SourceOK func(string) bool `json:"-"`
}

Request is what to draw.

func Normalize

func Normalize(r Request) (Request, error)

Normalize checks and fills a request.

type Service

type Service struct {
	// Dir is where the jobs' pictures are kept: <Dir>/<job id>/<n>.png.
	Dir string
	// CodexHome is Codex's folder (~/.codex).
	CodexHome string
	// Env is the environment `codex exec` runs with (the agents' sign-in included); nil for this process's.
	Env func() []string
	// Binary is the codex binary; "" for "codex".
	Binary string
	// DefaultProvider is used when a request omits provider; empty means Codex.
	DefaultProvider Provider
	// AntigravityBinary is the official agy executable (empty: agy on PATH).
	AntigravityBinary string
	// AntigravityHome is its application data directory (empty: ~/.gemini/antigravity-cli).
	AntigravityHome string
	// AntigravityEnv is separate from Codex's Env; nil inherits this process's environment.
	AntigravityEnv func() []string
	// Media draws the pictures of provider Higgsfield (nil: it is refused).
	Media Media
	// Changed is told a job's new state; nil for nothing.
	Changed func(Snapshot)
	// contains filtered or unexported fields
}

Service runs and remembers the jobs.

func (*Service) Get

func (s *Service) Get(id string) (Snapshot, bool)

Get is a job's snapshot.

func (*Service) ImagePath

func (s *Service) ImagePath(id string, n int) (string, bool)

ImagePath is the file of a job's n-th picture, when there is one.

func (*Service) List

func (s *Service) List(conv, owner string) []Snapshot

List gives the jobs of a chat or an owner (either may be ""), oldest first.

func (*Service) Start

func (s *Service) Start(r Request) (Snapshot, error)

Start begins a job and gives its first snapshot.

func (*Service) Wait

func (s *Service) Wait(ctx context.Context, id string) (Snapshot, bool)

Wait waits for a job to end, or for ctx; it gives the job as it is then.

type Shape

type Shape string

Shape is the frame asked for.

const (
	Square    Shape = "square"
	Landscape Shape = "landscape"
	Portrait  Shape = "portrait"
)

type Snapshot

type Snapshot struct {
	Provider Provider `json:"provider,omitempty"`
	Model    string   `json:"model,omitempty"`
	ID       string   `json:"id"`
	Prompt   string   `json:"prompt"`
	Count    int      `json:"count"`
	Shape    Shape    `json:"shape,omitempty"`
	Conv     string   `json:"conv,omitempty"`
	Owner    string   `json:"owner,omitempty"`
	// Source and Mode: the picture this job changes, and how; absent for a new picture.
	Source    string  `json:"source,omitempty"`
	Mode      Mode    `json:"mode,omitempty"`
	State     State   `json:"state"`
	Images    []Image `json:"images"`
	Error     string  `json:"error,omitempty"`
	StartedAt int64   `json:"startedAt"`
	EndedAt   int64   `json:"endedAt,omitempty"`
}

Snapshot is a job as it is now.

type State

type State string

State is where a job is.

const (
	Running State = "running"
	Done    State = "done"
	Failed  State = "failed"
)

type Tools

type Tools struct {
	S *Service
	// Caller is the calling chat ("" when it could not be told), Why says why not.
	Caller, Why string
	// Cwd is the chat's folder, a relative save_to is taken from.
	Cwd string
	// Codex is true for a Codex caller.
	Codex bool
	// Allowed says whether a folder may receive the pictures (inside the workspace).
	Allowed func(path string) bool
	// Readable says whether a picture may be changed (source).
	Readable func(path string) bool
	// Media draws a picture with a media provider (Higgsfield) when a call names a provider or a model of its own; nil: the
	// provider and model arguments are refused.
	Media interface {
		Image(ctx context.Context, args *jsvalue.Object) *jsvalue.Object
	}
}

Tools are the picture tools for one calling agent.

func (Tools) Call

func (t Tools) Call(ctx context.Context, name string, args *jsvalue.Object) *jsvalue.Object

func (Tools) Has

func (Tools) Has(name string) bool

func (Tools) Instructions

func (Tools) Instructions() string

func (Tools) Tools

func (Tools) Tools() []any

Jump to

Keyboard shortcuts

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