Skip to main content

API reference

scp-protocol v0.5.0.

PatternStore

new PatternStore(opts: {
  featureExtractor?: (entity) => object
  confidenceThreshold?: number    // default 0.6
  maxPatterns?: number            // default 500
  explorationRate?: number        // default 0.1
  failureThreshold?: number       // default 3
  storage?: "memory" | "localStorage" | "sqlite"
  storageKey?: string
})
MethodReturnsNotes
lookup(entity){ decision, confidence, source } | nullsource is "exact" or "similar"
learn(entity, decision)voidbumps count or creates new pattern
correct(entity, brainDecision)voidresets confidence and decision
report(entity, success: boolean){ found, invalidated }tracks consecutive failures
getSuccessRate(entity)number | nullper-pattern rate
save()voidlocalStorage or sqlite
load()voidlocalStorage or sqlite
stats()objecthits, misses, hitRate, …
Events: pattern_invalidated fires when a pattern crosses failureThreshold.

AdaptiveMemory

new AdaptiveMemory(opts: {
  threshold?: number           // default 0.8
  maxHistory?: number          // default 500
  k?: number                   // default 5
  weights?: { [key: string]: number }
  failureThreshold?: number    // default 3
  storage?: "memory" | "sqlite"
  storageKey?: string          // default "scp_adaptive.db"
})
MethodReturnsNotes
store(features, decision, outcome?)entryreinforces if features already present
lookup(features){ decision, confidence } | nulltop-k blended
report(features, success){ found, purged? }auto-purges at threshold
save()numberrows written to SQLite
load()numberrows loaded from SQLite
stats()objectentries, hits, misses, avgConfidence
Events: entry_purged fires when a decision is removed.

SCPBody

new SCPBody(opts: {
  name?: string
  transport?: "inprocess" | "http"   // default "inprocess"
  host?: string
  port?: number                       // required when transport="http"
  patternStore?: PatternStore
  adaptiveMemory?: AdaptiveMemory
})
Static fields a subclass may override:
static bodyName: string
static transport: "inprocess" | "http"
static host: string
static port: number
static tools: { [name: string]: { description, parameters } }
MethodReturnsNotes
invokeTool(name, params)resultcalls the matching async method
decideLocally(entity){ decision, confidence, source } | nullwalks pattern then adaptive
learnFromBrain(entity, decision)voidwrites to both cache layers
notifyDecision(entity, decision, meta)voidtells the Space (managed mode)
rememberCachedEntity(entity)voidfor outcome reporting
evaluateOutcome(state)true | false | nulloverride to enable auto-report
tick()promisesensor loop; override and call super
setState(patch)voidmerges into the live snapshot
emit(type, payload, priority)voidpriority is CRITICAL, HIGH, NORMAL, LOW
snapshot()objectaggregator-shaped state
installShutdownHandlers()thispersists stores on SIGINT / SIGTERM
clearPendingEvents()voidempties the queue

SCPAdapter (legacy)

The v0.1 contract. Kept for backward compatibility. New code should use SCPBody.
MethodNotes
reflex(name, fn)register a sync rule
_scpCall(method, args)legacy transport hook

SCPBridge

Base class for any LLM client.
new SCPBridge(opts: {
  model?: string
  systemPrompt?: string
  maxTokens?: number       // default 512
  temperature?: number     // default 0.1
})
MethodReturnsNotes
call(prompt, tools){ decision, raw }implement in subclass
invoke(prompt, tools)same as calladds timing
stats()objectcallCount, errorCount, lastCallMs, avgCallMs

Bridges

  • OllamaBridge — local, free. host defaults to http://localhost:11434.
  • BedrockBridge — AWS Converse. Requires @aws-sdk/client-bedrock-runtime.
  • OpenAIBridge — raw HTTPS to api.openai.com. Reads OPENAI_API_KEY.

Transports

SCPBody is in-process by default. Two explicit transports:
  • HTTPTransportnode:http server with /emit, /poll, /health. No external dependency.
  • WebSocketTransport — requires ws (optional peer dep).

PRIORITY

const { PRIORITY } = require("scp-protocol")
// { CRITICAL: 0, HIGH: 1, NORMAL: 2, LOW: 3 }