crtx-llm 0.1.1

Claude, Ollama, and replay adapters behind a shared trait.
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
//! HTTP adapter that posts to any OpenAI-compatible `/v1/chat/completions`
//! endpoint.
//!
//! [`OpenAiCompatAdapter`] implements [`LlmAdapter`] and works with any server
//! that speaks the OpenAI chat-completions wire format, including LM Studio
//! (default `http://localhost:1234`), LocalAI, Ollama in OpenAI-proxy mode
//! (`http://localhost:11434`), vLLM, and the hosted OpenAI API itself.
//!
//! ## Key differences from [`crate::ollama_http::OllamaHttpAdapter`]
//!
//! - No loopback enforcement — `base_url` may be any valid HTTP(S) URL.
//! - No digest-pin requirement — local model names are arbitrary strings;
//!   any non-empty model identifier is accepted.
//! - `api_key` is optional; when absent or empty, the `Authorization` header
//!   is omitted entirely (appropriate for local servers).
//!
//! ## Runtime ceiling
//!
//! Determined at construction from `base_url`:
//!
//! - Loopback host (`localhost`, `127.x.x.x`, `::1`) → [`RuntimeCeiling::LocalUnsigned`].
//! - Any other host → [`RuntimeCeiling::RemoteUnsigned`].
//!
//! ## SSE streaming
//!
//! [`OpenAiCompatAdapter::stream_boxed`] overrides the trait default and reads
//! the response body as Server-Sent Events. Each `data:` line is parsed as an
//! OpenAI streaming chunk; `data: [DONE]` terminates the stream.

use std::net::IpAddr;
use std::time::Duration;

use async_trait::async_trait;
use serde::{Deserialize, Serialize};

use crate::adapter::{
    blake3_hex, BoxStream, LlmAdapter, LlmError, LlmRequest, LlmResponse, LlmRole, StreamChunk,
    TokenUsage,
};
use crate::sensitivity::{check_remote_prompt_sensitivity, MaxSensitivity};

// ---------------------------------------------------------------------------
// Runtime ceiling
// ---------------------------------------------------------------------------

/// Runtime ceiling derived from the `base_url` at adapter construction time.
///
/// Used by `cortex run` to gate persistence policy decisions (ADR 0037
/// weakest-link): a loopback endpoint carries `LocalUnsigned`; any other
/// endpoint carries `RemoteUnsigned`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RuntimeCeiling {
    /// Endpoint host is a loopback address (`localhost`, `127.x.x.x`, `::1`).
    LocalUnsigned,
    /// Endpoint host is non-loopback.
    RemoteUnsigned,
}

// ---------------------------------------------------------------------------
// Adapter struct
// ---------------------------------------------------------------------------

/// HTTP adapter for any OpenAI-compatible `/v1/chat/completions` server.
///
/// Construct via [`OpenAiCompatAdapter::new`]. The adapter is `Send + Sync`
/// and may be held behind an `Arc<dyn LlmAdapter>`.
#[derive(Debug, Clone)]
pub struct OpenAiCompatAdapter {
    /// Base URL, e.g. `http://localhost:1234`. No trailing slash.
    base_url: String,
    /// Model identifier passed verbatim in the JSON body.
    model: String,
    /// Optional API key; `None` means no `Authorization` header is sent.
    api_key: Option<String>,
    /// Per-call HTTP timeout.
    timeout_ms: u64,
    /// ADR 0037 runtime ceiling computed from `base_url` at construction.
    ceiling: RuntimeCeiling,
    /// Maximum data-classification level permitted in remote prompts.
    /// Defaults to [`MaxSensitivity::Medium`] when constructed via [`Self::new`].
    max_sensitivity: MaxSensitivity,
}

impl OpenAiCompatAdapter {
    /// Construct an adapter.
    ///
    /// `api_key` accepts `None` or an empty-string `Some("")`; both result in
    /// no `Authorization` header being sent.
    ///
    /// `max_sensitivity` controls the data-classification gate before remote
    /// dispatch. Pass `None` to use the default of [`MaxSensitivity::Medium`],
    /// which blocks high-sensitivity memories from being sent to the endpoint.
    ///
    /// A warning is printed to stderr when `base_url` resolves to a non-loopback
    /// host, because all prompt content will be sent to that remote server.
    ///
    /// Returns [`LlmError::InvalidRequest`] when:
    /// - `base_url` does not start with `http://` or `https://`.
    /// - `base_url` contains no host.
    /// - `model` is empty.
    pub fn new(
        base_url: impl Into<String>,
        model: impl Into<String>,
        api_key: Option<String>,
        timeout_ms: u64,
        max_sensitivity: Option<MaxSensitivity>,
    ) -> Result<Self, LlmError> {
        let base_url = base_url.into();
        let model = model.into();

        if model.is_empty() {
            return Err(LlmError::InvalidRequest(
                "openai-compat: model must not be empty".to_string(),
            ));
        }

        let ceiling = ceiling_for_url(&base_url)?;

        // Warn when the endpoint is non-loopback: all prompt content is sent remotely.
        if ceiling == RuntimeCeiling::RemoteUnsigned {
            eprintln!(
                "cortex: openai-compat: WARNING: endpoint {} is not loopback-only. \
                 All prompt content will be sent to this remote server.",
                base_url
            );
        }

        // Normalise: treat empty string as absent key.
        let api_key = api_key.filter(|k| !k.is_empty());

        Ok(Self {
            base_url,
            model,
            api_key,
            timeout_ms,
            ceiling,
            max_sensitivity: max_sensitivity.unwrap_or(MaxSensitivity::Medium),
        })
    }

