Files
JARVIS/backend/app/agents/context.py

25 lines
585 B
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
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)