Skip to content

Commit 55fa36a

Browse files
committed
Use html comment delimiter
To increase the chance that the json will not be rendered on the UI
1 parent 62f372b commit 55fa36a

File tree

4 files changed

+37
-16
lines changed

4 files changed

+37
-16
lines changed

src/Elastic.Documentation.Site/Assets/web-components/SearchOrAskAi/AskAi/ChatMessage.tsx

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,22 @@ const getAccumulatedContent = (messages: LlmGatewayMessage[]) => {
6161
const splitContentAndReferences = (
6262
content: string
6363
): { mainContent: string; referencesJson: string | null } => {
64-
const delimiter = '--- references ---'
65-
const delimiterIndex = content.indexOf(delimiter)
64+
const startDelimiter = '<!--REFERENCES'
65+
const endDelimiter = '-->'
6666

67-
if (delimiterIndex === -1) {
67+
const startIndex = content.indexOf(startDelimiter)
68+
if (startIndex === -1) {
6869
return { mainContent: content, referencesJson: null }
6970
}
7071

71-
const mainContent = content.substring(0, delimiterIndex).trim()
72+
const endIndex = content.indexOf(endDelimiter, startIndex)
73+
if (endIndex === -1) {
74+
return { mainContent: content, referencesJson: null }
75+
}
76+
77+
const mainContent = content.substring(0, startIndex).trim()
7278
const referencesJson = content
73-
.substring(delimiterIndex + delimiter.length)
79+
.substring(startIndex + startDelimiter.length, endIndex)
7480
.trim()
7581

7682
return { mainContent, referencesJson }
@@ -112,7 +118,7 @@ const hasReachedReferences = (messages: LlmGatewayMessage[]): boolean => {
112118
.filter((m) => m.type === 'ai_message_chunk')
113119
.map((m) => m.data.content)
114120
.join('')
115-
return accumulatedContent.includes('--- references ---')
121+
return accumulatedContent.includes('<!--REFERENCES')
116122
}
117123

118124
const computeAiStatus = (
@@ -246,10 +252,13 @@ export const ChatMessage = ({
246252
return splitContentAndReferences(content)
247253
}
248254
// During streaming, strip out unparsed references but don't parse them yet
249-
const delimiter = '--- references ---'
250-
const delimiterIndex = content.indexOf(delimiter)
255+
const startDelimiter = '<!--REFERENCES'
256+
const delimiterIndex = content.indexOf(startDelimiter)
251257
if (delimiterIndex !== -1) {
252-
return { mainContent: content.substring(0, delimiterIndex).trim(), referencesJson: null }
258+
return {
259+
mainContent: content.substring(0, delimiterIndex).trim(),
260+
referencesJson: null,
261+
}
253262
}
254263
return { mainContent: content, referencesJson: null }
255264
}, [content, isComplete])
@@ -351,7 +360,10 @@ export const ChatMessage = ({
351360
{isComplete && content && (
352361
<>
353362
<EuiSpacer size="m" />
354-
<ActionBar content={mainContent} onRetry={onRetry} />
363+
<ActionBar
364+
content={mainContent}
365+
onRetry={onRetry}
366+
/>
355367
</>
356368
)}
357369

src/Elastic.Documentation.Site/Assets/web-components/SearchOrAskAi/AskAi/RelatedResources.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ const parseReferences = (jsonString: string): Reference[] => {
4747
}
4848
return []
4949
} catch (e) {
50+
console.warn('Failed to parse references JSON:', e)
5051
return []
5152
}
5253
}

src/Elastic.Documentation.Site/Assets/web-components/SearchOrAskAi/Search/SearchResults.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,12 @@ function Breadcrumbs({ parents }: { parents: SearchResultItem['parents'] }) {
213213
display: inline-flex;
214214
`}
215215
>
216-
<EuiLink href={parent.url} color="subdued" tabIndex={-1}>
217-
<EuiText
218-
size="xs"
219-
color="subdued"
220-
>
216+
<EuiLink
217+
href={parent.url}
218+
color="subdued"
219+
tabIndex={-1}
220+
>
221+
<EuiText size="xs" color="subdued">
221222
{parent.title}
222223
</EuiText>
223224
</EuiLink>

src/api/Elastic.Documentation.Api.Core/AskAi/AskAiUsecase.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,14 @@ public record AskAiRequest(string Message, string? ThreadId)
8484
}
8585
```
8686
- Ensure that the URLs you provide are directly relevant to the user's question and the content of the documents.
87-
- Add a delimiter "--- references ---" before the sources section
87+
- Add a multi-line delimiter before the sources section using this exact format:
88+
```
89+
<!--REFERENCES
90+
91+
[your JSON array here]
92+
93+
-->
94+
```
8895
8996
""";
9097
}

0 commit comments

Comments
 (0)