index.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <template>
  2. <div>
  3. <page-header :title="$t('aice.llm_image.import_community_image')" />
  4. <a-form-model class="mt-3 mb-2" v-bind="layout">
  5. <a-form-model-item>
  6. <a-radio-group v-model="form.llm_type" size="large">
  7. <a-radio-button v-for="item in typeList" :key="item.value" :value="item.value" style="width:80px;height:80px;text-align:center;line-height:80px;vertical-align:middle;padding:0;">
  8. <img v-if="item.icon" :src="item.icon" style="height:56px;" />
  9. </a-radio-button>
  10. </a-radio-group>
  11. </a-form-model-item>
  12. </a-form-model>
  13. <page-list
  14. :list="list"
  15. :columns="columns"
  16. :single-actions="singleActions"
  17. :showSearchbox="false"
  18. :showGroupActions="false"
  19. :showPage="false" />
  20. </div>
  21. </template>
  22. <script>
  23. import axios from 'axios'
  24. import yaml from 'js-yaml'
  25. import marked from 'marked'
  26. import WindowsMixin from '@/mixins/windows'
  27. import ListMixin from '@/mixins/list'
  28. const LLM_IMAGES_URL = 'http://localhost:3000/llmimages.yaml'
  29. const LLM_TYPE_ICONS = {
  30. openclaw: require('@/assets/images/llm-images/openclaw.svg'),
  31. ollama: require('@/assets/images/llm-images/ollama.svg'),
  32. vllm: require('@/assets/images/llm-images/vllm.svg'),
  33. dify: require('@/assets/images/llm-images/dify.svg'),
  34. comfyui: require('@/assets/images/llm-images/comfyui.svg'),
  35. }
  36. const DEFAULT_ICON = require('@/assets/images/llm-images/default.svg')
  37. function getTypeIcon (llmType) {
  38. return LLM_TYPE_ICONS[llmType] || DEFAULT_ICON
  39. }
  40. function parseImageField (imageStr) {
  41. const idx = imageStr.lastIndexOf(':')
  42. if (idx > 0) {
  43. return { image_name: imageStr.substring(0, idx), image_label: imageStr.substring(idx + 1) }
  44. }
  45. return { image_name: imageStr, image_label: 'latest' }
  46. }
  47. export default {
  48. name: 'LlmImageImportCommunityPage',
  49. mixins: [WindowsMixin, ListMixin],
  50. data () {
  51. return {
  52. allItems: [],
  53. existingImages: {},
  54. layout: {
  55. wrapperCol: { span: 20 },
  56. labelCol: { span: 4 },
  57. },
  58. form: {
  59. llm_type: '',
  60. },
  61. list: this.$list.createList(this, {
  62. id: 'LlmImageImportCommunity',
  63. resource: 'llm_images',
  64. responseData: { data: [] },
  65. }),
  66. }
  67. },
  68. computed: {
  69. typeList () {
  70. const seen = {}
  71. const ret = []
  72. this.allItems.forEach(item => {
  73. const t = item.llm_type
  74. if (t && !seen[t]) {
  75. seen[t] = true
  76. ret.push({ value: t, label: t, icon: getTypeIcon(t) })
  77. }
  78. })
  79. return ret
  80. },
  81. columns () {
  82. return [
  83. {
  84. field: 'image',
  85. title: this.$t('aice.llm_image.name'),
  86. minWidth: 300,
  87. showOverflow: 'tooltip',
  88. },
  89. {
  90. field: 'llm_type',
  91. title: this.$t('aice.llm_type.app'),
  92. width: 120,
  93. slots: {
  94. default: ({ row }, h) => {
  95. return [h('span', this.$t(`aice.llm_type.${row.llm_type}`) || row.llm_type)]
  96. },
  97. },
  98. },
  99. {
  100. field: 'description',
  101. title: this.$t('common.description'),
  102. minWidth: 200,
  103. slots: {
  104. default: ({ row }, h) => {
  105. const html = this.renderMarkdown(row.description)
  106. return [h('div', { class: 'md-desc', domProps: { innerHTML: html } })]
  107. },
  108. },
  109. },
  110. {
  111. field: '_action_import',
  112. title: this.$t('common.action'),
  113. width: 100,
  114. resizable: false,
  115. slots: {
  116. default: ({ row }, h) => {
  117. if (this.isImported(row)) {
  118. return [h('a-button', { props: { size: 'small', disabled: true } }, this.$t('aice.llm_image.imported'))]
  119. }
  120. return [h('a-button', {
  121. props: { type: 'primary', size: 'small' },
  122. on: { click: () => this.handleImport(row) },
  123. }, this.$t('aice.import'))]
  124. },
  125. },
  126. },
  127. ]
  128. },
  129. singleActions () {
  130. return []
  131. },
  132. },
  133. watch: {
  134. 'form.llm_type' (val) {
  135. this.fetchExistingByType(val).then(() => {
  136. this.refreshList()
  137. })
  138. },
  139. typeList (val) {
  140. if (val.length && !val.some(item => item.value === this.form.llm_type)) {
  141. this.form.llm_type = val[0].value
  142. }
  143. },
  144. },
  145. created () {
  146. this.fetchData()
  147. },
  148. methods: {
  149. async fetchExistingByType (llmType) {
  150. if (!llmType) return
  151. try {
  152. const manager = new this.$Manager('llm_images')
  153. const res = await manager.list({ params: { limit: 0, llm_type: llmType } })
  154. const images = res.data?.data || []
  155. // 先清除该类型旧数据
  156. const prefix = `${llmType}:`
  157. Object.keys(this.existingImages).forEach(key => {
  158. if (key.startsWith(prefix)) {
  159. this.$delete(this.existingImages, key)
  160. }
  161. })
  162. // 写入最新数据
  163. images.forEach(img => {
  164. const key = `${img.llm_type}:${img.image_name}:${img.image_label}`
  165. this.$set(this.existingImages, key, true)
  166. })
  167. } catch (e) {
  168. // ignore
  169. }
  170. },
  171. isImported (record) {
  172. return !!this.existingImages[`${record.llm_type}:${record.image_name}:${record.image_label}`]
  173. },
  174. getFilteredItems () {
  175. if (!this.form.llm_type) return this.allItems
  176. return this.allItems.filter(item => item.llm_type === this.form.llm_type)
  177. },
  178. refreshList () {
  179. const filtered = this.getFilteredItems()
  180. this.list.responseData = { data: filtered, total: filtered.length }
  181. this.list.fetchData()
  182. },
  183. async fetchData () {
  184. this.list.loading = true
  185. try {
  186. const res = await axios.get(LLM_IMAGES_URL)
  187. const items = yaml.safeLoad(res.data)
  188. if (!Array.isArray(items)) { this.list.loading = false; return }
  189. this.allItems = items.filter(i => i?.image).map((item, index) => {
  190. const { image_name, image_label } = parseImageField(item.image)
  191. return {
  192. id: String(index + 1),
  193. image: item.image,
  194. image_name,
  195. image_label,
  196. llm_type: item.llm_type || '',
  197. description: item.description || '-',
  198. icon: getTypeIcon(item.llm_type || ''),
  199. }
  200. })
  201. // 查询当前选中类型的已导入镜像
  202. await this.fetchExistingByType(this.form.llm_type)
  203. this.refreshList()
  204. } catch (err) {
  205. this.list.loading = false
  206. this.$message.error(this.$t('aice.llm_image.community_image_fetch_failed'))
  207. throw err
  208. }
  209. },
  210. renderMarkdown (text) {
  211. if (!text || text === '-') return text || '-'
  212. return marked(text)
  213. },
  214. async handleImport (record) {
  215. try {
  216. const createData = {
  217. generate_name: `${record.llm_type}-${record.image_label}`,
  218. llm_type: record.llm_type,
  219. image_name: record.image_name,
  220. image_label: record.image_label,
  221. }
  222. await new this.$Manager('llm_images').create({ data: createData })
  223. this.$set(this.existingImages, `${record.llm_type}:${record.image_name}:${record.image_label}`, true)
  224. this.$message.success(this.$t('common.success'))
  225. } catch (err) {
  226. throw err
  227. }
  228. },
  229. },
  230. }
  231. </script>
  232. <style scoped>
  233. .mt-3 {
  234. margin-top: 12px;
  235. }
  236. .mb-2 {
  237. margin-bottom: 8px;
  238. }
  239. </style>
  240. <style>
  241. .md-desc p {
  242. margin-bottom: 0;
  243. }
  244. .md-desc a {
  245. color: #1890ff;
  246. }
  247. </style>