cortext

package module
v1.2.2 Latest Latest
Warning

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

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

README

cortext.go

Pure Go package for Cortext — no CGO required.

The package loads a prebuilt shared library from native/<platform>/ (Zig cross-compiled from the Cortext tree) at runtime via purego. Consumers can go get this module and cross-compile with CGO_ENABLED=0.

Install

go get github.com/augmem/cortext.go

Use

package main

import (
	"fmt"

	cortext "github.com/augmem/cortext.go"
)

func main() {
	engine, err := cortext.New(":memory:", nil)
	if err != nil {
		panic(err)
	}
	defer engine.Close()

	ctx, err := engine.ProcessText("Bailey likes tennis balls.", "chat/main")
	if err != nil {
		panic(err)
	}
	fmt.Println(ctx["should_interrupt"])

	embedding, err := engine.EmbedText("embed without storing")
	if err != nil {
		panic(err)
	}
	fmt.Println(len(embedding))
}

Optional config (nil fields keep native defaults):

cfg := &cortext.Config{
	Focus:           cortext.Ptr(0.6),
	AffectInterrupt: cortext.Ptr(true),
}
engine, err := cortext.New("memory.db", cfg)

Native libraries

Bundled platforms (see native/manifest.json):

Tag Artifact
linux-x86_64 libcortext.so
linux-aarch64 libcortext.so
macos-x86_64 libcortext.dylib
macos-aarch64 libcortext.dylib
windows-x86_64 cortext.dll
windows-aarch64 cortext.dll

Load order:

  1. CORTEXT_LIBRARY_PATH (explicit shared library path)
  2. native/<platform-tag>/ inside this module
  3. Sibling development trees (../cortext/build/ffi-release, ../cortext/zig-out/lib, …)

Rebuild / refresh natives from a Cortext checkout (default ../cortext):

# Zig cross-build every platform into native/
python3 scripts/build_native.py

# One platform only
python3 scripts/build_native.py --target macos-aarch64

# Copy already-built Python package natives
python3 scripts/build_native.py --from-python-natives

Models

Cortext requires the AIST GGUF encoder. The ~135 MiB AIST-87M_q8_0.gguf is sharded into Git-friendly chunks under models/AIST-87M-GGUF/chunks/ (same layout as cortext-hermes-plugin): parts stay under 100 MiB so ordinary Git works without LFS.

On first New, the package:

  1. Verifies each chunk’s SHA-256 from models/manifest.json
  2. Concatenates them into a local cache
  3. Verifies the reassembled model SHA-256
  4. Copies models/mdbr-leaf-ir/vocab.txt beside it
  5. Sets CORTEXT_AIST_MODEL_PATH for the native create call

Cache location (first match):

  1. CORTEXT_MODEL_CACHE_DIR
  2. <dir(dbPath)>/.cortext-assets (for :memory:, the process cwd)
  3. OS user cache under augmem/cortext/models

Override with a full model path anytime:

export CORTEXT_AIST_MODEL_PATH=/path/to/AIST-87M_q8_0.gguf

Refresh shards from a full GGUF (or adopt existing hermes/plugin chunks):

python3 scripts/shard_model.py
python3 scripts/shard_model.py --source ../cortext/models/AIST-87M-GGUF/AIST-87M_q8_0.gguf

API

Matches the Cortext C ABI / other language bindings:

  • Config, Ptr, Media, Retention, ProcessOptions
  • New, Close, Version, LastError, LoadLibrary
  • ProcessText / ProcessAudio / ProcessImage (+ JSON / options / media variants)
  • EmbedText / EmbedAudio / EmbedImage
  • Consolidate, Flush, Reset

Audio input is 16 kHz mono float32 PCM. Image input is row-major RGB/RGBA with explicit dimensions.

Develop

# pure Go, no toolchain CGO; materializes model from checked-in chunks
CGO_ENABLED=0 go test .

CGO_ENABLED=0 go test . -run 'TestMaterialize|TestNewProcessEmbed' -v

Relation to augmem/cortext

Source of truth for the engine and C ABI is augmem/cortext. This repository is the distributable Go package: purego bindings plus shipped Zig-built shared libraries so application code never needs CGO or a local C++ build.

Release

Tags follow semver and are intended to track the bundled Cortext C ABI major line (for example v1.2.0 ships Cortext 1.2.x natives).

