From 5280bffde266fa2218f0996a545c54dd09959d42 Mon Sep 17 00:00:00 2001 From: Joel Date: Wed, 17 Sep 2025 11:17:12 +0800 Subject: [PATCH] feat: change api to new --- .../app/text-generate/item/index.tsx | 8 ++++--- web/app/components/base/audio-btn/audio.ts | 4 ++-- .../chat/chat-with-history/chat-wrapper.tsx | 7 ++++-- .../base/chat/chat-with-history/hooks.tsx | 24 ++++++++++--------- .../chat/embedded-chatbot/chat-wrapper.tsx | 7 ++++-- .../base/chat/embedded-chatbot/hooks.tsx | 14 ++++++----- web/app/components/base/voice-input/index.tsx | 4 ++-- .../share/text-generation/index.tsx | 11 +++++---- .../share/text-generation/result/index.tsx | 6 ++--- web/service/use-explore.ts | 6 ++--- web/service/use-share.ts | 6 ++--- 11 files changed, 55 insertions(+), 42 deletions(-) diff --git a/web/app/components/app/text-generate/item/index.tsx b/web/app/components/app/text-generate/item/index.tsx index 92d86351e0..5b12e9a669 100644 --- a/web/app/components/app/text-generate/item/index.tsx +++ b/web/app/components/app/text-generate/item/index.tsx @@ -21,7 +21,7 @@ import { Markdown } from '@/app/components/base/markdown' import Loading from '@/app/components/base/loading' import Toast from '@/app/components/base/toast' import type { FeedbackType } from '@/app/components/base/chat/chat/type' -import { fetchMoreLikeThis, updateFeedback } from '@/service/share' +import { AppSourceType, fetchMoreLikeThis, updateFeedback } from '@/service/share' import { fetchTextGenerationMessage } from '@/service/debug' import { useStore as useAppStore } from '@/app/components/app/store' import WorkflowProcessItem from '@/app/components/base/chat/chat/answer/workflow-process' @@ -111,8 +111,10 @@ const GenerationItem: FC = ({ const setCurrentLogItem = useAppStore(s => s.setCurrentLogItem) const setShowPromptLogModal = useAppStore(s => s.setShowPromptLogModal) + const appSourceType = isInstalledApp ? AppSourceType.installedApp : AppSourceType.webApp + const handleFeedback = async (childFeedback: FeedbackType) => { - await updateFeedback({ url: `/messages/${childMessageId}/feedbacks`, body: { rating: childFeedback.rating } }, isInstalledApp, installedAppId) + await updateFeedback({ url: `/messages/${childMessageId}/feedbacks`, body: { rating: childFeedback.rating } }, appSourceType, installedAppId) setChildFeedback(childFeedback) } @@ -144,7 +146,7 @@ const GenerationItem: FC = ({ return } startQuerying() - const res: any = await fetchMoreLikeThis(messageId as string, isInstalledApp, installedAppId) + const res: any = await fetchMoreLikeThis(messageId as string, appSourceType, installedAppId) setCompletionRes(res.answer) setChildFeedback({ rating: null, diff --git a/web/app/components/base/audio-btn/audio.ts b/web/app/components/base/audio-btn/audio.ts index 00797d04e4..6cabc186af 100644 --- a/web/app/components/base/audio-btn/audio.ts +++ b/web/app/components/base/audio-btn/audio.ts @@ -1,5 +1,5 @@ import Toast from '@/app/components/base/toast' -import { textToAudioStream } from '@/service/share' +import { AppSourceType, textToAudioStream } from '@/service/share' declare global { // eslint-disable-next-line ts/consistent-type-definitions @@ -100,7 +100,7 @@ export default class AudioPlayer { private async loadAudio() { try { - const audioResponse: any = await textToAudioStream(this.url, this.isPublic, { content_type: 'audio/mpeg' }, { + const audioResponse: any = await textToAudioStream(this.url, this.isPublic ? AppSourceType.webApp : AppSourceType.installedApp, { content_type: 'audio/mpeg' }, { message_id: this.msgId, streaming: true, voice: this.voice, diff --git a/web/app/components/base/chat/chat-with-history/chat-wrapper.tsx b/web/app/components/base/chat/chat-with-history/chat-wrapper.tsx index cc5e46deeb..c3cd649631 100644 --- a/web/app/components/base/chat/chat-with-history/chat-wrapper.tsx +++ b/web/app/components/base/chat/chat-with-history/chat-wrapper.tsx @@ -13,6 +13,7 @@ import { InputVarType } from '@/app/components/workflow/types' import { TransferMethod } from '@/types/app' import InputsForm from '@/app/components/base/chat/chat-with-history/inputs-form' import { + AppSourceType, fetchSuggestedQuestions, getUrl, stopChatMessageResponding, @@ -53,6 +54,8 @@ const ChatWrapper = () => { initUserVariables, } = useChatWithHistoryContext() + const appSourceType = isInstalledApp ? AppSourceType.installedApp : AppSourceType.webApp + // Semantic variable for better code readability const isHistoryConversation = !!currentConversationId @@ -142,10 +145,10 @@ const ChatWrapper = () => { } handleSend( - getUrl('chat-messages', isInstalledApp, appId || ''), + getUrl('chat-messages', appSourceType, appId || ''), data, { - onGetSuggestedQuestions: responseItemId => fetchSuggestedQuestions(responseItemId, isInstalledApp, appId), + onGetSuggestedQuestions: responseItemId => fetchSuggestedQuestions(responseItemId, appSourceType, appId), onConversationComplete: isHistoryConversation ? undefined : handleNewConversationCompleted, isPublicAPI: !isInstalledApp, }, diff --git a/web/app/components/base/chat/chat-with-history/hooks.tsx b/web/app/components/base/chat/chat-with-history/hooks.tsx index 0e8da0d26d..2dfd6ed8cb 100644 --- a/web/app/components/base/chat/chat-with-history/hooks.tsx +++ b/web/app/components/base/chat/chat-with-history/hooks.tsx @@ -20,6 +20,7 @@ import { buildChatItemTree, getProcessedSystemVariablesFromUrlParams, getRawInpu import { addFileInfos, sortAgentSorts } from '../../../tools/utils' import { getProcessedFilesFromResponse } from '@/app/components/base/file-uploader/utils' import { + AppSourceType, delConversation, fetchChatList, fetchConversations, @@ -70,6 +71,7 @@ function getFormattedChatList(messages: any[]) { export const useChatWithHistory = (installedAppInfo?: InstalledApp) => { const isInstalledApp = useMemo(() => !!installedAppInfo, [installedAppInfo]) + const appSourceType = isInstalledApp ? AppSourceType.installedApp : AppSourceType.webApp const appInfo = useWebAppStore(s => s.appInfo) const appParams = useWebAppStore(s => s.appParams) const appMeta = useWebAppStore(s => s.appMeta) @@ -164,17 +166,17 @@ export const useChatWithHistory = (installedAppInfo?: InstalledApp) => { const { data: appPinnedConversationData, mutate: mutateAppPinnedConversationData } = useSWR( appId ? ['appConversationData', isInstalledApp, appId, true] : null, - () => fetchConversations(isInstalledApp, appId, undefined, true, 100), + () => fetchConversations(appSourceType, appId, undefined, true, 100), { revalidateOnFocus: false, revalidateOnReconnect: false }, ) const { data: appConversationData, isLoading: appConversationDataLoading, mutate: mutateAppConversationData } = useSWR( appId ? ['appConversationData', isInstalledApp, appId, false] : null, - () => fetchConversations(isInstalledApp, appId, undefined, false, 100), + () => fetchConversations(appSourceType, appId, undefined, false, 100), { revalidateOnFocus: false, revalidateOnReconnect: false }, ) const { data: appChatListData, isLoading: appChatListDataLoading } = useSWR( - chatShouldReloadKey ? ['appChatList', chatShouldReloadKey, isInstalledApp, appId] : null, - () => fetchChatList(chatShouldReloadKey, isInstalledApp, appId), + chatShouldReloadKey ? ['appChatList', chatShouldReloadKey, appSourceType, appId] : null, + () => fetchChatList(chatShouldReloadKey, appSourceType, appId), { revalidateOnFocus: false, revalidateOnReconnect: false }, ) @@ -295,7 +297,7 @@ export const useChatWithHistory = (installedAppInfo?: InstalledApp) => { handleNewConversationInputsChange(conversationInputs) }, [handleNewConversationInputsChange, inputsForms]) - const { data: newConversation } = useSWR(newConversationId ? [isInstalledApp, appId, newConversationId] : null, () => generationConversationName(isInstalledApp, appId, newConversationId), { revalidateOnFocus: false }) + const { data: newConversation } = useSWR(newConversationId ? [isInstalledApp, appId, newConversationId] : null, () => generationConversationName(appSourceType, appId, newConversationId), { revalidateOnFocus: false }) const [originConversationList, setOriginConversationList] = useState([]) useEffect(() => { if (appConversationData?.data && !appConversationDataLoading) @@ -420,16 +422,16 @@ export const useChatWithHistory = (installedAppInfo?: InstalledApp) => { }, [mutateAppConversationData, mutateAppPinnedConversationData]) const handlePinConversation = useCallback(async (conversationId: string) => { - await pinConversation(isInstalledApp, appId, conversationId) + await pinConversation(appSourceType, appId, conversationId) notify({ type: 'success', message: t('common.api.success') }) handleUpdateConversationList() - }, [isInstalledApp, appId, notify, t, handleUpdateConversationList]) + }, [appSourceType, appId, notify, t, handleUpdateConversationList]) const handleUnpinConversation = useCallback(async (conversationId: string) => { - await unpinConversation(isInstalledApp, appId, conversationId) + await unpinConversation(appSourceType, appId, conversationId) notify({ type: 'success', message: t('common.api.success') }) handleUpdateConversationList() - }, [isInstalledApp, appId, notify, t, handleUpdateConversationList]) + }, [appSourceType, appId, notify, t, handleUpdateConversationList]) const [conversationDeleting, setConversationDeleting] = useState(false) const handleDeleteConversation = useCallback(async ( @@ -443,7 +445,7 @@ export const useChatWithHistory = (installedAppInfo?: InstalledApp) => { try { setConversationDeleting(true) - await delConversation(isInstalledApp, appId, conversationId) + await delConversation(appSourceType, appId, conversationId) notify({ type: 'success', message: t('common.api.success') }) onSuccess() } @@ -478,7 +480,7 @@ export const useChatWithHistory = (installedAppInfo?: InstalledApp) => { setConversationRenaming(true) try { - await renameConversation(isInstalledApp, appId, conversationId, newName) + await renameConversation(appSourceType, appId, conversationId, newName) notify({ type: 'success', diff --git a/web/app/components/base/chat/embedded-chatbot/chat-wrapper.tsx b/web/app/components/base/chat/embedded-chatbot/chat-wrapper.tsx index 14a291e9fd..e802fd87b0 100644 --- a/web/app/components/base/chat/embedded-chatbot/chat-wrapper.tsx +++ b/web/app/components/base/chat/embedded-chatbot/chat-wrapper.tsx @@ -14,6 +14,7 @@ import { InputVarType } from '@/app/components/workflow/types' import { TransferMethod } from '@/types/app' import InputsForm from '@/app/components/base/chat/embedded-chatbot/inputs-form' import { + AppSourceType, fetchSuggestedQuestions, getUrl, stopChatMessageResponding, @@ -52,6 +53,8 @@ const ChatWrapper = () => { allInputsHidden, initUserVariables, } = useEmbeddedChatbotContext() + + const appSourceType = isInstalledApp ? AppSourceType.installedApp : AppSourceType.webApp const appConfig = useMemo(() => { const config = appParams || {} @@ -137,10 +140,10 @@ const ChatWrapper = () => { } handleSend( - getUrl('chat-messages', isInstalledApp, appId || ''), + getUrl('chat-messages', appSourceType, appId || ''), data, { - onGetSuggestedQuestions: responseItemId => fetchSuggestedQuestions(responseItemId, isInstalledApp, appId), + onGetSuggestedQuestions: responseItemId => fetchSuggestedQuestions(responseItemId, appSourceType, appId), onConversationComplete: currentConversationId ? undefined : handleNewConversationCompleted, isPublicAPI: !isInstalledApp, }, diff --git a/web/app/components/base/chat/embedded-chatbot/hooks.tsx b/web/app/components/base/chat/embedded-chatbot/hooks.tsx index 14a32860b9..c5e1b582a5 100644 --- a/web/app/components/base/chat/embedded-chatbot/hooks.tsx +++ b/web/app/components/base/chat/embedded-chatbot/hooks.tsx @@ -18,6 +18,7 @@ import { CONVERSATION_ID_INFO } from '../constants' import { buildChatItemTree, getProcessedInputsFromUrlParams, getProcessedSystemVariablesFromUrlParams, getProcessedUserVariablesFromUrlParams } from '../utils' import { getProcessedFilesFromResponse } from '../../file-uploader/utils' import { + AppSourceType, fetchAppInfo, fetchAppMeta, fetchAppParams, @@ -67,6 +68,7 @@ function getFormattedChatList(messages: any[]) { export const useEmbeddedChatbot = () => { const isInstalledApp = false + const appSourceType = AppSourceType.webApp const systemFeatures = useGlobalPublicStore(s => s.systemFeatures) const { data: appInfo, isLoading: appInfoLoading, error: appInfoError } = useSWR('appInfo', fetchAppInfo) const { isPending: isCheckingPermission, data: userCanAccessResult } = useGetUserCanAccessApp({ @@ -145,11 +147,11 @@ export const useEmbeddedChatbot = () => { return currentConversationId }, [currentConversationId, newConversationId]) - const { data: appParams } = useSWR(['appParams', isInstalledApp, appId], () => fetchAppParams(isInstalledApp, appId)) - const { data: appMeta } = useSWR(['appMeta', isInstalledApp, appId], () => fetchAppMeta(isInstalledApp, appId)) - const { data: appPinnedConversationData } = useSWR(['appConversationData', isInstalledApp, appId, true], () => fetchConversations(isInstalledApp, appId, undefined, true, 100)) - const { data: appConversationData, isLoading: appConversationDataLoading, mutate: mutateAppConversationData } = useSWR(['appConversationData', isInstalledApp, appId, false], () => fetchConversations(isInstalledApp, appId, undefined, false, 100)) - const { data: appChatListData, isLoading: appChatListDataLoading } = useSWR(chatShouldReloadKey ? ['appChatList', chatShouldReloadKey, isInstalledApp, appId] : null, () => fetchChatList(chatShouldReloadKey, isInstalledApp, appId)) + const { data: appParams } = useSWR(['appParams', isInstalledApp, appId], () => fetchAppParams(appSourceType, appId)) + const { data: appMeta } = useSWR(['appMeta', isInstalledApp, appId], () => fetchAppMeta(appSourceType, appId)) + const { data: appPinnedConversationData } = useSWR(['appConversationData', isInstalledApp, appId, true], () => fetchConversations(appSourceType, appId, undefined, true, 100)) + const { data: appConversationData, isLoading: appConversationDataLoading, mutate: mutateAppConversationData } = useSWR(['appConversationData', isInstalledApp, appId, false], () => fetchConversations(appSourceType, appId, undefined, false, 100)) + const { data: appChatListData, isLoading: appChatListDataLoading } = useSWR(chatShouldReloadKey ? ['appChatList', chatShouldReloadKey, isInstalledApp, appId] : null, () => fetchChatList(chatShouldReloadKey, appSourceType, appId)) const [clearChatList, setClearChatList] = useState(false) const [isResponding, setIsResponding] = useState(false) @@ -266,7 +268,7 @@ export const useEmbeddedChatbot = () => { handleNewConversationInputsChange(conversationInputs) }, [handleNewConversationInputsChange, inputsForms]) - const { data: newConversation } = useSWR(newConversationId ? [isInstalledApp, appId, newConversationId] : null, () => generationConversationName(isInstalledApp, appId, newConversationId), { revalidateOnFocus: false }) + const { data: newConversation } = useSWR(newConversationId ? [isInstalledApp, appId, newConversationId] : null, () => generationConversationName(appSourceType, appId, newConversationId), { revalidateOnFocus: false }) const [originConversationList, setOriginConversationList] = useState([]) useEffect(() => { if (appConversationData?.data && !appConversationDataLoading) diff --git a/web/app/components/base/voice-input/index.tsx b/web/app/components/base/voice-input/index.tsx index 5a5400ad30..cd5c2a87b9 100644 --- a/web/app/components/base/voice-input/index.tsx +++ b/web/app/components/base/voice-input/index.tsx @@ -11,7 +11,7 @@ import { convertToMp3 } from './utils' import s from './index.module.css' import cn from '@/utils/classnames' import { StopCircle } from '@/app/components/base/icons/src/vender/solid/mediaAndDevices' -import { audioToText } from '@/service/share' +import { AppSourceType, audioToText } from '@/service/share' type VoiceInputTypes = { onConverted: (text: string) => void @@ -107,7 +107,7 @@ const VoiceInput = ({ } try { - const audioResponse = await audioToText(url, isPublic, formData) + const audioResponse = await audioToText(url, isPublic ? AppSourceType.webApp : AppSourceType.installedApp, formData) onConverted(audioResponse.text) onCancel() } diff --git a/web/app/components/share/text-generation/index.tsx b/web/app/components/share/text-generation/index.tsx index da5b09b065..2384e1fc3a 100644 --- a/web/app/components/share/text-generation/index.tsx +++ b/web/app/components/share/text-generation/index.tsx @@ -14,7 +14,7 @@ import RunBatch from './run-batch' import ResDownload from './run-batch/res-download' import useBreakpoints, { MediaType } from '@/hooks/use-breakpoints' import RunOnce from '@/app/components/share/text-generation/run-once' -import { fetchSavedMessage as doFetchSavedMessage, removeMessage, saveMessage } from '@/service/share' +import { AppSourceType, fetchSavedMessage as doFetchSavedMessage, removeMessage, saveMessage } from '@/service/share' import type { SiteInfo } from '@/models/share' import type { MoreLikeThisConfig, @@ -72,6 +72,7 @@ const TextGeneration: FC = ({ isWorkflow = false, }) => { const { notify } = Toast + const appSourceType = isInstalledApp ? AppSourceType.installedApp : AppSourceType.webApp const { t } = useTranslation() const media = useBreakpoints() @@ -101,16 +102,16 @@ const TextGeneration: FC = ({ // save message const [savedMessages, setSavedMessages] = useState([]) const fetchSavedMessage = useCallback(async () => { - const res: any = await doFetchSavedMessage(isInstalledApp, appId) + const res: any = await doFetchSavedMessage(appSourceType, appId) setSavedMessages(res.data) - }, [isInstalledApp, appId]) + }, [appSourceType, appId]) const handleSaveMessage = async (messageId: string) => { - await saveMessage(messageId, isInstalledApp, appId) + await saveMessage(messageId, appSourceType, appId) notify({ type: 'success', message: t('common.api.saved') }) fetchSavedMessage() } const handleRemoveSavedMessage = async (messageId: string) => { - await removeMessage(messageId, isInstalledApp, appId) + await removeMessage(messageId, appSourceType, appId) notify({ type: 'success', message: t('common.api.remove') }) fetchSavedMessage() } diff --git a/web/app/components/share/text-generation/result/index.tsx b/web/app/components/share/text-generation/result/index.tsx index a7eb7f7591..0224490b2d 100644 --- a/web/app/components/share/text-generation/result/index.tsx +++ b/web/app/components/share/text-generation/result/index.tsx @@ -7,7 +7,7 @@ import produce from 'immer' import TextGenerationRes from '@/app/components/app/text-generate/item' import NoData from '@/app/components/share/text-generation/no-data' import Toast from '@/app/components/base/toast' -import { sendCompletionMessage, sendWorkflowMessage, updateFeedback } from '@/service/share' +import { AppSourceType, sendCompletionMessage, sendWorkflowMessage, updateFeedback } from '@/service/share' import type { FeedbackType } from '@/app/components/base/chat/chat/type' import Loading from '@/app/components/base/loading' import type { PromptConfig } from '@/models/debug' @@ -358,7 +358,7 @@ const Result: FC = ({ })) }, }, - isInstalledApp, + isInstalledApp ? AppSourceType.installedApp : AppSourceType.webApp, installedAppInfo?.id, ) } @@ -392,7 +392,7 @@ const Result: FC = ({ onCompleted(getCompletionRes(), taskId, false) isEnd = true }, - }, isInstalledApp, installedAppInfo?.id) + }, isInstalledApp ? AppSourceType.installedApp : AppSourceType.webApp, installedAppInfo?.id) } } diff --git a/web/service/use-explore.ts b/web/service/use-explore.ts index b7d078edbc..cbbb774cdc 100644 --- a/web/service/use-explore.ts +++ b/web/service/use-explore.ts @@ -2,7 +2,7 @@ import { useGlobalPublicStore } from '@/context/global-public-context' import { AccessMode } from '@/models/access-control' import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query' import { fetchInstalledAppList, getAppAccessModeByAppId, uninstallApp, updatePinStatus } from './explore' -import { fetchAppMeta, fetchAppParams } from './share' +import { AppSourceType, fetchAppMeta, fetchAppParams } from './share' const NAME_SPACE = 'explore' @@ -62,7 +62,7 @@ export const useGetInstalledAppParams = (appId: string | null) => { queryFn: () => { if (!appId || appId.length === 0) return Promise.reject(new Error('App ID is required to get app params')) - return fetchAppParams(true, appId) + return fetchAppParams(AppSourceType.installedApp, appId) }, enabled: !!appId, }) @@ -74,7 +74,7 @@ export const useGetInstalledAppMeta = (appId: string | null) => { queryFn: () => { if (!appId || appId.length === 0) return Promise.reject(new Error('App ID is required to get app meta')) - return fetchAppMeta(true, appId) + return fetchAppMeta(AppSourceType.installedApp, appId) }, enabled: !!appId, }) diff --git a/web/service/use-share.ts b/web/service/use-share.ts index 267975fd38..d430c77877 100644 --- a/web/service/use-share.ts +++ b/web/service/use-share.ts @@ -1,5 +1,5 @@ import { useQuery } from '@tanstack/react-query' -import { fetchAppInfo, fetchAppMeta, fetchAppParams, getAppAccessModeByAppCode } from './share' +import { AppSourceType, fetchAppInfo, fetchAppMeta, fetchAppParams, getAppAccessModeByAppCode } from './share' const NAME_SPACE = 'webapp' @@ -24,7 +24,7 @@ export const useGetWebAppParams = () => { return useQuery({ queryKey: [NAME_SPACE, 'appParams'], queryFn: () => { - return fetchAppParams(false) + return fetchAppParams(AppSourceType.webApp) }, }) } @@ -33,7 +33,7 @@ export const useGetWebAppMeta = () => { return useQuery({ queryKey: [NAME_SPACE, 'appMeta'], queryFn: () => { - return fetchAppMeta(false) + return fetchAppMeta(AppSourceType.webApp) }, }) }