Skip to content

Commit fd3461c

Browse files
Merge pull request #578 from microsoft/PSL-BUG-24399
fix: Fixed reference num and section generation issue
2 parents b9f6538 + a044e9e commit fd3461c

File tree

3 files changed

+40
-20
lines changed

3 files changed

+40
-20
lines changed

src/frontend/src/components/Answer/AnswerParser.tsx

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,41 @@ export const enumerateCitations = (citations: Citation[]) => {
2323

2424
export function parseAnswer(answer: AskResponse): ParsedAnswer {
2525
let answerText = answer.answer
26-
// const citationLinks = answerText.match(/\[(doc\d\d?\d?)]/g)
27-
// const lengthDocN = '[doc'.length
28-
// let filteredCitations = [] as Citation[]
29-
// let citationReindex = 0
30-
// citationLinks?.forEach(link => {
31-
// // Replacing the links/citations with number
32-
// const citationIndex = link.slice(lengthDocN, link.length - 1)
33-
// const citation = cloneDeep(answer.citations[Number(citationIndex) - 1]) as Citation
34-
// if (!filteredCitations.find(c => c.id === citationIndex) && citation) {
35-
// answerText = answerText.replaceAll(link, ` ^${++citationReindex}^ `)
36-
// citation.id = citationIndex // original doc index to de-dupe
37-
// citation.reindex_id = citationReindex.toString() // reindex from 1 for display
38-
// filteredCitations.push(citation)
39-
// }
40-
// })
26+
27+
const citationMarkerRegex = /\[\d+\]/g;
28+
// Early return if no citations available
29+
if (!answer.citations?.length) {
30+
return {
31+
citations: [],
32+
markdownFormatText: answerText.replace(citationMarkerRegex, '')
33+
}
34+
}
4135

42-
// filteredCitations = enumerateCitations(filteredCitations)
43-
// console.log('filteredCitations', filteredCitations)
36+
// Extract unique citation markers and process them
37+
const citationMarkers = [...new Set(answerText.match(citationMarkerRegex) || [])]
38+
const processedCitations: Citation[] = []
39+
40+
citationMarkers.forEach((marker, index) => {
41+
const citationIndex = parseInt(marker.slice(1, -1)) - 1 // Convert to 0-based index
42+
const citation = answer.citations[citationIndex]
43+
44+
if (citation) {
45+
// Replace all instances of this marker with the new citation number
46+
const newCitationNumber = index + 1
47+
answerText = answerText.replaceAll(marker, ` ^${newCitationNumber}^ `)
48+
49+
processedCitations.push({
50+
...cloneDeep(citation),
51+
reindex_id: newCitationNumber.toString()
52+
})
53+
} else {
54+
// Remove invalid citation markers
55+
answerText = answerText.replaceAll(marker, '')
56+
}
57+
})
58+
4459
return {
45-
citations: answer.citations,
60+
citations: enumerateCitations(processedCitations),
4661
markdownFormatText: answerText
4762
}
4863
}

src/frontend/src/helpers/helpers.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ const enum contentTemplateSections {
88
Intro = 'The proposal will include the following sections:',
99
Closing = 'Does this look good? If so, you can **generate the document** now. You can also ask me to **add an item** or **change the order of the sections**.',
1010
JSONParseError = 'I was unable to find content related to your query and could not generate a template. Please try again.',
11-
JSONStructureError = 'Unable to render the sections within the template. Please try again.'
11+
JSONStructureError = 'Unable to render the sections within the template. Please try again.',
12+
EmptySectionsError = 'No sections have been selected to include in the proposal.'
1213
}
1314

1415

@@ -54,6 +55,10 @@ export const generateTemplateSections = (jsonString: string) => {
5455
return contentTemplateSections.JSONStructureError
5556
}
5657

58+
if (jsonResponse.template.length === 0) {
59+
return contentTemplateSections.EmptySectionsError
60+
}
61+
5762
let sections = `${contentTemplateSections.Intro}${contentTemplateSections.NewLine}`
5863
jsonResponse.template.forEach((row: { section_title: string }) => {
5964
sections += `${row.section_title}${contentTemplateSections.NewLine}`

src/frontend/src/pages/chat/Chat.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -973,7 +973,7 @@ const Chat = ({ type = ChatType.Browse }: Props) => {
973973
className={styles.generateDocumentIcon}
974974
iconProps={{ iconName: 'Generate' }}
975975
onClick={generateDocument} //Update for Document Generation
976-
disabled={draftDocument === undefined || disabledButton()}
976+
disabled={draftDocument === undefined || !draftDocument?.sections?.length || disabledButton()}
977977
aria-label="generate draft"
978978
title="Generate Draft"
979979
/>

0 commit comments

Comments
 (0)