nighthawk 0.2.0

AI terminal autocomplete — zero config, zero login, zero telemetry
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
use super::tier::PredictionTier;
use crate::daemon::config::LlmConfig;
use crate::proto::{CompletionRequest, Suggestion, SuggestionSource};
use async_trait::async_trait;
use reqwest::Client;
use serde::{Deserialize, Serialize};
use std::sync::atomic::{AtomicBool, Ordering};
use tracing::{debug, warn};

const DEFAULT_SYSTEM_PROMPT: &str = "\
You are a terminal command autocomplete engine. \
Given a partial shell command, output ONLY the text that should be appended \
after the cursor to complete the command. Do not repeat text before the cursor. \
Do not add explanation, markdown, or quotes. Output only the raw completion text. \
If you cannot suggest anything, output nothing.";

/// Maximum number of input characters to send to the LLM.
/// Prevents exceeding model context windows on very long inputs.
const MAX_INPUT_CHARS: usize = 2048;

pub struct LlmTier {
    client: Client,
    config: LlmConfig,
    system_prompt: String,
    /// Tracks whether we've already warned about a connection failure,
    /// so subsequent failures log at debug instead of warn.
    has_warned_connection: AtomicBool,
}

// ── OpenAI-compatible API types (private) ───────────────────────

#[derive(Serialize)]
struct ChatRequest {
    model: String,
    messages: Vec<ChatMessage>,
    temperature: f32,
    max_tokens: u32,
    stream: bool,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    stop: Vec<String>,
    /// Disable reasoning/thinking mode for models that support it.
    /// SGLang uses chat_template_kwargs, others may ignore this field.
    #[serde(skip_serializing_if = "Option::is_none")]
    chat_template_kwargs: Option<ChatTemplateKwargs>,
}

#[derive(Serialize)]
struct ChatTemplateKwargs {
    enable_thinking: bool,
}

#[derive(Serialize)]
struct ChatMessage {
    role: String,
    content: String,
}

#[derive(Deserialize)]
struct ChatResponse {
    choices: Vec<ChatChoice>,
}

#[derive(Deserialize)]
struct ChatChoice {
    message: ChatResponseMessage,
}

#[derive(Deserialize)]
struct ChatResponseMessage {
    /// The main response content. Can be null for reasoning models
    /// that put all output into reasoning_content.
    content: Option<String>,
    /// Reasoning models (Qwen3, DeepSeek-R1) put thinking here
    /// when served with a reasoning parser (e.g. SGLang --reasoning-parser).
    #[serde(default)]
    #[allow(dead_code)]
    reasoning_content: Option<String>,
}

// ── Construction ────────────────────────────────────────────────

impl LlmTier {
    pub fn new(config: LlmConfig) -> Self {
        let client = Client::builder()
            .connect_timeout(std::time::Duration::from_millis(500))
            .timeout(std::time::Duration::from_millis(config.budget_ms as u64))
            .build()
            .unwrap_or_else(|_| Client::new());

        let system_prompt = config
            .system_prompt
            .clone()
            .unwrap_or_else(|| DEFAULT_SYSTEM_PROMPT.to_string());

        Self {
            client,
            config,
            system_prompt,
            has_warned_connection: AtomicBool::new(false),
        }
    }
}

// ── Helper functions (pub(crate) for testing) ───────────────────

/// Build the user prompt from the completion request.
pub(crate) fn build_user_prompt(req: &CompletionRequest) -> String {
    let input_before_cursor = &req.input[..req.cursor];
    // Truncate to last MAX_INPUT_CHARS to avoid exceeding model context
    let truncated = if input_before_cursor.len() > MAX_INPUT_CHARS {
        &input_before_cursor[input_before_cursor.len() - MAX_INPUT_CHARS..]
    } else {
        input_before_cursor
    };
    format!(
        "Shell: {}\nWorking directory: {}\nCommand so far: {}",
        req.shell.as_str(),
        req.cwd.display(),
        truncated
    )
}

