mirror of
https://github.com/coaidev/coai.git
synced 2025-05-20 21:40:15 +09:00
add conversation clean feature
This commit is contained in:
parent
3b240aed58
commit
aaf32d198e
@ -15,13 +15,14 @@ import {
|
||||
useEffectAsync,
|
||||
} from "../../utils.ts";
|
||||
import {
|
||||
deleteAllConversations,
|
||||
deleteConversation,
|
||||
toggleConversation,
|
||||
updateConversationList,
|
||||
} from "../../conversation/history.ts";
|
||||
import { Button } from "../ui/button.tsx";
|
||||
import { setMenu } from "../../store/menu.ts";
|
||||
import { Copy, LogIn, Plus, RotateCw } from "lucide-react";
|
||||
import {Copy, Eraser, LogIn, Plus, RotateCw} from "lucide-react";
|
||||
import ConversationSegment from "./ConversationSegment.tsx";
|
||||
import {
|
||||
AlertDialog,
|
||||
@ -31,7 +32,7 @@ import {
|
||||
AlertDialogDescription,
|
||||
AlertDialogFooter,
|
||||
AlertDialogHeader,
|
||||
AlertDialogTitle,
|
||||
AlertDialogTitle, AlertDialogTrigger,
|
||||
} from "../ui/alert-dialog.tsx";
|
||||
import {
|
||||
getSharedLink,
|
||||
@ -54,6 +55,7 @@ function SideBar() {
|
||||
const history: ConversationInstance[] = useSelector(selectHistory);
|
||||
const refresh = useRef(null);
|
||||
const [shared, setShared] = useState<string>("");
|
||||
const [removeAll, setRemoveAll] = useState<boolean>(false);
|
||||
useEffectAsync(async () => {
|
||||
await updateConversationList(dispatch);
|
||||
}, []);
|
||||
@ -75,6 +77,47 @@ function SideBar() {
|
||||
<Plus className={`h-4 w-4`} />
|
||||
</Button>
|
||||
<div className={`grow`} />
|
||||
<AlertDialog open={removeAll} onOpenChange={setRemoveAll}>
|
||||
<AlertDialogTrigger asChild>
|
||||
<Button variant={`ghost`} size={`icon`}>
|
||||
<Eraser className={`h-4 w-4`} />
|
||||
</Button>
|
||||
</AlertDialogTrigger>
|
||||
<AlertDialogContent>
|
||||
<AlertDialogHeader>
|
||||
<AlertDialogTitle>{t("conversation.remove-all-title")}</AlertDialogTitle>
|
||||
<AlertDialogDescription>
|
||||
{t("conversation.remove-all-description")}
|
||||
</AlertDialogDescription>
|
||||
</AlertDialogHeader>
|
||||
<AlertDialogFooter>
|
||||
<AlertDialogCancel>
|
||||
{t("conversation.cancel")}
|
||||
</AlertDialogCancel>
|
||||
<AlertDialogAction
|
||||
onClick={async (e) => {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
|
||||
if (await deleteAllConversations(dispatch))
|
||||
toast({
|
||||
title: t("conversation.delete-success"),
|
||||
description: t("conversation.delete-success-prompt"),
|
||||
});
|
||||
else
|
||||
toast({
|
||||
title: t("conversation.delete-failed"),
|
||||
description: t("conversation.delete-failed-prompt"),
|
||||
});
|
||||
setOperateConversation({ target: null, type: "" });
|
||||
setRemoveAll(false);
|
||||
}}
|
||||
>
|
||||
{t("conversation.delete")}
|
||||
</AlertDialogAction>
|
||||
</AlertDialogFooter>
|
||||
</AlertDialogContent>
|
||||
</AlertDialog>
|
||||
<Button
|
||||
className={`refresh-action`}
|
||||
variant={`ghost`}
|
||||
|
@ -1,7 +1,7 @@
|
||||
import axios from "axios";
|
||||
import { Model } from "./conversation/types.ts";
|
||||
|
||||
export const version = "3.5.2";
|
||||
export const version = "3.5.3";
|
||||
export const dev: boolean = window.location.hostname === "localhost";
|
||||
export const deploy: boolean = true;
|
||||
export let rest_api: string = "http://localhost:8094";
|
||||
|
@ -36,6 +36,15 @@ export async function deleteConversation(
|
||||
return true;
|
||||
}
|
||||
|
||||
export async function deleteAllConversations(
|
||||
dispatch: AppDispatch,
|
||||
): Promise<boolean> {
|
||||
const resp = await axios.get("/conversation/clean");
|
||||
if (!resp.data.status) return false;
|
||||
await manager.deleteAll(dispatch);
|
||||
return true;
|
||||
}
|
||||
|
||||
export async function toggleConversation(
|
||||
dispatch: AppDispatch,
|
||||
id: number,
|
||||
|
@ -82,6 +82,13 @@ export class Manager {
|
||||
if (this.conversations[id]) delete this.conversations[id];
|
||||
}
|
||||
|
||||
public async deleteAll(dispatch: AppDispatch): Promise<void> {
|
||||
const ids = Object.keys(this.conversations).map((v) => parseInt(v));
|
||||
for (const id of ids) await this.delete(dispatch, id);
|
||||
|
||||
await this.toggle(dispatch, -1);
|
||||
}
|
||||
|
||||
public async send(t: any, auth: boolean, props: ChatProps): Promise<boolean> {
|
||||
const id = this.getCurrent();
|
||||
if (!this.conversations[id]) return false;
|
||||
|
@ -54,7 +54,7 @@ function calc_prize(month: number): number {
|
||||
return base * 0.9;
|
||||
}
|
||||
|
||||
return 8 * month;
|
||||
return base;
|
||||
}
|
||||
|
||||
type UpgradeProps = {
|
||||
|
@ -38,6 +38,9 @@ const resources = {
|
||||
"remove-title": "Are you absolutely sure?",
|
||||
"remove-description":
|
||||
"This action cannot be undone. This will permanently delete the conversation ",
|
||||
"remove-all-title": "Clear History",
|
||||
"remove-all-description":
|
||||
"This action cannot be undone. This will permanently delete all conversations, continue?",
|
||||
cancel: "Cancel",
|
||||
delete: "Delete",
|
||||
"delete-conversation": "Delete Conversation",
|
||||
@ -225,6 +228,8 @@ const resources = {
|
||||
"refresh-failed-prompt": "请求出错,请重试。",
|
||||
"remove-title": "是否确定?",
|
||||
"remove-description": "此操作无法撤消。这将永久删除对话 ",
|
||||
"remove-all-title": "清除历史",
|
||||
"remove-all-description": "此操作无法撤消。这将永久删除所有对话,是否继续?",
|
||||
cancel: "取消",
|
||||
delete: "删除",
|
||||
"delete-conversation": "删除对话",
|
||||
@ -411,6 +416,9 @@ const resources = {
|
||||
"remove-title": "Вы уверены?",
|
||||
"remove-description":
|
||||
"Это действие нельзя отменить. Это навсегда удалит разговор ",
|
||||
"remove-all-title": "Очистить историю",
|
||||
"remove-all-description":
|
||||
"Это действие нельзя отменить. Это навсегда удалит все разговоры, продолжить?",
|
||||
cancel: "Отмена",
|
||||
delete: "Удалить",
|
||||
"delete-conversation": "Удалить разговор",
|
||||
|
@ -101,6 +101,31 @@ func DeleteAPI(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
func CleanAPI(c *gin.Context) {
|
||||
user := auth.GetUser(c)
|
||||
if user == nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": false,
|
||||
"message": "user not found",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
db := utils.GetDBFromContext(c)
|
||||
if err := DeleteAllConversations(db, *user); err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": false,
|
||||
"message": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": true,
|
||||
"message": "",
|
||||
})
|
||||
}
|
||||
|
||||
func ShareAPI(c *gin.Context) {
|
||||
user := auth.GetUser(c)
|
||||
if user == nil {
|
||||
|
@ -8,6 +8,7 @@ func Register(app *gin.Engine) {
|
||||
router.GET("/list", ListAPI)
|
||||
router.GET("/load", LoadAPI)
|
||||
router.GET("/delete", DeleteAPI)
|
||||
router.GET("/clean", CleanAPI)
|
||||
|
||||
// share
|
||||
router.POST("/share", ShareAPI)
|
||||
|
@ -1,6 +1,7 @@
|
||||
package conversation
|
||||
|
||||
import (
|
||||
"chat/auth"
|
||||
"chat/globals"
|
||||
"chat/utils"
|
||||
"database/sql"
|
||||
@ -97,3 +98,8 @@ func (c *Conversation) DeleteConversation(db *sql.DB) bool {
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func DeleteAllConversations(db *sql.DB, user auth.User) error {
|
||||
_, err := db.Exec("DELETE FROM conversation WHERE user_id = ?", user.GetID(db))
|
||||
return err
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user