Skip to content

@stello-ai/server

Server package providing HTTP/WebSocket service, PostgreSQL persistence, and multi-tenant Space management.

createStelloServer

typescript
function createStelloServer(options: StelloServerOptions): Promise<StelloServer>

Create and initialize a Stello server instance (including database migration).

StelloServerOptions

typescript
interface StelloServerOptions {
  pool: pg.Pool
  agentPoolOptions: AgentPoolOptions
  skipMigrate?: boolean
}
FieldTypeDescription
poolpg.PoolPostgreSQL connection pool
agentPoolOptionsAgentPoolOptionsAgent pool configuration
skipMigratebooleanSkip database migration (for tests or pre-migrated databases)

StelloServer

typescript
interface StelloServer {
  app: Hono<AuthEnv>
  listen(port?: number): Promise<{ port: number; close: () => Promise<void> }>
  spaceManager: SpaceManager
  agentPool: AgentPool
  pool: pg.Pool
}
Property/MethodDescription
appHono app instance, testable via app.request()
listenStart HTTP + WebSocket service, returns port and close function
spaceManagerSpace CRUD manager
agentPoolLazily-created StelloAgent pool keyed by spaceId
poolPG connection pool reference

createPool

typescript
function createPool(options: PoolOptions): pg.Pool

Create a PostgreSQL connection pool.

PoolOptions

typescript
interface PoolOptions {
  connectionString: string
  max?: number
  idleTimeoutMillis?: number
}
FieldTypeDescription
connectionStringstringPostgreSQL connection string
maxnumberMaximum number of connections (default: 20)
idleTimeoutMillisnumberIdle connection timeout in milliseconds (default: 30000)

migrate

typescript
function migrate(pool: pg.Pool): Promise<void>

Execute database schema migration. createStelloServer calls this automatically on startup unless skipMigrate: true is set.

AgentPoolOptions

typescript
interface AgentPoolOptions {
  buildConfig: (ctx: AgentBuildContext) => Omit<StelloAgentConfig, 'sessions' | 'memory'>
  llm?: LLMCallFn
  idleTtlMs?: number
}
FieldDescription
buildConfigBuild StelloAgentConfig for each Space (sessions and memory are auto-provided by the pool)
llmLLM call function for built-in consolidation/integration defaults
idleTtlMsIdle eviction time (ms, defaults to 5 minutes)

AgentBuildContext

typescript
interface AgentBuildContext {
  spaceId: string
  space: Space
  pool: pg.Pool
  sessionStorage: PgSessionStorage
  mainStorage: PgMainStorage
  sessionTree: PgSessionTree
  memoryEngine: PgMemoryEngine
}
FieldDescription
spaceIdSpace identifier
spaceFull Space data (including consolidatePrompt / integratePrompt)
poolPG connection pool
sessionStoragePG Session storage for this Space
mainStoragePG Main storage for this Space
sessionTreePG topology tree for this Space
memoryEnginePG memory engine for this Space

AgentPool

Manages lazy creation and caching of StelloAgent instances per Space, with automatic idle eviction.

Methods

MethodReturnDescription
getAgent(spaceId)Promise<StelloAgent>Get or create a cached agent for the given Space
evict(spaceId)voidManually remove an agent from the cache
dispose()voidStop the eviction loop and clear all cached agents

Properties

PropertyTypeDescription
sizenumberCurrent number of cached agents

SpaceManager

Manages Space lifecycle. One Space corresponds to one StelloAgent and one session tree.

Methods

MethodReturnDescription
createSpace(userId, config)Promise<Space>Create a Space and its root session
getSpace(spaceId)Promise<Space | null>Get Space by ID
updateSpace(spaceId, updates)Promise<Space>Update Space configuration
listSpaces(userId)Promise<Space[]>List all Spaces for a user
deleteSpace(spaceId)Promise<void>Delete Space (cascades to all sessions)

TIP

createSpace() automatically creates a root session (role=main) and writes the system prompt to it. updateSpace() syncs system prompt changes to the root session automatically.

Space Types

SpaceConfig

typescript
interface SpaceConfig {
  label: string
  systemPrompt?: string
  consolidatePrompt?: string
  integratePrompt?: string
  config?: Record<string, unknown>
}

Space

typescript
interface Space {
  id: string
  userId: string
  label: string
  systemPrompt: string | null
  consolidatePrompt: string | null
  integratePrompt: string | null
  config: Record<string, unknown>
  createdAt: string
  updatedAt: string
}

LLM Defaults

Re-exported from @stello-ai/core for convenience.

createDefaultConsolidateFn

