Directive
The constraint-driven runtime for TypeScript. Declare what must be true. The runtime makes it happen.

- Constraint-Driven Resolution – declare requirements, resolvers fulfill them automatically with retry, batching, and error boundaries
- AI Guardrails – prompt injection detection, PII redaction, cost tracking, multi-agent orchestration with 4 LLM adapters (zero SDK dependencies)
- Auto-Tracking Derivations – computed values that track their own dependencies, no manual dep arrays
- Typed Inbound Sources – declare external event streams (Supabase, WebSocket, polling, browser events) as first-class module fields; engine owns the subscription lifecycle
- 5 Framework Adapters + 1 Vanilla – React, Vue, Svelte, Solid, Lit, plus
el() for plain DOM – from a single state layer
- Zero Runtime Dependencies – nothing to audit, nothing to break
- Time-Travel Debugging – snapshot, rewind, replay, export/import system state
How It Works
Other state libraries react to what happened. Directive enforces what must be true.
// Traditional: describe HOW to change state
dispatch({ type: 'FETCH_USER', id: 123 });
// then write a thunk, saga, or effect to handle it...
// Directive: declare WHAT must be true
constraints: {
needsUser: {
when: (facts) => facts.userId > 0 && !facts.user,
require: { type: "FETCH_USER" },
},
}
// The runtime handles when, how, retry, and error recovery.
Set userId = 123 and the constraint fires, the resolver fetches the user, and the system settles – automatically.
Quick Start
npm install @directive-run/core
import { createModule, createSystem, t, type ModuleSchema } from '@directive-run/core';
const userModule = createModule("user", {
schema: {
facts: {
userId: t.number(),
user: t.object<{ id: number; name: string } | null>(),
},
derivations: { isLoggedIn: t.boolean() },
events: {},
requirements: { FETCH_USER: {} },
} satisfies ModuleSchema,
init: (facts) => {
facts.userId = 0;
facts.user = null;
},
constraints: {
needsUser: {
when: (facts) => facts.userId > 0 && facts.user === null,
require: { type: "FETCH_USER" },
},
},
resolvers: {
fetchUser: {
requirement: "FETCH_USER",
resolve: async (req, context) => {
const res = await fetch(`/api/users/${context.facts.userId}`);
context.facts.user = await res.json();
},
},
},
derive: {
isLoggedIn: (facts) => facts.user !== null,
},
});
const system = createSystem({ module: userModule });
system.start();
system.facts.userId = 123; // Constraint fires automatically
await system.settle(); // Wait for resolution
console.log(system.facts.user); // { id: 123, name: "John" }
AI Guardrails and Orchestration
The same constraint-driven model powers AI agent safety. Guardrails are constraints. Agent runs are resolvers. The runtime enforces them automatically.
npm install @directive-run/ai
import {
createAgentOrchestrator,
createModerationGuardrail,
createPIIGuardrail,
} from '@directive-run/ai';
import { createAnthropicRunner } from '@directive-run/ai/anthropic';
const orchestrator = createAgentOrchestrator({
runner: createAnthropicRunner({
apiKey: process.env.ANTHROPIC_API_KEY!,
model: "claude-sonnet-4-5-20250929",
}),
guardrails: {
// Redacts SSNs and card numbers by default; pass `patterns` to add your own.
input: [createPIIGuardrail()],
// `checkFn` is yours — call your moderation endpoint, or match locally.
output: [
createModerationGuardrail({
checkFn: (text) => /\b(secret|confidential)\b/i.test(text),
}),
],
},
maxTokenBudget: 100_000,
});
const result = await orchestrator.run(
{ name: "assistant", instructions: "You are a helpful assistant." },
"Summarize this thread.",
);