Scaffold web app
This commit is contained in:
34
src/app/(app)/ai.tsx
Normal file
34
src/app/(app)/ai.tsx
Normal file
@@ -0,0 +1,34 @@
|
||||
'use server'
|
||||
|
||||
import { executeTodoAgent } from '~/agents/todo'
|
||||
import env from '~/env'
|
||||
import { publish } from '~/events/server'
|
||||
|
||||
export async function sendMessage({ userId, message }: { userId: string; message: string }) {
|
||||
const { response } = await executeTodoAgent({
|
||||
messages: [{ content: message, role: 'user' }],
|
||||
onEvent: async events => {
|
||||
switch (events.type) {
|
||||
case 'assistant_message': {
|
||||
await publish({
|
||||
channel: userId,
|
||||
eventType: events.type,
|
||||
payload: events
|
||||
})
|
||||
break
|
||||
}
|
||||
case 'function_call': {
|
||||
await publish({
|
||||
channel: userId,
|
||||
eventType: events.name,
|
||||
payload: events
|
||||
})
|
||||
break
|
||||
}
|
||||
}
|
||||
},
|
||||
openAIKey: env.OPENAI_API_KEY
|
||||
})
|
||||
|
||||
console.log(response)
|
||||
}
|
||||
90
src/app/(app)/chat.tsx
Normal file
90
src/app/(app)/chat.tsx
Normal file
@@ -0,0 +1,90 @@
|
||||
'use client'
|
||||
|
||||
import { useState } from 'react'
|
||||
import type { TodoAgentResponseEvent, TodoAgentToolEvent } from '~/agents/todo'
|
||||
import { useSession } from '~/auth/client'
|
||||
import { ChatBox } from '~/components/ChatBox'
|
||||
import { AssistantMessage, ToolMessage, UserMessage } from '~/components/Message'
|
||||
|
||||
import { useEvents } from '~/events/client'
|
||||
import { sendMessage } from './ai'
|
||||
|
||||
type Message =
|
||||
| TodoAgentToolEvent
|
||||
| TodoAgentResponseEvent
|
||||
| {
|
||||
id: string
|
||||
type: 'user_message'
|
||||
message: string
|
||||
}
|
||||
|
||||
function MessageSwitch({ message }: { message: Message }) {
|
||||
switch (message.type) {
|
||||
case 'user_message': {
|
||||
return <UserMessage>{message.message}</UserMessage>
|
||||
}
|
||||
|
||||
case 'assistant_message': {
|
||||
return <AssistantMessage>{message.message.response}</AssistantMessage>
|
||||
}
|
||||
case 'function_call': {
|
||||
return (
|
||||
<ToolMessage
|
||||
name={message.name}
|
||||
args={JSON.stringify(message.arguments)}
|
||||
result={JSON.stringify(message.result)}
|
||||
/>
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function ChatMessages({
|
||||
userId,
|
||||
messages,
|
||||
addMessage
|
||||
}: {
|
||||
userId: string
|
||||
messages: Message[]
|
||||
addMessage: (message: Message) => void
|
||||
}) {
|
||||
useEvents({
|
||||
id: userId,
|
||||
on: {
|
||||
assistant_message: addMessage
|
||||
}
|
||||
})
|
||||
|
||||
return (
|
||||
<div className="pb-16">
|
||||
{messages.map(message => (
|
||||
<MessageSwitch key={message.id} message={message} />
|
||||
))}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default function Chat() {
|
||||
const { userId } = useSession()
|
||||
const [messages, setMessages] = useState<Message[]>([])
|
||||
|
||||
function addMessage(message: Message) {
|
||||
setMessages(prev => [...prev, message])
|
||||
}
|
||||
|
||||
function handleSubmit(message: string) {
|
||||
addMessage({
|
||||
id: Date.now().toString(),
|
||||
message,
|
||||
type: 'user_message'
|
||||
})
|
||||
sendMessage({ message, userId })
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="w-full">
|
||||
<ChatMessages userId={userId} messages={messages} addMessage={addMessage} />
|
||||
<ChatBox placeholder="What is my todo list?" submit={handleSubmit} />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
17
src/app/(app)/layout.tsx
Normal file
17
src/app/(app)/layout.tsx
Normal file
@@ -0,0 +1,17 @@
|
||||
import { getSession } from '~/auth/actions'
|
||||
import { ClientAuthProvider } from '~/auth/client'
|
||||
import { Nav } from '~/components/Nav'
|
||||
|
||||
export default async function AppLayout({ children }: { children: React.ReactNode }) {
|
||||
return (
|
||||
// @ts-expect-error: Auth Package Bug
|
||||
<ClientAuthProvider session={await getSession({ redirectUnauthorized: '/signin' })}>
|
||||
<div className="flex min-h-screen flex-col">
|
||||
<Nav />
|
||||
<div className="mx-auto flex w-full max-w-4xl flex-1 flex-col items-center p-10">
|
||||
<div className="flex w-full flex-col items-center">{children}</div>
|
||||
</div>
|
||||
</div>
|
||||
</ClientAuthProvider>
|
||||
)
|
||||
}
|
||||
5
src/app/(app)/page.tsx
Normal file
5
src/app/(app)/page.tsx
Normal file
@@ -0,0 +1,5 @@
|
||||
import Chat from './chat'
|
||||
|
||||
export default function Page() {
|
||||
return <Chat />
|
||||
}
|
||||
5
src/app/(landing)/signin/page.tsx
Normal file
5
src/app/(landing)/signin/page.tsx
Normal file
@@ -0,0 +1,5 @@
|
||||
import { SignInWithGithubButton } from '~/components/SignIn'
|
||||
|
||||
export default function SignInPage() {
|
||||
return <SignInWithGithubButton />
|
||||
}
|
||||
3
src/app/api/auth/[...auth]/route.ts
Normal file
3
src/app/api/auth/[...auth]/route.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import { routes } from '~/auth/server'
|
||||
|
||||
export const { GET } = routes
|
||||
1
src/app/api/events/route.ts
Normal file
1
src/app/api/events/route.ts
Normal file
@@ -0,0 +1 @@
|
||||
export { GET, maxDuration } from '~/events/server'
|
||||
30
src/app/icon.tsx
Normal file
30
src/app/icon.tsx
Normal file
@@ -0,0 +1,30 @@
|
||||
import { ImageResponse } from 'next/og'
|
||||
|
||||
export const contentType = 'image/png'
|
||||
export const size = {
|
||||
height: 32,
|
||||
width: 32
|
||||
}
|
||||
|
||||
export default async function Icon() {
|
||||
return new ImageResponse(
|
||||
<div
|
||||
style={{
|
||||
alignItems: 'center',
|
||||
background: 'black',
|
||||
color: 'white',
|
||||
display: 'flex',
|
||||
fontSize: 28,
|
||||
fontWeight: 700,
|
||||
height: '100%',
|
||||
justifyContent: 'center',
|
||||
width: '100%'
|
||||
}}
|
||||
>
|
||||
R
|
||||
</div>,
|
||||
{
|
||||
...size
|
||||
}
|
||||
)
|
||||
}
|
||||
14
src/app/layout.tsx
Normal file
14
src/app/layout.tsx
Normal file
@@ -0,0 +1,14 @@
|
||||
import './styles.css'
|
||||
|
||||
export const metadata = {
|
||||
description: 'Generated by Create Rubric App',
|
||||
title: 'Create Rubric App'
|
||||
}
|
||||
|
||||
export default function RootLayout({ children }: { children: React.ReactNode }) {
|
||||
return (
|
||||
<html lang="en">
|
||||
<body>{children}</body>
|
||||
</html>
|
||||
)
|
||||
}
|
||||
41
src/app/opengraph-image.tsx
Normal file
41
src/app/opengraph-image.tsx
Normal file
@@ -0,0 +1,41 @@
|
||||
import { readFile } from 'node:fs/promises'
|
||||
import { ImageResponse } from 'next/og'
|
||||
|
||||
export const alt = 'Create Rubric App'
|
||||
export const size = {
|
||||
height: 630,
|
||||
width: 1200
|
||||
}
|
||||
|
||||
export const contentType = 'image/png'
|
||||
|
||||
export default async function Image() {
|
||||
const interSemiBold = await readFile('public/fonts/PlusJakartaSans-Bold.ttf')
|
||||
|
||||
return new ImageResponse(
|
||||
<div
|
||||
style={{
|
||||
alignItems: 'center',
|
||||
background: 'white',
|
||||
display: 'flex',
|
||||
fontSize: 128,
|
||||
height: '100%',
|
||||
justifyContent: 'center',
|
||||
width: '100%'
|
||||
}}
|
||||
>
|
||||
R
|
||||
</div>,
|
||||
{
|
||||
...size,
|
||||
fonts: [
|
||||
{
|
||||
data: interSemiBold,
|
||||
name: 'Inter',
|
||||
style: 'normal',
|
||||
weight: 400
|
||||
}
|
||||
]
|
||||
}
|
||||
)
|
||||
}
|
||||
3
src/app/styles.css
Normal file
3
src/app/styles.css
Normal file
@@ -0,0 +1,3 @@
|
||||
/** biome-ignore-all lint/suspicious/noUnknownAtRules: Tailwind Grammar */
|
||||
|
||||
@import "tailwindcss";
|
||||
1
src/app/twitter-image.tsx
Normal file
1
src/app/twitter-image.tsx
Normal file
@@ -0,0 +1 @@
|
||||
export { alt, contentType, default, size } from './opengraph-image'
|
||||
Reference in New Issue
Block a user