anything-llm/server/models/embedChats.js
Sean Hatfield 1846a99b93
[FEAT] Embedded AnythingLLM (#656)
* WIP embedded app

* WIP got response from backend in embedded app

* WIP streaming prints to embedded app

* implemented streaming and tailwind min for styling into embedded app

* WIP embedded app history functional

* load params from script tag into embedded app

* rough in modularization of embed chat
cleanup dev process for easier dev support
move all chat to components
todo: build process
todo: backend support

* remove eslint config

* Implement models and cleanup embed chat endpoints
Improve build process for embed
prod minification and bundle size awareness
WIP

* forgot files

* rename to embed folder

* introduce chat modal styles

* add middleware validations on embed chat

* auto open param and default greeting

* reset chat history

* Admin embed config page

* Admin Embed Chats mgmt page

* update embed

* nonpriv

* more style support
reopen if chat was last opened

* update comments

* remove unused imports

* allow change of workspace for embedconfig

* update failure to lookup message

* update reset script

* update instructions

* Add more styling options
Add sponsor text at bottom
Support dynamic container height
Loading animations

* publish new embed script

* Add back syntax highlighting and keep bundle small via dynamic script build

* add hint

* update readme

* update copy model for snippet with link to styles

---------

Co-authored-by: timothycarambat <rambat1010@gmail.com>
2024-02-05 14:21:34 -08:00

163 lines
3.6 KiB
JavaScript

const prisma = require("../utils/prisma");
const EmbedChats = {
new: async function ({
embedId,
prompt,
response = {},
connection_information = {},
sessionId,
}) {
try {
const chat = await prisma.embed_chats.create({
data: {
prompt,
embed_id: Number(embedId),
response: JSON.stringify(response),
connection_information: JSON.stringify(connection_information),
session_id: sessionId,
},
});
return { chat, message: null };
} catch (error) {
console.error(error.message);
return { chat: null, message: error.message };
}
},
forEmbedByUser: async function (
embedId = null,
sessionId = null,
limit = null,
orderBy = null
) {
if (!embedId || !sessionId) return [];
try {
const chats = await prisma.embed_chats.findMany({
where: {
embed_id: embedId,
session_id: sessionId,
include: true,
},
...(limit !== null ? { take: limit } : {}),
...(orderBy !== null ? { orderBy } : { orderBy: { id: "asc" } }),
});
return chats;
} catch (error) {
console.error(error.message);
return [];
}
},
markHistoryInvalid: async function (embedId = null, sessionId = null) {
if (!embedId || !sessionId) return [];
try {
await prisma.embed_chats.updateMany({
where: {
embed_id: embedId,
session_id: sessionId,
},
data: {
include: false,
},
});
return;
} catch (error) {
console.error(error.message);
}
},
get: async function (clause = {}, limit = null, orderBy = null) {
try {
const chat = await prisma.embed_chats.findFirst({
where: clause,
...(limit !== null ? { take: limit } : {}),
...(orderBy !== null ? { orderBy } : {}),
});
return chat || null;
} catch (error) {
console.error(error.message);
return null;
}
},
delete: async function (clause = {}) {
try {
await prisma.embed_chats.deleteMany({
where: clause,
});
return true;
} catch (error) {
console.error(error.message);
return false;
}
},
where: async function (
clause = {},
limit = null,
orderBy = null,
offset = null
) {
try {
const chats = await prisma.embed_chats.findMany({
where: clause,
...(limit !== null ? { take: limit } : {}),
...(offset !== null ? { skip: offset } : {}),
...(orderBy !== null ? { orderBy } : {}),
});
return chats;
} catch (error) {
console.error(error.message);
return [];
}
},
whereWithEmbedAndWorkspace: async function (
clause = {},
limit = null,
orderBy = null,
offset = null
) {
try {
const chats = await prisma.embed_chats.findMany({
where: clause,
include: {
embed_config: {
select: {
workspace: {
select: {
name: true,
},
},
},
},
},
...(limit !== null ? { take: limit } : {}),
...(offset !== null ? { skip: offset } : {}),
...(orderBy !== null ? { orderBy } : {}),
});
return chats;
} catch (error) {
console.error(error.message);
return [];
}
},
count: async function (clause = {}) {
try {
const count = await prisma.embed_chats.count({
where: clause,
});
return count;
} catch (error) {
console.error(error.message);
return 0;
}
},
};
module.exports = { EmbedChats };