fix: fixed the chatbox server side communication

This commit is contained in:
Lion Arar 2025-10-01 14:10:34 -04:00
parent 8ac36ec4fc
commit 4aeafb1087
5 changed files with 46 additions and 34 deletions

View File

@ -2,11 +2,11 @@
import { isChatVisible } from "src/stores/dialogue-box"; import { isChatVisible } from "src/stores/dialogue-box";
import DialogueContent from "./dialogue-content.vue"; import DialogueContent from "./dialogue-content.vue";
import { ref } from "vue"; import { ref } from "vue";
import type { Message } from "../types/dialogue-message";
import { useChatApi } from "../composables/chat-api"; import { useChatApi } from "../composables/chat-api";
import { useChatStore } from "src/stores/message-store";
const text = ref(''); const text = ref('');
const messages = ref<Message[]>([]); const { messages } = useChatStore();
const { sendMessage } = useChatApi(); const { sendMessage } = useChatApi();
const handleSend = async () => { const handleSend = async () => {
@ -19,14 +19,26 @@ const handleSend = async () => {
<template> <template>
<transition name="fade-slide"> <transition name="fade-slide">
<div v-if="isChatVisible" class="dialogue"> <div
v-if="isChatVisible"
class="dialogue"
>
<div class="chat-header">AI Assistant</div> <div class="chat-header">AI Assistant</div>
<div class="chat-body"> <div class="chat-body">
<DialogueContent :messages="messages"></DialogueContent> <DialogueContent :messages="messages"></DialogueContent>
</div> </div>
<q-form v-on:submit="handleSend" class="row chat-footer"> <q-form
<q-input v-model="text" label="Enter a Message" autogrow class="col" style="margin-right: 5px;" v-on:submit="handleSend"
@keydown.enter="handleSend" /> class="row chat-footer"
>
<q-input
v-model="text"
label="Enter a Message"
autogrow
class="col"
style="margin-right: 5px;"
@keydown.enter="handleSend"
/>
</q-form> </q-form>
</div> </div>
</transition> </transition>

View File

@ -9,6 +9,11 @@ defineProps<{
<template> <template>
<q-infinite-scroll> <q-infinite-scroll>
<DialoguePhrase v-for="(msg, index) in messages" :key="index" :text="msg.text" :sent="msg.sent" /> <DialoguePhrase
v-for="(msg, index) in messages"
:key="index"
:text="msg.text"
:sent="msg.sent"
/>
</q-infinite-scroll> </q-infinite-scroll>
</template> </template>

View File

@ -1,6 +1,5 @@
// composables/chat-api.ts // composables/chat-api.ts
import { useChatStore } from "src/stores/message-store" import { useChatStore } from "src/stores/message-store";
export const useChatApi = () => { export const useChatApi = () => {
const chatStore = useChatStore(); const chatStore = useChatStore();
@ -10,4 +9,4 @@ export const useChatApi = () => {
}; };
return { sendMessage }; return { sendMessage };
}; };

View File

@ -2,11 +2,11 @@ import type { Message } from "../types/dialogue-message";
import { api } from "src/boot/axios"; import { api } from "src/boot/axios";
export const chatbotService = { export const chatbotService = {
getChatMessage: async (userInput: string): Promise<Message> => { getChatMessage: async (userInput: string): Promise<Message> => {
console.log('messageService'); console.log('messageService');
const response = await api.post('/chat/respond'); const response = await api.post('/chat/respond', {userInput});
console.log({userInput}); console.log({userInput});
return response.data as Message; return response.data as Message;
} }
}; };

View File

@ -1,27 +1,23 @@
import type { Message } from "src/modules/chatbot/types/dialogue-message"; import type { Message } from "src/modules/chatbot/types/dialogue-message";
import { defineStore } from "pinia"; import { defineStore } from "pinia";
import { chatbotService } from "src/modules/chatbot/services/messageService"; import { chatbotService } from "src/modules/chatbot/services/messageService";
import { ref } from "vue";
// const localMessages: Message[] = []; export const useChatStore = defineStore("chat", () => {
const messages = ref<Message[]>([{ text: "test", sent: false }]);
// export const saveMessage = (msg: Message) =>{ const sendMessage = async (userInput: string) => {
// localMessages.push(msg); console.log(`'useChatStore' ${userInput}`);
// };
// export const getStoredMessages = (): Message[] =>{ const reply = await chatbotService.getChatMessage(userInput);
// return [...localMessages];
// }
export const useChatStore = defineStore('chat', { messages.value.push({ text: userInput, sent: true });
state: () => ({ messages.value.push(reply);
messages: [] as Message[], return reply;
}), };
actions: {
async sendMessage(userInput: string) { return {
console.log(`'useChatStore' ${userInput}`); messages,
const reply = await chatbotService.getChatMessage(userInput); sendMessage,
this.messages.push({ text: userInput, sent: true }); };
this.messages.push(reply);
},
},
}); });