@@ -76,6 +76,47 @@ class ActResponse(BaseModel):
7676 message : str
7777
7878
79+ def build_conversation_context (
80+ project_id : str ,
81+ conversation_id : str | None ,
82+ db : Session ,
83+ * ,
84+ exclude_message_id : str | None = None ,
85+ limit : int = 20
86+ ) -> str :
87+ """Return a formatted snippet of recent chat history for context transfer."""
88+
89+ query = db .query (Message ).filter (Message .project_id == project_id )
90+ if conversation_id :
91+ query = query .filter (Message .conversation_id == conversation_id )
92+
93+ history : list [Message ] = []
94+ for msg in query .order_by (Message .created_at .desc ()):
95+ if exclude_message_id and msg .id == exclude_message_id :
96+ continue
97+ if msg .metadata_json and msg .metadata_json .get ("hidden_from_ui" ):
98+ continue
99+ if msg .role not in ("user" , "assistant" ):
100+ continue
101+ history .append (msg )
102+ if len (history ) >= limit :
103+ break
104+
105+ if not history :
106+ return ""
107+
108+ history .reverse ()
109+ lines = []
110+ for msg in history :
111+ role = "User" if msg .role == "user" else "Assistant"
112+ content = (msg .content or "" ).strip ()
113+ if not content :
114+ continue
115+ lines .append (f"{ role } :\n { content } " )
116+
117+ return "\n " .join (lines )
118+
119+
79120async def execute_act_instruction (
80121 project_id : str ,
81122 instruction : str ,
@@ -136,7 +177,9 @@ async def execute_chat_task(
136177 db : Session ,
137178 cli_preference : CLIType = None ,
138179 fallback_enabled : bool = True ,
139- is_initial_prompt : bool = False
180+ is_initial_prompt : bool = False ,
181+ _request_id : str | None = None ,
182+ user_message_id : str | None = None
140183):
141184 """Background task for executing Chat instructions"""
142185 try :
@@ -182,8 +225,26 @@ async def execute_chat_task(
182225 # Qwen Coder does not support images yet; drop them to prevent errors
183226 safe_images = [] if cli_preference == CLIType .QWEN else images
184227
228+ instruction_payload = instruction
229+ if not is_initial_prompt :
230+ context_block = build_conversation_context (
231+ project_id ,
232+ conversation_id ,
233+ db ,
234+ exclude_message_id = user_message_id
235+ )
236+ if context_block :
237+ instruction_payload = (
238+ "You are continuing an ongoing coding session. Reference the recent conversation history below before acting.\n "
239+ "<conversation_history>\n "
240+ f"{ context_block } \n "
241+ "</conversation_history>\n \n "
242+ "Latest user instruction: \n "
243+ f"{ instruction } "
244+ )
245+
185246 result = await cli_manager .execute_instruction (
186- instruction = instruction ,
247+ instruction = instruction_payload ,
187248 cli_type = cli_preference ,
188249 fallback_enabled = project_fallback_enabled ,
189250 images = safe_images ,
@@ -291,7 +352,8 @@ async def execute_act_task(
291352 cli_preference : CLIType = None ,
292353 fallback_enabled : bool = True ,
293354 is_initial_prompt : bool = False ,
294- request_id : str = None
355+ request_id : str = None ,
356+ user_message_id : str | None = None
295357):
296358 """Background task for executing Act instructions"""
297359 try :
@@ -347,8 +409,26 @@ async def execute_act_task(
347409 # Qwen Coder does not support images yet; drop them to prevent errors
348410 safe_images = [] if cli_preference == CLIType .QWEN else images
349411
412+ instruction_payload = instruction
413+ if not is_initial_prompt :
414+ context_block = build_conversation_context (
415+ project_id ,
416+ conversation_id ,
417+ db ,
418+ exclude_message_id = user_message_id
419+ )
420+ if context_block :
421+ instruction_payload = (
422+ "You are continuing an ongoing coding session. Reference the recent conversation history below before acting.\n "
423+ "<conversation_history>\n "
424+ f"{ context_block } \n "
425+ "</conversation_history>\n \n "
426+ "Latest user instruction: \n "
427+ f"{ instruction } "
428+ )
429+
350430 result = await cli_manager .execute_instruction (
351- instruction = instruction ,
431+ instruction = instruction_payload ,
352432 cli_type = cli_preference ,
353433 fallback_enabled = project_fallback_enabled ,
354434 images = safe_images ,
@@ -690,7 +770,8 @@ async def run_act(
690770 cli_preference ,
691771 fallback_enabled ,
692772 body .is_initial_prompt ,
693- request_id
773+ request_id ,
774+ user_message .id
694775 )
695776 return ActResponse (
696777 session_id = session .id ,
@@ -825,7 +906,9 @@ async def run_chat(
825906 db ,
826907 cli_preference ,
827908 fallback_enabled ,
828- body .is_initial_prompt
909+ body .is_initial_prompt ,
910+ None ,
911+ user_message .id
829912 )
830913
831914 return ActResponse (
0 commit comments