Skip to content

Commit

Permalink
🐛 (openai) Fix ask assistant not correctly referencing uploaded f… (#…
Browse files Browse the repository at this point in the history
…1469)

…iles

Closes #1468, closes #1467, closes #1211
  • Loading branch information
baptisteArno authored Apr 24, 2024
1 parent a45e8ec commit dc1929e
Show file tree
Hide file tree
Showing 57 changed files with 1,572 additions and 444 deletions.
2 changes: 1 addition & 1 deletion apps/builder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@
"eslint-config-custom": "workspace:*",
"next-runtime-env": "1.6.2",
"superjson": "1.12.4",
"typescript": "5.3.2",
"typescript": "5.4.5",
"zod": "3.22.4"
}
}
4 changes: 2 additions & 2 deletions apps/chat-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
"@typebot.io/prisma": "workspace:*",
"@typebot.io/schemas": "workspace:*",
"@typebot.io/variables": "workspace:*",
"ai": "3.0.12",
"ai": "3.0.31",
"hono": "4.0.5",
"openai": "4.28.4",
"openai": "4.38.3",
"prom-client": "15.1.0"
},
"devDependencies": {
Expand Down
5 changes: 4 additions & 1 deletion apps/docs/openapi/builder.json
Original file line number Diff line number Diff line change
Expand Up @@ -19149,7 +19149,7 @@
"assistantId": {
"type": "string"
},
"threadId": {
"threadVariableId": {
"type": "string"
},
"message": {
Expand Down Expand Up @@ -19186,6 +19186,9 @@
}
}
}
},
"threadId": {
"type": "string"
}
},
"required": [
Expand Down
25 changes: 6 additions & 19 deletions apps/docs/openapi/viewer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9977,7 +9977,7 @@
"assistantId": {
"type": "string"
},
"threadId": {
"threadVariableId": {
"type": "string"
},
"message": {
Expand Down Expand Up @@ -10014,6 +10014,9 @@
}
}
}
},
"threadId": {
"type": "string"
}
},
"required": [
Expand Down Expand Up @@ -12480,18 +12483,10 @@
}
}
}
},
"runtime": {
"type": "string",
"enum": [
"edge",
"nodejs"
]
}
},
"required": [
"messages",
"runtime"
"messages"
]
},
"lastBubbleBlockId": {
Expand Down Expand Up @@ -12768,13 +12763,6 @@
true
]
},
"runtime": {
"type": "string",
"enum": [
"edge",
"nodejs"
]
},
"lastBubbleBlockId": {
"type": "string"
},
Expand All @@ -12784,8 +12772,7 @@
},
"required": [
"type",
"stream",
"runtime"
"stream"
],
"title": "Exec stream"
},
Expand Down
2 changes: 1 addition & 1 deletion apps/landing-page/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@
"postcss": "8.4.26",
"prettier": "3.0.0",
"tailwindcss": "3.3.3",
"typescript": "5.3.2"
"typescript": "5.4.5"
}
}
6 changes: 3 additions & 3 deletions apps/viewer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"@typebot.io/js": "workspace:*",
"@typebot.io/nextjs": "workspace:*",
"@typebot.io/prisma": "workspace:*",
"ai": "3.0.12",
"ai": "3.0.31",
"bot-engine": "workspace:*",
"cors": "2.8.5",
"google-spreadsheet": "4.1.1",
Expand All @@ -30,7 +30,7 @@
"next": "14.1.0",
"nextjs-cors": "2.1.2",
"nodemailer": "6.9.8",
"openai": "4.28.4",
"openai": "4.38.3",
"qs": "6.11.2",
"react": "18.2.0",
"react-dom": "18.2.0",
Expand Down Expand Up @@ -62,7 +62,7 @@
"next-runtime-env": "1.6.2",
"papaparse": "5.4.1",
"superjson": "1.12.4",
"typescript": "5.3.2",
"typescript": "5.4.5",
"zod": "3.22.4",
"@typebot.io/playwright": "workspace:*",
"@typebot.io/results": "workspace:*"
Expand Down
7 changes: 5 additions & 2 deletions apps/viewer/src/app/api/integrations/openai/streamer/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { NextResponse } from 'next/dist/server/web/spec-extension/response'
import { getBlockById } from '@typebot.io/schemas/helpers'
import { forgedBlocks } from '@typebot.io/forge-repository/definitions'
import { decryptV2 } from '@typebot.io/lib/api/encryption/decryptV2'
import { ReadOnlyVariableStore } from '@typebot.io/forge'
import { VariableStore } from '@typebot.io/forge'
import {
ParseVariablesOptions,
parseVariables,
Expand Down Expand Up @@ -38,6 +38,7 @@ export async function OPTIONS() {
})
}

