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,25 @@
import type { AppMode } from '@/types/app'
export const getRedirectionPath = (
isCurrentWorkspaceEditor: boolean,
app: { id: string, mode: AppMode },
) => {
if (!isCurrentWorkspaceEditor) {
return `/app/${app.id}/overview`
}
else {
if (app.mode === 'workflow' || app.mode === 'advanced-chat')
return `/app/${app.id}/workflow`
else
return `/app/${app.id}/configuration`
}
}
export const getRedirection = (
isCurrentWorkspaceEditor: boolean,
app: { id: string, mode: AppMode },
redirectionFunc: (href: string) => void,
) => {
const redirectionPath = getRedirectionPath(isCurrentWorkspaceEditor, app)
redirectionFunc(redirectionPath)
}

View File

@@ -0,0 +1,56 @@
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', () => {
/* eslint-disable tailwindcss/classnames-order */
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,89 @@
import type { FormValue, ModelParameterRule } from '@/app/components/header/account-setting/model-provider-page/declarations'
export const mergeValidCompletionParams = (
oldParams: FormValue | undefined,
rules: ModelParameterRule[],
isAdvancedMode: boolean = false,
): { params: FormValue; removedDetails: Record<string, string> } => {
if (!oldParams || Object.keys(oldParams).length === 0)
return { params: {}, removedDetails: {} }
const ruleMap: Record<string, ModelParameterRule> = {}
rules.forEach((r) => {
ruleMap[r.name] = r
})
const nextParams: FormValue = {}
const removedDetails: Record<string, string> = {}
Object.entries(oldParams).forEach(([key, value]) => {
if (key === 'stop' && isAdvancedMode) {
// keep stop in advanced mode
nextParams[key] = value
return
}
const rule = ruleMap[key]
if (!rule) {
removedDetails[key] = 'unsupported'
return
}
switch (rule.type) {
case 'int':
case 'float': {
if (typeof value !== 'number') {
removedDetails[key] = 'invalid type'
return
}
const min = rule.min ?? Number.NEGATIVE_INFINITY
const max = rule.max ?? Number.POSITIVE_INFINITY
if (value < min || value > max) {
removedDetails[key] = `out of range (${min}-${max})`
return
}
nextParams[key] = value
return
}
case 'boolean': {
if (typeof value !== 'boolean') {
removedDetails[key] = 'invalid type'
return
}
nextParams[key] = value
return
}
case 'string':
case 'text': {
if (typeof value !== 'string') {
removedDetails[key] = 'invalid type'
return
}
if (Array.isArray(rule.options) && rule.options.length) {
if (!(rule.options as string[]).includes(value)) {
removedDetails[key] = 'unsupported option'
return
}
}
nextParams[key] = value
return
}
default: {
removedDetails[key] = `unsupported rule type: ${(rule as any)?.type ?? 'unknown'}`
}
}
})
return { params: nextParams, removedDetails }
}
export const fetchAndMergeValidCompletionParams = async (
provider: string,
modelId: string,
oldParams: FormValue | undefined,
isAdvancedMode: boolean = false,
): Promise<{ params: FormValue; removedDetails: Record<string, string> }> => {
const { fetchModelParameterRules } = await import('@/service/common')
const url = `/workspaces/current/model-providers/${provider}/models/parameter-rules?model=${modelId}`
const { data: parameterRules } = await fetchModelParameterRules(url)
return mergeValidCompletionParams(oldParams, parameterRules ?? [], isAdvancedMode)
}

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,245 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://json-schema.org/draft-07/schema#",
"title": "Core schema meta-schema",
"definitions": {
"schemaArray": {
"type": "array",
"minItems": 1,
"items": {
"$ref": "#"
}
},
"nonNegativeInteger": {
"type": "integer",
"minimum": 0
},
"nonNegativeIntegerDefault0": {
"allOf": [
{
"$ref": "#/definitions/nonNegativeInteger"
},
{
"default": 0
}
]
},
"simpleTypes": {
"enum": [
"array",
"boolean",
"integer",
"null",
"number",
"object",
"string"
]
},
"stringArray": {
"type": "array",
"items": {
"type": "string"
},
"uniqueItems": true,
"default": []
}
},
"type": [
"object",
"boolean"
],
"properties": {
"$id": {
"type": "string",
"format": "uri-reference"
},
"$schema": {
"type": "string",
"format": "uri"
},
"$ref": {
"type": "string",
"format": "uri-reference"
},
"$comment": {
"type": "string"
},
"title": {
"type": "string"
},
"description": {
"type": "string"
},
"default": true,
"readOnly": {
"type": "boolean",
"default": false
},
"writeOnly": {
"type": "boolean",
"default": false
},
"examples": {
"type": "array",
"items": true
},
"multipleOf": {
"type": "number",
"exclusiveMinimum": 0
},
"maximum": {
"type": "number"
},
"exclusiveMaximum": {
"type": "number"
},
"minimum": {
"type": "number"
},
"exclusiveMinimum": {
"type": "number"
},
"maxLength": {
"$ref": "#/definitions/nonNegativeInteger"
},
"minLength": {
"$ref": "#/definitions/nonNegativeIntegerDefault0"
},
"pattern": {
"type": "string",
"format": "regex"
},
"additionalItems": {
"$ref": "#"
},
"items": {
"anyOf": [
{
"$ref": "#"
},
{
"$ref": "#/definitions/schemaArray"
}
],
"default": true
},
"maxItems": {
"$ref": "#/definitions/nonNegativeInteger"
},
"minItems": {
"$ref": "#/definitions/nonNegativeIntegerDefault0"
},
"uniqueItems": {
"type": "boolean",
"default": false
},
"contains": {
"$ref": "#"
},
"maxProperties": {
"$ref": "#/definitions/nonNegativeInteger"
},
"minProperties": {
"$ref": "#/definitions/nonNegativeIntegerDefault0"
},
"required": {
"$ref": "#/definitions/stringArray"
},
"additionalProperties": {
"$ref": "#"
},
"definitions": {
"type": "object",
"additionalProperties": {
"$ref": "#"
},
"default": {}
},
"properties": {
"type": "object",
"additionalProperties": {
"$ref": "#"
},
"default": {}
},
"patternProperties": {
"type": "object",
"additionalProperties": {
"$ref": "#"
},
"propertyNames": {
"format": "regex"
},
"default": {}
},
"dependencies": {
"type": "object",
"additionalProperties": {
"anyOf": [
{
"$ref": "#"
},
{
"$ref": "#/definitions/stringArray"
}
]
}
},
"propertyNames": {
"$ref": "#"
},
"const": true,
"enum": {
"type": "array",
"items": true,
"minItems": 1,
"uniqueItems": true
},
"type": {
"anyOf": [
{
"$ref": "#/definitions/simpleTypes"
},
{
"type": "array",
"items": {
"$ref": "#/definitions/simpleTypes"
},
"minItems": 1,
"uniqueItems": true
}
]
},
"format": {
"type": "string"
},
"contentMediaType": {
"type": "string"
},
"contentEncoding": {
"type": "string"
},
"if": {
"$ref": "#"
},
"then": {
"$ref": "#"
},
"else": {
"$ref": "#"
},
"allOf": {
"$ref": "#/definitions/schemaArray"
},
"anyOf": {
"$ref": "#/definitions/schemaArray"
},
"oneOf": {
"$ref": "#/definitions/schemaArray"
},
"not": {
"$ref": "#"
}
},
"default": true
}

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,104 @@
import { downloadFile, 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.00 bytes')
})
test('should format kilobytes correctly', () => {
expect(formatFileSize(1500)).toBe('1.46 KB')
})
test('should format megabytes correctly', () => {
expect(formatFileSize(1500000)).toBe('1.43 MB')
})
test('should format gigabytes correctly', () => {
expect(formatFileSize(1500000000)).toBe('1.40 GB')
})
test('should format terabytes correctly', () => {
expect(formatFileSize(1500000000000)).toBe('1.36 TB')
})
test('should format petabytes correctly', () => {
expect(formatFileSize(1500000000000000)).toBe('1.33 PB')
})
})
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')
})
})
describe('downloadFile', () => {
test('should create a link and trigger a download correctly', () => {
// Mock data
const blob = new Blob(['test content'], { type: 'text/plain' })
const fileName = 'test-file.txt'
const mockUrl = 'blob:mockUrl'
// Mock URL.createObjectURL
const createObjectURLMock = jest.fn().mockReturnValue(mockUrl)
const revokeObjectURLMock = jest.fn()
Object.defineProperty(window.URL, 'createObjectURL', { value: createObjectURLMock })
Object.defineProperty(window.URL, 'revokeObjectURL', { value: revokeObjectURLMock })
// Mock createElement and appendChild
const mockLink = {
href: '',
download: '',
click: jest.fn(),
remove: jest.fn(),
}
const createElementMock = jest.spyOn(document, 'createElement').mockReturnValue(mockLink as any)
const appendChildMock = jest.spyOn(document.body, 'appendChild').mockImplementation((node: Node) => {
return node
})
// Call the function
downloadFile({ data: blob, fileName })
// Assertions
expect(createObjectURLMock).toHaveBeenCalledWith(blob)
expect(createElementMock).toHaveBeenCalledWith('a')
expect(mockLink.href).toBe(mockUrl)
expect(mockLink.download).toBe(fileName)
expect(appendChildMock).toHaveBeenCalledWith(mockLink)
expect(mockLink.click).toHaveBeenCalled()
expect(mockLink.remove).toHaveBeenCalled()
expect(revokeObjectURLMock).toHaveBeenCalledWith(mockUrl)
// Clean up mocks
jest.restoreAllMocks()
})
})

