2
0

🔥 Remove chat-api app

This commit is contained in:
Baptiste Arnaud
2024-05-21 15:51:49 +02:00
parent b71dbdb783
commit e91fced41d
9 changed files with 1 additions and 482 deletions

View File

@@ -1,36 +0,0 @@
{
"name": "chat-api",
"version": "1.0.0",
"license": "AGPL-3.0-or-later",
"private": true,
"scripts": {
"dev": "dotenv -e ./.env -e ../../.env -- bun --hot src/index.ts",
"build": "dotenv -e ./.env -e ../../.env -- bun build --target=bun ./src/index.ts --outdir ./dist",
"start": "bun src/index.ts"
},
"dependencies": {
"@hono/prometheus": "1.0.0",
"@hono/sentry": "1.0.1",
"@hono/typebox-validator": "0.2.2",
"@sinclair/typebox": "0.32.5",
"@trpc/server": "10.40.0",
"@typebot.io/bot-engine": "workspace:*",
"@typebot.io/env": "workspace:*",
"@typebot.io/forge": "workspace:*",
"@typebot.io/forge-repository": "workspace:*",
"@typebot.io/lib": "workspace:*",
"@typebot.io/prisma": "workspace:*",
"@typebot.io/schemas": "workspace:*",
"@typebot.io/variables": "workspace:*",
"ai": "3.0.31",
"hono": "4.0.5",
"openai": "4.38.3",
"prom-client": "15.1.0"
},
"devDependencies": {
"dotenv-cli": "7.2.1",
"@typebot.io/tsconfig": "workspace:*",
"@types/react": "18.2.15",
"react": "18.2.0"
}
}

View File

@@ -1,30 +0,0 @@
import { env } from '@typebot.io/env'
import { mockedUser } from '@typebot.io/lib/mockedUser'
import prisma from '@typebot.io/lib/prisma'
export const getAuthenticatedUserId = async (
authorizationHeaderValue: string | undefined
): Promise<string | undefined> => {
if (env.NEXT_PUBLIC_E2E_TEST) return mockedUser.id
const bearerToken = extractBearerToken(authorizationHeaderValue)
if (!bearerToken) return
return authenticateByToken(bearerToken)
}
const authenticateByToken = async (
token: string
): Promise<string | undefined> => {
if (typeof window !== 'undefined') return
const apiToken = await prisma.apiToken.findFirst({
where: {
token,
},
select: {
ownerId: true,
},
})
return apiToken?.ownerId
}
const extractBearerToken = (authorizationHeaderValue: string | undefined) =>
authorizationHeaderValue?.slice(7)

View File

@@ -1,36 +0,0 @@
import { Hono } from 'hono'
import { webRuntime } from './runtimes/web'
import { whatsAppRuntime } from './runtimes/whatsapp'
import { prometheus } from '@hono/prometheus'
import { sentry } from '@hono/sentry'
import { env } from '@typebot.io/env'
import prisma from '@typebot.io/lib/prisma'
const app = new Hono()
app.use(
'*',
sentry({
environment: env.NODE_ENV,
dsn: env.NEXT_PUBLIC_SENTRY_DSN,
})
)
const { printMetrics, registerMetrics } = prometheus()
app.use('*', registerMetrics)
app.get('/metrics', async (c) => {
const honoMetrics = await (await printMetrics(c)).text()
const prismaMetrics = await prisma.$metrics.prometheus()
return c.text(`${honoMetrics}\n\n${prismaMetrics}`, 200)
})
app.get('/ping', (c) => c.json({ status: 'ok' }, 200))
app.route('/', webRuntime)
app.route('/', whatsAppRuntime)
export default {
port: process.env.PORT ?? 3002,
fetch: app.fetch,
}

View File

