这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
};
}, []);

Expand Down
14 changes: 0 additions & 14 deletions frontend/src/components/WorkspaceChat/ChatContainer/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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" },
Expand Down
16 changes: 16 additions & 0 deletions frontend/src/utils/chat/index.js
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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) {
Expand Down
10 changes: 10 additions & 0 deletions server/endpoints/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -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", {
Expand Down
14 changes: 11 additions & 3 deletions server/models/workspaceThread.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
},
};
Expand Down