    /// Return the runtime ceiling determined at construction from `base_url`.
    #[must_use]
    pub fn runtime_ceiling(&self) -> RuntimeCeiling {
        self.ceiling
    }
}

// ---------------------------------------------------------------------------
// URL / ceiling helper
// ---------------------------------------------------------------------------

fn ceiling_for_url(base_url: &str) -> Result<RuntimeCeiling, LlmError> {
    let rest = if let Some(r) = base_url.strip_prefix("http://") {
        r
    } else if let Some(r) = base_url.strip_prefix("https://") {
        r
    } else {
        return Err(LlmError::InvalidRequest(format!(
            "openai-compat: base_url must start with http:// or https://: {base_url}"
        )));
    };

    let host = extract_host(rest).ok_or_else(|| {
        LlmError::InvalidRequest(format!(
            "openai-compat: base_url must contain a host: {base_url}"
        ))
    })?;

    if is_loopback_host(host) {
        Ok(RuntimeCeiling::LocalUnsigned)
    } else {
        Ok(RuntimeCeiling::RemoteUnsigned)
    }
}

fn extract_host(rest: &str) -> Option<&str> {
    // Strip userinfo if present (e.g. `user:pass@host:port/path`).
    let authority = rest.split(['/', '?', '#']).next().unwrap_or_default();
    if authority.is_empty() {
        return None;
    }

    // IPv6 literal `[::1]:port`
    if let Some(after_open) = authority.strip_prefix('[') {
        let (host, suffix) = after_open.split_once(']')?;
        if suffix.is_empty() || suffix.starts_with(':') {
            return Some(host);
        }
        return None;
    }

    // Strip optional port.
    let host = authority.split(':').next().unwrap_or_default();
    if host.is_empty() {
        None
    } else {
        Some(host)
    }
}

fn is_loopback_host(host: &str) -> bool {
    if host.eq_ignore_ascii_case("localhost") {
        return true;
    }
    host.parse::<IpAddr>().is_ok_and(|ip| ip.is_loopback())
}

// ---------------------------------------------------------------------------
// Wire types — non-streaming
// ---------------------------------------------------------------------------

/// Outgoing body for `POST /v1/chat/completions` (non-streaming).
#[derive(Debug, Serialize)]
struct ChatCompletionRequest<'a> {
    model: &'a str,
    messages: Vec<OpenAiMessage<'a>>,
    stream: bool,
    max_tokens: u32,
}

/// One message in the OpenAI chat format.
#[derive(Debug, Serialize)]
struct OpenAiMessage<'a> {
    role: &'a str,
    content: &'a str,
}

/// Top-level `/v1/chat/completions` response envelope.
#[derive(Debug, Deserialize)]
struct ChatCompletionResponse {
    #[serde(default)]
    choices: Vec<Choice>,
    #[serde(default)]
    usage: Option<OpenAiUsage>,
}

/// One element of the `choices` array.
#[derive(Debug, Deserialize)]
struct Choice {
    #[serde(default)]
    message: ChoiceMessage,
}

/// The `message` field inside a non-streaming choice.
#[derive(Debug, Default, Deserialize)]
struct ChoiceMessage {
    #[serde(default)]
    content: String,
}

/// Token usage reported by the provider (optional).
#[derive(Debug, Deserialize)]
struct OpenAiUsage {
    #[serde(default)]
    prompt_tokens: u32,
    #[serde(default)]
    completion_tokens: u32,
}

// ---------------------------------------------------------------------------
// Wire types — SSE streaming
// ---------------------------------------------------------------------------

/// One `data:` line from an OpenAI SSE stream.
#[derive(Debug, Deserialize)]
struct StreamChunkEnvelope {
    #[serde(default)]
    choices: Vec<StreamChoice>,
}

/// One element of `choices` in a streaming delta.
#[derive(Debug, Default, Deserialize)]
struct StreamChoice {
    #[serde(default)]
    delta: StreamDelta,
    finish_reason: Option<String>,
}