typescript
function createDefaultConsolidateFn(prompt: string, llm: LLMCallFn): ConsolidateFn

Create a default consolidation function using the provided prompt and LLM.

createDefaultIntegrateFn

typescript
function createDefaultIntegrateFn(prompt: string, llm: LLMCallFn): IntegrateFn

Create a default integration function using the provided prompt and LLM.

LLMCallFn

typescript
type LLMCallFn = (messages: Message[]) => Promise<string>

A simple LLM call function that takes a message array and returns the text response. Used by the built-in consolidation/integration defaults.

HTTP REST Endpoints

All routes are mounted under the /spaces prefix. Authentication via X-API-Key header.

Space Management

MethodPathRequest BodyResponse
POST/spaces{ label, systemPrompt?, consolidatePrompt?, integratePrompt? }201 Space
GET/spaces200 Space[]
GET/spaces/:spaceId200 Space
PATCH/spaces/:spaceId{ label?, systemPrompt?, consolidatePrompt?, integratePrompt? }200 Space
DELETE/spaces/:spaceId204 No Content

Session Operations

MethodPathRequest BodyResponse
GET/:spaceId/sessions200 Session tree
GET/:spaceId/sessions/:id200 SessionMeta
GET/:spaceId/sessions/:id/messages200 Message[]
POST/:spaceId/sessions/:id/turn{ input: string }200 EngineTurnResult
POST/:spaceId/sessions/:id/fork{ label: string, scope?: string }201 SessionMeta
POST/:spaceId/sessions/:id/archive200 SessionMeta

Session Data

MethodPathRequest BodyResponse
GET/:spaceId/sessions/:id/system-prompt{ content: string | null }
PUT/:spaceId/sessions/:id/system-prompt{ content: string }{ content: string }
GET/:spaceId/sessions/:id/memory{ content: string | null }
GET/:spaceId/sessions/:id/insight{ content: string | null }
GET/:spaceId/sessions/:id/consolidate-prompt{ content: string | null }
PUT/:spaceId/sessions/:id/consolidate-prompt{ content: string }{ content: string }
GET/:spaceId/sessions/:id/integrate-prompt{ content: string | null }
PUT/:spaceId/sessions/:id/integrate-prompt{ content: string }{ content: string }

WebSocket API

Connection

ws://host/spaces/:spaceId/ws

Authentication via X-API-Key header in the upgrade request.

Client → Server Messages

typescript
// Enter a session
{ type: 'session.enter', sessionId: string }

// Leave current session
{ type: 'session.leave' }

// Non-streaming turn
{ type: 'session.message', input: string }

// Streaming turn
{ type: 'session.stream', input: string }

// Fork a child session
{ type: 'session.fork', options: { label: string, scope?: string } }

Server → Client Messages

TypePayloadDescription
session.entered{ sessionId, bootstrap }Session entered successfully
session.left{ sessionId }Session left
turn.complete{ result }Non-streaming turn result
stream.delta{ chunk }Streaming chunk (multiple)
stream.end{ result }Streaming complete
session.forked{ child }Child session created
error{ message, code? }Error response

Error Codes

CodeDescription
PARSE_ERRORInvalid JSON
UNKNOWN_TYPEUnknown message type
ALREADY_ENTEREDAlready in a session (need to leave first)
NOT_ENTEREDOperation requires being in a session
HANDLER_ERRORHandler internal error

ConnectionManager

In-memory WebSocket connection state management.

Methods

MethodReturnDescription
bind(connectionId, userId, spaceId)voidRegister a new connection
getState(connectionId)ConnectionState | nullGet connection state
attachSession(connectionId, sessionId)voidAttach connection to a session
detachSession(connectionId)string | nullDetach from session, returns previous sessionId
unbind(connectionId)ConnectionState | nullRemove connection, returns final state

Properties

PropertyTypeDescription
sizenumberCurrent connection count

ConnectionState

typescript
interface ConnectionState {
  connectionId: string
  userId: string
  spaceId: string
  sessionId: string | null   // null when not in any session
}

PG Storage Classes

PostgreSQL storage adapter implementations. These are available for direct use but are also auto-created by AgentPool via AgentBuildContext.

ClassImplementsDescription
PgSessionStorageSessionStoragePG storage for a single Session
PgMainStorageMainStoragePG storage for Main Session
PgSessionTreeSessionTreePG storage for the topology tree
PgMemoryEngineMemoryEnginePG storage for the memory engine

All constructors take (client: pg.Pool | pg.PoolClient, spaceId: string). For interface details, see @stello-ai/session.