imagegen

package
v0.12.3 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package imagegen provides provider-neutral image generation primitives.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsContextError

func IsContextError(err error) bool

IsContextError lets the tool distinguish cancellation/deadline failures from provider errors without parsing strings.

func XAIImageAspectRatios

func XAIImageAspectRatios() []string

XAIImageAspectRatios returns the supported xAI image geometry values without exposing mutable package state to capability consumers.

func XAIImageResolutions

func XAIImageResolutions() []string

XAIImageResolutions returns the supported xAI resolution values.

Types

type Client

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

Client implements the shared synchronous Images transport with a protocol-specific request encoder.

func NewClient

func NewClient(cfg ClientConfig) (*Client, error)

NewClient validates config eagerly so a misconfigured image provider can be omitted from the agent tool list instead of failing only after a tool call.

func NewXAIImagesClient

func NewXAIImagesClient(cfg ClientConfig) (*Client, error)

NewXAIImagesClient constructs the xAI-native image adapter. Keeping this protocol separate prevents OpenAI-only fields from crossing the managed xAI boundary and makes the approved request match the dispatched payload.

func (*Client) Generate

func (c *Client) Generate(ctx context.Context, input Request) (Result, error)

Generate performs exactly one upstream image-generation request.

type ClientConfig

type ClientConfig struct {
	Protocol Protocol
	BaseURL  string
	APIKey   string
	Headers  map[string]string
	// Credential resolves a managed bearer token immediately before dispatch.
	// When set, an empty token fails closed and protected headers are applied
	// after configured headers.
	Credential CredentialFunc
	Model      string
	// AssetHosts explicitly allows temporary image URL hosts in addition to the
	// provider API host. Values are exact hosts or "*.example.com" wildcards.
	AssetHosts []string

	HTTPClient   *http.Client
	MaxImageSize int64

	// AllowInsecureHTTP exists for local tests and development endpoints. Real
	// provider configuration must leave it false.
	AllowInsecureHTTP bool
}

ClientConfig describes one concrete image-generation endpoint. Secrets are deliberately kept out of result and error types so callers can safely serialize those values into session records.

type CredentialFunc

type CredentialFunc func(context.Context) (token string, headers map[string]string, err error)

CredentialFunc resolves request-scoped managed authorization without exposing bearer tokens to image runtime configuration or session records.

type Generator

type Generator interface {
	Generate(context.Context, Request) (Result, error)
}

Generator is the seam used by the generate_image tool and provider adapters.

func NewGenerator

func NewGenerator(cfg ClientConfig) (Generator, error)

NewGenerator constructs the exact protocol adapter selected by provider capability resolution. Callers must not infer a protocol from a provider or model name.

type Image

type Image struct {
	Data          []byte
	MIMEType      string
	Width         int
	Height        int
	SourceURL     string
	RevisedPrompt string
}

Image is validated binary output. SourceURL is retained only as provenance; callers should persist Data in managed local storage instead of depending on the provider's temporary URL.

type Protocol

type Protocol string

Protocol identifies the upstream image-generation wire protocol.

const (
	// ProtocolOpenAIImages is the POST /images/generations JSON protocol used
	// by OpenAI and compatible providers such as BigModel.
	ProtocolOpenAIImages Protocol = "openai_images"
	// ProtocolXAIImages is xAI's image-generation protocol. Although it shares
	// the OpenAI Images endpoint shape, its native output controls are
	// aspect_ratio and resolution rather than size.
	ProtocolXAIImages Protocol = "xai_images"
	// ProtocolTokenPlanMultimodal is the synchronous multimodal-generation
	// protocol exposed by Alibaba Token Plan. It is deliberately distinct from
	// both OpenAI Images and the general DashScope asynchronous task API.
	ProtocolTokenPlanMultimodal Protocol = "token_plan_multimodal"
)

type Request

type Request struct {
	Prompt         string
	Size           string
	AspectRatio    string
	Resolution     string
	Quality        string
	Background     string
	OutputFormat   string
	ResponseFormat string
	Count          int
}

Request is the provider-neutral subset supported by the POC protocol. Empty optional values are omitted so older OpenAI-compatible gateways do not reject newer fields they do not understand.

type Result

type Result struct {
	Images []Image
}

Result contains all images returned by one billable upstream invocation.

type TokenPlanMultimodalClient

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

TokenPlanMultimodalClient implements Alibaba Token Plan's synchronous image endpoint. Token Plan uses the DashScope-style messages schema on a plan-specific host; it is not an OpenAI Images endpoint and must not send the X-DashScope-Async header used by the general asynchronous task API.

func NewTokenPlanMultimodalClient

func NewTokenPlanMultimodalClient(cfg ClientConfig) (*TokenPlanMultimodalClient, error)

NewTokenPlanMultimodalClient validates the endpoint eagerly so an invalid route never enters the agent's tool catalog.

func (*TokenPlanMultimodalClient) Generate

func (c *TokenPlanMultimodalClient) Generate(ctx context.Context, input Request) (Result, error)

Generate performs exactly one synchronous, billable upstream request and requires exactly one image result. There is intentionally no application retry: retrying an ambiguous POST could create and charge for another image.

Jump to

Keyboard shortcuts

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