Skip to content

Commit

Permalink
Added OpenAI Integration
Browse files Browse the repository at this point in the history
  • Loading branch information
anoopkarnik committed Jun 26, 2024
1 parent 56f4f67 commit 36648f8
Show file tree
Hide file tree
Showing 23 changed files with 2,481 additions and 5,279 deletions.
23 changes: 23 additions & 0 deletions apps/dashboard-app/actions/connections/openai-connections.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use server'

import { createOpenAI,getOpenAIByAPIKey,getOpenAIByUserId } from '@repo/prisma-db/repo/openAi'

interface Props {
apiKey: string,
userId: string
}

export const onOpenAIConnection = async ({apiKey,userId}:Props) => {
if(apiKey){
const openai_connected = await getOpenAIByAPIKey(apiKey)
if (!openai_connected){
await createOpenAI({apiKey,openai_connected, userId})
}
}

}

export const getOpenAIConnection = async (userId: string) => {
const connection = await getOpenAIByUserId(userId)
return connection
}
4 changes: 2 additions & 2 deletions apps/dashboard-app/app/api/callback/notion/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ export async function GET(req: NextRequest) {


return NextResponse.redirect(
`http://localhost:4000/dashboard/connections?access_token=${response.data.access_token}&workspace_name=${response.data.workspace_name}&workspace_icon=${response.data.workspace_icon}&workspace_id=${response.data.workspace_id}&database_id=${databaseId}`
`${process.env.NEXT_PUBLIC_URL}/dashboard/connections?access_token=${response.data.access_token}&workspace_name=${response.data.workspace_name}&workspace_icon=${response.data.workspace_icon}&workspace_id=${response.data.workspace_id}&database_id=${databaseId}`
);
}
}

return NextResponse.redirect('http://localhost:4000/dashboard/connections');
return NextResponse.redirect(`${process.env.NEXT_PUBLIC_URL}/dashboard/connections`);
}
9 changes: 9 additions & 0 deletions apps/dashboard-app/app/api/callback/openai/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { NextRequest, NextResponse } from 'next/server';

export async function GET(req: NextRequest) {
const apiKey = req.nextUrl.searchParams.get('apiKey');
if (apiKey) {
return NextResponse.redirect(`${process.env.NEXT_PUBLIC_URL}/dashboard/connections?apiKey=${apiKey}`);
}
return NextResponse.redirect(`${process.env.NEXT_PUBLIC_URL}/dashboard/connections`);
}
8 changes: 7 additions & 1 deletion apps/dashboard-app/app/dashboard/connections/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { onNotionConnection } from '../../../actions/connections/notion-connecti
import { getUserInfo } from '../../../actions/connections/user-connections'
import { useSearchParams } from 'next/navigation'
import ConnectionClient from '../../../components/ConnectionClient'
import { onOpenAIConnection } from '../../../actions/connections/openai-connections'

