2
0

🐛 (openai) Fix assistant sequence was not correctly saved

Also add logs to debug open ai errors. Related to #393.
This commit is contained in:
Baptiste Arnaud
2023-03-15 17:47:05 +01:00
parent 76a8064e7c
commit 5aec8b6c66
4 changed files with 62 additions and 35 deletions

View File

@@ -1,27 +1,38 @@
import { Variable } from '@typebot.io/schemas'
import { parseVariables } from './parseVariables'
import {
defaultParseVariablesOptions,
parseVariables,
ParseVariablesOptions,
} from './parseVariables'
export const deepParseVariables =
(variables: Variable[]) =>
(
variables: Variable[],
options: ParseVariablesOptions = defaultParseVariablesOptions
) =>
<T extends Record<string, unknown>>(object: T): T =>
Object.keys(object).reduce<T>((newObj, key) => {
const currentValue = object[key]
if (typeof currentValue === 'string')
return { ...newObj, [key]: parseVariables(variables)(currentValue) }
return {
...newObj,
[key]: parseVariables(variables, options)(currentValue),
}
if (currentValue instanceof Object && currentValue.constructor === Object)
return {
...newObj,
[key]: deepParseVariables(variables)(
currentValue as Record<string, unknown>
),
[key]: deepParseVariables(
variables,
options
)(currentValue as Record<string, unknown>),
}
if (currentValue instanceof Array)
return {
...newObj,
[key]: currentValue.map(deepParseVariables(variables)),
[key]: currentValue.map(deepParseVariables(variables, options)),
}
return { ...newObj, [key]: currentValue }

View File

@@ -2,13 +2,22 @@ import { isDefined } from '@typebot.io/lib'
import { Variable, VariableWithValue } from '@typebot.io/schemas'
import { safeStringify } from './safeStringify'
export type ParseVariablesOptions = {
fieldToParse?: 'value' | 'id'
escapeForJson?: boolean
takeLatestIfList?: boolean
}
export const defaultParseVariablesOptions: ParseVariablesOptions = {
fieldToParse: 'value',
escapeForJson: false,
takeLatestIfList: false,
}
export const parseVariables =
(
variables: Variable[],
options: { fieldToParse?: 'value' | 'id'; escapeForJson?: boolean } = {
fieldToParse: 'value',
escapeForJson: false,
}
options: ParseVariablesOptions = defaultParseVariablesOptions
) =>
(text: string | undefined): string => {
if (!text || text === '') return ''
@@ -27,7 +36,11 @@ export const parseVariables =
return jsonParse(
typeof value !== 'string' ? JSON.stringify(value) : value
)
const parsedValue = safeStringify(value)
const parsedValue = safeStringify(
options.takeLatestIfList && Array.isArray(value)
? value[value.length - 1]
: value
)
if (!parsedValue) return ''
return parsedValue
})