11from langchain_core .messages import SystemMessage , AIMessage
22import re
33from typing import List , Optional
4+ import uuid
45
56from core .shared .states .states import CustomsAgentState
67from core .shared .utils .llm import get_llm
@@ -89,57 +90,61 @@ def _classify_with_llm(query: str) -> str:
8990
9091
9192def _add_classification_message (state : CustomsAgentState , intent : str , reason : str ) -> None :
92- """의도 분류 완료 메시지를 추가합니다."""
93- state ["messages" ].append (
94- AIMessage (content = f"의도 분류 완료: { intent } ({ reason } )" )
95- )
93+ # 의도 분류 메시지를 messages에 남기지 않음
94+ pass
9695
9796
9897def intent_router (state : CustomsAgentState ) -> CustomsAgentState :
99- """사용자 쿼리의 의도를 분류합니다."""
100-
98+ prev_intent = state .get ("intent" )
10199 current_query = state ["query" ].strip ()
102100 if not current_query :
103101 state ["intent" ] = DEFAULT_INTENT
104102 _add_classification_message (state , DEFAULT_INTENT , "빈 쿼리" )
105103 return state
106-
107104 # 세션 연속성 확인
108105 is_in_tariff_session = _is_in_tariff_session (state )
109-
110106 # 관세 예측 세션 중이면 무조건 tariff_prediction으로 분류
111107 if is_in_tariff_session :
112108 state ["intent" ] = "tariff_prediction"
113109 _add_classification_message (state , "tariff_prediction" , "관세 예측 세션 연속성 유지" )
114110 return state
115-
116111 # 패턴 기반 분류
117112 is_number_selection = _is_number_selection (current_query )
118113 is_question = _is_question (current_query )
119-
120114 # 질문 형태이면서 관세 예측 세션이 아닌 경우 QnA로 분류
121115 if is_question :
122116 state ["intent" ] = "qna"
123- _add_classification_message (state , "qna" , "질문 형태 감지" )
117+ _add_classification_message (state , "qna" , "질문 형태 감지(우선)" )
118+ if prev_intent == "tariff_prediction" :
119+ try :
120+ from core .tariff_prediction .agent .tariff_prediction_agent import workflow_manager
121+ workflow_manager .cleanup_session ()
122+ except Exception :
123+ pass
124124 return state
125-
126125 # 키워드 기반 분류
127126 keyword_intent = _classify_by_keywords (current_query )
128127 if keyword_intent :
129128 state ["intent" ] = keyword_intent
130129 _add_classification_message (state , keyword_intent , "키워드 기반 분류" )
130+ if prev_intent == "tariff_prediction" and keyword_intent != "tariff_prediction" :
131+ try :
132+ from core .tariff_prediction .agent .tariff_prediction_agent import workflow_manager
133+ workflow_manager .cleanup_session ()
134+ except Exception :
135+ pass
131136 return state
132-
133- # 숫자 선택이지만 관세 예측 세션이 아닌 경우에도 tariff_prediction으로 분류
134- # (HS 코드 직접 입력 등의 경우)
135137 if is_number_selection :
136138 state ["intent" ] = "tariff_prediction"
137139 _add_classification_message (state , "tariff_prediction" , "숫자 선택 감지" )
138140 return state
139-
140- # LLM 기반 의도 분류를 수행
141141 intent = _classify_with_llm (current_query )
142142 state ["intent" ] = intent
143143 _add_classification_message (state , intent , "LLM 분류" )
144-
144+ if prev_intent == "tariff_prediction" and intent != "tariff_prediction" :
145+ try :
146+ from core .tariff_prediction .agent .tariff_prediction_agent import workflow_manager
147+ workflow_manager .cleanup_session ()
148+ except Exception :
149+ pass
145150 return state
0 commit comments