API Reference
Complete reference for the LoreMind Unity SDK. Each API includes practical usage examples.
LoreMindNPC
Namespace: Peek.LoreMind
Main component for NPC intelligence. Attach to any GameObject to enable AI conversations.
RespondAsync
Generate an NPC response to player text.
Task<NPCResponse> RespondAsync(string playerText)Example: Basic conversation
var npc = GetComponent<LoreMindNPC>();
var response = await npc.RespondAsync("What do you know about the dragon?");
if (response.success)
{
dialogueUI.ShowText(response.response);
Debug.Log($"Credits used: {response.metadata.creditsUsed}");
}
else
{
Debug.LogError($"Error: {response.error} - {response.message}");
}Respond
Fire-and-forget version. Use with events instead of await.
void Respond(string playerText)Example: Event-based dialogue
void Start()
{
npc.OnResponseReceived.AddListener(text => dialogueUI.ShowText(text));
npc.OnError.AddListener(error => Debug.LogError(error));
}
void OnPlayerSpeak(string text)
{
npc.Respond(text);
}SetLocation
Set the NPC’s current location context.
void SetLocation(string location, string details = null)Example: Location-aware responses
// Before conversation
npc.SetLocation("The Rusty Anvil Tavern", "Crowded, smell of ale");
var response = await npc.RespondAsync("Is it always this busy?");
// NPC responds knowing they're in a busy tavernSetCustomContext / GetCustomContext / RemoveCustomContext
Manage custom key-value context pairs.
void SetCustomContext(string key, string value)
string GetCustomContext(string key)
void RemoveCustomContext(string key)Example: Quest-aware NPC
// Set quest context
npc.SetCustomContext("active_quest", "Find the missing merchant");
npc.SetCustomContext("player_reputation", "trusted_ally");
var response = await npc.RespondAsync("Any news about missing people?");
// NPC knows about the quest and trusts the player
// Clear when quest completes
npc.RemoveCustomContext("active_quest");ClearConversationHistory
Clear the current conversation. Call when player walks away.
void ClearConversationHistory()Example: Ending a conversation
void OnPlayerLeaveNPC()
{
npc.ClearConversationHistory();
// Next conversation starts fresh
}Key Properties
| Property | Type | Description |
|---|---|---|
EntityMindId | string | Entity Mind to use (set in Inspector) |
Context | RuntimeContext | Current situational context |
TopK | int | Number of lore chunks to retrieve (configurable in Inspector) |
Verbosity | VerbosityPreset | Response length (Terse/Balanced/Verbose) |
Temperature | float | Creativity level (configurable in Inspector) |
IsProcessing | bool | True while waiting for response |
LastResponse | string | Most recent response text |
AutoSenseNearby | bool | Auto-detect nearby ContextTags |
SyncGlobalContext | bool | Sync with GlobalContextManager |
Events
| Event | Type | When |
|---|---|---|
OnResponseReceived | UnityEvent<string> | Response text received |
OnResponseWithMetadata | UnityEvent<NPCResponse> | Full response with metadata |
OnError | UnityEvent<string> | Error occurred |
RuntimeContext
Namespace: Peek.LoreMind.Cloud.Data
Holds situational context for NPC responses.
Fields
| Field | Type | Description |
|---|---|---|
location | string | Current location name |
locationDetails | string | Location description |
timeOfDay | string | dawn/morning/midday/afternoon/dusk/evening/night/midnight |
weather | string | clear/rainy/stormy/foggy/snowing |
atmosphere | string | peaceful/tense/festive/eerie/somber |
nearbyCharacters | string[] | Visible character names |
nearbyObjects | string[] | Notable objects |
situation | string | shopping/combat/exploring/trading |
recentEvents | string[] | Recent game events |
npcMood | string | cheerful/suspicious/nervous/tired/angry |
npcActivity | string | What NPC is doing |
playerAppearance | string | Player description |
playerReputation | string | How NPC views player |
playerVisibleItems | string[] | Notable player items |
Example: Full context setup
var context = npc.Context;
context.location = "Blacksmith Shop";
context.locationDetails = "Hot forge, smell of metal";
context.timeOfDay = "afternoon";
context.npcActivity = "hammering at the anvil";
context.npcMood = "focused";
context.playerAppearance = "carrying a broken sword";
context.recentEvents = new[] { "Dragon spotted near the village" };SetCustom / GetCustom / RemoveCustom
Manage custom context key-value pairs.
context.SetCustom("faction", "Silver Hawks");
string faction = context.GetCustom("faction");
context.RemoveCustom("faction");NPCResponse
Namespace: Peek.LoreMind.Cloud.Data
Response returned from RespondAsync().
Fields
| Field | Type | Description |
|---|---|---|
success | bool | True if response generated |
response | string | NPC’s dialogue text |
character | string | Character name |
metadata | ResponseMetadata | Usage stats |
error | string | Error code if failed |
message | string | Error message if failed |
retryAfter | int | Seconds to wait (rate limit) |
Example: Handling responses
var response = await npc.RespondAsync("Hello!");
if (response.success)
{
dialogueUI.ShowText(response.response);
Debug.Log($"Credits: {response.metadata.creditsUsed}");
}
else if (response.error == "INSUFFICIENT_CREDITS")
{
ShowBuyCreditsPrompt();
}
else if (response.error == "RATE_LIMITED")
{
await Task.Delay(response.retryAfter * 1000);
// Retry...
}ResponseMetadata
Namespace: Peek.LoreMind.Cloud.Data
Usage and performance data included with each response.
Fields
| Field | Type | Description |
|---|---|---|
model | string | LLM model used |
promptTokens | int | Input tokens |
completionTokens | int | Output tokens |
creditsUsed | float | Credits charged |
creditsRemaining | float | Credits left |
usedFallback | bool | True if fallback model was used |
loreChunksUsed | int | Lore chunks retrieved |
memoriesUsed | int | Memories retrieved |
memoryCheckpoint | bool | Auto-saved memories |
finishReason | string | stop/length/content_filter |
costBreakdown | CostBreakdown | Detailed costs |
latency | LatencyInfo | Timing info |
Example: Cost monitoring
npc.OnResponseWithMetadata.AddListener(response =>
{
var meta = response.metadata;
Debug.Log($"Model: {meta.model}");
Debug.Log($"Tokens: {meta.promptTokens} in, {meta.completionTokens} out");
Debug.Log($"Cost: {meta.creditsUsed} credits");
Debug.Log($"Remaining: {meta.creditsRemaining} credits");
// Set your own threshold based on your game's usage patterns
if (meta.creditsRemaining < YOUR_LOW_CREDITS_THRESHOLD)
ShowLowCreditsWarning();
});LocationZone
Namespace: Peek.LoreMind.Context
Trigger-based zone that auto-updates NPC location context.
Properties
| Property | Type | Description |
|---|---|---|
LocationName | string | Location name |
LocationDetails | string | Location description |
Weather | string | Weather override |
Atmosphere | string | Atmosphere override |
Priority | int | For overlapping zones |
CustomContext | IReadOnlyList<ContextEntry> | Custom key-value pairs |
Example: Zone setup via code
var zone = gameObject.AddComponent<LocationZone>();
// Configure via inspector fields or serialized propertiesNPCs implement ILocationZoneReceiver automatically and update their context when entering/exiting zones.
ContextTag
Namespace: Peek.LoreMind.Context
Marker for entities that NPCs can perceive.
Properties
| Property | Type | Description |
|---|---|---|
DisplayName | string | How NPCs see this |
Category | ContextCategory | Character/Object/Item/Landmark |
Details | string | Additional info when close |
DetailsRadius | float | Distance for details (0 = always) |
Example: Suspicious NPC tag
// In inspector:
// Display Name: "Hooded stranger"
// Category: Character
// Details: "Covered in dried blood, watching the door"
// Details Radius: 5.0
// When player asks NPC about surroundings:
// "There's a hooded stranger over there. Be careful - I saw blood on his cloak."GlobalContextManager
Namespace: Peek.LoreMind.Context
Singleton for shared world state.
Methods
| Method | Description |
|---|---|
SetTimeOfDay(string) | Update global time |
AddWorldEvent(string) | Add event NPCs know about |
ClearWorldEvents() | Remove all world events |
SetCustom(key, value) | Set global custom context |
GetCustom(key) | Get global custom context |
RemoveCustom(key) | Remove global custom context |
ResetAll() | Clear everything |
Example: Day/night cycle
public class DayNightManager : MonoBehaviour
{
void OnTimeChanged(float gameHours)
{
var global = GlobalContextManager.Instance;
if (gameHours < 6) global.SetTimeOfDay("night");
else if (gameHours < 12) global.SetTimeOfDay("morning");
else if (gameHours < 18) global.SetTimeOfDay("afternoon");
else global.SetTimeOfDay("evening");
}
void OnMajorEvent(string eventDescription)
{
GlobalContextManager.Instance.AddWorldEvent(eventDescription);
// All NPCs with SyncGlobalContext=true now know about this event
}
}LoreMindVoiceInput
Namespace: Peek.LoreMind.Voice
Optional voice input component. Requires whisper.unity.
Methods
| Method | Description |
|---|---|
StartCapture() | Begin recording |
StopCapture() | Stop and transcribe |
CaptureAndTranscribeAsync(float) | Record for duration, return text |
CancelCapture() | Cancel without transcribing |
Properties
| Property | Type | Description |
|---|---|---|
IsCapturing | bool | Currently recording |
IsTranscribing | bool | Currently processing |
CaptureDuration | float | Current recording length |
LastTranscription | string | Most recent text |
PushToTalk | bool | Use push-to-talk mode |
PushToTalkKey | KeyCode | Key to hold for recording |
TargetNPC | LoreMindNPC | Auto-send transcriptions here |
Example: Manual voice capture
async void OnVoiceButtonPressed()
{
// Duration depends on expected speech length - adjust for your game
string text = await voiceInput.CaptureAndTranscribeAsync(maxDuration);
if (!string.IsNullOrEmpty(text))
{
npc.Respond(text);
}
}INpcContextProvider
Namespace: Peek.LoreMind.Context
Interface for custom context providers. Implement this to inject context from your game systems.
public interface INpcContextProvider
{
NpcContextData GetCurrentContext();
}Example: Quest system integration
public class QuestContextProvider : MonoBehaviour, INpcContextProvider
{
public NpcContextData GetCurrentContext()
{
return new NpcContextData
{
situation = QuestManager.ActiveQuest?.Type ?? "exploring",
npcMood = HostilitySystem.IsHostile(gameObject) ? "hostile" : "neutral",
playerReputation = FactionSystem.GetReputation("Innkeepers") > 50 ? "trusted" : "stranger",
recentEvents = QuestManager.GetRecentEventSummaries(3)
};
}
}Attach to the same GameObject as LoreMindNPC. Context is fetched automatically before each API request.
Enums
VerbosityPreset
enum VerbosityPreset
{
Terse, // 1-2 sentences
Balanced, // 2-4 sentences (default)
Verbose // Detailed responses
}ContextCategory
enum ContextCategory
{
Character, // NPCs, creatures, enemies
Object, // Furniture, decorations
Item, // Weapons, tools, consumables
Landmark // Buildings, monuments
}AuthMode
enum AuthMode
{
DirectClient, // Device-based JWT sessions
ServerMediated // API key authentication
}Error Codes
| Code | Meaning | Action |
|---|---|---|
INSUFFICIENT_CREDITS | Out of credits | Purchase more at loremind.peekgames.dev |
RATE_LIMITED | Too many requests | Wait retryAfter seconds |
INVALID_SESSION | Session expired | SDK auto-refreshes |
INVALID_ENTITY_MIND | Entity Mind not found | Check Entity Mind ID |
PROJECT_SUSPENDED | Project disabled | Check billing/policy |
CONTENT_FILTERED | Response blocked | Adjust prompts/restrictions |
Next Steps
- Getting Started - Build your first NPC
- LoreMindNPC Component - Complete component guide
- Context System - Situational awareness
- Platform Documentation - Dashboard and API setup