@@ -6,7 +6,7 @@ namespace AIStudio.Tools;
66public static class ContentStreamSseHandler
77{
88 private static readonly ConcurrentDictionary < string , List < ContentStreamPptxImageData > > CHUNKED_IMAGES = new ( ) ;
9- private static readonly ConcurrentDictionary < string , int > CURRENT_SLIDE_NUMBERS = new ( ) ;
9+ private static readonly ConcurrentDictionary < string , SlideManager > SLIDE_MANAGERS = new ( ) ;
1010
1111 public static string ? ProcessEvent ( ContentStreamSseEvent ? sseEvent , bool extractImages = true )
1212 {
@@ -44,31 +44,13 @@ public static class ContentStreamSseHandler
4444 return sseEvent . Content ;
4545
4646 case ContentStreamPresentationMetadata presentationMetadata :
47- var slideNumber = presentationMetadata . Presentation ? . SlideNumber ?? 0 ;
48- var image = presentationMetadata . Presentation ? . Image ?? null ;
49- var presentationResult = new StringBuilder ( ) ;
50- var streamId = sseEvent . StreamId ;
47+ var slideManager = SLIDE_MANAGERS . GetOrAdd (
48+ sseEvent . StreamId ! ,
49+ _ => new ( )
50+ ) ;
5151
52- CURRENT_SLIDE_NUMBERS . TryGetValue ( streamId ! , out var currentSlideNumber ) ;
53- if ( slideNumber != currentSlideNumber )
54- {
55- presentationResult . AppendLine ( ) ;
56- presentationResult . AppendLine ( $ "# Slide { slideNumber } ") ;
57- }
58-
59- if ( ! string . IsNullOrWhiteSpace ( sseEvent . Content ) )
60- presentationResult . AppendLine ( sseEvent . Content ) ;
61-
62- if ( extractImages && image is not null )
63- {
64- var imageId = $ "{ streamId } -{ image . Id ! } ";
65- var isEnd = ProcessImageSegment ( imageId , image ) ;
66- if ( isEnd && extractImages )
67- presentationResult . AppendLine ( BuildImage ( imageId ) ) ;
68- }
69-
70- CURRENT_SLIDE_NUMBERS [ streamId ! ] = slideNumber ;
71- return presentationResult . Length is 0 ? null : presentationResult . ToString ( ) ;
52+ slideManager . AddSlide ( presentationMetadata , sseEvent . Content , extractImages ) ;
53+ return null ;
7254
7355 default :
7456 return sseEvent . Content ;
@@ -81,8 +63,8 @@ public static class ContentStreamSseHandler
8163 return null ;
8264 }
8365 }
84-
85- private static bool ProcessImageSegment ( string imageId , ContentStreamPptxImageData contentStreamPptxImageData )
66+
67+ public static bool ProcessImageSegment ( string imageId , ContentStreamPptxImageData contentStreamPptxImageData )
8668 {
8769 if ( string . IsNullOrWhiteSpace ( contentStreamPptxImageData . Id ) || string . IsNullOrWhiteSpace ( imageId ) )
8870 return false ;
@@ -112,7 +94,7 @@ private static bool ProcessImageSegment(string imageId, ContentStreamPptxImageDa
11294 return isEnd ;
11395 }
11496
115- private static string BuildImage ( string id )
97+ public static string BuildImage ( string id )
11698 {
11799 if ( ! CHUNKED_IMAGES . TryGetValue ( id , out var imageSegments ) )
118100 return string . Empty ;
@@ -128,4 +110,25 @@ private static string BuildImage(string id)
128110 CHUNKED_IMAGES . Remove ( id , out _ ) ;
129111 return base64Image ;
130112 }
113+
114+ public static string ? Clear ( string streamId )
115+ {
116+ if ( string . IsNullOrWhiteSpace ( streamId ) )
117+ return null ;
118+
119+ var finalContentChunk = new StringBuilder ( ) ;
120+ if ( SLIDE_MANAGERS . TryGetValue ( streamId , out var slideManager ) )
121+ {
122+ var result = slideManager . GetAllSlidesInOrder ( ) ;
123+ if ( ! string . IsNullOrWhiteSpace ( result ) )
124+ finalContentChunk . Append ( result ) ;
125+ }
126+
127+ SLIDE_MANAGERS . TryRemove ( streamId , out _ ) ;
128+ var imageIdPrefix = $ "{ streamId } -";
129+ foreach ( var key in CHUNKED_IMAGES . Keys . Where ( k => k . StartsWith ( imageIdPrefix , StringComparison . InvariantCultureIgnoreCase ) ) )
130+ CHUNKED_IMAGES . TryRemove ( key , out _ ) ;
131+
132+ return finalContentChunk . Length > 0 ? finalContentChunk . ToString ( ) : null ;
133+ }
131134}
0 commit comments