mirror of
https://github.com/open-webui/open-webui.git
synced 2026-07-16 14:39:31 -05:00
fix(chat): use code block language tag to detect Vega-Lite specs for rendering (#25843)
This commit is contained in:
@@ -379,7 +379,7 @@
|
||||
(token?.raw ?? '').slice(-4).includes('```')
|
||||
) {
|
||||
try {
|
||||
renderHTML = await renderVegaVisualization(code);
|
||||
renderHTML = await renderVegaVisualization(code, lang);
|
||||
} catch (error) {
|
||||
console.error('Failed to render Vega visualization:', error);
|
||||
const errorMsg = error instanceof Error ? error.message : String(error);
|
||||
|
||||
@@ -2000,11 +2000,23 @@ export const renderMermaidDiagram = async (
|
||||
}
|
||||
};
|
||||
|
||||
export const renderVegaVisualization = async (spec: string, i18n?: any) => {
|
||||
export const renderVegaVisualization = async (spec: string, lang: string = '', i18n?: any) => {
|
||||
const vega = await import('vega');
|
||||
const parsedSpec = JSON.parse(spec);
|
||||
const hasVegaLiteKeys =
|
||||
'mark' in parsedSpec ||
|
||||
'encoding' in parsedSpec ||
|
||||
'layer' in parsedSpec ||
|
||||
'hconcat' in parsedSpec ||
|
||||
'vconcat' in parsedSpec ||
|
||||
'repeat' in parsedSpec ||
|
||||
'facet' in parsedSpec;
|
||||
const isVegaLite =
|
||||
lang === 'vega-lite' ||
|
||||
(parsedSpec.$schema && parsedSpec.$schema.includes('vega-lite')) ||
|
||||
hasVegaLiteKeys;
|
||||
let vegaSpec = parsedSpec;
|
||||
if (parsedSpec.$schema && parsedSpec.$schema.includes('vega-lite')) {
|
||||
if (isVegaLite) {
|
||||
const vegaLite = await import('vega-lite');
|
||||
vegaSpec = vegaLite.compile(parsedSpec).spec;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user