-
Notifications
You must be signed in to change notification settings - Fork 2.6k
feat: Knowledge base workflow supports terminating execution #4536
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -168,6 +168,33 @@ def get(self, request, workspace_id: str, knowledge_id: str, knowledge_action_id | |
| data={'workspace_id': workspace_id, 'knowledge_id': knowledge_id, 'id': knowledge_action_id}) | ||
| .one()) | ||
|
|
||
| class Cancel(APIView): | ||
| authentication_classes = [TokenAuth] | ||
|
|
||
| @extend_schema( | ||
| methods=['POST'], | ||
| description=_('Cancel knowledge workflow action'), | ||
| summary=_('Cancel knowledge workflow action'), | ||
| operation_id=_('Cancel knowledge workflow action'), # type: ignore | ||
| parameters=KnowledgeWorkflowActionApi.get_parameters(), | ||
| responses=DefaultResultSerializer(), | ||
| tags=[_('Knowledge Base')] # type: ignore | ||
| ) | ||
| @has_permissions( | ||
| PermissionConstants.KNOWLEDGE_WORKFLOW_EDIT.get_workspace_knowledge_permission(), | ||
| PermissionConstants.KNOWLEDGE_WORKFLOW_EDIT.get_workspace_permission_workspace_manage_role(), | ||
| RoleConstants.WORKSPACE_MANAGE.get_workspace_role(), | ||
| ViewPermission( | ||
| [RoleConstants.USER.get_workspace_role()], | ||
| [PermissionConstants.KNOWLEDGE.get_workspace_knowledge_permission()], | ||
| CompareConstants.AND | ||
| ), | ||
| ) | ||
| def post(self, request, workspace_id: str, knowledge_id: str, knowledge_action_id: str): | ||
| return result.success(KnowledgeWorkflowActionSerializer.Operate( | ||
| data={'workspace_id': workspace_id, 'knowledge_id': knowledge_id, 'id': knowledge_action_id}) | ||
| .cancel()) | ||
|
|
||
|
|
||
| class KnowledgeWorkflowView(APIView): | ||
| authentication_classes = [TokenAuth] | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The provided code seems mostly correct, but there are a few improvements and corrections that could be made: Improvements and Corrections
Here is an updated version with some of these considerations: from rest_framework.views import APIView
from rest_framework.response import Response
from .models import KnowledgeWorkflowAction
from .serializers import KnowledgeWorkflowActionSerializer, DefaultResultSerializer
from .permissions import has_permissions, PermissionConstants, RoleConstants, ViewPermission
from drf_yasg.utils import extend_schema
import traceback
# Define the CancelAPIView class here
class Cancel(APIView):
authentication_classes = [TokenAuth]
@extend_schema(
methods=['POST'],
description=_('Cancel knowledge workflow action'),
summary=_('Cancel knowledge workflow action'),
operation_id='cancel_knowledge_workflow_action', # Use kebab case instead of underscores
parameters=KnowledgeWorkflowActionApi.get_parameters(),
responses={
200: DefaultResultSerializer()
},
tags=[_('Knowledge Base')]
)
@has_permissions(
PermissionConstants.KNOWLEDGE_WORKFLOW_EDIT.get_workspace_knowledge_permission(),
PermissionConstants.KNOWLEDGE_WORKFLOW_EDIT.get_workspace_permission_workspace_manage_role(),
RoleConstants.WORKSPACE_MANAGE.get_workspace_role(),
ViewPermission(
[RoleConstants.USER.get_workspace_role()],
[PermissionConstants.KNOWLEDGE.get_workspace_knowledge_permission()],
CompareConstants.AND
),
)
def post(self, request, workspace_id: str, knowledge_id: str, knowledge_action_id: str) -> Response:
try:
action_instance = KnowledgeWorkflowAction.objects.filter(
id=knowledge_action_id
).select_related(
'workflow__stage',
'workflow'
).first()
if not action_instance:
return Result.failure(404, "Knowledge workflow action not found")
cancel_result = action_instance.cancel()
if cancel_result:
return Result.success(serializer.instance)
else:
return Result.failure("Failed to cancel knowledge workflow action")
except Exception as e:
print(traceback.format_exc()) # Log exception details
return Result.failure(str(e))
class KnowledgeWorkflowView(APIView):
authentication_classes = [TokenAuth]Key Points:
These changes will make the code cleaner, more robust, maintainable, and secure. |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -64,6 +64,14 @@ | |
| <el-icon class="color-danger"><CircleCloseFilled /></el-icon> | ||
| {{ $t('common.status.fail') }} | ||
| </el-text> | ||
| <el-text class="color-text-primary" v-else-if="row.state === 'REVOKED'"> | ||
| <el-icon class="color-danger"><CircleCloseFilled /></el-icon> | ||
| {{ $t('common.status.REVOKED', '已取消') }} | ||
| </el-text> | ||
| <el-text class="color-text-primary" v-else-if="row.state === 'REVOKE'"> | ||
| <el-icon class="is-loading color-primary"><Loading /></el-icon> | ||
| {{ $t('views.document.fileStatus.REVOKE', '取消中') }} | ||
| </el-text> | ||
| <el-text class="color-text-primary" v-else> | ||
| <el-icon class="is-loading color-primary"><Loading /></el-icon> | ||
| {{ $t('common.status.padding') }} | ||
|
|
@@ -87,11 +95,22 @@ | |
|
|
||
| <el-table-column :label="$t('common.operation')" width="80"> | ||
| <template #default="{ row }"> | ||
| <el-tooltip effect="dark" :content="$t('chat.executionDetails.title')" placement="top"> | ||
| <el-button type="primary" text @click.stop="toDetails(row)"> | ||
| <AppIcon iconName="app-operate-log"></AppIcon> | ||
| </el-button> | ||
| </el-tooltip> | ||
| <div class="flex"> | ||
| <el-tooltip effect="dark" :content="$t('chat.executionDetails.title')" placement="top"> | ||
| <el-button type="primary" text @click.stop="toDetails(row)"> | ||
| <AppIcon iconName="app-operate-log"></AppIcon> | ||
| </el-button> | ||
| </el-tooltip> | ||
| <el-tooltip | ||
| effect="dark" | ||
| :content="$t('chat.executionDetails.cancel', '取消')" | ||
| placement="top" | ||
| > | ||
| <el-button type="primary" text @click.stop="cancel(row)"> | ||
| <el-icon><CircleCloseFilled /></el-icon> | ||
| </el-button> | ||
| </el-tooltip> | ||
| </div> | ||
| </template> | ||
| </el-table-column> | ||
| </app-table-infinite-scroll> | ||
|
|
@@ -157,6 +176,11 @@ const toDetails = (row: any) => { | |
| ExecutionDetailDrawerRef.value?.open() | ||
| } | ||
|
|
||
| const cancel = (row: any) => { | ||
| loadSharedApi({ type: 'knowledge', systemType: apiType.value }) | ||
| .cancelWorkflowAction(active_knowledge_id.value, row.id, loading) | ||
| .then((ok: any) => {}) | ||
| } | ||
| const changeFilterHandle = () => { | ||
| query.value = { user_name: '', status: '' } | ||
| } | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The provided code snippet has several areas that could be improved: Code Improvements
Here's an optimized version of the code with these improvements: <template>
<!-- Existing template content -->
<el-table-column :label="$t('common.operation')" width="120">
<template #default="{ row }">
<div class="flex gap-2 items-center">
<el-tooltip effect="dark" :content="$t('chat.executionDetails.title')" placement="top">
<el-button type="primary" text @click.stop="toDetails(row)">
<AppIcon iconName="app-operate-log"></AppIcon>
</el-button>
</el-tooltip>
<el-tooltip effect="dark" :content="$t('chat.executionDetails.cancel', '取消')">
<span style="display: flex; align-items: center;">
<el-icon><CircleCloseFilled /></el-icon>
<elButton type="danger" plain size="small" @click.stop="cancel(row)" />
</span>
</el-tooltip>
</div>
</template>
</el-table-column>
</template>
<script lang="ts">
import { ref, defineComponent, PropTybe } from 'vue'
// Import necessary components & libraries here
export default defineCompone({
// Component setup and data
methods: {
async cancel ({ id }: Record<string, unknown>) {
await loadSharedApi({ type: 'knowledge', systemType: this.apiType }).cancelWorkflowAction(
active_knowledge_id.value,
id,
this.loading
)
}
// Other functions remain unchanged
},
})
</script>
<style scoped>
/* Add any custom styling if needed */
.flex {
display: flex;
align-items: center;
}
.items-center {
justify-content: center;
}
</style>Summary of Changes
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code seems mostly correct, but there are a few areas that could be improved:
Redundant Imports:
These imports might not be needed at the beginning of
KnowledgeWorkflowActionListQuerySerializer.Useless Comment:
# 'details': knowledge_action.details,The comment doesn't add any value; you can remove it.
Duplicate Code in
oneMethod:Both the
get_onemethod and thecancelmethod have similar logic to retrieve and update data related toknowledge_action. You should consider refactoring this into a single function.Here's an example of how you could refactor the
cancelmethod to use a helper function:This way, you reduce redundancy and make the code cleaner.