@@ -1,132 +0,0 @@
import { startChat } from '@typebot.io/bot-engine/apiHandlers/startChat'
import { continueChat } from '@typebot.io/bot-engine/apiHandlers/continueChat'
import { startChatPreview } from '@typebot.io/bot-engine/apiHandlers/startChatPreview'
import { getMessageStream } from '@typebot.io/bot-engine/apiHandlers/getMessageStream'
import { Hono } from 'hono'
import { cors } from 'hono/cors'
import { tbValidator } from '@hono/typebox-validator'
import { Type as t } from '@sinclair/typebox'
import { getAuthenticatedUserId } from '../auth'
export const webRuntime = new Hono()
webRuntime.use('*', cors())
webRuntime.post(
'/api/v1/typebots/:publicId/startChat',
tbValidator(
'json',
t.Object({
message: t.Optional(t.String()),
isStreamEnabled: t.Optional(t.Boolean()),
resultId: t.Optional(t.String()),
isOnlyRegistering: t.Optional(t.Boolean()),
prefilledVariables: t.Optional(t.Record(t.String(), t.Unknown())),
}),
(result, c) => {
if (!result.success) return c.json({ message: 'Invalid input' }, 400)
}
),
async (c) => {
const data = c.req.valid('json')
const { corsOrigin, ...response } = await startChat({
...data,
publicId: c.req.param('publicId'),
isStreamEnabled: data.isStreamEnabled ?? true,
isOnlyRegistering: data.isOnlyRegistering ?? false,
origin: c.req.header('origin'),
})
if (corsOrigin) c.res.headers.set('Access-Control-Allow-Origin', corsOrigin)
return c.json(response)
}
)
webRuntime.post(
'/api/v1/typebots/:id/preview/startChat',
tbValidator(
'json',
t.Object({
message: t.Optional(t.String()),
isStreamEnabled: t.Optional(t.Boolean()),
resultId: t.Optional(t.String()),
isOnlyRegistering: t.Optional(t.Boolean()),
prefilledVariables: t.Optional(t.Record(t.String(), t.Unknown())),
startFrom: t.Optional(
t.Union([
t.Object({
type: t.Literal('group'),
groupId: t.String(),
}),
t.Object({
type: t.Literal('event'),
eventId: t.String(),
}),
])
),
typebot: t.Optional(t.Any()),
}),
(result, c) => {
if (!result.success) return c.json({ message: 'Invalid input' }, 400)
}
),
async (c) => {
const data = c.req.valid('json')
const userId = !data.typebot
? await getAuthenticatedUserId(c.req.header('Authorization'))
: undefined
return c.json(
await startChatPreview({
...data,
typebotId: c.req.param('id'),
userId,
isStreamEnabled: data.isStreamEnabled ?? true,
isOnlyRegistering: data.isOnlyRegistering ?? false,
})
)
}
)
webRuntime.post(
'/api/v1/sessions/:sessionId/continueChat',
tbValidator(
'json',
t.Object({
message: t.Optional(t.String()),
}),
(result, c) => {
if (!result.success) return c.json({ message: 'Invalid input' }, 400)
}
),
async (c) => {
const data = c.req.valid('json')
const { corsOrigin, ...response } = await continueChat({
...data,
sessionId: c.req.param('sessionId'),
origin: c.req.header('origin'),
})
if (corsOrigin) c.res.headers.set('Access-Control-Allow-Origin', corsOrigin)
return c.json(response)
}
)
webRuntime.post(
'/api/v1/sessions/:sessionId/streamMessage',
tbValidator(
'json',
t.Object({
messages: t.Optional(t.Array(t.Any())),
}),
(result, c) => {
if (!result.success) return c.json({ message: 'Invalid input' }, 400)
}
),
async (c) => {
const data = c.req.valid('json')
const { stream, status, message } = await getMessageStream({
sessionId: c.req.param('sessionId'),
messages: data.messages,
})
if (!stream) return c.json({ message }, (status ?? 400) as ResponseInit)
return new Response(stream)
}
)

View File

@@ -1,51 +0,0 @@
import { receiveMessage } from '@typebot.io/bot-engine/apiHandlers/receiveMessage'
import { receiveMessagePreview } from '@typebot.io/bot-engine/apiHandlers/receiveMessagePreview'
import { tbValidator } from '@hono/typebox-validator'
import { Hono } from 'hono'
import { Type as t } from '@sinclair/typebox'
export const whatsAppRuntime = new Hono()
whatsAppRuntime.post(
'/api/v1/workspaces/:workspaceId/whatsapp/:credentialsId/webhook',
tbValidator(
'json',
t.Object({
object: t.String(),
entry: t.Any(),
}),
(result, c) => {
if (!result.success) return c.json({ message: 'Invalid input' }, 400)
}
),
async (c) => {
const data = c.req.valid('json')
receiveMessage({
workspaceId: c.req.param('workspaceId'),
credentialsId: c.req.param('credentialsId'),
...data,
})
return c.json({ message: 'Webhook received' }, 200)
}
)
whatsAppRuntime.post(
'/api/v1/whatsapp/preview/webhook',
tbValidator(
'json',
t.Object({
object: t.String(),
entry: t.Any(),
}),
(result, c) => {
if (!result.success) return c.json({ message: 'Invalid input' }, 400)
}
),
async (c) => {
const data = c.req.valid('json')
receiveMessagePreview({
...data,
})
return c.json({ message: 'Webhook received' }, 200)
}
)

View File

@@ -1,8 +0,0 @@
{
"extends": "@typebot.io/tsconfig/base.json",
"include": ["src/**/*.ts"],
"exclude": ["node_modules"],
"compilerOptions": {
"jsx": "react"
}
}