Skip to content

Commit 5a9e6bd

Browse files
committed
Made diagramming working using kroki.io
1 parent c02ab40 commit 5a9e6bd

File tree

3 files changed

+41
-54
lines changed

3 files changed

+41
-54
lines changed

ai-assistant/package.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66
"@rocket.chat/apps-cli": "^1.11.1",
77
"@rocket.chat/apps-engine": "^1.19.0",
88
"@types/node": "14.14.6",
9+
"esbuild": "^0.19.4",
910
"tslint": "^5.10.0",
1011
"tsx": "^4.17.0",
11-
"typescript": "^4.0.5",
12-
"esbuild": "^0.19.4"
13-
},
14-
"dependencies": {}
15-
}
12+
"typescript": "^4.0.5"
13+
}
14+
}

ai-assistant/src/commands/DiagramCommand.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import {
88
SlashCommandContext,
99
} from "@rocket.chat/apps-engine/definition/slashcommands";
1010

11-
import { Neo4j } from "../core/services/db/neo4j";
12-
// import { renderDiagramToBase64URI } from "../core/diagram";
11+
import { renderDiagramToBase64URI } from "../core/diagram";
1312
import { PromptFactory } from "../core/prompt.factory";
1413
import { Query } from "../core/query";
14+
import { Neo4j } from "../core/services/db/neo4j";
1515
import { MiniLML6 } from "../core/services/embeddings/minilml6";
1616
import { Llama3_70B } from "../core/services/llm/llama3_70B";
1717
import { handleCommandResponse } from "../utils/handleCommandResponse";
@@ -61,19 +61,21 @@ export class DiagramCommand implements ISlashCommand {
6161
query
6262
)
6363
);
64-
console.log(diagram);
6564
if (!diagram) return null;
6665

66+
// @ts-ignore
6767
const diagramContent = diagram
6868
.replace("```mermaid", "")
6969
.replace("```", "")
7070
.split("<DIAGRAM_START>")[1]
7171
.split("<DIAGRAM_END>")[0]
7272
.trim();
73-
console.log("DIAGRAM:\n", diagramContent);
7473

75-
// const base64Diagram = await renderDiagramToBase64URI(diagramContent);
76-
return "";
74+
const base64Diagram = await renderDiagramToBase64URI(
75+
http,
76+
diagramContent
77+
);
78+
return base64Diagram;
7779
}
7880

7981
public async executor(

ai-assistant/src/core/diagram.ts

Lines changed: 29 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,33 @@
1-
export default {}
2-
// import { exec } from "child_process";
3-
// // import { randomUUID } from "crypto"
4-
// import { unlinkSync, writeFileSync } from "fs";
1+
import { IHttp } from "@rocket.chat/apps-engine/definition/accessors";
52

6-
// export async function renderDiagramToBase64URI(
7-
// diagram: string
8-
// ): Promise<string> {
9-
// const diagramID = "diagram";
10-
// // ?? randomUUID()
11-
// const diagramSourceFileName = `${diagramID}.txt`;
12-
// const diagramSVGFileName = `${diagramID}.svg`;
3+
export async function renderDiagramToBase64URI(
4+
http: IHttp,
5+
source: string
6+
): Promise<string> {
7+
source = source.trim();
138

14-
// writeFileSync(diagramSourceFileName, diagram);
9+
let svgContent = "";
10+
try {
11+
const response = await http.post("https://kroki.io", {
12+
headers: {
13+
Accept: "image/svg+xml",
14+
"Content-Type": "text/json",
15+
},
16+
content: JSON.stringify({
17+
diagram_source: source,
18+
diagram_type: "mermaid",
19+
output_format: "svg",
20+
}),
21+
});
22+
svgContent = response.content as string;
23+
} catch (error) {
24+
console.error("Error while rendering diagram", error);
25+
return "";
26+
}
1527

16-
// await new Promise<void>((resolve, reject) => {
17-
// const diagram = exec(
18-
// `npx mmdc -i ${diagramSourceFileName} -o ${diagramSVGFileName}`,
19-
// {
20-
// cwd: process.cwd(),
21-
// timeout: 10000,
22-
// }
23-
// );
28+
console.log(svgContent);
2429

25-
// diagram.on("exit", async (code) => {
26-
// // unlinkSync(diagramSourceFileName)
27-
// if (code === 0) {
28-
// resolve();
29-
// } else {
30-
// console.log(`Error: ${code}`);
31-
// reject();
32-
// }
33-
// });
34-
// });
35-
36-
// const base64 = await new Promise<string>((resolve, reject) => {
37-
// exec(`base64 ${diagramSVGFileName}`, async (err, stdout) => {
38-
// unlinkSync(diagramSVGFileName);
39-
// if (err) reject(err);
40-
// resolve(stdout);
41-
// });
42-
// });
43-
44-
// const uri = `data:image/svg+xml;base64,${base64.trim()}`;
45-
46-
// return uri;
47-
// }
30+
const svgContentBase64 = Buffer.from(svgContent).toString("base64");
31+
const uri = `data:image/svg+xml;base64,${svgContentBase64}`;
32+
return uri;
33+
}

0 commit comments

Comments
 (0)