/// The `delta` field inside a streaming choice.
#[derive(Debug, Default, Deserialize)]
struct StreamDelta {
    #[serde(default)]
    content: String,
}

// ---------------------------------------------------------------------------
// LlmAdapter implementation
// ---------------------------------------------------------------------------

#[async_trait]
impl LlmAdapter for OpenAiCompatAdapter {
    fn adapter_id(&self) -> &'static str {
        "openai-compat"
    }

    async fn complete(&self, req: LlmRequest) -> Result<LlmResponse, LlmError> {
        // Sensitivity gate: reject prompts containing high-sensitivity markers
        // before any bytes leave the machine.
        let prompt_text: String = std::iter::once(req.system.as_str())
            .chain(req.messages.iter().map(|m| m.content.as_str()))
            .collect::<Vec<_>>()
            .join("\n");
        check_remote_prompt_sensitivity(&prompt_text, self.max_sensitivity)?;

        let base_url = self.base_url.clone();
        let model = self.model.clone();
        let api_key = self.api_key.clone();
        let timeout_ms = self.timeout_ms;

        let result = tokio::task::spawn_blocking(move || {
            call_openai_compat(&base_url, &model, api_key.as_deref(), &req, timeout_ms)
        })
        .await
        .map_err(|e| LlmError::Transport(format!("spawn_blocking join error: {e}")))?;

        result
    }

    /// Override with true OpenAI SSE streaming via `POST /v1/chat/completions`
    /// with `"stream": true`.
    ///
    /// Parses `data:` lines, extracts `choices[0].delta.content`, and skips
    /// the terminal `data: [DONE]` sentinel.
    fn stream_boxed(&self, req: LlmRequest) -> BoxStream<'_> {
        stream_openai_compat_sse(
            self.base_url.clone(),
            self.model.clone(),
            self.api_key.clone(),
            req,
        )
    }
}

// ---------------------------------------------------------------------------
// Synchronous HTTP call (non-streaming)
// ---------------------------------------------------------------------------

fn call_openai_compat(
    base_url: &str,
    model: &str,
    api_key: Option<&str>,
    req: &LlmRequest,
    timeout_ms: u64,
) -> Result<LlmResponse, LlmError> {
    let url = format!("{base_url}/v1/chat/completions");

    let messages: Vec<OpenAiMessage<'_>> = req
        .messages
        .iter()
        .map(|m| OpenAiMessage {
            role: role_to_str(m.role),
            content: &m.content,
        })
        .collect();

    let body = ChatCompletionRequest {
        model,
        messages,
        stream: false,
        max_tokens: req.max_tokens,
    };

    let body_value = serde_json::to_value(&body)
        .map_err(|e| LlmError::Transport(format!("request serialization failed: {e}")))?;

    let timeout = Duration::from_millis(timeout_ms);
    let agent = ureq::AgentBuilder::new().timeout(timeout).build();

    let mut request = agent.post(&url).set("content-type", "application/json");
    if let Some(key) = api_key {
        request = request.set("authorization", &format!("Bearer {key}"));
    }

    let raw_response = request
        .send_json(body_value)
        .map_err(|err| map_ureq_error(err, timeout_ms))?;

    let status = raw_response.status();
    if status != 200 {
        return Err(LlmError::Upstream(format!("HTTP {status}")));
    }

    let response_text = raw_response
        .into_string()
        .map_err(|e| LlmError::Transport(format!("reading response body: {e}")))?;

    let parsed: ChatCompletionResponse = serde_json::from_str(&response_text)
        .map_err(|e| LlmError::Parse(format!("openai-compat response parse: {e}")))?;

    let text = parsed
        .choices
        .into_iter()
        .next()
        .map(|c| c.message.content)
        .ok_or_else(|| {
            LlmError::Parse("openai-compat response contained no choices".to_string())
        })?;

    let raw_hash = blake3_hex(response_text.as_bytes());
    let usage = parsed.usage.map(|u| TokenUsage {
        prompt_tokens: u.prompt_tokens,
        completion_tokens: u.completion_tokens,
    });

    Ok(LlmResponse {
        text,
        parsed_json: None,
        model: model.to_string(),
        usage,
        raw_hash,
    })
}

// ---------------------------------------------------------------------------
// SSE streaming implementation
// ---------------------------------------------------------------------------

/// Build a [`BoxStream`] that drives OpenAI-compatible SSE streaming.
///
/// Extracted as a free function so the `async_stream::stream!` macro is not
/// nested inside an `impl` block, which can confuse lifetime inference.
fn stream_openai_compat_sse(
    base_url: String,
    model: String,
    api_key: Option<String>,
    req: LlmRequest,
) -> BoxStream<'static> {
    Box::pin(async_stream::stream! {
        let timeout_ms = req.timeout_ms;
        let result = tokio::task::spawn_blocking(move || {
            call_openai_compat_streaming(&base_url, &model, api_key.as_deref(), &req, timeout_ms)
        })
        .await;

        match result {
            Ok(chunks) => {
                for chunk in chunks {
                    yield chunk;
                }
            }
            Err(e) => yield Err(LlmError::Transport(format!("spawn_blocking join error: {e}"))),
        }
    })
}