View File

@@ -0,0 +1,92 @@
/**
* Formats a number with comma separators.
* @example formatNumber(1234567) will return '1,234,567'
* @example 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('.')
}
/**
* Format file size into standard string format.
* @param fileSize file size (Byte)
* @example formatFileSize(1024) will return '1.00 KB'
* @example formatFileSize(1024 * 1024) will return '1.00 MB'
*/
export const formatFileSize = (fileSize: number) => {
if (!fileSize)
return fileSize
const units = ['', 'K', 'M', 'G', 'T', 'P']
let index = 0
while (fileSize >= 1024 && index < units.length) {
fileSize = fileSize / 1024
index++
}
if (index === 0)
return `${fileSize.toFixed(2)} bytes`
return `${fileSize.toFixed(2)} ${units[index]}B`
}
/**
* Format time into standard string format.
* @example formatTime(60) will return '1.00 min'
* @example formatTime(60 * 60) will return '1.00 h'
*/
export const formatTime = (seconds: number) => {
if (!seconds)
return seconds
const units = ['sec', 'min', 'h']
let index = 0
while (seconds >= 60 && index < units.length) {
seconds = seconds / 60
index++
}
return `${seconds.toFixed(2)} ${units[index]}`
}
export const downloadFile = ({ data, fileName }: { data: Blob; fileName: string }) => {
const url = window.URL.createObjectURL(data)
const a = document.createElement('a')
a.href = url
a.download = fileName
document.body.appendChild(a)
a.click()
a.remove()
window.URL.revokeObjectURL(url)
}
/**
* Formats a number into a readable string using "k", "M", or "B" suffix.
* @example
* 950 => "950"
* 1200 => "1.2k"
* 1500000 => "1.5M"
* 2000000000 => "2B"
*
* @param {number} num - The number to format
* @returns {string} - The formatted number string
*/
export const formatNumberAbbreviated = (num: number) => {
// If less than 1000, return as-is
if (num < 1000) return num.toString()
// Define thresholds and suffixes
const units = [
{ value: 1e9, symbol: 'B' },
{ value: 1e6, symbol: 'M' },
{ value: 1e3, symbol: 'k' },
]
for (let i = 0; i < units.length; i++) {
if (num >= units[i].value) {
const formatted = (num / units[i].value).toFixed(1)
return formatted.endsWith('.0')
? `${Number.parseInt(formatted)}${units[i].symbol}`
: `${formatted}${units[i].symbol}`
}
}
}

