vw-cli 0.2.7

Rust-first autonomous agent runtime CLI
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
//! Gateway-first TUI runtime 骨架。
//!
//! 本模块解决 Phase 1 的两个直接目标:
//! 1. 让 vw-cli 可以不经过其他 UI 子系统,直接构造 `GatewayClient`。
//! 2. 建立 `GatewayUiRuntime` 的最小宿主,集中保存后续新 TUI 所需的
//!    gateway client、目录上下文、session scope 与 session title。
//! 3. 在 runtime 边界补上 CLI 专用 stream adapter,把 gateway 原始事件
//!    规整为内部 `UiRuntimeEvent`,不再让新 TUI 直接解析 `Other(JSON)`。
//!
//! 当前实现刻意保持克制:
//! - 不在这里提前引入 reducer、view model 或渲染逻辑。
//! - 不提前定义“为以后准备”的胖接口。
//! - 只保留后续 slice 一定会复用的稳定入口。

use super::stream_adapter::{UiRuntimeEvent, UiRuntimeTerminalEvent, adapt_gateway_stream_event};
use crate::app::agent::config::Config;
use std::fmt::Write as _;
use std::future::Future;
#[cfg(not(target_arch = "wasm32"))]
use std::io::{Read, Write};
#[cfg(not(target_arch = "wasm32"))]
use std::net::{TcpStream, ToSocketAddrs};
use std::path::{Path, PathBuf};
#[cfg(not(target_arch = "wasm32"))]
use std::process::{Command, Stdio};
#[cfg(not(target_arch = "wasm32"))]
use std::time::{Duration, Instant};
use vw_gateway_client::vw_api_types::id::SessionId;
use vw_gateway_client::{GatewayAuth, GatewayChatStreamRequest, GatewayClient, GatewayEndpoint};

#[cfg(not(target_arch = "wasm32"))]
const GATEWAY_HEALTH_PATH: &str = "/v1/health";
#[cfg(not(target_arch = "wasm32"))]
const GATEWAY_HEALTH_CONNECT_TIMEOUT: Duration = Duration::from_millis(250);
#[cfg(not(target_arch = "wasm32"))]
const GATEWAY_STARTUP_TIMEOUT: Duration = Duration::from_secs(8);
#[cfg(not(target_arch = "wasm32"))]
const GATEWAY_STARTUP_POLL_INTERVAL: Duration = Duration::from_millis(150);

#[cfg(not(target_arch = "wasm32"))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum GatewayPreflightOutcome {
    Ready,
    Started,
}

#[cfg(not(target_arch = "wasm32"))]
impl GatewayPreflightOutcome {
    pub(crate) fn started_gateway(self) -> bool {
        matches!(self, Self::Started)
    }
}

/// CLI 侧用于构造 `GatewayClient` 的最小引导配置。
///
/// 该结构只覆盖客户端访问网关所需的字段:主机、端口与认证信息。
/// 字段来源保持与 `vibewindow.json` 顶层的 `gateway_client` 配置块一致,
/// 这样新 TUI 不需要依赖桌面端的设置模块,也能复用同一份配置语义。
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct GatewayClientBootstrapConfig {
    /// 目标网关主机名或 IP。
    pub(crate) host: String,
    /// 目标网关端口。
    pub(crate) port: u16,
    /// 可选的 Basic Auth 用户名。
    pub(crate) username: Option<String>,
    /// 可选的 Basic Auth 密码。
    pub(crate) password: Option<String>,
    /// 可选的 `x-skey` 请求头。
    pub(crate) skey: Option<String>,
}

impl Default for GatewayClientBootstrapConfig {
    fn default() -> Self {
        Self {
            host: "127.0.0.1".to_string(),
            port: 42617,
            username: None,
            password: None,
            skey: None,
        }
    }
}

