mirror of
https://github.com/langgenius/dify.git
synced 2026-01-14 06:07:33 +08:00
Signed-off-by: -LAN- <laipz8200@outlook.com> Signed-off-by: kenwoodjw <blackxin55+@gmail.com> Signed-off-by: Yongtao Huang <yongtaoh2022@gmail.com> Signed-off-by: yihong0618 <zouzou0208@gmail.com> Signed-off-by: zhanluxianshen <zhanluxianshen@163.com> Co-authored-by: -LAN- <laipz8200@outlook.com> Co-authored-by: GuanMu <ballmanjq@gmail.com> Co-authored-by: Davide Delbianco <davide.delbianco@outlook.com> Co-authored-by: NeatGuyCoding <15627489+NeatGuyCoding@users.noreply.github.com> Co-authored-by: kenwoodjw <blackxin55+@gmail.com> Co-authored-by: Yongtao Huang <yongtaoh2022@gmail.com> Co-authored-by: Yongtao Huang <99629139+hyongtao-db@users.noreply.github.com> Co-authored-by: Qiang Lee <18018968632@163.com> Co-authored-by: 李强04 <liqiang04@gaotu.cn> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Asuka Minato <i@asukaminato.eu.org> Co-authored-by: Matri Qi <matrixdom@126.com> Co-authored-by: huayaoyue6 <huayaoyue@163.com> Co-authored-by: Bowen Liang <liangbowen@gf.com.cn> Co-authored-by: znn <jubinkumarsoni@gmail.com> Co-authored-by: crazywoola <427733928@qq.com> Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: yihong <zouzou0208@gmail.com> Co-authored-by: Muke Wang <shaodwaaron@gmail.com> Co-authored-by: wangmuke <wangmuke@kingsware.cn> Co-authored-by: Wu Tianwei <30284043+WTW0313@users.noreply.github.com> Co-authored-by: quicksand <quicksandzn@gmail.com> Co-authored-by: 非法操作 <hjlarry@163.com> Co-authored-by: zxhlyh <jasonapring2015@outlook.com> Co-authored-by: Eric Guo <eric.guocz@gmail.com> Co-authored-by: Zhedong Cen <cenzhedong2@126.com> Co-authored-by: jiangbo721 <jiangbo721@163.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: hjlarry <25834719+hjlarry@users.noreply.github.com> Co-authored-by: lxsummer <35754229+lxjustdoit@users.noreply.github.com> Co-authored-by: 湛露先生 <zhanluxianshen@163.com> Co-authored-by: Guangdong Liu <liugddx@gmail.com> Co-authored-by: QuantumGhost <obelisk.reg+git@gmail.com> Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: Yessenia-d <yessenia.contact@gmail.com> Co-authored-by: huangzhuo1949 <167434202+huangzhuo1949@users.noreply.github.com> Co-authored-by: huangzhuo <huangzhuo1@xiaomi.com> Co-authored-by: 17hz <0x149527@gmail.com> Co-authored-by: Amy <1530140574@qq.com> Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: Nite Knite <nkCoding@gmail.com> Co-authored-by: Yeuoly <45712896+Yeuoly@users.noreply.github.com> Co-authored-by: Petrus Han <petrus.hanks@gmail.com> Co-authored-by: iamjoel <2120155+iamjoel@users.noreply.github.com> Co-authored-by: Kalo Chin <frog.beepers.0n@icloud.com> Co-authored-by: Ujjwal Maurya <ujjwalsbx@gmail.com> Co-authored-by: Maries <xh001x@hotmail.com>
200 lines
5.6 KiB
TypeScript
200 lines
5.6 KiB
TypeScript
import type { UserInputFormItem } from '@/types/app'
|
|
import type { PromptVariable } from '@/models/debug'
|
|
|
|
export const userInputsFormToPromptVariables = (useInputs: UserInputFormItem[] | null, dataset_query_variable?: string) => {
|
|
if (!useInputs)
|
|
return []
|
|
const promptVariables: PromptVariable[] = []
|
|
useInputs.forEach((item: any) => {
|
|
const isParagraph = !!item.paragraph
|
|
|
|
const [type, content] = (() => {
|
|
if (isParagraph)
|
|
return ['paragraph', item.paragraph]
|
|
|
|
if (item['text-input'])
|
|
return ['string', item['text-input']]
|
|
|
|
if (item.number)
|
|
return ['number', item.number]
|
|
|
|
if (item.checkbox)
|
|
return ['boolean', item.checkbox]
|
|
|
|
if (item.file)
|
|
return ['file', item.file]
|
|
|
|
if (item['file-list'])
|
|
return ['file-list', item['file-list']]
|
|
|
|
if (item.external_data_tool)
|
|
return [item.external_data_tool.type, item.external_data_tool]
|
|
|
|
if (item.json_object)
|
|
return ['json_object', item.json_object]
|
|
|
|
return ['select', item.select || {}]
|
|
})()
|
|
const is_context_var = dataset_query_variable === content?.variable
|
|
|
|
if (type === 'string' || type === 'paragraph') {
|
|
promptVariables.push({
|
|
key: content.variable,
|
|
name: content.label,
|
|
required: content.required,
|
|
type,
|
|
max_length: content.max_length,
|
|
options: [],
|
|
is_context_var,
|
|
hide: content.hide,
|
|
default: content.default,
|
|
})
|
|
}
|
|
else if (type === 'number') {
|
|
promptVariables.push({
|
|
key: content.variable,
|
|
name: content.label,
|
|
required: content.required,
|
|
type,
|
|
options: [],
|
|
hide: content.hide,
|
|
default: content.default,
|
|
})
|
|
}
|
|
else if (type === 'select') {
|
|
promptVariables.push({
|
|
key: content.variable,
|
|
name: content.label,
|
|
required: content.required,
|
|
type: 'select',
|
|
options: content.options,
|
|
is_context_var,
|
|
hide: content.hide,
|
|
default: content.default,
|
|
})
|
|
}
|
|
else if (type === 'file') {
|
|
promptVariables.push({
|
|
key: content.variable,
|
|
name: content.label,
|
|
required: content.required,
|
|
type,
|
|
config: {
|
|
allowed_file_types: content.allowed_file_types,
|
|
allowed_file_extensions: content.allowed_file_extensions,
|
|
allowed_file_upload_methods: content.allowed_file_upload_methods,
|
|
number_limits: 1,
|
|
},
|
|
hide: content.hide,
|
|
default: content.default,
|
|
})
|
|
}
|
|
else if (type === 'file-list') {
|
|
promptVariables.push({
|
|
key: content.variable,
|
|
name: content.label,
|
|
required: content.required,
|
|
type,
|
|
config: {
|
|
allowed_file_types: content.allowed_file_types,
|
|
allowed_file_extensions: content.allowed_file_extensions,
|
|
allowed_file_upload_methods: content.allowed_file_upload_methods,
|
|
number_limits: content.max_length,
|
|
},
|
|
hide: content.hide,
|
|
default: content.default,
|
|
})
|
|
}
|
|
else {
|
|
promptVariables.push({
|
|
key: content.variable,
|
|
name: content.label,
|
|
required: content.required,
|
|
type: content.type,
|
|
enabled: content.enabled,
|
|
config: content.config,
|
|
icon: content.icon,
|
|
icon_background: content.icon_background,
|
|
is_context_var,
|
|
hide: content.hide,
|
|
})
|
|
}
|
|
})
|
|
return promptVariables
|
|
}
|
|
|
|
export const promptVariablesToUserInputsForm = (promptVariables: PromptVariable[]) => {
|
|
const userInputs: UserInputFormItem[] = []
|
|
promptVariables.filter(({ key, name }) => {
|
|
return key && key.trim() && name && name.trim()
|
|
}).forEach((item: any) => {
|
|
if (item.type === 'string' || item.type === 'paragraph') {
|
|
userInputs.push({
|
|
[item.type === 'string' ? 'text-input' : 'paragraph']: {
|
|
label: item.name,
|
|
variable: item.key,
|
|
required: item.required !== false, // default true
|
|
max_length: item.max_length,
|
|
default: '',
|
|
hide: item.hide,
|
|
},
|
|
} as any)
|
|
return
|
|
}
|
|
if (item.type === 'number' || item.type === 'checkbox') {
|
|
userInputs.push({
|
|
[item.type]: {
|
|
label: item.name,
|
|
variable: item.key,
|
|
required: item.required !== false, // default true
|
|
default: '',
|
|
hide: item.hide,
|
|
},
|
|
} as any)
|
|
}
|
|
else if (item.type === 'select') {
|
|
userInputs.push({
|
|
select: {
|
|
label: item.name,
|
|
variable: item.key,
|
|
required: item.required !== false, // default true
|
|
options: item.options,
|
|
default: item.default ?? '',
|
|
hide: item.hide,
|
|
},
|
|
} as any)
|
|
}
|
|
else {
|
|
userInputs.push({
|
|
external_data_tool: {
|
|
label: item.name,
|
|
variable: item.key,
|
|
enabled: item.enabled,
|
|
type: item.type,
|
|
config: item.config,
|
|
required: item.required,
|
|
icon: item.icon,
|
|
icon_background: item.icon_background,
|
|
hide: item.hide,
|
|
},
|
|
} as any)
|
|
}
|
|
})
|
|
|
|
return userInputs
|
|
}
|
|
|
|
export const formatBooleanInputs = (useInputs?: PromptVariable[] | null, inputs?: Record<string, string | number | object | boolean> | null) => {
|
|
if(!useInputs)
|
|
return inputs
|
|
const res = { ...(inputs || {}) }
|
|
useInputs.forEach((item) => {
|
|
const isBooleanInput = item.type === 'boolean'
|
|
if (isBooleanInput) {
|
|
// Convert boolean inputs to boolean type
|
|
res[item.key] = !!res[item.key]
|
|
}
|
|
})
|
|
return res
|
|
}
|