apps.ts 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import type { Fetcher } from 'swr'
  2. import { del, get, patch, post, put } from './base'
  3. 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'
  4. import type { CommonResponse } from '@/models/common'
  5. import type { AppIconType, AppMode, ModelConfig } from '@/types/app'
  6. import type { TracingProvider } from '@/app/(commonLayout)/app/(appDetailLayout)/[appId]/overview/tracing/type'
  7. export const fetchAppList: Fetcher<AppListResponse, { url: string; params?: Record<string, any> }> = ({ url, params }) => {
  8. return get<AppListResponse>(url, { params })
  9. }
  10. export const fetchAppDetail = ({ url, id }: { url: string; id: string }) => {
  11. return get<AppDetailResponse>(`${url}/${id}`)
  12. }
  13. export const fetchAppSSO = async ({ appId }: { appId: string }) => {
  14. return get<AppSSOResponse>(`/enterprise/app-setting/sso?appID=${appId}`)
  15. }
  16. export const updateAppSSO = async ({ id, enabled }: { id: string; enabled: boolean }) => {
  17. return post('/enterprise/app-setting/sso', { body: { app_id: id, enabled } })
  18. }
  19. export const fetchAppTemplates: Fetcher<AppTemplatesResponse, { url: string }> = ({ url }) => {
  20. return get<AppTemplatesResponse>(url)
  21. }
  22. export const createApp: Fetcher<AppDetailResponse, { name: string; icon_type?: AppIconType; icon?: string; icon_background?: string; mode: AppMode; description?: string; config?: ModelConfig }> = ({ name, icon_type, icon, icon_background, mode, description, config }) => {
  23. return post<AppDetailResponse>('apps', { body: { name, icon_type, icon, icon_background, mode, description, model_config: config } })
  24. }
  25. export const updateAppInfo: Fetcher<AppDetailResponse, { appID: string; name: string; icon_type: AppIconType; icon: string; icon_background?: string; description: string; use_icon_as_answer_icon?: boolean }> = ({ appID, name, icon_type, icon, icon_background, description, use_icon_as_answer_icon }) => {
  26. return put<AppDetailResponse>(`apps/${appID}`, { body: { name, icon_type, icon, icon_background, description, use_icon_as_answer_icon } })
  27. }
  28. export const copyApp: Fetcher<AppDetailResponse, { appID: string; name: string; icon_type: AppIconType; icon: string; icon_background?: string | null; mode: AppMode; description?: string }> = ({ appID, name, icon_type, icon, icon_background, mode, description }) => {
  29. return post<AppDetailResponse>(`apps/${appID}/copy`, { body: { name, icon_type, icon, icon_background, mode, description } })
  30. }
  31. export const exportAppConfig: Fetcher<{ data: string }, { appID: string; include?: boolean }> = ({ appID, include = false }) => {
  32. return get<{ data: string }>(`apps/${appID}/export?include_secret=${include}`)
  33. }
  34. export const importApp: Fetcher<AppDetailResponse, { data: string; name?: string; description?: string; icon_type?: AppIconType; icon?: string; icon_background?: string }> = ({ data, name, description, icon_type, icon, icon_background }) => {
  35. return post<AppDetailResponse>('apps/import', { body: { data, name, description, icon_type, icon, icon_background } })
  36. }
  37. export const importAppFromUrl: Fetcher<AppDetailResponse, { url: string; name?: string; description?: string; icon?: string; icon_background?: string }> = ({ url, name, description, icon, icon_background }) => {
  38. return post<AppDetailResponse>('apps/import/url', { body: { url, name, description, icon, icon_background } })
  39. }
  40. 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 }) => {
  41. return post<{ new_app_id: string }>(`apps/${appID}/convert-to-workflow`, { body: { name, icon_type, icon, icon_background } })
  42. }
  43. export const deleteApp: Fetcher<CommonResponse, string> = (appID) => {
  44. return del<CommonResponse>(`apps/${appID}`)
  45. }
  46. export const updateAppSiteStatus: Fetcher<AppDetailResponse, { url: string; body: Record<string, any> }> = ({ url, body }) => {
  47. return post<AppDetailResponse>(url, { body })
  48. }
  49. export const updateAppApiStatus: Fetcher<AppDetailResponse, { url: string; body: Record<string, any> }> = ({ url, body }) => {
  50. return post<AppDetailResponse>(url, { body })
  51. }
  52. // path: /apps/{appId}/rate-limit
  53. export const updateAppRateLimit: Fetcher<AppDetailResponse, { url: string; body: Record<string, any> }> = ({ url, body }) => {
  54. return post<AppDetailResponse>(url, { body })
  55. }
  56. export const updateAppSiteAccessToken: Fetcher<UpdateAppSiteCodeResponse, { url: string }> = ({ url }) => {
  57. return post<UpdateAppSiteCodeResponse>(url)
  58. }
  59. export const updateAppSiteConfig = ({ url, body }: { url: string; body: Record<string, any> }) => {
  60. return post<AppDetailResponse>(url, { body })
  61. }
  62. export const getAppDailyMessages: Fetcher<AppDailyMessagesResponse, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  63. return get<AppDailyMessagesResponse>(url, { params })
  64. }
  65. export const getAppDailyConversations: Fetcher<AppDailyConversationsResponse, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  66. return get<AppDailyConversationsResponse>(url, { params })
  67. }
  68. export const getWorkflowDailyConversations: Fetcher<WorkflowDailyConversationsResponse, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  69. return get<WorkflowDailyConversationsResponse>(url, { params })
  70. }
  71. export const getAppStatistics: Fetcher<AppStatisticsResponse, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  72. return get<AppStatisticsResponse>(url, { params })
  73. }
  74. export const getAppDailyEndUsers: Fetcher<AppDailyEndUsersResponse, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  75. return get<AppDailyEndUsersResponse>(url, { params })
  76. }
  77. export const getAppTokenCosts: Fetcher<AppTokenCostsResponse, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  78. return get<AppTokenCostsResponse>(url, { params })
  79. }
  80. export const updateAppModelConfig: Fetcher<UpdateAppModelConfigResponse, { url: string; body: Record<string, any> }> = ({ url, body }) => {
  81. return post<UpdateAppModelConfigResponse>(url, { body })
  82. }
  83. // For temp testing
  84. export const fetchAppListNoMock: Fetcher<AppListResponse, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  85. return get<AppListResponse>(url, params)
  86. }
  87. export const fetchApiKeysList: Fetcher<ApiKeysListResponse, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  88. return get<ApiKeysListResponse>(url, params)
  89. }
  90. export const delApikey: Fetcher<CommonResponse, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  91. return del<CommonResponse>(url, params)
  92. }
  93. export const createApikey: Fetcher<CreateApiKeyResponse, { url: string; body: Record<string, any> }> = ({ url, body }) => {
  94. return post<CreateApiKeyResponse>(url, body)
  95. }
  96. export const validateOpenAIKey: Fetcher<ValidateOpenAIKeyResponse, { url: string; body: { token: string } }> = ({ url, body }) => {
  97. return post<ValidateOpenAIKeyResponse>(url, { body })
  98. }
  99. export const updateOpenAIKey: Fetcher<UpdateOpenAIKeyResponse, { url: string; body: { token: string } }> = ({ url, body }) => {
  100. return post<UpdateOpenAIKeyResponse>(url, { body })
  101. }
  102. export const generationIntroduction: Fetcher<GenerationIntroductionResponse, { url: string; body: { prompt_template: string } }> = ({ url, body }) => {
  103. return post<GenerationIntroductionResponse>(url, { body })
  104. }
  105. export const fetchAppVoices: Fetcher<AppVoicesListResponse, { appId: string; language?: string }> = ({ appId, language }) => {
  106. language = language || 'en-US'
  107. return get<AppVoicesListResponse>(`apps/${appId}/text-to-audio/voices?language=${language}`)
  108. }
  109. // Tracing
  110. export const fetchTracingStatus: Fetcher<TracingStatus, { appId: string }> = ({ appId }) => {
  111. return get(`/apps/${appId}/trace`)
  112. }
  113. export const updateTracingStatus: Fetcher<CommonResponse, { appId: string; body: Record<string, any> }> = ({ appId, body }) => {
  114. return post(`/apps/${appId}/trace`, { body })
  115. }
  116. export const fetchTracingConfig: Fetcher<TracingConfig & { has_not_configured: true }, { appId: string; provider: TracingProvider }> = ({ appId, provider }) => {
  117. return get(`/apps/${appId}/trace-config`, {
  118. params: {
  119. tracing_provider: provider,
  120. },
  121. })
  122. }
  123. export const addTracingConfig: Fetcher<CommonResponse, { appId: string; body: TracingConfig }> = ({ appId, body }) => {
  124. return post(`/apps/${appId}/trace-config`, { body })
  125. }
  126. export const updateTracingConfig: Fetcher<CommonResponse, { appId: string; body: TracingConfig }> = ({ appId, body }) => {
  127. return patch(`/apps/${appId}/trace-config`, { body })
  128. }
  129. export const removeTracingConfig: Fetcher<CommonResponse, { appId: string; provider: TracingProvider }> = ({ appId, provider }) => {
  130. return del(`/apps/${appId}/trace-config?tracing_provider=${provider}`)
  131. }