Skip to content

Commit 4c61ed2

Browse files
authored
refactor(web-api): align chat stream implementation with python sdk (#2388)
1 parent de8e9ec commit 4c61ed2

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

packages/web-api/src/WebClient.spec.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import fs from 'node:fs';
2+
import type { ContextActionsBlock } from '@slack/types';
23
import axios, { type InternalAxiosRequestConfig } from 'axios';
34
import { assert, expect } from 'chai';
45
import nock, { type ReplyHeaders } from 'nock';
@@ -1245,6 +1246,36 @@ describe('WebClient', () => {
12451246
});
12461247

12471248
it('streams a long message', async () => {
1249+
const contextActionsBlock: ContextActionsBlock = {
1250+
type: 'context_actions',
1251+
elements: [
1252+
{
1253+
type: 'feedback_buttons',
1254+
positive_button: {
1255+
text: {
1256+
type: 'plain_text',
1257+
text: 'good',
1258+
},
1259+
value: '+1',
1260+
},
1261+
negative_button: {
1262+
text: {
1263+
type: 'plain_text',
1264+
text: 'bad',
1265+
},
1266+
value: '-1',
1267+
},
1268+
},
1269+
{
1270+
type: 'icon_button',
1271+
icon: 'trash',
1272+
text: {
1273+
type: 'plain_text',
1274+
text: 'delete',
1275+
},
1276+
},
1277+
],
1278+
};
12481279
const scope = nock('https://slack.com')
12491280
.post('/api/chat.startStream', {
12501281
channel: 'C0123456789',
@@ -1267,6 +1298,7 @@ describe('WebClient', () => {
12671298
ok: true,
12681299
})
12691300
.post('/api/chat.stopStream', {
1301+
blocks: JSON.stringify([contextActionsBlock]),
12701302
channel: 'C0123456789',
12711303
markdown_text: '**',
12721304
token: 'xoxb-updated-2',
@@ -1296,6 +1328,7 @@ describe('WebClient', () => {
12961328
markdown_text: '*',
12971329
});
12981330
await streamer.stop({
1331+
blocks: [contextActionsBlock],
12991332
markdown_text: '*',
13001333
token: 'xoxb-updated-2',
13011334
});

packages/web-api/src/chat-stream.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export class ChatStreamer {
6161
}
6262

6363
/**
64-
* Append to a stream.
64+
* Append to the stream.
6565
*
6666
* @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.
6767
* @example
@@ -106,7 +106,7 @@ export class ChatStreamer {
106106
}
107107

108108
/**
109-
* Stop a stream.
109+
* Stop the stream and finalize the message.
110110
*
111111
* @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.
112112
*
@@ -130,6 +130,9 @@ export class ChatStreamer {
130130
if (args?.token) {
131131
this.token = args.token;
132132
}
133+
if (args?.markdown_text) {
134+
this.buffer += args.markdown_text;
135+
}
133136
if (!this.streamTs) {
134137
const response = await this.client.chat.startStream({
135138
...this.streamArgs,
@@ -146,7 +149,7 @@ export class ChatStreamer {
146149
channel: this.streamArgs.channel,
147150
ts: this.streamTs,
148151
...args,
149-
markdown_text: this.buffer + (args?.markdown_text ?? ''),
152+
markdown_text: this.buffer,
150153
});
151154
this.state = 'completed';
152155
return response;

0 commit comments

Comments
 (0)