apps.ts 8.2 KB

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