wwf
8 天以前 938c3e5a587ce950a94964ea509b9e7f8834dfae
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
import { escape } from 'lodash-es'
 
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}`
}
 
export const removeSpecificQueryParam = (key: string | string[]) => {
  const url = new URL(window.location.href)
  if (Array.isArray(key))
    key.forEach(k => url.searchParams.delete(k))
  else
    url.searchParams.delete(key)
  window.history.replaceState(null, '', url.toString())
}