11import type { Logger } from '@slack/logger' ;
2+ import type { AnyChunk } from '@slack/types' ;
23import type { ChatAppendStreamArguments , ChatStartStreamArguments , ChatStopStreamArguments } from './types/request' ;
34import type { ChatAppendStreamResponse , ChatStartStreamResponse , ChatStopStreamResponse } from './types/response' ;
45import type WebClient from './WebClient' ;
@@ -13,19 +14,12 @@ export interface ChatStreamerOptions {
1314
1415export class ChatStreamer {
1516 private buffer = '' ;
16-
1717 private client : WebClient ;
18-
1918 private logger : Logger ;
20-
2119 private options : Required < ChatStreamerOptions > ;
22-
2320 private state : 'starting' | 'in_progress' | 'completed' ;
24-
2521 private streamArgs : ChatStartStreamArguments ;
26-
2722 private streamTs : string | undefined ;
28-
2923 private token : string | undefined ;
3024
3125 /**
@@ -86,12 +80,15 @@ export class ChatStreamer {
8680 if ( this . state === 'completed' ) {
8781 throw new Error ( `failed to append stream: stream state is ${ this . state } ` ) ;
8882 }
89- if ( args . token ) {
90- this . token = args . token ;
83+ const { markdown_text, chunks, ...opts } = args ;
84+ if ( opts . token ) {
85+ this . token = opts . token ;
9186 }
92- this . buffer += args . markdown_text ;
93- if ( this . buffer . length >= this . options . buffer_size ) {
94- return await this . flushBuffer ( args ) ;
87+ if ( markdown_text ) {
88+ this . buffer += markdown_text ;
89+ }
90+ if ( this . buffer . length >= this . options . buffer_size || chunks ) {
91+ return await this . flushBuffer ( { chunks, ...opts } ) ;
9592 }
9693 const details = {
9794 bufferLength : this . buffer . length ,
@@ -127,11 +124,12 @@ export class ChatStreamer {
127124 if ( this . state === 'completed' ) {
128125 throw new Error ( `failed to stop stream: stream state is ${ this . state } ` ) ;
129126 }
130- if ( args ?. token ) {
131- this . token = args . token ;
127+ const { markdown_text, chunks, ...opts } = args ?? { } ;
128+ if ( opts . token ) {
129+ this . token = opts . token ;
132130 }
133- if ( args ?. markdown_text ) {
134- this . buffer += args . markdown_text ;
131+ if ( markdown_text ) {
132+ this . buffer += markdown_text ;
135133 }
136134 if ( ! this . streamTs ) {
137135 const response = await this . client . chat . startStream ( {
@@ -144,12 +142,22 @@ export class ChatStreamer {
144142 this . streamTs = response . ts ;
145143 this . state = 'in_progress' ;
146144 }
145+ const chunksToFlush : AnyChunk [ ] = [ ] ;
146+ if ( this . buffer . length > 0 ) {
147+ chunksToFlush . push ( {
148+ type : 'markdown_text' ,
149+ text : this . buffer ,
150+ } ) ;
151+ }
152+ if ( chunks ) {
153+ chunksToFlush . push ( ...chunks ) ;
154+ }
147155 const response = await this . client . chat . stopStream ( {
148156 token : this . token ,
149157 channel : this . streamArgs . channel ,
150158 ts : this . streamTs ,
151- ... args ,
152- markdown_text : this . buffer ,
159+ chunks : chunksToFlush ,
160+ ... opts ,
153161 } ) ;
154162 this . state = 'completed' ;
155163 return response ;
@@ -158,12 +166,23 @@ export class ChatStreamer {
158166 private async flushBuffer (
159167 args : Omit < ChatStartStreamArguments | ChatAppendStreamArguments , 'channel' | 'ts' > ,
160168 ) : Promise < ChatStartStreamResponse | ChatAppendStreamResponse > {
169+ const { chunks, ...opts } = args ?? { } ;
170+ const chunksToFlush : AnyChunk [ ] = [ ] ;
171+ if ( this . buffer . length > 0 ) {
172+ chunksToFlush . push ( {
173+ type : 'markdown_text' ,
174+ text : this . buffer ,
175+ } ) ;
176+ }
177+ if ( chunks ) {
178+ chunksToFlush . push ( ...chunks ) ;
179+ }
161180 if ( ! this . streamTs ) {
162181 const response = await this . client . chat . startStream ( {
163182 ...this . streamArgs ,
164183 token : this . token ,
165- ... args ,
166- markdown_text : this . buffer ,
184+ chunks : chunksToFlush ,
185+ ... opts ,
167186 } ) ;
168187 this . buffer = '' ;
169188 this . streamTs = response . ts ;
@@ -174,8 +193,8 @@ export class ChatStreamer {
174193 token : this . token ,
175194 channel : this . streamArgs . channel ,
176195 ts : this . streamTs ,
177- ... args ,
178- markdown_text : this . buffer ,
196+ chunks : chunksToFlush ,
197+ ... opts ,
179198 } ) ;
180199 this . buffer = '' ;
181200 return response ;
0 commit comments