anything-llm/server/models/workspaceChats.js

172 lines
4.9 KiB
JavaScript
Raw Normal View History

const { checkForMigrations } = require("../utils/database");
2023-06-04 04:28:07 +02:00
const WorkspaceChats = {
2023-06-08 06:31:35 +02:00
tablename: "workspace_chats",
2023-06-04 04:28:07 +02:00
colsInit: `
id INTEGER PRIMARY KEY AUTOINCREMENT,
workspaceId INTEGER NOT NULL,
prompt TEXT NOT NULL,
response TEXT NOT NULL,
include BOOL DEFAULT true,
user_id INTEGER DEFAULT NULL,
2023-06-04 04:28:07 +02:00
createdAt TEXT DEFAULT CURRENT_TIMESTAMP,
lastUpdatedAt TEXT DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE
2023-06-04 04:28:07 +02:00
`,
migrateTable: async function () {
console.log(
`\x1b[34m[MIGRATING]\x1b[0m Checking for WorkspaceChats migrations`
);
const db = await this.db(false);
await checkForMigrations(this, db);
},
migrations: function () {
return [
{
colName: "user_id",
execCmd: `ALTER TABLE ${this.tablename} ADD COLUMN user_id INTEGER DEFAULT NULL`,
doif: false,
},
];
},
db: async function (tracing = true) {
2023-06-08 06:31:35 +02:00
const sqlite3 = require("sqlite3").verbose();
const { open } = require("sqlite");
2023-06-04 04:28:07 +02:00
const db = await open({
filename: `${
!!process.env.STORAGE_DIR ? `${process.env.STORAGE_DIR}/` : "storage/"
}anythingllm.db`,
2023-06-08 06:31:35 +02:00
driver: sqlite3.Database,
});
2023-06-04 04:28:07 +02:00
2023-06-08 06:31:35 +02:00
await db.exec(
`PRAGMA foreign_keys = ON;CREATE TABLE IF NOT EXISTS ${this.tablename} (${this.colsInit})`
2023-06-08 06:31:35 +02:00
);
if (tracing) db.on("trace", (sql) => console.log(sql));
2023-06-08 06:31:35 +02:00
return db;
2023-06-04 04:28:07 +02:00
},
new: async function ({ workspaceId, prompt, response = {}, user = null }) {
2023-06-08 06:31:35 +02:00
const db = await this.db();
const { id, success, message } = await db
.run(
`INSERT INTO ${this.tablename} (workspaceId, prompt, response, user_id) VALUES (?, ?, ?, ?)`,
[workspaceId, prompt, JSON.stringify(response), user?.id || null]
2023-06-08 06:31:35 +02:00
)
2023-06-04 04:28:07 +02:00
.then((res) => {
2023-06-08 06:31:35 +02:00
return { id: res.lastID, success: true, message: null };
2023-06-04 04:28:07 +02:00
})
.catch((error) => {
2023-06-08 06:31:35 +02:00
return { id: null, success: false, message: error.message };
});
if (!success) {
db.close();
return { chat: null, message };
}
2023-06-04 04:28:07 +02:00
2023-06-08 06:31:35 +02:00
const chat = await db.get(
`SELECT * FROM ${this.tablename} WHERE id = ${id}`
);
db.close();
2023-06-08 06:31:35 +02:00
return { chat, message: null };
2023-06-04 04:28:07 +02:00
},
forWorkspaceByUser: async function (
workspaceId = null,
userId = null,
limit = null
) {
if (!workspaceId || !userId) return [];
return await this.where(
`workspaceId = ${workspaceId} AND include = true AND user_id = ${userId}`,
limit,
"ORDER BY id ASC"
);
},
forWorkspace: async function (workspaceId = null, limit = null) {
2023-06-04 04:28:07 +02:00
if (!workspaceId) return [];
2023-06-08 06:31:35 +02:00
return await this.where(
`workspaceId = ${workspaceId} AND include = true`,
limit,
2023-06-08 06:31:35 +02:00
"ORDER BY id ASC"
);
2023-06-04 04:28:07 +02:00
},
markHistoryInvalid: async function (workspaceId = null, user = null) {
2023-06-04 04:28:07 +02:00
if (!workspaceId) return;
2023-06-08 06:31:35 +02:00
const db = await this.db();
await db.run(
`UPDATE ${this.tablename} SET include = false WHERE workspaceId = ? ${
user ? `AND user_id = ${user.id}` : ""
}`,
2023-06-08 06:31:35 +02:00
[workspaceId]
);
db.close();
2023-06-04 04:28:07 +02:00
return;
},
get: async function (clause = "", limit = null, order = null) {
2023-06-08 06:31:35 +02:00
const db = await this.db();
const result = await db
.get(
`SELECT * FROM ${this.tablename} WHERE ${clause} ${
!!order ? order : ""
} ${!!limit ? `LIMIT ${limit}` : ""}`
)
2023-06-08 06:31:35 +02:00
.then((res) => res || null);
db.close();
2023-06-04 04:28:07 +02:00
if (!result) return null;
2023-06-08 06:31:35 +02:00
return result;
2023-06-04 04:28:07 +02:00
},
2023-06-08 06:31:35 +02:00
delete: async function (clause = "") {
const db = await this.db();
await db.get(`DELETE FROM ${this.tablename} WHERE ${clause}`);
db.close();
2023-06-08 06:31:35 +02:00
return true;
2023-06-04 04:28:07 +02:00
},
2023-06-08 06:31:35 +02:00
where: async function (clause = "", limit = null, order = null) {
const db = await this.db();
const results = await db.all(
`SELECT * FROM ${this.tablename} ${clause ? `WHERE ${clause}` : ""} ${
!!order ? order : ""
} ${!!limit ? `LIMIT ${limit}` : ""}`
);
db.close();
return results;
},
count: async function (clause = null) {
const db = await this.db();
const { count } = await db.get(
`SELECT COUNT(*) as count FROM ${this.tablename} ${
clause ? `WHERE ${clause}` : ""
} `
2023-06-08 06:31:35 +02:00
);
db.close();
return count;
},
whereWithData: async function (clause = "", limit = null, order = null) {
const { Workspace } = require("./workspace");
const { User } = require("./user");
const results = await this.where(clause, limit, order);
for (const res of results) {
const workspace = await Workspace.get(`id = ${res.workspaceId}`);
res.workspace = workspace
? { name: workspace.name, slug: workspace.slug }
: { name: "deleted workspace", slug: null };
const user = await User.get(`id = ${res.user_id}`);
res.user = user
? { username: user.username }
: { username: "deleted user" };
}
2023-06-08 06:31:35 +02:00
return results;
2023-06-04 04:28:07 +02:00
},
2023-06-08 06:31:35 +02:00
};
2023-06-04 04:28:07 +02:00
2023-06-08 06:31:35 +02:00
module.exports = { WorkspaceChats };