Initial commit

This commit is contained in:
2025-10-14 14:17:21 +08:00
commit ac715a8b88
35011 changed files with 3834178 additions and 0 deletions

View File

@@ -0,0 +1,15 @@
export const getRedirection = (
isCurrentWorkspaceEditor: boolean,
app: any,
redirectionFunc: (href: string) => void,
) => {
if (!isCurrentWorkspaceEditor) {
redirectionFunc(`/app/${app.id}/overview`)
}
else {
if (app.mode === 'workflow' || app.mode === 'advanced-chat')
redirectionFunc(`/app/${app.id}/workflow`)
else
redirectionFunc(`/app/${app.id}/configuration`)
}
}

View File

@@ -0,0 +1,55 @@
import cn from './classnames'
describe('classnames', () => {
test('classnames libs feature', () => {
expect(cn('foo')).toBe('foo')
expect(cn('foo', 'bar')).toBe('foo bar')
expect(cn(['foo', 'bar'])).toBe('foo bar')
expect(cn(undefined)).toBe('')
expect(cn(null)).toBe('')
expect(cn(false)).toBe('')
expect(cn({
foo: true,
bar: false,
baz: true,
})).toBe('foo baz')
})
test('tailwind-merge', () => {
expect(cn('p-0')).toBe('p-0')
expect(cn('text-right text-center text-left')).toBe('text-left')
expect(cn('pl-4 p-8')).toBe('p-8')
expect(cn('m-[2px] m-[4px]')).toBe('m-[4px]')
expect(cn('m-1 m-[4px]')).toBe('m-[4px]')
expect(cn('overflow-x-auto hover:overflow-x-hidden overflow-x-scroll')).toBe(
'hover:overflow-x-hidden overflow-x-scroll',
)
expect(cn('h-10 h-min')).toBe('h-min')
expect(cn('bg-grey-5 bg-hotpink')).toBe('bg-hotpink')
expect(cn('hover:block hover:inline')).toBe('hover:inline')
expect(cn('font-medium !font-bold')).toBe('font-medium !font-bold')
expect(cn('!font-medium !font-bold')).toBe('!font-bold')
expect(cn('text-gray-100 text-primary-200')).toBe('text-primary-200')
expect(cn('text-some-unknown-color text-components-input-bg-disabled text-primary-200')).toBe('text-primary-200')
expect(cn('bg-some-unknown-color bg-components-input-bg-disabled bg-primary-200')).toBe('bg-primary-200')
expect(cn('border-t border-white/10')).toBe('border-t border-white/10')
expect(cn('border-t border-white')).toBe('border-t border-white')
expect(cn('text-3.5xl text-black')).toBe('text-3.5xl text-black')
})
test('classnames combined with tailwind-merge', () => {
expect(cn('text-right', {
'text-center': true,
})).toBe('text-center')
expect(cn('text-right', {
'text-center': false,
})).toBe('text-right')
})
})

View File

@@ -0,0 +1,8 @@
import { twMerge } from 'tailwind-merge'
import cn from 'classnames'
const classNames = (...cls: cn.ArgumentArray) => {
return twMerge(cn(cls))
}
export default classNames

View File

@@ -0,0 +1,35 @@
export async function writeTextToClipboard(text: string): Promise<void> {
if (navigator.clipboard && navigator.clipboard.writeText)
return navigator.clipboard.writeText(text)
return fallbackCopyTextToClipboard(text)
}
async function fallbackCopyTextToClipboard(text: string): Promise<void> {
const textArea = document.createElement('textarea')
textArea.value = text
textArea.style.position = 'fixed' // Avoid scrolling to bottom
document.body.appendChild(textArea)
textArea.focus()
textArea.select()
try {
const successful = document.execCommand('copy')
if (successful)
return Promise.resolve()
return Promise.reject(new Error('document.execCommand failed'))
}
catch (err) {
return Promise.reject(convertAnyToError(err))
}
finally {
document.body.removeChild(textArea)
}
}
function convertAnyToError(err: any): Error {
if (err instanceof Error)
return err
return new Error(`Caught: ${String(err)}`)
}

View File

