rx4 0.4.0

The agent harness engine — loop, tools, providers, sessions, permissions, computer-use
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
//! Vector embeddings for semantic skill matching.
//!
//! Inspired by Unthinkclaw's embeddings module. Supports Gemini
//! `text-embedding-004` (free tier) and local Ollama `nomic-embed-text`.
//! The HTTP client is gated behind the `providers` feature; struct
//! definitions and `cosine_similarity` are always available so the module
//! compiles without any optional dependencies.

use std::collections::HashMap;

use serde::{Deserialize, Serialize};
use thiserror::Error;

use crate::skill_engine::Skill;

/// Embedding provider selection.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum EmbeddingProvider {
    /// Google Gemini `text-embedding-004` (free tier).
    Gemini,
    /// Local Ollama server (`nomic-embed-text` by default).
    Ollama,
}

impl EmbeddingProvider {
    /// Default model name for the provider.
    pub fn default_model(self) -> &'static str {
        match self {
            EmbeddingProvider::Gemini => "text-embedding-004",
            EmbeddingProvider::Ollama => "nomic-embed-text",
        }
    }

    /// Default embedding dimension for the provider.
    pub fn default_dimension(self) -> usize {
        match self {
            EmbeddingProvider::Gemini => 768,
            EmbeddingProvider::Ollama => 768,
        }
    }
}

/// Configuration for an [`EmbeddingClient`].
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EmbeddingConfig {
    /// Which embedding backend to use.
    pub provider: EmbeddingProvider,
    /// API key for hosted providers (Gemini). `None` for local Ollama.
    pub api_key: Option<String>,
    /// Model identifier passed to the provider.
    pub model: String,
    /// Expected vector dimensionality.
    pub dimension: usize,
}

impl Default for EmbeddingConfig {
    fn default() -> Self {
        Self::gemini()
    }
}

impl EmbeddingConfig {
    /// Gemini defaults: `text-embedding-004`, 768 dims, no key configured.
    pub fn gemini() -> Self {
        Self {
            provider: EmbeddingProvider::Gemini,
            api_key: None,
            model: EmbeddingProvider::Gemini.default_model().to_string(),
            dimension: EmbeddingProvider::Gemini.default_dimension(),
        }
    }

    /// Ollama defaults: `nomic-embed-text`, 768 dims, no key required.
    pub fn ollama() -> Self {
        Self {
            provider: EmbeddingProvider::Ollama,
            api_key: None,
            model: EmbeddingProvider::Ollama.default_model().to_string(),
            dimension: EmbeddingProvider::Ollama.default_dimension(),
        }
    }

    /// Attach an API key (used by hosted providers like Gemini).
    pub fn with_api_key(mut self, key: impl Into<String>) -> Self {
        self.api_key = Some(key.into());
        self
    }
}

/// Errors produced by the embeddings module.
#[derive(Debug, Error)]
pub enum EmbedError {
    #[error("io error: {0}")]
    Io(String),
    #[error("api error: {0}")]
    Api(String),
    #[error("parse error: {0}")]
    Parse(String),
}

#[cfg(feature = "providers")]
impl From<reqwest::Error> for EmbedError {
    fn from(err: reqwest::Error) -> Self {
        EmbedError::Api(err.to_string())
    }
}

/// Client for generating text embeddings via a remote or local provider.
///
/// The inner HTTP client only exists when the `providers` feature is
/// enabled. Without it, [`EmbeddingClient::embed`] returns a descriptive
/// error but the type itself remains constructible.
#[derive(Debug, Clone)]
pub struct EmbeddingClient {
    /// Provider + model configuration.
    pub config: EmbeddingConfig,
    #[cfg(feature = "providers")]
    client: reqwest::Client,
}

impl EmbeddingClient {
    /// Create a new client from a config.
    pub fn new(config: EmbeddingConfig) -> Self {
        Self {
            #[cfg(feature = "providers")]
            client: reqwest::Client::new(),
            config,
        }
    }

    /// Convenience constructor for a Gemini client with an API key.
    pub fn gemini(api_key: impl Into<String>) -> Self {
        Self::new(EmbeddingConfig::gemini().with_api_key(api_key))
    }

    /// Convenience constructor for a local Ollama client.
    pub fn ollama() -> Self {
        Self::new(EmbeddingConfig::ollama())
    }

