contracts

package
v0.0.16 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ErrCodeServiceNotFound = 1 // "service not found"
	ErrCodeMethodNotFound  = 2 // "method not found"
)

Variables

View Source
var (
	ErrMethodNotFound        = errors.New("method not found")
	ErrHandlerNotFound       = errors.New("handler not found")
	ErrInputRequired         = errors.New("input is required")
	ErrNoHealthCheckProvided = errors.New("no healthcheck provided")
)

Functions

func DefaultHTTPPingFunc

func DefaultHTTPPingFunc(opts HTTPOptions) func(ctx context.Context) error

DefaultHTTPPingFunc returns a ping function for HTTP requests based on the provided options.

func DefaultWSPingFunc

func DefaultWSPingFunc(opts WSOptions) func(ctx context.Context) error

DefaultWSPingFunc returns a ping function for WebSocket based on provided options.

Types

type CallOpts

type CallOpts struct {
	Messages Messages
}

type ChatRole

type ChatRole string
const (
	// UserRole represents a regular user in the chat
	UserRole ChatRole = "user"
	// AssistantRole represents an AI assistant in the chat
	AssistantRole ChatRole = "assistant"
)

type DefaultHandlerContext

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

DefaultHandlerContext is the default implementation of the HandlerContext interface.

func (*DefaultHandlerContext) Context

func (h *DefaultHandlerContext) Context() context.Context

func (*DefaultHandlerContext) Get

func (h *DefaultHandlerContext) Get(inputName string) any

func (*DefaultHandlerContext) GetBool

func (h *DefaultHandlerContext) GetBool(inputName string) (bool, bool)

func (*DefaultHandlerContext) GetFloat

func (h *DefaultHandlerContext) GetFloat(inputName string) (float64, bool)

func (*DefaultHandlerContext) GetInput

func (h *DefaultHandlerContext) GetInput(name string) (Input, bool)

func (*DefaultHandlerContext) GetInt

func (h *DefaultHandlerContext) GetInt(inputName string) (int, bool)

func (*DefaultHandlerContext) GetOutput

func (h *DefaultHandlerContext) GetOutput(name string) (Output, bool)

func (*DefaultHandlerContext) GetString

func (h *DefaultHandlerContext) GetString(inputName string) (string, bool)

func (*DefaultHandlerContext) Inputs

func (h *DefaultHandlerContext) Inputs() map[string]Input

func (*DefaultHandlerContext) JSON

func (h *DefaultHandlerContext) JSON(i any) error

JSON populates the outputs by marshaling the provided data to JSON and unmarshaling it into a map. If the input data is already a map, it is directly used to set outputs

func (*DefaultHandlerContext) Messages

func (h *DefaultHandlerContext) Messages() Messages

func (*DefaultHandlerContext) Outputs

func (h *DefaultHandlerContext) Outputs() map[string]Output

func (*DefaultHandlerContext) Set

func (h *DefaultHandlerContext) Set(outputName string, value any)

func (*DefaultHandlerContext) SetMessages

func (h *DefaultHandlerContext) SetMessages(messages Messages)

type HTTPOptions

type HTTPOptions struct {
	Host   string `json:"host,omitempty" yaml:"host,omitempty"`
	Port   string `json:"port,omitempty" yaml:"port,omitempty"`
	Path   string `json:"path,omitempty" yaml:"path,omitempty"`
	Method string `json:"method,omitempty" yaml:"method,omitempty"`
	Schema string `json:"schema,omitempty" yaml:"schema,omitempty"`
}

HTTPOptions holds configuration options for HTTP requests.

type Handler

type Handler struct {
	Name        string            `json:"name"`                  // Name is the name of the handler.
	Description map[string]string `json:"description,omitempty"` // Description is an optional description of the handler.
	Args        map[string]any    `json:"args"`                  // Args are the arguments for the handler.

	Do HandlerFunc `json:"-"` // Do is the handler function itself, which is not serialized to JSON.
}

Handler represents a method handler with metadata, arguments, and the handler function itself.

type HandlerContext

