object

package
v0.1.20 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package object defines provider-agnostic types and the Provider interface for object generation operations.

This package sits at the innermost domain layer and must not depend on any other internal pkg/ packages. Providers implement the Provider interface to translate between their API and the request/response types declared here.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoProvider indicates the Client has no underlying Provider configured.
	ErrNoProvider = errors.New("object: no provider configured")

	// ErrInvalidRequest indicates the Request is malformed or missing
	// required fields.
	ErrInvalidRequest = errors.New("object: invalid request")

	// ErrProviderUnavailable indicates the upstream provider is temporarily
	// unreachable or returned a transient failure.
	ErrProviderUnavailable = errors.New("object: provider unavailable")

	// ErrRateLimited indicates the upstream provider rejected the request due
	// to rate limiting or quota exhaustion.
	ErrRateLimited = errors.New("object: rate limited")

	// ErrAuthFailed indicates the provider rejected the supplied credentials.
	ErrAuthFailed = errors.New("object: authentication failed")

	// ErrUnsupported indicates the provider does not support a requested
	// capability.
	ErrUnsupported = errors.New("object: unsupported operation")
)

Functions

func ProviderOptionsFor

func ProviderOptionsFor[T any](po map[string]any, providerName string) (T, error)

ProviderOptionsFor extracts a provider-specific options bucket from a ProviderOptions map (typically Request.ProviderOptions) into a typed value. Behaviour mirrors the helpers used in other domain packages: if the bucket is already of type T it is returned as-is; if it is a plain map it will be JSON round-tripped into T so struct tags are honoured.

Types

type Client

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

Client is a thin, provider-agnostic facade over a Provider. It centralises concerns that are independent of the underlying backend and nil-guards provider calls for callers.

func NewClient

func NewClient(p Provider) *Client

NewClient returns a Client backed by the given Provider. The Provider may be nil; in that case the Client's methods will return ErrNoProvider.

func (*Client) GenerateObject

func (c *Client) GenerateObject(ctx context.Context, req Request) (ObjectResult, error)

GenerateObject performs a non-streaming object generation request. If the Client or its Provider is nil, ErrNoProvider is returned.

func (*Client) Provider

func (c *Client) Provider() Provider

Provider returns the underlying Provider, which may be nil.

func (*Client) StreamObject

func (c *Client) StreamObject(ctx context.Context, req Request) (ObjectStream, error)

StreamObject performs a streaming object generation request. If the Client or its Provider is nil, ErrNoProvider is returned. The caller must Close the returned ObjectStream when finished.

type Object

type Object struct {
	Name    string `json:"name"`
	Content string `json:"content"`
}

Object is a simple named artefact produced by providers. Name is a short identifier (for example a filename) and Content holds the textual payload.

type ObjectChunk

type ObjectChunk struct {
	// Delta is the JSON patch or partial object text.
	Delta string `json:"delta"`

	// Done is true when the stream is complete.
	Done bool `json:"done"`
}

ObjectChunk is a partial object emitted during streaming.

type ObjectResult

type ObjectResult any

ObjectResult is the abstract result returned by providers. It is kept as an alias for any so provider implementations may return either a concrete Object or any richer structure without forcing a single shape across providers.

type ObjectStream

type ObjectStream interface {
	// Next returns the next chunk. It returns io.EOF when the stream is
	// exhausted.
	Next(ctx context.Context) (ObjectChunk, error)

	// Close releases resources associated with the stream.
	Close() error
}

ObjectStream is an iterator over object chunks.

type Provider

type Provider interface {
	// Name returns a short, stable identifier for the provider
	// (for example, "openai", "ollama").
	Name() string

	// GenerateObject performs a non-streaming object generation operation.
	GenerateObject(ctx context.Context, req Request) (ObjectResult, error)

	// StreamObject performs a streaming object generation. Callers must
	// Close the returned ObjectStream when finished.
	StreamObject(ctx context.Context, req Request) (ObjectStream, error)
}

Provider is implemented by object generation backends. Implementations translate between the provider-agnostic Request/Response types defined in this package and their underlying API.

type Request

type Request struct {
	Model           string         `json:"model"`
	Prompt          string         `json:"prompt,omitempty"`
	MaxTokens       int            `json:"max_tokens,omitempty"`
	ProviderOptions map[string]any `json:"provider_options,omitempty"`
}

Request is a provider-agnostic object generation request. Only Model is required by convention; providers should treat zero values as "unspecified" and apply their own defaults.

type Response

type Response struct {
	ID       string    `json:"id,omitempty"`
	Model    string    `json:"model,omitempty"`
	Object   Object    `json:"object"`
	Warnings []Warning `json:"warnings,omitempty"`
}

Response is a non-streaming object generation result.

type UnsupportedContentError

type UnsupportedContentError struct {
	Provider string
	Detail   string
}

UnsupportedContentError is an optional typed error carrying context about unsupported content. It unwraps to ErrUnsupported to allow errors.Is.

func (*UnsupportedContentError) Error

func (e *UnsupportedContentError) Error() string

func (*UnsupportedContentError) Unwrap

func (e *UnsupportedContentError) Unwrap() error

type Warning

type Warning struct {
	Message string `json:"message"`
	Type    string `json:"type,omitempty"`
}

Warning is a non-fatal provider message attached to responses.

Jump to

Keyboard shortcuts

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