// Deprecated in favor of `/api/v1/sessions/:sessionId/streamMessage`.
export async function POST(req: Request) {
const { sessionId, messages } = (await req.json()) as {
messages: OpenAI.Chat.ChatCompletionMessage[] | undefined
Expand Down Expand Up @@ -140,7 +141,7 @@ export async function POST(req: Request) {
credentials.data,
credentials.iv
)
const variables: ReadOnlyVariableStore = {
const variables: VariableStore = {
list: () => state.typebotsQueue[0].typebot.variables,
get: (id: string) => {
const variable = state.typebotsQueue[0].typebot.variables.find(
Expand All @@ -150,6 +151,8 @@ export async function POST(req: Request) {
},
parse: (text: string, params?: ParseVariablesOptions) =>
parseVariables(state.typebotsQueue[0].typebot.variables, params)(text),
// eslint-disable-next-line @typescript-eslint/no-unused-vars
set: (_1: string, _2: unknown) => {},
}
const stream = await action.run.stream.run({
credentials: decryptedCredentials,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ export async function POST(
req: Request,
{ params }: { params: { sessionId: string } }
) {
if (process.env.VERCEL_ENV)
return NextResponse.json(
{ message: "Can't get streaming if hosted on Vercel" },
{ status: 400, headers: responseHeaders }
)
const messages =
typeof req.body === 'string' ? JSON.parse(req.body) : req.body
const { stream, status, message } = await getMessageStream({
Expand All @@ -39,7 +34,22 @@ export async function POST(
})
if (!stream)
return NextResponse.json({ message }, { status, headers: responseHeaders })
return new StreamingTextResponse(stream, {
headers: responseHeaders,
return new StreamingTextResponse(
stream.pipeThrough(createStreamDataTransformer()),
{
headers: responseHeaders,
}
)
}

const createStreamDataTransformer = () => {
const encoder = new TextEncoder()
const decoder = new TextDecoder()
return new TransformStream({
transform: async (chunk, controller) => {
const decodedChunk = decoder.decode(chunk)
if (decodedChunk[0] !== '0') return
controller.enqueue(encoder.encode(JSON.parse(decodedChunk.slice(2))))
},
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { getMessageStream } from '@typebot.io/bot-engine/apiHandlers/getMessageStream'
import { StreamingTextResponse } from 'ai'
import { NextResponse } from 'next/server'

export const dynamic = 'force-dynamic'

const responseHeaders = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'POST, OPTIONS',
'Access-Control-Expose-Headers': 'Content-Length, X-JSON',
'Access-Control-Allow-Headers': '*',
}

export async function OPTIONS() {
return new Response('ok', {
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'POST',
'Access-Control-Expose-Headers': 'Content-Length, X-JSON',
'Access-Control-Allow-Headers': '*',
},
})
}

export async function POST(
req: Request,
{ params }: { params: { sessionId: string } }
) {
const messages =
typeof req.body === 'string' ? JSON.parse(req.body) : req.body
const { stream, status, message } = await getMessageStream({
sessionId: params.sessionId,
messages,
})
if (!stream)
return NextResponse.json({ message }, { status, headers: responseHeaders })
return new StreamingTextResponse(stream, {
headers: responseHeaders,
})
}
25 changes: 22 additions & 3 deletions packages/bot-engine/apiHandlers/getMessageStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ChatCompletionOpenAIOptions } from '@typebot.io/schemas/features/blocks
import { OpenAI } from 'openai'
import { decryptV2 } from '@typebot.io/lib/api/encryption/decryptV2'
import { forgedBlocks } from '@typebot.io/forge-repository/definitions'
import { ReadOnlyVariableStore } from '@typebot.io/forge'
import { VariableStore } from '@typebot.io/forge'
import {
ParseVariablesOptions,
parseVariables,
Expand All @@ -13,6 +13,9 @@ import { getCredentials } from '../queries/getCredentials'
import { getSession } from '../queries/getSession'
import { getBlockById } from '@typebot.io/schemas/helpers'
import { isForgedBlockType } from '@typebot.io/schemas/features/blocks/forged/helpers'
import { updateVariablesInSession } from '@typebot.io/variables/updateVariablesInSession'
import { updateSession } from '../queries/updateSession'
import { deepParseVariables } from '@typebot.io/variables/deepParseVariables'

type Props = {
sessionId: string
Expand Down Expand Up @@ -92,7 +95,8 @@ export const getMessageStream = async ({ sessionId, messages }: Props) => {
credentials.data,
credentials.iv
)
const variables: ReadOnlyVariableStore = {

const variables: VariableStore = {
list: () => session.state.typebotsQueue[0].typebot.variables,
get: (id: string) => {
const variable = session.state.typebotsQueue[0].typebot.variables.find(
Expand All @@ -105,10 +109,25 @@ export const getMessageStream = async ({ sessionId, messages }: Props) => {
session.state.typebotsQueue[0].typebot.variables,
params
)(text),
set: async (id: string, value: unknown) => {
const variable = session.state.typebotsQueue[0].typebot.variables.find(
(variable) => variable.id === id
)
if (!variable) return
await updateSession({
id: session.id,
state: updateVariablesInSession(session.state)([
{ ...variable, value },
]),
isReplying: undefined,
})
},
}
const stream = await action.run.stream.run({
credentials: decryptedCredentials,
options: block.options,
options: deepParseVariables(
session.state.typebotsQueue[0].typebot.variables
)(block.options),
variables,
})
if (!stream) return { status: 500, message: 'Could not create stream' }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,7 @@ export const createChatCompletionOpenAI = async (
isNextBubbleMessageWithAssistantMessage(typebot)(
blockId,
assistantMessageVariableName
) &&
(!process.env.VERCEL_ENV ||
(isPlaneteScale() && credentials && isCredentialsV2(credentials)))
)
) {
return {
clientSideActions: [
Expand All @@ -102,7 +100,6 @@ export const createChatCompletionOpenAI = async (
content?: string
role: (typeof chatCompletionMessageRoles)[number]
}[],
runtime: process.env.VERCEL_ENV ? 'edge' : 'nodejs',
},
expectsDedicatedReply: true,
},
Expand Down
5 changes: 1 addition & 4 deletions packages/bot-engine/forge/executeForgedBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ export const executeForgedBlock = async (
action.run.stream.getStreamVariableId(block.options)
) &&
state.isStreamEnabled &&
!state.whatsApp &&
(!process.env.VERCEL_ENV ||
(isPlaneteScale() && credentials && isCredentialsV2(credentials)))
!state.whatsApp
) {
return {
outgoingEdgeId: block.outgoingEdgeId,
Expand All @@ -69,7 +67,6 @@ export const executeForgedBlock = async (
type: 'stream',
expectsDedicatedReply: true,
stream: true,
runtime: process.env.VERCEL_ENV ? 'edge' : 'nodejs',
},
],
}
Expand Down
4 changes: 2 additions & 2 deletions packages/bot-engine/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"@typebot.io/tsconfig": "workspace:*",
"@typebot.io/variables": "workspace:*",
"@udecode/plate-common": "30.4.5",
"ai": "3.0.12",
"ai": "3.0.31",
"chrono-node": "2.7.5",
"date-fns": "2.30.0",
"date-fns-tz": "2.0.0",
Expand All @@ -29,7 +29,7 @@
"libphonenumber-js": "1.10.37",
"node-html-parser": "6.1.5",
"nodemailer": "6.9.8",
"openai": "4.28.4",
"openai": "4.38.3",
"qs": "6.11.2",
"stripe": "12.13.0"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/bot-engine/queries/updateSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { SessionState } from '@typebot.io/schemas'
type Props = {
id: string
state: SessionState
isReplying: boolean
isReplying: boolean | undefined
}

export const updateSession = ({
Expand Down
4 changes: 2 additions & 2 deletions packages/deprecated/bot-engine/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"main": "dist/index.js",
"module": "dist/index.mjs",
"scripts": {
"build": "pnpm tsc --noEmit && tsup",
"build": "tsup",
"dev": "tsup --watch"
},
"dependencies": {
Expand Down Expand Up @@ -40,7 +40,7 @@
"@typebot.io/tsconfig": "workspace:*",
"tsup": "6.5.0",
"typebot-js": "workspace:*",
"typescript": "5.3.2",
"typescript": "5.4.5",
"@typebot.io/lib": "workspace:*",
"@typebot.io/env": "workspace:*"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/deprecated/typebot-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"jest-environment-jsdom": "29.4.1",
"prettier": "2.8.3",
"ts-jest": "29.0.5",
"typescript": "5.3.2",
"typescript": "5.4.5",
"@typebot.io/tsconfig": "workspace:*"
}
}
Loading

0 comments on commit dc1929e

Please sign in to comment.