您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import { PLUGIN_TYPE_SEARCH_MAP } from './plugin-type-switch'
  2. import type { Plugin } from '@/app/components/plugins/types'
  3. import { PluginType } from '@/app/components/plugins/types'
  4. import type {
  5. CollectionsAndPluginsSearchParams,
  6. MarketplaceCollection,
  7. PluginsSearchParams,
  8. } from '@/app/components/plugins/marketplace/types'
  9. import {
  10. MARKETPLACE_API_PREFIX,
  11. } from '@/config'
  12. import { getMarketplaceUrl } from '@/utils/var'
  13. export const getPluginIconInMarketplace = (plugin: Plugin) => {
  14. if (plugin.type === 'bundle')
  15. return `${MARKETPLACE_API_PREFIX}/bundles/${plugin.org}/${plugin.name}/icon`
  16. return `${MARKETPLACE_API_PREFIX}/plugins/${plugin.org}/${plugin.name}/icon`
  17. }
  18. export const getFormattedPlugin = (bundle: any) => {
  19. if (bundle.type === 'bundle') {
  20. return {
  21. ...bundle,
  22. icon: getPluginIconInMarketplace(bundle),
  23. brief: bundle.description,
  24. label: bundle.labels,
  25. }
  26. }
  27. return {
  28. ...bundle,
  29. icon: getPluginIconInMarketplace(bundle),
  30. }
  31. }
  32. export const getPluginLinkInMarketplace = (plugin: Plugin, params?: Record<string, string | undefined>) => {
  33. if (plugin.type === 'bundle')
  34. return getMarketplaceUrl(`/bundles/${plugin.org}/${plugin.name}`, params)
  35. return getMarketplaceUrl(`/plugins/${plugin.org}/${plugin.name}`, params)
  36. }
  37. export const getPluginDetailLinkInMarketplace = (plugin: Plugin) => {
  38. if (plugin.type === 'bundle')
  39. return `/bundles/${plugin.org}/${plugin.name}`
  40. return `/plugins/${plugin.org}/${plugin.name}`
  41. }
  42. export const getMarketplacePluginsByCollectionId = async (collectionId: string, query?: CollectionsAndPluginsSearchParams) => {
  43. let plugins: Plugin[]
  44. try {
  45. const url = `${MARKETPLACE_API_PREFIX}/collections/${collectionId}/plugins`
  46. const marketplaceCollectionPluginsData = await globalThis.fetch(
  47. url,
  48. {
  49. cache: 'no-store',
  50. method: 'POST',
  51. body: JSON.stringify({
  52. category: query?.category,
  53. exclude: query?.exclude,
  54. type: query?.type,
  55. }),
  56. },
  57. )
  58. const marketplaceCollectionPluginsDataJson = await marketplaceCollectionPluginsData.json()
  59. plugins = marketplaceCollectionPluginsDataJson.data.plugins.map((plugin: Plugin) => {
  60. return getFormattedPlugin(plugin)
  61. })
  62. }
  63. // eslint-disable-next-line unused-imports/no-unused-vars
  64. catch (e) {
  65. plugins = []
  66. }
  67. return plugins
  68. }
  69. export const getMarketplaceCollectionsAndPlugins = async (query?: CollectionsAndPluginsSearchParams) => {
  70. let marketplaceCollections = [] as MarketplaceCollection[]
  71. let marketplaceCollectionPluginsMap = {} as Record<string, Plugin[]>
  72. try {
  73. let marketplaceUrl = `${MARKETPLACE_API_PREFIX}/collections?page=1&page_size=100`
  74. if (query?.condition)
  75. marketplaceUrl += `&condition=${query.condition}`
  76. if (query?.type)
  77. marketplaceUrl += `&type=${query.type}`
  78. const marketplaceCollectionsData = await globalThis.fetch(marketplaceUrl, { cache: 'no-store' })
  79. const marketplaceCollectionsDataJson = await marketplaceCollectionsData.json()
  80. marketplaceCollections = marketplaceCollectionsDataJson.data.collections
  81. await Promise.all(marketplaceCollections.map(async (collection: MarketplaceCollection) => {
  82. const plugins = await getMarketplacePluginsByCollectionId(collection.name, query)
  83. marketplaceCollectionPluginsMap[collection.name] = plugins
  84. }))
  85. }
  86. // eslint-disable-next-line unused-imports/no-unused-vars
  87. catch (e) {
  88. marketplaceCollections = []
  89. marketplaceCollectionPluginsMap = {}
  90. }
  91. return {
  92. marketplaceCollections,
  93. marketplaceCollectionPluginsMap,
  94. }
  95. }
  96. export const getMarketplaceListCondition = (pluginType: string) => {
  97. if (pluginType === PluginType.tool)
  98. return 'category=tool'
  99. if (pluginType === PluginType.agent)
  100. return 'category=agent-strategy'
  101. if (pluginType === PluginType.model)
  102. return 'category=model'
  103. if (pluginType === PluginType.extension)
  104. return 'category=endpoint'
  105. if (pluginType === 'bundle')
  106. return 'type=bundle'
  107. return ''
  108. }
  109. export const getMarketplaceListFilterType = (category: string) => {
  110. if (category === PLUGIN_TYPE_SEARCH_MAP.all)
  111. return undefined
  112. if (category === PLUGIN_TYPE_SEARCH_MAP.bundle)
  113. return 'bundle'
  114. return 'plugin'
  115. }
  116. export const updateSearchParams = (pluginsSearchParams: PluginsSearchParams) => {
  117. const { query, category, tags } = pluginsSearchParams
  118. const url = new URL(window.location.href)
  119. const categoryChanged = url.searchParams.get('category') !== category
  120. if (query)
  121. url.searchParams.set('q', query)
  122. else
  123. url.searchParams.delete('q')
  124. if (category)
  125. url.searchParams.set('category', category)
  126. else
  127. url.searchParams.delete('category')
  128. if (tags && tags.length)
  129. url.searchParams.set('tags', tags.join(','))
  130. else
  131. url.searchParams.delete('tags')
  132. history[`${categoryChanged ? 'pushState' : 'replaceState'}`]({}, '', url)
  133. }