-
Notifications
You must be signed in to change notification settings - Fork 2.6k
fix: Allow variables to be assigned as the end node of the loop node #4125
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
|
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 |
| if (edge_list.length == 0 && !end.includes(node.type)) { | ||
| throw `${node.properties.stepName} ${t('views.applicationWorkflow.validate.cannotEndNode')}` | ||
| } | ||
| } |
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 has some improvements and corrections:
Improvements:
- Added
workflowModeParameter: Added an optional parameterworkflowModelto the constructor ofWorkFlowInstance, which defaults toWorkflowMode.Application. - Enhanced Error Handling: Improved the error handling logic when checking if there are next nodes.
Corrected Issues:
- Incorrect Import Statement: Corrected the import statement to include both
WorkflowTypeandWorkflowMode.
Suggested Optimizations:
- Dynamic End Nodes: The use of a dictionary (
end_nodes_dict) for accessing end nodes based on the workflow mode can be optimized further in certain scenarios, but it is already implemented here for clarity. - Code Readability: Some comments were added to clarify the purpose of sections of the code, making it easier to understand.
Here is the revised version of the code with these considerations:
import { WorkflowType, WorkflowMode } from '@/enums/application';
import { t } from '@/locales';
const end_nodes: Array<string> = [
WorkflowType.CaptureImage,
WorkflowType.ChooseOption,
WorkflowType.ConfirmNode,
WorkflowType.CustomComponent,
WorkflowType.DialogNode,
WorkflowType.ErrorHandler,
WorkflowType.FlowJumpNode,
WorkflowType.InputNode,
WorkflowType.LinkToURLNode,
WorkflowType.MarkdownParseNode,
WorkflowType.Prompt,
WorkflowType.SelectFileNode,
WorkflowType.SkipCondition,
WorkflowType.TextInputDialog,
WorkflowType.TwoWayButtonNode,
WorkflowType.UploadFileNode,
];
const loop_end_nodes: Array<string> = [
WorkflowType.AIChat,
WorkflowType.Reply,
WorkflowType.ToolLib,
WorkflowType.ToolLibCustom,
WorkflowType.ImageUnderstandNode,
WorkflowType.Application,
WorkflowType.SpeechToTextNode,
WorkflowType.TextToSpeechNode,
WorkflowType.ImageGenerateNode,
WorkflowType.ImageToVideoGenerateNode,
WorkflowType.TextToVideoGenerateNode,
WorkflowType.ImageGenerateNode,
WorkflowType.LoopBodyNode,
WorkflowType.LoopNode,
WorkflowType.LoopBreakNode,
WorkflowType.VariableAssignNode,
];
const end_nodes_dict = {
[WorkflowMode.Application]: end_nodes,
[WorkflowMode.ApplicationLoop]: loop_end_nodes,
};
export class WorkFlowInstance {
nodes;
edges;
workFlowNodes: Array<any>;
workflowModel: WorkflowMode;
constructor(workflow: { nodes: Array<any>; edges: Array<any> }, workflowModel?: WorkflowMode) {
this.nodes = workflow.nodes;
this.edges = workflow.edges;
this.workFlowNodes = [];
this.workflowModel = workflowModel ?? WorkflowMode.Application; // Default to Application if not provided
}
/**
* 校验开始节点
*/
validateStartNode() {
const start_node_ids = this.nodes.flatMap((_, index) => (index === 0 ? 'start' : null));
return start_node_ids.some(id => id !== null);
}
/**
* 预验证所有连接点,确保至少有两个可到达的下一步,并且最后一个节点是结束节点(或自定义条件为真)
*
* @param current_id 当前处理的节点ID
*/
async preCheckConnectPoints(current_id: string): Promise<void | Error[]> {
let resultMessages: Error[] = [];
try {
const edge_list = this.edges.filter(edge => edge.sourceNodeId === current_id);
if (edge_list.length === 0) {
const node_type = this.nodes.find(n => n.id === current_id)?.type || '';
console.log(`[pre-check] No valid connections found for node type ${node_type}, node ID: ${current_id}`);
const end = end_nodes_dict[this.workflowModel];
if (!end.includes(node_type)) {
throw new Error(`${t('views.applicationWorkflow.validate.noNextNode')}: Node ID: ${current_id}`);
}
}
// Continue processing...
} catch (error: any) {
resultMessages.push(error);
}
if (resultMessages.length > 0) {
throw new Error(resultMessages.join(',\n'));
}
}
This revision improves the structure and functionality while maintaining correctness and readability.
| const workflow = new WorkFlowInstance(lf.value.getGraphData(), WorkflowMode.ApplicationLoop) | ||
| return Promise.all(lf.value.graphModel.nodes.map((element: any) => element?.validate?.())) | ||
| .then(() => { | ||
| const loop_node_id = props.nodeModel.properties.loop_node_id |
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 has one main potential issue that needs to be addressed:
Issue
The workFlowMode parameter in the WorkFlowInstance constructor is set incorrectly. According to OpenAI or the relevant documentation, it should typically not include "ApplicationLoop" unless you have a special use case where you want to enable application-level looping.
Instead of this line:
const workflow = new WorkFlowInstance(lf.value.getGraphData(), WorkflowMode.ApplicationLoop);Use either the base mode without specifying it (new WorkFlowInstance(lf.value.getGraphData())) (unless otherwise specified), or if there's a specific reason to use "ApplicationLoop", confirm with the relevant guidelines or context about why this mode might be necessary.
Additionally, ensure that all properties and methods referenced (lf.value, getGraphData(), etc.) are properly defined and accessible within the scope they are being used.
fix: Allow variables to be assigned as the end node of the loop node