Files
dify/web/utils/index.ts
yyh 2aaaa4bd34
Some checks failed
autofix.ci / autofix (push) Has been cancelled
Build and Push API & Web / build (api, DIFY_API_IMAGE_NAME, linux/amd64, build-api-amd64) (push) Has been cancelled
Build and Push API & Web / build (api, DIFY_API_IMAGE_NAME, linux/arm64, build-api-arm64) (push) Has been cancelled
Build and Push API & Web / build (web, DIFY_WEB_IMAGE_NAME, linux/amd64, build-web-amd64) (push) Has been cancelled
Build and Push API & Web / build (web, DIFY_WEB_IMAGE_NAME, linux/arm64, build-web-arm64) (push) Has been cancelled
Build and Push API & Web / create-manifest (api, DIFY_API_IMAGE_NAME, merge-api-images) (push) Has been cancelled
Build and Push API & Web / create-manifest (web, DIFY_WEB_IMAGE_NAME, merge-web-images) (push) Has been cancelled
Main CI Pipeline / Check Changed Files (push) Has been cancelled
Main CI Pipeline / API Tests (push) Has been cancelled
Main CI Pipeline / Web Tests (push) Has been cancelled
Main CI Pipeline / Style Check (push) Has been cancelled
Main CI Pipeline / VDB Tests (push) Has been cancelled
Main CI Pipeline / DB Migration Test (push) Has been cancelled
feat(web): migrate from es-toolkit/compat to native es-toolkit (#30244) (#30246)
2025-12-31 11:13:22 +08:00

93 lines
2.5 KiB
TypeScript

import { escape } from 'es-toolkit/string'
export const sleep = (ms: number) => {
return new Promise(resolve => setTimeout(resolve, ms))
}
export async function asyncRunSafe<T = any>(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
}
const chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_'
export function randomString(length: number) {
let result = ''
for (let i = length; i > 0; --i) result += chars[Math.floor(Math.random() * chars.length)]
return result
}
export const getPurifyHref = (href: string) => {
if (!href)
return ''
return escape(href)
}
export async function fetchWithRetry<T = any>(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}`
}