Compare commits
31 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 8d5c2e2b7f | |||
| 788d2c12f2 | |||
| b25a9746cf | |||
| 41fda9aeee | |||
| 550078268c | |||
| 0582690dca | |||
| 88278bd1dd | |||
| d24d6459a0 | |||
| 4b866e5212 | |||
| 6c6bd20f0d | |||
| 2764228abf | |||
| f95d69c915 | |||
| 70351e9d9a | |||
| ff4905126d | |||
| 0e5b5a7de4 | |||
| c8c032336b | |||
| dda27e55fc | |||
| 83ac67471e | |||
| e3c8232e38 | |||
| a4df9fc759 | |||
| f1989c1d77 | |||
| 8efdf5d0e0 | |||
| 96bc5de942 | |||
| 84ccdd53ad | |||
| 29978058ee | |||
| e510f64336 | |||
| f189f67aaf | |||
| 8efa148739 | |||
| 8f4dd3e9d6 | |||
| bf38b6695f | |||
| 49a82d05f7 |
12
.env.example
12
.env.example
@@ -38,3 +38,15 @@ LLM_MODEL=gemma4:26b
|
|||||||
# 1min.ai (si LLM_PROVIDER=onemin)
|
# 1min.ai (si LLM_PROVIDER=onemin)
|
||||||
ONEMIN_API_KEY=
|
ONEMIN_API_KEY=
|
||||||
ONEMIN_MODEL=gpt-4o-mini
|
ONEMIN_MODEL=gpt-4o-mini
|
||||||
|
|
||||||
|
# --- Mises a jour automatiques (Watchtower) ------------------------------
|
||||||
|
# Watchtower verifie les nouvelles versions de core/brain/web et permet
|
||||||
|
# le declenchement manuel via l'UI (bouton "Mettre a jour"). Postgres et
|
||||||
|
# MinIO sont exclus volontairement.
|
||||||
|
#
|
||||||
|
# Activer : COMPOSE_PROFILES=autoupdate + WATCHTOWER_TOKEN non vide.
|
||||||
|
# COMPOSE_PROFILES=autoupdate
|
||||||
|
# WATCHTOWER_TOKEN=change-me-use-openssl-rand-hex-32
|
||||||
|
# WATCHTOWER_MONITOR_ONLY=false # true = detecter sans appliquer
|
||||||
|
# WATCHTOWER_SCHEDULE=0 0 4 * * *
|
||||||
|
# TZ=Europe/Paris
|
||||||
|
|||||||
95
.gitea/workflows/e2e.yml
Normal file
95
.gitea/workflows/e2e.yml
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
name: E2E Tests
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
pull_request:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
e2e:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
timeout-minutes: 30
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Set up Docker Buildx
|
||||||
|
uses: docker/setup-buildx-action@v3
|
||||||
|
|
||||||
|
- name: Setup Node.js
|
||||||
|
uses: actions/setup-node@v4
|
||||||
|
with:
|
||||||
|
node-version: '20'
|
||||||
|
cache: 'npm'
|
||||||
|
cache-dependency-path: web/package-lock.json
|
||||||
|
|
||||||
|
- name: Create .env for stack
|
||||||
|
run: |
|
||||||
|
cat > .env <<'EOF'
|
||||||
|
POSTGRES_PASSWORD=ci-postgres-pass
|
||||||
|
BRAIN_INTERNAL_SECRET=ci-brain-secret
|
||||||
|
ADMIN_USERNAME=admin
|
||||||
|
ADMIN_PASSWORD=ci-admin-pass
|
||||||
|
WEB_PORT=8081
|
||||||
|
LLM_PROVIDER=ollama
|
||||||
|
EOF
|
||||||
|
|
||||||
|
- name: Build & start stack
|
||||||
|
run: |
|
||||||
|
docker compose -f docker-compose.yml -f docker-compose.e2e.yml up -d --build
|
||||||
|
|
||||||
|
- name: Attach runner to compose network
|
||||||
|
run: |
|
||||||
|
NET=$(docker network ls --format '{{.Name}}' | grep -E '(^|_)loremind(_|$)' | grep -i default | head -1)
|
||||||
|
if [ -z "$NET" ]; then
|
||||||
|
echo "Compose network not found" >&2
|
||||||
|
docker network ls
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
echo "Connecting $(hostname) to network $NET"
|
||||||
|
docker network connect "$NET" "$(hostname)"
|
||||||
|
|
||||||
|
- name: Wait for web to be ready
|
||||||
|
run: |
|
||||||
|
timeout 180 bash -c 'until curl -sf http://web/ > /dev/null; do echo "waiting..."; sleep 3; done'
|
||||||
|
|
||||||
|
- name: Install web deps
|
||||||
|
working-directory: web
|
||||||
|
run: npm ci
|
||||||
|
|
||||||
|
- name: Work around runner clock skew for apt
|
||||||
|
run: |
|
||||||
|
sudo tee /etc/apt/apt.conf.d/99no-check-valid-until >/dev/null <<'EOF'
|
||||||
|
Acquire::Check-Valid-Until "false";
|
||||||
|
Acquire::Check-Date "false";
|
||||||
|
EOF
|
||||||
|
|
||||||
|
- name: Install Playwright browsers
|
||||||
|
working-directory: web
|
||||||
|
run: npx playwright install --with-deps chromium
|
||||||
|
|
||||||
|
- name: Run Playwright tests
|
||||||
|
working-directory: web
|
||||||
|
env:
|
||||||
|
E2E_BASE_URL: http://web
|
||||||
|
CI: 'true'
|
||||||
|
run: npm run e2e
|
||||||
|
|
||||||
|
- name: Dump container logs on failure
|
||||||
|
if: failure()
|
||||||
|
run: docker compose -f docker-compose.yml -f docker-compose.e2e.yml logs --no-color
|
||||||
|
|
||||||
|
- name: Upload Playwright report
|
||||||
|
if: always()
|
||||||
|
uses: actions/upload-artifact@v4
|
||||||
|
with:
|
||||||
|
name: playwright-report
|
||||||
|
path: web/playwright-report/
|
||||||
|
retention-days: 14
|
||||||
|
|
||||||
|
- name: Stop stack
|
||||||
|
if: always()
|
||||||
|
run: docker compose -f docker-compose.yml -f docker-compose.e2e.yml down -v
|
||||||
6
.gitignore
vendored
6
.gitignore
vendored
@@ -53,6 +53,12 @@ yarn-error.log*
|
|||||||
.pnpm-debug.log*
|
.pnpm-debug.log*
|
||||||
coverage/
|
coverage/
|
||||||
|
|
||||||
|
# Playwright (E2E)
|
||||||
|
web/test-results/
|
||||||
|
web/playwright-report/
|
||||||
|
web/blob-report/
|
||||||
|
web/playwright/.cache/
|
||||||
|
|
||||||
# ============================================================================
|
# ============================================================================
|
||||||
# IDE / Editeurs
|
# IDE / Editeurs
|
||||||
# ============================================================================
|
# ============================================================================
|
||||||
|
|||||||
@@ -20,6 +20,8 @@ from app.domain.models import (
|
|||||||
CampaignStructuralContext,
|
CampaignStructuralContext,
|
||||||
ChatMessage,
|
ChatMessage,
|
||||||
ChapterSummary,
|
ChapterSummary,
|
||||||
|
CharacterSummary,
|
||||||
|
GameSystemContext,
|
||||||
LoreStructuralContext,
|
LoreStructuralContext,
|
||||||
NarrativeEntityContext,
|
NarrativeEntityContext,
|
||||||
PageContext,
|
PageContext,
|
||||||
@@ -63,16 +65,17 @@ class ChatUseCase:
|
|||||||
page_context: PageContext | None = None,
|
page_context: PageContext | None = None,
|
||||||
campaign_context: CampaignStructuralContext | None = None,
|
campaign_context: CampaignStructuralContext | None = None,
|
||||||
narrative_entity: NarrativeEntityContext | None = None,
|
narrative_entity: NarrativeEntityContext | None = None,
|
||||||
|
game_system_context: GameSystemContext | None = None,
|
||||||
) -> AsyncIterator[str]:
|
) -> AsyncIterator[str]:
|
||||||
"""Streame les tokens de la réponse assistant pour le dernier message user.
|
"""Streame les tokens de la réponse assistant pour le dernier message user.
|
||||||
|
|
||||||
Les 4 contextes sont tous optionnels, mais au moins l'un des deux
|
Les contextes sont tous optionnels, mais au moins l'un des deux
|
||||||
"niveaux haut" (lore_context ou campaign_context) doit être fourni
|
"niveaux haut" (lore_context ou campaign_context) doit être fourni
|
||||||
pour que le prompt ait du sens. Le controller (main.py) applique
|
pour que le prompt ait du sens. Le controller (main.py) applique
|
||||||
cette règle à la frontière HTTP.
|
cette règle à la frontière HTTP.
|
||||||
"""
|
"""
|
||||||
system_prompt = self._build_system_prompt(
|
system_prompt = self._build_system_prompt(
|
||||||
lore_context, page_context, campaign_context, narrative_entity
|
lore_context, page_context, campaign_context, narrative_entity, game_system_context
|
||||||
)
|
)
|
||||||
async for token in self._llm.stream_chat(
|
async for token in self._llm.stream_chat(
|
||||||
messages,
|
messages,
|
||||||
@@ -81,6 +84,21 @@ class ChatUseCase:
|
|||||||
):
|
):
|
||||||
yield token
|
yield token
|
||||||
|
|
||||||
|
def build_system_prompt(
|
||||||
|
self,
|
||||||
|
lore_context: LoreStructuralContext | None = None,
|
||||||
|
page_context: PageContext | None = None,
|
||||||
|
campaign_context: CampaignStructuralContext | None = None,
|
||||||
|
narrative_entity: NarrativeEntityContext | None = None,
|
||||||
|
game_system_context: GameSystemContext | None = None,
|
||||||
|
) -> str:
|
||||||
|
"""Version publique — utilisée par le controller HTTP pour compter
|
||||||
|
les tokens du system prompt avant de streamer (jauge de contexte).
|
||||||
|
"""
|
||||||
|
return self._build_system_prompt(
|
||||||
|
lore_context, page_context, campaign_context, narrative_entity, game_system_context
|
||||||
|
)
|
||||||
|
|
||||||
# --- Construction du system prompt --------------------------------------
|
# --- Construction du system prompt --------------------------------------
|
||||||
|
|
||||||
def _build_system_prompt(
|
def _build_system_prompt(
|
||||||
@@ -89,12 +107,15 @@ class ChatUseCase:
|
|||||||
page: PageContext | None,
|
page: PageContext | None,
|
||||||
campaign: CampaignStructuralContext | None,
|
campaign: CampaignStructuralContext | None,
|
||||||
narrative: NarrativeEntityContext | None,
|
narrative: NarrativeEntityContext | None,
|
||||||
|
game_system: GameSystemContext | None = None,
|
||||||
) -> str:
|
) -> str:
|
||||||
sections = [_BASE_SYSTEM]
|
sections = [_BASE_SYSTEM]
|
||||||
if lore is not None:
|
if lore is not None:
|
||||||
sections.append(self._format_lore(lore))
|
sections.append(self._format_lore(lore))
|
||||||
if campaign is not None:
|
if campaign is not None:
|
||||||
sections.append(self._format_campaign(campaign, lore_present=lore is not None))
|
sections.append(self._format_campaign(campaign, lore_present=lore is not None))
|
||||||
|
if game_system is not None:
|
||||||
|
sections.append(self._format_game_system(game_system))
|
||||||
if page is not None:
|
if page is not None:
|
||||||
sections.append(self._format_page(page))
|
sections.append(self._format_page(page))
|
||||||
if narrative is not None:
|
if narrative is not None:
|
||||||
@@ -176,14 +197,40 @@ class ChatUseCase:
|
|||||||
if lore_present
|
if lore_present
|
||||||
else "\n(Cette campagne n'est associée à aucun univers — tu peux proposer des éléments d'ambiance libres.)"
|
else "\n(Cette campagne n'est associée à aucun univers — tu peux proposer des éléments d'ambiance libres.)"
|
||||||
)
|
)
|
||||||
|
characters_block = ChatUseCase._format_characters(ctx.characters)
|
||||||
return (
|
return (
|
||||||
"--- CAMPAGNE COURANTE ---\n"
|
"--- CAMPAGNE COURANTE ---\n"
|
||||||
f"Nom : {ctx.campaign_name}{desc}{lore_note}\n\n"
|
f"Nom : {ctx.campaign_name}{desc}{lore_note}\n"
|
||||||
|
f"{characters_block}\n"
|
||||||
"Structure narrative (les flèches → indiquent des transitions de scène "
|
"Structure narrative (les flèches → indiquent des transitions de scène "
|
||||||
"déclenchées par un choix des joueurs) :\n"
|
"déclenchées par un choix des joueurs) :\n"
|
||||||
f"{arcs_block}"
|
f"{arcs_block}"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _format_characters(characters: list[CharacterSummary]) -> str:
|
||||||
|
"""Bloc PJ — liste nom + snippet. Rappel anti-hallucination IA.
|
||||||
|
|
||||||
|
Si la campagne n'a aucun PJ, on le signale explicitement : l'IA ne
|
||||||
|
doit pas inventer "les héros" ou leurs noms dans ses suggestions.
|
||||||
|
"""
|
||||||
|
if not characters:
|
||||||
|
return (
|
||||||
|
"\nPersonnages joueurs : aucune fiche pour l'instant. Ne suppose "
|
||||||
|
"ni noms ni classes pour les PJ tant que le MJ ne les a pas créés.\n"
|
||||||
|
)
|
||||||
|
lines = ["\nPersonnages joueurs (PJ) :"]
|
||||||
|
for c in characters:
|
||||||
|
if c.snippet:
|
||||||
|
lines.append(f"- **{c.name}** — {c.snippet}")
|
||||||
|
else:
|
||||||
|
lines.append(f"- **{c.name}** (fiche vide)")
|
||||||
|
lines.append(
|
||||||
|
"Pour une fiche complète (stats, backstory), n'invente rien : "
|
||||||
|
"demande au MJ d'ouvrir l'éditeur du PJ pour te donner les détails."
|
||||||
|
)
|
||||||
|
return "\n".join(lines) + "\n"
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _format_arcs(arcs: list[ArcSummary]) -> str:
|
def _format_arcs(arcs: list[ArcSummary]) -> str:
|
||||||
if not arcs:
|
if not arcs:
|
||||||
@@ -234,12 +281,46 @@ class ChatUseCase:
|
|||||||
noun = "illustration" if count == 1 else "illustrations"
|
noun = "illustration" if count == 1 else "illustrations"
|
||||||
return f" [{count} {noun}]"
|
return f" [{count} {noun}]"
|
||||||
|
|
||||||
|
# --- Bloc Système de JDR ------------------------------------------------
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _format_game_system(gs: GameSystemContext) -> str:
|
||||||
|
"""Bloc des règles du système de JDR de la campagne.
|
||||||
|
|
||||||
|
Les sections ont été filtrées côté Core selon l'intent (combat,
|
||||||
|
classes, lore...). Si aucune section n'a matché, on affiche juste
|
||||||
|
le nom du système comme rappel de cadre.
|
||||||
|
"""
|
||||||
|
desc = f"\nDescription : {gs.system_description}" if gs.system_description else ""
|
||||||
|
if not gs.sections:
|
||||||
|
return (
|
||||||
|
"--- SYSTÈME DE JDR ---\n"
|
||||||
|
f"Nom : {gs.system_name}{desc}\n"
|
||||||
|
"(Aucune section de règles pertinente pour ce type de génération — "
|
||||||
|
"reste cohérent avec l'univers et les conventions du système.)"
|
||||||
|
)
|
||||||
|
sections_block = "\n\n".join(
|
||||||
|
f"### {title}\n{content}" for title, content in gs.sections.items()
|
||||||
|
)
|
||||||
|
return (
|
||||||
|
"--- SYSTÈME DE JDR ---\n"
|
||||||
|
f"Nom : {gs.system_name}{desc}\n\n"
|
||||||
|
"Respecte scrupuleusement les règles et conventions ci-dessous quand "
|
||||||
|
"tu proposes des stats, classes, rencontres, mécaniques ou éléments "
|
||||||
|
"d'ambiance. Les noms propres (classes, sorts, monstres) doivent "
|
||||||
|
"venir de ces règles — n'en invente pas d'autres.\n\n"
|
||||||
|
f"{sections_block}"
|
||||||
|
)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _format_narrative_entity(ne: NarrativeEntityContext) -> str:
|
def _format_narrative_entity(ne: NarrativeEntityContext) -> str:
|
||||||
"""Bloc équivalent à _format_page mais pour Arc/Chapter/Scene."""
|
"""Bloc équivalent à _format_page mais pour Arc/Chapter/Scene."""
|
||||||
type_label = {"arc": "ARC", "chapter": "CHAPITRE", "scene": "SCÈNE"}.get(
|
type_label = {
|
||||||
ne.entity_type.lower(), ne.entity_type.upper()
|
"arc": "ARC",
|
||||||
)
|
"chapter": "CHAPITRE",
|
||||||
|
"scene": "SCÈNE",
|
||||||
|
"character": "FICHE DE PERSONNAGE",
|
||||||
|
}.get(ne.entity_type.lower(), ne.entity_type.upper())
|
||||||
if ne.fields:
|
if ne.fields:
|
||||||
fields_block = "\n".join(
|
fields_block = "\n".join(
|
||||||
f'- "{key}" : {value or "(vide)"}'
|
f'- "{key}" : {value or "(vide)"}'
|
||||||
|
|||||||
@@ -169,6 +169,20 @@ class CampaignStructuralContext:
|
|||||||
campaign_name: str
|
campaign_name: str
|
||||||
campaign_description: str | None
|
campaign_description: str | None
|
||||||
arcs: list[ArcSummary]
|
arcs: list[ArcSummary]
|
||||||
|
characters: list["CharacterSummary"] = field(default_factory=list)
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class CharacterSummary:
|
||||||
|
"""Résumé d'un PJ : nom + snippet court extrait du markdown de la fiche.
|
||||||
|
|
||||||
|
La fiche complète n'est JAMAIS dans ce résumé — elle n'arrive que si le PJ
|
||||||
|
est l'entité focus (via NarrativeEntityContext entity_type="character").
|
||||||
|
Ça plafonne le coût token à ~40 tokens/PJ quel que soit le détail des fiches.
|
||||||
|
"""
|
||||||
|
|
||||||
|
name: str
|
||||||
|
snippet: str
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
@dataclass(frozen=True)
|
||||||
@@ -184,3 +198,20 @@ class NarrativeEntityContext:
|
|||||||
entity_type: str
|
entity_type: str
|
||||||
title: str
|
title: str
|
||||||
fields: dict[str, str]
|
fields: dict[str, str]
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class GameSystemContext:
|
||||||
|
"""Règles d'un système de JDR (D&D, Nimble, homebrew...) injectées
|
||||||
|
dans le system prompt pour que l'IA respecte les mécaniques du jeu.
|
||||||
|
|
||||||
|
Les sections ont été présélectionnées côté Core selon l'intent
|
||||||
|
(SCENE → combat/PNJ, CHAPTER → combat/classes, ARC → lore/factions,
|
||||||
|
GENERIC → toutes). Indexées par titre H2 original.
|
||||||
|
|
||||||
|
Campagne uniquement au MVP : jamais présent sur un chat Lore.
|
||||||
|
"""
|
||||||
|
|
||||||
|
system_name: str
|
||||||
|
system_description: str | None
|
||||||
|
sections: dict[str, str]
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ from typing import Annotated, AsyncIterator, Literal
|
|||||||
|
|
||||||
import hmac
|
import hmac
|
||||||
import httpx
|
import httpx
|
||||||
|
import tiktoken
|
||||||
from fastapi import Depends, FastAPI, HTTPException, Request
|
from fastapi import Depends, FastAPI, HTTPException, Request
|
||||||
from fastapi.responses import JSONResponse, StreamingResponse
|
from fastapi.responses import JSONResponse, StreamingResponse
|
||||||
from pydantic import BaseModel, Field
|
from pydantic import BaseModel, Field
|
||||||
@@ -21,7 +22,9 @@ from app.domain.models import (
|
|||||||
ArcSummary,
|
ArcSummary,
|
||||||
CampaignStructuralContext,
|
CampaignStructuralContext,
|
||||||
ChapterSummary,
|
ChapterSummary,
|
||||||
|
CharacterSummary,
|
||||||
ChatMessage,
|
ChatMessage,
|
||||||
|
GameSystemContext,
|
||||||
LoreStructuralContext,
|
LoreStructuralContext,
|
||||||
NarrativeEntityContext,
|
NarrativeEntityContext,
|
||||||
PageContext,
|
PageContext,
|
||||||
@@ -37,10 +40,27 @@ from app.infrastructure.onemin_adapter import OneMinAiLLMProvider
|
|||||||
app = FastAPI(
|
app = FastAPI(
|
||||||
title="LoreMind Brain",
|
title="LoreMind Brain",
|
||||||
description="Backend IA pour la génération de contenu narratif.",
|
description="Backend IA pour la génération de contenu narratif.",
|
||||||
version="0.3.0",
|
version="0.6.6",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# Encodeur tiktoken partagé — chargé une fois pour éviter le coût de lookup
|
||||||
|
# à chaque requête. On utilise cl100k_base (GPT-3.5/4) comme tokenizer
|
||||||
|
# universel approximatif : ±10% d'écart avec Llama/Gemma mais largement
|
||||||
|
# suffisant pour une jauge visuelle à l'utilisateur.
|
||||||
|
_TOKEN_ENCODER: tiktoken.Encoding | None = None
|
||||||
|
|
||||||
|
|
||||||
|
def _count_tokens(text: str | None) -> int:
|
||||||
|
"""Compte les tokens d'un texte via tiktoken. Null/empty → 0."""
|
||||||
|
if not text:
|
||||||
|
return 0
|
||||||
|
global _TOKEN_ENCODER
|
||||||
|
if _TOKEN_ENCODER is None:
|
||||||
|
_TOKEN_ENCODER = tiktoken.get_encoding("cl100k_base")
|
||||||
|
return len(_TOKEN_ENCODER.encode(text))
|
||||||
|
|
||||||
|
|
||||||
# Chemins exemptes d'auth inter-service : healthcheck docker + introspection
|
# Chemins exemptes d'auth inter-service : healthcheck docker + introspection
|
||||||
# FastAPI (docs uniquement utiles en dev ; en prod docker-compose, le Brain
|
# FastAPI (docs uniquement utiles en dev ; en prod docker-compose, le Brain
|
||||||
# n'est pas expose en dehors du reseau interne donc pas un risque).
|
# n'est pas expose en dehors du reseau interne donc pas un risque).
|
||||||
@@ -178,22 +198,42 @@ class ArcSummaryDTO(BaseModel):
|
|||||||
illustration_count: int = 0
|
illustration_count: int = 0
|
||||||
|
|
||||||
|
|
||||||
|
class CharacterSummaryDTO(BaseModel):
|
||||||
|
"""Résumé d'un PJ : nom + snippet. Pas de fiche complète au niveau résumé."""
|
||||||
|
|
||||||
|
name: str
|
||||||
|
snippet: str = ""
|
||||||
|
|
||||||
|
|
||||||
class CampaignContextDTO(BaseModel):
|
class CampaignContextDTO(BaseModel):
|
||||||
"""Carte narrative enrichie : arcs → chapitres → scènes avec synopsis."""
|
"""Carte narrative enrichie : arcs → chapitres → scènes avec synopsis."""
|
||||||
|
|
||||||
campaign_name: str
|
campaign_name: str
|
||||||
campaign_description: str | None = None
|
campaign_description: str | None = None
|
||||||
arcs: list[ArcSummaryDTO] = Field(default_factory=list)
|
arcs: list[ArcSummaryDTO] = Field(default_factory=list)
|
||||||
|
characters: list[CharacterSummaryDTO] = Field(default_factory=list)
|
||||||
|
|
||||||
|
|
||||||
class NarrativeEntityDTO(BaseModel):
|
class NarrativeEntityDTO(BaseModel):
|
||||||
"""Entité narrative (arc/chapter/scene) en cours d'édition — focus optionnel."""
|
"""Entité narrative (arc/chapter/scene/character) en cours d'édition — focus optionnel."""
|
||||||
|
|
||||||
entity_type: str = Field(pattern="^(arc|chapter|scene)$")
|
entity_type: str = Field(pattern="^(arc|chapter|scene|character)$")
|
||||||
title: str
|
title: str
|
||||||
fields: dict[str, str] = Field(default_factory=dict)
|
fields: dict[str, str] = Field(default_factory=dict)
|
||||||
|
|
||||||
|
|
||||||
|
class GameSystemContextDTO(BaseModel):
|
||||||
|
"""Règles de JDR présélectionnées par le Core (filtrées par intent).
|
||||||
|
|
||||||
|
Les sections sont un dict titre_H2 → contenu_markdown. Peuvent être
|
||||||
|
vides si aucune section ne matchait l'intent de génération courant.
|
||||||
|
"""
|
||||||
|
|
||||||
|
system_name: str
|
||||||
|
system_description: str | None = None
|
||||||
|
sections: dict[str, str] = Field(default_factory=dict)
|
||||||
|
|
||||||
|
|
||||||
class ChatStreamRequestDTO(BaseModel):
|
class ChatStreamRequestDTO(BaseModel):
|
||||||
"""Requête de chat streamé : historique + contextes structurels.
|
"""Requête de chat streamé : historique + contextes structurels.
|
||||||
|
|
||||||
@@ -208,6 +248,7 @@ class ChatStreamRequestDTO(BaseModel):
|
|||||||
page_context: PageContextDTO | None = None
|
page_context: PageContextDTO | None = None
|
||||||
campaign_context: CampaignContextDTO | None = None
|
campaign_context: CampaignContextDTO | None = None
|
||||||
narrative_entity: NarrativeEntityDTO | None = None
|
narrative_entity: NarrativeEntityDTO | None = None
|
||||||
|
game_system_context: GameSystemContextDTO | None = None
|
||||||
|
|
||||||
def has_scope(self) -> bool:
|
def has_scope(self) -> bool:
|
||||||
"""Vrai si au moins un contexte racine (Lore ou Campagne) est fourni."""
|
"""Vrai si au moins un contexte racine (Lore ou Campagne) est fourni."""
|
||||||
@@ -334,8 +375,35 @@ async def chat_stream(
|
|||||||
page_context = _to_page_context(body.page_context)
|
page_context = _to_page_context(body.page_context)
|
||||||
campaign_context = _to_campaign_context(body.campaign_context)
|
campaign_context = _to_campaign_context(body.campaign_context)
|
||||||
narrative_entity = _to_narrative_entity(body.narrative_entity)
|
narrative_entity = _to_narrative_entity(body.narrative_entity)
|
||||||
|
game_system_context = _to_game_system_context(body.game_system_context)
|
||||||
|
|
||||||
|
# --- Comptage tokens pour la jauge de contexte frontend ---
|
||||||
|
# On construit le system prompt une fois ici pour le compter — le use case
|
||||||
|
# le reconstruira à l'identique en interne (coût négligeable : concat de str).
|
||||||
|
# Cette duplication évite de complexifier le contrat stream() avec un
|
||||||
|
# paramètre optionnel system_prompt précalculé.
|
||||||
|
system_prompt_preview = use_case.build_system_prompt(
|
||||||
|
lore_context=lore_context,
|
||||||
|
page_context=page_context,
|
||||||
|
campaign_context=campaign_context,
|
||||||
|
narrative_entity=narrative_entity,
|
||||||
|
game_system_context=game_system_context,
|
||||||
|
)
|
||||||
|
# Dernier message = "current" (souvent user), le reste = historique accumulé.
|
||||||
|
current_msg = messages[-1] if messages else None
|
||||||
|
history_msgs = messages[:-1] if messages else []
|
||||||
|
settings = get_settings()
|
||||||
|
usage_payload = {
|
||||||
|
"system": _count_tokens(system_prompt_preview),
|
||||||
|
"history": sum(_count_tokens(m.content) for m in history_msgs),
|
||||||
|
"current": _count_tokens(current_msg.content) if current_msg else 0,
|
||||||
|
"max": settings.llm_num_ctx,
|
||||||
|
}
|
||||||
|
|
||||||
async def event_stream() -> AsyncIterator[str]:
|
async def event_stream() -> AsyncIterator[str]:
|
||||||
|
# Event 'usage' émis en tout premier : le frontend peut afficher la
|
||||||
|
# jauge avant même le premier token de réponse.
|
||||||
|
yield f"event: usage\ndata: {json.dumps(usage_payload, ensure_ascii=False)}\n\n"
|
||||||
try:
|
try:
|
||||||
async for token in use_case.stream(
|
async for token in use_case.stream(
|
||||||
messages,
|
messages,
|
||||||
@@ -343,6 +411,7 @@ async def chat_stream(
|
|||||||
page_context=page_context,
|
page_context=page_context,
|
||||||
campaign_context=campaign_context,
|
campaign_context=campaign_context,
|
||||||
narrative_entity=narrative_entity,
|
narrative_entity=narrative_entity,
|
||||||
|
game_system_context=game_system_context,
|
||||||
):
|
):
|
||||||
# json.dumps avec ensure_ascii=False pour préserver les accents
|
# json.dumps avec ensure_ascii=False pour préserver les accents
|
||||||
yield f"data: {json.dumps({'token': token}, ensure_ascii=False)}\n\n"
|
yield f"data: {json.dumps({'token': token}, ensure_ascii=False)}\n\n"
|
||||||
@@ -353,6 +422,60 @@ async def chat_stream(
|
|||||||
return StreamingResponse(event_stream(), media_type="text/event-stream")
|
return StreamingResponse(event_stream(), media_type="text/event-stream")
|
||||||
|
|
||||||
|
|
||||||
|
# --- Auto-titre d'une conversation persistee --------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
class SummarizeTitleMessageDTO(BaseModel):
|
||||||
|
role: Literal["user", "assistant", "system"]
|
||||||
|
content: str
|
||||||
|
|
||||||
|
|
||||||
|
class SummarizeTitleRequestDTO(BaseModel):
|
||||||
|
"""Premiers messages d'une conversation pour auto-generer un titre court."""
|
||||||
|
|
||||||
|
messages: list[SummarizeTitleMessageDTO] = Field(default_factory=list)
|
||||||
|
|
||||||
|
|
||||||
|
class SummarizeTitleResponseDTO(BaseModel):
|
||||||
|
title: str
|
||||||
|
|
||||||
|
|
||||||
|
_TITLE_SYSTEM_PROMPT = (
|
||||||
|
"Tu generes un titre court (4 a 7 mots max) qui resume le sujet de la "
|
||||||
|
"conversation ci-dessous. Reponds UNIQUEMENT par le titre, sans guillemets, "
|
||||||
|
"sans ponctuation finale, sans prefixe type 'Titre :'. Le titre doit etre "
|
||||||
|
"en francais et capturer le sujet metier (pas 'Conversation IA')."
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@app.post("/summarize/conversation-title", response_model=SummarizeTitleResponseDTO)
|
||||||
|
async def summarize_conversation_title(
|
||||||
|
body: SummarizeTitleRequestDTO,
|
||||||
|
llm: Annotated[LLMProvider, Depends(get_llm_provider)],
|
||||||
|
) -> SummarizeTitleResponseDTO:
|
||||||
|
"""Genere un titre court a partir des premiers echanges de la conversation.
|
||||||
|
|
||||||
|
Appele par le core apres le 1er couple user/assistant, pour remplacer le
|
||||||
|
titre provisoire "Nouvelle conversation" par quelque chose de parlant.
|
||||||
|
"""
|
||||||
|
if not body.messages:
|
||||||
|
raise HTTPException(status_code=422, detail="Au moins un message requis")
|
||||||
|
|
||||||
|
transcript = "\n".join(f"{m.role.upper()}: {m.content}" for m in body.messages[:6])
|
||||||
|
prompt = f"{_TITLE_SYSTEM_PROMPT}\n\nConversation :\n{transcript}\n\nTitre :"
|
||||||
|
try:
|
||||||
|
raw = await llm.generate(prompt)
|
||||||
|
except LLMProviderError as exc:
|
||||||
|
raise HTTPException(status_code=502, detail=str(exc)) from exc
|
||||||
|
|
||||||
|
title = raw.strip().splitlines()[0].strip().strip('"').strip("'").rstrip(".")
|
||||||
|
if len(title) > 80:
|
||||||
|
title = title[:80].rstrip()
|
||||||
|
if not title:
|
||||||
|
title = "Nouvelle conversation"
|
||||||
|
return SummarizeTitleResponseDTO(title=title)
|
||||||
|
|
||||||
|
|
||||||
# --- Mapping DTO → domaine (frontière HTTP) ---------------------------------
|
# --- Mapping DTO → domaine (frontière HTTP) ---------------------------------
|
||||||
|
|
||||||
|
|
||||||
@@ -426,10 +549,15 @@ def _to_campaign_context(dto: CampaignContextDTO | None) -> CampaignStructuralCo
|
|||||||
)
|
)
|
||||||
for arc in dto.arcs
|
for arc in dto.arcs
|
||||||
]
|
]
|
||||||
|
characters = [
|
||||||
|
CharacterSummary(name=c.name, snippet=c.snippet)
|
||||||
|
for c in dto.characters
|
||||||
|
]
|
||||||
return CampaignStructuralContext(
|
return CampaignStructuralContext(
|
||||||
campaign_name=dto.campaign_name,
|
campaign_name=dto.campaign_name,
|
||||||
campaign_description=dto.campaign_description,
|
campaign_description=dto.campaign_description,
|
||||||
arcs=arcs,
|
arcs=arcs,
|
||||||
|
characters=characters,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -449,6 +577,9 @@ class SettingsDTO(BaseModel):
|
|||||||
onemin_model: str
|
onemin_model: str
|
||||||
# True si une cle 1min.ai est deja configuree — pas de leak de la cle elle-meme.
|
# True si une cle 1min.ai est deja configuree — pas de leak de la cle elle-meme.
|
||||||
onemin_api_key_set: bool
|
onemin_api_key_set: bool
|
||||||
|
# Fenetre de contexte effective passee au modele (num_ctx Ollama) — sert
|
||||||
|
# aussi de plafond a la jauge de contexte UI.
|
||||||
|
llm_num_ctx: int
|
||||||
|
|
||||||
|
|
||||||
class SettingsUpdateDTO(BaseModel):
|
class SettingsUpdateDTO(BaseModel):
|
||||||
@@ -460,6 +591,7 @@ class SettingsUpdateDTO(BaseModel):
|
|||||||
onemin_model: str | None = None
|
onemin_model: str | None = None
|
||||||
# Chaine vide => on efface la cle. None => pas de changement.
|
# Chaine vide => on efface la cle. None => pas de changement.
|
||||||
onemin_api_key: str | None = None
|
onemin_api_key: str | None = None
|
||||||
|
llm_num_ctx: int | None = None
|
||||||
|
|
||||||
|
|
||||||
def _to_settings_dto(s: Settings) -> SettingsDTO:
|
def _to_settings_dto(s: Settings) -> SettingsDTO:
|
||||||
@@ -469,6 +601,7 @@ def _to_settings_dto(s: Settings) -> SettingsDTO:
|
|||||||
llm_model=s.llm_model,
|
llm_model=s.llm_model,
|
||||||
onemin_model=s.onemin_model,
|
onemin_model=s.onemin_model,
|
||||||
onemin_api_key_set=bool(s.onemin_api_key),
|
onemin_api_key_set=bool(s.onemin_api_key),
|
||||||
|
llm_num_ctx=s.llm_num_ctx,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -512,6 +645,50 @@ async def list_ollama_models(
|
|||||||
return {"models": sorted(models)}
|
return {"models": sorted(models)}
|
||||||
|
|
||||||
|
|
||||||
|
class OllamaModelInfoDTO(BaseModel):
|
||||||
|
"""Info utile extraite de /api/show pour un modele Ollama donne.
|
||||||
|
|
||||||
|
`context_length` = fenetre de contexte max supportee par le modele
|
||||||
|
(extraite des metadonnees GGUF). 0 si inconnue. Le frontend s'en sert
|
||||||
|
pour borner le slider de num_ctx dans les Parametres.
|
||||||
|
"""
|
||||||
|
|
||||||
|
context_length: int = 0
|
||||||
|
|
||||||
|
|
||||||
|
@app.post("/models/ollama/info", response_model=OllamaModelInfoDTO)
|
||||||
|
async def get_ollama_model_info(
|
||||||
|
body: dict[str, str],
|
||||||
|
settings: Annotated[Settings, Depends(get_settings)],
|
||||||
|
) -> OllamaModelInfoDTO:
|
||||||
|
"""Retourne les metadonnees d'un modele Ollama via /api/show.
|
||||||
|
|
||||||
|
On passe par POST (et pas GET /models/ollama/{name}) parce que les noms
|
||||||
|
Ollama contiennent souvent un `:` (ex: `gemma3:e2b`) qui se segmente
|
||||||
|
mal dans une URL — le body JSON evite le probleme d'escaping.
|
||||||
|
|
||||||
|
Le champ qui nous interesse est `model_info["<arch>.context_length"]`
|
||||||
|
(ex: `gemma3.context_length: 131072`). L'arch varie selon le modele, on
|
||||||
|
scanne donc tous les champs finissant par `.context_length`.
|
||||||
|
"""
|
||||||
|
name = (body.get("name") or "").strip()
|
||||||
|
if not name:
|
||||||
|
raise HTTPException(status_code=400, detail="name requis")
|
||||||
|
url = f"{settings.ollama_base_url}/api/show"
|
||||||
|
try:
|
||||||
|
async with httpx.AsyncClient(timeout=5) as client:
|
||||||
|
response = await client.post(url, json={"model": name})
|
||||||
|
response.raise_for_status()
|
||||||
|
data = response.json()
|
||||||
|
except httpx.HTTPError:
|
||||||
|
return OllamaModelInfoDTO(context_length=0)
|
||||||
|
model_info = data.get("model_info") or {}
|
||||||
|
for key, value in model_info.items():
|
||||||
|
if key.endswith(".context_length") and isinstance(value, int):
|
||||||
|
return OllamaModelInfoDTO(context_length=value)
|
||||||
|
return OllamaModelInfoDTO(context_length=0)
|
||||||
|
|
||||||
|
|
||||||
@app.get("/models/onemin")
|
@app.get("/models/onemin")
|
||||||
def list_onemin_models() -> dict[str, list[dict[str, object]]]:
|
def list_onemin_models() -> dict[str, list[dict[str, object]]]:
|
||||||
"""Catalogue statique des modeles 1min.ai, groupes par fournisseur.
|
"""Catalogue statique des modeles 1min.ai, groupes par fournisseur.
|
||||||
@@ -596,3 +773,13 @@ def _to_narrative_entity(dto: NarrativeEntityDTO | None) -> NarrativeEntityConte
|
|||||||
title=dto.title,
|
title=dto.title,
|
||||||
fields=dict(dto.fields),
|
fields=dict(dto.fields),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _to_game_system_context(dto: GameSystemContextDTO | None) -> GameSystemContext | None:
|
||||||
|
if dto is None:
|
||||||
|
return None
|
||||||
|
return GameSystemContext(
|
||||||
|
system_name=dto.system_name,
|
||||||
|
system_description=dto.system_description,
|
||||||
|
sections=dict(dto.sections),
|
||||||
|
)
|
||||||
|
|||||||
@@ -4,3 +4,9 @@ httpx==0.27.*
|
|||||||
pydantic-settings==2.6.*
|
pydantic-settings==2.6.*
|
||||||
|
|
||||||
pydantic
|
pydantic
|
||||||
|
|
||||||
|
# Comptage de tokens pour la jauge de contexte (UI chat drawer).
|
||||||
|
# L'encodage cl100k_base (GPT-4/3.5) donne une approximation correcte pour
|
||||||
|
# la plupart des modeles Llama/Gemma/Mistral (~5-10% d'ecart) — suffisant
|
||||||
|
# pour une jauge visuelle.
|
||||||
|
tiktoken==0.8.*
|
||||||
|
|||||||
11
core/lombok.config
Normal file
11
core/lombok.config
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
## LoreMind Core - Configuration Lombok
|
||||||
|
#
|
||||||
|
# addLombokGeneratedAnnotation : ajoute @lombok.Generated sur toutes les
|
||||||
|
# methodes generees par Lombok (equals, hashCode, toString, builders,
|
||||||
|
# getters/setters, etc.). JaCoCo 0.8.2+ reconnait cette annotation et
|
||||||
|
# exclut automatiquement ces methodes du rapport de couverture.
|
||||||
|
#
|
||||||
|
# Objectif : mesurer la couverture UNIQUEMENT sur le code que nous ecrivons,
|
||||||
|
# pas sur le bytecode auto-genere (qui fausse les metriques : branches et
|
||||||
|
# instructions gonflees par les equals/hashCode).
|
||||||
|
lombok.addLombokGeneratedAnnotation = true
|
||||||
@@ -14,7 +14,7 @@
|
|||||||
|
|
||||||
<groupId>com.loremind</groupId>
|
<groupId>com.loremind</groupId>
|
||||||
<artifactId>loremind-core</artifactId>
|
<artifactId>loremind-core</artifactId>
|
||||||
<version>0.3.0</version>
|
<version>0.6.6</version>
|
||||||
<name>LoreMind Core</name>
|
<name>LoreMind Core</name>
|
||||||
<description>Backend Core - Architecture Hexagonale</description>
|
<description>Backend Core - Architecture Hexagonale</description>
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,13 @@
|
|||||||
package com.loremind.application.campaigncontext;
|
package com.loremind.application.campaigncontext;
|
||||||
|
|
||||||
import com.loremind.domain.campaigncontext.Arc;
|
import com.loremind.domain.campaigncontext.Arc;
|
||||||
|
import com.loremind.domain.campaigncontext.Chapter;
|
||||||
import com.loremind.domain.campaigncontext.ports.ArcRepository;
|
import com.loremind.domain.campaigncontext.ports.ArcRepository;
|
||||||
|
import com.loremind.domain.campaigncontext.ports.ChapterRepository;
|
||||||
|
import com.loremind.domain.campaigncontext.ports.SceneRepository;
|
||||||
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.BeanUtils;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
@@ -17,17 +21,31 @@ import java.util.Optional;
|
|||||||
public class ArcService {
|
public class ArcService {
|
||||||
|
|
||||||
private final ArcRepository arcRepository;
|
private final ArcRepository arcRepository;
|
||||||
|
private final ChapterRepository chapterRepository;
|
||||||
|
private final SceneRepository sceneRepository;
|
||||||
|
|
||||||
public ArcService(ArcRepository arcRepository) {
|
public ArcService(ArcRepository arcRepository,
|
||||||
|
ChapterRepository chapterRepository,
|
||||||
|
SceneRepository sceneRepository) {
|
||||||
this.arcRepository = arcRepository;
|
this.arcRepository = arcRepository;
|
||||||
|
this.chapterRepository = chapterRepository;
|
||||||
|
this.sceneRepository = sceneRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Compte des entités qui seront supprimées en cascade avec l'arc. */
|
||||||
|
public record DeletionImpact(int chapters, int scenes) {}
|
||||||
|
|
||||||
public Arc createArc(String name, String description, String campaignId, int order) {
|
public Arc createArc(String name, String description, String campaignId, int order) {
|
||||||
|
return createArc(name, description, campaignId, order, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Arc createArc(String name, String description, String campaignId, int order, String icon) {
|
||||||
Arc arc = Arc.builder()
|
Arc arc = Arc.builder()
|
||||||
.name(name)
|
.name(name)
|
||||||
.description(description)
|
.description(description)
|
||||||
.campaignId(campaignId)
|
.campaignId(campaignId)
|
||||||
.order(order)
|
.order(order)
|
||||||
|
.icon(icon)
|
||||||
.build();
|
.build();
|
||||||
return arcRepository.save(arc);
|
return arcRepository.save(arc);
|
||||||
}
|
}
|
||||||
@@ -59,7 +77,31 @@ public class ArcService {
|
|||||||
return arcRepository.save(arc);
|
return arcRepository.save(arc);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calcule l'impact d'une suppression en cascade : chapitres + scènes
|
||||||
|
* qui disparaîtront avec l'arc.
|
||||||
|
*/
|
||||||
|
public DeletionImpact getDeletionImpact(String id) {
|
||||||
|
List<Chapter> chapters = chapterRepository.findByArcId(id);
|
||||||
|
int sceneTotal = 0;
|
||||||
|
for (Chapter chapter : chapters) {
|
||||||
|
sceneTotal += sceneRepository.findByChapterId(chapter.getId()).size();
|
||||||
|
}
|
||||||
|
return new DeletionImpact(chapters.size(), sceneTotal);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Supprime l'arc et toutes ses entités dépendantes (chapitres → scènes).
|
||||||
|
* Transactionnel : atomique.
|
||||||
|
*/
|
||||||
|
@Transactional
|
||||||
public void deleteArc(String id) {
|
public void deleteArc(String id) {
|
||||||
|
for (Chapter chapter : chapterRepository.findByArcId(id)) {
|
||||||
|
for (var scene : sceneRepository.findByChapterId(chapter.getId())) {
|
||||||
|
sceneRepository.deleteById(scene.getId());
|
||||||
|
}
|
||||||
|
chapterRepository.deleteById(chapter.getId());
|
||||||
|
}
|
||||||
arcRepository.deleteById(id);
|
arcRepository.deleteById(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,15 @@
|
|||||||
package com.loremind.application.campaigncontext;
|
package com.loremind.application.campaigncontext;
|
||||||
|
|
||||||
|
import com.loremind.domain.campaigncontext.Arc;
|
||||||
import com.loremind.domain.campaigncontext.Campaign;
|
import com.loremind.domain.campaigncontext.Campaign;
|
||||||
|
import com.loremind.domain.campaigncontext.Chapter;
|
||||||
|
import com.loremind.domain.campaigncontext.ports.ArcRepository;
|
||||||
import com.loremind.domain.campaigncontext.ports.CampaignRepository;
|
import com.loremind.domain.campaigncontext.ports.CampaignRepository;
|
||||||
|
import com.loremind.domain.campaigncontext.ports.ChapterRepository;
|
||||||
|
import com.loremind.domain.campaigncontext.ports.CharacterRepository;
|
||||||
|
import com.loremind.domain.campaigncontext.ports.SceneRepository;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
@@ -16,9 +23,22 @@ import java.util.Optional;
|
|||||||
public class CampaignService {
|
public class CampaignService {
|
||||||
|
|
||||||
private final CampaignRepository campaignRepository;
|
private final CampaignRepository campaignRepository;
|
||||||
|
private final ArcRepository arcRepository;
|
||||||
|
private final ChapterRepository chapterRepository;
|
||||||
|
private final SceneRepository sceneRepository;
|
||||||
|
private final CharacterRepository characterRepository;
|
||||||
|
|
||||||
public CampaignService(CampaignRepository campaignRepository) {
|
public CampaignService(
|
||||||
|
CampaignRepository campaignRepository,
|
||||||
|
ArcRepository arcRepository,
|
||||||
|
ChapterRepository chapterRepository,
|
||||||
|
SceneRepository sceneRepository,
|
||||||
|
CharacterRepository characterRepository) {
|
||||||
this.campaignRepository = campaignRepository;
|
this.campaignRepository = campaignRepository;
|
||||||
|
this.arcRepository = arcRepository;
|
||||||
|
this.chapterRepository = chapterRepository;
|
||||||
|
this.sceneRepository = sceneRepository;
|
||||||
|
this.characterRepository = characterRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -28,13 +48,20 @@ public class CampaignService {
|
|||||||
*
|
*
|
||||||
* <p>{@code loreId} est nullable : une campagne peut exister sans univers associé.</p>
|
* <p>{@code loreId} est nullable : une campagne peut exister sans univers associé.</p>
|
||||||
*/
|
*/
|
||||||
public record CampaignData(String name, String description, String loreId) {}
|
public record CampaignData(String name, String description, String loreId, String gameSystemId) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compte des entités qui seront supprimées en cascade si la campagne est effacée.
|
||||||
|
* Utilisé par l'UI pour afficher un récapitulatif dans le dialogue de confirmation.
|
||||||
|
*/
|
||||||
|
public record DeletionImpact(int arcs, int chapters, int scenes, int characters) {}
|
||||||
|
|
||||||
public Campaign createCampaign(CampaignData data) {
|
public Campaign createCampaign(CampaignData data) {
|
||||||
Campaign campaign = Campaign.builder()
|
Campaign campaign = Campaign.builder()
|
||||||
.name(data.name())
|
.name(data.name())
|
||||||
.description(data.description())
|
.description(data.description())
|
||||||
.loreId(normalizeLoreId(data.loreId()))
|
.loreId(normalizeId(data.loreId()))
|
||||||
|
.gameSystemId(normalizeId(data.gameSystemId()))
|
||||||
.arcsCount(0)
|
.arcsCount(0)
|
||||||
.build();
|
.build();
|
||||||
return campaignRepository.save(campaign);
|
return campaignRepository.save(campaign);
|
||||||
@@ -57,19 +84,61 @@ public class CampaignService {
|
|||||||
Campaign campaign = existingCampaign.get();
|
Campaign campaign = existingCampaign.get();
|
||||||
campaign.setName(data.name());
|
campaign.setName(data.name());
|
||||||
campaign.setDescription(data.description());
|
campaign.setDescription(data.description());
|
||||||
campaign.setLoreId(normalizeLoreId(data.loreId()));
|
campaign.setLoreId(normalizeId(data.loreId()));
|
||||||
|
campaign.setGameSystemId(normalizeId(data.gameSystemId()));
|
||||||
return campaignRepository.save(campaign);
|
return campaignRepository.save(campaign);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Normalise un loreId entrant : une chaîne vide/blanche est traitée comme "pas de lien".
|
* Normalise un ID entrant : une chaîne vide/blanche est traitée comme "pas de lien".
|
||||||
* Utile car les payloads JSON peuvent envoyer "" au lieu de null.
|
* Utile car les payloads JSON peuvent envoyer "" au lieu de null.
|
||||||
*/
|
*/
|
||||||
private String normalizeLoreId(String loreId) {
|
private String normalizeId(String id) {
|
||||||
return (loreId == null || loreId.isBlank()) ? null : loreId;
|
return (id == null || id.isBlank()) ? null : id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calcule l'impact d'une suppression en cascade : nombre d'arcs, chapitres,
|
||||||
|
* scènes et personnages qui disparaîtront avec la campagne. Utilisé par l'UI
|
||||||
|
* pour afficher "X arcs, Y chapitres, Z scènes seront supprimés".
|
||||||
|
*/
|
||||||
|
public DeletionImpact getDeletionImpact(String id) {
|
||||||
|
List<Arc> arcs = arcRepository.findByCampaignId(id);
|
||||||
|
int chapterTotal = 0;
|
||||||
|
int sceneTotal = 0;
|
||||||
|
for (Arc arc : arcs) {
|
||||||
|
List<Chapter> chapters = chapterRepository.findByArcId(arc.getId());
|
||||||
|
chapterTotal += chapters.size();
|
||||||
|
for (Chapter chapter : chapters) {
|
||||||
|
sceneTotal += sceneRepository.findByChapterId(chapter.getId()).size();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int characterTotal = characterRepository.findByCampaignId(id).size();
|
||||||
|
return new DeletionImpact(arcs.size(), chapterTotal, sceneTotal, characterTotal);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Supprime la campagne et toutes ses entités dépendantes (arcs → chapitres →
|
||||||
|
* scènes, plus les personnages). L'opération est transactionnelle : soit
|
||||||
|
* tout disparaît, soit rien ne change. Les FKs applicatives n'ayant pas
|
||||||
|
* de contrainte CASCADE au niveau DB, on orchestre la cascade ici.
|
||||||
|
*/
|
||||||
|
@Transactional
|
||||||
public void deleteCampaign(String id) {
|
public void deleteCampaign(String id) {
|
||||||
|
List<Arc> arcs = arcRepository.findByCampaignId(id);
|
||||||
|
for (Arc arc : arcs) {
|
||||||
|
List<Chapter> chapters = chapterRepository.findByArcId(arc.getId());
|
||||||
|
for (Chapter chapter : chapters) {
|
||||||
|
for (var scene : sceneRepository.findByChapterId(chapter.getId())) {
|
||||||
|
sceneRepository.deleteById(scene.getId());
|
||||||
|
}
|
||||||
|
chapterRepository.deleteById(chapter.getId());
|
||||||
|
}
|
||||||
|
arcRepository.deleteById(arc.getId());
|
||||||
|
}
|
||||||
|
for (var character : characterRepository.findByCampaignId(id)) {
|
||||||
|
characterRepository.deleteById(character.getId());
|
||||||
|
}
|
||||||
campaignRepository.deleteById(id);
|
campaignRepository.deleteById(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,8 +2,10 @@ package com.loremind.application.campaigncontext;
|
|||||||
|
|
||||||
import com.loremind.domain.campaigncontext.Chapter;
|
import com.loremind.domain.campaigncontext.Chapter;
|
||||||
import com.loremind.domain.campaigncontext.ports.ChapterRepository;
|
import com.loremind.domain.campaigncontext.ports.ChapterRepository;
|
||||||
|
import com.loremind.domain.campaigncontext.ports.SceneRepository;
|
||||||
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.BeanUtils;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
@@ -17,17 +19,27 @@ import java.util.Optional;
|
|||||||
public class ChapterService {
|
public class ChapterService {
|
||||||
|
|
||||||
private final ChapterRepository chapterRepository;
|
private final ChapterRepository chapterRepository;
|
||||||
|
private final SceneRepository sceneRepository;
|
||||||
|
|
||||||
public ChapterService(ChapterRepository chapterRepository) {
|
public ChapterService(ChapterRepository chapterRepository, SceneRepository sceneRepository) {
|
||||||
this.chapterRepository = chapterRepository;
|
this.chapterRepository = chapterRepository;
|
||||||
|
this.sceneRepository = sceneRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Compte des scènes qui seront supprimées en cascade avec le chapitre. */
|
||||||
|
public record DeletionImpact(int scenes) {}
|
||||||
|
|
||||||
public Chapter createChapter(String name, String description, String arcId, int order) {
|
public Chapter createChapter(String name, String description, String arcId, int order) {
|
||||||
|
return createChapter(name, description, arcId, order, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Chapter createChapter(String name, String description, String arcId, int order, String icon) {
|
||||||
Chapter chapter = Chapter.builder()
|
Chapter chapter = Chapter.builder()
|
||||||
.name(name)
|
.name(name)
|
||||||
.description(description)
|
.description(description)
|
||||||
.arcId(arcId)
|
.arcId(arcId)
|
||||||
.order(order)
|
.order(order)
|
||||||
|
.icon(icon)
|
||||||
.build();
|
.build();
|
||||||
return chapterRepository.save(chapter);
|
return chapterRepository.save(chapter);
|
||||||
}
|
}
|
||||||
@@ -58,7 +70,17 @@ public class ChapterService {
|
|||||||
return chapterRepository.save(chapter);
|
return chapterRepository.save(chapter);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Compte des scènes qui tomberont avec le chapitre. */
|
||||||
|
public DeletionImpact getDeletionImpact(String id) {
|
||||||
|
return new DeletionImpact(sceneRepository.findByChapterId(id).size());
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Supprime le chapitre et toutes ses scènes. Transactionnel : atomique. */
|
||||||
|
@Transactional
|
||||||
public void deleteChapter(String id) {
|
public void deleteChapter(String id) {
|
||||||
|
for (var scene : sceneRepository.findByChapterId(id)) {
|
||||||
|
sceneRepository.deleteById(scene.getId());
|
||||||
|
}
|
||||||
chapterRepository.deleteById(id);
|
chapterRepository.deleteById(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,72 @@
|
|||||||
|
package com.loremind.application.campaigncontext;
|
||||||
|
|
||||||
|
import com.loremind.domain.campaigncontext.Character;
|
||||||
|
import com.loremind.domain.campaigncontext.ports.CharacterRepository;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Service d'application pour les fiches de personnages (PJ).
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
public class CharacterService {
|
||||||
|
|
||||||
|
private final CharacterRepository characterRepository;
|
||||||
|
|
||||||
|
public CharacterService(CharacterRepository characterRepository) {
|
||||||
|
this.characterRepository = characterRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parameter Object pour la création / mise à jour d'un Character.
|
||||||
|
* `order` est fourni par le controller ; si absent, le service le calcule.
|
||||||
|
*/
|
||||||
|
public record CharacterData(String name, String markdownContent, String campaignId, Integer order) {}
|
||||||
|
|
||||||
|
public Character createCharacter(CharacterData data) {
|
||||||
|
int order = data.order() != null
|
||||||
|
? data.order()
|
||||||
|
: nextOrderFor(data.campaignId());
|
||||||
|
Character character = Character.builder()
|
||||||
|
.name(data.name())
|
||||||
|
.markdownContent(data.markdownContent())
|
||||||
|
.campaignId(data.campaignId())
|
||||||
|
.order(order)
|
||||||
|
.build();
|
||||||
|
return characterRepository.save(character);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<Character> getCharacterById(String id) {
|
||||||
|
return characterRepository.findById(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Character> getCharactersByCampaignId(String campaignId) {
|
||||||
|
return characterRepository.findByCampaignId(campaignId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Character updateCharacter(String id, CharacterData data) {
|
||||||
|
Character existing = characterRepository.findById(id)
|
||||||
|
.orElseThrow(() -> new IllegalArgumentException("Character non trouvé avec l'ID: " + id));
|
||||||
|
existing.setName(data.name());
|
||||||
|
existing.setMarkdownContent(data.markdownContent());
|
||||||
|
if (data.order() != null) {
|
||||||
|
existing.setOrder(data.order());
|
||||||
|
}
|
||||||
|
// campaignId n'est pas modifiable après création (cross-campagne move hors scope MVP).
|
||||||
|
return characterRepository.save(existing);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void deleteCharacter(String id) {
|
||||||
|
characterRepository.deleteById(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Renvoie la prochaine position libre — append en fin de liste. */
|
||||||
|
private int nextOrderFor(String campaignId) {
|
||||||
|
return characterRepository.findByCampaignId(campaignId).stream()
|
||||||
|
.mapToInt(Character::getOrder)
|
||||||
|
.max()
|
||||||
|
.orElse(-1) + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -26,11 +26,16 @@ public class SceneService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Scene createScene(String name, String description, String chapterId, int order) {
|
public Scene createScene(String name, String description, String chapterId, int order) {
|
||||||
|
return createScene(name, description, chapterId, order, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Scene createScene(String name, String description, String chapterId, int order, String icon) {
|
||||||
Scene scene = Scene.builder()
|
Scene scene = Scene.builder()
|
||||||
.name(name)
|
.name(name)
|
||||||
.description(description)
|
.description(description)
|
||||||
.chapterId(chapterId)
|
.chapterId(chapterId)
|
||||||
.order(order)
|
.order(order)
|
||||||
|
.icon(icon)
|
||||||
.build();
|
.build();
|
||||||
return sceneRepository.save(scene);
|
return sceneRepository.save(scene);
|
||||||
}
|
}
|
||||||
@@ -93,7 +98,7 @@ public class SceneService {
|
|||||||
.collect(Collectors.toSet());
|
.collect(Collectors.toSet());
|
||||||
|
|
||||||
for (SceneBranch b : branches) {
|
for (SceneBranch b : branches) {
|
||||||
String target = b.getTargetSceneId();
|
String target = b.targetSceneId();
|
||||||
if (target == null || target.isBlank()) {
|
if (target == null || target.isBlank()) {
|
||||||
throw new IllegalArgumentException("Une branche doit avoir une scène de destination");
|
throw new IllegalArgumentException("Une branche doit avoir une scène de destination");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,134 @@
|
|||||||
|
package com.loremind.application.conversationcontext;
|
||||||
|
|
||||||
|
import com.loremind.domain.conversationcontext.Conversation;
|
||||||
|
import com.loremind.domain.conversationcontext.ConversationMessage;
|
||||||
|
import com.loremind.domain.conversationcontext.ports.ConversationRepository;
|
||||||
|
import com.loremind.domain.conversationcontext.ports.ConversationTitleGenerator;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Service d'application du contexte Conversation.
|
||||||
|
*
|
||||||
|
* Regroupe les cas d'usage CRUD + append message + rename. Un seul
|
||||||
|
* service suffit — le contexte est simple et les operations fortement
|
||||||
|
* liees (meme aggregat).
|
||||||
|
*
|
||||||
|
* Regles metier :
|
||||||
|
* - exactement un ancrage parent (loreId XOR campaignId) ;
|
||||||
|
* - entityType et entityId vont ensemble (tous deux null = niveau racine,
|
||||||
|
* tous deux non-null = niveau entite precise).
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
public class ConversationService {
|
||||||
|
|
||||||
|
private final ConversationRepository repository;
|
||||||
|
private final ConversationTitleGenerator titleGenerator;
|
||||||
|
|
||||||
|
public ConversationService(ConversationRepository repository,
|
||||||
|
ConversationTitleGenerator titleGenerator) {
|
||||||
|
this.repository = repository;
|
||||||
|
this.titleGenerator = titleGenerator;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Donnees de creation d'une conversation. Titre optionnel — sera auto-genere si absent. */
|
||||||
|
public record CreateData(
|
||||||
|
String title,
|
||||||
|
String loreId,
|
||||||
|
String campaignId,
|
||||||
|
String entityType,
|
||||||
|
String entityId) {}
|
||||||
|
|
||||||
|
public Conversation create(CreateData data) {
|
||||||
|
validateAnchor(data.loreId(), data.campaignId(), data.entityType(), data.entityId());
|
||||||
|
|
||||||
|
String title = (data.title() == null || data.title().isBlank())
|
||||||
|
? "Nouvelle conversation"
|
||||||
|
: data.title().trim();
|
||||||
|
|
||||||
|
Conversation conv = Conversation.builder()
|
||||||
|
.title(title)
|
||||||
|
.loreId(data.loreId())
|
||||||
|
.campaignId(data.campaignId())
|
||||||
|
.entityType(data.entityType())
|
||||||
|
.entityId(data.entityId())
|
||||||
|
.build();
|
||||||
|
return repository.save(conv);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<Conversation> getById(String id) {
|
||||||
|
return repository.findById(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Conversation> listByContext(String loreId, String campaignId, String entityType, String entityId) {
|
||||||
|
validateAnchor(loreId, campaignId, entityType, entityId);
|
||||||
|
return repository.findByContext(loreId, campaignId, entityType, entityId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void rename(String id, String title) {
|
||||||
|
if (title == null || title.isBlank()) {
|
||||||
|
throw new IllegalArgumentException("Le titre ne peut pas etre vide");
|
||||||
|
}
|
||||||
|
if (repository.findById(id).isEmpty()) {
|
||||||
|
throw new IllegalArgumentException("Conversation introuvable : " + id);
|
||||||
|
}
|
||||||
|
repository.updateTitle(id, title.trim());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void delete(String id) {
|
||||||
|
repository.deleteById(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Auto-genere un titre a partir des premiers messages et le persiste.
|
||||||
|
* Appele typiquement apres le 1er couple user/assistant pour remplacer
|
||||||
|
* le titre provisoire. Echec silencieux (fallback dans l'adaptateur) —
|
||||||
|
* on n'empeche pas la conversation de fonctionner si le Brain est down.
|
||||||
|
*/
|
||||||
|
public String autoGenerateTitle(String conversationId) {
|
||||||
|
Conversation conv = repository.findById(conversationId)
|
||||||
|
.orElseThrow(() -> new IllegalArgumentException("Conversation introuvable : " + conversationId));
|
||||||
|
List<ConversationMessage> seeds = conv.getMessages();
|
||||||
|
if (seeds == null || seeds.isEmpty()) {
|
||||||
|
return conv.getTitle();
|
||||||
|
}
|
||||||
|
String title = titleGenerator.generate(seeds);
|
||||||
|
repository.updateTitle(conversationId, title);
|
||||||
|
return title;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ajoute un message (user ou assistant) a une conversation existante.
|
||||||
|
* L'horodatage et l'id sont assignes par la couche persistance.
|
||||||
|
*/
|
||||||
|
public ConversationMessage appendMessage(String conversationId, String role, String content) {
|
||||||
|
if (role == null || (!role.equals("user") && !role.equals("assistant") && !role.equals("system"))) {
|
||||||
|
throw new IllegalArgumentException("Role invalide : " + role);
|
||||||
|
}
|
||||||
|
if (content == null || content.isEmpty()) {
|
||||||
|
throw new IllegalArgumentException("Contenu vide interdit");
|
||||||
|
}
|
||||||
|
ConversationMessage msg = ConversationMessage.builder()
|
||||||
|
.role(role)
|
||||||
|
.content(content)
|
||||||
|
.build();
|
||||||
|
return repository.appendMessage(conversationId, msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------- Validation ----------
|
||||||
|
|
||||||
|
private void validateAnchor(String loreId, String campaignId, String entityType, String entityId) {
|
||||||
|
boolean hasLore = loreId != null && !loreId.isBlank();
|
||||||
|
boolean hasCamp = campaignId != null && !campaignId.isBlank();
|
||||||
|
if (hasLore == hasCamp) {
|
||||||
|
throw new IllegalArgumentException("Exactement un parent attendu : loreId XOR campaignId");
|
||||||
|
}
|
||||||
|
boolean hasType = entityType != null && !entityType.isBlank();
|
||||||
|
boolean hasEntId = entityId != null && !entityId.isBlank();
|
||||||
|
if (hasType != hasEntId) {
|
||||||
|
throw new IllegalArgumentException("entityType et entityId doivent etre tous deux null ou tous deux non-null");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,88 @@
|
|||||||
|
package com.loremind.application.gamesystemcontext;
|
||||||
|
|
||||||
|
import com.loremind.domain.gamesystemcontext.GameSystem;
|
||||||
|
import com.loremind.domain.gamesystemcontext.GenerationIntent;
|
||||||
|
import com.loremind.domain.gamesystemcontext.ports.GameSystemRepository;
|
||||||
|
import com.loremind.domain.generationcontext.GameSystemContext;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construit un {@link GameSystemContext} à partir d'un gameSystemId et d'un intent.
|
||||||
|
* <p>
|
||||||
|
* Pipeline :
|
||||||
|
* 1. Charge le GameSystem (retourne Optional.empty si introuvable — dégradation gracieuse).
|
||||||
|
* 2. Parse le markdown par titres H2 (## Section) → Map<Titre, Contenu>.
|
||||||
|
* 3. Filtre les sections selon l'intent via les alias {@link GenerationIntent#getSectionAliases()}.
|
||||||
|
* GENERIC = pas de filtre.
|
||||||
|
* <p>
|
||||||
|
* Parsing à la volée (pas de cache) : les règles d'un système font
|
||||||
|
* typiquement 5-20kB, le coût de parsing est négligeable devant l'appel LLM.
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
public class GameSystemContextBuilder {
|
||||||
|
|
||||||
|
/** Matche "## Titre" en début de ligne (multiline). Capture le titre en groupe 1. */
|
||||||
|
private static final Pattern H2_HEADER = Pattern.compile("(?m)^##\\s+(.+?)\\s*$");
|
||||||
|
|
||||||
|
private final GameSystemRepository gameSystemRepository;
|
||||||
|
|
||||||
|
public GameSystemContextBuilder(GameSystemRepository gameSystemRepository) {
|
||||||
|
this.gameSystemRepository = gameSystemRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<GameSystemContext> buildOptional(String gameSystemId, GenerationIntent intent) {
|
||||||
|
if (gameSystemId == null || gameSystemId.isBlank()) return Optional.empty();
|
||||||
|
return gameSystemRepository.findById(gameSystemId)
|
||||||
|
.map(gs -> build(gs, intent));
|
||||||
|
}
|
||||||
|
|
||||||
|
private GameSystemContext build(GameSystem gs, GenerationIntent intent) {
|
||||||
|
Map<String, String> allSections = parseH2Sections(gs.getRulesMarkdown());
|
||||||
|
Map<String, String> filtered = filterByIntent(allSections, intent);
|
||||||
|
return new GameSystemContext(gs.getName(), gs.getDescription(), filtered);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Découpe le markdown par titres H2. Préserve l'ordre d'apparition (LinkedHashMap).
|
||||||
|
* Le contenu avant le premier H2 est ignoré (préambule libre).
|
||||||
|
*/
|
||||||
|
Map<String, String> parseH2Sections(String markdown) {
|
||||||
|
Map<String, String> sections = new LinkedHashMap<>();
|
||||||
|
if (markdown == null || markdown.isBlank()) return sections;
|
||||||
|
|
||||||
|
Matcher m = H2_HEADER.matcher(markdown);
|
||||||
|
String currentTitle = null;
|
||||||
|
int currentContentStart = -1;
|
||||||
|
|
||||||
|
while (m.find()) {
|
||||||
|
if (currentTitle != null) {
|
||||||
|
sections.put(currentTitle, markdown.substring(currentContentStart, m.start()).strip());
|
||||||
|
}
|
||||||
|
currentTitle = m.group(1).trim();
|
||||||
|
currentContentStart = m.end();
|
||||||
|
}
|
||||||
|
if (currentTitle != null) {
|
||||||
|
sections.put(currentTitle, markdown.substring(currentContentStart).strip());
|
||||||
|
}
|
||||||
|
return sections;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String, String> filterByIntent(Map<String, String> sections, GenerationIntent intent) {
|
||||||
|
if (intent.matchesAllSections()) return sections;
|
||||||
|
Map<String, String> filtered = new LinkedHashMap<>();
|
||||||
|
for (Map.Entry<String, String> e : sections.entrySet()) {
|
||||||
|
String titleLower = e.getKey().toLowerCase();
|
||||||
|
boolean match = intent.getSectionAliases().stream().anyMatch(titleLower::contains);
|
||||||
|
if (match) {
|
||||||
|
filtered.put(e.getKey(), e.getValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return filtered;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,76 @@
|
|||||||
|
package com.loremind.application.gamesystemcontext;
|
||||||
|
|
||||||
|
import com.loremind.domain.gamesystemcontext.GameSystem;
|
||||||
|
import com.loremind.domain.gamesystemcontext.ports.GameSystemRepository;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class GameSystemService {
|
||||||
|
|
||||||
|
private final GameSystemRepository gameSystemRepository;
|
||||||
|
|
||||||
|
public GameSystemService(GameSystemRepository gameSystemRepository) {
|
||||||
|
this.gameSystemRepository = gameSystemRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parameter Object pour la création / mise à jour d'un GameSystem.
|
||||||
|
*/
|
||||||
|
public record GameSystemData(
|
||||||
|
String name,
|
||||||
|
String description,
|
||||||
|
String rulesMarkdown,
|
||||||
|
String author,
|
||||||
|
boolean isPublic
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public GameSystem createGameSystem(GameSystemData data) {
|
||||||
|
GameSystem gameSystem = GameSystem.builder()
|
||||||
|
.name(data.name())
|
||||||
|
.description(data.description())
|
||||||
|
.rulesMarkdown(data.rulesMarkdown())
|
||||||
|
.author(normalize(data.author()))
|
||||||
|
.isPublic(data.isPublic())
|
||||||
|
.build();
|
||||||
|
return gameSystemRepository.save(gameSystem);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<GameSystem> getGameSystemById(String id) {
|
||||||
|
return gameSystemRepository.findById(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<GameSystem> getAllGameSystems() {
|
||||||
|
return gameSystemRepository.findAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
public GameSystem updateGameSystem(String id, GameSystemData data) {
|
||||||
|
GameSystem existing = gameSystemRepository.findById(id)
|
||||||
|
.orElseThrow(() -> new IllegalArgumentException("GameSystem non trouvé avec l'ID: " + id));
|
||||||
|
existing.setName(data.name());
|
||||||
|
existing.setDescription(data.description());
|
||||||
|
existing.setRulesMarkdown(data.rulesMarkdown());
|
||||||
|
existing.setAuthor(normalize(data.author()));
|
||||||
|
existing.setPublic(data.isPublic());
|
||||||
|
return gameSystemRepository.save(existing);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void deleteGameSystem(String id) {
|
||||||
|
gameSystemRepository.deleteById(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean gameSystemExists(String id) {
|
||||||
|
return gameSystemRepository.existsById(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<GameSystem> searchGameSystems(String query) {
|
||||||
|
if (query == null || query.isBlank()) return List.of();
|
||||||
|
return gameSystemRepository.searchByName(query.trim());
|
||||||
|
}
|
||||||
|
|
||||||
|
private String normalize(String value) {
|
||||||
|
return (value == null || value.isBlank()) ? null : value;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,15 +3,18 @@ package com.loremind.application.generationcontext;
|
|||||||
import com.loremind.domain.campaigncontext.Arc;
|
import com.loremind.domain.campaigncontext.Arc;
|
||||||
import com.loremind.domain.campaigncontext.Campaign;
|
import com.loremind.domain.campaigncontext.Campaign;
|
||||||
import com.loremind.domain.campaigncontext.Chapter;
|
import com.loremind.domain.campaigncontext.Chapter;
|
||||||
|
import com.loremind.domain.campaigncontext.Character;
|
||||||
import com.loremind.domain.campaigncontext.Scene;
|
import com.loremind.domain.campaigncontext.Scene;
|
||||||
import com.loremind.domain.campaigncontext.ports.ArcRepository;
|
import com.loremind.domain.campaigncontext.ports.ArcRepository;
|
||||||
import com.loremind.domain.campaigncontext.ports.CampaignRepository;
|
import com.loremind.domain.campaigncontext.ports.CampaignRepository;
|
||||||
import com.loremind.domain.campaigncontext.ports.ChapterRepository;
|
import com.loremind.domain.campaigncontext.ports.ChapterRepository;
|
||||||
|
import com.loremind.domain.campaigncontext.ports.CharacterRepository;
|
||||||
import com.loremind.domain.campaigncontext.ports.SceneRepository;
|
import com.loremind.domain.campaigncontext.ports.SceneRepository;
|
||||||
import com.loremind.domain.generationcontext.CampaignStructuralContext;
|
import com.loremind.domain.generationcontext.CampaignStructuralContext;
|
||||||
import com.loremind.domain.generationcontext.CampaignStructuralContext.ArcSummary;
|
import com.loremind.domain.generationcontext.CampaignStructuralContext.ArcSummary;
|
||||||
import com.loremind.domain.generationcontext.CampaignStructuralContext.BranchHint;
|
import com.loremind.domain.generationcontext.CampaignStructuralContext.BranchHint;
|
||||||
import com.loremind.domain.generationcontext.CampaignStructuralContext.ChapterSummary;
|
import com.loremind.domain.generationcontext.CampaignStructuralContext.ChapterSummary;
|
||||||
|
import com.loremind.domain.generationcontext.CampaignStructuralContext.CharacterSummary;
|
||||||
import com.loremind.domain.generationcontext.CampaignStructuralContext.SceneSummary;
|
import com.loremind.domain.generationcontext.CampaignStructuralContext.SceneSummary;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
@@ -38,18 +41,24 @@ public class CampaignStructuralContextBuilder {
|
|||||||
private final ArcRepository arcRepository;
|
private final ArcRepository arcRepository;
|
||||||
private final ChapterRepository chapterRepository;
|
private final ChapterRepository chapterRepository;
|
||||||
private final SceneRepository sceneRepository;
|
private final SceneRepository sceneRepository;
|
||||||
|
private final CharacterRepository characterRepository;
|
||||||
|
|
||||||
public CampaignStructuralContextBuilder(
|
public CampaignStructuralContextBuilder(
|
||||||
CampaignRepository campaignRepository,
|
CampaignRepository campaignRepository,
|
||||||
ArcRepository arcRepository,
|
ArcRepository arcRepository,
|
||||||
ChapterRepository chapterRepository,
|
ChapterRepository chapterRepository,
|
||||||
SceneRepository sceneRepository) {
|
SceneRepository sceneRepository,
|
||||||
|
CharacterRepository characterRepository) {
|
||||||
this.campaignRepository = campaignRepository;
|
this.campaignRepository = campaignRepository;
|
||||||
this.arcRepository = arcRepository;
|
this.arcRepository = arcRepository;
|
||||||
this.chapterRepository = chapterRepository;
|
this.chapterRepository = chapterRepository;
|
||||||
this.sceneRepository = sceneRepository;
|
this.sceneRepository = sceneRepository;
|
||||||
|
this.characterRepository = characterRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Longueur max du snippet de PJ injecté dans le contexte (coût tokens maîtrisé). */
|
||||||
|
private static final int CHARACTER_SNIPPET_MAX_LEN = 160;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construit la carte narrative d'une Campagne (arcs → chapitres → scènes,
|
* Construit la carte narrative d'une Campagne (arcs → chapitres → scènes,
|
||||||
* nom + description courte à chaque niveau).
|
* nom + description courte à chaque niveau).
|
||||||
@@ -65,11 +74,36 @@ public class CampaignStructuralContextBuilder {
|
|||||||
.map(this::toArcSummary)
|
.map(this::toArcSummary)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
return CampaignStructuralContext.builder()
|
List<CharacterSummary> characters = characterRepository.findByCampaignId(campaignId).stream()
|
||||||
.campaignName(campaign.getName())
|
.sorted(Comparator.comparingInt(Character::getOrder))
|
||||||
.campaignDescription(campaign.getDescription())
|
.map(this::toCharacterSummary)
|
||||||
.arcs(arcs)
|
.collect(Collectors.toList());
|
||||||
.build();
|
|
||||||
|
return new CampaignStructuralContext(
|
||||||
|
campaign.getName(),
|
||||||
|
campaign.getDescription(),
|
||||||
|
arcs,
|
||||||
|
characters);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Projette un PJ vers un résumé court : nom + 1re ligne "signifiante" du
|
||||||
|
* markdown (ni vide, ni un titre). Permet à l'IA de savoir "qui est Thorin"
|
||||||
|
* sans injecter toute sa fiche.
|
||||||
|
*/
|
||||||
|
private CharacterSummary toCharacterSummary(Character c) {
|
||||||
|
return new CharacterSummary(c.getName(), extractSnippet(c.getMarkdownContent()));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String extractSnippet(String markdown) {
|
||||||
|
if (markdown == null || markdown.isBlank()) return "";
|
||||||
|
String firstLine = markdown.lines()
|
||||||
|
.map(String::strip)
|
||||||
|
.filter(l -> !l.isEmpty() && !l.startsWith("#"))
|
||||||
|
.findFirst()
|
||||||
|
.orElse("");
|
||||||
|
if (firstLine.length() <= CHARACTER_SNIPPET_MAX_LEN) return firstLine;
|
||||||
|
return firstLine.substring(0, CHARACTER_SNIPPET_MAX_LEN - 1).stripTrailing() + "…";
|
||||||
}
|
}
|
||||||
|
|
||||||
private ArcSummary toArcSummary(Arc arc) {
|
private ArcSummary toArcSummary(Arc arc) {
|
||||||
@@ -77,12 +111,11 @@ public class CampaignStructuralContextBuilder {
|
|||||||
.sorted(Comparator.comparingInt(Chapter::getOrder))
|
.sorted(Comparator.comparingInt(Chapter::getOrder))
|
||||||
.map(this::toChapterSummary)
|
.map(this::toChapterSummary)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
return ArcSummary.builder()
|
return new ArcSummary(
|
||||||
.name(arc.getName())
|
arc.getName(),
|
||||||
.description(arc.getDescription())
|
arc.getDescription(),
|
||||||
.illustrationCount(countImages(arc.getIllustrationImageIds()))
|
countImages(arc.getIllustrationImageIds()),
|
||||||
.chapters(chapters)
|
chapters);
|
||||||
.build();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private ChapterSummary toChapterSummary(Chapter chapter) {
|
private ChapterSummary toChapterSummary(Chapter chapter) {
|
||||||
@@ -99,32 +132,28 @@ public class CampaignStructuralContextBuilder {
|
|||||||
.map(s -> toSceneSummary(s, nameById))
|
.map(s -> toSceneSummary(s, nameById))
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
return ChapterSummary.builder()
|
return new ChapterSummary(
|
||||||
.name(chapter.getName())
|
chapter.getName(),
|
||||||
.description(chapter.getDescription())
|
chapter.getDescription(),
|
||||||
.illustrationCount(countImages(chapter.getIllustrationImageIds()))
|
countImages(chapter.getIllustrationImageIds()),
|
||||||
.scenes(summaries)
|
summaries);
|
||||||
.build();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private SceneSummary toSceneSummary(Scene scene, Map<String, String> nameById) {
|
private SceneSummary toSceneSummary(Scene scene, Map<String, String> nameById) {
|
||||||
List<BranchHint> hints = scene.getBranches() == null
|
List<BranchHint> hints = scene.getBranches() == null
|
||||||
? List.of()
|
? List.of()
|
||||||
: scene.getBranches().stream()
|
: scene.getBranches().stream()
|
||||||
.map(b -> BranchHint.builder()
|
.map(b -> new BranchHint(
|
||||||
.label(b.getLabel())
|
b.label(),
|
||||||
.targetSceneName(nameById.getOrDefault(
|
nameById.getOrDefault(b.targetSceneId(), "(scène inconnue)"),
|
||||||
b.getTargetSceneId(), "(scène inconnue)"))
|
b.condition()))
|
||||||
.condition(b.getCondition())
|
|
||||||
.build())
|
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
return SceneSummary.builder()
|
return new SceneSummary(
|
||||||
.name(scene.getName())
|
scene.getName(),
|
||||||
.description(scene.getDescription())
|
scene.getDescription(),
|
||||||
.illustrationCount(countImages(scene.getIllustrationImageIds()))
|
countImages(scene.getIllustrationImageIds()),
|
||||||
.branches(hints)
|
hints);
|
||||||
.build();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Helper defensif : compte les illustrations attachees (null-safe). */
|
/** Helper defensif : compte les illustrations attachees (null-safe). */
|
||||||
|
|||||||
@@ -67,16 +67,15 @@ public class GeneratePageValuesUseCase {
|
|||||||
|
|
||||||
requireNonEmptyFields(template);
|
requireNonEmptyFields(template);
|
||||||
|
|
||||||
GenerationContext context = GenerationContext.builder()
|
|
||||||
.loreName(lore.getName())
|
|
||||||
.loreDescription(lore.getDescription())
|
|
||||||
.folderName(folder.getName())
|
|
||||||
.templateName(template.getName())
|
|
||||||
// Seuls les champs TEXT sont envoyes a l'IA : les champs IMAGE
|
// Seuls les champs TEXT sont envoyes a l'IA : les champs IMAGE
|
||||||
// necessitent un workflow different (pas de generation LLM texte).
|
// necessitent un workflow different (pas de generation LLM texte).
|
||||||
.templateFields(template.textFieldNames())
|
GenerationContext context = new GenerationContext(
|
||||||
.pageTitle(page.getTitle())
|
lore.getName(),
|
||||||
.build();
|
lore.getDescription(),
|
||||||
|
folder.getName(),
|
||||||
|
template.getName(),
|
||||||
|
template.textFieldNames(),
|
||||||
|
page.getTitle());
|
||||||
|
|
||||||
GenerationResult result = aiProvider.generatePage(context);
|
GenerationResult result = aiProvider.generatePage(context);
|
||||||
return result.values();
|
return result.values();
|
||||||
|
|||||||
@@ -82,12 +82,11 @@ public class LoreStructuralContextBuilder {
|
|||||||
Map<String, String> pageTitleById = pages.stream()
|
Map<String, String> pageTitleById = pages.stream()
|
||||||
.collect(Collectors.toMap(Page::getId, Page::getTitle, (a, b) -> a));
|
.collect(Collectors.toMap(Page::getId, Page::getTitle, (a, b) -> a));
|
||||||
|
|
||||||
return LoreStructuralContext.builder()
|
return new LoreStructuralContext(
|
||||||
.loreName(lore.getName())
|
lore.getName(),
|
||||||
.loreDescription(lore.getDescription())
|
lore.getDescription(),
|
||||||
.folders(buildFoldersMap(nodes, pages, templateNameById, pageTitleById))
|
buildFoldersMap(nodes, pages, templateNameById, pageTitleById),
|
||||||
.tags(extractUniqueTags(pages))
|
extractUniqueTags(pages));
|
||||||
.build();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Map<String, List<PageSummary>> buildFoldersMap(
|
private Map<String, List<PageSummary>> buildFoldersMap(
|
||||||
@@ -118,13 +117,12 @@ public class LoreStructuralContextBuilder {
|
|||||||
Page page,
|
Page page,
|
||||||
Map<String, String> templateNameById,
|
Map<String, String> templateNameById,
|
||||||
Map<String, String> pageTitleById) {
|
Map<String, String> pageTitleById) {
|
||||||
return PageSummary.builder()
|
return new PageSummary(
|
||||||
.title(page.getTitle())
|
page.getTitle(),
|
||||||
.templateName(templateNameById.getOrDefault(page.getTemplateId(), "?"))
|
templateNameById.getOrDefault(page.getTemplateId(), "?"),
|
||||||
.values(truncatedValues(page.getValues()))
|
truncatedValues(page.getValues()),
|
||||||
.tags(page.getTags() != null ? List.copyOf(page.getTags()) : Collections.emptyList())
|
page.getTags() != null ? List.copyOf(page.getTags()) : Collections.emptyList(),
|
||||||
.relatedPageTitles(resolveRelatedTitles(page.getRelatedPageIds(), pageTitleById))
|
resolveRelatedTitles(page.getRelatedPageIds(), pageTitleById));
|
||||||
.build();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -2,9 +2,11 @@ package com.loremind.application.generationcontext;
|
|||||||
|
|
||||||
import com.loremind.domain.campaigncontext.Arc;
|
import com.loremind.domain.campaigncontext.Arc;
|
||||||
import com.loremind.domain.campaigncontext.Chapter;
|
import com.loremind.domain.campaigncontext.Chapter;
|
||||||
|
import com.loremind.domain.campaigncontext.Character;
|
||||||
import com.loremind.domain.campaigncontext.Scene;
|
import com.loremind.domain.campaigncontext.Scene;
|
||||||
import com.loremind.domain.campaigncontext.ports.ArcRepository;
|
import com.loremind.domain.campaigncontext.ports.ArcRepository;
|
||||||
import com.loremind.domain.campaigncontext.ports.ChapterRepository;
|
import com.loremind.domain.campaigncontext.ports.ChapterRepository;
|
||||||
|
import com.loremind.domain.campaigncontext.ports.CharacterRepository;
|
||||||
import com.loremind.domain.campaigncontext.ports.SceneRepository;
|
import com.loremind.domain.campaigncontext.ports.SceneRepository;
|
||||||
import com.loremind.domain.generationcontext.NarrativeEntityContext;
|
import com.loremind.domain.generationcontext.NarrativeEntityContext;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
@@ -26,20 +28,23 @@ public class NarrativeEntityContextBuilder {
|
|||||||
private final ArcRepository arcRepository;
|
private final ArcRepository arcRepository;
|
||||||
private final ChapterRepository chapterRepository;
|
private final ChapterRepository chapterRepository;
|
||||||
private final SceneRepository sceneRepository;
|
private final SceneRepository sceneRepository;
|
||||||
|
private final CharacterRepository characterRepository;
|
||||||
|
|
||||||
public NarrativeEntityContextBuilder(
|
public NarrativeEntityContextBuilder(
|
||||||
ArcRepository arcRepository,
|
ArcRepository arcRepository,
|
||||||
ChapterRepository chapterRepository,
|
ChapterRepository chapterRepository,
|
||||||
SceneRepository sceneRepository) {
|
SceneRepository sceneRepository,
|
||||||
|
CharacterRepository characterRepository) {
|
||||||
this.arcRepository = arcRepository;
|
this.arcRepository = arcRepository;
|
||||||
this.chapterRepository = chapterRepository;
|
this.chapterRepository = chapterRepository;
|
||||||
this.sceneRepository = sceneRepository;
|
this.sceneRepository = sceneRepository;
|
||||||
|
this.characterRepository = characterRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Charge l'entité narrative ciblée et la projette vers un VO du GenerationContext.
|
* Charge l'entité narrative ciblée et la projette vers un VO du GenerationContext.
|
||||||
*
|
*
|
||||||
* @param entityType "arc", "chapter" ou "scene" (insensible à la casse)
|
* @param entityType "arc", "chapter", "scene" ou "character" (insensible à la casse)
|
||||||
* @param entityId l'ID de l'entité
|
* @param entityId l'ID de l'entité
|
||||||
* @throws IllegalArgumentException si le type est inconnu ou l'entité introuvable
|
* @throws IllegalArgumentException si le type est inconnu ou l'entité introuvable
|
||||||
*/
|
*/
|
||||||
@@ -49,6 +54,7 @@ public class NarrativeEntityContextBuilder {
|
|||||||
case "arc" -> fromArc(loadArc(entityId));
|
case "arc" -> fromArc(loadArc(entityId));
|
||||||
case "chapter" -> fromChapter(loadChapter(entityId));
|
case "chapter" -> fromChapter(loadChapter(entityId));
|
||||||
case "scene" -> fromScene(loadScene(entityId));
|
case "scene" -> fromScene(loadScene(entityId));
|
||||||
|
case "character" -> fromCharacter(loadCharacter(entityId));
|
||||||
default -> throw new IllegalArgumentException("Type d'entité narrative inconnu: " + entityType);
|
default -> throw new IllegalArgumentException("Type d'entité narrative inconnu: " + entityType);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -70,6 +76,11 @@ public class NarrativeEntityContextBuilder {
|
|||||||
.orElseThrow(() -> new IllegalArgumentException("Scène non trouvée: " + id));
|
.orElseThrow(() -> new IllegalArgumentException("Scène non trouvée: " + id));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Character loadCharacter(String id) {
|
||||||
|
return characterRepository.findById(id)
|
||||||
|
.orElseThrow(() -> new IllegalArgumentException("Personnage non trouvé: " + id));
|
||||||
|
}
|
||||||
|
|
||||||
// --- Mapping entité → VO ------------------------------------------------
|
// --- Mapping entité → VO ------------------------------------------------
|
||||||
|
|
||||||
private NarrativeEntityContext fromArc(Arc a) {
|
private NarrativeEntityContext fromArc(Arc a) {
|
||||||
@@ -80,11 +91,7 @@ public class NarrativeEntityContextBuilder {
|
|||||||
putField(fields, "rewards", a.getRewards());
|
putField(fields, "rewards", a.getRewards());
|
||||||
putField(fields, "resolution", a.getResolution());
|
putField(fields, "resolution", a.getResolution());
|
||||||
putField(fields, "gmNotes", a.getGmNotes());
|
putField(fields, "gmNotes", a.getGmNotes());
|
||||||
return NarrativeEntityContext.builder()
|
return new NarrativeEntityContext("arc", a.getName(), fields);
|
||||||
.entityType("arc")
|
|
||||||
.title(a.getName())
|
|
||||||
.fields(fields)
|
|
||||||
.build();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private NarrativeEntityContext fromChapter(Chapter c) {
|
private NarrativeEntityContext fromChapter(Chapter c) {
|
||||||
@@ -93,11 +100,7 @@ public class NarrativeEntityContextBuilder {
|
|||||||
putField(fields, "playerObjectives", c.getPlayerObjectives());
|
putField(fields, "playerObjectives", c.getPlayerObjectives());
|
||||||
putField(fields, "narrativeStakes", c.getNarrativeStakes());
|
putField(fields, "narrativeStakes", c.getNarrativeStakes());
|
||||||
putField(fields, "gmNotes", c.getGmNotes());
|
putField(fields, "gmNotes", c.getGmNotes());
|
||||||
return NarrativeEntityContext.builder()
|
return new NarrativeEntityContext("chapter", c.getName(), fields);
|
||||||
.entityType("chapter")
|
|
||||||
.title(c.getName())
|
|
||||||
.fields(fields)
|
|
||||||
.build();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private NarrativeEntityContext fromScene(Scene s) {
|
private NarrativeEntityContext fromScene(Scene s) {
|
||||||
@@ -111,11 +114,13 @@ public class NarrativeEntityContextBuilder {
|
|||||||
putField(fields, "combatDifficulty", s.getCombatDifficulty());
|
putField(fields, "combatDifficulty", s.getCombatDifficulty());
|
||||||
putField(fields, "enemies", s.getEnemies());
|
putField(fields, "enemies", s.getEnemies());
|
||||||
putField(fields, "gmSecretNotes", s.getGmSecretNotes());
|
putField(fields, "gmSecretNotes", s.getGmSecretNotes());
|
||||||
return NarrativeEntityContext.builder()
|
return new NarrativeEntityContext("scene", s.getName(), fields);
|
||||||
.entityType("scene")
|
}
|
||||||
.title(s.getName())
|
|
||||||
.fields(fields)
|
private NarrativeEntityContext fromCharacter(Character c) {
|
||||||
.build();
|
Map<String, String> fields = new LinkedHashMap<>();
|
||||||
|
putField(fields, "fiche complète (markdown)", c.getMarkdownContent());
|
||||||
|
return new NarrativeEntityContext("character", c.getName(), fields);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Null/blank devient chaîne vide — uniforme côté prompt, pas de NPE côté LLM. */
|
/** Null/blank devient chaîne vide — uniforme côté prompt, pas de NPE côté LLM. */
|
||||||
|
|||||||
@@ -1,10 +1,14 @@
|
|||||||
package com.loremind.application.generationcontext;
|
package com.loremind.application.generationcontext;
|
||||||
|
|
||||||
|
import com.loremind.application.gamesystemcontext.GameSystemContextBuilder;
|
||||||
import com.loremind.domain.campaigncontext.Campaign;
|
import com.loremind.domain.campaigncontext.Campaign;
|
||||||
import com.loremind.domain.campaigncontext.ports.CampaignRepository;
|
import com.loremind.domain.campaigncontext.ports.CampaignRepository;
|
||||||
|
import com.loremind.domain.gamesystemcontext.GenerationIntent;
|
||||||
import com.loremind.domain.generationcontext.CampaignStructuralContext;
|
import com.loremind.domain.generationcontext.CampaignStructuralContext;
|
||||||
import com.loremind.domain.generationcontext.ChatMessage;
|
import com.loremind.domain.generationcontext.ChatMessage;
|
||||||
import com.loremind.domain.generationcontext.ChatRequest;
|
import com.loremind.domain.generationcontext.ChatRequest;
|
||||||
|
import com.loremind.domain.generationcontext.ChatUsage;
|
||||||
|
import com.loremind.domain.generationcontext.GameSystemContext;
|
||||||
import com.loremind.domain.generationcontext.LoreStructuralContext;
|
import com.loremind.domain.generationcontext.LoreStructuralContext;
|
||||||
import com.loremind.domain.generationcontext.NarrativeEntityContext;
|
import com.loremind.domain.generationcontext.NarrativeEntityContext;
|
||||||
import com.loremind.domain.generationcontext.ports.AiChatProvider;
|
import com.loremind.domain.generationcontext.ports.AiChatProvider;
|
||||||
@@ -33,6 +37,7 @@ public class StreamChatForCampaignUseCase {
|
|||||||
private final CampaignStructuralContextBuilder campaignContextBuilder;
|
private final CampaignStructuralContextBuilder campaignContextBuilder;
|
||||||
private final LoreStructuralContextBuilder loreContextBuilder;
|
private final LoreStructuralContextBuilder loreContextBuilder;
|
||||||
private final NarrativeEntityContextBuilder narrativeEntityContextBuilder;
|
private final NarrativeEntityContextBuilder narrativeEntityContextBuilder;
|
||||||
|
private final GameSystemContextBuilder gameSystemContextBuilder;
|
||||||
private final AiChatProvider aiChatProvider;
|
private final AiChatProvider aiChatProvider;
|
||||||
|
|
||||||
public StreamChatForCampaignUseCase(
|
public StreamChatForCampaignUseCase(
|
||||||
@@ -40,11 +45,13 @@ public class StreamChatForCampaignUseCase {
|
|||||||
CampaignStructuralContextBuilder campaignContextBuilder,
|
CampaignStructuralContextBuilder campaignContextBuilder,
|
||||||
LoreStructuralContextBuilder loreContextBuilder,
|
LoreStructuralContextBuilder loreContextBuilder,
|
||||||
NarrativeEntityContextBuilder narrativeEntityContextBuilder,
|
NarrativeEntityContextBuilder narrativeEntityContextBuilder,
|
||||||
|
GameSystemContextBuilder gameSystemContextBuilder,
|
||||||
AiChatProvider aiChatProvider) {
|
AiChatProvider aiChatProvider) {
|
||||||
this.campaignRepository = campaignRepository;
|
this.campaignRepository = campaignRepository;
|
||||||
this.campaignContextBuilder = campaignContextBuilder;
|
this.campaignContextBuilder = campaignContextBuilder;
|
||||||
this.loreContextBuilder = loreContextBuilder;
|
this.loreContextBuilder = loreContextBuilder;
|
||||||
this.narrativeEntityContextBuilder = narrativeEntityContextBuilder;
|
this.narrativeEntityContextBuilder = narrativeEntityContextBuilder;
|
||||||
|
this.gameSystemContextBuilder = gameSystemContextBuilder;
|
||||||
this.aiChatProvider = aiChatProvider;
|
this.aiChatProvider = aiChatProvider;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,6 +72,7 @@ public class StreamChatForCampaignUseCase {
|
|||||||
String entityType,
|
String entityType,
|
||||||
String entityId,
|
String entityId,
|
||||||
List<ChatMessage> messages,
|
List<ChatMessage> messages,
|
||||||
|
Consumer<ChatUsage> onUsage,
|
||||||
Consumer<String> onToken,
|
Consumer<String> onToken,
|
||||||
Runnable onComplete,
|
Runnable onComplete,
|
||||||
Consumer<Throwable> onError) {
|
Consumer<Throwable> onError) {
|
||||||
@@ -76,15 +84,17 @@ public class StreamChatForCampaignUseCase {
|
|||||||
CampaignStructuralContext campaignContext = campaignContextBuilder.build(campaignId);
|
CampaignStructuralContext campaignContext = campaignContextBuilder.build(campaignId);
|
||||||
LoreStructuralContext loreContext = loadLinkedLoreContextOrNull(campaign);
|
LoreStructuralContext loreContext = loadLinkedLoreContextOrNull(campaign);
|
||||||
NarrativeEntityContext narrativeEntity = buildNarrativeEntityOrNull(entityType, entityId);
|
NarrativeEntityContext narrativeEntity = buildNarrativeEntityOrNull(entityType, entityId);
|
||||||
|
GameSystemContext gameSystemContext = loadGameSystemContextOrNull(campaign, entityType);
|
||||||
|
|
||||||
ChatRequest request = ChatRequest.builder()
|
ChatRequest request = ChatRequest.builder()
|
||||||
.messages(messages)
|
.messages(messages)
|
||||||
.loreContext(loreContext)
|
.loreContext(loreContext)
|
||||||
.campaignContext(campaignContext)
|
.campaignContext(campaignContext)
|
||||||
.narrativeEntity(narrativeEntity)
|
.narrativeEntity(narrativeEntity)
|
||||||
|
.gameSystemContext(gameSystemContext)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
aiChatProvider.streamChat(request, onToken, onComplete, onError);
|
aiChatProvider.streamChat(request, onUsage, onToken, onComplete, onError);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -102,4 +112,16 @@ public class StreamChatForCampaignUseCase {
|
|||||||
if (entityId == null || entityId.isBlank()) return null;
|
if (entityId == null || entityId.isBlank()) return null;
|
||||||
return narrativeEntityContextBuilder.build(entityType, entityId);
|
return narrativeEntityContextBuilder.build(entityType, entityId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Charge le GameSystemContext si la campagne est liée à un GameSystem.
|
||||||
|
* L'entityType détermine quelles sections de règles sont injectées
|
||||||
|
* (SCENE → combat/PNJ, CHAPTER → combat/classes, ARC → lore/factions, autre → toutes).
|
||||||
|
* Retourne null en cas de GameSystem introuvable (dégradation gracieuse).
|
||||||
|
*/
|
||||||
|
private GameSystemContext loadGameSystemContextOrNull(Campaign campaign, String entityType) {
|
||||||
|
if (!campaign.isLinkedToGameSystem()) return null;
|
||||||
|
GenerationIntent intent = GenerationIntent.fromNarrativeEntityType(entityType);
|
||||||
|
return gameSystemContextBuilder.buildOptional(campaign.getGameSystemId(), intent).orElse(null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package com.loremind.application.generationcontext;
|
|||||||
|
|
||||||
import com.loremind.domain.generationcontext.ChatMessage;
|
import com.loremind.domain.generationcontext.ChatMessage;
|
||||||
import com.loremind.domain.generationcontext.ChatRequest;
|
import com.loremind.domain.generationcontext.ChatRequest;
|
||||||
|
import com.loremind.domain.generationcontext.ChatUsage;
|
||||||
import com.loremind.domain.generationcontext.LoreStructuralContext;
|
import com.loremind.domain.generationcontext.LoreStructuralContext;
|
||||||
import com.loremind.domain.generationcontext.PageContext;
|
import com.loremind.domain.generationcontext.PageContext;
|
||||||
import com.loremind.domain.generationcontext.ports.AiChatProvider;
|
import com.loremind.domain.generationcontext.ports.AiChatProvider;
|
||||||
@@ -60,6 +61,7 @@ public class StreamChatForLoreUseCase {
|
|||||||
String loreId,
|
String loreId,
|
||||||
String pageId,
|
String pageId,
|
||||||
List<ChatMessage> messages,
|
List<ChatMessage> messages,
|
||||||
|
Consumer<ChatUsage> onUsage,
|
||||||
Consumer<String> onToken,
|
Consumer<String> onToken,
|
||||||
Runnable onComplete,
|
Runnable onComplete,
|
||||||
Consumer<Throwable> onError) {
|
Consumer<Throwable> onError) {
|
||||||
@@ -75,7 +77,7 @@ public class StreamChatForLoreUseCase {
|
|||||||
.pageContext(pageContext)
|
.pageContext(pageContext)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
aiChatProvider.streamChat(request, onToken, onComplete, onError);
|
aiChatProvider.streamChat(request, onUsage, onToken, onComplete, onError);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -105,11 +107,6 @@ public class StreamChatForLoreUseCase {
|
|||||||
? page.getValues()
|
? page.getValues()
|
||||||
: Collections.emptyMap();
|
: Collections.emptyMap();
|
||||||
|
|
||||||
return PageContext.builder()
|
return new PageContext(page.getTitle(), templateName, templateFields, values);
|
||||||
.title(page.getTitle())
|
|
||||||
.templateName(templateName)
|
|
||||||
.templateFields(templateFields)
|
|
||||||
.values(values)
|
|
||||||
.build();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,13 @@
|
|||||||
package com.loremind.application.lorecontext;
|
package com.loremind.application.lorecontext;
|
||||||
|
|
||||||
import com.loremind.domain.lorecontext.LoreNode;
|
import com.loremind.domain.lorecontext.LoreNode;
|
||||||
|
import com.loremind.domain.lorecontext.Page;
|
||||||
import com.loremind.domain.lorecontext.ports.LoreNodeRepository;
|
import com.loremind.domain.lorecontext.ports.LoreNodeRepository;
|
||||||
|
import com.loremind.domain.lorecontext.ports.PageRepository;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@@ -16,11 +20,20 @@ import java.util.Optional;
|
|||||||
public class LoreNodeService {
|
public class LoreNodeService {
|
||||||
|
|
||||||
private final LoreNodeRepository loreNodeRepository;
|
private final LoreNodeRepository loreNodeRepository;
|
||||||
|
private final PageRepository pageRepository;
|
||||||
|
|
||||||
public LoreNodeService(LoreNodeRepository loreNodeRepository) {
|
public LoreNodeService(LoreNodeRepository loreNodeRepository, PageRepository pageRepository) {
|
||||||
this.loreNodeRepository = loreNodeRepository;
|
this.loreNodeRepository = loreNodeRepository;
|
||||||
|
this.pageRepository = pageRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compte des entités qui seront supprimées en cascade si le dossier est effacé :
|
||||||
|
* le dossier lui-même n'est pas compté, seuls les descendants (sous-dossiers
|
||||||
|
* récursifs + pages de l'ensemble du sous-arbre).
|
||||||
|
*/
|
||||||
|
public record DeletionImpact(int folders, int pages) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Crée un LoreNode (dossier) à partir d'un "objet changes" porteur des valeurs
|
* Crée un LoreNode (dossier) à partir d'un "objet changes" porteur des valeurs
|
||||||
* souhaitées (pattern Parameter Object) : évite les signatures qui gonflent
|
* souhaitées (pattern Parameter Object) : évite les signatures qui gonflent
|
||||||
@@ -68,7 +81,64 @@ public class LoreNodeService {
|
|||||||
return loreNodeRepository.save(existing);
|
return loreNodeRepository.save(existing);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calcule l'impact d'une suppression en cascade : nombre de sous-dossiers
|
||||||
|
* (récursif, sans compter la racine) et de pages dans l'ensemble du sous-arbre.
|
||||||
|
*/
|
||||||
|
public DeletionImpact getDeletionImpact(String id) {
|
||||||
|
List<LoreNode> descendants = collectDescendants(id);
|
||||||
|
int pageTotal = pageRepository.findByNodeId(id).size();
|
||||||
|
for (LoreNode descendant : descendants) {
|
||||||
|
pageTotal += pageRepository.findByNodeId(descendant.getId()).size();
|
||||||
|
}
|
||||||
|
return new DeletionImpact(descendants.size(), pageTotal);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Supprime le dossier et tout son sous-arbre (sous-dossiers récursifs + pages).
|
||||||
|
* Suppression en profondeur d'abord (feuilles → racine) pour limiter les
|
||||||
|
* références orphelines en cours de transaction. Les FKs applicatives n'ayant
|
||||||
|
* pas de CASCADE en DB, on orchestre la descente ici.
|
||||||
|
*/
|
||||||
|
@Transactional
|
||||||
public void deleteLoreNode(String id) {
|
public void deleteLoreNode(String id) {
|
||||||
|
List<LoreNode> descendants = collectDescendants(id);
|
||||||
|
// Descendants retournés en ordre BFS (haut → bas) : on inverse pour
|
||||||
|
// supprimer les feuilles en premier, puis on finit par la racine.
|
||||||
|
for (int i = descendants.size() - 1; i >= 0; i--) {
|
||||||
|
String descendantId = descendants.get(i).getId();
|
||||||
|
deletePagesOfNode(descendantId);
|
||||||
|
loreNodeRepository.deleteById(descendantId);
|
||||||
|
}
|
||||||
|
deletePagesOfNode(id);
|
||||||
loreNodeRepository.deleteById(id);
|
loreNodeRepository.deleteById(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void deletePagesOfNode(String nodeId) {
|
||||||
|
for (Page page : pageRepository.findByNodeId(nodeId)) {
|
||||||
|
pageRepository.deleteById(page.getId());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retourne tous les descendants (hors racine) d'un dossier, en ordre BFS.
|
||||||
|
* Parcours itératif pour éviter tout risque de débordement de pile sur
|
||||||
|
* une arborescence profonde malicieuse.
|
||||||
|
*/
|
||||||
|
private List<LoreNode> collectDescendants(String rootId) {
|
||||||
|
List<LoreNode> result = new ArrayList<>();
|
||||||
|
List<String> frontier = new ArrayList<>();
|
||||||
|
frontier.add(rootId);
|
||||||
|
while (!frontier.isEmpty()) {
|
||||||
|
List<String> nextFrontier = new ArrayList<>();
|
||||||
|
for (String parentId : frontier) {
|
||||||
|
for (LoreNode child : loreNodeRepository.findByParentId(parentId)) {
|
||||||
|
result.add(child);
|
||||||
|
nextFrontier.add(child.getId());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
frontier = nextFrontier;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,17 @@
|
|||||||
package com.loremind.application.lorecontext;
|
package com.loremind.application.lorecontext;
|
||||||
|
|
||||||
|
import com.loremind.domain.campaigncontext.Campaign;
|
||||||
|
import com.loremind.domain.campaigncontext.ports.CampaignRepository;
|
||||||
import com.loremind.domain.lorecontext.Lore;
|
import com.loremind.domain.lorecontext.Lore;
|
||||||
|
import com.loremind.domain.lorecontext.LoreNode;
|
||||||
|
import com.loremind.domain.lorecontext.Page;
|
||||||
|
import com.loremind.domain.lorecontext.Template;
|
||||||
import com.loremind.domain.lorecontext.ports.LoreNodeRepository;
|
import com.loremind.domain.lorecontext.ports.LoreNodeRepository;
|
||||||
import com.loremind.domain.lorecontext.ports.LoreRepository;
|
import com.loremind.domain.lorecontext.ports.LoreRepository;
|
||||||
import com.loremind.domain.lorecontext.ports.PageRepository;
|
import com.loremind.domain.lorecontext.ports.PageRepository;
|
||||||
|
import com.loremind.domain.lorecontext.ports.TemplateRepository;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
@@ -26,15 +33,28 @@ public class LoreService {
|
|||||||
private final LoreRepository loreRepository;
|
private final LoreRepository loreRepository;
|
||||||
private final LoreNodeRepository loreNodeRepository;
|
private final LoreNodeRepository loreNodeRepository;
|
||||||
private final PageRepository pageRepository;
|
private final PageRepository pageRepository;
|
||||||
|
private final TemplateRepository templateRepository;
|
||||||
|
private final CampaignRepository campaignRepository;
|
||||||
|
|
||||||
public LoreService(LoreRepository loreRepository,
|
public LoreService(LoreRepository loreRepository,
|
||||||
LoreNodeRepository loreNodeRepository,
|
LoreNodeRepository loreNodeRepository,
|
||||||
PageRepository pageRepository) {
|
PageRepository pageRepository,
|
||||||
|
TemplateRepository templateRepository,
|
||||||
|
CampaignRepository campaignRepository) {
|
||||||
this.loreRepository = loreRepository;
|
this.loreRepository = loreRepository;
|
||||||
this.loreNodeRepository = loreNodeRepository;
|
this.loreNodeRepository = loreNodeRepository;
|
||||||
this.pageRepository = pageRepository;
|
this.pageRepository = pageRepository;
|
||||||
|
this.templateRepository = templateRepository;
|
||||||
|
this.campaignRepository = campaignRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compte des entités qui seront supprimées / détachées en cascade si le Lore
|
||||||
|
* est effacé. `detachedCampaigns` : campagnes qui perdront leur référence à
|
||||||
|
* ce Lore (leur loreId sera nullé) mais resteront présentes.
|
||||||
|
*/
|
||||||
|
public record DeletionImpact(int folders, int pages, int templates, int detachedCampaigns) {}
|
||||||
|
|
||||||
public Lore createLore(String name, String description) {
|
public Lore createLore(String name, String description) {
|
||||||
Lore lore = Lore.builder()
|
Lore lore = Lore.builder()
|
||||||
.name(name)
|
.name(name)
|
||||||
@@ -83,7 +103,54 @@ public class LoreService {
|
|||||||
return loreRepository.save(lore);
|
return loreRepository.save(lore);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calcule l'impact d'une suppression de Lore en cascade : dossiers + pages
|
||||||
|
* + templates supprimés, et campagnes qui seront détachées (loreId → null
|
||||||
|
* sans être supprimées, car une campagne peut vivre sans univers).
|
||||||
|
*/
|
||||||
|
public DeletionImpact getDeletionImpact(String id) {
|
||||||
|
int folders = (int) loreNodeRepository.countByLoreId(id);
|
||||||
|
int pages = (int) pageRepository.countByLoreId(id);
|
||||||
|
int templates = templateRepository.findByLoreId(id).size();
|
||||||
|
int detached = countCampaignsReferencingLore(id);
|
||||||
|
return new DeletionImpact(folders, pages, templates, detached);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Supprime le Lore et toutes ses entités dépendantes (dossiers, pages, templates).
|
||||||
|
* Les campagnes qui référençaient ce Lore sont conservées — leur loreId est
|
||||||
|
* mis à null (une campagne peut légitimement exister sans univers associé).
|
||||||
|
* Opération transactionnelle : atomique.
|
||||||
|
*/
|
||||||
|
@Transactional
|
||||||
public void deleteLore(String id) {
|
public void deleteLore(String id) {
|
||||||
|
// Pages d'abord : elles référencent nodeId ET loreId, on les supprime
|
||||||
|
// globalement via loreId pour éviter d'en rater une rattachée à un
|
||||||
|
// node orphelin (ne devrait pas arriver, mais ceinture+bretelles).
|
||||||
|
for (Page page : pageRepository.findByLoreId(id)) {
|
||||||
|
pageRepository.deleteById(page.getId());
|
||||||
|
}
|
||||||
|
for (LoreNode node : loreNodeRepository.findByLoreId(id)) {
|
||||||
|
loreNodeRepository.deleteById(node.getId());
|
||||||
|
}
|
||||||
|
for (Template template : templateRepository.findByLoreId(id)) {
|
||||||
|
templateRepository.deleteById(template.getId());
|
||||||
|
}
|
||||||
|
// Détache les campagnes : on garde la campagne, on nulle juste la référence.
|
||||||
|
for (Campaign campaign : campaignRepository.findAll()) {
|
||||||
|
if (id.equals(campaign.getLoreId())) {
|
||||||
|
campaign.setLoreId(null);
|
||||||
|
campaignRepository.save(campaign);
|
||||||
|
}
|
||||||
|
}
|
||||||
loreRepository.deleteById(id);
|
loreRepository.deleteById(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private int countCampaignsReferencingLore(String id) {
|
||||||
|
int count = 0;
|
||||||
|
for (Campaign campaign : campaignRepository.findAll()) {
|
||||||
|
if (id.equals(campaign.getLoreId())) count++;
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,6 +21,9 @@ public class Arc {
|
|||||||
private String campaignId; // Référence vers la Campaign parente
|
private String campaignId; // Référence vers la Campaign parente
|
||||||
private int order; // Ordre de l'arc dans la campagne
|
private int order; // Ordre de l'arc dans la campagne
|
||||||
|
|
||||||
|
/** Cle d'icone choisie par l'utilisateur (cf. CAMPAIGN_ICON_OPTIONS cote front). */
|
||||||
|
private String icon;
|
||||||
|
|
||||||
// Champs narratifs enrichis (voir docs/maquettes/campagne/détail/)
|
// Champs narratifs enrichis (voir docs/maquettes/campagne/détail/)
|
||||||
private String themes; // Thèmes principaux explorés dans cet arc
|
private String themes; // Thèmes principaux explorés dans cet arc
|
||||||
private String stakes; // Enjeux globaux pour les personnages
|
private String stakes; // Enjeux globaux pour les personnages
|
||||||
|
|||||||
@@ -28,7 +28,18 @@ public class Campaign {
|
|||||||
*/
|
*/
|
||||||
private String loreId;
|
private String loreId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Référence faible (weak reference) vers un GameSystem.
|
||||||
|
* Nullable : une campagne peut être "générique" (pas de système de JDR déclaré).
|
||||||
|
* Weak reference pour respecter la séparation des Bounded Contexts.
|
||||||
|
*/
|
||||||
|
private String gameSystemId;
|
||||||
|
|
||||||
public boolean isLinkedToLore() {
|
public boolean isLinkedToLore() {
|
||||||
return this.loreId != null && !this.loreId.isBlank();
|
return this.loreId != null && !this.loreId.isBlank();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isLinkedToGameSystem() {
|
||||||
|
return this.gameSystemId != null && !this.gameSystemId.isBlank();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,6 +21,9 @@ public class Chapter {
|
|||||||
private String arcId; // Référence vers l'Arc parent
|
private String arcId; // Référence vers l'Arc parent
|
||||||
private int order; // Ordre du chapitre dans l'arc
|
private int order; // Ordre du chapitre dans l'arc
|
||||||
|
|
||||||
|
/** Cle d'icone choisie par l'utilisateur (cf. CAMPAIGN_ICON_OPTIONS cote front). */
|
||||||
|
private String icon;
|
||||||
|
|
||||||
// Champs narratifs enrichis (voir docs/maquettes/campagne/détail/)
|
// Champs narratifs enrichis (voir docs/maquettes/campagne/détail/)
|
||||||
private String gmNotes; // Notes privées du MJ (non exportées vers FoundryVTT)
|
private String gmNotes; // Notes privées du MJ (non exportées vers FoundryVTT)
|
||||||
private String playerObjectives; // Objectifs des joueurs dans ce chapitre
|
private String playerObjectives; // Objectifs des joueurs dans ce chapitre
|
||||||
|
|||||||
@@ -0,0 +1,40 @@
|
|||||||
|
package com.loremind.domain.campaigncontext;
|
||||||
|
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fiche de personnage joueur (PJ) d'une campagne.
|
||||||
|
* <p>
|
||||||
|
* MVP : contenu markdown libre, l'utilisateur met ce qu'il veut (stats,
|
||||||
|
* backstory, équipement). Évolution prévue vers un système templaté par
|
||||||
|
* GameSystem (la fiche Nimble n'a pas les mêmes champs qu'une fiche D&D).
|
||||||
|
* <p>
|
||||||
|
* Scope strict PJ : les PNJ restent dans le Lore (pages templatées) ou
|
||||||
|
* dans les scènes elles-mêmes. Si le besoin de PNJ spécifiques à une
|
||||||
|
* campagne remonte, on étendra l'entité (ex: type enum PJ/PNJ).
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
public class Character {
|
||||||
|
|
||||||
|
private String id;
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Contenu libre en markdown — stats + backstory + notes. Nullable à la création,
|
||||||
|
* renseigné progressivement par le MJ.
|
||||||
|
*/
|
||||||
|
private String markdownContent;
|
||||||
|
|
||||||
|
/** Référence vers la Campaign parente. */
|
||||||
|
private String campaignId;
|
||||||
|
|
||||||
|
/** Ordre d'affichage dans la liste des PJ de la campagne. */
|
||||||
|
private int order;
|
||||||
|
|
||||||
|
private LocalDateTime createdAt;
|
||||||
|
private LocalDateTime updatedAt;
|
||||||
|
}
|
||||||
@@ -21,6 +21,9 @@ public class Scene {
|
|||||||
private String chapterId; // Référence vers le Chapter parent
|
private String chapterId; // Référence vers le Chapter parent
|
||||||
private int order; // Ordre de la scène dans le chapitre
|
private int order; // Ordre de la scène dans le chapitre
|
||||||
|
|
||||||
|
/** Cle d'icone choisie par l'utilisateur (cf. CAMPAIGN_ICON_OPTIONS cote front). */
|
||||||
|
private String icon;
|
||||||
|
|
||||||
// === Contexte et ambiance ===
|
// === Contexte et ambiance ===
|
||||||
private String location; // Lieu de la scène (ex: Taverne du Dragon d'Or)
|
private String location; // Lieu de la scène (ex: Taverne du Dragon d'Or)
|
||||||
private String timing; // Moment (ex: Soir, à la tombée de la nuit)
|
private String timing; // Moment (ex: Soir, à la tombée de la nuit)
|
||||||
|
|||||||
@@ -1,31 +1,25 @@
|
|||||||
package com.loremind.domain.campaigncontext;
|
package com.loremind.domain.campaigncontext;
|
||||||
|
|
||||||
import lombok.Builder;
|
|
||||||
import lombok.Value;
|
|
||||||
import lombok.extern.jackson.Jacksonized;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Value Object représentant une "sortie" narrative depuis une Scene.
|
* Value Object représentant une "sortie" narrative depuis une Scene.
|
||||||
* Décrit un choix offert aux joueurs et la scène de destination associée.
|
* Décrit un choix offert aux joueurs et la scène de destination associée.
|
||||||
* <p>
|
* <p>
|
||||||
* Immuable (@Value) : pour "modifier" une branche on la remplace.
|
* Record Java : immuable par construction, sans aucune dépendance technique
|
||||||
* @Jacksonized : permet à Jackson (sérialisation JSON via le converter JPA)
|
* (pas de Lombok, pas de Jackson). Jackson 2.12+ sait sérialiser/désérialiser
|
||||||
* de reconstruire l'objet en passant par le builder malgré l'absence de setters.
|
* les records nativement via le constructeur canonique — c'est ce dont
|
||||||
|
* dépend le SceneBranchListJsonConverter pour le stockage JSONB.
|
||||||
* <p>
|
* <p>
|
||||||
* Règle métier : targetSceneId DOIT pointer vers une Scene du MÊME Chapter
|
* Règle métier : targetSceneId DOIT pointer vers une Scene du MÊME Chapter
|
||||||
* (validation portée par SceneService).
|
* (validation portée par SceneService).
|
||||||
|
*
|
||||||
|
* @param label Libellé du choix (ex: "Si les joueurs attaquent le garde").
|
||||||
|
* @param targetSceneId Id de la Scene de destination, intra-chapitre uniquement.
|
||||||
|
* @param condition Notes MJ privées sur la condition de déclenchement (optionnel).
|
||||||
*/
|
*/
|
||||||
@Value
|
public record SceneBranch(String label, String targetSceneId, String condition) {
|
||||||
@Builder
|
|
||||||
@Jacksonized
|
|
||||||
public class SceneBranch {
|
|
||||||
|
|
||||||
/** Libellé du choix (ex: "Si les joueurs attaquent le garde"). */
|
/** Raccourci pour construire une branche sans condition (cas le plus courant). */
|
||||||
String label;
|
public static SceneBranch of(String label, String targetSceneId) {
|
||||||
|
return new SceneBranch(label, targetSceneId, null);
|
||||||
/** Id de la Scene de destination, intra-chapitre uniquement. */
|
}
|
||||||
String targetSceneId;
|
|
||||||
|
|
||||||
/** Notes MJ privées sur la condition de déclenchement (optionnel). */
|
|
||||||
String condition;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package com.loremind.domain.campaigncontext.ports;
|
||||||
|
|
||||||
|
import com.loremind.domain.campaigncontext.Character;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Port de sortie pour la persistance des fiches de personnages (PJ).
|
||||||
|
*/
|
||||||
|
public interface CharacterRepository {
|
||||||
|
|
||||||
|
Character save(Character character);
|
||||||
|
|
||||||
|
Optional<Character> findById(String id);
|
||||||
|
|
||||||
|
List<Character> findByCampaignId(String campaignId);
|
||||||
|
|
||||||
|
void deleteById(String id);
|
||||||
|
|
||||||
|
boolean existsById(String id);
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
package com.loremind.domain.conversationcontext;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Agregat d'une conversation de chat IA persistee.
|
||||||
|
*
|
||||||
|
* Une conversation est ancree sur exactement un niveau de contexte :
|
||||||
|
* - un Lore (optionnellement une page precise)
|
||||||
|
* - une Campagne (optionnellement une entite narrative : arc/chapitre/scene)
|
||||||
|
*
|
||||||
|
* C'est cet ancrage qui permet au drawer de filtrer les conversations
|
||||||
|
* a afficher dans la sidebar selon l'ecran en cours.
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class Conversation {
|
||||||
|
|
||||||
|
private String id;
|
||||||
|
private String title;
|
||||||
|
private LocalDateTime createdAt;
|
||||||
|
private LocalDateTime updatedAt;
|
||||||
|
|
||||||
|
/** Un seul des deux est non-null. */
|
||||||
|
private String loreId;
|
||||||
|
private String campaignId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Type d'entite focus, null si la conversation est ancree au niveau
|
||||||
|
* Lore/Campagne racine (pas sur une page/scene precise).
|
||||||
|
* Valeurs : "page", "arc", "chapter", "scene", "character".
|
||||||
|
*/
|
||||||
|
private String entityType;
|
||||||
|
private String entityId;
|
||||||
|
|
||||||
|
@Builder.Default
|
||||||
|
private List<ConversationMessage> messages = new ArrayList<>();
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package com.loremind.domain.conversationcontext;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Un message persiste d'une conversation.
|
||||||
|
*
|
||||||
|
* Distinct de {@link com.loremind.domain.generationcontext.ChatMessage}
|
||||||
|
* qui reste un simple record role+content pour le streaming LLM. Ici
|
||||||
|
* on ajoute id et horodatage, necessaires pour l'affichage / l'ordre.
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class ConversationMessage {
|
||||||
|
|
||||||
|
private String id;
|
||||||
|
/** "user" | "assistant" | "system". */
|
||||||
|
private String role;
|
||||||
|
private String content;
|
||||||
|
private LocalDateTime createdAt;
|
||||||
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
package com.loremind.domain.conversationcontext.ports;
|
||||||
|
|
||||||
|
import com.loremind.domain.conversationcontext.Conversation;
|
||||||
|
import com.loremind.domain.conversationcontext.ConversationMessage;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Port de persistance des conversations de chat IA.
|
||||||
|
*
|
||||||
|
* Les methodes de lecture par contexte acceptent des filtres nullables :
|
||||||
|
* - `loreId` OU `campaignId` doit etre non-null (mais pas les deux)
|
||||||
|
* - `entityType` + `entityId` : soit tous les deux null (niveau racine),
|
||||||
|
* soit tous les deux non-null (niveau entite precise).
|
||||||
|
*/
|
||||||
|
public interface ConversationRepository {
|
||||||
|
|
||||||
|
Conversation save(Conversation conversation);
|
||||||
|
|
||||||
|
Optional<Conversation> findById(String id);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Liste les conversations filtrees par contexte strict, triees par
|
||||||
|
* updatedAt desc. Les messages ne sont PAS chargees (liste vide) pour
|
||||||
|
* garder la payload legere — la sidebar n'affiche que les titres.
|
||||||
|
*/
|
||||||
|
List<Conversation> findByContext(
|
||||||
|
String loreId,
|
||||||
|
String campaignId,
|
||||||
|
String entityType,
|
||||||
|
String entityId);
|
||||||
|
|
||||||
|
void deleteById(String id);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ajoute un message a une conversation existante. Met a jour updatedAt
|
||||||
|
* de la conversation parent. Renvoie le message persiste (avec id + ts).
|
||||||
|
*/
|
||||||
|
ConversationMessage appendMessage(String conversationId, ConversationMessage message);
|
||||||
|
|
||||||
|
/** Rename atomique — ne touche pas aux messages. */
|
||||||
|
void updateTitle(String conversationId, String title);
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package com.loremind.domain.conversationcontext.ports;
|
||||||
|
|
||||||
|
import com.loremind.domain.conversationcontext.ConversationMessage;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Port : generation d'un titre court a partir des premiers echanges d'une
|
||||||
|
* conversation. Implemente via un appel Brain /summarize/conversation-title.
|
||||||
|
*/
|
||||||
|
public interface ConversationTitleGenerator {
|
||||||
|
|
||||||
|
/** Renvoie un titre en francais (4-7 mots max). Jamais null ni vide. */
|
||||||
|
String generate(List<ConversationMessage> firstMessages);
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
package com.loremind.domain.gamesystemcontext;
|
||||||
|
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Entité de domaine représentant un GameSystem (système de JDR).
|
||||||
|
* <p>
|
||||||
|
* Porte les règles d'un système (D&D, Nimble, Pathfinder, homebrew...) sous forme
|
||||||
|
* d'un markdown monolithique structuré par titres H2. Les sections sont extraites
|
||||||
|
* à la volée lors de l'injection dans les prompts IA (cf. GameSystemContextSelector).
|
||||||
|
* <p>
|
||||||
|
* {@code author} et {@code isPublic} sont des champs pensés pour un futur marketplace
|
||||||
|
* de rulesets partagés — non exploités au MVP mais persistés dès maintenant pour
|
||||||
|
* éviter une migration ultérieure.
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
public class GameSystem {
|
||||||
|
|
||||||
|
private String id;
|
||||||
|
private String name;
|
||||||
|
private String description;
|
||||||
|
|
||||||
|
/** Markdown monolithique. Sections découpées par titres H2 (## Combat, ## Classes, etc.). */
|
||||||
|
private String rulesMarkdown;
|
||||||
|
|
||||||
|
/** Auteur déclaré — futur marketplace. Nullable. */
|
||||||
|
private String author;
|
||||||
|
|
||||||
|
/** Flag de partage — futur marketplace. False par défaut. */
|
||||||
|
private boolean isPublic;
|
||||||
|
|
||||||
|
private LocalDateTime createdAt;
|
||||||
|
private LocalDateTime updatedAt;
|
||||||
|
}
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
package com.loremind.domain.gamesystemcontext;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Intent de génération utilisé pour sélectionner les sections d'un GameSystem
|
||||||
|
* à injecter dans le prompt IA.
|
||||||
|
* <p>
|
||||||
|
* Chaque intent porte une liste d'alias (case-insensitive, comparaison par
|
||||||
|
* {@code contains}) utilisée pour matcher les titres H2 du markdown de règles.
|
||||||
|
* <p>
|
||||||
|
* MVP : mapping codé en dur. Évoluera vers un mapping configurable par
|
||||||
|
* l'utilisateur dans l'éditeur de GameSystem (futur marketplace).
|
||||||
|
*/
|
||||||
|
public enum GenerationIntent {
|
||||||
|
|
||||||
|
/** Scène (combat / rencontre) : règles de résolution + format de stat block. */
|
||||||
|
SCENE(Set.of("combat", "monstre", "monster")),
|
||||||
|
|
||||||
|
/** Chapitre (segment narratif) : règles de combat + archétypes pour PNJ. */
|
||||||
|
CHAPTER(Set.of("combat", "classe", "class")),
|
||||||
|
|
||||||
|
/** Arc (structure narrative longue) : pas de règles spécifiques — toutes. */
|
||||||
|
ARC(Set.of()),
|
||||||
|
|
||||||
|
/** Fallback : toutes les sections (intent inconnu). */
|
||||||
|
GENERIC(Set.of());
|
||||||
|
|
||||||
|
private final Set<String> sectionAliases;
|
||||||
|
|
||||||
|
GenerationIntent(Set<String> sectionAliases) {
|
||||||
|
this.sectionAliases = sectionAliases;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<String> getSectionAliases() {
|
||||||
|
return sectionAliases;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** True si l'intent veut toutes les sections (pas de filtre). */
|
||||||
|
public boolean matchesAllSections() {
|
||||||
|
return sectionAliases.isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mappe un entityType de NarrativeEntityContext ("arc"/"chapter"/"scene")
|
||||||
|
* vers l'intent correspondant. Tout le reste (null, inconnu) tombe sur GENERIC.
|
||||||
|
*/
|
||||||
|
public static GenerationIntent fromNarrativeEntityType(String entityType) {
|
||||||
|
if (entityType == null) return GENERIC;
|
||||||
|
return switch (entityType.toLowerCase()) {
|
||||||
|
case "scene" -> SCENE;
|
||||||
|
case "chapter" -> CHAPTER;
|
||||||
|
case "arc" -> ARC;
|
||||||
|
default -> GENERIC;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package com.loremind.domain.gamesystemcontext.ports;
|
||||||
|
|
||||||
|
import com.loremind.domain.gamesystemcontext.GameSystem;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Port de sortie pour la persistance des GameSystems.
|
||||||
|
*/
|
||||||
|
public interface GameSystemRepository {
|
||||||
|
|
||||||
|
GameSystem save(GameSystem gameSystem);
|
||||||
|
|
||||||
|
Optional<GameSystem> findById(String id);
|
||||||
|
|
||||||
|
List<GameSystem> findAll();
|
||||||
|
|
||||||
|
void deleteById(String id);
|
||||||
|
|
||||||
|
boolean existsById(String id);
|
||||||
|
|
||||||
|
List<GameSystem> searchByName(String query);
|
||||||
|
}
|
||||||
@@ -1,9 +1,5 @@
|
|||||||
package com.loremind.domain.generationcontext;
|
package com.loremind.domain.generationcontext;
|
||||||
|
|
||||||
import lombok.Builder;
|
|
||||||
import lombok.Singular;
|
|
||||||
import lombok.Value;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -22,55 +18,62 @@ import java.util.List;
|
|||||||
* <p>
|
* <p>
|
||||||
* La liste `arcs` préserve l'ordre narratif (tri sur `order` ascendant
|
* La liste `arcs` préserve l'ordre narratif (tri sur `order` ascendant
|
||||||
* fait par le use case côté application layer).
|
* fait par le use case côté application layer).
|
||||||
|
* <p>
|
||||||
|
* Record Java : pur domaine, aucune dépendance technique.
|
||||||
|
*
|
||||||
|
* @param characters Personnages joueurs (PJ) de la campagne. Vide si aucun.
|
||||||
*/
|
*/
|
||||||
@Value
|
public record CampaignStructuralContext(
|
||||||
@Builder
|
String campaignName,
|
||||||
public class CampaignStructuralContext {
|
String campaignDescription,
|
||||||
|
List<ArcSummary> arcs,
|
||||||
|
List<CharacterSummary> characters) {
|
||||||
|
|
||||||
String campaignName;
|
/**
|
||||||
String campaignDescription;
|
* Résumé d'un PJ : nom + snippet court du markdown.
|
||||||
@Singular List<ArcSummary> arcs;
|
* Pas le markdown complet pour maîtriser le coût token (chaque campagne
|
||||||
|
* peut avoir 4-6 PJ × potentiellement 1-2k tokens/fiche = trop lourd).
|
||||||
|
* La fiche complète n'est injectée que si le PJ est l'entité focus
|
||||||
|
* (via NarrativeEntityContext, entity_type="character").
|
||||||
|
*/
|
||||||
|
public record CharacterSummary(String name, String snippet) {
|
||||||
|
}
|
||||||
|
|
||||||
/** Résumé d'un arc : nom + description courte + ses chapitres. */
|
/**
|
||||||
@Value
|
* Résumé d'un arc : nom + description courte + ses chapitres.
|
||||||
@Builder
|
*
|
||||||
public static class ArcSummary {
|
* @param illustrationCount Nombre d'illustrations attachees a cet arc (pour hint dans le prompt IA).
|
||||||
String name;
|
*/
|
||||||
String description;
|
public record ArcSummary(
|
||||||
/** Nombre d'illustrations attachees a cet arc (pour hint dans le prompt IA). */
|
String name,
|
||||||
int illustrationCount;
|
String description,
|
||||||
@Singular List<ChapterSummary> chapters;
|
int illustrationCount,
|
||||||
|
List<ChapterSummary> chapters) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Résumé d'un chapitre : nom + description courte + ses scènes. */
|
/** Résumé d'un chapitre : nom + description courte + ses scènes. */
|
||||||
@Value
|
public record ChapterSummary(
|
||||||
@Builder
|
String name,
|
||||||
public static class ChapterSummary {
|
String description,
|
||||||
String name;
|
int illustrationCount,
|
||||||
String description;
|
List<SceneSummary> scenes) {
|
||||||
int illustrationCount;
|
|
||||||
@Singular List<SceneSummary> scenes;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Résumé d'une scène : nom + description courte + branches narratives. */
|
/** Résumé d'une scène : nom + description courte + branches narratives. */
|
||||||
@Value
|
public record SceneSummary(
|
||||||
@Builder
|
String name,
|
||||||
public static class SceneSummary {
|
String description,
|
||||||
String name;
|
int illustrationCount,
|
||||||
String description;
|
List<BranchHint> branches) {
|
||||||
int illustrationCount;
|
|
||||||
@Singular List<BranchHint> branches;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Indice d'une branche narrative vers une autre scène du même chapitre. */
|
/**
|
||||||
@Value
|
* Indice d'une branche narrative vers une autre scène du même chapitre.
|
||||||
@Builder
|
*
|
||||||
public static class BranchHint {
|
* @param label Libellé du choix joueur (ex: "Si les joueurs attaquent le garde").
|
||||||
/** Libellé du choix joueur (ex: "Si les joueurs attaquent le garde"). */
|
* @param targetSceneName Nom de la scène cible (résolu depuis targetSceneId côté builder).
|
||||||
String label;
|
* @param condition Condition MJ privée (optionnel).
|
||||||
/** Nom de la scène cible (résolu depuis targetSceneId côté builder). */
|
*/
|
||||||
String targetSceneName;
|
public record BranchHint(String label, String targetSceneName, String condition) {
|
||||||
/** Condition MJ privée (optionnel). */
|
|
||||||
String condition;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,5 @@
|
|||||||
package com.loremind.domain.generationcontext;
|
package com.loremind.domain.generationcontext;
|
||||||
|
|
||||||
import lombok.Builder;
|
|
||||||
import lombok.Value;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -21,22 +18,74 @@ import java.util.List;
|
|||||||
* Un chat Lore ne reçoit JAMAIS de campaignContext : un Lore ne voit pas
|
* Un chat Lore ne reçoit JAMAIS de campaignContext : un Lore ne voit pas
|
||||||
* ses campagnes (asymétrie métier : la campagne est l'emprunteur du Lore,
|
* ses campagnes (asymétrie métier : la campagne est l'emprunteur du Lore,
|
||||||
* pas l'inverse).
|
* pas l'inverse).
|
||||||
|
* <p>
|
||||||
|
* Record Java : pur domaine. Builder manuel fourni en raison des 6 champs
|
||||||
|
* dont 5 sont nullables — l'API fluide reste plus lisible aux call sites
|
||||||
|
* qu'un constructeur à 6 paramètres souvent à null.
|
||||||
|
*
|
||||||
|
* @param loreContext Optionnel : carte structurelle du Lore. Null si campagne non liée à un Lore.
|
||||||
|
* @param pageContext Optionnel : contexte d'une page précise en cours d'édition (chat Lore uniquement).
|
||||||
|
* @param campaignContext Optionnel : carte narrative d'une Campagne (chat Campagne uniquement).
|
||||||
|
* @param narrativeEntity Optionnel : entité narrative en cours d'édition (arc/chapter/scene).
|
||||||
|
* @param gameSystemContext Optionnel : règles du système de JDR de la campagne (filtrées par intent).
|
||||||
|
* Null si la campagne n'a pas de GameSystem associé. Campagne uniquement au MVP.
|
||||||
*/
|
*/
|
||||||
@Value
|
public record ChatRequest(
|
||||||
@Builder
|
List<ChatMessage> messages,
|
||||||
public class ChatRequest {
|
LoreStructuralContext loreContext,
|
||||||
|
PageContext pageContext,
|
||||||
|
CampaignStructuralContext campaignContext,
|
||||||
|
NarrativeEntityContext narrativeEntity,
|
||||||
|
GameSystemContext gameSystemContext) {
|
||||||
|
|
||||||
List<ChatMessage> messages;
|
public static Builder builder() {
|
||||||
|
return new Builder();
|
||||||
|
}
|
||||||
|
|
||||||
/** Optionnel : carte structurelle du Lore. Null si campagne non liée à un Lore. */
|
/** Builder fluide : permet d'omettre les contextes non pertinents. */
|
||||||
LoreStructuralContext loreContext;
|
public static final class Builder {
|
||||||
|
private List<ChatMessage> messages;
|
||||||
|
private LoreStructuralContext loreContext;
|
||||||
|
private PageContext pageContext;
|
||||||
|
private CampaignStructuralContext campaignContext;
|
||||||
|
private NarrativeEntityContext narrativeEntity;
|
||||||
|
private GameSystemContext gameSystemContext;
|
||||||
|
|
||||||
/** Optionnel : contexte d'une page précise en cours d'édition (chat Lore uniquement). */
|
private Builder() {}
|
||||||
PageContext pageContext;
|
|
||||||
|
|
||||||
/** Optionnel : carte narrative d'une Campagne (chat Campagne uniquement). */
|
public Builder messages(List<ChatMessage> messages) {
|
||||||
CampaignStructuralContext campaignContext;
|
this.messages = messages;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/** Optionnel : entité narrative en cours d'édition (arc/chapter/scene). */
|
public Builder loreContext(LoreStructuralContext loreContext) {
|
||||||
NarrativeEntityContext narrativeEntity;
|
this.loreContext = loreContext;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder pageContext(PageContext pageContext) {
|
||||||
|
this.pageContext = pageContext;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder campaignContext(CampaignStructuralContext campaignContext) {
|
||||||
|
this.campaignContext = campaignContext;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder narrativeEntity(NarrativeEntityContext narrativeEntity) {
|
||||||
|
this.narrativeEntity = narrativeEntity;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder gameSystemContext(GameSystemContext gameSystemContext) {
|
||||||
|
this.gameSystemContext = gameSystemContext;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ChatRequest build() {
|
||||||
|
return new ChatRequest(messages, loreContext, pageContext,
|
||||||
|
campaignContext, narrativeEntity, gameSystemContext);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package com.loremind.domain.generationcontext;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Instantané d'occupation de la fenêtre de contexte à l'instant t du chat.
|
||||||
|
* <p>
|
||||||
|
* Émis une fois par tour de chat (juste avant le streaming des tokens) pour
|
||||||
|
* alimenter la jauge de contexte côté frontend. Les unités sont des tokens
|
||||||
|
* (approximés via tiktoken côté Brain — ±10% vs le tokenizer réel du modèle).
|
||||||
|
*
|
||||||
|
* @param system tokens consommés par le system prompt (contextes Lore/campagne injectés)
|
||||||
|
* @param history tokens consommés par l'historique de la conversation (hors dernier message)
|
||||||
|
* @param current tokens du dernier message utilisateur en attente de réponse
|
||||||
|
* @param max taille maximale configurée de la fenêtre de contexte
|
||||||
|
*/
|
||||||
|
public record ChatUsage(int system, int history, int current, int max) {
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package com.loremind.domain.generationcontext;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Value Object représentant les règles de JDR injectées dans un prompt IA.
|
||||||
|
* <p>
|
||||||
|
* Contient uniquement les sections pertinentes pour l'intent de génération
|
||||||
|
* en cours (sélection effectuée par GameSystemContextBuilder). Les sections
|
||||||
|
* sont indexées par leur titre H2 original (ex : "Combat", "Classes").
|
||||||
|
*
|
||||||
|
* @param systemName Nom du système de JDR (ex : "Nimble", "D&D 5.1 SRD").
|
||||||
|
* @param systemDescription Description courte du système (nullable).
|
||||||
|
* @param sections Sections de règles pertinentes, indexées par titre H2.
|
||||||
|
* Vide si le GameSystem n'a aucune règle ou si aucune section ne matche l'intent.
|
||||||
|
*/
|
||||||
|
public record GameSystemContext(
|
||||||
|
String systemName,
|
||||||
|
String systemDescription,
|
||||||
|
Map<String, String> sections) {
|
||||||
|
}
|
||||||
@@ -1,8 +1,5 @@
|
|||||||
package com.loremind.domain.generationcontext;
|
package com.loremind.domain.generationcontext;
|
||||||
|
|
||||||
import lombok.Builder;
|
|
||||||
import lombok.Value;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -10,19 +7,16 @@ import java.util.List;
|
|||||||
* pour remplir une Page à partir d'un Template.
|
* pour remplir une Page à partir d'un Template.
|
||||||
* <p>
|
* <p>
|
||||||
* Équivalent Java du PageGenerationContext Python (brain/app/domain/models.py).
|
* Équivalent Java du PageGenerationContext Python (brain/app/domain/models.py).
|
||||||
* Entité pure du domaine : aucune dépendance technique.
|
* Record Java : pur domaine, aucune dépendance technique.
|
||||||
* <p>
|
*
|
||||||
* Immuable via @Value (Lombok) : pas de setters, tous les champs final.
|
* @param templateFields Champs à générer (clés attendues dans la réponse).
|
||||||
* C'est un DTO de domaine entrant dans le port AiProvider.
|
* @param folderName Nom du LoreNode parent (ex: "PNJ", "Lieux").
|
||||||
*/
|
*/
|
||||||
@Value
|
public record GenerationContext(
|
||||||
@Builder
|
String loreName,
|
||||||
public class GenerationContext {
|
String loreDescription,
|
||||||
|
String folderName,
|
||||||
String loreName;
|
String templateName,
|
||||||
String loreDescription;
|
List<String> templateFields,
|
||||||
String folderName; // Nom du LoreNode parent (ex: "PNJ", "Lieux")
|
String pageTitle) {
|
||||||
String templateName;
|
|
||||||
List<String> templateFields; // Champs à générer (clés attendues dans la réponse)
|
|
||||||
String pageTitle;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,5 @@
|
|||||||
package com.loremind.domain.generationcontext;
|
package com.loremind.domain.generationcontext;
|
||||||
|
|
||||||
import lombok.Builder;
|
|
||||||
import lombok.Singular;
|
|
||||||
import lombok.Value;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@@ -16,15 +12,14 @@ import java.util.Map;
|
|||||||
* <p>
|
* <p>
|
||||||
* La map `folders` est indexée par nom de dossier et mappe vers la liste
|
* La map `folders` est indexée par nom de dossier et mappe vers la liste
|
||||||
* des pages qu'il contient (liste vide autorisée pour les dossiers vides).
|
* des pages qu'il contient (liste vide autorisée pour les dossiers vides).
|
||||||
|
* <p>
|
||||||
|
* Record Java : pur domaine, aucune dépendance technique.
|
||||||
*/
|
*/
|
||||||
@Value
|
public record LoreStructuralContext(
|
||||||
@Builder
|
String loreName,
|
||||||
public class LoreStructuralContext {
|
String loreDescription,
|
||||||
|
Map<String, List<PageSummary>> folders,
|
||||||
String loreName;
|
List<String> tags) {
|
||||||
String loreDescription;
|
|
||||||
Map<String, List<PageSummary>> folders;
|
|
||||||
@Singular List<String> tags;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Résumé projeté d'une page pour l'IA.
|
* Résumé projeté d'une page pour l'IA.
|
||||||
@@ -40,13 +35,11 @@ public class LoreStructuralContext {
|
|||||||
* uniquement ce qui est partageable en narration — les secrets MJ
|
* uniquement ce qui est partageable en narration — les secrets MJ
|
||||||
* restent confinés à leur page d'édition).
|
* restent confinés à leur page d'édition).
|
||||||
*/
|
*/
|
||||||
@Value
|
public record PageSummary(
|
||||||
@Builder
|
String title,
|
||||||
public static class PageSummary {
|
String templateName,
|
||||||
String title;
|
Map<String, String> values,
|
||||||
String templateName;
|
List<String> tags,
|
||||||
Map<String, String> values;
|
List<String> relatedPageTitles) {
|
||||||
List<String> tags;
|
|
||||||
List<String> relatedPageTitles;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,5 @@
|
|||||||
package com.loremind.domain.generationcontext;
|
package com.loremind.domain.generationcontext;
|
||||||
|
|
||||||
import lombok.Builder;
|
|
||||||
import lombok.Value;
|
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -17,13 +14,11 @@ import java.util.Map;
|
|||||||
* `fields` associe le nom d'un champ (ex: "themes", "playerNarration")
|
* `fields` associe le nom d'un champ (ex: "themes", "playerNarration")
|
||||||
* à sa valeur actuelle (chaîne vide si non renseigné). Utiliser une
|
* à sa valeur actuelle (chaîne vide si non renseigné). Utiliser une
|
||||||
* LinkedHashMap à la construction pour un prompt lisible (ordre préservé).
|
* LinkedHashMap à la construction pour un prompt lisible (ordre préservé).
|
||||||
|
*
|
||||||
|
* @param entityType "arc", "chapter" ou "scene" — utilisé pour libeller le bloc du prompt.
|
||||||
*/
|
*/
|
||||||
@Value
|
public record NarrativeEntityContext(
|
||||||
@Builder
|
String entityType,
|
||||||
public class NarrativeEntityContext {
|
String title,
|
||||||
|
Map<String, String> fields) {
|
||||||
/** "arc", "chapter" ou "scene" — utilisé pour libeller le bloc du prompt. */
|
|
||||||
String entityType;
|
|
||||||
String title;
|
|
||||||
Map<String, String> fields;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,5 @@
|
|||||||
package com.loremind.domain.generationcontext;
|
package com.loremind.domain.generationcontext;
|
||||||
|
|
||||||
import lombok.Builder;
|
|
||||||
import lombok.Value;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@@ -14,14 +11,11 @@ import java.util.Map;
|
|||||||
* à l'IA de focaliser ses suggestions sur les bons champs sans déborder
|
* à l'IA de focaliser ses suggestions sur les bons champs sans déborder
|
||||||
* sur d'autres pages/templates.
|
* sur d'autres pages/templates.
|
||||||
* <p>
|
* <p>
|
||||||
* Object de valeur immuable, pur domaine — aucune dépendance technique.
|
* Record Java : immuable, pur domaine, aucune dépendance technique.
|
||||||
*/
|
*/
|
||||||
@Value
|
public record PageContext(
|
||||||
@Builder
|
String title,
|
||||||
public class PageContext {
|
String templateName,
|
||||||
|
List<String> templateFields,
|
||||||
String title;
|
Map<String, String> values) {
|
||||||
String templateName;
|
|
||||||
List<String> templateFields;
|
|
||||||
Map<String, String> values;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.loremind.domain.generationcontext.ports;
|
package com.loremind.domain.generationcontext.ports;
|
||||||
|
|
||||||
import com.loremind.domain.generationcontext.ChatRequest;
|
import com.loremind.domain.generationcontext.ChatRequest;
|
||||||
|
import com.loremind.domain.generationcontext.ChatUsage;
|
||||||
|
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
@@ -26,6 +27,10 @@ public interface AiChatProvider {
|
|||||||
* HTTP côté controller SSE).
|
* HTTP côté controller SSE).
|
||||||
*
|
*
|
||||||
* @param request messages + contexte Lore
|
* @param request messages + contexte Lore
|
||||||
|
* @param onUsage invoqué une fois au début du stream avec le bilan
|
||||||
|
* d'occupation de la fenêtre de contexte (tokens system /
|
||||||
|
* history / current / max). Peut ne jamais être invoqué
|
||||||
|
* si le provider ne supporte pas le comptage.
|
||||||
* @param onToken invoqué à chaque token reçu du LLM (peut être appelé
|
* @param onToken invoqué à chaque token reçu du LLM (peut être appelé
|
||||||
* de nombreuses fois)
|
* de nombreuses fois)
|
||||||
* @param onComplete invoqué une fois le stream terminé avec succès
|
* @param onComplete invoqué une fois le stream terminé avec succès
|
||||||
@@ -34,6 +39,7 @@ public interface AiChatProvider {
|
|||||||
*/
|
*/
|
||||||
void streamChat(
|
void streamChat(
|
||||||
ChatRequest request,
|
ChatRequest request,
|
||||||
|
Consumer<ChatUsage> onUsage,
|
||||||
Consumer<String> onToken,
|
Consumer<String> onToken,
|
||||||
Runnable onComplete,
|
Runnable onComplete,
|
||||||
Consumer<Throwable> onError
|
Consumer<Throwable> onError
|
||||||
|
|||||||
@@ -1,16 +1,7 @@
|
|||||||
package com.loremind.infrastructure.ai;
|
package com.loremind.infrastructure.ai;
|
||||||
|
|
||||||
import com.loremind.domain.generationcontext.CampaignStructuralContext;
|
|
||||||
import com.loremind.domain.generationcontext.CampaignStructuralContext.ArcSummary;
|
|
||||||
import com.loremind.domain.generationcontext.CampaignStructuralContext.BranchHint;
|
|
||||||
import com.loremind.domain.generationcontext.CampaignStructuralContext.ChapterSummary;
|
|
||||||
import com.loremind.domain.generationcontext.CampaignStructuralContext.SceneSummary;
|
|
||||||
import com.loremind.domain.generationcontext.ChatMessage;
|
|
||||||
import com.loremind.domain.generationcontext.ChatRequest;
|
import com.loremind.domain.generationcontext.ChatRequest;
|
||||||
import com.loremind.domain.generationcontext.LoreStructuralContext;
|
import com.loremind.domain.generationcontext.ChatUsage;
|
||||||
import com.loremind.domain.generationcontext.LoreStructuralContext.PageSummary;
|
|
||||||
import com.loremind.domain.generationcontext.NarrativeEntityContext;
|
|
||||||
import com.loremind.domain.generationcontext.PageContext;
|
|
||||||
import com.loremind.domain.generationcontext.ports.AiChatProvider;
|
import com.loremind.domain.generationcontext.ports.AiChatProvider;
|
||||||
import com.loremind.domain.generationcontext.ports.AiProviderException;
|
import com.loremind.domain.generationcontext.ports.AiProviderException;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
@@ -22,25 +13,21 @@ import org.springframework.web.reactive.function.client.WebClient;
|
|||||||
import reactor.core.publisher.Flux;
|
import reactor.core.publisher.Flux;
|
||||||
|
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.util.LinkedHashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adapter de sortie (Architecture Hexagonale) : implémente AiChatProvider
|
* Adapter de sortie (Architecture Hexagonale) : implémente AiChatProvider
|
||||||
* en appelant le Brain Python via WebClient + SSE (Server-Sent Events).
|
* en appelant le Brain Python via WebClient + SSE (Server-Sent Events).
|
||||||
* <p>
|
* <p>
|
||||||
* Responsabilités :
|
* Responsabilités (après extraction) :
|
||||||
* 1. Traduire ChatRequest (domaine) -> JSON attendu par /chat/stream.
|
* 1. Transport HTTP + consommation du flux SSE.
|
||||||
* Sérialise lore_context, page_context, campaign_context et
|
* 2. Dispatch des évènements SSE (data / done / error / usage).
|
||||||
* narrative_entity de façon conditionnelle selon le scénario d'appel
|
* 3. Traduction des erreurs techniques en AiProviderException.
|
||||||
* (chat Lore / chat Lore focalisé page / chat Campagne / chat Campagne
|
* <p>
|
||||||
* focalisé arc-chapter-scene).
|
* Les responsabilités auxiliaires sont déléguées :
|
||||||
* 2. Consommer le flux SSE token par token.
|
* - Construction du payload JSON : {@link BrainChatPayloadBuilder}.
|
||||||
* 3. Invoquer onToken / onComplete / onError au bon moment.
|
* - Parsing des payloads SSE : {@link BrainSseParser}.
|
||||||
* 4. Traduire toute erreur technique en AiProviderException.
|
|
||||||
* <p>
|
* <p>
|
||||||
* Le domaine ne voit JAMAIS WebClient, Flux, ni la moindre URL.
|
* Le domaine ne voit JAMAIS WebClient, Flux, ni la moindre URL.
|
||||||
*/
|
*/
|
||||||
@@ -52,21 +39,28 @@ public class BrainAiChatClient implements AiChatProvider {
|
|||||||
new ParameterizedTypeReference<>() {};
|
new ParameterizedTypeReference<>() {};
|
||||||
|
|
||||||
private final WebClient webClient;
|
private final WebClient webClient;
|
||||||
|
private final BrainChatPayloadBuilder payloadBuilder;
|
||||||
|
private final BrainSseParser sseParser;
|
||||||
|
|
||||||
public BrainAiChatClient(
|
public BrainAiChatClient(
|
||||||
WebClient.Builder builder,
|
WebClient.Builder builder,
|
||||||
@Value("${brain.base-url}") String baseUrl) {
|
@Value("${brain.base-url}") String baseUrl,
|
||||||
|
BrainChatPayloadBuilder payloadBuilder,
|
||||||
|
BrainSseParser sseParser) {
|
||||||
this.webClient = builder.baseUrl(baseUrl).build();
|
this.webClient = builder.baseUrl(baseUrl).build();
|
||||||
|
this.payloadBuilder = payloadBuilder;
|
||||||
|
this.sseParser = sseParser;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void streamChat(
|
public void streamChat(
|
||||||
ChatRequest request,
|
ChatRequest request,
|
||||||
|
Consumer<ChatUsage> onUsage,
|
||||||
Consumer<String> onToken,
|
Consumer<String> onToken,
|
||||||
Runnable onComplete,
|
Runnable onComplete,
|
||||||
Consumer<Throwable> onError) {
|
Consumer<Throwable> onError) {
|
||||||
|
|
||||||
Map<String, Object> payload = toPayload(request);
|
Map<String, Object> payload = payloadBuilder.build(request);
|
||||||
|
|
||||||
Flux<ServerSentEvent<String>> flux = webClient.post()
|
Flux<ServerSentEvent<String>> flux = webClient.post()
|
||||||
.uri(CHAT_STREAM_PATH)
|
.uri(CHAT_STREAM_PATH)
|
||||||
@@ -81,7 +75,7 @@ public class BrainAiChatClient implements AiChatProvider {
|
|||||||
// au contrat synchrone du port. L'appelant choisit le thread.
|
// au contrat synchrone du port. L'appelant choisit le thread.
|
||||||
flux
|
flux
|
||||||
.timeout(Duration.ofSeconds(120))
|
.timeout(Duration.ofSeconds(120))
|
||||||
.doOnNext(sse -> handleEvent(sse, onToken, onError))
|
.doOnNext(sse -> handleEvent(sse, onUsage, onToken, onError))
|
||||||
.blockLast();
|
.blockLast();
|
||||||
onComplete.run();
|
onComplete.run();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@@ -90,12 +84,13 @@ public class BrainAiChatClient implements AiChatProvider {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Dispatch selon le type d'événement SSE (data par défaut, done, error). */
|
/** Dispatch selon le type d'évènement SSE (data par défaut, done, error, usage). */
|
||||||
private void handleEvent(
|
private void handleEvent(
|
||||||
ServerSentEvent<String> sse,
|
ServerSentEvent<String> sse,
|
||||||
|
Consumer<ChatUsage> onUsage,
|
||||||
Consumer<String> onToken,
|
Consumer<String> onToken,
|
||||||
Consumer<Throwable> onError) {
|
Consumer<Throwable> onError) {
|
||||||
String event = sse.event(); // null si pas d'event: xxx -> c'est un data par défaut
|
String event = sse.event(); // null si pas d'event: xxx -> data par défaut
|
||||||
String data = sse.data();
|
String data = sse.data();
|
||||||
|
|
||||||
if ("error".equals(event)) {
|
if ("error".equals(event)) {
|
||||||
@@ -104,197 +99,17 @@ public class BrainAiChatClient implements AiChatProvider {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if ("done".equals(event)) {
|
if ("done".equals(event)) {
|
||||||
return; // la fin est gérée par blockLast + onComplete
|
return; // fin gérée par blockLast + onComplete
|
||||||
}
|
}
|
||||||
// Défaut : événement data avec JSON {"token":"..."}.
|
if ("usage".equals(event)) {
|
||||||
String token = extractToken(data);
|
ChatUsage usage = sseParser.parseUsage(data);
|
||||||
|
if (usage != null) onUsage.accept(usage);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// Défaut : évènement data avec JSON {"token":"..."}.
|
||||||
|
String token = sseParser.parseToken(data);
|
||||||
if (token != null && !token.isEmpty()) {
|
if (token != null && !token.isEmpty()) {
|
||||||
onToken.accept(token);
|
onToken.accept(token);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Parse minimaliste du JSON {"token":"..."} sans pull Jackson ici.
|
|
||||||
* Si le format se complexifie, on remplacera par un DTO Jackson.
|
|
||||||
*/
|
|
||||||
private String extractToken(String json) {
|
|
||||||
if (json == null) return null;
|
|
||||||
int idx = json.indexOf("\"token\"");
|
|
||||||
if (idx < 0) return null;
|
|
||||||
int colon = json.indexOf(':', idx);
|
|
||||||
int firstQuote = json.indexOf('"', colon + 1);
|
|
||||||
int lastQuote = json.lastIndexOf('"');
|
|
||||||
if (firstQuote < 0 || lastQuote <= firstQuote) return null;
|
|
||||||
return json.substring(firstQuote + 1, lastQuote)
|
|
||||||
.replace("\\n", "\n")
|
|
||||||
.replace("\\\"", "\"")
|
|
||||||
.replace("\\\\", "\\");
|
|
||||||
}
|
|
||||||
|
|
||||||
// --- Construction du payload JSON vers le Brain -------------------------
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Construit le payload JSON. Chaque contexte optionnel est omis s'il est
|
|
||||||
* null, pour s'aligner sur le schéma Pydantic côté Brain (champs
|
|
||||||
* Optional qui restent absents du dict transmis au LLM).
|
|
||||||
*/
|
|
||||||
private Map<String, Object> toPayload(ChatRequest request) {
|
|
||||||
Map<String, Object> root = new LinkedHashMap<>();
|
|
||||||
root.put("messages", request.getMessages().stream()
|
|
||||||
.map(this::messageToMap)
|
|
||||||
.collect(Collectors.toList()));
|
|
||||||
|
|
||||||
if (request.getLoreContext() != null) {
|
|
||||||
root.put("lore_context", loreContextToMap(request.getLoreContext()));
|
|
||||||
}
|
|
||||||
if (request.getPageContext() != null) {
|
|
||||||
root.put("page_context", pageContextToMap(request.getPageContext()));
|
|
||||||
}
|
|
||||||
if (request.getCampaignContext() != null) {
|
|
||||||
root.put("campaign_context", campaignContextToMap(request.getCampaignContext()));
|
|
||||||
}
|
|
||||||
if (request.getNarrativeEntity() != null) {
|
|
||||||
root.put("narrative_entity", narrativeEntityToMap(request.getNarrativeEntity()));
|
|
||||||
}
|
|
||||||
return root;
|
|
||||||
}
|
|
||||||
|
|
||||||
private Map<String, Object> messageToMap(ChatMessage m) {
|
|
||||||
Map<String, Object> map = new LinkedHashMap<>();
|
|
||||||
map.put("role", m.role());
|
|
||||||
map.put("content", m.content());
|
|
||||||
return map;
|
|
||||||
}
|
|
||||||
|
|
||||||
private Map<String, Object> loreContextToMap(LoreStructuralContext ctx) {
|
|
||||||
Map<String, Object> map = new LinkedHashMap<>();
|
|
||||||
map.put("lore_name", ctx.getLoreName());
|
|
||||||
map.put("lore_description", ctx.getLoreDescription());
|
|
||||||
|
|
||||||
Map<String, Object> foldersMap = new LinkedHashMap<>();
|
|
||||||
for (Map.Entry<String, List<PageSummary>> e : ctx.getFolders().entrySet()) {
|
|
||||||
foldersMap.put(e.getKey(), e.getValue().stream()
|
|
||||||
.map(this::pageSummaryToMap)
|
|
||||||
.collect(Collectors.toList()));
|
|
||||||
}
|
|
||||||
map.put("folders", foldersMap);
|
|
||||||
map.put("tags", ctx.getTags());
|
|
||||||
return map;
|
|
||||||
}
|
|
||||||
|
|
||||||
private Map<String, Object> pageSummaryToMap(PageSummary ps) {
|
|
||||||
Map<String, Object> map = new LinkedHashMap<>();
|
|
||||||
map.put("title", ps.getTitle());
|
|
||||||
map.put("template_name", ps.getTemplateName());
|
|
||||||
// values/tags/related_page_titles ne sont sérialisés que s'ils contiennent
|
|
||||||
// de l'info — payload réseau plus léger quand la page est vierge.
|
|
||||||
if (ps.getValues() != null && !ps.getValues().isEmpty()) {
|
|
||||||
map.put("values", ps.getValues());
|
|
||||||
}
|
|
||||||
if (ps.getTags() != null && !ps.getTags().isEmpty()) {
|
|
||||||
map.put("tags", ps.getTags());
|
|
||||||
}
|
|
||||||
if (ps.getRelatedPageTitles() != null && !ps.getRelatedPageTitles().isEmpty()) {
|
|
||||||
map.put("related_page_titles", ps.getRelatedPageTitles());
|
|
||||||
}
|
|
||||||
return map;
|
|
||||||
}
|
|
||||||
|
|
||||||
private Map<String, Object> pageContextToMap(PageContext pc) {
|
|
||||||
Map<String, Object> map = new LinkedHashMap<>();
|
|
||||||
map.put("title", pc.getTitle());
|
|
||||||
map.put("template_name", pc.getTemplateName());
|
|
||||||
map.put("template_fields", pc.getTemplateFields());
|
|
||||||
map.put("values", pc.getValues());
|
|
||||||
return map;
|
|
||||||
}
|
|
||||||
|
|
||||||
private Map<String, Object> campaignContextToMap(CampaignStructuralContext ctx) {
|
|
||||||
Map<String, Object> map = new LinkedHashMap<>();
|
|
||||||
map.put("campaign_name", ctx.getCampaignName());
|
|
||||||
map.put("campaign_description", ctx.getCampaignDescription());
|
|
||||||
map.put("arcs", ctx.getArcs().stream()
|
|
||||||
.map(this::arcSummaryToMap)
|
|
||||||
.collect(Collectors.toList()));
|
|
||||||
return map;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Helper generic pour serialiser les entites structurelles (Arc/Chapter/Scene)
|
|
||||||
* avec name, description et illustration_count conditionnel.
|
|
||||||
*/
|
|
||||||
private <T> Map<String, Object> structuralSummaryToMap(
|
|
||||||
T entity,
|
|
||||||
java.util.function.Function<T, String> nameExtractor,
|
|
||||||
java.util.function.Function<T, String> descriptionExtractor,
|
|
||||||
java.util.function.Function<T, Integer> illustrationCountExtractor,
|
|
||||||
java.util.function.BiConsumer<Map<String, Object>, T> childSerializer) {
|
|
||||||
Map<String, Object> map = new LinkedHashMap<>();
|
|
||||||
map.put("name", nameExtractor.apply(entity));
|
|
||||||
map.put("description", descriptionExtractor.apply(entity));
|
|
||||||
// Envoye au Python pour enrichir le prompt ("N illustrations attachees").
|
|
||||||
// Serialise uniquement si > 0 pour economiser le payload sur les entites sans images.
|
|
||||||
if (illustrationCountExtractor.apply(entity) > 0) {
|
|
||||||
map.put("illustration_count", illustrationCountExtractor.apply(entity));
|
|
||||||
}
|
|
||||||
childSerializer.accept(map, entity);
|
|
||||||
return map;
|
|
||||||
}
|
|
||||||
|
|
||||||
private Map<String, Object> arcSummaryToMap(ArcSummary a) {
|
|
||||||
return structuralSummaryToMap(
|
|
||||||
a,
|
|
||||||
ArcSummary::getName,
|
|
||||||
ArcSummary::getDescription,
|
|
||||||
ArcSummary::getIllustrationCount,
|
|
||||||
(map, arc) -> map.put("chapters", arc.getChapters().stream()
|
|
||||||
.map(this::chapterSummaryToMap)
|
|
||||||
.collect(Collectors.toList())));
|
|
||||||
}
|
|
||||||
|
|
||||||
private Map<String, Object> chapterSummaryToMap(ChapterSummary c) {
|
|
||||||
return structuralSummaryToMap(
|
|
||||||
c,
|
|
||||||
ChapterSummary::getName,
|
|
||||||
ChapterSummary::getDescription,
|
|
||||||
ChapterSummary::getIllustrationCount,
|
|
||||||
(map, chapter) -> map.put("scenes", chapter.getScenes().stream()
|
|
||||||
.map(this::sceneSummaryToMap)
|
|
||||||
.collect(Collectors.toList())));
|
|
||||||
}
|
|
||||||
|
|
||||||
private Map<String, Object> sceneSummaryToMap(SceneSummary s) {
|
|
||||||
return structuralSummaryToMap(
|
|
||||||
s,
|
|
||||||
SceneSummary::getName,
|
|
||||||
SceneSummary::getDescription,
|
|
||||||
SceneSummary::getIllustrationCount,
|
|
||||||
(map, scene) -> {
|
|
||||||
// Branches narratives : serialise uniquement si presentes, pour garder
|
|
||||||
// un payload leger sur les scenes lineaires classiques.
|
|
||||||
if (s.getBranches() != null && !s.getBranches().isEmpty()) {
|
|
||||||
map.put("branches", s.getBranches().stream()
|
|
||||||
.map(this::branchHintToMap)
|
|
||||||
.collect(Collectors.toList()));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private Map<String, Object> branchHintToMap(BranchHint b) {
|
|
||||||
Map<String, Object> map = new LinkedHashMap<>();
|
|
||||||
map.put("label", b.getLabel());
|
|
||||||
map.put("target_scene_name", b.getTargetSceneName());
|
|
||||||
if (b.getCondition() != null && !b.getCondition().isBlank()) {
|
|
||||||
map.put("condition", b.getCondition());
|
|
||||||
}
|
|
||||||
return map;
|
|
||||||
}
|
|
||||||
|
|
||||||
private Map<String, Object> narrativeEntityToMap(NarrativeEntityContext ne) {
|
|
||||||
Map<String, Object> map = new LinkedHashMap<>();
|
|
||||||
map.put("entity_type", ne.getEntityType());
|
|
||||||
map.put("title", ne.getTitle());
|
|
||||||
map.put("fields", ne.getFields());
|
|
||||||
return map;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -53,12 +53,12 @@ public class BrainAiClient implements AiProvider {
|
|||||||
|
|
||||||
private BrainGeneratePageRequest toBrainRequest(GenerationContext context) {
|
private BrainGeneratePageRequest toBrainRequest(GenerationContext context) {
|
||||||
return new BrainGeneratePageRequest(
|
return new BrainGeneratePageRequest(
|
||||||
context.getLoreName(),
|
context.loreName(),
|
||||||
context.getLoreDescription(),
|
context.loreDescription(),
|
||||||
context.getFolderName(),
|
context.folderName(),
|
||||||
context.getTemplateName(),
|
context.templateName(),
|
||||||
context.getTemplateFields(),
|
context.templateFields(),
|
||||||
context.getPageTitle()
|
context.pageTitle()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,222 @@
|
|||||||
|
package com.loremind.infrastructure.ai;
|
||||||
|
|
||||||
|
import com.loremind.domain.generationcontext.CampaignStructuralContext;
|
||||||
|
import com.loremind.domain.generationcontext.CampaignStructuralContext.ArcSummary;
|
||||||
|
import com.loremind.domain.generationcontext.CampaignStructuralContext.BranchHint;
|
||||||
|
import com.loremind.domain.generationcontext.CampaignStructuralContext.ChapterSummary;
|
||||||
|
import com.loremind.domain.generationcontext.CampaignStructuralContext.CharacterSummary;
|
||||||
|
import com.loremind.domain.generationcontext.CampaignStructuralContext.SceneSummary;
|
||||||
|
import com.loremind.domain.generationcontext.ChatMessage;
|
||||||
|
import com.loremind.domain.generationcontext.ChatRequest;
|
||||||
|
import com.loremind.domain.generationcontext.GameSystemContext;
|
||||||
|
import com.loremind.domain.generationcontext.LoreStructuralContext;
|
||||||
|
import com.loremind.domain.generationcontext.LoreStructuralContext.PageSummary;
|
||||||
|
import com.loremind.domain.generationcontext.NarrativeEntityContext;
|
||||||
|
import com.loremind.domain.generationcontext.PageContext;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.function.BiConsumer;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper d'infrastructure : traduit un ChatRequest (domaine) vers le dict JSON
|
||||||
|
* attendu par le Brain Python (/chat/stream).
|
||||||
|
* <p>
|
||||||
|
* Extrait de BrainAiChatClient pour isoler la responsabilité "sérialisation
|
||||||
|
* de payload" (SRP) — le client HTTP se concentre désormais uniquement sur le
|
||||||
|
* transport et le streaming SSE.
|
||||||
|
* <p>
|
||||||
|
* Chaque contexte optionnel (lore, page, campaign, entité narrative) est omis
|
||||||
|
* si null, pour s'aligner sur le schéma Pydantic (champs Optional absents).
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class BrainChatPayloadBuilder {
|
||||||
|
|
||||||
|
public Map<String, Object> build(ChatRequest request) {
|
||||||
|
Map<String, Object> root = new LinkedHashMap<>();
|
||||||
|
root.put("messages", request.messages().stream()
|
||||||
|
.map(this::messageToMap)
|
||||||
|
.collect(Collectors.toList()));
|
||||||
|
|
||||||
|
if (request.loreContext() != null) {
|
||||||
|
root.put("lore_context", loreContextToMap(request.loreContext()));
|
||||||
|
}
|
||||||
|
if (request.pageContext() != null) {
|
||||||
|
root.put("page_context", pageContextToMap(request.pageContext()));
|
||||||
|
}
|
||||||
|
if (request.campaignContext() != null) {
|
||||||
|
root.put("campaign_context", campaignContextToMap(request.campaignContext()));
|
||||||
|
}
|
||||||
|
if (request.narrativeEntity() != null) {
|
||||||
|
root.put("narrative_entity", narrativeEntityToMap(request.narrativeEntity()));
|
||||||
|
}
|
||||||
|
if (request.gameSystemContext() != null) {
|
||||||
|
root.put("game_system_context", gameSystemContextToMap(request.gameSystemContext()));
|
||||||
|
}
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String, Object> gameSystemContextToMap(GameSystemContext gs) {
|
||||||
|
Map<String, Object> map = new LinkedHashMap<>();
|
||||||
|
map.put("system_name", gs.systemName());
|
||||||
|
if (gs.systemDescription() != null && !gs.systemDescription().isBlank()) {
|
||||||
|
map.put("system_description", gs.systemDescription());
|
||||||
|
}
|
||||||
|
map.put("sections", gs.sections() != null ? gs.sections() : Map.of());
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String, Object> messageToMap(ChatMessage m) {
|
||||||
|
Map<String, Object> map = new LinkedHashMap<>();
|
||||||
|
map.put("role", m.role());
|
||||||
|
map.put("content", m.content());
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String, Object> loreContextToMap(LoreStructuralContext ctx) {
|
||||||
|
Map<String, Object> map = new LinkedHashMap<>();
|
||||||
|
map.put("lore_name", ctx.loreName());
|
||||||
|
map.put("lore_description", ctx.loreDescription());
|
||||||
|
|
||||||
|
Map<String, Object> foldersMap = new LinkedHashMap<>();
|
||||||
|
for (Map.Entry<String, List<PageSummary>> e : ctx.folders().entrySet()) {
|
||||||
|
foldersMap.put(e.getKey(), e.getValue().stream()
|
||||||
|
.map(this::pageSummaryToMap)
|
||||||
|
.collect(Collectors.toList()));
|
||||||
|
}
|
||||||
|
map.put("folders", foldersMap);
|
||||||
|
map.put("tags", ctx.tags());
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String, Object> pageSummaryToMap(PageSummary ps) {
|
||||||
|
Map<String, Object> map = new LinkedHashMap<>();
|
||||||
|
map.put("title", ps.title());
|
||||||
|
map.put("template_name", ps.templateName());
|
||||||
|
// values/tags/related_page_titles : omis si vides pour alléger le payload.
|
||||||
|
if (ps.values() != null && !ps.values().isEmpty()) {
|
||||||
|
map.put("values", ps.values());
|
||||||
|
}
|
||||||
|
if (ps.tags() != null && !ps.tags().isEmpty()) {
|
||||||
|
map.put("tags", ps.tags());
|
||||||
|
}
|
||||||
|
if (ps.relatedPageTitles() != null && !ps.relatedPageTitles().isEmpty()) {
|
||||||
|
map.put("related_page_titles", ps.relatedPageTitles());
|
||||||
|
}
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String, Object> pageContextToMap(PageContext pc) {
|
||||||
|
Map<String, Object> map = new LinkedHashMap<>();
|
||||||
|
map.put("title", pc.title());
|
||||||
|
map.put("template_name", pc.templateName());
|
||||||
|
map.put("template_fields", pc.templateFields());
|
||||||
|
map.put("values", pc.values());
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String, Object> campaignContextToMap(CampaignStructuralContext ctx) {
|
||||||
|
Map<String, Object> map = new LinkedHashMap<>();
|
||||||
|
map.put("campaign_name", ctx.campaignName());
|
||||||
|
map.put("campaign_description", ctx.campaignDescription());
|
||||||
|
map.put("arcs", ctx.arcs().stream()
|
||||||
|
.map(this::arcSummaryToMap)
|
||||||
|
.collect(Collectors.toList()));
|
||||||
|
// Liste des PJ : omise si aucun pour alléger le prompt des campagnes sans fiches.
|
||||||
|
if (ctx.characters() != null && !ctx.characters().isEmpty()) {
|
||||||
|
map.put("characters", ctx.characters().stream()
|
||||||
|
.map(this::characterSummaryToMap)
|
||||||
|
.collect(Collectors.toList()));
|
||||||
|
}
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String, Object> characterSummaryToMap(CharacterSummary c) {
|
||||||
|
Map<String, Object> map = new LinkedHashMap<>();
|
||||||
|
map.put("name", c.name());
|
||||||
|
if (c.snippet() != null && !c.snippet().isBlank()) {
|
||||||
|
map.put("snippet", c.snippet());
|
||||||
|
}
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper générique pour sérialiser les entités structurelles (Arc/Chapter/Scene)
|
||||||
|
* avec name, description et illustration_count conditionnel.
|
||||||
|
*/
|
||||||
|
private <T> Map<String, Object> structuralSummaryToMap(
|
||||||
|
T entity,
|
||||||
|
Function<T, String> nameExtractor,
|
||||||
|
Function<T, String> descriptionExtractor,
|
||||||
|
Function<T, Integer> illustrationCountExtractor,
|
||||||
|
BiConsumer<Map<String, Object>, T> childSerializer) {
|
||||||
|
Map<String, Object> map = new LinkedHashMap<>();
|
||||||
|
map.put("name", nameExtractor.apply(entity));
|
||||||
|
map.put("description", descriptionExtractor.apply(entity));
|
||||||
|
if (illustrationCountExtractor.apply(entity) > 0) {
|
||||||
|
map.put("illustration_count", illustrationCountExtractor.apply(entity));
|
||||||
|
}
|
||||||
|
childSerializer.accept(map, entity);
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String, Object> arcSummaryToMap(ArcSummary a) {
|
||||||
|
return structuralSummaryToMap(
|
||||||
|
a,
|
||||||
|
ArcSummary::name,
|
||||||
|
ArcSummary::description,
|
||||||
|
ArcSummary::illustrationCount,
|
||||||
|
(map, arc) -> map.put("chapters", arc.chapters().stream()
|
||||||
|
.map(this::chapterSummaryToMap)
|
||||||
|
.collect(Collectors.toList())));
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String, Object> chapterSummaryToMap(ChapterSummary c) {
|
||||||
|
return structuralSummaryToMap(
|
||||||
|
c,
|
||||||
|
ChapterSummary::name,
|
||||||
|
ChapterSummary::description,
|
||||||
|
ChapterSummary::illustrationCount,
|
||||||
|
(map, chapter) -> map.put("scenes", chapter.scenes().stream()
|
||||||
|
.map(this::sceneSummaryToMap)
|
||||||
|
.collect(Collectors.toList())));
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String, Object> sceneSummaryToMap(SceneSummary s) {
|
||||||
|
return structuralSummaryToMap(
|
||||||
|
s,
|
||||||
|
SceneSummary::name,
|
||||||
|
SceneSummary::description,
|
||||||
|
SceneSummary::illustrationCount,
|
||||||
|
(map, scene) -> {
|
||||||
|
// Branches narratives : omises si absentes (scènes linéaires classiques).
|
||||||
|
if (s.branches() != null && !s.branches().isEmpty()) {
|
||||||
|
map.put("branches", s.branches().stream()
|
||||||
|
.map(this::branchHintToMap)
|
||||||
|
.collect(Collectors.toList()));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String, Object> branchHintToMap(BranchHint b) {
|
||||||
|
Map<String, Object> map = new LinkedHashMap<>();
|
||||||
|
map.put("label", b.label());
|
||||||
|
map.put("target_scene_name", b.targetSceneName());
|
||||||
|
if (b.condition() != null && !b.condition().isBlank()) {
|
||||||
|
map.put("condition", b.condition());
|
||||||
|
}
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String, Object> narrativeEntityToMap(NarrativeEntityContext ne) {
|
||||||
|
Map<String, Object> map = new LinkedHashMap<>();
|
||||||
|
map.put("entity_type", ne.entityType());
|
||||||
|
map.put("title", ne.title());
|
||||||
|
map.put("fields", ne.fields());
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
package com.loremind.infrastructure.ai;
|
||||||
|
|
||||||
|
import com.loremind.domain.conversationcontext.ConversationMessage;
|
||||||
|
import com.loremind.domain.conversationcontext.ports.ConversationTitleGenerator;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.reactive.function.client.WebClient;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adaptateur : appelle le Brain POST /summarize/conversation-title pour
|
||||||
|
* obtenir un titre court a partir des premiers messages.
|
||||||
|
*
|
||||||
|
* Fallback volontairement silencieux : si le Brain est indisponible, on
|
||||||
|
* renvoie un titre par defaut plutot que de casser l'UX chat.
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class BrainConversationTitleClient implements ConversationTitleGenerator {
|
||||||
|
|
||||||
|
private static final String PATH = "/summarize/conversation-title";
|
||||||
|
private static final String FALLBACK = "Nouvelle conversation";
|
||||||
|
|
||||||
|
private final WebClient webClient;
|
||||||
|
|
||||||
|
public BrainConversationTitleClient(
|
||||||
|
WebClient.Builder builder,
|
||||||
|
@Value("${brain.base-url}") String baseUrl) {
|
||||||
|
this.webClient = builder.baseUrl(baseUrl).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String generate(List<ConversationMessage> firstMessages) {
|
||||||
|
if (firstMessages == null || firstMessages.isEmpty()) {
|
||||||
|
return FALLBACK;
|
||||||
|
}
|
||||||
|
Map<String, Object> payload = new LinkedHashMap<>();
|
||||||
|
payload.put("messages", firstMessages.stream()
|
||||||
|
.map(m -> Map.<String, Object>of(
|
||||||
|
"role", m.getRole(),
|
||||||
|
"content", m.getContent() == null ? "" : m.getContent()))
|
||||||
|
.collect(Collectors.toList()));
|
||||||
|
|
||||||
|
try {
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
Map<String, Object> resp = webClient.post()
|
||||||
|
.uri(PATH)
|
||||||
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
|
.bodyValue(payload)
|
||||||
|
.retrieve()
|
||||||
|
.bodyToMono(Map.class)
|
||||||
|
.timeout(Duration.ofSeconds(20))
|
||||||
|
.block();
|
||||||
|
if (resp == null) return FALLBACK;
|
||||||
|
Object title = resp.get("title");
|
||||||
|
if (title == null) return FALLBACK;
|
||||||
|
String s = title.toString().trim();
|
||||||
|
return s.isEmpty() ? FALLBACK : s;
|
||||||
|
} catch (Exception e) {
|
||||||
|
return FALLBACK;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
package com.loremind.infrastructure.ai;
|
||||||
|
|
||||||
|
import com.loremind.domain.generationcontext.ChatUsage;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper d'infrastructure : parse les payloads JSON véhiculés dans les
|
||||||
|
* évènements SSE reçus du Brain Python.
|
||||||
|
* <p>
|
||||||
|
* Implémentation volontairement minimaliste (pas de Jackson ici) car les
|
||||||
|
* schémas attendus sont figés et simples : {"token":"..."} et
|
||||||
|
* {"system":N,"history":N,"current":N,"max":N}. Si la complexité augmente,
|
||||||
|
* remplacer par un ObjectMapper + DTOs.
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class BrainSseParser {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse un JSON {"system":N,"history":N,"current":N,"max":N} en ChatUsage.
|
||||||
|
* Renvoie null si le payload est illisible — l'appelant décidera de ne
|
||||||
|
* simplement pas propager l'usage (le stream token continue).
|
||||||
|
*/
|
||||||
|
public ChatUsage parseUsage(String json) {
|
||||||
|
if (json == null) return null;
|
||||||
|
try {
|
||||||
|
int system = extractIntField(json, "system");
|
||||||
|
int history = extractIntField(json, "history");
|
||||||
|
int current = extractIntField(json, "current");
|
||||||
|
int max = extractIntField(json, "max");
|
||||||
|
return new ChatUsage(system, history, current, max);
|
||||||
|
} catch (Exception e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse {"token":"..."} et renvoie la valeur du champ token (chaîne vide
|
||||||
|
* ou null si introuvable).
|
||||||
|
*/
|
||||||
|
public String parseToken(String json) {
|
||||||
|
if (json == null) return null;
|
||||||
|
int idx = json.indexOf("\"token\"");
|
||||||
|
if (idx < 0) return null;
|
||||||
|
int colon = json.indexOf(':', idx);
|
||||||
|
int firstQuote = json.indexOf('"', colon + 1);
|
||||||
|
int lastQuote = json.lastIndexOf('"');
|
||||||
|
if (firstQuote < 0 || lastQuote <= firstQuote) return null;
|
||||||
|
return json.substring(firstQuote + 1, lastQuote)
|
||||||
|
.replace("\\n", "\n")
|
||||||
|
.replace("\\\"", "\"")
|
||||||
|
.replace("\\\\", "\\");
|
||||||
|
}
|
||||||
|
|
||||||
|
private int extractIntField(String json, String field) {
|
||||||
|
String needle = "\"" + field + "\"";
|
||||||
|
int idx = json.indexOf(needle);
|
||||||
|
if (idx < 0) return 0;
|
||||||
|
int colon = json.indexOf(':', idx);
|
||||||
|
if (colon < 0) return 0;
|
||||||
|
int start = colon + 1;
|
||||||
|
while (start < json.length() && Character.isWhitespace(json.charAt(start))) start++;
|
||||||
|
int end = start;
|
||||||
|
while (end < json.length() && (Character.isDigit(json.charAt(end)) || json.charAt(end) == '-')) end++;
|
||||||
|
if (end == start) return 0;
|
||||||
|
return Integer.parseInt(json.substring(start, end));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,143 @@
|
|||||||
|
package com.loremind.infrastructure.persistence;
|
||||||
|
|
||||||
|
import com.loremind.domain.gamesystemcontext.GameSystem;
|
||||||
|
import com.loremind.domain.gamesystemcontext.ports.GameSystemRepository;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||||
|
import org.springframework.context.event.EventListener;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Seed 3 rulesets libres au premier démarrage (si la table game_systems est vide).
|
||||||
|
* <p>
|
||||||
|
* Objectif : donner à l'utilisateur un point de départ pour comprendre le format
|
||||||
|
* attendu (markdown structuré par titres H2) et permettre une démo "out of the box"
|
||||||
|
* sans devoir taper ses propres règles.
|
||||||
|
* <p>
|
||||||
|
* Les rulesets fournis sont des <b>extraits libres</b> (Nimble, SRD 5.1 extrait,
|
||||||
|
* homebrew exemple) — pas des règles officielles complètes. L'utilisateur est
|
||||||
|
* libre de les éditer, supprimer, ou les utiliser comme template.
|
||||||
|
* <p>
|
||||||
|
* Idempotence : ne seed qu'une fois. Si l'utilisateur supprime un ruleset seedé,
|
||||||
|
* il ne revient pas au redémarrage — c'est voulu (respect du choix utilisateur).
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class GameSystemSeeder {
|
||||||
|
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(GameSystemSeeder.class);
|
||||||
|
|
||||||
|
private final GameSystemRepository gameSystemRepository;
|
||||||
|
|
||||||
|
public GameSystemSeeder(GameSystemRepository gameSystemRepository) {
|
||||||
|
this.gameSystemRepository = gameSystemRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventListener(ApplicationReadyEvent.class)
|
||||||
|
public void seedIfEmpty() {
|
||||||
|
if (!gameSystemRepository.findAll().isEmpty()) {
|
||||||
|
log.debug("GameSystem seed skipped — table non vide.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
log.info("Seed initial des GameSystems (table vide)...");
|
||||||
|
for (GameSystem gs : defaultSystems()) {
|
||||||
|
gameSystemRepository.save(gs);
|
||||||
|
}
|
||||||
|
log.info("GameSystems seedés : {}", defaultSystems().size());
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<GameSystem> defaultSystems() {
|
||||||
|
return List.of(
|
||||||
|
GameSystem.builder()
|
||||||
|
.name("Nimble (extrait)")
|
||||||
|
.description("Système léger et narratif, résolution rapide des combats.")
|
||||||
|
.author("LoreMind seed")
|
||||||
|
.isPublic(false)
|
||||||
|
.rulesMarkdown(NIMBLE_RULES)
|
||||||
|
.build(),
|
||||||
|
GameSystem.builder()
|
||||||
|
.name("D&D 5e SRD (extrait)")
|
||||||
|
.description("Extrait libre des bases du System Reference Document 5.1.")
|
||||||
|
.author("LoreMind seed")
|
||||||
|
.isPublic(false)
|
||||||
|
.rulesMarkdown(DND_SRD_RULES)
|
||||||
|
.build(),
|
||||||
|
GameSystem.builder()
|
||||||
|
.name("Homebrew Exemple")
|
||||||
|
.description("Template minimaliste à dupliquer pour créer votre propre système.")
|
||||||
|
.author("LoreMind seed")
|
||||||
|
.isPublic(false)
|
||||||
|
.rulesMarkdown(HOMEBREW_EXAMPLE)
|
||||||
|
.build()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final String NIMBLE_RULES = """
|
||||||
|
Système Nimble — résolution rapide, narration fluide, peu de tableaux. Agnostique (aucun univers imposé).
|
||||||
|
|
||||||
|
## Combat
|
||||||
|
- Initiative libre : les joueurs décrivent leur action dans l'ordre qu'ils veulent, le MJ joue les ennemis quand la fiction l'exige.
|
||||||
|
- Résolution : 1d20 + mod, difficulté 10/15/20 (facile/normal/dur). 20 naturel = critique (double dégâts).
|
||||||
|
- Dégâts : arme légère 1d6, arme lourde 1d10, projectile 1d8. Pas de table d'armure, l'armure augmente la difficulté à toucher.
|
||||||
|
- Blessures : un PJ peut encaisser 3 blessures graves avant de tomber. Pas de PV fins — on raconte les coups.
|
||||||
|
|
||||||
|
## Classes
|
||||||
|
- **Guerrier** : +2 en combat, peut relancer un dé de dégât 1×/scène.
|
||||||
|
- **Explorateur** : +2 en perception/survie, ignore la première blessure d'une scène.
|
||||||
|
- **Mage** : peut lancer un effet de magie par scène, nécessite une composante racontée.
|
||||||
|
- **Barde** : +2 en social, peut inspirer un allié (relance de dé).
|
||||||
|
|
||||||
|
## Monstres
|
||||||
|
Les monstres ont 3 stats : Menace (difficulté à toucher), Dégâts (dé de dégât), Résistance (nombre de blessures).
|
||||||
|
Exemples : Gobelin (Menace 10, 1d6, 1), Ogre (Menace 13, 1d10, 3), Dragon adulte (Menace 18, 2d10, 6).
|
||||||
|
""";
|
||||||
|
|
||||||
|
private static final String DND_SRD_RULES = """
|
||||||
|
Extrait libre du SRD 5.1 (Open Game License). Pour les règles complètes, consulter le SRD officiel.
|
||||||
|
|
||||||
|
## Combat
|
||||||
|
- Initiative : 1d20 + mod Dex au début du combat, ordre fixe par round.
|
||||||
|
- Action par tour : une action, une action bonus (si classe le permet), une réaction, mouvement jusqu'à la vitesse.
|
||||||
|
- Attaque : 1d20 + mod caractéristique + bonus maîtrise vs CA de la cible.
|
||||||
|
- Dégâts : dé de l'arme + mod caractéristique. Critique sur 20 naturel (double les dés de dégâts).
|
||||||
|
- Avantage/Désavantage : lancer 2d20 et garder le meilleur / pire.
|
||||||
|
|
||||||
|
## Classes
|
||||||
|
- **Barbare** : d12 PV, rage (+dégâts, résistance). Caractéristique principale : Force.
|
||||||
|
- **Barde** : d8 PV, sorts + inspiration bardique. Caractéristique : Charisme.
|
||||||
|
- **Clerc** : d8 PV, sorts divins, canalise la divinité. Caractéristique : Sagesse.
|
||||||
|
- **Druide** : d8 PV, sorts nature + forme animale. Caractéristique : Sagesse.
|
||||||
|
- **Ensorceleur** : d6 PV, sorts innés + métamagie. Caractéristique : Charisme.
|
||||||
|
- **Guerrier** : d10 PV, maîtrise martiale, second souffle. Caractéristique : Force ou Dextérité.
|
||||||
|
- **Magicien** : d6 PV, livre de sorts, grande flexibilité. Caractéristique : Intelligence.
|
||||||
|
- **Moine** : d8 PV, arts martiaux + ki. Caractéristique : Dextérité + Sagesse.
|
||||||
|
- **Paladin** : d10 PV, sorts + serment + imposition des mains. Caractéristique : Force + Charisme.
|
||||||
|
- **Rôdeur** : d10 PV, ennemi juré + explorateur + sorts. Caractéristique : Dextérité + Sagesse.
|
||||||
|
- **Roublard** : d8 PV, attaque sournoise + expertise. Caractéristique : Dextérité.
|
||||||
|
|
||||||
|
## Monstres
|
||||||
|
Stat block standard : CA, PV, Vitesse, For/Dex/Con/Int/Sag/Cha, jets de sauvegarde, compétences, sens, langues, Facteur de Puissance (FP).
|
||||||
|
Exemples : Gobelin (FP 1/4, CA 15, 7 PV), Ogre (FP 2, CA 11, 59 PV), Dragon rouge adulte (FP 17, CA 19, 256 PV).
|
||||||
|
""";
|
||||||
|
|
||||||
|
private static final String HOMEBREW_EXAMPLE = """
|
||||||
|
Template vide à dupliquer et remplir pour créer votre propre système.
|
||||||
|
|
||||||
|
## Combat
|
||||||
|
(Décrivez ici comment se résout un combat : initiative, jet d'attaque, dégâts, points de vie, critiques...)
|
||||||
|
|
||||||
|
## Classes
|
||||||
|
(Listez les archétypes jouables : nom, stats de base, capacités signature.)
|
||||||
|
|
||||||
|
## Monstres
|
||||||
|
(Format de stat block pour vos créatures : stats, capacités spéciales, FP/niveau.)
|
||||||
|
|
||||||
|
## Magie
|
||||||
|
(Si votre système a un système de magie : écoles, coût, composantes, listes de sorts/pouvoirs.)
|
||||||
|
|
||||||
|
## Progression
|
||||||
|
(Comment les PJ montent en puissance : XP, niveaux, acquisitions par niveau.)
|
||||||
|
""";
|
||||||
|
}
|
||||||
@@ -37,6 +37,9 @@ public class ArcJpaEntity {
|
|||||||
@Column(name = "\"order\"", nullable = false)
|
@Column(name = "\"order\"", nullable = false)
|
||||||
private int order;
|
private int order;
|
||||||
|
|
||||||
|
@Column
|
||||||
|
private String icon;
|
||||||
|
|
||||||
// Champs narratifs enrichis — ajoutés automatiquement par Hibernate DDL (ddl-auto=update)
|
// Champs narratifs enrichis — ajoutés automatiquement par Hibernate DDL (ddl-auto=update)
|
||||||
@Column(columnDefinition = "TEXT")
|
@Column(columnDefinition = "TEXT")
|
||||||
private String themes;
|
private String themes;
|
||||||
|
|||||||
@@ -45,6 +45,13 @@ public class CampaignJpaEntity {
|
|||||||
@Column(name = "lore_id")
|
@Column(name = "lore_id")
|
||||||
private String loreId;
|
private String loreId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ID du GameSystem associé (nullable).
|
||||||
|
* Weak reference inter-contexte — pas de @ManyToOne / pas de FK DB.
|
||||||
|
*/
|
||||||
|
@Column(name = "game_system_id")
|
||||||
|
private String gameSystemId;
|
||||||
|
|
||||||
@PrePersist
|
@PrePersist
|
||||||
protected void onCreate() {
|
protected void onCreate() {
|
||||||
createdAt = LocalDateTime.now();
|
createdAt = LocalDateTime.now();
|
||||||
|
|||||||
@@ -37,6 +37,9 @@ public class ChapterJpaEntity {
|
|||||||
@Column(name = "\"order\"", nullable = false)
|
@Column(name = "\"order\"", nullable = false)
|
||||||
private int order;
|
private int order;
|
||||||
|
|
||||||
|
@Column
|
||||||
|
private String icon;
|
||||||
|
|
||||||
// Champs narratifs enrichis — ajoutés automatiquement par Hibernate DDL (ddl-auto=update)
|
// Champs narratifs enrichis — ajoutés automatiquement par Hibernate DDL (ddl-auto=update)
|
||||||
@Column(name = "gm_notes", columnDefinition = "TEXT")
|
@Column(name = "gm_notes", columnDefinition = "TEXT")
|
||||||
private String gmNotes;
|
private String gmNotes;
|
||||||
|
|||||||
@@ -0,0 +1,56 @@
|
|||||||
|
package com.loremind.infrastructure.persistence.entity;
|
||||||
|
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Entité JPA pour les fiches de personnages (PJ) d'une campagne.
|
||||||
|
* Pas de FK physique vers campaigns (weak reference cross-agrégat intra-contexte :
|
||||||
|
* on reste dans le Campaign Context, mais l'agrégat Character est autonome).
|
||||||
|
*/
|
||||||
|
@Entity
|
||||||
|
@Table(name = "characters")
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class CharacterJpaEntity {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
@Column(nullable = false)
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
@Column(name = "markdown_content", columnDefinition = "TEXT")
|
||||||
|
private String markdownContent;
|
||||||
|
|
||||||
|
@Column(name = "campaign_id", nullable = false)
|
||||||
|
private Long campaignId;
|
||||||
|
|
||||||
|
@Column(name = "\"order\"", nullable = false)
|
||||||
|
private int order;
|
||||||
|
|
||||||
|
@Column(name = "created_at", nullable = false, updatable = false)
|
||||||
|
private LocalDateTime createdAt;
|
||||||
|
|
||||||
|
@Column(name = "updated_at", nullable = false)
|
||||||
|
private LocalDateTime updatedAt;
|
||||||
|
|
||||||
|
@PrePersist
|
||||||
|
protected void onCreate() {
|
||||||
|
createdAt = LocalDateTime.now();
|
||||||
|
updatedAt = LocalDateTime.now();
|
||||||
|
}
|
||||||
|
|
||||||
|
@PreUpdate
|
||||||
|
protected void onUpdate() {
|
||||||
|
updatedAt = LocalDateTime.now();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,89 @@
|
|||||||
|
package com.loremind.infrastructure.persistence.entity;
|
||||||
|
|
||||||
|
import jakarta.persistence.CascadeType;
|
||||||
|
import jakarta.persistence.Column;
|
||||||
|
import jakarta.persistence.Entity;
|
||||||
|
import jakarta.persistence.FetchType;
|
||||||
|
import jakarta.persistence.GeneratedValue;
|
||||||
|
import jakarta.persistence.GenerationType;
|
||||||
|
import jakarta.persistence.Id;
|
||||||
|
import jakarta.persistence.Index;
|
||||||
|
import jakarta.persistence.OneToMany;
|
||||||
|
import jakarta.persistence.OrderBy;
|
||||||
|
import jakarta.persistence.PrePersist;
|
||||||
|
import jakarta.persistence.PreUpdate;
|
||||||
|
import jakarta.persistence.Table;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Persistance d'une conversation de chat IA.
|
||||||
|
*
|
||||||
|
* Les refs loreId / campaignId / entityId sont des weak references (String,
|
||||||
|
* pas de FK) — coherent avec la politique inter-contexte du reste du code.
|
||||||
|
* Indexes compose pour accelerer le listing par contexte dans la sidebar.
|
||||||
|
*/
|
||||||
|
@Entity
|
||||||
|
@Table(name = "conversations", indexes = {
|
||||||
|
@Index(name = "idx_conv_lore_entity", columnList = "lore_id,entity_type,entity_id,updated_at"),
|
||||||
|
@Index(name = "idx_conv_campaign_entity", columnList = "campaign_id,entity_type,entity_id,updated_at")
|
||||||
|
})
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class ConversationJpaEntity {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
@Column(nullable = false)
|
||||||
|
private String title;
|
||||||
|
|
||||||
|
@Column(name = "lore_id")
|
||||||
|
private String loreId;
|
||||||
|
|
||||||
|
@Column(name = "campaign_id")
|
||||||
|
private String campaignId;
|
||||||
|
|
||||||
|
@Column(name = "entity_type")
|
||||||
|
private String entityType;
|
||||||
|
|
||||||
|
@Column(name = "entity_id")
|
||||||
|
private String entityId;
|
||||||
|
|
||||||
|
@Column(name = "created_at", nullable = false, updatable = false)
|
||||||
|
private LocalDateTime createdAt;
|
||||||
|
|
||||||
|
@Column(name = "updated_at", nullable = false)
|
||||||
|
private LocalDateTime updatedAt;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Messages enfants. Charges a la demande (fetch=LAZY) pour ne pas plomber
|
||||||
|
* le listing sidebar. Cascade ALL + orphanRemoval : la suppression d'une
|
||||||
|
* conversation efface ses messages.
|
||||||
|
*/
|
||||||
|
@OneToMany(mappedBy = "conversation", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
|
||||||
|
@OrderBy("createdAt ASC, id ASC")
|
||||||
|
@Builder.Default
|
||||||
|
private List<ConversationMessageJpaEntity> messages = new ArrayList<>();
|
||||||
|
|
||||||
|
@PrePersist
|
||||||
|
protected void onCreate() {
|
||||||
|
LocalDateTime now = LocalDateTime.now();
|
||||||
|
createdAt = now;
|
||||||
|
updatedAt = now;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PreUpdate
|
||||||
|
protected void onUpdate() {
|
||||||
|
updatedAt = LocalDateTime.now();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
package com.loremind.infrastructure.persistence.entity;
|
||||||
|
|
||||||
|
import jakarta.persistence.Column;
|
||||||
|
import jakarta.persistence.Entity;
|
||||||
|
import jakarta.persistence.GeneratedValue;
|
||||||
|
import jakarta.persistence.GenerationType;
|
||||||
|
import jakarta.persistence.Id;
|
||||||
|
import jakarta.persistence.JoinColumn;
|
||||||
|
import jakarta.persistence.ManyToOne;
|
||||||
|
import jakarta.persistence.PrePersist;
|
||||||
|
import jakarta.persistence.Table;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.ToString;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Persistance d'un message appartenant a une {@link ConversationJpaEntity}.
|
||||||
|
* Les messages sont ordonnes par createdAt ASC (ordre d'ajout = ordre lu).
|
||||||
|
*/
|
||||||
|
@Entity
|
||||||
|
@Table(name = "conversation_messages")
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class ConversationMessageJpaEntity {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
/** "user" | "assistant" | "system". */
|
||||||
|
@Column(nullable = false, length = 16)
|
||||||
|
private String role;
|
||||||
|
|
||||||
|
@Column(nullable = false, columnDefinition = "TEXT")
|
||||||
|
private String content;
|
||||||
|
|
||||||
|
@Column(name = "created_at", nullable = false, updatable = false)
|
||||||
|
private LocalDateTime createdAt;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reference vers la conversation parent. ToString exclu pour eviter une
|
||||||
|
* boucle infinie quand Lombok genere toString() (conv -> messages -> conv...).
|
||||||
|
*/
|
||||||
|
@ManyToOne(optional = false)
|
||||||
|
@JoinColumn(name = "conversation_id", nullable = false)
|
||||||
|
@ToString.Exclude
|
||||||
|
private ConversationJpaEntity conversation;
|
||||||
|
|
||||||
|
@PrePersist
|
||||||
|
protected void onCreate() {
|
||||||
|
createdAt = LocalDateTime.now();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
package com.loremind.infrastructure.persistence.entity;
|
||||||
|
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Entité JPA pour la persistance des GameSystems (systèmes de JDR).
|
||||||
|
*/
|
||||||
|
@Entity
|
||||||
|
@Table(name = "game_systems")
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class GameSystemJpaEntity {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
@Column(nullable = false)
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
@Column(columnDefinition = "TEXT")
|
||||||
|
private String description;
|
||||||
|
|
||||||
|
@Column(name = "rules_markdown", columnDefinition = "TEXT")
|
||||||
|
private String rulesMarkdown;
|
||||||
|
|
||||||
|
@Column
|
||||||
|
private String author;
|
||||||
|
|
||||||
|
@Column(name = "is_public", nullable = false)
|
||||||
|
private boolean isPublic;
|
||||||
|
|
||||||
|
@Column(name = "created_at", nullable = false, updatable = false)
|
||||||
|
private LocalDateTime createdAt;
|
||||||
|
|
||||||
|
@Column(name = "updated_at", nullable = false)
|
||||||
|
private LocalDateTime updatedAt;
|
||||||
|
|
||||||
|
@PrePersist
|
||||||
|
protected void onCreate() {
|
||||||
|
createdAt = LocalDateTime.now();
|
||||||
|
updatedAt = LocalDateTime.now();
|
||||||
|
}
|
||||||
|
|
||||||
|
@PreUpdate
|
||||||
|
protected void onUpdate() {
|
||||||
|
updatedAt = LocalDateTime.now();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -39,6 +39,9 @@ public class SceneJpaEntity {
|
|||||||
@Column(name = "\"order\"", nullable = false)
|
@Column(name = "\"order\"", nullable = false)
|
||||||
private int order;
|
private int order;
|
||||||
|
|
||||||
|
@Column
|
||||||
|
private String icon;
|
||||||
|
|
||||||
// Champs narratifs enrichis — ajoutés automatiquement par Hibernate (ddl-auto=update)
|
// Champs narratifs enrichis — ajoutés automatiquement par Hibernate (ddl-auto=update)
|
||||||
|
|
||||||
// Contexte et ambiance
|
// Contexte et ambiance
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package com.loremind.infrastructure.persistence.jpa;
|
||||||
|
|
||||||
|
import com.loremind.infrastructure.persistence.entity.CharacterJpaEntity;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public interface CharacterJpaRepository extends JpaRepository<CharacterJpaEntity, Long> {
|
||||||
|
|
||||||
|
List<CharacterJpaEntity> findByCampaignIdOrderByOrderAsc(Long campaignId);
|
||||||
|
}
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
package com.loremind.infrastructure.persistence.jpa;
|
||||||
|
|
||||||
|
import com.loremind.infrastructure.persistence.entity.ConversationJpaEntity;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
import org.springframework.data.repository.query.Param;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Repository Spring Data JPA pour ConversationJpaEntity.
|
||||||
|
*
|
||||||
|
* Les requetes de listing par contexte gerent explicitement les NULL parce
|
||||||
|
* que JPQL `=` ne matche pas NULL. On combine `IS NULL` / `=` selon si le
|
||||||
|
* filtre est fourni — plus simple qu'une Specification Criteria API.
|
||||||
|
*/
|
||||||
|
@Repository
|
||||||
|
public interface ConversationJpaRepository extends JpaRepository<ConversationJpaEntity, Long> {
|
||||||
|
|
||||||
|
/** Listing Lore racine (entity_type IS NULL). */
|
||||||
|
@Query("""
|
||||||
|
SELECT c FROM ConversationJpaEntity c
|
||||||
|
WHERE c.loreId = :loreId
|
||||||
|
AND c.entityType IS NULL
|
||||||
|
ORDER BY c.updatedAt DESC
|
||||||
|
""")
|
||||||
|
List<ConversationJpaEntity> findByLoreRoot(@Param("loreId") String loreId);
|
||||||
|
|
||||||
|
/** Listing Lore + entite precise. */
|
||||||
|
@Query("""
|
||||||
|
SELECT c FROM ConversationJpaEntity c
|
||||||
|
WHERE c.loreId = :loreId
|
||||||
|
AND c.entityType = :entityType
|
||||||
|
AND c.entityId = :entityId
|
||||||
|
ORDER BY c.updatedAt DESC
|
||||||
|
""")
|
||||||
|
List<ConversationJpaEntity> findByLoreAndEntity(
|
||||||
|
@Param("loreId") String loreId,
|
||||||
|
@Param("entityType") String entityType,
|
||||||
|
@Param("entityId") String entityId);
|
||||||
|
|
||||||
|
/** Listing Campagne racine. */
|
||||||
|
@Query("""
|
||||||
|
SELECT c FROM ConversationJpaEntity c
|
||||||
|
WHERE c.campaignId = :campaignId
|
||||||
|
AND c.entityType IS NULL
|
||||||
|
ORDER BY c.updatedAt DESC
|
||||||
|
""")
|
||||||
|
List<ConversationJpaEntity> findByCampaignRoot(@Param("campaignId") String campaignId);
|
||||||
|
|
||||||
|
/** Listing Campagne + entite precise. */
|
||||||
|
@Query("""
|
||||||
|
SELECT c FROM ConversationJpaEntity c
|
||||||
|
WHERE c.campaignId = :campaignId
|
||||||
|
AND c.entityType = :entityType
|
||||||
|
AND c.entityId = :entityId
|
||||||
|
ORDER BY c.updatedAt DESC
|
||||||
|
""")
|
||||||
|
List<ConversationJpaEntity> findByCampaignAndEntity(
|
||||||
|
@Param("campaignId") String campaignId,
|
||||||
|
@Param("entityType") String entityType,
|
||||||
|
@Param("entityId") String entityId);
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package com.loremind.infrastructure.persistence.jpa;
|
||||||
|
|
||||||
|
import com.loremind.infrastructure.persistence.entity.GameSystemJpaEntity;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
import org.springframework.data.repository.query.Param;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public interface GameSystemJpaRepository extends JpaRepository<GameSystemJpaEntity, Long> {
|
||||||
|
|
||||||
|
@Query("SELECT g FROM GameSystemJpaEntity g WHERE LOWER(g.name) LIKE LOWER(CONCAT('%', :query, '%'))")
|
||||||
|
List<GameSystemJpaEntity> findByNameContainingIgnoreCase(@Param("query") String query);
|
||||||
|
}
|
||||||
@@ -71,6 +71,7 @@ public class PostgresArcRepository implements ArcRepository {
|
|||||||
.description(jpaEntity.getDescription())
|
.description(jpaEntity.getDescription())
|
||||||
.campaignId(jpaEntity.getCampaignId().toString())
|
.campaignId(jpaEntity.getCampaignId().toString())
|
||||||
.order(jpaEntity.getOrder())
|
.order(jpaEntity.getOrder())
|
||||||
|
.icon(jpaEntity.getIcon())
|
||||||
.themes(jpaEntity.getThemes())
|
.themes(jpaEntity.getThemes())
|
||||||
.stakes(jpaEntity.getStakes())
|
.stakes(jpaEntity.getStakes())
|
||||||
.gmNotes(jpaEntity.getGmNotes())
|
.gmNotes(jpaEntity.getGmNotes())
|
||||||
@@ -99,6 +100,7 @@ public class PostgresArcRepository implements ArcRepository {
|
|||||||
.description(arc.getDescription())
|
.description(arc.getDescription())
|
||||||
.campaignId(Long.parseLong(arc.getCampaignId()))
|
.campaignId(Long.parseLong(arc.getCampaignId()))
|
||||||
.order(arc.getOrder())
|
.order(arc.getOrder())
|
||||||
|
.icon(arc.getIcon())
|
||||||
.themes(arc.getThemes())
|
.themes(arc.getThemes())
|
||||||
.stakes(arc.getStakes())
|
.stakes(arc.getStakes())
|
||||||
.gmNotes(arc.getGmNotes())
|
.gmNotes(arc.getGmNotes())
|
||||||
|
|||||||
@@ -71,6 +71,7 @@ public class PostgresCampaignRepository implements CampaignRepository {
|
|||||||
.updatedAt(jpaEntity.getUpdatedAt())
|
.updatedAt(jpaEntity.getUpdatedAt())
|
||||||
.arcsCount(jpaEntity.getArcsCount())
|
.arcsCount(jpaEntity.getArcsCount())
|
||||||
.loreId(jpaEntity.getLoreId())
|
.loreId(jpaEntity.getLoreId())
|
||||||
|
.gameSystemId(jpaEntity.getGameSystemId())
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,6 +85,7 @@ public class PostgresCampaignRepository implements CampaignRepository {
|
|||||||
.updatedAt(campaign.getUpdatedAt())
|
.updatedAt(campaign.getUpdatedAt())
|
||||||
.arcsCount(campaign.getArcsCount())
|
.arcsCount(campaign.getArcsCount())
|
||||||
.loreId(campaign.getLoreId())
|
.loreId(campaign.getLoreId())
|
||||||
|
.gameSystemId(campaign.getGameSystemId())
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -71,6 +71,7 @@ public class PostgresChapterRepository implements ChapterRepository {
|
|||||||
.description(jpaEntity.getDescription())
|
.description(jpaEntity.getDescription())
|
||||||
.arcId(jpaEntity.getArcId().toString())
|
.arcId(jpaEntity.getArcId().toString())
|
||||||
.order(jpaEntity.getOrder())
|
.order(jpaEntity.getOrder())
|
||||||
|
.icon(jpaEntity.getIcon())
|
||||||
.gmNotes(jpaEntity.getGmNotes())
|
.gmNotes(jpaEntity.getGmNotes())
|
||||||
.playerObjectives(jpaEntity.getPlayerObjectives())
|
.playerObjectives(jpaEntity.getPlayerObjectives())
|
||||||
.narrativeStakes(jpaEntity.getNarrativeStakes())
|
.narrativeStakes(jpaEntity.getNarrativeStakes())
|
||||||
@@ -96,6 +97,7 @@ public class PostgresChapterRepository implements ChapterRepository {
|
|||||||
.description(chapter.getDescription())
|
.description(chapter.getDescription())
|
||||||
.arcId(Long.parseLong(chapter.getArcId()))
|
.arcId(Long.parseLong(chapter.getArcId()))
|
||||||
.order(chapter.getOrder())
|
.order(chapter.getOrder())
|
||||||
|
.icon(chapter.getIcon())
|
||||||
.gmNotes(chapter.getGmNotes())
|
.gmNotes(chapter.getGmNotes())
|
||||||
.playerObjectives(chapter.getPlayerObjectives())
|
.playerObjectives(chapter.getPlayerObjectives())
|
||||||
.narrativeStakes(chapter.getNarrativeStakes())
|
.narrativeStakes(chapter.getNarrativeStakes())
|
||||||
|
|||||||
@@ -0,0 +1,75 @@
|
|||||||
|
package com.loremind.infrastructure.persistence.postgres;
|
||||||
|
|
||||||
|
import com.loremind.domain.campaigncontext.Character;
|
||||||
|
import com.loremind.domain.campaigncontext.ports.CharacterRepository;
|
||||||
|
import com.loremind.infrastructure.persistence.entity.CharacterJpaEntity;
|
||||||
|
import com.loremind.infrastructure.persistence.jpa.CharacterJpaRepository;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public class PostgresCharacterRepository implements CharacterRepository {
|
||||||
|
|
||||||
|
private final CharacterJpaRepository jpaRepository;
|
||||||
|
|
||||||
|
public PostgresCharacterRepository(CharacterJpaRepository jpaRepository) {
|
||||||
|
this.jpaRepository = jpaRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Character save(Character character) {
|
||||||
|
CharacterJpaEntity entity = toJpaEntity(character);
|
||||||
|
CharacterJpaEntity saved = jpaRepository.save(entity);
|
||||||
|
return toDomainEntity(saved);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Optional<Character> findById(String id) {
|
||||||
|
return jpaRepository.findById(Long.parseLong(id)).map(this::toDomainEntity);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Character> findByCampaignId(String campaignId) {
|
||||||
|
return jpaRepository.findByCampaignIdOrderByOrderAsc(Long.parseLong(campaignId)).stream()
|
||||||
|
.map(this::toDomainEntity)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void deleteById(String id) {
|
||||||
|
jpaRepository.deleteById(Long.parseLong(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean existsById(String id) {
|
||||||
|
return jpaRepository.existsById(Long.parseLong(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
private Character toDomainEntity(CharacterJpaEntity e) {
|
||||||
|
return Character.builder()
|
||||||
|
.id(e.getId().toString())
|
||||||
|
.name(e.getName())
|
||||||
|
.markdownContent(e.getMarkdownContent())
|
||||||
|
.campaignId(e.getCampaignId().toString())
|
||||||
|
.order(e.getOrder())
|
||||||
|
.createdAt(e.getCreatedAt())
|
||||||
|
.updatedAt(e.getUpdatedAt())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
private CharacterJpaEntity toJpaEntity(Character c) {
|
||||||
|
Long id = c.getId() != null ? Long.parseLong(c.getId()) : null;
|
||||||
|
return CharacterJpaEntity.builder()
|
||||||
|
.id(id)
|
||||||
|
.name(c.getName())
|
||||||
|
.markdownContent(c.getMarkdownContent())
|
||||||
|
.campaignId(Long.parseLong(c.getCampaignId()))
|
||||||
|
.order(c.getOrder())
|
||||||
|
.createdAt(c.getCreatedAt())
|
||||||
|
.updatedAt(c.getUpdatedAt())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,148 @@
|
|||||||
|
package com.loremind.infrastructure.persistence.postgres;
|
||||||
|
|
||||||
|
import com.loremind.domain.conversationcontext.Conversation;
|
||||||
|
import com.loremind.domain.conversationcontext.ConversationMessage;
|
||||||
|
import com.loremind.domain.conversationcontext.ports.ConversationRepository;
|
||||||
|
import com.loremind.infrastructure.persistence.entity.ConversationJpaEntity;
|
||||||
|
import com.loremind.infrastructure.persistence.entity.ConversationMessageJpaEntity;
|
||||||
|
import com.loremind.infrastructure.persistence.jpa.ConversationJpaRepository;
|
||||||
|
import jakarta.persistence.EntityNotFoundException;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adaptateur Postgres pour ConversationRepository.
|
||||||
|
*
|
||||||
|
* Les methodes de listing ne chargent PAS les messages (messages LAZY,
|
||||||
|
* liste vide renvoyee cote domaine) — la sidebar n'a besoin que des
|
||||||
|
* meta-donnees. findById charge les messages via fetch explicite de la
|
||||||
|
* collection dans une transaction.
|
||||||
|
*/
|
||||||
|
@Repository
|
||||||
|
public class PostgresConversationRepository implements ConversationRepository {
|
||||||
|
|
||||||
|
private final ConversationJpaRepository jpa;
|
||||||
|
|
||||||
|
public PostgresConversationRepository(ConversationJpaRepository jpa) {
|
||||||
|
this.jpa = jpa;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Transactional
|
||||||
|
public Conversation save(Conversation conversation) {
|
||||||
|
ConversationJpaEntity entity = toJpaEntity(conversation);
|
||||||
|
ConversationJpaEntity saved = jpa.save(entity);
|
||||||
|
return toDomain(saved, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public Optional<Conversation> findById(String id) {
|
||||||
|
return jpa.findById(Long.parseLong(id))
|
||||||
|
.map(e -> {
|
||||||
|
// Force l'initialisation LAZY avant de sortir de la transaction.
|
||||||
|
e.getMessages().size();
|
||||||
|
return toDomain(e, true);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public List<Conversation> findByContext(String loreId, String campaignId, String entityType, String entityId) {
|
||||||
|
List<ConversationJpaEntity> rows;
|
||||||
|
if (loreId != null) {
|
||||||
|
rows = (entityType == null)
|
||||||
|
? jpa.findByLoreRoot(loreId)
|
||||||
|
: jpa.findByLoreAndEntity(loreId, entityType, entityId);
|
||||||
|
} else if (campaignId != null) {
|
||||||
|
rows = (entityType == null)
|
||||||
|
? jpa.findByCampaignRoot(campaignId)
|
||||||
|
: jpa.findByCampaignAndEntity(campaignId, entityType, entityId);
|
||||||
|
} else {
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
return rows.stream().map(e -> toDomain(e, false)).collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Transactional
|
||||||
|
public void deleteById(String id) {
|
||||||
|
jpa.deleteById(Long.parseLong(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Transactional
|
||||||
|
public ConversationMessage appendMessage(String conversationId, ConversationMessage message) {
|
||||||
|
ConversationJpaEntity conv = jpa.findById(Long.parseLong(conversationId))
|
||||||
|
.orElseThrow(() -> new EntityNotFoundException("Conversation " + conversationId));
|
||||||
|
|
||||||
|
ConversationMessageJpaEntity msg = ConversationMessageJpaEntity.builder()
|
||||||
|
.role(message.getRole())
|
||||||
|
.content(message.getContent())
|
||||||
|
.conversation(conv)
|
||||||
|
.build();
|
||||||
|
conv.getMessages().add(msg);
|
||||||
|
// Force updatedAt via @PreUpdate en modifiant la conv (touch).
|
||||||
|
conv.setUpdatedAt(java.time.LocalDateTime.now());
|
||||||
|
|
||||||
|
ConversationJpaEntity saved = jpa.save(conv);
|
||||||
|
ConversationMessageJpaEntity persisted = saved.getMessages().get(saved.getMessages().size() - 1);
|
||||||
|
return toDomainMessage(persisted);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Transactional
|
||||||
|
public void updateTitle(String conversationId, String title) {
|
||||||
|
ConversationJpaEntity conv = jpa.findById(Long.parseLong(conversationId))
|
||||||
|
.orElseThrow(() -> new EntityNotFoundException("Conversation " + conversationId));
|
||||||
|
conv.setTitle(title);
|
||||||
|
jpa.save(conv);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------- Mapping ----------
|
||||||
|
|
||||||
|
private ConversationJpaEntity toJpaEntity(Conversation c) {
|
||||||
|
Long id = c.getId() != null ? Long.parseLong(c.getId()) : null;
|
||||||
|
return ConversationJpaEntity.builder()
|
||||||
|
.id(id)
|
||||||
|
.title(c.getTitle())
|
||||||
|
.loreId(c.getLoreId())
|
||||||
|
.campaignId(c.getCampaignId())
|
||||||
|
.entityType(c.getEntityType())
|
||||||
|
.entityId(c.getEntityId())
|
||||||
|
.createdAt(c.getCreatedAt())
|
||||||
|
.updatedAt(c.getUpdatedAt())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Conversation toDomain(ConversationJpaEntity e, boolean withMessages) {
|
||||||
|
List<ConversationMessage> msgs = withMessages
|
||||||
|
? e.getMessages().stream().map(this::toDomainMessage).collect(Collectors.toList())
|
||||||
|
: new java.util.ArrayList<>();
|
||||||
|
return Conversation.builder()
|
||||||
|
.id(e.getId().toString())
|
||||||
|
.title(e.getTitle())
|
||||||
|
.loreId(e.getLoreId())
|
||||||
|
.campaignId(e.getCampaignId())
|
||||||
|
.entityType(e.getEntityType())
|
||||||
|
.entityId(e.getEntityId())
|
||||||
|
.createdAt(e.getCreatedAt())
|
||||||
|
.updatedAt(e.getUpdatedAt())
|
||||||
|
.messages(msgs)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
private ConversationMessage toDomainMessage(ConversationMessageJpaEntity e) {
|
||||||
|
return ConversationMessage.builder()
|
||||||
|
.id(e.getId() != null ? e.getId().toString() : null)
|
||||||
|
.role(e.getRole())
|
||||||
|
.content(e.getContent())
|
||||||
|
.createdAt(e.getCreatedAt())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,84 @@
|
|||||||
|
package com.loremind.infrastructure.persistence.postgres;
|
||||||
|
|
||||||
|
import com.loremind.domain.gamesystemcontext.GameSystem;
|
||||||
|
import com.loremind.domain.gamesystemcontext.ports.GameSystemRepository;
|
||||||
|
import com.loremind.infrastructure.persistence.entity.GameSystemJpaEntity;
|
||||||
|
import com.loremind.infrastructure.persistence.jpa.GameSystemJpaRepository;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public class PostgresGameSystemRepository implements GameSystemRepository {
|
||||||
|
|
||||||
|
private final GameSystemJpaRepository jpaRepository;
|
||||||
|
|
||||||
|
public PostgresGameSystemRepository(GameSystemJpaRepository jpaRepository) {
|
||||||
|
this.jpaRepository = jpaRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public GameSystem save(GameSystem gameSystem) {
|
||||||
|
GameSystemJpaEntity entity = toJpaEntity(gameSystem);
|
||||||
|
GameSystemJpaEntity saved = jpaRepository.save(entity);
|
||||||
|
return toDomainEntity(saved);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Optional<GameSystem> findById(String id) {
|
||||||
|
return jpaRepository.findById(Long.parseLong(id)).map(this::toDomainEntity);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<GameSystem> findAll() {
|
||||||
|
return jpaRepository.findAll().stream()
|
||||||
|
.map(this::toDomainEntity)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void deleteById(String id) {
|
||||||
|
jpaRepository.deleteById(Long.parseLong(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean existsById(String id) {
|
||||||
|
return jpaRepository.existsById(Long.parseLong(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<GameSystem> searchByName(String query) {
|
||||||
|
return jpaRepository.findByNameContainingIgnoreCase(query).stream()
|
||||||
|
.map(this::toDomainEntity)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
private GameSystem toDomainEntity(GameSystemJpaEntity e) {
|
||||||
|
return GameSystem.builder()
|
||||||
|
.id(e.getId().toString())
|
||||||
|
.name(e.getName())
|
||||||
|
.description(e.getDescription())
|
||||||
|
.rulesMarkdown(e.getRulesMarkdown())
|
||||||
|
.author(e.getAuthor())
|
||||||
|
.isPublic(e.isPublic())
|
||||||
|
.createdAt(e.getCreatedAt())
|
||||||
|
.updatedAt(e.getUpdatedAt())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
private GameSystemJpaEntity toJpaEntity(GameSystem g) {
|
||||||
|
Long id = g.getId() != null ? Long.parseLong(g.getId()) : null;
|
||||||
|
return GameSystemJpaEntity.builder()
|
||||||
|
.id(id)
|
||||||
|
.name(g.getName())
|
||||||
|
.description(g.getDescription())
|
||||||
|
.rulesMarkdown(g.getRulesMarkdown())
|
||||||
|
.author(g.getAuthor())
|
||||||
|
.isPublic(g.isPublic())
|
||||||
|
.createdAt(g.getCreatedAt())
|
||||||
|
.updatedAt(g.getUpdatedAt())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -71,6 +71,7 @@ public class PostgresSceneRepository implements SceneRepository {
|
|||||||
.description(jpaEntity.getDescription())
|
.description(jpaEntity.getDescription())
|
||||||
.chapterId(jpaEntity.getChapterId().toString())
|
.chapterId(jpaEntity.getChapterId().toString())
|
||||||
.order(jpaEntity.getOrder())
|
.order(jpaEntity.getOrder())
|
||||||
|
.icon(jpaEntity.getIcon())
|
||||||
.location(jpaEntity.getLocation())
|
.location(jpaEntity.getLocation())
|
||||||
.timing(jpaEntity.getTiming())
|
.timing(jpaEntity.getTiming())
|
||||||
.atmosphere(jpaEntity.getAtmosphere())
|
.atmosphere(jpaEntity.getAtmosphere())
|
||||||
@@ -104,6 +105,7 @@ public class PostgresSceneRepository implements SceneRepository {
|
|||||||
.description(scene.getDescription())
|
.description(scene.getDescription())
|
||||||
.chapterId(Long.parseLong(scene.getChapterId()))
|
.chapterId(Long.parseLong(scene.getChapterId()))
|
||||||
.order(scene.getOrder())
|
.order(scene.getOrder())
|
||||||
|
.icon(scene.getIcon())
|
||||||
.location(scene.getLocation())
|
.location(scene.getLocation())
|
||||||
.timing(scene.getTiming())
|
.timing(scene.getTiming())
|
||||||
.atmosphere(scene.getAtmosphere())
|
.atmosphere(scene.getAtmosphere())
|
||||||
|
|||||||
@@ -0,0 +1,283 @@
|
|||||||
|
package com.loremind.infrastructure.updates;
|
||||||
|
|
||||||
|
import jakarta.annotation.PostConstruct;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.boot.web.client.RestTemplateBuilder;
|
||||||
|
import org.springframework.http.HttpEntity;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.HttpMethod;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.web.client.HttpClientErrorException;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
|
import java.net.URLEncoder;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.time.Duration;
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Detection des mises a jour disponibles + declenchement via Watchtower.
|
||||||
|
*
|
||||||
|
* Strategie :
|
||||||
|
* - Au demarrage, on interroge le registry pour le digest courant de chaque
|
||||||
|
* image suivie ({@code update-check.images}). On stocke ces digests comme
|
||||||
|
* "baseline" (= ce que le conteneur en cours d'execution est cense faire
|
||||||
|
* tourner, puisque le `docker compose pull` precede toujours `up -d`).
|
||||||
|
* - {@link #check()} re-interroge le registry et compare. Si un digest a
|
||||||
|
* change, une mise a jour est disponible.
|
||||||
|
* - {@link #apply()} POST sur /v1/update de Watchtower (qui doit etre lance
|
||||||
|
* avec WATCHTOWER_HTTP_API_UPDATE=true et le meme token).
|
||||||
|
*
|
||||||
|
* Apres un apply reussi, Watchtower redemarre core => ce service est
|
||||||
|
* re-instancie => baseline re-aligne sur le registry => check renvoie
|
||||||
|
* "pas de MAJ" (etat coherent).
|
||||||
|
*
|
||||||
|
* La feature est <b>desactivee silencieusement</b> si {@code WATCHTOWER_TOKEN}
|
||||||
|
* n'est pas defini : check/apply renvoient des reponses neutres et l'UI
|
||||||
|
* masque le badge / bouton.
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
public class UpdateCheckService {
|
||||||
|
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(UpdateCheckService.class);
|
||||||
|
|
||||||
|
private static final List<MediaType> MANIFEST_ACCEPT = List.of(
|
||||||
|
MediaType.parseMediaType("application/vnd.docker.distribution.manifest.v2+json"),
|
||||||
|
MediaType.parseMediaType("application/vnd.docker.distribution.manifest.list.v2+json"),
|
||||||
|
MediaType.parseMediaType("application/vnd.oci.image.manifest.v1+json"),
|
||||||
|
MediaType.parseMediaType("application/vnd.oci.image.index.v1+json")
|
||||||
|
);
|
||||||
|
|
||||||
|
private final RestTemplate http;
|
||||||
|
private final String registry;
|
||||||
|
private final List<String> images;
|
||||||
|
private final String tag;
|
||||||
|
private final String watchtowerUrl;
|
||||||
|
private final String watchtowerToken;
|
||||||
|
|
||||||
|
private final Map<String, String> baselineDigests = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
public UpdateCheckService(
|
||||||
|
RestTemplateBuilder builder,
|
||||||
|
@Value("${update-check.registry:}") String registry,
|
||||||
|
@Value("${update-check.images:}") String imagesCsv,
|
||||||
|
@Value("${update-check.tag:latest}") String tag,
|
||||||
|
@Value("${update-check.watchtower-url:http://watchtower:8080}") String watchtowerUrl,
|
||||||
|
@Value("${update-check.watchtower-token:}") String watchtowerToken) {
|
||||||
|
this.http = builder
|
||||||
|
.setConnectTimeout(Duration.ofSeconds(5))
|
||||||
|
.setReadTimeout(Duration.ofSeconds(15))
|
||||||
|
.build();
|
||||||
|
this.registry = normalizeRegistry(registry);
|
||||||
|
this.images = parseImages(imagesCsv);
|
||||||
|
this.tag = tag;
|
||||||
|
this.watchtowerUrl = watchtowerUrl;
|
||||||
|
this.watchtowerToken = watchtowerToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
void initBaseline() {
|
||||||
|
if (!isEnabled()) {
|
||||||
|
log.info("Update check disabled (WATCHTOWER_TOKEN not set)");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
log.info("Update check enabled - registry={} images={} tag={}", registry, images, tag);
|
||||||
|
for (String image : images) {
|
||||||
|
try {
|
||||||
|
String digest = fetchRemoteDigest(image);
|
||||||
|
if (digest != null) {
|
||||||
|
baselineDigests.put(image, digest);
|
||||||
|
log.debug("Baseline digest for {} = {}", image, digest);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn("Cannot baseline digest for {}: {}", image, e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isEnabled() {
|
||||||
|
return watchtowerToken != null && !watchtowerToken.isBlank() && !images.isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
public UpdateStatus check() {
|
||||||
|
if (!isEnabled()) {
|
||||||
|
return new UpdateStatus(false, false, List.of(), Instant.now());
|
||||||
|
}
|
||||||
|
List<ImageStatus> statuses = new ArrayList<>();
|
||||||
|
boolean anyUpdate = false;
|
||||||
|
for (String image : images) {
|
||||||
|
String baseline = baselineDigests.get(image);
|
||||||
|
String remote = null;
|
||||||
|
try {
|
||||||
|
remote = fetchRemoteDigest(image);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn("Check failed for {}: {}", image, e.getMessage());
|
||||||
|
}
|
||||||
|
// Si on n'a pas de baseline (echec au boot), on l'aligne maintenant
|
||||||
|
// pour eviter un faux positif "MAJ dispo".
|
||||||
|
if (baseline == null && remote != null) {
|
||||||
|
baselineDigests.put(image, remote);
|
||||||
|
baseline = remote;
|
||||||
|
}
|
||||||
|
boolean updateAvailable = baseline != null && remote != null && !baseline.equals(remote);
|
||||||
|
if (updateAvailable) anyUpdate = true;
|
||||||
|
statuses.add(new ImageStatus(image, baseline, remote, updateAvailable));
|
||||||
|
}
|
||||||
|
return new UpdateStatus(true, anyUpdate, statuses, Instant.now());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void apply() {
|
||||||
|
if (!isEnabled()) {
|
||||||
|
throw new IllegalStateException("Update apply not configured (WATCHTOWER_TOKEN missing)");
|
||||||
|
}
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
headers.setBearerAuth(watchtowerToken);
|
||||||
|
// Watchtower /v1/update declenche un scan+update immediat de tous les
|
||||||
|
// conteneurs labellises. La reponse est synchrone et peut prendre
|
||||||
|
// plusieurs secondes; en cas de redemarrage de core, le client
|
||||||
|
// recevra une connexion coupee — c'est attendu, l'UI le gere.
|
||||||
|
http.exchange(
|
||||||
|
watchtowerUrl + "/v1/update",
|
||||||
|
HttpMethod.POST,
|
||||||
|
new HttpEntity<>(headers),
|
||||||
|
Void.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------
|
||||||
|
// Registry HTTP API v2
|
||||||
|
// -----------------------------------------------------------------------
|
||||||
|
|
||||||
|
private String fetchRemoteDigest(String image) {
|
||||||
|
String url = registry + "/v2/" + image + "/manifests/" + tag;
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
headers.setAccept(MANIFEST_ACCEPT);
|
||||||
|
try {
|
||||||
|
return digestCall(url, headers);
|
||||||
|
} catch (HttpClientErrorException.Unauthorized e) {
|
||||||
|
String www = e.getResponseHeaders() == null ? null
|
||||||
|
: e.getResponseHeaders().getFirst(HttpHeaders.WWW_AUTHENTICATE);
|
||||||
|
String token = obtainBearerToken(www);
|
||||||
|
if (token == null) {
|
||||||
|
log.warn("Cannot obtain bearer token for {} (registry response: {})", image, www);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
headers.setBearerAuth(token);
|
||||||
|
return digestCall(url, headers);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String digestCall(String url, HttpHeaders headers) {
|
||||||
|
ResponseEntity<Void> resp = http.exchange(
|
||||||
|
url, HttpMethod.HEAD, new HttpEntity<>(headers), Void.class);
|
||||||
|
return resp.getHeaders().getFirst("Docker-Content-Digest");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Suit le challenge {@code WWW-Authenticate: Bearer realm="...",service="...",scope="..."}
|
||||||
|
* pour obtenir un jeton (anonyme — suffisant pour les images publiques).
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("rawtypes")
|
||||||
|
private String obtainBearerToken(String wwwAuth) {
|
||||||
|
if (wwwAuth == null) return null;
|
||||||
|
String prefix = "Bearer ";
|
||||||
|
if (!wwwAuth.regionMatches(true, 0, prefix, 0, prefix.length())) return null;
|
||||||
|
Map<String, String> params = parseAuthParams(wwwAuth.substring(prefix.length()));
|
||||||
|
String realm = params.get("realm");
|
||||||
|
if (realm == null) return null;
|
||||||
|
StringBuilder url = new StringBuilder(realm);
|
||||||
|
boolean hasQuery = realm.contains("?");
|
||||||
|
for (String key : new String[]{"service", "scope"}) {
|
||||||
|
String v = params.get(key);
|
||||||
|
if (v != null) {
|
||||||
|
url.append(hasQuery ? '&' : '?')
|
||||||
|
.append(key).append('=')
|
||||||
|
.append(URLEncoder.encode(v, StandardCharsets.UTF_8));
|
||||||
|
hasQuery = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
ResponseEntity<Map> resp = http.getForEntity(url.toString(), Map.class);
|
||||||
|
Map<?, ?> body = resp.getBody();
|
||||||
|
if (body == null) return null;
|
||||||
|
Object t = body.get("token");
|
||||||
|
if (t == null) t = body.get("access_token");
|
||||||
|
return t == null ? null : t.toString();
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn("Bearer token request failed: {}", e.getMessage());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Parser minimaliste pour {@code key="value", key2="value2"}. */
|
||||||
|
private static Map<String, String> parseAuthParams(String s) {
|
||||||
|
Map<String, String> out = new HashMap<>();
|
||||||
|
int i = 0;
|
||||||
|
int n = s.length();
|
||||||
|
while (i < n) {
|
||||||
|
while (i < n && (s.charAt(i) == ',' || s.charAt(i) == ' ')) i++;
|
||||||
|
int eq = s.indexOf('=', i);
|
||||||
|
if (eq < 0) break;
|
||||||
|
String key = s.substring(i, eq).trim();
|
||||||
|
int valStart = eq + 1;
|
||||||
|
String val;
|
||||||
|
if (valStart < n && s.charAt(valStart) == '"') {
|
||||||
|
int valEnd = s.indexOf('"', valStart + 1);
|
||||||
|
if (valEnd < 0) break;
|
||||||
|
val = s.substring(valStart + 1, valEnd);
|
||||||
|
i = valEnd + 1;
|
||||||
|
} else {
|
||||||
|
int valEnd = s.indexOf(',', valStart);
|
||||||
|
if (valEnd < 0) valEnd = n;
|
||||||
|
val = s.substring(valStart, valEnd).trim();
|
||||||
|
i = valEnd;
|
||||||
|
}
|
||||||
|
out.put(key, val);
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String normalizeRegistry(String value) {
|
||||||
|
if (value == null || value.isBlank()) return "";
|
||||||
|
String v = value.trim();
|
||||||
|
if (!v.startsWith("http://") && !v.startsWith("https://")) {
|
||||||
|
v = "https://" + v;
|
||||||
|
}
|
||||||
|
if (v.endsWith("/")) v = v.substring(0, v.length() - 1);
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static List<String> parseImages(String csv) {
|
||||||
|
if (csv == null || csv.isBlank()) return List.of();
|
||||||
|
List<String> out = new ArrayList<>();
|
||||||
|
for (String part : csv.split(",")) {
|
||||||
|
String p = part.trim();
|
||||||
|
if (!p.isEmpty()) out.add(p);
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------
|
||||||
|
// Records de retour (sortis sous forme JSON par Jackson)
|
||||||
|
// -----------------------------------------------------------------------
|
||||||
|
|
||||||
|
public record UpdateStatus(
|
||||||
|
boolean enabled,
|
||||||
|
boolean updateAvailable,
|
||||||
|
List<ImageStatus> images,
|
||||||
|
Instant checkedAt) {}
|
||||||
|
|
||||||
|
public record ImageStatus(
|
||||||
|
String image,
|
||||||
|
String localDigest,
|
||||||
|
String remoteDigest,
|
||||||
|
boolean updateAvailable) {}
|
||||||
|
}
|
||||||
@@ -66,6 +66,7 @@ public class SecurityConfig {
|
|||||||
// Preflight CORS toujours libre (le browser n'envoie pas Authorization sur OPTIONS)
|
// Preflight CORS toujours libre (le browser n'envoie pas Authorization sur OPTIONS)
|
||||||
.requestMatchers(HttpMethod.OPTIONS, "/**").permitAll()
|
.requestMatchers(HttpMethod.OPTIONS, "/**").permitAll()
|
||||||
.requestMatchers("/api/settings/**").hasRole("ADMIN")
|
.requestMatchers("/api/settings/**").hasRole("ADMIN")
|
||||||
|
.requestMatchers("/api/admin/**").hasRole("ADMIN")
|
||||||
.anyRequest().permitAll()
|
.anyRequest().permitAll()
|
||||||
)
|
)
|
||||||
.httpBasic(basic -> {});
|
.httpBasic(basic -> {});
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package com.loremind.infrastructure.web.controller;
|
|||||||
import com.loremind.application.generationcontext.StreamChatForCampaignUseCase;
|
import com.loremind.application.generationcontext.StreamChatForCampaignUseCase;
|
||||||
import com.loremind.application.generationcontext.StreamChatForLoreUseCase;
|
import com.loremind.application.generationcontext.StreamChatForLoreUseCase;
|
||||||
import com.loremind.domain.generationcontext.ChatMessage;
|
import com.loremind.domain.generationcontext.ChatMessage;
|
||||||
|
import com.loremind.domain.generationcontext.ChatUsage;
|
||||||
import com.loremind.infrastructure.web.dto.generationcontext.ChatMessageDTO;
|
import com.loremind.infrastructure.web.dto.generationcontext.ChatMessageDTO;
|
||||||
import com.loremind.infrastructure.web.dto.generationcontext.ChatStreamCampaignRequestDTO;
|
import com.loremind.infrastructure.web.dto.generationcontext.ChatStreamCampaignRequestDTO;
|
||||||
import com.loremind.infrastructure.web.dto.generationcontext.ChatStreamRequestDTO;
|
import com.loremind.infrastructure.web.dto.generationcontext.ChatStreamRequestDTO;
|
||||||
@@ -80,6 +81,7 @@ public class AiChatController {
|
|||||||
try {
|
try {
|
||||||
streamChatForLoreUseCase.execute(
|
streamChatForLoreUseCase.execute(
|
||||||
loreId, pageId, messages,
|
loreId, pageId, messages,
|
||||||
|
usage -> sendUsage(emitter, usage),
|
||||||
token -> sendToken(emitter, token),
|
token -> sendToken(emitter, token),
|
||||||
() -> complete(emitter),
|
() -> complete(emitter),
|
||||||
error -> fail(emitter, error));
|
error -> fail(emitter, error));
|
||||||
@@ -100,6 +102,7 @@ public class AiChatController {
|
|||||||
try {
|
try {
|
||||||
streamChatForCampaignUseCase.execute(
|
streamChatForCampaignUseCase.execute(
|
||||||
campaignId, entityType, entityId, messages,
|
campaignId, entityType, entityId, messages,
|
||||||
|
usage -> sendUsage(emitter, usage),
|
||||||
token -> sendToken(emitter, token),
|
token -> sendToken(emitter, token),
|
||||||
() -> complete(emitter),
|
() -> complete(emitter),
|
||||||
error -> fail(emitter, error));
|
error -> fail(emitter, error));
|
||||||
@@ -110,6 +113,18 @@ public class AiChatController {
|
|||||||
|
|
||||||
// --- Helpers SSE (un seul point d'écriture par type d'événement) --------
|
// --- Helpers SSE (un seul point d'écriture par type d'événement) --------
|
||||||
|
|
||||||
|
private void sendUsage(SseEmitter emitter, ChatUsage usage) {
|
||||||
|
try {
|
||||||
|
String payload = "{\"system\":" + usage.system()
|
||||||
|
+ ",\"history\":" + usage.history()
|
||||||
|
+ ",\"current\":" + usage.current()
|
||||||
|
+ ",\"max\":" + usage.max() + "}";
|
||||||
|
emitter.send(SseEmitter.event().name("usage").data(payload));
|
||||||
|
} catch (IOException e) {
|
||||||
|
emitter.completeWithError(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void sendToken(SseEmitter emitter, String token) {
|
private void sendToken(SseEmitter emitter, String token) {
|
||||||
try {
|
try {
|
||||||
emitter.send(SseEmitter.event()
|
emitter.send(SseEmitter.event()
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ public class ArcController {
|
|||||||
@PostMapping
|
@PostMapping
|
||||||
public ResponseEntity<ArcDTO> createArc(@RequestBody ArcDTO arcDTO) {
|
public ResponseEntity<ArcDTO> createArc(@RequestBody ArcDTO arcDTO) {
|
||||||
Arc arc = arcMapper.toDomain(arcDTO);
|
Arc arc = arcMapper.toDomain(arcDTO);
|
||||||
Arc createdArc = arcService.createArc(arc.getName(), arc.getDescription(), arc.getCampaignId(), arc.getOrder());
|
Arc createdArc = arcService.createArc(arc.getName(), arc.getDescription(), arc.getCampaignId(), arc.getOrder(), arc.getIcon());
|
||||||
return ResponseEntity.ok(arcMapper.toDTO(createdArc));
|
return ResponseEntity.ok(arcMapper.toDTO(createdArc));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,17 +40,11 @@ public class ArcController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping
|
@GetMapping
|
||||||
public ResponseEntity<List<ArcDTO>> getAllArcs() {
|
public ResponseEntity<List<ArcDTO>> getAllArcs(
|
||||||
List<Arc> arcs = arcService.getAllArcs();
|
@RequestParam(value = "campaignId", required = false) String campaignId) {
|
||||||
List<ArcDTO> arcDTOs = arcs.stream()
|
List<Arc> arcs = (campaignId != null && !campaignId.isBlank())
|
||||||
.map(arcMapper::toDTO)
|
? arcService.getArcsByCampaignId(campaignId)
|
||||||
.collect(Collectors.toList());
|
: arcService.getAllArcs();
|
||||||
return ResponseEntity.ok(arcDTOs);
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping("/campaign/{campaignId}")
|
|
||||||
public ResponseEntity<List<ArcDTO>> getArcsByCampaignId(@PathVariable String campaignId) {
|
|
||||||
List<Arc> arcs = arcService.getArcsByCampaignId(campaignId);
|
|
||||||
List<ArcDTO> arcDTOs = arcs.stream()
|
List<ArcDTO> arcDTOs = arcs.stream()
|
||||||
.map(arcMapper::toDTO)
|
.map(arcMapper::toDTO)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
@@ -68,4 +62,12 @@ public class ArcController {
|
|||||||
arcService.deleteArc(id);
|
arcService.deleteArc(id);
|
||||||
return ResponseEntity.noContent().build();
|
return ResponseEntity.noContent().build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("/{id}/deletion-impact")
|
||||||
|
public ResponseEntity<ArcService.DeletionImpact> getDeletionImpact(@PathVariable String id) {
|
||||||
|
if (!arcService.arcExists(id)) {
|
||||||
|
return ResponseEntity.notFound().build();
|
||||||
|
}
|
||||||
|
return ResponseEntity.ok(arcService.getDeletionImpact(id));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ public class CampaignController {
|
|||||||
public ResponseEntity<CampaignDTO> createCampaign(@RequestBody CampaignDTO campaignDTO) {
|
public ResponseEntity<CampaignDTO> createCampaign(@RequestBody CampaignDTO campaignDTO) {
|
||||||
Campaign campaign = campaignMapper.toDomain(campaignDTO);
|
Campaign campaign = campaignMapper.toDomain(campaignDTO);
|
||||||
Campaign createdCampaign = campaignService.createCampaign(
|
Campaign createdCampaign = campaignService.createCampaign(
|
||||||
new CampaignService.CampaignData(campaign.getName(), campaign.getDescription(), campaign.getLoreId())
|
new CampaignService.CampaignData(campaign.getName(), campaign.getDescription(), campaign.getLoreId(), campaign.getGameSystemId())
|
||||||
);
|
);
|
||||||
return ResponseEntity.ok(campaignMapper.toDTO(createdCampaign));
|
return ResponseEntity.ok(campaignMapper.toDTO(createdCampaign));
|
||||||
}
|
}
|
||||||
@@ -64,7 +64,7 @@ public class CampaignController {
|
|||||||
public ResponseEntity<CampaignDTO> updateCampaign(@PathVariable String id, @RequestBody CampaignDTO campaignDTO) {
|
public ResponseEntity<CampaignDTO> updateCampaign(@PathVariable String id, @RequestBody CampaignDTO campaignDTO) {
|
||||||
Campaign updatedCampaign = campaignService.updateCampaign(
|
Campaign updatedCampaign = campaignService.updateCampaign(
|
||||||
id,
|
id,
|
||||||
new CampaignService.CampaignData(campaignDTO.getName(), campaignDTO.getDescription(), campaignDTO.getLoreId())
|
new CampaignService.CampaignData(campaignDTO.getName(), campaignDTO.getDescription(), campaignDTO.getLoreId(), campaignDTO.getGameSystemId())
|
||||||
);
|
);
|
||||||
return ResponseEntity.ok(campaignMapper.toDTO(updatedCampaign));
|
return ResponseEntity.ok(campaignMapper.toDTO(updatedCampaign));
|
||||||
}
|
}
|
||||||
@@ -74,4 +74,16 @@ public class CampaignController {
|
|||||||
campaignService.deleteCampaign(id);
|
campaignService.deleteCampaign(id);
|
||||||
return ResponseEntity.noContent().build();
|
return ResponseEntity.noContent().build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Récapitulatif des entités qui seront supprimées en cascade : utilisé par
|
||||||
|
* l'UI pour afficher "X arcs, Y chapitres, Z scènes..." dans la confirmation.
|
||||||
|
*/
|
||||||
|
@GetMapping("/{id}/deletion-impact")
|
||||||
|
public ResponseEntity<CampaignService.DeletionImpact> getDeletionImpact(@PathVariable String id) {
|
||||||
|
if (!campaignService.campaignExists(id)) {
|
||||||
|
return ResponseEntity.notFound().build();
|
||||||
|
}
|
||||||
|
return ResponseEntity.ok(campaignService.getDeletionImpact(id));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ public class ChapterController {
|
|||||||
@PostMapping
|
@PostMapping
|
||||||
public ResponseEntity<ChapterDTO> createChapter(@RequestBody ChapterDTO chapterDTO) {
|
public ResponseEntity<ChapterDTO> createChapter(@RequestBody ChapterDTO chapterDTO) {
|
||||||
Chapter chapter = chapterMapper.toDomain(chapterDTO);
|
Chapter chapter = chapterMapper.toDomain(chapterDTO);
|
||||||
Chapter createdChapter = chapterService.createChapter(chapter.getName(), chapter.getDescription(), chapter.getArcId(), chapter.getOrder());
|
Chapter createdChapter = chapterService.createChapter(chapter.getName(), chapter.getDescription(), chapter.getArcId(), chapter.getOrder(), chapter.getIcon());
|
||||||
return ResponseEntity.ok(chapterMapper.toDTO(createdChapter));
|
return ResponseEntity.ok(chapterMapper.toDTO(createdChapter));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,17 +40,11 @@ public class ChapterController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping
|
@GetMapping
|
||||||
public ResponseEntity<List<ChapterDTO>> getAllChapters() {
|
public ResponseEntity<List<ChapterDTO>> getAllChapters(
|
||||||
List<Chapter> chapters = chapterService.getAllChapters();
|
@RequestParam(value = "arcId", required = false) String arcId) {
|
||||||
List<ChapterDTO> chapterDTOs = chapters.stream()
|
List<Chapter> chapters = (arcId != null && !arcId.isBlank())
|
||||||
.map(chapterMapper::toDTO)
|
? chapterService.getChaptersByArcId(arcId)
|
||||||
.collect(Collectors.toList());
|
: chapterService.getAllChapters();
|
||||||
return ResponseEntity.ok(chapterDTOs);
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping("/arc/{arcId}")
|
|
||||||
public ResponseEntity<List<ChapterDTO>> getChaptersByArcId(@PathVariable String arcId) {
|
|
||||||
List<Chapter> chapters = chapterService.getChaptersByArcId(arcId);
|
|
||||||
List<ChapterDTO> chapterDTOs = chapters.stream()
|
List<ChapterDTO> chapterDTOs = chapters.stream()
|
||||||
.map(chapterMapper::toDTO)
|
.map(chapterMapper::toDTO)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
@@ -68,4 +62,12 @@ public class ChapterController {
|
|||||||
chapterService.deleteChapter(id);
|
chapterService.deleteChapter(id);
|
||||||
return ResponseEntity.noContent().build();
|
return ResponseEntity.noContent().build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("/{id}/deletion-impact")
|
||||||
|
public ResponseEntity<ChapterService.DeletionImpact> getDeletionImpact(@PathVariable String id) {
|
||||||
|
if (!chapterService.chapterExists(id)) {
|
||||||
|
return ResponseEntity.notFound().build();
|
||||||
|
}
|
||||||
|
return ResponseEntity.ok(chapterService.getDeletionImpact(id));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,62 @@
|
|||||||
|
package com.loremind.infrastructure.web.controller;
|
||||||
|
|
||||||
|
import com.loremind.application.campaigncontext.CharacterService;
|
||||||
|
import com.loremind.domain.campaigncontext.Character;
|
||||||
|
import com.loremind.infrastructure.web.dto.campaigncontext.CharacterDTO;
|
||||||
|
import com.loremind.infrastructure.web.mapper.CharacterMapper;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/characters")
|
||||||
|
public class CharacterController {
|
||||||
|
|
||||||
|
private final CharacterService characterService;
|
||||||
|
private final CharacterMapper characterMapper;
|
||||||
|
|
||||||
|
public CharacterController(CharacterService characterService, CharacterMapper characterMapper) {
|
||||||
|
this.characterService = characterService;
|
||||||
|
this.characterMapper = characterMapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping
|
||||||
|
public ResponseEntity<CharacterDTO> createCharacter(@RequestBody CharacterDTO dto) {
|
||||||
|
Character created = characterService.createCharacter(
|
||||||
|
new CharacterService.CharacterData(dto.getName(), dto.getMarkdownContent(), dto.getCampaignId(), null)
|
||||||
|
);
|
||||||
|
return ResponseEntity.ok(characterMapper.toDTO(created));
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/{id}")
|
||||||
|
public ResponseEntity<CharacterDTO> getCharacterById(@PathVariable String id) {
|
||||||
|
return characterService.getCharacterById(id)
|
||||||
|
.map(c -> ResponseEntity.ok(characterMapper.toDTO(c)))
|
||||||
|
.orElse(ResponseEntity.notFound().build());
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/campaign/{campaignId}")
|
||||||
|
public ResponseEntity<List<CharacterDTO>> getCharactersByCampaign(@PathVariable String campaignId) {
|
||||||
|
List<CharacterDTO> dtos = characterService.getCharactersByCampaignId(campaignId).stream()
|
||||||
|
.map(characterMapper::toDTO)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
return ResponseEntity.ok(dtos);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PutMapping("/{id}")
|
||||||
|
public ResponseEntity<CharacterDTO> updateCharacter(@PathVariable String id, @RequestBody CharacterDTO dto) {
|
||||||
|
Character updated = characterService.updateCharacter(
|
||||||
|
id,
|
||||||
|
new CharacterService.CharacterData(dto.getName(), dto.getMarkdownContent(), dto.getCampaignId(), dto.getOrder())
|
||||||
|
);
|
||||||
|
return ResponseEntity.ok(characterMapper.toDTO(updated));
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("/{id}")
|
||||||
|
public ResponseEntity<Void> deleteCharacter(@PathVariable String id) {
|
||||||
|
characterService.deleteCharacter(id);
|
||||||
|
return ResponseEntity.noContent().build();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package com.loremind.infrastructure.web.controller;
|
||||||
|
|
||||||
|
import com.loremind.infrastructure.updates.UpdateCheckService;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Expose la configuration publique consommee par le frontend au demarrage.
|
||||||
|
* Activer le mode demo via la variable d'env DEMO_MODE=true : le front
|
||||||
|
* masque alors Settings / Export VTT, et les endpoints sensibles sont
|
||||||
|
* verrouilles cote serveur (cf. SettingsController).
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/config")
|
||||||
|
public class ConfigController {
|
||||||
|
|
||||||
|
private final boolean demoMode;
|
||||||
|
private final UpdateCheckService updates;
|
||||||
|
|
||||||
|
public ConfigController(@Value("${app.demo-mode:false}") boolean demoMode,
|
||||||
|
UpdateCheckService updates) {
|
||||||
|
this.demoMode = demoMode;
|
||||||
|
this.updates = updates;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
public Map<String, Object> getPublicConfig() {
|
||||||
|
return Map.of(
|
||||||
|
"demoMode", demoMode,
|
||||||
|
"updateCheckEnabled", updates.isEnabled());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,100 @@
|
|||||||
|
package com.loremind.infrastructure.web.controller;
|
||||||
|
|
||||||
|
import com.loremind.application.conversationcontext.ConversationService;
|
||||||
|
import com.loremind.domain.conversationcontext.Conversation;
|
||||||
|
import com.loremind.domain.conversationcontext.ConversationMessage;
|
||||||
|
import com.loremind.infrastructure.web.dto.conversationcontext.AppendMessageDTO;
|
||||||
|
import com.loremind.infrastructure.web.dto.conversationcontext.ConversationDTO;
|
||||||
|
import com.loremind.infrastructure.web.dto.conversationcontext.ConversationMessageDTO;
|
||||||
|
import com.loremind.infrastructure.web.dto.conversationcontext.CreateConversationDTO;
|
||||||
|
import com.loremind.infrastructure.web.dto.conversationcontext.RenameConversationDTO;
|
||||||
|
import com.loremind.infrastructure.web.mapper.ConversationMapper;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* API REST des conversations persistees.
|
||||||
|
*
|
||||||
|
* GET /api/conversations?loreId=...&entityType=...&entityId=... (listing filtre)
|
||||||
|
* GET /api/conversations?campaignId=...&entityType=...&entityId=...
|
||||||
|
* GET /api/conversations/{id} (detail + messages)
|
||||||
|
* POST /api/conversations (create)
|
||||||
|
* PATCH /api/conversations/{id}/title (rename)
|
||||||
|
* DELETE /api/conversations/{id}
|
||||||
|
*
|
||||||
|
* L'ajout de messages est piloje cote chat stream (use case dedie),
|
||||||
|
* pas par ce controller.
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/conversations")
|
||||||
|
public class ConversationController {
|
||||||
|
|
||||||
|
private final ConversationService service;
|
||||||
|
private final ConversationMapper mapper;
|
||||||
|
|
||||||
|
public ConversationController(ConversationService service, ConversationMapper mapper) {
|
||||||
|
this.service = service;
|
||||||
|
this.mapper = mapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
public ResponseEntity<List<ConversationDTO>> list(
|
||||||
|
@RequestParam(required = false) String loreId,
|
||||||
|
@RequestParam(required = false) String campaignId,
|
||||||
|
@RequestParam(required = false) String entityType,
|
||||||
|
@RequestParam(required = false) String entityId) {
|
||||||
|
List<Conversation> rows = service.listByContext(loreId, campaignId, entityType, entityId);
|
||||||
|
return ResponseEntity.ok(rows.stream().map(mapper::toListDTO).collect(Collectors.toList()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/{id}")
|
||||||
|
public ResponseEntity<ConversationDTO> getById(@PathVariable String id) {
|
||||||
|
return service.getById(id)
|
||||||
|
.map(c -> ResponseEntity.ok(mapper.toDTO(c)))
|
||||||
|
.orElse(ResponseEntity.notFound().build());
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping
|
||||||
|
public ResponseEntity<ConversationDTO> create(@RequestBody CreateConversationDTO dto) {
|
||||||
|
Conversation created = service.create(new ConversationService.CreateData(
|
||||||
|
dto.getTitle(),
|
||||||
|
dto.getLoreId(),
|
||||||
|
dto.getCampaignId(),
|
||||||
|
dto.getEntityType(),
|
||||||
|
dto.getEntityId()));
|
||||||
|
return ResponseEntity.ok(mapper.toDTO(created));
|
||||||
|
}
|
||||||
|
|
||||||
|
@PatchMapping("/{id}/title")
|
||||||
|
public ResponseEntity<Void> rename(@PathVariable String id, @RequestBody RenameConversationDTO dto) {
|
||||||
|
service.rename(id, dto.getTitle());
|
||||||
|
return ResponseEntity.noContent().build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("/{id}")
|
||||||
|
public ResponseEntity<Void> delete(@PathVariable String id) {
|
||||||
|
service.delete(id);
|
||||||
|
return ResponseEntity.noContent().build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/{id}/messages")
|
||||||
|
public ResponseEntity<ConversationMessageDTO> appendMessage(
|
||||||
|
@PathVariable String id,
|
||||||
|
@RequestBody AppendMessageDTO dto) {
|
||||||
|
ConversationMessage saved = service.appendMessage(id, dto.getRole(), dto.getContent());
|
||||||
|
return ResponseEntity.ok(mapper.toMessageDTO(saved));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Auto-genere et persiste un titre base sur les premiers messages.
|
||||||
|
* Appele par le front apres le 1er couple user/assistant.
|
||||||
|
*/
|
||||||
|
@PostMapping("/{id}/auto-title")
|
||||||
|
public ResponseEntity<RenameConversationDTO> autoTitle(@PathVariable String id) {
|
||||||
|
String title = service.autoGenerateTitle(id);
|
||||||
|
return ResponseEntity.ok(RenameConversationDTO.builder().title(title).build());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,75 @@
|
|||||||
|
package com.loremind.infrastructure.web.controller;
|
||||||
|
|
||||||
|
import com.loremind.application.gamesystemcontext.GameSystemService;
|
||||||
|
import com.loremind.domain.gamesystemcontext.GameSystem;
|
||||||
|
import com.loremind.infrastructure.web.dto.gamesystemcontext.GameSystemDTO;
|
||||||
|
import com.loremind.infrastructure.web.mapper.GameSystemMapper;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/game-systems")
|
||||||
|
public class GameSystemController {
|
||||||
|
|
||||||
|
private final GameSystemService gameSystemService;
|
||||||
|
private final GameSystemMapper gameSystemMapper;
|
||||||
|
|
||||||
|
public GameSystemController(GameSystemService gameSystemService, GameSystemMapper gameSystemMapper) {
|
||||||
|
this.gameSystemService = gameSystemService;
|
||||||
|
this.gameSystemMapper = gameSystemMapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping
|
||||||
|
public ResponseEntity<GameSystemDTO> createGameSystem(@RequestBody GameSystemDTO dto) {
|
||||||
|
GameSystem created = gameSystemService.createGameSystem(toData(dto));
|
||||||
|
return ResponseEntity.ok(gameSystemMapper.toDTO(created));
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/{id}")
|
||||||
|
public ResponseEntity<GameSystemDTO> getGameSystemById(@PathVariable String id) {
|
||||||
|
return gameSystemService.getGameSystemById(id)
|
||||||
|
.map(g -> ResponseEntity.ok(gameSystemMapper.toDTO(g)))
|
||||||
|
.orElse(ResponseEntity.notFound().build());
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
public ResponseEntity<List<GameSystemDTO>> getAllGameSystems() {
|
||||||
|
List<GameSystemDTO> dtos = gameSystemService.getAllGameSystems().stream()
|
||||||
|
.map(gameSystemMapper::toDTO)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
return ResponseEntity.ok(dtos);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/search")
|
||||||
|
public ResponseEntity<List<GameSystemDTO>> searchGameSystems(@RequestParam("q") String query) {
|
||||||
|
List<GameSystemDTO> dtos = gameSystemService.searchGameSystems(query).stream()
|
||||||
|
.map(gameSystemMapper::toDTO)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
return ResponseEntity.ok(dtos);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PutMapping("/{id}")
|
||||||
|
public ResponseEntity<GameSystemDTO> updateGameSystem(@PathVariable String id, @RequestBody GameSystemDTO dto) {
|
||||||
|
GameSystem updated = gameSystemService.updateGameSystem(id, toData(dto));
|
||||||
|
return ResponseEntity.ok(gameSystemMapper.toDTO(updated));
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("/{id}")
|
||||||
|
public ResponseEntity<Void> deleteGameSystem(@PathVariable String id) {
|
||||||
|
gameSystemService.deleteGameSystem(id);
|
||||||
|
return ResponseEntity.noContent().build();
|
||||||
|
}
|
||||||
|
|
||||||
|
private GameSystemService.GameSystemData toData(GameSystemDTO dto) {
|
||||||
|
return new GameSystemService.GameSystemData(
|
||||||
|
dto.getName(),
|
||||||
|
dto.getDescription(),
|
||||||
|
dto.getRulesMarkdown(),
|
||||||
|
dto.getAuthor(),
|
||||||
|
dto.isPublic()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -69,4 +69,17 @@ public class LoreController {
|
|||||||
loreService.deleteLore(id);
|
loreService.deleteLore(id);
|
||||||
return ResponseEntity.noContent().build();
|
return ResponseEntity.noContent().build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Récapitulatif des entités qui seront supprimées / détachées en cascade.
|
||||||
|
* Utilisé par l'UI pour afficher "X dossiers, Y pages, Z templates,
|
||||||
|
* N campagne(s) détachée(s)" dans la confirmation.
|
||||||
|
*/
|
||||||
|
@GetMapping("/{id}/deletion-impact")
|
||||||
|
public ResponseEntity<LoreService.DeletionImpact> getDeletionImpact(@PathVariable String id) {
|
||||||
|
if (loreService.getLoreById(id).isEmpty()) {
|
||||||
|
return ResponseEntity.notFound().build();
|
||||||
|
}
|
||||||
|
return ResponseEntity.ok(loreService.getDeletionImpact(id));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -97,4 +97,16 @@ public class LoreNodeController {
|
|||||||
loreNodeService.deleteLoreNode(id);
|
loreNodeService.deleteLoreNode(id);
|
||||||
return ResponseEntity.noContent().build();
|
return ResponseEntity.noContent().build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Récapitulatif des entités qui seront supprimées en cascade : utilisé par
|
||||||
|
* l'UI pour afficher "X sous-dossiers, Y pages..." dans la confirmation.
|
||||||
|
*/
|
||||||
|
@GetMapping("/{id}/deletion-impact")
|
||||||
|
public ResponseEntity<LoreNodeService.DeletionImpact> getDeletionImpact(@PathVariable String id) {
|
||||||
|
if (loreNodeService.getLoreNodeById(id).isEmpty()) {
|
||||||
|
return ResponseEntity.notFound().build();
|
||||||
|
}
|
||||||
|
return ResponseEntity.ok(loreNodeService.getDeletionImpact(id));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ public class SceneController {
|
|||||||
@PostMapping
|
@PostMapping
|
||||||
public ResponseEntity<SceneDTO> createScene(@RequestBody SceneDTO sceneDTO) {
|
public ResponseEntity<SceneDTO> createScene(@RequestBody SceneDTO sceneDTO) {
|
||||||
Scene scene = sceneMapper.toDomain(sceneDTO);
|
Scene scene = sceneMapper.toDomain(sceneDTO);
|
||||||
Scene createdScene = sceneService.createScene(scene.getName(), scene.getDescription(), scene.getChapterId(), scene.getOrder());
|
Scene createdScene = sceneService.createScene(scene.getName(), scene.getDescription(), scene.getChapterId(), scene.getOrder(), scene.getIcon());
|
||||||
return ResponseEntity.ok(sceneMapper.toDTO(createdScene));
|
return ResponseEntity.ok(sceneMapper.toDTO(createdScene));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,17 +40,11 @@ public class SceneController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping
|
@GetMapping
|
||||||
public ResponseEntity<List<SceneDTO>> getAllScenes() {
|
public ResponseEntity<List<SceneDTO>> getAllScenes(
|
||||||
List<Scene> scenes = sceneService.getAllScenes();
|
@RequestParam(value = "chapterId", required = false) String chapterId) {
|
||||||
List<SceneDTO> sceneDTOs = scenes.stream()
|
List<Scene> scenes = (chapterId != null && !chapterId.isBlank())
|
||||||
.map(sceneMapper::toDTO)
|
? sceneService.getScenesByChapterId(chapterId)
|
||||||
.collect(Collectors.toList());
|
: sceneService.getAllScenes();
|
||||||
return ResponseEntity.ok(sceneDTOs);
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping("/chapter/{chapterId}")
|
|
||||||
public ResponseEntity<List<SceneDTO>> getScenesByChapterId(@PathVariable String chapterId) {
|
|
||||||
List<Scene> scenes = sceneService.getScenesByChapterId(chapterId);
|
|
||||||
List<SceneDTO> sceneDTOs = scenes.stream()
|
List<SceneDTO> sceneDTOs = scenes.stream()
|
||||||
.map(sceneMapper::toDTO)
|
.map(sceneMapper::toDTO)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
|||||||
@@ -4,14 +4,17 @@ import org.springframework.beans.factory.annotation.Value;
|
|||||||
import org.springframework.http.HttpEntity;
|
import org.springframework.http.HttpEntity;
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.http.HttpMethod;
|
import org.springframework.http.HttpMethod;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.PutMapping;
|
import org.springframework.web.bind.annotation.PutMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
import org.springframework.web.server.ResponseStatusException;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@@ -31,20 +34,25 @@ public class SettingsController {
|
|||||||
|
|
||||||
private final RestTemplate restTemplate;
|
private final RestTemplate restTemplate;
|
||||||
private final String brainBaseUrl;
|
private final String brainBaseUrl;
|
||||||
|
private final boolean demoMode;
|
||||||
|
|
||||||
public SettingsController(RestTemplate restTemplate,
|
public SettingsController(RestTemplate restTemplate,
|
||||||
@Value("${brain.base-url}") String brainBaseUrl) {
|
@Value("${brain.base-url}") String brainBaseUrl,
|
||||||
|
@Value("${app.demo-mode:false}") boolean demoMode) {
|
||||||
this.restTemplate = restTemplate;
|
this.restTemplate = restTemplate;
|
||||||
this.brainBaseUrl = brainBaseUrl;
|
this.brainBaseUrl = brainBaseUrl;
|
||||||
|
this.demoMode = demoMode;
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping
|
@GetMapping
|
||||||
public ResponseEntity<Map<String, Object>> getSettings() {
|
public ResponseEntity<Map<String, Object>> getSettings() {
|
||||||
|
guardDemoMode();
|
||||||
return forward(HttpMethod.GET, "/settings", null);
|
return forward(HttpMethod.GET, "/settings", null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PutMapping
|
@PutMapping
|
||||||
public ResponseEntity<Map<String, Object>> updateSettings(@RequestBody Map<String, Object> patch) {
|
public ResponseEntity<Map<String, Object>> updateSettings(@RequestBody Map<String, Object> patch) {
|
||||||
|
guardDemoMode();
|
||||||
return forward(HttpMethod.PUT, "/settings", patch);
|
return forward(HttpMethod.PUT, "/settings", patch);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,11 +61,22 @@ public class SettingsController {
|
|||||||
return forward(HttpMethod.GET, "/models/ollama", null);
|
return forward(HttpMethod.GET, "/models/ollama", null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping("/models/ollama/info")
|
||||||
|
public ResponseEntity<Map<String, Object>> getOllamaModelInfo(@RequestBody Map<String, Object> body) {
|
||||||
|
return forward(HttpMethod.POST, "/models/ollama/info", body);
|
||||||
|
}
|
||||||
|
|
||||||
@GetMapping("/models/onemin")
|
@GetMapping("/models/onemin")
|
||||||
public ResponseEntity<Map<String, Object>> listOneMinModels() {
|
public ResponseEntity<Map<String, Object>> listOneMinModels() {
|
||||||
return forward(HttpMethod.GET, "/models/onemin", null);
|
return forward(HttpMethod.GET, "/models/onemin", null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void guardDemoMode() {
|
||||||
|
if (demoMode) {
|
||||||
|
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "Settings disabled in demo mode");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||||
private ResponseEntity<Map<String, Object>> forward(HttpMethod method, String path, Object body) {
|
private ResponseEntity<Map<String, Object>> forward(HttpMethod method, String path, Object body) {
|
||||||
HttpHeaders headers = new HttpHeaders();
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
|||||||
@@ -0,0 +1,76 @@
|
|||||||
|
package com.loremind.infrastructure.web.controller;
|
||||||
|
|
||||||
|
import com.loremind.infrastructure.updates.UpdateCheckService;
|
||||||
|
import com.loremind.infrastructure.updates.UpdateCheckService.UpdateStatus;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import org.springframework.web.server.ResponseStatusException;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Endpoints admin pour la verification et le declenchement des mises a jour
|
||||||
|
* des conteneurs LoreMind (core/brain/web).
|
||||||
|
*
|
||||||
|
* Protege par HTTP Basic via SecurityConfig (path /api/admin/**).
|
||||||
|
* Si la feature n'est pas configuree (WATCHTOWER_TOKEN vide), check renvoie
|
||||||
|
* {enabled:false} et apply repond 503.
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/admin/updates")
|
||||||
|
public class UpdatesController {
|
||||||
|
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(UpdatesController.class);
|
||||||
|
|
||||||
|
private final UpdateCheckService updates;
|
||||||
|
private final boolean demoMode;
|
||||||
|
|
||||||
|
public UpdatesController(UpdateCheckService updates,
|
||||||
|
@Value("${app.demo-mode:false}") boolean demoMode) {
|
||||||
|
this.updates = updates;
|
||||||
|
this.demoMode = demoMode;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/check")
|
||||||
|
public UpdateStatus check() {
|
||||||
|
guardDemoMode();
|
||||||
|
return updates.check();
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/apply")
|
||||||
|
public ResponseEntity<Map<String, Object>> apply() {
|
||||||
|
guardDemoMode();
|
||||||
|
if (!updates.isEnabled()) {
|
||||||
|
return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE)
|
||||||
|
.body(Map.of("error", "Update apply not configured"));
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
updates.apply();
|
||||||
|
return ResponseEntity.accepted()
|
||||||
|
.body(Map.of("status", "triggered",
|
||||||
|
"message", "Watchtower va telecharger et redemarrer les conteneurs."));
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("Apply update failed", e);
|
||||||
|
return ResponseEntity.status(HttpStatus.BAD_GATEWAY)
|
||||||
|
.body(Map.of("error", "Watchtower unreachable: " + e.getMessage()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* En mode demo, les instances ne doivent pas se mettre a jour ni meme
|
||||||
|
* exposer leur statut (pas de surface d'attaque, et pas de redemarrage
|
||||||
|
* intempestif d'une demo en cours). Cohérent avec SettingsController.
|
||||||
|
*/
|
||||||
|
private void guardDemoMode() {
|
||||||
|
if (demoMode) {
|
||||||
|
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "Updates disabled in demo mode");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -17,6 +17,9 @@ public class ArcDTO {
|
|||||||
private String campaignId;
|
private String campaignId;
|
||||||
private int order;
|
private int order;
|
||||||
|
|
||||||
|
/** Cle d'icone (cf. CAMPAIGN_ICON_OPTIONS cote front). */
|
||||||
|
private String icon;
|
||||||
|
|
||||||
// Champs narratifs enrichis
|
// Champs narratifs enrichis
|
||||||
private String themes;
|
private String themes;
|
||||||
private String stakes;
|
private String stakes;
|
||||||
|
|||||||
@@ -15,4 +15,6 @@ public class CampaignDTO {
|
|||||||
private int arcsCount;
|
private int arcsCount;
|
||||||
/** Nullable : campagne sans univers associé. */
|
/** Nullable : campagne sans univers associé. */
|
||||||
private String loreId;
|
private String loreId;
|
||||||
|
/** Nullable : campagne sans système de JDR associé (générique). */
|
||||||
|
private String gameSystemId;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,6 +17,9 @@ public class ChapterDTO {
|
|||||||
private String arcId;
|
private String arcId;
|
||||||
private int order;
|
private int order;
|
||||||
|
|
||||||
|
/** Cle d'icone (cf. CAMPAIGN_ICON_OPTIONS cote front). */
|
||||||
|
private String icon;
|
||||||
|
|
||||||
// Champs narratifs enrichis
|
// Champs narratifs enrichis
|
||||||
private String gmNotes;
|
private String gmNotes;
|
||||||
private String playerObjectives;
|
private String playerObjectives;
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package com.loremind.infrastructure.web.dto.campaigncontext;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* DTO pour les fiches de personnages (PJ) d'une campagne.
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class CharacterDTO {
|
||||||
|
|
||||||
|
private String id;
|
||||||
|
private String name;
|
||||||
|
private String markdownContent;
|
||||||
|
private String campaignId;
|
||||||
|
private int order;
|
||||||
|
}
|
||||||
@@ -17,6 +17,9 @@ public class SceneDTO {
|
|||||||
private String chapterId;
|
private String chapterId;
|
||||||
private int order;
|
private int order;
|
||||||
|
|
||||||
|
/** Cle d'icone (cf. CAMPAIGN_ICON_OPTIONS cote front). */
|
||||||
|
private String icon;
|
||||||
|
|
||||||
// Champs narratifs enrichis
|
// Champs narratifs enrichis
|
||||||
private String location;
|
private String location;
|
||||||
private String timing;
|
private String timing;
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package com.loremind.infrastructure.web.dto.conversationcontext;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class AppendMessageDTO {
|
||||||
|
/** "user" | "assistant" | "system". */
|
||||||
|
private String role;
|
||||||
|
private String content;
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package com.loremind.infrastructure.web.dto.conversationcontext;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* DTO d'une conversation. Les messages sont inclus uniquement sur GET /{id}
|
||||||
|
* (null pour les reponses de listing afin d'alleger la sidebar).
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class ConversationDTO {
|
||||||
|
private String id;
|
||||||
|
private String title;
|
||||||
|
private String loreId;
|
||||||
|
private String campaignId;
|
||||||
|
private String entityType;
|
||||||
|
private String entityId;
|
||||||
|
private LocalDateTime createdAt;
|
||||||
|
private LocalDateTime updatedAt;
|
||||||
|
private List<ConversationMessageDTO> messages;
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package com.loremind.infrastructure.web.dto.conversationcontext;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class ConversationMessageDTO {
|
||||||
|
private String id;
|
||||||
|
private String role;
|
||||||
|
private String content;
|
||||||
|
private LocalDateTime createdAt;
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package com.loremind.infrastructure.web.dto.conversationcontext;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Payload de creation. Le client fournit l'ancrage (lore ou campagne, +/-
|
||||||
|
* entite focus). Le titre est optionnel — sera auto-genere apres le 1er
|
||||||
|
* echange IA si absent.
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class CreateConversationDTO {
|
||||||
|
private String title;
|
||||||
|
private String loreId;
|
||||||
|
private String campaignId;
|
||||||
|
private String entityType;
|
||||||
|
private String entityId;
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package com.loremind.infrastructure.web.dto.conversationcontext;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class RenameConversationDTO {
|
||||||
|
private String title;
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package com.loremind.infrastructure.web.dto.gamesystemcontext;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* DTO pour l'entité GameSystem (système de JDR).
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class GameSystemDTO {
|
||||||
|
|
||||||
|
private String id;
|
||||||
|
private String name;
|
||||||
|
private String description;
|
||||||
|
private String rulesMarkdown;
|
||||||
|
private String author;
|
||||||
|
private boolean isPublic;
|
||||||
|
}
|
||||||
@@ -24,6 +24,7 @@ public class ArcMapper {
|
|||||||
dto.setDescription(arc.getDescription());
|
dto.setDescription(arc.getDescription());
|
||||||
dto.setCampaignId(arc.getCampaignId());
|
dto.setCampaignId(arc.getCampaignId());
|
||||||
dto.setOrder(arc.getOrder());
|
dto.setOrder(arc.getOrder());
|
||||||
|
dto.setIcon(arc.getIcon());
|
||||||
dto.setThemes(arc.getThemes());
|
dto.setThemes(arc.getThemes());
|
||||||
dto.setStakes(arc.getStakes());
|
dto.setStakes(arc.getStakes());
|
||||||
dto.setGmNotes(arc.getGmNotes());
|
dto.setGmNotes(arc.getGmNotes());
|
||||||
@@ -46,6 +47,7 @@ public class ArcMapper {
|
|||||||
.description(dto.getDescription())
|
.description(dto.getDescription())
|
||||||
.campaignId(dto.getCampaignId())
|
.campaignId(dto.getCampaignId())
|
||||||
.order(dto.getOrder())
|
.order(dto.getOrder())
|
||||||
|
.icon(dto.getIcon())
|
||||||
.themes(dto.getThemes())
|
.themes(dto.getThemes())
|
||||||
.stakes(dto.getStakes())
|
.stakes(dto.getStakes())
|
||||||
.gmNotes(dto.getGmNotes())
|
.gmNotes(dto.getGmNotes())
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ public class CampaignMapper {
|
|||||||
dto.setDescription(campaign.getDescription());
|
dto.setDescription(campaign.getDescription());
|
||||||
dto.setArcsCount(campaign.getArcsCount());
|
dto.setArcsCount(campaign.getArcsCount());
|
||||||
dto.setLoreId(campaign.getLoreId());
|
dto.setLoreId(campaign.getLoreId());
|
||||||
|
dto.setGameSystemId(campaign.getGameSystemId());
|
||||||
return dto;
|
return dto;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -35,6 +36,7 @@ public class CampaignMapper {
|
|||||||
.description(dto.getDescription())
|
.description(dto.getDescription())
|
||||||
.arcsCount(dto.getArcsCount())
|
.arcsCount(dto.getArcsCount())
|
||||||
.loreId(dto.getLoreId())
|
.loreId(dto.getLoreId())
|
||||||
|
.gameSystemId(dto.getGameSystemId())
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user