Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Bot menu: delete closed threads in one step

AdityaUmale0411d4104bdd
diff --git a/src/components/BotMenuDelete.test.ts b/src/components/BotMenuDelete.test.ts
index d4753df084..3466017178 100644
--- a/src/components/BotMenuDelete.test.ts
+++ b/src/components/BotMenuDelete.test.ts
@@ -1,6 +1,6 @@
import { describe, expect, it, vi } from "vitest";
import { api, type Bot } from "@/state/store";
-import { allThreadRows, deleteAllThreads, deleteAllThreadsCopy } from "./BotMenuDelete";
+import { allThreadRows, closedThreadRows, deleteAllThreads, deleteAllThreadsCopy } from "./BotMenuDelete";
vi.mock("@/state/store", async (original) => ({ ...await original<typeof import("@/state/store")>(), api: vi.fn() }));
@@ -42,3 +42,41 @@ describe("delete all threads", () => {
expect(dispatch).toHaveBeenLastCalledWith({ type: "notice", notice: { kind: "note", botName: null, text: "Deleted: 1. Kept because they are running: 1." } });
});
});
+
+describe("delete closed threads", () => {
+ const closer = { botId: "helper", name: "Helper", at: 5 };
+ const closedBot: Bot = {
+ ...bot,
+ tasks: [
+ { threadId: "open", title: "Open chat", createdAt: 5 },
+ { threadId: "done", title: "Finished job", createdAt: 4, closedBy: closer },
+ { threadId: "done-pinned", title: "Pinned finished job", createdAt: 3, pinned: true, closedBy: closer },
+ { threadId: "done-run", title: "Closed routine run", createdAt: 2, routineRunId: "run-2", closedBy: closer },
+ { threadId: "old", title: "Older finished job", createdAt: 1, archivedAt: 9, closedBy: closer },
+ ],
+ };
+
+ it("takes only closed threads, never pinned ones or routine runs", () => {
+ expect(closedThreadRows(allThreadRows(closedBot)).map((row) => row.threadId)).toEqual(["done", "old"]);
+ });
+
+ it("names the closed threads in the confirm and keeps the other threads", () => {
+ const copy = deleteAllThreadsCopy(allThreadRows(closedBot), false, "closed");
+ expect(copy.title).toBe("Delete closed threads (2)?");
+ expect(copy.confirmLabel).toBe("Delete 2");
+ expect(copy.body).not.toContain("Pinned threads included");
+ expect(copy.body).not.toContain("a new empty conversation will be started");
+ expect(deleteAllThreadsCopy(allThreadRows(closedBot), true, "closed").title).toBe("Delete closed conversations (2)?");
+ });
+
+ it("sends only the closed threads", async () => {
+ const post = vi.mocked(api);
+ post.mockResolvedValueOnce({ deleted: ["done", "old"], refused: [], bot: { id: "maus" } });
+ const dispatch = vi.fn();
+
+ await deleteAllThreads(closedBot, dispatch, "closed");
+
+ expect(post).toHaveBeenLastCalledWith("/api/bots/maus/tasks/delete", { method: "POST", body: JSON.stringify({ threadIds: ["done", "old"] }) });
+ expect(dispatch).toHaveBeenLastCalledWith({ type: "notice", notice: { kind: "note", botName: null, text: "Deleted: 2." } });
+ });
+});
diff --git a/src/components/BotMenuDelete.tsx b/src/components/BotMenuDelete.tsx
index bd9434df72..e7f28f3e59 100644
--- a/src/components/BotMenuDelete.tsx
+++ b/src/components/BotMenuDelete.tsx
@@ -35,33 +35,47 @@ export function allThreadRows(bot: Bot) {
}
type Dispatch = (action: Action) => void;
+type ThreadRows = ReturnType<typeof allThreadRows>;
+export type DeleteScope = "all" | "closed";
-export async function deleteAllThreads(bot: Bot, dispatch: Dispatch): Promise<void> {
- const outcome = await deleteThreadsInBulk({ botId: bot.id }, allThreadRows(bot).map((row) => row.threadId), dispatch);
+export const closedThreadRows = (rows: ThreadRows): ThreadRows =>
+ rows.filter((row) => Boolean(row.closedBy) && row.pinned !== true && isTickable(row));
+
+const scopedRows = (rows: ThreadRows, scope: DeleteScope): ThreadRows => (scope === "closed" ? closedThreadRows(rows) : rows);
+
+export async function deleteAllThreads(bot: Bot, dispatch: Dispatch, scope: DeleteScope = "all"): Promise<void> {
+ const outcome = await deleteThreadsInBulk({ botId: bot.id }, scopedRows(allThreadRows(bot), scope).map((row) => row.threadId), dispatch);
dispatch({ type: "notice", notice: { kind: "note", botName: null, text: bulkDeleteOutcome(outcome) } });
}
-export function deleteAllThreadsCopy(rows: ReturnType<typeof allThreadRows>, simple = false) {
- const ids = new Set(rows.map((row) => row.threadId));
+const confirmTitle = {
+ all: { simple: "task.deleteAllConversationsConfirm", advanced: "task.deleteAllConfirm" },
+ closed: { simple: "task.deleteClosedConversationsConfirm", advanced: "task.deleteClosedConfirm" },
+} as const;
+
+export function deleteAllThreadsCopy(rows: ThreadRows, simple = false, scope: DeleteScope = "all") {
+ const picked = scopedRows(rows, scope);
+ const ids = new Set(picked.map((row) => row.threadId));
const startsFresh = rows.every(isTickable);
return {
- title: t(simple ? "task.deleteAllConversationsConfirm" : "task.deleteAllConfirm", { count: rows.length }),
+ title: t(confirmTitle[scope][simple ? "simple" : "advanced"], { count: picked.length }),
body: bulkDeleteBody(rows, ids, startsFresh),
- confirmLabel: t("task.bulk.delete", { count: rows.length }),
+ confirmLabel: t("task.bulk.delete", { count: picked.length }),
};
}
export function useDeleteAllThreads(simple = false) {
const { state, dispatch } = useStore();
- const [botId, setBotId] = useState<string | null>(null);
+ const [asked, setAsked] = useState<{ botId: string; scope: DeleteScope } | null>(null);
const [pending, setPending] = useState(false);
- const bot = botId === null ? undefined : state.bots.find((candidate) => candidate.id === botId);
+ const bot = asked === null ? undefined : state.bots.find((candidate) => candidate.id === asked.botId);
+ const scope = asked?.scope ?? "all";
const run = () => {
if (!bot || pending) return;
setPending(true);
- void deleteAllThreads(bot, dispatch).finally(() => { setPending(false); setBotId(null); });
+ void deleteAllThreads(bot, dispatch, scope).finally(() => { setPending(false); setAsked(null); });
};
- const dialog = <ConfirmDialog open={bot !== undefined} {...deleteAllThreadsCopy(bot ? allThreadRows(bot) : [], simple)} icon={<Trash2 size={18} />}
- pending={pending} onCancel={() => { if (!pending) setBotId(null); }} onConfirm={run} />;
- return { ask: setBotId, dialog };
+ const dialog = <ConfirmDialog open={bot !== undefined} {...deleteAllThreadsCopy(bot ? allThreadRows(bot) : [], simple, scope)} icon={<Trash2 size={18} />}
+ pending={pending} onCancel={() => { if (!pending) setAsked(null); }} onConfirm={run} />;
+ return { ask: (botId: string, askedScope: DeleteScope = "all") => setAsked({ botId, scope: askedScope }), dialog };
}
diff --git a/src/components/SidebarBotMenu.tsx b/src/components/SidebarBotMenu.tsx
index 2e4c205606..0529557b6a 100644
--- a/src/components/SidebarBotMenu.tsx
+++ b/src/components/SidebarBotMenu.tsx
@@ -7,7 +7,7 @@ import { cn } from "@/lib/cn";
import { useHeldMenuMotion } from "./MenuMotion";
import { t } from "@/lib/i18n";
import { copyText } from "@/lib/copy-text";
-import { allThreadRows, BotDeleteMenuItem, useDeleteAllThreads } from "./BotMenuDelete";
+import { allThreadRows, BotDeleteMenuItem, closedThreadRows, useDeleteAllThreads } from "./BotMenuDelete";
import { DemoteDefaultBotItem, HideDefaultBotItem } from "./DefaultBotControls";
import { navigateThreadMenu } from "./BotProjects";
import { useOwnerOrAdmin } from "@/lib/use-owner-or-admin";
@@ -265,7 +265,12 @@ export function BotContextMenu({
if (!state.settingsOpen) dispatch({ type: "toggleSettings", open: true });
});
const owner = team && mayChangeOwner(bot, team) ? item(<Users size={16} className="text-ink-secondary" />, ownerMenuLabel(bot, team), () => pickOwnerOf(bot, team, shown)) : null;
- const deleteAll = !member && allThreadRows(bot).length >= 2 ? [divider("more-threads"), item(<Trash2 size={16} />, t("task.deleteAllConversations"), () => clearThreads.ask(bot.id), { danger: true })] : [];
+ const rows = member ? [] : allThreadRows(bot);
+ const threadDeletes = [
+ ...(rows.length >= 2 ? [item(<Trash2 size={16} />, t("task.deleteAllConversations"), () => clearThreads.ask(bot.id), { danger: true })] : []),
+ ...(closedThreadRows(rows).length > 0 ? [item(<Trash2 size={16} />, t("task.deleteClosedConversations"), () => clearThreads.ask(bot.id, "closed"), { danger: true })] : []),
+ ];
+ const deleteAll = threadDeletes.length > 0 ? [divider("more-threads"), ...threadDeletes] : [];
const readState = readingBotId === bot.id || botShowsUnread(bot)
? item(
readingBotId === bot.id ? <Loader2 size={16} className="animate-spin text-ink-secondary" /> : <CheckCheck size={16} className="text-ink-secondary" />,
@@ -397,6 +402,7 @@ export function BotContextMenu({
{item(<FolderPlus size={16} className="text-ink-secondary" />, t("folder.new"), () => onNewFolder(bot.id))}
</>}
{!member && item(<Trash2 size={16} />, t("task.deleteAll"), () => clearThreads.ask(bot.id), { danger: true })}
+ {!member && closedThreadRows(allThreadRows(bot)).length > 0 && item(<Trash2 size={16} />, t("task.deleteClosed"), () => clearThreads.ask(bot.id, "closed"), { danger: true })}
{divider("threads")}
{team && mayChangeOwner(bot, team) && item(<Users size={16} className="text-ink-secondary" />, ownerMenuLabel(bot, team), () => pickOwnerOf(bot, team, shown))}
{remoteClient ? [
diff --git a/src/locales/de.json b/src/locales/de.json
index f1e269695b..a58bb8062b 100644
--- a/src/locales/de.json
+++ b/src/locales/de.json
@@ -1266,6 +1266,10 @@
"task.deleteTitle": "Diesen Thread und sein Gespräch löschen",
"task.deleteAll": "Alle Threads löschen…",
"task.deleteAllConfirm": "Alle Threads löschen ({count})?",
+ "task.deleteClosed": "Geschlossene Threads löschen…",
+ "task.deleteClosedConfirm": "Geschlossene Threads löschen ({count})?",
+ "task.deleteClosedConversations": "Geschlossene Unterhaltungen löschen…",
+ "task.deleteClosedConversationsConfirm": "Geschlossene Unterhaltungen löschen ({count})?",
"model.setupRequired": "Einrichtung nötig",
"model.signInRequired": "Anmeldung nötig",
"model.ready": "Bereit",
diff --git a/src/locales/en.json b/src/locales/en.json
index c86657d93e..408b9ae643 100644
--- a/src/locales/en.json
+++ b/src/locales/en.json
@@ -3473,6 +3473,10 @@
"task.deleteAllConfirm": "Delete all threads ({count})?",
"task.deleteAllConversations": "Delete all conversations…",
"task.deleteAllConversationsConfirm": "Delete all conversations ({count})?",
+ "task.deleteClosed": "Delete closed threads…",
+ "task.deleteClosedConfirm": "Delete closed threads ({count})?",
+ "task.deleteClosedConversations": "Delete closed conversations…",
+ "task.deleteClosedConversationsConfirm": "Delete closed conversations ({count})?",
"folder.settings": "Folder settings",
"folder.markRead": "Mark folder as read",
"folder.showAll": "Show all",
diff --git a/src/locales/pt-br.json b/src/locales/pt-br.json
index 0840202a50..ff21d52d1f 100644
--- a/src/locales/pt-br.json
+++ b/src/locales/pt-br.json
@@ -3260,6 +3260,10 @@
"task.deleteAllConfirm": "Excluir todas as conversas ({count})?",
"task.deleteAllConversations": "Excluir todas as conversas…",
"task.deleteAllConversationsConfirm": "Excluir todas as conversas ({count})?",
+ "task.deleteClosed": "Excluir conversas encerradas…",
+ "task.deleteClosedConfirm": "Excluir conversas encerradas ({count})?",
+ "task.deleteClosedConversations": "Excluir conversas encerradas…",
+ "task.deleteClosedConversationsConfirm": "Excluir conversas encerradas ({count})?",
"folder.settings": "Configurações da pasta",
"folder.markRead": "Marcar a pasta como lida",
"folder.showAll": "Mostrar tudo",