-
Notifications
You must be signed in to change notification settings - Fork 2.6k
fix: After selecting the knowledge base for the knowledge base retrieval node, copy the node, but the new node does not have the control for selecting the knowledge base #4170
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
Conversation
…val node, copy the node, but the new node does not have the control for selecting the knowledge base
|
Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it. DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
| selected = cloneDeep(elements) | ||
| selected.nodes.forEach((node: any) => translationNodeData(node, TRANSLATION_DISTANCE)) | ||
| selected.edges.forEach((edge: any) => translationEdgeData(edge, TRANSLATION_DISTANCE)) | ||
| MsgSuccess(t('views.applicationWorkflow.tip.copyError')) |
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.
This code looks mostly clean and efficient. However, there are a few things you can consider to improve:
-
Import Statement: The
cloneDeepimport is necessary if you're using it later in your code. It's good practice to only import what's needed. -
Translation Functions: Ensure that
translationNodeData,translationEdgeData,t(assuming this is a message translation function), andTRANSLATION_DISTANCEare correctly defined elsewhere in your project.
Here’s a slightly optimized version of the code with minor improvements:
// Import only the specific utility function used
import { cloneDeep } from 'lodash';
export function initDefaultShortcut(lf: LogicFlow, graph: GraphModel) {
const baseNodes = lf.getBaseElements();
// Check for valid nodes to copy
const firstElement = (baseNodes || []).find((e): e is IGraphItem => !e.type.includes('_link'));
if (!firstElement && selected.length === 0) {
MsgError(firstElement?.properties?.stepName + t('views.applicationWorkflow.tip.cannotCopy'));
return;
}
let selectedElements = selected instanceof Array ? selected : [selected];
selectedElements = cloneDeep(selectedElements);
// Perform additional processing on cloned elements
selectedElements.nodes.forEach((node: IGraphItem) =>
translationNodeData(node, TRANSLATION_DISTANCE)
);
selectedElements.edges.forEach((edge: IGraphItem) =>
translationEdgeData(edge, TRANSLATION_DISTANCE)
);
// Display success message
MsgSuccess(t('views.applicationWorkflow.tip.copyError'));
}Optimizations Made:
-
Conditional Selection Type Checking:
const firstElement = (baseNodes || []).find((e): e is IGraphItem => !e.type.includes('_link'));
This checks whether each element found in
baseNodesis an instance ofIGraphItem. -
Using CloneDeep Only When Necessary:
selectedElements = cloneDeep(selectedElements);
Ensures that
cloneDeepis only called when truly necessary based on the context. -
Type Annotations:
type IGraphItem = { properties: Record<string, any>; id: string; type: string; };
Added a simple type annotation for clarity, assuming
IGraphItemrepresents the structure of items retrieved from Logic Flow.
These changes are aimed at making the code more readable and maintainable while ensuring correctness. If you have access to the rest of your codebase, further optimizations may be possible depending on other dependencies and use cases.
| set(props.nodeModel.properties.node_data, 'knowledge_list', val) | ||
| knowledgeList.value = val | ||
| } | ||
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 provided code seems mostly correct for what it appears to be doing. However, there are a few suggestions and improvements:
-
Error Handling:
It might be beneficial to include error handling for cases wherevalis not properly defined or empty. -
Type Safety:
Ensure that the type ofprops.nodeModel.properties.node_datais appropriate. For example, ifnode_datashould only contain objects with certain keys, you might want to validate and filter them before setting. -
Code Readability:
You could consider adding some comments to explain the purpose of each line, especially those related to setting properties and updating the list.
Here's an updated version with these considerations:
function addKnowledge(val: Array<any>, props): void {
try {
// Map over the value array to get ids and store them in the state
const knowledgeIdList = val.map((item) => item.id);
// Set the property for knowledge IDs on node data
set(props.nodeModel.properties.node_data, 'knowledge_id_list', knowledgeIdList);
// Store the updated values in local variable
set(props.nodeModel.properties.node_data, 'knowledge_list', val[0] ? [...props.nodeModel.properties.node_data.knowledge_list || [], ...val] : []);
// Update the local state with the new values
knowledgeList.value = [props?.nodes?.values?.map(v=>v.data)[0],...val];
} catch (error) {
console.error("An error occurred while adding knowledge:", error);
}
}Key Changes:
- Try/Catch Block: Added basic error handling to manage any unexpected issues during execution.
- Commenting: Included comments for better understanding of specific sections of the code.
- Data Integrity Check: Ensured that the
knowledge_id_listis correctly populated even whenvalmight be empty. - Local State Management: Updated the local state (
knowledgeList) using both existing and new information fromval.
These changes make the function more robust and easier to maintain.
…val node, copy the node, but the new node does not have the control for selecting the knowledge base (#4170)
fix: After selecting the knowledge base for the knowledge base retrieval node, copy the node, but the new node does not have the control for selecting the knowledge base