Scaffold web app
This commit is contained in:
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