sync: 鍚屾灞€鍩熺綉鏈€鏂颁唬鐮?(source master @ b69568e4)
This commit is contained in:
1 parent
4f7f07504a
commit
8bc8b0e082
1651 files changed
+278599
-11416
No files matched your search
@@ -108,9 +108,9 @@ async def create_run(thread_id: str, body: RunCreateRequest, request: Request) -
|
||||
async def stream_run(thread_id: str, body: RunCreateRequest, request: Request) -> StreamingResponse:
|
||||
"""Create a run and stream events via SSE.
|
||||
|
||||
The response includes a ``Content-Location`` header with the run's
|
||||
resource URL, matching the LangGraph Platform protocol. The
|
||||
``useStream`` React hook uses this to extract run metadata.
|
||||
``Content-Location`` identifies the canonical run resource. ``Location``
|
||||
points at the resumable stream endpoint; the LangGraph JS SDK uses it as
|
||||
``reconnectPath`` and sends ``Last-Event-ID`` there after a network drop.
|
||||
"""
|
||||
bridge = get_stream_bridge(request)
|
||||
run_mgr = get_run_manager(request)
|
||||
@@ -127,6 +127,8 @@ async def stream_run(thread_id: str, body: RunCreateRequest, request: Request) -
|
||||
# The SDK uses a greedy regex to extract the run id from this path,
|
||||
# so it must point at the canonical run resource without extra suffixes.
|
||||
"Content-Location": f"/api/threads/{thread_id}/runs/{record.run_id}",
|
||||
# The SDK prefixes this value with apiUrl (already ending in /api).
|
||||
"Location": f"/threads/{thread_id}/runs/{record.run_id}/stream",
|
||||
},
|
||||
)
|
||||
|
||||
@@ -318,47 +320,65 @@ async def stream_existing_run(
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
@router.get("/{thread_id}/messages")
|
||||
@router.get("/{thread_id}/run-messages")
|
||||
@require_permission("runs", "read", owner_check=True)
|
||||
async def list_thread_messages(
|
||||
async def list_thread_run_messages(
|
||||
thread_id: str,
|
||||
request: Request,
|
||||
limit: int = Query(default=50, le=200),
|
||||
before_seq: int | None = Query(default=None),
|
||||
after_seq: int | None = Query(default=None),
|
||||
) -> list[dict]:
|
||||
"""Return displayable messages for a thread (across all runs), with feedback attached."""
|
||||
"""Return event-store messages for a thread (across all runs), with feedback attached."""
|
||||
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
|
||||
# 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_map = await feedback_repo.list_by_thread_grouped(thread_id, user_id=user_id)
|
||||
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")
|
||||
}
|
||||
|
||||
# Find the last ai_message per run_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") == "ai_message":
|
||||
if msg.get("event_type") in {"llm.ai.response", "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
|
||||
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