@@ -7,6 +7,7 @@ import { Query } from "../core/query"
77import { MiniLML6 } from "../core/services/embeddings/minilml6"
88import { Llama3_70B } from "../core/services/llm/llama3_70B"
99import { handleCommandResponse } from "../utils/handleCommandResponse"
10+ import { renderDiagramToBase64URI } from "../core/diagram"
1011
1112export class WhyUsedCommand implements ISlashCommand {
1213 public command = "rcc-whyused"
@@ -24,10 +25,13 @@ export class WhyUsedCommand implements ISlashCommand {
2425 private async process (
2526 http : IHttp ,
2627 query : string
27- ) : Promise < {
28- explanation : string
29- diagram : string
30- } | null > {
28+ ) : Promise <
29+ | {
30+ explanation : string
31+ diagram : string
32+ }
33+ | string
34+ > {
3135 const db = new Neo4j ( http )
3236 const llm = new Llama3_70B ( http )
3337 const embeddingModel = new MiniLML6 ( http )
@@ -39,7 +43,7 @@ export class WhyUsedCommand implements ISlashCommand {
3943 * ---------------------------------------------------------------------------------------------
4044 */
4145 const keywords = await Query . getDBKeywordsFromQuery ( llm , query )
42- if ( ! keywords . length ) return null
46+ if ( ! keywords . length ) return "I'm sorry, I couldn't understand your query. Please try again."
4347
4448 /**
4549 * ---------------------------------------------------------------------------------------------
@@ -48,7 +52,7 @@ export class WhyUsedCommand implements ISlashCommand {
4852 * ---------------------------------------------------------------------------------------------
4953 */
5054 const codeNodes = await Query . getCodeNodesFromKeywords ( db , embeddingModel , keywords )
51- if ( ! codeNodes . length ) return null
55+ if ( ! codeNodes . length ) return "I'm sorry, I couldn't find any code related to your query."
5256
5357 /**
5458 * ---------------------------------------------------------------------------------------------
@@ -59,7 +63,7 @@ export class WhyUsedCommand implements ISlashCommand {
5963 const result = await llm . ask (
6064 PromptFactory . makeWhyUsedPrompt ( codeNodes . map ( ( x ) => x . code ) . join ( "\n\n" ) , query )
6165 )
62- if ( ! result ) return null
66+ if ( ! result ) return "I'm sorry, I couldn't find any references for your query."
6367
6468 const explanation = result . split ( "<EXPLANATION>" ) [ 1 ] . split ( "</EXPLANATION>" ) [ 0 ] . trim ( )
6569 const diagram = result . split ( "<DIAGRAM>" ) [ 1 ] . split ( "</DIAGRAM>" ) [ 0 ] . trim ( )
@@ -71,17 +75,12 @@ export class WhyUsedCommand implements ISlashCommand {
7175 * ---------------------------------------------------------------------------------------------
7276 */
7377 const data = { explanation, diagram : "" }
74- // TODO:
75- // if (diagram) {
76- // const parsedDiagram = diagram
77- // .replace("```mermaid", "")
78- // .replace("```", "")
79- // .trim();
80- // writeFileSync("output.txt", parsedDiagram);
81- // try {
82- // // data.diagram = await renderDiagramToBase64URI(parsedDiagram);
83- // } catch {}
84- // }
78+ if ( diagram ) {
79+ const parsedDiagram = diagram . replace ( "```mermaid" , "" ) . replace ( "```" , "" ) . trim ( )
80+ try {
81+ data . diagram = await renderDiagramToBase64URI ( http , parsedDiagram )
82+ } catch { }
83+ }
8584
8685 return data
8786 }
@@ -113,11 +112,10 @@ export class WhyUsedCommand implements ISlashCommand {
113112 )
114113
115114 const res = await this . process ( http , query )
116- if ( ! res ) {
117- await sendEditedMessage ( "❌ No references found!" )
115+ if ( typeof res === "string" ) {
116+ await sendEditedMessage ( res )
118117 return
119118 }
120-
121119 await sendEditedMessage ( res . explanation , [ res . diagram ! ] )
122120 }
123121}
0 commit comments