(workflow_manage_new_instance, node: INode, generate_loop)
| 125 | |
| 126 | |
| 127 | def loop(workflow_manage_new_instance, node: INode, generate_loop): |
| 128 | loop_global_data = {} |
| 129 | break_outer = False |
| 130 | is_interrupt_exec = False |
| 131 | loop_node_data = node.context.get('loop_node_data') or [] |
| 132 | loop_answer_data = node.context.get("loop_answer_data") or [] |
| 133 | start_index = node.context.get("current_index") or 0 |
| 134 | current_index = start_index |
| 135 | node_params = node.node_params |
| 136 | start_node_id = node_params.get('child_node', {}).get('runtime_node_id') |
| 137 | loop_type = node_params.get('loop_type') |
| 138 | start_node_data = None |
| 139 | chat_record = None |
| 140 | child_node = None |
| 141 | if start_node_id: |
| 142 | chat_record_id = node_params.get('child_node', {}).get('chat_record_id') |
| 143 | child_node = node_params.get('child_node', {}).get('child_node') |
| 144 | start_node_data = node_params.get('node_data') |
| 145 | chat_record = ChatRecord(id=chat_record_id, answer_text_list=[], answer_text='', |
| 146 | details=loop_node_data[current_index]) |
| 147 | |
| 148 | for item, index in generate_loop(current_index): |
| 149 | if 0 < max_loop_count <= index - start_index and loop_type == 'LOOP': |
| 150 | raise Exception(_('Exceeding the maximum number of cycles')) |
| 151 | """ |
| 152 | 指定次数循环 |
| 153 | @return: |
| 154 | """ |
| 155 | instance = workflow_manage_new_instance({'index': index, 'item': item}, loop_global_data, start_node_id, |
| 156 | start_node_data, chat_record, child_node) |
| 157 | response = instance.stream() |
| 158 | answer = '' |
| 159 | current_index = index |
| 160 | reasoning_content = '' |
| 161 | child_node_node_dict = {} |
| 162 | for chunk in response: |
| 163 | if chunk.get('node_type') == 'loop-break-node' and chunk.get('content', '') == 'BREAK': |
| 164 | break_outer = True |
| 165 | continue |
| 166 | child_node = chunk.get('child_node') |
| 167 | runtime_node_id = chunk.get('runtime_node_id', '') |
| 168 | chat_record_id = chunk.get('chat_record_id', '') |
| 169 | child_node_node_dict[runtime_node_id] = { |
| 170 | 'runtime_node_id': runtime_node_id, |
| 171 | 'chat_record_id': chat_record_id, |
| 172 | 'child_node': child_node} |
| 173 | content_chunk = (chunk.get('content', '') or '') |
| 174 | reasoning_content_chunk = (chunk.get('reasoning_content', '') or '') |
| 175 | if chunk.get('real_node_id'): |
| 176 | chunk['real_node_id'] = chunk['real_node_id'] + '__' + node.runtime_node_id + '__' + str(index) |
| 177 | reasoning_content += reasoning_content_chunk |
| 178 | answer += content_chunk |
| 179 | yield chunk |
| 180 | if chunk.get('node_status', "SUCCESS") == 'ERROR': |
| 181 | insert_or_replace(loop_node_data, index, instance.get_runtime_details()) |
| 182 | insert_or_replace(loop_answer_data, index, |
| 183 | get_answer_list(instance, child_node_node_dict, node.runtime_node_id)) |
| 184 | node.context['is_interrupt_exec'] = is_interrupt_exec |
no test coverage detected