Add per-game adventure text with RAG-based retrieval

Players can now paste the full text of a freely available adventure
when creating a game, and the DM will follow it instead of inventing
its own plot/NPCs/locations. Reuses the existing rulebook RAG
pipeline (chunking, local embedding) rather than injecting the raw
text into every turn, since real adventure modules range from a few
pages to hundreds — far beyond what fits in a prompt.

- New adventure_chunks table (game-scoped, unlike the global
  rulebook_chunks) + Game.adventure_text storing the raw source.
- ingest_adventure_text() chunks/embeds synchronously during
  POST /api/games when adventure_text is non-empty.
- build_adventure_rag_block() retrieves by similarity to the latest
  player message, scoped to game_id — plus always includes chunk 0
  (the adventure's opening) regardless of the query, since early
  Session-0 messages rarely resemble the adventure's actual hook and
  pure similarity search could miss the beginning entirely.
- System prompt instructs the DM to follow provided adventure
  excerpts strictly, deviating only when players clearly go off-script.
- Frontend: optional adventure-text field in the game creation form,
  a has_adventure flag surfaced as a small badge on the game card and
  session header.

Verified with an isolated two-game test that chunks/retrieval never
leak across games (the critical failure mode here), and live against
x.ai: a custom-written one-shot's specific NPCs, location, and hook
appeared verbatim in the DM's opening scene instead of invented ones.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Thorsten
2026-09-02 13:13:59 +02:00
parent ff4c6f07e3
commit b77f066259
14 changed files with 224 additions and 6 deletions
+6 -2
View File
@@ -14,6 +14,7 @@ export interface Game {
my_character_id: string | null;
status: "active" | "ended";
ended_reason: string | null;
has_adventure: boolean;
}
export interface Message {
@@ -47,8 +48,11 @@ export const runGameSetupChat = (messages: Record<string, unknown>[], signal?: A
signal,
});
export const createGame = (name: string, description: string) =>
apiFetch<Game>("/api/games", { method: "POST", body: JSON.stringify({ name, description }) });
export const createGame = (name: string, description: string, adventureText?: string) =>
apiFetch<Game>("/api/games", {
method: "POST",
body: JSON.stringify({ name, description, adventure_text: adventureText ?? "" }),
});
export const getGame = (id: string) => apiFetch<Game>(`/api/games/${id}`);
+1
View File
@@ -58,6 +58,7 @@ export default function GameCard({ game }: { game: Game }) {
{game.player_count} Spieler
{game.player_names.length > 0 && `: ${game.player_names.join(", ")}`}
</span>
{game.has_adventure && <span>📖 Eigenes Abenteuer</span>}
</div>
</Link>
);
+20 -2
View File
@@ -15,6 +15,7 @@ export default function GameSetupChat({ onCreated }: { onCreated: (game: Game) =
const [proposal, setProposal] = useState<GameSetupProposal | null>(null);
const [name, setName] = useState("");
const [description, setDescription] = useState("");
const [adventureText, setAdventureText] = useState("");
const [loading, setLoading] = useState(true);
const [creating, setCreating] = useState(false);
const [error, setError] = useState<string | null>(null);
@@ -76,7 +77,7 @@ export default function GameSetupChat({ onCreated }: { onCreated: (game: Game) =
setCreating(true);
setError(null);
try {
const game = await createGame(name, description);
const game = await createGame(name, description, adventureText);
onCreated(game);
} catch {
setError("Spiel konnte nicht erstellt werden.");
@@ -120,13 +121,30 @@ export default function GameSetupChat({ onCreated }: { onCreated: (game: Game) =
className="rounded bg-slate-800 px-3 py-2 outline-none focus:ring-2 focus:ring-amber-400"
rows={3}
/>
<div>
<textarea
placeholder="Abenteuertext (optional) — vollständiger Text eines frei verfügbaren Abenteuers"
value={adventureText}
onChange={(e) => setAdventureText(e.target.value)}
className="w-full rounded bg-slate-800 px-3 py-2 font-mono text-xs outline-none focus:ring-2 focus:ring-amber-400"
rows={6}
/>
<p className="mt-1 text-xs text-slate-500">
Wenn hier etwas steht, hält sich der Dungeon Master strikt an diesen Text (Handlung, NPCs,
Orte, Encounter) statt eigene Inhalte zu erfinden.
</p>
</div>
{error && <p className="text-sm text-red-400">{error}</p>}
<button
type="submit"
disabled={creating}
className="self-start rounded bg-amber-500 px-4 py-2 text-sm font-semibold text-slate-950 hover:bg-amber-400 disabled:opacity-50"
>
{creating ? "Erstelle…" : "Erstellen"}
{creating
? adventureText.trim()
? "Erstelle… (Abenteuertext wird verarbeitet, kann etwas dauern)"
: "Erstelle…"
: "Erstellen"}
</button>
</form>
)}
+3 -1
View File
@@ -163,7 +163,9 @@ export default function GameSession() {
<div className="mx-auto flex h-[calc(100vh-56px)] max-w-6xl flex-col px-4 py-4">
<div className="mb-3 flex items-center justify-between">
<div>
<h1 className="text-xl font-bold text-amber-400">{game.name}</h1>
<h1 className="text-xl font-bold text-amber-400">
{game.name} {game.has_adventure && <span className="text-sm font-normal">📖</span>}
</h1>
<p className="text-xs text-slate-500">{game.description}</p>
</div>
<div className="flex gap-2">