npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@synapsis/brain

v0.1.0

Published

Core runtime primitives for Synapsis.

Readme

@synapsis/brain

@synapsis/brain is the orchestration layer for Synapsis.

It combines:

  • @synapsis/neuron for typed LLM steps
  • @synapsis/pathway for step composition
  • @synapsis/cortex for memory, queues, and worker leases

The result is a runtime that can register steps by stable keys, queue pathway runs, execute them through workers, and persist their state.

What This Package Means

Use a brain when you want more than isolated executables.

A brain gives you:

  • a shared registry of neurons, actions, and pathways
  • queued pathway execution
  • worker polling and lease ownership
  • pathway run snapshots and step history in Cortex
  • shared neuron learning storage when configured

If @synapsis/neuron is a typed intelligent step, @synapsis/brain is the runtime that turns many steps into a running system.

Installation

npm install @synapsis/brain @synapsis/cortex @synapsis/openai @synapsis/neuron zod

If you already use a different agent adapter, you only need @synapsis/openai for the examples in this README.

Quick Start

import { createBrain } from "@synapsis/brain";
import { createMemoryCortex } from "@synapsis/cortex/memory";
import { createOpenAIAgentAdapter } from "@synapsis/openai";
import { z } from "@synapsis/neuron";

const cortex = createMemoryCortex();
const agent = createOpenAIAgentAdapter({
  apiKey: process.env.OPENAI_API_KEY!,
  model: "gpt-5.4-mini",
  request: {
    temperature: 0
  }
});

const brain = createBrain({
  agent,
  cortex
});

const extractIntent = brain.createNeuron({
  key: "extract-intent",
  name: "extract-intent",
  description: "Turn a raw message into structured intent.",
  input: z.object({
    message: z.string()
  }),
  output: z.object({
    summary: z.string()
  }),
  prompt: {
    role: "Support triage assistant",
    task: "Summarize the support issue in one short sentence.",
    important: ["Return JSON only."]
  },
  critic: false
});

const draftReply = brain.createAction({
  key: "draft-reply",
  name: "draft-reply",
  description: "Draft a reply from the structured summary.",
  input: z.object({
    summary: z.string()
  }),
  output: z.object({
    reply: z.string()
  }),
  run: async ({ summary }) => ({
    reply: `We received your request: ${summary}`
  })
});

const supportReply = brain.createPathway({
  key: "support-reply",
  name: "support-reply",
  description: "Extract intent and then draft a reply.",
  steps: [extractIntent, draftReply] as const
});

const stopWorker = brain.startExecutionWorker("local-worker");

try {
  const result = await supportReply.execute({
    message: "My VPN login stopped working this morning."
  });

  console.log(result.reply);
} finally {
  stopWorker();
}

Core API

createBrain(definition)

Creates a runtime with:

  • createNeuron
  • createAction
  • createPathway
  • startExecutionWorker
  • registry
  • agent
  • cortex
  • workerId

Constructor input:

createBrain({
  agent,
  cortex,
  options
})

agent is any Agent implementation from @synapsis/neuron.

cortex is any Cortex implementation from @synapsis/cortex.

brain.createNeuron(definition)

Wraps @synapsis/neuron#createNeuron, but adds:

  • a required stable key
  • automatic registry registration
  • shared Cortex-backed learning storage when learning is configured

brain.createAction(definition)

Creates a deterministic typed executable for non-LLM logic.

Actions:

  • validate input and output with Zod
  • register themselves in the brain registry
  • receive { cortex, registry } in run

Example:

const normalize = brain.createAction({
  key: "normalize-message",
  name: "normalize-message",
  description: "Normalize a support message.",
  input: z.object({ message: z.string() }),
  output: z.object({ summary: z.string() }),
  run: async ({ message }) => ({
    summary: message.trim().toUpperCase()
  })
});

brain.createPathway(definition)

Creates a queued executable pathway with:

  • a stable key
  • static pathway compatibility validation
  • registry registration
  • async execution through Cortex queues and workers

Unlike @synapsis/pathway#createPathway, the brain version is executable.

brain.startExecutionWorker(label?)

Starts a lightweight polling worker that drains:

  • the pathway queue
  • the executable queue

It returns a stop function:

const stop = brain.startExecutionWorker();
stop();

Runtime Options

createBrain({ options }) accepts BrainRuntimeOptions:

  • pathwayWaitIntervalMs
  • pathwayWaitTimeoutMs
  • pathwayLockTtlMs
  • pathwayMaxRetries
  • executableWaitIntervalMs
  • executableWaitTimeoutMs
  • executableLockTtlMs
  • executableMaxRetries

Use these when you need to tune:

  • worker polling cadence
  • timeout behavior while waiting for queued runs
  • lease time-to-live
  • retry budgets for queued work

What Gets Persisted

The brain runtime stores pathway and executable state in Cortex memory.

That includes:

  • pathway run records
  • per-step pathway records
  • executable run records
  • executable event timelines
  • shared neuron learning examples when enabled

This makes it possible to:

  • inspect run progress
  • debug failed steps
  • share state across workers
  • keep pathway execution reproducible

Safe Execution Behavior

For pathways created through the brain:

  • execute(input) throws on failure
  • safeExecute(input) returns structured details

The safe pathway result includes details such as:

  • queued
  • runId
  • status
  • steps

This is useful when you want to surface workflow status to a UI or logs.

Mental Model

Think of the package like this:

  • neurons are intelligent typed steps
  • actions are deterministic typed steps
  • pathways are ordered chains of steps
  • the brain is the runtime that registers, queues, and runs them

Exports

Primary exports:

  • createBrain

Useful types:

  • Brain
  • BrainDefinition
  • BrainRuntimeOptions
  • BrainNeuronDefinition
  • BrainActionDefinition
  • BrainPathwayDefinition
  • BrainPathwayRun
  • BrainExecutableRun
  • BrainExecutableRunEvent

When To Use What

Use @synapsis/brain when:

  • you want stable keys for executables
  • you want background workers
  • you want queued pathway execution
  • you want persisted run state
  • you want shared learning through Cortex

Use @synapsis/neuron directly when:

  • you only need one intelligent step

Use @synapsis/pathway directly when:

  • you only need compile-time and runtime validation of step composition