Skip to content

Commit

Permalink
Add optional streaming feature using Chat API.
Browse files Browse the repository at this point in the history
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`
  • Loading branch information
Dylan-Dipasupil committed Aug 27, 2024
1 parent f489d3d commit 9e504d9
Show file tree
Hide file tree
Showing 16 changed files with 10,576 additions and 2,309 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.3.0] - 2024-08-29
### Added
Add optional streaming response feature.
- New Lambda function queries the Amazon Q Business Chat API and updates the Slack channel in real-time as LLM generates response.
- Can easily switch between ChatSync and Chat response modes by modifying the Slack app's event subscription endpoint.

## [0.2.0] - 2024-05-28
### Added
Add support for Q Business Apps integrated with IdC
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,19 @@ Let's now add your app into your workspace, this is required to generate the `Bo
4. Choose `Edit`
5. Replace the value of `OidcClientSecret`, you will find the value in the Okta app client settings (step 1.1).

#### 4.4 (Optional) Configure Slack app to use chat stream feature

To enable the streaming response feature, we need to modify the Slack app.

1. Login to your AWS console
2. In your AWS account go to Cloudformation
3. Copy the URL of the stack output ending with name : `SlackStreamEventHandlerApiEndpoint`.
4. Open the Slack app settings (api.slack.com) and go to the Event Subscriptions section.
5. In the Request URL block, select "change" and paste the copied URL into the "New Request URL" field.
6. Save the changes.

To revert to using the ChatSync feature, follow the same steps, but in step 3, copy the stack output with the name `SlackEventHandlerApiEndpoint` instead.

### Say hello
> Time to say Hi!
Expand Down
13 changes: 13 additions & 0 deletions README_DEVELOPERS.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,19 @@ Let's now add your app into your workspace, this is required to generate the `Bo
4. Choose `Edit`
5. Replace the value of `OidcClientSecret`, you will find those values in the IdP app client configuration.

#### 4.4 (Optional) Configure Slack app to use chat stream feature

To enable the streaming response feature, we need to modify the Slack app.

1. Login to your AWS console
2. In your AWS account go to Cloudformation
3. Copy the URL of the stack output ending with name : `SlackStreamEventHandlerApiEndpoint`.
4. Open the Slack app settings (api.slack.com) and go to the Event Subscriptions section.
5. In the Request URL block, select "change" and paste the copied URL into the "New Request URL" field.
6. Save the changes.

To revert to using the ChatSync feature, follow the same steps, but in step 3, copy the stack output with the name `SlackEventHandlerApiEndpoint` instead.

### Say hello
> Time to say Hi!
Expand Down
9 changes: 7 additions & 2 deletions lib/my-amazon-q-slack-bot-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ export class MyAmazonQSlackBotStack extends cdk.Stack {
ChatPolicy: new PolicyDocument({
statements: [
new PolicyStatement({
actions: ['qbusiness:ChatSync', 'qbusiness:PutFeedback'],
actions: ['qbusiness:ChatSync', 'qbusiness:Chat', 'qbusiness:PutFeedback'],
// parametrized
resources: [`arn:aws:qbusiness:*:*:application/${env.AmazonQAppId}`]
})
Expand Down Expand Up @@ -249,7 +249,7 @@ export class MyAmazonQSlackBotStack extends cdk.Stack {
ChatPolicy: new PolicyDocument({
statements: [
new PolicyStatement({
actions: ['qbusiness:ChatSync', 'qbusiness:PutFeedback'],
actions: ['qbusiness:ChatSync', 'qbusiness:Chat', 'qbusiness:PutFeedback'],
resources: ['arn:aws:qbusiness:*:*:application/*']
})
]
Expand Down Expand Up @@ -316,6 +316,11 @@ export class MyAmazonQSlackBotStack extends cdk.Stack {
handler: 'slack-command-handler',
id: 'SlackCommandHandler',
description: 'Handler for Slack commands'
},
{
handler: 'slack-stream-event-handler',
id: 'SlackStreamEventHandler',
description: 'Handler for Slack events - Streams response'
}
].map((p) => {
const prefix = `${props.stackName}-${p.id}`;
Expand Down
Loading

0 comments on commit 9e504d9

Please sign in to comment.