Compare commits
70 Commits
v1.0.2-bet
...
bf91b75b5f
| Author | SHA1 | Date | |
|---|---|---|---|
| bf91b75b5f | |||
| 2fffad1b6b | |||
| 439f43875b | |||
| 79a68bc27b | |||
| c3d3394ec7 | |||
| 694f687fec | |||
| 87865338a0 | |||
| 586ddceff6 | |||
| 4b9b7f0995 | |||
| 3d73b1e6a7 | |||
| 759e47fc1f | |||
| f71bf3fcad | |||
| 0cd99dfb32 | |||
| f24ef0891e | |||
| 7c74c12f3e | |||
| 86836ad81c | |||
| 7c4a42327d | |||
| 52e389db24 | |||
| efaf5a3794 | |||
| 4fe93b5ff3 | |||
| 0f2d1b1efe | |||
| 5ff05242a8 | |||
| b06c77a1eb | |||
| 03bc669efe | |||
| c3873ddd84 | |||
| d7ceeac1b0 | |||
| cdbd3cd9b4 | |||
| a708c74425 | |||
| 9ad7651c44 | |||
| 389392fd1d | |||
| aaebeaa547 | |||
| 03ee3855f5 | |||
| 94a39cf3b4 | |||
| efe6f6c2b0 | |||
| 73a9d15786 | |||
| dfe05cf2d2 | |||
| fcba907438 | |||
| 5739602702 | |||
| addf78f01d | |||
| 5e04e84ee4 | |||
| 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 |
12
.env.example
12
.env.example
@@ -38,3 +38,15 @@ LLM_MODEL=gemma4:26b
|
||||
# 1min.ai (si LLM_PROVIDER=onemin)
|
||||
ONEMIN_API_KEY=
|
||||
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,8 +6,10 @@ on:
|
||||
- 'v*'
|
||||
|
||||
env:
|
||||
REGISTRY: git.igmlcreation.fr
|
||||
REGISTRY_USER: ietm64
|
||||
GITEA_REGISTRY: git.igmlcreation.fr
|
||||
GITEA_REGISTRY_USER: ietm64
|
||||
GHCR_REGISTRY: ghcr.io
|
||||
GHCR_NAMESPACE: igmlcreation
|
||||
|
||||
jobs:
|
||||
build:
|
||||
@@ -26,19 +28,114 @@ jobs:
|
||||
- name: Login to Gitea Registry
|
||||
uses: docker/login-action@v3
|
||||
with:
|
||||
registry: ${{ env.REGISTRY }}
|
||||
username: ${{ env.REGISTRY_USER }}
|
||||
registry: ${{ env.GITEA_REGISTRY }}
|
||||
username: ${{ env.GITEA_REGISTRY_USER }}
|
||||
password: ${{ secrets.DOCKER_PAT }}
|
||||
|
||||
- name: Extract version
|
||||
id: meta
|
||||
run: echo "version=${GITHUB_REF_NAME#v}" >> $GITHUB_OUTPUT
|
||||
# Login to GHCR (GitHub Container Registry) pour distribuer les images
|
||||
# publiquement aux utilisateurs finaux. Reputation domaine plus elevee
|
||||
# que git.igmlcreation.fr (mieux pour les antivirus / SmartScreen).
|
||||
- name: Login to GHCR
|
||||
uses: docker/login-action@v3
|
||||
with:
|
||||
registry: ${{ env.GHCR_REGISTRY }}
|
||||
username: ${{ env.GHCR_NAMESPACE }}
|
||||
password: ${{ secrets.GHCR_TOKEN }}
|
||||
|
||||
- name: Build & push ${{ matrix.component }}
|
||||
# Detection du canal :
|
||||
# - tag vX.Y.Z -> stable (push :latest + :version sur les repos publics)
|
||||
# - tag vX.Y.Z-beta* -> beta (push :beta + :version sur les repos GHCR prives
|
||||
# loremind-beta-<component> ; backup Gitea avec :version)
|
||||
- name: Extract version & channel
|
||||
id: meta
|
||||
run: |
|
||||
VERSION="${GITHUB_REF_NAME#v}"
|
||||
echo "version=${VERSION}" >> $GITHUB_OUTPUT
|
||||
if [[ "${VERSION}" == *-beta* ]]; then
|
||||
echo "channel=beta" >> $GITHUB_OUTPUT
|
||||
else
|
||||
echo "channel=stable" >> $GITHUB_OUTPUT
|
||||
fi
|
||||
|
||||
# Build & push canal STABLE
|
||||
- name: Build & push ${{ matrix.component }} (stable)
|
||||
if: steps.meta.outputs.channel == 'stable'
|
||||
uses: docker/build-push-action@v5
|
||||
with:
|
||||
context: ./${{ matrix.component }}
|
||||
push: true
|
||||
tags: |
|
||||
${{ env.REGISTRY }}/${{ env.REGISTRY_USER }}/${{ matrix.component }}:latest
|
||||
${{ env.REGISTRY }}/${{ env.REGISTRY_USER }}/${{ matrix.component }}:${{ steps.meta.outputs.version }}
|
||||
${{ env.GITEA_REGISTRY }}/${{ env.GITEA_REGISTRY_USER }}/${{ matrix.component }}:latest
|
||||
${{ env.GITEA_REGISTRY }}/${{ env.GITEA_REGISTRY_USER }}/${{ matrix.component }}:${{ steps.meta.outputs.version }}
|
||||
${{ env.GHCR_REGISTRY }}/${{ env.GHCR_NAMESPACE }}/loremind-${{ matrix.component }}:latest
|
||||
${{ env.GHCR_REGISTRY }}/${{ env.GHCR_NAMESPACE }}/loremind-${{ matrix.component }}:${{ steps.meta.outputs.version }}
|
||||
|
||||
# Build & push canal BETA
|
||||
# GHCR : repos prives loremind-beta-<component> (gated par PAT distribue
|
||||
# via le relais Patreon aux tiers Compagnon).
|
||||
# Gitea : backup prive avec :version uniquement (pas de :latest pour ne
|
||||
# pas faire upgrader les installs branchees sur Gitea).
|
||||
- name: Build & push ${{ matrix.component }} (beta)
|
||||
if: steps.meta.outputs.channel == 'beta'
|
||||
uses: docker/build-push-action@v5
|
||||
with:
|
||||
context: ./${{ matrix.component }}
|
||||
push: true
|
||||
tags: |
|
||||
${{ env.GITEA_REGISTRY }}/${{ env.GITEA_REGISTRY_USER }}/${{ matrix.component }}:${{ steps.meta.outputs.version }}
|
||||
${{ env.GHCR_REGISTRY }}/${{ env.GHCR_NAMESPACE }}/loremind-beta-${{ matrix.component }}:beta
|
||||
${{ env.GHCR_REGISTRY }}/${{ env.GHCR_NAMESPACE }}/loremind-beta-${{ matrix.component }}:${{ steps.meta.outputs.version }}
|
||||
|
||||
# Job separe pour le sidecar `switcher`.
|
||||
# Pourquoi separe : le switcher est volontairement HORS de IMAGE_NAMESPACE
|
||||
# (cf. docker-compose.yml). Il est toujours pulle depuis le repo public
|
||||
# `loremind-switcher`, quel que soit le canal de l'instance. On le build
|
||||
# donc uniquement sur les releases stables — pas la peine de re-publier
|
||||
# une variante beta du switcher, c'est une infrastructure neutre.
|
||||
build-switcher:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@v3
|
||||
|
||||
- name: Detect channel
|
||||
id: meta
|
||||
run: |
|
||||
VERSION="${GITHUB_REF_NAME#v}"
|
||||
echo "version=${VERSION}" >> $GITHUB_OUTPUT
|
||||
if [[ "${VERSION}" == *-beta* ]]; then
|
||||
echo "channel=beta" >> $GITHUB_OUTPUT
|
||||
else
|
||||
echo "channel=stable" >> $GITHUB_OUTPUT
|
||||
fi
|
||||
|
||||
- name: Login to Gitea Registry
|
||||
if: steps.meta.outputs.channel == 'stable'
|
||||
uses: docker/login-action@v3
|
||||
with:
|
||||
registry: ${{ env.GITEA_REGISTRY }}
|
||||
username: ${{ env.GITEA_REGISTRY_USER }}
|
||||
password: ${{ secrets.DOCKER_PAT }}
|
||||
|
||||
- name: Login to GHCR
|
||||
if: steps.meta.outputs.channel == 'stable'
|
||||
uses: docker/login-action@v3
|
||||
with:
|
||||
registry: ${{ env.GHCR_REGISTRY }}
|
||||
username: ${{ env.GHCR_NAMESPACE }}
|
||||
password: ${{ secrets.GHCR_TOKEN }}
|
||||
|
||||
- name: Build & push switcher (stable only)
|
||||
if: steps.meta.outputs.channel == 'stable'
|
||||
uses: docker/build-push-action@v5
|
||||
with:
|
||||
context: ./switcher
|
||||
push: true
|
||||
tags: |
|
||||
${{ env.GITEA_REGISTRY }}/${{ env.GITEA_REGISTRY_USER }}/switcher:latest
|
||||
${{ env.GITEA_REGISTRY }}/${{ env.GITEA_REGISTRY_USER }}/switcher:${{ steps.meta.outputs.version }}
|
||||
${{ env.GHCR_REGISTRY }}/${{ env.GHCR_NAMESPACE }}/loremind-switcher:latest
|
||||
${{ env.GHCR_REGISTRY }}/${{ env.GHCR_NAMESPACE }}/loremind-switcher:${{ steps.meta.outputs.version }}
|
||||
|
||||
18
.gitignore
vendored
18
.gitignore
vendored
@@ -7,6 +7,11 @@
|
||||
brain/data/settings.json
|
||||
*.key
|
||||
*.pem
|
||||
# Exception : la cle PUBLIQUE JWT du relais Patreon est destinee a etre
|
||||
# embarquee dans le binaire. Pas de risque a la committer (c'est une cle
|
||||
# publique par construction). Sans cette exception, le module licensing
|
||||
# est silencieusement desactive dans les builds CI.
|
||||
!core/src/main/resources/licensing/jwt-public-key.pem
|
||||
|
||||
# ============================================================================
|
||||
# Java / Spring Boot / Maven
|
||||
@@ -53,6 +58,12 @@ yarn-error.log*
|
||||
.pnpm-debug.log*
|
||||
coverage/
|
||||
|
||||
# Playwright (E2E)
|
||||
web/test-results/
|
||||
web/playwright-report/
|
||||
web/blob-report/
|
||||
web/playwright/.cache/
|
||||
|
||||
# ============================================================================
|
||||
# IDE / Editeurs
|
||||
# ============================================================================
|
||||
@@ -85,8 +96,15 @@ Thumbs.db
|
||||
# Documentation hors-code (conservee hors du repo)
|
||||
# ============================================================================
|
||||
docs/
|
||||
loremind-docs/
|
||||
|
||||
# ============================================================================
|
||||
# Docker Compose override (dev uniquement, non-distribue aux end users)
|
||||
# ============================================================================
|
||||
docker-compose.override.yml
|
||||
|
||||
# ============================================================================
|
||||
# Relais OAuth Patreon (repo Gitea separe, clone localement pour facilite)
|
||||
# ============================================================================
|
||||
relay/
|
||||
scripts/bump-version.mjs
|
||||
|
||||
311
INSTALL.md
311
INSTALL.md
@@ -1,311 +0,0 @@
|
||||
# Installation de LoreMindMJ
|
||||
|
||||
Ce document decrit la procedure d'installation de LoreMindMJ. Temps estime :
|
||||
5 a 10 minutes selon la qualite de la connexion reseau.
|
||||
|
||||
## 1. Prerequis
|
||||
|
||||
- **Docker Desktop** ([Windows](https://www.docker.com/products/docker-desktop/) /
|
||||
[Mac](https://www.docker.com/products/docker-desktop/)) ou
|
||||
**Docker Engine + Compose v2** (Linux). Verification :
|
||||
```
|
||||
docker --version
|
||||
docker compose version
|
||||
```
|
||||
Compose v2 est requis : la commande est `docker compose`, non `docker-compose`.
|
||||
|
||||
- **Un fournisseur LLM**, au choix :
|
||||
- **[Ollama](https://ollama.com/)** installe sur la machine hote (gratuit,
|
||||
local, necessite environ 6 Go de RAM libre pour les modeles recommandes).
|
||||
- **Une cle API [1min.ai](https://1min.ai)** (hebergement cloud, facturation
|
||||
a l'usage, aucune installation supplementaire requise).
|
||||
|
||||
- Environ **2 Go d'espace disque** pour les images Docker, auxquels s'ajoute
|
||||
la taille des modeles Ollama si l'option locale est retenue.
|
||||
|
||||
## 2. Recuperation des fichiers
|
||||
|
||||
Telecharger les deux fichiers suivants depuis la
|
||||
[derniere release](https://git.igmlcreation.fr/ietm64/LoreMindMJ/releases) et
|
||||
les placer dans un dossier dedie (par exemple `~/loremind/` ou
|
||||
`C:\Programs\loremind\`) :
|
||||
|
||||
- `docker-compose.yml`
|
||||
- `.env.example`
|
||||
|
||||
Le code source n'est pas necessaire : les images sont pre-construites et
|
||||
publiees sur le registry Gitea `git.igmlcreation.fr` (non Docker Hub). Le
|
||||
premier `docker compose pull` les telechargera automatiquement.
|
||||
|
||||
## 3. Configuration du fichier `.env`
|
||||
|
||||
Renommer `.env.example` en `.env` et l'ouvrir dans un editeur de texte. **Trois
|
||||
variables sont obligatoires** ; sans elles, `docker compose up` refusera de
|
||||
demarrer. Ce comportement est volontaire afin d'eviter tout deploiement
|
||||
non-securise par defaut.
|
||||
|
||||
### `POSTGRES_PASSWORD`
|
||||
|
||||
Mot de passe de la base de donnees PostgreSQL. Choisir une valeur robuste.
|
||||
Seuls les conteneurs utilisent cette valeur : il n'est pas necessaire de la
|
||||
memoriser au-dela du fichier `.env`.
|
||||
|
||||
### `ADMIN_PASSWORD`
|
||||
|
||||
Protege l'ecran **Parametres** de l'application via HTTP Basic. Cette valeur
|
||||
sera demandee par le navigateur lors de toute modification de la configuration
|
||||
(changement de modele LLM, saisie de cle API, etc.). Le nom d'utilisateur par
|
||||
defaut est `admin`, modifiable via la variable `ADMIN_USERNAME`.
|
||||
|
||||
### `BRAIN_INTERNAL_SECRET`
|
||||
|
||||
Secret partage entre le service Java (`core`) et le service Python (`brain`).
|
||||
Empeche toute requete externe d'atteindre directement le service Brain.
|
||||
Generer une valeur aleatoire de 64 caracteres hexadecimaux :
|
||||
|
||||
```
|
||||
openssl rand -hex 32
|
||||
```
|
||||
|
||||
Sous Windows sans `openssl`, utiliser PowerShell :
|
||||
|
||||
```powershell
|
||||
-join ((48..57) + (97..102) | Get-Random -Count 64 | % {[char]$_})
|
||||
```
|
||||
|
||||
### Variables optionnelles
|
||||
|
||||
- `WEB_PORT` (defaut `8081`) : port d'ecoute de l'interface web.
|
||||
- `ADMIN_USERNAME` (defaut `admin`) : identifiant de la popup Parametres.
|
||||
- `LLM_PROVIDER` (defaut `ollama`) : choix du fournisseur LLM (voir
|
||||
section 5).
|
||||
|
||||
Les autres variables (`MINIO_USER`/`MINIO_PASSWORD`, `POSTGRES_DB`,
|
||||
`POSTGRES_USER`) disposent de valeurs par defaut adaptees a un deploiement
|
||||
personnel et peuvent etre conservees en l'etat.
|
||||
|
||||
## 4. Lancement de la stack
|
||||
|
||||
Depuis le dossier contenant `docker-compose.yml` et `.env` :
|
||||
|
||||
```
|
||||
docker compose up -d
|
||||
```
|
||||
|
||||
Le premier demarrage telecharge les images (environ 1 a 2 Go au total) et
|
||||
initialise la base. Compter 2 a 5 minutes selon la qualite de la connexion.
|
||||
La progression peut etre suivie via :
|
||||
|
||||
```
|
||||
docker compose logs -f
|
||||
```
|
||||
|
||||
(`Ctrl+C` pour quitter l'affichage ; les services continuent de fonctionner
|
||||
en arriere-plan.)
|
||||
|
||||
Une fois les services en etat `healthy`, ouvrir **http://localhost:8081**
|
||||
dans un navigateur.
|
||||
|
||||
### Verification du fonctionnement
|
||||
|
||||
```
|
||||
docker compose ps
|
||||
```
|
||||
|
||||
Cinq conteneurs doivent apparaitre en etat `Up` ou `healthy` :
|
||||
`loremind-postgres`, `loremind-minio`, `loremind-core`, `loremind-brain`,
|
||||
`loremind-web`. Le conteneur `loremind-minio-init` s'arrete automatiquement
|
||||
apres creation du bucket d'images : ce comportement est normal.
|
||||
|
||||
## 5. Configuration du fournisseur LLM
|
||||
|
||||
### Ollama (local, gratuit)
|
||||
|
||||
Installer Ollama sur la machine hote (pas dans Docker), puis telecharger un
|
||||
modele :
|
||||
|
||||
```
|
||||
ollama pull gemma4:26b
|
||||
```
|
||||
|
||||
Dans `.env` :
|
||||
|
||||
```
|
||||
LLM_PROVIDER=ollama
|
||||
LLM_MODEL=gemma4:26b
|
||||
OLLAMA_BASE_URL=http://host.docker.internal:11434
|
||||
```
|
||||
|
||||
L'adresse `host.docker.internal` permet au conteneur `brain` d'atteindre
|
||||
Ollama sur la machine hote. Cette resolution est native sous Docker Desktop
|
||||
(Mac / Windows). Sous Linux, le fichier `docker-compose.yml` declare un
|
||||
`extra_hosts` equivalent.
|
||||
|
||||
### 1min.ai (cloud, paye)
|
||||
|
||||
Dans `.env` :
|
||||
|
||||
```
|
||||
LLM_PROVIDER=onemin
|
||||
ONEMIN_API_KEY=sk-...
|
||||
ONEMIN_MODEL=gpt-4o-mini
|
||||
```
|
||||
|
||||
### Modification a chaud
|
||||
|
||||
Le fournisseur, le modele et la cle API peuvent etre modifies a chaud depuis
|
||||
l'ecran **Parametres** de l'application. Les modifications sont persistees
|
||||
dans un volume Docker et survivent aux redemarrages. Les variables d'env du
|
||||
fichier `.env` sont uniquement utilisees comme valeurs initiales au premier
|
||||
demarrage.
|
||||
|
||||
## 6. Mise a jour
|
||||
|
||||
```
|
||||
docker compose pull
|
||||
docker compose up -d
|
||||
```
|
||||
|
||||
Les donnees (base PostgreSQL, images MinIO, configuration Brain) sont
|
||||
stockees dans des volumes Docker et survivent aux mises a jour.
|
||||
|
||||
## 7. Sauvegarde
|
||||
|
||||
Les donnees sont reparties dans trois volumes Docker :
|
||||
|
||||
- `loremindmj_postgres-data` — ensemble des donnees applicatives (lores,
|
||||
campagnes, pages, templates, branches narratives, etc.).
|
||||
- `loremindmj_minio-data` — images uploadees.
|
||||
- `loremindmj_brain-data` — parametres IA (fournisseur courant, cle API
|
||||
1min.ai).
|
||||
|
||||
### Export SQL de la base
|
||||
|
||||
```
|
||||
docker compose exec postgres pg_dump -U loremind loremind > backup.sql
|
||||
```
|
||||
|
||||
### Sauvegarde complete des volumes
|
||||
|
||||
Arreter la stack au prealable afin de garantir la coherence des donnees :
|
||||
|
||||
```
|
||||
docker compose stop
|
||||
docker run --rm -v loremindmj_postgres-data:/data -v $(pwd):/backup alpine tar czf /backup/postgres-data.tar.gz -C /data .
|
||||
docker run --rm -v loremindmj_minio-data:/data -v $(pwd):/backup alpine tar czf /backup/minio-data.tar.gz -C /data .
|
||||
docker compose start
|
||||
```
|
||||
|
||||
Sous Windows PowerShell, remplacer `$(pwd)` par `${PWD}`.
|
||||
|
||||
## 8. Resolution des problemes
|
||||
|
||||
### Port 8081 deja utilise
|
||||
|
||||
Modifier `WEB_PORT=8082` (ou toute autre valeur libre) dans `.env`, puis
|
||||
relancer :
|
||||
|
||||
```
|
||||
docker compose up -d
|
||||
```
|
||||
|
||||
### Erreur "set POSTGRES_PASSWORD in .env" (ou variable equivalente) au lancement
|
||||
|
||||
Une des trois variables obligatoires de l'etape 3 est manquante. Verifier le
|
||||
contenu du fichier `.env`.
|
||||
|
||||
### Popup "Ce site vous demande de vous connecter" sur l'ecran Parametres
|
||||
|
||||
Comportement attendu : il s'agit de l'authentification HTTP Basic. Utiliser
|
||||
la valeur de `ADMIN_USERNAME` (par defaut `admin`) et celle de
|
||||
`ADMIN_PASSWORD`.
|
||||
|
||||
### Erreurs `password authentication failed` en boucle dans les logs Postgres
|
||||
|
||||
Si la variable `POSTGRES_PASSWORD` a ete modifiee apres un premier lancement,
|
||||
le volume Postgres conserve l'ancien mot de passe (initialise une seule fois).
|
||||
Deux options :
|
||||
|
||||
- **Redemarrer avec un volume vierge** (entraine la perte des donnees) :
|
||||
```
|
||||
docker compose down -v
|
||||
docker compose up -d
|
||||
```
|
||||
- **Modifier le mot de passe en base** sans toucher au volume :
|
||||
```
|
||||
docker compose exec postgres psql -U postgres
|
||||
```
|
||||
Puis dans le prompt `psql` :
|
||||
```sql
|
||||
ALTER USER loremind WITH PASSWORD 'valeur_exacte_du_env';
|
||||
\q
|
||||
```
|
||||
Redemarrer ensuite le Core : `docker compose restart core`.
|
||||
|
||||
### Erreur "502 Bad Gateway" ou message d'erreur IA dans l'interface
|
||||
|
||||
Le service Brain ne parvient pas a contacter le fournisseur LLM. Verifier :
|
||||
|
||||
- **Ollama** : `ollama serve` est-il actif ? Le modele est-il telecharge
|
||||
(`ollama list`) ? La valeur de `LLM_MODEL` correspond-elle exactement au
|
||||
nom d'un modele liste ?
|
||||
- **1min.ai** : la cle API est-elle valide ? Le modele existe-t-il ?
|
||||
- Consulter les logs du Brain :
|
||||
```
|
||||
docker compose logs brain
|
||||
```
|
||||
|
||||
### Un service ne demarre pas ou reste en etat `unhealthy`
|
||||
|
||||
Consulter les logs du service concerne :
|
||||
|
||||
```
|
||||
docker compose logs <service>
|
||||
```
|
||||
|
||||
Services disponibles : `postgres`, `minio`, `core`, `brain`, `web`.
|
||||
|
||||
### Redemarrage d'un service apres modification du `.env`
|
||||
|
||||
```
|
||||
docker compose up -d <service>
|
||||
```
|
||||
|
||||
Redemarrage complet : `docker compose restart`.
|
||||
|
||||
### Remise a zero complete (PERTE DES DONNEES)
|
||||
|
||||
```
|
||||
docker compose down -v
|
||||
```
|
||||
|
||||
L'option `-v` supprime les volumes. L'ensemble des lores, campagnes, images
|
||||
et parametres est perdu de maniere definitive.
|
||||
|
||||
### "No such image" ou "pull access denied" au premier lancement
|
||||
|
||||
Le registry Gitea peut necessiter une authentification selon la visibilite
|
||||
configuree pour les images. Contacter l'editeur du projet.
|
||||
|
||||
## 9. Exposition reseau des services
|
||||
|
||||
- **Interface web** : http://localhost:8081 (port configurable via
|
||||
`WEB_PORT`).
|
||||
- **PostgreSQL** : accessible uniquement via le reseau Docker interne, non
|
||||
expose vers l'hote.
|
||||
- **MinIO** : accessible uniquement via le reseau Docker interne. Les images
|
||||
transitent par le reverse-proxy Java sur `/api/images/{id}/content`. Le
|
||||
binding `127.0.0.1:9000/9001` defini dans `docker-compose.override.yml`
|
||||
n'est actif qu'en developpement.
|
||||
- **Brain Python** : accessible uniquement via le reseau Docker interne.
|
||||
Toute requete doit porter l'en-tete `X-Internal-Secret`, injectee
|
||||
automatiquement par le Core Java et jamais exposee au navigateur.
|
||||
|
||||
## 10. Desinstallation
|
||||
|
||||
```
|
||||
docker compose down -v
|
||||
docker image rm git.igmlcreation.fr/ietm64/core git.igmlcreation.fr/ietm64/brain git.igmlcreation.fr/ietm64/web
|
||||
```
|
||||
|
||||
Supprimer ensuite le dossier contenant `docker-compose.yml` et `.env`.
|
||||
87
README.md
87
README.md
@@ -1,75 +1,66 @@
|
||||
# LoreMind
|
||||
|
||||
Application web d'aide aux Maîtres de Jeu (JDR) pour centraliser la gestion de l'univers (Lore) et le suivi des campagnes, avec un moteur IA intégré pour générer du contenu structuré.
|
||||
> Application web auto-hébergeable pour MJ qui veulent centraliser leur univers, leurs campagnes et leurs personnages — avec un assistant IA contextuel.
|
||||
|
||||
## Fonctionnalités
|
||||
[](LICENSE)
|
||||
[](https://loremind-docs.igmlcreation.fr/)
|
||||
[](https://loremind-demo.igmlcreation.fr/)
|
||||
[](https://www.patreon.com/c/IGMLCreation)
|
||||
[](https://discord.gg/cPpFzCjEzQ)
|
||||
|
||||
- Gestion centralisée du Lore : Lieux, Factions, PNJ, et tous les éléments de votre univers
|
||||
- Suivi de campagnes : Sessions, actions des joueurs, chronologie
|
||||
- Moteur IA intégré : Génération automatique de contenu (PNJ, Villes, Quêtes) à partir de templates
|
||||
- Export vers FoundryVTT : Transfert structuré des données vers votre VTT préféré (en développement)
|
||||
## Découvrir LoreMind en vidéo
|
||||
|
||||
## Captures d'écran
|
||||
[](https://www.youtube.com/watch?v=llJkmlotbB8)
|
||||
|
||||
### Page d'accueil
|
||||

|
||||

|
||||
|
||||
### Recherche
|
||||

|
||||
## Ce que ça fait
|
||||
|
||||
## Stack Technologique
|
||||
LoreMind regroupe ce qu'un MJ utilise habituellement éparpillé entre plusieurs outils. L'application s'articule autour de trois modules principaux, augmentés par un assistant IA qui exploite tout votre contenu.
|
||||
|
||||
LoreMind utilise une architecture distribuée pour séparer les responsabilités :
|
||||
### Lore
|
||||
|
||||
- **Frontend** : Angular (Interface utilisateur, affichage du lore, formulaires de templates)
|
||||
- **Backend Core** : Java (Spring Boot) - Orchestration, persistance, export VTT
|
||||
- **Backend IA** : Python - Traitement des LLM et génération de contenu
|
||||
- **Base de données** : PostgreSQL avec JSONB pour les templates flexibles
|
||||
Construire votre univers avec une arborescence de pages templatées : lieux, factions, PNJ, événements, organisations... Chaque type de page suit un template configurable, ce qui garantit la cohérence et facilite la navigation dans des univers riches.
|
||||
|
||||
## Architecture
|
||||
### Game System
|
||||
|
||||
### Backend Java (Domain-Driven Design & Hexagonal)
|
||||
Stocker les règles de votre système de jeu (D&D, Nimble, créations maison...) et définir les modèles de fiches de personnages associés. Les règles indexées peuvent être injectées dans le contexte de l'IA pour des réponses fidèles à votre système.
|
||||
|
||||
Le Backend Core respecte strictement :
|
||||
- **Domain-Driven Design (DDD)** : Séparation en Bounded Contexts autonomes
|
||||
- **Architecture Hexagonale (Ports et Adaptateurs)** : Domaine pur sans dépendances techniques
|
||||
### Campaign
|
||||
|
||||
#### Bounded Contexts
|
||||
- **LoreContext** : Gestion de l'encyclopédie de l'univers
|
||||
- **CampaignContext** : Suivi des sessions et chronologie
|
||||
- **GenerationContext** : Gestion des requêtes IA et templates
|
||||
Structurer vos campagnes en Arcs → Chapitres → Scènes avec séparation claire du contenu MJ et du contenu joueurs. Gérer les PJ et PNJ via des fiches dynamiques basées sur les templates du game system retenu.
|
||||
|
||||
#### Couches
|
||||
- **Domaine (Core)** : Entités métier pures et interfaces (Ports)
|
||||
- **Application** : Orchestration des flux (Use Cases)
|
||||
- **Infrastructure** : Implémentation technique (Adapters)
|
||||
### Assistant IA
|
||||
|
||||
## Installation
|
||||
Un assistant contextuel qui pioche dans votre Lore, vos règles et vos campagnes pour répondre à vos questions, suggérer du contenu cohérent, ou rebondir sur une situation improvisée en table.
|
||||
|
||||
Pour installer LoreMind chez vous (Docker requis), suivez le guide **[INSTALL.md](INSTALL.md)** — 3 étapes, 5 minutes chrono :
|
||||
L'IA s'exécute **en local via [Ollama](https://ollama.com/)** ou via **[1min.ai](https://1min.ai/)**. D'autres moteurs seront supportés à l'avenir.
|
||||
|
||||
1. Télécharger `docker-compose.yml` + `.env.example` depuis la [dernière release](https://git.igmlcreation.fr/ietm64/LoreMindMJ/releases)
|
||||
2. Renommer `.env.example` → `.env` et changer `POSTGRES_PASSWORD`
|
||||
3. `docker compose up -d` → ouvrir http://localhost:8081
|
||||
## Documentation
|
||||
|
||||
Mise à jour : `docker compose pull && docker compose up -d`.
|
||||
Toute la documentation (installation, configuration, prise en main) est sur **[loremind-docs.igmlcreation.fr](https://loremind-docs.igmlcreation.fr/)**.
|
||||
|
||||
## Développement (contributeurs)
|
||||
## Démo en ligne
|
||||
|
||||
Pour builder les images localement depuis les sources :
|
||||
Une instance de démonstration est disponible sur **[loremind-demo.igmlcreation.fr](https://loremind-demo.igmlcreation.fr/)**.
|
||||
|
||||
```bash
|
||||
git clone https://git.igmlcreation.fr/ietm64/LoreMindMJ.git
|
||||
cd LoreMindMJ
|
||||
# Créer un docker-compose.override.yml local (voir docs de contrib)
|
||||
docker compose up -d --build
|
||||
```
|
||||
Quelques limites à connaître :
|
||||
- 10 utilisateurs maximum simultanés (instances isolées)
|
||||
- Session limitée à 20 minutes avant réinitialisation
|
||||
- Partie IA non incluse dans la démo (nécessite Ollama ou 1min.ai côté serveur)
|
||||
|
||||
## License
|
||||
## Soutenir le projet
|
||||
|
||||
LoreMind est **et restera gratuit en auto-hébergement**. Le développement avance plus vite avec votre soutien :
|
||||
|
||||
- **[Patreon](https://www.patreon.com/c/IGMLCreation)** — accès anticipé aux features, vote sur la roadmap, devlogs exclusifs
|
||||
- **[Discord](https://discord.gg/cPpFzCjEzQ)** — annonces, support, retours utilisateurs
|
||||
|
||||
## Licence
|
||||
|
||||
LoreMind est distribué sous licence **[GNU AGPL v3](LICENSE)**.
|
||||
|
||||
En pratique :
|
||||
- Tu peux l'utiliser gratuitement, l'héberger où tu veux, le modifier, le redistribuer.
|
||||
- Si tu modifies le code et que tu exposes l'application modifiée sur un réseau (même en SaaS privé), tu dois rendre tes modifications publiques sous la même licence.
|
||||
- Les univers (Lore) et campagnes que tu crées avec LoreMind **t'appartiennent entièrement** — la licence ne couvre que le code de l'application.
|
||||
- Vous pouvez l'utiliser gratuitement, l'héberger, la modifier, la redistribuer.
|
||||
- Si vous modifiez le code et que vous exposez l'application modifiée sur un réseau (même en SaaS privé), vous devez rendre vos modifications publiques sous la même licence.
|
||||
- Les univers (Lore) et campagnes que vous créez avec LoreMind **vous appartiennent entièrement** — la licence ne couvre que le code de l'application.
|
||||
|
||||
@@ -1,7 +1,15 @@
|
||||
FROM python:3.12-slim
|
||||
WORKDIR /app
|
||||
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends curl \
|
||||
# curl : healthcheck docker.
|
||||
# tesseract-ocr (+ langues fra/eng) : repli OCR de l'import de PDF de regles
|
||||
# pour les pages sans couche texte (scans). Inutile pour les PDF born-digital
|
||||
# mais necessaire pour couvrir tous les cas.
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||
curl \
|
||||
tesseract-ocr \
|
||||
tesseract-ocr-fra \
|
||||
tesseract-ocr-eng \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
COPY requirements.txt .
|
||||
|
||||
125
brain/app/application/adapt_campaign.py
Normal file
125
brain/app/application/adapt_campaign.py
Normal file
@@ -0,0 +1,125 @@
|
||||
"""Use case : conseils d'adaptation d'un PDF à une campagne EXISTANTE.
|
||||
|
||||
L'IA connaît la campagne de l'utilisateur (un « brief » : structure arcs/chapitres/
|
||||
scènes + PNJ + univers/lore), lit le contenu du PDF, et rédige des recommandations
|
||||
d'INTÉGRATION/ADAPTATION (où insérer, reskins de PNJ, transposition à l'univers,
|
||||
doublons à réconcilier…). Sortie en markdown, streamée token par token.
|
||||
|
||||
Contrairement à l'IMPORT (qui produit une arborescence à créer), ici on produit
|
||||
du CONSEIL libre : rien n'est créé, l'utilisateur applique à la main.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import AsyncIterator
|
||||
|
||||
from app.domain.models import ChatMessage
|
||||
from app.domain.ports import LLMChatProvider, PdfExtractionError, PdfTextExtractor
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Plus créatif que l'import (tâche de structuration) : ici on conseille/adapte.
|
||||
_TEMPERATURE = 0.7
|
||||
|
||||
_SYSTEM_PREFIX = (
|
||||
"Tu es un assistant pour Maître de Jeu de jeu de rôle. L'utilisateur a une "
|
||||
"campagne EXISTANTE (décrite plus bas) et souhaite ADAPTER et INTÉGRER le "
|
||||
"contenu d'un PDF (aventure, donjon, supplément) à CETTE campagne précise."
|
||||
)
|
||||
|
||||
_SYSTEM_SUFFIX = (
|
||||
"Produis des CONSEILS D'ADAPTATION concrets, actionnables et en FRANÇAIS, "
|
||||
"en markdown structuré (titres ##, listes). Couvre notamment :\n"
|
||||
"- **Où l'insérer** : à quel(s) arc(s)/chapitre(s) EXISTANT(s) rattacher ce "
|
||||
"contenu, dans quel ordre, et — si l'arc est un hub — sous quelles conditions de déblocage.\n"
|
||||
"- **Reskins / liens PNJ** : quels PNJ EXISTANTS de la campagne peuvent incarner "
|
||||
"ou remplacer les personnages clés du PDF.\n"
|
||||
"- **Adaptation à l'univers** : comment transposer lieux, factions, noms propres et "
|
||||
"ton vers l'univers de l'utilisateur plutôt que le cadre d'origine du PDF.\n"
|
||||
"- **Doublons / conflits** : ce qui recoupe l'existant et comment le réconcilier.\n"
|
||||
"- **Ajustements de ton et de difficulté**.\n\n"
|
||||
"Réfère-toi TOUJOURS aux éléments existants par leur NOM. Ne réécris PAS le PDF en "
|
||||
"entier : donne des recommandations. Si une information manque, propose des options."
|
||||
)
|
||||
|
||||
|
||||
class AdaptCampaignUseCase:
|
||||
"""Génère (en streaming) des conseils d'adaptation d'un PDF à une campagne."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
llm: LLMChatProvider,
|
||||
extractor: PdfTextExtractor,
|
||||
max_input_tokens: int = 10000,
|
||||
) -> None:
|
||||
self._llm = llm
|
||||
self._extractor = extractor
|
||||
# L'adaptation envoie le PDF en UNE requête (pas de découpage). On plafonne
|
||||
# donc l'entrée pour ne pas dépasser la taille de requête acceptée par le
|
||||
# provider (sinon HTTP 400). Calé sur la taille des morceaux d'import.
|
||||
self._max_input_tokens = max_input_tokens
|
||||
|
||||
async def stream(
|
||||
self,
|
||||
pdf_bytes: bytes,
|
||||
brief: str,
|
||||
messages: list[ChatMessage],
|
||||
) -> AsyncIterator[str]:
|
||||
"""Conversationnel : le PDF + la campagne sont le CONTEXTE (system prompt),
|
||||
`messages` est l'échange (demande initiale, puis feedbacks de l'utilisateur)."""
|
||||
doc = self._extractor.extract(pdf_bytes)
|
||||
pdf_text = doc.full_text
|
||||
if not pdf_text.strip():
|
||||
raise PdfExtractionError("Aucun texte exploitable n'a été extrait du PDF.")
|
||||
|
||||
brief = brief or ""
|
||||
pdf_text, truncated = self._fit_pdf_to_budget(pdf_text, brief)
|
||||
|
||||
logger.info(
|
||||
"Adaptation campagne : %s page(s) (%s via OCR), brief %s car., PDF %s car.%s, %s message(s).",
|
||||
doc.page_count, doc.ocr_page_count, len(brief), len(pdf_text),
|
||||
" (tronqué)" if truncated else "", len(messages),
|
||||
)
|
||||
|
||||
trunc_note = (
|
||||
"\n[Note : PDF tronqué pour tenir dans une requête — base-toi sur ce début.]"
|
||||
if truncated else ""
|
||||
)
|
||||
# Concaténation (pas .format) : brief/PDF peuvent contenir des { } littéraux.
|
||||
system_prompt = (
|
||||
f"{_SYSTEM_PREFIX}\n\n"
|
||||
"--- CAMPAGNE EXISTANTE DE L'UTILISATEUR ---\n"
|
||||
f"{brief.strip() or '(campagne encore vide)'}\n\n"
|
||||
"--- CONTENU DU PDF À ADAPTER ---\n"
|
||||
f"{pdf_text}{trunc_note}\n\n"
|
||||
f"{_SYSTEM_SUFFIX}\n\n"
|
||||
"Tu es en CONVERSATION : à chaque message de l'utilisateur, ajuste, corrige "
|
||||
"ou propose des alternatives en gardant tout ce contexte à l'esprit."
|
||||
)
|
||||
|
||||
# 1er tour : si aucun message, on lance la demande initiale par défaut.
|
||||
convo = messages or [ChatMessage(
|
||||
role="user",
|
||||
content="Propose-moi comment intégrer et adapter ce PDF à ma campagne.",
|
||||
)]
|
||||
|
||||
async for token in self._llm.stream_chat(
|
||||
convo, system_prompt=system_prompt, temperature=_TEMPERATURE
|
||||
):
|
||||
yield token
|
||||
|
||||
def _fit_pdf_to_budget(self, pdf_text: str, brief: str) -> tuple[str, bool]:
|
||||
"""Tronque le texte du PDF pour que (brief + PDF) tienne dans le budget tokens.
|
||||
|
||||
Évite un HTTP 400 « requête trop grosse » côté provider. Réserve une marge
|
||||
pour le prompt système et le brief.
|
||||
"""
|
||||
import tiktoken
|
||||
|
||||
enc = tiktoken.get_encoding("cl100k_base")
|
||||
brief_tokens = len(enc.encode(brief))
|
||||
budget = max(2000, self._max_input_tokens - brief_tokens - 1000) # 1000 = marge système
|
||||
pdf_tokens = enc.encode(pdf_text)
|
||||
if len(pdf_tokens) <= budget:
|
||||
return pdf_text, False
|
||||
return enc.decode(pdf_tokens[:budget]), True
|
||||
@@ -20,10 +20,16 @@ from app.domain.models import (
|
||||
CampaignStructuralContext,
|
||||
ChatMessage,
|
||||
ChapterSummary,
|
||||
CharacterSummary,
|
||||
NpcSummary,
|
||||
GameSystemContext,
|
||||
JournalEntrySummary,
|
||||
LoreStructuralContext,
|
||||
NarrativeEntityContext,
|
||||
PageContext,
|
||||
PageSummary,
|
||||
QuestSummary,
|
||||
SessionContext,
|
||||
)
|
||||
from app.domain.ports import LLMChatProvider
|
||||
|
||||
@@ -63,16 +69,18 @@ class ChatUseCase:
|
||||
page_context: PageContext | None = None,
|
||||
campaign_context: CampaignStructuralContext | None = None,
|
||||
narrative_entity: NarrativeEntityContext | None = None,
|
||||
game_system_context: GameSystemContext | None = None,
|
||||
session_context: SessionContext | None = None,
|
||||
) -> AsyncIterator[str]:
|
||||
"""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
|
||||
pour que le prompt ait du sens. Le controller (main.py) applique
|
||||
cette règle à la frontière HTTP.
|
||||
"""
|
||||
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, session_context
|
||||
)
|
||||
async for token in self._llm.stream_chat(
|
||||
messages,
|
||||
@@ -87,12 +95,14 @@ class ChatUseCase:
|
||||
page_context: PageContext | None = None,
|
||||
campaign_context: CampaignStructuralContext | None = None,
|
||||
narrative_entity: NarrativeEntityContext | None = None,
|
||||
game_system_context: GameSystemContext | None = None,
|
||||
session_context: SessionContext | 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
|
||||
lore_context, page_context, campaign_context, narrative_entity, game_system_context, session_context
|
||||
)
|
||||
|
||||
# --- Construction du system prompt --------------------------------------
|
||||
@@ -103,16 +113,22 @@ class ChatUseCase:
|
||||
page: PageContext | None,
|
||||
campaign: CampaignStructuralContext | None,
|
||||
narrative: NarrativeEntityContext | None,
|
||||
game_system: GameSystemContext | None = None,
|
||||
session: SessionContext | None = None,
|
||||
) -> str:
|
||||
sections = [_BASE_SYSTEM]
|
||||
if lore is not None:
|
||||
sections.append(self._format_lore(lore))
|
||||
if campaign 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:
|
||||
sections.append(self._format_page(page))
|
||||
if narrative is not None:
|
||||
sections.append(self._format_narrative_entity(narrative))
|
||||
if session is not None:
|
||||
sections.append(self._format_session(session))
|
||||
return "\n\n".join(sections)
|
||||
|
||||
# --- Blocs Lore ---------------------------------------------------------
|
||||
@@ -190,14 +206,69 @@ class ChatUseCase:
|
||||
if lore_present
|
||||
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)
|
||||
npcs_block = ChatUseCase._format_npcs(ctx.npcs)
|
||||
return (
|
||||
"--- 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}"
|
||||
f"{npcs_block}\n"
|
||||
"Structure narrative (les flèches → indiquent des transitions de scène "
|
||||
"déclenchées par un choix des joueurs) :\n"
|
||||
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
|
||||
def _format_npcs(npcs: list[NpcSummary]) -> str:
|
||||
"""Bloc PNJ — symétrique aux PJ avec sa propre instruction anti-halluci.
|
||||
|
||||
Distinction importante : pour les PNJ, l'IA est ENCOURAGÉE à proposer de
|
||||
nouveaux PNJ (création créative = OK). En revanche, elle ne doit pas
|
||||
référencer comme existant un PNJ qui n'est pas dans la liste ci-dessous.
|
||||
"""
|
||||
if not npcs:
|
||||
return (
|
||||
"\nPersonnages non-joueurs (PNJ) : aucun défini pour l'instant. "
|
||||
"Tu peux librement proposer de nouveaux PNJ au MJ, mais ne "
|
||||
"fais pas comme s'ils existaient déjà dans la campagne.\n"
|
||||
)
|
||||
lines = ["\nPersonnages non-joueurs (PNJ) connus :"]
|
||||
for n in npcs:
|
||||
if n.snippet:
|
||||
lines.append(f"- **{n.name}** — {n.snippet}")
|
||||
else:
|
||||
lines.append(f"- **{n.name}** (fiche vide)")
|
||||
lines.append(
|
||||
"Pour une fiche complète d'un PNJ existant (apparence, motivations), "
|
||||
"n'invente rien : demande au MJ d'ouvrir l'éditeur du PNJ. Tu peux "
|
||||
"en revanche proposer librement de NOUVEAUX PNJ."
|
||||
)
|
||||
return "\n".join(lines) + "\n"
|
||||
|
||||
@staticmethod
|
||||
def _format_arcs(arcs: list[ArcSummary]) -> str:
|
||||
if not arcs:
|
||||
@@ -225,7 +296,8 @@ class ChatUseCase:
|
||||
else:
|
||||
for scene in chapter.scenes:
|
||||
sc_hint = ChatUseCase._illustration_hint(scene.illustration_count)
|
||||
block.append(f" - {scene.name} (scène){sc_hint}")
|
||||
scene_kind = " (lieu explorable)" if scene.rooms else " (scène)"
|
||||
block.append(f" - {scene.name}{scene_kind}{sc_hint}")
|
||||
if scene.description:
|
||||
block.append(f" Description : {scene.description}")
|
||||
for br in scene.branches:
|
||||
@@ -233,6 +305,19 @@ class ChatUseCase:
|
||||
block.append(
|
||||
f' → "{br.label}" vers {br.target_scene_name}{cond}'
|
||||
)
|
||||
# Pièces du lieu explorable (mode donjon)
|
||||
for room in scene.rooms:
|
||||
floor = f" [étage {room.floor}]" if room.floor is not None else ""
|
||||
block.append(f" ◆ {room.name}{floor}")
|
||||
if room.description:
|
||||
block.append(f" {room.description}")
|
||||
if room.enemies:
|
||||
block.append(f" Ennemis : {room.enemies}")
|
||||
for rb in room.branches:
|
||||
cond = f" (si : {rb.condition})" if rb.condition else ""
|
||||
block.append(
|
||||
f' ↳ "{rb.label}" vers {rb.target_room_name}{cond}'
|
||||
)
|
||||
return block
|
||||
|
||||
@staticmethod
|
||||
@@ -248,12 +333,182 @@ class ChatUseCase:
|
||||
noun = "illustration" if count == 1 else "illustrations"
|
||||
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}"
|
||||
)
|
||||
|
||||
# --- Bloc Session de jeu (Play Context) ---------------------------------
|
||||
|
||||
_ENTRY_TYPE_LABELS = {
|
||||
"NOTE": "Note du MJ",
|
||||
"EVENT": "Évènement",
|
||||
"DICE_ROLL": "Jet de dés",
|
||||
"PLAYER_ACTION": "Action joueur",
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def _format_session(sc: SessionContext) -> str:
|
||||
"""Bloc journal de la session en cours + résumé des sessions précédentes.
|
||||
|
||||
Fournit à l'IA le contexte temporel : ce qui s'est passé jusqu'ici,
|
||||
dans l'ordre chronologique. Permet de référencer un PNJ rencontré,
|
||||
rappeler un évènement antérieur, ou rebondir sur une action joueur.
|
||||
|
||||
Pour les sessions PRÉCÉDENTES de la même campagne, on ne remonte que
|
||||
les EVENTs (les moments marquants) pour préserver le contexte LLM.
|
||||
"""
|
||||
status = "EN COURS" if sc.active else "TERMINÉE"
|
||||
started = f" — démarrée {sc.started_at}" if sc.started_at else ""
|
||||
|
||||
previous_block = ChatUseCase._format_previous_events(sc.previous_events)
|
||||
hub_block = ChatUseCase._format_hub_status(sc)
|
||||
|
||||
if not sc.entries:
|
||||
current_block = "(Aucune entrée dans le journal pour l'instant — la session vient de commencer.)"
|
||||
else:
|
||||
lines: list[str] = []
|
||||
for e in sc.entries:
|
||||
label = ChatUseCase._ENTRY_TYPE_LABELS.get(e.type, e.type)
|
||||
ts = f" [{e.occurred_at}]" if e.occurred_at else ""
|
||||
content = e.content.replace("\n", "\n ")
|
||||
lines.append(f"- {label}{ts} : {content}")
|
||||
current_block = "\n".join(lines)
|
||||
|
||||
return (
|
||||
"--- SESSION DE JEU EN COURS ---\n"
|
||||
f"Nom : {sc.session_name}\n"
|
||||
f"Statut : {status}{started}\n"
|
||||
f"{hub_block}"
|
||||
f"{previous_block}"
|
||||
"\nJournal chronologique de la session courante (du plus ancien au plus récent) :\n"
|
||||
f"{current_block}\n\n"
|
||||
"IMPORTANT : tu es l'assistant du MJ PENDANT la partie. Tes réponses doivent :\n"
|
||||
"- Tenir compte des évènements déjà capturés (sessions précédentes + journal courant).\n"
|
||||
"- Être concrètes et utiles en temps réel : descriptions sensorielles, "
|
||||
"réactions de PNJ cohérentes avec leur fiche, suggestions de complications "
|
||||
"qui s'enchaînent à ce qui vient de se passer.\n"
|
||||
"- Éviter les longs développements : le MJ est en train d'animer une partie, "
|
||||
"il a besoin d'idées immédiatement actionnables."
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _format_hub_status(sc: SessionContext) -> str:
|
||||
"""Bloc Hub : quêtes ouvertes + flags actifs.
|
||||
|
||||
Vide si la campagne n'a aucun Arc HUB (toutes les listes vides côté Core).
|
||||
Les quêtes LOCKED apparaissent par leur TITRE uniquement : l'IA sait
|
||||
qu'elles existent (utile pour les teasers, les rumeurs en jeu) mais ne
|
||||
peut pas spoiler leurs détails.
|
||||
"""
|
||||
if (not sc.available_quests
|
||||
and not sc.in_progress_quests
|
||||
and not sc.locked_quest_titles
|
||||
and not sc.active_flags):
|
||||
return ""
|
||||
|
||||
lines = ["", "État du Hub (quêtes parallèles et faits narratifs) :"]
|
||||
|
||||
if sc.in_progress_quests:
|
||||
lines.append(" Quêtes en cours :")
|
||||
lines.extend(ChatUseCase._format_quest_lines(sc.in_progress_quests))
|
||||
|
||||
if sc.available_quests:
|
||||
lines.append(" Quêtes disponibles (non démarrées, prêtes à être lancées) :")
|
||||
lines.extend(ChatUseCase._format_quest_lines(sc.available_quests))
|
||||
|
||||
if sc.locked_quest_titles:
|
||||
titles = ", ".join(f'"{t}"' for t in sc.locked_quest_titles)
|
||||
lines.append(
|
||||
" Quêtes encore verrouillées (existent mais non accessibles — "
|
||||
f"tu peux y faire allusion sous forme de rumeurs sans spoiler leur contenu) : {titles}"
|
||||
)
|
||||
|
||||
if sc.active_flags:
|
||||
lines.append(
|
||||
" Faits actifs : " + ", ".join(f"`{f}`" for f in sc.active_flags)
|
||||
)
|
||||
|
||||
lines.append(
|
||||
" Conseille des actions cohérentes avec ces quêtes ouvertes. Ne fais "
|
||||
"PAS comme si une quête verrouillée était déjà accessible aux PJ."
|
||||
)
|
||||
lines.append("") # séparateur visuel avant le récap des sessions précédentes
|
||||
return "\n".join(lines)
|
||||
|
||||
@staticmethod
|
||||
def _format_quest_lines(quests: list[QuestSummary]) -> list[str]:
|
||||
"""Sérialise une liste de QuestSummary en lignes indentées."""
|
||||
out: list[str] = []
|
||||
for q in quests:
|
||||
arc = f" [arc : {q.arc_name}]" if q.arc_name else ""
|
||||
out.append(f" - {q.name}{arc}")
|
||||
if q.description:
|
||||
out.append(f" Synopsis : {q.description}")
|
||||
return out
|
||||
|
||||
@staticmethod
|
||||
def _format_previous_events(events: list[JournalEntrySummary]) -> str:
|
||||
"""Bloc "Story so far" : EVENTs marquants des sessions antérieures.
|
||||
|
||||
Vide si la campagne en est à sa première session. On groupe par
|
||||
session source pour aider l'IA à situer chaque évènement temporellement.
|
||||
"""
|
||||
if not events:
|
||||
return ""
|
||||
|
||||
# Groupement par session source en préservant l'ordre d'apparition.
|
||||
grouped: dict[str, list[JournalEntrySummary]] = {}
|
||||
for e in events:
|
||||
key = e.source_session_name or "(session inconnue)"
|
||||
grouped.setdefault(key, []).append(e)
|
||||
|
||||
lines = ["\nRécapitulatif des sessions précédentes (évènements marquants uniquement) :"]
|
||||
for session_name, items in grouped.items():
|
||||
lines.append(f" • {session_name} :")
|
||||
for e in items:
|
||||
ts = f" [{e.occurred_at}]" if e.occurred_at else ""
|
||||
content = e.content.replace("\n", "\n ")
|
||||
lines.append(f" - {content}{ts}")
|
||||
lines.append("") # ligne vide avant le bloc journal courant
|
||||
return "\n".join(lines) + "\n"
|
||||
|
||||
@staticmethod
|
||||
def _format_narrative_entity(ne: NarrativeEntityContext) -> str:
|
||||
"""Bloc équivalent à _format_page mais pour Arc/Chapter/Scene."""
|
||||
type_label = {"arc": "ARC", "chapter": "CHAPITRE", "scene": "SCÈNE"}.get(
|
||||
ne.entity_type.lower(), ne.entity_type.upper()
|
||||
)
|
||||
type_label = {
|
||||
"arc": "ARC",
|
||||
"chapter": "CHAPITRE",
|
||||
"scene": "SCÈNE",
|
||||
"character": "FICHE DE PERSONNAGE (PJ)",
|
||||
"npc": "FICHE DE PNJ",
|
||||
}.get(ne.entity_type.lower(), ne.entity_type.upper())
|
||||
if ne.fields:
|
||||
fields_block = "\n".join(
|
||||
f'- "{key}" : {value or "(vide)"}'
|
||||
|
||||
55
brain/app/application/chunking.py
Normal file
55
brain/app/application/chunking.py
Normal file
@@ -0,0 +1,55 @@
|
||||
"""Découpage d'un long texte en morceaux qui tiennent dans la fenêtre LLM.
|
||||
|
||||
Partagé par les imports (règles, campagne) : un livre dépasse la fenêtre de
|
||||
contexte, on le découpe par paragraphes jusqu'à une cible de tokens, en coupant
|
||||
les paragraphes géants si besoin. Dimensionnement via tiktoken (cl100k_base),
|
||||
approximation suffisante (±10% vs tokenizer natif).
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
# Cible conservatrice : tient dans une fenêtre Ollama (num_ctx 16384) en laissant
|
||||
# la place au prompt + à la sortie JSON. Les providers à grand contexte (1min.ai)
|
||||
# le supportent largement.
|
||||
CHUNK_TARGET_TOKENS = 6000
|
||||
|
||||
|
||||
def chunk_text(full_text: str, target_tokens: int = CHUNK_TARGET_TOKENS) -> list[str]:
|
||||
"""Découpe `full_text` en morceaux ~`target_tokens` tokens (frontières de §)."""
|
||||
if not full_text.strip():
|
||||
return []
|
||||
|
||||
import tiktoken
|
||||
|
||||
enc = tiktoken.get_encoding("cl100k_base")
|
||||
paragraphs = [p for p in full_text.split("\n\n") if p.strip()]
|
||||
|
||||
chunks: list[str] = []
|
||||
current: list[str] = []
|
||||
current_tokens = 0
|
||||
for para in paragraphs:
|
||||
para_tokens = len(enc.encode(para))
|
||||
# Un paragraphe seul plus gros que la cible : on le coupe en sous-blocs.
|
||||
if para_tokens > target_tokens:
|
||||
if current:
|
||||
chunks.append("\n\n".join(current))
|
||||
current, current_tokens = [], 0
|
||||
chunks.extend(_split_oversized(para, enc, target_tokens))
|
||||
continue
|
||||
if current_tokens + para_tokens > target_tokens and current:
|
||||
chunks.append("\n\n".join(current))
|
||||
current, current_tokens = [], 0
|
||||
current.append(para)
|
||||
current_tokens += para_tokens
|
||||
|
||||
if current:
|
||||
chunks.append("\n\n".join(current))
|
||||
return chunks
|
||||
|
||||
|
||||
def _split_oversized(paragraph: str, enc, target_tokens: int) -> list[str]:
|
||||
"""Coupe un paragraphe géant en sous-blocs ~`target_tokens` tokens."""
|
||||
tokens = enc.encode(paragraph)
|
||||
out: list[str] = []
|
||||
for i in range(0, len(tokens), target_tokens):
|
||||
out.append(enc.decode(tokens[i : i + target_tokens]))
|
||||
return out
|
||||
312
brain/app/application/import_campaign.py
Normal file
312
brain/app/application/import_campaign.py
Normal file
@@ -0,0 +1,312 @@
|
||||
"""Use case : import d'un PDF de campagne → arbre arc → chapitre → scène.
|
||||
|
||||
Couche APPLICATION. Même chaîne que l'import de règles (extraction + OCR +
|
||||
chunking + map-reduce) mais la cible est une ARBORESCENCE narrative :
|
||||
- MAP : chaque morceau → un sous-arbre {arcs:[{chapters:[{scenes}]}]}
|
||||
- REDUCE : fusion par NOM à chaque niveau (un chapitre coupé entre 2 morceaux
|
||||
est recollé ; ses scènes s'accumulent).
|
||||
|
||||
PROPOSITION non persistée : le Core crée les entités seulement après revue.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
|
||||
from app.application.chunking import chunk_text
|
||||
from app.application.llm_json import load_json_object
|
||||
from app.application.llm_retry import generate_with_retry
|
||||
from app.domain.models import (
|
||||
ArcProposal,
|
||||
CampaignImportResult,
|
||||
ChapterProposal,
|
||||
RoomProposal,
|
||||
SceneProposal,
|
||||
)
|
||||
from app.domain.ports import LLMProvider, PdfTextExtractor
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
_TEMPERATURE = 0.2
|
||||
|
||||
# Nom de l'arc unique quand le livre n'est pas découpé en actes/parties.
|
||||
_DEFAULT_ARC_NAME = "Aventure principale"
|
||||
|
||||
# Morceaux PLUS GROS que pour les règles : l'IA voit une quête/un chapitre entier
|
||||
# d'un coup et le structure de façon cohérente (1 scène par lieu) au lieu de le
|
||||
# fragmenter en dizaines de scènes. Adapté aux providers à grand contexte (1min.ai).
|
||||
_CHUNK_TARGET_TOKENS = 10000
|
||||
|
||||
_MAP_SYSTEM = """Tu es un assistant qui structure un livre de campagne de jeu de rôle.
|
||||
On te donne un EXTRAIT brut d'un PDF de campagne (texte parfois mal coupé par la mise en page).
|
||||
|
||||
Ta tâche : en dégager une ARBORESCENCE narrative à GROS GRAIN : arcs → chapitres → scènes,
|
||||
et — pour les lieux explorables — leurs PIÈCES (rooms).
|
||||
- Un ARC = un acte / une grande partie de la campagne (souvent un seul pour une aventure courte).
|
||||
- Un CHAPITRE = une étape majeure du récit : un chapitre du livre, OU — dans une
|
||||
campagne "hub" / bac-à-sable — UNE QUÊTE ou UN LIEU principal débloqué depuis le
|
||||
point central (ex : Dragon of Icespire Peak → chaque quête/lieu = un chapitre).
|
||||
- Une SCÈNE = un temps fort jouable du chapitre : un lieu, une rencontre clé, un moment pivot.
|
||||
- Une PIÈCE (room) = une salle d'un lieu explorable (donjon, crypte, manoir...).
|
||||
|
||||
TYPE D'ARC ("type") :
|
||||
- "HUB" si la campagne est un bac-à-sable : des quêtes/lieux optionnels, parallèles,
|
||||
débloqués depuis un point central, SANS ordre fixe imposé (ex : Dragon of Icespire Peak).
|
||||
- "LINEAR" si les chapitres se jouent dans un ordre séquentiel imposé.
|
||||
- Dans le doute : "LINEAR".
|
||||
|
||||
GRANULARITÉ (évite la sur-détection) :
|
||||
- Vise PEU de scènes : typiquement 1 à 6 par chapitre. PAS des dizaines.
|
||||
- Un LIEU EXPLORABLE (donjon, crypte, manoir, grotte à plusieurs salles) = UNE SEULE
|
||||
scène. Ses salles vont dans le tableau "rooms" de cette scène — JAMAIS en scènes séparées.
|
||||
- NE crée PAS une scène par rencontre isolée, par PNJ, par monstre ou par paragraphe.
|
||||
- IGNORE : blocs de stats, listes de monstres, encarts de règles, légendes de cartes,
|
||||
pieds de page, sommaires, crédits.
|
||||
|
||||
CONTENU D'UNE SCÈNE (fidélité au livre — important) :
|
||||
- `description` = synopsis de la scène, 2 à 4 phrases (plus que 1 ligne, mais pas le texte intégral).
|
||||
- `player_narration` = le texte d'AMBIANCE « à lire aux joueurs » (encadrés / boxed text /
|
||||
« lecture à voix haute »), recopié FIDÈLEMENT s'il existe dans l'extrait. Vide sinon.
|
||||
- `gm_notes` = les informations pour le MJ : secrets, développement, ce qui se passe,
|
||||
conséquences, indices cachés. Vide si rien de tel.
|
||||
- Ne RÉSUME pas abusivement player_narration et gm_notes : recopie le contenu utile du livre.
|
||||
|
||||
PIÈCES (rooms) — uniquement pour les scènes qui sont des lieux explorables :
|
||||
- Une entrée par salle numérotée/nommée du donjon (ex : "1. Entrée", "2. Salle des gardes").
|
||||
- `enemies` = créatures/boss de la salle (vide si aucune). `loot` = trésor/récompense (vide si aucun).
|
||||
- Pour une scène narrative classique (pas un donjon), "rooms" est un tableau vide [].
|
||||
|
||||
Format de réponse :
|
||||
- Tu réponds UNIQUEMENT par un objet JSON valide, sans markdown ni commentaire autour.
|
||||
- Schéma EXACT :
|
||||
{{"arcs": [{{"name": "...", "description": "...", "type": "LINEAR",
|
||||
"chapters": [{{"name": "...", "description": "...", "scenes": [
|
||||
{{"name": "...", "description": "...", "player_narration": "...", "gm_notes": "...",
|
||||
"rooms": [{{"name": "...", "description": "...", "enemies": "...", "loot": "..."}}]}}
|
||||
]}}]}}
|
||||
]}}
|
||||
- Utilise les VRAIS titres du livre pour les noms (pas de paraphrase).
|
||||
- Si le livre n'est PAS découpé en actes/parties, regroupe tout sous un seul arc nommé "{default_arc}".
|
||||
- N'invente pas de contenu : tu réorganises et recopies ce qui est présent dans l'extrait.
|
||||
- Si l'extrait ne contient aucune matière narrative, renvoie {{"arcs": []}}."""
|
||||
|
||||
|
||||
class _TreeMerger:
|
||||
"""Fusionne les sous-arbres des morceaux en un seul arbre, ordre préservé.
|
||||
|
||||
Clés insensibles à la casse à chaque niveau (nom d'arc / chapitre / scène).
|
||||
Description : la première non-vide rencontrée l'emporte (les morceaux suivants
|
||||
ne l'écrasent pas).
|
||||
"""
|
||||
|
||||
def __init__(self) -> None:
|
||||
# arc_key -> {"name", "description", "chapters": {chap_key -> {...}}}
|
||||
self._arcs: dict[str, dict] = {}
|
||||
|
||||
def add(self, arcs_json: list[dict]) -> None:
|
||||
for arc in arcs_json or []:
|
||||
name = str(arc.get("name", "")).strip()
|
||||
if not name:
|
||||
continue
|
||||
a = self._arcs.setdefault(
|
||||
name.lower(), {"name": name, "description": "", "type": "LINEAR", "chapters": {}})
|
||||
self._fill_desc(a, arc)
|
||||
# Type d'arc : HUB l'emporte si un seul morceau le signale (propriété globale
|
||||
# souvent énoncée une fois, dans l'intro du livre).
|
||||
if str(arc.get("type", "")).strip().upper() == "HUB":
|
||||
a["type"] = "HUB"
|
||||
for chap in arc.get("chapters", []) or []:
|
||||
cname = str(chap.get("name", "")).strip()
|
||||
if not cname:
|
||||
continue
|
||||
c = a["chapters"].setdefault(cname.lower(), {"name": cname, "description": "", "scenes": {}})
|
||||
self._fill_desc(c, chap)
|
||||
for sc in chap.get("scenes", []) or []:
|
||||
sname = str(sc.get("name", "")).strip()
|
||||
if not sname:
|
||||
continue
|
||||
s = c["scenes"].setdefault(
|
||||
sname.lower(),
|
||||
{"name": sname, "description": "", "player_narration": "",
|
||||
"gm_notes": "", "rooms": {}})
|
||||
self._fill_desc(s, sc)
|
||||
self._fill_field(s, sc, "player_narration")
|
||||
self._fill_field(s, sc, "gm_notes")
|
||||
for rm in sc.get("rooms", []) or []:
|
||||
rname = str(rm.get("name", "")).strip()
|
||||
if not rname:
|
||||
continue
|
||||
r = s["rooms"].setdefault(
|
||||
rname.lower(),
|
||||
{"name": rname, "description": "", "enemies": "", "loot": ""})
|
||||
self._fill_desc(r, rm)
|
||||
self._fill_field(r, rm, "enemies")
|
||||
self._fill_field(r, rm, "loot")
|
||||
|
||||
@staticmethod
|
||||
def _fill_desc(node: dict, src: dict) -> None:
|
||||
if not node["description"]:
|
||||
node["description"] = str(src.get("description") or "").strip()
|
||||
|
||||
@staticmethod
|
||||
def _fill_field(node: dict, src: dict, field_name: str) -> None:
|
||||
if not node[field_name]:
|
||||
node[field_name] = str(src.get(field_name) or "").strip()
|
||||
|
||||
def result(self) -> list[ArcProposal]:
|
||||
arcs: list[ArcProposal] = []
|
||||
for a in self._arcs.values():
|
||||
chapters: list[ChapterProposal] = []
|
||||
for c in a["chapters"].values():
|
||||
scenes: list[SceneProposal] = []
|
||||
for s in c["scenes"].values():
|
||||
rooms = [
|
||||
RoomProposal(r["name"], r["description"], r["enemies"], r["loot"])
|
||||
for r in s["rooms"].values()
|
||||
]
|
||||
scenes.append(SceneProposal(
|
||||
s["name"], s["description"], s["player_narration"], s["gm_notes"], rooms))
|
||||
chapters.append(ChapterProposal(c["name"], c["description"], scenes))
|
||||
arcs.append(ArcProposal(a["name"], a["description"], a["type"], chapters))
|
||||
return arcs
|
||||
|
||||
def counts(self) -> tuple[int, int, int]:
|
||||
arcs = len(self._arcs)
|
||||
chapters = sum(len(a["chapters"]) for a in self._arcs.values())
|
||||
scenes = sum(len(c["scenes"]) for a in self._arcs.values() for c in a["chapters"].values())
|
||||
return arcs, chapters, scenes
|
||||
|
||||
|
||||
class ImportCampaignUseCase:
|
||||
"""Transforme un PDF de campagne en proposition d'arbre arc→chapitre→scène."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
llm: LLMProvider,
|
||||
extractor: PdfTextExtractor,
|
||||
chunk_target_tokens: int = _CHUNK_TARGET_TOKENS,
|
||||
) -> None:
|
||||
self._llm = llm
|
||||
self._extractor = extractor
|
||||
self._chunk_target_tokens = chunk_target_tokens
|
||||
|
||||
async def execute(self, pdf_bytes: bytes) -> CampaignImportResult:
|
||||
"""Variante non-streamée : traite tout puis renvoie l'arbre complet."""
|
||||
doc = self._extractor.extract(pdf_bytes)
|
||||
chunks = chunk_text(doc.full_text, self._chunk_target_tokens)
|
||||
merger = _TreeMerger()
|
||||
for i, chunk in enumerate(chunks):
|
||||
merger.add(await self._map_chunk(chunk, index=i, total=len(chunks)))
|
||||
return CampaignImportResult(
|
||||
arcs=merger.result(),
|
||||
page_count=doc.page_count,
|
||||
ocr_page_count=doc.ocr_page_count,
|
||||
)
|
||||
|
||||
async def stream(self, pdf_bytes: bytes):
|
||||
"""Variante streamée : yield des évènements d'avancement.
|
||||
|
||||
{"type":"extracting"}, puis {"type":"start", page_count, ocr_page_count,
|
||||
total}, puis un {"type":"progress", current, total, arc_count,
|
||||
chapter_count, scene_count} par morceau, et enfin
|
||||
{"type":"done", arcs:[...], page_count, ocr_page_count}.
|
||||
"""
|
||||
yield {"type": "extracting"}
|
||||
|
||||
doc = self._extractor.extract(pdf_bytes)
|
||||
chunks = chunk_text(doc.full_text, self._chunk_target_tokens)
|
||||
total = len(chunks)
|
||||
logger.info(
|
||||
"Import campagne (stream) : %s page(s) (%s via OCR), %s morceau(x).",
|
||||
doc.page_count, doc.ocr_page_count, total,
|
||||
)
|
||||
yield {
|
||||
"type": "start",
|
||||
"page_count": doc.page_count,
|
||||
"ocr_page_count": doc.ocr_page_count,
|
||||
"total": total,
|
||||
}
|
||||
|
||||
merger = _TreeMerger()
|
||||
for i, chunk in enumerate(chunks):
|
||||
merger.add(await self._map_chunk(chunk, index=i, total=total))
|
||||
arcs, chapters, scenes = merger.counts()
|
||||
yield {
|
||||
"type": "progress",
|
||||
"current": i + 1,
|
||||
"total": total,
|
||||
"arc_count": arcs,
|
||||
"chapter_count": chapters,
|
||||
"scene_count": scenes,
|
||||
}
|
||||
|
||||
yield {
|
||||
"type": "done",
|
||||
"arcs": _serialize_arcs(merger.result()),
|
||||
"page_count": doc.page_count,
|
||||
"ocr_page_count": doc.ocr_page_count,
|
||||
}
|
||||
|
||||
# --- MAP : un morceau → sous-arbre ---------------------------------------
|
||||
|
||||
async def _map_chunk(self, chunk: str, *, index: int, total: int) -> list[dict]:
|
||||
prompt = (
|
||||
_MAP_SYSTEM.format(default_arc=_DEFAULT_ARC_NAME)
|
||||
+ f"\n\n--- EXTRAIT {index + 1}/{total} ---\n{chunk}\n\n"
|
||||
"Renvoie maintenant le JSON de l'arborescence."
|
||||
)
|
||||
raw = await generate_with_retry(
|
||||
self._llm, prompt, output_format="json", temperature=_TEMPERATURE)
|
||||
return self._parse_arcs(raw, index=index)
|
||||
|
||||
@staticmethod
|
||||
def _parse_arcs(raw: str, *, index: int) -> list[dict]:
|
||||
"""Parse robuste : objet JSON équilibré, ou récupération partielle si tronqué."""
|
||||
parsed, recovered = load_json_object(raw)
|
||||
if parsed is None:
|
||||
logger.warning("Morceau %s : aucun objet JSON exploitable, ignoré.", index)
|
||||
return []
|
||||
if recovered:
|
||||
logger.warning(
|
||||
"Morceau %s : sortie tronquée — récupération des éléments complets "
|
||||
"(envisagez des morceaux plus petits).", index)
|
||||
if isinstance(parsed, dict):
|
||||
arcs = parsed.get("arcs", [])
|
||||
return arcs if isinstance(arcs, list) else []
|
||||
return []
|
||||
|
||||
|
||||
def _serialize_arcs(arcs: list[ArcProposal]) -> list[dict]:
|
||||
"""Sérialise l'arbre de dataclasses en dicts JSON pour le flux SSE."""
|
||||
return [
|
||||
{
|
||||
"name": a.name,
|
||||
"description": a.description,
|
||||
"type": a.arc_type,
|
||||
"chapters": [
|
||||
{
|
||||
"name": c.name,
|
||||
"description": c.description,
|
||||
"scenes": [
|
||||
{
|
||||
"name": s.name,
|
||||
"description": s.description,
|
||||
"player_narration": s.player_narration,
|
||||
"gm_notes": s.gm_notes,
|
||||
"rooms": [
|
||||
{
|
||||
"name": r.name,
|
||||
"description": r.description,
|
||||
"enemies": r.enemies,
|
||||
"loot": r.loot,
|
||||
}
|
||||
for r in s.rooms
|
||||
],
|
||||
}
|
||||
for s in c.scenes
|
||||
],
|
||||
}
|
||||
for c in a.chapters
|
||||
],
|
||||
}
|
||||
for a in arcs
|
||||
]
|
||||
197
brain/app/application/import_rules.py
Normal file
197
brain/app/application/import_rules.py
Normal file
@@ -0,0 +1,197 @@
|
||||
"""Use case : import d'un PDF de règles → sections markdown structurées.
|
||||
|
||||
Couche APPLICATION. Orchestre :
|
||||
PDF (bytes) → extraction texte (port PdfTextExtractor)
|
||||
→ CHUNKING (le texte d'un livre dépasse la fenêtre de contexte)
|
||||
→ MAP : chaque morceau → {titre de section → markdown}
|
||||
→ REDUCE: fusion des sections de même titre entre morceaux
|
||||
→ RulesImportResult (proposition, NON persistée)
|
||||
|
||||
Ne dépend que des abstractions du domaine (ports LLMProvider + PdfTextExtractor)
|
||||
→ testable avec des fakes, et indépendant du provider concret (Ollama/1min.ai).
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
|
||||
from app.application.chunking import CHUNK_TARGET_TOKENS, chunk_text
|
||||
from app.application.llm_json import load_json_object
|
||||
from app.application.llm_retry import generate_with_retry
|
||||
from app.domain.models import RulesImportResult
|
||||
from app.domain.ports import LLMProvider, LLMProviderError, PdfTextExtractor
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Température basse : tâche de tri/réécriture fidèle, pas de créativité.
|
||||
_TEMPERATURE = 0.2
|
||||
|
||||
# Taxonomie canonique suggérée au modèle pour homogénéiser les titres entre
|
||||
# morceaux (sinon "Combat" / "Le combat" / "Règles de combat" se dispersent).
|
||||
# Le modèle reste libre d'en créer d'autres si rien ne correspond.
|
||||
_CANONICAL_SECTIONS = [
|
||||
"Règles générales",
|
||||
"Création de personnage",
|
||||
"Caractéristiques et tests",
|
||||
"Compétences",
|
||||
"Combat",
|
||||
"Magie et sorts",
|
||||
"Équipement et objets",
|
||||
"États et conditions",
|
||||
"Repos et récupération",
|
||||
"Progression et niveaux",
|
||||
"Conseils au Maître de Jeu",
|
||||
]
|
||||
|
||||
_MAP_SYSTEM = """Tu es un assistant qui réorganise un livre de règles de jeu de rôle.
|
||||
On te donne un EXTRAIT brut d'un PDF de règles (texte parfois mal coupé par la mise en page).
|
||||
|
||||
Ta tâche : répartir le contenu de cet extrait dans des SECTIONS THÉMATIQUES.
|
||||
|
||||
Règles impératives :
|
||||
- Tu réponds UNIQUEMENT par un objet JSON valide, sans markdown ni commentaire autour.
|
||||
- Les CLÉS sont des titres de section (texte court). Les VALEURS sont le contenu de la règle en markdown.
|
||||
- Utilise EN PRIORITÉ ces titres canoniques quand le contenu y correspond :
|
||||
{canonical}
|
||||
- Si un contenu ne rentre dans aucun, crée un titre clair et concis (en français).
|
||||
- Reproduis FIDÈLEMENT les règles : tu peux nettoyer la coupure des lignes, recoller les mots coupés
|
||||
par un tiret en fin de ligne, retirer les en-têtes/pieds de page et numéros de page parasites.
|
||||
- N'INVENTE AUCUNE règle, ne résume pas abusivement : tu réorganises, tu ne réécris pas le fond.
|
||||
- Ignore les pages de garde, sommaires, crédits, pages vides (renvoie {{}} si l'extrait n'a aucune règle)."""
|
||||
|
||||
|
||||
class _SectionMerger:
|
||||
"""Fusionne les sections issues des différents morceaux, ordre préservé.
|
||||
|
||||
Titres insensibles à la casse ("Combat" / "combat" → une seule clé). Chaque
|
||||
`add()` renvoie la liste (dé-dupliquée, ordonnée) des titres touchés par ce
|
||||
morceau — sert au flux de progression pour annoncer les sections trouvées.
|
||||
"""
|
||||
|
||||
def __init__(self) -> None:
|
||||
self._merged: dict[str, list[str]] = {}
|
||||
self._canonical_key: dict[str, str] = {}
|
||||
|
||||
def add(self, sections: dict[str, str]) -> list[str]:
|
||||
touched: list[str] = []
|
||||
for title, content in sections.items():
|
||||
title = title.strip()
|
||||
content = (content or "").strip()
|
||||
if not title or not content:
|
||||
continue
|
||||
key = title.lower()
|
||||
if key not in self._canonical_key:
|
||||
self._canonical_key[key] = title
|
||||
self._merged[title] = []
|
||||
canonical = self._canonical_key[key]
|
||||
self._merged[canonical].append(content)
|
||||
touched.append(canonical)
|
||||
# Dé-duplication en préservant l'ordre d'apparition.
|
||||
seen: set[str] = set()
|
||||
return [t for t in touched if not (t in seen or seen.add(t))]
|
||||
|
||||
def result(self) -> dict[str, str]:
|
||||
return {title: "\n\n".join(parts) for title, parts in self._merged.items()}
|
||||
|
||||
|
||||
class ImportRulesUseCase:
|
||||
"""Transforme un PDF de règles en proposition de sections markdown."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
llm: LLMProvider,
|
||||
extractor: PdfTextExtractor,
|
||||
chunk_target_tokens: int = CHUNK_TARGET_TOKENS,
|
||||
) -> None:
|
||||
self._llm = llm
|
||||
self._extractor = extractor
|
||||
self._chunk_target_tokens = chunk_target_tokens
|
||||
|
||||
async def execute(self, pdf_bytes: bytes) -> RulesImportResult:
|
||||
"""Variante non-streamée : traite tout puis renvoie le résultat complet."""
|
||||
doc = self._extractor.extract(pdf_bytes)
|
||||
chunks = chunk_text(doc.full_text, self._chunk_target_tokens)
|
||||
logger.info(
|
||||
"Import règles : %s page(s) (%s via OCR), %s morceau(x) à traiter.",
|
||||
doc.page_count, doc.ocr_page_count, len(chunks),
|
||||
)
|
||||
merger = _SectionMerger()
|
||||
for i, chunk in enumerate(chunks):
|
||||
merger.add(await self._map_chunk(chunk, index=i, total=len(chunks)))
|
||||
return RulesImportResult(
|
||||
sections=merger.result(),
|
||||
page_count=doc.page_count,
|
||||
ocr_page_count=doc.ocr_page_count,
|
||||
)
|
||||
|
||||
async def stream(self, pdf_bytes: bytes):
|
||||
"""Variante streamée : yield des évènements d'avancement au fil de l'eau.
|
||||
|
||||
Évènements (dicts) : {"type": "extracting"}, puis
|
||||
{"type": "start", page_count, ocr_page_count, total}, puis un
|
||||
{"type": "progress", current, total, new_sections:[...]} par morceau,
|
||||
et enfin {"type": "done", sections, page_count, ocr_page_count}.
|
||||
"""
|
||||
# Émis AVANT l'extraction (potentiellement lente si OCR) pour que l'UI
|
||||
# affiche tout de suite "Extraction…" plutôt qu'un écran figé.
|
||||
yield {"type": "extracting"}
|
||||
|
||||
doc = self._extractor.extract(pdf_bytes)
|
||||
chunks = chunk_text(doc.full_text, self._chunk_target_tokens)
|
||||
total = len(chunks)
|
||||
logger.info(
|
||||
"Import règles (stream) : %s page(s) (%s via OCR), %s morceau(x).",
|
||||
doc.page_count, doc.ocr_page_count, total,
|
||||
)
|
||||
yield {
|
||||
"type": "start",
|
||||
"page_count": doc.page_count,
|
||||
"ocr_page_count": doc.ocr_page_count,
|
||||
"total": total,
|
||||
}
|
||||
|
||||
merger = _SectionMerger()
|
||||
for i, chunk in enumerate(chunks):
|
||||
new_titles = merger.add(await self._map_chunk(chunk, index=i, total=total))
|
||||
yield {
|
||||
"type": "progress",
|
||||
"current": i + 1,
|
||||
"total": total,
|
||||
"new_sections": new_titles,
|
||||
}
|
||||
|
||||
yield {
|
||||
"type": "done",
|
||||
"sections": merger.result(),
|
||||
"page_count": doc.page_count,
|
||||
"ocr_page_count": doc.ocr_page_count,
|
||||
}
|
||||
|
||||
# --- MAP : un morceau → sections -----------------------------------------
|
||||
|
||||
async def _map_chunk(self, chunk: str, *, index: int, total: int) -> dict[str, str]:
|
||||
prompt = (
|
||||
_MAP_SYSTEM.format(
|
||||
canonical="\n".join(f" - {s}" for s in _CANONICAL_SECTIONS)
|
||||
)
|
||||
+ f"\n\n--- EXTRAIT {index + 1}/{total} ---\n{chunk}\n\n"
|
||||
"Renvoie maintenant le JSON des sections."
|
||||
)
|
||||
raw = await generate_with_retry(
|
||||
self._llm, prompt, output_format="json", temperature=_TEMPERATURE)
|
||||
return self._parse_sections(raw, index=index)
|
||||
|
||||
@staticmethod
|
||||
def _parse_sections(raw: str, *, index: int) -> dict[str, str]:
|
||||
"""Parse robuste : objet JSON équilibré, ou récupération partielle si tronqué."""
|
||||
parsed, recovered = load_json_object(raw)
|
||||
if parsed is None:
|
||||
logger.warning("Morceau %s : aucun objet JSON exploitable, ignoré.", index)
|
||||
return {}
|
||||
if recovered:
|
||||
logger.warning(
|
||||
"Morceau %s : sortie tronquée — récupération des sections complètes "
|
||||
"(envisagez des morceaux plus petits).", index)
|
||||
if not isinstance(parsed, dict):
|
||||
logger.warning("Morceau %s : le LLM n'a pas renvoyé un objet, ignoré.", index)
|
||||
return {}
|
||||
return {str(k): str(v) for k, v in parsed.items()}
|
||||
126
brain/app/application/llm_json.py
Normal file
126
brain/app/application/llm_json.py
Normal file
@@ -0,0 +1,126 @@
|
||||
"""Extraction robuste d'un objet JSON depuis une réponse LLM.
|
||||
|
||||
Les LLM enrobent souvent leur JSON : fences markdown ```json … ```, texte
|
||||
d'introduction, commentaire de fin, voire un 2e objet. Un simple
|
||||
`json.loads(raw)` ou un `raw[first_brace:last_brace]` échoue dans ces cas
|
||||
("Extra data", accolade parasite dans une string, etc.).
|
||||
|
||||
Cette fonction scanne depuis la PREMIÈRE `{` et renvoie exactement le premier
|
||||
objet `{…}` ÉQUILIBRÉ, en ignorant les accolades à l'intérieur des chaînes JSON
|
||||
et tout ce qui suit. Renvoie None si aucun objet complet n'est trouvé
|
||||
(sortie tronquée / accolades non refermées).
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
|
||||
|
||||
def load_json_object(raw: str) -> tuple[object | None, bool]:
|
||||
"""Parse un objet JSON depuis une réponse LLM, avec récupération si tronqué.
|
||||
|
||||
Renvoie (objet_parsé, récupéré_partiellement) :
|
||||
- d'abord on tente le 1er objet complet (extract_json_object) ;
|
||||
- sinon on tente une réparation du JSON tronqué (repair_truncated_json),
|
||||
auquel cas le second élément vaut True.
|
||||
(None, False) si rien d'exploitable.
|
||||
"""
|
||||
obj = extract_json_object(raw)
|
||||
if obj is not None:
|
||||
try:
|
||||
return json.loads(obj), False
|
||||
except json.JSONDecodeError:
|
||||
pass
|
||||
repaired = repair_truncated_json(raw)
|
||||
if repaired is not None:
|
||||
try:
|
||||
return json.loads(repaired), True
|
||||
except json.JSONDecodeError:
|
||||
pass
|
||||
return None, False
|
||||
|
||||
|
||||
def extract_json_object(raw: str) -> str | None:
|
||||
if not raw:
|
||||
return None
|
||||
text = raw.strip()
|
||||
start = text.find("{")
|
||||
if start == -1:
|
||||
return None
|
||||
|
||||
depth = 0
|
||||
in_string = False
|
||||
escape = False
|
||||
for i in range(start, len(text)):
|
||||
c = text[i]
|
||||
if in_string:
|
||||
if escape:
|
||||
escape = False
|
||||
elif c == "\\":
|
||||
escape = True
|
||||
elif c == '"':
|
||||
in_string = False
|
||||
else:
|
||||
if c == '"':
|
||||
in_string = True
|
||||
elif c == "{":
|
||||
depth += 1
|
||||
elif c == "}":
|
||||
depth -= 1
|
||||
if depth == 0:
|
||||
return text[start : i + 1]
|
||||
return None # accolades non refermées (réponse probablement tronquée)
|
||||
|
||||
|
||||
# Fermeture correspondante de chaque ouvrant, pour reconstituer un JSON tronqué.
|
||||
_CLOSE_OF = {"{": "}", "[": "]"}
|
||||
|
||||
|
||||
def repair_truncated_json(raw: str) -> str | None:
|
||||
"""Répare un JSON COUPÉ (sortie LLM tronquée) en gardant les éléments complets.
|
||||
|
||||
On scanne depuis la première `{` et on retient le DERNIER point où un conteneur
|
||||
(`}` ou `]`) vient de se fermer — donc juste après une sous-structure complète
|
||||
(un arc / chapitre / scène / pièce / section entièrement écrit). On coupe là et
|
||||
on referme les conteneurs encore ouverts. L'élément en cours d'écriture au moment
|
||||
de la troncature est abandonné, mais tous les précédents sont sauvés.
|
||||
|
||||
Renvoie une chaîne JSON équilibrée (à valider par json.loads) ou None.
|
||||
"""
|
||||
if not raw:
|
||||
return None
|
||||
text = raw.strip()
|
||||
start = text.find("{")
|
||||
if start == -1:
|
||||
return None
|
||||
|
||||
stack: list[str] = []
|
||||
in_string = False
|
||||
escape = False
|
||||
best_cut = -1 # index (exclusif) où couper
|
||||
best_closing = "" # fermetures à ajouter pour rééquilibrer
|
||||
|
||||
for i in range(start, len(text)):
|
||||
c = text[i]
|
||||
if in_string:
|
||||
if escape:
|
||||
escape = False
|
||||
elif c == "\\":
|
||||
escape = True
|
||||
elif c == '"':
|
||||
in_string = False
|
||||
else:
|
||||
if c == '"':
|
||||
in_string = True
|
||||
elif c in "{[":
|
||||
stack.append(c)
|
||||
elif c in "}]":
|
||||
if stack:
|
||||
stack.pop()
|
||||
# Point de coupe sûr : on vient de fermer une sous-structure complète.
|
||||
best_cut = i + 1
|
||||
best_closing = "".join(_CLOSE_OF[b] for b in reversed(stack))
|
||||
|
||||
if best_cut == -1:
|
||||
return None # rien de complet à sauver
|
||||
head = text[start:best_cut].rstrip().rstrip(",")
|
||||
return head + best_closing
|
||||
47
brain/app/application/llm_retry.py
Normal file
47
brain/app/application/llm_retry.py
Normal file
@@ -0,0 +1,47 @@
|
||||
"""Retry avec backoff pour les appels LLM one-shot (imports).
|
||||
|
||||
Les imports enchaînent de nombreux appels en série ; un échec TRANSITOIRE sur un
|
||||
seul morceau (503/502 surcharge serveur, 504/524 passerelle, timeout réseau) ne
|
||||
doit pas faire échouer tout l'import. On réessaie quelques fois avec une attente
|
||||
croissante. Après épuisement, on relaie l'erreur (problème durable : quota, panne).
|
||||
|
||||
Réservé aux appels `generate` (one-shot, bufferisé) : réessayer est propre, sans
|
||||
risque de doublons. À NE PAS utiliser sur le streaming (re-jouerait des tokens).
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
|
||||
from app.domain.ports import LLMProvider, LLMProviderError
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
_ATTEMPTS = 3
|
||||
_BASE_DELAY_SECONDS = 2.0
|
||||
|
||||
|
||||
async def generate_with_retry(
|
||||
llm: LLMProvider,
|
||||
prompt: str,
|
||||
*,
|
||||
output_format: str | None = None,
|
||||
temperature: float | None = None,
|
||||
) -> str:
|
||||
"""Comme `llm.generate`, mais réessaie les erreurs transitoires (backoff x2)."""
|
||||
delay = _BASE_DELAY_SECONDS
|
||||
last_error: LLMProviderError | None = None
|
||||
for attempt in range(_ATTEMPTS):
|
||||
try:
|
||||
return await llm.generate(prompt, output_format=output_format, temperature=temperature)
|
||||
except LLMProviderError as exc:
|
||||
last_error = exc
|
||||
if attempt < _ATTEMPTS - 1:
|
||||
logger.warning(
|
||||
"Appel LLM échoué (tentative %s/%s) : %s — nouvelle tentative dans %ss.",
|
||||
attempt + 1, _ATTEMPTS, exc, delay,
|
||||
)
|
||||
await asyncio.sleep(delay)
|
||||
delay *= 2
|
||||
assert last_error is not None
|
||||
raise last_error
|
||||
@@ -30,7 +30,10 @@ class Settings(BaseSettings):
|
||||
|
||||
ollama_base_url: str = "http://localhost:11434"
|
||||
llm_model: str = "gemma4:26b"
|
||||
llm_timeout_seconds: int = 120
|
||||
# Timeout HTTP des appels au LLM. Les imports/adaptations PDF génèrent de gros
|
||||
# blocs (surtout avec l'extraction riche) → 120s était trop court. Surchargeable
|
||||
# depuis l'UI (Paramètres) si un import lourd dépasse encore.
|
||||
llm_timeout_seconds: int = 300
|
||||
|
||||
# Fenêtre de contexte (num_ctx Ollama). Défaut Ollama = 2048, trop étroit
|
||||
# dès que le Structural Context du Lore dépasse ~10 pages (b9). On monte
|
||||
@@ -44,6 +47,13 @@ class Settings(BaseSettings):
|
||||
onemin_api_key: str = ""
|
||||
onemin_model: str = "gpt-4o-mini"
|
||||
|
||||
# Taille cible d'un morceau (en tokens) pour l'import de PDF (regles/campagne).
|
||||
# Plus c'est gros, moins il y a de morceaux => moins de fragmentation et un
|
||||
# import plus rapide, MAIS il faut que ca tienne dans la fenetre du modele.
|
||||
# Defaut prudent (compatible Ollama num_ctx 16384). Sur un modele a grand
|
||||
# contexte (ex: GPT-5 mini, 400k), monter a ~100000 traite un livre en 1 passe.
|
||||
import_chunk_tokens: int = 10000
|
||||
|
||||
# Secret partage entre le Core Spring et le Brain. Le Brain n'accepte une
|
||||
# requete que si l'entete X-Internal-Secret correspond. Volontairement
|
||||
# non-surchargeable via settings_store (securite critique, .env-only).
|
||||
|
||||
@@ -29,6 +29,7 @@ _ALLOWED_KEYS = frozenset({
|
||||
"llm_num_ctx",
|
||||
"onemin_api_key",
|
||||
"onemin_model",
|
||||
"import_chunk_tokens",
|
||||
})
|
||||
|
||||
|
||||
|
||||
@@ -122,9 +122,29 @@ class SceneBranchHint:
|
||||
condition: str | None = None
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class RoomBranchHint:
|
||||
"""Indice d'une sortie entre pièces (donjon). target_room_name déjà résolu côté Core."""
|
||||
|
||||
label: str
|
||||
target_room_name: str
|
||||
condition: str | None = None
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class RoomSummary:
|
||||
"""Pièce d'un lieu explorable. Projection plate pour le prompt IA (pas de notes MJ)."""
|
||||
|
||||
name: str
|
||||
floor: int | None = None
|
||||
description: str | None = None
|
||||
enemies: str | None = None
|
||||
branches: list[RoomBranchHint] = field(default_factory=list)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class SceneSummary:
|
||||
"""Résumé d'une scène : nom + description courte + illustrations + branches."""
|
||||
"""Résumé d'une scène : nom + description courte + illustrations + branches + pièces."""
|
||||
|
||||
name: str
|
||||
description: str | None
|
||||
@@ -133,6 +153,8 @@ class SceneSummary:
|
||||
illustration_count: int = 0
|
||||
# Connexions narratives sortantes (livre dont vous etes le heros).
|
||||
branches: list[SceneBranchHint] = field(default_factory=list)
|
||||
# Pièces du lieu explorable (vide = scène classique).
|
||||
rooms: list[RoomSummary] = field(default_factory=list)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
@@ -169,6 +191,34 @@ class CampaignStructuralContext:
|
||||
campaign_name: str
|
||||
campaign_description: str | None
|
||||
arcs: list[ArcSummary]
|
||||
characters: list["CharacterSummary"] = field(default_factory=list)
|
||||
npcs: list["NpcSummary"] = 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)
|
||||
class NpcSummary:
|
||||
"""Résumé d'un PNJ : symétrique à CharacterSummary.
|
||||
|
||||
Permet à l'IA de connaître les PNJ d'une campagne (nom + snippet) sans
|
||||
injecter leurs fiches complètes. Évolution prévue : entity_type="npc"
|
||||
pour focus sur la fiche complète.
|
||||
"""
|
||||
|
||||
name: str
|
||||
snippet: str
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
@@ -184,3 +234,195 @@ class NarrativeEntityContext:
|
||||
entity_type: str
|
||||
title: 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]
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class JournalEntrySummary:
|
||||
"""Une entrée du journal d'une Session.
|
||||
|
||||
`source_session_name` n'est renseigné que pour les entrées issues de
|
||||
sessions précédentes (option 3 : continuité narrative entre séances).
|
||||
"""
|
||||
|
||||
type: str
|
||||
content: str
|
||||
occurred_at: str | None
|
||||
source_session_name: str | None = None
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class QuestSummary:
|
||||
"""Résumé d'une quête (Chapter dans un Arc HUB) pour le system prompt.
|
||||
|
||||
Volontairement sans notes MJ ni statut texte : c'est déjà classé côté Core
|
||||
dans available_quests / in_progress_quests / locked_quest_titles.
|
||||
"""
|
||||
|
||||
name: str
|
||||
arc_name: str
|
||||
description: str | None = None
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class SessionContext:
|
||||
"""Contexte d'une Session de jeu en cours (Play Context).
|
||||
|
||||
Combine plusieurs niveaux :
|
||||
- `entries` : journal COMPLET de la session courante (cappé ~80 entrées)
|
||||
- `previous_events` : EVENTs marquants des sessions précédentes (continuité)
|
||||
- `available_quests` / `in_progress_quests` : quêtes du Hub ouvertes
|
||||
- `locked_quest_titles` : titres seuls des quêtes verrouillées (anti-spoiler)
|
||||
- `active_flags` : noms des flags de campagne actuellement à true
|
||||
"""
|
||||
|
||||
session_name: str
|
||||
active: bool
|
||||
started_at: str | None
|
||||
entries: list[JournalEntrySummary]
|
||||
previous_events: list[JournalEntrySummary]
|
||||
available_quests: list[QuestSummary] = field(default_factory=list)
|
||||
in_progress_quests: list[QuestSummary] = field(default_factory=list)
|
||||
locked_quest_titles: list[str] = field(default_factory=list)
|
||||
active_flags: list[str] = field(default_factory=list)
|
||||
|
||||
|
||||
# ─────────────────────── Import de PDF (règles → GameSystem) ───────────────────────
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ExtractedPage:
|
||||
"""Texte extrait d'UNE page de PDF, avec la trace de la méthode utilisée.
|
||||
|
||||
`used_ocr=True` signale que la page n'avait pas de couche texte exploitable
|
||||
(born-digital absent) et a donc été rasterisée puis passée à l'OCR. Permet
|
||||
au CLI/diagnostic de dire à l'utilisateur si son PDF est "texte" ou "scan".
|
||||
"""
|
||||
|
||||
index: int # 0-based
|
||||
text: str
|
||||
used_ocr: bool
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ExtractedDocument:
|
||||
"""Résultat brut de l'extraction d'un PDF : une entrée par page."""
|
||||
|
||||
pages: list[ExtractedPage]
|
||||
|
||||
@property
|
||||
def page_count(self) -> int:
|
||||
return len(self.pages)
|
||||
|
||||
@property
|
||||
def ocr_page_count(self) -> int:
|
||||
return sum(1 for p in self.pages if p.used_ocr)
|
||||
|
||||
@property
|
||||
def full_text(self) -> str:
|
||||
"""Concatène le texte de toutes les pages, séparées par un saut double."""
|
||||
return "\n\n".join(p.text for p in self.pages if p.text.strip())
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class RulesImportResult:
|
||||
"""Proposition structurée de règles : sections markdown indexées par titre.
|
||||
|
||||
`sections` = {titre H2 → contenu markdown}. C'est une PROPOSITION : rien
|
||||
n'est persisté côté Core tant que l'utilisateur n'a pas validé/édité.
|
||||
`page_count` / `ocr_page_count` remontent au diagnostic d'extraction.
|
||||
"""
|
||||
|
||||
sections: dict[str, str]
|
||||
page_count: int
|
||||
ocr_page_count: int
|
||||
|
||||
def to_markdown(self) -> str:
|
||||
"""Assemble les sections en un markdown monolithique (## titre + contenu).
|
||||
|
||||
Format aligné sur `GameSystem.rulesMarkdown` côté Core (découpé par H2).
|
||||
"""
|
||||
blocks = [f"## {title}\n\n{content.strip()}" for title, content in self.sections.items()]
|
||||
return "\n\n".join(blocks).strip() + "\n"
|
||||
|
||||
|
||||
# ─────────────────────── Import de PDF de campagne (arbre arc→chapitre→scène) ──────────────
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class RoomProposal:
|
||||
"""Pièce d'un lieu explorable (donjon) proposée pour une scène."""
|
||||
|
||||
name: str
|
||||
description: str
|
||||
enemies: str = ""
|
||||
loot: str = ""
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class SceneProposal:
|
||||
"""Scène proposée. `rooms` non vide => donjon/lieu explorable.
|
||||
|
||||
On capture aussi, quand le livre les fournit, le texte d'encadré « à lire aux
|
||||
joueurs » (`player_narration`) et les secrets/développement MJ (`gm_notes`).
|
||||
"""
|
||||
|
||||
name: str
|
||||
description: str
|
||||
player_narration: str = ""
|
||||
gm_notes: str = ""
|
||||
rooms: list[RoomProposal] = field(default_factory=list)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ChapterProposal:
|
||||
"""Chapitre proposé : nom + synopsis + ses scènes."""
|
||||
|
||||
name: str
|
||||
description: str
|
||||
scenes: list[SceneProposal] = field(default_factory=list)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ArcProposal:
|
||||
"""Arc proposé : nom + synopsis + type (LINEAR/HUB) + ses chapitres."""
|
||||
|
||||
name: str
|
||||
description: str
|
||||
arc_type: str = "LINEAR"
|
||||
chapters: list[ChapterProposal] = field(default_factory=list)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class CampaignImportResult:
|
||||
"""Proposition d'arborescence narrative extraite d'un PDF de campagne.
|
||||
|
||||
PROPOSITION non persistée : l'UI laisse l'utilisateur réviser/éditer l'arbre
|
||||
avant la création effective des arcs/chapitres/scènes côté Core.
|
||||
"""
|
||||
|
||||
arcs: list[ArcProposal]
|
||||
page_count: int
|
||||
ocr_page_count: int
|
||||
|
||||
def counts(self) -> tuple[int, int, int]:
|
||||
"""(nb arcs, nb chapitres, nb scènes) — pour le diagnostic / la progression."""
|
||||
chapters = sum(len(a.chapters) for a in self.arcs)
|
||||
scenes = sum(len(c.scenes) for a in self.arcs for c in a.chapters)
|
||||
return len(self.arcs), chapters, scenes
|
||||
|
||||
@@ -7,7 +7,10 @@ En Python moderne on privilégie Protocol (PEP 544) sur ABC pour bénéficier
|
||||
du duck typing structurel : toute classe qui possède les bonnes méthodes
|
||||
satisfait le contrat, sans héritage explicite.
|
||||
"""
|
||||
from typing import AsyncIterator, Protocol
|
||||
from typing import TYPE_CHECKING, AsyncIterator, Protocol
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from app.domain.models import ExtractedDocument
|
||||
|
||||
|
||||
class LLMProvider(Protocol):
|
||||
@@ -78,6 +81,32 @@ class LLMChatProvider(Protocol):
|
||||
...
|
||||
|
||||
|
||||
class PdfTextExtractor(Protocol):
|
||||
"""Port sortant — extrait le texte d'un PDF (born-digital ou scan).
|
||||
|
||||
L'implémentation décide de sa stratégie (couche texte directe, repli OCR
|
||||
page par page…). Le domaine ne connaît ni PyMuPDF ni Tesseract.
|
||||
"""
|
||||
|
||||
def extract(self, pdf_bytes: bytes) -> "ExtractedDocument":
|
||||
"""Extrait le texte du PDF fourni sous forme d'octets.
|
||||
|
||||
Args:
|
||||
pdf_bytes: contenu binaire du fichier PDF.
|
||||
|
||||
Returns:
|
||||
ExtractedDocument : une entrée par page (texte + flag OCR).
|
||||
|
||||
Raises:
|
||||
PdfExtractionError: si le PDF est illisible/corrompu.
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
class PdfExtractionError(Exception):
|
||||
"""Erreur du domaine : un PDF n'a pas pu être lu/extrait."""
|
||||
|
||||
|
||||
class LLMProviderError(Exception):
|
||||
"""Erreur du domaine signalant qu'un LLMProvider n'a pas pu générer.
|
||||
|
||||
|
||||
@@ -61,7 +61,16 @@ class OllamaLLMProvider:
|
||||
async with httpx.AsyncClient(timeout=self._timeout) as client:
|
||||
try:
|
||||
response = await client.post(url, json=payload)
|
||||
response.raise_for_status()
|
||||
if response.status_code >= 400:
|
||||
body = response.text
|
||||
try:
|
||||
err_obj = json.loads(body)
|
||||
err_msg = err_obj.get("error") or body
|
||||
except json.JSONDecodeError:
|
||||
err_msg = body
|
||||
raise LLMProviderError(
|
||||
f"Ollama HTTP {response.status_code} : {err_msg.strip()[:500]}"
|
||||
)
|
||||
except httpx.HTTPError as exc:
|
||||
raise LLMProviderError(
|
||||
f"Erreur lors de l'appel à Ollama : {exc}"
|
||||
@@ -105,7 +114,20 @@ class OllamaLLMProvider:
|
||||
async with httpx.AsyncClient(timeout=self._timeout) as client:
|
||||
try:
|
||||
async with client.stream("POST", url, json=payload) as response:
|
||||
response.raise_for_status()
|
||||
if response.status_code >= 400:
|
||||
# On lit le body d'erreur pour le remonter a l'utilisateur,
|
||||
# sinon on ne voit que "500 Internal Server Error" sans
|
||||
# savoir POURQUOI Ollama refuse (modele introuvable, OOM,
|
||||
# num_ctx trop grand pour la VRAM, etc.).
|
||||
body = (await response.aread()).decode("utf-8", errors="replace")
|
||||
try:
|
||||
err_obj = json.loads(body)
|
||||
err_msg = err_obj.get("error") or body
|
||||
except json.JSONDecodeError:
|
||||
err_msg = body
|
||||
raise LLMProviderError(
|
||||
f"Ollama HTTP {response.status_code} : {err_msg.strip()[:500]}"
|
||||
)
|
||||
async for line in response.aiter_lines():
|
||||
if not line.strip():
|
||||
continue
|
||||
|
||||
@@ -14,6 +14,7 @@ avec des marqueurs de role lisibles pour le modele.
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
from typing import AsyncIterator
|
||||
|
||||
import httpx
|
||||
@@ -22,6 +23,8 @@ from app.core.config import Settings
|
||||
from app.domain.models import ChatMessage
|
||||
from app.domain.ports import LLMProviderError
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
_API_BASE = "https://api.1min.ai/api/chat-with-ai"
|
||||
_PAYLOAD_TYPE = "UNIFY_CHAT_WITH_AI"
|
||||
|
||||
@@ -48,6 +51,18 @@ class OneMinAiLLMProvider:
|
||||
"promptObject": {"prompt": prompt},
|
||||
}
|
||||
|
||||
def _format_http_error(self, exc: httpx.HTTPError) -> str:
|
||||
"""Message d'erreur lisible. Un timeout httpx a un str() vide → on le nomme."""
|
||||
if isinstance(exc, httpx.TimeoutException):
|
||||
return (
|
||||
f"Erreur 1min.ai : délai dépassé (timeout {self._timeout}s). Le modèle a mis "
|
||||
"trop de temps à répondre — typique d'un morceau d'import trop gros. "
|
||||
"Réduisez « Taille des morceaux à l'import » (Paramètres → Import de PDF) "
|
||||
"ou augmentez le timeout LLM."
|
||||
)
|
||||
detail = str(exc) or exc.__class__.__name__
|
||||
return f"Erreur 1min.ai ({exc.__class__.__name__}) : {detail}"
|
||||
|
||||
async def generate(
|
||||
self,
|
||||
prompt: str,
|
||||
@@ -55,18 +70,18 @@ class OneMinAiLLMProvider:
|
||||
output_format: str | None = None, # 1min.ai ne supporte pas format=json
|
||||
temperature: float | None = None, # idem, pas d'hyperparam expose ici
|
||||
) -> str:
|
||||
"""Appel one-shot : retourne la reponse complete sous forme de string."""
|
||||
async with httpx.AsyncClient(timeout=self._timeout) as client:
|
||||
try:
|
||||
response = await client.post(
|
||||
_API_BASE, headers=self._headers(), json=self._payload(prompt)
|
||||
)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
except httpx.HTTPError as exc:
|
||||
raise LLMProviderError(f"Erreur 1min.ai : {exc}") from exc
|
||||
"""One-shot, mais via l'endpoint STREAMING (puis recollage).
|
||||
|
||||
return self._extract_result(data)
|
||||
On NE passe PAS par l'endpoint non-streame `chat-with-ai` : sur les longues
|
||||
generations (gros imports), la passerelle Cloudflare de 1min.ai coupe la
|
||||
connexion au bout de ~100s et renvoie un HTTP 524. En streaming, des octets
|
||||
circulent en continu => pas de 524, quelle que soit la duree. On accumule
|
||||
tous les fragments pour reconstituer la reponse complete.
|
||||
"""
|
||||
chunks: list[str] = []
|
||||
async for token in self._stream_prompt(prompt):
|
||||
chunks.append(token)
|
||||
return "".join(chunks)
|
||||
|
||||
async def stream_chat(
|
||||
self,
|
||||
@@ -75,17 +90,18 @@ class OneMinAiLLMProvider:
|
||||
system_prompt: str | None = None,
|
||||
temperature: float | None = None,
|
||||
) -> AsyncIterator[str]:
|
||||
"""Streame via SSE.
|
||||
|
||||
1min.ai expose deux evenements utiles :
|
||||
- `event: content` → `data: {"content": "..."}`
|
||||
- `event: done` → fin du stream
|
||||
- `event: error` → erreur serveur
|
||||
On yield le champ `content` au fil de l'arrivee.
|
||||
"""
|
||||
"""Streame une conversation : aplatit les messages puis delegue au coeur SSE."""
|
||||
prompt = self._flatten_messages(messages, system_prompt)
|
||||
url = f"{_API_BASE}?isStreaming=true"
|
||||
async for token in self._stream_prompt(prompt):
|
||||
yield token
|
||||
|
||||
async def _stream_prompt(self, prompt: str) -> AsyncIterator[str]:
|
||||
"""Coeur du streaming SSE 1min.ai (`?isStreaming=true`) pour un prompt brut.
|
||||
|
||||
1min.ai expose : `event: content` → `data: {"content": "..."}`, `event: done`,
|
||||
`event: error`. On yield le champ `content` au fil de l'arrivee.
|
||||
"""
|
||||
url = f"{_API_BASE}?isStreaming=true"
|
||||
async with httpx.AsyncClient(timeout=self._timeout) as client:
|
||||
try:
|
||||
async with client.stream(
|
||||
@@ -95,9 +111,7 @@ class OneMinAiLLMProvider:
|
||||
async for token in self._parse_sse(response):
|
||||
yield token
|
||||
except httpx.HTTPError as exc:
|
||||
raise LLMProviderError(
|
||||
f"Erreur lors du streaming 1min.ai : {exc}"
|
||||
) from exc
|
||||
raise LLMProviderError(self._format_http_error(exc)) from exc
|
||||
|
||||
# --- Helpers ------------------------------------------------------------
|
||||
|
||||
@@ -146,12 +160,21 @@ class OneMinAiLLMProvider:
|
||||
"""
|
||||
record = payload.get("aiRecord") or {}
|
||||
detail = record.get("aiRecordDetail") or {}
|
||||
result = detail.get("resultObject") or []
|
||||
if isinstance(result, list):
|
||||
result = detail.get("resultObject")
|
||||
if isinstance(result, list) and result:
|
||||
return "".join(str(x) for x in result)
|
||||
if isinstance(result, str):
|
||||
if isinstance(result, str) and result:
|
||||
return result
|
||||
raise LLMProviderError("Reponse 1min.ai inattendue : resultObject absent.")
|
||||
|
||||
# Schema inattendu : on remonte un EXTRAIT du vrai payload pour diagnostiquer.
|
||||
# Causes frequentes : credits/quota 1min.ai epuises, moderation, modele
|
||||
# indisponible, ou reponse asynchrone (record cree mais resultat pas encore
|
||||
# pret). Sans ce detail, l'erreur "resultObject absent" est aveugle.
|
||||
snippet = json.dumps(payload, ensure_ascii=False)
|
||||
if len(snippet) > 800:
|
||||
snippet = snippet[:800] + "…"
|
||||
logger.warning("Reponse 1min.ai inattendue (resultObject absent) : %s", snippet)
|
||||
raise LLMProviderError(f"Reponse 1min.ai inattendue (resultObject absent) : {snippet}")
|
||||
|
||||
@staticmethod
|
||||
def _flatten_messages(
|
||||
|
||||
102
brain/app/infrastructure/pdf_extractor.py
Normal file
102
brain/app/infrastructure/pdf_extractor.py
Normal file
@@ -0,0 +1,102 @@
|
||||
"""Adapter d'extraction de texte PDF — implémente le port PdfTextExtractor.
|
||||
|
||||
Stratégie HYBRIDE auto :
|
||||
1. On tente d'abord l'extraction de la couche texte (PyMuPDF). Les PDF
|
||||
"born-digital" (livres de règles officiels type Nimble, faits dans
|
||||
InDesign/Affinity : très graphiques mais avec une vraie couche texte)
|
||||
passent par là → rapide, fidèle, AUCUN OCR.
|
||||
2. Si une page ne rend (quasi) aucun texte → c'est probablement une image
|
||||
(scan ou page 100% illustrée). On rasterise la page et on la passe à
|
||||
Tesseract (OCR). Gère donc aussi les scans purs et les PDF mixtes.
|
||||
|
||||
Tesseract est un binaire SYSTÈME (installé dans l'image Docker du Brain). S'il
|
||||
est absent (ex: exécution locale Windows sans install), l'OCR est désactivé
|
||||
proprement : les pages-images ressortent vides mais l'extraction ne plante pas,
|
||||
et le diagnostic le signale (used_ocr reste False, texte vide).
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
|
||||
import pymupdf as fitz # PyMuPDF — on importe par le nom canonique `pymupdf`
|
||||
# (et NON `import fitz`) pour éviter la collision avec le faux paquet PyPI "fitz"
|
||||
# qui échoue sur `from frontend import *`.
|
||||
|
||||
from app.domain.models import ExtractedDocument, ExtractedPage
|
||||
from app.domain.ports import PdfExtractionError
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# En dessous de ce nombre de caractères "significatifs" sur une page, on
|
||||
# considère qu'il n'y a pas de couche texte exploitable → repli OCR.
|
||||
_MIN_TEXT_CHARS = 20
|
||||
|
||||
# DPI de rasterisation avant OCR. 300 = bon compromis qualité/vitesse pour du
|
||||
# texte de livre. Plus haut = plus lent et plus gourmand en mémoire.
|
||||
_OCR_DPI = 300
|
||||
|
||||
# Langues Tesseract : français + anglais (la plupart des règles de JDR FR ont
|
||||
# des termes anglais résiduels). Doivent être installées dans l'image Docker
|
||||
# (tesseract-ocr-fra, tesseract-ocr-eng).
|
||||
_OCR_LANGS = "fra+eng"
|
||||
|
||||
|
||||
class PyMuPdfTextExtractor:
|
||||
"""Extracteur PDF basé sur PyMuPDF, avec repli OCR Tesseract optionnel."""
|
||||
|
||||
def __init__(self) -> None:
|
||||
# On détecte la disponibilité de l'OCR une seule fois (le binaire
|
||||
# Tesseract ne va pas apparaître/disparaître en cours d'exécution).
|
||||
self._ocr_available = self._detect_ocr()
|
||||
|
||||
@staticmethod
|
||||
def _detect_ocr() -> bool:
|
||||
"""True si pytesseract + le binaire Tesseract sont disponibles."""
|
||||
try:
|
||||
import pytesseract
|
||||
|
||||
pytesseract.get_tesseract_version()
|
||||
return True
|
||||
except Exception as exc: # ImportError, TesseractNotFoundError, etc.
|
||||
logger.warning(
|
||||
"OCR indisponible (Tesseract non installé ?) : %s. "
|
||||
"Les pages sans couche texte ressortiront vides.",
|
||||
exc,
|
||||
)
|
||||
return False
|
||||
|
||||
def extract(self, pdf_bytes: bytes) -> ExtractedDocument:
|
||||
try:
|
||||
doc = fitz.open(stream=pdf_bytes, filetype="pdf")
|
||||
except Exception as exc:
|
||||
raise PdfExtractionError(f"PDF illisible ou corrompu : {exc}") from exc
|
||||
|
||||
pages: list[ExtractedPage] = []
|
||||
try:
|
||||
for index, page in enumerate(doc):
|
||||
text = (page.get_text() or "").strip()
|
||||
used_ocr = False
|
||||
if len(text) < _MIN_TEXT_CHARS and self._ocr_available:
|
||||
ocr_text = self._ocr_page(page)
|
||||
if ocr_text.strip():
|
||||
text = ocr_text.strip()
|
||||
used_ocr = True
|
||||
pages.append(ExtractedPage(index=index, text=text, used_ocr=used_ocr))
|
||||
finally:
|
||||
doc.close()
|
||||
|
||||
return ExtractedDocument(pages=pages)
|
||||
|
||||
@staticmethod
|
||||
def _ocr_page(page: "fitz.Page") -> str:
|
||||
"""Rasterise une page et lui applique l'OCR Tesseract."""
|
||||
import pytesseract
|
||||
from PIL import Image
|
||||
|
||||
pix = page.get_pixmap(dpi=_OCR_DPI)
|
||||
img = Image.frombytes("RGB", (pix.width, pix.height), pix.samples)
|
||||
try:
|
||||
return pytesseract.image_to_string(img, lang=_OCR_LANGS)
|
||||
except Exception as exc:
|
||||
logger.warning("Échec OCR sur la page %s : %s", page.number, exc)
|
||||
return ""
|
||||
@@ -10,35 +10,47 @@ from typing import Annotated, AsyncIterator, Literal
|
||||
import hmac
|
||||
import httpx
|
||||
import tiktoken
|
||||
from fastapi import Depends, FastAPI, HTTPException, Request
|
||||
from fastapi import Depends, FastAPI, File, Form, HTTPException, Request, UploadFile
|
||||
from fastapi.responses import JSONResponse, StreamingResponse
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from app.application.adapt_campaign import AdaptCampaignUseCase
|
||||
from app.application.chat import ChatUseCase
|
||||
from app.application.generate_page import GeneratePageUseCase
|
||||
from app.application.import_campaign import ImportCampaignUseCase
|
||||
from app.application.import_rules import ImportRulesUseCase
|
||||
from app.core.config import Settings, get_settings
|
||||
from app.core.settings_store import save_overrides
|
||||
from app.domain.models import (
|
||||
ArcSummary,
|
||||
CampaignStructuralContext,
|
||||
ChapterSummary,
|
||||
CharacterSummary,
|
||||
NpcSummary,
|
||||
ChatMessage,
|
||||
GameSystemContext,
|
||||
JournalEntrySummary,
|
||||
LoreStructuralContext,
|
||||
NarrativeEntityContext,
|
||||
PageContext,
|
||||
PageGenerationContext,
|
||||
PageSummary,
|
||||
QuestSummary,
|
||||
RoomBranchHint,
|
||||
RoomSummary,
|
||||
SceneBranchHint,
|
||||
SceneSummary,
|
||||
SessionContext,
|
||||
)
|
||||
from app.domain.ports import LLMProvider, LLMProviderError
|
||||
from app.domain.ports import LLMProvider, LLMProviderError, PdfExtractionError
|
||||
from app.infrastructure.ollama_adapter import OllamaLLMProvider
|
||||
from app.infrastructure.onemin_adapter import OneMinAiLLMProvider
|
||||
from app.infrastructure.pdf_extractor import PyMuPdfTextExtractor
|
||||
|
||||
app = FastAPI(
|
||||
title="LoreMind Brain",
|
||||
description="Backend IA pour la génération de contenu narratif.",
|
||||
version="0.4.0",
|
||||
version="0.10.1-beta",
|
||||
)
|
||||
|
||||
|
||||
@@ -166,6 +178,24 @@ class SceneBranchHintDTO(BaseModel):
|
||||
condition: str | None = None
|
||||
|
||||
|
||||
class RoomBranchHintDTO(BaseModel):
|
||||
"""Sortie d'une pièce vers une autre pièce du même lieu (donjon)."""
|
||||
|
||||
label: str
|
||||
target_room_name: str
|
||||
condition: str | None = None
|
||||
|
||||
|
||||
class RoomSummaryDTO(BaseModel):
|
||||
"""Pièce d'un lieu explorable. Omise par le Core si la scène est classique."""
|
||||
|
||||
name: str
|
||||
floor: int | None = None
|
||||
description: str | None = None
|
||||
enemies: str | None = None
|
||||
branches: list[RoomBranchHintDTO] = Field(default_factory=list)
|
||||
|
||||
|
||||
class SceneSummaryDTO(BaseModel):
|
||||
"""Résumé d'une scène : nom + description courte (synopsis)."""
|
||||
|
||||
@@ -176,6 +206,8 @@ class SceneSummaryDTO(BaseModel):
|
||||
illustration_count: int = 0
|
||||
# Branches narratives sortantes, omises cote Core si vides.
|
||||
branches: list[SceneBranchHintDTO] = Field(default_factory=list)
|
||||
# Pièces du lieu explorable, omises par Core si scène classique.
|
||||
rooms: list[RoomSummaryDTO] = Field(default_factory=list)
|
||||
|
||||
|
||||
class ChapterSummaryDTO(BaseModel):
|
||||
@@ -196,29 +228,98 @@ class ArcSummaryDTO(BaseModel):
|
||||
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 NpcSummaryDTO(BaseModel):
|
||||
"""Résumé d'un PNJ : symétrique à CharacterSummaryDTO."""
|
||||
|
||||
name: str
|
||||
snippet: str = ""
|
||||
|
||||
|
||||
class CampaignContextDTO(BaseModel):
|
||||
"""Carte narrative enrichie : arcs → chapitres → scènes avec synopsis."""
|
||||
|
||||
campaign_name: str
|
||||
campaign_description: str | None = None
|
||||
arcs: list[ArcSummaryDTO] = Field(default_factory=list)
|
||||
characters: list[CharacterSummaryDTO] = Field(default_factory=list)
|
||||
npcs: list[NpcSummaryDTO] = Field(default_factory=list)
|
||||
|
||||
|
||||
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|npc)$")
|
||||
title: str
|
||||
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 JournalEntrySummaryDTO(BaseModel):
|
||||
"""Une entrée du journal de session.
|
||||
|
||||
`source_session_name` est présent uniquement pour les évènements issus
|
||||
des sessions précédentes — sert à ancrer temporellement dans le prompt.
|
||||
"""
|
||||
|
||||
type: str
|
||||
content: str
|
||||
occurred_at: str | None = None
|
||||
source_session_name: str | None = None
|
||||
|
||||
|
||||
class QuestSummaryDTO(BaseModel):
|
||||
"""Résumé d'une quête (Chapter dans un Arc HUB). Voir QuestSummary côté domaine."""
|
||||
|
||||
name: str
|
||||
arc_name: str
|
||||
description: str | None = None
|
||||
|
||||
|
||||
class SessionContextDTO(BaseModel):
|
||||
"""Contexte d'une Session de jeu en cours (Play Context).
|
||||
|
||||
Combine le journal complet (`entries`), les EVENTs des sessions précédentes
|
||||
(`previous_events`), et — depuis l'ajout du mode Hub — l'état des quêtes
|
||||
Hub de la campagne (disponibles / en cours / verrouillées) plus les flags
|
||||
narratifs actuellement actifs.
|
||||
"""
|
||||
|
||||
session_name: str
|
||||
active: bool
|
||||
started_at: str | None = None
|
||||
entries: list[JournalEntrySummaryDTO] = Field(default_factory=list)
|
||||
previous_events: list[JournalEntrySummaryDTO] = Field(default_factory=list)
|
||||
available_quests: list[QuestSummaryDTO] = Field(default_factory=list)
|
||||
in_progress_quests: list[QuestSummaryDTO] = Field(default_factory=list)
|
||||
locked_quest_titles: list[str] = Field(default_factory=list)
|
||||
active_flags: list[str] = Field(default_factory=list)
|
||||
|
||||
|
||||
class ChatStreamRequestDTO(BaseModel):
|
||||
"""Requête de chat streamé : historique + contextes structurels.
|
||||
|
||||
Les 4 contextes (lore, page, campaign, narrative_entity) sont optionnels,
|
||||
mais au moins l'un des deux "niveaux haut" (lore_context ou
|
||||
campaign_context) doit être fourni. Le validateur `check_scope` applique
|
||||
cette règle à la frontière HTTP.
|
||||
Les contextes (lore, page, campaign, narrative_entity, session) sont
|
||||
optionnels, mais au moins l'un des contextes "racines" (lore_context,
|
||||
campaign_context ou session_context) doit être fourni. Le validateur
|
||||
`check_scope` applique cette règle à la frontière HTTP.
|
||||
"""
|
||||
|
||||
messages: list[ChatMessageDTO] = Field(min_length=1)
|
||||
@@ -226,10 +327,16 @@ class ChatStreamRequestDTO(BaseModel):
|
||||
page_context: PageContextDTO | None = None
|
||||
campaign_context: CampaignContextDTO | None = None
|
||||
narrative_entity: NarrativeEntityDTO | None = None
|
||||
game_system_context: GameSystemContextDTO | None = None
|
||||
session_context: SessionContextDTO | None = None
|
||||
|
||||
def has_scope(self) -> bool:
|
||||
"""Vrai si au moins un contexte racine (Lore ou Campagne) est fourni."""
|
||||
return self.lore_context is not None or self.campaign_context is not None
|
||||
"""Vrai si au moins un contexte racine (Lore, Campagne ou Session) est fourni."""
|
||||
return (
|
||||
self.lore_context is not None
|
||||
or self.campaign_context is not None
|
||||
or self.session_context is not None
|
||||
)
|
||||
|
||||
|
||||
# --- Factories d'injection de dépendance ---
|
||||
@@ -272,6 +379,40 @@ def get_chat_use_case(
|
||||
return ChatUseCase(llm=llm) # type: ignore[arg-type]
|
||||
|
||||
|
||||
# Extracteur PDF partagé : la détection OCR (version Tesseract) a un coût
|
||||
# (subprocess) qu'on ne veut pas payer à chaque requête → singleton module.
|
||||
_PDF_EXTRACTOR = PyMuPdfTextExtractor()
|
||||
|
||||
|
||||
def get_import_rules_use_case(
|
||||
llm: Annotated[LLMProvider, Depends(get_llm_provider)],
|
||||
settings: Annotated[Settings, Depends(get_settings)],
|
||||
) -> ImportRulesUseCase:
|
||||
"""Factory du use case d'import de règles PDF (extraction + structuration)."""
|
||||
return ImportRulesUseCase(
|
||||
llm=llm, extractor=_PDF_EXTRACTOR, chunk_target_tokens=settings.import_chunk_tokens)
|
||||
|
||||
|
||||
def get_import_campaign_use_case(
|
||||
llm: Annotated[LLMProvider, Depends(get_llm_provider)],
|
||||
settings: Annotated[Settings, Depends(get_settings)],
|
||||
) -> ImportCampaignUseCase:
|
||||
"""Factory du use case d'import de campagne PDF (extraction + arborescence)."""
|
||||
return ImportCampaignUseCase(
|
||||
llm=llm, extractor=_PDF_EXTRACTOR, chunk_target_tokens=settings.import_chunk_tokens)
|
||||
|
||||
|
||||
def get_adapt_campaign_use_case(
|
||||
llm: Annotated[LLMProvider, Depends(get_llm_provider)],
|
||||
settings: Annotated[Settings, Depends(get_settings)],
|
||||
) -> AdaptCampaignUseCase:
|
||||
"""Factory du use case d'adaptation d'un PDF à une campagne (conseils streamés)."""
|
||||
# L'adapter satisfait aussi LLMChatProvider (stream_chat) par duck typing.
|
||||
# Budget d'entrée = taille de morceau configurée (qui passe déjà côté provider).
|
||||
return AdaptCampaignUseCase( # type: ignore[arg-type]
|
||||
llm=llm, extractor=_PDF_EXTRACTOR, max_input_tokens=settings.import_chunk_tokens)
|
||||
|
||||
|
||||
# --- Endpoints ---
|
||||
|
||||
|
||||
@@ -325,6 +466,177 @@ async def generate_page(
|
||||
return GeneratePageResponseDTO(values=result.values)
|
||||
|
||||
|
||||
class RulesImportResponseDTO(BaseModel):
|
||||
"""Proposition de sections de règles extraites d'un PDF.
|
||||
|
||||
`sections` = {titre → contenu markdown}. C'est une PROPOSITION : le Core
|
||||
et l'UI laissent l'utilisateur réviser/éditer avant toute persistance.
|
||||
`ocr_page_count` permet d'indiquer si le PDF était un scan (OCR utilisé).
|
||||
"""
|
||||
|
||||
sections: dict[str, str]
|
||||
page_count: int
|
||||
ocr_page_count: int
|
||||
|
||||
|
||||
# Garde-fou taille : un livre de règles dépasse rarement quelques dizaines de Mo.
|
||||
# Au-delà, on refuse (probable erreur d'upload) plutôt que d'OOM le conteneur.
|
||||
_MAX_PDF_BYTES = 60 * 1024 * 1024 # 60 Mo
|
||||
|
||||
|
||||
@app.post("/import/rules", response_model=RulesImportResponseDTO)
|
||||
async def import_rules(
|
||||
use_case: Annotated[ImportRulesUseCase, Depends(get_import_rules_use_case)],
|
||||
file: UploadFile = File(...),
|
||||
) -> RulesImportResponseDTO:
|
||||
"""Import d'un PDF de règles → sections markdown structurées (proposition).
|
||||
|
||||
Extrait le texte (couche texte + repli OCR par page pour les scans), découpe,
|
||||
et demande au LLM de répartir les règles en sections thématiques. Ne persiste
|
||||
rien : renvoie la proposition au Core, qui la présente pour révision.
|
||||
"""
|
||||
content = await file.read()
|
||||
if not content:
|
||||
raise HTTPException(status_code=422, detail="Fichier PDF vide.")
|
||||
if len(content) > _MAX_PDF_BYTES:
|
||||
raise HTTPException(
|
||||
status_code=413,
|
||||
detail=f"PDF trop volumineux (> {_MAX_PDF_BYTES // (1024 * 1024)} Mo).",
|
||||
)
|
||||
|
||||
try:
|
||||
result = await use_case.execute(content)
|
||||
except PdfExtractionError as exc:
|
||||
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
||||
except LLMProviderError as exc:
|
||||
raise HTTPException(status_code=502, detail=str(exc)) from exc
|
||||
|
||||
return RulesImportResponseDTO(
|
||||
sections=result.sections,
|
||||
page_count=result.page_count,
|
||||
ocr_page_count=result.ocr_page_count,
|
||||
)
|
||||
|
||||
|
||||
@app.post("/import/rules/stream")
|
||||
async def import_rules_stream(
|
||||
use_case: Annotated[ImportRulesUseCase, Depends(get_import_rules_use_case)],
|
||||
file: UploadFile = File(...),
|
||||
) -> StreamingResponse:
|
||||
"""Import streamé : émet l'avancement (SSE) puis le résultat final.
|
||||
|
||||
Évènements SSE :
|
||||
- `event: extracting` → data: {} (extraction en cours)
|
||||
- `event: start` → data: {page_count, ocr_page_count, total}
|
||||
- `event: progress` → data: {current, total, new_sections:[...]}
|
||||
- `event: done` → data: {sections, page_count, ocr_page_count}
|
||||
- `event: error` → data: {message}
|
||||
"""
|
||||
content = await file.read()
|
||||
|
||||
def _sse(event: str, data: dict) -> str:
|
||||
return f"event: {event}\ndata: {json.dumps(data, ensure_ascii=False)}\n\n"
|
||||
|
||||
async def event_stream() -> AsyncIterator[str]:
|
||||
if not content:
|
||||
yield _sse("error", {"message": "Fichier PDF vide."})
|
||||
return
|
||||
if len(content) > _MAX_PDF_BYTES:
|
||||
yield _sse("error", {"message": f"PDF trop volumineux (> {_MAX_PDF_BYTES // (1024 * 1024)} Mo)."})
|
||||
return
|
||||
try:
|
||||
async for ev in use_case.stream(content):
|
||||
event_type = ev.pop("type")
|
||||
yield _sse(event_type, ev)
|
||||
except PdfExtractionError as exc:
|
||||
yield _sse("error", {"message": str(exc)})
|
||||
except LLMProviderError as exc:
|
||||
yield _sse("error", {"message": str(exc)})
|
||||
|
||||
return StreamingResponse(event_stream(), media_type="text/event-stream")
|
||||
|
||||
|
||||
@app.post("/import/campaign/stream")
|
||||
async def import_campaign_stream(
|
||||
use_case: Annotated[ImportCampaignUseCase, Depends(get_import_campaign_use_case)],
|
||||
file: UploadFile = File(...),
|
||||
) -> StreamingResponse:
|
||||
"""Import streamé d'un PDF de campagne → arbre arc→chapitre→scène (SSE).
|
||||
|
||||
Évènements : `extracting`, `start` {page_count, ocr_page_count, total},
|
||||
`progress` {current, total, arc_count, chapter_count, scene_count},
|
||||
`done` {arcs:[...], page_count, ocr_page_count}, `error` {message}.
|
||||
"""
|
||||
content = await file.read()
|
||||
|
||||
def _sse(event: str, data: dict) -> str:
|
||||
return f"event: {event}\ndata: {json.dumps(data, ensure_ascii=False)}\n\n"
|
||||
|
||||
async def event_stream() -> AsyncIterator[str]:
|
||||
if not content:
|
||||
yield _sse("error", {"message": "Fichier PDF vide."})
|
||||
return
|
||||
if len(content) > _MAX_PDF_BYTES:
|
||||
yield _sse("error", {"message": f"PDF trop volumineux (> {_MAX_PDF_BYTES // (1024 * 1024)} Mo)."})
|
||||
return
|
||||
try:
|
||||
async for ev in use_case.stream(content):
|
||||
event_type = ev.pop("type")
|
||||
yield _sse(event_type, ev)
|
||||
except PdfExtractionError as exc:
|
||||
yield _sse("error", {"message": str(exc)})
|
||||
except LLMProviderError as exc:
|
||||
yield _sse("error", {"message": str(exc)})
|
||||
|
||||
return StreamingResponse(event_stream(), media_type="text/event-stream")
|
||||
|
||||
|
||||
@app.post("/adapt/campaign/stream")
|
||||
async def adapt_campaign_stream(
|
||||
use_case: Annotated[AdaptCampaignUseCase, Depends(get_adapt_campaign_use_case)],
|
||||
file: UploadFile = File(...),
|
||||
brief: str = Form(""),
|
||||
messages: str = Form("[]"),
|
||||
) -> StreamingResponse:
|
||||
"""Adaptation CONVERSATIONNELLE d'un PDF à une campagne (SSE markdown).
|
||||
|
||||
`brief` = description de la campagne (Core). `messages` = JSON de l'échange
|
||||
([{role, content}, …]) ; vide au 1er tour. Évènements : `token`, `done`, `error`.
|
||||
"""
|
||||
content = await file.read()
|
||||
|
||||
try:
|
||||
raw_messages = json.loads(messages) if messages else []
|
||||
except json.JSONDecodeError:
|
||||
raw_messages = []
|
||||
convo = [
|
||||
ChatMessage(role=str(m.get("role", "user")), content=str(m.get("content", "")))
|
||||
for m in raw_messages
|
||||
if isinstance(m, dict) and str(m.get("content", "")).strip()
|
||||
]
|
||||
|
||||
def _sse(event: str, data: dict) -> str:
|
||||
return f"event: {event}\ndata: {json.dumps(data, ensure_ascii=False)}\n\n"
|
||||
|
||||
async def event_stream() -> AsyncIterator[str]:
|
||||
if not content:
|
||||
yield _sse("error", {"message": "Fichier PDF vide."})
|
||||
return
|
||||
if len(content) > _MAX_PDF_BYTES:
|
||||
yield _sse("error", {"message": f"PDF trop volumineux (> {_MAX_PDF_BYTES // (1024 * 1024)} Mo)."})
|
||||
return
|
||||
try:
|
||||
async for token in use_case.stream(content, brief, convo):
|
||||
yield _sse("token", {"token": token})
|
||||
yield _sse("done", {})
|
||||
except PdfExtractionError as exc:
|
||||
yield _sse("error", {"message": str(exc)})
|
||||
except LLMProviderError as exc:
|
||||
yield _sse("error", {"message": str(exc)})
|
||||
|
||||
return StreamingResponse(event_stream(), media_type="text/event-stream")
|
||||
|
||||
|
||||
@app.post("/chat/stream")
|
||||
async def chat_stream(
|
||||
body: ChatStreamRequestDTO,
|
||||
@@ -352,6 +664,8 @@ async def chat_stream(
|
||||
page_context = _to_page_context(body.page_context)
|
||||
campaign_context = _to_campaign_context(body.campaign_context)
|
||||
narrative_entity = _to_narrative_entity(body.narrative_entity)
|
||||
game_system_context = _to_game_system_context(body.game_system_context)
|
||||
session_context = _to_session_context(body.session_context)
|
||||
|
||||
# --- Comptage tokens pour la jauge de contexte frontend ---
|
||||
# On construit le system prompt une fois ici pour le compter — le use case
|
||||
@@ -363,6 +677,8 @@ async def chat_stream(
|
||||
page_context=page_context,
|
||||
campaign_context=campaign_context,
|
||||
narrative_entity=narrative_entity,
|
||||
game_system_context=game_system_context,
|
||||
session_context=session_context,
|
||||
)
|
||||
# Dernier message = "current" (souvent user), le reste = historique accumulé.
|
||||
current_msg = messages[-1] if messages else None
|
||||
@@ -386,6 +702,8 @@ async def chat_stream(
|
||||
page_context=page_context,
|
||||
campaign_context=campaign_context,
|
||||
narrative_entity=narrative_entity,
|
||||
game_system_context=game_system_context,
|
||||
session_context=session_context,
|
||||
):
|
||||
# json.dumps avec ensure_ascii=False pour préserver les accents
|
||||
yield f"data: {json.dumps({'token': token}, ensure_ascii=False)}\n\n"
|
||||
@@ -514,6 +832,23 @@ def _to_campaign_context(dto: CampaignContextDTO | None) -> CampaignStructuralCo
|
||||
)
|
||||
for br in sc.branches
|
||||
],
|
||||
rooms=[
|
||||
RoomSummary(
|
||||
name=room.name,
|
||||
floor=room.floor,
|
||||
description=room.description,
|
||||
enemies=room.enemies,
|
||||
branches=[
|
||||
RoomBranchHint(
|
||||
label=rb.label,
|
||||
target_room_name=rb.target_room_name,
|
||||
condition=rb.condition,
|
||||
)
|
||||
for rb in room.branches
|
||||
],
|
||||
)
|
||||
for room in sc.rooms
|
||||
],
|
||||
)
|
||||
for sc in ch.scenes
|
||||
],
|
||||
@@ -523,10 +858,20 @@ def _to_campaign_context(dto: CampaignContextDTO | None) -> CampaignStructuralCo
|
||||
)
|
||||
for arc in dto.arcs
|
||||
]
|
||||
characters = [
|
||||
CharacterSummary(name=c.name, snippet=c.snippet)
|
||||
for c in dto.characters
|
||||
]
|
||||
npcs = [
|
||||
NpcSummary(name=n.name, snippet=n.snippet)
|
||||
for n in dto.npcs
|
||||
]
|
||||
return CampaignStructuralContext(
|
||||
campaign_name=dto.campaign_name,
|
||||
campaign_description=dto.campaign_description,
|
||||
arcs=arcs,
|
||||
characters=characters,
|
||||
npcs=npcs,
|
||||
)
|
||||
|
||||
|
||||
@@ -549,6 +894,10 @@ class SettingsDTO(BaseModel):
|
||||
# Fenetre de contexte effective passee au modele (num_ctx Ollama) — sert
|
||||
# aussi de plafond a la jauge de contexte UI.
|
||||
llm_num_ctx: int
|
||||
# Taille cible d'un morceau (tokens) pour l'import de PDF (regles/campagne).
|
||||
import_chunk_tokens: int
|
||||
# Timeout HTTP des appels LLM (s). A monter si les imports lourds expirent.
|
||||
llm_timeout_seconds: int
|
||||
|
||||
|
||||
class SettingsUpdateDTO(BaseModel):
|
||||
@@ -561,6 +910,8 @@ class SettingsUpdateDTO(BaseModel):
|
||||
# Chaine vide => on efface la cle. None => pas de changement.
|
||||
onemin_api_key: str | None = None
|
||||
llm_num_ctx: int | None = None
|
||||
import_chunk_tokens: int | None = None
|
||||
llm_timeout_seconds: int | None = None
|
||||
|
||||
|
||||
def _to_settings_dto(s: Settings) -> SettingsDTO:
|
||||
@@ -571,6 +922,8 @@ def _to_settings_dto(s: Settings) -> SettingsDTO:
|
||||
onemin_model=s.onemin_model,
|
||||
onemin_api_key_set=bool(s.onemin_api_key),
|
||||
llm_num_ctx=s.llm_num_ctx,
|
||||
import_chunk_tokens=s.import_chunk_tokens,
|
||||
llm_timeout_seconds=s.llm_timeout_seconds,
|
||||
)
|
||||
|
||||
|
||||
@@ -658,6 +1011,76 @@ async def get_ollama_model_info(
|
||||
return OllamaModelInfoDTO(context_length=0)
|
||||
|
||||
|
||||
@app.post("/models/ollama/pull")
|
||||
async def pull_ollama_model(
|
||||
body: dict[str, str],
|
||||
settings: Annotated[Settings, Depends(get_settings)],
|
||||
) -> StreamingResponse:
|
||||
"""Telecharge un modele depuis Ollama et streame la progression.
|
||||
|
||||
Proxifie l'endpoint `/api/pull` d'Ollama qui renvoie du JSON ligne par
|
||||
ligne (NDJSON) avec le statut de chaque etape : manifest, layers,
|
||||
digest, success. On reemet ce flux tel quel au client (le front
|
||||
parsera les lignes et affichera une barre de progression).
|
||||
|
||||
Le timeout est intentionnellement tres long (60 min) car certains
|
||||
modeles font 30+ Go.
|
||||
"""
|
||||
name = (body.get("name") or "").strip()
|
||||
if not name:
|
||||
raise HTTPException(status_code=400, detail="name requis")
|
||||
url = f"{settings.ollama_base_url}/api/pull"
|
||||
|
||||
async def stream() -> AsyncIterator[bytes]:
|
||||
# On utilise un timeout long pour la lecture (60 min) mais court pour
|
||||
# la connexion (10s) — si Ollama n'est pas joignable, on echoue vite.
|
||||
timeout = httpx.Timeout(connect=10, read=3600, write=10, pool=10)
|
||||
try:
|
||||
async with httpx.AsyncClient(timeout=timeout) as client:
|
||||
async with client.stream("POST", url, json={"model": name, "stream": True}) as r:
|
||||
if r.status_code != 200:
|
||||
# Ollama renvoie un message JSON d'erreur. On le passe
|
||||
# tel quel au client en preservant le code HTTP.
|
||||
body_text = await r.aread()
|
||||
yield body_text
|
||||
return
|
||||
async for chunk in r.aiter_bytes():
|
||||
yield chunk
|
||||
except httpx.HTTPError as e:
|
||||
# Erreur reseau : on emet une ligne JSON d'erreur compatible
|
||||
# avec le format NDJSON d'Ollama.
|
||||
err = json.dumps({"error": f"Connexion a Ollama impossible : {e}"}) + "\n"
|
||||
yield err.encode("utf-8")
|
||||
|
||||
# application/x-ndjson : un objet JSON par ligne, pas de wrapping SSE.
|
||||
# C'est le format natif d'Ollama, le front le parsera ligne par ligne.
|
||||
return StreamingResponse(stream(), media_type="application/x-ndjson")
|
||||
|
||||
|
||||
@app.delete("/models/ollama/{name:path}")
|
||||
async def delete_ollama_model(
|
||||
name: str,
|
||||
settings: Annotated[Settings, Depends(get_settings)],
|
||||
) -> dict[str, str]:
|
||||
"""Supprime un modele du serveur Ollama.
|
||||
|
||||
Le `:path` dans le pattern autorise les `:` du nom (ex: `gemma4:e4b`)
|
||||
sans avoir besoin de URL-encoder cote client.
|
||||
"""
|
||||
if not name.strip():
|
||||
raise HTTPException(status_code=400, detail="name requis")
|
||||
url = f"{settings.ollama_base_url}/api/delete"
|
||||
try:
|
||||
async with httpx.AsyncClient(timeout=10) as client:
|
||||
response = await client.request("DELETE", url, json={"model": name})
|
||||
if response.status_code == 404:
|
||||
raise HTTPException(status_code=404, detail=f"Modele '{name}' introuvable")
|
||||
response.raise_for_status()
|
||||
except httpx.HTTPError as e:
|
||||
raise HTTPException(status_code=502, detail=f"Ollama injoignable : {e}")
|
||||
return {"status": "deleted", "name": name}
|
||||
|
||||
|
||||
@app.get("/models/onemin")
|
||||
def list_onemin_models() -> dict[str, list[dict[str, object]]]:
|
||||
"""Catalogue statique des modeles 1min.ai, groupes par fournisseur.
|
||||
@@ -742,3 +1165,46 @@ def _to_narrative_entity(dto: NarrativeEntityDTO | None) -> NarrativeEntityConte
|
||||
title=dto.title,
|
||||
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),
|
||||
)
|
||||
|
||||
|
||||
def _to_session_context(dto: SessionContextDTO | None) -> SessionContext | None:
|
||||
if dto is None:
|
||||
return None
|
||||
return SessionContext(
|
||||
session_name=dto.session_name,
|
||||
active=dto.active,
|
||||
started_at=dto.started_at,
|
||||
entries=[_to_journal_entry(e) for e in dto.entries],
|
||||
previous_events=[_to_journal_entry(e) for e in dto.previous_events],
|
||||
available_quests=[_to_quest_summary(q) for q in dto.available_quests],
|
||||
in_progress_quests=[_to_quest_summary(q) for q in dto.in_progress_quests],
|
||||
locked_quest_titles=list(dto.locked_quest_titles),
|
||||
active_flags=list(dto.active_flags),
|
||||
)
|
||||
|
||||
|
||||
def _to_quest_summary(dto: QuestSummaryDTO) -> QuestSummary:
|
||||
return QuestSummary(
|
||||
name=dto.name,
|
||||
arc_name=dto.arc_name,
|
||||
description=dto.description,
|
||||
)
|
||||
|
||||
|
||||
def _to_journal_entry(dto: JournalEntrySummaryDTO) -> JournalEntrySummary:
|
||||
return JournalEntrySummary(
|
||||
type=dto.type,
|
||||
content=dto.content,
|
||||
occurred_at=dto.occurred_at,
|
||||
source_session_name=dto.source_session_name,
|
||||
)
|
||||
|
||||
@@ -2,6 +2,8 @@ fastapi==0.115.*
|
||||
uvicorn[standard]==0.32.*
|
||||
httpx==0.27.*
|
||||
pydantic-settings==2.6.*
|
||||
# Requis par FastAPI pour les uploads multipart (UploadFile) — import de PDF.
|
||||
python-multipart==0.0.*
|
||||
|
||||
pydantic
|
||||
|
||||
@@ -10,3 +12,12 @@ pydantic
|
||||
# la plupart des modeles Llama/Gemma/Mistral (~5-10% d'ecart) — suffisant
|
||||
# pour une jauge visuelle.
|
||||
tiktoken==0.8.*
|
||||
|
||||
# Import de PDF de regles (-> GameSystem). Extraction de la couche texte
|
||||
# (born-digital) avec repli OCR par page pour les scans.
|
||||
# - pymupdf : extraction texte + rasterisation des pages (pas besoin de poppler)
|
||||
# - pytesseract + Pillow : OCR Tesseract sur les pages sans couche texte
|
||||
# (le binaire tesseract-ocr est installe dans le Dockerfile, langues fra+eng)
|
||||
pymupdf==1.24.*
|
||||
pytesseract==0.3.*
|
||||
Pillow==11.*
|
||||
|
||||
107
brain/scripts/test_import_rules.py
Normal file
107
brain/scripts/test_import_rules.py
Normal file
@@ -0,0 +1,107 @@
|
||||
"""CLI de test pour l'import de règles PDF — boucle de feedback rapide.
|
||||
|
||||
But : tester l'extraction + la structuration en sections sur un VRAI PDF
|
||||
(ex: livre Nimble), SANS passer par HTTP, le Core ou l'UI.
|
||||
|
||||
Usage (depuis le dossier brain/, venv activé) :
|
||||
python scripts/test_import_rules.py "chemin/vers/regles.pdf"
|
||||
|
||||
Le provider LLM utilisé est celui configuré (.env + overrides de l'écran
|
||||
Paramètres) — donc 1min.ai si tu l'as sélectionné. Le script :
|
||||
1. extrait le texte (et dit, page par page, si l'OCR s'est déclenché),
|
||||
2. découpe + structure via le LLM,
|
||||
3. affiche un résumé des sections,
|
||||
4. écrit le markdown complet dans "<pdf>.rules.md" à côté du PDF.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
# Permet `import app...` quel que soit le cwd : on ajoute la racine brain/.
|
||||
_BRAIN_ROOT = Path(__file__).resolve().parent.parent
|
||||
sys.path.insert(0, str(_BRAIN_ROOT))
|
||||
|
||||
from app.application.import_rules import ImportRulesUseCase # noqa: E402
|
||||
from app.core.config import get_settings # noqa: E402
|
||||
from app.domain.ports import LLMProvider, LLMProviderError, PdfExtractionError # noqa: E402
|
||||
from app.infrastructure.ollama_adapter import OllamaLLMProvider # noqa: E402
|
||||
from app.infrastructure.onemin_adapter import OneMinAiLLMProvider # noqa: E402
|
||||
from app.infrastructure.pdf_extractor import PyMuPdfTextExtractor # noqa: E402
|
||||
|
||||
|
||||
def _build_provider() -> LLMProvider:
|
||||
"""Réplique la factory de main.py (choix provider selon les settings)."""
|
||||
settings = get_settings()
|
||||
print(f"→ Provider LLM : {settings.llm_provider} "
|
||||
f"(modèle : {settings.onemin_model if settings.llm_provider == 'onemin' else settings.llm_model})")
|
||||
if settings.llm_provider == "onemin":
|
||||
return OneMinAiLLMProvider(settings)
|
||||
return OllamaLLMProvider(settings)
|
||||
|
||||
|
||||
async def _run(pdf_path: Path) -> None:
|
||||
pdf_bytes = pdf_path.read_bytes()
|
||||
print(f"→ PDF chargé : {pdf_path.name} ({len(pdf_bytes) // 1024} Ko)\n")
|
||||
|
||||
extractor = PyMuPdfTextExtractor()
|
||||
|
||||
# 1. Extraction seule d'abord, pour le diagnostic page/OCR avant tout LLM.
|
||||
try:
|
||||
doc = extractor.extract(pdf_bytes)
|
||||
except PdfExtractionError as exc:
|
||||
print(f"✗ Extraction impossible : {exc}")
|
||||
return
|
||||
|
||||
print(f"=== Extraction : {doc.page_count} page(s), "
|
||||
f"{doc.ocr_page_count} via OCR, "
|
||||
f"{len(doc.full_text)} caractères ===")
|
||||
if doc.ocr_page_count == 0:
|
||||
print(" → PDF born-digital détecté (couche texte présente, OCR non nécessaire).")
|
||||
else:
|
||||
print(f" → {doc.ocr_page_count} page(s) sans couche texte : OCR déclenché.")
|
||||
if not doc.full_text.strip():
|
||||
print("\n✗ Aucun texte extrait. Si le PDF est un scan, vérifie que Tesseract "
|
||||
"est installé (sinon l'OCR est désactivé).")
|
||||
return
|
||||
|
||||
# 2. Structuration via le LLM (réutilise l'extraction déjà faite indirectement :
|
||||
# le use case ré-extrait, coût négligeable vs l'appel LLM).
|
||||
print("\n=== Structuration via LLM (peut prendre un moment selon le nombre de morceaux)… ===")
|
||||
use_case = ImportRulesUseCase(llm=_build_provider(), extractor=extractor)
|
||||
try:
|
||||
result = await use_case.execute(pdf_bytes)
|
||||
except LLMProviderError as exc:
|
||||
print(f"✗ Échec LLM : {exc}")
|
||||
return
|
||||
|
||||
if not result.sections:
|
||||
print("\n✗ Aucune section proposée (le modèle n'a rien renvoyé d'exploitable).")
|
||||
return
|
||||
|
||||
print(f"\n=== {len(result.sections)} section(s) proposée(s) ===")
|
||||
for title, content in result.sections.items():
|
||||
preview = content.strip().replace("\n", " ")[:90]
|
||||
print(f" • {title} ({len(content)} car.) — {preview}…")
|
||||
|
||||
out_path = pdf_path.with_suffix(".rules.md")
|
||||
out_path.write_text(result.to_markdown(), encoding="utf-8")
|
||||
print(f"\n✓ Markdown complet écrit dans : {out_path}")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
logging.basicConfig(level=logging.INFO, format="%(levelname)s %(name)s — %(message)s")
|
||||
if len(sys.argv) != 2:
|
||||
print("Usage : python scripts/test_import_rules.py \"chemin/vers/regles.pdf\"")
|
||||
sys.exit(1)
|
||||
pdf_path = Path(sys.argv[1]).expanduser()
|
||||
if not pdf_path.is_file():
|
||||
print(f"✗ Fichier introuvable : {pdf_path}")
|
||||
sys.exit(1)
|
||||
asyncio.run(_run(pdf_path))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
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
|
||||
36
core/pom.xml
36
core/pom.xml
@@ -8,13 +8,13 @@
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>3.2.0</version>
|
||||
<version>3.2.12</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
<groupId>com.loremind</groupId>
|
||||
<artifactId>loremind-core</artifactId>
|
||||
<version>0.4.0</version>
|
||||
<version>0.10.1-beta</version>
|
||||
<name>LoreMind Core</name>
|
||||
<description>Backend Core - Architecture Hexagonale</description>
|
||||
|
||||
@@ -83,6 +83,28 @@
|
||||
<artifactId>minio</artifactId>
|
||||
<version>8.5.11</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Nimbus JOSE+JWT — verification des JWT Ed25519 (EdDSA) emis par le relais
|
||||
Patreon. Supporte nativement les cles Ed25519 via BouncyCastle. -->
|
||||
<dependency>
|
||||
<groupId>com.nimbusds</groupId>
|
||||
<artifactId>nimbus-jose-jwt</artifactId>
|
||||
<version>9.40</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.bouncycastle</groupId>
|
||||
<artifactId>bcprov-jdk18on</artifactId>
|
||||
<version>1.78.1</version>
|
||||
</dependency>
|
||||
<!-- Google Tink : runtime requis par com.nimbusds.jose.crypto.Ed25519Verifier
|
||||
(depuis Nimbus 9.x, la verification EdDSA delegue a Tink.subtle.Ed25519Verify).
|
||||
Tink n'est PAS une dependance transitive de nimbus-jose-jwt → il faut
|
||||
l'ajouter explicitement, sinon NoClassDefFoundError au premier verify(). -->
|
||||
<dependency>
|
||||
<groupId>com.google.crypto.tink</groupId>
|
||||
<artifactId>tink</artifactId>
|
||||
<version>1.14.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@@ -98,6 +120,16 @@
|
||||
</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
<executions>
|
||||
<!-- Genere META-INF/build-info.properties (project.version)
|
||||
consomme par Spring BuildProperties pour exposer la
|
||||
version courante a l'application (UpdateCheckService). -->
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>build-info</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<!-- JaCoCo : rapport de couverture des tests unitaires.
|
||||
|
||||
@@ -2,12 +2,14 @@ package com.loremind;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
|
||||
/**
|
||||
* Classe principale de l'application LoreMind.
|
||||
* Point d'entrée Spring Boot qui démarre l'application.
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@EnableScheduling
|
||||
public class LoreMindApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
package com.loremind.application.campaigncontext;
|
||||
|
||||
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.ChapterRepository;
|
||||
import com.loremind.domain.campaigncontext.ports.SceneRepository;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
@@ -17,21 +21,44 @@ import java.util.Optional;
|
||||
public class ArcService {
|
||||
|
||||
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.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) {
|
||||
return createArc(name, description, campaignId, order, null);
|
||||
}
|
||||
|
||||
public Arc createArc(String name, String description, String campaignId, int order, String icon) {
|
||||
Arc arc = Arc.builder()
|
||||
.name(name)
|
||||
.description(description)
|
||||
.campaignId(campaignId)
|
||||
.order(order)
|
||||
.icon(icon)
|
||||
.build();
|
||||
return arcRepository.save(arc);
|
||||
}
|
||||
|
||||
/**
|
||||
* Création à partir d'un Arc complet (utilisé par le controller pour faire passer
|
||||
* les nouveaux champs comme type sans démultiplier les paramètres).
|
||||
*/
|
||||
public Arc createArc(Arc input) {
|
||||
input.setId(null);
|
||||
return arcRepository.save(input);
|
||||
}
|
||||
|
||||
public Optional<Arc> getArcById(String id) {
|
||||
return arcRepository.findById(id);
|
||||
}
|
||||
@@ -59,7 +86,31 @@ public class ArcService {
|
||||
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) {
|
||||
for (Chapter chapter : chapterRepository.findByArcId(id)) {
|
||||
for (var scene : sceneRepository.findByChapterId(chapter.getId())) {
|
||||
sceneRepository.deleteById(scene.getId());
|
||||
}
|
||||
chapterRepository.deleteById(chapter.getId());
|
||||
}
|
||||
arcRepository.deleteById(id);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
package com.loremind.application.campaigncontext;
|
||||
|
||||
import com.loremind.application.generationcontext.CampaignStructuralContextBuilder;
|
||||
import com.loremind.application.generationcontext.LoreStructuralContextBuilder;
|
||||
import com.loremind.domain.campaigncontext.Campaign;
|
||||
import com.loremind.domain.campaigncontext.ports.CampaignPdfAdvisor;
|
||||
import com.loremind.domain.campaigncontext.ports.CampaignRepository;
|
||||
import com.loremind.domain.generationcontext.CampaignStructuralContext;
|
||||
import com.loremind.domain.generationcontext.CampaignStructuralContext.ArcSummary;
|
||||
import com.loremind.domain.generationcontext.CampaignStructuralContext.ChapterSummary;
|
||||
import com.loremind.domain.generationcontext.CampaignStructuralContext.NpcSummary;
|
||||
import com.loremind.domain.generationcontext.CampaignStructuralContext.SceneSummary;
|
||||
import com.loremind.domain.generationcontext.LoreStructuralContext;
|
||||
import com.loremind.domain.generationcontext.LoreStructuralContext.PageSummary;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* Service applicatif : conseils d'adaptation d'un PDF à une campagne existante.
|
||||
*
|
||||
* <p>Assemble un « brief » de la campagne (structure + PNJ + univers/lore) — la même
|
||||
* matière que le chat de campagne — et délègue la génération streamée au Brain via
|
||||
* {@link CampaignPdfAdvisor}. Ne persiste rien : la sortie est du conseil libre.</p>
|
||||
*/
|
||||
@Service
|
||||
public class CampaignAdaptService {
|
||||
|
||||
private final CampaignRepository campaignRepository;
|
||||
private final CampaignStructuralContextBuilder campaignContextBuilder;
|
||||
private final LoreStructuralContextBuilder loreContextBuilder;
|
||||
private final CampaignPdfAdvisor advisor;
|
||||
|
||||
public CampaignAdaptService(
|
||||
CampaignRepository campaignRepository,
|
||||
CampaignStructuralContextBuilder campaignContextBuilder,
|
||||
LoreStructuralContextBuilder loreContextBuilder,
|
||||
CampaignPdfAdvisor advisor) {
|
||||
this.campaignRepository = campaignRepository;
|
||||
this.campaignContextBuilder = campaignContextBuilder;
|
||||
this.loreContextBuilder = loreContextBuilder;
|
||||
this.advisor = advisor;
|
||||
}
|
||||
|
||||
public void adviseStreaming(
|
||||
String campaignId,
|
||||
byte[] pdfBytes,
|
||||
String filename,
|
||||
String messagesJson,
|
||||
Consumer<String> onToken,
|
||||
Runnable onComplete,
|
||||
Consumer<Throwable> onError) {
|
||||
Campaign campaign = campaignRepository.findById(campaignId)
|
||||
.orElseThrow(() -> new IllegalArgumentException("Campagne introuvable : " + campaignId));
|
||||
advisor.adviseStreaming(
|
||||
pdfBytes, filename, buildBrief(campaign), messagesJson, onToken, onComplete, onError);
|
||||
}
|
||||
|
||||
/** Construit un résumé markdown de la campagne (structure + PNJ + lore). */
|
||||
private String buildBrief(Campaign campaign) {
|
||||
CampaignStructuralContext cc = campaignContextBuilder.build(campaign.getId());
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
sb.append("# Campagne : ").append(cc.campaignName()).append("\n");
|
||||
if (notBlank(cc.campaignDescription())) sb.append(cc.campaignDescription()).append("\n");
|
||||
|
||||
sb.append("\n## Structure (arcs → chapitres → scènes)\n");
|
||||
if (cc.arcs().isEmpty()) {
|
||||
sb.append("_(aucun arc pour le moment)_\n");
|
||||
}
|
||||
for (ArcSummary arc : cc.arcs()) {
|
||||
sb.append("### Arc : ").append(arc.name());
|
||||
if (notBlank(arc.description())) sb.append(" — ").append(arc.description());
|
||||
sb.append("\n");
|
||||
for (ChapterSummary ch : arc.chapters()) {
|
||||
sb.append("- Chapitre : ").append(ch.name());
|
||||
if (notBlank(ch.description())) sb.append(" — ").append(ch.description());
|
||||
sb.append("\n");
|
||||
for (SceneSummary sc : ch.scenes()) {
|
||||
sb.append(" - Scène : ").append(sc.name());
|
||||
if (notBlank(sc.description())) sb.append(" — ").append(sc.description());
|
||||
sb.append("\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!cc.npcs().isEmpty()) {
|
||||
sb.append("\n## PNJ existants\n");
|
||||
for (NpcSummary n : cc.npcs()) {
|
||||
sb.append("- ").append(n.name());
|
||||
if (notBlank(n.snippet())) sb.append(" : ").append(n.snippet());
|
||||
sb.append("\n");
|
||||
}
|
||||
}
|
||||
|
||||
if (campaign.isLinkedToLore()) {
|
||||
loreContextBuilder.buildOptional(campaign.getLoreId()).ifPresent(lore -> appendLore(sb, lore));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private void appendLore(StringBuilder sb, LoreStructuralContext lore) {
|
||||
sb.append("\n## Univers (Lore) : ").append(lore.loreName()).append("\n");
|
||||
if (notBlank(lore.loreDescription())) sb.append(lore.loreDescription()).append("\n");
|
||||
for (Map.Entry<String, List<PageSummary>> entry : lore.folders().entrySet()) {
|
||||
sb.append("### ").append(entry.getKey()).append("\n");
|
||||
for (PageSummary page : entry.getValue()) {
|
||||
sb.append("- ").append(page.title()).append("\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean notBlank(String s) {
|
||||
return s != null && !s.isBlank();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,181 @@
|
||||
package com.loremind.application.campaigncontext;
|
||||
|
||||
import com.loremind.domain.campaigncontext.Arc;
|
||||
import com.loremind.domain.campaigncontext.ArcType;
|
||||
import com.loremind.domain.campaigncontext.CampaignImportProgress;
|
||||
import com.loremind.domain.campaigncontext.CampaignImportProposal;
|
||||
import com.loremind.domain.campaigncontext.CampaignImportProposal.ArcProposal;
|
||||
import com.loremind.domain.campaigncontext.CampaignImportProposal.ChapterProposal;
|
||||
import com.loremind.domain.campaigncontext.CampaignImportProposal.RoomProposal;
|
||||
import com.loremind.domain.campaigncontext.CampaignImportProposal.SceneProposal;
|
||||
import com.loremind.domain.campaigncontext.Chapter;
|
||||
import com.loremind.domain.campaigncontext.Room;
|
||||
import com.loremind.domain.campaigncontext.Scene;
|
||||
import com.loremind.domain.campaigncontext.ports.CampaignPdfImporter;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* Service applicatif pour l'import d'un PDF de campagne.
|
||||
*
|
||||
* <p>Deux temps, conformes au principe « revue avant écriture » :
|
||||
* 1. {@link #importStructureStreaming} génère une PROPOSITION d'arbre (rien
|
||||
* n'est persisté), streamée pour l'avancement.
|
||||
* 2. {@link #applyStructure} crée réellement les arcs/chapitres/scènes une fois
|
||||
* l'arbre révisé/édité par l'utilisateur.</p>
|
||||
*/
|
||||
@Service
|
||||
public class CampaignImportService {
|
||||
|
||||
private final CampaignPdfImporter campaignPdfImporter;
|
||||
private final CampaignService campaignService;
|
||||
private final ArcService arcService;
|
||||
private final ChapterService chapterService;
|
||||
private final SceneService sceneService;
|
||||
|
||||
public CampaignImportService(
|
||||
CampaignPdfImporter campaignPdfImporter,
|
||||
CampaignService campaignService,
|
||||
ArcService arcService,
|
||||
ChapterService chapterService,
|
||||
SceneService sceneService) {
|
||||
this.campaignPdfImporter = campaignPdfImporter;
|
||||
this.campaignService = campaignService;
|
||||
this.arcService = arcService;
|
||||
this.chapterService = chapterService;
|
||||
this.sceneService = sceneService;
|
||||
}
|
||||
|
||||
/** Résumé de ce qui a été créé par {@link #applyStructure}. */
|
||||
public record ApplyResult(int arcsCreated, int chaptersCreated, int scenesCreated) {}
|
||||
|
||||
/** Génère la proposition d'arbre (streamée). Ne persiste rien. */
|
||||
public void importStructureStreaming(
|
||||
byte[] pdfBytes,
|
||||
String filename,
|
||||
Consumer<CampaignImportProgress> onProgress,
|
||||
Consumer<CampaignImportProposal> onDone,
|
||||
Consumer<Throwable> onError) {
|
||||
campaignPdfImporter.importCampaignStreaming(pdfBytes, filename, onProgress, onDone, onError);
|
||||
}
|
||||
|
||||
/**
|
||||
* Crée les arcs/chapitres/scènes de l'arbre révisé dans la campagne. Les
|
||||
* arcs sont ajoutés APRÈS les arcs existants (ordre continué). Tout est créé
|
||||
* dans une seule transaction (rollback si une étape échoue).
|
||||
*/
|
||||
@Transactional
|
||||
public ApplyResult applyStructure(String campaignId, CampaignImportProposal proposal) {
|
||||
if (!campaignService.campaignExists(campaignId)) {
|
||||
throw new IllegalArgumentException("Campagne introuvable : " + campaignId);
|
||||
}
|
||||
|
||||
int arcsCreated = 0, chaptersCreated = 0, scenesCreated = 0;
|
||||
|
||||
// Les nouveaux nœuds sont ordonnés APRÈS les frères existants (déjà comptés
|
||||
// via leur existingId dans l'arbre fusionné venu de la revue).
|
||||
int arcOrder = countExisting(proposal.arcs(), ArcProposal::existingId);
|
||||
for (ArcProposal arcP : proposal.arcs()) {
|
||||
if (isBlank(arcP.name())) continue;
|
||||
String arcId;
|
||||
if (!isBlank(arcP.existingId())) {
|
||||
arcId = arcP.existingId(); // arc déjà présent → on s'y rattache
|
||||
} else {
|
||||
arcOrder++;
|
||||
Arc arc = arcService.createArc(Arc.builder()
|
||||
.name(arcP.name().trim())
|
||||
.description(nullIfBlank(arcP.description()))
|
||||
.campaignId(campaignId)
|
||||
.order(arcOrder)
|
||||
.type(parseArcType(arcP.type()))
|
||||
.build());
|
||||
arcId = arc.getId();
|
||||
arcsCreated++;
|
||||
}
|
||||
|
||||
int chapterOrder = countExisting(arcP.chapters(), ChapterProposal::existingId);
|
||||
for (ChapterProposal chapP : safe(arcP.chapters())) {
|
||||
if (isBlank(chapP.name())) continue;
|
||||
String chapId;
|
||||
if (!isBlank(chapP.existingId())) {
|
||||
chapId = chapP.existingId();
|
||||
} else {
|
||||
chapterOrder++;
|
||||
Chapter chapter = chapterService.createChapter(
|
||||
chapP.name().trim(), nullIfBlank(chapP.description()), arcId, chapterOrder);
|
||||
chapId = chapter.getId();
|
||||
chaptersCreated++;
|
||||
}
|
||||
|
||||
int sceneOrder = countExisting(chapP.scenes(), SceneProposal::existingId);
|
||||
for (SceneProposal sceneP : safe(chapP.scenes())) {
|
||||
if (isBlank(sceneP.name())) continue;
|
||||
if (!isBlank(sceneP.existingId())) continue; // scène déjà présente
|
||||
sceneOrder++;
|
||||
sceneService.createScene(Scene.builder()
|
||||
.name(sceneP.name().trim())
|
||||
.description(nullIfBlank(sceneP.description()))
|
||||
.playerNarration(nullIfBlank(sceneP.playerNarration()))
|
||||
.gmSecretNotes(nullIfBlank(sceneP.gmNotes()))
|
||||
.chapterId(chapId)
|
||||
.order(sceneOrder)
|
||||
.rooms(toRooms(sceneP.rooms()))
|
||||
.build());
|
||||
scenesCreated++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return new ApplyResult(arcsCreated, chaptersCreated, scenesCreated);
|
||||
}
|
||||
|
||||
/** Compte les nœuds déjà présents (existingId non vide) d'une liste. */
|
||||
private static <T> int countExisting(List<T> list, java.util.function.Function<T, String> idOf) {
|
||||
if (list == null) return 0;
|
||||
int n = 0;
|
||||
for (T t : list) {
|
||||
if (!isBlank(idOf.apply(t))) n++;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
/** "HUB" (insensible à la casse) → {@link ArcType#HUB} ; tout le reste → LINEAR. */
|
||||
private static ArcType parseArcType(String type) {
|
||||
return "HUB".equalsIgnoreCase(type == null ? "" : type.trim()) ? ArcType.HUB : ArcType.LINEAR;
|
||||
}
|
||||
|
||||
/** Convertit les pièces proposées en {@link Room} (ID généré, ordre = index). */
|
||||
private static List<Room> toRooms(List<RoomProposal> proposals) {
|
||||
List<Room> rooms = new ArrayList<>();
|
||||
if (proposals == null) return rooms;
|
||||
int order = 0;
|
||||
for (RoomProposal r : proposals) {
|
||||
if (isBlank(r.name())) continue;
|
||||
rooms.add(Room.builder()
|
||||
.id(UUID.randomUUID().toString())
|
||||
.name(r.name().trim())
|
||||
.description(nullIfBlank(r.description()))
|
||||
.enemies(nullIfBlank(r.enemies()))
|
||||
.loot(nullIfBlank(r.loot()))
|
||||
.order(order++)
|
||||
.build());
|
||||
}
|
||||
return rooms;
|
||||
}
|
||||
|
||||
private static <T> java.util.List<T> safe(java.util.List<T> list) {
|
||||
return list == null ? java.util.List.of() : list;
|
||||
}
|
||||
|
||||
private static boolean isBlank(String s) {
|
||||
return s == null || s.isBlank();
|
||||
}
|
||||
|
||||
private static String nullIfBlank(String s) {
|
||||
return isBlank(s) ? null : s.trim();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.loremind.application.campaigncontext;
|
||||
|
||||
import com.loremind.domain.campaigncontext.Arc;
|
||||
import com.loremind.domain.campaigncontext.Chapter;
|
||||
import com.loremind.domain.campaigncontext.Prerequisite;
|
||||
import com.loremind.domain.campaigncontext.ports.ArcRepository;
|
||||
import com.loremind.domain.campaigncontext.ports.ChapterRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.TreeSet;
|
||||
|
||||
/**
|
||||
* Service applicatif : énumère les noms de faits ({@link Prerequisite.FlagSet})
|
||||
* référencés par les chapitres d'une Campagne.
|
||||
*
|
||||
* <p>Modèle "déclaration implicite" : il n'existe pas de table de faits déclarés
|
||||
* globalement. Un fait existe dès qu'au moins une quête le référence dans ses
|
||||
* prérequis. Ce service expose la liste dédupliquée pour les UIs (toggle dans
|
||||
* la Partie, autocomplete dans l'éditeur de prérequis).</p>
|
||||
*/
|
||||
@Service
|
||||
public class CampaignReferencedFlagsService {
|
||||
|
||||
private final ArcRepository arcRepository;
|
||||
private final ChapterRepository chapterRepository;
|
||||
|
||||
public CampaignReferencedFlagsService(ArcRepository arcRepository,
|
||||
ChapterRepository chapterRepository) {
|
||||
this.arcRepository = arcRepository;
|
||||
this.chapterRepository = chapterRepository;
|
||||
}
|
||||
|
||||
/** Retourne la liste triée alphabétiquement des noms de faits référencés. */
|
||||
public List<String> listForCampaign(String campaignId) {
|
||||
TreeSet<String> unique = new TreeSet<>();
|
||||
for (Arc arc : arcRepository.findByCampaignId(campaignId)) {
|
||||
for (Chapter chapter : chapterRepository.findByArcId(arc.getId())) {
|
||||
if (chapter.getPrerequisites() == null) continue;
|
||||
for (Prerequisite p : chapter.getPrerequisites()) {
|
||||
if (p instanceof Prerequisite.FlagSet f && f.flagName() != null && !f.flagName().isBlank()) {
|
||||
unique.add(f.flagName());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return List.copyOf(unique);
|
||||
}
|
||||
}
|
||||
@@ -1,43 +1,71 @@
|
||||
package com.loremind.application.campaigncontext;
|
||||
|
||||
import com.loremind.application.playcontext.PlaythroughService;
|
||||
import com.loremind.domain.campaigncontext.Arc;
|
||||
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.ChapterRepository;
|
||||
import com.loremind.domain.campaigncontext.ports.SceneRepository;
|
||||
import com.loremind.domain.playcontext.Playthrough;
|
||||
import com.loremind.domain.playcontext.ports.PlaythroughRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Service d'application pour le contexte Campaign.
|
||||
* Orchestre la logique métier en utilisant le Port CampaignRepository.
|
||||
* Fait partie de la couche Application de l'Architecture Hexagonale.
|
||||
* Service d'application pour le contexte Campaign (scénario).
|
||||
*
|
||||
* <p>Depuis Playthrough : les PJ et les sessions ne sont plus rattachés directement
|
||||
* à la Campagne ; ils dépendent d'un Playthrough (Partie). La cascade de suppression
|
||||
* d'une campagne englobe donc ses Playthroughs (qui à leur tour cascadent).</p>
|
||||
*/
|
||||
@Service
|
||||
public class CampaignService {
|
||||
|
||||
private final CampaignRepository campaignRepository;
|
||||
private final ArcRepository arcRepository;
|
||||
private final ChapterRepository chapterRepository;
|
||||
private final SceneRepository sceneRepository;
|
||||
private final PlaythroughRepository playthroughRepository;
|
||||
private final PlaythroughService playthroughService;
|
||||
|
||||
public CampaignService(CampaignRepository campaignRepository) {
|
||||
public CampaignService(
|
||||
CampaignRepository campaignRepository,
|
||||
ArcRepository arcRepository,
|
||||
ChapterRepository chapterRepository,
|
||||
SceneRepository sceneRepository,
|
||||
PlaythroughRepository playthroughRepository,
|
||||
PlaythroughService playthroughService) {
|
||||
this.campaignRepository = campaignRepository;
|
||||
this.arcRepository = arcRepository;
|
||||
this.chapterRepository = chapterRepository;
|
||||
this.sceneRepository = sceneRepository;
|
||||
this.playthroughRepository = playthroughRepository;
|
||||
this.playthroughService = playthroughService;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parameter Object pour la création / mise à jour d'une Campaign.
|
||||
* Évite une signature à rallonge et rend les évolutions futures (theme,
|
||||
* coverImageUrl, etc.) sans casser les appelants.
|
||||
*
|
||||
* <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) {}
|
||||
|
||||
public record DeletionImpact(int arcs, int chapters, int scenes, int playthroughs) {}
|
||||
|
||||
public Campaign createCampaign(CampaignData data) {
|
||||
Campaign campaign = Campaign.builder()
|
||||
.name(data.name())
|
||||
.description(data.description())
|
||||
.loreId(normalizeLoreId(data.loreId()))
|
||||
.loreId(normalizeId(data.loreId()))
|
||||
.gameSystemId(normalizeId(data.gameSystemId()))
|
||||
.arcsCount(0)
|
||||
.build();
|
||||
return campaignRepository.save(campaign);
|
||||
Campaign saved = campaignRepository.save(campaign);
|
||||
|
||||
// Une campagne sans Partie n'a pas de sens jouable : on crée d'office
|
||||
// une "Partie principale" pour que l'utilisateur puisse jouer immédiatement.
|
||||
playthroughService.create(saved.getId(), "Partie principale", null);
|
||||
return saved;
|
||||
}
|
||||
|
||||
public Optional<Campaign> getCampaignById(String id) {
|
||||
@@ -57,19 +85,48 @@ public class CampaignService {
|
||||
Campaign campaign = existingCampaign.get();
|
||||
campaign.setName(data.name());
|
||||
campaign.setDescription(data.description());
|
||||
campaign.setLoreId(normalizeLoreId(data.loreId()));
|
||||
campaign.setLoreId(normalizeId(data.loreId()));
|
||||
campaign.setGameSystemId(normalizeId(data.gameSystemId()));
|
||||
return campaignRepository.save(campaign);
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalise un loreId entrant : une chaîne vide/blanche est traitée comme "pas de lien".
|
||||
* Utile car les payloads JSON peuvent envoyer "" au lieu de null.
|
||||
*/
|
||||
private String normalizeLoreId(String loreId) {
|
||||
return (loreId == null || loreId.isBlank()) ? null : loreId;
|
||||
private String normalizeId(String id) {
|
||||
return (id == null || id.isBlank()) ? null : id;
|
||||
}
|
||||
|
||||
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 playthroughTotal = playthroughRepository.findByCampaignId(id).size();
|
||||
return new DeletionImpact(arcs.size(), chapterTotal, sceneTotal, playthroughTotal);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteCampaign(String id) {
|
||||
// 1. Cascade des Playthroughs (qui cascadent eux-mêmes sur PJ/sessions/valeurs flags/progressions).
|
||||
for (Playthrough p : playthroughRepository.findByCampaignId(id)) {
|
||||
playthroughService.delete(p.getId());
|
||||
}
|
||||
// 2. Cascade du scénario : arcs → chapitres → scènes
|
||||
// (Pas de déclarations globales de faits : ils existent implicitement via les
|
||||
// prérequis FLAG_SET des chapitres, qui partent avec les chapitres.)
|
||||
for (Arc arc : arcRepository.findByCampaignId(id)) {
|
||||
for (Chapter chapter : chapterRepository.findByArcId(arc.getId())) {
|
||||
for (var scene : sceneRepository.findByChapterId(chapter.getId())) {
|
||||
sceneRepository.deleteById(scene.getId());
|
||||
}
|
||||
chapterRepository.deleteById(chapter.getId());
|
||||
}
|
||||
arcRepository.deleteById(arc.getId());
|
||||
}
|
||||
campaignRepository.deleteById(id);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,8 +2,10 @@ package com.loremind.application.campaigncontext;
|
||||
|
||||
import com.loremind.domain.campaigncontext.Chapter;
|
||||
import com.loremind.domain.campaigncontext.ports.ChapterRepository;
|
||||
import com.loremind.domain.campaigncontext.ports.SceneRepository;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
@@ -17,21 +19,41 @@ import java.util.Optional;
|
||||
public class ChapterService {
|
||||
|
||||
private final ChapterRepository chapterRepository;
|
||||
private final SceneRepository sceneRepository;
|
||||
|
||||
public ChapterService(ChapterRepository chapterRepository) {
|
||||
public ChapterService(ChapterRepository chapterRepository, SceneRepository sceneRepository) {
|
||||
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) {
|
||||
return createChapter(name, description, arcId, order, null);
|
||||
}
|
||||
|
||||
public Chapter createChapter(String name, String description, String arcId, int order, String icon) {
|
||||
Chapter chapter = Chapter.builder()
|
||||
.name(name)
|
||||
.description(description)
|
||||
.arcId(arcId)
|
||||
.order(order)
|
||||
.icon(icon)
|
||||
.build();
|
||||
return chapterRepository.save(chapter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Création à partir d'un Chapter complet (utilisé par le controller pour faire passer
|
||||
* les nouveaux champs comme progressionStatus / prerequisites sans démultiplier les
|
||||
* paramètres). L'id est forcé à null pour laisser la DB le générer.
|
||||
*/
|
||||
public Chapter createChapter(Chapter input) {
|
||||
input.setId(null);
|
||||
return chapterRepository.save(input);
|
||||
}
|
||||
|
||||
public Optional<Chapter> getChapterById(String id) {
|
||||
return chapterRepository.findById(id);
|
||||
}
|
||||
@@ -58,7 +80,17 @@ public class ChapterService {
|
||||
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) {
|
||||
for (var scene : sceneRepository.findByChapterId(id)) {
|
||||
sceneRepository.deleteById(scene.getId());
|
||||
}
|
||||
chapterRepository.deleteById(id);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
package com.loremind.application.campaigncontext;
|
||||
|
||||
import com.loremind.domain.campaigncontext.Chapter;
|
||||
import com.loremind.domain.campaigncontext.PrerequisiteEvaluator;
|
||||
import com.loremind.domain.campaigncontext.ProgressionStatus;
|
||||
import com.loremind.domain.campaigncontext.QuestStatus;
|
||||
import com.loremind.domain.playcontext.QuestProgression;
|
||||
import com.loremind.domain.playcontext.ports.PlaythroughFlagRepository;
|
||||
import com.loremind.domain.playcontext.ports.PlaythroughRepository;
|
||||
import com.loremind.domain.playcontext.ports.QuestProgressionRepository;
|
||||
import com.loremind.domain.playcontext.ports.SessionRepository;
|
||||
import com.loremind.infrastructure.web.dto.campaigncontext.ChapterDTO;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Service applicatif : enrichit des ChapterDTO avec leur {@link QuestStatus} effectif,
|
||||
* relatif à un Playthrough donné.
|
||||
*
|
||||
* <p>Depuis l'introduction de Playthrough : la progression et les flags vivent au niveau
|
||||
* de la Partie, plus de la Campagne. L'enrichissement nécessite donc un playthroughId.</p>
|
||||
*/
|
||||
@Service
|
||||
public class ChapterStatusEnricher {
|
||||
|
||||
private final PlaythroughRepository playthroughRepository;
|
||||
private final QuestProgressionRepository progressionRepository;
|
||||
private final PlaythroughFlagRepository flagRepository;
|
||||
private final SessionRepository sessionRepository;
|
||||
private final PrerequisiteEvaluator evaluator = new PrerequisiteEvaluator();
|
||||
|
||||
public ChapterStatusEnricher(PlaythroughRepository playthroughRepository,
|
||||
QuestProgressionRepository progressionRepository,
|
||||
PlaythroughFlagRepository flagRepository,
|
||||
SessionRepository sessionRepository) {
|
||||
this.playthroughRepository = playthroughRepository;
|
||||
this.progressionRepository = progressionRepository;
|
||||
this.flagRepository = flagRepository;
|
||||
this.sessionRepository = sessionRepository;
|
||||
}
|
||||
|
||||
/** Contexte d'évaluation + map chapterId -> ProgressionStatus pour ce Playthrough. */
|
||||
public record PlaythroughEvalSnapshot(
|
||||
PrerequisiteEvaluator.EvaluationContext ctx,
|
||||
Map<String, ProgressionStatus> progressionByChapterId
|
||||
) {}
|
||||
|
||||
/** Construit le snapshot d'évaluation pour un Playthrough. */
|
||||
public PlaythroughEvalSnapshot buildSnapshot(String playthroughId) {
|
||||
if (playthroughId == null || playthroughRepository.findById(playthroughId).isEmpty()) {
|
||||
return new PlaythroughEvalSnapshot(
|
||||
new PrerequisiteEvaluator.EvaluationContext(Collections.emptySet(), 0, Collections.emptyMap()),
|
||||
Collections.emptyMap()
|
||||
);
|
||||
}
|
||||
Map<String, Boolean> flags = flagRepository.findByPlaythroughId(playthroughId);
|
||||
Set<String> completedQuestIds = progressionRepository.findCompletedChapterIdsByPlaythroughId(playthroughId);
|
||||
int sessionCount = sessionRepository.findByPlaythroughId(playthroughId).size();
|
||||
|
||||
Map<String, ProgressionStatus> progressionMap = new HashMap<>();
|
||||
for (QuestProgression qp : progressionRepository.findByPlaythroughId(playthroughId)) {
|
||||
progressionMap.put(qp.getChapterId(), qp.getStatus());
|
||||
}
|
||||
|
||||
return new PlaythroughEvalSnapshot(
|
||||
new PrerequisiteEvaluator.EvaluationContext(completedQuestIds, sessionCount, flags),
|
||||
progressionMap
|
||||
);
|
||||
}
|
||||
|
||||
/** Calcule le statut effectif d'un seul chapitre relatif à un Playthrough. */
|
||||
public QuestStatus computeFor(Chapter chapter, String playthroughId) {
|
||||
PlaythroughEvalSnapshot snap = buildSnapshot(playthroughId);
|
||||
ProgressionStatus progression = snap.progressionByChapterId()
|
||||
.getOrDefault(chapter.getId(), ProgressionStatus.NOT_STARTED);
|
||||
return evaluator.computeStatus(progression, chapter.getPrerequisites(), snap.ctx());
|
||||
}
|
||||
|
||||
/**
|
||||
* Injecte le {@code effectiveStatus} et le {@code progressionStatus} dans une liste de DTOs.
|
||||
* Un seul build du snapshot pour toute la liste (optimal pour les vues qui listent un arc).
|
||||
*/
|
||||
public void enrich(List<ChapterDTO> dtos, List<Chapter> domain, String playthroughId) {
|
||||
if (dtos == null || dtos.isEmpty()) return;
|
||||
PlaythroughEvalSnapshot snap = buildSnapshot(playthroughId);
|
||||
Map<String, Chapter> byId = domain.stream()
|
||||
.collect(Collectors.toMap(Chapter::getId, c -> c));
|
||||
for (ChapterDTO dto : dtos) {
|
||||
Chapter c = byId.get(dto.getId());
|
||||
if (c == null) continue;
|
||||
ProgressionStatus progression = snap.progressionByChapterId()
|
||||
.getOrDefault(c.getId(), ProgressionStatus.NOT_STARTED);
|
||||
QuestStatus status = evaluator.computeStatus(progression, c.getPrerequisites(), snap.ctx());
|
||||
dto.setProgressionStatus(progression.name());
|
||||
dto.setEffectiveStatus(status.name());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
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.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Service d'application pour les fiches de personnages (PJ).
|
||||
* Les PJ appartiennent désormais à un Playthrough (Partie), pas à la Campagne.
|
||||
*/
|
||||
@Service
|
||||
public class CharacterService {
|
||||
|
||||
private final CharacterRepository characterRepository;
|
||||
|
||||
public CharacterService(CharacterRepository characterRepository) {
|
||||
this.characterRepository = characterRepository;
|
||||
}
|
||||
|
||||
public record CharacterData(
|
||||
String name,
|
||||
String portraitImageId,
|
||||
String headerImageId,
|
||||
Map<String, String> values,
|
||||
Map<String, List<String>> imageValues,
|
||||
Map<String, Map<String, String>> keyValueValues,
|
||||
String playthroughId,
|
||||
Integer order
|
||||
) {}
|
||||
|
||||
public Character createCharacter(CharacterData data) {
|
||||
int order = data.order() != null
|
||||
? data.order()
|
||||
: nextOrderFor(data.playthroughId());
|
||||
Character character = Character.builder()
|
||||
.name(data.name())
|
||||
.portraitImageId(data.portraitImageId())
|
||||
.headerImageId(data.headerImageId())
|
||||
.values(data.values() != null ? new HashMap<>(data.values()) : new HashMap<>())
|
||||
.imageValues(data.imageValues() != null ? new HashMap<>(data.imageValues()) : new HashMap<>())
|
||||
.keyValueValues(data.keyValueValues() != null ? new HashMap<>(data.keyValueValues()) : new HashMap<>())
|
||||
.playthroughId(data.playthroughId())
|
||||
.order(order)
|
||||
.build();
|
||||
return characterRepository.save(character);
|
||||
}
|
||||
|
||||
public Optional<Character> getCharacterById(String id) {
|
||||
return characterRepository.findById(id);
|
||||
}
|
||||
|
||||
public List<Character> getCharactersByPlaythroughId(String playthroughId) {
|
||||
return characterRepository.findByPlaythroughId(playthroughId);
|
||||
}
|
||||
|
||||
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.setPortraitImageId(data.portraitImageId());
|
||||
existing.setHeaderImageId(data.headerImageId());
|
||||
existing.setValues(data.values() != null ? new HashMap<>(data.values()) : new HashMap<>());
|
||||
existing.setImageValues(data.imageValues() != null ? new HashMap<>(data.imageValues()) : new HashMap<>());
|
||||
existing.setKeyValueValues(data.keyValueValues() != null ? new HashMap<>(data.keyValueValues()) : new HashMap<>());
|
||||
if (data.order() != null) {
|
||||
existing.setOrder(data.order());
|
||||
}
|
||||
// playthroughId immuable après création.
|
||||
return characterRepository.save(existing);
|
||||
}
|
||||
|
||||
public void deleteCharacter(String id) {
|
||||
characterRepository.deleteById(id);
|
||||
}
|
||||
|
||||
private int nextOrderFor(String playthroughId) {
|
||||
return characterRepository.findByPlaythroughId(playthroughId).stream()
|
||||
.mapToInt(Character::getOrder)
|
||||
.max()
|
||||
.orElse(-1) + 1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package com.loremind.application.campaigncontext;
|
||||
|
||||
import com.loremind.domain.campaigncontext.Npc;
|
||||
import com.loremind.domain.campaigncontext.ports.NpcRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Service d'application pour les fiches de PNJ (campagne).
|
||||
*/
|
||||
@Service
|
||||
public class NpcService {
|
||||
|
||||
private final NpcRepository npcRepository;
|
||||
|
||||
public NpcService(NpcRepository npcRepository) {
|
||||
this.npcRepository = npcRepository;
|
||||
}
|
||||
|
||||
public record NpcData(
|
||||
String name,
|
||||
String portraitImageId,
|
||||
String headerImageId,
|
||||
Map<String, String> values,
|
||||
Map<String, List<String>> imageValues,
|
||||
Map<String, Map<String, String>> keyValueValues,
|
||||
String campaignId,
|
||||
Integer order
|
||||
) {}
|
||||
|
||||
public Npc createNpc(NpcData data) {
|
||||
int order = data.order() != null
|
||||
? data.order()
|
||||
: nextOrderFor(data.campaignId());
|
||||
Npc npc = Npc.builder()
|
||||
.name(data.name())
|
||||
.portraitImageId(data.portraitImageId())
|
||||
.headerImageId(data.headerImageId())
|
||||
.values(data.values() != null ? new HashMap<>(data.values()) : new HashMap<>())
|
||||
.imageValues(data.imageValues() != null ? new HashMap<>(data.imageValues()) : new HashMap<>())
|
||||
.keyValueValues(data.keyValueValues() != null ? new HashMap<>(data.keyValueValues()) : new HashMap<>())
|
||||
.campaignId(data.campaignId())
|
||||
.order(order)
|
||||
.build();
|
||||
return npcRepository.save(npc);
|
||||
}
|
||||
|
||||
public Optional<Npc> getNpcById(String id) {
|
||||
return npcRepository.findById(id);
|
||||
}
|
||||
|
||||
public List<Npc> getNpcsByCampaignId(String campaignId) {
|
||||
return npcRepository.findByCampaignId(campaignId);
|
||||
}
|
||||
|
||||
public Npc updateNpc(String id, NpcData data) {
|
||||
Npc existing = npcRepository.findById(id)
|
||||
.orElseThrow(() -> new IllegalArgumentException("Npc non trouvé avec l'ID: " + id));
|
||||
existing.setName(data.name());
|
||||
existing.setPortraitImageId(data.portraitImageId());
|
||||
existing.setHeaderImageId(data.headerImageId());
|
||||
existing.setValues(data.values() != null ? new HashMap<>(data.values()) : new HashMap<>());
|
||||
existing.setImageValues(data.imageValues() != null ? new HashMap<>(data.imageValues()) : new HashMap<>());
|
||||
existing.setKeyValueValues(data.keyValueValues() != null ? new HashMap<>(data.keyValueValues()) : new HashMap<>());
|
||||
if (data.order() != null) {
|
||||
existing.setOrder(data.order());
|
||||
}
|
||||
return npcRepository.save(existing);
|
||||
}
|
||||
|
||||
public void deleteNpc(String id) {
|
||||
npcRepository.deleteById(id);
|
||||
}
|
||||
|
||||
private int nextOrderFor(String campaignId) {
|
||||
return npcRepository.findByCampaignId(campaignId).stream()
|
||||
.mapToInt(Npc::getOrder)
|
||||
.max()
|
||||
.orElse(-1) + 1;
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import com.loremind.domain.campaigncontext.ports.SceneRepository;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
@@ -26,15 +27,33 @@ public class SceneService {
|
||||
}
|
||||
|
||||
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()
|
||||
.name(name)
|
||||
.description(description)
|
||||
.chapterId(chapterId)
|
||||
.order(order)
|
||||
.icon(icon)
|
||||
.build();
|
||||
return sceneRepository.save(scene);
|
||||
}
|
||||
|
||||
/**
|
||||
* Crée une scène à partir d'un objet Scene complet (tous les champs : narration
|
||||
* joueurs, notes MJ, pièces…). Utilisé par l'import de campagne. L'ID est forcé
|
||||
* à null pour laisser le repo en générer un nouveau.
|
||||
*/
|
||||
public Scene createScene(Scene input) {
|
||||
input.setId(null);
|
||||
if (input.getRooms() == null) {
|
||||
input.setRooms(new ArrayList<>());
|
||||
}
|
||||
return sceneRepository.save(input);
|
||||
}
|
||||
|
||||
public Optional<Scene> getSceneById(String id) {
|
||||
return sceneRepository.findById(id);
|
||||
}
|
||||
@@ -93,7 +112,7 @@ public class SceneService {
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
for (SceneBranch b : branches) {
|
||||
String target = b.getTargetSceneId();
|
||||
String target = b.targetSceneId();
|
||||
if (target == null || target.isBlank()) {
|
||||
throw new IllegalArgumentException("Une branche doit avoir une scène de destination");
|
||||
}
|
||||
|
||||
@@ -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,111 @@
|
||||
package com.loremind.application.gamesystemcontext;
|
||||
|
||||
import com.loremind.domain.gamesystemcontext.GameSystem;
|
||||
import com.loremind.domain.gamesystemcontext.RulesImportResult;
|
||||
import com.loremind.domain.gamesystemcontext.ports.GameSystemRepository;
|
||||
import com.loremind.domain.gamesystemcontext.ports.RulesPdfImporter;
|
||||
import com.loremind.domain.shared.template.TemplateField;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class GameSystemService {
|
||||
|
||||
private final GameSystemRepository gameSystemRepository;
|
||||
private final RulesPdfImporter rulesPdfImporter;
|
||||
|
||||
public GameSystemService(GameSystemRepository gameSystemRepository,
|
||||
RulesPdfImporter rulesPdfImporter) {
|
||||
this.gameSystemRepository = gameSystemRepository;
|
||||
this.rulesPdfImporter = rulesPdfImporter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Importe un PDF de règles et renvoie une PROPOSITION de sections (titre →
|
||||
* markdown). Ne persiste rien : l'UI laisse l'utilisateur réviser/éditer
|
||||
* puis enregistrer le GameSystem via {@link #updateGameSystem}/{@link #createGameSystem}.
|
||||
*/
|
||||
public RulesImportResult importRulesFromPdf(byte[] pdfBytes, String filename) {
|
||||
return rulesPdfImporter.importRules(pdfBytes, filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* Variante streamée de {@link #importRulesFromPdf} : remonte l'avancement via
|
||||
* callbacks (import long → l'UI affiche une progression). Ne persiste rien.
|
||||
*/
|
||||
public void importRulesFromPdfStreaming(
|
||||
byte[] pdfBytes,
|
||||
String filename,
|
||||
java.util.function.Consumer<com.loremind.domain.gamesystemcontext.RulesImportProgress> onProgress,
|
||||
java.util.function.Consumer<RulesImportResult> onDone,
|
||||
java.util.function.Consumer<Throwable> onError) {
|
||||
rulesPdfImporter.importRulesStreaming(pdfBytes, filename, onProgress, onDone, onError);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parameter Object pour la création / mise à jour d'un GameSystem.
|
||||
* Les templates peuvent etre null (interpretes comme listes vides).
|
||||
*/
|
||||
public record GameSystemData(
|
||||
String name,
|
||||
String description,
|
||||
String rulesMarkdown,
|
||||
List<TemplateField> characterTemplate,
|
||||
List<TemplateField> npcTemplate,
|
||||
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();
|
||||
gameSystem.replaceCharacterTemplate(data.characterTemplate());
|
||||
gameSystem.replaceNpcTemplate(data.npcTemplate());
|
||||
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.replaceCharacterTemplate(data.characterTemplate());
|
||||
existing.replaceNpcTemplate(data.npcTemplate());
|
||||
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,23 @@ package com.loremind.application.generationcontext;
|
||||
import com.loremind.domain.campaigncontext.Arc;
|
||||
import com.loremind.domain.campaigncontext.Campaign;
|
||||
import com.loremind.domain.campaigncontext.Chapter;
|
||||
import com.loremind.domain.campaigncontext.Character;
|
||||
import com.loremind.domain.campaigncontext.Npc;
|
||||
import com.loremind.domain.campaigncontext.Scene;
|
||||
import com.loremind.domain.campaigncontext.ports.ArcRepository;
|
||||
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.NpcRepository;
|
||||
import com.loremind.domain.campaigncontext.ports.SceneRepository;
|
||||
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.NpcSummary;
|
||||
import com.loremind.domain.generationcontext.CampaignStructuralContext.RoomBranchHint;
|
||||
import com.loremind.domain.generationcontext.CampaignStructuralContext.RoomSummary;
|
||||
import com.loremind.domain.generationcontext.CampaignStructuralContext.SceneSummary;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -38,24 +46,40 @@ public class CampaignStructuralContextBuilder {
|
||||
private final ArcRepository arcRepository;
|
||||
private final ChapterRepository chapterRepository;
|
||||
private final SceneRepository sceneRepository;
|
||||
private final CharacterRepository characterRepository;
|
||||
private final NpcRepository npcRepository;
|
||||
|
||||
public CampaignStructuralContextBuilder(
|
||||
CampaignRepository campaignRepository,
|
||||
ArcRepository arcRepository,
|
||||
ChapterRepository chapterRepository,
|
||||
SceneRepository sceneRepository) {
|
||||
SceneRepository sceneRepository,
|
||||
CharacterRepository characterRepository,
|
||||
NpcRepository npcRepository) {
|
||||
this.campaignRepository = campaignRepository;
|
||||
this.arcRepository = arcRepository;
|
||||
this.chapterRepository = chapterRepository;
|
||||
this.sceneRepository = sceneRepository;
|
||||
this.characterRepository = characterRepository;
|
||||
this.npcRepository = npcRepository;
|
||||
}
|
||||
|
||||
/** Longueur max du snippet de PJ/PNJ 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. Sans playthroughId, les PJ
|
||||
* sont omis (ils sont propres à une Partie).
|
||||
*/
|
||||
public CampaignStructuralContext build(String campaignId) {
|
||||
return build(campaignId, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Construit la carte narrative d'une Campagne (arcs → chapitres → scènes,
|
||||
* nom + description courte à chaque niveau).
|
||||
* @throws IllegalArgumentException si la Campagne est introuvable
|
||||
* Variante avec playthroughId : injecte les PJ de la Partie indiquée.
|
||||
* Les PNJ restent campagne-scope (donnée de scénario).
|
||||
*/
|
||||
public CampaignStructuralContext build(String campaignId) {
|
||||
public CampaignStructuralContext build(String campaignId, String playthroughId) {
|
||||
Campaign campaign = campaignRepository.findById(campaignId)
|
||||
.orElseThrow(() -> new IllegalArgumentException(
|
||||
"Campagne non trouvée avec l'ID: " + campaignId));
|
||||
@@ -65,11 +89,58 @@ public class CampaignStructuralContextBuilder {
|
||||
.map(this::toArcSummary)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return CampaignStructuralContext.builder()
|
||||
.campaignName(campaign.getName())
|
||||
.campaignDescription(campaign.getDescription())
|
||||
.arcs(arcs)
|
||||
.build();
|
||||
List<CharacterSummary> characters = (playthroughId == null || playthroughId.isBlank())
|
||||
? List.of()
|
||||
: characterRepository.findByPlaythroughId(playthroughId).stream()
|
||||
.sorted(Comparator.comparingInt(Character::getOrder))
|
||||
.map(this::toCharacterSummary)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
List<NpcSummary> npcs = npcRepository.findByCampaignId(campaignId).stream()
|
||||
.sorted(Comparator.comparingInt(Npc::getOrder))
|
||||
.map(this::toNpcSummary)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return new CampaignStructuralContext(
|
||||
campaign.getName(),
|
||||
campaign.getDescription(),
|
||||
arcs,
|
||||
characters,
|
||||
npcs);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.getValues()));
|
||||
}
|
||||
|
||||
/** Symétrique à {@link #toCharacterSummary} pour les PNJ. */
|
||||
private NpcSummary toNpcSummary(Npc n) {
|
||||
return new NpcSummary(n.getName(), extractSnippet(n.getValues()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Snippet pour le resume IA : 1re ligne signifiante de la 1re valeur non vide
|
||||
* du template (refonte 2026-04-30 — remplace l'ancien parsing markdown).
|
||||
*/
|
||||
private static String extractSnippet(java.util.Map<String, String> values) {
|
||||
if (values == null || values.isEmpty()) return "";
|
||||
for (String value : values.values()) {
|
||||
if (value == null || value.isBlank()) continue;
|
||||
String firstLine = value.lines()
|
||||
.map(String::strip)
|
||||
.filter(l -> !l.isEmpty() && !l.startsWith("#"))
|
||||
.findFirst()
|
||||
.orElse("");
|
||||
if (firstLine.isEmpty()) continue;
|
||||
if (firstLine.length() <= CHARACTER_SNIPPET_MAX_LEN) return firstLine;
|
||||
return firstLine.substring(0, CHARACTER_SNIPPET_MAX_LEN - 1).stripTrailing() + "…";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
private ArcSummary toArcSummary(Arc arc) {
|
||||
@@ -77,12 +148,11 @@ public class CampaignStructuralContextBuilder {
|
||||
.sorted(Comparator.comparingInt(Chapter::getOrder))
|
||||
.map(this::toChapterSummary)
|
||||
.collect(Collectors.toList());
|
||||
return ArcSummary.builder()
|
||||
.name(arc.getName())
|
||||
.description(arc.getDescription())
|
||||
.illustrationCount(countImages(arc.getIllustrationImageIds()))
|
||||
.chapters(chapters)
|
||||
.build();
|
||||
return new ArcSummary(
|
||||
arc.getName(),
|
||||
arc.getDescription(),
|
||||
countImages(arc.getIllustrationImageIds()),
|
||||
chapters);
|
||||
}
|
||||
|
||||
private ChapterSummary toChapterSummary(Chapter chapter) {
|
||||
@@ -99,32 +169,59 @@ public class CampaignStructuralContextBuilder {
|
||||
.map(s -> toSceneSummary(s, nameById))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return ChapterSummary.builder()
|
||||
.name(chapter.getName())
|
||||
.description(chapter.getDescription())
|
||||
.illustrationCount(countImages(chapter.getIllustrationImageIds()))
|
||||
.scenes(summaries)
|
||||
.build();
|
||||
return new ChapterSummary(
|
||||
chapter.getName(),
|
||||
chapter.getDescription(),
|
||||
countImages(chapter.getIllustrationImageIds()),
|
||||
summaries);
|
||||
}
|
||||
|
||||
private SceneSummary toSceneSummary(Scene scene, Map<String, String> nameById) {
|
||||
List<BranchHint> hints = scene.getBranches() == null
|
||||
? List.of()
|
||||
: scene.getBranches().stream()
|
||||
.map(b -> BranchHint.builder()
|
||||
.label(b.getLabel())
|
||||
.targetSceneName(nameById.getOrDefault(
|
||||
b.getTargetSceneId(), "(scène inconnue)"))
|
||||
.condition(b.getCondition())
|
||||
.build())
|
||||
.map(b -> new BranchHint(
|
||||
b.label(),
|
||||
nameById.getOrDefault(b.targetSceneId(), "(scène inconnue)"),
|
||||
b.condition()))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return SceneSummary.builder()
|
||||
.name(scene.getName())
|
||||
.description(scene.getDescription())
|
||||
.illustrationCount(countImages(scene.getIllustrationImageIds()))
|
||||
.branches(hints)
|
||||
.build();
|
||||
List<RoomSummary> rooms = toRoomSummaries(scene);
|
||||
|
||||
return new SceneSummary(
|
||||
scene.getName(),
|
||||
scene.getDescription(),
|
||||
countImages(scene.getIllustrationImageIds()),
|
||||
hints,
|
||||
rooms);
|
||||
}
|
||||
|
||||
/**
|
||||
* Projette les pièces d'une scène en RoomSummary pour le contexte IA.
|
||||
* Pas de notes MJ, pas de loot ni pièges : le prompt reste lisible. L'IA
|
||||
* connaît la structure du lieu (nom des pièces, ennemis, sorties) — c'est
|
||||
* suffisant pour proposer de la narration ou anticiper les choix.
|
||||
*/
|
||||
private List<RoomSummary> toRoomSummaries(Scene scene) {
|
||||
if (scene.getRooms() == null || scene.getRooms().isEmpty()) return List.of();
|
||||
Map<String, String> nameById = scene.getRooms().stream()
|
||||
.collect(Collectors.toMap(
|
||||
com.loremind.domain.campaigncontext.Room::getId,
|
||||
com.loremind.domain.campaigncontext.Room::getName,
|
||||
(a, b) -> a));
|
||||
return scene.getRooms().stream()
|
||||
.map(r -> {
|
||||
List<RoomBranchHint> hints = r.getBranches() == null
|
||||
? List.of()
|
||||
: r.getBranches().stream()
|
||||
.map(b -> new RoomBranchHint(
|
||||
b.label(),
|
||||
nameById.getOrDefault(b.targetRoomId(), "(pièce inconnue)"),
|
||||
b.condition()))
|
||||
.collect(Collectors.toList());
|
||||
return new RoomSummary(r.getName(), r.getFloor(), r.getDescription(), r.getEnemies(), hints);
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/** Helper defensif : compte les illustrations attachees (null-safe). */
|
||||
|
||||
@@ -67,16 +67,15 @@ public class GeneratePageValuesUseCase {
|
||||
|
||||
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
|
||||
// necessitent un workflow different (pas de generation LLM texte).
|
||||
.templateFields(template.textFieldNames())
|
||||
.pageTitle(page.getTitle())
|
||||
.build();
|
||||
GenerationContext context = new GenerationContext(
|
||||
lore.getName(),
|
||||
lore.getDescription(),
|
||||
folder.getName(),
|
||||
template.getName(),
|
||||
template.textFieldNames(),
|
||||
page.getTitle());
|
||||
|
||||
GenerationResult result = aiProvider.generatePage(context);
|
||||
return result.values();
|
||||
|
||||
@@ -82,12 +82,11 @@ public class LoreStructuralContextBuilder {
|
||||
Map<String, String> pageTitleById = pages.stream()
|
||||
.collect(Collectors.toMap(Page::getId, Page::getTitle, (a, b) -> a));
|
||||
|
||||
return LoreStructuralContext.builder()
|
||||
.loreName(lore.getName())
|
||||
.loreDescription(lore.getDescription())
|
||||
.folders(buildFoldersMap(nodes, pages, templateNameById, pageTitleById))
|
||||
.tags(extractUniqueTags(pages))
|
||||
.build();
|
||||
return new LoreStructuralContext(
|
||||
lore.getName(),
|
||||
lore.getDescription(),
|
||||
buildFoldersMap(nodes, pages, templateNameById, pageTitleById),
|
||||
extractUniqueTags(pages));
|
||||
}
|
||||
|
||||
private Map<String, List<PageSummary>> buildFoldersMap(
|
||||
@@ -118,13 +117,12 @@ public class LoreStructuralContextBuilder {
|
||||
Page page,
|
||||
Map<String, String> templateNameById,
|
||||
Map<String, String> pageTitleById) {
|
||||
return PageSummary.builder()
|
||||
.title(page.getTitle())
|
||||
.templateName(templateNameById.getOrDefault(page.getTemplateId(), "?"))
|
||||
.values(truncatedValues(page.getValues()))
|
||||
.tags(page.getTags() != null ? List.copyOf(page.getTags()) : Collections.emptyList())
|
||||
.relatedPageTitles(resolveRelatedTitles(page.getRelatedPageIds(), pageTitleById))
|
||||
.build();
|
||||
return new PageSummary(
|
||||
page.getTitle(),
|
||||
templateNameById.getOrDefault(page.getTemplateId(), "?"),
|
||||
truncatedValues(page.getValues()),
|
||||
page.getTags() != null ? List.copyOf(page.getTags()) : Collections.emptyList(),
|
||||
resolveRelatedTitles(page.getRelatedPageIds(), pageTitleById));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,9 +2,13 @@ package com.loremind.application.generationcontext;
|
||||
|
||||
import com.loremind.domain.campaigncontext.Arc;
|
||||
import com.loremind.domain.campaigncontext.Chapter;
|
||||
import com.loremind.domain.campaigncontext.Character;
|
||||
import com.loremind.domain.campaigncontext.Npc;
|
||||
import com.loremind.domain.campaigncontext.Scene;
|
||||
import com.loremind.domain.campaigncontext.ports.ArcRepository;
|
||||
import com.loremind.domain.campaigncontext.ports.ChapterRepository;
|
||||
import com.loremind.domain.campaigncontext.ports.CharacterRepository;
|
||||
import com.loremind.domain.campaigncontext.ports.NpcRepository;
|
||||
import com.loremind.domain.campaigncontext.ports.SceneRepository;
|
||||
import com.loremind.domain.generationcontext.NarrativeEntityContext;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -26,20 +30,26 @@ public class NarrativeEntityContextBuilder {
|
||||
private final ArcRepository arcRepository;
|
||||
private final ChapterRepository chapterRepository;
|
||||
private final SceneRepository sceneRepository;
|
||||
private final CharacterRepository characterRepository;
|
||||
private final NpcRepository npcRepository;
|
||||
|
||||
public NarrativeEntityContextBuilder(
|
||||
ArcRepository arcRepository,
|
||||
ChapterRepository chapterRepository,
|
||||
SceneRepository sceneRepository) {
|
||||
SceneRepository sceneRepository,
|
||||
CharacterRepository characterRepository,
|
||||
NpcRepository npcRepository) {
|
||||
this.arcRepository = arcRepository;
|
||||
this.chapterRepository = chapterRepository;
|
||||
this.sceneRepository = sceneRepository;
|
||||
this.characterRepository = characterRepository;
|
||||
this.npcRepository = npcRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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", "character" ou "npc" (insensible à la casse)
|
||||
* @param entityId l'ID de l'entité
|
||||
* @throws IllegalArgumentException si le type est inconnu ou l'entité introuvable
|
||||
*/
|
||||
@@ -49,6 +59,8 @@ public class NarrativeEntityContextBuilder {
|
||||
case "arc" -> fromArc(loadArc(entityId));
|
||||
case "chapter" -> fromChapter(loadChapter(entityId));
|
||||
case "scene" -> fromScene(loadScene(entityId));
|
||||
case "character" -> fromCharacter(loadCharacter(entityId));
|
||||
case "npc" -> fromNpc(loadNpc(entityId));
|
||||
default -> throw new IllegalArgumentException("Type d'entité narrative inconnu: " + entityType);
|
||||
};
|
||||
}
|
||||
@@ -70,6 +82,16 @@ public class NarrativeEntityContextBuilder {
|
||||
.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));
|
||||
}
|
||||
|
||||
private Npc loadNpc(String id) {
|
||||
return npcRepository.findById(id)
|
||||
.orElseThrow(() -> new IllegalArgumentException("PNJ non trouvé: " + id));
|
||||
}
|
||||
|
||||
// --- Mapping entité → VO ------------------------------------------------
|
||||
|
||||
private NarrativeEntityContext fromArc(Arc a) {
|
||||
@@ -80,11 +102,7 @@ public class NarrativeEntityContextBuilder {
|
||||
putField(fields, "rewards", a.getRewards());
|
||||
putField(fields, "resolution", a.getResolution());
|
||||
putField(fields, "gmNotes", a.getGmNotes());
|
||||
return NarrativeEntityContext.builder()
|
||||
.entityType("arc")
|
||||
.title(a.getName())
|
||||
.fields(fields)
|
||||
.build();
|
||||
return new NarrativeEntityContext("arc", a.getName(), fields);
|
||||
}
|
||||
|
||||
private NarrativeEntityContext fromChapter(Chapter c) {
|
||||
@@ -93,11 +111,7 @@ public class NarrativeEntityContextBuilder {
|
||||
putField(fields, "playerObjectives", c.getPlayerObjectives());
|
||||
putField(fields, "narrativeStakes", c.getNarrativeStakes());
|
||||
putField(fields, "gmNotes", c.getGmNotes());
|
||||
return NarrativeEntityContext.builder()
|
||||
.entityType("chapter")
|
||||
.title(c.getName())
|
||||
.fields(fields)
|
||||
.build();
|
||||
return new NarrativeEntityContext("chapter", c.getName(), fields);
|
||||
}
|
||||
|
||||
private NarrativeEntityContext fromScene(Scene s) {
|
||||
@@ -111,11 +125,25 @@ public class NarrativeEntityContextBuilder {
|
||||
putField(fields, "combatDifficulty", s.getCombatDifficulty());
|
||||
putField(fields, "enemies", s.getEnemies());
|
||||
putField(fields, "gmSecretNotes", s.getGmSecretNotes());
|
||||
return NarrativeEntityContext.builder()
|
||||
.entityType("scene")
|
||||
.title(s.getName())
|
||||
.fields(fields)
|
||||
.build();
|
||||
return new NarrativeEntityContext("scene", s.getName(), fields);
|
||||
}
|
||||
|
||||
private NarrativeEntityContext fromCharacter(Character c) {
|
||||
Map<String, String> fields = new LinkedHashMap<>();
|
||||
if (c.getValues() != null) {
|
||||
// Champs templates exposes individuellement — meilleur pour le LLM que
|
||||
// l'ancien blob markdown monolithique.
|
||||
c.getValues().forEach((k, v) -> putField(fields, k, v));
|
||||
}
|
||||
return new NarrativeEntityContext("character", c.getName(), fields);
|
||||
}
|
||||
|
||||
private NarrativeEntityContext fromNpc(Npc n) {
|
||||
Map<String, String> fields = new LinkedHashMap<>();
|
||||
if (n.getValues() != null) {
|
||||
n.getValues().forEach((k, v) -> putField(fields, k, v));
|
||||
}
|
||||
return new NarrativeEntityContext("npc", n.getName(), fields);
|
||||
}
|
||||
|
||||
/** Null/blank devient chaîne vide — uniforme côté prompt, pas de NPE côté LLM. */
|
||||
|
||||
@@ -0,0 +1,233 @@
|
||||
package com.loremind.application.generationcontext;
|
||||
|
||||
import com.loremind.domain.campaigncontext.Arc;
|
||||
import com.loremind.domain.campaigncontext.ArcType;
|
||||
import com.loremind.domain.campaigncontext.Chapter;
|
||||
import com.loremind.domain.campaigncontext.PrerequisiteEvaluator;
|
||||
import com.loremind.domain.campaigncontext.ProgressionStatus;
|
||||
import com.loremind.domain.campaigncontext.QuestStatus;
|
||||
import com.loremind.domain.campaigncontext.ports.ArcRepository;
|
||||
import com.loremind.domain.campaigncontext.ports.ChapterRepository;
|
||||
import com.loremind.domain.generationcontext.SessionContext;
|
||||
import com.loremind.domain.generationcontext.SessionContext.JournalEntrySummary;
|
||||
import com.loremind.domain.generationcontext.SessionContext.QuestSummary;
|
||||
import com.loremind.domain.playcontext.EntryType;
|
||||
import com.loremind.domain.playcontext.Playthrough;
|
||||
import com.loremind.domain.playcontext.QuestProgression;
|
||||
import com.loremind.domain.playcontext.Session;
|
||||
import com.loremind.domain.playcontext.SessionEntry;
|
||||
import com.loremind.domain.playcontext.ports.PlaythroughFlagRepository;
|
||||
import com.loremind.domain.playcontext.ports.PlaythroughRepository;
|
||||
import com.loremind.domain.playcontext.ports.QuestProgressionRepository;
|
||||
import com.loremind.domain.playcontext.ports.SessionEntryRepository;
|
||||
import com.loremind.domain.playcontext.ports.SessionRepository;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Construit le SessionContext injecté dans le prompt IA pendant une partie.
|
||||
*
|
||||
* <p>Depuis la refonte Playthrough : la session connaît son playthroughId ; la progression
|
||||
* et les flags viennent du Playthrough (pas plus de la Campagne ni du Chapter).</p>
|
||||
*/
|
||||
@Component
|
||||
public class SessionStructuralContextBuilder {
|
||||
|
||||
private static final int MAX_CURRENT_ENTRIES = 80;
|
||||
private static final int MAX_PREVIOUS_EVENTS = 60;
|
||||
|
||||
private final SessionRepository sessionRepository;
|
||||
private final SessionEntryRepository entryRepository;
|
||||
private final PlaythroughRepository playthroughRepository;
|
||||
private final ArcRepository arcRepository;
|
||||
private final ChapterRepository chapterRepository;
|
||||
private final PlaythroughFlagRepository playthroughFlagRepository;
|
||||
private final QuestProgressionRepository questProgressionRepository;
|
||||
private final PrerequisiteEvaluator prerequisiteEvaluator = new PrerequisiteEvaluator();
|
||||
|
||||
public SessionStructuralContextBuilder(SessionRepository sessionRepository,
|
||||
SessionEntryRepository entryRepository,
|
||||
PlaythroughRepository playthroughRepository,
|
||||
ArcRepository arcRepository,
|
||||
ChapterRepository chapterRepository,
|
||||
PlaythroughFlagRepository playthroughFlagRepository,
|
||||
QuestProgressionRepository questProgressionRepository) {
|
||||
this.sessionRepository = sessionRepository;
|
||||
this.entryRepository = entryRepository;
|
||||
this.playthroughRepository = playthroughRepository;
|
||||
this.arcRepository = arcRepository;
|
||||
this.chapterRepository = chapterRepository;
|
||||
this.playthroughFlagRepository = playthroughFlagRepository;
|
||||
this.questProgressionRepository = questProgressionRepository;
|
||||
}
|
||||
|
||||
public Optional<SessionContext> buildOptional(String sessionId) {
|
||||
return sessionRepository.findById(sessionId).map(this::toContext);
|
||||
}
|
||||
|
||||
public SessionContext build(String sessionId) {
|
||||
Session session = sessionRepository.findById(sessionId)
|
||||
.orElseThrow(() -> new IllegalArgumentException("Session introuvable : " + sessionId));
|
||||
return toContext(session);
|
||||
}
|
||||
|
||||
private SessionContext toContext(Session session) {
|
||||
List<JournalEntrySummary> currentEntries = loadCurrentEntries(session);
|
||||
List<JournalEntrySummary> previousEvents = loadPreviousEvents(session);
|
||||
HubStatus hub = computeHubStatus(session.getPlaythroughId());
|
||||
|
||||
return new SessionContext(
|
||||
session.getName(),
|
||||
session.isActive(),
|
||||
session.getStartedAt(),
|
||||
currentEntries,
|
||||
previousEvents,
|
||||
hub.available(),
|
||||
hub.inProgress(),
|
||||
hub.lockedTitles(),
|
||||
hub.activeFlags());
|
||||
}
|
||||
|
||||
private List<JournalEntrySummary> loadCurrentEntries(Session session) {
|
||||
List<SessionEntry> allEntries = entryRepository.findBySessionId(session.getId());
|
||||
List<SessionEntry> kept = allEntries.size() <= MAX_CURRENT_ENTRIES
|
||||
? allEntries
|
||||
: allEntries.subList(allEntries.size() - MAX_CURRENT_ENTRIES, allEntries.size());
|
||||
|
||||
return kept.stream()
|
||||
.map(e -> toSummary(e, null))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* EVENTs des sessions précédentes du MÊME Playthrough (même table).
|
||||
* On ne mélange jamais les EVENTs de tables différentes.
|
||||
*/
|
||||
private List<JournalEntrySummary> loadPreviousEvents(Session current) {
|
||||
if (current.getPlaythroughId() == null) return List.of();
|
||||
List<Session> siblingSessions = sessionRepository.findByPlaythroughId(current.getPlaythroughId());
|
||||
List<JournalEntrySummary> events = new ArrayList<>();
|
||||
|
||||
for (Session past : siblingSessions) {
|
||||
if (past.getId().equals(current.getId())) continue;
|
||||
for (SessionEntry entry : entryRepository.findBySessionId(past.getId())) {
|
||||
if (entry.getType() == EntryType.EVENT) {
|
||||
events.add(toSummary(entry, past.getName()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
events.sort(Comparator.comparing(
|
||||
JournalEntrySummary::occurredAt,
|
||||
Comparator.nullsLast(Comparator.naturalOrder())));
|
||||
|
||||
if (events.size() > MAX_PREVIOUS_EVENTS) {
|
||||
return events.subList(events.size() - MAX_PREVIOUS_EVENTS, events.size());
|
||||
}
|
||||
return events;
|
||||
}
|
||||
|
||||
private JournalEntrySummary toSummary(SessionEntry entry, String sourceSessionName) {
|
||||
return new JournalEntrySummary(
|
||||
entry.getType() != null ? entry.getType().name() : "NOTE",
|
||||
entry.getContent(),
|
||||
entry.getOccurredAt(),
|
||||
sourceSessionName);
|
||||
}
|
||||
|
||||
/** Agrégat interne des données Hub à injecter dans le SessionContext. */
|
||||
private record HubStatus(
|
||||
List<QuestSummary> available,
|
||||
List<QuestSummary> inProgress,
|
||||
List<String> lockedTitles,
|
||||
List<String> activeFlags
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Calcule l'état des quêtes Hub du Playthrough courant :
|
||||
* - AVAILABLE / IN_PROGRESS → résumé complet
|
||||
* - LOCKED → titre uniquement (anti-spoiler)
|
||||
* - COMPLETED → omis (déjà raconté par les EVENTs)
|
||||
*/
|
||||
private HubStatus computeHubStatus(String playthroughId) {
|
||||
if (playthroughId == null) {
|
||||
return new HubStatus(List.of(), List.of(), List.of(), List.of());
|
||||
}
|
||||
Optional<Playthrough> maybePlaythrough = playthroughRepository.findById(playthroughId);
|
||||
if (maybePlaythrough.isEmpty()) {
|
||||
return new HubStatus(List.of(), List.of(), List.of(), List.of());
|
||||
}
|
||||
String campaignId = maybePlaythrough.get().getCampaignId();
|
||||
|
||||
Map<String, Boolean> flags = playthroughFlagRepository.findByPlaythroughId(playthroughId);
|
||||
List<String> activeFlags = buildActiveFlags(flags);
|
||||
|
||||
List<Arc> arcs = arcRepository.findByCampaignId(campaignId);
|
||||
Map<String, Arc> arcsById = arcs.stream()
|
||||
.filter(a -> a.getId() != null)
|
||||
.collect(Collectors.toMap(Arc::getId, a -> a));
|
||||
|
||||
boolean anyHub = arcs.stream().anyMatch(a -> a.getType() == ArcType.HUB);
|
||||
if (!anyHub) {
|
||||
return new HubStatus(List.of(), List.of(), List.of(), activeFlags);
|
||||
}
|
||||
|
||||
// Map chapterId -> ProgressionStatus pour ce Playthrough
|
||||
Map<String, ProgressionStatus> progressionByChapter = new HashMap<>();
|
||||
for (QuestProgression qp : questProgressionRepository.findByPlaythroughId(playthroughId)) {
|
||||
progressionByChapter.put(qp.getChapterId(), qp.getStatus());
|
||||
}
|
||||
|
||||
// IDs des chapitres COMPLETED dans la campagne (pour les prérequis QuestCompleted)
|
||||
var completedIds = questProgressionRepository.findCompletedChapterIdsByPlaythroughId(playthroughId);
|
||||
|
||||
int sessionCount = sessionRepository.findByPlaythroughId(playthroughId).size();
|
||||
PrerequisiteEvaluator.EvaluationContext ctx =
|
||||
new PrerequisiteEvaluator.EvaluationContext(completedIds, sessionCount, flags);
|
||||
|
||||
List<QuestSummary> available = new ArrayList<>();
|
||||
List<QuestSummary> inProgress = new ArrayList<>();
|
||||
List<String> lockedTitles = new ArrayList<>();
|
||||
|
||||
for (Arc arc : arcs) {
|
||||
if (arc.getType() != ArcType.HUB) continue;
|
||||
for (Chapter c : chapterRepository.findByArcId(arc.getId())) {
|
||||
ProgressionStatus prog = progressionByChapter.getOrDefault(c.getId(), ProgressionStatus.NOT_STARTED);
|
||||
QuestStatus status = prerequisiteEvaluator.computeStatus(prog, c.getPrerequisites(), ctx);
|
||||
Arc parent = arcsById.get(c.getArcId());
|
||||
String arcName = parent != null ? parent.getName() : null;
|
||||
switch (status) {
|
||||
case AVAILABLE:
|
||||
available.add(new QuestSummary(c.getName(), arcName, c.getDescription()));
|
||||
break;
|
||||
case IN_PROGRESS:
|
||||
inProgress.add(new QuestSummary(c.getName(), arcName, c.getDescription()));
|
||||
break;
|
||||
case LOCKED:
|
||||
lockedTitles.add(c.getName());
|
||||
break;
|
||||
case COMPLETED:
|
||||
// Omis (déjà dans le journal des EVENTs).
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new HubStatus(available, inProgress, lockedTitles, activeFlags);
|
||||
}
|
||||
|
||||
private List<String> buildActiveFlags(Map<String, Boolean> flags) {
|
||||
return flags.entrySet().stream()
|
||||
.filter(Map.Entry::getValue)
|
||||
.map(Map.Entry::getKey)
|
||||
.sorted()
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,14 @@
|
||||
package com.loremind.application.generationcontext;
|
||||
|
||||
import com.loremind.application.gamesystemcontext.GameSystemContextBuilder;
|
||||
import com.loremind.domain.campaigncontext.Campaign;
|
||||
import com.loremind.domain.campaigncontext.ports.CampaignRepository;
|
||||
import com.loremind.domain.gamesystemcontext.GenerationIntent;
|
||||
import com.loremind.domain.generationcontext.CampaignStructuralContext;
|
||||
import com.loremind.domain.generationcontext.ChatMessage;
|
||||
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.NarrativeEntityContext;
|
||||
import com.loremind.domain.generationcontext.ports.AiChatProvider;
|
||||
@@ -34,6 +37,7 @@ public class StreamChatForCampaignUseCase {
|
||||
private final CampaignStructuralContextBuilder campaignContextBuilder;
|
||||
private final LoreStructuralContextBuilder loreContextBuilder;
|
||||
private final NarrativeEntityContextBuilder narrativeEntityContextBuilder;
|
||||
private final GameSystemContextBuilder gameSystemContextBuilder;
|
||||
private final AiChatProvider aiChatProvider;
|
||||
|
||||
public StreamChatForCampaignUseCase(
|
||||
@@ -41,11 +45,13 @@ public class StreamChatForCampaignUseCase {
|
||||
CampaignStructuralContextBuilder campaignContextBuilder,
|
||||
LoreStructuralContextBuilder loreContextBuilder,
|
||||
NarrativeEntityContextBuilder narrativeEntityContextBuilder,
|
||||
GameSystemContextBuilder gameSystemContextBuilder,
|
||||
AiChatProvider aiChatProvider) {
|
||||
this.campaignRepository = campaignRepository;
|
||||
this.campaignContextBuilder = campaignContextBuilder;
|
||||
this.loreContextBuilder = loreContextBuilder;
|
||||
this.narrativeEntityContextBuilder = narrativeEntityContextBuilder;
|
||||
this.gameSystemContextBuilder = gameSystemContextBuilder;
|
||||
this.aiChatProvider = aiChatProvider;
|
||||
}
|
||||
|
||||
@@ -78,12 +84,14 @@ public class StreamChatForCampaignUseCase {
|
||||
CampaignStructuralContext campaignContext = campaignContextBuilder.build(campaignId);
|
||||
LoreStructuralContext loreContext = loadLinkedLoreContextOrNull(campaign);
|
||||
NarrativeEntityContext narrativeEntity = buildNarrativeEntityOrNull(entityType, entityId);
|
||||
GameSystemContext gameSystemContext = loadGameSystemContextOrNull(campaign, entityType);
|
||||
|
||||
ChatRequest request = ChatRequest.builder()
|
||||
.messages(messages)
|
||||
.loreContext(loreContext)
|
||||
.campaignContext(campaignContext)
|
||||
.narrativeEntity(narrativeEntity)
|
||||
.gameSystemContext(gameSystemContext)
|
||||
.build();
|
||||
|
||||
aiChatProvider.streamChat(request, onUsage, onToken, onComplete, onError);
|
||||
@@ -104,4 +112,16 @@ public class StreamChatForCampaignUseCase {
|
||||
if (entityId == null || entityId.isBlank()) return null;
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -107,11 +107,6 @@ public class StreamChatForLoreUseCase {
|
||||
? page.getValues()
|
||||
: Collections.emptyMap();
|
||||
|
||||
return PageContext.builder()
|
||||
.title(page.getTitle())
|
||||
.templateName(templateName)
|
||||
.templateFields(templateFields)
|
||||
.values(values)
|
||||
.build();
|
||||
return new PageContext(page.getTitle(), templateName, templateFields, values);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,122 @@
|
||||
package com.loremind.application.generationcontext;
|
||||
|
||||
import com.loremind.application.gamesystemcontext.GameSystemContextBuilder;
|
||||
import com.loremind.domain.campaigncontext.Campaign;
|
||||
import com.loremind.domain.campaigncontext.ports.CampaignRepository;
|
||||
import com.loremind.domain.gamesystemcontext.GenerationIntent;
|
||||
import com.loremind.domain.generationcontext.CampaignStructuralContext;
|
||||
import com.loremind.domain.generationcontext.ChatMessage;
|
||||
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.SessionContext;
|
||||
import com.loremind.domain.generationcontext.ports.AiChatProvider;
|
||||
import com.loremind.domain.playcontext.Playthrough;
|
||||
import com.loremind.domain.playcontext.Session;
|
||||
import com.loremind.domain.playcontext.ports.PlaythroughRepository;
|
||||
import com.loremind.domain.playcontext.ports.SessionRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* Use case applicatif : chat IA pendant une Session de jeu.
|
||||
* <p>
|
||||
* Orchestre la composition des contextes :
|
||||
* 1. Charge la Session puis la Campagne associée (weak reference).
|
||||
* 2. Construit le CampaignStructuralContext (carte narrative + PJ/PNJ).
|
||||
* 3. Construit le LoreStructuralContext si la campagne est liée à un Lore.
|
||||
* 4. Construit le GameSystemContext si elle a un système de JDR.
|
||||
* 5. Construit le SessionContext (journal horodaté, statut).
|
||||
* 6. Délègue au port {@link AiChatProvider} pour le streaming.
|
||||
* </p>
|
||||
*
|
||||
* <p>La conversation est éphémère (pas de persistance) : pendant une partie,
|
||||
* l'utilité est d'avoir une assistance immédiate, pas de garder un historique.
|
||||
* Le journal de session joue déjà ce rôle de mémoire persistante.</p>
|
||||
*/
|
||||
@Service
|
||||
public class StreamChatForSessionUseCase {
|
||||
|
||||
private final SessionRepository sessionRepository;
|
||||
private final PlaythroughRepository playthroughRepository;
|
||||
private final CampaignRepository campaignRepository;
|
||||
private final CampaignStructuralContextBuilder campaignContextBuilder;
|
||||
private final LoreStructuralContextBuilder loreContextBuilder;
|
||||
private final GameSystemContextBuilder gameSystemContextBuilder;
|
||||
private final SessionStructuralContextBuilder sessionContextBuilder;
|
||||
private final AiChatProvider aiChatProvider;
|
||||
|
||||
public StreamChatForSessionUseCase(
|
||||
SessionRepository sessionRepository,
|
||||
PlaythroughRepository playthroughRepository,
|
||||
CampaignRepository campaignRepository,
|
||||
CampaignStructuralContextBuilder campaignContextBuilder,
|
||||
LoreStructuralContextBuilder loreContextBuilder,
|
||||
GameSystemContextBuilder gameSystemContextBuilder,
|
||||
SessionStructuralContextBuilder sessionContextBuilder,
|
||||
AiChatProvider aiChatProvider) {
|
||||
this.sessionRepository = sessionRepository;
|
||||
this.playthroughRepository = playthroughRepository;
|
||||
this.campaignRepository = campaignRepository;
|
||||
this.campaignContextBuilder = campaignContextBuilder;
|
||||
this.loreContextBuilder = loreContextBuilder;
|
||||
this.gameSystemContextBuilder = gameSystemContextBuilder;
|
||||
this.sessionContextBuilder = sessionContextBuilder;
|
||||
this.aiChatProvider = aiChatProvider;
|
||||
}
|
||||
|
||||
public void execute(
|
||||
String sessionId,
|
||||
List<ChatMessage> messages,
|
||||
Consumer<ChatUsage> onUsage,
|
||||
Consumer<String> onToken,
|
||||
Runnable onComplete,
|
||||
Consumer<Throwable> onError) {
|
||||
|
||||
Session session = sessionRepository.findById(sessionId)
|
||||
.orElseThrow(() -> new IllegalArgumentException("Session introuvable : " + sessionId));
|
||||
|
||||
// Chaîne de résolution : Session → Playthrough → Campaign.
|
||||
Playthrough playthrough = playthroughRepository.findById(session.getPlaythroughId())
|
||||
.orElseThrow(() -> new IllegalArgumentException(
|
||||
"Partie associée à la session introuvable : " + session.getPlaythroughId()));
|
||||
|
||||
Campaign campaign = campaignRepository.findById(playthrough.getCampaignId())
|
||||
.orElseThrow(() -> new IllegalArgumentException(
|
||||
"Campagne associée à la partie introuvable : " + playthrough.getCampaignId()));
|
||||
|
||||
// Le campaign context inclut les PJ de CE Playthrough (les PJ sont par-table).
|
||||
CampaignStructuralContext campaignContext = campaignContextBuilder.build(campaign.getId(), playthrough.getId());
|
||||
LoreStructuralContext loreContext = loadLoreContextOrNull(campaign);
|
||||
GameSystemContext gameSystemContext = loadGameSystemContextOrNull(campaign);
|
||||
SessionContext sessionContext = sessionContextBuilder.build(sessionId);
|
||||
|
||||
ChatRequest request = ChatRequest.builder()
|
||||
.messages(messages)
|
||||
.loreContext(loreContext)
|
||||
.campaignContext(campaignContext)
|
||||
.gameSystemContext(gameSystemContext)
|
||||
.sessionContext(sessionContext)
|
||||
.build();
|
||||
|
||||
aiChatProvider.streamChat(request, onUsage, onToken, onComplete, onError);
|
||||
}
|
||||
|
||||
private LoreStructuralContext loadLoreContextOrNull(Campaign campaign) {
|
||||
if (!campaign.isLinkedToLore()) return null;
|
||||
return loreContextBuilder.buildOptional(campaign.getLoreId()).orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Pendant une session active, on injecte les sections les plus utiles en partie
|
||||
* (combats, PNJ, mécaniques) — intent SCENE est le plus proche de ce besoin.
|
||||
*/
|
||||
private GameSystemContext loadGameSystemContextOrNull(Campaign campaign) {
|
||||
if (!campaign.isLinkedToGameSystem()) return null;
|
||||
return gameSystemContextBuilder.buildOptional(campaign.getGameSystemId(), GenerationIntent.SCENE)
|
||||
.orElse(null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
package com.loremind.application.licensing;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.time.Instant;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Orchestre la bascule de canal stable <-> beta via le sidecar `switcher`.
|
||||
*
|
||||
* <p>Le sidecar tourne en permanence et watch un fichier {@code command.json}
|
||||
* dans un volume partage. Quand on depose une commande, il :
|
||||
* <ol>
|
||||
* <li>Sed la ligne IMAGE_NAMESPACE du .env</li>
|
||||
* <li>Lance docker compose pull + up -d</li>
|
||||
* <li>Ecrit son resultat dans {@code result.json}</li>
|
||||
* </ol>
|
||||
*
|
||||
* <p>Le Core n'a PAS acces au socket Docker — il delegue tout au sidecar
|
||||
* via fichiers, ce qui evite que la compromission du Core ne donne RCE
|
||||
* sur l'hote. Le sidecar valide strictement le contenu de la commande
|
||||
* (channel ∈ {stable, beta} uniquement).
|
||||
*
|
||||
* <p>Le canal actuel se deduit du prefixe d'image courant (recupere via
|
||||
* la variable d'env {@code IMAGE_NAMESPACE} ou {@code UPDATE_CHECK_IMAGES}) :
|
||||
* presence de "loremind-beta-" => canal beta, sinon stable.
|
||||
*/
|
||||
@Service
|
||||
public class ChannelSwitcherService {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(ChannelSwitcherService.class);
|
||||
|
||||
public enum Channel { STABLE, BETA }
|
||||
|
||||
public enum SwitchStatus { IN_PROGRESS, SUCCESS, ERROR }
|
||||
|
||||
/** Snapshot du dernier resultat de switch ecrit par le sidecar. */
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
public record SwitchResult(
|
||||
String id,
|
||||
SwitchStatus status,
|
||||
Channel channel,
|
||||
String message,
|
||||
Instant completedAt) {}
|
||||
|
||||
private final Path switcherDataPath;
|
||||
private final String imageNamespace;
|
||||
private final ObjectMapper json = new ObjectMapper();
|
||||
|
||||
public ChannelSwitcherService(
|
||||
@Value("${SWITCHER_DATA_PATH:/shared/switcher}") String switcherDataPath,
|
||||
// On lit IMAGE_NAMESPACE en priorite, puis UPDATE_CHECK_IMAGES en fallback
|
||||
// (la deuxieme est toujours injectee par compose, contrairement a la premiere
|
||||
// qui peut etre absente dans les .env legacy).
|
||||
@Value("${IMAGE_NAMESPACE:${UPDATE_CHECK_IMAGES:}}") String imageNamespaceRaw) {
|
||||
this.switcherDataPath = Path.of(switcherDataPath);
|
||||
this.imageNamespace = imageNamespaceRaw != null ? imageNamespaceRaw : "";
|
||||
log.info("ChannelSwitcherService initialized: dataPath={} imageNamespace={}",
|
||||
switcherDataPath, this.imageNamespace);
|
||||
}
|
||||
|
||||
/**
|
||||
* Detection du canal courant a partir du prefixe d'image charge au demarrage.
|
||||
* Pas de magie : si le namespace contient "beta-" on est en beta, sinon stable.
|
||||
*/
|
||||
public Channel getCurrentChannel() {
|
||||
return imageNamespace.contains("loremind-beta-") ? Channel.BETA : Channel.STABLE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indique si le sidecar est disponible (volume partage accessible).
|
||||
* Si non, on degrade en lecture seule (l'UI affichera l'ancien message
|
||||
* avec instructions manuelles).
|
||||
*/
|
||||
public boolean isSwitcherAvailable() {
|
||||
return Files.isDirectory(switcherDataPath) && Files.isWritable(switcherDataPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* Depose une commande de switch dans le volume partage. Renvoie l'ID
|
||||
* de la commande, que le client peut utiliser pour poller le status.
|
||||
*
|
||||
* @throws IllegalStateException si le sidecar n'est pas disponible
|
||||
* @throws IOException si l'ecriture du fichier echoue
|
||||
*/
|
||||
public String requestSwitch(Channel target) throws IOException {
|
||||
if (!isSwitcherAvailable()) {
|
||||
throw new IllegalStateException("Switcher sidecar not available (volume mount missing)");
|
||||
}
|
||||
String id = UUID.randomUUID().toString();
|
||||
Map<String, Object> command = new LinkedHashMap<>();
|
||||
command.put("id", id);
|
||||
command.put("channel", target.name().toLowerCase());
|
||||
command.put("requestedAt", Instant.now().toString());
|
||||
|
||||
Path commandFile = switcherDataPath.resolve("command.json");
|
||||
Path tmp = Files.createTempFile(switcherDataPath, "command-", ".tmp");
|
||||
try {
|
||||
json.writerWithDefaultPrettyPrinter().writeValue(tmp.toFile(), command);
|
||||
// Atomic move : evite que le sidecar lise un fichier partiellement ecrit.
|
||||
Files.move(tmp, commandFile, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE);
|
||||
} finally {
|
||||
// Cleanup au cas ou move aurait echoue avant le rename.
|
||||
Files.deleteIfExists(tmp);
|
||||
}
|
||||
log.info("Switch command written: id={} channel={}", id, target);
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lit le dernier resultat ecrit par le sidecar, s'il existe.
|
||||
* Renvoie null si aucun switch n'a encore ete tente sur cette instance.
|
||||
*/
|
||||
public SwitchResult getLastResult() {
|
||||
Path resultFile = switcherDataPath.resolve("result.json");
|
||||
if (!Files.exists(resultFile)) return null;
|
||||
try {
|
||||
return json.readValue(resultFile.toFile(), SwitchResult.class);
|
||||
} catch (IOException e) {
|
||||
log.warn("Cannot parse switcher result.json: {}", e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,261 @@
|
||||
package com.loremind.application.licensing;
|
||||
|
||||
import com.loremind.domain.licensing.License;
|
||||
import com.loremind.domain.licensing.LicenseClaims;
|
||||
import com.loremind.domain.licensing.LicenseSnapshot;
|
||||
import com.loremind.domain.licensing.LicenseStatus;
|
||||
import com.loremind.domain.licensing.RegistryCredentials;
|
||||
import com.loremind.domain.licensing.ports.JwtVerifier;
|
||||
import com.loremind.domain.licensing.ports.LicenseRelay;
|
||||
import com.loremind.domain.licensing.ports.LicenseRepository;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Service application pour la gestion de la licence Patreon.
|
||||
* <p>
|
||||
* Responsabilites :
|
||||
* <ul>
|
||||
* <li>Installer un nouveau JWT recu du relais (apres OAuth utilisateur)</li>
|
||||
* <li>Calculer le {@link LicenseStatus} courant en respectant la grace period</li>
|
||||
* <li>Renouveler le JWT avant expiration en appelant le relais</li>
|
||||
* <li>Activer/desactiver le toggle "canal beta" cote utilisateur</li>
|
||||
* <li>Distribuer les credentials registry pour le pull beta</li>
|
||||
* </ul>
|
||||
*/
|
||||
@Service
|
||||
public class LicenseService {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(LicenseService.class);
|
||||
|
||||
private final LicenseRepository repository;
|
||||
private final JwtVerifier jwtVerifier;
|
||||
private final LicenseRelay relay;
|
||||
private final long gracePeriodSeconds;
|
||||
private final long refreshBeforeExpirySeconds;
|
||||
|
||||
public LicenseService(
|
||||
LicenseRepository repository,
|
||||
JwtVerifier jwtVerifier,
|
||||
LicenseRelay relay,
|
||||
@Value("${licensing.grace-period-days:14}") int gracePeriodDays,
|
||||
@Value("${licensing.refresh-before-expiry-days:2}") int refreshBeforeExpiryDays) {
|
||||
this.repository = repository;
|
||||
this.jwtVerifier = jwtVerifier;
|
||||
this.relay = relay;
|
||||
this.gracePeriodSeconds = (long) gracePeriodDays * 86_400L;
|
||||
this.refreshBeforeExpirySeconds = (long) refreshBeforeExpiryDays * 86_400L;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return true si le verifier est configure (cle publique presente).
|
||||
* L'UI peut masquer toute la section Patreon si false.
|
||||
*/
|
||||
public boolean isLicensingEnabled() {
|
||||
return jwtVerifier.isConfigured();
|
||||
}
|
||||
|
||||
/**
|
||||
* Genere ou retourne l'instance_id stable de cette installation.
|
||||
* Stocke dans la licence elle-meme. Si pas de licence, en cree un volatil
|
||||
* (sera persiste a la prochaine connexion).
|
||||
*/
|
||||
public String getOrCreateInstanceId() {
|
||||
return repository.findCurrent()
|
||||
.map(License::getInstanceId)
|
||||
.orElseGet(() -> "li-" + UUID.randomUUID());
|
||||
}
|
||||
|
||||
/**
|
||||
* Construit l'URL OAuth pour ouvrir dans le navigateur de l'utilisateur.
|
||||
*/
|
||||
public String buildConnectUrl() {
|
||||
return relay.buildConnectUrl(getOrCreateInstanceId());
|
||||
}
|
||||
|
||||
/**
|
||||
* Installe un JWT recu du relais (l'utilisateur l'a colle dans l'UI ou
|
||||
* recu via deep-link). Verifie la signature, extrait les claims, persiste.
|
||||
*/
|
||||
public LicenseSnapshot installToken(String rawJwt) throws InstallException {
|
||||
if (!jwtVerifier.isConfigured()) {
|
||||
throw new InstallException("Licensing feature not enabled (no public key configured)");
|
||||
}
|
||||
LicenseClaims claims;
|
||||
try {
|
||||
claims = jwtVerifier.verify(rawJwt);
|
||||
} catch (JwtVerifier.JwtVerificationException e) {
|
||||
throw new InstallException("Invalid JWT: " + e.getMessage());
|
||||
}
|
||||
|
||||
Instant now = Instant.now();
|
||||
if (claims.expiresAt().isBefore(now)) {
|
||||
throw new InstallException("JWT already expired");
|
||||
}
|
||||
|
||||
Optional<License> existing = repository.findCurrent();
|
||||
License toSave = License.builder()
|
||||
.id("current")
|
||||
.rawJwt(rawJwt)
|
||||
.patreonUserId(claims.subject())
|
||||
.tierId(claims.tierId())
|
||||
.instanceId(claims.instanceId())
|
||||
.issuedAt(claims.issuedAt())
|
||||
.expiresAt(claims.expiresAt())
|
||||
.lastRefreshAttemptAt(now)
|
||||
.lastRefreshSucceeded(true)
|
||||
// Au premier install, on active le canal beta par defaut.
|
||||
// Sur reinstall apres deconnexion, on respecte la valeur precedente.
|
||||
.betaChannelEnabled(existing.map(License::isBetaChannelEnabled).orElse(true))
|
||||
.createdAt(existing.map(License::getCreatedAt).orElse(now))
|
||||
.build();
|
||||
|
||||
License saved = repository.save(toSave);
|
||||
log.info("Patreon license installed for user={} tier={} expires={}",
|
||||
saved.getPatreonUserId(), saved.getTierId(), saved.getExpiresAt());
|
||||
return snapshotOf(saved, now);
|
||||
}
|
||||
|
||||
/**
|
||||
* Etat courant de la licence pour exposition UI / decision technique.
|
||||
*/
|
||||
public LicenseSnapshot getCurrentSnapshot() {
|
||||
Optional<License> opt = repository.findCurrent();
|
||||
if (opt.isEmpty()) return LicenseSnapshot.none();
|
||||
return snapshotOf(opt.get(), Instant.now());
|
||||
}
|
||||
|
||||
/**
|
||||
* Supprime la licence (deconnexion volontaire de Patreon par l'utilisateur).
|
||||
*/
|
||||
public void disconnect() {
|
||||
repository.deleteCurrent();
|
||||
log.info("Patreon license removed (user disconnect)");
|
||||
}
|
||||
|
||||
/**
|
||||
* Active ou desactive le canal beta. Necessite une licence valide ou en grace.
|
||||
*/
|
||||
public LicenseSnapshot setBetaChannelEnabled(boolean enabled) {
|
||||
License current = repository.findCurrent()
|
||||
.orElseThrow(() -> new IllegalStateException("No license installed"));
|
||||
current.setBetaChannelEnabled(enabled);
|
||||
License saved = repository.save(current);
|
||||
return snapshotOf(saved, Instant.now());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tente un refresh si la licence est proche de l'expiration. Idempotent.
|
||||
* Appele par le daemon planifie + manuellement via l'UI ("Reessayer").
|
||||
*
|
||||
* @return true si un refresh a ete tente (avec ou sans succes)
|
||||
*/
|
||||
public boolean refreshIfNeeded() {
|
||||
Optional<License> opt = repository.findCurrent();
|
||||
if (opt.isEmpty()) return false;
|
||||
License current = opt.get();
|
||||
Instant now = Instant.now();
|
||||
long secondsUntilExpiry = Duration.between(now, current.getExpiresAt()).getSeconds();
|
||||
if (secondsUntilExpiry > refreshBeforeExpirySeconds) {
|
||||
return false;
|
||||
}
|
||||
return doRefresh(current, now);
|
||||
}
|
||||
|
||||
/**
|
||||
* Force un refresh immediat (bouton UI "Reessayer maintenant").
|
||||
*/
|
||||
public boolean forceRefresh() {
|
||||
return repository.findCurrent()
|
||||
.map(license -> doRefresh(license, Instant.now()))
|
||||
.orElse(false);
|
||||
}
|
||||
|
||||
private boolean doRefresh(License current, Instant now) {
|
||||
log.info("Refreshing Patreon license (current expires {})", current.getExpiresAt());
|
||||
try {
|
||||
String newJwt = relay.refreshToken(current.getRawJwt());
|
||||
LicenseClaims claims = jwtVerifier.verify(newJwt);
|
||||
|
||||
current.setRawJwt(newJwt);
|
||||
current.setIssuedAt(claims.issuedAt());
|
||||
current.setExpiresAt(claims.expiresAt());
|
||||
current.setTierId(claims.tierId());
|
||||
current.setLastRefreshAttemptAt(now);
|
||||
current.setLastRefreshSucceeded(true);
|
||||
repository.save(current);
|
||||
log.info("License refreshed successfully (new expiry {})", claims.expiresAt());
|
||||
return true;
|
||||
} catch (LicenseRelay.RelayException e) {
|
||||
current.setLastRefreshAttemptAt(now);
|
||||
current.setLastRefreshSucceeded(false);
|
||||
repository.save(current);
|
||||
if (e.getKind() == LicenseRelay.RelayErrorKind.REJECTED) {
|
||||
log.warn("Relay rejected refresh ({}): tier may have been cancelled", e.getMessage());
|
||||
} else {
|
||||
log.warn("Relay refresh transient failure ({}): {}", e.getKind(), e.getMessage());
|
||||
}
|
||||
return true;
|
||||
} catch (JwtVerifier.JwtVerificationException e) {
|
||||
current.setLastRefreshAttemptAt(now);
|
||||
current.setLastRefreshSucceeded(false);
|
||||
repository.save(current);
|
||||
log.error("Relay returned a JWT that fails verification: {}", e.getMessage());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Recupere les credentials registry pour pull du canal beta.
|
||||
* @return empty si pas de licence valide ou relais en echec
|
||||
*/
|
||||
public Optional<RegistryCredentials> fetchRegistryCredentials() {
|
||||
LicenseSnapshot snap = getCurrentSnapshot();
|
||||
if (snap.status() != LicenseStatus.VALID && snap.status() != LicenseStatus.GRACE) {
|
||||
return Optional.empty();
|
||||
}
|
||||
License current = repository.findCurrent().orElse(null);
|
||||
if (current == null) return Optional.empty();
|
||||
try {
|
||||
return Optional.of(relay.fetchRegistryCredentials(current.getRawJwt()));
|
||||
} catch (LicenseRelay.RelayException e) {
|
||||
log.warn("Cannot fetch registry credentials ({}): {}", e.getKind(), e.getMessage());
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
private LicenseSnapshot snapshotOf(License l, Instant now) {
|
||||
LicenseStatus status = computeStatus(l, now);
|
||||
return new LicenseSnapshot(
|
||||
status,
|
||||
l.getPatreonUserId(),
|
||||
l.getTierId(),
|
||||
l.getInstanceId(),
|
||||
l.getExpiresAt(),
|
||||
l.getLastRefreshAttemptAt(),
|
||||
l.isLastRefreshSucceeded(),
|
||||
l.isBetaChannelEnabled()
|
||||
);
|
||||
}
|
||||
|
||||
private LicenseStatus computeStatus(License l, Instant now) {
|
||||
if (l.getExpiresAt() == null) return LicenseStatus.NONE;
|
||||
if (now.isBefore(l.getExpiresAt())) return LicenseStatus.VALID;
|
||||
long secondsPastExpiry = Duration.between(l.getExpiresAt(), now).getSeconds();
|
||||
if (secondsPastExpiry <= gracePeriodSeconds) return LicenseStatus.GRACE;
|
||||
return LicenseStatus.EXPIRED;
|
||||
}
|
||||
|
||||
public static class InstallException extends Exception {
|
||||
public InstallException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,13 @@
|
||||
package com.loremind.application.lorecontext;
|
||||
|
||||
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.PageRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@@ -16,11 +20,20 @@ import java.util.Optional;
|
||||
public class LoreNodeService {
|
||||
|
||||
private final LoreNodeRepository loreNodeRepository;
|
||||
private final PageRepository pageRepository;
|
||||
|
||||
public LoreNodeService(LoreNodeRepository loreNodeRepository) {
|
||||
public LoreNodeService(LoreNodeRepository loreNodeRepository, PageRepository pageRepository) {
|
||||
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
|
||||
* souhaitées (pattern Parameter Object) : évite les signatures qui gonflent
|
||||
@@ -68,7 +81,64 @@ public class LoreNodeService {
|
||||
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) {
|
||||
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);
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
import com.loremind.domain.campaigncontext.Campaign;
|
||||
import com.loremind.domain.campaigncontext.ports.CampaignRepository;
|
||||
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.LoreRepository;
|
||||
import com.loremind.domain.lorecontext.ports.PageRepository;
|
||||
import com.loremind.domain.lorecontext.ports.TemplateRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
@@ -26,15 +33,28 @@ public class LoreService {
|
||||
private final LoreRepository loreRepository;
|
||||
private final LoreNodeRepository loreNodeRepository;
|
||||
private final PageRepository pageRepository;
|
||||
private final TemplateRepository templateRepository;
|
||||
private final CampaignRepository campaignRepository;
|
||||
|
||||
public LoreService(LoreRepository loreRepository,
|
||||
LoreNodeRepository loreNodeRepository,
|
||||
PageRepository pageRepository) {
|
||||
PageRepository pageRepository,
|
||||
TemplateRepository templateRepository,
|
||||
CampaignRepository campaignRepository) {
|
||||
this.loreRepository = loreRepository;
|
||||
this.loreNodeRepository = loreNodeRepository;
|
||||
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) {
|
||||
Lore lore = Lore.builder()
|
||||
.name(name)
|
||||
@@ -83,7 +103,54 @@ public class LoreService {
|
||||
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) {
|
||||
// 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);
|
||||
}
|
||||
|
||||
private int countCampaignsReferencingLore(String id) {
|
||||
int count = 0;
|
||||
for (Campaign campaign : campaignRepository.findAll()) {
|
||||
if (id.equals(campaign.getLoreId())) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.loremind.application.lorecontext;
|
||||
|
||||
import com.loremind.domain.lorecontext.Template;
|
||||
import com.loremind.domain.lorecontext.TemplateField;
|
||||
import com.loremind.domain.shared.template.TemplateField;
|
||||
import com.loremind.domain.lorecontext.ports.TemplateRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
package com.loremind.application.playcontext;
|
||||
|
||||
import com.loremind.domain.campaigncontext.ports.CampaignRepository;
|
||||
import com.loremind.domain.campaigncontext.ports.CharacterRepository;
|
||||
import com.loremind.domain.playcontext.Playthrough;
|
||||
import com.loremind.domain.playcontext.Session;
|
||||
import com.loremind.domain.playcontext.ports.PlaythroughFlagRepository;
|
||||
import com.loremind.domain.playcontext.ports.PlaythroughRepository;
|
||||
import com.loremind.domain.playcontext.ports.QuestProgressionRepository;
|
||||
import com.loremind.domain.playcontext.ports.SessionRepository;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Service applicatif pour le cycle de vie d'un Playthrough (Partie).
|
||||
*
|
||||
* <p>Cascade de suppression : un Playthrough supprime ses flags, ses progressions,
|
||||
* ses PJ et ses sessions (avec leurs entrées de journal).</p>
|
||||
*/
|
||||
@Service
|
||||
public class PlaythroughService {
|
||||
|
||||
private final PlaythroughRepository playthroughRepository;
|
||||
private final CampaignRepository campaignRepository;
|
||||
private final PlaythroughFlagRepository flagRepository;
|
||||
private final QuestProgressionRepository progressionRepository;
|
||||
private final CharacterRepository characterRepository;
|
||||
private final SessionRepository sessionRepository;
|
||||
private final SessionService sessionService;
|
||||
|
||||
public PlaythroughService(PlaythroughRepository playthroughRepository,
|
||||
CampaignRepository campaignRepository,
|
||||
PlaythroughFlagRepository flagRepository,
|
||||
QuestProgressionRepository progressionRepository,
|
||||
CharacterRepository characterRepository,
|
||||
SessionRepository sessionRepository,
|
||||
SessionService sessionService) {
|
||||
this.playthroughRepository = playthroughRepository;
|
||||
this.campaignRepository = campaignRepository;
|
||||
this.flagRepository = flagRepository;
|
||||
this.progressionRepository = progressionRepository;
|
||||
this.characterRepository = characterRepository;
|
||||
this.sessionRepository = sessionRepository;
|
||||
this.sessionService = sessionService;
|
||||
}
|
||||
|
||||
/** Compte des entités qui seront supprimées en cascade avec la Partie. */
|
||||
public record DeletionImpact(int sessions, int characters, int flags, int progressions) {}
|
||||
|
||||
public Playthrough create(String campaignId, String name, String description) {
|
||||
if (campaignId == null || campaignId.isBlank()) {
|
||||
throw new IllegalArgumentException("campaignId requis.");
|
||||
}
|
||||
if (!campaignRepository.existsById(campaignId)) {
|
||||
throw new IllegalArgumentException("Campagne introuvable : " + campaignId);
|
||||
}
|
||||
Playthrough p = Playthrough.builder()
|
||||
.campaignId(campaignId)
|
||||
.name((name == null || name.isBlank()) ? "Partie principale" : name.trim())
|
||||
.description(description)
|
||||
.build();
|
||||
return playthroughRepository.save(p);
|
||||
}
|
||||
|
||||
public Playthrough update(String id, Playthrough updated) {
|
||||
Playthrough existing = playthroughRepository.findById(id)
|
||||
.orElseThrow(() -> new IllegalArgumentException("Partie introuvable : " + id));
|
||||
BeanUtils.copyProperties(updated, existing, "id", "campaignId", "createdAt");
|
||||
return playthroughRepository.save(existing);
|
||||
}
|
||||
|
||||
public Optional<Playthrough> getById(String id) {
|
||||
return playthroughRepository.findById(id);
|
||||
}
|
||||
|
||||
public List<Playthrough> getByCampaignId(String campaignId) {
|
||||
return playthroughRepository.findByCampaignId(campaignId);
|
||||
}
|
||||
|
||||
public DeletionImpact getDeletionImpact(String id) {
|
||||
int sessions = sessionRepository.findByPlaythroughId(id).size();
|
||||
int characters = characterRepository.findByPlaythroughId(id).size();
|
||||
int flags = flagRepository.findByPlaythroughId(id).size();
|
||||
int progressions = progressionRepository.findByPlaythroughId(id).size();
|
||||
return new DeletionImpact(sessions, characters, flags, progressions);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void delete(String id) {
|
||||
if (!playthroughRepository.existsById(id)) {
|
||||
throw new IllegalArgumentException("Partie introuvable : " + id);
|
||||
}
|
||||
// Cascade : sessions (et leurs entries), PJ, flags, progressions
|
||||
for (Session s : sessionRepository.findByPlaythroughId(id)) {
|
||||
sessionService.deleteSession(s.getId());
|
||||
}
|
||||
characterRepository.findByPlaythroughId(id).forEach(c -> characterRepository.deleteById(c.getId()));
|
||||
flagRepository.deleteAllByPlaythroughId(id);
|
||||
progressionRepository.deleteAllByPlaythroughId(id);
|
||||
playthroughRepository.deleteById(id);
|
||||
}
|
||||
|
||||
public boolean exists(String id) {
|
||||
return playthroughRepository.existsById(id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package com.loremind.application.playcontext;
|
||||
|
||||
import com.loremind.domain.playcontext.EntryType;
|
||||
import com.loremind.domain.playcontext.SessionEntry;
|
||||
import com.loremind.domain.playcontext.ports.SessionEntryRepository;
|
||||
import com.loremind.domain.playcontext.ports.SessionRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Service d'application pour le journal d'une Session.
|
||||
* Gère le cycle CRUD des entrées (note, évènement, jet, action joueur).
|
||||
*/
|
||||
@Service
|
||||
public class SessionEntryService {
|
||||
|
||||
private final SessionEntryRepository entryRepository;
|
||||
private final SessionRepository sessionRepository;
|
||||
|
||||
public SessionEntryService(SessionEntryRepository entryRepository,
|
||||
SessionRepository sessionRepository) {
|
||||
this.entryRepository = entryRepository;
|
||||
this.sessionRepository = sessionRepository;
|
||||
}
|
||||
|
||||
/** Données fournies par l'API pour créer ou éditer une entrée. */
|
||||
public record EntryData(EntryType type, String content, LocalDateTime occurredAt) {}
|
||||
|
||||
public SessionEntry createEntry(String sessionId, EntryData data) {
|
||||
if (sessionId == null || sessionId.isBlank()) {
|
||||
throw new IllegalArgumentException("sessionId est requis.");
|
||||
}
|
||||
if (!sessionRepository.existsById(sessionId)) {
|
||||
throw new IllegalArgumentException("Session introuvable : " + sessionId);
|
||||
}
|
||||
validateContent(data.content());
|
||||
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
SessionEntry entry = SessionEntry.builder()
|
||||
.sessionId(sessionId)
|
||||
.type(data.type() != null ? data.type() : EntryType.NOTE)
|
||||
.content(data.content().trim())
|
||||
.occurredAt(data.occurredAt() != null ? data.occurredAt() : now)
|
||||
.build();
|
||||
return entryRepository.save(entry);
|
||||
}
|
||||
|
||||
public SessionEntry updateEntry(String id, EntryData data) {
|
||||
validateContent(data.content());
|
||||
SessionEntry existing = entryRepository.findById(id)
|
||||
.orElseThrow(() -> new IllegalArgumentException("Entrée introuvable : " + id));
|
||||
if (data.type() != null) existing.setType(data.type());
|
||||
existing.setContent(data.content().trim());
|
||||
if (data.occurredAt() != null) existing.setOccurredAt(data.occurredAt());
|
||||
return entryRepository.save(existing);
|
||||
}
|
||||
|
||||
public Optional<SessionEntry> getById(String id) {
|
||||
return entryRepository.findById(id);
|
||||
}
|
||||
|
||||
public List<SessionEntry> getBySessionId(String sessionId) {
|
||||
return entryRepository.findBySessionId(sessionId);
|
||||
}
|
||||
|
||||
public void deleteEntry(String id) {
|
||||
if (!entryRepository.existsById(id)) {
|
||||
throw new IllegalArgumentException("Entrée introuvable : " + id);
|
||||
}
|
||||
entryRepository.deleteById(id);
|
||||
}
|
||||
|
||||
private void validateContent(String content) {
|
||||
if (content == null || content.isBlank()) {
|
||||
throw new IllegalArgumentException("Le contenu d'une entrée ne peut pas être vide.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
package com.loremind.application.playcontext;
|
||||
|
||||
import com.loremind.domain.playcontext.Session;
|
||||
import com.loremind.domain.playcontext.ports.PlaythroughRepository;
|
||||
import com.loremind.domain.playcontext.ports.SessionEntryRepository;
|
||||
import com.loremind.domain.playcontext.ports.SessionRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Service d'application pour le Play Context.
|
||||
* Orchestre le cycle de vie d'une Session (lancement, fin, renommage).
|
||||
*
|
||||
* <p>Règle métier : une seule Session peut être active (endedAt null) à la fois.</p>
|
||||
* <p>Depuis Playthrough : une Session appartient à un Playthrough (pas directement à une Campaign).</p>
|
||||
*/
|
||||
@Service
|
||||
public class SessionService {
|
||||
|
||||
private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
||||
|
||||
private final SessionRepository sessionRepository;
|
||||
private final SessionEntryRepository entryRepository;
|
||||
private final PlaythroughRepository playthroughRepository;
|
||||
|
||||
public SessionService(SessionRepository sessionRepository,
|
||||
SessionEntryRepository entryRepository,
|
||||
PlaythroughRepository playthroughRepository) {
|
||||
this.sessionRepository = sessionRepository;
|
||||
this.entryRepository = entryRepository;
|
||||
this.playthroughRepository = playthroughRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lance une nouvelle session sur le Playthrough donné.
|
||||
* Échoue si une session est déjà active ou si le Playthrough n'existe pas.
|
||||
*/
|
||||
public Session startSession(String playthroughId) {
|
||||
if (playthroughId == null || playthroughId.isBlank()) {
|
||||
throw new IllegalArgumentException("playthroughId est requis pour démarrer une session.");
|
||||
}
|
||||
if (!playthroughRepository.existsById(playthroughId)) {
|
||||
throw new IllegalArgumentException("Partie introuvable : " + playthroughId);
|
||||
}
|
||||
// Règle métier : une seule session active par Partie (pas de verrou global cross-Partie).
|
||||
sessionRepository.findActiveByPlaythroughId(playthroughId).ifPresent(s -> {
|
||||
throw new IllegalStateException(
|
||||
"Une session est déjà en cours pour cette Partie (id=" + s.getId() +
|
||||
"). Termine-la avant d'en lancer une nouvelle.");
|
||||
});
|
||||
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
Session session = Session.builder()
|
||||
.name(generateDefaultName(now))
|
||||
.playthroughId(playthroughId)
|
||||
.startedAt(now)
|
||||
.build();
|
||||
return sessionRepository.save(session);
|
||||
}
|
||||
|
||||
public Session endSession(String id) {
|
||||
Session session = sessionRepository.findById(id)
|
||||
.orElseThrow(() -> new IllegalArgumentException("Session introuvable : " + id));
|
||||
if (!session.isActive()) {
|
||||
throw new IllegalStateException("Cette session est déjà terminée.");
|
||||
}
|
||||
session.setEndedAt(LocalDateTime.now());
|
||||
return sessionRepository.save(session);
|
||||
}
|
||||
|
||||
public Session renameSession(String id, String newName) {
|
||||
if (newName == null || newName.isBlank()) {
|
||||
throw new IllegalArgumentException("Le nom de la session ne peut pas être vide.");
|
||||
}
|
||||
Session session = sessionRepository.findById(id)
|
||||
.orElseThrow(() -> new IllegalArgumentException("Session introuvable : " + id));
|
||||
session.setName(newName.trim());
|
||||
return sessionRepository.save(session);
|
||||
}
|
||||
|
||||
public Optional<Session> getById(String id) {
|
||||
return sessionRepository.findById(id);
|
||||
}
|
||||
|
||||
public Optional<Session> getActive() {
|
||||
return sessionRepository.findActive();
|
||||
}
|
||||
|
||||
public Optional<Session> getActiveByPlaythrough(String playthroughId) {
|
||||
return sessionRepository.findActiveByPlaythroughId(playthroughId);
|
||||
}
|
||||
|
||||
public List<Session> getAll() {
|
||||
return sessionRepository.findAll();
|
||||
}
|
||||
|
||||
public List<Session> getByPlaythroughId(String playthroughId) {
|
||||
return sessionRepository.findByPlaythroughId(playthroughId);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteSession(String id) {
|
||||
if (!sessionRepository.existsById(id)) {
|
||||
throw new IllegalArgumentException("Session introuvable : " + id);
|
||||
}
|
||||
entryRepository.deleteBySessionId(id);
|
||||
sessionRepository.deleteById(id);
|
||||
}
|
||||
|
||||
private String generateDefaultName(LocalDateTime startedAt) {
|
||||
return "Session du " + startedAt.format(DATE_FORMATTER);
|
||||
}
|
||||
}
|
||||
@@ -21,6 +21,17 @@ public class Arc {
|
||||
private String campaignId; // Référence vers la Campaign parente
|
||||
private int order; // Ordre de l'arc dans la campagne
|
||||
|
||||
/**
|
||||
* Type structurel de l'arc. Détermine son rendu UI et la sémantique de ses chapitres
|
||||
* (séquence narrative LINEAR vs. quêtes parallèles d'un HUB).
|
||||
* Défaut LINEAR pour rétro-compatibilité avec les arcs existants.
|
||||
*/
|
||||
@Builder.Default
|
||||
private ArcType type = ArcType.LINEAR;
|
||||
|
||||
/** 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/)
|
||||
private String themes; // Thèmes principaux explorés dans cet arc
|
||||
private String stakes; // Enjeux globaux pour les personnages
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.loremind.domain.campaigncontext;
|
||||
|
||||
/**
|
||||
* Type structurel d'un Arc.
|
||||
* - LINEAR : narration séquentielle classique (chapitres joués dans l'ordre).
|
||||
* - HUB : narration non linéaire ; les chapitres sont des "quêtes" satellites
|
||||
* potentiellement parallèles, soumises à des prérequis pour être débloquées.
|
||||
*
|
||||
* Value Object du domaine (Bounded Context : Campaign).
|
||||
*/
|
||||
public enum ArcType {
|
||||
LINEAR,
|
||||
HUB
|
||||
}
|
||||
@@ -6,8 +6,12 @@ import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* Entité de domaine représentant une Campaign.
|
||||
* Conteneur global pour organiser la narration d'une campagne.
|
||||
* Entité pure du domaine, sans dépendance technique.
|
||||
* Conteneur du SCÉNARIO (générique, ré-utilisable par plusieurs tables).
|
||||
*
|
||||
* <p>Toute donnée dynamique propre à une table jouée (progression des quêtes,
|
||||
* flags narratifs, sessions, PJ) vit dans un {@link com.loremind.domain.playcontext.Playthrough}.</p>
|
||||
*
|
||||
* <p>Entité pure du domaine, sans dépendance technique.</p>
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@@ -21,14 +25,21 @@ public class Campaign {
|
||||
private int arcsCount;
|
||||
|
||||
/**
|
||||
* Référence faible (weak reference) vers un Lore.
|
||||
* Nullable : une campagne peut exister sans univers associé (one-shot, test, pitch libre).
|
||||
* Ce n'est qu'un ID : le Campaign Context ne dépend PAS du Lore Context
|
||||
* (respect des Bounded Contexts en DDD).
|
||||
* Référence faible vers un Lore. Nullable.
|
||||
* Ce n'est qu'un ID : le Campaign Context ne dépend PAS du Lore Context.
|
||||
*/
|
||||
private String loreId;
|
||||
|
||||
/**
|
||||
* Référence faible vers un GameSystem. Nullable.
|
||||
*/
|
||||
private String gameSystemId;
|
||||
|
||||
public boolean isLinkedToLore() {
|
||||
return this.loreId != null && !this.loreId.isBlank();
|
||||
}
|
||||
|
||||
public boolean isLinkedToGameSystem() {
|
||||
return this.gameSystemId != null && !this.gameSystemId.isBlank();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.loremind.domain.campaigncontext;
|
||||
|
||||
/**
|
||||
* Évènement d'avancement émis pendant l'import streamé d'un PDF de campagne.
|
||||
* <p>
|
||||
* {@code total} = nombre de morceaux à traiter (0 pendant l'extraction).
|
||||
* {@code current} = morceaux traités. Les compteurs arc/chapitre/scène donnent
|
||||
* un aperçu de l'arbre trouvé jusqu'ici (affichage « au fil de l'eau »).
|
||||
*/
|
||||
public record CampaignImportProgress(
|
||||
int current,
|
||||
int total,
|
||||
int pageCount,
|
||||
int ocrPageCount,
|
||||
int arcCount,
|
||||
int chapterCount,
|
||||
int sceneCount) {
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.loremind.domain.campaigncontext;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Proposition d'arborescence narrative extraite d'un PDF de campagne.
|
||||
* <p>
|
||||
* PROPOSITION non persistée : l'UI laisse l'utilisateur réviser/éditer l'arbre
|
||||
* avant la création effective des arcs/chapitres/scènes. Records purs (domaine).
|
||||
*/
|
||||
public record CampaignImportProposal(List<ArcProposal> arcs) {
|
||||
|
||||
/**
|
||||
* {@code existingId} (nullable) : si présent, le nœud existe DÉJÀ dans la
|
||||
* campagne (rempli côté UI lors de la revue pré-chargée) → l'apply ne le
|
||||
* recrée pas, il l'utilise comme parent des nouveaux enfants. Null = à créer.
|
||||
*/
|
||||
|
||||
/** {@code type} = "LINEAR" ou "HUB" (mappé sur {@link ArcType} à l'apply). */
|
||||
public record ArcProposal(
|
||||
String name, String description, String type,
|
||||
List<ChapterProposal> chapters, String existingId) {
|
||||
}
|
||||
|
||||
public record ChapterProposal(
|
||||
String name, String description, List<SceneProposal> scenes, String existingId) {
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code rooms} non vide => lieu explorable (donjon). {@code playerNarration}
|
||||
* = encadré « à lire aux joueurs », {@code gmNotes} = secrets/développement MJ.
|
||||
*/
|
||||
public record SceneProposal(
|
||||
String name, String description, String playerNarration, String gmNotes,
|
||||
List<RoomProposal> rooms, String existingId) {
|
||||
}
|
||||
|
||||
public record RoomProposal(String name, String description, String enemies, String loot) {
|
||||
}
|
||||
}
|
||||
@@ -21,6 +21,17 @@ public class Chapter {
|
||||
private String arcId; // Référence vers l'Arc parent
|
||||
private int order; // Ordre du chapitre dans l'arc
|
||||
|
||||
/**
|
||||
* Conditions de déblocage (combinées en ET). Vide => quête immédiatement AVAILABLE.
|
||||
* Pertinent surtout pour les chapitres d'un Arc HUB ; ignoré pour LINEAR.
|
||||
* Donnée de SCÉNARIO — partagée par toutes les Parties de la campagne.
|
||||
*/
|
||||
@Builder.Default
|
||||
private List<Prerequisite> prerequisites = new ArrayList<>();
|
||||
|
||||
/** 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/)
|
||||
private String gmNotes; // Notes privées du MJ (non exportées vers FoundryVTT)
|
||||
private String playerObjectives; // Objectifs des joueurs dans ce chapitre
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
package com.loremind.domain.campaigncontext;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Fiche de personnage joueur (PJ) d'une campagne.
|
||||
* <p>
|
||||
* Champs universels hard-codes : {@code name}, {@code portraitImageId},
|
||||
* {@code headerImageId}. Tout le reste est piloté par le template PJ du
|
||||
* GameSystem associé à la campagne (cf. {@link com.loremind.domain.gamesystemcontext.GameSystem#getCharacterTemplate}).
|
||||
* <p>
|
||||
* Les valeurs des champs templates sont stockées dans deux maps :
|
||||
* - {@code values} : champs TEXT et NUMBER (numérique sérialisé en string,
|
||||
* parsé à l'usage cote presentation)
|
||||
* - {@code imageValues} : champs IMAGE (liste ordonnée d'IDs d'images par champ)
|
||||
* <p>
|
||||
* Le champ historique {@code markdownContent} a été supprimé (refonte 2026-04-30).
|
||||
* Le contenu pre-existant est migré dans {@code values["Notes"]} par défaut.
|
||||
* <p>
|
||||
* Scope strict PJ : les PNJ sont gérés par l'entité {@link Npc} (invariants divergents).
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
public class Character {
|
||||
|
||||
private String id;
|
||||
private String name;
|
||||
|
||||
/** ID de l'image portrait (champ universel hard-codé). Nullable. */
|
||||
private String portraitImageId;
|
||||
|
||||
/** ID de l'image header/banniere (champ universel hard-codé). Nullable. */
|
||||
private String headerImageId;
|
||||
|
||||
/**
|
||||
* Valeurs des champs TEXT et NUMBER du template PJ. Cle = nom du champ
|
||||
* (sensible a la casse cote stockage mais comparaison case-insensitive
|
||||
* dans le domaine GameSystem). Jamais null apres construction.
|
||||
*/
|
||||
private Map<String, String> values;
|
||||
|
||||
/**
|
||||
* Valeurs des champs IMAGE du template PJ. Cle = nom du champ, valeur =
|
||||
* liste ordonnee d'IDs d'images. Jamais null apres construction.
|
||||
*/
|
||||
private Map<String, List<String>> imageValues;
|
||||
|
||||
/**
|
||||
* Valeurs des champs KEY_VALUE_LIST du template PJ. Cle externe = nom du
|
||||
* champ template (ex: "Caracteristiques"), cle interne = label predefini
|
||||
* dans le template (ex: "FOR"), valeur = valeur saisie (ex: "16").
|
||||
* Les labels suivent l'ordre defini dans TemplateField.labels.
|
||||
*/
|
||||
private Map<String, Map<String, String>> keyValueValues;
|
||||
|
||||
/**
|
||||
* Référence vers le Playthrough (= la partie / table) auquel ce PJ appartient.
|
||||
* Les PJ sont propres à une table jouée, pas au scénario générique de la campagne.
|
||||
* Weak reference cross-context.
|
||||
*/
|
||||
private String playthroughId;
|
||||
|
||||
/** Ordre d'affichage dans la liste des PJ de la Partie. */
|
||||
private int order;
|
||||
|
||||
private LocalDateTime createdAt;
|
||||
private LocalDateTime updatedAt;
|
||||
|
||||
/** Garantit que les maps ne sont jamais null cote consommateur. */
|
||||
public Map<String, String> getValues() {
|
||||
if (values == null) values = new HashMap<>();
|
||||
return values;
|
||||
}
|
||||
|
||||
public Map<String, List<String>> getImageValues() {
|
||||
if (imageValues == null) imageValues = new HashMap<>();
|
||||
return imageValues;
|
||||
}
|
||||
|
||||
public Map<String, Map<String, String>> getKeyValueValues() {
|
||||
if (keyValueValues == null) keyValueValues = new HashMap<>();
|
||||
return keyValueValues;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package com.loremind.domain.campaigncontext;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Fiche de personnage non-joueur (PNJ) d'une campagne.
|
||||
* <p>
|
||||
* Entité dédiée distincte de {@link Character} (DDD assumé : invariants divergents
|
||||
* à terme — faction, statut vivant/mort, visibilité côté joueurs, etc.).
|
||||
* <p>
|
||||
* Mêmes champs universels hard-codés et meme structure de templating que Character,
|
||||
* pilotée par le template PNJ du GameSystem
|
||||
* ({@link com.loremind.domain.gamesystemcontext.GameSystem#getNpcTemplate}).
|
||||
* <p>
|
||||
* Scope campagne : les PNJ "univers" (worldboss, figures du Lore) restent gérés
|
||||
* via le système Page/Template du LoreContext.
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
public class Npc {
|
||||
|
||||
private String id;
|
||||
private String name;
|
||||
|
||||
/** ID de l'image portrait (champ universel hard-code). Nullable. */
|
||||
private String portraitImageId;
|
||||
|
||||
/** ID de l'image header/banniere (champ universel hard-code). Nullable. */
|
||||
private String headerImageId;
|
||||
|
||||
/** Valeurs TEXT/NUMBER du template PNJ. Jamais null apres construction. */
|
||||
private Map<String, String> values;
|
||||
|
||||
/** Valeurs IMAGE du template PNJ (listes d'IDs ordonnees par champ). Jamais null. */
|
||||
private Map<String, List<String>> imageValues;
|
||||
|
||||
/** Valeurs KEY_VALUE_LIST : fieldName -> label -> value. Jamais null. */
|
||||
private Map<String, Map<String, String>> keyValueValues;
|
||||
|
||||
/** Référence vers la Campaign parente (cross-aggregate via ID). */
|
||||
private String campaignId;
|
||||
|
||||
/** Ordre d'affichage dans la liste des PNJ de la campagne. */
|
||||
private int order;
|
||||
|
||||
private LocalDateTime createdAt;
|
||||
private LocalDateTime updatedAt;
|
||||
|
||||
public Map<String, String> getValues() {
|
||||
if (values == null) values = new HashMap<>();
|
||||
return values;
|
||||
}
|
||||
|
||||
public Map<String, List<String>> getImageValues() {
|
||||
if (imageValues == null) imageValues = new HashMap<>();
|
||||
return imageValues;
|
||||
}
|
||||
|
||||
public Map<String, Map<String, String>> getKeyValueValues() {
|
||||
if (keyValueValues == null) keyValueValues = new HashMap<>();
|
||||
return keyValueValues;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.loremind.domain.campaigncontext;
|
||||
|
||||
/**
|
||||
* Condition de déblocage d'une quête (Chapter dans un Arc HUB).
|
||||
*
|
||||
* Sealed : la liste des types est CLOSE et connue à la compilation. Pour ajouter
|
||||
* un nouveau type (ex : NpcMet), il faudra l'ajouter ici ET dans
|
||||
* {@link PrerequisiteEvaluator}.
|
||||
*
|
||||
* Sémantique MVP : une quête a une LISTE de prérequis, tous combinés en ET logique
|
||||
* (pas de OR pour le moment).
|
||||
*/
|
||||
public sealed interface Prerequisite
|
||||
permits Prerequisite.QuestCompleted,
|
||||
Prerequisite.SessionReached,
|
||||
Prerequisite.FlagSet {
|
||||
|
||||
/** La quête référencée par {@code questId} doit être en COMPLETED. */
|
||||
record QuestCompleted(String questId) implements Prerequisite {}
|
||||
|
||||
/** Le compteur de sessions de la campagne doit avoir atteint {@code minSessionNumber}. */
|
||||
record SessionReached(int minSessionNumber) implements Prerequisite {}
|
||||
|
||||
/** Le flag campagne nommé {@code flagName} doit être à true. */
|
||||
record FlagSet(String flagName) implements Prerequisite {}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package com.loremind.domain.campaigncontext;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Service de domaine (pur, sans effet de bord) : évalue les prérequis d'une quête
|
||||
* et en dérive le {@link QuestStatus} effectif.
|
||||
*
|
||||
* NB Java 17 : on utilise instanceof pattern matching (finalisé en Java 16) plutôt que
|
||||
* switch pattern matching (preview en 17, final en 21). La perte de l'exhaustivité
|
||||
* compile-time est compensée par le throw final qui fait crasher tout nouvel
|
||||
* implémentant non câblé.
|
||||
*/
|
||||
public final class PrerequisiteEvaluator {
|
||||
|
||||
/**
|
||||
* Contexte minimal nécessaire à l'évaluation. On ne passe pas la Campaign entière
|
||||
* pour ne pas créer de couplage fort ; juste les faits nécessaires.
|
||||
*/
|
||||
public record EvaluationContext(
|
||||
Set<String> completedQuestIds,
|
||||
int currentSessionCount,
|
||||
Map<String, Boolean> campaignFlags
|
||||
) {}
|
||||
|
||||
/** True si TOUS les prérequis sont satisfaits (ET logique). Vide => true. */
|
||||
public boolean areAllSatisfied(List<Prerequisite> prerequisites, EvaluationContext ctx) {
|
||||
if (prerequisites == null || prerequisites.isEmpty()) return true;
|
||||
return prerequisites.stream().allMatch(p -> isSatisfied(p, ctx));
|
||||
}
|
||||
|
||||
/** Évalue un seul prérequis. */
|
||||
public boolean isSatisfied(Prerequisite prereq, EvaluationContext ctx) {
|
||||
if (prereq instanceof Prerequisite.QuestCompleted q) {
|
||||
return ctx.completedQuestIds().contains(q.questId());
|
||||
}
|
||||
if (prereq instanceof Prerequisite.SessionReached s) {
|
||||
return ctx.currentSessionCount() >= s.minSessionNumber();
|
||||
}
|
||||
if (prereq instanceof Prerequisite.FlagSet f) {
|
||||
return Boolean.TRUE.equals(ctx.campaignFlags().get(f.flagName()));
|
||||
}
|
||||
throw new IllegalStateException("Prerequisite non géré : " + prereq.getClass().getName());
|
||||
}
|
||||
|
||||
/** Dérive le statut effectif à partir de la progression manuelle + des prérequis. */
|
||||
public QuestStatus computeStatus(
|
||||
ProgressionStatus progression,
|
||||
List<Prerequisite> prerequisites,
|
||||
EvaluationContext ctx
|
||||
) {
|
||||
switch (progression) {
|
||||
case COMPLETED: return QuestStatus.COMPLETED;
|
||||
case IN_PROGRESS: return QuestStatus.IN_PROGRESS;
|
||||
case NOT_STARTED:
|
||||
return areAllSatisfied(prerequisites, ctx)
|
||||
? QuestStatus.AVAILABLE
|
||||
: QuestStatus.LOCKED;
|
||||
default:
|
||||
throw new IllegalStateException("ProgressionStatus non géré : " + progression);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.loremind.domain.campaigncontext;
|
||||
|
||||
/**
|
||||
* Statut de progression d'une quête (= Chapter dans un Arc HUB), piloté manuellement par le MJ.
|
||||
*
|
||||
* NOT_STARTED : pas encore commencée. Peut être visible (AVAILABLE) ou cachée (LOCKED)
|
||||
* selon les prérequis — voir {@link QuestStatus}.
|
||||
* IN_PROGRESS : démarrée par le MJ via le bouton "Démarrer cette quête".
|
||||
* COMPLETED : marquée terminée par le MJ.
|
||||
*
|
||||
* NB : un Chapter d'Arc LINEAR conserve NOT_STARTED par défaut sans impact visible.
|
||||
*/
|
||||
public enum ProgressionStatus {
|
||||
NOT_STARTED,
|
||||
IN_PROGRESS,
|
||||
COMPLETED
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.loremind.domain.campaigncontext;
|
||||
|
||||
/**
|
||||
* Statut effectif d'une quête tel qu'affiché dans la vue Hub.
|
||||
* DÉRIVÉ — jamais persisté. Calculé par {@link PrerequisiteEvaluator} à partir
|
||||
* de la {@link ProgressionStatus} persistée et de l'évaluation des prérequis.
|
||||
*
|
||||
* Table de vérité :
|
||||
* NOT_STARTED + prérequis non remplis -> LOCKED
|
||||
* NOT_STARTED + prérequis remplis -> AVAILABLE
|
||||
* IN_PROGRESS -> IN_PROGRESS
|
||||
* COMPLETED -> COMPLETED
|
||||
*/
|
||||
public enum QuestStatus {
|
||||
LOCKED,
|
||||
AVAILABLE,
|
||||
IN_PROGRESS,
|
||||
COMPLETED
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package com.loremind.domain.campaigncontext;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Pièce d'un lieu explorable (donjon, crypte…) attaché à une Scene.
|
||||
*
|
||||
* <p>Une Scene devient « explorable » dès qu'elle a au moins une Room. Tant
|
||||
* qu'elle n'en a pas, elle se comporte comme un beat narratif classique.</p>
|
||||
*
|
||||
* <p>Pas un record Java parce que la liste {@code branches} est mutable côté
|
||||
* builder ; on garde la classe Lombok pour la cohérence avec le reste du
|
||||
* domaine (Arc, Chapter, Scene). L'ID est généré côté front (UUID) au moment
|
||||
* de la création — pas d'auto-increment DB puisque c'est un Value Object
|
||||
* sérialisé en JSONB sur Scene.</p>
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Room {
|
||||
|
||||
/** ID stable (UUID généré côté client). Sert de cible aux {@link RoomBranch}. */
|
||||
private String id;
|
||||
|
||||
/** Nom de la pièce (« Antichambre », « Salle du trône »). */
|
||||
private String name;
|
||||
|
||||
/** Narration / description lue ou résumée aux joueurs en entrant. */
|
||||
private String description;
|
||||
|
||||
/** Énemis, créatures, boss éventuels (markdown libre). */
|
||||
private String enemies;
|
||||
|
||||
/** Loot / récompenses présentes dans la pièce. */
|
||||
private String loot;
|
||||
|
||||
/** Pièges / dangers environnementaux. */
|
||||
private String traps;
|
||||
|
||||
/** Notes privées du MJ (cachées des joueurs). */
|
||||
private String gmNotes;
|
||||
|
||||
/** Étage / niveau de la pièce. 0 = rez-de-chaussée. Nullable = pas d'étage défini. */
|
||||
private Integer floor;
|
||||
|
||||
/** Ordre d'affichage dans la liste (au sein d'un même étage le cas échéant). */
|
||||
private int order;
|
||||
|
||||
/** IDs d'images d'illustration / ambiance. */
|
||||
@Builder.Default
|
||||
private List<String> illustrationImageIds = new ArrayList<>();
|
||||
|
||||
/** ID de l'image « plan » de la pièce (1 image dédiée, schéma tactique). */
|
||||
private String mapImageId;
|
||||
|
||||
/**
|
||||
* Sorties vers d'autres pièces. {@link RoomBranch#targetRoomId()} doit pointer
|
||||
* vers une Room de la même Scene.
|
||||
*/
|
||||
@Builder.Default
|
||||
private List<RoomBranch> branches = new ArrayList<>();
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.loremind.domain.campaigncontext;
|
||||
|
||||
/**
|
||||
* Sortie d'une pièce vers une autre pièce de la même Scene explorable.
|
||||
* Équivalent inter-pièces de {@link SceneBranch}.
|
||||
*
|
||||
* <p>Record Java immuable, sérialisé via Jackson dans la liste JSONB
|
||||
* {@code rooms} de la Scene.</p>
|
||||
*
|
||||
* <p>Règle métier : {@code targetRoomId} doit pointer vers une Room de la
|
||||
* MÊME Scene (validation côté service).</p>
|
||||
*
|
||||
* @param label Libellé visible (« Porte nord », « Trappe au sol »).
|
||||
* @param targetRoomId ID stable de la Room de destination (UUID Room.id).
|
||||
* @param condition Condition optionnelle (« si les PJ ont la clé en argent »).
|
||||
*/
|
||||
public record RoomBranch(String label, String targetRoomId, String condition) {
|
||||
|
||||
public static RoomBranch of(String label, String targetRoomId) {
|
||||
return new RoomBranch(label, targetRoomId, null);
|
||||
}
|
||||
}
|
||||
@@ -21,6 +21,9 @@ public class Scene {
|
||||
private String chapterId; // Référence vers le Chapter parent
|
||||
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 ===
|
||||
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)
|
||||
@@ -69,6 +72,15 @@ public class Scene {
|
||||
@Builder.Default
|
||||
private List<SceneBranch> branches = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* Pièces du lieu explorable représenté par cette scène (donjon, crypte, manoir…).
|
||||
* Vide => scène classique « beat narratif » (comportement inchangé).
|
||||
* Non vide => la scène devient explorable, l'UI affiche un layout dédié pièce-par-pièce.
|
||||
* Sérialisé en JSONB.
|
||||
*/
|
||||
@Builder.Default
|
||||
private List<Room> rooms = new ArrayList<>();
|
||||
|
||||
private LocalDateTime createdAt;
|
||||
private LocalDateTime updatedAt;
|
||||
}
|
||||
|
||||
@@ -1,31 +1,25 @@
|
||||
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.
|
||||
* Décrit un choix offert aux joueurs et la scène de destination associée.
|
||||
* <p>
|
||||
* Immuable (@Value) : pour "modifier" une branche on la remplace.
|
||||
* @Jacksonized : permet à Jackson (sérialisation JSON via le converter JPA)
|
||||
* de reconstruire l'objet en passant par le builder malgré l'absence de setters.
|
||||
* Record Java : immuable par construction, sans aucune dépendance technique
|
||||
* (pas de Lombok, pas de Jackson). Jackson 2.12+ sait sérialiser/désérialiser
|
||||
* les records nativement via le constructeur canonique — c'est ce dont
|
||||
* dépend le SceneBranchListJsonConverter pour le stockage JSONB.
|
||||
* <p>
|
||||
* Règle métier : targetSceneId DOIT pointer vers une Scene du MÊME Chapter
|
||||
* (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
|
||||
@Builder
|
||||
@Jacksonized
|
||||
public class SceneBranch {
|
||||
public record SceneBranch(String label, String targetSceneId, String condition) {
|
||||
|
||||
/** Libellé du choix (ex: "Si les joueurs attaquent le garde"). */
|
||||
String label;
|
||||
|
||||
/** Id de la Scene de destination, intra-chapitre uniquement. */
|
||||
String targetSceneId;
|
||||
|
||||
/** Notes MJ privées sur la condition de déclenchement (optionnel). */
|
||||
String condition;
|
||||
/** Raccourci pour construire une branche sans condition (cas le plus courant). */
|
||||
public static SceneBranch of(String label, String targetSceneId) {
|
||||
return new SceneBranch(label, targetSceneId, null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.loremind.domain.campaigncontext.ports;
|
||||
|
||||
/**
|
||||
* Erreur de domaine : l'import d'un PDF de campagne a échoué (PDF illisible,
|
||||
* Brain injoignable, LLM en erreur...).
|
||||
*/
|
||||
public class CampaignImportException extends RuntimeException {
|
||||
|
||||
public CampaignImportException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public CampaignImportException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.loremind.domain.campaigncontext.ports;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* Port de sortie : produit des CONSEILS d'adaptation d'un PDF à une campagne
|
||||
* existante, streamés token par token. Délègue au Brain (LLM + extraction PDF).
|
||||
* <p>
|
||||
* Contrairement à {@link CampaignPdfImporter} (qui structure pour créer), ici la
|
||||
* sortie est du texte libre (markdown) : l'utilisateur applique à la main.
|
||||
*/
|
||||
public interface CampaignPdfAdvisor {
|
||||
|
||||
/**
|
||||
* @param pdfBytes contenu du PDF à adapter.
|
||||
* @param filename nom d'origine (diagnostic ; peut être null).
|
||||
* @param brief description de la campagne existante (structure + PNJ + lore).
|
||||
* @param messagesJson JSON de l'échange conversationnel ([{role, content}, …]) ;
|
||||
* "[]" au 1er tour. Permet à l'utilisateur de répondre/corriger.
|
||||
* @param onToken invoqué à chaque fragment de texte généré.
|
||||
* @param onComplete invoqué à la fin normale du flux.
|
||||
* @param onError invoqué en cas d'échec (PDF illisible, Brain/LLM en erreur).
|
||||
*/
|
||||
void adviseStreaming(
|
||||
byte[] pdfBytes,
|
||||
String filename,
|
||||
String brief,
|
||||
String messagesJson,
|
||||
Consumer<String> onToken,
|
||||
Runnable onComplete,
|
||||
Consumer<Throwable> onError);
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.loremind.domain.campaigncontext.ports;
|
||||
|
||||
import com.loremind.domain.campaigncontext.CampaignImportProgress;
|
||||
import com.loremind.domain.campaigncontext.CampaignImportProposal;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* Port de sortie : extrait et structure un PDF de campagne en arbre
|
||||
* arc → chapitre → scène. L'implémentation délègue au Brain Python.
|
||||
*/
|
||||
public interface CampaignPdfImporter {
|
||||
|
||||
/**
|
||||
* Variante streamée : l'import peut durer plusieurs minutes, on remonte
|
||||
* l'avancement au fil de l'eau, puis la proposition finale.
|
||||
*
|
||||
* @param onProgress invoqué à chaque étape (extraction, puis par morceau).
|
||||
* @param onDone invoqué une fois avec l'arbre proposé (non persisté).
|
||||
* @param onError invoqué si l'extraction/structuration échoue.
|
||||
*/
|
||||
void importCampaignStreaming(
|
||||
byte[] pdfBytes,
|
||||
String filename,
|
||||
Consumer<CampaignImportProgress> onProgress,
|
||||
Consumer<CampaignImportProposal> onDone,
|
||||
Consumer<Throwable> onError);
|
||||
}
|
||||
@@ -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> findByPlaythroughId(String playthroughId);
|
||||
|
||||
void deleteById(String id);
|
||||
|
||||
boolean existsById(String id);
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.loremind.domain.campaigncontext.ports;
|
||||
|
||||
import com.loremind.domain.campaigncontext.Npc;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Port de sortie pour la persistance des fiches de PNJ (campagne).
|
||||
*/
|
||||
public interface NpcRepository {
|
||||
|
||||
Npc save(Npc npc);
|
||||
|
||||
Optional<Npc> findById(String id);
|
||||
|
||||
List<Npc> findByCampaignId(String campaignId);
|
||||
|
||||
void deleteById(String id);
|
||||
|
||||
boolean existsById(String id);
|
||||
}
|
||||
@@ -37,7 +37,7 @@ public class Conversation {
|
||||
/**
|
||||
* 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".
|
||||
* Valeurs : "page", "arc", "chapter", "scene", "character".
|
||||
*/
|
||||
private String entityType;
|
||||
private String entityId;
|
||||
|
||||
@@ -0,0 +1,145 @@
|
||||
package com.loremind.domain.gamesystemcontext;
|
||||
|
||||
import com.loremind.domain.shared.template.TemplateField;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* 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>
|
||||
* Porte aussi deux templates piloтant la structure des fiches PJ et PNJ d'une
|
||||
* campagne adossée à ce système. Les fiches markdown libres ont laissé place à
|
||||
* un système de champs typés (TEXT/IMAGE/NUMBER) défini ici.
|
||||
* <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;
|
||||
|
||||
/**
|
||||
* Template de fiche PJ : champs typés affichés pour chaque personnage joueur.
|
||||
* Hors champs universels hard-codés (nom, portrait, header). Jamais null après
|
||||
* persistance — un template vide est représenté par une liste vide.
|
||||
*/
|
||||
private List<TemplateField> characterTemplate;
|
||||
|
||||
/**
|
||||
* Template de fiche PNJ. Mêmes règles que {@link #characterTemplate}.
|
||||
* Distinct du template PJ car les invariants métier divergent (un PNJ peut
|
||||
* n'avoir qu'un nom + une motivation, un PJ porte généralement une feuille
|
||||
* de stats complète).
|
||||
*/
|
||||
private List<TemplateField> npcTemplate;
|
||||
|
||||
/** 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;
|
||||
|
||||
// --- Méthodes métier : templates PJ/PNJ --------------------------------
|
||||
|
||||
/**
|
||||
* Ajoute un champ au template PJ. Refuse les doublons de nom (insensible à la casse)
|
||||
* pour éviter les collisions de clés dans {@code Character.values}.
|
||||
*/
|
||||
public void addCharacterField(TemplateField field) {
|
||||
characterTemplate = appendField(characterTemplate, field);
|
||||
}
|
||||
|
||||
/** Pendant PNJ de {@link #addCharacterField}. */
|
||||
public void addNpcField(TemplateField field) {
|
||||
npcTemplate = appendField(npcTemplate, field);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retire un champ du template PJ par nom (insensible à la casse).
|
||||
* No-op silencieux si le champ n'existe pas — appelant n'a pas à pré-vérifier.
|
||||
*/
|
||||
public void removeCharacterField(String fieldName) {
|
||||
characterTemplate = removeFieldByName(characterTemplate, fieldName);
|
||||
}
|
||||
|
||||
public void removeNpcField(String fieldName) {
|
||||
npcTemplate = removeFieldByName(npcTemplate, fieldName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remplace intégralement le template PJ. Utilisé pour le réordonnancement
|
||||
* et l'édition en bloc côté UI. Valide l'unicité des noms.
|
||||
*/
|
||||
public void replaceCharacterTemplate(List<TemplateField> fields) {
|
||||
characterTemplate = validateAndCopy(fields);
|
||||
}
|
||||
|
||||
public void replaceNpcTemplate(List<TemplateField> fields) {
|
||||
npcTemplate = validateAndCopy(fields);
|
||||
}
|
||||
|
||||
// --- Helpers privés ----------------------------------------------------
|
||||
|
||||
private static List<TemplateField> appendField(List<TemplateField> current, TemplateField field) {
|
||||
if (field == null || field.getName() == null || field.getName().isBlank()) {
|
||||
throw new IllegalArgumentException("Field name is required");
|
||||
}
|
||||
List<TemplateField> next = current == null ? new ArrayList<>() : new ArrayList<>(current);
|
||||
if (containsName(next, field.getName())) {
|
||||
throw new IllegalArgumentException("Duplicate field name: " + field.getName());
|
||||
}
|
||||
next.add(field);
|
||||
return next;
|
||||
}
|
||||
|
||||
private static List<TemplateField> removeFieldByName(List<TemplateField> current, String fieldName) {
|
||||
if (current == null || fieldName == null) return current;
|
||||
List<TemplateField> next = new ArrayList<>(current);
|
||||
next.removeIf(f -> equalsIgnoreCase(f.getName(), fieldName));
|
||||
return next;
|
||||
}
|
||||
|
||||
private static List<TemplateField> validateAndCopy(List<TemplateField> fields) {
|
||||
if (fields == null) return new ArrayList<>();
|
||||
List<TemplateField> copy = new ArrayList<>(fields.size());
|
||||
for (TemplateField f : fields) {
|
||||
if (f == null || f.getName() == null || f.getName().isBlank()) {
|
||||
throw new IllegalArgumentException("Field name is required");
|
||||
}
|
||||
if (containsName(copy, f.getName())) {
|
||||
throw new IllegalArgumentException("Duplicate field name: " + f.getName());
|
||||
}
|
||||
copy.add(f);
|
||||
}
|
||||
return copy;
|
||||
}
|
||||
|
||||
private static boolean containsName(List<TemplateField> fields, String name) {
|
||||
return fields.stream().anyMatch(f -> equalsIgnoreCase(f.getName(), name));
|
||||
}
|
||||
|
||||
private static boolean equalsIgnoreCase(String a, String b) {
|
||||
if (a == null || b == null) return a == b;
|
||||
return a.toLowerCase(Locale.ROOT).equals(b.toLowerCase(Locale.ROOT));
|
||||
}
|
||||
}
|
||||
@@ -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,19 @@
|
||||
package com.loremind.domain.gamesystemcontext;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Évènement d'avancement émis pendant l'import streamé d'un PDF de règles.
|
||||
* <p>
|
||||
* {@code total} = nombre de morceaux à traiter (0 tant que l'extraction n'est
|
||||
* pas finie). {@code current} = morceaux déjà traités. {@code newSectionTitles}
|
||||
* = titres de sections nouvellement trouvés/complétés par le dernier morceau
|
||||
* (pour un affichage « au fil de l'eau »).
|
||||
*/
|
||||
public record RulesImportProgress(
|
||||
int current,
|
||||
int total,
|
||||
int pageCount,
|
||||
int ocrPageCount,
|
||||
List<String> newSectionTitles) {
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.loremind.domain.gamesystemcontext;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Proposition de règles extraites d'un PDF, prête à être révisée par l'utilisateur.
|
||||
* <p>
|
||||
* {@code sections} associe un titre de section à son contenu markdown — aligné
|
||||
* sur le format {@link GameSystem#getRulesMarkdown()} (découpé par titres H2).
|
||||
* C'est une PROPOSITION : rien n'est persisté ; l'UI laisse l'utilisateur
|
||||
* réviser/éditer avant d'enregistrer le GameSystem.
|
||||
* <p>
|
||||
* {@code ocrPageCount} indique combien de pages ont nécessité l'OCR (scan) —
|
||||
* 0 = PDF born-digital (couche texte présente).
|
||||
*/
|
||||
public record RulesImportResult(
|
||||
Map<String, String> sections,
|
||||
int pageCount,
|
||||
int ocrPageCount) {
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.loremind.domain.gamesystemcontext.ports;
|
||||
|
||||
/**
|
||||
* Erreur de domaine : l'import d'un PDF de règles a échoué (PDF illisible,
|
||||
* Brain injoignable, LLM en erreur...). Les couches supérieures la traduisent
|
||||
* en réponse HTTP sans connaître l'adapter concret.
|
||||
*/
|
||||
public class RulesImportException extends RuntimeException {
|
||||
|
||||
public RulesImportException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public RulesImportException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.loremind.domain.gamesystemcontext.ports;
|
||||
|
||||
import com.loremind.domain.gamesystemcontext.RulesImportProgress;
|
||||
import com.loremind.domain.gamesystemcontext.RulesImportResult;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* Port de sortie : extrait et structure les règles d'un PDF en sections.
|
||||
* <p>
|
||||
* L'implémentation (adapter) délègue au Brain Python (extraction texte + OCR +
|
||||
* structuration LLM). Le domaine ne connaît ni HTTP, ni le Brain, ni le LLM.
|
||||
*/
|
||||
public interface RulesPdfImporter {
|
||||
|
||||
/**
|
||||
* @param pdfBytes contenu binaire du PDF de règles.
|
||||
* @param filename nom d'origine (diagnostic/logs ; peut être null).
|
||||
* @return la proposition de sections (non persistée).
|
||||
* @throws RulesImportException si l'extraction ou la structuration échoue.
|
||||
*/
|
||||
RulesImportResult importRules(byte[] pdfBytes, String filename);
|
||||
|
||||
/**
|
||||
* Variante streamée : l'import peut durer plusieurs minutes, on remonte
|
||||
* l'avancement au fil de l'eau. Les callbacks sont invoqués depuis le thread
|
||||
* d'exécution de l'adapter (synchrone jusqu'à {@code onDone}/{@code onError}).
|
||||
*
|
||||
* @param onProgress invoqué à chaque étape (extraction, puis par morceau).
|
||||
* @param onDone invoqué une fois avec le résultat final.
|
||||
* @param onError invoqué si l'extraction/structuration échoue.
|
||||
*/
|
||||
void importRulesStreaming(
|
||||
byte[] pdfBytes,
|
||||
String filename,
|
||||
Consumer<RulesImportProgress> onProgress,
|
||||
Consumer<RulesImportResult> onDone,
|
||||
Consumer<Throwable> onError);
|
||||
}
|
||||
@@ -1,9 +1,5 @@
|
||||
package com.loremind.domain.generationcontext;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Singular;
|
||||
import lombok.Value;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -22,55 +18,96 @@ import java.util.List;
|
||||
* <p>
|
||||
* La liste `arcs` préserve l'ordre narratif (tri sur `order` ascendant
|
||||
* 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.
|
||||
* @param npcs Personnages non-joueurs (PNJ) de la campagne. Vide si aucun.
|
||||
*/
|
||||
@Value
|
||||
@Builder
|
||||
public class CampaignStructuralContext {
|
||||
public record CampaignStructuralContext(
|
||||
String campaignName,
|
||||
String campaignDescription,
|
||||
List<ArcSummary> arcs,
|
||||
List<CharacterSummary> characters,
|
||||
List<NpcSummary> npcs) {
|
||||
|
||||
String campaignName;
|
||||
String campaignDescription;
|
||||
@Singular List<ArcSummary> arcs;
|
||||
/**
|
||||
* Résumé d'un PJ : nom + snippet court du markdown.
|
||||
* 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
|
||||
@Builder
|
||||
public static class ArcSummary {
|
||||
String name;
|
||||
String description;
|
||||
/** Nombre d'illustrations attachees a cet arc (pour hint dans le prompt IA). */
|
||||
int illustrationCount;
|
||||
@Singular List<ChapterSummary> chapters;
|
||||
/**
|
||||
* Résumé d'un PNJ : symétrique à {@link CharacterSummary}.
|
||||
* Snippet court extrait du markdown — la fiche complète est réservée
|
||||
* à un usage focus (à venir, entity_type="npc").
|
||||
*/
|
||||
public record NpcSummary(String name, String snippet) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Résumé d'un arc : nom + description courte + ses chapitres.
|
||||
*
|
||||
* @param illustrationCount Nombre d'illustrations attachees a cet arc (pour hint dans le prompt IA).
|
||||
*/
|
||||
public record ArcSummary(
|
||||
String name,
|
||||
String description,
|
||||
int illustrationCount,
|
||||
List<ChapterSummary> chapters) {
|
||||
}
|
||||
|
||||
/** Résumé d'un chapitre : nom + description courte + ses scènes. */
|
||||
@Value
|
||||
@Builder
|
||||
public static class ChapterSummary {
|
||||
String name;
|
||||
String description;
|
||||
int illustrationCount;
|
||||
@Singular List<SceneSummary> scenes;
|
||||
public record ChapterSummary(
|
||||
String name,
|
||||
String description,
|
||||
int illustrationCount,
|
||||
List<SceneSummary> scenes) {
|
||||
}
|
||||
|
||||
/** Résumé d'une scène : nom + description courte + branches narratives. */
|
||||
@Value
|
||||
@Builder
|
||||
public static class SceneSummary {
|
||||
String name;
|
||||
String description;
|
||||
int illustrationCount;
|
||||
@Singular List<BranchHint> branches;
|
||||
/** Résumé d'une scène : nom + description courte + branches + pièces explorables. */
|
||||
public record SceneSummary(
|
||||
String name,
|
||||
String description,
|
||||
int illustrationCount,
|
||||
List<BranchHint> branches,
|
||||
List<RoomSummary> rooms) {
|
||||
}
|
||||
|
||||
/** Indice d'une branche narrative vers une autre scène du même chapitre. */
|
||||
@Value
|
||||
@Builder
|
||||
public static class BranchHint {
|
||||
/** Libellé du choix joueur (ex: "Si les joueurs attaquent le garde"). */
|
||||
String label;
|
||||
/** Nom de la scène cible (résolu depuis targetSceneId côté builder). */
|
||||
String targetSceneName;
|
||||
/** Condition MJ privée (optionnel). */
|
||||
String condition;
|
||||
/**
|
||||
* Indice d'une branche narrative vers une autre scène du même chapitre.
|
||||
*
|
||||
* @param label 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).
|
||||
* @param condition Condition MJ privée (optionnel).
|
||||
*/
|
||||
public record BranchHint(String label, String targetSceneName, String condition) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Pièce d'un lieu explorable (donjon, crypte). Projection volontairement plate
|
||||
* pour le prompt IA : pas de notes MJ (jamais leakées dans le contexte campagne),
|
||||
* la narration et les ennemis suffisent à camper la pièce.
|
||||
*
|
||||
* @param name Nom de la pièce.
|
||||
* @param floor Étage (nullable).
|
||||
* @param description Narration courte.
|
||||
* @param enemies Ennemis (texte libre).
|
||||
* @param branches Sorties vers d'autres pièces (noms résolus).
|
||||
*/
|
||||
public record RoomSummary(
|
||||
String name,
|
||||
Integer floor,
|
||||
String description,
|
||||
String enemies,
|
||||
List<RoomBranchHint> branches) {
|
||||
}
|
||||
|
||||
/** Indice d'une sortie entre pièces ; {@code targetRoomName} déjà résolu. */
|
||||
public record RoomBranchHint(String label, String targetRoomName, String condition) {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
package com.loremind.domain.generationcontext;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Value;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -21,22 +18,81 @@ import java.util.List;
|
||||
* 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,
|
||||
* 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
|
||||
@Builder
|
||||
public class ChatRequest {
|
||||
public record ChatRequest(
|
||||
List<ChatMessage> messages,
|
||||
LoreStructuralContext loreContext,
|
||||
PageContext pageContext,
|
||||
CampaignStructuralContext campaignContext,
|
||||
NarrativeEntityContext narrativeEntity,
|
||||
GameSystemContext gameSystemContext,
|
||||
SessionContext sessionContext) {
|
||||
|
||||
List<ChatMessage> messages;
|
||||
public static Builder builder() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
/** Optionnel : carte structurelle du Lore. Null si campagne non liée à un Lore. */
|
||||
LoreStructuralContext loreContext;
|
||||
/** Builder fluide : permet d'omettre les contextes non pertinents. */
|
||||
public static final class Builder {
|
||||
private List<ChatMessage> messages;
|
||||
private LoreStructuralContext loreContext;
|
||||
private PageContext pageContext;
|
||||
private CampaignStructuralContext campaignContext;
|
||||
private NarrativeEntityContext narrativeEntity;
|
||||
private GameSystemContext gameSystemContext;
|
||||
private SessionContext sessionContext;
|
||||
|
||||
/** Optionnel : contexte d'une page précise en cours d'édition (chat Lore uniquement). */
|
||||
PageContext pageContext;
|
||||
private Builder() {}
|
||||
|
||||
/** Optionnel : carte narrative d'une Campagne (chat Campagne uniquement). */
|
||||
CampaignStructuralContext campaignContext;
|
||||
public Builder messages(List<ChatMessage> messages) {
|
||||
this.messages = messages;
|
||||
return this;
|
||||
}
|
||||
|
||||
/** Optionnel : entité narrative en cours d'édition (arc/chapter/scene). */
|
||||
NarrativeEntityContext narrativeEntity;
|
||||
public Builder loreContext(LoreStructuralContext loreContext) {
|
||||
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 Builder sessionContext(SessionContext sessionContext) {
|
||||
this.sessionContext = sessionContext;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ChatRequest build() {
|
||||
return new ChatRequest(messages, loreContext, pageContext,
|
||||
campaignContext, narrativeEntity, gameSystemContext, sessionContext);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Value;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -10,19 +7,16 @@ import java.util.List;
|
||||
* pour remplir une Page à partir d'un Template.
|
||||
* <p>
|
||||
* Équivalent Java du PageGenerationContext Python (brain/app/domain/models.py).
|
||||
* Entité pure du domaine : aucune dépendance technique.
|
||||
* <p>
|
||||
* Immuable via @Value (Lombok) : pas de setters, tous les champs final.
|
||||
* C'est un DTO de domaine entrant dans le port AiProvider.
|
||||
* Record Java : pur domaine, aucune dépendance technique.
|
||||
*
|
||||
* @param templateFields Champs à générer (clés attendues dans la réponse).
|
||||
* @param folderName Nom du LoreNode parent (ex: "PNJ", "Lieux").
|
||||
*/
|
||||
@Value
|
||||
@Builder
|
||||
public class GenerationContext {
|
||||
|
||||
String loreName;
|
||||
String loreDescription;
|
||||
String folderName; // Nom du LoreNode parent (ex: "PNJ", "Lieux")
|
||||
String templateName;
|
||||
List<String> templateFields; // Champs à générer (clés attendues dans la réponse)
|
||||
String pageTitle;
|
||||
public record GenerationContext(
|
||||
String loreName,
|
||||
String loreDescription,
|
||||
String folderName,
|
||||
String templateName,
|
||||
List<String> templateFields,
|
||||
String pageTitle) {
|
||||
}
|
||||
|
||||
@@ -1,9 +1,5 @@
|
||||
package com.loremind.domain.generationcontext;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Singular;
|
||||
import lombok.Value;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -16,15 +12,14 @@ import java.util.Map;
|
||||
* <p>
|
||||
* 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).
|
||||
* <p>
|
||||
* Record Java : pur domaine, aucune dépendance technique.
|
||||
*/
|
||||
@Value
|
||||
@Builder
|
||||
public class LoreStructuralContext {
|
||||
|
||||
String loreName;
|
||||
String loreDescription;
|
||||
Map<String, List<PageSummary>> folders;
|
||||
@Singular List<String> tags;
|
||||
public record LoreStructuralContext(
|
||||
String loreName,
|
||||
String loreDescription,
|
||||
Map<String, List<PageSummary>> folders,
|
||||
List<String> tags) {
|
||||
|
||||
/**
|
||||
* 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
|
||||
* restent confinés à leur page d'édition).
|
||||
*/
|
||||
@Value
|
||||
@Builder
|
||||
public static class PageSummary {
|
||||
String title;
|
||||
String templateName;
|
||||
Map<String, String> values;
|
||||
List<String> tags;
|
||||
List<String> relatedPageTitles;
|
||||
public record PageSummary(
|
||||
String title,
|
||||
String templateName,
|
||||
Map<String, String> values,
|
||||
List<String> tags,
|
||||
List<String> relatedPageTitles) {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
package com.loremind.domain.generationcontext;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Value;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@@ -17,13 +14,11 @@ import java.util.Map;
|
||||
* `fields` associe le nom d'un champ (ex: "themes", "playerNarration")
|
||||
* à sa valeur actuelle (chaîne vide si non renseigné). Utiliser une
|
||||
* LinkedHashMap à la construction pour un prompt lisible (ordre préservé).
|
||||
*
|
||||
* @param entityType "arc", "chapter" ou "scene" — utilisé pour libeller le bloc du prompt.
|
||||
*/
|
||||
@Value
|
||||
@Builder
|
||||
public class NarrativeEntityContext {
|
||||
|
||||
/** "arc", "chapter" ou "scene" — utilisé pour libeller le bloc du prompt. */
|
||||
String entityType;
|
||||
String title;
|
||||
Map<String, String> fields;
|
||||
public record NarrativeEntityContext(
|
||||
String entityType,
|
||||
String title,
|
||||
Map<String, String> fields) {
|
||||
}
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
package com.loremind.domain.generationcontext;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Value;
|
||||
|
||||
import java.util.List;
|
||||
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
|
||||
* sur d'autres pages/templates.
|
||||
* <p>
|
||||
* Object de valeur immuable, pur domaine — aucune dépendance technique.
|
||||
* Record Java : immuable, pur domaine, aucune dépendance technique.
|
||||
*/
|
||||
@Value
|
||||
@Builder
|
||||
public class PageContext {
|
||||
|
||||
String title;
|
||||
String templateName;
|
||||
List<String> templateFields;
|
||||
Map<String, String> values;
|
||||
public record PageContext(
|
||||
String title,
|
||||
String templateName,
|
||||
List<String> templateFields,
|
||||
Map<String, String> values) {
|
||||
}
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.loremind.domain.generationcontext;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Contexte structurel d'une Session de jeu — injecté dans le system prompt
|
||||
* de l'IA pour qu'elle ait conscience de la partie en cours et de son journal.
|
||||
*
|
||||
* <p>Pendant qu'une session se joue, l'IA reçoit en plus du Lore/Campagne/GameSystem :
|
||||
* le nom de la session, son statut, un résumé chronologique du journal,
|
||||
* et — depuis l'ajout du mode Hub — l'état des quêtes ouvertes de la campagne et
|
||||
* les flags actifs.</p>
|
||||
*
|
||||
* <p>Value Object du Generation Context — record Java immutable.</p>
|
||||
*
|
||||
* @param sessionName Nom de la session courante telle qu'affichée au MJ.
|
||||
* @param active True si la session est en cours, false si terminée.
|
||||
* @param startedAt Horodatage de démarrage de la session courante.
|
||||
* @param entries Entrées du journal de la session courante (cap côté builder).
|
||||
* @param previousEvents Évènements marquants des sessions précédentes (continuité narrative).
|
||||
* @param availableQuests Quêtes Hub actuellement débloquées et non démarrées.
|
||||
* @param inProgressQuests Quêtes Hub en cours.
|
||||
* @param lockedQuestTitles Titres des quêtes Hub verrouillées — uniquement le titre
|
||||
* pour signaler leur existence sans spoiler.
|
||||
* @param activeFlags Noms des flags de campagne à true.
|
||||
*/
|
||||
public record SessionContext(
|
||||
String sessionName,
|
||||
boolean active,
|
||||
LocalDateTime startedAt,
|
||||
List<JournalEntrySummary> entries,
|
||||
List<JournalEntrySummary> previousEvents,
|
||||
List<QuestSummary> availableQuests,
|
||||
List<QuestSummary> inProgressQuests,
|
||||
List<String> lockedQuestTitles,
|
||||
List<String> activeFlags) {
|
||||
|
||||
/**
|
||||
* Résumé d'une entrée de journal — type + contenu + horodatage + (optionnel) session source.
|
||||
* {@code sourceSessionName} renseigné uniquement pour les évènements issus de sessions
|
||||
* précédentes, pour aider l'IA à les ancrer temporellement.
|
||||
*/
|
||||
public record JournalEntrySummary(
|
||||
String type,
|
||||
String content,
|
||||
LocalDateTime occurredAt,
|
||||
String sourceSessionName) {}
|
||||
|
||||
/**
|
||||
* Résumé d'une quête (= Chapter dans un Arc HUB) telle qu'exposée à l'IA.
|
||||
* On omet volontairement les notes MJ : pas de fuite côté prompt.
|
||||
*/
|
||||
public record QuestSummary(
|
||||
String name,
|
||||
String arcName,
|
||||
String description) {}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.loremind.domain.licensing;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
/**
|
||||
* Licence Patreon installee dans cette instance LoreMind.
|
||||
* <p>
|
||||
* Singleton (une seule licence par instance, identifiee logiquement par
|
||||
* {@code id = "current"}). Contient le JWT brut emis par le relais OAuth
|
||||
* + les claims extraits a la verification, plus l'etat operationnel
|
||||
* (derniere tentative de refresh, succes/echec).
|
||||
* <p>
|
||||
* <b>Note securite :</b> {@link #rawJwt} est stocke tel quel ; sa signature
|
||||
* Ed25519 est verifiee a chaque lecture. Pas besoin de chiffrement au repos
|
||||
* supplementaire — un attaquant qui a acces a la base a deja l'instance,
|
||||
* et le JWT ne donne aucun pouvoir au-dela du canal beta de cette instance.
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
public class License {
|
||||
|
||||
private String id;
|
||||
|
||||
private String rawJwt;
|
||||
|
||||
private String patreonUserId;
|
||||
|
||||
private String tierId;
|
||||
|
||||
private String instanceId;
|
||||
|
||||
private Instant issuedAt;
|
||||
|
||||
private Instant expiresAt;
|
||||
|
||||
private Instant lastRefreshAttemptAt;
|
||||
|
||||
private boolean lastRefreshSucceeded;
|
||||
|
||||
private boolean betaChannelEnabled;
|
||||
|
||||
private Instant createdAt;
|
||||
|
||||
private Instant updatedAt;
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.loremind.domain.licensing;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
/**
|
||||
* Claims extraits d'un JWT licence apres verification de signature.
|
||||
* Immuable.
|
||||
*/
|
||||
public record LicenseClaims(
|
||||
String subject,
|
||||
String tierId,
|
||||
String instanceId,
|
||||
Instant issuedAt,
|
||||
Instant expiresAt
|
||||
) {}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.loremind.domain.licensing;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
/**
|
||||
* Vue immuable de la licence pour exposition vers les couches superieures.
|
||||
* Decouple le domaine du DTO web et permet de calculer le {@link LicenseStatus}
|
||||
* a un instant donne sans muter l'entite.
|
||||
*/
|
||||
public record LicenseSnapshot(
|
||||
LicenseStatus status,
|
||||
String patreonUserId,
|
||||
String tierId,
|
||||
String instanceId,
|
||||
Instant expiresAt,
|
||||
Instant lastRefreshAttemptAt,
|
||||
boolean lastRefreshSucceeded,
|
||||
boolean betaChannelEnabled
|
||||
) {
|
||||
public static LicenseSnapshot none() {
|
||||
return new LicenseSnapshot(LicenseStatus.NONE, null, null, null, null, null, false, false);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.loremind.domain.licensing;
|
||||
|
||||
/**
|
||||
* Etat operationnel de la licence vis-a-vis de l'acces beta.
|
||||
* <p>
|
||||
* Calcule a partir de la presence de licence + son JWT exp + grace period.
|
||||
* <ul>
|
||||
* <li>{@link #NONE} : aucune licence installee</li>
|
||||
* <li>{@link #VALID} : JWT non expire, acces beta autorise</li>
|
||||
* <li>{@link #GRACE} : JWT expire mais dans la periode de tolerance ;
|
||||
* acces beta toujours autorise, l'UI doit avertir</li>
|
||||
* <li>{@link #EXPIRED} : au-dela de la grace period, acces beta refuse</li>
|
||||
* <li>{@link #UNVERIFIABLE} : JWT impossible a verifier (cle publique manquante,
|
||||
* signature invalide, claims malformes) — traite comme NONE pour la securite</li>
|
||||
* </ul>
|
||||
*/
|
||||
public enum LicenseStatus {
|
||||
NONE,
|
||||
VALID,
|
||||
GRACE,
|
||||
EXPIRED,
|
||||
UNVERIFIABLE
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.loremind.domain.licensing;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
/**
|
||||
* Credentials de pull pour un registry Docker, distribues par le relais
|
||||
* apres verification d'un JWT licence valide.
|
||||
* <p>
|
||||
* {@code expiresAt} peut etre {@code null} si le credential est statique
|
||||
* (cas du PAT GHCR partage en MVP) ; sinon, l'instance doit re-demander
|
||||
* de nouveaux credentials avant cette date.
|
||||
*/
|
||||
public record RegistryCredentials(
|
||||
String registry,
|
||||
String username,
|
||||
String password,
|
||||
Instant expiresAt
|
||||
) {}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.loremind.domain.licensing.ports;
|
||||
|
||||
import com.loremind.domain.licensing.RegistryCredentials;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Port de sortie : ecriture du docker config.json partage avec Watchtower.
|
||||
* <p>
|
||||
* Le fichier sert a Watchtower pour s'authentifier au registry prive (GHCR)
|
||||
* lors du pull des images du canal beta. Volume Docker {@code docker-config}
|
||||
* monte sur Core (en ecriture) et sur Watchtower (en lecture, via la variable
|
||||
* {@code DOCKER_CONFIG}).
|
||||
*/
|
||||
public interface DockerConfigWriter {
|
||||
|
||||
/**
|
||||
* Ecrit ou met a jour les credentials pour le registry indique.
|
||||
* Cree le fichier s'il n'existe pas, conserve les autres registries deja
|
||||
* presents (en theorie : aucun, mais defensif).
|
||||
*/
|
||||
void writeCredentials(RegistryCredentials credentials) throws IOException;
|
||||
|
||||
/**
|
||||
* Supprime le fichier de credentials. Appele quand la licence est invalidee
|
||||
* ou que le toggle beta passe a OFF.
|
||||
*/
|
||||
void clear() throws IOException;
|
||||
|
||||
/**
|
||||
* @return true si le fichier de creds existe actuellement.
|
||||
*/
|
||||
boolean isPresent();
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.loremind.domain.licensing.ports;
|
||||
|
||||
import com.loremind.domain.licensing.LicenseClaims;
|
||||
|
||||
/**
|
||||
* Port de sortie : verification de signature et extraction des claims
|
||||
* d'un JWT emis par le relais.
|
||||
* <p>
|
||||
* Implemente cote infrastructure avec la cle publique Ed25519 embarquee
|
||||
* (SPKI PEM via configuration {@code licensing.jwt.public-key}).
|
||||
*/
|
||||
public interface JwtVerifier {
|
||||
|
||||
/**
|
||||
* Verifie la signature, l'issuer, l'audience et l'expiration du JWT.
|
||||
* @throws JwtVerificationException si la signature est invalide ou les claims malformes
|
||||
*/
|
||||
LicenseClaims verify(String rawJwt) throws JwtVerificationException;
|
||||
|
||||
/**
|
||||
* @return true si la cle publique est configuree et utilisable.
|
||||
* Permet a l'application de masquer la feature licensing si pas configuree.
|
||||
*/
|
||||
boolean isConfigured();
|
||||
|
||||
class JwtVerificationException extends Exception {
|
||||
public JwtVerificationException(String message) {
|
||||
super(message);
|
||||
}
|
||||
public JwtVerificationException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.loremind.domain.licensing.ports;
|
||||
|
||||
import com.loremind.domain.licensing.RegistryCredentials;
|
||||
|
||||
/**
|
||||
* Port de sortie vers le service relais OAuth Patreon.
|
||||
* Encapsule les appels HTTP : refresh JWT et fetch registry credentials.
|
||||
*/
|
||||
public interface LicenseRelay {
|
||||
|
||||
/**
|
||||
* Demande au relais l'URL OAuth a ouvrir pour connecter le compte Patreon.
|
||||
*/
|
||||
String buildConnectUrl(String instanceId);
|
||||
|
||||
/**
|
||||
* Demande au relais de renouveler un JWT existant. Le relais re-verifie
|
||||
* le tier Patreon de l'utilisateur ; renvoie un nouveau JWT si toujours
|
||||
* actif, ou leve {@link RelayException} sinon.
|
||||
*/
|
||||
String refreshToken(String currentJwt) throws RelayException;
|
||||
|
||||
/**
|
||||
* Demande au relais les credentials de pull du registry beta.
|
||||
*/
|
||||
RegistryCredentials fetchRegistryCredentials(String currentJwt) throws RelayException;
|
||||
|
||||
/**
|
||||
* Erreurs distinctes emises par le relais. Permet au service application
|
||||
* de differencier "tier expire" (action utilisateur) de "relais down"
|
||||
* (action transitoire, garde la grace period).
|
||||
*/
|
||||
class RelayException extends Exception {
|
||||
private final RelayErrorKind kind;
|
||||
|
||||
public RelayException(RelayErrorKind kind, String message) {
|
||||
super(message);
|
||||
this.kind = kind;
|
||||
}
|
||||
|
||||
public RelayException(RelayErrorKind kind, String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
this.kind = kind;
|
||||
}
|
||||
|
||||
public RelayErrorKind getKind() {
|
||||
return kind;
|
||||
}
|
||||
}
|
||||
|
||||
enum RelayErrorKind {
|
||||
/** Le relais est joignable mais refuse : tier non actif, JWT trop ancien, etc. */
|
||||
REJECTED,
|
||||
/** Le relais a renvoye un JWT mais il est invalide / non parsable. */
|
||||
BAD_RESPONSE,
|
||||
/** Le relais est injoignable / 5xx / timeout. */
|
||||
TRANSIENT
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user