View File

@@ -0,0 +1,5 @@
import { MARKETPLACE_API_PREFIX } from '@/config'
export const getIconFromMarketPlace = (plugin_id: string) => {
return `${MARKETPLACE_API_PREFIX}/plugins/${plugin_id}/icon`
}

View File

@@ -0,0 +1,295 @@
import {
asyncRunSafe,
canFindTool,
correctModelProvider,
correctToolProvider,
fetchWithRetry,
getPurifyHref,
getTextWidthWithCanvas,
randomString,
removeSpecificQueryParam,
sleep,
} from './index'
describe('sleep', () => {
it('should wait for the specified time', async () => {
const timeVariance = 10
const sleepTime = 100
const start = Date.now()
await sleep(sleepTime)
const elapsed = Date.now() - start
expect(elapsed).toBeGreaterThanOrEqual(sleepTime - timeVariance)
})
})
describe('asyncRunSafe', () => {
it('should return [null, result] when promise resolves', async () => {
const result = await asyncRunSafe(Promise.resolve('success'))
expect(result).toEqual([null, 'success'])
})
it('should return [error] when promise rejects', async () => {
const error = new Error('test error')
const result = await asyncRunSafe(Promise.reject(error))
expect(result).toEqual([error])
})
it('should return [Error] when promise rejects with undefined', async () => {
// eslint-disable-next-line prefer-promise-reject-errors
const result = await asyncRunSafe(Promise.reject())
expect(result[0]).toBeInstanceOf(Error)
expect(result[0]?.message).toBe('unknown error')
})
})
describe('getTextWidthWithCanvas', () => {
let originalCreateElement: typeof document.createElement
beforeEach(() => {
// Store original implementation
originalCreateElement = document.createElement
// Mock canvas and context
const measureTextMock = jest.fn().mockReturnValue({ width: 100 })
const getContextMock = jest.fn().mockReturnValue({
measureText: measureTextMock,
font: '',
})
document.createElement = jest.fn().mockReturnValue({
getContext: getContextMock,
})
})
afterEach(() => {
// Restore original implementation
document.createElement = originalCreateElement
})
it('should return the width of text', () => {
const width = getTextWidthWithCanvas('test text')
expect(width).toBe(100)
})
it('should return 0 if context is not available', () => {
// Override mock for this test
document.createElement = jest.fn().mockReturnValue({
getContext: () => null,
})
const width = getTextWidthWithCanvas('test text')
expect(width).toBe(0)
})
})
describe('randomString', () => {
it('should generate string of specified length', () => {
const result = randomString(10)
expect(result.length).toBe(10)
})
it('should only contain valid characters', () => {
const result = randomString(100)
const validChars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_'
for (const char of result)
expect(validChars).toContain(char)
})
it('should generate different strings on consecutive calls', () => {
const result1 = randomString(20)
const result2 = randomString(20)
expect(result1).not.toEqual(result2)
})
})
describe('getPurifyHref', () => {
it('should return empty string for falsy input', () => {
expect(getPurifyHref('')).toBe('')
expect(getPurifyHref(undefined as any)).toBe('')
})
it('should escape HTML characters', () => {
expect(getPurifyHref('<script>alert("xss")</script>')).not.toContain('<script>')
})
})
describe('fetchWithRetry', () => {
it('should return successfully on first try', async () => {
const successData = { status: 'success' }
const promise = Promise.resolve(successData)
const result = await fetchWithRetry(promise)
expect(result).toEqual([null, successData])
})
// it('should retry and succeed on second attempt', async () => {
// let attemptCount = 0
// const mockFn = new Promise((resolve, reject) => {
// attemptCount++
// if (attemptCount === 1)
// reject(new Error('First attempt failed'))
// else
// resolve('success')
// })
// const result = await fetchWithRetry(mockFn)
// expect(result).toEqual([null, 'success'])
// expect(attemptCount).toBe(2)
// })
// it('should stop after max retries and return last error', async () => {
// const testError = new Error('Test error')
// const promise = Promise.reject(testError)
// const result = await fetchWithRetry(promise, 2)
// expect(result).toEqual([testError])
// })
// it('should handle non-Error rejection with custom error', async () => {
// const stringError = 'string error message'
// const promise = Promise.reject(stringError)
// const result = await fetchWithRetry(promise, 0)
// expect(result[0]).toBeInstanceOf(Error)
// expect(result[0]?.message).toBe('unknown error')
// })
// it('should use default 3 retries when retries parameter is not provided', async () => {
// let attempts = 0
// const mockFn = () => new Promise((resolve, reject) => {
// attempts++
// reject(new Error(`Attempt ${attempts} failed`))
// })
// await fetchWithRetry(mockFn())
// expect(attempts).toBe(4) // Initial attempt + 3 retries
// })
})
describe('correctModelProvider', () => {
it('should return empty string for falsy input', () => {
expect(correctModelProvider('')).toBe('')
})
it('should return the provider if it already contains a slash', () => {
expect(correctModelProvider('company/model')).toBe('company/model')
})
it('should format google provider correctly', () => {
expect(correctModelProvider('google')).toBe('langgenius/gemini/google')
})
it('should format standard providers correctly', () => {
expect(correctModelProvider('openai')).toBe('langgenius/openai/openai')
})
})
describe('correctToolProvider', () => {
it('should return empty string for falsy input', () => {
expect(correctToolProvider('')).toBe('')
})
it('should return the provider if toolInCollectionList is true', () => {
expect(correctToolProvider('any-provider', true)).toBe('any-provider')
})
it('should return the provider if it already contains a slash', () => {
expect(correctToolProvider('company/tool')).toBe('company/tool')
})
it('should format special tool providers correctly', () => {
expect(correctToolProvider('stepfun')).toBe('langgenius/stepfun_tool/stepfun')
expect(correctToolProvider('jina')).toBe('langgenius/jina_tool/jina')
})
it('should format standard tool providers correctly', () => {
expect(correctToolProvider('standard')).toBe('langgenius/standard/standard')
})
})
describe('canFindTool', () => {
it('should match when IDs are identical', () => {
expect(canFindTool('tool-id', 'tool-id')).toBe(true)
})
it('should match when provider ID is formatted with standard pattern', () => {
expect(canFindTool('langgenius/tool-id/tool-id', 'tool-id')).toBe(true)
})
it('should match when provider ID is formatted with tool pattern', () => {
expect(canFindTool('langgenius/tool-id_tool/tool-id', 'tool-id')).toBe(true)
})
it('should not match when IDs are completely different', () => {
expect(canFindTool('provider-a', 'tool-b')).toBe(false)
})
})
describe('removeSpecificQueryParam', () => {
let originalLocation: Location
let originalReplaceState: typeof window.history.replaceState
beforeEach(() => {
originalLocation = window.location
originalReplaceState = window.history.replaceState
const mockUrl = new URL('https://example.com?param1=value1&param2=value2&param3=value3')
// Mock window.location using defineProperty to handle URL properly
delete (window as any).location
Object.defineProperty(window, 'location', {
writable: true,
value: {
...originalLocation,
href: mockUrl.href,
search: mockUrl.search,
toString: () => mockUrl.toString(),
},
})
window.history.replaceState = jest.fn()
})
afterEach(() => {
Object.defineProperty(window, 'location', {
writable: true,
value: originalLocation,
})
window.history.replaceState = originalReplaceState
})
it('should remove a single query parameter', () => {
removeSpecificQueryParam('param2')
expect(window.history.replaceState).toHaveBeenCalledTimes(1)
const replaceStateCall = (window.history.replaceState as jest.Mock).mock.calls[0]
expect(replaceStateCall[0]).toBe(null)
expect(replaceStateCall[1]).toBe('')
expect(replaceStateCall[2]).toMatch(/param1=value1/)
expect(replaceStateCall[2]).toMatch(/param3=value3/)
expect(replaceStateCall[2]).not.toMatch(/param2=value2/)
})
it('should remove multiple query parameters', () => {
removeSpecificQueryParam(['param1', 'param3'])
expect(window.history.replaceState).toHaveBeenCalledTimes(1)
const replaceStateCall = (window.history.replaceState as jest.Mock).mock.calls[0]
expect(replaceStateCall[2]).toMatch(/param2=value2/)
expect(replaceStateCall[2]).not.toMatch(/param1=value1/)
expect(replaceStateCall[2]).not.toMatch(/param3=value3/)
})
it('should handle non-existent parameters gracefully', () => {
removeSpecificQueryParam('nonexistent')
expect(window.history.replaceState).toHaveBeenCalledTimes(1)
const replaceStateCall = (window.history.replaceState as jest.Mock).mock.calls[0]
expect(replaceStateCall[2]).toMatch(/param1=value1/)
expect(replaceStateCall[2]).toMatch(/param2=value2/)
expect(replaceStateCall[2]).toMatch(/param3=value3/)
})
})

