Skip to content

Commit

Permalink
Extended PlantUML support to mindmaps, improved syntax highlighting a…
Browse files Browse the repository at this point in the history
…nd language detection.
  • Loading branch information
enricoros committed Sep 22, 2023
1 parent b16fc0b commit ce08f6f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
8 changes: 7 additions & 1 deletion src/apps/chat/components/message/RenderCode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ function RenderCodeImpl(props: {
const isSVG = blockCode.startsWith('<svg') && blockCode.endsWith('</svg>');
const renderSVG = isSVG && showSVG;

const isPlantUML = blockCode.startsWith('@startuml') && blockCode.endsWith('@enduml');
const isPlantUML =
(blockCode.startsWith('@startuml') && blockCode.endsWith('@enduml'))
|| (blockCode.startsWith('@startmindmap') && blockCode.endsWith('@endmindmap'))
|| (blockCode.startsWith('@startsalt') && blockCode.endsWith('@endsalt'))
|| (blockCode.startsWith('@startwbs') && blockCode.endsWith('@endwbs'))
|| (blockCode.startsWith('@startgantt') && blockCode.endsWith('@endgantt'));

let renderPlantUML = isPlantUML && showPlantUML;
const { data: plantUmlHtmlData } = useQuery({
enabled: renderPlantUML,
Expand Down
9 changes: 6 additions & 3 deletions src/apps/chat/components/message/codePrism.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ import 'prismjs/components/prism-java';
import 'prismjs/components/prism-javascript';
import 'prismjs/components/prism-json';
import 'prismjs/components/prism-markdown';
import 'prismjs/components/prism-plant-uml';
import 'prismjs/components/prism-python';
import 'prismjs/components/prism-typescript';

// NOTE: must match Prism components imports
const hPrismLanguages = ['bash', 'css', 'java', 'javascript', 'json', 'markdown', 'plant-uml', 'python', 'typescript'];

const hFileExtensionsMap: { [key: string]: string } = {
cs: 'csharp', html: 'html', java: 'java', js: 'javascript', json: 'json', jsx: 'javascript',
Expand All @@ -24,11 +27,9 @@ const hCodeIncipitMap: { starts: string[], language: string }[] = [
{ starts: ['interface ', 'function '], language: 'typescript' }, // ambiguous
{ starts: ['package '], language: 'java' },
{ starts: ['using '], language: 'csharp' },
{ starts: ['@startuml', '@startmindmap', '@startsalt', '@startwbs', '@startgantt'], language: 'plant-uml' },
];

// NOTE: must match Prism components imports
const hPrismLanguages = ['bash', 'css', 'java', 'javascript', 'json', 'markdown', 'python', 'typescript'];


export function inferCodeLanguage(blockTitle: string, code: string): string | null {

Expand All @@ -50,6 +51,8 @@ export function inferCodeLanguage(blockTitle: string, code: string): string | nu
return codeIncipit.language;

// or, use Prism with language tokenization to and-detect the language
// FIXME: this is a very poor way to detect the language, as it's tokenizing it in any language
// and getting the one with the most tokens - which may as well be the wrong one
let detectedLanguage: string | null = null;
let maxTokens = 0;
hPrismLanguages.forEach((language) => {
Expand Down
6 changes: 3 additions & 3 deletions src/modules/aifn/autosuggestions/autoSuggestions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const suggestUserFollowUpFn: VChatFunctionIn = {

const suggestPlantUMLFn: VChatFunctionIn = {
name: 'draw_plantuml_diagram',
description: 'Generates a PlantUML diagram from the last message, if applicable, relevant, and no other diagrams are present.',
description: 'Generates a PlantUML diagram or mindmap from the last message, if applicable, relevant, and no other diagrams are present.',
parameters: {
type: 'object',
properties: {
Expand All @@ -35,7 +35,7 @@ const suggestPlantUMLFn: VChatFunctionIn = {
},
code: {
type: 'string',
description: 'A valid PlantUML string (@startuml...@enduml) to be rendered as a diagram, or an empty string. Quotations should be used, external references and spaces in participants/actors should be avoided.',
description: 'A valid PlantUML string (@startuml...@enduml) to be rendered as a diagram or mindmap, or an empty string. Quotations should be used, external references and spaces in participants/actors should be avoided.',
},
},
required: ['type', 'code'],
Expand Down Expand Up @@ -97,7 +97,7 @@ export async function autoSuggestions(conversationId: string, assistantMessageId

// validate the code
const plantUML = code.trim();
if (!plantUML.startsWith('@startuml') || !plantUML.endsWith('@enduml')) return;
if (!plantUML.startsWith('@start') || !(plantUML.endsWith('@enduml') || plantUML.endsWith('@endmindmap'))) return;

// append the PlantUML diagram to the assistant response
assistantMessageText += `\n\n\`\`\`${type}.diagram\n${plantUML}\n\`\`\`\n`;
Expand Down

1 comment on commit ce08f6f

@vercel
Copy link

@vercel vercel bot commented on ce08f6f Sep 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

big-agi – ./

big-agi-enricoros.vercel.app
get.big-agi.com
big-agi-git-main-enricoros.vercel.app

Please sign in to comment.