mirror of
https://github.com/open-webui/open-webui.git
synced 2025-12-16 14:25:19 +00:00
refac: source parsing
This commit is contained in:
parent
53e98620bf
commit
3c47e49cf0
1 changed files with 20 additions and 6 deletions
|
|
@ -68,12 +68,26 @@ export const replaceTokens = (content, sourceIds, char, user) => {
|
||||||
});
|
});
|
||||||
|
|
||||||
if (Array.isArray(sourceIds)) {
|
if (Array.isArray(sourceIds)) {
|
||||||
sourceIds.forEach((sourceId, idx) => {
|
// Match both [1], [2], and [1,2,3] forms
|
||||||
const regex = new RegExp(`\\[${idx + 1}\\]`, 'g');
|
const multiRefRegex = /\[([\d,\s]+)\]/g;
|
||||||
segment = segment.replace(
|
segment = segment.replace(multiRefRegex, (match, group) => {
|
||||||
regex,
|
// Extract numbers like 1,2,3
|
||||||
`<source_id data="${idx + 1}" title="${encodeURIComponent(sourceId)}" />`
|
const indices = group
|
||||||
);
|
.split(',')
|
||||||
|
.map((n) => parseInt(n.trim(), 10))
|
||||||
|
.filter((n) => !isNaN(n));
|
||||||
|
|
||||||
|
// Replace each index with a <source_id> tag
|
||||||
|
const sources = indices
|
||||||
|
.map((idx) => {
|
||||||
|
const sourceId = sourceIds[idx - 1];
|
||||||
|
return sourceId
|
||||||
|
? `<source_id data="${idx}" title="${encodeURIComponent(sourceId)}" />`
|
||||||
|
: match;
|
||||||
|
})
|
||||||
|
.join('');
|
||||||
|
|
||||||
|
return sources;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue