api

package
v0.2.6 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	RoleSystem    = "system"
	RoleAssistant = "assistant"
	RoleUser      = "user"
	RoleTool      = "tool"
)
View Source
const (
	ContentTypeText    = "text"
	ContentTypeB64JSON = string(openai.ImageGenerateParamsResponseFormatB64JSON)
)
View Source
const (
	ToolTypeFunc   = "func"
	ToolTypeSystem = "system"
	ToolTypeWeb    = "web"
	ToolTypeMcp    = "mcp"
	ToolTypeFaas   = "faas"
)
View Source
const (
	VarsEnvContainer = "container"
	VarsEnvHost      = "host"
)

Variables

View Source
var Levels = []Level{L1, L2, L3, Image, TTS}

Functions

func LogLevelToString added in v0.1.1

func LogLevelToString(level LogLevel) string

Types

type Advice

type Advice func(*Vars, *Request, *Response, Advice) error

type AdviceConfig

type AdviceConfig struct {
	Before string `yaml:"before"`
	After  string `yaml:"after"`
	Around string `yaml:"around"`
}

type Agent

type Agent struct {
	Owner string

	// The name of the agent.
	Name        string
	Display     string
	Description string

	Instruction *Instruction

	RawInput *UserInput

	// The model to be used by the agent
	Model *Model

	// Functions that the agent can call
	Tools []*ToolFunc

	Dependencies []string

	Entrypoint Entrypoint

	// advices
	BeforeAdvice Advice
	AfterAdvice  Advice
	AroundAdvice Advice

	// LLM adapter
	Adapter string

	//
	MaxTurns int
	MaxTime  int
	//
	Message string
	Format  string
	New     bool
	// MaxHistory int
	// MaxSpan    int
	LogLevel LogLevel

	//
	Config *AgentsConfig
}

type AgentConfig

type AgentConfig struct {
	Name        string `yaml:"name"`
	Display     string `yaml:"display"`
	Description string `yaml:"description"`

	//
	Instruction *Instruction `yaml:"instruction"`

	// [alias/]level
	Model string `yaml:"model"`

	// tools defined in tools config
	Functions []string `yaml:"functions"`

	Dependencies []string `yaml:"dependencies"`

	Entrypoint string `yaml:"entrypoint"`

	Advices *AdviceConfig `yaml:"advices"`

	// chat|image-get|docker/aider oh gptr
	Adapter string `yaml:"adapter"`

	//
	//
	MaxTurns int `yaml:"max_turns"`
	MaxTime  int `yaml:"max_time"`

	// user message
	Message string `yaml:"message"`

	// output format: json | text
	Format string `yaml:"format"`

	// memory
	// max history: 0 max span: 0
	New bool `yaml:"new"`

	// logging: quiet | info[rmative] | verbose | trace
	LogLevel string `yaml:"log_level"`

	//
	Store AssetStore `yaml:"-"`
	// relative to root
	BaseDir string `yaml:"-"`
}

type AgentCreator added in v0.0.6

type AgentCreator func(*Vars, *Request) (*Agent, error)

type AgentHandler added in v0.1.0

type AgentHandler func(*Vars, *Agent) Handler

type AgentResource

type AgentResource struct {
	Resources []*Resource `json:"resources"`
}

func LoadAgentResource

func LoadAgentResource(p string) (*AgentResource, error)

type AgentsConfig

type AgentsConfig struct {
	// agent app name
	Name string `yaml:"name"`

	// [alias/]level
	Model string `yaml:"model"`

	Agents []*AgentConfig `yaml:"agents"`
	Tools  []*ToolConfig  `yaml:"tools"`
	Models []*ModelConfig `yaml:"models"`

	//
	MaxTurns int `yaml:"max_turns"`
	MaxTime  int `yaml:"max_time"`

	// user message
	Message string `yaml:"message"`

	// output format: json | text
	Format string `yaml:"format"`

	// memory
	// max history: 0 max span: 0
	New bool `yaml:"new"`

	// logging: quiet | informative | verbose
	LogLevel string `yaml:"log_level"`
}

