chore: 纳入本地 Dify 1.19.0 源码及品牌与免登改动
演示目录只保留可重建的源码。运行密钥、数据卷、依赖和带口令的本地配置不进入仓库。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
60158935f4
commit
6519682885
15831 files changed
+3063428
No files matched your search
+174
@@ -0,0 +1,174 @@
|
||||
'use client'
|
||||
import type { FC } from 'react'
|
||||
import type { PluginDeclaration } from '../../../types'
|
||||
import { Button } from '@langgenius/dify-ui/button'
|
||||
import { RiLoader2Line } from '@remixicon/react'
|
||||
import { useSuspenseQuery } from '@tanstack/react-query'
|
||||
import * as React from 'react'
|
||||
import { useEffect, useMemo } from 'react'
|
||||
import { Trans, useTranslation } from 'react-i18next'
|
||||
import useCheckInstalled from '@/app/components/plugins/install-plugin/hooks/use-check-installed'
|
||||
import { userProfileQueryOptions } from '@/features/account-profile/client'
|
||||
import { uninstallPlugin } from '@/service/plugins'
|
||||
import { useInstallPackageFromLocal, usePluginTaskList } from '@/service/use-plugins'
|
||||
import { isEqualOrLaterThanVersion } from '@/utils/semver'
|
||||
import Card from '../../../card'
|
||||
import { TaskStatus } from '../../../types'
|
||||
import checkTaskStatus from '../../base/check-task-status'
|
||||
import Version from '../../base/version'
|
||||
import { pluginManifestToCardPluginProps } from '../../utils'
|
||||
|
||||
const i18nPrefix = 'installModal'
|
||||
|
||||
type Props = Readonly<{
|
||||
uniqueIdentifier: string
|
||||
payload: PluginDeclaration
|
||||
onCancel: () => void
|
||||
onStartToInstall?: () => void
|
||||
onInstalled: (notRefresh?: boolean) => void
|
||||
onFailed: (message?: string) => void
|
||||
}>
|
||||
|
||||
const Installed: FC<Props> = ({
|
||||
uniqueIdentifier,
|
||||
payload,
|
||||
onCancel,
|
||||
onStartToInstall,
|
||||
onInstalled,
|
||||
onFailed,
|
||||
}) => {
|
||||
const { t } = useTranslation(['common', 'plugin'])
|
||||
const toInstallVersion = payload.version
|
||||
const pluginId = `${payload.author}/${payload.name}`
|
||||
const { installedInfo, isLoading } = useCheckInstalled({
|
||||
pluginIds: [pluginId],
|
||||
enabled: !!pluginId,
|
||||
})
|
||||
const installedInfoPayload = installedInfo?.[pluginId]
|
||||
const installedVersion = installedInfoPayload?.installedVersion
|
||||
const hasInstalled = !!installedVersion
|
||||
|
||||
useEffect(() => {
|
||||
if (hasInstalled && uniqueIdentifier === installedInfoPayload.uniqueIdentifier) onInstalled()
|
||||
}, [hasInstalled])
|
||||
|
||||
const [isInstalling, setIsInstalling] = React.useState(false)
|
||||
const { mutateAsync: installPackageFromLocal } = useInstallPackageFromLocal()
|
||||
|
||||
const { check, stop } = checkTaskStatus()
|
||||
|
||||
const handleCancel = () => {
|
||||
stop()
|
||||
onCancel()
|
||||
}
|
||||
|
||||
const { handleInstallTaskStart } = usePluginTaskList(payload.category)
|
||||
const handleInstall = async () => {
|
||||
if (isInstalling) return
|
||||
setIsInstalling(true)
|
||||
onStartToInstall?.()
|
||||
|
||||
try {
|
||||
if (hasInstalled) {
|
||||
await uninstallPlugin(installedInfoPayload.installedId, { preserveCredentials: true })
|
||||
}
|
||||
|
||||
const response = await installPackageFromLocal(uniqueIdentifier)
|
||||
const { all_installed, task_id } = response
|
||||
handleInstallTaskStart(response)
|
||||
const taskId = task_id
|
||||
const isInstalled = all_installed
|
||||
|
||||
if (isInstalled) {
|
||||
onInstalled()
|
||||
return
|
||||
}
|
||||
const { status, error } = await check({
|
||||
taskId,
|
||||
pluginUniqueIdentifier: uniqueIdentifier,
|
||||
})
|
||||
if (status === TaskStatus.failed) {
|
||||
onFailed(error)
|
||||
return
|
||||
}
|
||||
onInstalled(true)
|
||||
} catch (e) {
|
||||
if (typeof e === 'string') {
|
||||
onFailed(e)
|
||||
return
|
||||
}
|
||||
onFailed()
|
||||
}
|
||||
}
|
||||
|
||||
const { data: currentVersion } = useSuspenseQuery({
|
||||
...userProfileQueryOptions(),
|
||||
select: (data) => data.meta.currentVersion ?? '',
|
||||
})
|
||||
const isDifyVersionCompatible = useMemo(() => {
|
||||
if (!currentVersion) return true
|
||||
return isEqualOrLaterThanVersion(currentVersion, payload.meta.minimum_dify_version ?? '0.0.0')
|
||||
}, [currentVersion, payload.meta.minimum_dify_version])
|
||||
|
||||
return (
|
||||
<>
|
||||
<div className="flex flex-col items-start justify-center gap-2 self-stretch px-6 py-3">
|
||||
<div className="system-md-regular text-text-secondary">
|
||||
<p>{t(($) => $[`${i18nPrefix}.readyToInstall`], { ns: 'plugin' })}</p>
|
||||
<p>
|
||||
<Trans
|
||||
i18nKey={($) => $[`${i18nPrefix}.fromTrustSource`]}
|
||||
ns="plugin"
|
||||
components={{ trustSource: <span className="system-md-semibold" /> }}
|
||||
/>
|
||||
</p>
|
||||
{!isDifyVersionCompatible && (
|
||||
<p className="flex items-center gap-1 system-md-regular text-text-warning">
|
||||
{t(($) => $.difyVersionNotCompatible, {
|
||||
ns: 'plugin',
|
||||
minimalDifyVersion: payload.meta.minimum_dify_version ?? '0.0.0',
|
||||
})}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
<div className="flex flex-wrap content-start items-start gap-1 self-stretch rounded-2xl bg-background-section-burn p-2">
|
||||
<Card
|
||||
className="w-full"
|
||||
payload={pluginManifestToCardPluginProps(payload)}
|
||||
titleLeft={
|
||||
!isLoading && (
|
||||
<Version
|
||||
hasInstalled={hasInstalled}
|
||||
installedVersion={installedVersion}
|
||||
toInstallVersion={toInstallVersion}
|
||||
/>
|
||||
)
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
{/* Action Buttons */}
|
||||
<div className="flex items-center justify-end gap-2 self-stretch p-6 pt-5">
|
||||
{!isInstalling && (
|
||||
<Button variant="secondary" className="min-w-18" onClick={handleCancel}>
|
||||
{t(($) => $['operation.cancel'], { ns: 'common' })}
|
||||
</Button>
|
||||
)}
|
||||
<Button
|
||||
variant="primary"
|
||||
className="flex min-w-18"
|
||||
disabled={isInstalling || isLoading}
|
||||
onClick={handleInstall}
|
||||
>
|
||||
{isInstalling && <RiLoader2Line className="size-4 animate-spin-slow" />}
|
||||
<span>
|
||||
{t(($) => $[`${i18nPrefix}.${isInstalling ? 'installing' : 'install'}`], {
|
||||
ns: 'plugin',
|
||||
})}
|
||||
</span>
|
||||
</Button>
|
||||
</div>
|
||||
</>
|
||||
)
|
||||
}
|
||||
export default React.memo(Installed)
|
||||
+124
@@ -0,0 +1,124 @@
|
||||
'use client'
|
||||
import type { FC } from 'react'
|
||||
import type { Dependency, Plugin, PluginDeclaration } from '../../../types'
|
||||
import { Button } from '@langgenius/dify-ui/button'
|
||||
import { ORPCError } from '@orpc/client'
|
||||
import * as React from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { uploadFile } from '@/service/plugins'
|
||||
import Card from '../../../card'
|
||||
|
||||
const i18nPrefix = 'installModal'
|
||||
|
||||
type PackageUploadResponse = {
|
||||
unique_identifier: string
|
||||
manifest: PluginDeclaration
|
||||
}
|
||||
|
||||
function isObject(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === 'object' && value !== null
|
||||
}
|
||||
|
||||
function isPackageUploadResponse(value: unknown): value is PackageUploadResponse {
|
||||
if (!isObject(value)) return false
|
||||
|
||||
return typeof value.unique_identifier === 'string' && isObject(value.manifest)
|
||||
}
|
||||
|
||||
type Props = Readonly<{
|
||||
isBundle: boolean
|
||||
file: File
|
||||
onCancel: () => void
|
||||
onPackageUploaded: (result: { uniqueIdentifier: string; manifest: PluginDeclaration }) => void
|
||||
onBundleUploaded: (result: Dependency[]) => void
|
||||
onFailed: (errorMsg: string) => void
|
||||
}>
|
||||
|
||||
const Uploading: FC<Props> = ({
|
||||
isBundle,
|
||||
file,
|
||||
onCancel,
|
||||
onPackageUploaded,
|
||||
onBundleUploaded,
|
||||
onFailed,
|
||||
}) => {
|
||||
const { t } = useTranslation(['common', 'plugin'])
|
||||
const fileName = file.name
|
||||
const handleUploadedResponse = React.useCallback(
|
||||
(response: unknown) => {
|
||||
if (isBundle) {
|
||||
if (Array.isArray(response)) {
|
||||
onBundleUploaded(response)
|
||||
return
|
||||
}
|
||||
onFailed(t(($) => $[`${i18nPrefix}.uploadFailed`], { ns: 'plugin' }))
|
||||
return
|
||||
}
|
||||
|
||||
if (!isPackageUploadResponse(response)) {
|
||||
onFailed(t(($) => $[`${i18nPrefix}.uploadFailed`], { ns: 'plugin' }))
|
||||
return
|
||||
}
|
||||
|
||||
onPackageUploaded({
|
||||
uniqueIdentifier: response.unique_identifier,
|
||||
manifest: response.manifest,
|
||||
})
|
||||
},
|
||||
[isBundle, onBundleUploaded, onFailed, onPackageUploaded, t],
|
||||
)
|
||||
|
||||
const handleUpload = React.useCallback(async () => {
|
||||
try {
|
||||
handleUploadedResponse(await uploadFile(file, isBundle))
|
||||
} catch (error: unknown) {
|
||||
// Only server answers carry a user-facing message; transport errors use the localized fallback.
|
||||
const message = error instanceof ORPCError ? error.message : undefined
|
||||
if (message) {
|
||||
onFailed(message)
|
||||
return
|
||||
}
|
||||
onFailed(t(($) => $[`${i18nPrefix}.uploadFailed`], { ns: 'plugin' }))
|
||||
}
|
||||
}, [file, handleUploadedResponse, isBundle, onFailed, t])
|
||||
|
||||
React.useEffect(() => {
|
||||
handleUpload()
|
||||
}, [handleUpload])
|
||||
return (
|
||||
<>
|
||||
<div className="flex flex-col items-start justify-center gap-2 self-stretch px-6 py-3">
|
||||
<div className="flex items-center gap-1 self-stretch">
|
||||
<span className="i-ri-loader-2-line size-4 animate-spin-slow text-text-accent" />
|
||||
<div className="system-md-regular text-text-secondary">
|
||||
{t(($) => $[`${i18nPrefix}.uploadingPackage`], {
|
||||
ns: 'plugin',
|
||||
packageName: fileName,
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex flex-wrap content-start items-start gap-1 self-stretch rounded-2xl bg-background-section-burn p-2">
|
||||
<Card
|
||||
className="w-full"
|
||||
payload={{ name: fileName } as Plugin}
|
||||
isLoading
|
||||
loadingFileName={fileName}
|
||||
installed={false}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Action Buttons */}
|
||||
<div className="flex items-center justify-end gap-2 self-stretch p-6 pt-5">
|
||||
<Button variant="secondary" className="min-w-18" onClick={onCancel}>
|
||||
{t(($) => $['operation.cancel'], { ns: 'common' })}
|
||||
</Button>
|
||||
<Button variant="primary" className="min-w-18" disabled>
|
||||
{t(($) => $[`${i18nPrefix}.install`], { ns: 'plugin' })}
|
||||
</Button>
|
||||
</div>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
export default React.memo(Uploading)
|
||||
Reference in new issue
Block a user