Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

2.2 KB
Raw Blame
import { escape } from 'es-toolkit/string'

export const sleep = (ms: number) => {
  return new Promise((resolve) => setTimeout(resolve, ms))
}

export async function asyncRunSafe<T>(fn: Promise<T>): Promise<[Error] | [null, T]> {
  try {
    return [null, await fn]
  } catch (e: any) {
    return [e || new Error('unknown error')]
  }
}

export const getTextWidthWithCanvas = (text: string, font?: string) => {
  const canvas = document.createElement('canvas')
  const ctx = canvas.getContext('2d')
  if (ctx) {
    ctx.font =
      font ??
      '12px Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"'
    return Number(ctx.measureText(text).width.toFixed(2))
  }
  return 0
}

export const getPurifyHref = (href: string) => {
  if (!href) return ''

  return escape(href)
}

export async function fetchWithRetry<T>(fn: Promise<T>, retries = 3): Promise<[Error] | [null, T]> {
  const [error, res] = await asyncRunSafe(fn)
  if (error) {
    if (retries > 0) {
      const res = await fetchWithRetry(fn, retries - 1)
      return res
    } else {
      if (error instanceof Error) return [error]
      return [new Error('unknown error')]
    }
  } else {
    return [null, res]
  }
}

export const correctModelProvider = (provider: string) => {
  if (!provider) return ''

  if (provider.includes('/')) return provider

  if (['google'].includes(provider)) return 'langgenius/gemini/google'

  return `langgenius/${provider}/${provider}`
}

export const correctToolProvider = (provider: string, toolInCollectionList?: boolean) => {
  if (!provider) return ''

  if (toolInCollectionList) return provider

  if (provider.includes('/')) return provider

  if (['stepfun', 'jina', 'siliconflow', 'gitee_ai'].includes(provider))
    return `langgenius/${provider}_tool/${provider}`

  return `langgenius/${provider}/${provider}`
}

export const canFindTool = (providerId: string, oldToolId?: string) => {
  return (
    providerId === oldToolId ||
    providerId === `langgenius/${oldToolId}/${oldToolId}` ||
    providerId === `langgenius/${oldToolId}_tool/${oldToolId}`
  )
}