agent app config

type AppConfig

type AppConfig struct {
	Version string

	ConfigFile string

	// rename WebResource?
	AgentResource *AgentResource

	// ToolSystemCommands []string
	SystemTools []*ToolFunc

	Agent string
	// Command string
	Args []string

	// --message takes precedence, skip stdin
	// command line arguments
	Message string

	// editor binary and args. e.g vim [options]
	Editor string

	Clipin   bool
	ClipWait bool

	Clipout    bool
	ClipAppend bool

	IsPiped bool
	Stdin   bool

	Files []string

	// Treated as file
	Screenshot bool

	// Treated as input text
	Voice bool

	// Output format: raw or markdown
	Format string

	// output file for saving response
	Output string

	Me *User

	//
	Template string

	// conversation history
	New        bool
	MaxHistory int
	MaxSpan    int

	// chat id to continue the conersation
	// <config_base>/chat/<id>.json
	ChatID string

	//<config_base>/chat/<id>/*.json
	// History []*Message
	History MemStore

	Models string

	//
	LogLevel string

	DenyList  []string
	AllowList []string
	Unsafe    bool

	//
	Base string

	Workspace string
	Home      string
	Temp      string

	Interactive bool
	Editing     bool
	Shell       string

	Watch     bool
	ClipWatch bool

	MaxTime  int
	MaxTurns int

	//
	Stdout string
	Stderr string

	// dry run
	DryRun        bool
	DryRunContent string
}

func (*AppConfig) Clone

func (cfg *AppConfig) Clone() *AppConfig

Clone is a shallow copy of member fields of the configration

func (*AppConfig) GetQuery

func (r *AppConfig) GetQuery() string

func (*AppConfig) HasInput

func (r *AppConfig) HasInput() bool

func (*AppConfig) IsClipin

func (r *AppConfig) IsClipin() bool

func (*AppConfig) IsInformative added in v0.1.1

func (cfg *AppConfig) IsInformative() bool

func (*AppConfig) IsMedia

func (r *AppConfig) IsMedia() bool

func (*AppConfig) IsQuiet added in v0.1.1

func (cfg *AppConfig) IsQuiet() bool

func (*AppConfig) IsSpecial

func (r *AppConfig) IsSpecial() bool

func (*AppConfig) IsStdin

func (r *AppConfig) IsStdin() bool

func (*AppConfig) IsTracing added in v0.1.1

func (cfg *AppConfig) IsTracing() bool

func (*AppConfig) IsVerbose added in v0.1.1

func (cfg *AppConfig) IsVerbose() bool

type AssetStore

type AssetStore interface {
	ReadDir(name string) ([]DirEntry, error)
	ReadFile(name string) ([]byte, error)
	Resolve(parent string, name string) string
}

type Chat

type Chat struct {
	// uuid
	ID      string    `json:"id"`
	UserID  string    `json:"userId"`
	Created time.Time `json:"created"`

	// data
	Title string `json:"title"`
}

type ClipboardProvider

type ClipboardProvider interface {
	Clear() error
	Read() (string, error)
	Get() (string, error)
	Write(string) error
	Append(string) error
}

type ConnectorConfig added in v0.0.2

type ConnectorConfig struct {
	// mcp | ssh ...
	Proto string `yaml:"proto"`

	Provider string `yaml:"provider"`
	BaseUrl  string `yaml:"base_url"`
	// name of api key
	ApiKey string `yaml:"api_key"`

	Extra map[string]string `yaml:"extra"`
}

type DirEntry

type DirEntry = fs.DirEntry

func NewDirEntry

func NewDirEntry(
	name string,
	isDir bool,
	mode uint32,
	info *FileInfo,
) DirEntry

type DirEntryInfo

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

func FsDirEntryInfo

func FsDirEntryInfo(de fs.DirEntry) *DirEntryInfo

func (*DirEntryInfo) Info

func (r *DirEntryInfo) Info() (fs.FileInfo, error)

func (*DirEntryInfo) IsDir

