Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Link auth + supabase local #6

Merged
merged 7 commits into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,21 @@ Features:
## Repo Structure
This is a [turborepo](https://turbo.build/repo/docs) managed monorepo. Read up on it if you have never heard of it before.

There are two main components: `apps/` and `packages/`.
There are three main components: `apps/`, `packages/`, and `infra/`.

`apps/` is where the applications live. Currently there is the chat UI and the backend server.

`packages/` is where any shareable code should go. With pnpm workspaces, you can import local modules by adding `"[package-name]": "workspace:*"` to a `package.json`.

`infra/` is for any infrastructure, like the local supabase setup.

## Development
First set up your `.env` files. You will need them in `apps/ui/.env`, `apps/server/.env`, and `infra/.env`. Follow the `.env.sample` files in each of them.

For Google OAuth, follow [these steps](https://supabase.com/docs/guides/auth/social-login/auth-google) to create a Google OAuth client. Then, for local development go to the Credentials page and edit your OAuth client. Add `http://localhost:54321/auth/v1/callback` to the Authorized redirect URIs.

Get the rest of the supabase environment variables from the output of your local supabase cli (it'll show up in `turbo dev` the first time you run it, otherwise just check `pnpm exec supabase status`).

Make sure you have [pnpm](https://pnpm.io/) installed. Then run:
```
pnpm i
Expand Down
74 changes: 66 additions & 8 deletions apps/server/src/trpc/routers/chat/create.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,33 @@
// Creates a new empty chat in the DB. Must call this before sending any messages, even from the home
// page.
// Creates a DB chat and streams down the response. Chats can only be created from the home page.

import { type DBChat, type DBChatMessage, DBChatSchema } from '@repo/db';
import { sql } from 'slonik';
import { ulid } from 'ulid';
import { z } from 'zod';
import { publicProcedure } from '../../trpc';
import { upsertDBChatMessage } from '../chatMessages/send';
import {
type SendMessageOutput,
updateDBChatMessage,
upsertDBChatMessage,
} from '../chatMessages/send';

export const CreateChatSchema = z.object({
initialMessage: z.string().optional(),
});

type CreateChatOutput =
| {
type: 'chat';
chat: DBChat;
}
| SendMessageOutput;

export const create = publicProcedure
.input(CreateChatSchema)
.mutation(async ({ input, ctx }) => {
.mutation(async function* ({
input,
ctx,
}): AsyncGenerator<CreateChatOutput> {
const chatID = ulid();

const newChat = (await ctx.dbPool.one(sql.type(DBChatSchema)`
Expand All @@ -40,14 +54,58 @@ export const create = publicProcedure
chatID,
messageContent: input.initialMessage,
messageType: 'user',
responseStatus: 'not_started',
responseStatus: 'streaming',
},
ctx.dbPool,
);
messages.push(initialMessage);
}
return {
...newChat,
messages,

yield {
type: 'chat',
chat: newChat,
};

if (!input.initialMessage) return;

const chatIterator = ctx.chatService.generateResponse({
userID: ctx.user.id,
chatID,
message: input.initialMessage,
previousMessages: [],
});

let fullMessage = '';
let messageID = '';
for await (const chunk of chatIterator) {
yield {
type: 'messageChunk',
messageChunk: chunk,
};
messageID = chunk.id;
fullMessage += chunk.messageContent;
}

const completedAssistantMessage = await upsertDBChatMessage(
{
id: messageID,
userID: ctx.user.id,
chatID: chatID,
messageType: 'assistant',
messageContent: fullMessage,
},
ctx.dbPool,
);
yield {
type: 'completeMessage',
message: completedAssistantMessage,
};

await updateDBChatMessage(
{
messageID: messageID,
responseStatus: 'done',
},
ctx.dbPool,
);
});
127 changes: 0 additions & 127 deletions apps/server/src/trpc/routers/chatMessages/generateResponse.ts

This file was deleted.

2 changes: 0 additions & 2 deletions apps/server/src/trpc/routers/chatMessages/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { router } from '../../trpc';
import { generateResponse } from './generateResponse';
import { infiniteList } from './infiniteList';
import { send } from './send';

export const chatMessagesRouter = router({
send,
infiniteList,
generateResponse,
});
8 changes: 1 addition & 7 deletions apps/server/src/trpc/routers/chatMessages/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ export const SendMessageSchema = z.object({
message: z.string(),
customSystemPrompt: z.string().optional(),
chatID: z.string(),
generateResponse: z.boolean().default(true),
});
type SendMessageOutput =
export type SendMessageOutput =
| {
type: 'userMessage';
message: DBChatMessage;
Expand Down Expand Up @@ -61,11 +60,6 @@ export const send = publicProcedure
ctx.dbPool,
);

if (!input.generateResponse) {
// Done
return;
}

let messageID = ulid();
await updateDBChatMessage(
{
Expand Down
38 changes: 19 additions & 19 deletions apps/ui/src/routeTree.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,44 @@

// This file is auto-generated by TanStack Router

import { createFileRoute } from '@tanstack/react-router'
import { createFileRoute } from "@tanstack/react-router"

// Import Routes

import { Route as rootRoute } from './routes/__root'
import { Route as CChatIDImport } from './routes/c/$chatID'
import { Route as rootRoute } from "./routes/~__root"
import { Route as CChatIDImport } from "./routes/~c/~$chatID"

// Create Virtual Routes

const IndexLazyImport = createFileRoute('/')()
const IndexLazyImport = createFileRoute("/")()

// Create/Update Routes

const IndexLazyRoute = IndexLazyImport.update({
path: '/',
path: "/",
getParentRoute: () => rootRoute,
} as any).lazy(() => import('./routes/index.lazy').then((d) => d.Route))
} as any).lazy(() => import("./routes/~index.lazy").then((d) => d.Route))

const CChatIDRoute = CChatIDImport.update({
path: '/c/$chatID',
path: "/c/$chatID",
getParentRoute: () => rootRoute,
} as any)

// Populate the FileRoutesByPath interface

declare module '@tanstack/react-router' {
declare module "@tanstack/react-router" {
interface FileRoutesByPath {
'/': {
id: '/'
path: '/'
fullPath: '/'
"/": {
id: "/"
path: "/"
fullPath: "/"
preLoaderRoute: typeof IndexLazyImport
parentRoute: typeof rootRoute
}
'/c/$chatID': {
id: '/c/$chatID'
path: '/c/$chatID'
fullPath: '/c/$chatID'
"/c/$chatID": {
id: "/c/$chatID"
path: "/c/$chatID"
fullPath: "/c/$chatID"
preLoaderRoute: typeof CChatIDImport
parentRoute: typeof rootRoute
}
Expand All @@ -62,17 +62,17 @@ export const routeTree = rootRoute.addChildren({ IndexLazyRoute, CChatIDRoute })
{
"routes": {
"__root__": {
"filePath": "__root.tsx",
"filePath": "~__root.tsx",
"children": [
"/",
"/c/$chatID"
]
},
"/": {
"filePath": "index.lazy.tsx"
"filePath": "~index.lazy.tsx"
},
"/c/$chatID": {
"filePath": "c/$chatID.tsx"
"filePath": "~c/~$chatID.tsx"
}
}
}
Expand Down
Loading
Loading