From 86551f244fc37cb874352bfa03e20d750649478a Mon Sep 17 00:00:00 2001 From: jackfiled Date: Sun, 26 Nov 2023 11:26:37 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=A9=BA=E8=BD=AC=E7=A7=BB=E6=97=B6?= =?UTF-8?q?=E9=98=B2=E6=AD=A2=E9=80=92=E5=BD=92=E8=BD=AC=E7=A7=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Katheryne/KatheryneChatRobot.cs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Katheryne/KatheryneChatRobot.cs b/Katheryne/KatheryneChatRobot.cs index f441609..1eece0a 100644 --- a/Katheryne/KatheryneChatRobot.cs +++ b/Katheryne/KatheryneChatRobot.cs @@ -1,5 +1,6 @@ using System.Text.RegularExpressions; using Katheryne.Abstractions; +using Katheryne.Exceptions; using Katheryne.Models; using Microsoft.Extensions.Logging; @@ -83,6 +84,10 @@ public class KatheryneChatRobot : IChatRobot /// 存放输出回答的列表 private void EmptyTransform(List result) { + // 经过的状态集合, 避免递归循环 + HashSet movedStages = new(); + movedStages.Add(_currentStage); + var flag = true; while (flag) { @@ -93,6 +98,16 @@ public class KatheryneChatRobot : IChatRobot { flag = true; _currentStage = transformer.NextStage; + if (movedStages.Contains(_currentStage)) + { + // 发生递归调用 + throw new GrammarException("Recursively transform detected!"); + } + else + { + movedStages.Add(_currentStage); + } + result.Add(_grammarTree[_currentStage].Answer.RowString); _logger.LogDebug("Moving to stage {} with empty transform.",