View File

@@ -0,0 +1,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())
}

View File

@@ -0,0 +1,199 @@
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.checkbox)
return ['boolean', item.checkbox]
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]
if (item.json_object)
return ['json_object', item.json_object]
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,
hide: content.hide,
default: content.default,
})
}
else if (type === 'number') {
promptVariables.push({
key: content.variable,
name: content.label,
required: content.required,
type,
options: [],
hide: content.hide,
default: content.default,
})
}
else if (type === 'select') {
promptVariables.push({
key: content.variable,
name: content.label,
required: content.required,
type: 'select',
options: content.options,
is_context_var,
hide: content.hide,
default: content.default,
})
}
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,
},
hide: content.hide,
default: content.default,
})
}
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,
},
hide: content.hide,
default: content.default,
})
}
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,
hide: content.hide,
})
}
})
return promptVariables
}
export const promptVariablesToUserInputsForm = (promptVariables: PromptVariable[]) => {
const userInputs: UserInputFormItem[] = []
promptVariables.filter(({ key, name }) => {
return key && key.trim() && name && name.trim()
}).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: '',
hide: item.hide,
},
} as any)
return
}
if (item.type === 'number' || item.type === 'checkbox') {
userInputs.push({
[item.type]: {
label: item.name,
variable: item.key,
required: item.required !== false, // default true
default: '',
hide: item.hide,
},
} 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: item.default ?? '',
hide: item.hide,
},
} 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,
hide: item.hide,
},
} as any)
}
})
return userInputs
}
export const formatBooleanInputs = (useInputs?: PromptVariable[] | null, inputs?: Record<string, string | number | object | boolean> | null) => {
if(!useInputs)
return inputs
const res = { ...inputs }
useInputs.forEach((item) => {
const isBooleanInput = item.type === 'boolean'
if (isBooleanInput) {
// Convert boolean inputs to boolean type
res[item.key] = !!res[item.key]
}
})
return res
}

