Skip to content

Go SDK

The Go SDK in pkg/client is the primary interface to contextdb. It follows database/sql conventions: open one DB, share it across your application.

Installation

bash
go get github.com/antiartificial/contextdb@latest

Opening a connection

go
import "github.com/antiartificial/contextdb/pkg/client"

// In-memory (no persistence)
db, err := client.Open(client.Options{})

// Persistent embedded (BadgerDB)
db, err := client.Open(client.Options{
    DataDir: "/var/lib/contextdb",
})

// Postgres
db, err := client.Open(client.Options{
    Mode: client.ModeStandard,
    DSN:  "postgres://user:pass@localhost:5432/contextdb?sslmode=disable",
})

// Remote (gRPC to contextdb server)
db, err := client.Open(client.Options{
    Mode: client.ModeRemote,
    Addr: "localhost:7700",
})

// Scaled (Qdrant + Redis + Postgres)
db, err := client.Open(client.Options{
    Mode:       client.ModeScaled,
    DSN:        "postgres://user:pass@localhost:5432/contextdb?sslmode=disable",
    QdrantAddr: "localhost:6334",
    RedisAddr:  "localhost:6379",
})

// With auto-embedding
db, err := client.Open(client.Options{
    Embedder:   embedding.NewOpenAI("https://api.openai.com/v1", apiKey, "text-embedding-3-small", 1536),
    EmbedModel: "text-embedding-3-small",
})

// MustOpen panics on error (useful in main/tests)
db := client.MustOpen(client.Options{})

Options

FieldTypeDefaultDescription
ModeMode"embedded"Storage backend: embedded, standard, remote, scaled
DataDirstring""BadgerDB directory (empty = in-memory)
DSNstring""Postgres connection string
Addrstring""Remote server address (for ModeRemote)
QdrantAddrstring""Qdrant gRPC address (for ModeScaled)
RedisAddrstring""Redis address (for ModeScaled)
ObserveAddrstring":7702"Metrics/pprof server bind address
Logger*slog.Loggerslog.Default()Structured logger
MaxOpenConnsint10Postgres connection pool size
ConnectTimeouttime.Duration5sBackend connection timeout
Extractorextract.ExtractornilLLM entity extractor for IngestText
LLMProviderextract.ProvidernilLLM provider for extraction/compaction
Embedderembedding.EmbeddernilAuto-embedding provider
EmbedModelstring""Embedding model identifier (provenance)
VectorDimensionsint1536Embedding dimensionality

Mode constants

go
const (
    ModeEmbedded Mode = "embedded"  // In-process, zero external deps
    ModeStandard Mode = "standard"  // Postgres + pgvector
    ModeRemote   Mode = "remote"    // gRPC to contextdb server
    ModeScaled   Mode = "scaled"    // Qdrant + Redis + Postgres
)

DB methods

Namespace(name string, mode namespace.Mode) *NamespaceHandle

Returns a handle scoped to the named namespace. Creates the namespace if it doesn't exist. Subsequent calls with the same name return the same handle.

go
ns := db.Namespace("channel:general", namespace.ModeBeliefSystem)

Ping(ctx context.Context) error

Checks the connection is alive.

Stats() DBStats

Returns runtime statistics: retrieval/ingest counters, latency percentiles.

Stores() (GraphStore, VectorIndex, KVStore, EventLog)

Returns direct access to the underlying store implementations. Useful for advanced operations and testing.

Registry() *observe.Registry

Returns the observability registry for custom metric registration.

Close() error

Releases all resources. The DB is unusable after Close.

NamespaceHandle methods

Write

go
func (h *NamespaceHandle) Write(ctx context.Context, req WriteRequest) (WriteResult, error)

Ingests a single item. Runs through auto-embedding, the admission gate, and conflict detection before persisting.

WriteRequest fields:

FieldTypeRequiredDescription
ContentstringYesThe text content
SourceIDstringYesExternal source identifier
Labels[]stringNoNode labels (e.g. "Claim", "Skill")
Propertiesmap[string]anyNoArbitrary metadata
Vector[]float32NoPre-computed embedding (auto-embedded from Content if omitted and Embedder configured)
ModelIDstringNoEmbedding model identifier
Confidencefloat64NoInitial confidence [0,1]
ValidFromtime.TimeNoWhen the fact became true (default: now)
MemTypecore.MemoryTypeNoMemory type for decay

