|
| 1 | +import { |
| 2 | + IHttp, |
| 3 | + IModify, |
| 4 | + IRead, |
| 5 | + IUIKitSurfaceViewParam, |
| 6 | +} from "@rocket.chat/apps-engine/definition/accessors"; |
| 7 | +import { IRoom } from "@rocket.chat/apps-engine/definition/rooms"; |
| 8 | +import { |
| 9 | + IUIKitSurface, |
| 10 | + UIKitSurfaceType, |
| 11 | +} from "@rocket.chat/apps-engine/definition/uikit"; |
| 12 | +import { IUser } from "@rocket.chat/apps-engine/definition/users"; |
| 13 | +import { PromptFactory } from "../core/prompt.factory"; |
| 14 | +import { Query } from "../core/query"; |
| 15 | +import { Neo4j } from "../core/services/db/neo4j"; |
| 16 | +import { MiniLML6 } from "../core/services/embeddings/minilml6"; |
| 17 | +import { Llama3_70B } from "../core/services/llm/llama3_70B"; |
| 18 | +import { getButton, getInputBox } from "../utils/blockBuilders"; |
| 19 | +import { handleCommandResponse } from "../utils/handleCommandResponse"; |
| 20 | + |
| 21 | +export const COMMAND = "rcc-testcases"; |
| 22 | +export const TESTCASES_COMMAND_MODAL = "testcases-command"; |
| 23 | + |
| 24 | +export async function testcasesModal(): Promise<IUIKitSurfaceViewParam> { |
| 25 | + return { |
| 26 | + id: TESTCASES_COMMAND_MODAL, |
| 27 | + type: UIKitSurfaceType.MODAL, |
| 28 | + title: { |
| 29 | + type: "plain_text", |
| 30 | + text: "Generate Testcases", |
| 31 | + }, |
| 32 | + close: await getButton("Close", "", ""), |
| 33 | + clearOnClose: true, |
| 34 | + submit: await getButton("Submit", "submit", "submit", "Submit"), |
| 35 | + blocks: [ |
| 36 | + await getInputBox( |
| 37 | + "", |
| 38 | + "What code you want to generate test cases for?", |
| 39 | + "testcases", |
| 40 | + "testcases", |
| 41 | + "", |
| 42 | + true |
| 43 | + ), |
| 44 | + ], |
| 45 | + }; |
| 46 | +} |
| 47 | + |
| 48 | +async function process(http: IHttp, query: string): Promise<string | null> { |
| 49 | + const db = new Neo4j(http); |
| 50 | + const llm = new Llama3_70B(http); |
| 51 | + const embeddingModel = new MiniLML6(http); |
| 52 | + |
| 53 | + /** |
| 54 | + * --------------------------------------------------------------------------------------------- |
| 55 | + * STEP 1: |
| 56 | + * Extract the possible keywords from the user's query |
| 57 | + * --------------------------------------------------------------------------------------------- |
| 58 | + */ |
| 59 | + const keywords = await Query.getDBKeywordsFromQuery(llm, query); |
| 60 | + if (!keywords.length) return null; |
| 61 | + |
| 62 | + /** |
| 63 | + * --------------------------------------------------------------------------------------------- |
| 64 | + * STEP 2: |
| 65 | + * Query the database to find the nodes names of which are similar to what user has requested |
| 66 | + * --------------------------------------------------------------------------------------------- |
| 67 | + */ |
| 68 | + const codeNodes = await Query.getCodeNodesFromKeywords( |
| 69 | + db, |
| 70 | + embeddingModel, |
| 71 | + keywords |
| 72 | + ); |
| 73 | + if (!codeNodes.length) return null; |
| 74 | + |
| 75 | + /** |
| 76 | + * --------------------------------------------------------------------------------------------- |
| 77 | + * STEP 3: |
| 78 | + * Generate the test cases for the code nodes |
| 79 | + * --------------------------------------------------------------------------------------------- |
| 80 | + */ |
| 81 | + const answer = await llm.ask( |
| 82 | + PromptFactory.makeTestcasesPrompt( |
| 83 | + codeNodes.map((x) => x.code).join("\n\n"), |
| 84 | + query, |
| 85 | + "playwright" |
| 86 | + ) |
| 87 | + ); |
| 88 | + if (!answer) return null; |
| 89 | + |
| 90 | + return answer; |
| 91 | +} |
| 92 | + |
| 93 | +export async function testcasesModalSubmitHandler( |
| 94 | + view: IUIKitSurface, |
| 95 | + sender: IUser, |
| 96 | + room: IRoom, |
| 97 | + read: IRead, |
| 98 | + modify: IModify, |
| 99 | + http: IHttp |
| 100 | +) { |
| 101 | + const state = view.state as Record<string, any> | undefined; |
| 102 | + if (!state) return; |
| 103 | + |
| 104 | + const query = state.testcases.testcases; |
| 105 | + const sendMessage = await handleCommandResponse( |
| 106 | + "\n```typescript\n" + query + "\n```", |
| 107 | + sender, |
| 108 | + room, |
| 109 | + modify, |
| 110 | + COMMAND |
| 111 | + ); |
| 112 | + |
| 113 | + const res = await process(http, query); |
| 114 | + if (res) { |
| 115 | + await sendMessage(res as string); |
| 116 | + } else { |
| 117 | + await sendMessage("❌ Failed to get test cases"); |
| 118 | + } |
| 119 | +} |
0 commit comments