Skip to content

Commit 97a67e4

Browse files
committed
Improve prompts and return assitant response when asking for clarification
1 parent 198136b commit 97a67e4

File tree

1 file changed

+36
-14
lines changed

1 file changed

+36
-14
lines changed

integrations/slack/src/actions/inferUserIntent.ts

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,23 @@ export async function inferUserIntentAndTriggerAction(
5353
content: `
5454
You are a Slack assistant designed to help users with their product documentation.
5555
56-
# Instructions:
56+
# Instructions
5757
1. Determine the user's intent from the message.
5858
2. Select the appropriate tool from the available tools listed below based on the user's intent to handle the request.
5959
3. If the intent is unclear, politely ask the user for clarification.
6060
4. Only return freeform text asking for clarification to the user when no tool clearly applies.
6161
62-
# Tools:
62+
# Tools
6363
- **askAIQuery**: Use when the user is clearly asking a question about their product, features, documentation, or content.
64-
- **ingestConversation**: Use to ingest the current Slack thread so that any feedback or information from the conversation can be used to improve the user's documentation. Only call this tool when the user explicitly asks to ingest, learn from the thread to improve their docs.
64+
- **ingestConversation**: Use to ingest the current Slack thread so that any feedback or discussion can be used to improve the user's documentation.
65+
- When a user refers to “this feedback,” “this conversation,” or similar phrases without specifying details, infer that they mean the feedback in the current Slack thread.
66+
- Do **not** ask for clarification unless it’s genuinely ambiguous (for example, if the user mentions feedback from another source).
6567
66-
# Rules:
68+
# Rules
6769
- Always pick the tool that best matches the user's intent.
68-
- Do not make assumptions; base your decision solely on the current message.
69-
- Ask for clarification in a polite, friendly tone if unsure.
70+
- Be concise and polite when asking for clarification.
71+
- Make reasonable inferences from context — for example, “this feedback” refers to the current thread.
72+
- Base decisions solely on the current message and its immediate context.
7073
`,
7174
},
7275
{
@@ -79,18 +82,35 @@ You are a Slack assistant designed to help users with their product documentatio
7982

8083
// If no tool was called, the AI couldn't get the user intent with confidence so ask for clarifications.
8184
if (!result.toolCalls || result.toolCalls.length === 0) {
85+
logger.debug(
86+
'Could not infer intent based on the message, asking for clarification. AI result: ',
87+
JSON.stringify(result.response),
88+
);
89+
90+
const assistantMessages = result.response.messages
91+
.filter((msg) => msg.role === 'assistant')
92+
.flatMap((msg) => {
93+
const { content } = msg;
94+
if (Array.isArray(content)) {
95+
return content;
96+
}
97+
return [
98+
{
99+
type: 'text',
100+
text: content,
101+
},
102+
] as const;
103+
})
104+
.filter((content) => content && content.type === 'text');
105+
const lastAssistantMessage = assistantMessages.pop();
106+
82107
const installation = await getIntegrationInstallationForTeam(context, teamId);
83108
if (!installation) {
84109
throw new Error('Installation not found');
85110
}
86111
const accessToken = (installation.configuration as SlackInstallationConfiguration)
87112
.oauth_credentials?.access_token;
88113

89-
logger.debug(
90-
'Could not infer intent based on the message, asking for clarification. AI result: ',
91-
JSON.stringify(result),
92-
);
93-
94114
await slackAPI(
95115
context,
96116
{
@@ -99,16 +119,18 @@ You are a Slack assistant designed to help users with their product documentatio
99119
payload: {
100120
channel: channelId,
101121
thread_ts: threadTs,
102-
text: `⚠️ Oops, I didn’t quite catch what you want me to do. Could you clarify?`,
122+
text: lastAssistantMessage
123+
? lastAssistantMessage.text
124+
: `⚠️ Oops, I didn’t quite catch what you want me to do. Could you clarify?`,
103125
},
104126
},
105127
{ accessToken },
106128
);
107129

108-
logger.debug('Actions called based on the user message:', JSON.stringify(result.toolCalls));
109-
110130
return;
111131
}
132+
133+
logger.debug('Actions called based on the user message:', JSON.stringify(result.toolCalls));
112134
}
113135

114136
function getActionsTools(

0 commit comments

Comments
 (0)