-
Notifications
You must be signed in to change notification settings - Fork 674
feat: accept chunks as arguments to chat stream helper #2470
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 14 commits
e3d7b9b
2601199
160cfb7
81140cc
dbca9c9
1d480f6
14c9f36
35cceed
6f5f8f1
5e86b29
425b897
076d3a5
0e553a7
fc5ed1a
9d6438e
c5b07ac
b076258
44aa882
032ad03
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| import type { Logger } from '@slack/logger'; | ||
| import type { AnyChunk } from '@slack/types'; | ||
| import type { ChatAppendStreamArguments, ChatStartStreamArguments, ChatStopStreamArguments } from './types/request'; | ||
| import type { ChatAppendStreamResponse, ChatStartStreamResponse, ChatStopStreamResponse } from './types/response'; | ||
| import type WebClient from './WebClient'; | ||
|
|
@@ -89,8 +90,11 @@ export class ChatStreamer { | |
| if (args.token) { | ||
| this.token = args.token; | ||
| } | ||
| this.buffer += args.markdown_text; | ||
| if (this.buffer.length >= this.options.buffer_size) { | ||
| if (args?.markdown_text) { | ||
| this.buffer += args.markdown_text; | ||
| args.markdown_text = undefined; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. note: I pushed commit fc5ed1a to unset the If this is not done, then the @zimeg Does this look correct to you? I noticed this wasn't explicitly done in the Python SDK.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mwbrooks A change similar to other comments might be useful to destructure const { markdown_text, chunks, ...opts } = args;IIRC the arguments alongside |
||
| } | ||
| if (this.buffer.length >= this.options.buffer_size || args?.chunks) { | ||
| return await this.flushBuffer(args); | ||
| } | ||
| const details = { | ||
|
|
@@ -132,6 +136,7 @@ export class ChatStreamer { | |
| } | ||
| if (args?.markdown_text) { | ||
| this.buffer += args.markdown_text; | ||
| args.markdown_text = undefined; | ||
| } | ||
| if (!this.streamTs) { | ||
| const response = await this.client.chat.startStream({ | ||
|
|
@@ -144,12 +149,25 @@ export class ChatStreamer { | |
| this.streamTs = response.ts; | ||
| this.state = 'in_progress'; | ||
| } | ||
|
|
||
| const flushings: AnyChunk[] = []; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🙊 quibble: This variable was changed alike to "chunks to flush" in updates to the Python SDK that we might mirror here? Also unrelated but empty lines might be nice to remove before merging- |
||
|
|
||
| if (this.buffer.length > 0) { | ||
| flushings.push({ | ||
| type: 'markdown_text', | ||
| text: this.buffer, | ||
| }); | ||
| } | ||
| if (args?.chunks) { | ||
| flushings.push(...args.chunks); | ||
| } | ||
|
|
||
| const response = await this.client.chat.stopStream({ | ||
| token: this.token, | ||
| channel: this.streamArgs.channel, | ||
| ts: this.streamTs, | ||
| chunks: flushings, | ||
| ...args, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mwbrooks Thanks so much for catching this! We might want to consider a "destructured" approach to separate const { markdown_text, chunks, ...opts } = args;This might help us avoid overriding new chunks with |
||
| markdown_text: this.buffer, | ||
| }); | ||
| this.state = 'completed'; | ||
| return response; | ||
|
|
@@ -158,12 +176,24 @@ export class ChatStreamer { | |
| private async flushBuffer( | ||
| args: Omit<ChatStartStreamArguments | ChatAppendStreamArguments, 'channel' | 'ts'>, | ||
| ): Promise<ChatStartStreamResponse | ChatAppendStreamResponse> { | ||
| const flushings: AnyChunk[] = []; | ||
|
|
||
| if (this.buffer.length > 0) { | ||
| flushings.push({ | ||
| type: 'markdown_text', | ||
| text: this.buffer, | ||
| }); | ||
| } | ||
| if (args?.chunks) { | ||
| flushings.push(...args.chunks); | ||
| } | ||
|
|
||
| if (!this.streamTs) { | ||
| const response = await this.client.chat.startStream({ | ||
| ...this.streamArgs, | ||
| token: this.token, | ||
| chunks: flushings, | ||
| ...args, | ||
| markdown_text: this.buffer, | ||
| }); | ||
| this.buffer = ''; | ||
| this.streamTs = response.ts; | ||
|
|
@@ -174,8 +204,8 @@ export class ChatStreamer { | |
| token: this.token, | ||
| channel: this.streamArgs.channel, | ||
| ts: this.streamTs, | ||
| chunks: flushings, | ||
| ...args, | ||
| markdown_text: this.buffer, | ||
| }); | ||
| this.buffer = ''; | ||
| return response; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note: I made commit fc5ed1a which updates this test to support
chunksinstead ofmarkdown_textbecause the streaming helper will now convert all markdown text into a chunk.