func (r *DirEntryInfo) IsDir() bool

func (DirEntryInfo) MarshalJSON

func (r DirEntryInfo) MarshalJSON() ([]byte, error)

func (*DirEntryInfo) Name

func (r *DirEntryInfo) Name() string

func (*DirEntryInfo) Type

func (r *DirEntryInfo) Type() fs.FileMode

func (*DirEntryInfo) UnmarshalJSON

func (r *DirEntryInfo) UnmarshalJSON(data []byte) error

type EditorProvider

type EditorProvider interface {
	Launch(string) (string, error)
}

type Entrypoint

type Entrypoint func(*Vars, *Agent, *UserInput) error

type Feature added in v0.0.3

type Feature string

type FileInfo

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

func FsFileInfo

func FsFileInfo(fi fs.FileInfo) *FileInfo

func NewFileInfo

func NewFileInfo(
	name string,
	size int64,
	mode uint32,
	modTime time.Time,
	isDir bool,
) *FileInfo

func (*FileInfo) IsDir

func (r *FileInfo) IsDir() bool

func (FileInfo) MarshalJSON

func (fi FileInfo) MarshalJSON() ([]byte, error)

func (*FileInfo) ModTime

func (r *FileInfo) ModTime() time.Time

func (*FileInfo) Mode

func (r *FileInfo) Mode() fs.FileMode

func (*FileInfo) Name

func (r *FileInfo) Name() string

func (*FileInfo) Size

func (r *FileInfo) Size() int64

func (*FileInfo) Sys

func (r *FileInfo) Sys() any

func (*FileInfo) UnmarshalJSON

func (fi *FileInfo) UnmarshalJSON(data []byte) error

type FuncBody added in v0.1.1

type FuncBody struct {
	Language string `yaml:"language"`
	Code     string `yaml:"code"`
	Url      string `yaml:"url"`
}

type Handler added in v0.1.0

type Handler interface {
	Serve(*Request, *Response) error
}

type InputType added in v0.0.3

type InputType string

type Instruction added in v0.0.6

type Instruction struct {
	Role string `yaml:"role"`
	// TODO add new field
	// Source ? resource/file/cloud...
	// prefix supported: file: resource:
	Content string `yaml:"content"`
	// template or not
	// tpl
	Type string `yaml:"type"`
}

type Level added in v0.0.3

type Level = string

Level represents the "intelligence" level of the model. i.e. basic, regular, advanced for example, OpenAI: gpt-4.1-mini, gpt-4.1, o3

const (
	// any of L1/L2/L3
	Any Level = "any"

	L1 Level = "L1"
	L2 Level = "L2"
	L3 Level = "L3"
	//
	Image Level = "image"
	TTS   Level = "tts"
)

type LogLevel added in v0.1.1

type LogLevel int
const (
	Quiet LogLevel = iota + 1
	Informative
	Verbose
	Tracing
)

func ToLogLevel added in v0.1.1

func ToLogLevel(level string) LogLevel

type MemOption added in v0.0.3

type MemOption struct {
	MaxHistory int
	MaxSpan    int
}

TODO

type MemStore added in v0.0.3

type MemStore interface {
	Save([]*Message) error
	Load(*MemOption) ([]*Message, error)
}

type Message

type Message struct {
	ID      string    `json:"id"`
	ChatID  string    `json:"chatId"`
	Created time.Time `json:"created"`

	// data
	ContentType string `json:"contentType"`
	Content     string `json:"content"`

	Role string `json:"role"`

	// agent name
	Sender string `json:"sender"`
}

type Model

type Model struct {
	Model string

	Provider string
	BaseUrl  string

	// resolved LLM api key/token
	ApiKey string

	// output
	Type OutputType

	//
	Config *ModelsConfig
}

func (*Model) String added in v0.1.1

func (r *Model) String() string

type ModelAlias added in v0.1.0

type ModelAlias = map[string]*Model

type ModelAliasConfig added in v0.1.0

type ModelAliasConfig = map[string]*ModelConfig

