2
0

🐛 (openai) Fix ask assistant not correctly referencing uploaded f… (#1469)

…iles

Closes #1468, closes #1467, closes #1211
This commit is contained in:
Baptiste Arnaud
2024-04-24 16:11:06 +02:00
committed by GitHub
parent a45e8ec8a8
commit dc1929e15b
57 changed files with 1576 additions and 448 deletions

View File

@ -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"
}
}

View File

@ -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": {

View File

@ -19149,7 +19149,7 @@
"assistantId": {
"type": "string"
},
"threadId": {
"threadVariableId": {
"type": "string"
},
"message": {
@ -19186,6 +19186,9 @@
}
}
}
},
"threadId": {
"type": "string"
}
},
"required": [

View File

@ -9977,7 +9977,7 @@
"assistantId": {
"type": "string"
},
"threadId": {
"threadVariableId": {
"type": "string"
},
"message": {
@ -10014,6 +10014,9 @@
}
}
}
},
"threadId": {
"type": "string"
}
},
"required": [
@ -12480,18 +12483,10 @@
}
}
}
},
"runtime": {
"type": "string",
"enum": [
"edge",
"nodejs"
]
}
},
"required": [
"messages",
"runtime"
"messages"
]
},
"lastBubbleBlockId": {
@ -12768,13 +12763,6 @@
true
]
},
"runtime": {
"type": "string",
"enum": [
"edge",
"nodejs"
]
},
"lastBubbleBlockId": {
"type": "string"
},
@ -12784,8 +12772,7 @@
},
"required": [
"type",
"stream",
"runtime"
"stream"
],
"title": "Exec stream"
},

View File

@ -48,6 +48,6 @@
"postcss": "8.4.26",
"prettier": "3.0.0",
"tailwindcss": "3.3.3",
"typescript": "5.3.2"
"typescript": "5.4.5"
}
}

View File

@ -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:*"

View File

@ -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,

View File

@ -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))))
},
})
}

View File

@ -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,
})
}