|
16 | 16 | // No direct network access from this module. |
17 | 17 |
|
18 | 18 | use fs_help::{HelpSystem, HelpTopic}; |
| 19 | +use serde_json; |
19 | 20 |
|
20 | 21 | use crate::sidebar_state::{SidebarMode, SidebarState}; |
21 | 22 |
|
@@ -84,18 +85,49 @@ impl HelpSource for LocalHelpTopicSource { |
84 | 85 |
|
85 | 86 | // ── AiHelpSource ────────────────────────────────────────────────────────────── |
86 | 87 |
|
87 | | -/// Resolves help via AI capability stub (gRPC to fs-ai when present). |
| 88 | +/// Resolves help via the running fs-ai REST service (`POST /ai/chat`). |
88 | 89 | /// |
89 | | -/// In this phase the implementation is a stub that returns a canned response. |
90 | | -/// Full gRPC wiring happens when fs-ai is registered in fs-registry (Phase 6). |
| 90 | +/// The endpoint is read from the `FS_AI_ENDPOINT` environment variable |
| 91 | +/// (e.g. `http://localhost:8080`). Falls back to `HelpContent::None` |
| 92 | +/// when the service is unreachable or not configured. |
91 | 93 | pub struct AiHelpSource; |
92 | 94 |
|
| 95 | +impl AiHelpSource { |
| 96 | + fn endpoint() -> Option<String> { |
| 97 | + std::env::var("FS_AI_ENDPOINT").ok() |
| 98 | + } |
| 99 | +} |
| 100 | + |
93 | 101 | impl HelpSource for AiHelpSource { |
94 | 102 | fn resolve(&self, context: &str) -> HelpContent { |
95 | | - // Stub: returns a placeholder response indicating AI is available. |
96 | | - // Real impl: gRPC call to fs-ai service → stream response. |
97 | | - let response = format!("AI help for '{context}' — connect fs-ai for live responses."); |
98 | | - HelpContent::AiResponse(response) |
| 103 | + let Some(base) = Self::endpoint() else { |
| 104 | + return HelpContent::None; |
| 105 | + }; |
| 106 | + |
| 107 | + let url = format!("{base}/ai/chat"); |
| 108 | + let body = serde_json::json!({ |
| 109 | + "question": format!("Help me with the context: {context}"), |
| 110 | + "context": context |
| 111 | + }); |
| 112 | + |
| 113 | + let client = reqwest::blocking::Client::new(); |
| 114 | + let result = client |
| 115 | + .post(&url) |
| 116 | + .json(&body) |
| 117 | + .send() |
| 118 | + .and_then(reqwest::blocking::Response::error_for_status) |
| 119 | + .and_then(reqwest::blocking::Response::json::<serde_json::Value>); |
| 120 | + |
| 121 | + match result { |
| 122 | + Ok(json) => { |
| 123 | + let answer = json["answer"] |
| 124 | + .as_str() |
| 125 | + .unwrap_or("(no response)") |
| 126 | + .to_owned(); |
| 127 | + HelpContent::AiResponse(answer) |
| 128 | + } |
| 129 | + Err(_) => HelpContent::None, |
| 130 | + } |
99 | 131 | } |
100 | 132 |
|
101 | 133 | fn source_name(&self) -> &'static str { |
@@ -225,9 +257,10 @@ mod tests { |
225 | 257 | } |
226 | 258 |
|
227 | 259 | #[test] |
228 | | - fn ai_help_source_returns_response() { |
| 260 | + fn ai_help_source_returns_none_without_endpoint() { |
| 261 | + // Without FS_AI_ENDPOINT set the source gracefully returns None. |
229 | 262 | let src = AiHelpSource; |
230 | | - assert!(matches!(src.resolve("browser"), HelpContent::AiResponse(_))); |
| 263 | + assert!(matches!(src.resolve("browser"), HelpContent::None)); |
231 | 264 | } |
232 | 265 |
|
233 | 266 | #[test] |
|
0 commit comments