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,76 @@ 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" , "관세 예측 세션 연속성 유지" )
110+ if not state .get ("session_id" ) or state .get ("session_id" ) in [None , '' ]:
111+ state ["session_id" ] = str (uuid .uuid4 ())
114112 return state
115-
116113 # 패턴 기반 분류
117114 is_number_selection = _is_number_selection (current_query )
118115 is_question = _is_question (current_query )
119-
120116 # 질문 형태이면서 관세 예측 세션이 아닌 경우 QnA로 분류
121117 if is_question :
122118 state ["intent" ] = "qna"
123119 _add_classification_message (state , "qna" , "질문 형태 감지(우선)" )
120+ if prev_intent == "tariff_prediction" and state .get ("session_id" ):
121+ try :
122+ from core .tariff_prediction .agent .tariff_prediction_agent import workflow_manager
123+ workflow_manager .cleanup_session (state ["session_id" ])
124+ except Exception :
125+ pass
126+ state ["session_id" ] = None
124127 return state
125-
126128 # 키워드 기반 분류
127129 keyword_intent = _classify_by_keywords (current_query )
128130 if keyword_intent :
129131 state ["intent" ] = keyword_intent
130132 _add_classification_message (state , keyword_intent , "키워드 기반 분류" )
133+ if prev_intent == "tariff_prediction" and keyword_intent != "tariff_prediction" and state .get ("session_id" ):
134+ try :
135+ from core .tariff_prediction .agent .tariff_prediction_agent import workflow_manager
136+ workflow_manager .cleanup_session (state ["session_id" ])
137+ except Exception :
138+ pass
139+ if keyword_intent == "tariff_prediction" :
140+ if not state .get ("session_id" ) or state .get ("session_id" ) in [None , '' ]:
141+ state ["session_id" ] = str (uuid .uuid4 ())
142+ else :
143+ state ["session_id" ] = None
131144 return state
132-
133- # 숫자 선택이지만 관세 예측 세션이 아닌 경우에도 tariff_prediction으로 분류
134- # (HS 코드 직접 입력 등의 경우)
135145 if is_number_selection :
136146 state ["intent" ] = "tariff_prediction"
137147 _add_classification_message (state , "tariff_prediction" , "숫자 선택 감지" )
148+ if not state .get ("session_id" ) or state .get ("session_id" ) in [None , '' ]:
149+ state ["session_id" ] = str (uuid .uuid4 ())
138150 return state
139-
140- # LLM 기반 의도 분류를 수행
141151 intent = _classify_with_llm (current_query )
142152 state ["intent" ] = intent
143153 _add_classification_message (state , intent , "LLM 분류" )
144-
154+ if prev_intent == "tariff_prediction" and intent != "tariff_prediction" and state .get ("session_id" ):
155+ try :
156+ from core .tariff_prediction .agent .tariff_prediction_agent import workflow_manager
157+ workflow_manager .cleanup_session (state ["session_id" ])
158+ except Exception :
159+ pass
160+ if intent == "tariff_prediction" :
161+ if not state .get ("session_id" ) or state .get ("session_id" ) in [None , '' ]:
162+ state ["session_id" ] = str (uuid .uuid4 ())
163+ else :
164+ state ["session_id" ] = None
145165 return state
0 commit comments