Skip to content

Commit fdf4b69

Browse files
committed
🐛 Bug fix: northbound api cannot store and get conversation history list
♻️ Improvement: Model configurations now save automatically
1 parent d0041f1 commit fdf4b69

File tree

8 files changed

+561
-230
lines changed

8 files changed

+561
-230
lines changed

backend/services/agent_service.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,7 @@ async def run_agent_stream(
835835
authorization: str,
836836
user_id: str = None,
837837
tenant_id: str = None,
838+
skip_user_save: bool = False,
838839
):
839840
"""
840841
Start an agent run and stream responses.
@@ -850,10 +851,13 @@ async def run_agent_stream(
850851
)
851852

852853
# Save user message only if not in debug mode (before streaming starts)
853-
if not agent_request.is_debug:
854-
save_messages(agent_request, target=MESSAGE_ROLE["USER"],
855-
user_id=resolved_user_id,
856-
tenant_id=resolved_tenant_id)
854+
if not agent_request.is_debug and not skip_user_save:
855+
save_messages(
856+
agent_request,
857+
target=MESSAGE_ROLE["USER"],
858+
user_id=resolved_user_id,
859+
tenant_id=resolved_tenant_id,
860+
)
857861

858862
memory_ctx_preview = build_memory_context(
859863
resolved_user_id, resolved_tenant_id, agent_request.agent_id

backend/services/northbound_service.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
get_agent_id_by_name
2626
)
2727
from services.conversation_management_service import (
28+
save_conversation_user,
2829
get_conversation_list_service,
2930
create_new_conversation,
3031
update_conversation_title as update_conversation_title_service,
@@ -165,7 +166,7 @@ async def start_streaming_chat(
165166
add_mapping_id(internal_id=internal_conversation_id, external_id=external_conversation_id, tenant_id=ctx.tenant_id, user_id=ctx.user_id)
166167

167168
# Get history according to internal_conversation_id
168-
history = await get_conversation_history(ctx, external_conversation_id)
169+
history_resp = await get_conversation_history(ctx, external_conversation_id)
169170
agent_id = await get_agent_id_by_name(agent_name=agent_name, tenant_id=ctx.tenant_id)
170171
# Idempotency: only prevent concurrent duplicate starts
171172
composed_key = idempotency_key or _build_idempotency_key(ctx.tenant_id, external_conversation_id, agent_id, query)
@@ -174,11 +175,18 @@ async def start_streaming_chat(
174175
conversation_id=internal_conversation_id,
175176
agent_id=agent_id,
176177
query=query,
177-
history=history.get("history"),
178+
history=(history_resp.get("data", {})).get("history", []),
178179
minio_files=None,
179180
is_debug=False,
180181
)
181182

183+
# Synchronously persist the user message before starting the stream to avoid race conditions
184+
try:
185+
save_conversation_user(
186+
agent_request, user_id=ctx.user_id, tenant_id=ctx.tenant_id)
187+
except Exception as e:
188+
raise Exception(f"Failed to persist user message: {str(e)}")
189+
182190
except LimitExceededError as _:
183191
raise LimitExceededError("Query rate exceeded limit. Please try again later.")
184192
except UnauthorizedError as _:
@@ -193,6 +201,7 @@ async def start_streaming_chat(
193201
authorization=ctx.authorization,
194202
user_id=ctx.user_id,
195203
tenant_id=ctx.tenant_id,
204+
skip_user_save=True,
196205
)
197206
finally:
198207
if composed_key:

0 commit comments

Comments
 (0)