    /// Embed a single text, returning its vector.
    pub async fn embed(&self, text: &str) -> Result<Vec<f32>, EmbedError> {
        #[cfg(feature = "providers")]
        {
            match self.config.provider {
                EmbeddingProvider::Gemini => self.embed_gemini(text).await,
                EmbeddingProvider::Ollama => self.embed_ollama(text).await,
            }
        }
        #[cfg(not(feature = "providers"))]
        {
            let _ = text;
            Err(EmbedError::Api("providers feature not enabled".into()))
        }
    }

    /// Embed multiple texts, returning one vector per input.
    pub async fn embed_batch(&self, texts: &[&str]) -> Result<Vec<Vec<f32>>, EmbedError> {
        let mut results = Vec::with_capacity(texts.len());
        for text in texts {
            results.push(self.embed(text).await?);
        }
        Ok(results)
    }

    #[cfg(feature = "providers")]
    async fn embed_gemini(&self, text: &str) -> Result<Vec<f32>, EmbedError> {
        let api_key = self
            .config
            .api_key
            .as_deref()
            .ok_or_else(|| EmbedError::Api("gemini provider requires an api key".into()))?;

        let url = "https://generativelanguage.googleapis.com/v1beta/models/text-embedding-004:embedContent";

        let request = serde_json::json!({
            "model": format!("models/{}", self.config.model),
            "content": {
                "parts": [{ "text": text }]
            }
        });

        let resp = self
            .client
            .post(url)
            .header("x-goog-api-key", api_key)
            .json(&request)
            .send()
            .await?
            .error_for_status()?;

        let result: GeminiResponse = resp.json().await?;
        Ok(result.embedding.values)
    }

    #[cfg(feature = "providers")]
    async fn embed_ollama(&self, text: &str) -> Result<Vec<f32>, EmbedError> {
        let url = "http://localhost:11434/api/embeddings";

        let request = serde_json::json!({
            "model": self.config.model,
            "prompt": text,
        });

        let resp = self
            .client
            .post(url)
            .json(&request)
            .send()
            .await?
            .error_for_status()?;

        let result: OllamaResponse = resp.json().await?;
        Ok(result.embedding)
    }
}

/// Cosine similarity between two vectors.
///
/// Returns `0.0` for empty vectors, mismatched lengths, or zero vectors
/// (which would otherwise divide by zero).
pub fn cosine_similarity(a: &[f32], b: &[f32]) -> f32 {
    if a.len() != b.len() || a.is_empty() {
        return 0.0;
    }

    let dot_product: f32 = a.iter().zip(b).map(|(x, y)| x * y).sum();
    let magnitude_a: f32 = a.iter().map(|x| x * x).sum::<f32>().sqrt();
    let magnitude_b: f32 = b.iter().map(|x| x * x).sum::<f32>().sqrt();

    if magnitude_a == 0.0 || magnitude_b == 0.0 {
        return 0.0;
    }

    dot_product / (magnitude_a * magnitude_b)
}

/// Semantic search over skills using cached embeddings.
///
/// Holds an [`EmbeddingClient`] plus a cache keyed by a hash of the input
/// text, so repeated queries or skill descriptions are not re-embedded.
#[derive(Debug, Clone)]
pub struct SemanticSearch {
    client: EmbeddingClient,
    cache: HashMap<u64, Vec<f32>>,
}

impl SemanticSearch {
    /// Create a new semantic search index backed by `client`.
    pub fn new(client: EmbeddingClient) -> Self {
        Self {
            client,
            cache: HashMap::new(),
        }
    }

    /// Embed `text`, using the cache when available.
    pub async fn embed_cached(&mut self, text: &str) -> Result<Vec<f32>, EmbedError> {
        let key = hash_text(text);
        if let Some(vec) = self.cache.get(&key) {
            return Ok(vec.clone());
        }
        let vec = self.client.embed(text).await?;
        self.cache.insert(key, vec.clone());
        Ok(vec)
    }

    /// Search `skills` for the ones most semantically similar to `query`.
    ///
    /// Returns up to `top_k` `(skill_id, similarity)` pairs sorted by
    /// similarity descending.
    pub async fn search_skills(
        &mut self,
        query: &str,
        skills: &[&Skill],
        top_k: usize,
    ) -> Result<Vec<(String, f32)>, EmbedError> {
        if skills.is_empty() || top_k == 0 {
            return Ok(Vec::new());
        }

        let query_vec = self.embed_cached(query).await?;

        let mut scored: Vec<(String, f32)> = Vec::with_capacity(skills.len());
        for skill in skills {
            let vec = self.embed_cached(&skill.description).await?;
            let sim = cosine_similarity(&query_vec, &vec);
            scored.push((skill.id.clone(), sim));
        }

        scored.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
        scored.truncate(top_k);
        Ok(scored)
    }

