LCOV - code coverage report
Current view: top level - src/workbench/extensions/agent/utils - replyAssets.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 2.3 % 130 3
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import type { Token } from 'marked'
       2              : 
       3              : import { isMediaRoute } from '@comfyorg/api-client/sessionMediaUrl'
       4              : import { isCloud } from '@/platform/distribution/types'
       5              : import { api } from '@/scripts/api'
       6              : import { isLoopbackHost } from '@/utils/hostWhitelist'
       7              : import type { AugmentedResultItem } from '@/utils/resultItem'
       8              : import type { MediaType } from '@/utils/formatUtil'
       9              : import { getMediaTypeFromFilename } from '@/utils/formatUtil'
      10              : 
      11              : type ReplyAssetKind = Extract<MediaType, 'image' | 'video' | 'audio' | '3D'>
      12              : 
      13              : export interface ReplyAsset {
      14              :   url: string
      15              :   filename: string
      16              :   kind: ReplyAssetKind
      17              :   label?: string
      18              : }
      19              : 
      20              : export type ReplyVisualAsset = ReplyAsset & {
      21              :   kind: Exclude<ReplyAssetKind, 'audio'>
      22              : }
      23              : 
      24              : export type ReplyAudioAsset = ReplyAsset & { kind: 'audio' }
      25              : 
      26            0 : export function isReplyVisualAsset(
      27              :   asset: ReplyAsset
      28              : ): asset is ReplyVisualAsset {
      29            0 :   return asset.kind !== 'audio'
      30              : }
      31              : 
      32            0 : export function isReplyAudioAsset(asset: ReplyAsset): asset is ReplyAudioAsset {
      33            0 :   return asset.kind === 'audio'
      34              : }
      35              : 
      36            0 : export function isReplyAssetKind(value: MediaType): value is ReplyAssetKind {
      37            0 :   return (
      38              :     value === 'image' ||
      39              :     value === 'video' ||
      40              :     value === 'audio' ||
      41              :     value === '3D'
      42              :   )
      43              : }
      44              : 
      45            0 : function parseUrl(href: string, base?: string): URL | null {
      46            0 :   try {
      47            0 :     return new URL(href, base)
      48              :   } catch {
      49            0 :     return null
      50              :   }
      51              : }
      52              : 
      53              : /**
      54              :  * Moves a loopback ComfyUI media URL onto the page's API route and returns any
      55              :  * other URL unchanged.
      56              :  */
      57            0 : export function resolveAgentAssetUrl(href: string): string {
      58            0 :   const url =
      59              :     parseUrl(href) ??
      60              :     (href.startsWith('//') ? parseUrl(href, window.location.origin) : null)
      61            0 :   if (!url) return href
      62            0 :   if (url.protocol !== 'http:' && url.protocol !== 'https:') return href
      63            0 :   if (!isLoopbackHost(url.hostname)) return href
      64            0 :   if (!isMediaRoute(`${url.pathname}${url.search}`)) return href
      65            0 :   const rehomed = parseUrl(
      66              :     api.apiURL(`${url.pathname}${url.search}`),
      67              :     window.location.origin
      68              :   )
      69            0 :   if (!rehomed) return href
      70            0 :   rehomed.hash = url.hash
      71            0 :   return rehomed.href
      72              : }
      73              : 
      74            1 : const SRCSET_SPACE = /[\t\n\f\r ]/
      75              : 
      76              : interface SrcsetCandidate {
      77              :   url: string
      78              :   descriptor: string
      79              : }
      80              : 
      81            0 : function srcsetCandidates(value: string): SrcsetCandidate[] {
      82            0 :   const candidates: SrcsetCandidate[] = []
      83            0 :   let position = skipSrcsetSeparators(value, 0)
      84            0 :   while (position < value.length) {
      85            0 :     const [rawUrl, afterUrl] = readSrcsetUrl(value, position)
      86            0 :     const closesCandidate = rawUrl.endsWith(',')
      87            0 :     const [descriptor, next] = closesCandidate
      88              :       ? ['', afterUrl]
      89              :       : readSrcsetDescriptor(value, afterUrl)
      90            0 :     const url = closesCandidate ? rawUrl.replace(/,+$/, '') : rawUrl
      91            0 :     if (url) candidates.push({ url, descriptor })
      92            0 :     position = skipSrcsetSeparators(value, next)
      93              :   }
      94            0 :   return candidates
      95              : }
      96              : 
      97            0 : function isSrcsetSpace(c: string): boolean {
      98            0 :   return SRCSET_SPACE.test(c)
      99              : }
     100              : 
     101            0 : function skipSrcsetSeparators(value: string, position: number): number {
     102            0 :   while (
     103              :     position < value.length &&
     104              :     (isSrcsetSpace(value[position]) || value[position] === ',')
     105              :   )
     106            0 :     position++
     107            0 :   return position
     108              : }
     109              : 
     110            0 : function readSrcsetUrl(value: string, position: number): [string, number] {
     111            0 :   const start = position
     112            0 :   while (position < value.length && !isSrcsetSpace(value[position])) position++
     113            0 :   return [value.slice(start, position), position]
     114              : }
     115              : 
     116            0 : function readSrcsetDescriptor(
     117              :   value: string,
     118              :   position: number
     119              : ): [string, number] {
     120            0 :   const start = position
     121            0 :   let inParens = false
     122            0 :   for (; position < value.length; position++) {
     123            0 :     const c = value[position]
     124            0 :     if (c === ',' && !inParens) break
     125            0 :     if (c === '(') inParens = true
     126            0 :     else if (c === ')') inParens = false
     127              :   }
     128            0 :   return [value.slice(start, position).trim(), position + 1]
     129              : }
     130              : 
     131            0 : function resolveSrcset(value: string): string {
     132            0 :   const candidates = srcsetCandidates(value).map((candidate) => ({
     133              :     ...candidate,
     134              :     target: resolveAgentAssetUrl(candidate.url)
     135              :   }))
     136            0 :   if (candidates.every(({ url, target }) => target === url)) return value
     137            0 :   return candidates
     138              :     .map(({ target, descriptor }) =>
     139              :       descriptor ? `${target} ${descriptor}` : target
     140              :     )
     141              :     .join(', ')
     142              : }
     143              : 
     144            1 : const REWRITABLE_ATTRS = ['src', 'href', 'poster', 'srcset'] as const
     145              : 
     146            1 : const MAYBE_MEDIA_ROUTE = /[/\\](?:view|vhs[/\\]|assets[/\\])/
     147              : 
     148            0 : export function rewriteAgentAssetHtml(html: string): string {
     149            0 :   if (!MAYBE_MEDIA_ROUTE.test(html)) return html
     150            0 :   const template = document.createElement('template')
     151            0 :   template.innerHTML = html
     152            0 :   let rewritten = false
     153            0 :   for (const element of template.content.querySelectorAll(
     154              :     REWRITABLE_ATTRS.map((name) => `[${name}]`).join(', ')
     155              :   )) {
     156            0 :     for (const name of REWRITABLE_ATTRS) {
     157            0 :       const value = element.getAttribute(name)
     158            0 :       if (value === null) continue
     159            0 :       const resolved =
     160              :         name === 'srcset' ? resolveSrcset(value) : resolveAgentAssetUrl(value)
     161            0 :       if (resolved === value) continue
     162            0 :       element.setAttribute(name, resolved)
     163            0 :       rewritten = true
     164              :     }
     165              :   }
     166            0 :   return rewritten ? template.innerHTML : html
     167              : }
     168              : 
     169            0 : export function classifyAssetUrl(
     170              :   href: string,
     171              :   baseUrl = window.location.origin
     172              : ): ReplyAsset | null {
     173            0 :   let url: URL
     174            0 :   try {
     175            0 :     url = new URL(href, baseUrl)
     176              :   } catch {
     177            0 :     return null
     178              :   }
     179            0 :   let filename = url.searchParams.get('filename')
     180            0 :   try {
     181            0 :     filename ??= decodeURIComponent(url.pathname.split('/').at(-1) ?? '')
     182              :   } catch {
     183            0 :     return null
     184              :   }
     185            0 :   if (!filename) return null
     186            0 :   const kind = getMediaTypeFromFilename(filename)
     187            0 :   if (!isReplyAssetKind(kind)) return null
     188            0 :   return { url: resolveAgentAssetUrl(href), filename, kind }
     189              : }
     190              : 
     191              : type InlineToken = { type: string; href?: string; text?: string }
     192              : 
     193              : interface InlineScan {
     194              :   assets: ReplyAsset[]
     195              :   sawImageSyntax: boolean
     196              : }
     197              : 
     198            0 : function scanInline(tokens: InlineToken[] | undefined): InlineScan | null {
     199            0 :   const scan: InlineScan = { assets: [], sawImageSyntax: false }
     200            0 :   for (const token of tokens ?? []) {
     201            0 :     if (token.type === 'image' || token.type === 'link') {
     202            0 :       const asset = token.href ? classifyAssetUrl(token.href) : null
     203            0 :       if (!asset) return null
     204            0 :       if (token.type === 'image') {
     205            0 :         scan.sawImageSyntax = true
     206            0 :         if (token.text) asset.label = token.text
     207              :       }
     208            0 :       scan.assets.push(asset)
     209            0 :     } else if (token.type === 'br' || token.type === 'space') {
     210            0 :       continue
     211            0 :     } else if (token.type === 'text' && (token.text ?? '').trim() === '') {
     212            0 :       continue
     213              :     } else {
     214            0 :       return null
     215              :     }
     216              :   }
     217            0 :   return scan.assets.length ? scan : null
     218              : }
     219              : 
     220              : // A lone image LINK keeps FE-1328's link contract; image syntax, non-image
     221              : // singles, and any multiple become the DES-530 asset treatment.
     222            0 : function selectAssets(scan: InlineScan): ReplyAsset[] | null {
     223            0 :   const { assets, sawImageSyntax } = scan
     224            0 :   if (assets.length > 1 || sawImageSyntax) return assets
     225            0 :   return assets[0].kind === 'image' ? null : assets
     226              : }
     227              : 
     228              : /* A block is an asset block only when every inline token is a media link. */
     229            0 : export function tokenReplyAssets(token: Token): ReplyAsset[] | null {
     230            0 :   if (token.type === 'paragraph') {
     231            0 :     const scan = scanInline(token.tokens)
     232            0 :     return scan ? selectAssets(scan) : null
     233              :   }
     234            0 :   if (token.type !== 'list') return null
     235              : 
     236            0 :   const combined: InlineScan = { assets: [], sawImageSyntax: false }
     237            0 :   for (const item of token.items) {
     238            0 :     for (const block of item.tokens as InlineToken[]) {
     239            0 :       if (block.type !== 'text' && block.type !== 'paragraph') return null
     240            0 :       const scan = scanInline((block as { tokens?: InlineToken[] }).tokens)
     241            0 :       if (!scan) return null
     242            0 :       combined.assets.push(...scan.assets)
     243            0 :       combined.sawImageSyntax ||= scan.sawImageSyntax
     244              :     }
     245              :   }
     246            0 :   return combined.assets.length ? selectAssets(combined) : null
     247              : }
     248              : 
     249            0 : export function htmlReplyAssets(html: string): ReplyAsset[] {
     250            0 :   const doc = new DOMParser().parseFromString(html, 'text/html')
     251            0 :   const seen = new Set<string>()
     252            0 :   const out: ReplyAsset[] = []
     253            0 :   for (const element of doc.querySelectorAll('a[href], img[src]')) {
     254              :     const href =
     255              :       element.getAttribute('href') ?? element.getAttribute('src') ?? ''
     256            0 :     const asset = classifyAssetUrl(href)
     257            0 :     if (asset && !seen.has(asset.url)) {
     258            0 :       seen.add(asset.url)
     259            0 :       out.push(asset)
     260              :     }
     261              :   }
     262            0 :   return out
     263              : }
     264              : 
     265            0 : export function replyAssetResultItem(asset: ReplyAsset): AugmentedResultItem {
     266            0 :   return {
     267              :     filename: asset.filename,
     268              :     subfolder: '',
     269              :     type: 'output',
     270              :     nodeId: '',
     271              :     mediaType: asset.kind === 'image' ? 'images' : asset.kind,
     272              :     mediaTypeIsResolved: true,
     273              :     url: asset.url,
     274              :     filenameIsDisplayName: isCloud
     275              :   }
     276              : }
        

Generated by: LCOV version 2.0-1