@@ -383,6 +383,38 @@ function renderInline(text: string, variant: Variant, COLORS: ThemeColors, VS: R
383383 const nodes : React . ReactNode [ ] = [ ]
384384 const codeFontSize = VS [ variant ] . code . fontSize
385385
386+ const scrollToCitationTarget = ( ids : number [ ] ) : boolean => {
387+ if ( typeof document === "undefined" ) return false
388+
389+ const candidates : string [ ] = [ ]
390+ if ( ids . length > 1 ) candidates . push ( "references" )
391+ for ( const id of ids ) {
392+ if ( Number . isFinite ( id ) && id > 0 ) {
393+ candidates . push ( `ref-${ id } ` )
394+ if ( id - 1 >= 0 ) candidates . push ( `ref-${ id - 1 } ` ) // backward-compat for old ref-0 IDs
395+ }
396+ }
397+ candidates . push ( "references" )
398+
399+ const seen = new Set < string > ( )
400+ for ( const c of candidates ) {
401+ if ( ! c || seen . has ( c ) ) continue
402+ seen . add ( c )
403+ const el = document . getElementById ( c )
404+ if ( el ) {
405+ el . scrollIntoView ( { behavior : "smooth" , block : "start" } )
406+ return true
407+ }
408+ }
409+
410+ const byAria = document . querySelector ( 'nav[aria-label="References"]' ) as HTMLElement | null
411+ if ( byAria ) {
412+ byAria . scrollIntoView ( { behavior : "smooth" , block : "start" } )
413+ return true
414+ }
415+ return false
416+ }
417+
386418 let cursor = 0
387419 let key = 0
388420
@@ -429,16 +461,19 @@ function renderInline(text: string, variant: Variant, COLORS: ThemeColors, VS: R
429461 const token = parseOpenAICitationToken ( text , cursor )
430462 if ( token ) {
431463 const citationText = token . ids . length ? `[${ token . ids . join ( "," ) } ]` : "[source]"
432- const isSingle = token . ids . length === 1
433- const targetId = isSingle ? `ref-${ token . ids [ 0 ] } ` : "references"
434464 const canScroll = token . ids . length > 0
435465
436466 if ( canScroll ) {
437467 nodes . push (
438468 < button
439469 key = { `cite-openai-${ key ++ } ` }
440470 type = "button"
441- onClick = { ( ) => document . getElementById ( targetId ) ?. scrollIntoView ( { behavior : "smooth" , block : "start" } ) }
471+ onClick = { ( ) => {
472+ const ok = scrollToCitationTarget ( token . ids )
473+ if ( ! ok && typeof window !== "undefined" ) {
474+ window . alert ( "No reference details are attached to this response." )
475+ }
476+ } }
442477 style = { {
443478 font : "inherit" ,
444479 fontSize : "0.7em" ,
@@ -526,13 +561,20 @@ function renderInline(text: string, variant: Variant, COLORS: ThemeColors, VS: R
526561 const isCitation = / ^ \d + ( [ , \- \s ] \d + ) * $ / . test ( inner . trim ( ) ) && text [ labelEnd + 1 ] !== "("
527562 if ( isCitation ) {
528563 const trimmed = inner . trim ( )
529- const isSingle = / ^ \d + $ / . test ( trimmed )
530- const targetId = isSingle ? `ref-${ trimmed } ` : "references"
564+ const ids = trimmed
565+ . split ( / [ , \- \s ] + / )
566+ . map ( ( v ) => Number ( v ) )
567+ . filter ( ( v ) => Number . isFinite ( v ) && v > 0 )
531568 nodes . push (
532569 < button
533570 key = { `cite-${ key ++ } ` }
534571 type = "button"
535- onClick = { ( ) => document . getElementById ( targetId ) ?. scrollIntoView ( { behavior : "smooth" , block : "start" } ) }
572+ onClick = { ( ) => {
573+ const ok = scrollToCitationTarget ( ids )
574+ if ( ! ok && typeof window !== "undefined" ) {
575+ window . alert ( "No reference details are attached to this response." )
576+ }
577+ } }
536578 style = { {
537579 font : "inherit" ,
538580 fontSize : "0.7em" ,
0 commit comments