Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(editor): Logs markdown block improvements #10681

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/@n8n/chat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
},
"dependencies": {
"@vueuse/core": "^10.11.0",
"highlight.js": "^11.8.0",
"highlight.js": "catalog:frontend",
"markdown-it-link-attributes": "^4.0.1",
"uuid": "catalog:",
"vue": "catalog:frontend",
Expand Down
1 change: 1 addition & 0 deletions packages/editor-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"fast-json-stable-stringify": "^2.1.0",
"file-saver": "^2.0.2",
"flatted": "^3.2.4",
"highlight.js": "catalog:frontend",
"humanize-duration": "^3.27.2",
"jsonpath": "^1.1.1",
"lodash-es": "^4.17.21",
Expand Down
39 changes: 36 additions & 3 deletions packages/editor-ui/src/components/RunDataAi/AiRunContentBlock.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ref, onMounted } from 'vue';
import type { ParsedAiContent } from './useAiContentParsers';
import { useAiContentParsers } from './useAiContentParsers';
import VueMarkdown from 'vue-markdown-render';
import hljs from 'highlight.js/lib/core';
import { useClipboard } from '@/composables/useClipboard';
import { useI18n } from '@/composables/useI18n';
import { useToast } from '@/composables/useToast';
Expand Down Expand Up @@ -38,6 +39,27 @@ function getInitialExpandedState() {
return !collapsedTypes[props.runData.inOut].includes(props.runData.type);
}

function isJsonString(text: string) {
try {
JSON.parse(text);
return true;
} catch (e) {
return false;
}
}

const markdownOptions = {
highlight(str: string, lang: string) {
if (lang && hljs.getLanguage(lang)) {
try {
return hljs.highlight(str, { language: lang }).value;
} catch {}
}

return ''; // use external default escaping
},
};

function parseAiRunData(run: IAiDataContent) {
if (!run.data) {
return;
Expand Down Expand Up @@ -75,7 +97,13 @@ function jsonToMarkdown(data: JsonMarkdown): string {
}

if (typeof data === 'string') {
return formatToJsonMarkdown(data);
// If data is a valid JSON string – format it as JSON markdown
if (isJsonString(data)) {
return formatToJsonMarkdown(data);
}

// Return original string otherwise
return data;
}

return formatToJsonMarkdown(JSON.stringify(data, null, 2));
Expand Down Expand Up @@ -145,10 +173,15 @@ onMounted(() => {
<VueMarkdown
:source="jsonToMarkdown(parsedContent.data as JsonMarkdown)"
:class="$style.markdown"
:options="markdownOptions"
/>
</template>
<template v-if="parsedContent.type === 'markdown'">
<VueMarkdown :source="parsedContent.data" :class="$style.markdown" />
<VueMarkdown
:source="parsedContent.data"
:class="$style.markdown"
:options="markdownOptions"
/>
</template>
<p
v-if="parsedContent.type === 'text'"
Expand Down Expand Up @@ -204,7 +237,7 @@ onMounted(() => {
}

pre {
background-color: var(--color-foreground-light);
background: var(--chat--message--pre--background);
border-radius: var(--border-radius-base);
line-height: var(--font-line-height-xloose);
padding: var(--spacing-s);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@ const outputTypeParsers: {
parsed: true,
};
}

// Use the memory parser if the response is a memory-like(chat) object
if (response.messages && Array.isArray(response.messages)) {
return outputTypeParsers[NodeConnectionType.AiMemory](execData);
}

if (response.generations) {
const generations = response.generations as LmGeneration[];

Expand Down Expand Up @@ -220,8 +222,8 @@ export const useAiContentParsers = () => {
}

const contentJson = executionData.map((node) => {
const hasBinarData = !isObjectEmpty(node.binary);
return hasBinarData ? node.binary : node.json;
const hasBinaryData = !isObjectEmpty(node.binary);
return hasBinaryData ? node.binary : node.json;
});

const parser = outputTypeParsers[endpointType as AllowedEndpointType];
Expand Down
16 changes: 11 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ catalogs:
vue: ^3.4.21
vue-tsc: ^2.0.19
vue-markdown-render: ^2.2.1
highlight.js: ^11.8.0
Loading