View File

@@ -0,0 +1,189 @@
/**
* Navigation Utilities
*
* Provides helper functions for consistent navigation behavior throughout the application,
* specifically for preserving query parameters when navigating between related pages.
*/
/**
* Creates a navigation path that preserves current URL query parameters
*
* @param basePath - The base path to navigate to (e.g., '/datasets/123/documents')
* @param preserveParams - Whether to preserve current query parameters (default: true)
* @returns The complete navigation path with preserved query parameters
*
* @example
* // Current URL: /datasets/123/documents/456?page=3&limit=10&keyword=test
* const backPath = createNavigationPath('/datasets/123/documents')
* // Returns: '/datasets/123/documents?page=3&limit=10&keyword=test'
*
* @example
* // Navigate without preserving params
* const cleanPath = createNavigationPath('/datasets/123/documents', false)
* // Returns: '/datasets/123/documents'
*/
export function createNavigationPath(basePath: string, preserveParams: boolean = true): string {
if (!preserveParams)
return basePath
try {
const searchParams = new URLSearchParams(window.location.search)
const queryString = searchParams.toString()
const separator = queryString ? '?' : ''
return `${basePath}${separator}${queryString}`
}
catch (error) {
// Fallback to base path if there's any error accessing location
console.warn('Failed to preserve query parameters:', error)
return basePath
}
}
/**
* Creates a back navigation function that preserves query parameters
*
* @param router - Next.js router instance
* @param basePath - The base path to navigate back to
* @param preserveParams - Whether to preserve current query parameters (default: true)
* @returns A function that navigates back with preserved parameters
*
* @example
* const router = useRouter()
* const backToPrev = createBackNavigation(router, `/datasets/${datasetId}/documents`)
*
* // Later, when user clicks back:
* backToPrev()
*/
export function createBackNavigation(
router: { push: (path: string) => void },
basePath: string,
preserveParams: boolean = true,
): () => void {
return () => {
const navigationPath = createNavigationPath(basePath, preserveParams)
router.push(navigationPath)
}
}
/**
* Extracts specific query parameters from current URL
*
* @param paramNames - Array of parameter names to extract
* @returns Object with extracted parameters
*
* @example
* // Current URL: /page?page=3&limit=10&keyword=test&other=value
* const params = extractQueryParams(['page', 'limit', 'keyword'])
* // Returns: { page: '3', limit: '10', keyword: 'test' }
*/
export function extractQueryParams(paramNames: string[]): Record<string, string> {
try {
const searchParams = new URLSearchParams(window.location.search)
const extracted: Record<string, string> = {}
paramNames.forEach((name) => {
const value = searchParams.get(name)
if (value !== null)
extracted[name] = value
})
return extracted
}
catch (error) {
console.warn('Failed to extract query parameters:', error)
return {}
}
}
/**
* Creates a navigation path with specific query parameters
*
* @param basePath - The base path
* @param params - Object of query parameters to include
* @returns Navigation path with specified parameters
*
* @example
* const path = createNavigationPathWithParams('/datasets/123/documents', {
* page: '1',
* limit: '25',
* keyword: 'search term'
* })
* // Returns: '/datasets/123/documents?page=1&limit=25&keyword=search+term'
*/
export function createNavigationPathWithParams(
basePath: string,
params: Record<string, string | number>,
): string {
try {
const searchParams = new URLSearchParams()
Object.entries(params).forEach(([key, value]) => {
if (value !== undefined && value !== null && value !== '')
searchParams.set(key, String(value))
})
const queryString = searchParams.toString()
const separator = queryString ? '?' : ''
return `${basePath}${separator}${queryString}`
}
catch (error) {
console.warn('Failed to create navigation path with params:', error)
return basePath
}
}
/**
* Merges current query parameters with new ones
*
* @param newParams - New parameters to add or override
* @param preserveExisting - Whether to preserve existing parameters (default: true)
* @returns URLSearchParams object with merged parameters
*
* @example
* // Current URL: /page?page=3&limit=10
* const merged = mergeQueryParams({ keyword: 'test', page: '1' })
* // Results in: page=1&limit=10&keyword=test (page overridden, limit preserved, keyword added)
*/
export function mergeQueryParams(
newParams: Record<string, string | number | null | undefined>,
preserveExisting: boolean = true,
): URLSearchParams {
const searchParams = preserveExisting
? new URLSearchParams(window.location.search)
: new URLSearchParams()
Object.entries(newParams).forEach(([key, value]) => {
if (value === null || value === undefined)
searchParams.delete(key)
else if (value !== '')
searchParams.set(key, String(value))
})
return searchParams
}
/**
* Navigation utilities for common dataset/document patterns
*/
export const datasetNavigation = {
/**
* Creates navigation back to dataset documents list with preserved state
*/
backToDocuments: (router: { push: (path: string) => void }, datasetId: string) => {
return createBackNavigation(router, `/datasets/${datasetId}/documents`)
},
/**
* Creates navigation to document detail
*/
toDocumentDetail: (router: { push: (path: string) => void }, datasetId: string, documentId: string) => {
return () => router.push(`/datasets/${datasetId}/documents/${documentId}`)
},
/**
* Creates navigation to document settings
*/
toDocumentSettings: (router: { push: (path: string) => void }, datasetId: string, documentId: string) => {
return () => router.push(`/datasets/${datasetId}/documents/${documentId}/settings`)
},
}

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,26 @@
import { isSupportMCP } from './plugin-version-feature'
describe('plugin-version-feature', () => {
beforeEach(() => {
jest.clearAllMocks()
})
describe('isSupportMCP', () => {
it('should call isEqualOrLaterThanVersion with the correct parameters', () => {
expect(isSupportMCP('0.0.3')).toBe(true)
expect(isSupportMCP('1.0.0')).toBe(true)
})
it('should return true when version is equal to the supported MCP version', () => {
const mockVersion = '0.0.2'
const result = isSupportMCP(mockVersion)
expect(result).toBe(true)
})
it('should return false when version is less than the supported MCP version', () => {
const mockVersion = '0.0.1'
const result = isSupportMCP(mockVersion)
expect(result).toBe(false)
})
})
})

