Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions apps/knowledge/serializers/knowledge.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,18 +416,6 @@ def edit(self, instance: Dict, select_one=True):
application_id=application_id, knowledge_id=self.data.get('knowledge_id')
) for application_id in application_id_list
]) if len(application_id_list) > 0 else None
if instance.get("work_flow"):
QuerySet(KnowledgeWorkflow).update_or_create(knowledge_id=self.data.get("knowledge_id"),
create_defaults={'id': uuid.uuid7(),
'knowledge_id': self.data.get(
"knowledge_id"),
"workspace_id": self.data.get(
'workspace_id'),
'work_flow': instance.get('work_flow',
{}), },
defaults={
'work_flow': instance.get('work_flow')
})
knowledge.save()
if select_one:
return self.one()
Expand Down
15 changes: 14 additions & 1 deletion apps/knowledge/serializers/knowledge_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,20 @@ def publish(self, with_valid=True):
return True

def edit(self, instance: Dict):
pass
self.is_valid(raise_exception=True)
if instance.get("work_flow"):
QuerySet(KnowledgeWorkflow).update_or_create(knowledge_id=self.data.get("knowledge_id"),
create_defaults={'id': uuid.uuid7(),
'knowledge_id': self.data.get(
"knowledge_id"),
"workspace_id": self.data.get(
'workspace_id'),
'work_flow': instance.get('work_flow',
{}), },
defaults={
'work_flow': instance.get('work_flow')
})
return self.one()

def one(self):
self.is_valid(raise_exception=True)
Expand Down
2 changes: 1 addition & 1 deletion apps/knowledge/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
path('workspace/<str:workspace_id>/knowledge/tags', views.KnowledgeView.Tags.as_view()),
path('workspace/<str:workspace_id>/knowledge/<str:knowledge_id>', views.KnowledgeView.Operate.as_view()),
path('workspace/<str:workspace_id>/knowledge/<str:knowledge_id>/sync', views.KnowledgeView.SyncWeb.as_view()),
path('workspace/<str:workspace_id>/knowledge/<str:knowledge_id>/workfolw', views.KnowledgeWorkflowView.Operate.as_view()),
path('workspace/<str:workspace_id>/knowledge/<str:knowledge_id>/workflow', views.KnowledgeWorkflowView.Operate.as_view()),
path('workspace/<str:workspace_id>/knowledge/<str:knowledge_id>/generate_related', views.KnowledgeView.GenerateRelated.as_view()),
path('workspace/<str:workspace_id>/knowledge/<str:knowledge_id>/embedding', views.KnowledgeView.Embedding.as_view()),
path('workspace/<str:workspace_id>/knowledge/<str:knowledge_id>/hit_test', views.KnowledgeView.HitTest.as_view()),
Expand Down
4 changes: 2 additions & 2 deletions apps/knowledge/views/knowledge_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ class Operate(APIView):
tags=[_('Knowledge Base')] # type: ignore
)
@has_permissions(
PermissionConstants.KNOWLEDGE_EDIT.get_workspace_knowledge_permission(),
PermissionConstants.KNOWLEDGE_EDIT.get_workspace_permission_workspace_manage_role(),
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()],
Expand Down
16 changes: 16 additions & 0 deletions ui/src/api/knowledge/knowledge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,21 @@ const publish: (knowledge_id: string, loading?: Ref<boolean>) => Promise<Result<
return put(`${prefix.value}/${knowledge_id}/publish`, {}, {}, loading)
}

/**
* 保存知识库工作流
* @param knowledge_id
* @param data
* @param loading
* @returns
*/
const putKnowledgeWorkflow: (
knowledge_id: string,
data: any,
loading?: Ref<boolean>,
) => Promise<Result<any>> = (knowledge_id, data, loading) => {
return put(`${prefix.value}/${knowledge_id}/workflow`, data, undefined, loading)
}

