UNPKG

@slack/web-api

Version:

Official library for using the Slack Platform's Web API

193 lines 6.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ChatStreamer = void 0; class ChatStreamer { buffer = ''; client; logger; options; state; streamArgs; streamTs; token; /** * Instantiate a new chat streamer. * * @description The "constructor" method creates a unique {@link ChatStreamer} instance that keeps track of one chat stream. * @example * const client = new WebClient(process.env.SLACK_BOT_TOKEN); * const logger = new ConsoleLogger(); * const args = { * channel: "C0123456789", * thread_ts: "1700000001.123456", * recipient_team_id: "T0123456789", * recipient_user_id: "U0123456789", * }; * const streamer = new ChatStreamer(client, logger, args, { buffer_size: 512 }); * await streamer.append({ * markdown_text: "**hello world!**", * }); * await streamer.stop(); * @see {@link https://docs.slack.dev/reference/methods/chat.startStream} * @see {@link https://docs.slack.dev/reference/methods/chat.appendStream} * @see {@link https://docs.slack.dev/reference/methods/chat.stopStream} */ constructor(client, logger, args, options) { this.client = client; this.logger = logger; this.options = { buffer_size: options.buffer_size ?? 256, }; this.state = 'starting'; this.streamArgs = args; } /** * @description The message timestamp of the stream. Returns `undefined` until the first flush * (when `chat.startStream` is called). * @see {@link https://docs.slack.dev/reference/methods/chat.update} */ get ts() { return this.streamTs; } /** * Append to the stream. * * @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. * @example * const streamer = client.chatStream({ * channel: "C0123456789", * thread_ts: "1700000001.123456", * recipient_team_id: "T0123456789", * recipient_user_id: "U0123456789", * }); * await streamer.append({ * markdown_text: "**hello wo", * }); * await streamer.append({ * markdown_text: "rld!**", * }); * await streamer.stop(); * @see {@link https://docs.slack.dev/reference/methods/chat.appendStream} */ async append(args) { if (this.state === 'completed') { throw new Error(`failed to append stream: stream state is ${this.state}`); } const { markdown_text, chunks, ...opts } = args; if (opts.token) { this.token = opts.token; } if (markdown_text) { this.buffer += markdown_text; } if (this.buffer.length >= this.options.buffer_size || chunks) { return await this.flushBuffer({ chunks, ...opts }); } const details = { bufferLength: this.buffer.length, bufferSize: this.options.buffer_size, channel: this.streamArgs.channel, recipientTeamId: this.streamArgs.recipient_team_id, recipientUserId: this.streamArgs.recipient_user_id, threadTs: this.streamArgs.thread_ts, }; this.logger.debug(`ChatStreamer appended to buffer: ${JSON.stringify(details)}`); return null; } /** * Stop the stream and finalize the message. * * @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. * * @example * const streamer = client.chatStream({ * channel: "C0123456789", * thread_ts: "1700000001.123456", * recipient_team_id: "T0123456789", * recipient_user_id: "U0123456789", * }); * await streamer.append({ * markdown_text: "**hello world!**", * }); * await streamer.stop(); * @see {@link https://docs.slack.dev/reference/methods/chat.stopStream} */ async stop(args) { if (this.state === 'completed') { throw new Error(`failed to stop stream: stream state is ${this.state}`); } const { markdown_text, chunks, ...opts } = args ?? {}; if (opts.token) { this.token = opts.token; } if (markdown_text) { this.buffer += markdown_text; } if (!this.streamTs) { const response = await this.client.chat.startStream({ ...this.streamArgs, token: this.token, }); if (!response.ts) { throw new Error('failed to stop stream: stream not started'); } this.streamTs = response.ts; this.state = 'in_progress'; } const chunksToFlush = []; if (this.buffer.length > 0) { chunksToFlush.push({ type: 'markdown_text', text: this.buffer, }); } if (chunks) { chunksToFlush.push(...chunks); } const response = await this.client.chat.stopStream({ token: this.token, channel: this.streamArgs.channel, ts: this.streamTs, chunks: chunksToFlush, ...opts, }); this.state = 'completed'; return response; } async flushBuffer(args) { const { chunks, ...opts } = args ?? {}; const chunksToFlush = []; if (this.buffer.length > 0) { chunksToFlush.push({ type: 'markdown_text', text: this.buffer, }); } if (chunks) { chunksToFlush.push(...chunks); } if (!this.streamTs) { const response = await this.client.chat.startStream({ ...this.streamArgs, token: this.token, chunks: chunksToFlush, ...opts, }); this.buffer = ''; this.streamTs = response.ts; this.state = 'in_progress'; return response; } const response = await this.client.chat.appendStream({ token: this.token, channel: this.streamArgs.channel, ts: this.streamTs, chunks: chunksToFlush, ...opts, }); this.buffer = ''; return response; } } exports.ChatStreamer = ChatStreamer; //# sourceMappingURL=chat-stream.js.map