type HandlerContext interface {

	// Context returns the underlying context.
	Context() context.Context

	// Get retrieves the value of the specified input.
	Get(inputName string) any

	// GetString retrieves the string representation of the specified input.
	GetString(inputName string) (string, bool)

	// GetInt retrieves the integer representation of the specified input.
	GetInt(inputName string) (int, bool)

	// GetFloat retrieves the float representation of the specified input.
	GetFloat(inputName string) (float64, bool)

	// GetBool retrieves the boolean representation of the specified input.
	GetBool(inputName string) (bool, bool)

	// GetInput retrieves an Input by its name, if present.
	GetInput(name string) (Input, bool)

	// Inputs returns all input mappings.
	Inputs() map[string]Input

	// Set assigns a value to the specified output.
	Set(outputName string, value any)

	// GetOutput retrieves an Output by its name, if present.
	GetOutput(name string) (Output, bool)

	// Outputs returns all output mappings.
	Outputs() map[string]Output

	// JSON populates the outputs by marshaling and unmarshaling the provided data.
	JSON(data any) error

	// Messages returns all messages in the context.
	Messages() Messages

	// SetMessages sets the messages in the context.
	SetMessages(messages Messages)
}

HandlerContext defines an interface for managing inputs and outputs and providing access to the underlying context.

func NewHandlerContext

func NewHandlerContext(ctx context.Context, inputs map[string]Input, outputs map[string]Output, opts ...HandlerContextOpts) HandlerContext

NewHandlerContext creates a new DefaultHandlerContext with the provided context, inputs, and outputs.

type HandlerContextOpts

type HandlerContextOpts func(handlerContext HandlerContext)

func WithMessages

func WithMessages(messages Messages) HandlerContextOpts

WithMessages returns a HandlerContextOpts that sets the messages in the handler context.

type HandlerFunc

type HandlerFunc func(ctx HandlerContext) error

HandlerFunc defines the function signature for method handlers, which receive a HandlerContext and return an error if something goes wrong.

type IOType

type IOType string

IOType represents the type of input or output in a method (e.g., text, audio, image).

const (
	IOTypeText    IOType = "text"
	IOTypeNumber  IOType = "number"
	IOTypeBoolean IOType = "boolean"
	IOTypeAudio   IOType = "audio"
	IOTypeImage   IOType = "image"
	IOTypeVideo   IOType = "video"
	IOTypeFile    IOType = "file"
	IOTypeJSON    IOType = "json"
)

Constants for different IOType values.

type Input

type Input struct {
	Name         string            `json:"name" yaml:"name"`                                   // Name of the input.
	Type         IOType            `json:"type" yaml:"type"`                                   // Type of the input (e.g., text, audio, image, etc.).
	Description  map[string]string `json:"description,omitempty" yaml:"description,omitempty"` // Optional description of the input.
	DefaultValue any               `json:"default,omitempty" yaml:"default,omitempty"`         // Optional default value for the input.
	IsRequired   bool              `json:"is_required" yaml:"is_required"`                     // Indicates if the input is required.
	// contains filtered or unexported fields
}

Input represents the input data of a method.

func (*Input) AsPublic

func (i *Input) AsPublic() InputPublic

func (*Input) SetValue

func (i *Input) SetValue(value any)

SetValue sets the internal value of the input.

func (*Input) Value

func (i *Input) Value() any

Value returns the internal value of the input.

type InputFilter

type InputFilter func(input Input) bool

InputFilter - filter for inputs. Returns true if the input should be included

type InputPublic

type InputPublic struct {
	Name         string            `json:"name" yaml:"name"`                                   // Name of the input.
	Type         IOType            `json:"type" yaml:"type"`                                   // Type of the input (e.g., text, audio, image, etc.).
	Description  map[string]string `json:"description,omitempty" yaml:"description,omitempty"` // Optional description of the input.
	DefaultValue any               `json:"default,omitempty" yaml:"default,omitempty"`         // Optional default value for the input.
	IsRequired   bool              `json:"is_required" yaml:"is_required"`                     // Indicates if the input is required.
}

InputPublic represents the input data of a method.

type Messages

type Messages []map[ChatRole]string

type Method