const listKnowledgeVersion: (
knowledge_id: string,
loading?: Ref<boolean>,
Expand Down Expand Up @@ -451,5 +466,6 @@ export default {
listKnowledgeVersion,
updateKnowledgeVersion,
publish,
putKnowledgeWorkflow,
workflowUpload,
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code appears to be well-formed and correctly implements REST API functions related to managing knowledge and its workflows. However, there are a few minor improvements and considerations:

  1. The putKnowledgeWorkflow function should specify the HTTP method as "PUT" instead of "POST". This is because it's updating an existing resource with new data.
  2. Consider adding type annotations for the parameters and return values in all functions defined within this file, especially those that use promises like Result. Using type definitions can help catch errors during development and make the code easier to read and maintain.

Overall, the function names follow conventional naming conventions and seem self-explanatory based on their purpose. There doesn't appear to be any significant issues or optimizations needed at this time.

16 changes: 16 additions & 0 deletions ui/src/api/system-resource-management/knowledge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,21 @@ const getWorkflowAction: (
return get(`${prefix}/${knowledge_id}/action/${knowledge_action_id}`, {}, loading)
}

/**
* 保存知识库工作流
* @param knowledge_id
* @param data
* @param loading
* @returns
*/
const putKnowledgeWorkflow: (
knowledge_id: string,
data: any,
loading?: Ref<boolean>,
) => Promise<Result<any>> = (knowledge_id, data, loading) => {
return put(`${prefix}/${knowledge_id}/workflow`, data, undefined, loading)
}

const publish: (knowledge_id: string, loading?: Ref<boolean>) => Promise<Result<any>> = (
knowledge_id: string,
loading,
Expand Down Expand Up @@ -342,6 +357,7 @@ export default {
workflowAction,
getWorkflowAction,
publish,
putKnowledgeWorkflow,
listKnowledgeVersion
} as {
[key: string]: any
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code provided is a TypeScript module with three functions related to working with knowledge workflows in an enterprise system. There are no significant irregularities or problems with the code itself, but here are some suggestions for improvements:

  1. Type Declarations: The getWorkflowAction and publish functions accept optional parameters for loading, which is good practice to avoid errors when passing arguments incorrectly.

  2. Function Name Descriptions:

    • For simplicity, you could rename the putKnowledgeWorkflow function call from post to updateWorkflows. This change might make the intention clearer.
    • Similarly, renaming getResultDataFromResponse to something like retrieveUpdateResult would improve readability.
  3. Code Clarity:

    • In the listKnowledgeVersion documentation, describe what it does. It's not clear if this function fetches all versions of knowledge items or just active ones.
    • Consider adding comments to explain why certain values, such as undefined passed to the post method in putKnowledgeWorkflow, are being used.

Here’s an improved version of the code with these suggestions applied:

// ... other imports ...

// Save Knowledge Workflow
const putKnowledgeWorkflow: (
  knowledgeId: string,
  data: any,
  loading?: Ref<boolean>,
) => Promise<Result<any>> = async (knowledgeId, data, loading?) => {
  try {
    return await post(`${prefix}/${knowledgeId}/workflow`, data, { loading })
  } catch (error) {
    throw new Error('Error saving knowledge flow.')
  }
}

export default {
  // ... other functions ...

  /**
   * Update Workflows
   * @param knowledgeId The unique identifier for the knowledge item.
   * @param data The updated workflow information.
   * @param loading A ref object indicating whether the component is currently in loading state.
   * @returns A Promise that resolves to the response data resulting from the update request.
   */
  updateWorkflows: putKnowledgeWorkflow,

  // ... other functions ...
} as {
  [key: string]: ((...args: any) => Promise<any>) | Record<string, any>
} satisfies Module<ReturnType<typeof import('@/modules/base').default>>;

Key Changes:

  • Renamed putKnowledgeWorkflow to updateWorkflows.
  • Simplified the function name descriptions where possible.
  • Added comments to clarify the usage of data and { loading } in putKnowledgeWorkflow.

These changes should help enhance clarity and maintainability of the codebase.

Expand Down
16 changes: 16 additions & 0 deletions ui/src/api/system-shared/knowledge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,21 @@ const getWorkflowAction: (
return get(`${prefix}/${knowledge_id}/action/${knowledge_action_id}`, {}, loading)
}

/**
* 保存知识库工作流
* @param knowledge_id
* @param data
* @param loading
* @returns
*/
const putKnowledgeWorkflow: (
knowledge_id: string,
data: any,
loading?: Ref<boolean>,
) => Promise<Result<any>> = (knowledge_id, data, loading) => {
return put(`${prefix}/${knowledge_id}/workflow`, data, undefined, loading)
}

const publish: (knowledge_id: string, loading?: Ref<boolean>) => Promise<Result<any>> = (
knowledge_id: string,
loading,
Expand Down Expand Up @@ -404,6 +419,7 @@ export default {
getKnowledgeWorkflowDatasourceDetails,
workflowAction,
publish,
putKnowledgeWorkflow,
listKnowledgeVersion,
} as {
[key: string]: any
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code provided looks mostly correct, but there are a few areas for improvement:

  1. Type Annotations: There are no type annotations on the input parameters and return types of putKnowledgeWorkflow. TypeScript would benefit by adding them to improve code clarity and static typing.

  2. Loading Hook: The loading parameter is defined twice in the signature (loadin? and loading) which should be consistent either with hooks or without.

Here's an improved version of the function with added type annotations and ensuring consistency in the loading paramter definition:

/**
 * 保存知识库工作流
 * @param knowledge_id 
 * @param data 
 * @param loading - Loading state reference
 * @returns 
 */
const putKnowledgeWorkflow: (
  knowledge_id: string,
  data: any,
  loading: Ref<boolean>,
) => Promise<Result<any>> = (knowledge_id, data, loading) => {
  return put(`${prefix}/${knowledge_id}/workflow`, data, undefined, loading);
};

export default {
  getKnowledgeWorkflowDatasourceDetails,
  workflowAction,
  publish,

  // Improved signature for putKnowledgeWorkflow
  putKnowledgeWorkflow,
  listKnowledgeVersion,
} as {
  [key: string]: (...args: any[]) => void | Promise<void>;
};

Key Changes:

  • Added type annotation Ref<boolean> for the loading parameter.
  • Removed the redundant loading? declaration.
  • Ensured consistency by aligning the parameter definitions.

Expand Down
2 changes: 1 addition & 1 deletion ui/src/views/knowledge-workflow/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ function saveknowledge(bool?: boolean, back?: boolean) {
}
loading.value = back || false
loadSharedApi({ type: 'knowledge', systemType: apiType.value })
.putKnowledge(id, obj)
.putKnowledgeWorkflow(id, obj)
.then(() => {
saveTime.value = new Date()
if (bool) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,8 @@ import { MsgSuccess, MsgConfirm } from '@/utils/message'
import { SourceTypeEnum } from '@/enums/common'
import { t } from '@/locales'
import useStore from '@/stores'
import { hasPermission } from '@/utils/permission'
import { PermissionConst, RoleConst } from '@/utils/permission/data'
const router = useRouter()
const { user } = useStore()

Expand All @@ -306,7 +308,8 @@ const ManagePermission = () => {
permissionPrecise.value.problem_read() ||
permissionPrecise.value.edit() ||
permissionPrecise.value.knowledge_chat_user_read() ||
permissionPrecise.value.hit_test()
permissionPrecise.value.hit_test() ||
hasPermission([RoleConst.ADMIN, PermissionConst.RESOURCE_KNOWLEDGE_WORKFLOW_READ],'OR')
)
}

Expand Down
Loading