Skip to content

Commit ff7eee7

Browse files
refactor(compass-assistant): clean up follow-up questions parsing and prompt format COMPASS-10712
- Share FOLLOW_UP_QUESTIONS_HEADER constant between prompt template and parser - Simplify parseFollowUpQuestions to use indexOf instead of regex for header detection - Specify explicit numbered format in the prompt so AI output is predictable - Clean up comments
1 parent 7ac053d commit ff7eee7

2 files changed

Lines changed: 8 additions & 20 deletions

File tree

packages/compass-assistant/src/components/follow-up-prompts.tsx

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@ import { FOLLOW_UP_QUESTIONS_HEADER } from '../prompts';
99

1010
const { MessagePrompts, MessagePrompt } = LgChatMessagePrompts;
1111

12-
/**
13-
* Parses follow-up questions from the "### Follow-Up Questions" section at
14-
* the end of an AI response. Returns the text with that section removed and
15-
* the extracted questions.
16-
*/
12+
/** Strips the FOLLOW_UP_QUESTIONS_HEADER section from the text and returns the extracted questions. */
1713
export function parseFollowUpQuestions(text: string): {
1814
strippedText: string;
1915
questions: string[];
@@ -24,17 +20,11 @@ export function parseFollowUpQuestions(text: string): {
2420
return { strippedText: text, questions: [] };
2521
}
2622

27-
const afterHeader = text.slice(headerIndex);
28-
// Match everything after the header line; the header is a single line so we
29-
// skip to the first newline after it.
30-
const contentMatch = afterHeader.match(/\n[^\n]+\n([\s\S]*)/);
31-
if (!contentMatch) {
32-
return { strippedText: text, questions: [] };
33-
}
34-
35-
const questions = contentMatch[1]
23+
const contentStart = text.indexOf('\n', headerIndex + 1) + 1;
24+
const questions = text
25+
.slice(contentStart)
3626
.split('\n')
37-
.map((line) => line.replace(/^\s*\d+\.\s*|^\s*[-*]\s*/, '').trim())
27+
.map((line) => line.trim().replace(/^\d+\.\s*/, ''))
3828
.filter((q) => q.length > 0);
3929

4030
if (questions.length === 0) {

packages/compass-assistant/src/prompts.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@ import { redactConnectionString } from 'mongodb-connection-string-url';
1111
import type { AssistantMessage } from './compass-assistant-provider';
1212
import { AVAILABLE_TOOLS } from '@mongodb-js/compass-generative-ai/provider';
1313

14-
/**
15-
* The markdown header used to delimit the follow-up questions section in AI
16-
* responses. The parser in follow-up-prompts.tsx looks for this exact string,
17-
* so any change here must be reflected there (and vice-versa).
18-
*/
1914
export const FOLLOW_UP_QUESTIONS_HEADER = '### Follow-Up Questions';
2015

2116
export type EntryPointMessage = {
@@ -121,6 +116,9 @@ Tell the user if indexes need to be created or modified to enable any recommenda
121116
122117
${FOLLOW_UP_QUESTIONS_HEADER}
123118
[Provide 3 follow-up questions you think the user might want to ask after reading this response]
119+
1. [First follow-up question]
120+
2. [Second follow-up question]
121+
3. [Third follow-up question]
124122
</output-format>
125123
126124
<guidelines>

0 commit comments

Comments
 (0)