Merge branch 'feat/plugins' of github.com:langgenius/dify into feat/plugins
commit
dbc10425c8
@ -1,105 +0,0 @@
|
|||||||
import { PluginSource, PluginType } from '../types'
|
|
||||||
|
|
||||||
export const toolNotion = {
|
|
||||||
id: 'dlfajkgjdga-dfjalksjfglkds-dfjakld',
|
|
||||||
created_at: '2024-10-16 16:05:33',
|
|
||||||
updated_at: '2024-10-16 16:05:33',
|
|
||||||
name: 'notion page search',
|
|
||||||
plugin_id: 'Notion/notion-page-search',
|
|
||||||
plugin_unique_identifier: 'Notion/notion-page-search:1.2.0@fldsjflkdsajfldsakajfkls',
|
|
||||||
declaration: {
|
|
||||||
version: '1.2.0',
|
|
||||||
author: 'Notion',
|
|
||||||
name: 'notion page search',
|
|
||||||
category: PluginType.tool,
|
|
||||||
icon: 'https://via.placeholder.com/150',
|
|
||||||
label: {
|
|
||||||
'en-US': 'Notion Page Search',
|
|
||||||
'zh-Hans': 'Notion 页面搜索',
|
|
||||||
},
|
|
||||||
description: {
|
|
||||||
'en-US': 'Description: Search Notion pages and open visited ones faster. No admin access required.More and more info...More and more info...More and more info...',
|
|
||||||
'zh-Hans': '搜索 Notion 页面并更快地打开已访问的页面。无需管理员访问权限。More and more info...More and more info...More and more info...',
|
|
||||||
},
|
|
||||||
created_at: '2024-10-16 16:05:33',
|
|
||||||
resource: {},
|
|
||||||
plugins: {},
|
|
||||||
endpoint: {
|
|
||||||
settings: [
|
|
||||||
{
|
|
||||||
type: 'secret-input',
|
|
||||||
name: 'api-key',
|
|
||||||
required: true,
|
|
||||||
default: null,
|
|
||||||
options: null,
|
|
||||||
label: {
|
|
||||||
en_US: 'API-key',
|
|
||||||
zh_Hans: 'API-key',
|
|
||||||
},
|
|
||||||
help: null,
|
|
||||||
url: null,
|
|
||||||
placeholder: {
|
|
||||||
en_US: 'Please input your API key',
|
|
||||||
zh_Hans: '请输入你的 API key',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
endpoints: [
|
|
||||||
{ path: '/duck/<app_id>', method: 'GET' },
|
|
||||||
{ path: '/neko', method: 'GET' },
|
|
||||||
],
|
|
||||||
},
|
|
||||||
tool: null, // TODO
|
|
||||||
verified: true,
|
|
||||||
},
|
|
||||||
installation_id: 'jflkdsjoewingljlsadjgoijg-dkfjldajglkajglask-dlfkajdg',
|
|
||||||
tenant_id: 'jflkdsjoewingljlsadjgoijg',
|
|
||||||
endpoints_setups: 2,
|
|
||||||
endpoints_active: 1,
|
|
||||||
version: '1.2.0',
|
|
||||||
source: PluginSource.marketplace,
|
|
||||||
meta: null,
|
|
||||||
}
|
|
||||||
|
|
||||||
export const toolNotionEndpoints = [
|
|
||||||
{
|
|
||||||
id: 'dlfajkgjdga-dfjalksjfglkds-dfjakld',
|
|
||||||
created_at: '2024-10-16 16:05:33',
|
|
||||||
updated_at: '2024-10-16 16:05:33',
|
|
||||||
settings: {
|
|
||||||
'api-key': '*******',
|
|
||||||
},
|
|
||||||
tenant_id: 'jflkdsjoewingljlsadjgoijg',
|
|
||||||
plugin_id: 'Notion/notion-page-search',
|
|
||||||
expired_at: '2024-10-16 16:05:33',
|
|
||||||
declaration: {
|
|
||||||
settings: [
|
|
||||||
{
|
|
||||||
type: 'secret-input',
|
|
||||||
name: 'api-key',
|
|
||||||
required: true,
|
|
||||||
default: null,
|
|
||||||
options: null,
|
|
||||||
label: {
|
|
||||||
en_US: 'API-key',
|
|
||||||
zh_Hans: 'API-key',
|
|
||||||
},
|
|
||||||
help: null,
|
|
||||||
url: null,
|
|
||||||
placeholder: {
|
|
||||||
en_US: 'Please input your API key',
|
|
||||||
zh_Hans: '请输入你的 API key',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
endpoints: [
|
|
||||||
{ path: '/duck/<app_id>', method: 'GET' },
|
|
||||||
{ path: '/neko', method: 'GET' },
|
|
||||||
],
|
|
||||||
},
|
|
||||||
name: 'default',
|
|
||||||
enabled: true,
|
|
||||||
url: 'http://localhost:5002/e/45rj9V4TRxAjL0I2wXRZgZdXjdHEKBh8',
|
|
||||||
hook_id: '45rj9V4TRxAjL0I2wXRZgZdXjdHEKBh8',
|
|
||||||
},
|
|
||||||
]
|
|
||||||
@ -0,0 +1,21 @@
|
|||||||
|
import { FormTypeEnum } from '@/app/components/header/account-setting/model-provider-page/declarations'
|
||||||
|
|
||||||
|
export const NAME_FIELD = {
|
||||||
|
type: FormTypeEnum.textInput,
|
||||||
|
name: 'name',
|
||||||
|
label: {
|
||||||
|
en_US: 'Endpoint Name',
|
||||||
|
zh_Hans: '端点名称',
|
||||||
|
ja_JP: 'エンドポイント名',
|
||||||
|
pt_BR: 'Nome do ponto final',
|
||||||
|
},
|
||||||
|
placeholder: {
|
||||||
|
en_US: 'Endpoint Name',
|
||||||
|
zh_Hans: '端点名称',
|
||||||
|
ja_JP: 'エンドポイント名',
|
||||||
|
pt_BR: 'Nome do ponto final',
|
||||||
|
},
|
||||||
|
required: true,
|
||||||
|
default: '',
|
||||||
|
help: null,
|
||||||
|
}
|
||||||
@ -1,10 +0,0 @@
|
|||||||
const translation = {
|
|
||||||
plugins: {
|
|
||||||
title: 'Plugins',
|
|
||||||
},
|
|
||||||
discover: {
|
|
||||||
title: 'Explore Marketplace',
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
export default translation
|
|
||||||
@ -1,10 +0,0 @@
|
|||||||
const translation = {
|
|
||||||
plugins: {
|
|
||||||
title: '插件',
|
|
||||||
},
|
|
||||||
discover: {
|
|
||||||
title: '探索市场',
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
export default translation
|
|
||||||
@ -0,0 +1,187 @@
|
|||||||
|
import type { AfterResponseHook, BeforeErrorHook, BeforeRequestHook, Hooks } from 'ky'
|
||||||
|
import ky from 'ky'
|
||||||
|
import type { IOtherOptions } from './base'
|
||||||
|
import Toast from '@/app/components/base/toast'
|
||||||
|
import { API_PREFIX, MARKETPLACE_API_PREFIX, PUBLIC_API_PREFIX } from '@/config'
|
||||||
|
|
||||||
|
const TIME_OUT = 100000
|
||||||
|
|
||||||
|
export const ContentType = {
|
||||||
|
json: 'application/json',
|
||||||
|
stream: 'text/event-stream',
|
||||||
|
audio: 'audio/mpeg',
|
||||||
|
form: 'application/x-www-form-urlencoded; charset=UTF-8',
|
||||||
|
download: 'application/octet-stream', // for download
|
||||||
|
upload: 'multipart/form-data', // for upload
|
||||||
|
}
|
||||||
|
|
||||||
|
export type FetchOptionType = Omit<RequestInit, 'body'> & {
|
||||||
|
params?: Record<string, any>
|
||||||
|
body?: BodyInit | Record<string, any> | null
|
||||||
|
}
|
||||||
|
|
||||||
|
const afterResponse204: AfterResponseHook = async (_request, _options, response) => {
|
||||||
|
if (response.status === 204) return Response.json({ result: 'success' })
|
||||||
|
}
|
||||||
|
|
||||||
|
export type ResponseError = {
|
||||||
|
code: string
|
||||||
|
message: string
|
||||||
|
status: number
|
||||||
|
}
|
||||||
|
|
||||||
|
const afterResponseErrorCode = (otherOptions: IOtherOptions): AfterResponseHook => {
|
||||||
|
return async (_request, _options, response) => {
|
||||||
|
if (!/^(2|3)\d{2}$/.test(String(response.status))) {
|
||||||
|
const bodyJson = response.json() as Promise<ResponseError>
|
||||||
|
switch (response.status) {
|
||||||
|
case 401:
|
||||||
|
return Promise.reject(response)
|
||||||
|
case 403:
|
||||||
|
bodyJson.then((data: ResponseError) => {
|
||||||
|
if (!otherOptions.silent)
|
||||||
|
Toast.notify({ type: 'error', message: data.message })
|
||||||
|
if (data.code === 'already_setup')
|
||||||
|
globalThis.location.href = `${globalThis.location.origin}/signin`
|
||||||
|
})
|
||||||
|
break
|
||||||
|
// fall through
|
||||||
|
default:
|
||||||
|
bodyJson.then((data: ResponseError) => {
|
||||||
|
if (!otherOptions.silent)
|
||||||
|
Toast.notify({ type: 'error', message: data.message })
|
||||||
|
})
|
||||||
|
}
|
||||||
|
throw response
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const beforeErrorToast = (otherOptions: IOtherOptions): BeforeErrorHook => {
|
||||||
|
return (error) => {
|
||||||
|
if (!otherOptions.silent)
|
||||||
|
Toast.notify({ type: 'error', message: error.message })
|
||||||
|
return error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const getPublicToken = () => {
|
||||||
|
let token = ''
|
||||||
|
const sharedToken = globalThis.location.pathname.split('/').slice(-1)[0]
|
||||||
|
const accessToken = localStorage.getItem('token') || JSON.stringify({ [sharedToken]: '' })
|
||||||
|
let accessTokenJson = { [sharedToken]: '' }
|
||||||
|
try {
|
||||||
|
accessTokenJson = JSON.parse(accessToken)
|
||||||
|
}
|
||||||
|
catch {}
|
||||||
|
token = accessTokenJson[sharedToken]
|
||||||
|
return token || ''
|
||||||
|
}
|
||||||
|
|
||||||
|
const beforeRequestPublicAuthorization: BeforeRequestHook = (request) => {
|
||||||
|
const token = getPublicToken()
|
||||||
|
request.headers.set('Authorization', `Bearer ${token}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
const beforeRequestAuthorization: BeforeRequestHook = (request) => {
|
||||||
|
const accessToken = localStorage.getItem('console_token') || ''
|
||||||
|
request.headers.set('Authorization', `Bearer ${accessToken}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
const beforeRequestDeleteContentType: BeforeRequestHook = (request) => {
|
||||||
|
request.headers.delete('Content-Type')
|
||||||
|
}
|
||||||
|
|
||||||
|
const baseHooks: Hooks = {
|
||||||
|
afterResponse: [
|
||||||
|
afterResponse204,
|
||||||
|
],
|
||||||
|
}
|
||||||
|
|
||||||
|
const client = ky.create({
|
||||||
|
hooks: baseHooks,
|
||||||
|
timeout: TIME_OUT,
|
||||||
|
})
|
||||||
|
|
||||||
|
export const baseOptions: RequestInit = {
|
||||||
|
method: 'GET',
|
||||||
|
mode: 'cors',
|
||||||
|
credentials: 'include', // always send cookies、HTTP Basic authentication.
|
||||||
|
headers: new Headers({
|
||||||
|
'Content-Type': ContentType.json,
|
||||||
|
}),
|
||||||
|
redirect: 'follow',
|
||||||
|
}
|
||||||
|
|
||||||
|
async function base<T>(url: string, options: FetchOptionType = {}, otherOptions: IOtherOptions = {}): Promise<T> {
|
||||||
|
const { params, body, ...init } = Object.assign({}, baseOptions, options)
|
||||||
|
const {
|
||||||
|
isPublicAPI = false,
|
||||||
|
isMarketplaceAPI = false,
|
||||||
|
bodyStringify = true,
|
||||||
|
needAllResponseContent,
|
||||||
|
deleteContentType,
|
||||||
|
getAbortController,
|
||||||
|
} = otherOptions
|
||||||
|
|
||||||
|
const base
|
||||||
|
= isMarketplaceAPI
|
||||||
|
? MARKETPLACE_API_PREFIX
|
||||||
|
: isPublicAPI
|
||||||
|
? PUBLIC_API_PREFIX
|
||||||
|
: API_PREFIX
|
||||||
|
|
||||||
|
if (getAbortController) {
|
||||||
|
const abortController = new AbortController()
|
||||||
|
getAbortController(abortController)
|
||||||
|
options.signal = abortController.signal
|
||||||
|
}
|
||||||
|
|
||||||
|
const fetchPathname = `${base}${url.startsWith('/') ? url : `/${url}`}`
|
||||||
|
|
||||||
|
const res = await client.extend({
|
||||||
|
hooks: {
|
||||||
|
...baseHooks,
|
||||||
|
beforeError: [
|
||||||
|
...baseHooks.beforeError || [],
|
||||||
|
beforeErrorToast(otherOptions),
|
||||||
|
],
|
||||||
|
beforeRequest: [
|
||||||
|
...baseHooks.beforeRequest || [],
|
||||||
|
isPublicAPI && beforeRequestPublicAuthorization,
|
||||||
|
!isPublicAPI && !isMarketplaceAPI && beforeRequestAuthorization,
|
||||||
|
deleteContentType && beforeRequestDeleteContentType,
|
||||||
|
].filter(i => !!i),
|
||||||
|
afterResponse: [
|
||||||
|
...baseHooks.afterResponse || [],
|
||||||
|
afterResponseErrorCode(otherOptions),
|
||||||
|
],
|
||||||
|
},
|
||||||
|
})(fetchPathname, {
|
||||||
|
...init,
|
||||||
|
credentials: isMarketplaceAPI
|
||||||
|
? 'omit'
|
||||||
|
: (options.credentials || 'include'),
|
||||||
|
retry: {
|
||||||
|
methods: [],
|
||||||
|
},
|
||||||
|
...(bodyStringify ? { json: body } : { body: body as BodyInit }),
|
||||||
|
searchParams: params,
|
||||||
|
})
|
||||||
|
|
||||||
|
if (needAllResponseContent)
|
||||||
|
return res as T
|
||||||
|
const contentType = res.headers.get('content-type')
|
||||||
|
if (
|
||||||
|
contentType
|
||||||
|
&& [ContentType.download, ContentType.audio].includes(contentType)
|
||||||
|
)
|
||||||
|
return await res.blob() as T
|
||||||
|
|
||||||
|
return await res.json() as T
|
||||||
|
}
|
||||||
|
|
||||||
|
export {
|
||||||
|
client,
|
||||||
|
base,
|
||||||
|
}
|
||||||
@ -0,0 +1,149 @@
|
|||||||
|
import { get, post } from './base'
|
||||||
|
import type {
|
||||||
|
EndpointsResponse,
|
||||||
|
} from '@/app/components/plugins/types'
|
||||||
|
import {
|
||||||
|
useMutation,
|
||||||
|
useQuery,
|
||||||
|
useQueryClient,
|
||||||
|
} from '@tanstack/react-query'
|
||||||
|
|
||||||
|
const NAME_SPACE = 'endpoints'
|
||||||
|
|
||||||
|
export const useEndpointList = (pluginID: string) => {
|
||||||
|
return useQuery({
|
||||||
|
queryKey: [NAME_SPACE, 'list', pluginID],
|
||||||
|
queryFn: () => get<EndpointsResponse>('/workspaces/current/endpoints/list/plugin', {
|
||||||
|
params: {
|
||||||
|
plugin_id: pluginID,
|
||||||
|
page: 1,
|
||||||
|
page_size: 100,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
export const useInvalidateEndpointList = () => {
|
||||||
|
const queryClient = useQueryClient()
|
||||||
|
return (pluginID: string) => {
|
||||||
|
queryClient.invalidateQueries(
|
||||||
|
{
|
||||||
|
queryKey: [NAME_SPACE, 'list', pluginID],
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const useCreateEndpoint = ({
|
||||||
|
onSuccess,
|
||||||
|
onError,
|
||||||
|
}: {
|
||||||
|
onSuccess?: () => void
|
||||||
|
onError?: (error: any) => void
|
||||||
|
}) => {
|
||||||
|
return useMutation({
|
||||||
|
mutationKey: [NAME_SPACE, 'create'],
|
||||||
|
mutationFn: (payload: { pluginUniqueID: string, state: Record<string, any> }) => {
|
||||||
|
const { pluginUniqueID, state } = payload
|
||||||
|
const newName = state.name
|
||||||
|
delete state.name
|
||||||
|
return post('/workspaces/current/endpoints/create', {
|
||||||
|
body: {
|
||||||
|
plugin_unique_identifier: pluginUniqueID,
|
||||||
|
settings: state,
|
||||||
|
name: newName,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
},
|
||||||
|
onSuccess,
|
||||||
|
onError,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
export const useUpdateEndpoint = ({
|
||||||
|
onSuccess,
|
||||||
|
onError,
|
||||||
|
}: {
|
||||||
|
onSuccess?: () => void
|
||||||
|
onError?: (error: any) => void
|
||||||
|
}) => {
|
||||||
|
return useMutation({
|
||||||
|
mutationKey: [NAME_SPACE, 'update'],
|
||||||
|
mutationFn: (payload: { endpointID: string, state: Record<string, any> }) => {
|
||||||
|
const { endpointID, state } = payload
|
||||||
|
const newName = state.name
|
||||||
|
delete state.name
|
||||||
|
return post('/workspaces/current/endpoints/update', {
|
||||||
|
body: {
|
||||||
|
endpoint_id: endpointID,
|
||||||
|
settings: state,
|
||||||
|
name: newName,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
},
|
||||||
|
onSuccess,
|
||||||
|
onError,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
export const useDeleteEndpoint = ({
|
||||||
|
onSuccess,
|
||||||
|
onError,
|
||||||
|
}: {
|
||||||
|
onSuccess?: () => void
|
||||||
|
onError?: (error: any) => void
|
||||||
|
}) => {
|
||||||
|
return useMutation({
|
||||||
|
mutationKey: [NAME_SPACE, 'delete'],
|
||||||
|
mutationFn: (endpointID: string) => {
|
||||||
|
return post('/workspaces/current/endpoints/delete', {
|
||||||
|
body: {
|
||||||
|
endpoint_id: endpointID,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
},
|
||||||
|
onSuccess,
|
||||||
|
onError,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
export const useEnableEndpoint = ({
|
||||||
|
onSuccess,
|
||||||
|
onError,
|
||||||
|
}: {
|
||||||
|
onSuccess?: () => void
|
||||||
|
onError?: (error: any) => void
|
||||||
|
}) => {
|
||||||
|
return useMutation({
|
||||||
|
mutationKey: [NAME_SPACE, 'enable'],
|
||||||
|
mutationFn: (endpointID: string) => {
|
||||||
|
return post('/workspaces/current/endpoints/enable', {
|
||||||
|
body: {
|
||||||
|
endpoint_id: endpointID,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
},
|
||||||
|
onSuccess,
|
||||||
|
onError,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
export const useDisableEndpoint = ({
|
||||||
|
onSuccess,
|
||||||
|
onError,
|
||||||
|
}: {
|
||||||
|
onSuccess?: () => void
|
||||||
|
onError?: (error: any) => void
|
||||||
|
}) => {
|
||||||
|
return useMutation({
|
||||||
|
mutationKey: [NAME_SPACE, 'disable'],
|
||||||
|
mutationFn: (endpointID: string) => {
|
||||||
|
return post('/workspaces/current/endpoints/disable', {
|
||||||
|
body: {
|
||||||
|
endpoint_id: endpointID,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
},
|
||||||
|
onSuccess,
|
||||||
|
onError,
|
||||||
|
})
|
||||||
|
}
|
||||||
@ -0,0 +1,17 @@
|
|||||||
|
import { get } from './base'
|
||||||
|
import type {
|
||||||
|
ModelItem,
|
||||||
|
} from '@/app/components/header/account-setting/model-provider-page/declarations'
|
||||||
|
import {
|
||||||
|
useQuery,
|
||||||
|
// useQueryClient,
|
||||||
|
} from '@tanstack/react-query'
|
||||||
|
|
||||||
|
const NAME_SPACE = 'models'
|
||||||
|
|
||||||
|
export const useModelProviderModelList = (provider: string) => {
|
||||||
|
return useQuery({
|
||||||
|
queryKey: [NAME_SPACE, 'model-list', provider],
|
||||||
|
queryFn: () => get<{ data: ModelItem[] }>(`/workspaces/current/model-providers/${provider}/models`),
|
||||||
|
})
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue