feat: can upload and parse bundle

pull/12372/head
Joel 2 years ago
parent 972eaa5948
commit c5c06c18f1

@ -47,7 +47,7 @@ const PluginList = () => {
{ {
type: 'marketplace', type: 'marketplace',
value: { value: {
plugin_unique_identifier: 'langgenius/openai:0.0.1@f88fdb98d104466db16a425bfe3af8c1bcad45047a40fb802d98a989ac57a5a3', plugin_unique_identifier: 'langgenius/openai:0.0.2@7baee9635a07573ea192621ebfdacb39db466fa691e75255beaf48bf41d44375',
}, },
}, },
]} /> ]} />

@ -3,9 +3,8 @@ import type { FC } from 'react'
import Modal from '@/app/components/base/modal' import Modal from '@/app/components/base/modal'
import React, { useCallback, useState } from 'react' import React, { useCallback, useState } from 'react'
import { InstallStep } from '../../types' import { InstallStep } from '../../types'
import type { Dependency, InstallStatusResponse, Plugin } from '../../types' import type { Dependency } from '../../types'
import Install from './steps/install' import ReadyToInstall from './ready-to-install'
import Installed from './steps/installed'
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
const i18nPrefix = 'plugin.installModal' const i18nPrefix = 'plugin.installModal'
@ -30,8 +29,7 @@ const InstallBundle: FC<Props> = ({
}) => { }) => {
const { t } = useTranslation() const { t } = useTranslation()
const [step, setStep] = useState<InstallStep>(installType === InstallType.fromMarketplace ? InstallStep.readyToInstall : InstallStep.uploading) const [step, setStep] = useState<InstallStep>(installType === InstallType.fromMarketplace ? InstallStep.readyToInstall : InstallStep.uploading)
const [installedPlugins, setInstalledPlugins] = useState<Plugin[]>([])
const [installStatus, setInstallStatus] = useState<InstallStatusResponse[]>([])
const getTitle = useCallback(() => { const getTitle = useCallback(() => {
if (step === InstallStep.uploadFailed) if (step === InstallStep.uploadFailed)
return t(`${i18nPrefix}.uploadFailed`) return t(`${i18nPrefix}.uploadFailed`)
@ -43,12 +41,6 @@ const InstallBundle: FC<Props> = ({
return t(`${i18nPrefix}.installPlugin`) return t(`${i18nPrefix}.installPlugin`)
}, [step, t]) }, [step, t])
const handleInstalled = useCallback((plugins: Plugin[], installStatus: InstallStatusResponse[]) => {
setInstallStatus(installStatus)
setInstalledPlugins(plugins)
setStep(InstallStep.installed)
}, [])
return ( return (
<Modal <Modal
isShow={true} isShow={true}
@ -61,20 +53,12 @@ const InstallBundle: FC<Props> = ({
{getTitle()} {getTitle()}
</div> </div>
</div> </div>
{step === InstallStep.readyToInstall && ( <ReadyToInstall
<Install step={step}
fromDSLPayload={fromDSLPayload} onStepChange={setStep}
onCancel={onClose} dependencies={fromDSLPayload}
onInstalled={handleInstalled} onClose={onClose}
/>
)}
{step === InstallStep.installed && (
<Installed
list={installedPlugins}
installStatus={installStatus}
onCancel={onClose}
/> />
)}
</Modal> </Modal>
) )
} }

@ -0,0 +1,48 @@
'use client'
import type { FC } from 'react'
import React, { useCallback, useState } from 'react'
import { InstallStep } from '../../types'
import Install from './steps/install'
import Installed from './steps/installed'
import type { Dependency, InstallStatusResponse, Plugin } from '../../types'
type Props = {
step: InstallStep
onStepChange: (step: InstallStep) => void,
dependencies: Dependency[]
onClose: () => void
}
const ReadyToInstall: FC<Props> = ({
step,
onStepChange,
dependencies,
onClose,
}) => {
const [installedPlugins, setInstalledPlugins] = useState<Plugin[]>([])
const [installStatus, setInstallStatus] = useState<InstallStatusResponse[]>([])
const handleInstalled = useCallback((plugins: Plugin[], installStatus: InstallStatusResponse[]) => {
setInstallStatus(installStatus)
setInstalledPlugins(plugins)
onStepChange(InstallStep.installed)
}, [onStepChange])
return (
<>
{step === InstallStep.readyToInstall && (
<Install
fromDSLPayload={dependencies}
onCancel={onClose}
onInstalled={handleInstalled}
/>
)}
{step === InstallStep.installed && (
<Installed
list={installedPlugins}
installStatus={installStatus}
onCancel={onClose}
/>
)}
</>
)
}
export default React.memo(ReadyToInstall)

@ -1,7 +1,7 @@
'use client' 'use client'
import type { FC } from 'react' import type { FC } from 'react'
import React from 'react' import React from 'react'
import type { Plugin } from '../../../types' import type { InstallStatusResponse, Plugin } from '../../../types'
import Card from '@/app/components/plugins/card' import Card from '@/app/components/plugins/card'
import Button from '@/app/components/base/button' import Button from '@/app/components/base/button'
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
@ -11,7 +11,7 @@ import { MARKETPLACE_API_PREFIX } from '@/config'
type Props = { type Props = {
list: Plugin[] list: Plugin[]
installStatus: { success: boolean, isFromMarketPlace: boolean }[] installStatus: InstallStatusResponse[]
onCancel: () => void onCancel: () => void
} }

@ -2,14 +2,13 @@
import React, { useCallback, useState } from 'react' import React, { useCallback, useState } from 'react'
import Modal from '@/app/components/base/modal' import Modal from '@/app/components/base/modal'
import type { PluginDeclaration } from '../../types' import type { Dependency, PluginDeclaration } from '../../types'
import { InstallStep } from '../../types' import { InstallStep } from '../../types'
import Uploading from './steps/uploading' import Uploading from './steps/uploading'
import Install from './steps/install'
import Installed from '../base/installed'
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
import useGetIcon from '@/app/components/plugins/install-plugin/base/use-get-icon' import useGetIcon from '@/app/components/plugins/install-plugin/base/use-get-icon'
import { useInvalidateInstalledPluginList } from '@/service/use-plugins' import ReadyToInstallPackage from './ready-to-install'
import ReadyToInstallBundle from '../install-bundle/ready-to-install'
const i18nPrefix = 'plugin.installModal' const i18nPrefix = 'plugin.installModal'
@ -29,7 +28,8 @@ const InstallFromLocalPackage: React.FC<InstallFromLocalPackageProps> = ({
const [uniqueIdentifier, setUniqueIdentifier] = useState<string | null>(null) const [uniqueIdentifier, setUniqueIdentifier] = useState<string | null>(null)
const [manifest, setManifest] = useState<PluginDeclaration | null>(null) const [manifest, setManifest] = useState<PluginDeclaration | null>(null)
const [errorMsg, setErrorMsg] = useState<string | null>(null) const [errorMsg, setErrorMsg] = useState<string | null>(null)
const invalidateInstalledPluginList = useInvalidateInstalledPluginList() const isBundle = file.name.endsWith('.bundle')
const [dependencies, setDependencies] = useState<Dependency[]>([])
const getTitle = useCallback(() => { const getTitle = useCallback(() => {
if (step === InstallStep.uploadFailed) if (step === InstallStep.uploadFailed)
@ -44,7 +44,7 @@ const InstallFromLocalPackage: React.FC<InstallFromLocalPackageProps> = ({
const { getIconUrl } = useGetIcon() const { getIconUrl } = useGetIcon()
const handleUploaded = useCallback(async (result: { const handlePackageUploaded = useCallback(async (result: {
uniqueIdentifier: string uniqueIdentifier: string
manifest: PluginDeclaration manifest: PluginDeclaration
}) => { }) => {
@ -61,20 +61,14 @@ const InstallFromLocalPackage: React.FC<InstallFromLocalPackageProps> = ({
setStep(InstallStep.readyToInstall) setStep(InstallStep.readyToInstall)
}, [getIconUrl]) }, [getIconUrl])
const handleUploadFail = useCallback((errorMsg: string) => { const handleBundleUploaded = useCallback((result: Dependency[]) => {
setErrorMsg(errorMsg) setDependencies(result)
setStep(InstallStep.uploadFailed) setStep(InstallStep.readyToInstall)
}, []) }, [])
const handleInstalled = useCallback(() => { const handleUploadFail = useCallback((errorMsg: string) => {
invalidateInstalledPluginList()
setStep(InstallStep.installed)
}, [invalidateInstalledPluginList])
const handleFailed = useCallback((errorMsg?: string) => {
setStep(InstallStep.installFailed)
if (errorMsg)
setErrorMsg(errorMsg) setErrorMsg(errorMsg)
setStep(InstallStep.uploadFailed)
}, []) }, [])
return ( return (
@ -91,33 +85,32 @@ const InstallFromLocalPackage: React.FC<InstallFromLocalPackageProps> = ({
</div> </div>
{step === InstallStep.uploading && ( {step === InstallStep.uploading && (
<Uploading <Uploading
isBundle={isBundle}
file={file} file={file}
onCancel={onClose} onCancel={onClose}
onUploaded={handleUploaded} onPackageUploaded={handlePackageUploaded}
onBundleUploaded={handleBundleUploaded}
onFailed={handleUploadFail} onFailed={handleUploadFail}
/> />
)} )}
{ {isBundle ? (
step === InstallStep.readyToInstall && ( <ReadyToInstallBundle
<Install step={step}
uniqueIdentifier={uniqueIdentifier!} onStepChange={setStep}
payload={manifest!} onClose={onClose}
onCancel={onClose} dependencies={dependencies}
onInstalled={handleInstalled}
onFailed={handleFailed}
/> />
) ) : (
} <ReadyToInstallPackage
{ step={step}
([InstallStep.uploadFailed, InstallStep.installed, InstallStep.installFailed].includes(step)) && ( onStepChange={setStep}
<Installed onClose={onClose}
payload={manifest} uniqueIdentifier={uniqueIdentifier}
isFailed={[InstallStep.uploadFailed, InstallStep.installFailed].includes(step)} manifest={manifest}
errMsg={errorMsg} errorMsg={errorMsg}
onCancel={onClose} onError={setErrorMsg}
/> />
) )}
}
</Modal> </Modal>
) )
} }

@ -0,0 +1,68 @@
'use client'
import type { FC } from 'react'
import React, { useCallback } from 'react'
import type { PluginDeclaration } from '../../types'
import { InstallStep } from '../../types'
import Install from './steps/install'
import Installed from '../base/installed'
import { useInvalidateInstalledPluginList } from '@/service/use-plugins'
type Props = {
step: InstallStep
onStepChange: (step: InstallStep) => void,
onClose: () => void
uniqueIdentifier: string | null,
manifest: PluginDeclaration | null,
errorMsg: string | null,
onError: (errorMsg: string) => void,
}
const ReadyToInstall: FC<Props> = ({
step,
onStepChange,
onClose,
uniqueIdentifier,
manifest,
errorMsg,
onError,
}) => {
const invalidateInstalledPluginList = useInvalidateInstalledPluginList()
const handleInstalled = useCallback(() => {
invalidateInstalledPluginList()
onStepChange(InstallStep.installed)
}, [invalidateInstalledPluginList, onStepChange])
const handleFailed = useCallback((errorMsg?: string) => {
onStepChange(InstallStep.installFailed)
if (errorMsg)
onError(errorMsg)
}, [onError, onStepChange])
return (
<>
{
step === InstallStep.readyToInstall && (
<Install
uniqueIdentifier={uniqueIdentifier!}
payload={manifest!}
onCancel={onClose}
onInstalled={handleInstalled}
onFailed={handleFailed}
/>
)
}
{
([InstallStep.uploadFailed, InstallStep.installed, InstallStep.installFailed].includes(step)) && (
<Installed
payload={manifest}
isFailed={[InstallStep.uploadFailed, InstallStep.installFailed].includes(step)}
errMsg={errorMsg}
onCancel={onClose}
/>
)
}
</>
)
}
export default React.memo(ReadyToInstall)

@ -3,34 +3,37 @@ import type { FC } from 'react'
import React from 'react' import React from 'react'
import { RiLoader2Line } from '@remixicon/react' import { RiLoader2Line } from '@remixicon/react'
import Card from '../../../card' import Card from '../../../card'
import type { PluginDeclaration } from '../../../types' import type { Dependency, PluginDeclaration } from '../../../types'
import Button from '@/app/components/base/button' import Button from '@/app/components/base/button'
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
import { uploadPackageFile } from '@/service/plugins' import { uploadFile } from '@/service/plugins'
const i18nPrefix = 'plugin.installModal' const i18nPrefix = 'plugin.installModal'
type Props = { type Props = {
isBundle: boolean
file: File file: File
onCancel: () => void onCancel: () => void
onUploaded: (result: { onPackageUploaded: (result: {
uniqueIdentifier: string uniqueIdentifier: string
manifest: PluginDeclaration manifest: PluginDeclaration
}) => void }) => void
onBundleUploaded: (result: Dependency[]) => void
onFailed: (errorMsg: string) => void onFailed: (errorMsg: string) => void
} }
const Uploading: FC<Props> = ({ const Uploading: FC<Props> = ({
isBundle,
file, file,
onCancel, onCancel,
onUploaded, onPackageUploaded,
onBundleUploaded,
onFailed, onFailed,
}) => { }) => {
const { t } = useTranslation() const { t } = useTranslation()
const fileName = file.name const fileName = file.name
const handleUpload = async () => { const handleUpload = async () => {
try { try {
const res = await uploadPackageFile(file) await uploadFile(file, isBundle)
onUploaded(res)
} }
catch (e: any) { catch (e: any) {
if (e.response?.message) { if (e.response?.message) {
@ -38,7 +41,11 @@ const Uploading: FC<Props> = ({
} }
else { // Why it would into this branch? else { // Why it would into this branch?
const res = e.response const res = e.response
onUploaded({ if (isBundle) {
onBundleUploaded(res)
return
}
onPackageUploaded({
uniqueIdentifier: res.unique_identifier, uniqueIdentifier: res.unique_identifier,
manifest: res.manifest, manifest: res.manifest,
}) })

@ -10,6 +10,7 @@ import { useSelector as useAppContextSelector } from '@/context/app-context'
import Line from '../../marketplace/empty/line' import Line from '../../marketplace/empty/line'
import { useInstalledPluginList, useInvalidateInstalledPluginList } from '@/service/use-plugins' import { useInstalledPluginList, useInvalidateInstalledPluginList } from '@/service/use-plugins'
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
import { SUPPORT_INSTALL_LOCAL_FILE_EXTENSIONS } from '@/config'
const Empty = () => { const Empty = () => {
const { t } = useTranslation() const { t } = useTranslation()
@ -66,7 +67,7 @@ const Empty = () => {
ref={fileInputRef} ref={fileInputRef}
style={{ display: 'none' }} style={{ display: 'none' }}
onChange={handleFileChange} onChange={handleFileChange}
accept='.difypkg' accept={SUPPORT_INSTALL_LOCAL_FILE_EXTENSIONS}
/> />
<div className='w-full flex flex-col gap-y-1'> <div className='w-full flex flex-col gap-y-1'>
{[ {[

@ -32,6 +32,7 @@ import type { PluginDeclaration, PluginManifestInMarket } from '../types'
import { sleep } from '@/utils' import { sleep } from '@/utils'
import { fetchManifestFromMarketPlace } from '@/service/plugins' import { fetchManifestFromMarketPlace } from '@/service/plugins'
import { marketplaceApiPrefix } from '@/config' import { marketplaceApiPrefix } from '@/config'
import { SUPPORT_INSTALL_LOCAL_FILE_EXTENSIONS } from '@/config'
const PACKAGE_IDS_KEY = 'package-ids' const PACKAGE_IDS_KEY = 'package-ids'
@ -186,7 +187,7 @@ const PluginPage = ({
className="hidden" className="hidden"
type="file" type="file"
id="fileUploader" id="fileUploader"
accept='.difypkg' accept={SUPPORT_INSTALL_LOCAL_FILE_EXTENSIONS}
onChange={fileChangeHandle ?? (() => { })} onChange={fileChangeHandle ?? (() => { })}
/> />
</> </>

@ -17,6 +17,7 @@ import {
import { useSelector as useAppContextSelector } from '@/context/app-context' import { useSelector as useAppContextSelector } from '@/context/app-context'
import { useInvalidateInstalledPluginList } from '@/service/use-plugins' import { useInvalidateInstalledPluginList } from '@/service/use-plugins'
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
import { SUPPORT_INSTALL_LOCAL_FILE_EXTENSIONS } from '@/config'
type Props = { type Props = {
onSwitchToMarketplaceTab: () => void onSwitchToMarketplaceTab: () => void
@ -81,7 +82,7 @@ const InstallPluginDropdown = ({
ref={fileInputRef} ref={fileInputRef}
style={{ display: 'none' }} style={{ display: 'none' }}
onChange={handleFileChange} onChange={handleFileChange}
accept='.difypkg' accept={SUPPORT_INSTALL_LOCAL_FILE_EXTENSIONS}
/> />
<div className='w-full'> <div className='w-full'>
{[ {[

@ -273,3 +273,5 @@ export const TEXT_GENERATION_TIMEOUT_MS = textGenerationTimeoutMs
export const DISABLE_UPLOAD_IMAGE_AS_ICON = process.env.NEXT_PUBLIC_DISABLE_UPLOAD_IMAGE_AS_ICON === 'true' export const DISABLE_UPLOAD_IMAGE_AS_ICON = process.env.NEXT_PUBLIC_DISABLE_UPLOAD_IMAGE_AS_ICON === 'true'
export const GITHUB_ACCESS_TOKEN = process.env.NEXT_PUBLIC_GITHUB_ACCESS_TOKEN || globalThis.document?.body?.getAttribute('data-public-github-access-token') || '' export const GITHUB_ACCESS_TOKEN = process.env.NEXT_PUBLIC_GITHUB_ACCESS_TOKEN || globalThis.document?.body?.getAttribute('data-public-github-access-token') || ''
export const SUPPORT_INSTALL_LOCAL_FILE_EXTENSIONS = '.difypkg,.bundle'

@ -16,13 +16,13 @@ import type {
MarketplaceCollectionsResponse, MarketplaceCollectionsResponse,
} from '@/app/components/plugins/marketplace/types' } from '@/app/components/plugins/marketplace/types'
export const uploadPackageFile = async (file: File) => { export const uploadFile = async (file: File, isBundle: boolean) => {
const formData = new FormData() const formData = new FormData()
formData.append('pkg', file) formData.append(isBundle ? 'bundle' : 'pkg', file)
return upload({ return upload({
xhr: new XMLHttpRequest(), xhr: new XMLHttpRequest(),
data: formData, data: formData,
}, false, '/workspaces/current/plugin/upload/pkg') }, false, `/workspaces/current/plugin/upload/${isBundle ? 'bundle' : 'pkg'}`)
} }
export const updateFromMarketPlace = async (body: Record<string, string>) => { export const updateFromMarketPlace = async (body: Record<string, string>) => {

Loading…
Cancel
Save