import type { Fetcher } from 'swr' import { del, get, patch, post, put } from './base' import type { ApiKeysListResponse, AppDailyConversationsResponse, AppDailyEndUsersResponse, AppDailyMessagesResponse, AppDetailResponse, AppListResponse, AppSSOResponse, AppStatisticsResponse, AppTemplatesResponse, AppTokenCostsResponse, AppVoicesListResponse, CreateApiKeyResponse, GenerationIntroductionResponse, TracingConfig, TracingStatus, UpdateAppModelConfigResponse, UpdateAppSiteCodeResponse, UpdateOpenAIKeyResponse, ValidateOpenAIKeyResponse, WorkflowDailyConversationsResponse } from '@/models/app' import type { CommonResponse } from '@/models/common' import type { AppIconType, AppMode, ModelConfig } from '@/types/app' import type { TracingProvider } from '@/app/(commonLayout)/app/(appDetailLayout)/[appId]/overview/tracing/type' export const fetchAppList: Fetcher }> = ({ url, params }) => { return get(url, { params }) } export const fetchAppDetail = ({ url, id }: { url: string; id: string }) => { return get(`${url}/${id}`) } export const fetchAppSSO = async ({ appId }: { appId: string }) => { return get(`/enterprise/app-setting/sso?appID=${appId}`) } export const updateAppSSO = async ({ id, enabled }: { id: string; enabled: boolean }) => { return post('/enterprise/app-setting/sso', { body: { app_id: id, enabled } }) } export const fetchAppTemplates: Fetcher = ({ url }) => { return get(url) } export const createApp: Fetcher = ({ name, icon_type, icon, icon_background, mode, description, config }) => { return post('apps', { body: { name, icon_type, icon, icon_background, mode, description, model_config: config } }) } export const updateAppInfo: Fetcher = ({ appID, name, icon_type, icon, icon_background, description, use_icon_as_answer_icon }) => { return put(`apps/${appID}`, { body: { name, icon_type, icon, icon_background, description, use_icon_as_answer_icon } }) } export const copyApp: Fetcher = ({ appID, name, icon_type, icon, icon_background, mode, description }) => { return post(`apps/${appID}/copy`, { body: { name, icon_type, icon, icon_background, mode, description } }) } export const exportAppConfig: Fetcher<{ data: string }, { appID: string; include?: boolean }> = ({ appID, include = false }) => { return get<{ data: string }>(`apps/${appID}/export?include_secret=${include}`) } export const importApp: Fetcher = ({ data, name, description, icon_type, icon, icon_background }) => { return post('apps/import', { body: { data, name, description, icon_type, icon, icon_background } }) } export const importAppFromUrl: Fetcher = ({ url, name, description, icon, icon_background }) => { return post('apps/import/url', { body: { url, name, description, icon, icon_background } }) } export const switchApp: Fetcher<{ new_app_id: string }, { appID: string; name: string; icon_type: AppIconType; icon: string; icon_background?: string | null }> = ({ appID, name, icon_type, icon, icon_background }) => { return post<{ new_app_id: string }>(`apps/${appID}/convert-to-workflow`, { body: { name, icon_type, icon, icon_background } }) } export const deleteApp: Fetcher = (appID) => { return del(`apps/${appID}`) } export const updateAppSiteStatus: Fetcher }> = ({ url, body }) => { return post(url, { body }) } export const updateAppApiStatus: Fetcher }> = ({ url, body }) => { return post(url, { body }) } // path: /apps/{appId}/rate-limit export const updateAppRateLimit: Fetcher }> = ({ url, body }) => { return post(url, { body }) } export const updateAppSiteAccessToken: Fetcher = ({ url }) => { return post(url) } export const updateAppSiteConfig = ({ url, body }: { url: string; body: Record }) => { return post(url, { body }) } export const getAppDailyMessages: Fetcher }> = ({ url, params }) => { return get(url, { params }) } export const getAppDailyConversations: Fetcher }> = ({ url, params }) => { return get(url, { params }) } export const getWorkflowDailyConversations: Fetcher }> = ({ url, params }) => { return get(url, { params }) } export const getAppStatistics: Fetcher }> = ({ url, params }) => { return get(url, { params }) } export const getAppDailyEndUsers: Fetcher }> = ({ url, params }) => { return get(url, { params }) } export const getAppTokenCosts: Fetcher }> = ({ url, params }) => { return get(url, { params }) } export const updateAppModelConfig: Fetcher }> = ({ url, body }) => { return post(url, { body }) } // For temp testing export const fetchAppListNoMock: Fetcher }> = ({ url, params }) => { return get(url, params) } export const fetchApiKeysList: Fetcher }> = ({ url, params }) => { return get(url, params) } export const delApikey: Fetcher }> = ({ url, params }) => { return del(url, params) } export const createApikey: Fetcher }> = ({ url, body }) => { return post(url, body) } export const validateOpenAIKey: Fetcher = ({ url, body }) => { return post(url, { body }) } export const updateOpenAIKey: Fetcher = ({ url, body }) => { return post(url, { body }) } export const generationIntroduction: Fetcher = ({ url, body }) => { return post(url, { body }) } export const fetchAppVoices: Fetcher = ({ appId, language }) => { language = language || 'en-US' return get(`apps/${appId}/text-to-audio/voices?language=${language}`) } // Tracing export const fetchTracingStatus: Fetcher = ({ appId }) => { return get(`/apps/${appId}/trace`) } export const updateTracingStatus: Fetcher }> = ({ appId, body }) => { return post(`/apps/${appId}/trace`, { body }) } export const fetchTracingConfig: Fetcher = ({ appId, provider }) => { return get(`/apps/${appId}/trace-config`, { params: { tracing_provider: provider, }, }) } export const addTracingConfig: Fetcher = ({ appId, body }) => { return post(`/apps/${appId}/trace-config`, { body }) } export const updateTracingConfig: Fetcher = ({ appId, body }) => { return patch(`/apps/${appId}/trace-config`, { body }) } export const removeTracingConfig: Fetcher = ({ appId, provider }) => { return del(`/apps/${appId}/trace-config?tracing_provider=${provider}`) }