impl GatewayClientBootstrapConfig {
    /// 从 CLI 已解析的配置文件中读取 gateway client 引导信息。
    ///
    /// 读取失败时会回退到默认值,而不是让 legacy CLI 因配置缺失直接中断。
    /// 这样可以保证 S1-1 先把依赖边界与 runtime 骨架接好,后续再继续补齐真实切换路径。
    pub(crate) fn load(config: &Config) -> Self {
        let mut bootstrap = Self::default();

        let contents = match std::fs::read_to_string(&config.config_path) {
            Ok(contents) => contents,
            Err(err) => {
                tracing::warn!(
                    path = %config.config_path.display(),
                    error = %err,
                    "failed to read cli gateway bootstrap config, using defaults"
                );
                return bootstrap;
            }
        };

        let root = match serde_json::from_str::<serde_json::Value>(&contents) {
            Ok(root) => root,
            Err(err) => {
                tracing::warn!(
                    path = %config.config_path.display(),
                    error = %err,
                    "failed to parse cli gateway bootstrap config, using defaults"
                );
                return bootstrap;
            }
        };

        bootstrap.apply_json_root(&root);
        bootstrap
    }

    /// 将引导配置转换为 `GatewayEndpoint`。
    pub(crate) fn endpoint(&self) -> GatewayEndpoint {
        let mut endpoint = GatewayEndpoint::new(self.host.clone(), self.port);
        if let Some(auth) = self.auth() {
            endpoint = endpoint.with_auth(auth);
        }
        endpoint
    }

    /// 将顶层 JSON 根对象中的 `gateway_client` 块映射到当前结构。
    fn apply_json_root(&mut self, root: &serde_json::Value) {
        let Some(gateway) = root.get("gateway_client").and_then(serde_json::Value::as_object)
        else {
            return;
        };

        if let Some(host) = gateway.get("host").and_then(serde_json::Value::as_str) {
            let host = host.trim();
            if !host.is_empty() {
                self.host = host.to_string();
            }
        }

        if let Some(port) = gateway.get("port").and_then(serde_json::Value::as_u64)
            && let Ok(port) = u16::try_from(port)
            && port != 0
        {
            self.port = port;
        }

        self.username = gateway_optional_string(gateway.get("username"));
        self.password = gateway_optional_string(gateway.get("password"));
        self.skey = gateway_optional_string(gateway.get("skey"));
    }

    /// 仅当配置中存在至少一种认证信息时才生成认证对象。
    fn auth(&self) -> Option<GatewayAuth> {
        let auth = GatewayAuth {
            bearer_token: None,
            username: self.username.clone(),
            password: self.password.clone(),
            skey: self.skey.clone(),
        };

        if auth.bearer_token.is_none()
            && auth.username.is_none()
            && auth.password.is_none()
            && auth.skey.is_none()
        {
            None
        } else {
            Some(auth)
        }
    }
}

/// 新 TUI 运行时的会话级上下文种子。
///
/// 这里先只保存后续 runtime 一定需要的三项数据:
/// - 当前会话 ID
/// - 请求目录
/// - 会话 scope
/// - 会话标题
///
/// 后续 slice 会继续围绕该结构补齐 session snapshot、restore 和状态线所需信息。
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct GatewaySessionSeed {
    id: Option<String>,
    directory: PathBuf,
    scope: Option<String>,
    title: Option<String>,
}

impl GatewaySessionSeed {
    /// 使用请求目录创建新的会话上下文。
    pub(crate) fn new(directory: PathBuf) -> Self {
        Self { id: None, directory, scope: None, title: None }
    }

    /// 返回当前 runtime 绑定的会话 ID(如果已设置)。
    pub(crate) fn id(&self) -> Option<&str> {
        self.id.as_deref()
    }

    /// 返回当前 runtime 绑定的请求目录。
    pub(crate) fn directory(&self) -> &Path {
        &self.directory
    }

    /// 返回会话 scope(如果已设置)。
    pub(crate) fn scope(&self) -> Option<&str> {
        self.scope.as_deref()
    }

