artifacts.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import type { ChatMessage, ConversationArtifact, JsonValue, SessionSummary } from '@shared/types'
  2. export const ARTIFACT_EXTRACTOR_VERSION = 2
  3. const markdownImage = /!\[([^\]]*)\]\(([^)\s]+)\)/gi
  4. const markdownLink = /(?<!!)\[([^\]]+)\]\(([^)\s]+)\)/gi
  5. const rawUrl = /https?:\/\/[^\s<>"')]+/gi
  6. const localPath = /(?:^|[\s(:"'`])((?:\/|~\/|\.\.?\/)[^\s"'`<>]+(?:\.[a-z0-9]{1,8})?)/gim
  7. const imageExtensions = new Set(['avif', 'bmp', 'gif', 'heic', 'heif', 'jpeg', 'jpg', 'png', 'svg', 'tif', 'tiff', 'webp'])
  8. const fileExtensions = new Set([...imageExtensions, 'pdf', 'txt', 'json', 'md', 'csv', 'zip', 'tar', 'gz', 'mp3', 'wav', '3gp', 'avi', 'm4v', 'mkv', 'mov', 'mp4', 'mpeg', 'mpg', 'webm', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx'])
  9. const keyHints = ['path', 'file', 'url', 'image', 'artifact', 'output', 'download', 'result', 'target']
  10. interface Candidate { value: string; label?: string; mimeType?: string; attachment?: ConversationArtifact['attachment'] }
  11. function extension(value: string): string {
  12. try {
  13. const path = new URL(value, window.location.origin).pathname
  14. return path.split('.').pop()?.toLowerCase() ?? ''
  15. } catch { return value.split(/[?#]/)[0].split('.').pop()?.toLowerCase() ?? '' }
  16. }
  17. function normalize(value: string): string {
  18. return value.trim().replace(/[),.;]+$/g, '')
  19. }
  20. function looksLikePathOrUrl(value: string): boolean {
  21. return ['http://', 'https://', 'file://', 'data:image/', '/', './', '../', '~/'].some(prefix => value.startsWith(prefix))
  22. }
  23. function looksLikeArtifact(value: string): boolean {
  24. if (/^(https?:\/\/|data:image\/)/i.test(value)) return true
  25. if (looksLikePathOrUrl(value) && fileExtensions.has(extension(value))) return true
  26. return value.startsWith('/') && value.includes('.')
  27. }
  28. function kind(value: string, mimeType?: string): ConversationArtifact['kind'] {
  29. if (mimeType?.startsWith('image/') || value.startsWith('data:image/') || imageExtensions.has(extension(value))) return 'image'
  30. if (/^(\/|\.\.?\/|~\/|file:\/\/)/.test(value)) return 'file'
  31. return 'link'
  32. }
  33. function label(value: string, artifactKind: ConversationArtifact['kind'], suggested?: string): string {
  34. const clean = suggested?.trim()
  35. if (artifactKind === 'link' && clean && clean !== value) return clean
  36. if (artifactKind === 'link') {
  37. try { return new URL(value).hostname.replace(/^www\./, '') || value } catch { return value }
  38. }
  39. if (value.startsWith('data:image/')) return '生成图片'
  40. try { return decodeURIComponent(new URL(value, window.location.origin).pathname.split('/').pop() || value) } catch { return value.split('/').pop() || value }
  41. }
  42. function textCandidates(text: string): Candidate[] {
  43. const result: Candidate[] = []
  44. for (const match of text.matchAll(markdownImage)) result.push({ value: match[2] })
  45. for (const match of text.matchAll(markdownLink)) result.push({ value: match[2], label: match[1] })
  46. for (const match of text.matchAll(rawUrl)) result.push({ value: match[0] })
  47. for (const match of text.matchAll(localPath)) result.push({ value: match[1] })
  48. return result
  49. }
  50. function collectStringValues(value: JsonValue, keyPath: string, output: Candidate[]): void {
  51. if (typeof value === 'string') {
  52. const normalized = normalize(value)
  53. if ((keyHints.some(hint => keyPath.toLowerCase().includes(hint)) || looksLikePathOrUrl(normalized)) && looksLikeArtifact(normalized)) {
  54. output.push({ value: normalized })
  55. }
  56. return
  57. }
  58. if (Array.isArray(value)) return value.forEach((child, index) => collectStringValues(child, `${keyPath}.${index}`, output))
  59. if (value && typeof value === 'object') {
  60. for (const key of Object.keys(value).sort()) collectStringValues(value[key], `${keyPath}.${key}`, output)
  61. }
  62. }
  63. function candidates(message: ChatMessage): Candidate[] {
  64. const result = textCandidates(message.content)
  65. for (const attachment of message.attachments ?? []) {
  66. const value = attachment.path || attachment.url
  67. if (value) result.push({ value, mimeType: attachment.mimeType, attachment })
  68. }
  69. for (const call of message.toolCalls ?? []) {
  70. if (call.arguments !== undefined) collectStringValues(call.arguments, 'tool.arguments', result)
  71. if (call.result !== undefined) collectStringValues(call.result, 'tool.result', result)
  72. if (call.preview) result.push(...textCandidates(call.preview))
  73. }
  74. if (message.role === 'tool') {
  75. try { collectStringValues(JSON.parse(message.content) as JsonValue, 'tool.result', result) } catch { /* plain tool text is already scanned */ }
  76. }
  77. return result
  78. }
  79. /** Extracts assistant/tool artifacts and intentionally ignores user rows. */
  80. export function extractArtifacts(session: SessionSummary, messages: ChatMessage[]): ConversationArtifact[] {
  81. const found = new Map<string, ConversationArtifact>()
  82. for (const message of messages) {
  83. if (message.role !== 'assistant' && message.role !== 'tool') continue
  84. for (const candidate of candidates(message)) {
  85. const value = normalize(candidate.value)
  86. if (!value || !looksLikeArtifact(value)) continue
  87. const id = `${ARTIFACT_EXTRACTOR_VERSION}:${session.profile ?? ''}:${session.id}:${message.id}:${value}`
  88. if (found.has(id)) continue
  89. const artifactKind = kind(value, candidate.mimeType)
  90. found.set(id, {
  91. id,
  92. kind: artifactKind,
  93. value,
  94. label: label(value, artifactKind, candidate.label),
  95. sessionId: session.id,
  96. sessionTitle: session.title,
  97. profile: session.profile,
  98. messageId: message.id,
  99. timestamp: message.timestamp || session.updatedAt,
  100. mimeType: candidate.mimeType,
  101. attachment: candidate.attachment,
  102. })
  103. }
  104. }
  105. return [...found.values()]
  106. }