You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.ts 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 getMarketplacePluginsByCollectionId = async (collectionId: string, query?: CollectionsAndPluginsSearchParams) => {
  38. let plugins = [] as Plugin[]
  39. try {
  40. const url = `${MARKETPLACE_API_PREFIX}/collections/${collectionId}/plugins`
  41. const marketplaceCollectionPluginsData = await globalThis.fetch(
  42. url,
  43. {
  44. cache: 'no-store',
  45. method: 'POST',
  46. body: JSON.stringify({
  47. category: query?.category,
  48. exclude: query?.exclude,
  49. type: query?.type,
  50. }),
  51. },
  52. )
  53. const marketplaceCollectionPluginsDataJson = await marketplaceCollectionPluginsData.json()
  54. plugins = marketplaceCollectionPluginsDataJson.data.plugins.map((plugin: Plugin) => {
  55. return getFormattedPlugin(plugin)
  56. })
  57. }
  58. // eslint-disable-next-line unused-imports/no-unused-vars
  59. catch (e) {
  60. plugins = []
  61. }
  62. return plugins
  63. }
  64. export const getMarketplaceCollectionsAndPlugins = async (query?: CollectionsAndPluginsSearchParams) => {
  65. let marketplaceCollections = [] as MarketplaceCollection[]
  66. let marketplaceCollectionPluginsMap = {} as Record<string, Plugin[]>
  67. try {
  68. let marketplaceUrl = `${MARKETPLACE_API_PREFIX}/collections?page=1&page_size=100`
  69. if (query?.condition)
  70. marketplaceUrl += `&condition=${query.condition}`
  71. if (query?.type)
  72. marketplaceUrl += `&type=${query.type}`
  73. const marketplaceCollectionsData = await globalThis.fetch(marketplaceUrl, { cache: 'no-store' })
  74. const marketplaceCollectionsDataJson = await marketplaceCollectionsData.json()
  75. marketplaceCollections = marketplaceCollectionsDataJson.data.collections
  76. await Promise.all(marketplaceCollections.map(async (collection: MarketplaceCollection) => {
  77. const plugins = await getMarketplacePluginsByCollectionId(collection.name, query)
  78. marketplaceCollectionPluginsMap[collection.name] = plugins
  79. }))
  80. }
  81. // eslint-disable-next-line unused-imports/no-unused-vars
  82. catch (e) {
  83. marketplaceCollections = []
  84. marketplaceCollectionPluginsMap = {}
  85. }
  86. return {
  87. marketplaceCollections,
  88. marketplaceCollectionPluginsMap,
  89. }
  90. }
  91. export const getMarketplaceListCondition = (pluginType: string) => {
  92. if (pluginType === PluginType.tool)
  93. return 'category=tool'
  94. if (pluginType === PluginType.agent)
  95. return 'category=agent-strategy'
  96. if (pluginType === PluginType.model)
  97. return 'category=model'
  98. if (pluginType === PluginType.extension)
  99. return 'category=endpoint'
  100. if (pluginType === 'bundle')
  101. return 'type=bundle'
  102. return ''
  103. }
  104. export const getMarketplaceListFilterType = (category: string) => {
  105. if (category === PLUGIN_TYPE_SEARCH_MAP.all)
  106. return undefined
  107. if (category === PLUGIN_TYPE_SEARCH_MAP.bundle)
  108. return 'bundle'
  109. return 'plugin'
  110. }
  111. export const updateSearchParams = (pluginsSearchParams: PluginsSearchParams) => {
  112. const { query, category, tags } = pluginsSearchParams
  113. const url = new URL(window.location.href)
  114. const categoryChanged = url.searchParams.get('category') !== category
  115. if (query)
  116. url.searchParams.set('q', query)
  117. else
  118. url.searchParams.delete('q')
  119. if (category)
  120. url.searchParams.set('category', category)
  121. else
  122. url.searchParams.delete('category')
  123. if (tags && tags.length)
  124. url.searchParams.set('tags', tags.join(','))
  125. else
  126. url.searchParams.delete('tags')
  127. history[`${categoryChanged ? 'pushState' : 'replaceState'}`]({}, '', url)
  128. }