mirror of
https://github.com/1Panel-dev/MaxKB.git
synced 2025-12-26 01:33:05 +00:00
feat: Application conversation
This commit is contained in:
parent
e61e4eb937
commit
1ef0146389
|
|
@ -11,9 +11,9 @@ from drf_spectacular.utils import OpenApiParameter
|
|||
|
||||
from application.serializers.application_chat_record import ChatRecordSerializerModel
|
||||
from chat.serializers.chat import ChatMessageSerializers
|
||||
from chat.serializers.chat_record import HistoryChatModel
|
||||
from chat.serializers.chat_record import HistoryChatModel, EditAbstractSerializer
|
||||
from common.mixins.api_mixin import APIMixin
|
||||
from common.result import ResultSerializer, ResultPageSerializer
|
||||
from common.result import ResultSerializer, ResultPageSerializer, DefaultResultSerializer
|
||||
|
||||
|
||||
class ChatAPI(APIMixin):
|
||||
|
|
@ -72,6 +72,26 @@ class PageHistoricalConversationAPI(APIMixin):
|
|||
return PageApplicationCreateResponse
|
||||
|
||||
|
||||
class HistoricalConversationOperateAPI(APIMixin):
|
||||
@staticmethod
|
||||
def get_parameters():
|
||||
return [OpenApiParameter(
|
||||
name="chat_id",
|
||||
description="对话id",
|
||||
type=OpenApiTypes.STR,
|
||||
location='path',
|
||||
required=True
|
||||
)]
|
||||
|
||||
@staticmethod
|
||||
def get_request():
|
||||
return EditAbstractSerializer
|
||||
|
||||
@staticmethod
|
||||
def get_response():
|
||||
return DefaultResultSerializer
|
||||
|
||||
|
||||
class HistoricalConversationRecordAPI(APIMixin):
|
||||
@staticmethod
|
||||
def get_parameters():
|
||||
|
|
|
|||
|
|
@ -96,6 +96,39 @@ class HistoricalConversationSerializer(serializers.Serializer):
|
|||
return page_search(current_page, page_size, self.get_queryset(), lambda r: HistoryChatModel(r).data)
|
||||
|
||||
|
||||
class EditAbstractSerializer(serializers.Serializer):
|
||||
abstract = serializers.CharField(required=True, label=_('Abstract'))
|
||||
|
||||
|
||||
class HistoricalConversationOperateSerializer(serializers.Serializer):
|
||||
application_id = serializers.UUIDField(required=True, label=_('Application ID'))
|
||||
chat_user_id = serializers.UUIDField(required=True, label=_('Chat User ID'))
|
||||
chat_id = serializers.UUIDField(required=True, label=_('Chat ID'))
|
||||
|
||||
def is_valid(self, *, raise_exception=False):
|
||||
super().is_valid(raise_exception=True)
|
||||
e = QuerySet(Chat).filter(id=self.data.get('chat_id'), application_id=self.data.get('application_id'),
|
||||
chat_user_id=self.data.get('chat_user_id')).exists()
|
||||
if not e:
|
||||
raise AppApiException(500, _('Chat is not exist'))
|
||||
|
||||
def edit_abstract(self, instance, with_valid=True):
|
||||
if with_valid:
|
||||
self.is_valid(raise_exception=True)
|
||||
EditAbstractSerializer(data=instance).is_valid(raise_exception=True)
|
||||
|
||||
QuerySet(Chat).filter(id=self.data.get('chat_id'), application_id=self.data.get('application_id'),
|
||||
chat_user_id=self.data.get('chat_user_id')).update(abstract=instance.get('abstract'))
|
||||
return True
|
||||
|
||||
def logic_delete(self, with_valid=True):
|
||||
if with_valid:
|
||||
self.is_valid(raise_exception=True)
|
||||
QuerySet(Chat).filter(id=self.data.get('chat_id'), application_id=self.data.get('application_id'),
|
||||
chat_user_id=self.data.get('chat_user_id')).update(is_deleted=True)
|
||||
return True
|
||||
|
||||
|
||||
class HistoricalConversationRecordSerializer(serializers.Serializer):
|
||||
application_id = serializers.UUIDField(required=True, label=_('Application ID'))
|
||||
chat_id = serializers.UUIDField(required=True, label=_('Chat ID'))
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ urlpatterns = [
|
|||
path('historical_conversation', views.HistoricalConversationView.as_view(), name='historical_conversation'),
|
||||
path('historical_conversation/<str:chat_id>/record/<str:chat_record_id>',views.ChatRecordView.as_view(),name='conversation_details'),
|
||||
path('historical_conversation/<int:current_page>/<int:page_size>', views.HistoricalConversationView.PageView.as_view(), name='historical_conversation'),
|
||||
path('historical_conversation/<str:chat_id>',views.HistoricalConversationView.Operate.as_view(), name='historical_conversation_operate'),
|
||||
path('historical_conversation_record/<str:chat_id>', views.HistoricalConversationRecordView.as_view(), name='historical_conversation_record'),
|
||||
path('historical_conversation_record/<str:chat_id>/<int:current_page>/<int:page_size>', views.HistoricalConversationRecordView.PageView.as_view(), name='historical_conversation_record')
|
||||
]
|
||||
|
|
|
|||
|
|
@ -13,10 +13,10 @@ from rest_framework.views import APIView
|
|||
|
||||
from application.serializers.application_chat_record import ChatRecordOperateSerializer
|
||||
from chat.api.chat_api import HistoricalConversationAPI, PageHistoricalConversationAPI, \
|
||||
PageHistoricalConversationRecordAPI, HistoricalConversationRecordAPI
|
||||
PageHistoricalConversationRecordAPI, HistoricalConversationRecordAPI, HistoricalConversationOperateAPI
|
||||
from chat.api.vote_api import VoteAPI
|
||||
from chat.serializers.chat_record import VoteSerializer, HistoricalConversationSerializer, \
|
||||
HistoricalConversationRecordSerializer
|
||||
HistoricalConversationRecordSerializer, HistoricalConversationOperateSerializer
|
||||
from common import result
|
||||
from common.auth import TokenAuth
|
||||
|
||||
|
|
@ -60,6 +60,45 @@ class HistoricalConversationView(APIView):
|
|||
'chat_user_id': request.auth.chat_user_id,
|
||||
}).list())
|
||||
|
||||
class Operate(APIView):
|
||||
authentication_classes = [TokenAuth]
|
||||
|
||||
@extend_schema(
|
||||
methods=['PUT'],
|
||||
description=_("Modify conversation about"),
|
||||
summary=_("Modify conversation about"),
|
||||
operation_id=_("Modify conversation about"), # type: ignore
|
||||
parameters=HistoricalConversationOperateAPI.get_parameters(),
|
||||
request=HistoricalConversationOperateAPI.get_request(),
|
||||
responses=HistoricalConversationOperateAPI.get_response(),
|
||||
tags=[_('Chat')] # type: ignore
|
||||
)
|
||||
def put(self, request: Request, chat_id: str):
|
||||
return result.success(HistoricalConversationOperateSerializer(
|
||||
data={
|
||||
'application_id': request.auth.application_id,
|
||||
'chat_user_id': request.auth.chat_user_id,
|
||||
'chat_id': chat_id,
|
||||
}).edit_abstract(request.data)
|
||||
)
|
||||
|
||||
@extend_schema(
|
||||
methods=['DELETE'],
|
||||
description=_("Delete history conversation"),
|
||||
summary=_("Delete history conversation"),
|
||||
operation_id=_("Delete history conversation"), # type: ignore
|
||||
parameters=HistoricalConversationOperateAPI.get_parameters(),
|
||||
responses=HistoricalConversationOperateAPI.get_response(),
|
||||
tags=[_('Chat')] # type: ignore
|
||||
)
|
||||
def delete(self, request: Request, chat_id: str):
|
||||
return result.success(HistoricalConversationOperateSerializer(
|
||||
data={
|
||||
'application_id': request.auth.application_id,
|
||||
'chat_user_id': request.auth.chat_user_id,
|
||||
'chat_id': chat_id,
|
||||
}).logic_delete())
|
||||
|
||||
class PageView(APIView):
|
||||
authentication_classes = [TokenAuth]
|
||||
|
||||
|
|
|
|||
|
|
@ -104,7 +104,7 @@ class ToolView(APIView):
|
|||
tags=[_('Tool')] # type: ignore
|
||||
)
|
||||
@has_permissions(
|
||||
PermissionConstants.TOOL_EDIT.get_workspace_permission(),
|
||||
PermissionConstants.TOOL_EDIT.get_workspace_permission(),PermissionConstants.TOOL_EDIT.get_workspace_permission_workspace_manage_role(),
|
||||
RoleConstants.WORKSPACE_MANAGE.get_workspace_role(), ViewPermission([RoleConstants.USER.get_workspace_role()],
|
||||
[PermissionConstants.TOOL.get_workspace_tool_permission()],
|
||||
CompareConstants.AND),
|
||||
|
|
|
|||
|
|
@ -264,6 +264,31 @@ const speechToText: (data: any, loading?: Ref<boolean>) => Promise<Result<any>>
|
|||
) => {
|
||||
return post(`speech_to_text`, data, undefined, loading)
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @param chat_id 对话ID
|
||||
* @param loading
|
||||
* @returns
|
||||
*/
|
||||
const deleteChat: (chat_id:string, loading?: Ref<boolean>) => Promise<Result<any>> = (
|
||||
chat_id,
|
||||
loading,
|
||||
) => {
|
||||
return del(`historical_conversation/${chat_id}`, loading)
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @param chat_id 对话id
|
||||
* @param data 对话简介
|
||||
* @param loading
|
||||
* @returns
|
||||
*/
|
||||
const modifyChat: (chat_id:string, data:any, loading?: Ref<boolean> ) => Promise<Result<any>> = (
|
||||
chat_id,data,loading
|
||||
) => {
|
||||
return put(`historical_conversation/${chat_id}`, data, undefined, loading)
|
||||
}
|
||||
|
||||
export default {
|
||||
open,
|
||||
chat,
|
||||
|
|
@ -290,4 +315,6 @@ export default {
|
|||
getChatRecord,
|
||||
textToSpeech,
|
||||
speechToText,
|
||||
deleteChat,
|
||||
modifyChat
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,36 +1,16 @@
|
|||
<template>
|
||||
<el-dialog
|
||||
class="responsive-dialog"
|
||||
:title="$t('chat.editTitle')"
|
||||
v-model="dialogVisible"
|
||||
:close-on-click-modal="false"
|
||||
:close-on-press-escape="false"
|
||||
:destroy-on-close="true"
|
||||
append-to-body
|
||||
>
|
||||
<el-form
|
||||
label-position="top"
|
||||
ref="fieldFormRef"
|
||||
:model="form"
|
||||
require-asterisk-position="right"
|
||||
>
|
||||
<el-form-item
|
||||
prop="abstract"
|
||||
:rules="[
|
||||
{
|
||||
required: true,
|
||||
message: $t('common.inputPlaceholder'),
|
||||
trigger: 'blur'
|
||||
}
|
||||
]"
|
||||
>
|
||||
<el-input
|
||||
v-model="form.abstract"
|
||||
maxlength="1024"
|
||||
show-word-limit
|
||||
type="textarea"
|
||||
@blur="form.abstract = form.abstract.trim()"
|
||||
/>
|
||||
<el-dialog class="responsive-dialog" :title="$t('chat.editTitle')" v-model="dialogVisible"
|
||||
:close-on-click-modal="false" :close-on-press-escape="false" :destroy-on-close="true" append-to-body>
|
||||
<el-form label-position="top" ref="fieldFormRef" :model="form" require-asterisk-position="right">
|
||||
<el-form-item prop="abstract" :rules="[
|
||||
{
|
||||
required: true,
|
||||
message: $t('common.inputPlaceholder'),
|
||||
trigger: 'blur'
|
||||
}
|
||||
]">
|
||||
<el-input v-model="form.abstract" maxlength="1024" show-word-limit type="textarea"
|
||||
@blur="form.abstract = form.abstract.trim()" />
|
||||
</el-form-item>
|
||||
</el-form>
|
||||
<template #footer>
|
||||
|
|
@ -44,12 +24,9 @@
|
|||
</el-dialog>
|
||||
</template>
|
||||
<script setup lang="ts">
|
||||
import { reactive, ref, watch } from 'vue'
|
||||
import { ref } from 'vue'
|
||||
import type { FormInstance } from 'element-plus'
|
||||
import useStore from '@/stores'
|
||||
import { t } from '@/locales'
|
||||
|
||||
const { chatLog } = useStore()
|
||||
import chatAPI from '@/api/chat/chat'
|
||||
const emit = defineEmits(['refresh'])
|
||||
|
||||
const fieldFormRef = ref()
|
||||
|
|
@ -71,10 +48,11 @@ const open = (row: any, id: string) => {
|
|||
}
|
||||
|
||||
const submit = async (formEl: FormInstance | undefined) => {
|
||||
|
||||
if (!formEl) return
|
||||
await formEl.validate((valid) => {
|
||||
if (valid) {
|
||||
chatLog.asyncPutChatClientLog(applicationId.value, chatId.value, form.value, loading).then(() => {
|
||||
chatAPI.modifyChat(chatId.value, form.value, loading).then(() => {
|
||||
emit('refresh', chatId.value, form.value.abstract)
|
||||
dialogVisible.value = false
|
||||
})
|
||||
|
|
|
|||
|
|
@ -331,7 +331,7 @@ import { cloneDeep } from 'lodash'
|
|||
|
||||
useResize()
|
||||
|
||||
const { user, chatLog, common, chatUser } = useStore()
|
||||
const { common, chatUser } = useStore()
|
||||
const router = useRouter()
|
||||
|
||||
const EditTitleDialogRef = ref()
|
||||
|
|
@ -425,7 +425,7 @@ function refreshFieldTitle(chatId: string, abstract: string) {
|
|||
}
|
||||
}
|
||||
function deleteLog(row: any) {
|
||||
chatLog.asyncDelChatClientLog(applicationDetail.value.id, row.id, left_loading).then(() => {
|
||||
chatAPI.deleteChat(row.id, left_loading).then(() => {
|
||||
if (currentChatId.value === row.id) {
|
||||
currentChatId.value = 'new'
|
||||
currentChatName.value = t('chat.createChat')
|
||||
|
|
@ -435,6 +435,7 @@ function deleteLog(row: any) {
|
|||
}
|
||||
getChatLog(applicationDetail.value.id)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
function handleScroll(event: any) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue