Skip to content

Commit 9e504d9

Browse files
Add optional streaming feature using Chat API.
Update package.json Update aws packages to v3.620.0 Fix event streaming bug with ChatAPI. Bugfix here: https://github.com/aws/aws-sdk-js-v3/releases/tag/v3.620.0 Prereq for Chat API feature. Update IAM permissions for Chat API Add Chat to IAM role. Prereq for Chat API streaming feature. Add slack-stream-event lambda handler To allow the optional streaming feature of ChatAPI we add an additional lambda handler. Admin can choose between which endpoint will handle slack events. Modify helper functions to fit both ChatSync & Chat Keep code DRY by not remaking each helper function. 1. Add "commandType" to chat & callClient to determine return type. 2. Modify getResponseAsBlocks inputs to fit Chat API outputstream. 3. Add optional MetadataEvent typing to saveMessageMetadata & getFeedbackBlocks for ChatAPI. Remove `chat` function and directly use client calls 1. Remove chat function to avoid double conditionals 2. Add the sendChatSyncCommand & sendChatCommand functions to dependencies. Add updated unit tests for ChatSync & Chat 1. Add mock responses for Chat API 2. Modify mocks to have two separate calls for ChatSync/Chat. 3. Modify unit tests to fit (2). 4. Create new unit test file for slack-stream-event-handler.ts. All the tests are the same as slack-event-handler.test.ts except for the 5 that start with "Should chat" -> Modified to fit Chat API output. refactor: remove 'any' type from input object in sendChatSyncCommand Update slack event handler to use modified helpers Create slack-stream-event-handler 1. Optional event subscription endpoint for slack. 2. Code is identical to slack-event-handler.ts until line 334. Update READMEs with chat stream feature setup Remove unnecessary variable `latestTextEvent` Previous implementations required the latestTextEvent for future references after loop -> this is no longer required. Update CHANGELOG date Modify `sendChatSyncCommand` and `sendChatCommand` to be thin wrappers 1. Moved the try catch & truncating to amazon-q-helpers.ts & dumbed logic down in amazon-q-client.ts functions to only be thing wrappers. Modify functions to fit `callChatSyncCommand` & `callChatCommand`
1 parent f489d3d commit 9e504d9

16 files changed

+10576
-2309
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [0.3.0] - 2024-08-29
10+
### Added
11+
Add optional streaming response feature.
12+
- New Lambda function queries the Amazon Q Business Chat API and updates the Slack channel in real-time as LLM generates response.
13+
- Can easily switch between ChatSync and Chat response modes by modifying the Slack app's event subscription endpoint.
14+
915
## [0.2.0] - 2024-05-28
1016
### Added
1117
Add support for Q Business Apps integrated with IdC

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,19 @@ Let's now add your app into your workspace, this is required to generate the `Bo
161161
4. Choose `Edit`
162162
5. Replace the value of `OidcClientSecret`, you will find the value in the Okta app client settings (step 1.1).
163163

164+
#### 4.4 (Optional) Configure Slack app to use chat stream feature
165+
166+
To enable the streaming response feature, we need to modify the Slack app.
167+
168+
1. Login to your AWS console
169+
2. In your AWS account go to Cloudformation
170+
3. Copy the URL of the stack output ending with name : `SlackStreamEventHandlerApiEndpoint`.
171+
4. Open the Slack app settings (api.slack.com) and go to the Event Subscriptions section.
172+
5. In the Request URL block, select "change" and paste the copied URL into the "New Request URL" field.
173+
6. Save the changes.
174+
175+
To revert to using the ChatSync feature, follow the same steps, but in step 3, copy the stack output with the name `SlackEventHandlerApiEndpoint` instead.
176+
164177
### Say hello
165178
> Time to say Hi!
166179

README_DEVELOPERS.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,19 @@ Let's now add your app into your workspace, this is required to generate the `Bo
132132
4. Choose `Edit`
133133
5. Replace the value of `OidcClientSecret`, you will find those values in the IdP app client configuration.
134134

135+
#### 4.4 (Optional) Configure Slack app to use chat stream feature
136+
137+
To enable the streaming response feature, we need to modify the Slack app.
138+
139+
1. Login to your AWS console
140+
2. In your AWS account go to Cloudformation
141+
3. Copy the URL of the stack output ending with name : `SlackStreamEventHandlerApiEndpoint`.
142+
4. Open the Slack app settings (api.slack.com) and go to the Event Subscriptions section.
143+
5. In the Request URL block, select "change" and paste the copied URL into the "New Request URL" field.
144+
6. Save the changes.
145+
146+
To revert to using the ChatSync feature, follow the same steps, but in step 3, copy the stack output with the name `SlackEventHandlerApiEndpoint` instead.
147+
135148
### Say hello
136149
> Time to say Hi!
137150

lib/my-amazon-q-slack-bot-stack.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ export class MyAmazonQSlackBotStack extends cdk.Stack {
208208
ChatPolicy: new PolicyDocument({
209209
statements: [
210210
new PolicyStatement({
211-
actions: ['qbusiness:ChatSync', 'qbusiness:PutFeedback'],
211+
actions: ['qbusiness:ChatSync', 'qbusiness:Chat', 'qbusiness:PutFeedback'],
212212
// parametrized
213213
resources: [`arn:aws:qbusiness:*:*:application/${env.AmazonQAppId}`]
214214
})
@@ -249,7 +249,7 @@ export class MyAmazonQSlackBotStack extends cdk.Stack {
249249
ChatPolicy: new PolicyDocument({
250250
statements: [
251251
new PolicyStatement({
252-
actions: ['qbusiness:ChatSync', 'qbusiness:PutFeedback'],
252+
actions: ['qbusiness:ChatSync', 'qbusiness:Chat', 'qbusiness:PutFeedback'],
253253
resources: ['arn:aws:qbusiness:*:*:application/*']
254254
})
255255
]
@@ -316,6 +316,11 @@ export class MyAmazonQSlackBotStack extends cdk.Stack {
316316
handler: 'slack-command-handler',
317317
id: 'SlackCommandHandler',
318318
description: 'Handler for Slack commands'
319+
},
320+
{
321+
handler: 'slack-stream-event-handler',
322+
id: 'SlackStreamEventHandler',
323+
description: 'Handler for Slack events - Streams response'
319324
}
320325
].map((p) => {
321326
const prefix = `${props.stackName}-${p.id}`;

0 commit comments

Comments
 (0)