    /// Number of cached embeddings.
    pub fn cache_len(&self) -> usize {
        self.cache.len()
    }

    /// Clear the embedding cache.
    pub fn clear_cache(&mut self) {
        self.cache.clear();
    }
}

/// Stable, allocation-free string hash for cache keys (FNV-1a 64-bit).
fn hash_text(text: &str) -> u64 {
    let mut hash: u64 = 0xcbf29ce484222325;
    for byte in text.as_bytes() {
        hash ^= *byte as u64;
        hash = hash.wrapping_mul(0x100000001b3);
    }
    hash
}

#[cfg(feature = "providers")]
#[derive(Deserialize)]
struct GeminiResponse {
    embedding: GeminiEmbedding,
}

#[cfg(feature = "providers")]
#[derive(Deserialize)]
struct GeminiEmbedding {
    values: Vec<f32>,
}

#[cfg(feature = "providers")]
#[derive(Deserialize)]
struct OllamaResponse {
    embedding: Vec<f32>,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_cosine_similarity_basic() {
        // Identical vectors -> 1.0
        let a = vec![1.0, 0.0, 0.0];
        let b = vec![1.0, 0.0, 0.0];
        assert!((cosine_similarity(&a, &b) - 1.0).abs() < 1e-6);

        // Opposite vectors -> -1.0
        let a = vec![1.0, 0.0, 0.0];
        let b = vec![-1.0, 0.0, 0.0];
        assert!((cosine_similarity(&a, &b) - (-1.0)).abs() < 1e-6);

        // 45-degree angle -> ~0.7071
        let a = vec![1.0, 0.0];
        let b = vec![1.0, 1.0];
        let sim = cosine_similarity(&a, &b);
        assert!((sim - std::f32::consts::FRAC_1_SQRT_2).abs() < 1e-5);
    }

    #[test]
    fn test_cosine_similarity_edge_cases() {
        // Empty vectors
        let a: Vec<f32> = vec![];
        let b: Vec<f32> = vec![];
        assert!((cosine_similarity(&a, &b) - 0.0).abs() < 1e-6);

        // Mismatched lengths
        let a = vec![1.0];
        let b = vec![1.0, 0.0];
        assert!((cosine_similarity(&a, &b) - 0.0).abs() < 1e-6);

        // Zero vectors
        let a = vec![0.0, 0.0];
        let b = vec![0.0, 0.0];
        assert!((cosine_similarity(&a, &b) - 0.0).abs() < 1e-6);

        // One zero vector, one non-zero
        let a = vec![0.0, 0.0];
        let b = vec![1.0, 1.0];
        assert!((cosine_similarity(&a, &b) - 0.0).abs() < 1e-6);
    }

    #[test]
    fn test_cosine_similarity_orthogonal() {
        // Orthogonal vectors -> 0.0
        let a = vec![1.0, 0.0];
        let b = vec![0.0, 1.0];
        assert!((cosine_similarity(&a, &b) - 0.0).abs() < 1e-6);

        // Orthogonal in higher dimensions
        let a = vec![1.0, 0.0, 0.0, 0.0];
        let b = vec![0.0, 0.0, 0.0, 1.0];
        assert!((cosine_similarity(&a, &b) - 0.0).abs() < 1e-6);
    }

    #[test]
    fn test_config_defaults() {
        let g = EmbeddingConfig::gemini();
        assert_eq!(g.provider, EmbeddingProvider::Gemini);
        assert_eq!(g.model, "text-embedding-004");
        assert_eq!(g.dimension, 768);
        assert!(g.api_key.is_none());

        let o = EmbeddingConfig::ollama();
        assert_eq!(o.provider, EmbeddingProvider::Ollama);
        assert_eq!(o.model, "nomic-embed-text");
        assert_eq!(o.dimension, 768);

        let with_key = EmbeddingConfig::gemini().with_api_key("secret");
        assert_eq!(with_key.api_key.as_deref(), Some("secret"));
    }

    #[test]
    fn test_hash_text_stable() {
        assert_eq!(hash_text("hello"), hash_text("hello"));
        assert_ne!(hash_text("hello"), hash_text("world"));
    }

    #[cfg(not(feature = "providers"))]
    #[tokio::test]
    async fn test_embed_without_providers_errors() {
        let client = EmbeddingClient::new(EmbeddingConfig::gemini());
        let result = client.embed("test").await;
        assert!(matches!(result, Err(EmbedError::Api(_))));
    }
}