您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. import { InputVarType } from '@/app/components/workflow/types'
  2. import { AgentStrategy } from '@/types/app'
  3. import { PromptRole } from '@/models/debug'
  4. export let apiPrefix = ''
  5. export let webPrefix = ''
  6. export let publicApiPrefix = ''
  7. export let publicWebPrefix = ''
  8. export let marketplaceApiPrefix = ''
  9. export let marketplaceUrlPrefix = ''
  10. // NEXT_PUBLIC_API_PREFIX=/console/api NEXT_PUBLIC_PUBLIC_API_PREFIX=/api npm run start
  11. if (
  12. process.env.NEXT_PUBLIC_API_PREFIX
  13. && process.env.NEXT_PUBLIC_WEB_PREFIX
  14. && process.env.NEXT_PUBLIC_PUBLIC_API_PREFIX
  15. && process.env.NEXT_PUBLIC_PUBLIC_WEB_PREFIX
  16. ) {
  17. apiPrefix = process.env.NEXT_PUBLIC_API_PREFIX
  18. webPrefix = process.env.NEXT_PUBLIC_WEB_PREFIX
  19. publicApiPrefix = process.env.NEXT_PUBLIC_PUBLIC_API_PREFIX
  20. publicWebPrefix = process.env.NEXT_PUBLIC_PUBLIC_WEB_PREFIX
  21. }
  22. else if (
  23. globalThis.document?.body?.getAttribute('data-api-prefix')
  24. && globalThis.document?.body?.getAttribute('data-pubic-api-prefix')
  25. ) {
  26. // Not build can not get env from process.env.NEXT_PUBLIC_ in browser https://nextjs.org/docs/basic-features/environment-variables#exposing-environment-variables-to-the-browser
  27. apiPrefix = globalThis.document.body.getAttribute('data-api-prefix') as string
  28. webPrefix = (globalThis.document.body.getAttribute('data-web-prefix') as string || globalThis.location.origin)
  29. publicApiPrefix = globalThis.document.body.getAttribute('data-pubic-api-prefix') as string
  30. publicWebPrefix = (globalThis.document.body.getAttribute('data-pubic-web-prefix') as string || globalThis.location.origin)
  31. }
  32. else {
  33. // const domainParts = globalThis.location?.host?.split('.');
  34. // in production env, the host is dify.app . In other env, the host is [dev].dify.app
  35. // const env = domainParts.length === 2 ? 'ai' : domainParts?.[0];
  36. apiPrefix = 'http://localhost:5001/console/api'
  37. webPrefix = 'http://localhost:3000'
  38. publicApiPrefix = 'http://localhost:5001/api' // avoid browser private mode api cross origin
  39. publicWebPrefix = 'http://localhost:3000'
  40. marketplaceApiPrefix = 'http://localhost:5002/api'
  41. }
  42. if (process.env.NEXT_PUBLIC_MARKETPLACE_API_PREFIX && process.env.NEXT_PUBLIC_MARKETPLACE_URL_PREFIX) {
  43. marketplaceApiPrefix = process.env.NEXT_PUBLIC_MARKETPLACE_API_PREFIX
  44. marketplaceUrlPrefix = process.env.NEXT_PUBLIC_MARKETPLACE_URL_PREFIX
  45. }
  46. else {
  47. marketplaceApiPrefix = globalThis.document?.body?.getAttribute('data-marketplace-api-prefix') || ''
  48. marketplaceUrlPrefix = globalThis.document?.body?.getAttribute('data-marketplace-url-prefix') || ''
  49. }
  50. export const API_PREFIX: string = apiPrefix
  51. export const WEB_PREFIX: string = webPrefix
  52. export const PUBLIC_API_PREFIX: string = publicApiPrefix
  53. export const PUBLIC_WEB_PREFIX: string = publicWebPrefix
  54. export const MARKETPLACE_API_PREFIX: string = marketplaceApiPrefix
  55. export const MARKETPLACE_URL_PREFIX: string = marketplaceUrlPrefix
  56. const EDITION = process.env.NEXT_PUBLIC_EDITION || globalThis.document?.body?.getAttribute('data-public-edition') || 'SELF_HOSTED'
  57. export const IS_CE_EDITION = EDITION === 'SELF_HOSTED'
  58. export const IS_CLOUD_EDITION = EDITION === 'CLOUD'
  59. export const SUPPORT_MAIL_LOGIN = !!(process.env.NEXT_PUBLIC_SUPPORT_MAIL_LOGIN || globalThis.document?.body?.getAttribute('data-public-support-mail-login'))
  60. export const TONE_LIST = [
  61. {
  62. id: 1,
  63. name: 'Creative',
  64. config: {
  65. temperature: 0.8,
  66. top_p: 0.9,
  67. presence_penalty: 0.1,
  68. frequency_penalty: 0.1,
  69. },
  70. },
  71. {
  72. id: 2,
  73. name: 'Balanced',
  74. config: {
  75. temperature: 0.5,
  76. top_p: 0.85,
  77. presence_penalty: 0.2,
  78. frequency_penalty: 0.3,
  79. },
  80. },
  81. {
  82. id: 3,
  83. name: 'Precise',
  84. config: {
  85. temperature: 0.2,
  86. top_p: 0.75,
  87. presence_penalty: 0.5,
  88. frequency_penalty: 0.5,
  89. },
  90. },
  91. {
  92. id: 4,
  93. name: 'Custom',
  94. },
  95. ]
  96. export const DEFAULT_CHAT_PROMPT_CONFIG = {
  97. prompt: [
  98. {
  99. role: PromptRole.system,
  100. text: '',
  101. },
  102. ],
  103. }
  104. export const DEFAULT_COMPLETION_PROMPT_CONFIG = {
  105. prompt: {
  106. text: '',
  107. },
  108. conversation_histories_role: {
  109. user_prefix: '',
  110. assistant_prefix: '',
  111. },
  112. }
  113. export const getMaxToken = (modelId: string) => {
  114. return (modelId === 'gpt-4' || modelId === 'gpt-3.5-turbo-16k') ? 8000 : 4000
  115. }
  116. export const LOCALE_COOKIE_NAME = 'locale'
  117. export const DEFAULT_VALUE_MAX_LEN = 48
  118. export const DEFAULT_PARAGRAPH_VALUE_MAX_LEN = 1000
  119. export const zhRegex = /^[\u4E00-\u9FA5]$/m
  120. export const emojiRegex = /^[\uD800-\uDBFF][\uDC00-\uDFFF]$/m
  121. export const emailRegex = /^[\w.!#$%&'*+\-/=?^{|}~]+@([\w-]+\.)+[\w-]{2,}$/m
  122. const MAX_ZN_VAR_NAME_LENGTH = 8
  123. const MAX_EN_VAR_VALUE_LENGTH = 30
  124. export const getMaxVarNameLength = (value: string) => {
  125. if (zhRegex.test(value))
  126. return MAX_ZN_VAR_NAME_LENGTH
  127. return MAX_EN_VAR_VALUE_LENGTH
  128. }
  129. export const MAX_VAR_KEY_LENGTH = 30
  130. export const MAX_PROMPT_MESSAGE_LENGTH = 10
  131. export const VAR_ITEM_TEMPLATE = {
  132. key: '',
  133. name: '',
  134. type: 'string',
  135. max_length: DEFAULT_VALUE_MAX_LEN,
  136. required: true,
  137. }
  138. export const VAR_ITEM_TEMPLATE_IN_WORKFLOW = {
  139. variable: '',
  140. label: '',
  141. type: InputVarType.textInput,
  142. max_length: DEFAULT_VALUE_MAX_LEN,
  143. required: true,
  144. options: [],
  145. }
  146. export const appDefaultIconBackground = '#D5F5F6'
  147. export const NEED_REFRESH_APP_LIST_KEY = 'needRefreshAppList'
  148. export const DATASET_DEFAULT = {
  149. top_k: 4,
  150. score_threshold: 0.8,
  151. }
  152. export const APP_PAGE_LIMIT = 10
  153. export const ANNOTATION_DEFAULT = {
  154. score_threshold: 0.9,
  155. }
  156. export let maxToolsNum = 10
  157. if (process.env.NEXT_PUBLIC_MAX_TOOLS_NUM && process.env.NEXT_PUBLIC_MAX_TOOLS_NUM !== '')
  158. maxToolsNum = Number.parseInt(process.env.NEXT_PUBLIC_MAX_TOOLS_NUM)
  159. else if (globalThis.document?.body?.getAttribute('data-public-max-tools-num') && globalThis.document.body.getAttribute('data-public-max-tools-num') !== '')
  160. maxToolsNum = Number.parseInt(globalThis.document.body.getAttribute('data-public-max-tools-num') as string)
  161. export const MAX_TOOLS_NUM = maxToolsNum
  162. export const DEFAULT_AGENT_SETTING = {
  163. enabled: false,
  164. max_iteration: 5,
  165. strategy: AgentStrategy.functionCall,
  166. tools: [],
  167. }
  168. export const DEFAULT_AGENT_PROMPT = {
  169. chat: `Respond to the human as helpfully and accurately as possible.
  170. {{instruction}}
  171. You have access to the following tools:
  172. {{tools}}
  173. Use a json blob to specify a tool by providing an {{TOOL_NAME_KEY}} key (tool name) and an {{ACTION_INPUT_KEY}} key (tool input).
  174. Valid "{{TOOL_NAME_KEY}}" values: "Final Answer" or {{tool_names}}
  175. Provide only ONE action per $JSON_BLOB, as shown:
  176. \`\`\`
  177. {
  178. "{{TOOL_NAME_KEY}}": $TOOL_NAME,
  179. "{{ACTION_INPUT_KEY}}": $ACTION_INPUT
  180. }
  181. \`\`\`
  182. Follow this format:
  183. Question: input question to answer
  184. Thought: consider previous and subsequent steps
  185. Action:
  186. \`\`\`
  187. $JSON_BLOB
  188. \`\`\`
  189. Observation: action result
  190. ... (repeat Thought/Action/Observation N times)
  191. Thought: I know what to respond
  192. Action:
  193. \`\`\`
  194. {
  195. "{{TOOL_NAME_KEY}}": "Final Answer",
  196. "{{ACTION_INPUT_KEY}}": "Final response to human"
  197. }
  198. \`\`\`
  199. Begin! Reminder to ALWAYS respond with a valid json blob of a single action. Use tools if necessary. Respond directly if appropriate. Format is Action:\`\`\`$JSON_BLOB\`\`\`then Observation:.`,
  200. completion: `
  201. Respond to the human as helpfully and accurately as possible.
  202. {{instruction}}
  203. You have access to the following tools:
  204. {{tools}}
  205. Use a json blob to specify a tool by providing an {{TOOL_NAME_KEY}} key (tool name) and an {{ACTION_INPUT_KEY}} key (tool input).
  206. Valid "{{TOOL_NAME_KEY}}" values: "Final Answer" or {{tool_names}}
  207. Provide only ONE action per $JSON_BLOB, as shown:
  208. \`\`\`
  209. {{{{
  210. "{{TOOL_NAME_KEY}}": $TOOL_NAME,
  211. "{{ACTION_INPUT_KEY}}": $ACTION_INPUT
  212. }}}}
  213. \`\`\`
  214. Follow this format:
  215. Question: input question to answer
  216. Thought: consider previous and subsequent steps
  217. Action:
  218. \`\`\`
  219. $JSON_BLOB
  220. \`\`\`
  221. Observation: action result
  222. ... (repeat Thought/Action/Observation N times)
  223. Thought: I know what to respond
  224. Action:
  225. \`\`\`
  226. {{{{
  227. "{{TOOL_NAME_KEY}}": "Final Answer",
  228. "{{ACTION_INPUT_KEY}}": "Final response to human"
  229. }}}}
  230. \`\`\`
  231. Begin! Reminder to ALWAYS respond with a valid json blob of a single action. Use tools if necessary. Respond directly if appropriate. Format is Action:\`\`\`$JSON_BLOB\`\`\`then Observation:.
  232. Question: {{query}}
  233. Thought: {{agent_scratchpad}}
  234. `,
  235. }
  236. export const VAR_REGEX = /\{\{(#[a-zA-Z0-9_-]{1,50}(\.[a-zA-Z_]\w{0,29}){1,10}#)\}\}/gi
  237. export const VAR_REGEX_TEXT = /\{\{#([a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)*)#\}\}/gi
  238. export const resetReg = () => VAR_REGEX.lastIndex = 0
  239. export let textGenerationTimeoutMs = 60000
  240. if (process.env.NEXT_PUBLIC_TEXT_GENERATION_TIMEOUT_MS && process.env.NEXT_PUBLIC_TEXT_GENERATION_TIMEOUT_MS !== '')
  241. textGenerationTimeoutMs = Number.parseInt(process.env.NEXT_PUBLIC_TEXT_GENERATION_TIMEOUT_MS)
  242. else if (globalThis.document?.body?.getAttribute('data-public-text-generation-timeout-ms') && globalThis.document.body.getAttribute('data-public-text-generation-timeout-ms') !== '')
  243. textGenerationTimeoutMs = Number.parseInt(globalThis.document.body.getAttribute('data-public-text-generation-timeout-ms') as string)
  244. export const TEXT_GENERATION_TIMEOUT_MS = textGenerationTimeoutMs
  245. export const DISABLE_UPLOAD_IMAGE_AS_ICON = process.env.NEXT_PUBLIC_DISABLE_UPLOAD_IMAGE_AS_ICON === 'true'
  246. export const GITHUB_ACCESS_TOKEN = process.env.NEXT_PUBLIC_GITHUB_ACCESS_TOKEN || ''
  247. export const SUPPORT_INSTALL_LOCAL_FILE_EXTENSIONS = '.difypkg,.difybndl'
  248. export const FULL_DOC_PREVIEW_LENGTH = 50
  249. export const JSON_SCHEMA_MAX_DEPTH = 10
  250. let loopNodeMaxCount = 100
  251. if (process.env.NEXT_PUBLIC_LOOP_NODE_MAX_COUNT && process.env.NEXT_PUBLIC_LOOP_NODE_MAX_COUNT !== '')
  252. loopNodeMaxCount = Number.parseInt(process.env.NEXT_PUBLIC_LOOP_NODE_MAX_COUNT)
  253. else if (globalThis.document?.body?.getAttribute('data-public-loop-node-max-count') && globalThis.document.body.getAttribute('data-public-loop-node-max-count') !== '')
  254. loopNodeMaxCount = Number.parseInt(globalThis.document.body.getAttribute('data-public-loop-node-max-count') as string)
  255. export const LOOP_NODE_MAX_COUNT = loopNodeMaxCount
  256. let maxIterationsNum = 5
  257. if (process.env.NEXT_PUBLIC_MAX_ITERATIONS_NUM && process.env.NEXT_PUBLIC_MAX_ITERATIONS_NUM !== '')
  258. maxIterationsNum = Number.parseInt(process.env.NEXT_PUBLIC_MAX_ITERATIONS_NUM)
  259. else if (globalThis.document?.body?.getAttribute('data-public-max-iterations-num') && globalThis.document.body.getAttribute('data-public-max-iterations-num') !== '')
  260. maxIterationsNum = Number.parseInt(globalThis.document.body.getAttribute('data-public-max-iterations-num') as string)
  261. export const MAX_ITERATIONS_NUM = maxIterationsNum
  262. export const ENABLE_WEBSITE_JINAREADER = process.env.NEXT_PUBLIC_ENABLE_WEBSITE_JINAREADER !== undefined
  263. ? process.env.NEXT_PUBLIC_ENABLE_WEBSITE_JINAREADER === 'true'
  264. : globalThis.document?.body?.getAttribute('data-public-enable-website-jinareader') === 'true' || true
  265. export const ENABLE_WEBSITE_FIRECRAWL = process.env.NEXT_PUBLIC_ENABLE_WEBSITE_FIRECRAWL !== undefined
  266. ? process.env.NEXT_PUBLIC_ENABLE_WEBSITE_FIRECRAWL === 'true'
  267. : globalThis.document?.body?.getAttribute('data-public-enable-website-firecrawl') === 'true' || true
  268. export const ENABLE_WEBSITE_WATERCRAWL = process.env.NEXT_PUBLIC_ENABLE_WEBSITE_WATERCRAWL !== undefined
  269. ? process.env.NEXT_PUBLIC_ENABLE_WEBSITE_WATERCRAWL === 'true'
  270. : globalThis.document?.body?.getAttribute('data-public-enable-website-watercrawl') === 'true' || true