2
0

🐛 (dify) Fix Dify error when inputs are empty

This commit is contained in:
Baptiste Arnaud
2024-02-12 11:41:57 +01:00
parent 5226b06fe1
commit f5bdba53b9
11 changed files with 132 additions and 67 deletions

View File

@@ -1,4 +0,0 @@
const urlRegex =
/^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)$/
export const validateUrl = (url: string) => urlRegex.test(url)

View File

@@ -11,7 +11,6 @@ import { executeGroup, parseInput } from './executeGroup'
import { getNextGroup } from './getNextGroup'
import { validateEmail } from './blocks/inputs/email/validateEmail'
import { formatPhoneNumber } from './blocks/inputs/phone/formatPhoneNumber'
import { validateUrl } from './blocks/inputs/url/validateUrl'
import { resumeWebhookExecution } from './blocks/integrations/webhook/resumeWebhookExecution'
import { upsertAnswer } from './queries/upsertAnswer'
import { parseButtonsReply } from './blocks/inputs/buttons/parseButtonsReply'
@@ -42,6 +41,7 @@ import { resumeChatCompletion } from './blocks/integrations/legacy/openai/resume
import { env } from '@typebot.io/env'
import { downloadMedia } from './whatsapp/downloadMedia'
import { uploadFileToBucket } from '@typebot.io/lib/s3/uploadFileToBucket'
import { isURL } from '@typebot.io/lib/validators/isURL'
type Params = {
version: 1 | 2
@@ -450,7 +450,7 @@ const parseReply =
}
case InputBlockType.URL: {
if (!reply) return { status: 'fail' }
const isValid = validateUrl(reply)
const isValid = isURL(reply, { require_protocol: false })
if (!isValid) return { status: 'fail' }
return { status: 'success', reply: reply }
}
@@ -477,7 +477,7 @@ const parseReply =
? { status: 'fail' }
: { status: 'skip' }
const urls = reply.split(', ')
const status = urls.some((url) => validateUrl(url)) ? 'success' : 'fail'
const status = urls.some((url) => isURL(url)) ? 'success' : 'fail'
return { status, reply: reply }
}
case InputBlockType.PAYMENT: {

View File

@@ -1,6 +1,6 @@
import { createAction, option } from '@typebot.io/forge'
import { isDefined, isEmpty } from '@typebot.io/lib'
import { got } from 'got'
import { HTTPError, got } from 'got'
import { auth } from '../auth'
import { DifyResponse } from '../types'
import { defaultBaseUrl } from '../constants'
@@ -41,41 +41,53 @@ export const createChatMessage = createAction({
credentials: { apiEndpoint, apiKey },
options: { conversation_id, query, user, inputs, responseMapping },
variables,
logs,
}) => {
const res: DifyResponse = await got
.post((apiEndpoint ?? defaultBaseUrl) + '/v1/chat-messages', {
headers: {
Authorization: `Bearer ${apiKey}`,
},
json: {
inputs: inputs?.reduce((acc, { key, value }) => {
if (isEmpty(key) || isEmpty(value)) return acc
return {
...acc,
[key]: value,
}
}, {}),
query,
response_mode: 'blocking',
conversation_id,
user,
files: []
},
try {
const res: DifyResponse = await got
.post((apiEndpoint ?? defaultBaseUrl) + '/v1/chat-messages', {
headers: {
Authorization: `Bearer ${apiKey}`,
},
json: {
inputs:
inputs?.reduce((acc, { key, value }) => {
if (isEmpty(key) || isEmpty(value)) return acc
return {
...acc,
[key]: value,
}
}, {}) ?? {},
query,
response_mode: 'blocking',
conversation_id,
user,
files: [],
},
})
.json()
responseMapping?.forEach((mapping) => {
if (!mapping.variableId) return
const item = mapping.item ?? 'Answer'
if (item === 'Answer') variables.set(mapping.variableId, res.answer)
if (item === 'Conversation ID')
variables.set(mapping.variableId, res.conversation_id)
if (item === 'Total Tokens')
variables.set(mapping.variableId, res.metadata.usage.total_tokens)
})
.json()
responseMapping?.forEach((mapping) => {
if (!mapping.variableId) return
const item = mapping.item ?? 'Answer'
if (item === 'Answer') variables.set(mapping.variableId, res.answer)
if (item === 'Conversation ID')
variables.set(mapping.variableId, res.conversation_id)
if (item === 'Total Tokens')
variables.set(mapping.variableId, res.metadata.usage.total_tokens)
})
} catch (error) {
if (error instanceof HTTPError)
return logs.add({
status: 'error',
description: error.message,
details: error.response.body,
})
console.error(error)
}
},
},
})

View File

@@ -1,17 +1,27 @@
import { option, AuthDefinition } from '@typebot.io/forge'
import { defaultBaseUrl } from './constants'
import { isURL } from '@typebot.io/lib/validators/isURL'
const extractBaseUrl = (val: string | undefined) => {
if (!val) return val
const url = new URL(val)
return url.origin
}
export const auth = {
type: 'encryptedCredentials',
name: 'Dify.AI account',
schema: option.object({
apiEndpoint: option.string.layout({
label: 'API Endpoint',
isRequired: true,
helperText: 'URI where the Service API is hosted.',
withVariableButton: false,
defaultValue: defaultBaseUrl,
}),
apiEndpoint: option.string
.layout({
label: 'API Endpoint',
isRequired: true,
helperText: 'URI where the Service API is hosted.',
withVariableButton: false,
defaultValue: defaultBaseUrl,
})
.refine((val) => !val || isURL(val))
.transform(extractBaseUrl),
apiKey: option.string.layout({
label: 'App API key',
isRequired: true,

View File

@@ -15,6 +15,7 @@
"@typebot.io/tsconfig": "workspace:*",
"@types/escape-html": "^1.0.4",
"@types/nodemailer": "6.4.8",
"@types/validator": "13.11.9",
"next": "14.1.0",
"nodemailer": "6.9.3",
"typescript": "5.3.2"
@@ -44,7 +45,7 @@
"remark-parse": "11.0.0",
"stripe": "12.13.0",
"unified": "11.0.4",
"zod": "3.22.4",
"ky": "1.1.3"
"validator": "13.11.0",
"zod": "3.22.4"
}
}

View File

@@ -0,0 +1,10 @@
import isURL, { IsURLOptions } from 'validator/lib/isURL'
const customIsURL = (val: string, options?: IsURLOptions) =>
isURL(val, {
protocols: ['https', 'http'],
require_protocol: true,
...options,
})
export { customIsURL as isURL }