# CI runs tests on push/PR (ubuntu, macOS, Windows; CGO_ENABLED=0)
# Cutting a release:
git tag -a v1.2.0 -m "cortext.go v1.2.0"
git push origin v1.2.0

Pushing a v* tag runs GoReleaser (.github/workflows/release.yml), which:

  1. Runs go test with CGO disabled
  2. Builds source + natives/model archives
  3. Publishes a GitHub Release with checksums and changelog

Manual snapshot (no publish): Actions → Release → Run workflow.

After the first tag is public, module consumers can:

go get github.com/augmem/cortext.go@v1.2.0

Documentation

Overview

Package cortext is a pure-Go (no CGO) binding for the Cortext multimodal memory engine. It loads a prebuilt shared library shipped under native/ for the current GOOS/GOARCH (Zig cross-builds), or a path from CORTEXT_LIBRARY_PATH.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LastError

func LastError() string

LastError returns the last error produced by this OS thread's C API calls. Note: goroutine migration between calls can lose thread-local error text; prefer the error returned from each method when available.

func LibraryPath

func LibraryPath() string

LibraryPath returns the absolute path that was successfully loaded, if any.

func LoadLibrary

func LoadLibrary() error

LoadLibrary loads the native Cortext shared library if it is not already loaded. New and Version call this automatically.

func MaterializeModel

func MaterializeModel(dbPath string) (string, error)

MaterializeModel reassembles the checked-in AIST model chunks into a local cache and returns the path to the complete .gguf file.

GitHub rejects single files over 100 MB, so the model is versioned as ordinary Git chunks (same layout as cortext-hermes-plugin). This is local file I/O, never a network download; every chunk and the assembled output are verified.

Cache location (first match wins):

  1. CORTEXT_MODEL_CACHE_DIR
  2. sibling of dbPath: <dir(dbPath)>/.cortext-assets (":memory:" → cwd)
  3. user cache: ~/Library/Caches/augmem/cortext/models (macOS), etc.

func Ptr

func Ptr[T any](value T) *T

Ptr returns a pointer to value for optional Config fields.

func Version

func Version() string

Version returns the native library version string.

Types

type Config

type Config struct {
	Focus                  *float64
	Sensitivity            *float64
	Stability              *float64
	AffectInterrupt        *bool
	AffectRetrieval        *bool
	ReinforcementEnabled   *bool
	ProceduralEnabled      *bool
	SequentialEdgesEnabled *bool
	SignalFilterAudio      *bool
	SignalFilterImage      *bool
	SignalFilterText       *bool
}

Config overrides native defaults. Nil fields retain their native defaults.

type EmbeddingResult

type EmbeddingResult struct {
	Embedding []float32 `json:"embedding"`
	Dimension int       `json:"dimension"`
}

EmbeddingResult is the JSON shape returned by embed helpers.

type Handle

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

Handle is an open Cortext engine instance.

func New

func New(dbPath string, cfg *Config) (*Handle, error)

New opens a Cortext instance at dbPath (file path or ":memory:"). cfg may be nil for all native defaults.

If CORTEXT_AIST_MODEL_PATH is unset, New materializes the bundled AIST model from Git-friendly chunks under models/ (hermes-plugin style), or falls back to a sibling full .gguf path, then sets the env var for the create call.

func (*Handle) Close

func (h *Handle) Close()

Close frees the native handle. Safe to call multiple times.

func (*Handle) Consolidate

func (h *Handle) Consolidate() (map[string]any, error)

func (*Handle) ConsolidateJSON

func (h *Handle) ConsolidateJSON() ([]byte, error)

func (*Handle) EmbedAudio

func (h *Handle) EmbedAudio(pcm []float32) ([]float32, error)

func (*Handle) EmbedAudioJSON

func (h *Handle) EmbedAudioJSON(pcm []float32) ([]byte, error)

func (*Handle) EmbedImage

func (h *Handle) EmbedImage(data []byte, width int, height int, channels int) ([]float32, error)

func (*Handle) EmbedImageJSON

func (h *Handle) EmbedImageJSON(data []byte, width int, height int, channels int) ([]byte, error)

func (*Handle) EmbedText

func (h *Handle) EmbedText(text string) ([]float32, error)

func (*Handle) EmbedTextJSON

func (h *Handle) EmbedTextJSON(text string) ([]byte, error)

func (*Handle) Flush

func (h *Handle) Flush() error

func (*Handle) ProcessAudio

