API Reference - ByteSower/qnce-engine GitHub Wiki
Notice (Feb 2026): The QNCE Engine is currently undergoing major reconstruction. Many features, including narrative rules and story flow, may not behave as expected. The engine is transitioning toward broader quantum modeling principles and improved performance. If you require stability, consider waiting for a future release. See GitHub and npm for updates.
Complete API documentation for QNCE Engine classes, interfaces, and functions.
- Core API Overview
- Core Engine API
- Data Models
- Performance API
- React UI Components
- CLI Tools
- Advanced Features
The QNCE Engine API is organized into several main modules:
| Module | Purpose | Key Exports |
|---|---|---|
| Core Engine | Main engine functionality |
createQNCEEngine, QNCEEngine
|
| Story Models | Data structures and types |
QNCEStory, Node, Choice
|
| React UI | React integration components |
useQNCE, UndoRedoControls
|
| Performance | Optimization and monitoring |
PerformanceMonitor, ObjectPool
|
| CLI Tools | Command-line utilities |
AuditTool, PerfTool, InitTool
|
Creates a new QNCE Engine instance.
function createQNCEEngine(
story: QNCEStory,
options?: QNCEEngineOptions
): QNCEEngine-
story(QNCEStory): The story data to load -
options(QNCEEngineOptions, optional): Configuration options
QNCEEngine - A new engine instance
import { createQNCEEngine } from 'qnce-engine';
const engine = createQNCEEngine(storyData, {
enablePerformanceMode: true,
enableBranching: true
});
// Persistence via StorageAdapter (Lane B)
// Choose an adapter and attach to the engine
import { createStorageAdapter } from '../persistence/StorageAdapters';
const storage = createStorageAdapter('memory'); // 'localStorage' | 'sessionStorage' | 'file' | 'indexedDB'
engine.attachStorageAdapter(storage);
// Save/load helpers
await engine.saveToStorage('slot1');
await engine.loadFromStorage('slot1');Main engine class for story execution and state management.
| Property | Type | Description |
|---|---|---|
currentNodeId |
string |
ID of the current story node |
flags |
Record<string, any> |
Current story flags |
history |
string[] |
Navigation history |
isComplete |
boolean |
Whether story has reached an end |
getCurrentNode(): NodeReturns the current story node.
Returns: Node - Current node object
Example:
const currentNode = engine.getCurrentNode();
console.log(currentNode.text);getAvailableChoices(): Choice[]Returns available choices from the current node.
Returns: Choice[] - Array of available choices
Example:
const choices = engine.getAvailableChoices();
choices.forEach((choice, index) => {
console.log(`${index + 1}. ${choice.text}`);
});goToNodeById(nodeId: string): voidNavigates directly to a specific node by its ID. Throws QNCENavigationError if the node ID is invalid.
Parameters:
-
nodeId(string): The ID of the node to navigate to.
Example:
try {
engine.goToNodeById('chapter_2_start');
} catch (error) {
console.error(error.message);
}makeChoice(choiceIndex: number): voidMakes a choice and advances the story.
Parameters:
-
choiceIndex(number): Index of the choice to make
Throws: Error if choice index is invalid
Example:
engine.makeChoice(0); // Make the first choicegetFlags(): Record<string, any>Returns current story flags.
Returns: Record<string, any> - Current flag values
Example:
const flags = engine.getFlags();
console.log('Player courage:', flags.courage);setFlag(key: string, value: any): voidSets a story flag value.
Parameters:
-
key(string): Flag name -
value(any): Flag value
Example:
engine.setFlag('player_level', 5);
engine.setFlag('has_sword', true);setConditionEvaluator(evaluator: CustomConditionEvaluator): voidSets a custom condition evaluator for complex choice logic.
Parameters:
-
evaluator(CustomConditionEvaluator): Function that evaluates custom conditions
Example:
engine.setConditionEvaluator((expression, context) => {
if (expression === 'hasSpecialItem') {
return context.flags.inventory?.includes('magic_sword');
}
return null; // Fall back to default evaluator
});clearConditionEvaluator(): voidRemoves the custom condition evaluator, reverting to default expression evaluation.
Example:
engine.clearConditionEvaluator();validateCondition(expression: string): booleanValidates whether a condition expression is syntactically correct and safe.
Parameters:
-
expression(string): The condition expression to validate
Returns: boolean - True if the expression is valid
Throws: ConditionEvaluationError if the expression is invalid
Example:
try {
engine.validateCondition('flags.level >= 5 && !flags.gameOver');
console.log('Expression is valid');
} catch (error) {
console.error('Invalid expression:', error.message);
}getConditionFlags(expression: string): string[]Extracts flag references from a condition expression for debugging or analysis.
Parameters:
-
expression(string): The condition expression to analyze
Returns: string[] - Array of flag names referenced in the expression
Example:
const flags = engine.getConditionFlags('flags.strength >= 3 && flags.hasKey');
console.log(flags); // ['strength', 'hasKey']reset(): voidResets the engine to the initial state.
Example:
engine.reset(); // Start story overasync saveState(options?: SerializationOptions): Promise<string>Serializes the complete engine state to a JSON string. This is ideal for saving game progress.
Parameters:
-
options(SerializationOptions, optional): Configuration for serialization, like enabling checksums or pretty printing.
Returns: Promise<string> - A JSON string representing the complete engine state.
Example:
const savedState = await engine.saveState({ prettyPrint: true });
// Store `savedState` in localStorage, a file, or a database.async loadState(serializedState: string | SerializedState, options?: LoadOptions): Promise<void>Restores the engine state from a serialized state object or a JSON string.
Parameters:
-
serializedState(string | SerializedState): The state to load. -
options(LoadOptions, optional): Configuration for deserialization, like verifying checksums.
Throws: Error if the state is invalid, corrupted, or incompatible.
Example:
await engine.loadState(savedState);
console.log('State loaded!');async createCheckpoint(name?: string): Promise<Checkpoint>Creates a lightweight, in-memory checkpoint of the current narrative state. Useful for implementing undo/redo functionality.
Parameters:
-
name(string, optional): A descriptive name for the checkpoint (e.g., "Before risky choice").
Returns: Promise<Checkpoint> - The created checkpoint object, containing its ID and a snapshot of the state.
Example:
const checkpoint = await engine.createCheckpoint('Before risky choice');async restoreFromCheckpoint(checkpointId: string): Promise<void>Restores the engine state from a previously created in-memory checkpoint.
Parameters:
-
checkpointId(string): The ID of the checkpoint to restore.
Throws: Error if the checkpoint ID is not found.
Example:
await engine.restoreFromCheckpoint(checkpoint.id);
console.log('Restored to previous state.');Main story data structure.
interface QNCEStory {
id: string;
title: string;
version: string;
metadata: StoryMetadata;
branchingConfig?: BranchingConfig;
chapters?: Chapter[];
nodes: Node[]; // For basic stories
initialNodeId?: string;
aiContext?: AIContext;
}| Property | Type | Required | Description |
|---|---|---|---|
id |
string |
✅ | Unique story identifier |
title |
string |
✅ | Story title |
version |
string |
✅ | Story version (semver) |
metadata |
StoryMetadata |
✅ | Story metadata |
branchingConfig |
BranchingConfig |
❌ | Advanced branching configuration |
chapters |
Chapter[] |
❌ | Organized story chapters |
nodes |
Node[] |
✅* | Story nodes (basic format) |
initialNodeId |
string |
❌ | Starting node ID |
aiContext |
AIContext |
❌ | AI integration context |
*Required for basic stories, optional for advanced branching stories
Individual story moment or scene.
interface Node {
id: string;
text: string;
choices: Choice[];
flags?: Record<string, any>;
metadata?: NodeMetadata;
conditions?: Condition[];
}| Property | Type | Required | Description |
|---|---|---|---|
id |
string |
✅ | Unique node identifier |
text |
string |
✅ | Node narrative text |
choices |
Choice[] |
✅ | Available player choices |
flags |
Record<string, any> |
❌ | Flags set when entering node |
metadata |
NodeMetadata |
❌ | Additional node information |
conditions |
Condition[] |
❌ | Conditions for node availability |
Player choice option.
interface Choice {
text: string;
nextNodeId: string;
flagEffects?: Record<string, any>;
condition?: string;
conditions?: Condition[];
metadata?: ChoiceMetadata;
}| Property | Type | Required | Description |
|---|---|---|---|
text |
string |
✅ | Choice display text |
nextNodeId |
string |
✅ | Target node ID |
flagEffects |
Record<string, any> |
❌ | Flags changed by this choice |
condition |
string |
❌ | Expression for choice availability (e.g., "flags.level >= 3") |
conditions |
Condition[] |
❌ | Legacy: Conditions for choice availability |
metadata |
ChoiceMetadata |
❌ | Additional choice information |
The condition property accepts JavaScript-like expressions for determining choice availability:
// Simple flag checks
"flags.hasKey"
"!flags.doorLocked"
// Numeric comparisons
"flags.level >= 5"
"flags.health > 0"
// Complex logical expressions
"flags.strength >= 3 && flags.hasWeapon"
"flags.magic > 2 || flags.charm >= 5"
"(flags.stealth >= 4 || flags.lockpick) && !flags.alarmTriggered"
// Context access
"context.timeElapsed < 300"
"context.visitCount <= 2"Types for custom condition evaluation logic.
type CustomConditionEvaluator = (
expression: string,
context: ConditionContext
) => boolean | null;
interface ConditionContext {
flags: Record<string, any>;
context: {
timeElapsed?: number;
visitCount?: number;
[key: string]: any;
};
}
class ConditionEvaluationError extends Error {
expression: string;
originalError?: Error;
constructor(message: string, expression: string, originalError?: Error);
}A function that evaluates custom condition logic. Return null to fall back to default expression evaluation.
Parameters:
-
expression(string): The condition expression to evaluate -
context(ConditionContext): Current game state and context
Returns: boolean | null - true if condition is met, false if not, null to use default evaluator
Context object passed to condition evaluators containing current game state.
Properties:
-
flags(Record<string, any>): Current story flags -
context(object): Additional context like time elapsed, visit counts, etc.
Error thrown when condition expressions are invalid or unsafe.
Properties:
-
expression(string): The expression that caused the error -
originalError(Error, optional): The underlying error if available
QNCE Engine includes enterprise-grade performance monitoring and optimization tools.
Real-time performance tracking and analytics for narrative engines.
import { PerformanceMonitor } from 'qnce-engine/performance';
const monitor = new PerformanceMonitor({
enableMetrics: true,
sampleRate: 100
});startTracking(sessionId?: string): voidBegin performance monitoring for the current session.
getMetrics(): PerformanceMetricsReturns current performance metrics including response times, memory usage, and choice analytics.
generateReport(): PerformanceReportGenerates a comprehensive performance report with recommendations.
Memory optimization through object pooling for high-performance applications.
import { ObjectPool } from 'qnce-engine/performance';
const nodePool = new ObjectPool(() => ({ /* node template */ }), 100);acquire(): TGet an object from the pool.
release(obj: T): voidReturn an object to the pool.
clear(): voidClear all objects from the pool.
QNCE Engine provides ready-to-use React components and hooks for building narrative interfaces.
React hook for integrating QNCE Engine with React components. Provides reactive state management and action handlers.
function useQNCE(engine: QNCEEngine, config?: UseQNCEConfig): UseQNCEReturn-
engine(QNCEEngine): The QNCE Engine instance -
config(UseQNCEConfig, optional): Configuration options
interface UseQNCEConfig {
autoUpdate?: boolean; // Auto re-render on state changes (default: true)
enableUndoRedo?: boolean; // Enable undo/redo functionality (default: true)
maxUndoEntries?: number; // Maximum undo entries (default: 50)
maxRedoEntries?: number; // Maximum redo entries (default: 25)
enableAutosave?: boolean; // Enable autosave (default: true)
autosaveThrottleMs?: number; // Autosave throttle in ms (default: 100)
}interface UseQNCEReturn {
currentNode: Node | null;
availableChoices: Choice[];
selectChoice: (choice: Choice) => void;
flags: Record<string, any>;
isComplete: boolean;
history: string[];
undo: () => void;
redo: () => void;
canUndo: boolean;
canRedo: boolean;
undoCount: number;
redoCount: number;
}import React from 'react';
import { useQNCE } from 'qnce-engine/integrations/react';
function StoryComponent({ engine }) {
const {
currentNode,
availableChoices,
selectChoice,
undo,
redo,
canUndo,
canRedo
} = useQNCE(engine, {
enableUndoRedo: true,
enableAutosave: true,
maxUndoEntries: 50
});
return (
<div>
<p>{currentNode?.text}</p>
<div>
{availableChoices.map(choice => (
<button key={choice.text} onClick={() => selectChoice(choice)}>
{choice.text}
</button>
))}
</div>
<div>
<button onClick={undo} disabled={!canUndo}>
Undo
</button>
<button onClick={redo} disabled={!canRedo}>
Redo
</button>
</div>
</div>
);
}Pre-built accessible undo/redo controls with theming support.
interface UndoRedoControlsProps {
engine: QNCEEngine;
theme?: Partial<QNCETheme>;
className?: string;
style?: React.CSSProperties;
disabled?: boolean;
showLabels?: boolean;
labels?: { undo: string; redo: string };
size?: 'sm' | 'md' | 'lg';
layout?: 'horizontal' | 'vertical';
onUndo?: () => void;
onRedo?: () => void;
}import React from 'react';
import { UndoRedoControls } from 'qnce-engine/ui';
function GameInterface({ engine }) {
return (
<div>
{/* Your game content */}
<UndoRedoControls
engine={engine}
size="lg"
showLabels={true}
theme={{
colors: {
primary: '#007bff',
secondary: '#6c757d'
}
}}
onUndo={() => console.log('Undo clicked')}
onRedo={() => console.log('Redo clicked')}
/>
</div>
);
}Visual indicator showing autosave status with real-time updates.
interface AutosaveIndicatorProps {
engine: QNCEEngine;
theme?: Partial<QNCETheme>;
className?: string;
style?: React.CSSProperties;
variant?: 'minimal' | 'detailed';
position?: 'inline' | 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right';
messages?: {
idle: string;
saving: string;
saved: string;
error: string;
disabled: string;
};
showTimestamp?: boolean;
autoHideDelay?: number;
}import React from 'react';
import { AutosaveIndicator } from 'qnce-engine/ui';
function GameInterface({ engine }) {
return (
<div>
<AutosaveIndicator
engine={engine}
variant="detailed"
position="top-right"
showTimestamp={true}
autoHideDelay={5000}
messages={{
saving: 'Saving your progress...',
saved: 'Progress saved!',
error: 'Save failed - check connection'
}}
/>
{/* Your game content */}
</div>
);
}React hook for adding keyboard shortcuts to QNCE Engine interactions.
function useKeyboardShortcuts(config: KeyboardShortcutsConfig): void
interface KeyboardShortcutsConfig {
engine: QNCEEngine;
shortcuts?: {
undo?: string; // Default: 'ctrl+z' or 'cmd+z'
redo?: string; // Default: 'ctrl+y' or 'cmd+shift+z'
save?: string; // Default: 'ctrl+s' or 'cmd+s'
[key: string]: string | undefined;
};
disabled?: boolean;
preventDefault?: boolean;
}import React from 'react';
import { useKeyboardShortcuts } from 'qnce-engine/ui';
function GameComponent({ engine }) {
useKeyboardShortcuts({
engine,
shortcuts: {
undo: 'ctrl+z',
redo: 'ctrl+shift+z',
save: 'ctrl+s'
},
preventDefault: true
});
// Component content...
}Command-line utilities for development, testing, and production monitoring.
Initialize a new QNCE project with templates and configuration.
# Create a new interactive story project
qnce-init my-story --template basic
# Advanced project with React integration
qnce-init my-game --template react-typescript-
--template <type>: Project template (basic, advanced, react, react-typescript) -
--output <dir>: Output directory -
--version: Show version -
--help: Show help
Validate story files for errors, performance issues, and best practices.
# Audit a story file
qnce-audit story.json
# Audit with detailed performance analysis
qnce-audit story.json --performance --verbose-
--performance: Include performance analysis -
--verbose: Detailed output -
--format <type>: Output format (json, table, markdown) -
--output <file>: Save report to file
Performance monitoring and benchmarking tools.
# Run performance tests
qnce-perf benchmark story.json
# Monitor live performance
qnce-perf monitor --port 3001-
--port <number>: Port for monitoring server -
--duration <ms>: Test duration -
--concurrent <number>: Concurrent operations -
--format <type>: Report format
Interactive story testing and debugging tool.
# Play through a story interactively
qnce-play story.json
# Start from a specific node
qnce-play story.json --start chapter_2-
--start <nodeId>: Starting node ID -
--debug: Enable debug mode -
--flags <json>: Initial flags (JSON object) -
--auto: Auto-play with random choices
Advanced configuration for state persistence.
interface SerializationOptions {
prettyPrint?: boolean; // Format JSON with indentation
includeMetadata?: boolean; // Include engine metadata
enableChecksum?: boolean; // Add data integrity checksum
compression?: 'none' | 'gzip'; // Compression algorithm
}Configuration for state loading and validation.
interface LoadOptions {
validateChecksum?: boolean; // Verify data integrity
allowMigration?: boolean; // Allow version migration
strict?: boolean; // Strict validation mode
timeout?: number; // Load timeout in ms
}QNCE Engine provides specific error types for better error handling.
// Navigation errors
class QNCENavigationError extends Error {
nodeId: string;
constructor(message: string, nodeId: string);
}
// Validation errors
class ValidationError extends Error {
field: string;
value: any;
constructor(message: string, field: string, value?: any);
}
// Condition evaluation errors
class ConditionEvaluationError extends Error {
expression: string;
originalError?: Error;
constructor(message: string, expression: string, originalError?: Error);
}
// Serialization errors
class SerializationError extends Error {
operation: 'save' | 'load';
constructor(message: string, operation: 'save' | 'load');
}Support for upgrading saved states between engine versions.
interface MigrationHandler {
fromVersion: string;
toVersion: string;
migrate: (oldState: any) => SerializedState;
}
// Register custom migration
engine.registerMigration({
fromVersion: '1.0.0',
toVersion: '1.2.0',
migrate: (oldState) => {
// Transform old state format to new format
return transformedState;
}
});- Getting Started - Quick start guide and tutorials
- Branching Guide - Advanced narrative branching techniques
- Performance Tuning - Optimization best practices
- CLI Usage - Command-line tools documentation
- Release Notes - Version history and migration guides
Need help with the API?
Getting Started → | Examples → | GitHub Issues →
Built with ❤️ by the QNCE development team
You are here: API Reference
Previous: CLI Usage ← | Next: Contributing →
All Pages: Home • Getting Started • Branching Guide • Performance Tuning • CLI Usage • API Reference • Contributing • Release Notes
This documentation is maintained for QNCE Engine v1.3.0 with advanced feature set including Choice Validation, State Persistence, Conditional Choices, Autosave & Undo/Redo, and UI Components.