    /// 返回会话标题(如果已设置)。
    pub(crate) fn title(&self) -> Option<&str> {
        self.title.as_deref()
    }

    /// 用新的会话 ID 覆盖当前种子。
    pub(crate) fn with_id(mut self, id: Option<String>) -> Self {
        self.id = normalize_optional_string(id);
        self
    }

    /// 用新的 scope 覆盖当前种子。
    pub(crate) fn with_scope(mut self, scope: Option<String>) -> Self {
        self.scope = normalize_optional_string(scope);
        self
    }

    /// 用新的标题覆盖当前种子。
    pub(crate) fn with_title(mut self, title: Option<String>) -> Self {
        self.title = normalize_optional_string(title);
        self
    }
}

/// 新 TUI 的 gateway-first runtime 宿主。
///
/// 当前 slice 中,该结构只负责两件事:
/// 1. 持有统一的 `GatewayClient`,作为后续 stream/session/question/todo 请求入口。
/// 2. 持有会话种子信息,为后续 session_ui 与状态层接线提供稳定上下文。
#[derive(Debug, Clone)]
pub(crate) struct GatewayUiRuntime {
    client: GatewayClient,
    session: GatewaySessionSeed,
}

impl GatewayUiRuntime {
    /// 基于外部已创建好的 client 与会话种子构造 runtime。
    pub(crate) fn new(client: GatewayClient, session: GatewaySessionSeed) -> Self {
        Self { client, session }
    }

    /// 基于 CLI 配置与给定会话种子直接创建 runtime。
    ///
    /// 这是 S1-1 的主要接线点:CLI 现在可以直接读取自身配置并构造 `GatewayClient`,
    /// 不必经过 legacy processor 或桌面端模块。
    pub(crate) fn from_config(
        config: &Config,
        session: GatewaySessionSeed,
    ) -> Result<Self, String> {
        let client = gateway_client(config)?;
        Ok(Self::new(client, session))
    }

    /// 以当前 shell 的请求目录作为默认请求目录创建 runtime。
    pub(crate) fn for_workspace(config: &Config) -> Result<Self, String> {
        let request_root = std::env::current_dir().unwrap_or_else(|_| config.workspace_dir.clone());
        Self::from_config(config, GatewaySessionSeed::new(request_root))
    }

    /// 返回统一的 gateway client 引用。
    pub(crate) fn client(&self) -> &GatewayClient {
        &self.client
    }

    /// 返回当前 runtime 绑定的 gateway endpoint。
    pub(crate) fn endpoint(&self) -> &GatewayEndpoint {
        self.client.endpoint()
    }

    /// 返回当前 runtime 的会话种子。
    pub(crate) fn session(&self) -> &GatewaySessionSeed {
        &self.session
    }

    /// 返回当前请求目录。
    pub(crate) fn directory(&self) -> &Path {
        self.session.directory()
    }

    /// 返回当前会话 scope。
    pub(crate) fn scope(&self) -> Option<&str> {
        self.session.scope()
    }

    /// 返回当前会话标题。
    pub(crate) fn title(&self) -> Option<&str> {
        self.session.title()
    }

    /// 返回当前会话 ID。
    pub(crate) fn session_id(&self) -> Option<&str> {
        self.session.id()
    }

    /// 用当前 UI 已确认的 session 元信息覆盖 runtime seed,保持后续默认回退一致。
    pub(crate) fn bind_session_seed(
        &mut self,
        session_id: Option<String>,
        scope: Option<String>,
        title: Option<String>,
    ) {
        self.session = self.session.clone().with_id(session_id).with_scope(scope).with_title(title);
    }

    /// 将缺失 `session_id` 的流式请求补齐为当前 runtime 绑定的会话。
    pub(crate) fn prepare_stream_request(
        &self,
        body: &GatewayChatStreamRequest,
    ) -> GatewayChatStreamRequest {
        let mut body = body.clone();
        if body.session_id.is_none()
            && let Some(session_id) = self.session_id()
        {
            body.session_id = Some(SessionId::from(session_id));
        }
        body
    }

