sync update from LAN repo (source ji @ 85078a31)
This commit is contained in:
1 parent
5d6627af50
commit
e602d88695
864 files changed
+99150
-6346
No files matched your search
@@ -331,34 +331,52 @@ async def list_thread_messages(
|
||||
event_store = get_run_event_store(request)
|
||||
messages = await event_store.list_messages(thread_id, limit=limit, before_seq=before_seq, after_seq=after_seq)
|
||||
|
||||
# Attach feedback to the last AI message of each run
|
||||
feedback_repo = get_feedback_repo(request)
|
||||
user_id = await get_current_user(request)
|
||||
feedback_map = await feedback_repo.list_by_thread_grouped(thread_id, user_id=user_id)
|
||||
|
||||
# Find the last ai_message per run_id
|
||||
last_ai_per_run: dict[str, int] = {} # run_id -> index in messages list
|
||||
for i, msg in enumerate(messages):
|
||||
if msg.get("event_type") == "ai_message":
|
||||
last_ai_per_run[msg["run_id"]] = i
|
||||
|
||||
# Attach feedback field
|
||||
last_ai_indices = set(last_ai_per_run.values())
|
||||
for i, msg in enumerate(messages):
|
||||
if i in last_ai_indices:
|
||||
run_id = msg["run_id"]
|
||||
fb = feedback_map.get(run_id)
|
||||
msg["feedback"] = (
|
||||
{
|
||||
"feedback_id": fb["feedback_id"],
|
||||
"rating": fb["rating"],
|
||||
"comment": fb.get("comment"),
|
||||
}
|
||||
if fb
|
||||
else None
|
||||
)
|
||||
else:
|
||||
msg["feedback"] = None
|
||||
# Attach feedback to its exact serialized assistant-message id. Legacy
|
||||
# rows created before answer-level feedback have no message_id and retain
|
||||
# the old fallback to the last AI response of their run.
|
||||
feedback_repo = get_feedback_repo(request)
|
||||
user_id = await get_current_user(request)
|
||||
feedback_rows = await feedback_repo.list_by_thread(thread_id, user_id=user_id)
|
||||
feedback_by_message = {
|
||||
str(row["message_id"]): row
|
||||
for row in feedback_rows
|
||||
if row.get("message_id")
|
||||
}
|
||||
legacy_feedback_by_run = {
|
||||
str(row["run_id"]): row
|
||||
for row in feedback_rows
|
||||
if not row.get("message_id")
|
||||
}
|
||||
|
||||
# The durable journal emits ``llm.ai.response``. ``ai_message`` remains
|
||||
# accepted for old event stores written by pre-journal builds.
|
||||
last_ai_per_run: dict[str, int] = {} # run_id -> index in messages list
|
||||
for i, msg in enumerate(messages):
|
||||
if msg.get("event_type") in {"llm.ai.response", "ai_message"}:
|
||||
last_ai_per_run[msg["run_id"]] = i
|
||||
|
||||
last_ai_indices = set(last_ai_per_run.values())
|
||||
for i, msg in enumerate(messages):
|
||||
content = msg.get("content")
|
||||
message_id = content.get("id") if isinstance(content, dict) else None
|
||||
fb = feedback_by_message.get(str(message_id)) if message_id else None
|
||||
if fb is None and i in last_ai_indices:
|
||||
fb = legacy_feedback_by_run.get(str(msg.get("run_id") or ""))
|
||||
msg["feedback"] = (
|
||||
{
|
||||
"feedback_id": fb["feedback_id"],
|
||||
"message_id": fb.get("message_id"),
|
||||
"agent_id": fb.get("agent_id"),
|
||||
"rating": fb["rating"],
|
||||
"reasons": fb.get("reasons") or [],
|
||||
"detail": fb.get("detail"),
|
||||
"comment": fb.get("comment"),
|
||||
"created_at": fb.get("created_at"),
|
||||
"updated_at": fb.get("updated_at"),
|
||||
}
|
||||
if fb
|
||||
else None
|
||||
)
|
||||
|
||||
return messages
|
||||
|
||||
|
||||
Reference in new issue
Block a user