@@ -0,0 +1,45 @@
import { type Context, type Provider, createContext, useContext } from 'react'
import * as selector from 'use-context-selector'
const createCreateCtxFunction = (
useContextImpl: typeof useContext,
createContextImpl: typeof createContext) => {
return function<T>({ name, defaultValue }: CreateCtxOptions<T> = {}): CreateCtxReturn<T> {
const emptySymbol = Symbol(`empty ${name}`)
// @ts-expect-error it's ok here
const context = createContextImpl<T>(defaultValue ?? emptySymbol)
const useContextValue = () => {
const ctx = useContextImpl(context)
if (ctx === emptySymbol)
throw new Error(`No ${name ?? 'related'} context found.`)
return ctx
}
const result = [context.Provider, useContextValue, context] as CreateCtxReturn<T>
result.context = context
result.provider = context.Provider
result.useContextValue = useContextValue
return result
}
}
type CreateCtxOptions<T> = {
defaultValue?: T
name?: string
}
type CreateCtxReturn<T> = [Provider<T>, () => T, Context<T>] & {
context: Context<T>
provider: Provider<T>
useContextValue: () => T
}
// example
// const [AppProvider, useApp, AppContext] = createCtx<AppContextValue>()
export const createCtx = createCreateCtxFunction(useContext, createContext)
export const createSelectorCtx = createCreateCtxFunction(
selector.useContext,
selector.createContext as typeof createContext,
)

View File

@@ -0,0 +1,11 @@
import { SearchIndex } from 'emoji-mart'
import type { Emoji } from '@emoji-mart/data'
export async function searchEmoji(value: string) {
const emojis: Emoji[] = await SearchIndex.search(value) || []
const results = emojis.map((emoji) => {
return emoji.skins[0].native
})
return results
}

View File

@@ -0,0 +1,61 @@
import { formatFileSize, formatNumber, formatTime } from './format'
describe('formatNumber', () => {
test('should correctly format integers', () => {
expect(formatNumber(1234567)).toBe('1,234,567')
})
test('should correctly format decimals', () => {
expect(formatNumber(1234567.89)).toBe('1,234,567.89')
})
test('should correctly handle string input', () => {
expect(formatNumber('1234567')).toBe('1,234,567')
})
test('should correctly handle zero', () => {
expect(formatNumber(0)).toBe(0)
})
test('should correctly handle negative numbers', () => {
expect(formatNumber(-1234567)).toBe('-1,234,567')
})
test('should correctly handle empty input', () => {
expect(formatNumber('')).toBe('')
})
})
describe('formatFileSize', () => {
test('should return the input if it is falsy', () => {
expect(formatFileSize(0)).toBe(0)
})
test('should format bytes correctly', () => {
expect(formatFileSize(500)).toBe('500.00B')
})
test('should format kilobytes correctly', () => {
expect(formatFileSize(1500)).toBe('1.46KB')
})
test('should format megabytes correctly', () => {
expect(formatFileSize(1500000)).toBe('1.43MB')
})
test('should format gigabytes correctly', () => {
expect(formatFileSize(1500000000)).toBe('1.40GB')
})
test('should format terabytes correctly', () => {
expect(formatFileSize(1500000000000)).toBe('1.36TB')
})
test('should format petabytes correctly', () => {
expect(formatFileSize(1500000000000000)).toBe('1.33PB')
})
})
describe('formatTime', () => {
test('should return the input if it is falsy', () => {
expect(formatTime(0)).toBe(0)
})
test('should format seconds correctly', () => {
expect(formatTime(30)).toBe('30.00 sec')
})
test('should format minutes correctly', () => {
expect(formatTime(90)).toBe('1.50 min')
})
test('should format hours correctly', () => {
expect(formatTime(3600)).toBe('1.00 h')
})
test('should handle large numbers', () => {
expect(formatTime(7200)).toBe('2.00 h')
})
})

View File

@@ -0,0 +1,36 @@
/*
* Formats a number with comma separators.
formatNumber(1234567) will return '1,234,567'
formatNumber(1234567.89) will return '1,234,567.89'
*/
export const formatNumber = (num: number | string) => {
if (!num)
return num
const parts = num.toString().split('.')
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',')
return parts.join('.')
}
export const formatFileSize = (num: number) => {
if (!num)
return num
const units = ['', 'K', 'M', 'G', 'T', 'P']
let index = 0
while (num >= 1024 && index < units.length) {
num = num / 1024
index++
}
return `${num.toFixed(2)}${units[index]}B`
}
export const formatTime = (num: number) => {
if (!num)
return num
const units = ['sec', 'min', 'h']
let index = 0
while (num >= 60 && index < units.length) {
num = num / 60
index++
}
return `${num.toFixed(2)} ${units[index]}`
}

