extension

package module
v1.9.0 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: MIT Imports: 23 Imported by: 0

Documentation

Overview

Package extension adapts the Cortex engine as a Forge extension.

It implements the forge.Extension interface to integrate Cortex into a Forge application with automatic dependency discovery, route registration, and lifecycle management.

Configuration can be provided programmatically via ExtOption functions or via YAML configuration files under "extensions.cortex" or "cortex" keys.

Index

Constants

View Source
const ExtensionDescription = "Human-emulating AI agent orchestration"

ExtensionDescription is the human-readable description.

View Source
const ExtensionName = "cortex"

ExtensionName is the name registered with Forge.

View Source
const ExtensionVersion = cortex.Version

ExtensionVersion is the semantic version.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// DisableRoutes prevents HTTP route registration.
	DisableRoutes bool `json:"disable_routes" mapstructure:"disable_routes" yaml:"disable_routes"`

	// DisableMigrate prevents auto-migration on start.
	DisableMigrate bool `json:"disable_migrate" mapstructure:"disable_migrate" yaml:"disable_migrate"`

	// BasePath is the URL prefix for cortex routes (default: "/cortex").
	BasePath string `json:"base_path" mapstructure:"base_path" yaml:"base_path"`

	// DefaultModel is the LLM model to use when none is specified.
	DefaultModel string `json:"default_model" mapstructure:"default_model" yaml:"default_model"`

	// DefaultMaxSteps is the maximum reasoning steps per run.
	DefaultMaxSteps int `json:"default_max_steps" mapstructure:"default_max_steps" yaml:"default_max_steps"`

	// DefaultMaxTokens is the maximum tokens per LLM call.
	DefaultMaxTokens int `json:"default_max_tokens" mapstructure:"default_max_tokens" yaml:"default_max_tokens"`

	// DefaultTemperature is the LLM sampling temperature.
	DefaultTemperature float64 `json:"default_temperature" mapstructure:"default_temperature" yaml:"default_temperature"`

	// DefaultReasoningLoop is the reasoning loop strategy (e.g., "react").
	DefaultReasoningLoop string `json:"default_reasoning_loop" mapstructure:"default_reasoning_loop" yaml:"default_reasoning_loop"`

	// ShutdownTimeout is the maximum time to wait for graceful shutdown.
	ShutdownTimeout time.Duration `json:"shutdown_timeout" mapstructure:"shutdown_timeout" yaml:"shutdown_timeout"`

	// RunConcurrency controls how many agent runs can execute in parallel.
	RunConcurrency int `json:"run_concurrency" mapstructure:"run_concurrency" yaml:"run_concurrency"`

	// GroveDatabase is the name of a grove.DB registered in the DI container.
	// When set, the extension resolves this named database and auto-constructs
	// the appropriate store based on the driver type (pg/sqlite/mongo).
	// When empty and WithGroveDatabase was called, the default (unnamed) DB is used.
	GroveDatabase string `json:"grove_database" mapstructure:"grove_database" yaml:"grove_database"`

	// RequireConfig requires config to be present in YAML files.
	// If true and no config is found, Register returns an error.
	RequireConfig bool `json:"-" yaml:"-"`
}

Config holds the Cortex extension configuration. Fields can be set programmatically via ExtOption functions or loaded from YAML configuration files (under "extensions.cortex" or "cortex" keys).

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns a Config with sensible defaults.

type ExtOption

type ExtOption func(*Extension)

ExtOption configures the Cortex Forge extension.

func WithBasePath

func WithBasePath(path string) ExtOption

WithBasePath sets the URL prefix for all cortex routes.

func WithConfig

func WithConfig(cfg Config) ExtOption

WithConfig sets the extension configuration.

func WithDashboard

func WithDashboard(deps cortexcontract.Deps) ExtOption

WithDashboard supplies the authenticated scope mapping and durable audit sink. Engine is supplied by the extension after initialization.

func WithDisableMigrate

func WithDisableMigrate() ExtOption

WithDisableMigrate disables auto-migration on start.

func WithDisableRoutes