type Method struct {
	Name        string            `json:"name" yaml:"name"`
	Description map[string]string `json:"description,omitempty" yaml:"description,omitempty"`

	Inputs  []Input  `json:"inputs" yaml:"inputs"`
	Outputs []Output `json:"outputs" yaml:"outputs"`

	IsDefault bool `json:"is_default" yaml:"is_default"`

	Handler *Handler `json:"handler" yaml:"handler"`
}

Method - describes the method of the service Contains the name, description, inputs and outputs. Also contains the name of the handler function and the handler itself

func (*Method) AsPublic

func (m *Method) AsPublic() MethodPublic

func (*Method) Call

func (m *Method) Call(inputData map[string]any, opts ...CallOpts) (*MethodResponse, error)

Call - calls the method If the method has no handler, it returns an error Otherwise, it calls the handler It returns the result of the handler Preferably use CallWithContext

func (*Method) CallWithContext

func (m *Method) CallWithContext(ctx context.Context, inputData map[string]any, opts ...CallOpts) (*MethodResponse, error)

CallWithContext - calls the method with the given context If the method has no handler, it returns an error Otherwise, it calls the handler It returns the result of the handler

func (*Method) GetAudioInputs

func (m *Method) GetAudioInputs() []Input

GetAudioInputs - returns the list of audio inputs

func (*Method) GetFileInputs

func (m *Method) GetFileInputs() []Input

GetFileInputs - returns the list of file inputs

func (*Method) GetImageInputs

func (m *Method) GetImageInputs() []Input

GetImageInputs - returns the list of image inputs

func (*Method) GetInput

func (m *Method) GetInput(name string) (Input, bool)

GetInput - returns the input with the given name

func (*Method) GetInputs

func (m *Method) GetInputs() []Input

GetInputs - returns the list of inputs

func (*Method) GetInputsWithFilter

func (m *Method) GetInputsWithFilter(filter InputFilter) []Input

GetInputsWithFilter - returns the list of inputs with the given filter.

func (*Method) GetOutput

func (m *Method) GetOutput(name string) (Output, bool)

GetOutput - returns the output with the given name

func (*Method) GetOutputs

func (m *Method) GetOutputs() []Output

GetOutputs - returns the list of outputs

func (*Method) GetTextInputs

func (m *Method) GetTextInputs() []Input

GetTextInputs - returns the list of text inputsq

func (*Method) GetVideoInputs

func (m *Method) GetVideoInputs() []Input

GetVideoInputs - returns the list of video inputs

type MethodPublic

type MethodPublic struct {
	Name        string            `json:"name" yaml:"name"`
	Description map[string]string `json:"description,omitempty" yaml:"description,omitempty"`
	Inputs      []InputPublic     `json:"inputs" yaml:"inputs"`
	Outputs     []OutputPublic    `json:"outputs" yaml:"outputs"`
	IsDefault   bool              `json:"is_default" yaml:"is_default"`
}

MethodPublic - represents a method in the public API It can be used to print information about the method without sensitive data

type MethodResponse

type MethodResponse struct {
	Outputs     map[string]Output // Outputs contains the output data of the method.
	ServiceName string            // ServiceName is the name of the service that the method belongs to.
	Err         error             // Err contains any error encountered during method execution.
	ErrCode     int               // ErrCode is the error code of the method.
}

MethodResponse describes the response of a method, including output data and any errors.

func (*MethodResponse) Get

func (m *MethodResponse) Get(name string) (any, bool)

Get retrieves the value of a specific output by name. It returns the value and a boolean indicating whether the output was found.

func (*MethodResponse) GetString

func (m *MethodResponse) GetString(name string) (string, bool)

GetString retrieves the string representation of a specific output by name. It returns the formatted value and a boolean indicating whether the output was found.

type Output

type Output struct {
	Name         string            `json:"name" yaml:"name"`                                   // Name of the output.
	Type         IOType            `json:"type" yaml:"type"`                                   // Type of the output (e.g., text, audio, image, etc.).
	Description  map[string]string `json:"description,omitempty" yaml:"description,omitempty"` // Optional description of the output.
	DefaultValue any               `json:"default,omitempty" yaml:"default,omitempty"`         // Optional default value for the output.
	IsPrivate    bool              `json:"is_private" yaml:"is_private"`                       // Indicates if the output is private (for users). TODO: rename.
	// contains filtered or unexported fields
}

