FIX(app): fix Chainlit ContextVar error

- Disable automatic Step wrapping
- Add error handling with try-except
This commit is contained in:
2025-12-24 15:12:42 +09:00
parent f5d542c2de
commit 351355a256
2 changed files with 49 additions and 28 deletions

View File

@@ -16,5 +16,5 @@ commonLabels:
# 이미지 태그 설정 (ArgoCD Image Updater가 자동으로 업데이트)
images:
- name: gitea0213.kro.kr/bluemayne/mas
newTag: main-sha-476581d4ff9af3aa80218edc7af6ffb51b39c099
newTag: main-sha-e13d204b4fba6e81be755f7aac0aa5614e31bca2

View File

@@ -6,9 +6,21 @@ from workflow import mas_graph
from agents import AgentState
import os
from dotenv import load_dotenv
from functools import wraps
load_dotenv()
# Chainlit의 자동 Step 래핑 비활성화
def disable_auto_step(func):
"""Disable Chainlit's automatic step wrapping"""
@wraps(func)
async def wrapper(*args, **kwargs):
return await func(*args, **kwargs)
wrapper.__wrapped__ = func
# Chainlit이 확인하는 속성 설정
wrapper._no_step = True
return wrapper
@cl.on_chat_start
async def start():
@@ -32,32 +44,34 @@ async def start():
@cl.on_message
@disable_auto_step
async def main(message: cl.Message):
"""메시지 수신 시"""
# 초기 상태
initial_state: AgentState = {
"messages": [{"role": "user", "content": message.content}],
"current_agent": "orchestrator",
"task_plan": None,
"research_data": None,
"code_outputs": {},
"review_feedback": None,
"iteration_count": 0,
"is_approved": False,
"error": None
}
# 응답 메시지 생성
response_msg = cl.Message(content="")
await response_msg.send()
# 상태 표시용 메시지
status_msg = cl.Message(content="⏳ 작업 중...")
await status_msg.send()
try:
# 초기 상태
initial_state: AgentState = {
"messages": [{"role": "user", "content": message.content}],
"current_agent": "orchestrator",
"task_plan": None,
"research_data": None,
"code_outputs": {},
"review_feedback": None,
"iteration_count": 0,
"is_approved": False,
"error": None
}
# 응답 메시지 생성
response_msg = cl.Message(content="")
await response_msg.send()
# 상태 표시용 메시지
status_msg = cl.Message(content="⏳ 작업 중...")
await status_msg.send()
# MAS 그래프 실행
async for event in mas_graph.astream(initial_state):
# MAS 그래프 실행
async for event in mas_graph.astream(initial_state):
for node_name, state in event.items():
if node_name != "__end__":
last_message = state["messages"][-1]
@@ -120,11 +134,18 @@ async def main(message: cl.Message):
status_msg.content = status_text
await status_msg.update()
# 상태 메시지 제거
await status_msg.remove()
# 최종 업데이트
await response_msg.update()
# 상태 메시지 제거
await status_msg.remove()
# 최종 업데이트
await response_msg.update()
except Exception as e:
error_msg = f"❌ 오류가 발생했습니다: {str(e)}"
await cl.Message(content=error_msg).send()
print(f"Error in main: {e}")
import traceback
traceback.print_exc()
@cl.on_settings_update