Skip to content

Commit

Permalink
X
Browse files Browse the repository at this point in the history
  • Loading branch information
mewim committed Feb 13, 2024
1 parent ed41fd9 commit 2a05cf5
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 22 deletions.
14 changes: 9 additions & 5 deletions src/components/ShellView/ShellCell.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@
:is-maximized="isMaximized"
:navbar-height="navbarHeight"
/>
<div
v-if="isLoading"
class="d-flex align-items-center"
>
<div v-if="isLoading" class="d-flex align-items-center">
<strong class="text-secondary">{{
loadingText ? loadingText : "Loading..."
}}</strong>
Expand Down Expand Up @@ -90,7 +87,12 @@ export default {
this.errorMessage = "";
this.isLoading = true;
this.loadingText = "Evaluating query...";
Axios.post("/api/cypher", { query })
Axios.post("/api/cypher",
{
query,
uuid: this.cellId,
isQueryGenerationMode: this.$refs.editor.isQueryGenerationMode
})
.then((res) => {
this.queryResult = res.data;
this.queryString = query;
Expand Down Expand Up @@ -158,6 +160,8 @@ export default {
question,
token,
model,
uuid: this.cellId,
isQueryGenerationMode: this.$refs.editor.isQueryGenerationMode
};
Axios.post(url, data)
.then((res) => {
Expand Down
18 changes: 14 additions & 4 deletions src/components/ShellView/ShellMainView.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
<template>
<div
class="shell-main-view__wrapper"
:style="{ height: `${containerHeight}px` }"
>
<div class="shell-main-view__wrapper" :style="{ height: `${containerHeight}px` }">
<ShellCell
v-for="(cell, index) in shellCell"
v-show="index === maximizedCellIndex || maximizedCellIndex < 0"
Expand All @@ -23,6 +20,7 @@
<script lang="js">
import ShellCell from "./ShellCell.vue";
import { v4 as uuidv4 } from 'uuid';
import Axios from "axios";
export default {
name: "ShellMainView",
components: {
Expand Down Expand Up @@ -75,12 +73,24 @@ export default {
this.containerHeight = window.innerHeight - this.navbarHeight;
},
removeCell(index) {
const uuid = this.shellCell[index].cellId;
this.shellCell.splice(index, 1);
this.$nextTick(() => {
if (this.shellCell.length === 0) {
this.shellCell.push(this.createCell());
}
});
if (!uuid) {
return;
}
try {
this.removeCellFromHistory(uuid);
} catch (e) {
// Ignore
}
},
removeCellFromHistory(uuid) {
return Axios.delete(`/api/session/history/${uuid}`);
},
addCell() {
const cell = this.createCell();
Expand Down
22 changes: 18 additions & 4 deletions src/server/Cypher.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const database = require("./utils/Database");
const express = require("express");
const router = express.Router();
const logger = require("./utils/Logger");
const MODES = require("./utils/Constants").MODES;
const database = require("./utils/Database");
const sessionDb = require("./utils/SessionDatabase");

const DEMO_MODE = MODES.DEMO;

Expand Down Expand Up @@ -67,16 +68,29 @@ router.post("/", async (req, res) => {
if (mode === MODES.READ_WRITE) {
const currentSchema = await database.getSchema();
isSchemaChanged =
JSON.stringify(schema) !== JSON.stringify(currentSchema);
JSON.stringify(schema) !== JSON.stringify(currentSchema);
}
// This is a workaround for the JSON stringify issue with INT128 values
const replacer = (key, value) => {
if (typeof value === 'bigint') {
if (typeof value === "bigint") {
return value.toString();
}
return value;
};
const responseBody = JSON.stringify({ rows, dataTypes, isSchemaChanged }, replacer);
const responseBody = JSON.stringify(
{ rows, dataTypes, isSchemaChanged },
replacer
);
try {
await sessionDb.upsertHistoryItem({
uuid: req.body.uuid,
isQueryGenerationMode: Boolean(req.body.isQueryGenerationMode),
cypherQuery: query,
});
} catch (err) {
// Ignore the error. It fails to record the history, but the query is
// still executed.
}
return res.send(responseBody);
} catch (err) {
return res.status(400).send({ error: err.message });
Expand Down
14 changes: 13 additions & 1 deletion src/server/Gpt.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const openai = require("openai").OpenAI;
const express = require("express");
const router = express.Router();
const database = require("./utils/Database");
const sessionDb = require("./utils/SessionDatabase");

const getPrompt = (question, schema) => {
const prompt = `Task:Generate Cypher statement for Kùzu Graph Database Mangagement System to query a graph database.
Expand Down Expand Up @@ -61,7 +62,18 @@ router.post("/", async (req, res) => {
let query;
try {
query = chatCompletion.choices[0].message.content;
query = query.split("\n").join(" ");
query = query.split("\n").join(" ");
try {
await sessionDb.upsertHistoryItem({
uuid: req.body.uuid,
isQueryGenerationMode: Boolean(req.body.isQueryGenerationMode),
gptQuestion: question,
cypherQuery: query,
});
} catch (err) {
// Ignore the error. It fails to record the history, but the query is
// still executed.
}
res.send({ query, prompt, schema, model });
} catch (err) {
return res
Expand Down
20 changes: 12 additions & 8 deletions src/server/utils/SessionDatabase.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,13 @@ class SessionDatabase {
}
await this.db.run("BEGIN TRANSACTION;");
try {
const { uuid, isQueryGenerationMode, gptQuestion, cypherQuery } =
let { uuid, isQueryGenerationMode, gptQuestion, cypherQuery } =
historyItem;
const count = (
await this.db.get(
"SELECT COUNT(*) as count FROM history WHERE uuid = ?",
uuid
)
).count;
if (count === 0) {
const currentRow = await this.db.get(
"SELECT * FROM history WHERE uuid = ?",
uuid
);
if (!currentRow) {
await this.db.run(
"INSERT INTO history (uuid, isQueryGenerationMode, gptQuestion, cypherQuery) VALUES (?, ?, ?, ?)",
uuid,
Expand All @@ -156,6 +154,12 @@ class SessionDatabase {
cypherQuery
);
} else {
if (!gptQuestion) {
gptQuestion = currentRow.gptQuestion;
}
if (!cypherQuery) {
cypherQuery = currentRow.cypherQuery;
}
await this.db.run(
"UPDATE history SET isQueryGenerationMode = ?, gptQuestion = ?, cypherQuery = ? WHERE uuid = ?",
isQueryGenerationMode,
Expand Down

0 comments on commit 2a05cf5

Please sign in to comment.