|
| 1 | +import { |
| 2 | + IHttp, |
| 3 | + IModify, |
| 4 | + IPersistence, |
| 5 | + IRead, |
| 6 | +} from "@rocket.chat/apps-engine/definition/accessors"; |
| 7 | +import { |
| 8 | + ApiEndpoint, |
| 9 | + IApiEndpointInfo, |
| 10 | + IApiRequest, |
| 11 | + IApiResponse, |
| 12 | +} from "@rocket.chat/apps-engine/definition/api"; |
| 13 | +import { IDB } from "../core/db/db.types"; |
| 14 | +import { Neo4j } from "../core/db/neo4j"; |
| 15 | + |
| 16 | +export class PurgeDBEndpoint extends ApiEndpoint { |
| 17 | + public path = "purgeDB"; |
| 18 | + |
| 19 | + async emptyDB(db: IDB) { |
| 20 | + const query = `MATCH (n) DETACH DELETE n`; |
| 21 | + await db.run(query); |
| 22 | + console.error("Failed to empty DB"); |
| 23 | + } |
| 24 | + |
| 25 | + async setupIndices(db: IDB) { |
| 26 | + const query = [ |
| 27 | + // Drop existing indices |
| 28 | + "DROP INDEX `nameEmbeddings`;", |
| 29 | + "DROP INDEX `codeEmbeddings`;", |
| 30 | + |
| 31 | + // Create indices for name embeddings |
| 32 | + "CREATE VECTOR INDEX `nameEmbeddings`", |
| 33 | + "FOR (n: Node) ON (n.nameEmbeddings)", |
| 34 | + "OPTIONS {indexConfig: {", |
| 35 | + " `vector.dimensions`: 768,", |
| 36 | + " `vector.similarity_function`: 'COSINE'", |
| 37 | + "}};", |
| 38 | + |
| 39 | + // Create indices for code embeddings |
| 40 | + "CREATE VECTOR INDEX `codeEmbeddings`", |
| 41 | + "FOR (n: Node) ON (n.codeEmbeddings)", |
| 42 | + "OPTIONS {indexConfig: {", |
| 43 | + " `vector.dimensions`: 768,", |
| 44 | + " `vector.similarity_function`: 'COSINE'", |
| 45 | + "}};", |
| 46 | + |
| 47 | + ].join("\n"); |
| 48 | + await db.run(query); |
| 49 | + } |
| 50 | + |
| 51 | + public async post( |
| 52 | + request: IApiRequest, |
| 53 | + endpoint: IApiEndpointInfo, |
| 54 | + read: IRead, |
| 55 | + modify: IModify, |
| 56 | + http: IHttp, |
| 57 | + persis: IPersistence |
| 58 | + ): Promise<IApiResponse> { |
| 59 | + const db = new Neo4j(http); |
| 60 | + |
| 61 | + await this.emptyDB(db) |
| 62 | + await this.setupIndices(db) |
| 63 | + |
| 64 | + return this.success(); |
| 65 | + } |
| 66 | +} |
0 commit comments