View File

@@ -0,0 +1,57 @@
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]
}
}

View File

@@ -0,0 +1,167 @@
import type { UserInputFormItem } from '@/types/app'
import type { PromptVariable } from '@/models/debug'
export const userInputsFormToPromptVariables = (useInputs: UserInputFormItem[] | null, dataset_query_variable?: string) => {
if (!useInputs)
return []
const promptVariables: PromptVariable[] = []
useInputs.forEach((item: any) => {
const isParagraph = !!item.paragraph
const [type, content] = (() => {
if (isParagraph)
return ['paragraph', item.paragraph]
if (item['text-input'])
return ['string', item['text-input']]
if (item.number)
return ['number', item.number]
if (item.file)
return ['file', item.file]
if (item['file-list'])
return ['file-list', item['file-list']]
if (item.external_data_tool)
return [item.external_data_tool.type, item.external_data_tool]
return ['select', item.select || {}]
})()
const is_context_var = dataset_query_variable === content?.variable
if (type === 'string' || type === 'paragraph') {
promptVariables.push({
key: content.variable,
name: content.label,
required: content.required,
type,
max_length: content.max_length,
options: [],
is_context_var,
})
}
else if (type === 'number') {
promptVariables.push({
key: content.variable,
name: content.label,
required: content.required,
type,
options: [],
})
}
else if (type === 'select') {
promptVariables.push({
key: content.variable,
name: content.label,
required: content.required,
type: 'select',
options: content.options,
is_context_var,
})
}
else if (type === 'file') {
promptVariables.push({
key: content.variable,
name: content.label,
required: content.required,
type,
config: {
allowed_file_types: content.allowed_file_types,
allowed_file_extensions: content.allowed_file_extensions,
allowed_file_upload_methods: content.allowed_file_upload_methods,
number_limits: 1,
},
})
}
else if (type === 'file-list') {
promptVariables.push({
key: content.variable,
name: content.label,
required: content.required,
type,
config: {
allowed_file_types: content.allowed_file_types,
allowed_file_extensions: content.allowed_file_extensions,
allowed_file_upload_methods: content.allowed_file_upload_methods,
number_limits: content.max_length,
},
})
}
else {
promptVariables.push({
key: content.variable,
name: content.label,
required: content.required,
type: content.type,
enabled: content.enabled,
config: content.config,
icon: content.icon,
icon_background: content.icon_background,
is_context_var,
})
}
})
return promptVariables
}
export const promptVariablesToUserInputsForm = (promptVariables: PromptVariable[]) => {
const userInputs: UserInputFormItem[] = []
promptVariables.filter(({ key, name }) => {
if (key && key.trim() && name && name.trim())
return true
return false
}).forEach((item: any) => {
if (item.type === 'string' || item.type === 'paragraph') {
userInputs.push({
[item.type === 'string' ? 'text-input' : 'paragraph']: {
label: item.name,
variable: item.key,
required: item.required !== false, // default true
max_length: item.max_length,
default: '',
},
} as any)
return
}
if (item.type === 'number') {
userInputs.push({
number: {
label: item.name,
variable: item.key,
required: item.required !== false, // default true
default: '',
},
} as any)
}
else if (item.type === 'select') {
userInputs.push({
select: {
label: item.name,
variable: item.key,
required: item.required !== false, // default true
options: item.options,
default: '',
},
} as any)
}
else {
userInputs.push({
external_data_tool: {
label: item.name,
variable: item.key,
enabled: item.enabled,
type: item.type,
config: item.config,
required: item.required,
icon: item.icon,
icon_background: item.icon_background,
},
} as any)
}
})
return userInputs
}

View File