func (h *Handle) ProcessAudio(pcm []float32, sourceID string) (map[string]any, error)

func (*Handle) ProcessAudioJSON

func (h *Handle) ProcessAudioJSON(pcm []float32, sourceID string) ([]byte, error)

func (*Handle) ProcessAudioJSONWithOptions

func (h *Handle) ProcessAudioJSONWithOptions(pcm []float32, sourceID string, options *ProcessOptions) ([]byte, error)

func (*Handle) ProcessAudioWithMedia

func (h *Handle) ProcessAudioWithMedia(pcm []float32, sourceID string, media *Media) (map[string]any, error)

func (*Handle) ProcessAudioWithMediaAndOptions

func (h *Handle) ProcessAudioWithMediaAndOptions(pcm []float32, sourceID string, media *Media, options *ProcessOptions) (map[string]any, error)

func (*Handle) ProcessAudioWithMediaJSON

func (h *Handle) ProcessAudioWithMediaJSON(pcm []float32, sourceID string, media *Media) ([]byte, error)

func (*Handle) ProcessAudioWithMediaJSONWithOptions

func (h *Handle) ProcessAudioWithMediaJSONWithOptions(pcm []float32, sourceID string, media *Media, options *ProcessOptions) ([]byte, error)

func (*Handle) ProcessAudioWithOptions

func (h *Handle) ProcessAudioWithOptions(pcm []float32, sourceID string, options *ProcessOptions) (map[string]any, error)

func (*Handle) ProcessImage

func (h *Handle) ProcessImage(data []byte, width int, height int, channels int, sourceID string) (map[string]any, error)

func (*Handle) ProcessImageJSON

func (h *Handle) ProcessImageJSON(data []byte, width int, height int, channels int, sourceID string) ([]byte, error)

func (*Handle) ProcessImageJSONWithOptions

func (h *Handle) ProcessImageJSONWithOptions(data []byte, width int, height int, channels int, sourceID string, options *ProcessOptions) ([]byte, error)

func (*Handle) ProcessImageWithMedia

func (h *Handle) ProcessImageWithMedia(data []byte, width int, height int, channels int, sourceID string, media *Media) (map[string]any, error)

func (*Handle) ProcessImageWithMediaAndOptions

func (h *Handle) ProcessImageWithMediaAndOptions(data []byte, width int, height int, channels int, sourceID string, media *Media, options *ProcessOptions) (map[string]any, error)

func (*Handle) ProcessImageWithMediaJSON

func (h *Handle) ProcessImageWithMediaJSON(data []byte, width int, height int, channels int, sourceID string, media *Media) ([]byte, error)

func (*Handle) ProcessImageWithMediaJSONWithOptions

func (h *Handle) ProcessImageWithMediaJSONWithOptions(data []byte, width int, height int, channels int, sourceID string, media *Media, options *ProcessOptions) ([]byte, error)

func (*Handle) ProcessImageWithOptions

func (h *Handle) ProcessImageWithOptions(data []byte, width int, height int, channels int, sourceID string, options *ProcessOptions) (map[string]any, error)

func (*Handle) ProcessText

func (h *Handle) ProcessText(text string, sourceID string) (map[string]any, error)

func (*Handle) ProcessTextJSON

func (h *Handle) ProcessTextJSON(text string, sourceID string) ([]byte, error)

func (*Handle) ProcessTextJSONWithOptions

func (h *Handle) ProcessTextJSONWithOptions(text string, sourceID string, options *ProcessOptions) ([]byte, error)

func (*Handle) ProcessTextWithOptions

func (h *Handle) ProcessTextWithOptions(text string, sourceID string, options *ProcessOptions) (map[string]any, error)

func (*Handle) Reset

func (h *Handle) Reset() error

type Media

type Media struct {
	Data     []byte
	MimeType string
}

Media is optional source media stored alongside audio/image processing.

type ProcessOptions

type ProcessOptions struct {
	OmitEmbedding bool
	// Retention is Natural when zero-value / omitted.
	Retention Retention
}

ProcessOptions configures JSON process result payloads.

type Retention

type Retention int

Retention controls whether an input is naturally gated, committed, bounded, or used only for retrieval. Its zero value is Natural.

const (
	RetentionNatural Retention = iota
	RetentionDurable
	RetentionBoundary
	RetentionEphemeral
)

Jump to

Keyboard shortcuts

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