|
|
@@ -34,15 +34,21 @@ const blankMeta = () => ({
|
|
|
export const useChatStore = defineStore('chat', () => {
|
|
|
const messages = ref<Message[]>([]);
|
|
|
const isLoading = ref(false);
|
|
|
+ const sessionId = ref(crypto.randomUUID());
|
|
|
|
|
|
const userStore = useUserStore();
|
|
|
|
|
|
+ const newConversation = () => {
|
|
|
+ sessionId.value = crypto.randomUUID();
|
|
|
+ messages.value = [];
|
|
|
+ };
|
|
|
+
|
|
|
const loadChatHistory = async () => {
|
|
|
if (!userStore.userId) return;
|
|
|
try {
|
|
|
const { data } = await axios.get(
|
|
|
`${import.meta.env.VITE_API_URL}/chat/history`,
|
|
|
- { headers: { 'Authorization': `Bearer ${userStore.userId}` } }
|
|
|
+ { headers: { 'Authorization': `Bearer ${userStore.userId}` }, params: { sessionId: sessionId.value } }
|
|
|
);
|
|
|
messages.value = data
|
|
|
.filter((msg: FormattedMessage) => msg.content)
|
|
|
@@ -74,7 +80,7 @@ export const useChatStore = defineStore('chat', () => {
|
|
|
} else {
|
|
|
const { data } = await axios.post(
|
|
|
`${import.meta.env.VITE_API_URL}/chat/chat`,
|
|
|
- { messages: [{ role: 'user', content: message }], stream: false },
|
|
|
+ { messages: [{ role: 'user', content: message }], stream: false, sessionId: sessionId.value },
|
|
|
{ headers: { 'Authorization': `Bearer ${userStore.userId}`, 'Content-Type': 'application/json' } }
|
|
|
);
|
|
|
messages.value.push({
|
|
|
@@ -119,7 +125,7 @@ export const useChatStore = defineStore('chat', () => {
|
|
|
'Content-Type': 'application/json',
|
|
|
'Authorization': `Bearer ${userStore.userId}`,
|
|
|
},
|
|
|
- body: JSON.stringify({ messages: historySnapshot, stream: true }),
|
|
|
+ body: JSON.stringify({ messages: historySnapshot, stream: true, sessionId: sessionId.value }),
|
|
|
});
|
|
|
|
|
|
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
|
|
|
@@ -213,5 +219,5 @@ export const useChatStore = defineStore('chat', () => {
|
|
|
}
|
|
|
};
|
|
|
|
|
|
- return { messages, isLoading, loadChatHistory, sendMessage };
|
|
|
+ return { messages, isLoading, sessionId, loadChatHistory, sendMessage, newConversation };
|
|
|
});
|