type ModelConfig

type ModelConfig struct {
	// LLM model
	Model string `yaml:"model"`

	// LLM service provider: openai | gemini | anthropic
	Provider string `yaml:"provider"`
	BaseUrl  string `yaml:"base_url"`
	// name of api key
	ApiKey string `yaml:"api_key"`
}

type ModelsConfig added in v0.0.3

type ModelsConfig struct {
	Alias string `yaml:"alias"`

	// default LLM model for ModelConfig
	Model string `yaml:"model"`

	Provider string `yaml:"provider"`
	BaseUrl  string `yaml:"base_url"`
	// name of api key
	ApiKey string `yaml:"api_key"`

	Models ModelAliasConfig `yaml:"models"`
}

type Output

type Output struct {
	// Agent icon and name
	Display string `json:"display"`

	Content     string `json:"content"`
	ContentType string `json:"content_type"`
}

type OutputType added in v0.0.3

type OutputType string
const (

	//
	OutputTypeUnknown OutputType = ""
	OutputTypeText    OutputType = "text"
	OutputTypeImage   OutputType = "image"
)

type Request

type Request struct {
	// The name/command of the active agent
	Agent string

	Messages []*Message

	RawInput *UserInput
	// contains filtered or unexported fields
}

func NewRequest added in v0.1.1

func NewRequest(ctx context.Context, agent string, input *UserInput) *Request

func (*Request) Clone

func (r *Request) Clone(ctx context.Context) *Request

Clone returns a shallow copy of r with its context changed to ctx. The provided ctx must be non-nil.

Clone only makes a shallow copy of the Body field.

For an outgoing client request, the context controls the entire lifetime of a request and its response: obtaining a connection, sending the request, and reading the response headers and body.

func (*Request) Context

func (r *Request) Context() context.Context

Context returns the request's context. To change the context, use Request.Clone or Request.WithContext.

The returned context is always non-nil; it defaults to the background context.

For outgoing client requests, the context controls cancellation.

For incoming server requests, the context is canceled when the client's connection closes, the request is canceled (with HTTP/2), or when the ServeHTTP method returns.

func (*Request) WithContext

func (r *Request) WithContext(ctx context.Context) *Request

WithContext returns a shallow copy of r with its context changed to ctx. The provided ctx must be non-nil.

For outgoing client request, the context controls the entire lifetime of a request and its response: obtaining a connection, sending the request, and reading the response headers and body.

To create a new request with a context, use [NewRequestWithContext]. To make a deep copy of a request with a new context, use Request.Clone.

type Resource

type Resource struct {
	// web resource base url
	// http://localhost:18080/resource
	// https://ai.dhnt.io/resource
	Base string `json:"base"`

	// access token
	Token string `json:"token"`
}

type Response

type Response struct {
	// A list of message objects generated during the conversation
	// with a sender field indicating which Agent the message originated from.
	Messages []*Message

	// The last agent to handle a message
	Agent *Agent

	Result *Result
}

type Result

type Result struct {
	// The result value as a string
	Value string

	// Tool call
	// https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/MIME_types
	MimeType string
	Message  string

	// The agent state
	State State

	// The agent name to transfer to for StateTransfer
	NextAgent string
}

Result encapsulates the possible return values for agent/function.

func (*Result) String

func (r *Result) String() string

type State

type State int
const (
	StateDefault State = iota

	StateExit
	StateTransfer
	StateInputWait
)

func ParseState

func ParseState(s string) State

func (State) Equal

func (r State) Equal(s string) bool

func (State) String

func (r State) String() string

type TemplateFuncMap

type TemplateFuncMap = template.FuncMap

type ToolCaller added in v0.1.0

type ToolCaller func(*Vars, *Agent) func(context.Context, string, map[string]any) (*Result, error)

type ToolCondition

type ToolCondition struct {
	// required env list
	Env []string `yaml:"env"`

	// found on PATH
	Lookup *string `yaml:"lookup"`

	// shell required
	Shell map[string]any `yaml:"shell"`
}

