@@ -2,11 +2,17 @@ import { RegistryProvider } from "@effect/atom-react";
22import {
33 ApprovalRequestId ,
44 CheckpointRef ,
5+ ComposerContextId ,
56 EnvironmentId ,
67 MessageId ,
78 RunId ,
89 ThreadId ,
910} from "@t3tools/contracts" ;
11+ import {
12+ COMPOSER_CONTEXT_CLIPBOARD_MIME ,
13+ decodeComposerContextClipboardHtml ,
14+ decodeComposerContextFragment ,
15+ } from "@t3tools/shared/composerContextClipboard" ;
1016import type { LegendListRef } from "@legendapp/list/react" ;
1117import { act , createRef , Profiler , useState , type ComponentProps , type ReactElement } from "react" ;
1218import { createRoot , type Root } from "react-dom/client" ;
@@ -154,6 +160,7 @@ beforeEach(() => {
154160
155161afterEach ( async ( ) => {
156162 for ( const root of roots . splice ( 0 ) ) await act ( async ( ) => root . unmount ( ) ) ;
163+ window . getSelection ( ) ?. removeAllRanges ( ) ;
157164 document . body . replaceChildren ( ) ;
158165 vi . unstubAllGlobals ( ) ;
159166} ) ;
@@ -173,6 +180,22 @@ function renderTimeline(entries: ReadonlyArray<Entry>, props: Partial<TimelinePr
173180 ) ;
174181}
175182
183+ async function copyRange ( range : Range ) {
184+ const selection = window . getSelection ( ) ! ;
185+ selection . removeAllRanges ( ) ;
186+ selection . addRange ( range ) ;
187+ const clipboardData = new DataTransfer ( ) ;
188+ const event = new ClipboardEvent ( "copy" , { bubbles : true , cancelable : true , clipboardData } ) ;
189+ const target = range . startContainer . parentElement ! ;
190+ try {
191+ await act ( async ( ) => target . dispatchEvent ( event ) ) ;
192+ expect ( event . defaultPrevented ) . toBe ( true ) ;
193+ return clipboardData ;
194+ } finally {
195+ selection . removeAllRanges ( ) ;
196+ }
197+ }
198+
176199function text ( ) {
177200 return container . textContent ?? "" ;
178201}
@@ -480,6 +503,121 @@ describe("MessagesTimeline work rows", () => {
480503} ) ;
481504
482505describe ( "MessagesTimeline messages" , ( ) => {
506+ it . each ( [
507+ { role : "assistant" , selection : "across" , findExpanded : false } ,
508+ { role : "assistant" , selection : "across" , findExpanded : true } ,
509+ { role : "user" , selection : "across" , findExpanded : false } ,
510+ { role : "assistant" , selection : "inside" , findExpanded : false } ,
511+ { role : "user" , selection : "source" , findExpanded : false } ,
512+ ] as const ) (
513+ "copies Mermaid selection $selection in $role messages (find expanded: $findExpanded)" ,
514+ async ( { role, selection, findExpanded } ) => {
515+ const code = "flowchart LR\n A[Select message] --> B[Copy Markdown]" ;
516+ const source = `\`\`\`mermaid\n${ code } \n\`\`\`` ;
517+ await renderTimeline ( [ message ( `Before diagram.\n\n${ source } \n\nAfter diagram.` , { role } ) ] , {
518+ findExpanded,
519+ } ) ;
520+ const diagram = await vi . waitFor (
521+ ( ) => {
522+ const svg = container . querySelector ( '[aria-label="Expand diagram"] svg' ) ;
523+ expect ( svg ) . not . toBeNull ( ) ;
524+ return svg ! ;
525+ } ,
526+ { timeout : 10_000 } ,
527+ ) ;
528+ const range = document . createRange ( ) ;
529+ let expected = source ;
530+ if ( selection === "across" ) {
531+ const paragraphs = [ ...container . querySelectorAll ( "p" ) ] ;
532+ const before = paragraphs . find ( ( p ) => p . textContent === "Before diagram." ) ! . firstChild ! ;
533+ const after = paragraphs . find ( ( p ) => p . textContent === "After diagram." ) ! . firstChild ! ;
534+ range . setStart ( before , 2 ) ;
535+ range . setEnd ( after , 5 ) ;
536+ expected = `fore diagram.\n\n${ source } \n\nAfter` ;
537+ } else if ( selection === "inside" ) {
538+ const label = [ ...diagram . querySelectorAll ( "p, text" ) ] . find (
539+ ( element ) => element . textContent === "Select message" ,
540+ ) ! ;
541+ const labelText = document . createTreeWalker ( label , NodeFilter . SHOW_TEXT ) . nextNode ( ) ! ;
542+ range . setStart ( labelText , 2 ) ;
543+ range . setEnd ( labelText , 5 ) ;
544+ } else {
545+ await click ( button ( "Show code" ) ) ;
546+ const code = container . querySelector ( "pre code" ) ! ;
547+ const firstToken = document . createTreeWalker ( code , NodeFilter . SHOW_TEXT ) . nextNode ( ) ! ;
548+ range . setStart ( firstToken , 0 ) ;
549+ range . setEnd ( firstToken , 9 ) ;
550+ expected = "flowchart" ;
551+ }
552+
553+ const clipboard = await copyRange ( range ) ;
554+ expect . soft ( clipboard . getData ( "text/plain" ) ) . toBe ( expected ) ;
555+ if ( selection !== "source" ) {
556+ const html = new DOMParser ( ) . parseFromString ( clipboard . getData ( "text/html" ) , "text/html" ) ;
557+ html . querySelectorAll ( "[hidden]" ) . forEach ( ( element ) => element . remove ( ) ) ;
558+ expect ( html . body . textContent ) . toContain ( code ) ;
559+ }
560+ } ,
561+ ) ;
562+
563+ it ( "exports context records only after selecting the chip, not its link in Mermaid source" , async ( ) => {
564+ const record = {
565+ version : 1 as const ,
566+ kind : "image" as const ,
567+ contextId : ComposerContextId . make ( "image-1" ) ,
568+ label : "shot.png" ,
569+ attachmentId : "attachment-1" ,
570+ name : "shot.png" ,
571+ mimeType : "image/png" ,
572+ sizeBytes : 1 ,
573+ } ;
574+ const link = `[shot.png](t3-context://v1/image/${ record . contextId } )` ;
575+ const code = `flowchart LR\n A --> B\n%% ${ link } ` ;
576+ const source = `\`\`\`mermaid\n${ code } \n\`\`\`` ;
577+ await renderTimeline ( [
578+ message ( `Before\n\n${ source } \n\nAfter\n\n${ link } ` , {
579+ context : { version : 1 , records : [ record ] } ,
580+ attachments : [
581+ {
582+ type : "image" ,
583+ id : record . attachmentId ,
584+ name : record . name ,
585+ mimeType : record . mimeType ,
586+ sizeBytes : record . sizeBytes ,
587+ } ,
588+ ] ,
589+ } ) ,
590+ ] ) ;
591+ await vi . waitFor (
592+ ( ) => expect ( container . querySelector ( '[aria-label="Expand diagram"] svg' ) ) . not . toBeNull ( ) ,
593+ { timeout : 10_000 } ,
594+ ) ;
595+ const paragraphs = [ ...container . querySelectorAll ( "p" ) ] ;
596+ const before = paragraphs . find ( ( p ) => p . textContent === "Before" ) ! . firstChild ! ;
597+ const after = paragraphs . find ( ( p ) => p . textContent === "After" ) ! . firstChild ! ;
598+ const chip = button ( / ^ I m a g e a t t a c h m e n t , s h o t \. p n g , / ) ;
599+ const range = document . createRange ( ) ;
600+ range . setStart ( before , 0 ) ;
601+ range . setEnd ( after , 5 ) ;
602+
603+ const withoutChip = await copyRange ( range ) ;
604+ expect ( withoutChip . getData ( "text/plain" ) ) . toBe ( `Before\n\n${ source } \n\nAfter` ) ;
605+ expect ( withoutChip . getData ( COMPOSER_CONTEXT_CLIPBOARD_MIME ) ) . toBe ( "" ) ;
606+ expect ( decodeComposerContextClipboardHtml ( withoutChip . getData ( "text/html" ) ) ) . toBeNull ( ) ;
607+
608+ range . setEndAfter ( chip ) ;
609+ const withChip = await copyRange ( range ) ;
610+ expect ( withChip . getData ( "text/plain" ) ) . toBe ( `Before\n\n${ source } \n\nAfter\n\n!${ link } ` ) ;
611+ expect (
612+ decodeComposerContextFragment ( withChip . getData ( COMPOSER_CONTEXT_CLIPBOARD_MIME ) ) ?. records ,
613+ ) . toEqual ( [ record ] ) ;
614+ expect ( decodeComposerContextClipboardHtml ( withChip . getData ( "text/html" ) ) ?. records ) . toEqual ( [
615+ record ,
616+ ] ) ;
617+ const html = new DOMParser ( ) . parseFromString ( withChip . getData ( "text/html" ) , "text/html" ) ;
618+ expect ( html . body . textContent ) . toContain ( code ) ;
619+ } ) ;
620+
483621 it ( "preserves XML-like tags and comparisons in user messages as text" , async ( ) => {
484622 const source = [
485623 'Without reading a file, do you have <global-agent-instructions scope="workspace">' ,
0 commit comments