| 1 | pub const DEFAULT_STREAM_CHUNK_TIMEOUT_SECS: u64 = 900; |
| 2 | |
| 3 | pub const MIN_STREAM_CHUNK_TIMEOUT_SECS: u64 = 1; |
| 4 | |
| 5 | pub const MAX_STREAM_CHUNK_TIMEOUT_SECS: u64 = 3600; |
| 6 | |
| 7 | /// Default bounded wait for SSE response headers. |
| 8 | pub const DEFAULT_STREAM_OPEN_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(180); |
| 9 | |
| 10 | /// Accepted response-header wait range, in seconds. |
| 11 | pub const MIN_STREAM_OPEN_TIMEOUT_SECS: u64 = 5; |
| 12 | |
| 13 | pub const MAX_STREAM_OPEN_TIMEOUT_SECS: u64 = 600; |
| 14 | |
| 15 | /// Resolve the response-header wait shared by every streaming adapter. |
| 16 | /// |
| 17 | /// A positive `[stream].open_timeout_secs` (legacy `[tui]` fallback) wins; omitted or `0` |
| 18 | /// falls back to the env override (`CODEWHALE_STREAM_OPEN_TIMEOUT_SECS`, |
| 19 | /// legacy `DEEPSEEK_STREAM_OPEN_TIMEOUT_SECS`), then the 180s default. Every |
| 20 | /// source clamps to `5..=600`. |
| 21 | #[must_use] |
| 22 | pub fn resolve_stream_open_timeout(configured_secs: Option<u64>) -> std::time::Duration { |
| 23 | match configured_secs { |
| 24 | Some(secs) if secs > 0 => std::time::Duration::from_secs( |
| 25 | secs.clamp(MIN_STREAM_OPEN_TIMEOUT_SECS, MAX_STREAM_OPEN_TIMEOUT_SECS), |
| 26 | ), |
| 27 | _ => stream_open_timeout_from_env( |
| 28 | crate::env_overrides::env_non_blank(&[ |
| 29 | "CODEWHALE_STREAM_OPEN_TIMEOUT_SECS", |
| 30 | "DEEPSEEK_STREAM_OPEN_TIMEOUT_SECS", |
| 31 | ]) |
| 32 | .as_deref(), |
| 33 | ), |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | pub fn stream_open_timeout_from_env(value: Option<&str>) -> std::time::Duration { |
| 38 | let secs = value |
| 39 | .and_then(|v| v.parse::<u64>().ok()) |
| 40 | .unwrap_or(DEFAULT_STREAM_OPEN_TIMEOUT.as_secs()) |
| 41 | .clamp(MIN_STREAM_OPEN_TIMEOUT_SECS, MAX_STREAM_OPEN_TIMEOUT_SECS); |
| 42 | std::time::Duration::from_secs(secs) |
| 43 | } |
| 44 | |
| 45 | /// Returns true when CODEWHALE_FORCE_HTTP1 (legacy alias: DEEPSEEK_FORCE_HTTP1) |
| 46 | /// is set to a truthy value (`1`, `true`, `yes`, `on`, case-insensitive). Read |
| 47 | /// only by `Config::force_http1`, which ORs it with the selected stream config flag; every |
| 48 | /// client builder and stream open takes that resolved value (#103, #6700). Anything else (unset, `0`, |
| 49 | /// `false`, ...) leaves HTTP/2 on. |
| 50 | #[must_use] |
| 51 | pub fn force_http1_from_env() -> bool { |
| 52 | crate::env_overrides::env_non_blank(&["CODEWHALE_FORCE_HTTP1", "DEEPSEEK_FORCE_HTTP1"]) |
| 53 | .map(|v| v.trim().to_ascii_lowercase()) |
| 54 | .is_some_and(|v| matches!(v.as_str(), "1" | "true" | "yes" | "on")) |
| 55 | } |
| 56 |