@warlock.js/ai
Standalone — usable in any Node project, no
@warlock.js/corerequired.
@warlock.js/ai is the AI orchestration layer of Warlock. It gives you a small ladder of primitives — agents, workflows, supervisors — that share one result envelope, one error hierarchy, one event model, and one persistence story. You pair it with a provider adapter (@warlock.js/ai-openai, @warlock.js/ai-anthropic, etc.) and you have a typed, testable, resumable AI runtime.
The package is provider-agnostic on purpose. The model, the embedder, the pricing — those live in the adapter. The orchestration — tool loops, retries, snapshot resume, structured output, middleware — lives here.
The 4-primitive ladder
Section titled “The 4-primitive ladder”ai.agent() → single LLM turn, tool loop, structured outputai.workflow() → deterministic multi-step pipeline, resumableai.supervisor() → multi-intent router with specialists, iterativeai.orchestrator() → stateful session-aware orchestration (v2 — not shipped)Each primitive is an escape hatch to the next rung of complexity. Start at the bottom; graduate up only when you need to. Every primitive returns the same envelope ({ data, error, usage, report }) and exposes .asTool(), so they compose freely.
Why a separate package
Section titled “Why a separate package”- Standalone. Drop it into a CLI, a worker, a script, an existing Express app. No Warlock framework required.
- Typed end to end.
ai.agent({ output: schema })flows the schema’s inferred type through toresult.data. Same forai.workflow<TInput, TOutput, TState>. execute()never throws. Failures funnel intoresult.erroras a typedAIErrorsubclass. You branch oninstanceofor on the stableerror.codestring.- One result shape.
{ data, error, usage, report }— agents, workflows, supervisors. Cost rolls up the tree. - Persistence is delegated. Snapshot resume + semantic cache both accept any
CacheDriverfrom@warlock.js/cache. The AI package owns no storage. - Logging is delegated. Every primitive emits through the
logsingleton from@warlock.js/logger. Configure channels once at boot; the framework picks it up.
What’s in this section
Section titled “What’s in this section”- Installation — install the core package plus a provider adapter.
- Pick a provider — OpenAI vs Anthropic vs Bedrock vs Google vs Ollama.
- Your first agent — five-minute walkthrough from zero to a streaming agent.
Where to go after
Section titled “Where to go after”- Architecture and concepts — the mental model behind agents, workflows, supervisors, middleware.
- The basics — the surface you’ll touch every day.
- Digging deeper — workflows, supervisors, persistence, errors.
- Recipes — copy-paste solutions for common patterns.
- Reference — every public export grouped by primitive.
A 30-second example
Section titled “A 30-second example”import { ai } from "@warlock.js/ai";import { OpenAISDK } from "@warlock.js/ai-openai";
const openai = new OpenAISDK({ apiKey: process.env.OPENAI_API_KEY! });
const myAgent = ai.agent({ model: openai.model({ name: "gpt-4o-mini" }), systemPrompt: "You are a concise senior TypeScript engineer.",});
const { text, usage, error } = await myAgent.execute("Why use generics?");
if (error) { console.warn(error.code, error.category);} else { console.log(text, usage.total);}No try/catch. No exception leaks. Typed errors with stable codes. That’s the contract you get from every primitive in this package.