Output represents the output data of a method.

func (*Output) AsPublic

func (o *Output) AsPublic() OutputPublic

func (*Output) SetValue

func (o *Output) SetValue(value any)

SetValue sets the internal value of the output.

func (*Output) Value

func (o *Output) Value() any

Value returns the internal value of the output.

type OutputPublic

type OutputPublic struct {
	Name         string            `json:"name" yaml:"name"`                                   // Name of the output.
	Type         IOType            `json:"type" yaml:"type"`                                   // Type of the output (e.g., text, audio, image, etc.).
	Description  map[string]string `json:"description,omitempty" yaml:"description,omitempty"` // Optional description of the output.
	DefaultValue any               `json:"default,omitempty" yaml:"default,omitempty"`         // Optional default value for the output.
	IsPrivate    bool              `json:"is_private" yaml:"is_private"`                       // Indicates if the output is private (for users). TODO: rename.
}

OutputPublic represents the output data of a method. It can be used to print information about the output without sensitive data

type Ping

type Ping struct {
	// Name of the ping operation
	Name string `json:"name" yaml:"name"`

	// Args - Arguments for the ping operation, can vary by implementation
	Args map[string]any `json:"args,omitempty" yaml:"args,omitempty"`
	// contains filtered or unexported fields
}

Ping represents a ping operation with its related data and function.

func NewHTTPPinger

func NewHTTPPinger(opts HTTPOptions) *Ping

NewHTTPPinger creates a new Ping instance configured for HTTP pinging.

func NewWSPinger

func NewWSPinger(opts WSOptions) *Ping

NewWSPinger creates a new Ping instance configured for WebSocket pinging.

func (*Ping) Do

func (p *Ping) Do(ctx context.Context) error

Do executes the ping operation defined by the Ping instance

func (*Ping) SetHandler

func (p *Ping) SetHandler(handler func(ctx context.Context) error)

SetHandler allows setting a custom ping handler function.

type PingFunc

type PingFunc func(ctx context.Context) error

PingFunc defines a function type for executing a ping operation.

type Service

type Service struct {
	Name        string            `json:"name" yaml:"name"`
	Description map[string]string `json:"description,omitempty" yaml:"description,omitempty"`

	Methods map[string]*Method `json:"methods" yaml:"methods"`

	Pinger *Ping `json:"pinger,omitempty" yaml:"pinger,omitempty"`
}

Service - describes the service of the application Contains the name, description and methods Can be used to call methods

func (*Service) AddMethod

func (s *Service) AddMethod(method *Method)

func (*Service) AsPublic

func (s *Service) AsPublic() ServicePublic

AsPublic - returns the service as a public service

func (*Service) CallMethod

func (s *Service) CallMethod(ctx context.Context, methodName string, inputData map[string]any, opts ...CallOpts) (*MethodResponse, error)

CallMethod - calls the method with the given name If the method does not exist, it returns an error Otherwise, it calls the method and returns the result

func (*Service) GetDefaultMethod

func (s *Service) GetDefaultMethod() *Method

GetDefaultMethod - returns the method with the default flag If the service does not have a default method, it returns nil

func (*Service) Ping

func (s *Service) Ping(ctx context.Context) error

Ping - pings the service If the service does not have a pinger, it returns an error Otherwise, it calls the pinger and returns the result If the pinger returns nil, it means that the service is ok

type ServicePublic

type ServicePublic struct {
	Name        string                  `json:"name" yaml:"name"`
	Description map[string]string       `json:"description,omitempty" yaml:"description,omitempty"`
	Methods     map[string]MethodPublic `json:"methods" yaml:"methods"`
}

ServicePublic - describes the service as public. It can be used to print information about the service without sensitive data

type WSOptions

type WSOptions struct {
	Host   string `json:"host,omitempty" yaml:"host,omitempty"`
	Port   string `json:"port,omitempty" yaml:"port,omitempty"`
	Path   string `json:"path,omitempty" yaml:"path,omitempty"`
	Schema string `json:"schema,omitempty" yaml:"schema,omitempty"`
}

WSOptions holds configuration options for WebSocket

Jump to

Keyboard shortcuts

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