index.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 publicApiPrefix = ''
  6. export let marketplaceApiPrefix = ''
  7. export let marketplaceUrlPrefix = ''
  8. // NEXT_PUBLIC_API_PREFIX=/console/api NEXT_PUBLIC_PUBLIC_API_PREFIX=/api npm run start
  9. if (process.env.NEXT_PUBLIC_API_PREFIX && process.env.NEXT_PUBLIC_PUBLIC_API_PREFIX) {
  10. apiPrefix = process.env.NEXT_PUBLIC_API_PREFIX
  11. publicApiPrefix = process.env.NEXT_PUBLIC_PUBLIC_API_PREFIX
  12. }
  13. else if (
  14. globalThis.document?.body?.getAttribute('data-api-prefix')
  15. && globalThis.document?.body?.getAttribute('data-pubic-api-prefix')
  16. ) {
  17. // 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
  18. apiPrefix = globalThis.document.body.getAttribute('data-api-prefix') as string
  19. publicApiPrefix = globalThis.document.body.getAttribute('data-pubic-api-prefix') as string
  20. }
  21. else {
  22. // const domainParts = globalThis.location?.host?.split('.');
  23. // in production env, the host is dify.app . In other env, the host is [dev].dify.app
  24. // const env = domainParts.length === 2 ? 'ai' : domainParts?.[0];
  25. apiPrefix = 'http://localhost:5001/console/api'
  26. publicApiPrefix = 'http://localhost:5001/api' // avoid browser private mode api cross origin
  27. marketplaceApiPrefix = 'http://localhost:5002/api'
  28. }
  29. if (process.env.NEXT_PUBLIC_MARKETPLACE_API_PREFIX && process.env.NEXT_PUBLIC_MARKETPLACE_URL_PREFIX) {
  30. marketplaceApiPrefix = process.env.NEXT_PUBLIC_MARKETPLACE_API_PREFIX
  31. marketplaceUrlPrefix = process.env.NEXT_PUBLIC_MARKETPLACE_URL_PREFIX
  32. }
  33. else {
  34. marketplaceApiPrefix = globalThis.document?.body?.getAttribute('data-marketplace-api-prefix') || ''
  35. marketplaceUrlPrefix = globalThis.document?.body?.getAttribute('data-marketplace-url-prefix') || ''
  36. }
  37. export const API_PREFIX: string = apiPrefix
  38. export const PUBLIC_API_PREFIX: string = publicApiPrefix
  39. export const MARKETPLACE_API_PREFIX: string = marketplaceApiPrefix
  40. export const MARKETPLACE_URL_PREFIX: string = marketplaceUrlPrefix
  41. const EDITION = process.env.NEXT_PUBLIC_EDITION || globalThis.document?.body?.getAttribute('data-public-edition') || 'SELF_HOSTED'
  42. export const IS_CE_EDITION = EDITION === 'SELF_HOSTED'
  43. export const SUPPORT_MAIL_LOGIN = !!(process.env.NEXT_PUBLIC_SUPPORT_MAIL_LOGIN || globalThis.document?.body?.getAttribute('data-public-support-mail-login'))
  44. export const TONE_LIST = [
  45. {
  46. id: 1,
  47. name: 'Creative',
  48. config: {
  49. temperature: 0.8,
  50. top_p: 0.9,
  51. presence_penalty: 0.1,
  52. frequency_penalty: 0.1,
  53. },
  54. },
  55. {
  56. id: 2,
  57. name: 'Balanced',
  58. config: {
  59. temperature: 0.5,
  60. top_p: 0.85,
  61. presence_penalty: 0.2,
  62. frequency_penalty: 0.3,
  63. },
  64. },
  65. {
  66. id: 3,
  67. name: 'Precise',
  68. config: {
  69. temperature: 0.2,
  70. top_p: 0.75,
  71. presence_penalty: 0.5,
  72. frequency_penalty: 0.5,
  73. },
  74. },
  75. {
  76. id: 4,
  77. name: 'Custom',
  78. },
  79. ]
  80. export const DEFAULT_CHAT_PROMPT_CONFIG = {
  81. prompt: [
  82. {
  83. role: PromptRole.system,
  84. text: '',
  85. },
  86. ],
  87. }
  88. export const DEFAULT_COMPLETION_PROMPT_CONFIG = {
  89. prompt: {
  90. text: '',
  91. },
  92. conversation_histories_role: {
  93. user_prefix: '',
  94. assistant_prefix: '',
  95. },
  96. }
  97. export const getMaxToken = (modelId: string) => {
  98. return (modelId === 'gpt-4' || modelId === 'gpt-3.5-turbo-16k') ? 8000 : 4000
  99. }
  100. export const LOCALE_COOKIE_NAME = 'locale'
  101. export const DEFAULT_VALUE_MAX_LEN = 48
  102. export const DEFAULT_PARAGRAPH_VALUE_MAX_LEN = 1000
  103. export const zhRegex = /^[\u4E00-\u9FA5]$/m
  104. export const emojiRegex = /^[\uD800-\uDBFF][\uDC00-\uDFFF]$/m
  105. export const emailRegex = /^[\w.!#$%&'*+\-/=?^{|}~]+@([\w-]+\.)+[\w-]{2,}$/m
  106. const MAX_ZN_VAR_NAME_LENGTH = 8
  107. const MAX_EN_VAR_VALUE_LENGTH = 30
  108. export const getMaxVarNameLength = (value: string) => {
  109. if (zhRegex.test(value))
  110. return MAX_ZN_VAR_NAME_LENGTH
  111. return MAX_EN_VAR_VALUE_LENGTH
  112. }
  113. export const MAX_VAR_KEY_LENGTH = 30
  114. export const MAX_PROMPT_MESSAGE_LENGTH = 10
  115. export const VAR_ITEM_TEMPLATE = {
  116. key: '',
  117. name: '',
  118. type: 'string',
  119. max_length: DEFAULT_VALUE_MAX_LEN,
  120. required: true,
  121. }
  122. export const VAR_ITEM_TEMPLATE_IN_WORKFLOW = {
  123. variable: '',
  124. label: '',
  125. type: InputVarType.textInput,
  126. max_length: DEFAULT_VALUE_MAX_LEN,
  127. required: true,
  128. options: [],
  129. }
  130. export const appDefaultIconBackground = '#D5F5F6'
  131. export const NEED_REFRESH_APP_LIST_KEY = 'needRefreshAppList'
  132. export const DATASET_DEFAULT = {
  133. top_k: 4,
  134. score_threshold: 0.8,
  135. }
  136. export const APP_PAGE_LIMIT = 10
  137. export const ANNOTATION_DEFAULT = {
  138. score_threshold: 0.9,
  139. }
  140. export const MAX_TOOLS_NUM = 10
  141. export const DEFAULT_AGENT_SETTING = {
  142. enabled: false,
  143. max_iteration: 5,
  144. strategy: AgentStrategy.functionCall,
  145. tools: [],
  146. }
  147. export const DEFAULT_AGENT_PROMPT = {
  148. chat: `Respond to the human as helpfully and accurately as possible.
  149. {{instruction}}
  150. You have access to the following tools:
  151. {{tools}}
  152. 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).
  153. Valid "{{TOOL_NAME_KEY}}" values: "Final Answer" or {{tool_names}}
  154. Provide only ONE action per $JSON_BLOB, as shown:
  155. \`\`\`
  156. {
  157. "{{TOOL_NAME_KEY}}": $TOOL_NAME,
  158. "{{ACTION_INPUT_KEY}}": $ACTION_INPUT
  159. }
  160. \`\`\`
  161. Follow this format:
  162. Question: input question to answer
  163. Thought: consider previous and subsequent steps
  164. Action:
  165. \`\`\`
  166. $JSON_BLOB
  167. \`\`\`
  168. Observation: action result
  169. ... (repeat Thought/Action/Observation N times)
  170. Thought: I know what to respond
  171. Action:
  172. \`\`\`
  173. {
  174. "{{TOOL_NAME_KEY}}": "Final Answer",
  175. "{{ACTION_INPUT_KEY}}": "Final response to human"
  176. }
  177. \`\`\`
  178. 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:.`,
  179. completion: `
  180. Respond to the human as helpfully and accurately as possible.
  181. {{instruction}}
  182. You have access to the following tools:
  183. {{tools}}
  184. 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).
  185. Valid "{{TOOL_NAME_KEY}}" values: "Final Answer" or {{tool_names}}
  186. Provide only ONE action per $JSON_BLOB, as shown:
  187. \`\`\`
  188. {{{{
  189. "{{TOOL_NAME_KEY}}": $TOOL_NAME,
  190. "{{ACTION_INPUT_KEY}}": $ACTION_INPUT
  191. }}}}
  192. \`\`\`
  193. Follow this format:
  194. Question: input question to answer
  195. Thought: consider previous and subsequent steps
  196. Action:
  197. \`\`\`
  198. $JSON_BLOB
  199. \`\`\`
  200. Observation: action result
  201. ... (repeat Thought/Action/Observation N times)
  202. Thought: I know what to respond
  203. Action:
  204. \`\`\`
  205. {{{{
  206. "{{TOOL_NAME_KEY}}": "Final Answer",
  207. "{{ACTION_INPUT_KEY}}": "Final response to human"
  208. }}}}
  209. \`\`\`
  210. 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:.
  211. Question: {{query}}
  212. Thought: {{agent_scratchpad}}
  213. `,
  214. }
  215. export const VAR_REGEX = /\{\{(#[a-zA-Z0-9_-]{1,50}(\.[a-zA-Z_][a-zA-Z0-9_]{0,29}){1,10}#)\}\}/gi
  216. export const resetReg = () => VAR_REGEX.lastIndex = 0
  217. export let textGenerationTimeoutMs = 60000
  218. if (process.env.NEXT_PUBLIC_TEXT_GENERATION_TIMEOUT_MS && process.env.NEXT_PUBLIC_TEXT_GENERATION_TIMEOUT_MS !== '')
  219. textGenerationTimeoutMs = Number.parseInt(process.env.NEXT_PUBLIC_TEXT_GENERATION_TIMEOUT_MS)
  220. else if (globalThis.document?.body?.getAttribute('data-public-text-generation-timeout-ms') && globalThis.document.body.getAttribute('data-public-text-generation-timeout-ms') !== '')
  221. textGenerationTimeoutMs = Number.parseInt(globalThis.document.body.getAttribute('data-public-text-generation-timeout-ms') as string)
  222. export const TEXT_GENERATION_TIMEOUT_MS = textGenerationTimeoutMs
  223. export const DISABLE_UPLOAD_IMAGE_AS_ICON = process.env.NEXT_PUBLIC_DISABLE_UPLOAD_IMAGE_AS_ICON === 'true'
  224. export const GITHUB_ACCESS_TOKEN = process.env.NEXT_PUBLIC_GITHUB_ACCESS_TOKEN || ''
  225. export const SUPPORT_INSTALL_LOCAL_FILE_EXTENSIONS = '.difypkg,.difybndl'
  226. export const FULL_DOC_PREVIEW_LENGTH = 50