From 0228dd48bf6d2e4de4ed61fe68c508c6f99528cd Mon Sep 17 00:00:00 2001 From: codewithcheese Date: Thu, 4 Jul 2024 22:31:37 +1000 Subject: [PATCH 01/98] Add chat page and message input --- src/routes/(app)/+layout.ts | 1 + src/routes/(app)/+page.ts | 2 +- src/routes/(app)/chat/[id]/$data.ts | 20 ++++- src/routes/(app)/chat/[id]/+layout.svelte | 13 +-- src/routes/(app)/chat/[id]/+layout.ts | 2 + src/routes/(app)/chat/[id]/+page.svelte | 49 +++++++++++ src/routes/(app)/chat/[id]/+page.ts | 17 +++- .../(app)/chat/[id]/MessageInput.svelte | 82 +++++++++++++++++++ .../(app)/chat/[id]/revise/+page.svelte | 34 ++++---- 9 files changed, 194 insertions(+), 26 deletions(-) create mode 100644 src/routes/(app)/chat/[id]/+page.svelte create mode 100644 src/routes/(app)/chat/[id]/MessageInput.svelte diff --git a/src/routes/(app)/+layout.ts b/src/routes/(app)/+layout.ts index 7e77f43..0dfd710 100644 --- a/src/routes/(app)/+layout.ts +++ b/src/routes/(app)/+layout.ts @@ -22,6 +22,7 @@ export async function load({ depends }) { console.error(err); } const chats = await useDb().query.chatTable.findMany({}); + console.log("chats", chats); registerModel(chatTable, chats, depends); depends("view:chats"); return { diff --git a/src/routes/(app)/+page.ts b/src/routes/(app)/+page.ts index 390103b..d2bbbe6 100644 --- a/src/routes/(app)/+page.ts +++ b/src/routes/(app)/+page.ts @@ -3,5 +3,5 @@ import { redirect } from "@sveltejs/kit"; export async function load({ parent }) { const data = await parent(); const chat = data.chats[data.chats.length - 1]; - redirect(301, `/chat/${chat.id}/revise`); + redirect(301, `/chat/${chat.id}`); } diff --git a/src/routes/(app)/chat/[id]/$data.ts b/src/routes/(app)/chat/[id]/$data.ts index e879554..c47a3f9 100644 --- a/src/routes/(app)/chat/[id]/$data.ts +++ b/src/routes/(app)/chat/[id]/$data.ts @@ -7,7 +7,7 @@ import { responseTable, useDb, } from "@/database"; -import { eq } from "drizzle-orm"; +import { desc, eq } from "drizzle-orm"; import { nanoid } from "nanoid"; import { invalidate } from "$app/navigation"; import { toast } from "svelte-french-toast"; @@ -23,6 +23,24 @@ export function loadServices() { }); } +export async function getLatestResponse(chatId: string) { + // get latest revision + const response = await useDb().query.responseTable.findMany({ + where: eq(responseTable.chatId, chatId), + with: { + messages: true, + model: { + with: { + service: true, + }, + }, + }, + orderBy: [desc(responseTable.createdAt)], + limit: 1, + }); + return { response }; +} + export async function updateChat(chat: Chat) { console.log("updateChat", chat); const result = await useDb() diff --git a/src/routes/(app)/chat/[id]/+layout.svelte b/src/routes/(app)/chat/[id]/+layout.svelte index c7a5b6b..4e62c77 100644 --- a/src/routes/(app)/chat/[id]/+layout.svelte +++ b/src/routes/(app)/chat/[id]/+layout.svelte @@ -12,7 +12,11 @@ let chat = $derived(data.chat); function tabClick(value?: string) { - goto(`/chat/${chat.id}/${value}`); + if (value === "chat") { + goto(`/chat/${chat.id}`); + } else { + goto(`/chat/${chat.id}/${value}`); + } } $inspect("chat layout", data); @@ -23,8 +27,8 @@ > + Chat Revise - @@ -60,9 +64,8 @@ {/if} -
- -
+ + diff --git a/src/routes/(app)/chat/[id]/+layout.ts b/src/routes/(app)/chat/[id]/+layout.ts index 750f0fb..b5e6948 100644 --- a/src/routes/(app)/chat/[id]/+layout.ts +++ b/src/routes/(app)/chat/[id]/+layout.ts @@ -12,6 +12,8 @@ export async function load({ route, url, params, depends }) { // tab = "chat"; } else if (route.id.includes(`[id]/revise`)) { tab = "revise"; + } else { + tab = "chat"; } const chat = await useDb().query.chatTable.findFirst({ diff --git a/src/routes/(app)/chat/[id]/+page.svelte b/src/routes/(app)/chat/[id]/+page.svelte new file mode 100644 index 0000000..c8635cd --- /dev/null +++ b/src/routes/(app)/chat/[id]/+page.svelte @@ -0,0 +1,49 @@ + + + + + +
+
Hello
+ +
diff --git a/src/routes/(app)/chat/[id]/+page.ts b/src/routes/(app)/chat/[id]/+page.ts index be5565d..a3c2214 100644 --- a/src/routes/(app)/chat/[id]/+page.ts +++ b/src/routes/(app)/chat/[id]/+page.ts @@ -1,5 +1,16 @@ -import { redirect } from "@sveltejs/kit"; +import { getLatestResponse } from "./$data"; +import { registerModel, responseTable } from "@/database"; +import { error } from "@sveltejs/kit"; -export function load({ params }) { - return redirect(301, `/chat/${params.id}/revise`); +export async function load({ params, depends }) { + try { + const { response } = await getLatestResponse(params.id); + registerModel(responseTable, response, depends); + depends("view:messages"); + console.log("response", response); + return { response, hello: "world" }; + } catch (e) { + console.error("error", e); + return error(500, "Error loading chat"); + } } diff --git a/src/routes/(app)/chat/[id]/MessageInput.svelte b/src/routes/(app)/chat/[id]/MessageInput.svelte new file mode 100644 index 0000000..54f2013 --- /dev/null +++ b/src/routes/(app)/chat/[id]/MessageInput.svelte @@ -0,0 +1,82 @@ + + +
+ + + {#if isUploadOpen} +
+ + + +
+ {/if} +
+ + diff --git a/src/routes/(app)/chat/[id]/+page.svelte b/src/routes/(app)/chat/[id]/+page.svelte index c8635cd..16ba503 100644 --- a/src/routes/(app)/chat/[id]/+page.svelte +++ b/src/routes/(app)/chat/[id]/+page.svelte @@ -1,10 +1,15 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Hello
diff --git a/src/routes/(app)/chat/[id]/MessageInput.svelte b/src/routes/(app)/chat/[id]/MessageInput.svelte index 54f2013..d99a941 100644 --- a/src/routes/(app)/chat/[id]/MessageInput.svelte +++ b/src/routes/(app)/chat/[id]/MessageInput.svelte @@ -17,13 +17,12 @@ function handleInput(event: Event) { const textarea = event.target as HTMLTextAreaElement; message = textarea.value; - // adjustTextareaHeight(); + adjustTextareaHeight(); } function adjustTextareaHeight() { - // if (textareaElement) { - // textareaElement.style.height = `${textareaElement.scrollHeight}px`; - // } + textareaElement.style.height = "auto"; + textareaElement.style.height = `${textareaElement.scrollHeight}px`; } function handleUpload(type: string) { @@ -44,8 +43,8 @@ }); -
- +
+ {#if isUploadOpen}
@@ -68,10 +67,11 @@