From 61540f933877ea80208069806d7e05fa4bc212b4 Mon Sep 17 00:00:00 2001 From: Avraham Sakal Date: Thu, 18 Sep 2025 10:45:00 -0400 Subject: [PATCH] basic user-tailored UI --- database/common.ts | 2 +- database/postgres.ts | 12 ++++++----- pages/chat/@id/+data.ts | 1 + pages/chat/conversations.ts | 9 +++++---- pages/chat/trpc.ts | 6 +++--- server/authjs-handler.ts | 40 ++++++++++++++++++------------------- server/trpc-handler.ts | 5 ++++- trpc/server.ts | 2 ++ 8 files changed, 43 insertions(+), 34 deletions(-) diff --git a/database/common.ts b/database/common.ts index f4b28ce..ee736c3 100644 --- a/database/common.ts +++ b/database/common.ts @@ -34,7 +34,7 @@ export interface Entity { construct: (data: T) => T; create: (data: Omit) => Promise; createMany: (data: Omit[]) => Promise; - findAll: () => Promise; + findAll: (user?: { userId: string }) => Promise; findById: (id: string) => Promise; update: (id: string, data: Partial) => Promise; delete: (id: string) => Promise; diff --git a/database/postgres.ts b/database/postgres.ts index 85382a2..0697162 100644 --- a/database/postgres.ts +++ b/database/postgres.ts @@ -58,11 +58,13 @@ export function getDb(POSTGRES_CONNECTION_STRING: string) { .execute(); return insertedRows; }, - findAll: async () => { - const rows = await dbClient - .selectFrom("conversations") - .selectAll() - .execute(); + findAll: async (user) => { + const userId = user?.userId; + let query = await dbClient.selectFrom("conversations"); + if (userId) { + query = query.where("userId", "=", userId); + } + const rows = query.selectAll().execute(); return rows; }, findById: async (id) => { diff --git a/pages/chat/@id/+data.ts b/pages/chat/@id/+data.ts index e688897..00c67aa 100644 --- a/pages/chat/@id/+data.ts +++ b/pages/chat/@id/+data.ts @@ -16,6 +16,7 @@ export const data = async (pageContext: PageContextServer) => { openrouter: getOpenrouter( (pageContext.env?.OPENROUTER_API_KEY || env.OPENROUTER_API_KEY) as string ), + // jwt: pageContext., }); const [ diff --git a/pages/chat/conversations.ts b/pages/chat/conversations.ts index e448f21..3d9baa4 100644 --- a/pages/chat/conversations.ts +++ b/pages/chat/conversations.ts @@ -5,18 +5,19 @@ import { } from "../../trpc/server"; export const conversations = router({ - fetchAll: publicProcedure.query(async ({ ctx: { db } }) => { - return await db.conversations.findAll(); + fetchAll: publicProcedure.query(async ({ ctx: { db, jwt } }) => { + console.log("jwt", jwt); + return await db.conversations.findAll({ userId: jwt?.id as string }); }), fetchOne: publicProcedure .input((x) => x as { id: string }) .query(async ({ input: { id }, ctx: { db } }) => { return await db.conversations.findById(id); }), - start: publicProcedure.mutation(async ({ ctx: { db } }) => { + start: publicProcedure.mutation(async ({ ctx: { db, jwt } }) => { const row = { title: "New Conversation", - userId: "019900bb-61b3-7333-b760-b27784dfe33b", + userId: jwt?.id as string, }; return await db.conversations.create(row); }), diff --git a/pages/chat/trpc.ts b/pages/chat/trpc.ts index 55aa2ad..a9dc5f6 100644 --- a/pages/chat/trpc.ts +++ b/pages/chat/trpc.ts @@ -73,7 +73,7 @@ export const chat = router({ input: { conversationId, messages, systemPrompt, parameters }, ctx, }) { - const { db, openrouter } = ctx; + const { db, openrouter, jwt } = ctx; const factsCaller = createCallerFacts(ctx); const messagesCaller = createCallerMessages(ctx); const factTriggerCaller = createCallerFactTriggers(ctx); @@ -173,7 +173,7 @@ export const chat = router({ }); const insertedFactsFromUserMessage = await db.facts.createMany( factsFromUserMessageResponse.object.facts.map((fact) => ({ - userId: "019900bb-61b3-7333-b760-b27784dfe33b", + userId: jwt?.id as string, sourceMessageId: insertedUserMessage.id, content: fact, })) @@ -228,7 +228,7 @@ export const chat = router({ const insertedFactsFromAssistantMessage = await db.facts.createMany( factsFromAssistantMessageResponse.object.facts.map((factContent) => ({ - userId: "019900bb-61b3-7333-b760-b27784dfe33b", + userId: jwt?.id as string, sourceMessageId: insertedAssistantMessage.id, content: factContent, createdAt: new Date().toISOString(), diff --git a/server/authjs-handler.ts b/server/authjs-handler.ts index 9399430..9488162 100644 --- a/server/authjs-handler.ts +++ b/server/authjs-handler.ts @@ -4,7 +4,7 @@ import { createActionURL, setEnvDefaults, } from "@auth/core"; -import CredentialsProvider from "@auth/core/providers/credentials"; +// import CredentialsProvider from "@auth/core/providers/credentials"; import GoogleProvider from "@auth/core/providers/google"; import type { Session } from "@auth/core/types"; // TODO: stop using universal-middleware and directly integrate server middlewares instead and/or use vike-server https://vike.dev/server. (Bati generates boilerplates that use universal-middleware https://github.com/magne4000/universal-middleware to make Bati's internal logic easier. This is temporary and will be removed soon.) @@ -41,26 +41,26 @@ const authjsConfig = { secret: "buginoo", providers: [ // TODO: Choose and implement providers - CredentialsProvider({ - name: "Credentials", - credentials: { - username: { label: "Username", type: "text", placeholder: "jsmith" }, - password: { label: "Password", type: "password" }, - }, - async authorize() { - // Add logic here to look up the user from the credentials supplied - const user = { - id: "019900bb-61b3-7333-b760-b27784dfe33b", - name: "J Smith", - email: "jsmith@example.com", - }; + // CredentialsProvider({ + // name: "Credentials", + // credentials: { + // username: { label: "Username", type: "text", placeholder: "jsmith" }, + // password: { label: "Password", type: "password" }, + // }, + // async authorize() { + // // Add logic here to look up the user from the credentials supplied + // const user = { + // id: "019900bb-61b3-7333-b760-b27784dfe33b", + // name: "J Smith", + // email: "jsmith@example.com", + // }; - // Any object returned will be saved in `user` property of the JWT - // If you return null then an error will be displayed advising the user to check their details. - // You can also Reject this callback with an Error thus the user will be sent to the error page with the error message as a query parameter - return user ?? null; - }, - }), + // // Any object returned will be saved in `user` property of the JWT + // // If you return null then an error will be displayed advising the user to check their details. + // // You can also Reject this callback with an Error thus the user will be sent to the error page with the error message as a query parameter + // return user ?? null; + // }, + // }), GoogleProvider({ clientId: "697711350664-t6237s5n3ttjd1npp1qif1aupptkr0va.apps.googleusercontent.com", diff --git a/server/trpc-handler.ts b/server/trpc-handler.ts index 1e43959..6619526 100644 --- a/server/trpc-handler.ts +++ b/server/trpc-handler.ts @@ -9,13 +9,14 @@ import { fetchRequestHandler } from "@trpc/server/adapters/fetch"; import { getDb, getDbClient } from "../database/postgres"; import { getOpenrouter } from "../pages/chat/provider"; import { env as processEnv } from "./env.js"; +import { getToken } from "@auth/core/jwt"; export const trpcHandler = ((endpoint) => (request, context, runtime) => { return fetchRequestHandler({ endpoint, req: request, router: appRouter, - createContext({ req, resHeaders }) { + async createContext({ req, resHeaders }) { const env = getEnv(runtime); const dbClient = getDbClient( (env.POSTGRES_CONNECTION_STRING || @@ -28,6 +29,7 @@ export const trpcHandler = ((endpoint) => (request, context, runtime) => { const openrouter = getOpenrouter( (env.OPENROUTER_API_KEY || processEnv.OPENROUTER_API_KEY) as string ); + const jwt = await getToken({ req: request, secret: "buginoo" }); return { ...context, ...runtime, @@ -36,6 +38,7 @@ export const trpcHandler = ((endpoint) => (request, context, runtime) => { dbClient, db, openrouter, + jwt, }; }, allowMethodOverride: true, diff --git a/trpc/server.ts b/trpc/server.ts index 182c5d3..5032091 100644 --- a/trpc/server.ts +++ b/trpc/server.ts @@ -3,6 +3,7 @@ import { TypeCompiler } from "@sinclair/typebox/compiler"; import { initTRPC, TRPCError } from "@trpc/server"; import type { getDb } from "../database/postgres"; import type { getOpenrouter } from "../pages/chat/provider"; +import type { JWT } from "@auth/core/jwt"; /** * Initialization of tRPC backend @@ -13,6 +14,7 @@ const t = initTRPC object & { db: ReturnType; openrouter: ReturnType; + jwt?: JWT | null; } >() .create(/*{