View File

@@ -0,0 +1,10 @@
import { isEqualOrLaterThanVersion } from './semver'
const SUPPORT_MCP_VERSION = '0.0.2'
export const isSupportMCP = (version?: string): boolean => {
if (!version)
return false
return isEqualOrLaterThanVersion(version, SUPPORT_MCP_VERSION)
}

View File

@@ -0,0 +1,75 @@
import { compareVersion, getLatestVersion, isEqualOrLaterThanVersion } from './semver'
describe('semver utilities', () => {
describe('getLatestVersion', () => {
it('should return the latest version from a list of versions', () => {
expect(getLatestVersion(['1.0.0', '1.1.0', '1.0.1'])).toBe('1.1.0')
expect(getLatestVersion(['2.0.0', '1.9.9', '1.10.0'])).toBe('2.0.0')
expect(getLatestVersion(['1.0.0-alpha', '1.0.0-beta', '1.0.0'])).toBe('1.0.0')
})
it('should handle patch versions correctly', () => {
expect(getLatestVersion(['1.0.1', '1.0.2', '1.0.0'])).toBe('1.0.2')
expect(getLatestVersion(['1.0.10', '1.0.9', '1.0.11'])).toBe('1.0.11')
})
it('should handle mixed version formats', () => {
expect(getLatestVersion(['v1.0.0', '1.1.0', 'v1.2.0'])).toBe('v1.2.0')
expect(getLatestVersion(['1.0.0-rc.1', '1.0.0', '1.0.0-beta'])).toBe('1.0.0')
})
it('should return the only version if only one version is provided', () => {
expect(getLatestVersion(['1.0.0'])).toBe('1.0.0')
})
})
describe('compareVersion', () => {
it('should return 1 when first version is greater', () => {
expect(compareVersion('1.1.0', '1.0.0')).toBe(1)
expect(compareVersion('2.0.0', '1.9.9')).toBe(1)
expect(compareVersion('1.0.1', '1.0.0')).toBe(1)
})
it('should return -1 when first version is less', () => {
expect(compareVersion('1.0.0', '1.1.0')).toBe(-1)
expect(compareVersion('1.9.9', '2.0.0')).toBe(-1)
expect(compareVersion('1.0.0', '1.0.1')).toBe(-1)
})
it('should return 0 when versions are equal', () => {
expect(compareVersion('1.0.0', '1.0.0')).toBe(0)
expect(compareVersion('2.1.3', '2.1.3')).toBe(0)
})
it('should handle pre-release versions correctly', () => {
expect(compareVersion('1.0.0-beta', '1.0.0-alpha')).toBe(1)
expect(compareVersion('1.0.0', '1.0.0-beta')).toBe(1)
expect(compareVersion('1.0.0-alpha', '1.0.0-beta')).toBe(-1)
})
})
describe('isEqualOrLaterThanVersion', () => {
it('should return true when baseVersion is greater than targetVersion', () => {
expect(isEqualOrLaterThanVersion('1.1.0', '1.0.0')).toBe(true)
expect(isEqualOrLaterThanVersion('2.0.0', '1.9.9')).toBe(true)
expect(isEqualOrLaterThanVersion('1.0.1', '1.0.0')).toBe(true)
})
it('should return true when baseVersion is equal to targetVersion', () => {
expect(isEqualOrLaterThanVersion('1.0.0', '1.0.0')).toBe(true)
expect(isEqualOrLaterThanVersion('2.1.3', '2.1.3')).toBe(true)
})
it('should return false when baseVersion is less than targetVersion', () => {
expect(isEqualOrLaterThanVersion('1.0.0', '1.1.0')).toBe(false)
expect(isEqualOrLaterThanVersion('1.9.9', '2.0.0')).toBe(false)
expect(isEqualOrLaterThanVersion('1.0.0', '1.0.1')).toBe(false)
})
it('should handle pre-release versions correctly', () => {
expect(isEqualOrLaterThanVersion('1.0.0', '1.0.0-beta')).toBe(true)
expect(isEqualOrLaterThanVersion('1.0.0-beta', '1.0.0-alpha')).toBe(true)
expect(isEqualOrLaterThanVersion('1.0.0-alpha', '1.0.0')).toBe(false)
})
})
})

