Skip to content

Commit a9a1579

Browse files
authored
Merge pull request #42 from aws-samples/fix/iam-session-caching
Fix caching Q client issue.
2 parents 613e81d + e772e1e commit a9a1579

File tree

6 files changed

+41
-25
lines changed

6 files changed

+41
-25
lines changed

src/functions/slack-event-handler.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,15 @@ export const handler = async (
332332
}
333333

334334
const [output, slackMessage] = await Promise.all([
335-
chat(prompt, attachments, dependencies, slackEventsEnv, iamSessionCreds, context),
335+
chat(
336+
body.event.user,
337+
prompt,
338+
attachments,
339+
dependencies,
340+
slackEventsEnv,
341+
iamSessionCreds,
342+
context
343+
),
336344
dependencies.sendSlackMessage(
337345
slackEventsEnv,
338346
body.event.channel,

src/functions/slack-interaction-handler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { APIGatewayProxyResult, Callback, Context } from 'aws-lambda';
22
import {
33
createModal,
4-
getMarkdownBlock,
54
getMarkdownBlocks,
65
openModal,
76
SLACK_ACTION,
@@ -193,6 +192,7 @@ export const handler = async (
193192
}
194193

195194
await dependencies.submitFeedbackRequest(
195+
payload.user.id,
196196
slackInteractionsEnv,
197197
iamSessionCreds,
198198
{

src/helpers/amazon-q/amazon-q-client.ts

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,44 @@ import { SlackInteractionsEnv } from '@functions/slack-interaction-handler';
33
import { makeLogger } from '@src/logging';
44
import { v4 as uuid } from 'uuid';
55
import {
6-
QBusinessClient,
6+
AttachmentInput,
77
ChatSyncCommand,
8+
ChatSyncCommandOutput,
9+
MessageUsefulness,
10+
MessageUsefulnessReason,
811
PutFeedbackCommand,
912
PutFeedbackCommandInput,
10-
MessageUsefulnessReason,
11-
MessageUsefulness,
1213
PutFeedbackCommandOutput,
13-
ChatSyncCommandOutput,
14-
AttachmentInput
14+
QBusinessClient
1515
} from '@aws-sdk/client-qbusiness';
1616
import { Credentials } from 'aws-sdk';
1717

1818
const logger = makeLogger('amazon-q-client');
1919

20-
let amazonQClient: QBusinessClient | null = null;
20+
const amazonQClientBySlackUserId: { [key: string]: QBusinessClient } = {};
2121

22-
export const getClient = (env: SlackEventsEnv, iamSessionCreds: Credentials) => {
23-
if (amazonQClient === null) {
24-
logger.debug(`Initiating AmazonQ client with region ${env.AMAZON_Q_REGION}`);
25-
amazonQClient = new QBusinessClient({
26-
credentials: iamSessionCreds,
27-
region: env.AMAZON_Q_REGION
28-
});
22+
export const getClient = (
23+
env: SlackEventsEnv,
24+
slackUserId: string,
25+
iamSessionCreds: Credentials
26+
) => {
27+
logger.debug(`Initiating AmazonQ client with region ${env.AMAZON_Q_REGION}`);
28+
if (amazonQClientBySlackUserId[slackUserId]) {
29+
return amazonQClientBySlackUserId[slackUserId];
2930
}
3031

31-
return amazonQClient;
32+
const newClient = new QBusinessClient({
33+
credentials: iamSessionCreds,
34+
region: env.AMAZON_Q_REGION
35+
});
36+
37+
amazonQClientBySlackUserId[slackUserId] = newClient;
38+
39+
return newClient;
3240
};
3341

3442
export const callClient = async (
43+
slackUserId: string,
3544
message: string,
3645
attachments: AttachmentInput[],
3746
env: SlackEventsEnv,
@@ -50,10 +59,11 @@ export const callClient = async (
5059
};
5160

5261
logger.debug(`callClient input ${JSON.stringify(input)}`);
53-
return await getClient(env, iamSessionCreds).send(new ChatSyncCommand(input));
62+
return await getClient(env, slackUserId, iamSessionCreds).send(new ChatSyncCommand(input));
5463
};
5564

5665
export const submitFeedbackRequest = async (
66+
slackUserId: string,
5767
env: SlackInteractionsEnv,
5868
iamSessionCreds: Credentials,
5969
context: {
@@ -76,7 +86,9 @@ export const submitFeedbackRequest = async (
7686
};
7787

7888
logger.debug(`putFeedbackRequest input ${JSON.stringify(input)}`);
79-
const response = await getClient(env, iamSessionCreds).send(new PutFeedbackCommand(input));
89+
const response = await getClient(env, slackUserId, iamSessionCreds).send(
90+
new PutFeedbackCommand(input)
91+
);
8092
logger.debug(`putFeedbackRequest output ${JSON.stringify(response)}`);
8193

8294
return response;

src/helpers/amazon-q/amazon-q-helpers.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const AMAZON_Q_MSG_LIMIT = 7000;
1515
const WARN_TRUNCATED = `| Please note that you do not have all the conversation history due to limitation`;
1616

1717
export const chat = async (
18+
slackUserId: string,
1819
incomingMessage: string,
1920
attachments: AttachmentInput[],
2021
dependencies: ChatDependencies,
@@ -37,6 +38,7 @@ export const chat = async (
3738
: incomingMessage;
3839

3940
const response = await dependencies.callClient(
41+
slackUserId,
4042
inputMessage,
4143
attachments,
4244
env,

src/helpers/idc/session-helpers.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,6 @@ const exchangeIdPTokenForIAMSessionCreds = async (
277277

278278
const idcIdToken = idcResponse.idToken!;
279279

280-
console.log(`IdC response ${JSON.stringify(idcResponse)}`);
281-
282280
// decode the jwt token
283281
const decodedToken = jwt.decode(idcIdToken, { complete: true });
284282

@@ -294,10 +292,6 @@ const exchangeIdPTokenForIAMSessionCreds = async (
294292
// Extract 'sts:identity-context' claim using type assertion
295293
const identityContext = (decodedToken.payload as Payload)['sts:identity_context'];
296294

297-
console.log(identityContext);
298-
299-
logger.debug(`IdC response ${JSON.stringify(idcResponse)}`);
300-
301295
// call sts assume role
302296
const stsClient = new STS({ region: env.region });
303297
const assumeRoleRequest: AssumeRoleRequest = {

tst/helpers/amazon-q/amazon-q-helpers.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { ChatSyncCommandOutput } from '@aws-sdk/client-qbusiness';
1414

1515
describe('AmazonQ helpers test', () => {
1616
test('Should get a response as block with context', async () => {
17-
const response = await chat('message', [], MOCK_DEPENDENCIES, MOCK_ENV, MOCK_IAM_SESSION_CREDS);
17+
const response = await chat('slackUserId', 'message', [], MOCK_DEPENDENCIES, MOCK_ENV, MOCK_IAM_SESSION_CREDS);
1818
expect(response).toEqual(amazonQValidResponse1);
1919
});
2020

0 commit comments

Comments
 (0)