From f9292f05f488a40b79aefc77a6a8553e97d53f5c Mon Sep 17 00:00:00 2001 From: dxlliv Date: Thu, 2 Jan 2025 23:32:20 +0100 Subject: [PATCH] Implement agent --- app/app.config.ts | 3 + app/components/Agent/AgentDialog.vue | 23 ++++++ app/components/Agent/AgentForm.vue | 58 +++++++++++++++ app/components/Agent/AgentMessage.vue | 20 ++++++ app/components/Agent/AgentMessageWaiting.vue | 5 ++ app/components/Toolbar/Toolbar.vue | 6 +- app/models/ChatManager.ts | 74 ++++++++++++++++++++ nuxt.config.ts | 2 +- 8 files changed, 185 insertions(+), 6 deletions(-) create mode 100644 app/components/Agent/AgentDialog.vue create mode 100644 app/components/Agent/AgentForm.vue create mode 100644 app/components/Agent/AgentMessage.vue create mode 100644 app/components/Agent/AgentMessageWaiting.vue create mode 100644 app/models/ChatManager.ts diff --git a/app/app.config.ts b/app/app.config.ts index c5f70fa..f01b39a 100644 --- a/app/app.config.ts +++ b/app/app.config.ts @@ -1,4 +1,7 @@ export default defineAppConfig({ + agent: { + baseURL: 'https://agent.dxlliv.dev', + }, links: { instagram: "https://instagram.com/dxlliv", email: "mailto:hello@dxlliv.dev", diff --git a/app/components/Agent/AgentDialog.vue b/app/components/Agent/AgentDialog.vue new file mode 100644 index 0000000..e263595 --- /dev/null +++ b/app/components/Agent/AgentDialog.vue @@ -0,0 +1,23 @@ + + + \ No newline at end of file diff --git a/app/components/Agent/AgentForm.vue b/app/components/Agent/AgentForm.vue new file mode 100644 index 0000000..b0eb37a --- /dev/null +++ b/app/components/Agent/AgentForm.vue @@ -0,0 +1,58 @@ + + + diff --git a/app/components/Agent/AgentMessage.vue b/app/components/Agent/AgentMessage.vue new file mode 100644 index 0000000..310e05e --- /dev/null +++ b/app/components/Agent/AgentMessage.vue @@ -0,0 +1,20 @@ + + + + + \ No newline at end of file diff --git a/app/components/Agent/AgentMessageWaiting.vue b/app/components/Agent/AgentMessageWaiting.vue new file mode 100644 index 0000000..45968c1 --- /dev/null +++ b/app/components/Agent/AgentMessageWaiting.vue @@ -0,0 +1,5 @@ + \ No newline at end of file diff --git a/app/components/Toolbar/Toolbar.vue b/app/components/Toolbar/Toolbar.vue index 9fc1cf8..9614c4a 100644 --- a/app/components/Toolbar/Toolbar.vue +++ b/app/components/Toolbar/Toolbar.vue @@ -52,11 +52,7 @@ const sliderHorizontalStore = useSliderHorizontalStore() - - + diff --git a/app/models/ChatManager.ts b/app/models/ChatManager.ts new file mode 100644 index 0000000..1e78c65 --- /dev/null +++ b/app/models/ChatManager.ts @@ -0,0 +1,74 @@ +export class ChatManager { + chat: Ref = ref([]) + listElement: any + agentBaseURL: string + userId: string + roomId: string + + constructor(listElement: any, agentBaseURL: string) { + this.listElement = listElement + this.agentBaseURL = agentBaseURL + + this.userId = `user-${ChatManager.generateUUID()}` + this.roomId = `default-room-${ChatManager.generateUUID()}` + } + + static generateUUID(): string { + return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => { + const r = Math.random() * 16 | 0 + const v = c === 'x' ? r : (r & 0x3 | 0x8) + return v.toString(16) + }) + } + + scrollToBottom(): void { + this.listElement.value.$el.scrollTop = this.listElement.value.$el.scrollHeight + } + + addNewMessage(author: string, text: string): void { + this.chat.value.push({ + author, + text, + }) + + setTimeout(() => { + this.scrollToBottom() + }, 25) + } + + async sendMessage(text: string): Promise { + const data = { + text, + userId: this.userId, + roomId: this.roomId, + } + + const url = `${this.agentBaseURL}/message` + + await fetch(url, { + method: 'POST', + headers: { + 'Content-Type': 'application/json; charset=utf-8', + }, + body: JSON.stringify(data), + }) + .then((response) => { + if (!response.ok) { + throw new Error(`Request failed with status: ${response.status}`) + } + + return response.json() + }) + .then((messages) => { + if (messages.length > 0) { + this.addNewMessage('bot', messages[0].text) + } + }) + .catch(async () => { + return new Promise(resolve => setTimeout(() => { + this.addNewMessage('bot', 'There was an error') + resolve(true) + }, 3000)) + }) + } +} \ No newline at end of file diff --git a/nuxt.config.ts b/nuxt.config.ts index 5170fcc..36b349c 100644 --- a/nuxt.config.ts +++ b/nuxt.config.ts @@ -48,7 +48,7 @@ export default defineNuxtConfig({ ], imports: { - dirs: ['composables', 'stores', 'utils'] + dirs: ['composables', 'models', 'stores', 'utils'] }, i18n: {