/// Synchronous OpenAI-compatible SSE streaming call, executed inside
/// `spawn_blocking`.
///
/// Posts to `/v1/chat/completions` with `stream: true`, then reads the
/// response body line by line. SSE protocol:
/// - Empty lines are event separators — skip them.
/// - Lines beginning with `event:` are event-type hints — skip them.
/// - `data: [DONE]` is the terminal sentinel — stop processing.
/// - Lines beginning with `data:` carry the JSON delta payload.
fn call_openai_compat_streaming(
    base_url: &str,
    model: &str,
    api_key: Option<&str>,
    req: &LlmRequest,
    timeout_ms: u64,
) -> Vec<Result<StreamChunk, LlmError>> {
    let url = format!("{base_url}/v1/chat/completions");

    let messages: Vec<OpenAiMessage<'_>> = req
        .messages
        .iter()
        .map(|m| OpenAiMessage {
            role: role_to_str(m.role),
            content: &m.content,
        })
        .collect();

    let body = ChatCompletionRequest {
        model,
        messages,
        stream: true,
        max_tokens: req.max_tokens,
    };

    let body_value = match serde_json::to_value(&body) {
        Ok(v) => v,
        Err(e) => {
            return vec![Err(LlmError::Transport(format!(
                "request serialization failed: {e}"
            )))]
        }
    };

    let timeout = Duration::from_millis(timeout_ms);
    let agent = ureq::AgentBuilder::new().timeout(timeout).build();

    let mut request = agent.post(&url).set("content-type", "application/json");
    if let Some(key) = api_key {
        request = request.set("authorization", &format!("Bearer {key}"));
    }

    let raw_response = match request.send_json(body_value) {
        Ok(r) => r,
        Err(err) => return vec![Err(map_ureq_error(err, timeout_ms))],
    };

    let status = raw_response.status();
    if status != 200 {
        return vec![Err(LlmError::Upstream(format!("HTTP {status}")))];
    }

    let body_text = match raw_response.into_string() {
        Ok(s) => s,
        Err(e) => {
            return vec![Err(LlmError::Transport(format!(
                "reading streaming response body: {e}"
            )))]
        }
    };

    let mut chunks = Vec::new();

    for line in body_text.lines() {
        if line.is_empty() || line.starts_with("event:") {
            continue;
        }

        let data = match line.strip_prefix("data:") {
            Some(rest) => rest.trim(),
            None => continue,
        };

        // Terminal sentinel — no further lines need processing.
        if data == "[DONE]" {
            chunks.push(Ok(StreamChunk {
                delta: String::new(),
                finish_reason: Some("stop".into()),
            }));
            return chunks;
        }

        let envelope: StreamChunkEnvelope = match serde_json::from_str(data) {
            Ok(v) => v,
            Err(e) => {
                chunks.push(Err(LlmError::Parse(format!(
                    "openai-compat SSE data parse: {e}: {data}"
                ))));
                continue;
            }
        };

        let choice = match envelope.choices.into_iter().next() {
            Some(c) => c,
            None => continue,
        };

        let finish_reason = choice.finish_reason;
        let delta_text = choice.delta.content;

        chunks.push(Ok(StreamChunk {
            delta: delta_text,
            finish_reason,
        }));
    }

    chunks
}

// ---------------------------------------------------------------------------
// ureq error mapping
// ---------------------------------------------------------------------------

fn map_ureq_error(err: ureq::Error, timeout_ms: u64) -> LlmError {
    match err {
        ureq::Error::Transport(t) => {
            let msg = t.to_string();
            if is_timeout_message(&msg) {
                LlmError::Timeout { timeout_ms }
            } else {
                LlmError::Transport(msg)
            }
        }
        ureq::Error::Status(code, _) => LlmError::Upstream(format!("HTTP {code}")),
    }
}

fn is_timeout_message(msg: &str) -> bool {
    let lower = msg.to_ascii_lowercase();
    lower.contains("timed out") || lower.contains("deadline exceeded") || lower.contains("timeout")
}

// ---------------------------------------------------------------------------
// Role serialization helper
// ---------------------------------------------------------------------------

/// Return the lowercase role string used by the OpenAI chat-completions API.
fn role_to_str(role: LlmRole) -> &'static str {
    match role {
        LlmRole::User => "user",
        LlmRole::Assistant => "assistant",
        LlmRole::Tool => "tool",
    }
}