@@ -0,0 +1,18 @@
import { DatasetPermission } from '@/models/datasets'
type DatasetConfig = {
createdBy: string
partialMemberList: string[]
permission: DatasetPermission
}
export const hasEditPermissionForDataset = (userId: string, datasetConfig: DatasetConfig) => {
const { createdBy, partialMemberList, permission } = datasetConfig
if (permission === DatasetPermission.onlyMe)
return userId === createdBy
if (permission === DatasetPermission.allTeamMembers)
return true
if (permission === DatasetPermission.partialMembers)
return partialMemberList.includes(userId)
return false
}

View File

@@ -0,0 +1,12 @@
import dayjs, { type ConfigType } from 'dayjs'
import utc from 'dayjs/plugin/utc'
dayjs.extend(utc)
export const isAfter = (date: ConfigType, compare: ConfigType) => {
return dayjs(date).isAfter(dayjs(compare))
}
export const formatTime = ({ date, dateFormat }: { date: ConfigType; dateFormat: string }) => {
return dayjs(date).format(dateFormat)
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,7 @@
import tz from './timezone.json'
type Item = {
value: number | string
name: string
}
export const timezones: Item[] = tz

View File

@@ -0,0 +1,106 @@
import { MAX_VAR_KEY_LENGTH, VAR_ITEM_TEMPLATE, VAR_ITEM_TEMPLATE_IN_WORKFLOW, getMaxVarNameLength } from '@/config'
import {
CONTEXT_PLACEHOLDER_TEXT,
HISTORY_PLACEHOLDER_TEXT,
PRE_PROMPT_PLACEHOLDER_TEXT,
QUERY_PLACEHOLDER_TEXT,
} from '@/app/components/base/prompt-editor/constants'
import { InputVarType } from '@/app/components/workflow/types'
const otherAllowedRegex = /^[a-zA-Z0-9_]+$/
export const getNewVar = (key: string, type: string) => {
const { ...rest } = VAR_ITEM_TEMPLATE
if (type !== 'string') {
return {
...rest,
type: type || 'string',
key,
name: key.slice(0, getMaxVarNameLength(key)),
}
}
return {
...VAR_ITEM_TEMPLATE,
type: type || 'string',
key,
name: key.slice(0, getMaxVarNameLength(key)),
}
}
export const getNewVarInWorkflow = (key: string, type = InputVarType.textInput) => {
const { max_length, ...rest } = VAR_ITEM_TEMPLATE_IN_WORKFLOW
if (type !== InputVarType.textInput) {
return {
...rest,
type,
variable: key,
label: key.slice(0, getMaxVarNameLength(key)),
}
}
return {
...VAR_ITEM_TEMPLATE_IN_WORKFLOW,
type,
variable: key,
label: key.slice(0, getMaxVarNameLength(key)),
}
}
export const checkKey = (key: string, canBeEmpty?: boolean) => {
if (key.length === 0 && !canBeEmpty)
return 'canNoBeEmpty'
if (canBeEmpty && key === '')
return true
if (key.length > MAX_VAR_KEY_LENGTH)
return 'tooLong'
if (otherAllowedRegex.test(key)) {
if (/[0-9]/.test(key[0]))
return 'notStartWithNumber'
return true
}
return 'notValid'
}
export const checkKeys = (keys: string[], canBeEmpty?: boolean) => {
let isValid = true
let errorKey = ''
let errorMessageKey = ''
keys.forEach((key) => {
if (!isValid)
return
const res = checkKey(key, canBeEmpty)
if (res !== true) {
isValid = false
errorKey = key
errorMessageKey = res
}
})
return { isValid, errorKey, errorMessageKey }
}
const varRegex = /\{\{([a-zA-Z_][a-zA-Z0-9_]*)\}\}/g
export const getVars = (value: string) => {
if (!value)
return []
const keys = value.match(varRegex)?.filter((item) => {
return ![CONTEXT_PLACEHOLDER_TEXT, HISTORY_PLACEHOLDER_TEXT, QUERY_PLACEHOLDER_TEXT, PRE_PROMPT_PLACEHOLDER_TEXT].includes(item)
}).map((item) => {
return item.replace('{{', '').replace('}}', '')
}).filter(key => key.length <= MAX_VAR_KEY_LENGTH) || []
const keyObj: Record<string, boolean> = {}
// remove duplicate keys
const res: string[] = []
keys.forEach((key) => {
if (keyObj[key])
return
keyObj[key] = true
res.push(key)
})
return res
}