diff --git a/crates/aien-cli/src/client.rs b/crates/aien-cli/src/client.rs index fbe8cf7b..c5980976 100644 --- a/crates/aien-cli/src/client.rs +++ b/crates/aien-cli/src/client.rs @@ -8,6 +8,73 @@ use std::time::Duration; pub const DEFAULT_ENDPOINT: &str = "http://127.0.0.1:18006/v1/chat/completions"; pub const DEFAULT_MODEL: &str = "atlas-lightning-omni"; +/// Explicit chat execution path. Native runtime is canonical. +/// The 18006 HTTP service remains only as a compatibility adapter. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum ChatBackend { + NativeRuntime, + RemoteAdapter(String), +} + +pub fn resolve_chat_backend() -> ChatBackend { + if let Ok(mode) = std::env::var("AIEN_CHAT_BACKEND") { + let mode = mode.trim().to_lowercase(); + if mode == "remote" { + let ep = std::env::var("AIEN_MODEL_ENDPOINT") + .ok() + .filter(|s| !s.trim().is_empty()) + .unwrap_or_else(|| DEFAULT_ENDPOINT.to_string()); + return ChatBackend::RemoteAdapter(ep); + } + if mode == "native" { + return ChatBackend::NativeRuntime; + } + } + let sock = aien_runtime::client::AienRuntimeClient::default_socket_path(); + if sock.exists() { + ChatBackend::NativeRuntime + } else { + ChatBackend::RemoteAdapter( + std::env::var("AIEN_MODEL_ENDPOINT") + .ok() + .filter(|s| !s.trim().is_empty()) + .unwrap_or_else(|| DEFAULT_ENDPOINT.to_string()), + ) + } +} + +pub fn describe_chat_backend(backend: &ChatBackend) -> String { + match backend { + ChatBackend::NativeRuntime => "native runtime (canonical)".to_string(), + ChatBackend::RemoteAdapter(ep) => format!("remote adapter (compatibility): {}", ep), + } +} + +/// Structured per request telemetry. A benchmark or test is only evidence of +/// the native runtime when path is native_runtime. Adapter traffic must never +/// be mistaken for native success. +pub fn emit_chat_telemetry(backend: &ChatBackend, model: &str) { + let (path, endpoint) = match backend { + ChatBackend::NativeRuntime => ("native_runtime", ""), + ChatBackend::RemoteAdapter(ep) => ("remote_adapter", ep.as_str()), + }; + let runtime_socket_present = + aien_runtime::client::AienRuntimeClient::default_socket_path().exists(); + let require_blackwell = std::env::var("AIEN_REQUIRE_BLACKWELL") + .map(|v| v.trim() == "1" || v.trim().eq_ignore_ascii_case("true")) + .unwrap_or(false); + let record = serde_json::json!({ + "event": "chat_path", + "path": path, + "backend": describe_chat_backend(backend), + "endpoint": endpoint, + "model": model, + "runtime_socket_present": runtime_socket_present, + "require_blackwell": require_blackwell, + }); + eprintln!("{}", record); +} + pub fn get_system_prompt() -> String { let mut p = String::from("You are AIEN. Drake is the operator. This Spark desk is ours.\n"); p.push_str("You are an autonomous operator-builder running on NVIDIA DGX Spark Grace Blackwell GB10 hardware.\n"); @@ -91,16 +158,23 @@ pub struct ChatClient { client: Client, endpoint: String, model: String, + backend: ChatBackend, } impl ChatClient { pub fn new(endpoint: Option, model: Option) -> Self { + let explicit_endpoint = endpoint.is_some(); let ep = endpoint .or_else(|| std::env::var("AIEN_MODEL_ENDPOINT").ok()) .unwrap_or_else(|| DEFAULT_ENDPOINT.to_string()); let md = model .or_else(|| std::env::var("AIEN_MODEL_NAME").ok()) .unwrap_or_else(|| DEFAULT_MODEL.to_string()); + let backend = if explicit_endpoint { + ChatBackend::RemoteAdapter(ep.clone()) + } else { + resolve_chat_backend() + }; let client = Client::builder() .connect_timeout(Duration::from_secs(10)) .read_timeout(Duration::from_secs(120)) @@ -110,6 +184,7 @@ impl ChatClient { client, endpoint: ep, model: md, + backend, } } @@ -128,6 +203,7 @@ impl ChatClient { stream_to_stdout: bool, max_tokens: usize, ) -> Result { + emit_chat_telemetry(&self.backend, &self.model); let payload = json!({ "model": self.model, "messages": messages,