feat(viking): add local server management for privacy-first deployment
Backend (Rust): - viking_commands.rs: Tauri commands for server status/start/stop/restart - memory/mod.rs: Memory module exports - memory/context_builder.rs: Context building with memory injection - memory/extractor.rs: Memory extraction from conversations - llm/mod.rs: LLM integration for memory summarization Frontend (TypeScript): - context-builder.ts: Context building with OpenViking integration - viking-client.ts: OpenViking API client - viking-local.ts: Local storage fallback when Viking unavailable - viking-memory-adapter.ts: Memory extraction and persistence Features: - Multi-mode adapter (local/sidecar/remote) with auto-detection - Privacy-first: all data stored in ~/.openviking/, server only on 127.0.0.1 - Graceful degradation when local server unavailable - Context compaction with memory flush before compression Tests: 21 passing (viking-adapter.test.ts) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
506
desktop/src-tauri/src/memory/extractor.rs
Normal file
506
desktop/src-tauri/src/memory/extractor.rs
Normal file
@@ -0,0 +1,506 @@
|
||||
//! Session Memory Extractor
|
||||
//!
|
||||
//! Extracts structured memories from conversation sessions using LLM analysis.
|
||||
//! This supplements OpenViking CLI which lacks built-in memory extraction.
|
||||
//!
|
||||
//! Categories:
|
||||
//! - user_preference: User's stated preferences and settings
|
||||
//! - user_fact: Facts about the user (name, role, projects, etc.)
|
||||
//! - agent_lesson: Lessons learned by the agent from interactions
|
||||
//! - agent_pattern: Recurring patterns the agent should remember
|
||||
//! - task: Task-related information for follow-up
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
|
||||
// === Types ===
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum MemoryCategory {
|
||||
UserPreference,
|
||||
UserFact,
|
||||
AgentLesson,
|
||||
AgentPattern,
|
||||
Task,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct ExtractedMemory {
|
||||
pub category: MemoryCategory,
|
||||
pub content: String,
|
||||
pub tags: Vec<String>,
|
||||
pub importance: u8, // 1-10 scale
|
||||
pub suggested_uri: String,
|
||||
pub reasoning: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct ExtractionResult {
|
||||
pub memories: Vec<ExtractedMemory>,
|
||||
pub summary: String,
|
||||
pub tokens_saved: Option<u32>,
|
||||
pub extraction_time_ms: u64,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ExtractionConfig {
|
||||
/// Maximum memories to extract per session
|
||||
pub max_memories: usize,
|
||||
/// Minimum importance threshold (1-10)
|
||||
pub min_importance: u8,
|
||||
/// Whether to include reasoning in output
|
||||
pub include_reasoning: bool,
|
||||
/// Agent ID for URI generation
|
||||
pub agent_id: String,
|
||||
}
|
||||
|
||||
impl Default for ExtractionConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
max_memories: 10,
|
||||
min_importance: 5,
|
||||
include_reasoning: true,
|
||||
agent_id: "zclaw-main".to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ChatMessage {
|
||||
pub role: String,
|
||||
pub content: String,
|
||||
pub timestamp: Option<String>,
|
||||
}
|
||||
|
||||
// === Session Extractor ===
|
||||
|
||||
pub struct SessionExtractor {
|
||||
config: ExtractionConfig,
|
||||
llm_endpoint: Option<String>,
|
||||
api_key: Option<String>,
|
||||
}
|
||||
|
||||
impl SessionExtractor {
|
||||
pub fn new(config: ExtractionConfig) -> Self {
|
||||
Self {
|
||||
config,
|
||||
llm_endpoint: None,
|
||||
api_key: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Configure LLM endpoint for extraction
|
||||
pub fn with_llm(mut self, endpoint: String, api_key: String) -> Self {
|
||||
self.llm_endpoint = Some(endpoint);
|
||||
self.api_key = Some(api_key);
|
||||
self
|
||||
}
|
||||
|
||||
/// Extract memories from a conversation session
|
||||
pub async fn extract(&self, messages: &[ChatMessage]) -> Result<ExtractionResult, String> {
|
||||
let start_time = std::time::Instant::now();
|
||||
|
||||
// Build extraction prompt
|
||||
let prompt = self.build_extraction_prompt(messages);
|
||||
|
||||
// Call LLM for extraction
|
||||
let response = self.call_llm(&prompt).await?;
|
||||
|
||||
// Parse LLM response into structured memories
|
||||
let memories = self.parse_extraction(&response)?;
|
||||
|
||||
// Filter by importance and limit
|
||||
let filtered: Vec<ExtractedMemory> = memories
|
||||
.into_iter()
|
||||
.filter(|m| m.importance >= self.config.min_importance)
|
||||
.take(self.config.max_memories)
|
||||
.collect();
|
||||
|
||||
// Generate session summary
|
||||
let summary = self.generate_summary(&filtered);
|
||||
|
||||
let elapsed = start_time.elapsed().as_millis() as u64;
|
||||
|
||||
Ok(ExtractionResult {
|
||||
tokens_saved: Some(self.estimate_tokens_saved(messages, &summary)),
|
||||
memories: filtered,
|
||||
summary,
|
||||
extraction_time_ms: elapsed,
|
||||
})
|
||||
}
|
||||
|
||||
/// Build the extraction prompt for the LLM
|
||||
fn build_extraction_prompt(&self, messages: &[ChatMessage]) -> String {
|
||||
let conversation = messages
|
||||
.iter()
|
||||
.map(|m| format!("[{}]: {}", m.role, m.content))
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n\n");
|
||||
|
||||
format!(
|
||||
r#"Analyze the following conversation and extract structured memories.
|
||||
Focus on information that would be useful for future interactions.
|
||||
|
||||
## Conversation
|
||||
{}
|
||||
|
||||
## Extraction Instructions
|
||||
Extract memories in these categories:
|
||||
- user_preference: User's stated preferences (UI preferences, workflow preferences, tool choices)
|
||||
- user_fact: Facts about the user (name, role, projects, skills, constraints)
|
||||
- agent_lesson: Lessons the agent learned (what worked, what didn't, corrections needed)
|
||||
- agent_pattern: Recurring patterns to remember (common workflows, frequent requests)
|
||||
- task: Tasks or follow-ups mentioned (todos, pending work, deadlines)
|
||||
|
||||
For each memory, provide:
|
||||
1. category: One of the above categories
|
||||
2. content: The actual memory content (concise, actionable)
|
||||
3. tags: 2-5 relevant tags for retrieval
|
||||
4. importance: 1-10 scale (10 = critical, 1 = trivial)
|
||||
5. reasoning: Brief explanation of why this is worth remembering
|
||||
|
||||
Output as JSON array:
|
||||
```json
|
||||
[
|
||||
{{
|
||||
"category": "user_preference",
|
||||
"content": "...",
|
||||
"tags": ["tag1", "tag2"],
|
||||
"importance": 7,
|
||||
"reasoning": "..."
|
||||
}}
|
||||
]
|
||||
```
|
||||
|
||||
If no significant memories found, return empty array: []"#,
|
||||
conversation
|
||||
)
|
||||
}
|
||||
|
||||
/// Call LLM for extraction
|
||||
async fn call_llm(&self, prompt: &str) -> Result<String, String> {
|
||||
// If LLM endpoint is configured, use it
|
||||
if let (Some(endpoint), Some(api_key)) = (&self.llm_endpoint, &self.api_key) {
|
||||
return self.call_llm_api(endpoint, api_key, prompt).await;
|
||||
}
|
||||
|
||||
// Otherwise, use rule-based extraction as fallback
|
||||
self.rule_based_extraction(prompt)
|
||||
}
|
||||
|
||||
/// Call external LLM API (doubao, OpenAI, etc.)
|
||||
async fn call_llm_api(
|
||||
&self,
|
||||
endpoint: &str,
|
||||
api_key: &str,
|
||||
prompt: &str,
|
||||
) -> Result<String, String> {
|
||||
let client = reqwest::Client::new();
|
||||
|
||||
let response = client
|
||||
.post(endpoint)
|
||||
.header("Authorization", format!("Bearer {}", api_key))
|
||||
.header("Content-Type", "application/json")
|
||||
.json(&serde_json::json!({
|
||||
"model": "doubao-pro-32k",
|
||||
"messages": [
|
||||
{"role": "user", "content": prompt}
|
||||
],
|
||||
"temperature": 0.3,
|
||||
"max_tokens": 2000
|
||||
}))
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| format!("LLM API request failed: {}", e))?;
|
||||
|
||||
if !response.status().is_success() {
|
||||
return Err(format!("LLM API error: {}", response.status()));
|
||||
}
|
||||
|
||||
let json: serde_json::Value = response
|
||||
.json()
|
||||
.await
|
||||
.map_err(|e| format!("Failed to parse LLM response: {}", e))?;
|
||||
|
||||
// Extract content from response (adjust based on API format)
|
||||
let content = json
|
||||
.get("choices")
|
||||
.and_then(|c| c.get(0))
|
||||
.and_then(|c| c.get("message"))
|
||||
.and_then(|m| m.get("content"))
|
||||
.and_then(|c| c.as_str())
|
||||
.ok_or("Invalid LLM response format")?
|
||||
.to_string();
|
||||
|
||||
Ok(content)
|
||||
}
|
||||
|
||||
/// Rule-based extraction as fallback when LLM is not available
|
||||
fn rule_based_extraction(&self, prompt: &str) -> Result<String, String> {
|
||||
// Simple pattern matching for common memory patterns
|
||||
let mut memories: Vec<ExtractedMemory> = Vec::new();
|
||||
|
||||
// Pattern: User preferences
|
||||
let pref_patterns = [
|
||||
(r"I prefer (.+)", "user_preference"),
|
||||
(r"My preference is (.+)", "user_preference"),
|
||||
(r"I like (.+)", "user_preference"),
|
||||
(r"I don't like (.+)", "user_preference"),
|
||||
];
|
||||
|
||||
// Pattern: User facts
|
||||
let fact_patterns = [
|
||||
(r"My name is (.+)", "user_fact"),
|
||||
(r"I work on (.+)", "user_fact"),
|
||||
(r"I'm a (.+)", "user_fact"),
|
||||
(r"My project is (.+)", "user_fact"),
|
||||
];
|
||||
|
||||
// Extract using regex (simplified implementation)
|
||||
for (pattern, category) in pref_patterns.iter().chain(fact_patterns.iter()) {
|
||||
if let Ok(re) = regex::Regex::new(pattern) {
|
||||
for cap in re.captures_iter(prompt) {
|
||||
if let Some(content) = cap.get(1) {
|
||||
let memory = ExtractedMemory {
|
||||
category: if *category == "user_preference" {
|
||||
MemoryCategory::UserPreference
|
||||
} else {
|
||||
MemoryCategory::UserFact
|
||||
},
|
||||
content: content.as_str().to_string(),
|
||||
tags: vec!["auto-extracted".to_string()],
|
||||
importance: 6,
|
||||
suggested_uri: format!(
|
||||
"viking://user/memories/{}/{}",
|
||||
category,
|
||||
chrono::Utc::now().timestamp_millis()
|
||||
),
|
||||
reasoning: Some("Extracted via rule-based pattern matching".to_string()),
|
||||
};
|
||||
memories.push(memory);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Return as JSON
|
||||
serde_json::to_string_pretty(&memories)
|
||||
.map_err(|e| format!("Failed to serialize memories: {}", e))
|
||||
}
|
||||
|
||||
/// Parse LLM response into structured memories
|
||||
fn parse_extraction(&self, response: &str) -> Result<Vec<ExtractedMemory>, String> {
|
||||
// Try to extract JSON from the response
|
||||
let json_start = response.find('[').unwrap_or(0);
|
||||
let json_end = response.rfind(']').map(|i| i + 1).unwrap_or(response.len());
|
||||
let json_str = &response[json_start..json_end];
|
||||
|
||||
// Parse JSON
|
||||
let raw_memories: Vec<serde_json::Value> = serde_json::from_str(json_str)
|
||||
.unwrap_or_default();
|
||||
|
||||
let memories: Vec<ExtractedMemory> = raw_memories
|
||||
.into_iter()
|
||||
.filter_map(|m| self.parse_memory(&m))
|
||||
.collect();
|
||||
|
||||
Ok(memories)
|
||||
}
|
||||
|
||||
/// Parse a single memory from JSON
|
||||
fn parse_memory(&self, value: &serde_json::Value) -> Option<ExtractedMemory> {
|
||||
let category_str = value.get("category")?.as_str()?;
|
||||
let category = match category_str {
|
||||
"user_preference" => MemoryCategory::UserPreference,
|
||||
"user_fact" => MemoryCategory::UserFact,
|
||||
"agent_lesson" => MemoryCategory::AgentLesson,
|
||||
"agent_pattern" => MemoryCategory::AgentPattern,
|
||||
"task" => MemoryCategory::Task,
|
||||
_ => return None,
|
||||
};
|
||||
|
||||
let content = value.get("content")?.as_str()?.to_string();
|
||||
let tags = value
|
||||
.get("tags")
|
||||
.and_then(|t| t.as_array())
|
||||
.map(|arr| {
|
||||
arr.iter()
|
||||
.filter_map(|v| v.as_str().map(String::from))
|
||||
.collect()
|
||||
})
|
||||
.unwrap_or_default();
|
||||
|
||||
let importance = value
|
||||
.get("importance")
|
||||
.and_then(|v| v.as_u64())
|
||||
.unwrap_or(5) as u8;
|
||||
|
||||
let reasoning = value
|
||||
.get("reasoning")
|
||||
.and_then(|v| v.as_str())
|
||||
.map(String::from);
|
||||
|
||||
// Generate URI based on category
|
||||
let suggested_uri = self.generate_uri(&category, &content);
|
||||
|
||||
Some(ExtractedMemory {
|
||||
category,
|
||||
content,
|
||||
tags,
|
||||
importance,
|
||||
suggested_uri,
|
||||
reasoning,
|
||||
})
|
||||
}
|
||||
|
||||
/// Generate a URI for the memory
|
||||
fn generate_uri(&self, category: &MemoryCategory, content: &str) -> String {
|
||||
let timestamp = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.map(|d| d.as_millis())
|
||||
.unwrap_or(0);
|
||||
|
||||
let content_hash = &content[..content.len().min(20)]
|
||||
.to_lowercase()
|
||||
.replace(' ', "_")
|
||||
.replace(|c: char| !c.is_alphanumeric() && c != '_', "");
|
||||
|
||||
match category {
|
||||
MemoryCategory::UserPreference => {
|
||||
format!("viking://user/memories/preferences/{}_{}", content_hash, timestamp)
|
||||
}
|
||||
MemoryCategory::UserFact => {
|
||||
format!("viking://user/memories/facts/{}_{}", content_hash, timestamp)
|
||||
}
|
||||
MemoryCategory::AgentLesson => {
|
||||
format!(
|
||||
"viking://agent/{}/memories/lessons/{}_{}",
|
||||
self.config.agent_id, content_hash, timestamp
|
||||
)
|
||||
}
|
||||
MemoryCategory::AgentPattern => {
|
||||
format!(
|
||||
"viking://agent/{}/memories/patterns/{}_{}",
|
||||
self.config.agent_id, content_hash, timestamp
|
||||
)
|
||||
}
|
||||
MemoryCategory::Task => {
|
||||
format!(
|
||||
"viking://agent/{}/tasks/{}_{}",
|
||||
self.config.agent_id, content_hash, timestamp
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Generate a summary of extracted memories
|
||||
fn generate_summary(&self, memories: &[ExtractedMemory]) -> String {
|
||||
if memories.is_empty() {
|
||||
return "No significant memories extracted from this session.".to_string();
|
||||
}
|
||||
|
||||
let mut summary_parts = Vec::new();
|
||||
|
||||
let user_prefs = memories
|
||||
.iter()
|
||||
.filter(|m| matches!(m.category, MemoryCategory::UserPreference))
|
||||
.count();
|
||||
if user_prefs > 0 {
|
||||
summary_parts.push(format!("{} user preferences", user_prefs));
|
||||
}
|
||||
|
||||
let user_facts = memories
|
||||
.iter()
|
||||
.filter(|m| matches!(m.category, MemoryCategory::UserFact))
|
||||
.count();
|
||||
if user_facts > 0 {
|
||||
summary_parts.push(format!("{} user facts", user_facts));
|
||||
}
|
||||
|
||||
let lessons = memories
|
||||
.iter()
|
||||
.filter(|m| matches!(m.category, MemoryCategory::AgentLesson))
|
||||
.count();
|
||||
if lessons > 0 {
|
||||
summary_parts.push(format!("{} agent lessons", lessons));
|
||||
}
|
||||
|
||||
let patterns = memories
|
||||
.iter()
|
||||
.filter(|m| matches!(m.category, MemoryCategory::AgentPattern))
|
||||
.count();
|
||||
if patterns > 0 {
|
||||
summary_parts.push(format!("{} patterns", patterns));
|
||||
}
|
||||
|
||||
let tasks = memories
|
||||
.iter()
|
||||
.filter(|m| matches!(m.category, MemoryCategory::Task))
|
||||
.count();
|
||||
if tasks > 0 {
|
||||
summary_parts.push(format!("{} tasks", tasks));
|
||||
}
|
||||
|
||||
format!(
|
||||
"Extracted {} memories: {}.",
|
||||
memories.len(),
|
||||
summary_parts.join(", ")
|
||||
)
|
||||
}
|
||||
|
||||
/// Estimate tokens saved by extraction
|
||||
fn estimate_tokens_saved(&self, messages: &[ChatMessage], summary: &str) -> u32 {
|
||||
// Rough estimation: original messages vs summary
|
||||
let original_tokens: u32 = messages
|
||||
.iter()
|
||||
.map(|m| (m.content.len() as f32 * 0.4) as u32)
|
||||
.sum();
|
||||
|
||||
let summary_tokens = (summary.len() as f32 * 0.4) as u32;
|
||||
|
||||
original_tokens.saturating_sub(summary_tokens)
|
||||
}
|
||||
}
|
||||
|
||||
// === Tauri Commands ===
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn extract_session_memories(
|
||||
messages: Vec<ChatMessage>,
|
||||
agent_id: String,
|
||||
) -> Result<ExtractionResult, String> {
|
||||
let config = ExtractionConfig {
|
||||
agent_id,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let extractor = SessionExtractor::new(config);
|
||||
extractor.extract(&messages).await
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_extraction_config_default() {
|
||||
let config = ExtractionConfig::default();
|
||||
assert_eq!(config.max_memories, 10);
|
||||
assert_eq!(config.min_importance, 5);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_uri_generation() {
|
||||
let config = ExtractionConfig::default();
|
||||
let extractor = SessionExtractor::new(config);
|
||||
|
||||
let uri = extractor.generate_uri(
|
||||
&MemoryCategory::UserPreference,
|
||||
"dark mode enabled"
|
||||
);
|
||||
assert!(uri.starts_with("viking://user/memories/preferences/"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user