    /// 解析调用方传入的会话 ID;若未提供,则回退到 runtime 当前会话。
    pub(crate) fn resolve_session_id<'a>(
        &'a self,
        session_id: Option<&'a str>,
    ) -> Result<&'a str, String> {
        normalize_optional_str_ref(session_id)
            .or_else(|| self.session_id())
            .ok_or_else(|| "gateway runtime session id is required".to_string())
    }

    /// 返回当前 runtime 应透传给 gateway 的目录查询值。
    pub(crate) fn directory_value(&self) -> Option<String> {
        runtime_directory_value(self.directory())
    }

    /// 以异步方式发起聊天流,并把 gateway 事件规整为 `UiRuntimeEvent`。
    ///
    /// 返回值始终是统一的终态控制事件,用于让上层明确区分
    /// done、cancel、timeout 与 error。
    pub(crate) async fn stream_chat(
        &self,
        body: &GatewayChatStreamRequest,
        mut on_event: impl FnMut(UiRuntimeEvent) -> bool,
    ) -> UiRuntimeTerminalEvent {
        let directory = self.directory_value();
        let body = self.prepare_stream_request(body);
        let mut terminal = None;

        let result =
            GatewayClient::stream_chat(self.endpoint(), directory.as_deref(), &body, |event| {
                let runtime_event = adapt_gateway_stream_event(event);
                if let UiRuntimeEvent::Terminal(runtime_terminal) = &runtime_event {
                    terminal = Some(runtime_terminal.clone());
                }

                if on_event(runtime_event) {
                    true
                } else {
                    if terminal.is_none() {
                        terminal = Some(cancelled_by_consumer_terminal());
                    }
                    false
                }
            })
            .await
            .map_err(|err| annotate_gateway_transport_error(err, self.endpoint()));

        finalize_stream_terminal(result, terminal)
    }

    /// 以阻塞方式发起聊天流,并把 gateway 事件规整为 `UiRuntimeEvent`。
    #[cfg(not(target_arch = "wasm32"))]
    pub(crate) fn stream_chat_blocking(
        &self,
        body: &GatewayChatStreamRequest,
        mut on_event: impl FnMut(UiRuntimeEvent) -> bool,
    ) -> UiRuntimeTerminalEvent {
        let directory = self.directory_value();
        let body = self.prepare_stream_request(body);
        let mut terminal = None;

        let result = GatewayClient::stream_chat_blocking(
            self.endpoint(),
            directory.as_deref(),
            &body,
            |event| {
                let runtime_event = adapt_gateway_stream_event(event);
                if let UiRuntimeEvent::Terminal(runtime_terminal) = &runtime_event {
                    terminal = Some(runtime_terminal.clone());
                }

                if on_event(runtime_event) {
                    true
                } else {
                    if terminal.is_none() {
                        terminal = Some(cancelled_by_consumer_terminal());
                    }
                    false
                }
            },
        )
        .map_err(|err| annotate_gateway_transport_error(err, self.endpoint()));

        finalize_stream_terminal(result, terminal)
    }

    #[cfg(not(target_arch = "wasm32"))]
    /// 在进入 tui_v2 前预检 gateway;若 endpoint 指向本地 loopback,则自动尝试拉起。
    pub(crate) fn ensure_local_gateway_ready_blocking(
        &self,
    ) -> Result<GatewayPreflightOutcome, String> {
        if gateway_health_ready(self.endpoint()) {
            return Ok(GatewayPreflightOutcome::Ready);
        }

        if !is_local_loopback_endpoint(self.endpoint()) {
            return Err(annotate_gateway_transport_error(
                "gateway preflight failed".to_string(),
                self.endpoint(),
            ));
        }

        let startup_error = start_local_gateway_process(self.endpoint()).err();
        if wait_for_gateway_health(self.endpoint(), GATEWAY_STARTUP_TIMEOUT) {
            return Ok(GatewayPreflightOutcome::Started);
        }

        let mut message = annotate_gateway_transport_error(
            "gateway preflight failed".to_string(),
            self.endpoint(),
        );
        if let Some(startup_error) = startup_error {
            message.push_str(" Auto-start failed: ");
            message.push_str(startup_error.as_str());
        } else {
            let _ = write!(
                message,
                " Auto-start did not reach {} with status=ok within {}s.",
                GATEWAY_HEALTH_PATH,
                GATEWAY_STARTUP_TIMEOUT.as_secs()
            );
        }
        Err(message)
    }
}