/// Extract a clean completion from the raw LLM response.
///
/// Small models frequently return unexpected formats: fenced code blocks,
/// inline backticks, shell prompt prefixes, trailing comments, or echo
/// the input prefix. This function strips all of that down to the raw
/// completion suffix.
pub(crate) fn extract_completion(raw: &str, input_before_cursor: &str) -> Option<String> {
    let mut text = raw.trim().to_string();
    if text.is_empty() {
        return None;
    }

    // Strip fenced code blocks: ```...\ncontent\n```
    if text.starts_with("```") {
        // Remove opening fence (optionally with language tag)
        if let Some(after_fence) = text.strip_prefix("```") {
            let after_lang = after_fence
                .find('\n')
                .map(|i| &after_fence[i + 1..])
                .unwrap_or(after_fence);
            // Remove closing fence
            text = after_lang
                .strip_suffix("```")
                .unwrap_or(after_lang)
                .trim()
                .to_string();
        }
    }

    // Strip inline backticks: `completion`
    if text.starts_with('`') && text.ends_with('`') && text.len() >= 2 {
        text = text[1..text.len() - 1].to_string();
    }

    // Strip shell prompt prefixes
    for prefix in &["$ ", "> "] {
        if let Some(rest) = text.strip_prefix(prefix) {
            text = rest.to_string();
            break;
        }
    }

    // Take first line only
    if let Some(first_line) = text.lines().next() {
        text = first_line.to_string();
    }

    // Strip trailing comments (e.g., "checkout  # switch branches")
    if let Some(hash_pos) = text.find(" #") {
        text = text[..hash_pos].to_string();
    }

    // If the model echoed the full input, strip the prefix
    let trimmed_input = input_before_cursor.trim();
    if !trimmed_input.is_empty() && text.starts_with(trimmed_input) {
        text = text[trimmed_input.len()..].to_string();
    }

    let result = text.trim().to_string();
    if result.is_empty() {
        None
    } else {
        Some(result)
    }
}

// ── PredictionTier impl ─────────────────────────────────────────

#[async_trait]
impl PredictionTier for LlmTier {
    fn name(&self) -> &str {
        "local-llm"
    }

    fn budget_ms(&self) -> u32 {
        self.config.budget_ms
    }

    async fn predict(&self, req: &CompletionRequest) -> Vec<Suggestion> {
        let input_before_cursor = &req.input[..req.cursor];
        if input_before_cursor.trim().is_empty() {
            return vec![];
        }

        let user_prompt = build_user_prompt(req);
        let url = format!(
            "{}/chat/completions",
            self.config.endpoint.trim_end_matches('/')
        );

        let chat_req = ChatRequest {
            model: self.config.model.clone(),
            messages: vec![
                ChatMessage {
                    role: "system".into(),
                    content: self.system_prompt.clone(),
                },
                ChatMessage {
                    role: "user".into(),
                    content: user_prompt,
                },
            ],
            temperature: self.config.temperature,
            max_tokens: self.config.max_tokens,
            stream: false,
            stop: vec!["\n".into()],
            chat_template_kwargs: Some(ChatTemplateKwargs {
                enable_thinking: false,
            }),
        };

        let response = match self.client.post(&url).json(&chat_req).send().await {
            Ok(resp) => resp,
            Err(e) => {
                if self
                    .has_warned_connection
                    .compare_exchange(false, true, Ordering::Relaxed, Ordering::Relaxed)
                    .is_ok()
                {
                    warn!(
                        error = %e,
                        endpoint = %self.config.endpoint,
                        "Local LLM tier: cannot reach endpoint — is the server running?"
                    );
                } else {
                    debug!(error = %e, "LLM request failed");
                }
                return vec![];
            }
        };

        if !response.status().is_success() {
            debug!(status = %response.status(), "LLM endpoint returned non-success status");
            return vec![];
        }

        let chat_resp: ChatResponse = match response.json().await {
            Ok(resp) => resp,
            Err(e) => {
                debug!(error = %e, "Failed to parse LLM response JSON");
                return vec![];
            }
        };

        let raw_content = match chat_resp.choices.first() {
            Some(choice) => match &choice.message.content {
                Some(c) if !c.trim().is_empty() => c.clone(),
                _ => {
                    debug!("LLM response had empty/null content (reasoning model may need enable_thinking=false)");
                    return vec![];
                }
            },
            None => {
                debug!("LLM response had no choices");
                return vec![];
            }
        };

        match extract_completion(&raw_content, input_before_cursor) {
            Some(completion) => vec![Suggestion {
                text: completion,
                replace_start: req.cursor,
                replace_end: req.input.len(),
                confidence: 0.6,
                source: SuggestionSource::LocalModel,
                description: Some(format!("via {}", self.config.model)),
                diff_ops: None,
            }],
            None => vec![],
        }
    }
}

