feat(chat): Update trace ID handling in chat stream API
- Modified `chat_stream_api` to use `new_trace()` instead of an empty string for `trace_id`, enhancing traceability. - Added tests to verify the correct generation and propagation of `trace_id` in responses, ensuring no empty trace IDs are sent to clients. This update improves the tracking of chat sessions and ensures compliance with traceability standards.
This commit is contained in:
@@ -157,6 +157,9 @@ def test_stream_contract_and_persist(env, fake_llm):
|
||||
assert first["meta"]["session_id"].startswith("sess-")
|
||||
assert first["meta"]["has_disclaimer"] is True
|
||||
assert first["meta"]["disclaimer"] == agent_service.CHAT_DISCLAIMER
|
||||
assert first["meta"]["trace_id"]
|
||||
assert first["meta"]["trace_id"] == first["id"]
|
||||
assert first["meta"]["trace_id"] == r.headers["X-Trace-Id"]
|
||||
|
||||
delta_text = "".join(
|
||||
p["choices"][0]["delta"].get("content", "") for p in payloads if "content" in p["choices"][0]["delta"]
|
||||
@@ -178,6 +181,16 @@ def test_stream_contract_and_persist(env, fake_llm):
|
||||
assert hist.status_code == 200 and hist.json()["total"] == 2
|
||||
|
||||
|
||||
def test_stream_meta_trace_id_when_context_empty(env, fake_llm, monkeypatch):
|
||||
"""无上下文 trace 时须 new_trace(),禁止向客户端下发空 trace_id。"""
|
||||
monkeypatch.setattr(chat_mod, "current_trace", lambda: "")
|
||||
r = env["client"].post("/api/chat/stream", json={"message": "hi"}, headers=CUSTOMER)
|
||||
assert r.status_code == 200
|
||||
first = _payloads(r)[0]
|
||||
assert first["meta"]["trace_id"].startswith("trc-")
|
||||
assert first["meta"]["trace_id"] == first["id"]
|
||||
|
||||
|
||||
def test_stream_advisor_no_disclaimer(env, fake_llm):
|
||||
"""内部角色(advisor)无免责声明:meta.disclaimer=None,落库 has_disclaimer=0。"""
|
||||
r = env["client"].post("/api/chat/stream", json={"message": "客户情况"}, headers=ADVISOR)
|
||||
|
||||
Reference in New Issue
Block a user