TODO condidtion needs to be met for tools to be enabled

type ToolConfig

type ToolConfig struct {
	Type string `yaml:"type"`

	Name string `yaml:"name"`

	Description string         `yaml:"description"`
	Parameters  map[string]any `yaml:"parameters"`

	Body *FuncBody `yaml:"body"`

	Condition *ToolCondition `yaml:"condition"`

	//
	Provider string `yaml:"provider"`
	BaseUrl  string `yaml:"base_url"`
	// name of api key
	ApiKey string `yaml:"api_key"`

	Extra map[string]string `yaml:"extra"`
}

type ToolFunc

type ToolFunc struct {
	Type string

	Kit         string
	Name        string
	Description string
	Parameters  map[string]any

	Body *FuncBody

	//
	State State
	//
	Config *ToolsConfig

	//
	Provider string
	BaseUrl  string
	// name of api key - used to resolve api key/token before tool call
	ApiKey string

	Extra map[string]string
}

func (*ToolFunc) ID

func (r *ToolFunc) ID() string

ID returns a unique identifier for the tool, combining the tool kit and name.

type ToolSystem added in v0.0.2

type ToolSystem interface {
	Call(context.Context, *Vars, *ToolFunc, map[string]any) (*Result, error)
}

type ToolsConfig

type ToolsConfig struct {
	// kit name
	// Namespace:
	//
	// func class
	// Agent name
	// MCP server name
	// Virtual file system name
	// Container name
	// Virtual machine name
	// Tool/function (Gemini)
	Kit string `yaml:"kit"`

	// func (server) | system (client) | remote
	Type string `yaml:"type"`

	Provider string `yaml:"provider"`
	BaseUrl  string `yaml:"base_url"`
	// name of api key
	ApiKey string `yaml:"api_key"`

	Connector *ConnectorConfig `yaml:"connector"`

	// system commands used by tools
	Commands []string `yaml:"commands"`

	Tools []*ToolConfig `yaml:"tools"`
}

Toolkit configuration

type User

type User struct {
	// uuid
	ID      string    `json:"id"`
	Created time.Time `json:"created"`

	// data
	Username string `json:"username"`
	Display  string `json:"display"`
}

type UserInput

type UserInput struct {
	Agent string `json:"agent"`

	// query - command line args
	Message string `json:"message"`

	// query - clipboard/stdin/editor
	Content string `json:"content"`

	// TODO deprecate
	Template string `json:"template"`

	Files []string `json:"files"`

	Extra map[string]any `json:"extra"`

	// cached file contents
	Messages []*Message `json:"-"`
}

func (*UserInput) Intent

func (r *UserInput) Intent() string

Intent returns a clipped version of the query. This is intended for "smart" agents to make decisions based on user inputs.

func (*UserInput) IsEmpty

func (r *UserInput) IsEmpty() bool

No user input.

func (*UserInput) Query

func (r *UserInput) Query() string

Text input from command line args, clipboard, stdin, or editor

func (*UserInput) String

func (r *UserInput) String() string

type Vars

type Vars struct {
	Config *AppConfig `json:"config"`

	OS        string            `json:"os"`
	Arch      string            `json:"arch"`
	ShellInfo map[string]string `json:"shell_info"`
	OSInfo    map[string]string `json:"os_info"`

	UserInfo map[string]string `json:"user_info"`

	UserInput *UserInput `json:"user_input"`

	Workspace string `json:"workspace"`
	// Repo      string `json:"repo"`
	Home string `json:"home"`
	Temp string `json:"temp"`

	// EnvType indicates the environment type where the agent is running
	// It can be "container" for Docker containers or "host" for the host machine
	EnvType string `json:"env_type"`

	Roots []string `json:"roots"`

	//
	Extra map[string]string `json:"extra"`

	// conversation history
	History []*Message
}

global context

func NewVars

func NewVars() *Vars

func (*Vars) Get

func (r *Vars) Get(key string) string

func (*Vars) GetString

func (r *Vars) GetString(key string) string

Jump to

Keyboard shortcuts

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