use-metadata.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. 'use client'
  2. import { useTranslation } from 'react-i18next'
  3. import dayjs from 'dayjs'
  4. import { formatFileSize, formatNumber, formatTime } from '@/utils/format'
  5. import type { DocType } from '@/models/datasets'
  6. export type inputType = 'input' | 'select' | 'textarea'
  7. export type metadataType = DocType | 'originInfo' | 'technicalParameters'
  8. type MetadataMap =
  9. Record<
  10. metadataType,
  11. {
  12. text: string
  13. allowEdit?: boolean
  14. icon?: React.ReactNode
  15. iconName?: string
  16. subFieldsMap: Record<
  17. string,
  18. {
  19. label: string
  20. inputType?: inputType
  21. field?: string
  22. render?: (value: any, total?: number) => React.ReactNode | string
  23. }
  24. >
  25. }
  26. >
  27. const fieldPrefix = 'datasetDocuments.metadata.field'
  28. export const useMetadataMap = (): MetadataMap => {
  29. const { t } = useTranslation()
  30. return {
  31. book: {
  32. text: t('datasetDocuments.metadata.type.book'),
  33. iconName: 'bookOpen',
  34. subFieldsMap: {
  35. title: { label: t(`${fieldPrefix}.book.title`) },
  36. language: {
  37. label: t(`${fieldPrefix}.book.language`),
  38. inputType: 'select',
  39. },
  40. author: { label: t(`${fieldPrefix}.book.author`) },
  41. publisher: { label: t(`${fieldPrefix}.book.publisher`) },
  42. publication_date: { label: t(`${fieldPrefix}.book.publicationDate`) },
  43. isbn: { label: t(`${fieldPrefix}.book.ISBN`) },
  44. category: {
  45. label: t(`${fieldPrefix}.book.category`),
  46. inputType: 'select',
  47. },
  48. },
  49. },
  50. web_page: {
  51. text: t('datasetDocuments.metadata.type.webPage'),
  52. iconName: 'globe',
  53. subFieldsMap: {
  54. 'title': { label: t(`${fieldPrefix}.webPage.title`) },
  55. 'url': { label: t(`${fieldPrefix}.webPage.url`) },
  56. 'language': {
  57. label: t(`${fieldPrefix}.webPage.language`),
  58. inputType: 'select',
  59. },
  60. 'author/publisher': { label: t(`${fieldPrefix}.webPage.authorPublisher`) },
  61. 'publish_date': { label: t(`${fieldPrefix}.webPage.publishDate`) },
  62. 'topics/keywords': { label: t(`${fieldPrefix}.webPage.topicsKeywords`) },
  63. 'description': { label: t(`${fieldPrefix}.webPage.description`) },
  64. },
  65. },
  66. paper: {
  67. text: t('datasetDocuments.metadata.type.paper'),
  68. iconName: 'graduationHat',
  69. subFieldsMap: {
  70. 'title': { label: t(`${fieldPrefix}.paper.title`) },
  71. 'language': {
  72. label: t(`${fieldPrefix}.paper.language`),
  73. inputType: 'select',
  74. },
  75. 'author': { label: t(`${fieldPrefix}.paper.author`) },
  76. 'publish_date': { label: t(`${fieldPrefix}.paper.publishDate`) },
  77. 'journal/conference_name': {
  78. label: t(`${fieldPrefix}.paper.journalConferenceName`),
  79. },
  80. 'volume/issue/page_numbers': { label: t(`${fieldPrefix}.paper.volumeIssuePage`) },
  81. 'doi': { label: t(`${fieldPrefix}.paper.DOI`) },
  82. 'topics/keywords': { label: t(`${fieldPrefix}.paper.topicsKeywords`) },
  83. 'abstract': {
  84. label: t(`${fieldPrefix}.paper.abstract`),
  85. inputType: 'textarea',
  86. },
  87. },
  88. },
  89. social_media_post: {
  90. text: t('datasetDocuments.metadata.type.socialMediaPost'),
  91. iconName: 'atSign',
  92. subFieldsMap: {
  93. 'platform': { label: t(`${fieldPrefix}.socialMediaPost.platform`) },
  94. 'author/username': {
  95. label: t(`${fieldPrefix}.socialMediaPost.authorUsername`),
  96. },
  97. 'publish_date': { label: t(`${fieldPrefix}.socialMediaPost.publishDate`) },
  98. 'post_url': { label: t(`${fieldPrefix}.socialMediaPost.postURL`) },
  99. 'topics/tags': { label: t(`${fieldPrefix}.socialMediaPost.topicsTags`) },
  100. },
  101. },
  102. personal_document: {
  103. text: t('datasetDocuments.metadata.type.personalDocument'),
  104. iconName: 'file',
  105. subFieldsMap: {
  106. 'title': { label: t(`${fieldPrefix}.personalDocument.title`) },
  107. 'author': { label: t(`${fieldPrefix}.personalDocument.author`) },
  108. 'creation_date': {
  109. label: t(`${fieldPrefix}.personalDocument.creationDate`),
  110. },
  111. 'last_modified_date': {
  112. label: t(`${fieldPrefix}.personalDocument.lastModifiedDate`),
  113. },
  114. 'document_type': {
  115. label: t(`${fieldPrefix}.personalDocument.documentType`),
  116. inputType: 'select',
  117. },
  118. 'tags/category': {
  119. label: t(`${fieldPrefix}.personalDocument.tagsCategory`),
  120. },
  121. },
  122. },
  123. business_document: {
  124. text: t('datasetDocuments.metadata.type.businessDocument'),
  125. iconName: 'briefcase',
  126. subFieldsMap: {
  127. 'title': { label: t(`${fieldPrefix}.businessDocument.title`) },
  128. 'author': { label: t(`${fieldPrefix}.businessDocument.author`) },
  129. 'creation_date': {
  130. label: t(`${fieldPrefix}.businessDocument.creationDate`),
  131. },
  132. 'last_modified_date': {
  133. label: t(`${fieldPrefix}.businessDocument.lastModifiedDate`),
  134. },
  135. 'document_type': {
  136. label: t(`${fieldPrefix}.businessDocument.documentType`),
  137. inputType: 'select',
  138. },
  139. 'department/team': {
  140. label: t(`${fieldPrefix}.businessDocument.departmentTeam`),
  141. },
  142. },
  143. },
  144. im_chat_log: {
  145. text: t('datasetDocuments.metadata.type.IMChat'),
  146. iconName: 'messageTextCircle',
  147. subFieldsMap: {
  148. 'chat_platform': { label: t(`${fieldPrefix}.IMChat.chatPlatform`) },
  149. 'chat_participants/group_name': {
  150. label: t(`${fieldPrefix}.IMChat.chatPartiesGroupName`),
  151. },
  152. 'start_date': { label: t(`${fieldPrefix}.IMChat.startDate`) },
  153. 'end_date': { label: t(`${fieldPrefix}.IMChat.endDate`) },
  154. 'participants': { label: t(`${fieldPrefix}.IMChat.participants`) },
  155. 'topicsKeywords': {
  156. label: t(`${fieldPrefix}.IMChat.topicsKeywords`),
  157. inputType: 'textarea',
  158. },
  159. 'fileType': { label: t(`${fieldPrefix}.IMChat.fileType`) },
  160. },
  161. },
  162. wikipedia_entry: {
  163. text: t('datasetDocuments.metadata.type.wikipediaEntry'),
  164. allowEdit: false,
  165. subFieldsMap: {
  166. 'title': { label: t(`${fieldPrefix}.wikipediaEntry.title`) },
  167. 'language': {
  168. label: t(`${fieldPrefix}.wikipediaEntry.language`),
  169. inputType: 'select',
  170. },
  171. 'web_page_url': { label: t(`${fieldPrefix}.wikipediaEntry.webpageURL`) },
  172. 'editor/contributor': {
  173. label: t(`${fieldPrefix}.wikipediaEntry.editorContributor`),
  174. },
  175. 'last_edit_date': {
  176. label: t(`${fieldPrefix}.wikipediaEntry.lastEditDate`),
  177. },
  178. 'summary/introduction': {
  179. label: t(`${fieldPrefix}.wikipediaEntry.summaryIntroduction`),
  180. inputType: 'textarea',
  181. },
  182. },
  183. },
  184. synced_from_notion: {
  185. text: t('datasetDocuments.metadata.type.notion'),
  186. allowEdit: false,
  187. subFieldsMap: {
  188. 'title': { label: t(`${fieldPrefix}.notion.title`) },
  189. 'language': { label: t(`${fieldPrefix}.notion.lang`), inputType: 'select' },
  190. 'author/creator': { label: t(`${fieldPrefix}.notion.author`) },
  191. 'creation_date': { label: t(`${fieldPrefix}.notion.createdTime`) },
  192. 'last_modified_date': {
  193. label: t(`${fieldPrefix}.notion.lastModifiedTime`),
  194. },
  195. 'notion_page_link': { label: t(`${fieldPrefix}.notion.url`) },
  196. 'category/tags': { label: t(`${fieldPrefix}.notion.tag`) },
  197. 'description': { label: t(`${fieldPrefix}.notion.desc`) },
  198. },
  199. },
  200. synced_from_github: {
  201. text: t('datasetDocuments.metadata.type.github'),
  202. allowEdit: false,
  203. subFieldsMap: {
  204. 'repository_name': { label: t(`${fieldPrefix}.github.repoName`) },
  205. 'repository_description': { label: t(`${fieldPrefix}.github.repoDesc`) },
  206. 'repository_owner/organization': { label: t(`${fieldPrefix}.github.repoOwner`) },
  207. 'code_filename': { label: t(`${fieldPrefix}.github.fileName`) },
  208. 'code_file_path': { label: t(`${fieldPrefix}.github.filePath`) },
  209. 'programming_language': { label: t(`${fieldPrefix}.github.programmingLang`) },
  210. 'github_link': { label: t(`${fieldPrefix}.github.url`) },
  211. 'open_source_license': { label: t(`${fieldPrefix}.github.license`) },
  212. 'commit_date': { label: t(`${fieldPrefix}.github.lastCommitTime`) },
  213. 'commit_author': {
  214. label: t(`${fieldPrefix}.github.lastCommitAuthor`),
  215. },
  216. },
  217. },
  218. originInfo: {
  219. text: '',
  220. allowEdit: false,
  221. subFieldsMap: {
  222. 'name': { label: t(`${fieldPrefix}.originInfo.originalFilename`) },
  223. 'data_source_info.upload_file.size': {
  224. label: t(`${fieldPrefix}.originInfo.originalFileSize`),
  225. render: value => formatFileSize(value),
  226. },
  227. 'created_at': {
  228. label: t(`${fieldPrefix}.originInfo.uploadDate`),
  229. render: value => dayjs.unix(value).format(t('datasetDocuments.metadata.dateTimeFormat') as string),
  230. },
  231. 'completed_at': {
  232. label: t(`${fieldPrefix}.originInfo.lastUpdateDate`),
  233. render: value => dayjs.unix(value).format(t('datasetDocuments.metadata.dateTimeFormat') as string),
  234. },
  235. 'data_source_type': {
  236. label: t(`${fieldPrefix}.originInfo.source`),
  237. render: value => t(`datasetDocuments.metadata.source.${value}`),
  238. },
  239. },
  240. },
  241. technicalParameters: {
  242. text: t('datasetDocuments.metadata.type.technicalParameters'),
  243. allowEdit: false,
  244. subFieldsMap: {
  245. 'dataset_process_rule.mode': {
  246. label: t(`${fieldPrefix}.technicalParameters.segmentSpecification`),
  247. render: value => value === 'automatic' ? (t('datasetDocuments.embedding.automatic') as string) : (t('datasetDocuments.embedding.custom') as string),
  248. },
  249. 'dataset_process_rule.rules.segmentation.max_tokens': {
  250. label: t(`${fieldPrefix}.technicalParameters.segmentLength`),
  251. render: value => formatNumber(value),
  252. },
  253. 'average_segment_length': {
  254. label: t(`${fieldPrefix}.technicalParameters.avgParagraphLength`),
  255. render: value => `${formatNumber(value)} characters`,
  256. },
  257. 'segment_count': {
  258. label: t(`${fieldPrefix}.technicalParameters.paragraphs`),
  259. render: value => `${formatNumber(value)} paragraphs`,
  260. },
  261. 'hit_count': {
  262. label: t(`${fieldPrefix}.technicalParameters.hitCount`),
  263. render: (value, total) => {
  264. const v = value || 0
  265. return `${!total ? 0 : ((v / total) * 100).toFixed(2)}% (${v}/${total})`
  266. },
  267. },
  268. 'indexing_latency': {
  269. label: t(`${fieldPrefix}.technicalParameters.embeddingTime`),
  270. render: value => formatTime(value),
  271. },
  272. 'tokens': {
  273. label: t(`${fieldPrefix}.technicalParameters.embeddedSpend`),
  274. render: value => `${formatNumber(value)} tokens`,
  275. },
  276. },
  277. },
  278. }
  279. }
  280. const langPrefix = 'datasetDocuments.metadata.languageMap.'
  281. export const useLanguages = () => {
  282. const { t } = useTranslation()
  283. return {
  284. zh: t(`${langPrefix}zh`),
  285. en: t(`${langPrefix}en`),
  286. es: t(`${langPrefix}es`),
  287. fr: t(`${langPrefix}fr`),
  288. de: t(`${langPrefix}de`),
  289. ja: t(`${langPrefix}ja`),
  290. ko: t(`${langPrefix}ko`),
  291. ru: t(`${langPrefix}ru`),
  292. ar: t(`${langPrefix}ar`),
  293. pt: t(`${langPrefix}pt`),
  294. it: t(`${langPrefix}it`),
  295. nl: t(`${langPrefix}nl`),
  296. pl: t(`${langPrefix}pl`),
  297. sv: t(`${langPrefix}sv`),
  298. tr: t(`${langPrefix}tr`),
  299. he: t(`${langPrefix}he`),
  300. hi: t(`${langPrefix}hi`),
  301. da: t(`${langPrefix}da`),
  302. fi: t(`${langPrefix}fi`),
  303. no: t(`${langPrefix}no`),
  304. hu: t(`${langPrefix}hu`),
  305. el: t(`${langPrefix}el`),
  306. cs: t(`${langPrefix}cs`),
  307. th: t(`${langPrefix}th`),
  308. id: t(`${langPrefix}id`),
  309. }
  310. }
  311. const bookCategoryPrefix = 'datasetDocuments.metadata.categoryMap.book.'
  312. export const useBookCategories = () => {
  313. const { t } = useTranslation()
  314. return {
  315. fiction: t(`${bookCategoryPrefix}fiction`),
  316. biography: t(`${bookCategoryPrefix}biography`),
  317. history: t(`${bookCategoryPrefix}history`),
  318. science: t(`${bookCategoryPrefix}science`),
  319. technology: t(`${bookCategoryPrefix}technology`),
  320. education: t(`${bookCategoryPrefix}education`),
  321. philosophy: t(`${bookCategoryPrefix}philosophy`),
  322. religion: t(`${bookCategoryPrefix}religion`),
  323. socialSciences: t(`${bookCategoryPrefix}socialSciences`),
  324. art: t(`${bookCategoryPrefix}art`),
  325. travel: t(`${bookCategoryPrefix}travel`),
  326. health: t(`${bookCategoryPrefix}health`),
  327. selfHelp: t(`${bookCategoryPrefix}selfHelp`),
  328. businessEconomics: t(`${bookCategoryPrefix}businessEconomics`),
  329. cooking: t(`${bookCategoryPrefix}cooking`),
  330. childrenYoungAdults: t(`${bookCategoryPrefix}childrenYoungAdults`),
  331. comicsGraphicNovels: t(`${bookCategoryPrefix}comicsGraphicNovels`),
  332. poetry: t(`${bookCategoryPrefix}poetry`),
  333. drama: t(`${bookCategoryPrefix}drama`),
  334. other: t(`${bookCategoryPrefix}other`),
  335. }
  336. }
  337. const personalDocCategoryPrefix
  338. = 'datasetDocuments.metadata.categoryMap.personalDoc.'
  339. export const usePersonalDocCategories = () => {
  340. const { t } = useTranslation()
  341. return {
  342. notes: t(`${personalDocCategoryPrefix}notes`),
  343. blogDraft: t(`${personalDocCategoryPrefix}blogDraft`),
  344. diary: t(`${personalDocCategoryPrefix}diary`),
  345. researchReport: t(`${personalDocCategoryPrefix}researchReport`),
  346. bookExcerpt: t(`${personalDocCategoryPrefix}bookExcerpt`),
  347. schedule: t(`${personalDocCategoryPrefix}schedule`),
  348. list: t(`${personalDocCategoryPrefix}list`),
  349. projectOverview: t(`${personalDocCategoryPrefix}projectOverview`),
  350. photoCollection: t(`${personalDocCategoryPrefix}photoCollection`),
  351. creativeWriting: t(`${personalDocCategoryPrefix}creativeWriting`),
  352. codeSnippet: t(`${personalDocCategoryPrefix}codeSnippet`),
  353. designDraft: t(`${personalDocCategoryPrefix}designDraft`),
  354. personalResume: t(`${personalDocCategoryPrefix}personalResume`),
  355. other: t(`${personalDocCategoryPrefix}other`),
  356. }
  357. }
  358. const businessDocCategoryPrefix
  359. = 'datasetDocuments.metadata.categoryMap.businessDoc.'
  360. export const useBusinessDocCategories = () => {
  361. const { t } = useTranslation()
  362. return {
  363. meetingMinutes: t(`${businessDocCategoryPrefix}meetingMinutes`),
  364. researchReport: t(`${businessDocCategoryPrefix}researchReport`),
  365. proposal: t(`${businessDocCategoryPrefix}proposal`),
  366. employeeHandbook: t(`${businessDocCategoryPrefix}employeeHandbook`),
  367. trainingMaterials: t(`${businessDocCategoryPrefix}trainingMaterials`),
  368. requirementsDocument: t(`${businessDocCategoryPrefix}requirementsDocument`),
  369. designDocument: t(`${businessDocCategoryPrefix}designDocument`),
  370. productSpecification: t(`${businessDocCategoryPrefix}productSpecification`),
  371. financialReport: t(`${businessDocCategoryPrefix}financialReport`),
  372. marketAnalysis: t(`${businessDocCategoryPrefix}marketAnalysis`),
  373. projectPlan: t(`${businessDocCategoryPrefix}projectPlan`),
  374. teamStructure: t(`${businessDocCategoryPrefix}teamStructure`),
  375. policiesProcedures: t(`${businessDocCategoryPrefix}policiesProcedures`),
  376. contractsAgreements: t(`${businessDocCategoryPrefix}contractsAgreements`),
  377. emailCorrespondence: t(`${businessDocCategoryPrefix}emailCorrespondence`),
  378. other: t(`${businessDocCategoryPrefix}other`),
  379. }
  380. }