View File

@@ -0,0 +1,13 @@
import semver from 'semver'
export const getLatestVersion = (versionList: string[]) => {
return semver.rsort(versionList)[0]
}
export const compareVersion = (v1: string, v2: string) => {
return semver.compare(v1, v2)
}
export const isEqualOrLaterThanVersion = (baseVersion: string, targetVersion: string) => {
return semver.gte(baseVersion, targetVersion)
}

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,6 @@
import { ModelFeatureEnum } from '@/app/components/header/account-setting/model-provider-page/declarations'
export const supportFunctionCall = (features: ModelFeatureEnum[] = []): boolean => {
if (!features || !features.length) return false
return features.some(feature => [ModelFeatureEnum.toolCall, ModelFeatureEnum.multiToolCall, ModelFeatureEnum.streamToolCall].includes(feature))
}

View File

@@ -0,0 +1,27 @@
import type { Schema } from 'jsonschema'
import { Validator } from 'jsonschema'
import draft07Schema from './draft-07.json'
const validator = new Validator()
export const draft07Validator = (schema: any) => {
return validator.validate(schema, draft07Schema as unknown as Schema)
}
export const forbidBooleanProperties = (schema: any, path: string[] = []): string[] => {
let errors: string[] = []
if (schema && typeof schema === 'object' && schema.properties) {
for (const [key, val] of Object.entries(schema.properties)) {
if (typeof val === 'boolean') {
errors.push(
`Error: Property '${[...path, key].join('.')}' must not be a boolean schema`,
)
}
else if (typeof val === 'object') {
errors = errors.concat(forbidBooleanProperties(val, [...path, key]))
}
}
}
return errors
}

147
dify_1.9.0/web/utils/var.ts Normal file
View File

@@ -0,0 +1,147 @@
import { MARKETPLACE_URL_PREFIX, 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 type { InputVar } from '@/app/components/workflow/types'
import { InputVarType } from '@/app/components/workflow/types'
const otherAllowedRegex = /^\w+$/
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): InputVar => {
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)),
placeholder: '',
default: '',
hint: '',
}
}
export const checkKey = (key: string, canBeEmpty?: boolean, keys?: string[]) => {
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 (/\d/.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 }
}
export const hasDuplicateStr = (strArr: string[]) => {
const strObj: Record<string, number> = {}
strArr.forEach((str) => {
if (strObj[str])
strObj[str] += 1
else
strObj[str] = 1
})
return !!Object.keys(strObj).find(key => strObj[key] > 1)
}
const varRegex = /\{\{([a-zA-Z_]\w*)\}\}/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
}
// Set the value of basePath
// example: /dify
export const basePath = process.env.NEXT_PUBLIC_BASE_PATH || ''
export function getMarketplaceUrl(path: string, params?: Record<string, string | undefined>) {
const searchParams = new URLSearchParams({ source: encodeURIComponent(window.location.origin) })
if (params) {
Object.keys(params).forEach((key) => {
const value = params[key]
if (value !== undefined && value !== null)
searchParams.append(key, value)
})
}
return `${MARKETPLACE_URL_PREFIX}${path}?${searchParams.toString()}`
}
export const replaceSpaceWithUnderscoreInVarNameInput = (input: HTMLInputElement) => {
const start = input.selectionStart
const end = input.selectionEnd
input.value = input.value.replaceAll(' ', '_')
if (start !== null && end !== null)
input.setSelectionRange(start, end)
}

View File