WriteResult fields:

FieldTypeDescription
NodeIDuuid.UUIDThe written (or existing duplicate) node ID
AdmittedboolWhether the write was accepted
ReasonstringRejection reason (if not admitted)
ConflictIDs[]uuid.UUIDIDs of contradicting nodes

WriteBatch

go
func (h *NamespaceHandle) WriteBatch(ctx context.Context, reqs []WriteRequest) ([]WriteResult, error)

Writes multiple items. Partial failures are possible. Results are returned in request order. The first error is returned alongside partial results.

Retrieve

go
func (h *NamespaceHandle) Retrieve(ctx context.Context, req RetrieveRequest) ([]Result, error)

Runs hybrid retrieval (vector + graph + session) with optional reranking and returns scored results.

RetrieveRequest fields:

FieldTypeRequiredDescription
Vector[]float32For vector searchQuery embedding
TextstringFor text searchAuto-embedded query (requires Embedder)
Vectors[][]float32For multi-vectorMultiple query embeddings fused
SeedIDs[]uuid.UUIDFor graph walkKnown relevant node IDs
TopKintNoMax results (default: 10)
Labels[]stringNoFilter to nodes with all specified labels
ScoreParamscore.ScoreParamsNoOverride scoring weights
Strategyretrieval.HybridStrategyNoOverride retrieval strategy
AsOftime.TimeNoPoint-in-time query (default: now)

Result fields:

FieldTypeDescription
Nodecore.NodeThe full node
Scorefloat64Composite score [0, 1]
SimilarityScorefloat64Vector similarity component
ConfidenceScorefloat64Confidence component
RecencyScorefloat64Recency component
UtilityScorefloat64Utility component
RetrievalSourcestring"vector", "graph", "session", or "fused"

GetNode

go
func (h *NamespaceHandle) GetNode(ctx context.Context, id uuid.UUID) (*core.Node, error)

Retrieves a single node by ID.

Walk

go
func (h *NamespaceHandle) Walk(ctx context.Context, seedIDs []uuid.UUID, maxDepth int) ([]WalkResult, error)

Breadth-first graph traversal from seed nodes. Returns nodes with depth and path information.

WalkResult fields:

FieldTypeDescription
Nodecore.NodeThe discovered node
DepthintHops from nearest seed
Path[]uuid.UUIDNode IDs from seed to this node

AddEdge

go
func (h *NamespaceHandle) AddEdge(ctx context.Context, edge core.Edge) error

Creates a directed edge between two nodes. Auto-fills namespace, ID, and timestamps if empty.

History

go
func (h *NamespaceHandle) History(ctx context.Context, nodeID uuid.UUID) ([]core.Node, error)

Returns all versions of a node, ordered oldest-first by transaction time.

IngestText

go
func (h *NamespaceHandle) IngestText(ctx context.Context, text, sourceID string) (*ingest.IngestResult, error)

Runs text through the LLM extraction pipeline to automatically produce nodes and edges. Requires Options.Extractor to be configured.

LabelSource

go
func (h *NamespaceHandle) LabelSource(ctx context.Context, externalID string, labels []string) error

Sets labels on a source. Use "moderator"/"admin" for full trust, "troll"/"flagged" for floor.

Export / Import

The snapshot package provides namespace export and import via NDJSON:

go
import "github.com/antiartificial/contextdb/internal/snapshot"

// Export a namespace
graph, vecs, _, _ := db.Stores()
exporter := snapshot.NewExporter(graph)

var buf bytes.Buffer
err := exporter.Export(ctx, "my-app", &buf)

// Export subgraph from seeds
err = exporter.ExportFromSeeds(ctx, "my-app", seedIDs, 3, &buf)

// Import into another DB
importer := snapshot.NewImporter(graph, vecs)
err = importer.Import(ctx, "my-app", &buf)

The NDJSON format contains one record per line:

json
{"type":"node","data":{...}}
{"type":"edge","data":{...}}
{"type":"source","data":{...}}

Released under the MIT License.