跳转至

prompting

feishu.agent.prompting

build_time_aware_system_prompt

Python
build_time_aware_system_prompt(base_prompt: str, timezone_resolver: Callable[..., Any], *, now: Callable[[ZoneInfo], datetime] | None = None) -> Callable[..., Any]

构建会追加当前日期、时间与时区上下文的 system prompt 提供方。

源代码位于: feishu/agent/prompting.py
Python
def build_time_aware_system_prompt(
    base_prompt: str,
    timezone_resolver: Callable[..., Any],
    *,
    now: Callable[[ZoneInfo], datetime] | None = None,
) -> Callable[..., Any]:
    r"""构建会追加当前日期、时间与时区上下文的 system prompt 提供方。"""
    prompt = base_prompt.rstrip()

    async def system(event: Any | None = None, timezone: str | None = None) -> str:
        tz = _valid_timezone(timezone) or await _call_timezone_resolver(timezone_resolver, event)
        zone = ZoneInfo(tz)
        current = now(zone) if now is not None else datetime.now(zone)
        return f"{prompt}\n\n{_render_time_context(current, tz)}"

    return system

build_time_context

Python
build_time_context(timezone_resolver: Callable[..., Any], *, now: Callable[[ZoneInfo], datetime] | None = None) -> Callable[..., Any]

构建每轮动态时间上下文;调用方可把它附加到当前用户消息,避免破坏 system/历史前缀缓存。

源代码位于: feishu/agent/prompting.py
Python
def build_time_context(
    timezone_resolver: Callable[..., Any],
    *,
    now: Callable[[ZoneInfo], datetime] | None = None,
) -> Callable[..., Any]:
    r"""构建每轮动态时间上下文;调用方可把它附加到当前用户消息,避免破坏 system/历史前缀缓存。"""

    async def context(event: Any | None = None, timezone: str | None = None) -> str:
        tz = _valid_timezone(timezone) or await _call_timezone_resolver(timezone_resolver, event)
        zone = ZoneInfo(tz)
        current = now(zone) if now is not None else datetime.now(zone)
        return _render_time_context(current, tz)

    return context

build_timezone_resolver

Python
build_timezone_resolver(default_timezone: str, *, user_tokens: Any | None = None, client: Any | None = None, cache_ttl_seconds: float = 3600.0) -> Callable[..., Any]

构建按事件上下文、用户资料与配置默认值解析时区的 resolver。

源代码位于: feishu/agent/prompting.py
Python
def build_timezone_resolver(
    default_timezone: str,
    *,
    user_tokens: Any | None = None,
    client: Any | None = None,
    cache_ttl_seconds: float = 3600.0,
) -> Callable[..., Any]:
    r"""构建按事件上下文、用户资料与配置默认值解析时区的 resolver。"""
    fallback_timezone = _valid_timezone(default_timezone) or "Asia/Shanghai"
    cache: dict[str, tuple[float, str]] = {}

    async def resolve(event: Any | None = None) -> str:
        event_timezone = _valid_timezone(_event_timezone(event))
        if event_timezone:
            return event_timezone
        user = _event_user(event)
        cache_key = _user_cache_key(user)
        if cache_key:
            cached = cache.get(cache_key)
            if cached and time.monotonic() - cached[0] < cache_ttl_seconds:
                return cached[1]
        user_timezone = await _user_timezone(user_tokens, client, user)
        if user_timezone:
            if cache_key:
                cache[cache_key] = (time.monotonic(), user_timezone)
            return user_timezone
        return fallback_timezone

    return resolve