25 lines
585 B
Python
25 lines
585 B
Python
"""
|
||
Agent 运行时上下文
|
||
用于在工具调用链中传递 user_id 等上下文信息
|
||
"""
|
||
|
||
from contextvars import ContextVar
|
||
from typing import Optional
|
||
|
||
_current_user_id: ContextVar[Optional[str]] = ContextVar("current_user_id", default=None)
|
||
|
||
|
||
def set_current_user(user_id: str):
|
||
"""设置当前用户ID(线程/协程安全)"""
|
||
_current_user_id.set(user_id)
|
||
|
||
|
||
def get_current_user() -> str:
|
||
"""获取当前用户ID"""
|
||
return _current_user_id.get() or "default"
|
||
|
||
|
||
def clear_current_user():
|
||
"""清除当前用户上下文"""
|
||
_current_user_id.set(None)
|