mirror of
https://github.com/Mintplex-Labs/anything-llm.git
synced 2024-11-13 02:00:10 +01:00
patch: Auto thread renaming (#1636)
This commit is contained in:
parent
3c98d15c6a
commit
b30dc1c956
@ -5,6 +5,7 @@ import { Plus, CircleNotch, Trash } from "@phosphor-icons/react";
|
||||
import { useEffect, useState } from "react";
|
||||
import ThreadItem from "./ThreadItem";
|
||||
import { useParams } from "react-router-dom";
|
||||
export const THREAD_RENAME_EVENT = "renameThread";
|
||||
|
||||
export default function ThreadContainer({ workspace }) {
|
||||
const { threadSlug = null } = useParams();
|
||||
@ -25,10 +26,10 @@ export default function ThreadContainer({ workspace }) {
|
||||
);
|
||||
};
|
||||
|
||||
window.addEventListener("renameThread", chatHandler);
|
||||
window.addEventListener(THREAD_RENAME_EVENT, chatHandler);
|
||||
|
||||
return () => {
|
||||
window.removeEventListener("renameThread", chatHandler);
|
||||
window.removeEventListener(THREAD_RENAME_EVENT, chatHandler);
|
||||
};
|
||||
}, []);
|
||||
|
||||
|
@ -12,7 +12,6 @@ import handleSocketResponse, {
|
||||
AGENT_SESSION_END,
|
||||
AGENT_SESSION_START,
|
||||
} from "@/utils/chat/agent";
|
||||
import truncate from "truncate";
|
||||
|
||||
export default function ChatContainer({ workspace, knownHistory = [] }) {
|
||||
const { threadSlug = null } = useParams();
|
||||
@ -39,19 +38,6 @@ export default function ChatContainer({ workspace, knownHistory = [] }) {
|
||||
const handleSubmit = async (event) => {
|
||||
event.preventDefault();
|
||||
if (!message || message === "") return false;
|
||||
|
||||
// If first message and it is a thread
|
||||
// and message is not blank/whitespace,
|
||||
// then send event to rename the thread
|
||||
if (threadSlug && chatHistory.length === 0 && message.trim().length > 0) {
|
||||
const truncatedName = truncate(message, 22);
|
||||
window.dispatchEvent(
|
||||
new CustomEvent("renameThread", {
|
||||
detail: { threadSlug, newName: truncatedName },
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
const prevChatHistory = [
|
||||
...chatHistory,
|
||||
{ content: message, role: "user" },
|
||||
|
@ -1,3 +1,4 @@
|
||||
import { THREAD_RENAME_EVENT } from "@/components/Sidebar/ActiveWorkspaces/ThreadContainer";
|
||||
export const ABORT_STREAM_EVENT = "abort-chat-stream";
|
||||
|
||||
// For handling of chat responses in the frontend by their various types.
|
||||
@ -136,6 +137,21 @@ export default function handleChat(
|
||||
// Chat was reset, keep reset message and clear everything else.
|
||||
setChatHistory([_chatHistory.pop()]);
|
||||
}
|
||||
|
||||
// If thread was updated automatically based on chat prompt
|
||||
// then we can handle the updating of the thread here.
|
||||
if (action === "rename_thread") {
|
||||
if (!!chatResult?.thread?.slug && chatResult.thread.name) {
|
||||
window.dispatchEvent(
|
||||
new CustomEvent(THREAD_RENAME_EVENT, {
|
||||
detail: {
|
||||
threadSlug: chatResult.thread.slug,
|
||||
newName: chatResult.thread.name,
|
||||
},
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function chatPrompt(workspace) {
|
||||
|
@ -199,11 +199,21 @@ function chatEndpoints(app) {
|
||||
thread
|
||||
);
|
||||
|
||||
// If thread was renamed emit event to frontend via special `action` response.
|
||||
await WorkspaceThread.autoRenameThread({
|
||||
thread,
|
||||
workspace,
|
||||
user,
|
||||
newName: truncate(message, 22),
|
||||
onRename: (thread) => {
|
||||
writeResponseChunk(response, {
|
||||
action: "rename_thread",
|
||||
thread: {
|
||||
slug: thread.slug,
|
||||
name: thread.name,
|
||||
},
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
await Telemetry.sendTelemetry("sent_chat", {
|
||||
|
@ -2,13 +2,14 @@ const prisma = require("../utils/prisma");
|
||||
const { v4: uuidv4 } = require("uuid");
|
||||
|
||||
const WorkspaceThread = {
|
||||
defaultName: "Thread",
|
||||
writable: ["name"],
|
||||
|
||||
new: async function (workspace, userId = null) {
|
||||
try {
|
||||
const thread = await prisma.workspace_threads.create({
|
||||
data: {
|
||||
name: "Thread",
|
||||
name: this.defaultName,
|
||||
slug: uuidv4(),
|
||||
user_id: userId ? Number(userId) : null,
|
||||
workspace_id: workspace.id,
|
||||
@ -91,16 +92,23 @@ const WorkspaceThread = {
|
||||
thread = null,
|
||||
user = null,
|
||||
newName = null,
|
||||
onRename = null,
|
||||
}) {
|
||||
if (!workspace || !thread || !newName) return false;
|
||||
if (thread.name !== this.defaultName) return false; // don't rename if already named.
|
||||
|
||||
const { WorkspaceChats } = require("./workspaceChats");
|
||||
const chatCount = await WorkspaceChats.count({
|
||||
workspaceId: workspace.id,
|
||||
user_id: user?.id || null,
|
||||
thread_id: thread.id,
|
||||
});
|
||||
if (chatCount !== 1) return false;
|
||||
await this.update(thread, { name: newName });
|
||||
if (chatCount !== 1) return { renamed: false, thread };
|
||||
const { thread: updatedThread } = await this.update(thread, {
|
||||
name: newName,
|
||||
});
|
||||
|
||||
onRename?.(updatedThread);
|
||||
return true;
|
||||
},
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user