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 (required, 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 |
custom | string | Freeform custom context (max 500 chars) |
Properties
| Property | Type | Description |
|---|---|---|
CustomEntries | IReadOnlyList<ContextEntry> | Read-only list of custom key-value pairs |
Methods
| Method | Returns | Description |
|---|---|---|
Clone() | RuntimeContext | Create a deep copy of this context |
HasAnyContext() | bool | True if any context field is set |
ClearCustomEntries() | void | Remove all custom key-value pairs |
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 (maps from API’s memoriesRetrieved) |
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 |
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);
}
}LoreMindVoiceOutput
Namespace: Peek.LoreMind.Voice
Optional text-to-speech component for NPC voices. Uses Kokoro for fully local, offline speech synthesis.
Methods
| Method | Description |
|---|---|
Speak(string) | Fire-and-forget speech synthesis |
SpeakAsync(string) | Await-able speech synthesis |
Stop() | Stop current playback |
GetAvailableVoices() | Get voice list from provider |
GetProviderName() | Current provider name |
ReinitializeProviderAsync() | Reinitialize after settings change |
Properties
| Property | Type | Description |
|---|---|---|
IsSpeaking | bool | Currently synthesizing or playing |
IsSynthesizing | bool | Currently generating audio |
LastSpokenText | string | Most recent spoken text |
AudioSource | AudioSource | AudioSource for playback |
VoiceId | string | Voice identifier (provider-specific) |
OverrideProvider | bool | Use custom provider instead of project settings |
ProviderOverride | ITextToSpeechProvider | Custom TTS provider instance |
AudioSourceOverride | AudioSource | Custom AudioSource for this component |
Events
| Event | Type | When |
|---|---|---|
OnSpeechStarted | UnityEvent | Speech begins |
OnSpeechCompleted | UnityEvent | Speech finishes |
OnError | UnityEvent<string> | Error occurred |
Example: NPC speaks responses
void Start()
{
// Auto-speak NPC responses
npc.OnResponseReceived.AddListener(text =>
{
voiceOutput.Speak(text);
});
// Track speech state
voiceOutput.OnSpeechStarted.AddListener(() => speakingIcon.SetActive(true));
voiceOutput.OnSpeechCompleted.AddListener(() => speakingIcon.SetActive(false));
}Example: Different voices per NPC
// Assign unique voices
blacksmithVoice.VoiceId = "bm_george"; // British male
apprenticeVoice.VoiceId = "af_sky"; // American femaleTTSVoiceInfo
Namespace: Peek.LoreMind.Services
Voice metadata returned by TTS providers.
Fields
| Field | Type | Description |
|---|---|---|
id | string | Provider-specific voice ID |
name | string | Display name |
language | string | Language code (e.g., “en-US”) |
gender | string | ”male” or “female” |
description | string | Voice description |
tags | string[] | Voice attributes |
isDefault | bool | Provider’s default voice |
isLocal | bool | Runs locally (no API calls) |
Example: List available voices
foreach (var voice in voiceOutput.GetAvailableVoices())
{
Debug.Log($"{voice.name} ({voice.gender}): {voice.id}");
}TTSProviderType
Namespace: Peek.LoreMind.Services
enum TTSProviderType
{
None,
Kokoro
}STTProviderType
Namespace: Peek.LoreMind.Services
enum STTProviderType
{
None,
Whisper
}ISpeechToTextProvider
Namespace: Peek.LoreMind.Services
Interface for speech-to-text providers. Implement this to use your own STT solution.
public interface ISpeechToTextProvider
{
Task<string> TranscribeAsync(float[] audioData, int sampleRate, int channels);
Task<bool> InitializeAsync();
void Dispose();
bool IsReady { get; }
string ProviderName { get; }
}The SDK includes a Whisper implementation. See Voice Input for custom provider examples.
ITextToSpeechProvider
Namespace: Peek.LoreMind.Services
Interface for text-to-speech providers. Implement this to use your own TTS solution.
public interface ITextToSpeechProvider : IDisposable
{
Task<AudioClip> SynthesizeAsync(string text, string voiceId = null, TTSSynthesisOptions options = null);
Task SynthesizeStreamingAsync(string text, AudioSource audioSource, string voiceId = null, TTSSynthesisOptions options = null);
Task<bool> InitializeAsync();
bool IsReady { get; }
string ProviderName { get; }
bool SupportsStreaming { get; }
TTSVoiceInfo[] GetAvailableVoices();
string GetDefaultVoiceId();
}The SDK includes a Kokoro implementation. See Voice Output for custom provider examples.
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
}TransportMode
enum TransportMode
{
EditorDirect, // Direct API calls in Editor (testing)
CustomBackend // Route through your backend (production)
}Transport System
The transport system handles how NPC requests are sent. Most developers configure this through the Control Panel, but you can use these APIs for advanced scenarios.
ILoreMindTransport
Interface for custom transport implementations.
public interface ILoreMindTransport
{
Task<NPCResponse> SendRequestAsync(NPCRespondRequest request);
bool IsReady { get; }
string TransportName { get; }
}Example: Custom logging transport
public class LoggingTransport : ILoreMindTransport
{
private readonly ILoreMindTransport _inner;
public LoggingTransport(ILoreMindTransport inner)
{
_inner = inner;
}
public bool IsReady => _inner.IsReady;
public string TransportName => $"Logging({_inner.TransportName})";
public async Task<NPCResponse> SendRequestAsync(NPCRespondRequest request)
{
Debug.Log($"Sending: {request.text}");
var response = await _inner.SendRequestAsync(request);
Debug.Log($"Received: {response.response}");
return response;
}
}DirectApiTransport
Sends requests directly to LoreMind API. Used for Editor testing.
// Editor-only: The SDK creates this automatically from Control Panel settings.
// Direct construction requires baseUrl, serverApiKey, and playerId:
var transport = new DirectApiTransport(baseUrl, serverApiKey, playerId);
npc.SetTransport(transport);Note: This transport is for Editor testing only. The SDK automatically creates it when you configure your Server API Key in the Control Panel. In builds, use CustomBackendTransport.
CustomBackendTransport
Routes requests through your game server. Used for production.
var transport = new CustomBackendTransport(
backendUrl: "https://api.yourgame.com/api/npc/interact"
);
npc.SetTransport(transport);Parameters:
backendUrl- Your server endpoint that forwards to LoreMindtimeoutSeconds- Request timeout in seconds (default: 60)
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_ENTITY_MIND | Entity Mind not found | Check Entity Mind ID |
PROJECT_SUSPENDED | Project disabled | Check billing/policy |
CONTENT_FILTERED | Response blocked | Adjust prompts/restrictions |
TRANSPORT_ERROR | Request failed to send | Check network/backend configuration |
Next Steps
- Getting Started - Build your first NPC
- LoreMindNPC Component - Complete component guide
- Context System - Situational awareness
- Platform Documentation - Dashboard and API setup