import type { Fetcher } from 'swr' import { del, get, patch, post, put } from './base' import type { AccountIntegrate, ApiBasedExtension, CodeBasedExtension, CommonResponse, DataSourceNotion, FileUploadConfigResponse, ICurrentWorkspace, IWorkspace, InitValidateStatusResponse, InvitationResponse, LangGeniusVersionResponse, Member, ModerateResponse, OauthResponse, PluginProvider, Provider, ProviderAnthropicToken, ProviderAzureToken, SetupStatusResponse, UserProfileOriginResponse, } from '@/models/common' import type { UpdateOpenAIKeyResponse, ValidateOpenAIKeyResponse, } from '@/models/app' import type { DefaultModelResponse, Model, ModelItem, ModelLoadBalancingConfig, ModelParameterRule, ModelProvider, ModelTypeEnum, } from '@/app/components/header/account-setting/model-provider-page/declarations' import type { RETRIEVE_METHOD } from '@/types/app' import type { SystemFeatures } from '@/types/feature' type LoginSuccess = { result: 'success' data: { access_token: string;refresh_token: string } } type LoginFail = { result: 'fail' data: string code: string message: string } type LoginResponse = LoginSuccess | LoginFail export const login: Fetcher }> = ({ url, body }) => { return post(url, { body }) as Promise } export const fetchNewToken: Fetcher }> = ({ body }) => { return post('/refresh-token', { body }) as Promise } export const setup: Fetcher }> = ({ body }) => { return post('/setup', { body }) } export const initValidate: Fetcher }> = ({ body }) => { return post('/init', { body }) } export const fetchInitValidateStatus = () => { return get('/init') } export const fetchSetupStatus = () => { return get('/setup') } export const fetchUserProfile: Fetcher }> = ({ url, params }) => { return get(url, params, { needAllResponseContent: true }) } export const updateUserProfile: Fetcher }> = ({ url, body }) => { return post(url, { body }) } export const logout: Fetcher }> = ({ url, params }) => { return get(url, params) } export const fetchLanggeniusVersion: Fetcher }> = ({ url, params }) => { return get(url, { params }) } export const oauth: Fetcher }> = ({ url, params }) => { return get(url, { params }) } export const oneMoreStep: Fetcher }> = ({ url, body }) => { return post(url, { body }) } export const fetchMembers: Fetcher<{ accounts: Member[] | null }, { url: string; params: Record }> = ({ url, params }) => { return get<{ accounts: Member[] | null }>(url, { params }) } export const fetchProviders: Fetcher }> = ({ url, params }) => { return get(url, { params }) } export const validateProviderKey: Fetcher = ({ url, body }) => { return post(url, { body }) } export const updateProviderAIKey: Fetcher = ({ url, body }) => { return post(url, { body }) } export const fetchAccountIntegrates: Fetcher<{ data: AccountIntegrate[] | null }, { url: string; params: Record }> = ({ url, params }) => { return get<{ data: AccountIntegrate[] | null }>(url, { params }) } export const inviteMember: Fetcher }> = ({ url, body }) => { return post(url, { body }) } export const updateMemberRole: Fetcher }> = ({ url, body }) => { return put(url, { body }) } export const deleteMemberOrCancelInvitation: Fetcher = ({ url }) => { return del(url) } export const fetchFilePreview: Fetcher<{ content: string }, { fileID: string }> = ({ fileID }) => { return get<{ content: string }>(`/files/${fileID}/preview`) } export const fetchCurrentWorkspace: Fetcher }> = ({ url, params }) => { return get(url, { params }) } export const updateCurrentWorkspace: Fetcher }> = ({ url, body }) => { return post(url, { body }) } export const fetchWorkspaces: Fetcher<{ workspaces: IWorkspace[] }, { url: string; params: Record }> = ({ url, params }) => { return get<{ workspaces: IWorkspace[] }>(url, { params }) } export const switchWorkspace: Fetcher }> = ({ url, body }) => { return post(url, { body }) } export const fetchDataSource: Fetcher<{ data: DataSourceNotion[] }, { url: string }> = ({ url }) => { return get<{ data: DataSourceNotion[] }>(url) } export const syncDataSourceNotion: Fetcher = ({ url }) => { return get(url) } export const updateDataSourceNotionAction: Fetcher = ({ url }) => { return patch(url) } export const fetchPluginProviders: Fetcher = (url) => { return get(url) } export const validatePluginProviderKey: Fetcher = ({ url, body }) => { return post(url, { body }) } export const updatePluginProviderAIKey: Fetcher = ({ url, body }) => { return post(url, { body }) } export const invitationCheck: Fetcher = ({ url, params }) => { return get(url, { params }) } export const activateMember: Fetcher = ({ url, body }) => { return post(url, { body }) } export const fetchModelProviders: Fetcher<{ data: ModelProvider[] }, string> = (url) => { return get<{ data: ModelProvider[] }>(url) } export type ModelProviderCredentials = { credentials?: Record load_balancing: ModelLoadBalancingConfig } export const fetchModelProviderCredentials: Fetcher = (url) => { return get(url) } export const fetchModelLoadBalancingConfig: Fetcher<{ credentials?: Record load_balancing: ModelLoadBalancingConfig }, string> = (url) => { return get<{ credentials?: Record load_balancing: ModelLoadBalancingConfig }>(url) } export const fetchModelProviderModelList: Fetcher<{ data: ModelItem[] }, string> = (url) => { return get<{ data: ModelItem[] }>(url) } export const fetchModelList: Fetcher<{ data: Model[] }, string> = (url) => { return get<{ data: Model[] }>(url) } export const validateModelProvider: Fetcher = ({ url, body }) => { return post(url, { body }) } export const validateModelLoadBalancingCredentials: Fetcher = ({ url, body }) => { return post(url, { body }) } export const setModelProvider: Fetcher = ({ url, body }) => { return post(url, { body }) } export const deleteModelProvider: Fetcher = ({ url, body }) => { return del(url, { body }) } export const changeModelProviderPriority: Fetcher = ({ url, body }) => { return post(url, { body }) } export const setModelProviderModel: Fetcher = ({ url, body }) => { return post(url, { body }) } export const deleteModelProviderModel: Fetcher = ({ url }) => { return del(url) } export const getPayUrl: Fetcher<{ url: string }, string> = (url) => { return get<{ url: string }>(url) } export const fetchDefaultModal: Fetcher<{ data: DefaultModelResponse }, string> = (url) => { return get<{ data: DefaultModelResponse }>(url) } export const updateDefaultModel: Fetcher = ({ url, body }) => { return post(url, { body }) } export const fetchModelParameterRules: Fetcher<{ data: ModelParameterRule[] }, string> = (url) => { return get<{ data: ModelParameterRule[] }>(url) } export const fetchFileUploadConfig: Fetcher = ({ url }) => { return get(url) } export const fetchFreeQuotaVerify: Fetcher<{ result: string; flag: boolean; reason: string }, string> = (url) => { return get(url) as Promise<{ result: string; flag: boolean; reason: string }> } export const fetchNotionConnection: Fetcher<{ data: string }, string> = (url) => { return get(url) as Promise<{ data: string }> } export const fetchDataSourceNotionBinding: Fetcher<{ result: string }, string> = (url) => { return get(url) as Promise<{ result: string }> } export const fetchApiBasedExtensionList: Fetcher = (url) => { return get(url) as Promise } export const fetchApiBasedExtensionDetail: Fetcher = (url) => { return get(url) as Promise } export const addApiBasedExtension: Fetcher = ({ url, body }) => { return post(url, { body }) as Promise } export const updateApiBasedExtension: Fetcher = ({ url, body }) => { return post(url, { body }) as Promise } export const deleteApiBasedExtension: Fetcher<{ result: string }, string> = (url) => { return del(url) as Promise<{ result: string }> } export const fetchCodeBasedExtensionList: Fetcher = (url) => { return get(url) as Promise } export const moderate = (url: string, body: { app_id: string; text: string }) => { return post(url, { body }) as Promise } type RetrievalMethodsRes = { 'retrieval_method': RETRIEVE_METHOD[] } export const fetchSupportRetrievalMethods: Fetcher = (url) => { return get(url) } export const getSystemFeatures = () => { return get('/system-features') } export const enableModel = (url: string, body: { model: string; model_type: ModelTypeEnum }) => patch(url, { body }) export const disableModel = (url: string, body: { model: string; model_type: ModelTypeEnum }) => patch(url, { body }) export const sendForgotPasswordEmail: Fetcher = ({ url, body }) => post(url, { body }) export const verifyForgotPasswordToken: Fetcher = ({ url, body }) => { return post(url, { body }) as Promise } export const changePasswordWithToken: Fetcher = ({ url, body }) => post(url, { body }) export const fetchRemoteFileInfo = (url: string) => { return get<{ file_type: string; file_length: number }>(`/remote-files/${url}`) } export const sendEMailLoginCode = (email: string, language = 'en-US') => post('/email-code-login', { body: { email, language } }) export const emailLoginWithCode = (data: { email: string;code: string;token: string }) => post('/email-code-login/validity', { body: data }) export const sendResetPasswordCode = (email: string, language = 'en-US') => post('/forgot-password', { body: { email, language } }) export const verifyResetPasswordCode = (body: { email: string;code: string;token: string }) => post('/forgot-password/validity', { body })