TRUSTED AI
Context when it helps
Run structured generation behind your trusted boundary while deterministic logic keeps gameplay available through every provider failure.Trusted generation architecture
01Game request→
02Your backend→
03PlayState server client→
04Validated model output→
05Sanitized action
Never generate from the client.
Provider keys, PlayState credentials, reward mutations, and administrative operations belong only on an authenticated backend.
Create a server client
ts
import { PlaystateServerClient } from "playstate-labs";
const server = new PlaystateServerClient({
projectId: process.env.PLAYSTATE_PROJECT_ID!,
baseUrl: process.env.PLAYSTATE_BASE_URL,
apiKey: process.env.PLAYSTATE_API_KEY,
});Generation APIs
| Method | Output | Typical input |
|---|---|---|
recommendQuest | Quest structure | Biome, level, progression |
suggestEncounter | Encounter plan | Tension, party, enemies |
explainAction | Player-safe explanation | Decision and context |
suggestBalance | Bounded balance values | Performance aggregates |
ts
const result = await server.ai.recommendQuest({ biome: "forest", playerLevel: 6 });
// result.source: "ai" | "fallback"
// result.value is always typed and usableStructured output
Provider text never reaches privileged execution. Responses are parsed, validated with Zod, constrained to the requested schema, and converted into typed values.
01Prompt template→
02Provider response→
03JSON parse→
04Zod validation→
05Allowlist filter→
06Typed result
Safe fallback behavior
Timeout
Return conservative deterministic content.
Rate limit
Retry with backoff, then fall back.
Malformed output
Reject schema and use fallback.
Provider unavailable
Gameplay continues without generation.
ts
const { value, source, error } = await server.ai.suggestEncounter(context);
if (source === "fallback") logger.warn("AI unavailable", error);
applyValidatedEncounter(value);Protected operations
ts
await server.updatePlayerProfile({ playerId: "player_123", level: 7 });
await server.claimReward("player_123", "reward_456");
await server.administerEvent("summer-event", "start");- Authenticate the player before every mutation.
- Rate-limit by player, session, device, and IP.
- Audit reward claims and event administration.
- Redact secrets and sensitive player data from logs.
