Moved away from SSR to regular Node API server.

This commit is contained in:
Cameron Redmore 2025-04-25 12:50:44 +01:00
parent 9aea69c7be
commit 83d93aefc0
30 changed files with 939 additions and 1024 deletions

View file

@ -1,8 +1,8 @@
import { defineStore } from 'pinia';
import { ref, computed, watch } from 'vue'; // Import watch
import axios from 'axios';
import axios from 'boot/axios';
export const useChatStore = defineStore('chat', () =>
export const useChatStore = defineStore('chat', () =>
{
const isVisible = ref(false);
const currentThreadId = ref(null);
@ -19,13 +19,13 @@ export const useChatStore = defineStore('chat', () =>
// --- Actions ---
// New action to create a thread if it doesn't exist
async function createThreadIfNotExists()
async function createThreadIfNotExists()
{
if (currentThreadId.value) return; // Already have a thread
isLoading.value = true;
error.value = null;
try
try
{
// Call the endpoint without content to just create the thread
const response = await axios.post('/api/chat/threads', {});
@ -34,50 +34,50 @@ export const useChatStore = defineStore('chat', () =>
console.log('Created new chat thread:', currentThreadId.value);
// Start polling now that we have a thread ID
startPolling();
}
catch (err)
}
catch (err)
{
console.error('Error creating chat thread:', err);
error.value = 'Failed to start chat.';
// Don't set isVisible to false, let the user see the error
}
finally
}
finally
{
isLoading.value = false;
}
}
function toggleChat()
function toggleChat()
{
isVisible.value = !isVisible.value;
if (isVisible.value)
if (isVisible.value)
{
if (!currentThreadId.value)
if (!currentThreadId.value)
{
// If opening and no thread exists, create one
createThreadIfNotExists();
}
else
}
else
{
// If opening and thread exists, fetch messages if empty and start polling
if (messages.value.length === 0)
if (messages.value.length === 0)
{
fetchMessages();
}
startPolling();
}
}
else
}
else
{
// If closing, stop polling
stopPolling();
}
}
async function fetchMessages()
async function fetchMessages()
{
if (!currentThreadId.value)
if (!currentThreadId.value)
{
console.log('No active thread to fetch messages for.');
// Don't try to fetch if no thread ID yet. createThreadIfNotExists handles the initial state.
@ -86,7 +86,7 @@ export const useChatStore = defineStore('chat', () =>
// Avoid setting isLoading if polling, maybe use a different flag? For now, keep it simple.
// isLoading.value = true; // Might cause flickering during polling
error.value = null; // Clear previous errors on fetch attempt
try
try
{
const response = await axios.get(`/api/chat/threads/${currentThreadId.value}/messages`);
const newMessages = response.data.map(msg => ({
@ -97,28 +97,28 @@ export const useChatStore = defineStore('chat', () =>
})).sort((a, b) => a.createdAt - b.createdAt);
// Only update if messages have actually changed to prevent unnecessary re-renders
if (JSON.stringify(messages.value) !== JSON.stringify(newMessages))
if (JSON.stringify(messages.value) !== JSON.stringify(newMessages))
{
messages.value = newMessages;
}
}
catch (err)
}
catch (err)
{
console.error('Error fetching messages:', err);
error.value = 'Failed to load messages.';
// Don't clear messages on polling error, keep the last known state
// messages.value = [];
stopPolling(); // Stop polling if there's an error fetching
}
finally
}
finally
{
// isLoading.value = false;
}
}
// Function to start polling
function startPolling()
function startPolling()
{
if (pollingIntervalId.value) return; // Already polling
if (!currentThreadId.value) return; // No thread to poll for
@ -128,9 +128,9 @@ export const useChatStore = defineStore('chat', () =>
}
// Function to stop polling
function stopPolling()
function stopPolling()
{
if (pollingIntervalId.value)
if (pollingIntervalId.value)
{
console.log('Stopping chat polling.');
clearInterval(pollingIntervalId.value);
@ -139,10 +139,10 @@ export const useChatStore = defineStore('chat', () =>
}
async function sendMessage(content)
async function sendMessage(content)
{
if (!content.trim()) return;
if (!currentThreadId.value)
if (!currentThreadId.value)
{
error.value = 'Cannot send message: No active chat thread.';
console.error('Attempted to send message without a thread ID.');
@ -165,7 +165,7 @@ export const useChatStore = defineStore('chat', () =>
isLoading.value = true; // Indicate activity
error.value = null;
try
try
{
const payload = { content: userMessage.content };
// Always post to the existing thread once it's created
@ -180,8 +180,8 @@ export const useChatStore = defineStore('chat', () =>
// Immediately fetch messages after sending to get the updated list
await fetchMessages();
}
catch (err)
}
catch (err)
{
console.error('Error sending message:', err);
error.value = 'Failed to send message.';
@ -190,8 +190,8 @@ export const useChatStore = defineStore('chat', () =>
// Optionally add an error message to the chat
// Ensure the object is correctly formatted
messages.value.push({ sender: 'bot', content: "Sorry, I couldn't send that message.", createdAt: new Date() });
}
finally
}
finally
{
isLoading.value = false;
// Restart polling after sending attempt is complete
@ -200,7 +200,7 @@ export const useChatStore = defineStore('chat', () =>
}
// Call this when the user logs out or the app closes if you want to clear state
function resetChat()
function resetChat()
{
stopPolling(); // Ensure polling stops on reset
isVisible.value = false;