Chat persistant pour la partie lore et la partie campagne pour chaque page / scène.....
Correction du carroussel Passage en v0.4.0 Correction du docker compose pour tout le temps utiliser le bon port que ce soit prod ou dev
This commit is contained in:
@@ -14,7 +14,7 @@
|
||||
|
||||
<groupId>com.loremind</groupId>
|
||||
<artifactId>loremind-core</artifactId>
|
||||
<version>0.3.0</version>
|
||||
<version>0.4.0</version>
|
||||
<name>LoreMind Core</name>
|
||||
<description>Backend Core - Architecture Hexagonale</description>
|
||||
|
||||
|
||||
@@ -0,0 +1,134 @@
|
||||
package com.loremind.application.conversationcontext;
|
||||
|
||||
import com.loremind.domain.conversationcontext.Conversation;
|
||||
import com.loremind.domain.conversationcontext.ConversationMessage;
|
||||
import com.loremind.domain.conversationcontext.ports.ConversationRepository;
|
||||
import com.loremind.domain.conversationcontext.ports.ConversationTitleGenerator;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Service d'application du contexte Conversation.
|
||||
*
|
||||
* Regroupe les cas d'usage CRUD + append message + rename. Un seul
|
||||
* service suffit — le contexte est simple et les operations fortement
|
||||
* liees (meme aggregat).
|
||||
*
|
||||
* Regles metier :
|
||||
* - exactement un ancrage parent (loreId XOR campaignId) ;
|
||||
* - entityType et entityId vont ensemble (tous deux null = niveau racine,
|
||||
* tous deux non-null = niveau entite precise).
|
||||
*/
|
||||
@Service
|
||||
public class ConversationService {
|
||||
|
||||
private final ConversationRepository repository;
|
||||
private final ConversationTitleGenerator titleGenerator;
|
||||
|
||||
public ConversationService(ConversationRepository repository,
|
||||
ConversationTitleGenerator titleGenerator) {
|
||||
this.repository = repository;
|
||||
this.titleGenerator = titleGenerator;
|
||||
}
|
||||
|
||||
/** Donnees de creation d'une conversation. Titre optionnel — sera auto-genere si absent. */
|
||||
public record CreateData(
|
||||
String title,
|
||||
String loreId,
|
||||
String campaignId,
|
||||
String entityType,
|
||||
String entityId) {}
|
||||
|
||||
public Conversation create(CreateData data) {
|
||||
validateAnchor(data.loreId(), data.campaignId(), data.entityType(), data.entityId());
|
||||
|
||||
String title = (data.title() == null || data.title().isBlank())
|
||||
? "Nouvelle conversation"
|
||||
: data.title().trim();
|
||||
|
||||
Conversation conv = Conversation.builder()
|
||||
.title(title)
|
||||
.loreId(data.loreId())
|
||||
.campaignId(data.campaignId())
|
||||
.entityType(data.entityType())
|
||||
.entityId(data.entityId())
|
||||
.build();
|
||||
return repository.save(conv);
|
||||
}
|
||||
|
||||
public Optional<Conversation> getById(String id) {
|
||||
return repository.findById(id);
|
||||
}
|
||||
|
||||
public List<Conversation> listByContext(String loreId, String campaignId, String entityType, String entityId) {
|
||||
validateAnchor(loreId, campaignId, entityType, entityId);
|
||||
return repository.findByContext(loreId, campaignId, entityType, entityId);
|
||||
}
|
||||
|
||||
public void rename(String id, String title) {
|
||||
if (title == null || title.isBlank()) {
|
||||
throw new IllegalArgumentException("Le titre ne peut pas etre vide");
|
||||
}
|
||||
if (repository.findById(id).isEmpty()) {
|
||||
throw new IllegalArgumentException("Conversation introuvable : " + id);
|
||||
}
|
||||
repository.updateTitle(id, title.trim());
|
||||
}
|
||||
|
||||
public void delete(String id) {
|
||||
repository.deleteById(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Auto-genere un titre a partir des premiers messages et le persiste.
|
||||
* Appele typiquement apres le 1er couple user/assistant pour remplacer
|
||||
* le titre provisoire. Echec silencieux (fallback dans l'adaptateur) —
|
||||
* on n'empeche pas la conversation de fonctionner si le Brain est down.
|
||||
*/
|
||||
public String autoGenerateTitle(String conversationId) {
|
||||
Conversation conv = repository.findById(conversationId)
|
||||
.orElseThrow(() -> new IllegalArgumentException("Conversation introuvable : " + conversationId));
|
||||
List<ConversationMessage> seeds = conv.getMessages();
|
||||
if (seeds == null || seeds.isEmpty()) {
|
||||
return conv.getTitle();
|
||||
}
|
||||
String title = titleGenerator.generate(seeds);
|
||||
repository.updateTitle(conversationId, title);
|
||||
return title;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ajoute un message (user ou assistant) a une conversation existante.
|
||||
* L'horodatage et l'id sont assignes par la couche persistance.
|
||||
*/
|
||||
public ConversationMessage appendMessage(String conversationId, String role, String content) {
|
||||
if (role == null || (!role.equals("user") && !role.equals("assistant") && !role.equals("system"))) {
|
||||
throw new IllegalArgumentException("Role invalide : " + role);
|
||||
}
|
||||
if (content == null || content.isEmpty()) {
|
||||
throw new IllegalArgumentException("Contenu vide interdit");
|
||||
}
|
||||
ConversationMessage msg = ConversationMessage.builder()
|
||||
.role(role)
|
||||
.content(content)
|
||||
.build();
|
||||
return repository.appendMessage(conversationId, msg);
|
||||
}
|
||||
|
||||
// ---------- Validation ----------
|
||||
|
||||
private void validateAnchor(String loreId, String campaignId, String entityType, String entityId) {
|
||||
boolean hasLore = loreId != null && !loreId.isBlank();
|
||||
boolean hasCamp = campaignId != null && !campaignId.isBlank();
|
||||
if (hasLore == hasCamp) {
|
||||
throw new IllegalArgumentException("Exactement un parent attendu : loreId XOR campaignId");
|
||||
}
|
||||
boolean hasType = entityType != null && !entityType.isBlank();
|
||||
boolean hasEntId = entityId != null && !entityId.isBlank();
|
||||
if (hasType != hasEntId) {
|
||||
throw new IllegalArgumentException("entityType et entityId doivent etre tous deux null ou tous deux non-null");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import com.loremind.domain.campaigncontext.ports.CampaignRepository;
|
||||
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.LoreStructuralContext;
|
||||
import com.loremind.domain.generationcontext.NarrativeEntityContext;
|
||||
import com.loremind.domain.generationcontext.ports.AiChatProvider;
|
||||
@@ -65,6 +66,7 @@ public class StreamChatForCampaignUseCase {
|
||||
String entityType,
|
||||
String entityId,
|
||||
List<ChatMessage> messages,
|
||||
Consumer<ChatUsage> onUsage,
|
||||
Consumer<String> onToken,
|
||||
Runnable onComplete,
|
||||
Consumer<Throwable> onError) {
|
||||
@@ -84,7 +86,7 @@ public class StreamChatForCampaignUseCase {
|
||||
.narrativeEntity(narrativeEntity)
|
||||
.build();
|
||||
|
||||
aiChatProvider.streamChat(request, onToken, onComplete, onError);
|
||||
aiChatProvider.streamChat(request, onUsage, onToken, onComplete, onError);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.loremind.application.generationcontext;
|
||||
|
||||
import com.loremind.domain.generationcontext.ChatMessage;
|
||||
import com.loremind.domain.generationcontext.ChatRequest;
|
||||
import com.loremind.domain.generationcontext.ChatUsage;
|
||||
import com.loremind.domain.generationcontext.LoreStructuralContext;
|
||||
import com.loremind.domain.generationcontext.PageContext;
|
||||
import com.loremind.domain.generationcontext.ports.AiChatProvider;
|
||||
@@ -60,6 +61,7 @@ public class StreamChatForLoreUseCase {
|
||||
String loreId,
|
||||
String pageId,
|
||||
List<ChatMessage> messages,
|
||||
Consumer<ChatUsage> onUsage,
|
||||
Consumer<String> onToken,
|
||||
Runnable onComplete,
|
||||
Consumer<Throwable> onError) {
|
||||
@@ -75,7 +77,7 @@ public class StreamChatForLoreUseCase {
|
||||
.pageContext(pageContext)
|
||||
.build();
|
||||
|
||||
aiChatProvider.streamChat(request, onToken, onComplete, onError);
|
||||
aiChatProvider.streamChat(request, onUsage, onToken, onComplete, onError);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.loremind.domain.conversationcontext;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Agregat d'une conversation de chat IA persistee.
|
||||
*
|
||||
* Une conversation est ancree sur exactement un niveau de contexte :
|
||||
* - un Lore (optionnellement une page precise)
|
||||
* - une Campagne (optionnellement une entite narrative : arc/chapitre/scene)
|
||||
*
|
||||
* C'est cet ancrage qui permet au drawer de filtrer les conversations
|
||||
* a afficher dans la sidebar selon l'ecran en cours.
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Conversation {
|
||||
|
||||
private String id;
|
||||
private String title;
|
||||
private LocalDateTime createdAt;
|
||||
private LocalDateTime updatedAt;
|
||||
|
||||
/** Un seul des deux est non-null. */
|
||||
private String loreId;
|
||||
private String campaignId;
|
||||
|
||||
/**
|
||||
* Type d'entite focus, null si la conversation est ancree au niveau
|
||||
* Lore/Campagne racine (pas sur une page/scene precise).
|
||||
* Valeurs : "page", "arc", "chapter", "scene".
|
||||
*/
|
||||
private String entityType;
|
||||
private String entityId;
|
||||
|
||||
@Builder.Default
|
||||
private List<ConversationMessage> messages = new ArrayList<>();
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.loremind.domain.conversationcontext;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* Un message persiste d'une conversation.
|
||||
*
|
||||
* Distinct de {@link com.loremind.domain.generationcontext.ChatMessage}
|
||||
* qui reste un simple record role+content pour le streaming LLM. Ici
|
||||
* on ajoute id et horodatage, necessaires pour l'affichage / l'ordre.
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class ConversationMessage {
|
||||
|
||||
private String id;
|
||||
/** "user" | "assistant" | "system". */
|
||||
private String role;
|
||||
private String content;
|
||||
private LocalDateTime createdAt;
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.loremind.domain.conversationcontext.ports;
|
||||
|
||||
import com.loremind.domain.conversationcontext.Conversation;
|
||||
import com.loremind.domain.conversationcontext.ConversationMessage;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Port de persistance des conversations de chat IA.
|
||||
*
|
||||
* Les methodes de lecture par contexte acceptent des filtres nullables :
|
||||
* - `loreId` OU `campaignId` doit etre non-null (mais pas les deux)
|
||||
* - `entityType` + `entityId` : soit tous les deux null (niveau racine),
|
||||
* soit tous les deux non-null (niveau entite precise).
|
||||
*/
|
||||
public interface ConversationRepository {
|
||||
|
||||
Conversation save(Conversation conversation);
|
||||
|
||||
Optional<Conversation> findById(String id);
|
||||
|
||||
/**
|
||||
* Liste les conversations filtrees par contexte strict, triees par
|
||||
* updatedAt desc. Les messages ne sont PAS chargees (liste vide) pour
|
||||
* garder la payload legere — la sidebar n'affiche que les titres.
|
||||
*/
|
||||
List<Conversation> findByContext(
|
||||
String loreId,
|
||||
String campaignId,
|
||||
String entityType,
|
||||
String entityId);
|
||||
|
||||
void deleteById(String id);
|
||||
|
||||
/**
|
||||
* Ajoute un message a une conversation existante. Met a jour updatedAt
|
||||
* de la conversation parent. Renvoie le message persiste (avec id + ts).
|
||||
*/
|
||||
ConversationMessage appendMessage(String conversationId, ConversationMessage message);
|
||||
|
||||
/** Rename atomique — ne touche pas aux messages. */
|
||||
void updateTitle(String conversationId, String title);
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.loremind.domain.conversationcontext.ports;
|
||||
|
||||
import com.loremind.domain.conversationcontext.ConversationMessage;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Port : generation d'un titre court a partir des premiers echanges d'une
|
||||
* conversation. Implemente via un appel Brain /summarize/conversation-title.
|
||||
*/
|
||||
public interface ConversationTitleGenerator {
|
||||
|
||||
/** Renvoie un titre en francais (4-7 mots max). Jamais null ni vide. */
|
||||
String generate(List<ConversationMessage> firstMessages);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.loremind.domain.generationcontext;
|
||||
|
||||
/**
|
||||
* Instantané d'occupation de la fenêtre de contexte à l'instant t du chat.
|
||||
* <p>
|
||||
* Émis une fois par tour de chat (juste avant le streaming des tokens) pour
|
||||
* alimenter la jauge de contexte côté frontend. Les unités sont des tokens
|
||||
* (approximés via tiktoken côté Brain — ±10% vs le tokenizer réel du modèle).
|
||||
*
|
||||
* @param system tokens consommés par le system prompt (contextes Lore/campagne injectés)
|
||||
* @param history tokens consommés par l'historique de la conversation (hors dernier message)
|
||||
* @param current tokens du dernier message utilisateur en attente de réponse
|
||||
* @param max taille maximale configurée de la fenêtre de contexte
|
||||
*/
|
||||
public record ChatUsage(int system, int history, int current, int max) {
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.loremind.domain.generationcontext.ports;
|
||||
|
||||
import com.loremind.domain.generationcontext.ChatRequest;
|
||||
import com.loremind.domain.generationcontext.ChatUsage;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
@@ -26,6 +27,10 @@ public interface AiChatProvider {
|
||||
* HTTP côté controller SSE).
|
||||
*
|
||||
* @param request messages + contexte Lore
|
||||
* @param onUsage invoqué une fois au début du stream avec le bilan
|
||||
* d'occupation de la fenêtre de contexte (tokens system /
|
||||
* history / current / max). Peut ne jamais être invoqué
|
||||
* si le provider ne supporte pas le comptage.
|
||||
* @param onToken invoqué à chaque token reçu du LLM (peut être appelé
|
||||
* de nombreuses fois)
|
||||
* @param onComplete invoqué une fois le stream terminé avec succès
|
||||
@@ -34,6 +39,7 @@ public interface AiChatProvider {
|
||||
*/
|
||||
void streamChat(
|
||||
ChatRequest request,
|
||||
Consumer<ChatUsage> onUsage,
|
||||
Consumer<String> onToken,
|
||||
Runnable onComplete,
|
||||
Consumer<Throwable> onError
|
||||
|
||||
@@ -7,6 +7,7 @@ import com.loremind.domain.generationcontext.CampaignStructuralContext.ChapterSu
|
||||
import com.loremind.domain.generationcontext.CampaignStructuralContext.SceneSummary;
|
||||
import com.loremind.domain.generationcontext.ChatMessage;
|
||||
import com.loremind.domain.generationcontext.ChatRequest;
|
||||
import com.loremind.domain.generationcontext.ChatUsage;
|
||||
import com.loremind.domain.generationcontext.LoreStructuralContext;
|
||||
import com.loremind.domain.generationcontext.LoreStructuralContext.PageSummary;
|
||||
import com.loremind.domain.generationcontext.NarrativeEntityContext;
|
||||
@@ -62,6 +63,7 @@ public class BrainAiChatClient implements AiChatProvider {
|
||||
@Override
|
||||
public void streamChat(
|
||||
ChatRequest request,
|
||||
Consumer<ChatUsage> onUsage,
|
||||
Consumer<String> onToken,
|
||||
Runnable onComplete,
|
||||
Consumer<Throwable> onError) {
|
||||
@@ -81,7 +83,7 @@ public class BrainAiChatClient implements AiChatProvider {
|
||||
// au contrat synchrone du port. L'appelant choisit le thread.
|
||||
flux
|
||||
.timeout(Duration.ofSeconds(120))
|
||||
.doOnNext(sse -> handleEvent(sse, onToken, onError))
|
||||
.doOnNext(sse -> handleEvent(sse, onUsage, onToken, onError))
|
||||
.blockLast();
|
||||
onComplete.run();
|
||||
} catch (Exception e) {
|
||||
@@ -90,9 +92,10 @@ public class BrainAiChatClient implements AiChatProvider {
|
||||
}
|
||||
}
|
||||
|
||||
/** Dispatch selon le type d'événement SSE (data par défaut, done, error). */
|
||||
/** Dispatch selon le type d'événement SSE (data par défaut, done, error, usage). */
|
||||
private void handleEvent(
|
||||
ServerSentEvent<String> sse,
|
||||
Consumer<ChatUsage> onUsage,
|
||||
Consumer<String> onToken,
|
||||
Consumer<Throwable> onError) {
|
||||
String event = sse.event(); // null si pas d'event: xxx -> c'est un data par défaut
|
||||
@@ -106,6 +109,11 @@ public class BrainAiChatClient implements AiChatProvider {
|
||||
if ("done".equals(event)) {
|
||||
return; // la fin est gérée par blockLast + onComplete
|
||||
}
|
||||
if ("usage".equals(event)) {
|
||||
ChatUsage usage = extractUsage(data);
|
||||
if (usage != null) onUsage.accept(usage);
|
||||
return;
|
||||
}
|
||||
// Défaut : événement data avec JSON {"token":"..."}.
|
||||
String token = extractToken(data);
|
||||
if (token != null && !token.isEmpty()) {
|
||||
@@ -113,6 +121,39 @@ public class BrainAiChatClient implements AiChatProvider {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse un JSON {"system":N,"history":N,"current":N,"max":N} en ChatUsage.
|
||||
* Renvoie null si le payload est illisible — dans ce cas on ne propage
|
||||
* simplement pas d'usage, le stream token continue normalement.
|
||||
*/
|
||||
private ChatUsage extractUsage(String json) {
|
||||
if (json == null) return null;
|
||||
try {
|
||||
int system = extractIntField(json, "system");
|
||||
int history = extractIntField(json, "history");
|
||||
int current = extractIntField(json, "current");
|
||||
int max = extractIntField(json, "max");
|
||||
return new ChatUsage(system, history, current, max);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** Parse minimaliste d'un champ entier JSON sans dépendre de Jackson. */
|
||||
private int extractIntField(String json, String field) {
|
||||
String needle = "\"" + field + "\"";
|
||||
int idx = json.indexOf(needle);
|
||||
if (idx < 0) return 0;
|
||||
int colon = json.indexOf(':', idx);
|
||||
if (colon < 0) return 0;
|
||||
int start = colon + 1;
|
||||
while (start < json.length() && Character.isWhitespace(json.charAt(start))) start++;
|
||||
int end = start;
|
||||
while (end < json.length() && (Character.isDigit(json.charAt(end)) || json.charAt(end) == '-')) end++;
|
||||
if (end == start) return 0;
|
||||
return Integer.parseInt(json.substring(start, end));
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse minimaliste du JSON {"token":"..."} sans pull Jackson ici.
|
||||
* Si le format se complexifie, on remplacera par un DTO Jackson.
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
package com.loremind.infrastructure.ai;
|
||||
|
||||
import com.loremind.domain.conversationcontext.ConversationMessage;
|
||||
import com.loremind.domain.conversationcontext.ports.ConversationTitleGenerator;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Adaptateur : appelle le Brain POST /summarize/conversation-title pour
|
||||
* obtenir un titre court a partir des premiers messages.
|
||||
*
|
||||
* Fallback volontairement silencieux : si le Brain est indisponible, on
|
||||
* renvoie un titre par defaut plutot que de casser l'UX chat.
|
||||
*/
|
||||
@Component
|
||||
public class BrainConversationTitleClient implements ConversationTitleGenerator {
|
||||
|
||||
private static final String PATH = "/summarize/conversation-title";
|
||||
private static final String FALLBACK = "Nouvelle conversation";
|
||||
|
||||
private final WebClient webClient;
|
||||
|
||||
public BrainConversationTitleClient(
|
||||
WebClient.Builder builder,
|
||||
@Value("${brain.base-url}") String baseUrl) {
|
||||
this.webClient = builder.baseUrl(baseUrl).build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String generate(List<ConversationMessage> firstMessages) {
|
||||
if (firstMessages == null || firstMessages.isEmpty()) {
|
||||
return FALLBACK;
|
||||
}
|
||||
Map<String, Object> payload = new LinkedHashMap<>();
|
||||
payload.put("messages", firstMessages.stream()
|
||||
.map(m -> Map.<String, Object>of(
|
||||
"role", m.getRole(),
|
||||
"content", m.getContent() == null ? "" : m.getContent()))
|
||||
.collect(Collectors.toList()));
|
||||
|
||||
try {
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> resp = webClient.post()
|
||||
.uri(PATH)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.bodyValue(payload)
|
||||
.retrieve()
|
||||
.bodyToMono(Map.class)
|
||||
.timeout(Duration.ofSeconds(20))
|
||||
.block();
|
||||
if (resp == null) return FALLBACK;
|
||||
Object title = resp.get("title");
|
||||
if (title == null) return FALLBACK;
|
||||
String s = title.toString().trim();
|
||||
return s.isEmpty() ? FALLBACK : s;
|
||||
} catch (Exception e) {
|
||||
return FALLBACK;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package com.loremind.infrastructure.persistence.entity;
|
||||
|
||||
import jakarta.persistence.CascadeType;
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.FetchType;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.Index;
|
||||
import jakarta.persistence.OneToMany;
|
||||
import jakarta.persistence.OrderBy;
|
||||
import jakarta.persistence.PrePersist;
|
||||
import jakarta.persistence.PreUpdate;
|
||||
import jakarta.persistence.Table;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Persistance d'une conversation de chat IA.
|
||||
*
|
||||
* Les refs loreId / campaignId / entityId sont des weak references (String,
|
||||
* pas de FK) — coherent avec la politique inter-contexte du reste du code.
|
||||
* Indexes compose pour accelerer le listing par contexte dans la sidebar.
|
||||
*/
|
||||
@Entity
|
||||
@Table(name = "conversations", indexes = {
|
||||
@Index(name = "idx_conv_lore_entity", columnList = "lore_id,entity_type,entity_id,updated_at"),
|
||||
@Index(name = "idx_conv_campaign_entity", columnList = "campaign_id,entity_type,entity_id,updated_at")
|
||||
})
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class ConversationJpaEntity {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String title;
|
||||
|
||||
@Column(name = "lore_id")
|
||||
private String loreId;
|
||||
|
||||
@Column(name = "campaign_id")
|
||||
private String campaignId;
|
||||
|
||||
@Column(name = "entity_type")
|
||||
private String entityType;
|
||||
|
||||
@Column(name = "entity_id")
|
||||
private String entityId;
|
||||
|
||||
@Column(name = "created_at", nullable = false, updatable = false)
|
||||
private LocalDateTime createdAt;
|
||||
|
||||
@Column(name = "updated_at", nullable = false)
|
||||
private LocalDateTime updatedAt;
|
||||
|
||||
/**
|
||||
* Messages enfants. Charges a la demande (fetch=LAZY) pour ne pas plomber
|
||||
* le listing sidebar. Cascade ALL + orphanRemoval : la suppression d'une
|
||||
* conversation efface ses messages.
|
||||
*/
|
||||
@OneToMany(mappedBy = "conversation", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
|
||||
@OrderBy("createdAt ASC, id ASC")
|
||||
@Builder.Default
|
||||
private List<ConversationMessageJpaEntity> messages = new ArrayList<>();
|
||||
|
||||
@PrePersist
|
||||
protected void onCreate() {
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
createdAt = now;
|
||||
updatedAt = now;
|
||||
}
|
||||
|
||||
@PreUpdate
|
||||
protected void onUpdate() {
|
||||
updatedAt = LocalDateTime.now();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.loremind.infrastructure.persistence.entity;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.JoinColumn;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
import jakarta.persistence.PrePersist;
|
||||
import jakarta.persistence.Table;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.ToString;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* Persistance d'un message appartenant a une {@link ConversationJpaEntity}.
|
||||
* Les messages sont ordonnes par createdAt ASC (ordre d'ajout = ordre lu).
|
||||
*/
|
||||
@Entity
|
||||
@Table(name = "conversation_messages")
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class ConversationMessageJpaEntity {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
/** "user" | "assistant" | "system". */
|
||||
@Column(nullable = false, length = 16)
|
||||
private String role;
|
||||
|
||||
@Column(nullable = false, columnDefinition = "TEXT")
|
||||
private String content;
|
||||
|
||||
@Column(name = "created_at", nullable = false, updatable = false)
|
||||
private LocalDateTime createdAt;
|
||||
|
||||
/**
|
||||
* Reference vers la conversation parent. ToString exclu pour eviter une
|
||||
* boucle infinie quand Lombok genere toString() (conv -> messages -> conv...).
|
||||
*/
|
||||
@ManyToOne(optional = false)
|
||||
@JoinColumn(name = "conversation_id", nullable = false)
|
||||
@ToString.Exclude
|
||||
private ConversationJpaEntity conversation;
|
||||
|
||||
@PrePersist
|
||||
protected void onCreate() {
|
||||
createdAt = LocalDateTime.now();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.loremind.infrastructure.persistence.jpa;
|
||||
|
||||
import com.loremind.infrastructure.persistence.entity.ConversationJpaEntity;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Repository Spring Data JPA pour ConversationJpaEntity.
|
||||
*
|
||||
* Les requetes de listing par contexte gerent explicitement les NULL parce
|
||||
* que JPQL `=` ne matche pas NULL. On combine `IS NULL` / `=` selon si le
|
||||
* filtre est fourni — plus simple qu'une Specification Criteria API.
|
||||
*/
|
||||
@Repository
|
||||
public interface ConversationJpaRepository extends JpaRepository<ConversationJpaEntity, Long> {
|
||||
|
||||
/** Listing Lore racine (entity_type IS NULL). */
|
||||
@Query("""
|
||||
SELECT c FROM ConversationJpaEntity c
|
||||
WHERE c.loreId = :loreId
|
||||
AND c.entityType IS NULL
|
||||
ORDER BY c.updatedAt DESC
|
||||
""")
|
||||
List<ConversationJpaEntity> findByLoreRoot(@Param("loreId") String loreId);
|
||||
|
||||
/** Listing Lore + entite precise. */
|
||||
@Query("""
|
||||
SELECT c FROM ConversationJpaEntity c
|
||||
WHERE c.loreId = :loreId
|
||||
AND c.entityType = :entityType
|
||||
AND c.entityId = :entityId
|
||||
ORDER BY c.updatedAt DESC
|
||||
""")
|
||||
List<ConversationJpaEntity> findByLoreAndEntity(
|
||||
@Param("loreId") String loreId,
|
||||
@Param("entityType") String entityType,
|
||||
@Param("entityId") String entityId);
|
||||
|
||||
/** Listing Campagne racine. */
|
||||
@Query("""
|
||||
SELECT c FROM ConversationJpaEntity c
|
||||
WHERE c.campaignId = :campaignId
|
||||
AND c.entityType IS NULL
|
||||
ORDER BY c.updatedAt DESC
|
||||
""")
|
||||
List<ConversationJpaEntity> findByCampaignRoot(@Param("campaignId") String campaignId);
|
||||
|
||||
/** Listing Campagne + entite precise. */
|
||||
@Query("""
|
||||
SELECT c FROM ConversationJpaEntity c
|
||||
WHERE c.campaignId = :campaignId
|
||||
AND c.entityType = :entityType
|
||||
AND c.entityId = :entityId
|
||||
ORDER BY c.updatedAt DESC
|
||||
""")
|
||||
List<ConversationJpaEntity> findByCampaignAndEntity(
|
||||
@Param("campaignId") String campaignId,
|
||||
@Param("entityType") String entityType,
|
||||
@Param("entityId") String entityId);
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
package com.loremind.infrastructure.persistence.postgres;
|
||||
|
||||
import com.loremind.domain.conversationcontext.Conversation;
|
||||
import com.loremind.domain.conversationcontext.ConversationMessage;
|
||||
import com.loremind.domain.conversationcontext.ports.ConversationRepository;
|
||||
import com.loremind.infrastructure.persistence.entity.ConversationJpaEntity;
|
||||
import com.loremind.infrastructure.persistence.entity.ConversationMessageJpaEntity;
|
||||
import com.loremind.infrastructure.persistence.jpa.ConversationJpaRepository;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Adaptateur Postgres pour ConversationRepository.
|
||||
*
|
||||
* Les methodes de listing ne chargent PAS les messages (messages LAZY,
|
||||
* liste vide renvoyee cote domaine) — la sidebar n'a besoin que des
|
||||
* meta-donnees. findById charge les messages via fetch explicite de la
|
||||
* collection dans une transaction.
|
||||
*/
|
||||
@Repository
|
||||
public class PostgresConversationRepository implements ConversationRepository {
|
||||
|
||||
private final ConversationJpaRepository jpa;
|
||||
|
||||
public PostgresConversationRepository(ConversationJpaRepository jpa) {
|
||||
this.jpa = jpa;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public Conversation save(Conversation conversation) {
|
||||
ConversationJpaEntity entity = toJpaEntity(conversation);
|
||||
ConversationJpaEntity saved = jpa.save(entity);
|
||||
return toDomain(saved, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public Optional<Conversation> findById(String id) {
|
||||
return jpa.findById(Long.parseLong(id))
|
||||
.map(e -> {
|
||||
// Force l'initialisation LAZY avant de sortir de la transaction.
|
||||
e.getMessages().size();
|
||||
return toDomain(e, true);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public List<Conversation> findByContext(String loreId, String campaignId, String entityType, String entityId) {
|
||||
List<ConversationJpaEntity> rows;
|
||||
if (loreId != null) {
|
||||
rows = (entityType == null)
|
||||
? jpa.findByLoreRoot(loreId)
|
||||
: jpa.findByLoreAndEntity(loreId, entityType, entityId);
|
||||
} else if (campaignId != null) {
|
||||
rows = (entityType == null)
|
||||
? jpa.findByCampaignRoot(campaignId)
|
||||
: jpa.findByCampaignAndEntity(campaignId, entityType, entityId);
|
||||
} else {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
return rows.stream().map(e -> toDomain(e, false)).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void deleteById(String id) {
|
||||
jpa.deleteById(Long.parseLong(id));
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public ConversationMessage appendMessage(String conversationId, ConversationMessage message) {
|
||||
ConversationJpaEntity conv = jpa.findById(Long.parseLong(conversationId))
|
||||
.orElseThrow(() -> new EntityNotFoundException("Conversation " + conversationId));
|
||||
|
||||
ConversationMessageJpaEntity msg = ConversationMessageJpaEntity.builder()
|
||||
.role(message.getRole())
|
||||
.content(message.getContent())
|
||||
.conversation(conv)
|
||||
.build();
|
||||
conv.getMessages().add(msg);
|
||||
// Force updatedAt via @PreUpdate en modifiant la conv (touch).
|
||||
conv.setUpdatedAt(java.time.LocalDateTime.now());
|
||||
|
||||
ConversationJpaEntity saved = jpa.save(conv);
|
||||
ConversationMessageJpaEntity persisted = saved.getMessages().get(saved.getMessages().size() - 1);
|
||||
return toDomainMessage(persisted);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void updateTitle(String conversationId, String title) {
|
||||
ConversationJpaEntity conv = jpa.findById(Long.parseLong(conversationId))
|
||||
.orElseThrow(() -> new EntityNotFoundException("Conversation " + conversationId));
|
||||
conv.setTitle(title);
|
||||
jpa.save(conv);
|
||||
}
|
||||
|
||||
// ---------- Mapping ----------
|
||||
|
||||
private ConversationJpaEntity toJpaEntity(Conversation c) {
|
||||
Long id = c.getId() != null ? Long.parseLong(c.getId()) : null;
|
||||
return ConversationJpaEntity.builder()
|
||||
.id(id)
|
||||
.title(c.getTitle())
|
||||
.loreId(c.getLoreId())
|
||||
.campaignId(c.getCampaignId())
|
||||
.entityType(c.getEntityType())
|
||||
.entityId(c.getEntityId())
|
||||
.createdAt(c.getCreatedAt())
|
||||
.updatedAt(c.getUpdatedAt())
|
||||
.build();
|
||||
}
|
||||
|
||||
private Conversation toDomain(ConversationJpaEntity e, boolean withMessages) {
|
||||
List<ConversationMessage> msgs = withMessages
|
||||
? e.getMessages().stream().map(this::toDomainMessage).collect(Collectors.toList())
|
||||
: new java.util.ArrayList<>();
|
||||
return Conversation.builder()
|
||||
.id(e.getId().toString())
|
||||
.title(e.getTitle())
|
||||
.loreId(e.getLoreId())
|
||||
.campaignId(e.getCampaignId())
|
||||
.entityType(e.getEntityType())
|
||||
.entityId(e.getEntityId())
|
||||
.createdAt(e.getCreatedAt())
|
||||
.updatedAt(e.getUpdatedAt())
|
||||
.messages(msgs)
|
||||
.build();
|
||||
}
|
||||
|
||||
private ConversationMessage toDomainMessage(ConversationMessageJpaEntity e) {
|
||||
return ConversationMessage.builder()
|
||||
.id(e.getId() != null ? e.getId().toString() : null)
|
||||
.role(e.getRole())
|
||||
.content(e.getContent())
|
||||
.createdAt(e.getCreatedAt())
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package com.loremind.infrastructure.web.controller;
|
||||
import com.loremind.application.generationcontext.StreamChatForCampaignUseCase;
|
||||
import com.loremind.application.generationcontext.StreamChatForLoreUseCase;
|
||||
import com.loremind.domain.generationcontext.ChatMessage;
|
||||
import com.loremind.domain.generationcontext.ChatUsage;
|
||||
import com.loremind.infrastructure.web.dto.generationcontext.ChatMessageDTO;
|
||||
import com.loremind.infrastructure.web.dto.generationcontext.ChatStreamCampaignRequestDTO;
|
||||
import com.loremind.infrastructure.web.dto.generationcontext.ChatStreamRequestDTO;
|
||||
@@ -80,6 +81,7 @@ public class AiChatController {
|
||||
try {
|
||||
streamChatForLoreUseCase.execute(
|
||||
loreId, pageId, messages,
|
||||
usage -> sendUsage(emitter, usage),
|
||||
token -> sendToken(emitter, token),
|
||||
() -> complete(emitter),
|
||||
error -> fail(emitter, error));
|
||||
@@ -100,6 +102,7 @@ public class AiChatController {
|
||||
try {
|
||||
streamChatForCampaignUseCase.execute(
|
||||
campaignId, entityType, entityId, messages,
|
||||
usage -> sendUsage(emitter, usage),
|
||||
token -> sendToken(emitter, token),
|
||||
() -> complete(emitter),
|
||||
error -> fail(emitter, error));
|
||||
@@ -110,6 +113,18 @@ public class AiChatController {
|
||||
|
||||
// --- Helpers SSE (un seul point d'écriture par type d'événement) --------
|
||||
|
||||
private void sendUsage(SseEmitter emitter, ChatUsage usage) {
|
||||
try {
|
||||
String payload = "{\"system\":" + usage.system()
|
||||
+ ",\"history\":" + usage.history()
|
||||
+ ",\"current\":" + usage.current()
|
||||
+ ",\"max\":" + usage.max() + "}";
|
||||
emitter.send(SseEmitter.event().name("usage").data(payload));
|
||||
} catch (IOException e) {
|
||||
emitter.completeWithError(e);
|
||||
}
|
||||
}
|
||||
|
||||
private void sendToken(SseEmitter emitter, String token) {
|
||||
try {
|
||||
emitter.send(SseEmitter.event()
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
package com.loremind.infrastructure.web.controller;
|
||||
|
||||
import com.loremind.application.conversationcontext.ConversationService;
|
||||
import com.loremind.domain.conversationcontext.Conversation;
|
||||
import com.loremind.domain.conversationcontext.ConversationMessage;
|
||||
import com.loremind.infrastructure.web.dto.conversationcontext.AppendMessageDTO;
|
||||
import com.loremind.infrastructure.web.dto.conversationcontext.ConversationDTO;
|
||||
import com.loremind.infrastructure.web.dto.conversationcontext.ConversationMessageDTO;
|
||||
import com.loremind.infrastructure.web.dto.conversationcontext.CreateConversationDTO;
|
||||
import com.loremind.infrastructure.web.dto.conversationcontext.RenameConversationDTO;
|
||||
import com.loremind.infrastructure.web.mapper.ConversationMapper;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* API REST des conversations persistees.
|
||||
*
|
||||
* GET /api/conversations?loreId=...&entityType=...&entityId=... (listing filtre)
|
||||
* GET /api/conversations?campaignId=...&entityType=...&entityId=...
|
||||
* GET /api/conversations/{id} (detail + messages)
|
||||
* POST /api/conversations (create)
|
||||
* PATCH /api/conversations/{id}/title (rename)
|
||||
* DELETE /api/conversations/{id}
|
||||
*
|
||||
* L'ajout de messages est piloje cote chat stream (use case dedie),
|
||||
* pas par ce controller.
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/api/conversations")
|
||||
public class ConversationController {
|
||||
|
||||
private final ConversationService service;
|
||||
private final ConversationMapper mapper;
|
||||
|
||||
public ConversationController(ConversationService service, ConversationMapper mapper) {
|
||||
this.service = service;
|
||||
this.mapper = mapper;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public ResponseEntity<List<ConversationDTO>> list(
|
||||
@RequestParam(required = false) String loreId,
|
||||
@RequestParam(required = false) String campaignId,
|
||||
@RequestParam(required = false) String entityType,
|
||||
@RequestParam(required = false) String entityId) {
|
||||
List<Conversation> rows = service.listByContext(loreId, campaignId, entityType, entityId);
|
||||
return ResponseEntity.ok(rows.stream().map(mapper::toListDTO).collect(Collectors.toList()));
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public ResponseEntity<ConversationDTO> getById(@PathVariable String id) {
|
||||
return service.getById(id)
|
||||
.map(c -> ResponseEntity.ok(mapper.toDTO(c)))
|
||||
.orElse(ResponseEntity.notFound().build());
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public ResponseEntity<ConversationDTO> create(@RequestBody CreateConversationDTO dto) {
|
||||
Conversation created = service.create(new ConversationService.CreateData(
|
||||
dto.getTitle(),
|
||||
dto.getLoreId(),
|
||||
dto.getCampaignId(),
|
||||
dto.getEntityType(),
|
||||
dto.getEntityId()));
|
||||
return ResponseEntity.ok(mapper.toDTO(created));
|
||||
}
|
||||
|
||||
@PatchMapping("/{id}/title")
|
||||
public ResponseEntity<Void> rename(@PathVariable String id, @RequestBody RenameConversationDTO dto) {
|
||||
service.rename(id, dto.getTitle());
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public ResponseEntity<Void> delete(@PathVariable String id) {
|
||||
service.delete(id);
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
|
||||
@PostMapping("/{id}/messages")
|
||||
public ResponseEntity<ConversationMessageDTO> appendMessage(
|
||||
@PathVariable String id,
|
||||
@RequestBody AppendMessageDTO dto) {
|
||||
ConversationMessage saved = service.appendMessage(id, dto.getRole(), dto.getContent());
|
||||
return ResponseEntity.ok(mapper.toMessageDTO(saved));
|
||||
}
|
||||
|
||||
/**
|
||||
* Auto-genere et persiste un titre base sur les premiers messages.
|
||||
* Appele par le front apres le 1er couple user/assistant.
|
||||
*/
|
||||
@PostMapping("/{id}/auto-title")
|
||||
public ResponseEntity<RenameConversationDTO> autoTitle(@PathVariable String id) {
|
||||
String title = service.autoGenerateTitle(id);
|
||||
return ResponseEntity.ok(RenameConversationDTO.builder().title(title).build());
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
@@ -53,6 +54,11 @@ public class SettingsController {
|
||||
return forward(HttpMethod.GET, "/models/ollama", null);
|
||||
}
|
||||
|
||||
@PostMapping("/models/ollama/info")
|
||||
public ResponseEntity<Map<String, Object>> getOllamaModelInfo(@RequestBody Map<String, Object> body) {
|
||||
return forward(HttpMethod.POST, "/models/ollama/info", body);
|
||||
}
|
||||
|
||||
@GetMapping("/models/onemin")
|
||||
public ResponseEntity<Map<String, Object>> listOneMinModels() {
|
||||
return forward(HttpMethod.GET, "/models/onemin", null);
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.loremind.infrastructure.web.dto.conversationcontext;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class AppendMessageDTO {
|
||||
/** "user" | "assistant" | "system". */
|
||||
private String role;
|
||||
private String content;
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.loremind.infrastructure.web.dto.conversationcontext;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* DTO d'une conversation. Les messages sont inclus uniquement sur GET /{id}
|
||||
* (null pour les reponses de listing afin d'alleger la sidebar).
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class ConversationDTO {
|
||||
private String id;
|
||||
private String title;
|
||||
private String loreId;
|
||||
private String campaignId;
|
||||
private String entityType;
|
||||
private String entityId;
|
||||
private LocalDateTime createdAt;
|
||||
private LocalDateTime updatedAt;
|
||||
private List<ConversationMessageDTO> messages;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.loremind.infrastructure.web.dto.conversationcontext;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class ConversationMessageDTO {
|
||||
private String id;
|
||||
private String role;
|
||||
private String content;
|
||||
private LocalDateTime createdAt;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.loremind.infrastructure.web.dto.conversationcontext;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* Payload de creation. Le client fournit l'ancrage (lore ou campagne, +/-
|
||||
* entite focus). Le titre est optionnel — sera auto-genere apres le 1er
|
||||
* echange IA si absent.
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class CreateConversationDTO {
|
||||
private String title;
|
||||
private String loreId;
|
||||
private String campaignId;
|
||||
private String entityType;
|
||||
private String entityId;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.loremind.infrastructure.web.dto.conversationcontext;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class RenameConversationDTO {
|
||||
private String title;
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package com.loremind.infrastructure.web.mapper;
|
||||
|
||||
import com.loremind.domain.conversationcontext.Conversation;
|
||||
import com.loremind.domain.conversationcontext.ConversationMessage;
|
||||
import com.loremind.infrastructure.web.dto.conversationcontext.ConversationDTO;
|
||||
import com.loremind.infrastructure.web.dto.conversationcontext.ConversationMessageDTO;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Conversion Domaine <-> DTO pour le contexte Conversation.
|
||||
*
|
||||
* {@link #toListDTO(Conversation)} omet les messages — utilise pour le
|
||||
* listing sidebar ou on n'expose que les metadonnees.
|
||||
*/
|
||||
@Component
|
||||
public class ConversationMapper {
|
||||
|
||||
public ConversationDTO toDTO(Conversation c) {
|
||||
List<ConversationMessageDTO> msgs = c.getMessages() == null
|
||||
? List.of()
|
||||
: c.getMessages().stream().map(this::toMessageDTO).collect(Collectors.toList());
|
||||
return ConversationDTO.builder()
|
||||
.id(c.getId())
|
||||
.title(c.getTitle())
|
||||
.loreId(c.getLoreId())
|
||||
.campaignId(c.getCampaignId())
|
||||
.entityType(c.getEntityType())
|
||||
.entityId(c.getEntityId())
|
||||
.createdAt(c.getCreatedAt())
|
||||
.updatedAt(c.getUpdatedAt())
|
||||
.messages(msgs)
|
||||
.build();
|
||||
}
|
||||
|
||||
/** Variante listing : pas de messages pour alleger la payload. */
|
||||
public ConversationDTO toListDTO(Conversation c) {
|
||||
return ConversationDTO.builder()
|
||||
.id(c.getId())
|
||||
.title(c.getTitle())
|
||||
.loreId(c.getLoreId())
|
||||
.campaignId(c.getCampaignId())
|
||||
.entityType(c.getEntityType())
|
||||
.entityId(c.getEntityId())
|
||||
.createdAt(c.getCreatedAt())
|
||||
.updatedAt(c.getUpdatedAt())
|
||||
.messages(null)
|
||||
.build();
|
||||
}
|
||||
|
||||
public ConversationMessageDTO toMessageDTO(ConversationMessage m) {
|
||||
return ConversationMessageDTO.builder()
|
||||
.id(m.getId())
|
||||
.role(m.getRole())
|
||||
.content(m.getContent())
|
||||
.createdAt(m.getCreatedAt())
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import com.loremind.domain.campaigncontext.ports.CampaignRepository;
|
||||
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.LoreStructuralContext;
|
||||
import com.loremind.domain.generationcontext.NarrativeEntityContext;
|
||||
import com.loremind.domain.generationcontext.ports.AiChatProvider;
|
||||
@@ -46,6 +47,7 @@ public class StreamChatForCampaignUseCaseTest {
|
||||
|
||||
private CampaignStructuralContext campaignCtx;
|
||||
private List<ChatMessage> messages;
|
||||
private Consumer<ChatUsage> onUsage;
|
||||
private Consumer<String> onToken;
|
||||
private Runnable onComplete;
|
||||
private Consumer<Throwable> onError;
|
||||
@@ -57,6 +59,7 @@ public class StreamChatForCampaignUseCaseTest {
|
||||
.campaignName("X").campaignDescription("d")
|
||||
.build();
|
||||
messages = List.of();
|
||||
onUsage = mock(Consumer.class);
|
||||
onToken = mock(Consumer.class);
|
||||
onComplete = mock(Runnable.class);
|
||||
onError = mock(Consumer.class);
|
||||
@@ -67,7 +70,7 @@ public class StreamChatForCampaignUseCaseTest {
|
||||
when(campaignRepository.findById("missing")).thenReturn(Optional.empty());
|
||||
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> useCase.execute("missing", null, null, messages, onToken, onComplete, onError));
|
||||
() -> useCase.execute("missing", null, null, messages, onUsage, onToken, onComplete, onError));
|
||||
verifyNoInteractions(aiChatProvider);
|
||||
}
|
||||
|
||||
@@ -77,10 +80,10 @@ public class StreamChatForCampaignUseCaseTest {
|
||||
when(campaignRepository.findById("c-1")).thenReturn(Optional.of(standalone));
|
||||
when(campaignContextBuilder.build("c-1")).thenReturn(campaignCtx);
|
||||
|
||||
useCase.execute("c-1", null, null, messages, onToken, onComplete, onError);
|
||||
useCase.execute("c-1", null, null, messages, onUsage, onToken, onComplete, onError);
|
||||
|
||||
ArgumentCaptor<ChatRequest> captor = ArgumentCaptor.forClass(ChatRequest.class);
|
||||
verify(aiChatProvider).streamChat(captor.capture(), eq(onToken), eq(onComplete), eq(onError));
|
||||
verify(aiChatProvider).streamChat(captor.capture(), eq(onUsage), eq(onToken), eq(onComplete), eq(onError));
|
||||
ChatRequest req = captor.getValue();
|
||||
assertSame(campaignCtx, req.getCampaignContext());
|
||||
assertNull(req.getLoreContext());
|
||||
@@ -100,10 +103,10 @@ public class StreamChatForCampaignUseCaseTest {
|
||||
when(campaignContextBuilder.build("c-1")).thenReturn(campaignCtx);
|
||||
when(loreContextBuilder.buildOptional("lore-1")).thenReturn(Optional.of(loreCtx));
|
||||
|
||||
useCase.execute("c-1", null, null, messages, onToken, onComplete, onError);
|
||||
useCase.execute("c-1", null, null, messages, onUsage, onToken, onComplete, onError);
|
||||
|
||||
ArgumentCaptor<ChatRequest> captor = ArgumentCaptor.forClass(ChatRequest.class);
|
||||
verify(aiChatProvider).streamChat(captor.capture(), any(), any(), any());
|
||||
verify(aiChatProvider).streamChat(captor.capture(), any(), any(), any(), any());
|
||||
assertSame(loreCtx, captor.getValue().getLoreContext());
|
||||
}
|
||||
|
||||
@@ -115,10 +118,10 @@ public class StreamChatForCampaignUseCaseTest {
|
||||
when(campaignContextBuilder.build("c-1")).thenReturn(campaignCtx);
|
||||
when(loreContextBuilder.buildOptional("lore-ghost")).thenReturn(Optional.empty());
|
||||
|
||||
useCase.execute("c-1", null, null, messages, onToken, onComplete, onError);
|
||||
useCase.execute("c-1", null, null, messages, onUsage, onToken, onComplete, onError);
|
||||
|
||||
ArgumentCaptor<ChatRequest> captor = ArgumentCaptor.forClass(ChatRequest.class);
|
||||
verify(aiChatProvider).streamChat(captor.capture(), any(), any(), any());
|
||||
verify(aiChatProvider).streamChat(captor.capture(), any(), any(), any(), any());
|
||||
assertNull(captor.getValue().getLoreContext());
|
||||
// La requete doit tout de meme partir (pas d'exception).
|
||||
}
|
||||
@@ -133,10 +136,10 @@ public class StreamChatForCampaignUseCaseTest {
|
||||
when(campaignContextBuilder.build("c-1")).thenReturn(campaignCtx);
|
||||
when(narrativeEntityContextBuilder.build("scene", "s-1")).thenReturn(entity);
|
||||
|
||||
useCase.execute("c-1", "scene", "s-1", messages, onToken, onComplete, onError);
|
||||
useCase.execute("c-1", "scene", "s-1", messages, onUsage, onToken, onComplete, onError);
|
||||
|
||||
ArgumentCaptor<ChatRequest> captor = ArgumentCaptor.forClass(ChatRequest.class);
|
||||
verify(aiChatProvider).streamChat(captor.capture(), any(), any(), any());
|
||||
verify(aiChatProvider).streamChat(captor.capture(), any(), any(), any(), any());
|
||||
assertSame(entity, captor.getValue().getNarrativeEntity());
|
||||
}
|
||||
|
||||
@@ -146,10 +149,10 @@ public class StreamChatForCampaignUseCaseTest {
|
||||
when(campaignRepository.findById("c-1")).thenReturn(Optional.of(standalone));
|
||||
when(campaignContextBuilder.build("c-1")).thenReturn(campaignCtx);
|
||||
|
||||
useCase.execute("c-1", "scene", " ", messages, onToken, onComplete, onError);
|
||||
useCase.execute("c-1", "scene", " ", messages, onUsage, onToken, onComplete, onError);
|
||||
|
||||
ArgumentCaptor<ChatRequest> captor = ArgumentCaptor.forClass(ChatRequest.class);
|
||||
verify(aiChatProvider).streamChat(captor.capture(), any(), any(), any());
|
||||
verify(aiChatProvider).streamChat(captor.capture(), any(), any(), any(), any());
|
||||
assertNull(captor.getValue().getNarrativeEntity());
|
||||
verifyNoInteractions(narrativeEntityContextBuilder);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.loremind.application.generationcontext;
|
||||
|
||||
import com.loremind.domain.generationcontext.ChatMessage;
|
||||
import com.loremind.domain.generationcontext.ChatRequest;
|
||||
import com.loremind.domain.generationcontext.ChatUsage;
|
||||
import com.loremind.domain.generationcontext.LoreStructuralContext;
|
||||
import com.loremind.domain.generationcontext.ports.AiChatProvider;
|
||||
import com.loremind.domain.lorecontext.FieldType;
|
||||
@@ -46,6 +47,7 @@ public class StreamChatForLoreUseCaseTest {
|
||||
|
||||
private LoreStructuralContext loreCtx;
|
||||
private List<ChatMessage> messages;
|
||||
private Consumer<ChatUsage> onUsage;
|
||||
private Consumer<String> onToken;
|
||||
private Runnable onComplete;
|
||||
private Consumer<Throwable> onError;
|
||||
@@ -58,6 +60,7 @@ public class StreamChatForLoreUseCaseTest {
|
||||
.folders(Collections.emptyMap())
|
||||
.build();
|
||||
messages = List.of();
|
||||
onUsage = mock(Consumer.class);
|
||||
onToken = mock(Consumer.class);
|
||||
onComplete = mock(Runnable.class);
|
||||
onError = mock(Consumer.class);
|
||||
@@ -67,10 +70,10 @@ public class StreamChatForLoreUseCaseTest {
|
||||
void testExecute_NoPageId_SendsRequestWithoutPageContext() {
|
||||
when(loreContextBuilder.build("lore-1")).thenReturn(loreCtx);
|
||||
|
||||
useCase.execute("lore-1", null, messages, onToken, onComplete, onError);
|
||||
useCase.execute("lore-1", null, messages, onUsage, onToken, onComplete, onError);
|
||||
|
||||
ArgumentCaptor<ChatRequest> captor = ArgumentCaptor.forClass(ChatRequest.class);
|
||||
verify(aiChatProvider).streamChat(captor.capture(), eq(onToken), eq(onComplete), eq(onError));
|
||||
verify(aiChatProvider).streamChat(captor.capture(), eq(onUsage), eq(onToken), eq(onComplete), eq(onError));
|
||||
ChatRequest req = captor.getValue();
|
||||
assertSame(loreCtx, req.getLoreContext());
|
||||
assertNull(req.getPageContext());
|
||||
@@ -81,10 +84,10 @@ public class StreamChatForLoreUseCaseTest {
|
||||
void testExecute_BlankPageId_TreatedAsNoPage() {
|
||||
when(loreContextBuilder.build("lore-1")).thenReturn(loreCtx);
|
||||
|
||||
useCase.execute("lore-1", " ", messages, onToken, onComplete, onError);
|
||||
useCase.execute("lore-1", " ", messages, onUsage, onToken, onComplete, onError);
|
||||
|
||||
ArgumentCaptor<ChatRequest> captor = ArgumentCaptor.forClass(ChatRequest.class);
|
||||
verify(aiChatProvider).streamChat(captor.capture(), any(), any(), any());
|
||||
verify(aiChatProvider).streamChat(captor.capture(), any(), any(), any(), any());
|
||||
assertNull(captor.getValue().getPageContext());
|
||||
verifyNoInteractions(pageRepository);
|
||||
}
|
||||
@@ -108,10 +111,10 @@ public class StreamChatForLoreUseCaseTest {
|
||||
when(pageRepository.findById("p-1")).thenReturn(Optional.of(page));
|
||||
when(templateRepository.findById("tpl-1")).thenReturn(Optional.of(tpl));
|
||||
|
||||
useCase.execute("lore-1", "p-1", messages, onToken, onComplete, onError);
|
||||
useCase.execute("lore-1", "p-1", messages, onUsage, onToken, onComplete, onError);
|
||||
|
||||
ArgumentCaptor<ChatRequest> captor = ArgumentCaptor.forClass(ChatRequest.class);
|
||||
verify(aiChatProvider).streamChat(captor.capture(), any(), any(), any());
|
||||
verify(aiChatProvider).streamChat(captor.capture(), any(), any(), any(), any());
|
||||
ChatRequest req = captor.getValue();
|
||||
assertNotNull(req.getPageContext());
|
||||
assertEquals("Alice", req.getPageContext().getTitle());
|
||||
@@ -130,10 +133,10 @@ public class StreamChatForLoreUseCaseTest {
|
||||
when(loreContextBuilder.build("lore-1")).thenReturn(loreCtx);
|
||||
when(pageRepository.findById("p-1")).thenReturn(Optional.of(page));
|
||||
|
||||
useCase.execute("lore-1", "p-1", messages, onToken, onComplete, onError);
|
||||
useCase.execute("lore-1", "p-1", messages, onUsage, onToken, onComplete, onError);
|
||||
|
||||
ArgumentCaptor<ChatRequest> captor = ArgumentCaptor.forClass(ChatRequest.class);
|
||||
verify(aiChatProvider).streamChat(captor.capture(), any(), any(), any());
|
||||
verify(aiChatProvider).streamChat(captor.capture(), any(), any(), any(), any());
|
||||
var pageCtx = captor.getValue().getPageContext();
|
||||
assertNotNull(pageCtx);
|
||||
assertEquals("Orphan", pageCtx.getTitle());
|
||||
@@ -153,10 +156,10 @@ public class StreamChatForLoreUseCaseTest {
|
||||
when(pageRepository.findById("p-1")).thenReturn(Optional.of(page));
|
||||
when(templateRepository.findById("tpl-ghost")).thenReturn(Optional.empty());
|
||||
|
||||
useCase.execute("lore-1", "p-1", messages, onToken, onComplete, onError);
|
||||
useCase.execute("lore-1", "p-1", messages, onUsage, onToken, onComplete, onError);
|
||||
|
||||
ArgumentCaptor<ChatRequest> captor = ArgumentCaptor.forClass(ChatRequest.class);
|
||||
verify(aiChatProvider).streamChat(captor.capture(), any(), any(), any());
|
||||
verify(aiChatProvider).streamChat(captor.capture(), any(), any(), any(), any());
|
||||
var pageCtx = captor.getValue().getPageContext();
|
||||
assertEquals("?", pageCtx.getTemplateName());
|
||||
assertTrue(pageCtx.getTemplateFields().isEmpty());
|
||||
@@ -168,7 +171,7 @@ public class StreamChatForLoreUseCaseTest {
|
||||
when(pageRepository.findById("missing")).thenReturn(Optional.empty());
|
||||
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> useCase.execute("lore-1", "missing", messages, onToken, onComplete, onError));
|
||||
() -> useCase.execute("lore-1", "missing", messages, onUsage, onToken, onComplete, onError));
|
||||
verifyNoInteractions(aiChatProvider);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user