Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import type { Fetcher } from 'swr'
  2. import { del, get, patch, post, put } from './base'
  3. import type { ApiKeysListResponse, AppDailyConversationsResponse, AppDailyEndUsersResponse, AppDailyMessagesResponse, AppDetailResponse, AppListResponse, 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 fetchAppTemplates: Fetcher<AppTemplatesResponse, { url: string }> = ({ url }) => {
  14. return get<AppTemplatesResponse>(url)
  15. }
  16. 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 }) => {
  17. return post<AppDetailResponse>('apps', { body: { name, icon_type, icon, icon_background, mode, description, model_config: config } })
  18. }
  19. 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; max_active_requests?: number | null }> = ({ appID, name, icon_type, icon, icon_background, description, use_icon_as_answer_icon, max_active_requests }) => {
  20. const body = { name, icon_type, icon, icon_background, description, use_icon_as_answer_icon, max_active_requests }
  21. return put<AppDetailResponse>(`apps/${appID}`, { body })
  22. }
  23. 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 }) => {
  24. return post<AppDetailResponse>(`apps/${appID}/copy`, { body: { name, icon_type, icon, icon_background, mode, description } })
  25. }
  26. export const exportAppConfig: Fetcher<{ data: string }, { appID: string; include?: boolean }> = ({ appID, include = false }) => {
  27. return get<{ data: string }>(`apps/${appID}/export?include_secret=${include}`)
  28. }
  29. // TODO: delete
  30. 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 }) => {
  31. return post<AppDetailResponse>('apps/import', { body: { data, name, description, icon_type, icon, icon_background } })
  32. }
  33. // TODO: delete
  34. export const importAppFromUrl: Fetcher<AppDetailResponse, { url: string; name?: string; description?: string; icon?: string; icon_background?: string }> = ({ url, name, description, icon, icon_background }) => {
  35. return post<AppDetailResponse>('apps/import/url', { body: { url, name, description, icon, icon_background } })
  36. }
  37. 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 }) => {
  38. return post<DSLImportResponse>('apps/imports', { body: { mode, yaml_content, yaml_url, app_id, name, description, icon, icon_type, icon_background } })
  39. }
  40. export const importDSLConfirm: Fetcher<DSLImportResponse, { import_id: string }> = ({ import_id }) => {
  41. return post<DSLImportResponse>(`apps/imports/${import_id}/confirm`, { body: {} })
  42. }
  43. 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 }) => {
  44. return post<{ new_app_id: string }>(`apps/${appID}/convert-to-workflow`, { body: { name, icon_type, icon, icon_background } })
  45. }
  46. export const deleteApp: Fetcher<CommonResponse, string> = (appID) => {
  47. return del<CommonResponse>(`apps/${appID}`)
  48. }
  49. export const fetchAppWithTags = async (appID: string) => {
  50. try {
  51. const appListResponse = await fetchAppList({
  52. url: '/apps',
  53. params: { page: 1, limit: 100 },
  54. })
  55. const appWithTags = appListResponse.data.find(app => app.id === appID)
  56. return appWithTags || null
  57. }
  58. catch (error) {
  59. console.warn('Failed to fetch app with tags:', error)
  60. return null
  61. }
  62. }
  63. export const updateAppSiteStatus: Fetcher<AppDetailResponse, { url: string; body: Record<string, any> }> = ({ url, body }) => {
  64. return post<AppDetailResponse>(url, { body })
  65. }
  66. export const updateAppApiStatus: Fetcher<AppDetailResponse, { url: string; body: Record<string, any> }> = ({ url, body }) => {
  67. return post<AppDetailResponse>(url, { body })
  68. }
  69. // path: /apps/{appId}/rate-limit
  70. export const updateAppRateLimit: Fetcher<AppDetailResponse, { url: string; body: Record<string, any> }> = ({ url, body }) => {
  71. return post<AppDetailResponse>(url, { body })
  72. }
  73. export const updateAppSiteAccessToken: Fetcher<UpdateAppSiteCodeResponse, { url: string }> = ({ url }) => {
  74. return post<UpdateAppSiteCodeResponse>(url)
  75. }
  76. export const updateAppSiteConfig = ({ url, body }: { url: string; body: Record<string, any> }) => {
  77. return post<AppDetailResponse>(url, { body })
  78. }
  79. export const getAppDailyMessages: Fetcher<AppDailyMessagesResponse, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  80. return get<AppDailyMessagesResponse>(url, { params })
  81. }
  82. export const getAppDailyConversations: Fetcher<AppDailyConversationsResponse, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  83. return get<AppDailyConversationsResponse>(url, { params })
  84. }
  85. export const getWorkflowDailyConversations: Fetcher<WorkflowDailyConversationsResponse, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  86. return get<WorkflowDailyConversationsResponse>(url, { params })
  87. }
  88. export const getAppStatistics: Fetcher<AppStatisticsResponse, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  89. return get<AppStatisticsResponse>(url, { params })
  90. }
  91. export const getAppDailyEndUsers: Fetcher<AppDailyEndUsersResponse, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  92. return get<AppDailyEndUsersResponse>(url, { params })
  93. }
  94. export const getAppTokenCosts: Fetcher<AppTokenCostsResponse, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  95. return get<AppTokenCostsResponse>(url, { params })
  96. }
  97. export const updateAppModelConfig: Fetcher<UpdateAppModelConfigResponse, { url: string; body: Record<string, any> }> = ({ url, body }) => {
  98. return post<UpdateAppModelConfigResponse>(url, { body })
  99. }
  100. // For temp testing
  101. export const fetchAppListNoMock: Fetcher<AppListResponse, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  102. return get<AppListResponse>(url, params)
  103. }
  104. export const fetchApiKeysList: Fetcher<ApiKeysListResponse, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  105. return get<ApiKeysListResponse>(url, params)
  106. }
  107. export const delApikey: Fetcher<CommonResponse, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  108. return del<CommonResponse>(url, params)
  109. }
  110. export const createApikey: Fetcher<CreateApiKeyResponse, { url: string; body: Record<string, any> }> = ({ url, body }) => {
  111. return post<CreateApiKeyResponse>(url, body)
  112. }
  113. export const validateOpenAIKey: Fetcher<ValidateOpenAIKeyResponse, { url: string; body: { token: string } }> = ({ url, body }) => {
  114. return post<ValidateOpenAIKeyResponse>(url, { body })
  115. }
  116. export const updateOpenAIKey: Fetcher<UpdateOpenAIKeyResponse, { url: string; body: { token: string } }> = ({ url, body }) => {
  117. return post<UpdateOpenAIKeyResponse>(url, { body })
  118. }
  119. export const generationIntroduction: Fetcher<GenerationIntroductionResponse, { url: string; body: { prompt_template: string } }> = ({ url, body }) => {
  120. return post<GenerationIntroductionResponse>(url, { body })
  121. }
  122. export const fetchAppVoices: Fetcher<AppVoicesListResponse, { appId: string; language?: string }> = ({ appId, language }) => {
  123. language = language || 'en-US'
  124. return get<AppVoicesListResponse>(`apps/${appId}/text-to-audio/voices?language=${language}`)
  125. }
  126. // Tracing
  127. export const fetchTracingStatus: Fetcher<TracingStatus, { appId: string }> = ({ appId }) => {
  128. return get(`/apps/${appId}/trace`)
  129. }
  130. export const updateTracingStatus: Fetcher<CommonResponse, { appId: string; body: Record<string, any> }> = ({ appId, body }) => {
  131. return post(`/apps/${appId}/trace`, { body })
  132. }
  133. export const fetchTracingConfig: Fetcher<TracingConfig & { has_not_configured: true }, { appId: string; provider: TracingProvider }> = ({ appId, provider }) => {
  134. return get(`/apps/${appId}/trace-config`, {
  135. params: {
  136. tracing_provider: provider,
  137. },
  138. })
  139. }
  140. export const addTracingConfig: Fetcher<CommonResponse, { appId: string; body: TracingConfig }> = ({ appId, body }) => {
  141. return post(`/apps/${appId}/trace-config`, { body })
  142. }
  143. export const updateTracingConfig: Fetcher<CommonResponse, { appId: string; body: TracingConfig }> = ({ appId, body }) => {
  144. return patch(`/apps/${appId}/trace-config`, { body })
  145. }
  146. export const removeTracingConfig: Fetcher<CommonResponse, { appId: string; provider: TracingProvider }> = ({ appId, provider }) => {
  147. return del(`/apps/${appId}/trace-config?tracing_provider=${provider}`)
  148. }