***

title: Story Engine
subtitle: Composing multi-NPC narrative systems with thunk orchestration
slug: npm/story-engine
----------------------

Story systems are built by coordinating multiple NPC entities in store state and dispatching protocol turns per entity.

## Pattern

1. Register multiple NPCs via `setNPCInfo`
2. Reuse a shared local cortex runtime where appropriate
3. Dispatch `processNPC` per active speaker/actor
4. Persist turn outputs into game-specific narrative state

## Example

```typescript
import { createSDKStore, setNPCInfo, processNPC } from '@forbocai/core';
import { createNodeCortex } from '@forbocai/node';

const store = createSDKStore();
const cortex = createNodeCortex('smollm2-135m');
await cortex.init();

store.dispatch(setNPCInfo({ id: 'logic', persona: 'Cold deductive voice.' }));
store.dispatch(setNPCInfo({ id: 'authority', persona: 'Commanding voice.' }));

const logicTurn = await store.dispatch(processNPC({
  npcId: 'logic',
  text: 'Analyze the footprints at the alley.',
  apiUrl: 'https://api.forboc.ai',
  apiKey: process.env.FORBOCAI_API_KEY,
  cortex
})).unwrap();

const authorityTurn = await store.dispatch(processNPC({
  npcId: 'authority',
  text: 'Issue an order to secure the perimeter.',
  apiUrl: 'https://api.forboc.ai',
  apiKey: process.env.FORBOCAI_API_KEY,
  cortex
})).unwrap();

console.log(logicTurn.dialogue, authorityTurn.dialogue);
```

## Operational Guidance

* Keep one initialized cortex per process when possible
* Treat API verdict as authoritative for action validity
* Use explicit npc IDs for deterministic story routing
