extension

package module
v1.10.4 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 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 bounds Stop: how long it waits for runs in flight to
	// finish. Zero takes the default, 30s; a negative value leaves Stop
	// bounded only by its ctx.
	ShutdownTimeout time.Duration `json:"shutdown_timeout" mapstructure:"shutdown_timeout" yaml:"shutdown_timeout"`

	// RunConcurrency caps how many agent runs execute at once in this
	// process, applied with engine.WithRunConcurrency. Zero, the default,
	// is unlimited. Runs started by the experimental task workers, prepared
	// orchestrations and plan replanning are not counted.
	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 WithNexusOptions added in v1.10.3

func WithNexusOptions(opts ...nexusllm.Option) ExtOption

WithNexusOptions configures the nexus adapter the extension wires in when it finds a nexus gateway in the container: nexusllm.WithTenant to map a scope to its nexus tenant, nexusllm.WithProvider, WithMetadata and WithSamplingUnsupported. The adapter keeps its place in the option order, so an engine.WithLLM passed with WithEngineOption still replaces it, and WithoutAutoAdapters still leaves it out.

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 WithShieldOptions added in v1.10.3

func WithShieldOptions(opts ...shieldsafety.Option) ExtOption

WithShieldOptions configures the shield adapter the extension wires in when it finds a shield engine in the container: shieldsafety.WithTenant to map a scope to its shield tenant, shieldsafety.WithApp to name the shield app, and shieldsafety.WithUntrustedSources. The adapter keeps its place in the option order, so an engine.WithSafety or engine.WithPublicationGate passed with WithEngineOption still replaces the matching half of it, and WithoutAutoAdapters still leaves it out.

func WithStore

func WithStore(s store.Store) ExtOption

WithStore sets the composite store. It wins over any grove database in the container, and the extension then skips discovering a default one.

func WithWeaveOptions added in v1.10.3

func WithWeaveOptions(opts ...weaveknowledge.Option) ExtOption

WithWeaveOptions configures the weave adapter the extension wires in when it finds a weave engine in the container: weaveknowledge.WithTenant to map a scope to its weave tenant, and weaveknowledge.WithShared to add read-only collections every run may search. The adapter keeps its place in the option order, so an engine.WithKnowledge passed with WithEngineOption still replaces it, and WithoutAutoAdapters still leaves it out.

func WithoutAutoAdapters added in v1.10.3

func WithoutAutoAdapters() ExtOption

WithoutAutoAdapters stops the extension from discovering nexus, shield, weave and sentinel in the DI container. Without it, each one found is wired into the engine: nexus as the model client, shield as the safety scanner and the publication gate, weave as the knowledge provider and sentinel as an extension. Use it when the host wires those itself, or wants none of them.

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. It waits for runs in flight for at most shutdown_timeout (when positive), or until ctx ends, whichever comes first. If the wait runs out, Stop returns an error matching context.DeadlineExceeded and leaves the extension started; calling Stop again completes the shutdown.

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