Story Engine

Composing multi-NPC narrative systems with thunk orchestration

View as Markdown

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

1import { createSDKStore, setNPCInfo, processNPC } from '@forbocai/core';
2import { createNodeCortex } from '@forbocai/node';
3
4const store = createSDKStore();
5const cortex = createNodeCortex('smollm2-135m');
6await cortex.init();
7
8store.dispatch(setNPCInfo({ id: 'logic', persona: 'Cold deductive voice.' }));
9store.dispatch(setNPCInfo({ id: 'authority', persona: 'Commanding voice.' }));
10
11const logicTurn = await store.dispatch(processNPC({
12 npcId: 'logic',
13 text: 'Analyze the footprints at the alley.',
14 apiUrl: 'https://api.forboc.ai',
15 apiKey: process.env.FORBOCAI_API_KEY,
16 cortex
17})).unwrap();
18
19const authorityTurn = await store.dispatch(processNPC({
20 npcId: 'authority',
21 text: 'Issue an order to secure the perimeter.',
22 apiUrl: 'https://api.forboc.ai',
23 apiKey: process.env.FORBOCAI_API_KEY,
24 cortex
25})).unwrap();
26
27console.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