// ── Tests ───────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::proto::Shell;
    use std::path::PathBuf;

    fn test_request() -> CompletionRequest {
        CompletionRequest {
            input: "git checkout -b feat/".into(),
            cursor: 21,
            cwd: PathBuf::from("/home/user/project"),
            shell: Shell::Bash,
        }
    }

    // ── Prompt construction tests ───────────────────────────────

    #[test]
    fn prompt_includes_shell_and_cwd() {
        let req = test_request();
        let prompt = build_user_prompt(&req);
        assert!(prompt.contains("Shell: bash"));
        assert!(prompt.contains("/home/user/project"));
        assert!(prompt.contains("git checkout -b feat/"));
    }

    #[test]
    fn prompt_truncates_at_cursor() {
        let req = CompletionRequest {
            input: "git checkout main extra_stuff".into(),
            cursor: 17, // "git checkout main"
            cwd: PathBuf::from("/tmp"),
            shell: Shell::Zsh,
        };
        let prompt = build_user_prompt(&req);
        assert!(prompt.contains("git checkout main"));
        assert!(!prompt.contains("extra_stuff"));
    }

    #[test]
    fn prompt_truncates_long_input() {
        let long_input = "a".repeat(MAX_INPUT_CHARS + 500);
        let req = CompletionRequest {
            input: long_input.clone(),
            cursor: long_input.len(),
            cwd: PathBuf::from("/tmp"),
            shell: Shell::Bash,
        };
        let prompt = build_user_prompt(&req);
        // The "Command so far:" portion should be at most MAX_INPUT_CHARS
        let after_prefix = prompt.split("Command so far: ").nth(1).unwrap();
        assert_eq!(after_prefix.len(), MAX_INPUT_CHARS);
    }

    // ── Completion extraction tests ─────────────────────────────

    #[test]
    fn extract_completion_simple() {
        assert_eq!(
            extract_completion("new-feature-branch", "git checkout -b "),
            Some("new-feature-branch".into())
        );
    }

    #[test]
    fn extract_completion_multiline_takes_first() {
        assert_eq!(
            extract_completion("new-feature\ngit push origin", "git checkout -b "),
            Some("new-feature".into())
        );
    }

    #[test]
    fn extract_completion_empty() {
        assert_eq!(extract_completion("", "git "), None);
        assert_eq!(extract_completion("  \n  ", "git "), None);
    }

    #[test]
    fn extract_completion_strips_prefix_echo() {
        assert_eq!(
            extract_completion("git checkout main", "git checkout "),
            Some("main".into())
        );
    }

    #[test]
    fn extract_completion_strips_backticks() {
        // Inline backticks
        assert_eq!(
            extract_completion("`checkout main`", "git "),
            Some("checkout main".into())
        );
    }

    #[test]
    fn extract_completion_strips_fenced_code_block() {
        let raw = "```bash\ncheckout main\n```";
        assert_eq!(
            extract_completion(raw, "git "),
            Some("checkout main".into())
        );
    }

    #[test]
    fn extract_completion_strips_comments() {
        assert_eq!(
            extract_completion("checkout # switch branches", "git "),
            Some("checkout".into())
        );
    }

    #[test]
    fn extract_completion_strips_prompt_prefix() {
        assert_eq!(
            extract_completion("$ checkout main", "git "),
            Some("checkout main".into())
        );
        assert_eq!(
            extract_completion("> checkout main", "git "),
            Some("checkout main".into())
        );
    }

    #[test]
    fn extract_completion_trims_whitespace() {
        assert_eq!(
            extract_completion("  new-feature  \n", "git checkout -b "),
            Some("new-feature".into())
        );
    }

    // ── Tier construction tests ─────────────────────────────────

    #[test]
    fn default_config_produces_valid_tier() {
        let tier = LlmTier::new(LlmConfig::default());
        assert_eq!(tier.name(), "local-llm");
        assert_eq!(tier.budget_ms(), 500);
    }

    // ── Serialization tests ─────────────────────────────────────

    #[test]
    fn chat_request_serializes_stream_false() {
        let req = ChatRequest {
            model: "test".into(),
            messages: vec![],
            temperature: 0.0,
            max_tokens: 64,
            stream: false,
            stop: vec!["\n".into()],
            chat_template_kwargs: Some(ChatTemplateKwargs {
                enable_thinking: false,
            }),
        };
        let json = serde_json::to_value(&req).unwrap();
        assert_eq!(json["stream"], false);
        assert_eq!(json["chat_template_kwargs"]["enable_thinking"], false);
        assert_eq!(json["stop"][0], "\n");
    }
}