codekingpro/portable-devtools
114k
1from __future__ import annotations2 3from datetime import timedelta4from typing import Literal5 6from langgraph.types import TimeoutPolicy7 8_SYNC_TIMEOUT_PREFIX = (9 "Node timeouts are only supported for async nodes because sync Python "10 "execution cannot be safely cancelled in-process."11)12 13 14def coerce_timeout_policy(15 value: float | timedelta | TimeoutPolicy | None,16) -> TimeoutPolicy | None:17 """Normalize a timeout value to positive-second policy fields."""18 return TimeoutPolicy.coerce(value)19 20 21def sync_timeout_unsupported(22 name: str, *, kind: Literal["Node", "Task"] = "Node"23) -> ValueError:24 """Build the canonical error for using `timeout` with a sync target."""25 return ValueError(f"{_SYNC_TIMEOUT_PREFIX} {kind} {name!r} is sync.")26 