forked from agentic-review-benchmarks/dify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvar.ts
More file actions
154 lines (133 loc) · 4.24 KB
/
var.ts
File metadata and controls
154 lines (133 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import type { InputVar } from '@/app/components/workflow/types'
import type { I18nKeysByPrefix } from '@/types/i18n'
import {
CONTEXT_PLACEHOLDER_TEXT,
HISTORY_PLACEHOLDER_TEXT,
PRE_PROMPT_PLACEHOLDER_TEXT,
QUERY_PLACEHOLDER_TEXT,
} from '@/app/components/base/prompt-editor/constants'
import { InputVarType } from '@/app/components/workflow/types'
import { getMaxVarNameLength, MARKETPLACE_URL_PREFIX, MAX_VAR_KEY_LENGTH, VAR_ITEM_TEMPLATE, VAR_ITEM_TEMPLATE_IN_WORKFLOW } from '@/config'
const otherAllowedRegex = /^\w+$/
export const getNewVar = (key: string, type: string) => {
const { ...rest } = VAR_ITEM_TEMPLATE
if (type !== 'string') {
return {
...rest,
type: type || 'string',
key,
name: key.slice(0, getMaxVarNameLength(key)),
}
}
return {
...VAR_ITEM_TEMPLATE,
type: type || 'string',
key,
name: key.slice(0, getMaxVarNameLength(key)),
}
}
export const getNewVarInWorkflow = (key: string, type = InputVarType.textInput): InputVar => {
const { ...rest } = VAR_ITEM_TEMPLATE_IN_WORKFLOW
if (type !== InputVarType.textInput) {
return {
...rest,
type,
variable: key,
label: key.slice(0, getMaxVarNameLength(key)),
}
}
return {
...VAR_ITEM_TEMPLATE_IN_WORKFLOW,
type,
variable: key,
label: key.slice(0, getMaxVarNameLength(key)),
placeholder: '',
default: '',
hint: '',
}
}
export type VarKeyErrorMessageKey = I18nKeysByPrefix<'appDebug', 'varKeyError.'>
export const checkKey = (key: string, canBeEmpty?: boolean, _keys?: string[]): true | VarKeyErrorMessageKey => {
if (key.length === 0 && !canBeEmpty)
return 'canNoBeEmpty'
if (canBeEmpty && key === '')
return true
if (key.length > MAX_VAR_KEY_LENGTH)
return 'tooLong'
if (otherAllowedRegex.test(key)) {
if (/\d/.test(key[0]))
return 'notStartWithNumber'
return true
}
return 'notValid'
}
type CheckKeysResult
= | { isValid: true, errorKey: '', errorMessageKey: '' }
| { isValid: false, errorKey: string, errorMessageKey: VarKeyErrorMessageKey }
export const checkKeys = (keys: string[], canBeEmpty?: boolean): CheckKeysResult => {
let isValid = true
let errorKey = ''
let errorMessageKey: VarKeyErrorMessageKey | '' = ''
keys.forEach((key) => {
if (!isValid)
return
const res = checkKey(key, canBeEmpty)
if (res !== true) {
isValid = false
errorKey = key
errorMessageKey = res
}
})
return { isValid, errorKey, errorMessageKey } as CheckKeysResult
}
export const hasDuplicateStr = (strArr: string[]) => {
const strObj: Record<string, number> = {}
strArr.forEach((str) => {
if (strObj[str])
strObj[str] += 1
else
strObj[str] = 1
})
return !!Object.keys(strObj).find(key => strObj[key] > 1)
}
const varRegex = /\{\{([a-z_]\w*)\}\}/gi
export const getVars = (value: string) => {
if (!value)
return []
const keys = value.match(varRegex)?.filter((item) => {
return ![CONTEXT_PLACEHOLDER_TEXT, HISTORY_PLACEHOLDER_TEXT, QUERY_PLACEHOLDER_TEXT, PRE_PROMPT_PLACEHOLDER_TEXT].includes(item)
}).map((item) => {
return item.replace('{{', '').replace('}}', '')
}).filter(key => key.length <= MAX_VAR_KEY_LENGTH) || []
const keyObj: Record<string, boolean> = {}
// remove duplicate keys
const res: string[] = []
keys.forEach((key) => {
if (keyObj[key])
return
keyObj[key] = true
res.push(key)
})
return res
}
// Set the value of basePath
// example: /dify
export const basePath = process.env.NEXT_PUBLIC_BASE_PATH || ''
export function getMarketplaceUrl(path: string, params?: Record<string, string | undefined>) {
const searchParams = new URLSearchParams({ source: encodeURIComponent(window.location.origin) })
if (params) {
Object.keys(params).forEach((key) => {
const value = params[key]
if (value !== undefined && value !== null)
searchParams.append(key, value)
})
}
return `${MARKETPLACE_URL_PREFIX}${path}?${searchParams.toString()}`
}
export const replaceSpaceWithUnderscoreInVarNameInput = (input: HTMLInputElement) => {
const start = input.selectionStart
const end = input.selectionEnd
input.value = input.value.replaceAll(' ', '_')
if (start !== null && end !== null)
input.setSelectionRange(start, end)
}