REFERENCE

API and security

Review the public runtime, trusted-server boundary, exported modules, contracts, and failure behavior before shipping.
TypeScript strictNode 18+ESM + CommonJSMIT

API surface

APIRuntimePurpose
PlaystateClientClient / serverLocal decisions, rules, events, sessions.
PlaystateServerClientServerAI and authenticated mutations.
director.decideBothCreate a validated decision.
director.addRuleBothRegister a deterministic rule.
events.trackBothValidate and ingest telemetry.
sessions.start/add/endBothBuild session summaries.
server.ai.*ServerStructured generation and fallback.
server.claimRewardServerAuthorized reward mutation.

Exported modules

client

Runtime and server clients, HTTP transport.

director

Difficulty, encounters, quests, rewards, events.

rules

Conditions, actions, builders, evaluation.

ai

Provider, prompts, schemas, fallbacks.

events / runtime

Telemetry, sessions, validated state.

adapters

Unity, Godot, Unreal, generic payloads.

types

Public contracts and typed errors.

utils

Retries, cache, logging, validation, IDs.

Core public types

ts
type DecisionSource = "rules" | "ai" | "fallback";
type DecisionPriority = "low" | "medium" | "high";

interface DirectorAction {
  type: DirectorActionType;
  priority: DecisionPriority;
  reason: string;
  payload: Record<string, unknown>;
}

Public input schemas reject unknown or malformed state at the boundary. Keep custom payloads JSON-serializable for engine adapters.

Security model

01Untrusted game
02Authenticated backend
03Server client
04PlayState / provider
05Sanitized response
CapabilityGame clientTrusted server
Local deterministic decisionsYesYes
Non-sensitive telemetryYesYes
OpenAI generationNoYes
Profile mutationNoYes
Reward and event adminNoYes
Hold API keysNeverYes

Errors, retries, and caching

ErrorMeaningResponse
ValidationErrorInvalid public inputFix request; do not retry.
HttpErrorNon-success API responseInspect status.
TimeoutErrorDeadline exceededRetry or fall back.
RateLimitErrorQuota exceededBackoff with jitter.
ProviderUnavailableErrorAI unavailableUse structured fallback.
ServerOnlyErrorTrusted API used in clientMove operation to backend.
ts
try {
  await playstate.events.track(event);
} catch (error) {
  if (error instanceof ValidationError) reportInvalidTelemetry(error.details);
  else if (error instanceof RateLimitError) scheduleRetry(error.retryAfterMs);
  else if (error instanceof PlaystateError) logger.error(error.code, error.message);
}

Production shipping checklist

  • Keep every secret out of game builds and client-visible configuration.
  • Authenticate and authorize all sensitive backend requests.
  • Allowlist actions and validate payloads inside the game.
  • Set explicit timeouts, retries, cache TTL, and a redacting logger.
  • Test rules, provider failure, malformed output, and offline fallback.
  • Audit rewards, profile updates, and live-event administration.