import { HttpClient, HttpParams } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { Observable } from 'rxjs'; import { Conversation, ConversationContext, ConversationMessage, CreateConversationPayload, } from './conversation.model'; /** * Service HTTP des conversations persistees. * * Le streaming (chat/stream) reste pris en charge par AiChatService. Ce * service ne gere que la persistance (metadonnees + messages) et * l'auto-titre declenche apres le 1er echange. */ @Injectable({ providedIn: 'root' }) export class ConversationService { private readonly apiUrl = '/api/conversations'; constructor(private http: HttpClient) {} list(ctx: ConversationContext): Observable { let params = new HttpParams(); if (ctx.loreId) params = params.set('loreId', ctx.loreId); if (ctx.campaignId) params = params.set('campaignId', ctx.campaignId); if (ctx.entityType) params = params.set('entityType', ctx.entityType); if (ctx.entityId) params = params.set('entityId', ctx.entityId); return this.http.get(this.apiUrl, { params }); } getById(id: string): Observable { return this.http.get(`${this.apiUrl}/${id}`); } create(payload: CreateConversationPayload): Observable { return this.http.post(this.apiUrl, payload); } rename(id: string, title: string): Observable { return this.http.patch(`${this.apiUrl}/${id}/title`, { title }); } delete(id: string): Observable { return this.http.delete(`${this.apiUrl}/${id}`); } appendMessage( id: string, role: 'user' | 'assistant' | 'system', content: string, ): Observable { return this.http.post(`${this.apiUrl}/${id}/messages`, { role, content, }); } /** Declenche la generation auto du titre cote Brain. */ autoTitle(id: string): Observable<{ title: string }> { return this.http.post<{ title: string }>(`${this.apiUrl}/${id}/auto-title`, {}); } }