@@ -0,0 +1,173 @@
import { ZodError, z } from 'zod'
describe('Zod Features', () => {
it('should support string', () => {
const stringSchema = z.string()
const numberLikeStringSchema = z.coerce.string() // 12 would be converted to '12'
const stringSchemaWithError = z.string({
required_error: 'Name is required',
invalid_type_error: 'Invalid name type, expected string',
})
const urlSchema = z.string().url()
const uuidSchema = z.string().uuid()
expect(stringSchema.parse('hello')).toBe('hello')
expect(() => stringSchema.parse(12)).toThrow()
expect(numberLikeStringSchema.parse('12')).toBe('12')
expect(numberLikeStringSchema.parse(12)).toBe('12')
expect(() => stringSchemaWithError.parse(undefined)).toThrow('Name is required')
expect(() => stringSchemaWithError.parse(12)).toThrow('Invalid name type, expected string')
expect(urlSchema.parse('https://dify.ai')).toBe('https://dify.ai')
expect(uuidSchema.parse('123e4567-e89b-12d3-a456-426614174000')).toBe('123e4567-e89b-12d3-a456-426614174000')
})
it('should support enum', () => {
enum JobStatus {
waiting = 'waiting',
processing = 'processing',
completed = 'completed',
}
expect(z.nativeEnum(JobStatus).parse(JobStatus.waiting)).toBe(JobStatus.waiting)
expect(z.nativeEnum(JobStatus).parse('completed')).toBe('completed')
expect(() => z.nativeEnum(JobStatus).parse('invalid')).toThrow()
})
it('should support number', () => {
const numberSchema = z.number()
const numberWithMin = z.number().gt(0) // alias min
const numberWithMinEqual = z.number().gte(0)
const numberWithMax = z.number().lt(100) // alias max
expect(numberSchema.parse(123)).toBe(123)
expect(numberWithMin.parse(50)).toBe(50)
expect(numberWithMinEqual.parse(0)).toBe(0)
expect(() => numberWithMin.parse(-1)).toThrow()
expect(numberWithMax.parse(50)).toBe(50)
expect(() => numberWithMax.parse(101)).toThrow()
})
it('should support boolean', () => {
const booleanSchema = z.boolean()
expect(booleanSchema.parse(true)).toBe(true)
expect(booleanSchema.parse(false)).toBe(false)
expect(() => booleanSchema.parse('true')).toThrow()
})
it('should support date', () => {
const dateSchema = z.date()
expect(dateSchema.parse(new Date('2023-01-01'))).toEqual(new Date('2023-01-01'))
})
it('should support object', () => {
const userSchema = z.object({
id: z.union([z.string(), z.number()]),
name: z.string(),
email: z.string().email(),
age: z.number().min(0).max(120).optional(),
})
type User = z.infer<typeof userSchema>
const validUser: User = {
id: 1,
name: 'John',
email: 'john@example.com',
age: 30,
}
expect(userSchema.parse(validUser)).toEqual(validUser)
})
it('should support object optional field', () => {
const userSchema = z.object({
name: z.string(),
optionalField: z.optional(z.string()),
})
type User = z.infer<typeof userSchema>
const user: User = {
name: 'John',
}
const userWithOptionalField: User = {
name: 'John',
optionalField: 'optional',
}
expect(userSchema.safeParse(user).success).toEqual(true)
expect(userSchema.safeParse(userWithOptionalField).success).toEqual(true)
})
it('should support object intersection', () => {
const Person = z.object({
name: z.string(),
})
const Employee = z.object({
role: z.string(),
})
const EmployedPerson = z.intersection(Person, Employee)
const validEmployedPerson = {
name: 'John',
role: 'Developer',
}
expect(EmployedPerson.parse(validEmployedPerson)).toEqual(validEmployedPerson)
})
it('should support record', () => {
const recordSchema = z.record(z.string(), z.number())
const validRecord = {
a: 1,
b: 2,
}
expect(recordSchema.parse(validRecord)).toEqual(validRecord)
})
it('should support array', () => {
const numbersSchema = z.array(z.number())
const stringArraySchema = z.string().array()
expect(numbersSchema.parse([1, 2, 3])).toEqual([1, 2, 3])
expect(stringArraySchema.parse(['a', 'b', 'c'])).toEqual(['a', 'b', 'c'])
})
it('should support promise', () => {
const promiseSchema = z.promise(z.string())
const validPromise = Promise.resolve('success')
expect(promiseSchema.parse(validPromise)).resolves.toBe('success')
})
it('should support unions', () => {
const unionSchema = z.union([z.string(), z.number()])
expect(unionSchema.parse('success')).toBe('success')
expect(unionSchema.parse(404)).toBe(404)
})
it('should support functions', () => {
const functionSchema = z.function().args(z.string(), z.number(), z.optional(z.string())).returns(z.number())
const validFunction = (name: string, age: number, _optional?: string): number => {
return age
}
expect(functionSchema.safeParse(validFunction).success).toEqual(true)
})
it('should support undefined, null, any, and void', () => {
const undefinedSchema = z.undefined()
const nullSchema = z.null()
const anySchema = z.any()
expect(undefinedSchema.parse(undefined)).toBeUndefined()
expect(nullSchema.parse(null)).toBeNull()
expect(anySchema.parse('anything')).toBe('anything')
expect(anySchema.parse(3)).toBe(3)
})
it('should safeParse would not throw', () => {
expect(z.string().safeParse('abc').success).toBe(true)
expect(z.string().safeParse(123).success).toBe(false)
expect(z.string().safeParse(123).error).toBeInstanceOf(ZodError)
})
})