🐛 (openai) Fix ask assistant not correctly referencing uploaded f… (#1469)
…iles Closes #1468, closes #1467, closes #1211
This commit is contained in:
@ -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",
|
||||
@ -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",
|
||||
@ -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:*"
|
||||
|
@ -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,
|
||||
@ -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
|
||||
@ -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(
|
||||
@ -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,
|
||||
|
@ -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({
|
||||
@ -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))))
|
||||
},
|
||||
})
|
||||
}
|
||||
|
@ -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,
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user