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'
|
||||
33
src/lib/agents/todo.ts
Normal file
33
src/lib/agents/todo.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { createAgent, createResponseFormat, noTabs } from '@rubriclab/agents'
|
||||
import { z } from 'zod/v4'
|
||||
import createTodo from '~/tools/createTodo'
|
||||
import getTodoList from '~/tools/getTodoList'
|
||||
|
||||
const responseFormat = createResponseFormat({
|
||||
name: 'todo_agent_response_format',
|
||||
schema: z.object({
|
||||
response: z.string()
|
||||
})
|
||||
})
|
||||
|
||||
const systemPrompt = noTabs`
|
||||
You are a todo agent.
|
||||
The user will ask you to do CRUD operations against a TODO database.
|
||||
You should use tools to help them.
|
||||
`
|
||||
|
||||
const { executeAgent, eventTypes, __ToolEvent, __ResponseEvent } = createAgent({
|
||||
model: 'gpt-4.1-mini',
|
||||
responseFormat,
|
||||
systemPrompt,
|
||||
tools: {
|
||||
createTodo,
|
||||
getTodoList
|
||||
}
|
||||
})
|
||||
|
||||
export { eventTypes as todoAgentEventTypes }
|
||||
export { executeAgent as executeTodoAgent }
|
||||
|
||||
export type TodoAgentToolEvent = typeof __ToolEvent
|
||||
export type TodoAgentResponseEvent = typeof __ResponseEvent
|
||||
5
src/lib/auth/actions.ts
Normal file
5
src/lib/auth/actions.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
'use server'
|
||||
|
||||
import { actions } from './server'
|
||||
|
||||
export const { signIn, signOut, sendMagicLink, getAuthConstants, getSession } = actions
|
||||
19
src/lib/auth/client.ts
Normal file
19
src/lib/auth/client.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
'use client'
|
||||
|
||||
import type { Prisma } from '@prisma/client'
|
||||
import { CreateAuthContext } from '@rubriclab/auth/client'
|
||||
|
||||
export const { ClientAuthProvider, useSession } =
|
||||
CreateAuthContext<
|
||||
Prisma.SessionGetPayload<{
|
||||
include: {
|
||||
user: {
|
||||
include: {
|
||||
apiKeyAuthorizationAccounts: true
|
||||
oAuth2AuthenticationAccounts: true
|
||||
oAuth2AuthorizationAccounts: true
|
||||
}
|
||||
}
|
||||
}
|
||||
}>
|
||||
>()
|
||||
14
src/lib/auth/server.ts
Normal file
14
src/lib/auth/server.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { createAuth, createGithubAuthenticationProvider, prismaAdapter } from '@rubriclab/auth'
|
||||
import db from '~/db'
|
||||
import env from '~/env'
|
||||
|
||||
export const { routes, actions, __types } = createAuth({
|
||||
authUrl: env.NEXT_PUBLIC_AUTH_URL,
|
||||
databaseProvider: prismaAdapter(db),
|
||||
oAuth2AuthenticationProviders: {
|
||||
github: createGithubAuthenticationProvider({
|
||||
githubClientId: env.GITHUB_CLIENT_ID,
|
||||
githubClientSecret: env.GITHUB_CLIENT_SECRET
|
||||
})
|
||||
}
|
||||
})
|
||||
51
src/lib/components/ChatBox.tsx
Normal file
51
src/lib/components/ChatBox.tsx
Normal file
@@ -0,0 +1,51 @@
|
||||
'use client'
|
||||
|
||||
import { type KeyboardEvent, useState } from 'react'
|
||||
|
||||
export function ChatBox({
|
||||
submit,
|
||||
placeholder = 'Type a message...'
|
||||
}: {
|
||||
submit: (message: string) => void
|
||||
placeholder?: string
|
||||
}) {
|
||||
const [message, setMessage] = useState(placeholder)
|
||||
|
||||
return (
|
||||
<div className="fixed right-0 bottom-0 left-0 bg-white p-4 dark:bg-black">
|
||||
<div className="flex w-full items-center justify-center gap-2">
|
||||
<textarea
|
||||
value={message}
|
||||
onChange={e => {
|
||||
setMessage(e.target.value)
|
||||
e.target.style.height = 'auto'
|
||||
e.target.style.height = `${e.target.scrollHeight}px`
|
||||
}}
|
||||
onKeyDown={(e: KeyboardEvent<HTMLTextAreaElement>) => {
|
||||
if (e.key === 'Enter' && !e.shiftKey) {
|
||||
e.preventDefault()
|
||||
if (message.trim()) {
|
||||
submit(message)
|
||||
setMessage('')
|
||||
}
|
||||
}
|
||||
}}
|
||||
rows={1}
|
||||
className="input-field max-w-[800px] flex-1 resize-none rounded-lg border px-3 py-2 focus:outline-none focus:ring-2 focus:ring-black dark:focus:ring-white"
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => {
|
||||
if (message.trim()) {
|
||||
submit(message)
|
||||
setMessage(placeholder)
|
||||
}
|
||||
}}
|
||||
className="input-field self-end rounded-lg border bg-black px-4 py-2 text-white dark:bg-white dark:text-black"
|
||||
>
|
||||
→
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
52
src/lib/components/Message.tsx
Normal file
52
src/lib/components/Message.tsx
Normal file
@@ -0,0 +1,52 @@
|
||||
export function UserMessage({ children }: { children: React.ReactNode }) {
|
||||
return (
|
||||
<div className="mb-3 flex justify-end">
|
||||
<div className="message-user max-w-3xl rounded-lg px-3 py-2">{children}</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export function AssistantMessage({ children }: { children: React.ReactNode }) {
|
||||
return (
|
||||
<div className="mb-3 flex justify-start">
|
||||
<div className="message-assistant max-w-3xl rounded-lg px-3 py-2">{children}</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export function ToolMessage({
|
||||
name,
|
||||
args,
|
||||
result
|
||||
}: {
|
||||
name: string
|
||||
args: React.ReactNode
|
||||
result?: React.ReactNode
|
||||
}) {
|
||||
return (
|
||||
<div className="mb-3 flex justify-start">
|
||||
<div className="message-assistant max-w-3xl rounded-lg px-3 py-2">
|
||||
<div className="mb-2 font-medium text-neutral-700 text-sm dark:text-neutral-300">
|
||||
Tool: {name}
|
||||
</div>
|
||||
|
||||
<div className="space-y-2 text-sm">
|
||||
<div>
|
||||
<div className="mb-1 font-medium text-neutral-500 text-xs uppercase tracking-wide dark:text-neutral-400">
|
||||
Input
|
||||
</div>
|
||||
<div className="surface rounded p-2">{args}</div>
|
||||
</div>
|
||||
{result && (
|
||||
<div>
|
||||
<div className="mb-1 font-medium text-neutral-500 text-xs uppercase tracking-wide dark:text-neutral-400">
|
||||
Output
|
||||
</div>
|
||||
<div className="surface rounded p-2">{result}</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
17
src/lib/components/Nav.tsx
Normal file
17
src/lib/components/Nav.tsx
Normal file
@@ -0,0 +1,17 @@
|
||||
'use client'
|
||||
|
||||
import { useSession } from '~/auth/client'
|
||||
import { SignOutButton } from '~/components/SignOut'
|
||||
|
||||
export function Nav() {
|
||||
const { user } = useSession()
|
||||
return (
|
||||
<div className="flex flex-row items-center justify-between gap-4 p-4">
|
||||
<a href="/">Home</a>
|
||||
<div className="flex flex-row items-center gap-4">
|
||||
<p>Signed in as {user.email}</p>
|
||||
<SignOutButton />
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
11
src/lib/components/SignIn.tsx
Normal file
11
src/lib/components/SignIn.tsx
Normal file
@@ -0,0 +1,11 @@
|
||||
'use client'
|
||||
|
||||
import { signIn } from '~/auth/actions'
|
||||
|
||||
export function SignInWithGithubButton() {
|
||||
return (
|
||||
<button type="button" onClick={async () => signIn({ callbackUrl: '/', provider: 'github' })}>
|
||||
Sign In With Github
|
||||
</button>
|
||||
)
|
||||
}
|
||||
15
src/lib/components/SignOut.tsx
Normal file
15
src/lib/components/SignOut.tsx
Normal file
@@ -0,0 +1,15 @@
|
||||
'use client'
|
||||
|
||||
import { signOut } from '~/auth/actions'
|
||||
|
||||
export function SignOutButton() {
|
||||
return (
|
||||
<button
|
||||
type="button"
|
||||
className="underline underline-offset-4"
|
||||
onClick={async () => signOut({ redirect: '/signin' })}
|
||||
>
|
||||
Sign Out
|
||||
</button>
|
||||
)
|
||||
}
|
||||
3
src/lib/db.ts
Normal file
3
src/lib/db.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import { PrismaClient } from '@prisma/client'
|
||||
|
||||
export default new PrismaClient()
|
||||
19
src/lib/env.ts
Normal file
19
src/lib/env.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { createEnv } from '@t3-oss/env-nextjs'
|
||||
import z from 'zod'
|
||||
|
||||
export default createEnv({
|
||||
client: {
|
||||
NEXT_PUBLIC_AUTH_URL: z.string().min(1)
|
||||
},
|
||||
experimental__runtimeEnv: {
|
||||
NEXT_PUBLIC_AUTH_URL: process.env.NEXT_PUBLIC_AUTH_URL
|
||||
},
|
||||
server: {
|
||||
DATABASE_URL: z.string().min(1),
|
||||
GITHUB_CLIENT_ID: z.string().min(1),
|
||||
GITHUB_CLIENT_SECRET: z.string().min(1),
|
||||
NODE_ENV: z.string(),
|
||||
OPENAI_API_KEY: z.string().min(1),
|
||||
UPSTASH_REDIS_URL: z.string().min(1)
|
||||
}
|
||||
})
|
||||
7
src/lib/events/client.ts
Normal file
7
src/lib/events/client.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { createEventsClient } from '@rubriclab/events/client'
|
||||
import { eventTypes } from '~/events/types'
|
||||
|
||||
export const { useEvents } = createEventsClient({
|
||||
eventTypes,
|
||||
url: '/api/events'
|
||||
})
|
||||
8
src/lib/events/server.ts
Normal file
8
src/lib/events/server.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { createEventsServer } from '@rubriclab/events/server'
|
||||
import env from '~/env'
|
||||
import { eventTypes } from '~/events/types'
|
||||
|
||||
export const { publish, GET, maxDuration } = createEventsServer({
|
||||
eventTypes,
|
||||
redisURL: env.UPSTASH_REDIS_URL
|
||||
})
|
||||
6
src/lib/events/types.ts
Normal file
6
src/lib/events/types.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { createEventTypes } from '@rubriclab/events'
|
||||
import { todoAgentEventTypes } from '~/agents/todo'
|
||||
|
||||
export const eventTypes = createEventTypes({
|
||||
...todoAgentEventTypes
|
||||
})
|
||||
22
src/lib/tools/createTodo.ts
Normal file
22
src/lib/tools/createTodo.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { createTool } from '@rubriclab/agents'
|
||||
import z from 'zod/v4'
|
||||
import db from '~/db'
|
||||
|
||||
export default createTool({
|
||||
async execute({ status, title }) {
|
||||
await db.task.create({
|
||||
data: {
|
||||
status,
|
||||
title
|
||||
}
|
||||
})
|
||||
return undefined
|
||||
},
|
||||
schema: {
|
||||
input: z.object({
|
||||
status: z.boolean(),
|
||||
title: z.string()
|
||||
}),
|
||||
output: z.undefined()
|
||||
}
|
||||
})
|
||||
31
src/lib/tools/getTodoList.ts
Normal file
31
src/lib/tools/getTodoList.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { createTool } from '@rubriclab/agents'
|
||||
import z from 'zod/v4'
|
||||
import db from '~/db'
|
||||
|
||||
export default createTool({
|
||||
async execute() {
|
||||
return await db.task.findMany({
|
||||
include: {
|
||||
user: {
|
||||
select: {
|
||||
email: true
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
},
|
||||
schema: {
|
||||
input: z.object({}),
|
||||
output: z.array(
|
||||
z.object({
|
||||
status: z.boolean(),
|
||||
title: z.string(),
|
||||
user: z
|
||||
.object({
|
||||
email: z.string()
|
||||
})
|
||||
.nullable()
|
||||
})
|
||||
)
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user