apps.ts 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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, DSLImportMode, DSLImportResponse, 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. // TODO: delete
  35. 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 }) => {
  36. return post<AppDetailResponse>('apps/import', { body: { data, name, description, icon_type, icon, icon_background } })
  37. }
  38. // TODO: delete
  39. export const importAppFromUrl: Fetcher<AppDetailResponse, { url: string; name?: string; description?: string; icon?: string; icon_background?: string }> = ({ url, name, description, icon, icon_background }) => {
  40. return post<AppDetailResponse>('apps/import/url', { body: { url, name, description, icon, icon_background } })
  41. }
  42. export const importDSL: Fetcher<DSLImportResponse, { mode: DSLImportMode; yaml_content?: string; yaml_url?: string; app_id?: string; name?: string; description?: string; icon_type?: AppIconType; icon?: string; icon_background?: string }> = ({ mode, yaml_content, yaml_url, app_id, name, description, icon_type, icon, icon_background }) => {
  43. return post<DSLImportResponse>('apps/imports', { body: { mode, yaml_content, yaml_url, app_id, name, description, icon, icon_type, icon_background } })
  44. }
  45. export const importDSLConfirm: Fetcher<DSLImportResponse, { import_id: string }> = ({ import_id }) => {
  46. return post<DSLImportResponse>(`apps/imports/${import_id}/confirm`, { body: {} })
  47. }
  48. 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 }) => {
  49. return post<{ new_app_id: string }>(`apps/${appID}/convert-to-workflow`, { body: { name, icon_type, icon, icon_background } })
  50. }
  51. export const deleteApp: Fetcher<CommonResponse, string> = (appID) => {
  52. return del<CommonResponse>(`apps/${appID}`)
  53. }
  54. export const updateAppSiteStatus: Fetcher<AppDetailResponse, { url: string; body: Record<string, any> }> = ({ url, body }) => {
  55. return post<AppDetailResponse>(url, { body })
  56. }
  57. export const updateAppApiStatus: Fetcher<AppDetailResponse, { url: string; body: Record<string, any> }> = ({ url, body }) => {
  58. return post<AppDetailResponse>(url, { body })
  59. }
  60. // path: /apps/{appId}/rate-limit
  61. export const updateAppRateLimit: Fetcher<AppDetailResponse, { url: string; body: Record<string, any> }> = ({ url, body }) => {
  62. return post<AppDetailResponse>(url, { body })
  63. }
  64. export const updateAppSiteAccessToken: Fetcher<UpdateAppSiteCodeResponse, { url: string }> = ({ url }) => {
  65. return post<UpdateAppSiteCodeResponse>(url)
  66. }
  67. export const updateAppSiteConfig = ({ url, body }: { url: string; body: Record<string, any> }) => {
  68. return post<AppDetailResponse>(url, { body })
  69. }
  70. export const getAppDailyMessages: Fetcher<AppDailyMessagesResponse, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  71. return get<AppDailyMessagesResponse>(url, { params })
  72. }
  73. export const getAppDailyConversations: Fetcher<AppDailyConversationsResponse, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  74. return get<AppDailyConversationsResponse>(url, { params })
  75. }
  76. export const getWorkflowDailyConversations: Fetcher<WorkflowDailyConversationsResponse, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  77. return get<WorkflowDailyConversationsResponse>(url, { params })
  78. }
  79. export const getAppStatistics: Fetcher<AppStatisticsResponse, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  80. return get<AppStatisticsResponse>(url, { params })
  81. }
  82. export const getAppDailyEndUsers: Fetcher<AppDailyEndUsersResponse, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  83. return get<AppDailyEndUsersResponse>(url, { params })
  84. }
  85. export const getAppTokenCosts: Fetcher<AppTokenCostsResponse, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  86. return get<AppTokenCostsResponse>(url, { params })
  87. }
  88. export const updateAppModelConfig: Fetcher<UpdateAppModelConfigResponse, { url: string; body: Record<string, any> }> = ({ url, body }) => {
  89. return post<UpdateAppModelConfigResponse>(url, { body })
  90. }
  91. // For temp testing
  92. export const fetchAppListNoMock: Fetcher<AppListResponse, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  93. return get<AppListResponse>(url, params)
  94. }
  95. export const fetchApiKeysList: Fetcher<ApiKeysListResponse, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  96. return get<ApiKeysListResponse>(url, params)
  97. }
  98. export const delApikey: Fetcher<CommonResponse, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  99. return del<CommonResponse>(url, params)
  100. }
  101. export const createApikey: Fetcher<CreateApiKeyResponse, { url: string; body: Record<string, any> }> = ({ url, body }) => {
  102. return post<CreateApiKeyResponse>(url, body)
  103. }
  104. export const validateOpenAIKey: Fetcher<ValidateOpenAIKeyResponse, { url: string; body: { token: string } }> = ({ url, body }) => {
  105. return post<ValidateOpenAIKeyResponse>(url, { body })
  106. }
  107. export const updateOpenAIKey: Fetcher<UpdateOpenAIKeyResponse, { url: string; body: { token: string } }> = ({ url, body }) => {
  108. return post<UpdateOpenAIKeyResponse>(url, { body })
  109. }
  110. export const generationIntroduction: Fetcher<GenerationIntroductionResponse, { url: string; body: { prompt_template: string } }> = ({ url, body }) => {
  111. return post<GenerationIntroductionResponse>(url, { body })
  112. }
  113. export const fetchAppVoices: Fetcher<AppVoicesListResponse, { appId: string; language?: string }> = ({ appId, language }) => {
  114. language = language || 'en-US'
  115. return get<AppVoicesListResponse>(`apps/${appId}/text-to-audio/voices?language=${language}`)
  116. }
  117. // Tracing
  118. export const fetchTracingStatus: Fetcher<TracingStatus, { appId: string }> = ({ appId }) => {
  119. return get(`/apps/${appId}/trace`)
  120. }
  121. export const updateTracingStatus: Fetcher<CommonResponse, { appId: string; body: Record<string, any> }> = ({ appId, body }) => {
  122. return post(`/apps/${appId}/trace`, { body })
  123. }
  124. export const fetchTracingConfig: Fetcher<TracingConfig & { has_not_configured: true }, { appId: string; provider: TracingProvider }> = ({ appId, provider }) => {
  125. return get(`/apps/${appId}/trace-config`, {
  126. params: {
  127. tracing_provider: provider,
  128. },
  129. })
  130. }
  131. export const addTracingConfig: Fetcher<CommonResponse, { appId: string; body: TracingConfig }> = ({ appId, body }) => {
  132. return post(`/apps/${appId}/trace-config`, { body })
  133. }
  134. export const updateTracingConfig: Fetcher<CommonResponse, { appId: string; body: TracingConfig }> = ({ appId, body }) => {
  135. return patch(`/apps/${appId}/trace-config`, { body })
  136. }
  137. export const removeTracingConfig: Fetcher<CommonResponse, { appId: string; provider: TracingProvider }> = ({ appId, provider }) => {
  138. return del(`/apps/${appId}/trace-config?tracing_provider=${provider}`)
  139. }