Skip to content

Commit b72b434

Browse files
committed
Additional perf improvement
1 parent 7d7dad6 commit b72b434

File tree

1 file changed

+52
-46
lines changed

1 file changed

+52
-46
lines changed

integrations/slack/src/actions/inferUserIntent.ts

Lines changed: 52 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -45,44 +45,49 @@ export async function inferUserIntentAndTriggerAction(
4545

4646
logger.debug('Infering user intent from message', userMessage);
4747

48-
const installation = await getIntegrationInstallationForTeam(context, teamId);
49-
if (!installation) {
50-
throw new Error('Installation not found');
51-
}
52-
const installationConfiguration = installation.configuration as SlackInstallationConfiguration;
53-
const accessToken = installationConfiguration.oauth_credentials?.access_token;
54-
const accessTokenScopes = installationConfiguration.oauth_credentials?.requested_scopes || [];
55-
56-
// Installations created before the Docs Agents feature was released may not have a token with the
57-
// required scope to call this API. Since this is non-blocking, we skip the API call if the token lacks
58-
// the necessary permission.
59-
if (accessTokenScopes.includes('assistant:write')) {
60-
await slackAPI(
61-
context,
62-
{
63-
method: 'POST',
64-
path: 'assistant.threads.setStatus',
65-
payload: {
66-
channel_id: channelId,
67-
thread_ts: threadTs,
68-
status: 'is working on your request...',
69-
loading_messages: [
70-
'Reading your message…',
71-
'Understanding your request…',
72-
'Determining the best action…',
73-
],
74-
},
75-
},
76-
{ accessToken },
77-
);
78-
}
79-
80-
const result = await generateText({
81-
model: openai('gpt-5-nano'),
82-
messages: [
83-
{
84-
role: 'system',
85-
content: `
48+
const [accessToken, result] = await Promise.all([
49+
(async () => {
50+
const installation = await getIntegrationInstallationForTeam(context, teamId);
51+
if (!installation) {
52+
throw new Error('Installation not found');
53+
}
54+
const installationConfiguration =
55+
installation.configuration as SlackInstallationConfiguration;
56+
const accessToken = installationConfiguration.oauth_credentials?.access_token;
57+
const accessTokenScopes =
58+
installationConfiguration.oauth_credentials?.requested_scopes || [];
59+
60+
// Installations created before the Docs Agents feature was released may not have a token with the
61+
// required scope to call this API. Since this is non-blocking, we skip the API call if the token lacks
62+
// the necessary permission.
63+
if (accessTokenScopes.includes('assistant:write')) {
64+
await slackAPI(
65+
context,
66+
{
67+
method: 'POST',
68+
path: 'assistant.threads.setStatus',
69+
payload: {
70+
channel_id: channelId,
71+
thread_ts: threadTs,
72+
status: 'is working on your request...',
73+
loading_messages: [
74+
'Reading your message…',
75+
'Understanding your request…',
76+
'Determining the best action…',
77+
],
78+
},
79+
},
80+
{ accessToken },
81+
);
82+
}
83+
return accessToken;
84+
})(),
85+
generateText({
86+
model: openai('gpt-5-nano'),
87+
messages: [
88+
{
89+
role: 'system',
90+
content: `
8691
You are a Slack assistant designed to help users with their product documentation.
8792
8893
# Instructions
@@ -103,14 +108,15 @@ You are a Slack assistant designed to help users with their product documentatio
103108
- Make reasonable inferences from context — for example, “this feedback” refers to the current thread.
104109
- Base decisions solely on the current message and its immediate context.
105110
`,
106-
},
107-
{
108-
role: 'user',
109-
content: userMessage,
110-
},
111-
],
112-
tools,
113-
});
111+
},
112+
{
113+
role: 'user',
114+
content: userMessage,
115+
},
116+
],
117+
tools,
118+
}),
119+
]);
114120

115121
// If no tool was called, the AI couldn't get the user intent with confidence so ask for clarifications.
116122
if (!result.toolCalls || result.toolCalls.length === 0) {

0 commit comments

Comments
 (0)