Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
public class ChatMessage {
private String role;
private String content;
private String senderName; //For chat
private LocalDateTime timeStamp;

public ChatMessage(String role, String message) {
public ChatMessage(String role, String message, String senderName) {
this.role = role;
this.content = message;
this.senderName = senderName;
this.timeStamp = LocalDateTime.now();//Adds the current time to each message
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ public String chatWithLLM(ChatRequestDTO dto) {
try {
String content = fetchResponseFromLLM(apiMessages);
// Only store in history if we got a real LLM response (not a circuit breaker fallback)
chatSession.addMessage(new ChatMessage("user", dto.message()));
chatSession.addMessage(new ChatMessage("assistant", content));
chatSession.addMessage(new ChatMessage("user", dto.message(), "You"));
chatSession.addMessage(new ChatMessage("assistant", content, dto.personality().toString()));
return content;
} catch (RetryableHttpException e) {
// Circuit breaker is open or service unavailable - don't pollute history
Expand Down
40 changes: 37 additions & 3 deletions src/main/resources/static/app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
const getSessionId = () => {
let id = localStorage.getItem('bifrost_session_id'); //Try to find the old sessionID or create a new one
if (!id) {
id = crypto.randomUUID();
localStorage.setItem('bifrost_session_id', id);
}
return id;
};

const chatState = {
sessionId: crypto.randomUUID(),
isWaiting: false //Flag for waiting for response, to prevent multiple sends
sessionId: getSessionId(),
isWaiting: false
};

//Save all HTML-elements to reduce boilerplate
Expand Down Expand Up @@ -135,4 +144,29 @@ dom.input.addEventListener('keydown', (e) => {
sendMessage().then(r => {
}).catch(err => console.error(err));
}
});
});

// Körs automatiskt när sidan laddas om
window.addEventListener('DOMContentLoaded', async () => {
try {
// Vi anropar din befintliga GetMapping: /api/v1/chat/{sessionId}
const response = await fetch(`/api/v1/chat/${chatState.sessionId}`);

if (response.ok) {
const sessionData = await response.json(); // Detta matchar din ChatSession-klass

// if there is sessionData.chatHistory saved, we loop through it and display the messages in the chat window
if (sessionData && sessionData.chatHistory && sessionData.chatHistory.length > 0) {
sessionData.chatHistory.forEach(msg => {
const role = msg.role === 'assistant' ? 'assistant' : 'user';
// Name saved from backend
const name = role === 'assistant' ? getGodName(msg.senderName) : null;

appendMessage(role, name, msg.content);
});
}
}
} catch (err) {
console.error("Could not fetch chat history:", err);
}
});
Loading