type Props = {
searchParams?: { [key: string]: string | undefined }
Expand All @@ -19,6 +20,7 @@ const Connections = () => {
const workspace_icon = params.get('workspace_icon')
const workspace_id = params.get('workspace_id')
const database_id = params.get('database_id')
const apiKey = params.get('apiKey')
const session = useSession()
const user = session?.data?.user
const userId = user?.id
Expand All @@ -29,6 +31,8 @@ const Connections = () => {
const onUserConnection = async () =>{
// @ts-ignore
await onNotionConnection({access_token,workspace_id,workspace_icon,workspace_name,database_id,userId})
// @ts-ignore
await onOpenAIConnection({apiKey,userId})
const user_info = await getUserInfo(userId || '')
const newConnections: Record<string, boolean> = {}
user_info?.connections.forEach((connection: any) => {
Expand All @@ -37,7 +41,7 @@ const Connections = () => {
setConnections(newConnections)
}
onUserConnection()
},[access_token,workspace_id,workspace_icon,workspace_name,database_id,userId])
},[access_token,workspace_id,workspace_icon,workspace_name,database_id,apiKey,userId])

return (
<div className='m-6'>
Expand All @@ -53,6 +57,8 @@ const Connections = () => {
icon={connection.image}
type={connection.title}
connected={connections}
showModal={connection.showModal}
formElements={connection.formElements}
/>
))}
</div>
Expand Down
14 changes: 12 additions & 2 deletions apps/dashboard-app/components/ConnectionClient.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,27 @@ type Props = {
description: string
callback?: () => void
connected: {} & any
formElements?: {
label: string
placeholder: string
type: string
name: string
}[]
showModal: boolean
}

const ConnectionClient = ({description,type,icon,title,connected}:Props) => {
const ConnectionClient = ({description,type,icon,title,connected,formElements,showModal}:Props) => {
return (
<ConnectionCard
description={description}
title={title}
icon={icon}
type={type}
connected={connected}
oauth_url={process.env.NEXT_PUBLIC_NOTION_OAUTH_URL || ''}
formElements={formElements}
showModal={showModal}
callback_url={(type==='OpenAI' ? process.env.NEXT_PUBLIC_URL+'/api/callback/openai' : '') || ''}
oauth_url={(type==='Notion' ? process.env.NEXT_PUBLIC_NOTION_OAUTH_URL : '' )|| ''}

/>
)
Expand Down
18 changes: 17 additions & 1 deletion apps/dashboard-app/lib/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,23 @@ export const CONNECTIONS: Connection[] = [
title: 'Notion',
description: 'Create entries in your notion dashboard and automate tasks.',
image: '/notion.png',
connectionKey: 'notionNode',
accessTokenKey: 'accessToken',
showModal: false
},
{
title: 'OpenAI',
description: 'Interact with openAI API',
image: '/openai.png',
accessTokenKey: 'accessToken',
showModal: true,
formElements: [
{
label: 'API Key',
placeholder: 'Enter your OpenAI API Key',
type: 'text',
name: 'apiKey'
}
]

}
]
87 changes: 8 additions & 79 deletions apps/dashboard-app/lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,87 +1,16 @@
import { ConnectionProviderProps } from '../providers/connections-provider'
import { z } from 'zod'

export const EditUserProfileSchema = z.object({
email: z.string().email('Required'),
name: z.string().min(1, 'Required'),
})

export const WorkflowFormSchema = z.object({
name: z.string().min(1, 'Required'),
description: z.string().min(1, 'Required'),
})

export type ConnectionTypes = 'Notion'
export type ConnectionTypes = 'Notion' | 'OpenAI'

export type Connection = {
title: ConnectionTypes
description: string
image: string
connectionKey: keyof ConnectionProviderProps
accessTokenKey?: string
alwaysTrue?: boolean
slackSpecial?: boolean
}

export type EditorCanvasTypes =
| 'Email'
| 'Condition'
| 'AI'
| 'Notion'
| 'Custom Webhook'
| 'Google Calendar'
| 'Trigger'
| 'Action'
| 'Wait'

export type EditorCanvasCardType = {
title: string
description: string
completed: boolean
current: boolean
metadata: any
type: EditorCanvasTypes
}

export type EditorNodeType = {
id: string
type: EditorCanvasCardType['type']
position: {
x: number
y: number
}
data: EditorCanvasCardType
}

export type EditorNode = EditorNodeType

export type EditorActions =
| {
type: 'LOAD_DATA'
payload: {
elements: EditorNode[]
edges: {
id: string
source: string
target: string
}[]
}
}
| {
type: 'UPDATE_NODE'
payload: {
elements: EditorNode[]
}
}
| { type: 'REDO' }
| { type: 'UNDO' }
| {
type: 'SELECTED_ELEMENT'
payload: {
element: EditorNode
}
}

export const nodeMapper: Record<string, string> = {
Notion: 'notionNode'
showModal: boolean
formElements?: {
label: string
placeholder: string
type: string
name: string
}[]
}
71 changes: 0 additions & 71 deletions apps/dashboard-app/providers/connections-provider.tsx
Original file line number Diff line number Diff line change
@@ -1,71 +0,0 @@
'use client'
import { createContext, useContext, useState } from 'react'

export type ConnectionProviderProps = {
notionNode: {
accessToken: string
databaseId: string
workspaceName: string
content: ''
}
workflowTemplate: {
notion?: string
}
setNotionNode: React.Dispatch<React.SetStateAction<any>>
setWorkFlowTemplate: React.Dispatch<
React.SetStateAction<{
discord?: string
notion?: string
slack?: string
}>
>
isLoading: boolean
setIsLoading: React.Dispatch<React.SetStateAction<boolean>>
}

type ConnectionWithChildProps = {
children: React.ReactNode
}

const InitialValues: ConnectionProviderProps = {
notionNode: {
accessToken: '',
databaseId: '',
workspaceName: '',
content: '',
},
workflowTemplate: {
notion: '',
},
isLoading: false,
setNotionNode: () => undefined,
setIsLoading: () => undefined,
setWorkFlowTemplate: () => undefined,
}

const ConnectionsContext = createContext(InitialValues)
const { Provider } = ConnectionsContext

export const ConnectionsProvider = ({ children }: ConnectionWithChildProps) => {
const [notionNode, setNotionNode] = useState(InitialValues.notionNode)
const [isLoading, setIsLoading] = useState(InitialValues.isLoading)
const [workflowTemplate, setWorkFlowTemplate] = useState(
InitialValues.workflowTemplate
)

const values = {
notionNode,
setNotionNode,
isLoading,
setIsLoading,
workflowTemplate,
setWorkFlowTemplate,
}

return <Provider value={values}>{children}</Provider>
}

export const useNodeConnections = () => {
const nodeConnection = useContext(ConnectionsContext)
return { nodeConnection }
}
Binary file added apps/dashboard-app/public/openai.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 36648f8

Please sign in to comment.