Skip to content

Commit decb44c

Browse files
authored
Merge pull request #199 from n4ze3m/next
v1.7.1
2 parents d5fd600 + 76a3445 commit decb44c

File tree

12 files changed

+317
-20
lines changed

12 files changed

+317
-20
lines changed

app/ui/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "app",
33
"private": true,
4-
"version": "1.7.0",
4+
"version": "1.7.1",
55
"type": "module",
66
"scripts": {
77
"dev": "vite",
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,83 @@
1-
import { ChatBubbleLeftIcon } from "@heroicons/react/24/outline";
2-
import { Link, useParams } from "react-router-dom";
1+
import { Link, useNavigate, useParams } from "react-router-dom";
32
import { useMessage } from "../../../hooks/useMessage";
43
import { PlaygroundHistory } from "./types";
4+
import {
5+
EllipsisHorizontalIcon,
6+
PencilIcon,
7+
TrashIcon,
8+
} from "@heroicons/react/24/outline";
9+
import { Dropdown, notification } from "antd";
10+
import api from "../../../services/api";
11+
import { useMutation, useQueryClient } from "@tanstack/react-query";
12+
import { Spin } from "antd";
13+
import React from "react";
514

615
export const PlaygroundHistoryCard = ({
716
item,
817
}: {
918
item: PlaygroundHistory;
1019
}) => {
11-
const { historyId, setMessages, setHistory, setIsLoading } = useMessage();
20+
const { historyId, setHistoryId, setMessages, setHistory } = useMessage();
1221
const params = useParams<{ id: string; history_id?: string }>();
22+
const navigate = useNavigate();
23+
const queryClient = useQueryClient();
24+
const [processingId, setProcessingId] = React.useState<string | null>(null);
25+
26+
const onDelete = async () => {
27+
const url = `bot/playground/history/${item.id}`;
28+
const response = await api.delete(url);
29+
return response.data;
30+
};
31+
32+
const onEdit = async (title: string) => {
33+
const url = `bot/playground/history/${item.id}`;
34+
const response = await api.put(url, { title });
35+
return response.data;
36+
};
37+
38+
const { mutate: editHistory, isLoading: isEditing } = useMutation(onEdit, {
39+
onSuccess: () => {
40+
queryClient.invalidateQueries([
41+
"getBotPlaygroundHistory",
42+
params.id,
43+
params.history_id,
44+
]);
45+
},
46+
onError: () => {
47+
notification.error({
48+
message: "Error",
49+
description: "Something went wrong",
50+
});
51+
},
52+
});
53+
54+
const { mutate: deleteHistory, isLoading: isDeleting } = useMutation(
55+
onDelete,
56+
{
57+
onSuccess: () => {
58+
if (historyId === item.id) {
59+
setHistoryId(null);
60+
setMessages([]);
61+
setHistory([]);
62+
navigate(`/bot/${params.id}`);
63+
}
64+
queryClient.invalidateQueries([
65+
"getBotPlaygroundHistory",
66+
params.id,
67+
params.history_id,
68+
]);
69+
},
70+
onError: () => {
71+
notification.error({
72+
message: "Error",
73+
description: "Something went wrong",
74+
});
75+
},
76+
}
77+
);
78+
1379
return (
14-
<Link
15-
to={`/bot/${params.id}/playground/${item.id}`}
16-
onClick={() => {
17-
setIsLoading(true);
18-
setMessages([]);
19-
setHistory([]);
20-
}}
80+
<div
2181
className={`flex py-2 px-2 items-center gap-3 relative rounded-md truncate hover:pr-4 group transition-opacity duration-300 ease-in-out ${
2282
historyId === item.id
2383
? item.id === params.history_id
@@ -26,17 +86,73 @@ export const PlaygroundHistoryCard = ({
2686
: "bg-gray-100 dark:bg-[#0a0a0a] dark:text-gray-200"
2787
}`}
2888
>
29-
<ChatBubbleLeftIcon className="w-5 h-5 text-gray-400 dark:text-gray-600 group-hover:text-gray-500 dark:group-hover:text-gray-400 transition-colors" />
30-
<div className="flex-1 overflow-hidden break-all">
89+
<Link
90+
to={`/bot/${params.id}/playground/${item.id}`}
91+
className="flex-1 overflow-hidden break-all"
92+
>
3193
<span
3294
className="text-gray-500 dark:text-gray-400 text-sm font-semibold truncate"
3395
title={item.title}
3496
>
35-
{item.title.length > 20
36-
? item.title.substring(0, 20) + "..."
37-
: item.title}
97+
{item.title}
3898
</span>
39-
</div>
40-
</Link>
99+
</Link>
100+
<Dropdown
101+
disabled={(isDeleting || isEditing) && processingId === item.id}
102+
menu={{
103+
items: [
104+
{
105+
key: "1",
106+
label: (
107+
<button
108+
className="flex items-center gap-2 w-full "
109+
onClick={() => {
110+
const newTitle = prompt("Enter new title", item.title);
111+
if (newTitle) {
112+
editHistory(newTitle);
113+
setProcessingId(item.id);
114+
}
115+
}}
116+
>
117+
<PencilIcon className="w-3 h-3 text-gray-500 dark:text-gray-400" />
118+
<span>Edit</span>
119+
</button>
120+
),
121+
},
122+
{
123+
key: "2",
124+
label: (
125+
<button
126+
onClick={() => {
127+
deleteHistory();
128+
setProcessingId(item.id);
129+
}}
130+
className="flex items-center gap-2 w-full text-red-500"
131+
>
132+
<TrashIcon className="w-3 h-3 text-red-500 dark:text-red-400" />
133+
<span>Delete</span>
134+
</button>
135+
),
136+
},
137+
],
138+
}}
139+
>
140+
{(isDeleting || isEditing) && processingId === item.id ? (
141+
<Spin />
142+
) : (
143+
<EllipsisHorizontalIcon
144+
className={`text-gray-500 dark:text-gray-400 w-5 h-5
145+
${
146+
historyId === item.id
147+
? item.id === params.history_id
148+
? "opacity-100"
149+
: "opacity-100"
150+
: "opacity-0 hover:opacity-100 group-hover:opacity-100"
151+
}
152+
`}
153+
/>
154+
)}
155+
</Dropdown>
156+
</div>
41157
);
42158
};

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dialoqbase",
3-
"version": "1.7.0",
3+
"version": "1.7.1",
44
"description": "Create chatbots with ease",
55
"scripts": {
66
"ui:dev": "pnpm run --filter ui dev",

server/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,8 @@ npm run dev
2626
```
2727

2828
The server will be running on `http://localhost:3000`. API documentation is available at `http://localhost:3000/docs`.
29+
30+
31+
## Note
32+
33+
I made a huge mistake on migrations folder naming. :/ I don't know how to fix it. I'm sorry.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- AlterTable
2+
ALTER TABLE "DialoqbaseSettings" ADD COLUMN "numberOfDocuments" INTEGER NOT NULL DEFAULT 10;

server/prisma/schema.prisma

+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ model DialoqbaseSettings {
8585
noOfBotsPerUser Int @default(10)
8686
allowUserToCreateBots Boolean @default(true)
8787
allowUserToRegister Boolean @default(false)
88+
numberOfDocuments Int @default(10)
8889
}
8990

9091
model BotIntegration {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { FastifyReply, FastifyRequest } from "fastify";
2+
import { DeleteBotByPlaygroundId } from "./types";
3+
4+
export const deleteBotByPlaygroundId = async (
5+
request: FastifyRequest<DeleteBotByPlaygroundId>,
6+
reply: FastifyReply
7+
) => {
8+
const user = request.user;
9+
const { id } = request.params;
10+
11+
const prisma = request.server.prisma;
12+
13+
const isRealOwner = await prisma.botPlayground.findFirst({
14+
where: {
15+
id,
16+
Bot: {
17+
user_id: user.user_id,
18+
},
19+
},
20+
});
21+
22+
if (!isRealOwner) {
23+
return reply.status(404).send({
24+
message: "Playground not found",
25+
});
26+
}
27+
await prisma.botPlaygroundMessage.deleteMany({
28+
where: {
29+
botPlaygroundId: id,
30+
},
31+
});
32+
33+
await prisma.botPlayground.delete({
34+
where: {
35+
id,
36+
},
37+
});
38+
39+
return reply.status(200).send({
40+
message: "Playground deleted",
41+
});
42+
};
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
export * from "./post.handler"
22
export * from "./get.handler"
3-
export * from "./chat.handler"
3+
export * from "./chat.handler"
4+
export * from "./delete.handler"
5+
export * from "./put.handler"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { FastifyReply, FastifyRequest } from "fastify";
2+
import { UpdateBotPlaygroundTitleById } from "./types";
3+
4+
export const updateBotPlaygroundTitleById = async (
5+
request: FastifyRequest<UpdateBotPlaygroundTitleById>,
6+
reply: FastifyReply
7+
) => {
8+
const user = request.user;
9+
const prisma = request.server.prisma;
10+
11+
const { title } = request.body;
12+
const { id } = request.params;
13+
14+
const isRealOwner = await prisma.botPlayground.findFirst({
15+
where: {
16+
id,
17+
Bot: {
18+
user_id: user.user_id,
19+
},
20+
},
21+
});
22+
23+
if (!isRealOwner) {
24+
return reply.status(404).send({
25+
message: "Playground not found",
26+
});
27+
}
28+
29+
await prisma.botPlayground.update({
30+
where: {
31+
id,
32+
},
33+
data: {
34+
title,
35+
},
36+
});
37+
38+
return reply.status(200).send({
39+
message: "Playground updated",
40+
});
41+
};

server/src/handlers/api/v1/bot/playground/types.ts

+15
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,18 @@ export interface UpdateBotAudioSettings {
5151
enabled: boolean;
5252
};
5353
}
54+
55+
export interface DeleteBotByPlaygroundId {
56+
Params: {
57+
id: string;
58+
};
59+
}
60+
61+
export interface UpdateBotPlaygroundTitleById {
62+
Body: {
63+
title: string;
64+
};
65+
Params: {
66+
id: string;
67+
};
68+
}

server/src/routes/api/v1/bot/playground/root.ts

+22
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@ import {
55
getPlaygroundHistoryByBotId,
66
getPlaygroundHistoryByBotIdAndHistoryId,
77
updateBotAudioSettingsHandler,
8+
deleteBotByPlaygroundId,
9+
updateBotPlaygroundTitleById
810
} from "../../../../../handlers/api/v1/bot/playground";
911
import {
1012
audioSettingsSchema,
1113
chatPlaygroundHistoryIdSchema,
1214
chatPlaygroundHistorySchema,
1315
chatRequestSchema,
1416
chatRequestStreamSchema,
17+
deleteBotByPlaygroundIdSchema,
18+
updateBotPlaygroundTitleByIdSchema,
1519
} from "../../../../../schema/api/v1/bot/playground";
1620

1721
const root: FastifyPluginAsync = async (fastify, _): Promise<void> => {
@@ -60,6 +64,24 @@ const root: FastifyPluginAsync = async (fastify, _): Promise<void> => {
6064
},
6165
updateBotAudioSettingsHandler
6266
);
67+
68+
fastify.delete(
69+
"/history/:id",
70+
{
71+
onRequest: [fastify.authenticate],
72+
schema: deleteBotByPlaygroundIdSchema
73+
},
74+
deleteBotByPlaygroundId
75+
);
76+
77+
fastify.put(
78+
"/history/:id",
79+
{
80+
onRequest: [fastify.authenticate],
81+
schema: updateBotPlaygroundTitleByIdSchema
82+
},
83+
updateBotPlaygroundTitleById
84+
);
6385
};
6486

6587
export default root;

0 commit comments

Comments
 (0)