feat:对接中心平台,完善接口鉴权
This commit is contained in:
@@ -61,24 +61,28 @@ async def ainvoke_screen(
|
||||
*,
|
||||
thread_id: str,
|
||||
session_id: str | None = None,
|
||||
user_id: int | None = None,
|
||||
) -> Any:
|
||||
"""每轮调用记录一个根 span,返回、等待用户或异常时结束。
|
||||
|
||||
session_id 由调用方在首次与恢复时一致传入(通常为 conversation_id)。
|
||||
未传时始终使用 thread_id,不从仅首次请求才有的 payload 推断。
|
||||
user_id 为用户中心的用户 ID,接口调用时传入,本地脚本不传。
|
||||
"""
|
||||
with _screen_trace(payload, thread_id, session_id) as (config, root):
|
||||
with _screen_trace(payload, thread_id, session_id, user_id) as (config, root):
|
||||
result = await graph.ainvoke(payload, config=config)
|
||||
_finish_trace(root, result)
|
||||
return result
|
||||
|
||||
|
||||
async def astream_screen(graph, payload, *, thread_id, session_id=None):
|
||||
async def astream_screen(
|
||||
graph, payload, *, thread_id, session_id=None, user_id=None
|
||||
):
|
||||
"""同一轮流保持同一个追踪上下文,关闭流时结束追踪"""
|
||||
from contextlib import aclosing
|
||||
|
||||
result = {}
|
||||
with _screen_trace(payload, thread_id, session_id) as (config, root):
|
||||
with _screen_trace(payload, thread_id, session_id, user_id) as (config, root):
|
||||
async with aclosing(
|
||||
graph.astream(payload, config=config, stream_mode=["custom", "values"])
|
||||
) as events:
|
||||
@@ -98,7 +102,7 @@ def _finish_trace(root, result):
|
||||
|
||||
|
||||
@contextmanager
|
||||
def _screen_trace(payload, thread_id, session_id):
|
||||
def _screen_trace(payload, thread_id, session_id, user_id=None):
|
||||
config = {"configurable": {"thread_id": thread_id}}
|
||||
client = _ensure_client() if tracing_enabled() else None
|
||||
if client is None:
|
||||
@@ -128,6 +132,8 @@ def _screen_trace(payload, thread_id, session_id):
|
||||
propagate_attributes(
|
||||
trace_name=TRACE_NAME,
|
||||
session_id=session_id or thread_id,
|
||||
# 用户中心的 user_id,面板 Users 页按用户聚合;脚本调用没有用户时不设
|
||||
user_id=str(user_id) if user_id is not None else None,
|
||||
tags=[FEATURE_TAG],
|
||||
metadata={
|
||||
"thread_id": thread_id,
|
||||
|
||||
Reference in New Issue
Block a user