Skip to main content

devsper_core/
traits.rs

1use crate::types::{BusMessage, LlmRequest, LlmResponse, ToolCall, ToolDef, ToolResult};
2use anyhow::Result;
3use serde_json::Value;
4
5/// A search result from semantic memory
6#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
7pub struct MemoryHit {
8    pub key: String,
9    pub value: Value,
10    pub score: f32,
11}
12
13/// Trait for LLM providers (Anthropic, OpenAI, Ollama, etc.)
14#[async_trait::async_trait]
15pub trait LlmProvider: Send + Sync {
16    /// Generate a response (non-streaming)
17    async fn generate(&self, req: LlmRequest) -> Result<LlmResponse>;
18
19    /// Provider name for routing/logging
20    fn name(&self) -> &str;
21
22    /// Models supported by this provider (prefix matching)
23    fn supports_model(&self, model: &str) -> bool;
24}
25
26/// Trait for message bus backends
27#[async_trait::async_trait]
28pub trait Bus: Send + Sync {
29    /// Publish a message to a topic
30    async fn publish(&self, msg: BusMessage) -> Result<()>;
31
32    /// Subscribe to a topic with a handler
33    async fn subscribe(
34        &self,
35        topic: &str,
36        handler: Box<dyn Fn(BusMessage) -> std::pin::Pin<Box<dyn std::future::Future<Output = ()> + Send>> + Send + Sync>,
37    ) -> Result<()>;
38
39    /// Start the bus
40    async fn start(&self) -> Result<()>;
41
42    /// Stop the bus
43    async fn stop(&self) -> Result<()>;
44}
45
46/// Trait for memory storage backends
47#[async_trait::async_trait]
48pub trait MemoryStore: Send + Sync {
49    /// Store a memory fact
50    async fn store(&self, namespace: &str, key: &str, value: Value) -> Result<()>;
51
52    /// Retrieve a memory fact
53    async fn retrieve(&self, namespace: &str, key: &str) -> Result<Option<Value>>;
54
55    /// Semantic search over stored memories
56    async fn search(&self, namespace: &str, query: &str, top_k: usize) -> Result<Vec<MemoryHit>>;
57
58    /// Delete a memory fact
59    async fn delete(&self, namespace: &str, key: &str) -> Result<()>;
60}
61
62/// Trait for tool executors (Lua plugins, external processes)
63#[async_trait::async_trait]
64pub trait ToolExecutor: Send + Sync {
65    /// Execute a tool call
66    async fn execute(&self, call: ToolCall) -> Result<ToolResult>;
67
68    /// List available tools
69    fn list_tools(&self) -> Vec<ToolDef>;
70}