func WithDisableRoutes() ExtOption

WithDisableRoutes disables HTTP route registration.

func WithEngineOption

func WithEngineOption(opt engine.Option) ExtOption

WithEngineOption passes an engine option directly.

func WithExtension

func WithExtension(x plugin.Extension) ExtOption

WithExtension registers a Cortex extension (lifecycle hooks).

func WithGroveDatabase

func WithGroveDatabase(name string) ExtOption

WithGroveDatabase sets the name of the grove.DB to resolve from the DI container. The extension will auto-construct the appropriate store backend (postgres/sqlite/mongo) based on the grove driver type. Pass an empty string to use the default (unnamed) grove.DB.

func WithOptimization

func WithOptimization(value *optimization.Optimizer) ExtOption

WithOptimization supplies the dashboard's operator service. Worker scope enumeration and shutdown remain with the host that owns the optimizer. This option takes precedence over the service supplied through WithDashboard.

func WithRequireConfig

func WithRequireConfig(require bool) ExtOption

WithRequireConfig requires config to be present in YAML files. If true and no config is found, Register returns an error.

func WithRoutes

func WithRoutes(build func(eng *engine.Engine, router forge.Router) RouteSet) ExtOption

WithRoutes mounts an HTTP surface on the extension.

The REST handlers live in github.com/xraph/cortex/api, a separate module, so nothing here forces a host to compile them in. Pass this when you want them:

extension.New(
	extension.WithRoutes(func(eng *engine.Engine, r forge.Router) extension.RouteSet {
		return api.New(eng, r)
	}),
)

Leave it out and the extension registers no routes, Routes returns nil and Handler answers 404. The engine, the dashboard and the DI wiring are unaffected either way.

func WithStore

func WithStore(s store.Store) ExtOption

WithStore sets the composite store.

type Extension

type Extension struct {
	*forge.BaseExtension
	// contains filtered or unexported fields
}

Extension adapts Cortex as a Forge extension.

func New

func New(opts ...ExtOption) *Extension

New creates a Cortex Forge extension with the given options.

func (*Extension) Engine

func (e *Extension) Engine() *engine.Engine

Engine returns the underlying Cortex engine. This is nil until Register is called.

func (*Extension) Handler

func (e *Extension) Handler() http.Handler

Handler returns the HTTP handler for all API routes. Convenience for standalone use outside Forge.

func (*Extension) Health

func (e *Extension) Health(ctx context.Context) error

Health implements forge.Extension.

func (*Extension) Register

func (e *Extension) Register(fapp forge.App) error

Register implements forge.Extension. It loads configuration, initializes the engine, and registers it in the DI container.

func (*Extension) RegisterContractContributor

func (e *Extension) RegisterContractContributor(d *dispatcher.Dispatcher, reg dc.Registry, wreg dc.WardenRegistry) error

func (*Extension) RegisterRoutes

func (e *Extension) RegisterRoutes(router forge.Router) error

RegisterRoutes registers all Cortex API routes into a Forge router.

func (*Extension) Routes

func (e *Extension) Routes() RouteSet

Routes returns the mounted HTTP surface, or nil when the extension was built without one. Nil is the ordinary case for a host that wants the engine and the dashboard and no REST API.

func (*Extension) Start

func (e *Extension) Start(ctx context.Context) error

Start begins the Cortex engine and runs auto-migration if enabled.

func (*Extension) Stop

func (e *Extension) Stop(ctx context.Context) error

Stop gracefully shuts down the Cortex engine.

type RouteSet

type RouteSet interface {
	Handler() http.Handler
	RegisterRoutes(router forge.Router) error
}

RouteSet is the HTTP surface an extension mounts.

It is an interface so the REST handlers can live in their own module. A host that wants them passes WithRoutes and takes the dependency; a host that does not never compiles them in. The api package satisfies this.

Directories

Path Synopsis
Package contract exposes Cortex through the Forge dashboard dispatcher.
Package contract exposes Cortex through the Forge dashboard dispatcher.

Jump to

Keyboard shortcuts

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