feat: Prototype chat-response: made a message store and service, so that the current chatbox sends the inputted txt and retrieves the defined mock response.

This commit is contained in:
Lion Arar 2025-09-25 10:57:04 -04:00
parent 398ef5f888
commit 76f21e56ab
6 changed files with 56 additions and 26 deletions

View File

@ -1,34 +1,31 @@
<script setup lang="ts"> <script setup lang="ts">
import { isChatVisible } from "src/stores/dialogue-box"; import { isChatVisible } from "src/stores/dialogue-box";
import { provide, ref } from "vue";
import DialogueContent from "./dialogue-content.vue"; import DialogueContent from "./dialogue-content.vue";
import { Message } from "../types/dialogue-message"; import { ref } from "vue";
import { saveMessage } from "src/stores/message-store";
import { getResponse } from "../services/messageService";
import type { Message } from "../types/dialogue-message";
const text = ref(""); const text = ref('');
const messages = ref<Message[]>([]); const messages = ref<Message[]>([]);
const sendMessage = () => { const sendMessage = async () => {
if (!text.value.trim()) return; if (!text.value.trim()) return;
messages.value.push({ const userMessage: Message = {
text: text.value, text: text.value,
sender: 'user', sent: true
timestamp: new Date() };
});
setTimeout(() => { saveMessage(userMessage);
messages.value.push({ messages.value.push(userMessage);
text: 'Let me check that for you!',
sender: 'assistant',
timestamp: new Date()
});
}, 500);
provide('message', messages); const response = await getResponse(text.value);
provide('sendMessage', sendMessage); saveMessage(response);
provide('text', text); messages.value.push(response);
text.value = '';
} }
</script> </script>
<template> <template>
@ -36,11 +33,11 @@ const sendMessage = () => {
<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></DialogueContent> <DialogueContent :messages="messages"></DialogueContent>
</div> </div>
<div class="row chat-footer"> <div class="row chat-footer">
<q-input v-model="text" label="Enter a Message" class="col" /> <q-input v-model="text" label="Enter a Message" class="col" />
<q-btn :ripple="{ center: true }" color="primary" glossy label="Send" <q-btn @click="sendMessage" :ripple="{ center: true }" color="primary" glossy label="Send"
style="width:60px; height:45px; margin-top: 8px; font-size: 14px;" class="col-2"> style="width:60px; height:45px; margin-top: 8px; font-size: 14px;" class="col-2">
</q-btn> </q-btn>
</div> </div>

View File

@ -1,10 +1,14 @@
<script setup lang="ts"> <script setup lang="ts">
import DialoguePhrase from './dialogue-phrase.vue'; import DialoguePhrase from './dialogue-phrase.vue';
import type { Message } from '../types/dialogue-message';
defineProps<{
messages: Message[];
}>();
</script> </script>
<template> <template>
<q-infinite-scroll> <q-infinite-scroll>
<DialoguePhrase /> <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,8 +1,12 @@
<script setup lang="ts"> <script setup lang="ts">
defineProps<{
text: string;
sent: boolean;
}>();
</script> </script>
<template> <template>
<q-chat-message sent :text="['What\'s the temperature outside?']"> <q-chat-message :sent="sent" :text="[text]">
</q-chat-message> </q-chat-message>
</template> </template>

View File

@ -0,0 +1,15 @@
import type { Message } from "../types/dialogue-message";
// export const getMessages = async (): Promise<Message[]> => {
// return[
// {text: "Let me check that for you.", sent: true}
// ];
// };
export const getResponse = async (userInput: string): Promise<Message> => {
await new Promise(resolve => setTimeout(resolve, 500)); // simulate delay
return {
text: `You said' ${userInput} 'Let me check that for you.`,
sent: false
};
};

View File

@ -1,5 +1,4 @@
export interface Message{ export interface Message{
text:string; text:string;
sender: 'user' | 'assistant'; sent: boolean
timestamp: Date;
}; };

View File

@ -0,0 +1,11 @@
import type { Message } from "src/modules/chatbot/types/dialogue-message";
const localMessages: Message[] = [];
export const saveMessage = (msg: Message) =>{
localMessages.push(msg);
};
export const getStoredMessages = (): Message[] =>{
return [...localMessages];
}