mirror of
https://git.mirrors.martin98.com/https://github.com/langgenius/dify.git
synced 2025-08-13 03:59:01 +08:00
fix: providererror message when token validated fail (#190)
This commit is contained in:
parent
d93365d429
commit
0bb253efe0
@ -5,7 +5,7 @@ import Link from 'next/link'
|
|||||||
import { ArrowTopRightOnSquareIcon } from '@heroicons/react/24/outline'
|
import { ArrowTopRightOnSquareIcon } from '@heroicons/react/24/outline'
|
||||||
import { useState, useEffect } from 'react'
|
import { useState, useEffect } from 'react'
|
||||||
import ProviderInput from '../provider-input'
|
import ProviderInput from '../provider-input'
|
||||||
import useValidateToken, { ValidatedStatus } from '../provider-input/useValidateToken'
|
import useValidateToken, { ValidatedStatus, ValidatedStatusState } from '../provider-input/useValidateToken'
|
||||||
import {
|
import {
|
||||||
ValidatedErrorIcon,
|
ValidatedErrorIcon,
|
||||||
ValidatedSuccessIcon,
|
ValidatedSuccessIcon,
|
||||||
@ -15,7 +15,7 @@ import {
|
|||||||
|
|
||||||
interface IAzureProviderProps {
|
interface IAzureProviderProps {
|
||||||
provider: Provider
|
provider: Provider
|
||||||
onValidatedStatus: (status?: ValidatedStatus) => void
|
onValidatedStatus: (status?: ValidatedStatusState) => void
|
||||||
onTokenChange: (token: ProviderAzureToken) => void
|
onTokenChange: (token: ProviderAzureToken) => void
|
||||||
}
|
}
|
||||||
const AzureProvider = ({
|
const AzureProvider = ({
|
||||||
@ -31,7 +31,7 @@ const AzureProvider = ({
|
|||||||
token[type] = ''
|
token[type] = ''
|
||||||
setToken({...token})
|
setToken({...token})
|
||||||
onTokenChange({...token})
|
onTokenChange({...token})
|
||||||
setValidatedStatus(undefined)
|
setValidatedStatus({})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const handleChange = (type: keyof ProviderAzureToken, v: string, validate: any) => {
|
const handleChange = (type: keyof ProviderAzureToken, v: string, validate: any) => {
|
||||||
@ -41,7 +41,7 @@ const AzureProvider = ({
|
|||||||
validate({...token}, {
|
validate({...token}, {
|
||||||
beforeValidating: () => {
|
beforeValidating: () => {
|
||||||
if (!token.openai_api_base || !token.openai_api_key) {
|
if (!token.openai_api_base || !token.openai_api_key) {
|
||||||
setValidatedStatus(undefined)
|
setValidatedStatus({})
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
@ -49,10 +49,10 @@ const AzureProvider = ({
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
const getValidatedIcon = () => {
|
const getValidatedIcon = () => {
|
||||||
if (validatedStatus === ValidatedStatus.Error || validatedStatus === ValidatedStatus.Exceed) {
|
if (validatedStatus.status === ValidatedStatus.Error || validatedStatus.status === ValidatedStatus.Exceed) {
|
||||||
return <ValidatedErrorIcon />
|
return <ValidatedErrorIcon />
|
||||||
}
|
}
|
||||||
if (validatedStatus === ValidatedStatus.Success) {
|
if (validatedStatus.status === ValidatedStatus.Success) {
|
||||||
return <ValidatedSuccessIcon />
|
return <ValidatedSuccessIcon />
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -60,8 +60,8 @@ const AzureProvider = ({
|
|||||||
if (validating) {
|
if (validating) {
|
||||||
return <ValidatingTip />
|
return <ValidatingTip />
|
||||||
}
|
}
|
||||||
if (validatedStatus === ValidatedStatus.Error) {
|
if (validatedStatus.status === ValidatedStatus.Error) {
|
||||||
return <ValidatedErrorOnAzureOpenaiTip />
|
return <ValidatedErrorOnAzureOpenaiTip errorMessage={validatedStatus.message ?? ''} />
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
@ -4,7 +4,7 @@ import { useTranslation } from 'react-i18next'
|
|||||||
import ProviderInput from '../provider-input'
|
import ProviderInput from '../provider-input'
|
||||||
import Link from 'next/link'
|
import Link from 'next/link'
|
||||||
import { ArrowTopRightOnSquareIcon } from '@heroicons/react/24/outline'
|
import { ArrowTopRightOnSquareIcon } from '@heroicons/react/24/outline'
|
||||||
import useValidateToken, { ValidatedStatus } from '../provider-input/useValidateToken'
|
import useValidateToken, { ValidatedStatus, ValidatedStatusState } from '../provider-input/useValidateToken'
|
||||||
import {
|
import {
|
||||||
ValidatedErrorIcon,
|
ValidatedErrorIcon,
|
||||||
ValidatedSuccessIcon,
|
ValidatedSuccessIcon,
|
||||||
@ -15,7 +15,7 @@ import {
|
|||||||
|
|
||||||
interface IOpenaiProviderProps {
|
interface IOpenaiProviderProps {
|
||||||
provider: Provider
|
provider: Provider
|
||||||
onValidatedStatus: (status?: ValidatedStatus) => void
|
onValidatedStatus: (status?: ValidatedStatusState) => void
|
||||||
onTokenChange: (token: string) => void
|
onTokenChange: (token: string) => void
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -31,7 +31,7 @@ const OpenaiProvider = ({
|
|||||||
if (token === provider.token) {
|
if (token === provider.token) {
|
||||||
setToken('')
|
setToken('')
|
||||||
onTokenChange('')
|
onTokenChange('')
|
||||||
setValidatedStatus(undefined)
|
setValidatedStatus({})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const handleChange = (v: string) => {
|
const handleChange = (v: string) => {
|
||||||
@ -40,7 +40,7 @@ const OpenaiProvider = ({
|
|||||||
validate(v, {
|
validate(v, {
|
||||||
beforeValidating: () => {
|
beforeValidating: () => {
|
||||||
if (!v) {
|
if (!v) {
|
||||||
setValidatedStatus(undefined)
|
setValidatedStatus({})
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
@ -54,10 +54,10 @@ const OpenaiProvider = ({
|
|||||||
}, [validatedStatus])
|
}, [validatedStatus])
|
||||||
|
|
||||||
const getValidatedIcon = () => {
|
const getValidatedIcon = () => {
|
||||||
if (validatedStatus === ValidatedStatus.Error || validatedStatus === ValidatedStatus.Exceed) {
|
if (validatedStatus?.status === ValidatedStatus.Error || validatedStatus.status === ValidatedStatus.Exceed) {
|
||||||
return <ValidatedErrorIcon />
|
return <ValidatedErrorIcon />
|
||||||
}
|
}
|
||||||
if (validatedStatus === ValidatedStatus.Success) {
|
if (validatedStatus.status === ValidatedStatus.Success) {
|
||||||
return <ValidatedSuccessIcon />
|
return <ValidatedSuccessIcon />
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -65,11 +65,11 @@ const OpenaiProvider = ({
|
|||||||
if (validating) {
|
if (validating) {
|
||||||
return <ValidatingTip />
|
return <ValidatingTip />
|
||||||
}
|
}
|
||||||
if (validatedStatus === ValidatedStatus.Exceed) {
|
if (validatedStatus?.status === ValidatedStatus.Success) {
|
||||||
return <ValidatedExceedOnOpenaiTip />
|
return <ValidatedExceedOnOpenaiTip />
|
||||||
}
|
}
|
||||||
if (validatedStatus === ValidatedStatus.Error) {
|
if (validatedStatus?.status === ValidatedStatus.Error) {
|
||||||
return <ValidatedErrorOnOpenaiTip />
|
return <ValidatedErrorOnOpenaiTip errorMessage={validatedStatus.message ?? ''} />
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -38,22 +38,22 @@ export const ValidatedExceedOnOpenaiTip = () => {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export const ValidatedErrorOnOpenaiTip = () => {
|
export const ValidatedErrorOnOpenaiTip = ({ errorMessage }: { errorMessage: string }) => {
|
||||||
const { t } = useTranslation()
|
const { t } = useTranslation()
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={`mt-2 text-[#D92D20] text-xs font-normal`}>
|
<div className={`mt-2 text-[#D92D20] text-xs font-normal`}>
|
||||||
{t('common.provider.invalidKey')}
|
{t('common.provider.validatedError')}{errorMessage}
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export const ValidatedErrorOnAzureOpenaiTip = () => {
|
export const ValidatedErrorOnAzureOpenaiTip = ({ errorMessage }: { errorMessage: string }) => {
|
||||||
const { t } = useTranslation()
|
const { t } = useTranslation()
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={`mt-2 text-[#D92D20] text-xs font-normal`}>
|
<div className={`mt-2 text-[#D92D20] text-xs font-normal`}>
|
||||||
{t('common.provider.invalidApiKey')}
|
{t('common.provider.validatedError')}{errorMessage}
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
@ -8,11 +8,16 @@ export enum ValidatedStatus {
|
|||||||
Error = 'error',
|
Error = 'error',
|
||||||
Exceed = 'exceed'
|
Exceed = 'exceed'
|
||||||
}
|
}
|
||||||
export type SetValidatedStatus = Dispatch<SetStateAction<ValidatedStatus | undefined>>
|
export type ValidatedStatusState = {
|
||||||
|
status?: ValidatedStatus,
|
||||||
|
message?: string
|
||||||
|
}
|
||||||
|
// export type ValidatedStatusState = ValidatedStatus | undefined | ValidatedError
|
||||||
|
export type SetValidatedStatus = Dispatch<SetStateAction<ValidatedStatusState>>
|
||||||
export type ValidateFn = DebouncedFunc<(token: any, config: ValidateFnConfig) => void>
|
export type ValidateFn = DebouncedFunc<(token: any, config: ValidateFnConfig) => void>
|
||||||
type ValidateTokenReturn = [
|
type ValidateTokenReturn = [
|
||||||
boolean,
|
boolean,
|
||||||
ValidatedStatus | undefined,
|
ValidatedStatusState,
|
||||||
SetValidatedStatus,
|
SetValidatedStatus,
|
||||||
ValidateFn
|
ValidateFn
|
||||||
]
|
]
|
||||||
@ -22,7 +27,7 @@ export type ValidateFnConfig = {
|
|||||||
|
|
||||||
const useValidateToken = (providerName: string): ValidateTokenReturn => {
|
const useValidateToken = (providerName: string): ValidateTokenReturn => {
|
||||||
const [validating, setValidating] = useState(false)
|
const [validating, setValidating] = useState(false)
|
||||||
const [validatedStatus, setValidatedStatus] = useState<ValidatedStatus | undefined>()
|
const [validatedStatus, setValidatedStatus] = useState<ValidatedStatusState>({})
|
||||||
const validate = useCallback(debounce(async (token: string, config: ValidateFnConfig) => {
|
const validate = useCallback(debounce(async (token: string, config: ValidateFnConfig) => {
|
||||||
if (!config.beforeValidating(token)) {
|
if (!config.beforeValidating(token)) {
|
||||||
return false
|
return false
|
||||||
@ -30,19 +35,12 @@ const useValidateToken = (providerName: string): ValidateTokenReturn => {
|
|||||||
setValidating(true)
|
setValidating(true)
|
||||||
try {
|
try {
|
||||||
const res = await validateProviderKey({ url: `/workspaces/current/providers/${providerName}/token-validate`, body: { token } })
|
const res = await validateProviderKey({ url: `/workspaces/current/providers/${providerName}/token-validate`, body: { token } })
|
||||||
setValidatedStatus(res.result === 'success' ? ValidatedStatus.Success : ValidatedStatus.Error)
|
setValidatedStatus(
|
||||||
|
res.result === 'success'
|
||||||
|
? { status: ValidatedStatus.Success }
|
||||||
|
: { status: ValidatedStatus.Error, message: res.error })
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
if (e.status === 400) {
|
setValidatedStatus({ status: ValidatedStatus.Error, message: e.message })
|
||||||
e.json().then(({ code }: any) => {
|
|
||||||
if (code === 'provider_request_failed' && providerName === 'openai') {
|
|
||||||
setValidatedStatus(ValidatedStatus.Exceed)
|
|
||||||
} else {
|
|
||||||
setValidatedStatus(ValidatedStatus.Error)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
setValidatedStatus(ValidatedStatus.Error)
|
|
||||||
}
|
|
||||||
} finally {
|
} finally {
|
||||||
setValidating(false)
|
setValidating(false)
|
||||||
}
|
}
|
||||||
|
@ -140,6 +140,7 @@ const translation = {
|
|||||||
apiKey: "API Key",
|
apiKey: "API Key",
|
||||||
enterYourKey: "Enter your API key here",
|
enterYourKey: "Enter your API key here",
|
||||||
invalidKey: "Invalid OpenAI API key",
|
invalidKey: "Invalid OpenAI API key",
|
||||||
|
validatedError: "Validation failed: ",
|
||||||
validating: "Validating key...",
|
validating: "Validating key...",
|
||||||
saveFailed: "Save api key failed",
|
saveFailed: "Save api key failed",
|
||||||
apiKeyExceedBill: "This API KEY has no quota available, please read",
|
apiKeyExceedBill: "This API KEY has no quota available, please read",
|
||||||
|
@ -141,6 +141,7 @@ const translation = {
|
|||||||
apiKey: "API 密钥",
|
apiKey: "API 密钥",
|
||||||
enterYourKey: "输入你的 API 密钥",
|
enterYourKey: "输入你的 API 密钥",
|
||||||
invalidKey: '无效的 OpenAI API 密钥',
|
invalidKey: '无效的 OpenAI API 密钥',
|
||||||
|
validatedError: "校验失败:",
|
||||||
validating: "验证密钥中...",
|
validating: "验证密钥中...",
|
||||||
saveFailed: "API 密钥保存失败",
|
saveFailed: "API 密钥保存失败",
|
||||||
apiKeyExceedBill: "此 API KEY 已没有可用配额,请阅读",
|
apiKeyExceedBill: "此 API KEY 已没有可用配额,请阅读",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user