Skip to content

Commit 09649ef

Browse files
authored
Merge pull request FlowiseAI#671 from FlowiseAI/feature/Hyperlink
add hyperlink
2 parents 00f2dd7 + 6bfb2b5 commit 09649ef

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

packages/ui/src/utils/genericHelper.js

+8
Original file line numberDiff line numberDiff line change
@@ -401,3 +401,11 @@ export const getInputVariables = (paramValue) => {
401401
}
402402
return inputVariables
403403
}
404+
405+
export const isValidURL = (url) => {
406+
try {
407+
return new URL(url)
408+
} catch (err) {
409+
return undefined
410+
}
411+
}

packages/ui/src/views/chatmessage/ChatMessage.js

+29-3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { baseURL, maxScroll } from 'store/constant'
3030

3131
import robotPNG from 'assets/images/robot.png'
3232
import userPNG from 'assets/images/account.png'
33+
import { isValidURL } from 'utils/genericHelper'
3334

3435
export const ChatMessage = ({ open, chatflowid, isDialog }) => {
3536
const theme = useTheme()
@@ -59,6 +60,24 @@ export const ChatMessage = ({ open, chatflowid, isDialog }) => {
5960
setSourceDialogOpen(true)
6061
}
6162

63+
const onURLClick = (data) => {
64+
window.open(data, '_blank')
65+
}
66+
67+
const removeDuplicateURL = (message) => {
68+
const visitedURLs = []
69+
const newSourceDocuments = []
70+
message.sourceDocuments.forEach((source) => {
71+
if (isValidURL(source.metadata.source) && !visitedURLs.includes(source.metadata.source)) {
72+
visitedURLs.push(source.metadata.source)
73+
newSourceDocuments.push(source)
74+
} else if (!isValidURL(source.metadata.source)) {
75+
newSourceDocuments.push(source)
76+
}
77+
})
78+
return newSourceDocuments
79+
}
80+
6281
const scrollToBottom = () => {
6382
if (ps.current) {
6483
ps.current.scrollTo({ top: maxScroll })
@@ -319,17 +338,24 @@ export const ChatMessage = ({ open, chatflowid, isDialog }) => {
319338
</div>
320339
{message.sourceDocuments && (
321340
<div style={{ display: 'block', flexDirection: 'row', width: '100%' }}>
322-
{message.sourceDocuments.map((source, index) => {
341+
{removeDuplicateURL(message).map((source, index) => {
342+
const URL = isValidURL(source.metadata.source)
323343
return (
324344
<Chip
325345
size='small'
326346
key={index}
327-
label={`${source.pageContent.substring(0, 15)}...`}
347+
label={
348+
URL
349+
? `${(URL.hostname + URL.pathname).substring(0, 15)}...`
350+
: `${source.pageContent.substring(0, 15)}...`
351+
}
328352
component='a'
329353
sx={{ mr: 1, mb: 1 }}
330354
variant='outlined'
331355
clickable
332-
onClick={() => onSourceDialogClick(source)}
356+
onClick={() =>
357+
URL ? onURLClick(source.metadata.source) : onSourceDialogClick(source)
358+
}
333359
/>
334360
)
335361
})}

0 commit comments

Comments
 (0)