|
| 1 | +import type { Logger } from '@slack/logger'; |
| 2 | +import type { ChatAppendStreamArguments, ChatStartStreamArguments, ChatStopStreamArguments } from './types/request'; |
| 3 | +import type { ChatAppendStreamResponse, ChatStartStreamResponse, ChatStopStreamResponse } from './types/response'; |
| 4 | +import type WebClient from './WebClient'; |
| 5 | + |
| 6 | +export interface ChatStreamerOptions { |
| 7 | + /** |
| 8 | + * @description The length of markdown_text to buffer in-memory before calling a method. Increasing this value decreases the number of method calls made for the same amount of text, which is useful to avoid rate limits. |
| 9 | + * @default 256 |
| 10 | + */ |
| 11 | + buffer_size?: number; |
| 12 | +} |
| 13 | + |
| 14 | +export class ChatStreamer { |
| 15 | + private buffer = ''; |
| 16 | + |
| 17 | + private client: WebClient; |
| 18 | + |
| 19 | + private logger: Logger; |
| 20 | + |
| 21 | + private options: Required<ChatStreamerOptions>; |
| 22 | + |
| 23 | + private state: 'starting' | 'in_progress' | 'completed'; |
| 24 | + |
| 25 | + private streamArgs: ChatStartStreamArguments; |
| 26 | + |
| 27 | + private streamTs: string | undefined; |
| 28 | + |
| 29 | + private token: string | undefined; |
| 30 | + |
| 31 | + /** |
| 32 | + * Instantiate a new chat streamer. |
| 33 | + * |
| 34 | + * @description The "constructor" method creates a unique {@link ChatStreamer} instance that keeps track of one chat stream. |
| 35 | + * @example |
| 36 | + * const client = new WebClient(process.env.SLACK_BOT_TOKEN); |
| 37 | + * const logger = new ConsoleLogger(); |
| 38 | + * const args = { |
| 39 | + * channel: "C0123456789", |
| 40 | + * thread_ts: "1700000001.123456", |
| 41 | + * recipient_team_id: "T0123456789", |
| 42 | + * recipient_user_id: "U0123456789", |
| 43 | + * }; |
| 44 | + * const streamer = new ChatStreamer(client, logger, args, { buffer_size: 512 }); |
| 45 | + * await streamer.append({ |
| 46 | + * markdown_text: "**hello world!**", |
| 47 | + * }); |
| 48 | + * await streamer.stop(); |
| 49 | + * @see {@link https://docs.slack.dev/reference/methods/chat.startStream} |
| 50 | + * @see {@link https://docs.slack.dev/reference/methods/chat.appendStream} |
| 51 | + * @see {@link https://docs.slack.dev/reference/methods/chat.stopStream} |
| 52 | + */ |
| 53 | + constructor(client: WebClient, logger: Logger, args: ChatStartStreamArguments, options: ChatStreamerOptions) { |
| 54 | + this.client = client; |
| 55 | + this.logger = logger; |
| 56 | + this.options = { |
| 57 | + buffer_size: options.buffer_size ?? 256, |
| 58 | + }; |
| 59 | + this.state = 'starting'; |
| 60 | + this.streamArgs = args; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Append to a stream. |
| 65 | + * |
| 66 | + * @description The "append" method appends to the chat stream being used. This method can be called multiple times. After the stream is stopped this method cannot be called. |
| 67 | + * @example |
| 68 | + * const streamer = client.chatStream({ |
| 69 | + * channel: "C0123456789", |
| 70 | + * thread_ts: "1700000001.123456", |
| 71 | + * recipient_team_id: "T0123456789", |
| 72 | + * recipient_user_id: "U0123456789", |
| 73 | + * }); |
| 74 | + * await streamer.append({ |
| 75 | + * markdown_text: "**hello wo", |
| 76 | + * }); |
| 77 | + * await streamer.append({ |
| 78 | + * markdown_text: "rld!**", |
| 79 | + * }); |
| 80 | + * await streamer.stop(); |
| 81 | + * @see {@link https://docs.slack.dev/reference/methods/chat.appendStream} |
| 82 | + */ |
| 83 | + async append( |
| 84 | + args: Omit<ChatAppendStreamArguments, 'channel' | 'ts'>, |
| 85 | + ): Promise<ChatStartStreamResponse | ChatAppendStreamResponse | null> { |
| 86 | + if (this.state === 'completed') { |
| 87 | + throw new Error(`failed to append stream: stream state is ${this.state}`); |
| 88 | + } |
| 89 | + if (args.token) { |
| 90 | + this.token = args.token; |
| 91 | + } |
| 92 | + this.buffer += args.markdown_text; |
| 93 | + if (this.buffer.length >= this.options.buffer_size) { |
| 94 | + return await this.flushBuffer(args); |
| 95 | + } |
| 96 | + const details = { |
| 97 | + bufferLength: this.buffer.length, |
| 98 | + bufferSize: this.options.buffer_size, |
| 99 | + channel: this.streamArgs.channel, |
| 100 | + recipientTeamId: this.streamArgs.recipient_team_id, |
| 101 | + recipientUserId: this.streamArgs.recipient_user_id, |
| 102 | + threadTs: this.streamArgs.thread_ts, |
| 103 | + }; |
| 104 | + this.logger.debug(`ChatStreamer appended to buffer: ${JSON.stringify(details)}`); |
| 105 | + return null; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Stop a stream. |
| 110 | + * |
| 111 | + * @description The "stop" method stops the chat stream being used. This method can be called once to end the stream. Additional "blocks" and "metadata" can be provided. |
| 112 | + * |
| 113 | + * @example |
| 114 | + * const streamer = client.chatStream({ |
| 115 | + * channel: "C0123456789", |
| 116 | + * thread_ts: "1700000001.123456", |
| 117 | + * recipient_team_id: "T0123456789", |
| 118 | + * recipient_user_id: "U0123456789", |
| 119 | + * }); |
| 120 | + * await streamer.append({ |
| 121 | + * markdown_text: "**hello world!**", |
| 122 | + * }); |
| 123 | + * await streamer.stop(); |
| 124 | + * @see {@link https://docs.slack.dev/reference/methods/chat.stopStream} |
| 125 | + */ |
| 126 | + async stop(args?: Omit<ChatStopStreamArguments, 'channel' | 'ts'>): Promise<ChatStopStreamResponse> { |
| 127 | + if (this.state === 'completed') { |
| 128 | + throw new Error(`failed to stop stream: stream state is ${this.state}`); |
| 129 | + } |
| 130 | + if (args?.token) { |
| 131 | + this.token = args.token; |
| 132 | + } |
| 133 | + if (!this.streamTs) { |
| 134 | + const response = await this.client.chat.startStream({ |
| 135 | + ...this.streamArgs, |
| 136 | + token: this.token, |
| 137 | + }); |
| 138 | + if (!response.ts) { |
| 139 | + throw new Error('failed to stop stream: stream not started'); |
| 140 | + } |
| 141 | + this.streamTs = response.ts; |
| 142 | + this.state = 'in_progress'; |
| 143 | + } |
| 144 | + const response = await this.client.chat.stopStream({ |
| 145 | + token: this.token, |
| 146 | + channel: this.streamArgs.channel, |
| 147 | + ts: this.streamTs, |
| 148 | + ...args, |
| 149 | + markdown_text: this.buffer + (args?.markdown_text ?? ''), |
| 150 | + }); |
| 151 | + this.state = 'completed'; |
| 152 | + return response; |
| 153 | + } |
| 154 | + |
| 155 | + private async flushBuffer( |
| 156 | + args: Omit<ChatStartStreamArguments | ChatAppendStreamArguments, 'channel' | 'ts'>, |
| 157 | + ): Promise<ChatStartStreamResponse | ChatAppendStreamResponse> { |
| 158 | + if (!this.streamTs) { |
| 159 | + const response = await this.client.chat.startStream({ |
| 160 | + ...this.streamArgs, |
| 161 | + token: this.token, |
| 162 | + ...args, |
| 163 | + markdown_text: this.buffer, |
| 164 | + }); |
| 165 | + this.buffer = ''; |
| 166 | + this.streamTs = response.ts; |
| 167 | + this.state = 'in_progress'; |
| 168 | + return response; |
| 169 | + } |
| 170 | + const response = await this.client.chat.appendStream({ |
| 171 | + token: this.token, |
| 172 | + channel: this.streamArgs.channel, |
| 173 | + ts: this.streamTs, |
| 174 | + ...args, |
| 175 | + markdown_text: this.buffer, |
| 176 | + }); |
| 177 | + this.buffer = ''; |
| 178 | + return response; |
| 179 | + } |
| 180 | +} |
0 commit comments