#[cfg(not(target_arch = "wasm32"))]
/// 以最小依赖方式在 CLI runtime 中阻塞等待 gateway 异步请求。
pub(crate) fn block_on_gateway<F, T>(future: F) -> Result<T, String>
where
    F: Future<Output = Result<T, String>>,
{
    match tokio::runtime::Handle::try_current() {
        Ok(handle) => tokio::task::block_in_place(|| handle.block_on(future)),
        Err(_) => {
            let runtime = tokio::runtime::Builder::new_current_thread()
                .enable_all()
                .build()
                .map_err(|err| err.to_string())?;
            runtime.block_on(future)
        }
    }
}

/// 根据 CLI 运行时配置构造 gateway endpoint。
pub(crate) fn gateway_client_endpoint(config: &Config) -> GatewayEndpoint {
    GatewayClientBootstrapConfig::load(config).endpoint()
}

/// 根据 CLI 运行时配置直接构造 `GatewayClient`。
pub(crate) fn gateway_client(config: &Config) -> Result<GatewayClient, String> {
    GatewayClient::new(gateway_client_endpoint(config))
}

pub(crate) fn annotate_gateway_transport_error(
    error: String,
    endpoint: &GatewayEndpoint,
) -> String {
    let message = normalize_optional_string(Some(error))
        .unwrap_or_else(|| "gateway request failed".to_string());

    if !looks_like_gateway_transport_error(message.as_str()) {
        return message;
    }

    format!(
        "{message}. Gateway endpoint {} is unavailable. Start it with `vibewindow gateway --host {} --port {}` and retry.",
        endpoint.describe(),
        endpoint.normalized_host(),
        endpoint.port
    )
}

/// 将 JSON 字符串字段规整为可选字符串。
fn gateway_optional_string(value: Option<&serde_json::Value>) -> Option<String> {
    value
        .and_then(serde_json::Value::as_str)
        .map(str::trim)
        .filter(|value| !value.is_empty())
        .map(ToString::to_string)
}

/// 将外部传入的字符串归一化为“空白即无值”。
fn normalize_optional_string(value: Option<String>) -> Option<String> {
    value.map(|value| value.trim().to_string()).filter(|value| !value.is_empty())
}

fn looks_like_gateway_transport_error(message: &str) -> bool {
    let normalized = message.trim().to_ascii_lowercase();
    normalized.contains("error sending request")
        || normalized.contains("connection refused")
        || normalized.contains("tcp connect error")
        || normalized.contains("dns error")
        || normalized.contains("couldn't connect to server")
}

/// 将可选字符串引用归一化为“空白即无值”。
pub(crate) fn normalize_optional_str_ref(value: Option<&str>) -> Option<&str> {
    value.map(str::trim).filter(|value| !value.is_empty())
}

/// 将目录路径规整为适合 gateway 查询参数的 UTF-8 字符串。
fn runtime_directory_value(directory: &Path) -> Option<String> {
    let directory = directory.to_string_lossy();
    let directory = directory.trim();
    if directory.is_empty() { None } else { Some(directory.to_string()) }
}
/// 生成“调用方主动停止消费流”时的统一终态。
fn cancelled_by_consumer_terminal() -> UiRuntimeTerminalEvent {
    UiRuntimeTerminalEvent::Cancelled {
        reason: Some("cancel requested; stream stopped after the next runtime event".to_string()),
        usage: None,
        message_id: None,
        parent_message_id: None,
    }
}

