Skip to content

Commit bbca222

Browse files
committed
Added typings in Neo4j & fixed it's implementation details
1 parent e7625a9 commit bbca222

File tree

6 files changed

+82
-49
lines changed

6 files changed

+82
-49
lines changed

ai-assistant/src/commands/AskCommand.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
22
IHttp,
33
IModify,
4-
IRead,
4+
IRead
55
} from "@rocket.chat/apps-engine/definition/accessors";
66
import {
77
ISlashCommand,

ai-assistant/src/commands/ImportanceCommand.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@ export class ImportanceCommand implements ISlashCommand {
3131
LIMIT 1
3232
`
3333
);
34-
const maxOutDegree = maxOutDegreeQuery.records[0]
35-
.get("outDegree")
36-
.toNumber();
34+
if (!maxOutDegreeQuery) return 0;
35+
const maxOutDegree = maxOutDegreeQuery[0].get("outDegree").toNumber();
3736

3837
const outDegree = await db.run(
3938
`
@@ -42,8 +41,8 @@ export class ImportanceCommand implements ISlashCommand {
4241
`,
4342
{ id: node.id }
4443
);
45-
46-
const centrality = outDegree.records[0].get("outDegree").toNumber();
44+
if (!outDegree) return 0;
45+
const centrality = outDegree[0].get("outDegree").toNumber();
4746

4847
const relativeCentrality = centrality / maxOutDegree;
4948
return relativeCentrality;
@@ -59,9 +58,8 @@ export class ImportanceCommand implements ISlashCommand {
5958
LIMIT 1
6059
`
6160
);
62-
const maxInDegree = maxInDegreeQuery.records[0]
63-
.get("inDegree")
64-
.toNumber();
61+
if (!maxInDegreeQuery) return 0;
62+
const maxInDegree = maxInDegreeQuery[0].get("inDegree").toNumber();
6563

6664
const inDegree = await db.run(
6765
`
@@ -70,7 +68,8 @@ export class ImportanceCommand implements ISlashCommand {
7068
`,
7169
{ id: node.id }
7270
);
73-
const criticality = inDegree.records[0].get("inDegree").toNumber();
71+
if (!inDegree) return 0;
72+
const criticality = inDegree[0].get("inDegree").toNumber();
7473

7574
const relativeCriticality = criticality / maxInDegree;
7675
return relativeCriticality;

ai-assistant/src/core/db/db.types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ export interface IDB {
99

1010
rollbackTransaction(): Promise<void>;
1111

12-
run(query: string, params?: any): Promise<any>;
12+
run(query: string, params?: any): Promise<Record<string, any>[] | null>;
1313
}

ai-assistant/src/core/db/neo4j.ts

Lines changed: 64 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
1-
import {
2-
IHttp,
3-
IHttpResponse,
4-
} from "@rocket.chat/apps-engine/definition/accessors";
1+
import { IHttp } from "@rocket.chat/apps-engine/definition/accessors";
52
import { IDB } from "./db.types";
63

4+
export type Neo4jResponse = {
5+
transactionUrl?: string;
6+
results: {
7+
columns: string[];
8+
data: {
9+
row: Record<string, any>[];
10+
meta: {
11+
id: number;
12+
elementId: string;
13+
type: string;
14+
deleted: boolean;
15+
}[];
16+
}[];
17+
}[];
18+
errors: any[];
19+
lastBookmarks: string[];
20+
};
21+
722
export class Neo4j implements IDB {
823
private http: IHttp;
924
private readonly baseUrl: string;
@@ -18,34 +33,44 @@ export class Neo4j implements IDB {
1833
// password: string
1934
) {
2035
this.http = http;
21-
// this.baseUrl = baseUrl;
22-
// this.username = username;
23-
// this.password = password;
36+
// this.baseUrl = "http://neo4j:7474";
37+
// this.username = "neo4j";
38+
// this.password = "strongpasswordsafe123";
39+
this.baseUrl = "http://3.89.86.217:7474";
40+
this.username = "neo4j";
41+
this.password = "errors-fourths-seeds";
2442
}
2543

2644
private async sendRequest(
2745
endpoint: string,
2846
method: string,
2947
data?: any
30-
): Promise<IHttpResponse> {
48+
): Promise<Neo4jResponse | null> {
3149
const url = `${this.baseUrl}${endpoint}`;
3250
const headers = {
3351
"Content-Type": "application/json",
3452
Authorization: `Basic ${Buffer.from(
3553
`${this.username}:${this.password}`
3654
).toString("base64")}`,
3755
};
38-
return this.http.post(url, {
56+
57+
const res = await this.http.post(url, {
3958
headers,
40-
data: data ? JSON.stringify(data) : undefined,
59+
data: data,
4160
});
61+
if (!res || ![200, 201].includes(res.statusCode) || !res.content) {
62+
console.log(res);
63+
return null;
64+
}
65+
66+
return JSON.parse(res.content);
4267
}
4368

4469
async verifyConnectivity(): Promise<void> {
4570
const response = await this.sendRequest("/db/neo4j/tx/commit", "POST", {
4671
statements: [],
4772
});
48-
if (response.statusCode !== 200) {
73+
if (!response) {
4974
throw new Error("Failed to connect to Neo4j");
5075
}
5176
}
@@ -63,10 +88,10 @@ export class Neo4j implements IDB {
6388
const response = await this.sendRequest("/db/neo4j/tx", "POST", {
6489
statements: [],
6590
});
66-
if (response.statusCode !== 201) {
67-
throw new Error(`Failed to begin transaction: ${response.content}`);
91+
if (!response) {
92+
throw new Error(`Failed to begin transaction`);
6893
}
69-
this.transactionUrl = response.headers?.["location"];
94+
this.transactionUrl = response.transactionUrl;
7095
}
7196

7297
async commitTransaction(): Promise<void> {
@@ -77,10 +102,8 @@ export class Neo4j implements IDB {
77102
`${this.transactionUrl}/commit`,
78103
"POST"
79104
);
80-
if (response.statusCode !== 200) {
81-
throw new Error(
82-
`Failed to commit transaction: ${response.content}`
83-
);
105+
if (!response) {
106+
throw new Error("Failed to commit transaction");
84107
}
85108
this.transactionUrl = undefined;
86109
}
@@ -90,15 +113,16 @@ export class Neo4j implements IDB {
90113
throw new Error("No transaction to rollback");
91114
}
92115
const response = await this.sendRequest(this.transactionUrl, "DELETE");
93-
if (response.statusCode !== 200) {
94-
throw new Error(
95-
`Failed to rollback transaction: ${response.content}`
96-
);
116+
if (!response) {
117+
throw new Error("Failed to rollback transaction");
97118
}
98119
this.transactionUrl = undefined;
99120
}
100121

101-
async run(query: string, params?: any): Promise<any> {
122+
async run(
123+
query: string,
124+
params?: any
125+
): Promise<Record<string, any>[] | null> {
102126
const data = {
103127
statements: [
104128
{
@@ -108,7 +132,7 @@ export class Neo4j implements IDB {
108132
],
109133
};
110134

111-
let response;
135+
let response: Neo4jResponse | null = null;
112136
if (this.transactionUrl) {
113137
response = await this.sendRequest(
114138
this.transactionUrl,
@@ -123,10 +147,23 @@ export class Neo4j implements IDB {
123147
);
124148
}
125149

126-
if (response.statusCode !== 200) {
127-
throw new Error(`Failed to run query: ${response.content}`);
150+
if (!response) {
151+
throw new Error("Failed to run query");
152+
}
153+
154+
if (response.errors.length) {
155+
return null;
156+
}
157+
158+
const nodes: Record<string, any>[] = [];
159+
for (const result of response.results) {
160+
for (const data of result.data) {
161+
for (const row of data.row) {
162+
nodes.push(row);
163+
}
164+
}
128165
}
129166

130-
return response.data;
167+
return nodes;
131168
}
132169
}

ai-assistant/src/core/query.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,14 @@ export namespace Query {
2323
`,
2424
{ vector }
2525
);
26+
if (!result) return [];
2627

27-
const data = result.records.map((record) => record.toObject());
28-
const results: any[] = [];
29-
for (const record of data) {
30-
const n = record.node.properties;
31-
delete n["nameEmbeddings"];
32-
delete n["codeEmbeddings"];
33-
results.push(n);
28+
const nodes: DBNode[] = [];
29+
for (const record of result) {
30+
nodes.push(record as DBNode);
3431
}
3532

36-
return results;
33+
return nodes;
3734
}
3835

3936
export async function getCodeNodesFromKeywords(

ai-assistant/src/modals/styleguideModal.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ async function process(http: IHttp, query: string): Promise<string | null> {
5454
* ---------------------------------------------------------------------------------------------
5555
*/
5656
const dbResults = await db.run(`MATCH (n:Styleguide) RETURN n`);
57-
const styleGuides = dbResults.records.map(
58-
(record) => record.get("n").properties
59-
);
57+
if (!dbResults) return null;
58+
59+
const styleGuides = dbResults.map((record) => record.get("n").properties);
6060
if (!styleGuides.length) return null;
6161

6262
/**

0 commit comments

Comments
 (0)