/// 在 stream 结束后,收口成稳定的终态返回值。
fn finalize_stream_terminal(
    result: Result<(), String>,
    terminal: Option<UiRuntimeTerminalEvent>,
) -> UiRuntimeTerminalEvent {
    if let Some(terminal) = terminal {
        return terminal;
    }

    match result {
        Ok(()) => {
            UiRuntimeTerminalEvent::Error("gateway stream closed before terminal event".to_string())
        }
        Err(err) => UiRuntimeTerminalEvent::from_error_message(err),
    }
}

#[cfg(not(target_arch = "wasm32"))]
fn is_local_loopback_endpoint(endpoint: &GatewayEndpoint) -> bool {
    matches!(
        endpoint
            .normalized_host()
            .trim()
            .trim_matches(|ch| ch == '[' || ch == ']')
            .to_ascii_lowercase()
            .as_str(),
        "127.0.0.1" | "localhost" | "::1"
    )
}

#[cfg(not(target_arch = "wasm32"))]
fn start_local_gateway_process(endpoint: &GatewayEndpoint) -> Result<(), String> {
    let executable = std::env::current_exe()
        .map_err(|err| format!("resolve current executable failed: {err}"))?;

    Command::new(executable)
        .arg("gateway")
        .arg("--host")
        .arg(endpoint.normalized_host())
        .arg("--port")
        .arg(endpoint.port.to_string())
        .stdin(Stdio::null())
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .spawn()
        .map(|_| ())
        .map_err(|err| format!("failed to start local gateway: {err}"))
}

#[cfg(not(target_arch = "wasm32"))]
fn wait_for_gateway_health(endpoint: &GatewayEndpoint, timeout: Duration) -> bool {
    let started = Instant::now();
    while started.elapsed() < timeout {
        if gateway_health_ready(endpoint) {
            return true;
        }
        std::thread::sleep(GATEWAY_STARTUP_POLL_INTERVAL);
    }
    false
}

#[cfg(not(target_arch = "wasm32"))]
fn gateway_health_ready(endpoint: &GatewayEndpoint) -> bool {
    let address = match resolve_gateway_socket_address(endpoint) {
        Some(address) => address,
        None => return false,
    };

    let mut stream = match TcpStream::connect_timeout(&address, GATEWAY_HEALTH_CONNECT_TIMEOUT) {
        Ok(stream) => stream,
        Err(_) => return false,
    };
    let _ = stream.set_read_timeout(Some(GATEWAY_HEALTH_CONNECT_TIMEOUT));
    let _ = stream.set_write_timeout(Some(GATEWAY_HEALTH_CONNECT_TIMEOUT));

    let request = format!(
        "GET {} HTTP/1.1\r\nHost: {}\r\nConnection: close\r\n\r\n",
        GATEWAY_HEALTH_PATH,
        endpoint.describe()
    );
    if stream.write_all(request.as_bytes()).is_err() {
        return false;
    }

    let mut response = String::new();
    if stream.read_to_string(&mut response).is_err() {
        return false;
    }

    let Some(status_line) = response.lines().next() else {
        return false;
    };

    status_line.contains(" 200 ")
        && (response.contains("\"status\":\"ok\"") || response.contains("\"status\": \"ok\""))
}

#[cfg(not(target_arch = "wasm32"))]
fn resolve_gateway_socket_address(endpoint: &GatewayEndpoint) -> Option<std::net::SocketAddr> {
    format!("{}:{}", endpoint.normalized_host(), endpoint.port)
        .to_socket_addrs()
        .ok()?
        .find(|addr| addr.is_ipv4() || addr.is_ipv6())
}

#[cfg(test)]
#[path = "gateway_tests.rs"]
mod gateway_tests;