YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context
Rust agentsagiaiai-memoryaudio-recordingcomputer-visionhermeshermes-agentllmlocal-ailocal-firstmachine-learningmcpmultimodalopenclawprivacyrewindscreen-recordingspeech-to-textycombinator
feat: implement api plugin
Louis Beaumont629d0b240cf3
diff --git a/screenpipe-server/Cargo.toml b/screenpipe-server/Cargo.tomlindex 2b2f266c9..d90461ab1 100644--- a/screenpipe-server/Cargo.toml+++ b/screenpipe-server/Cargo.toml@@ -73,6 +73,10 @@ colored = "2.0"# Tracingtracing = { workspace = true }+# Plugins+tower = "0.4"+futures = "0.3.17"+[dev-dependencies]tempfile = "3.3.0"@@ -82,7 +86,6 @@ reqwest = { version = "0.11", features = ["json"] }criterion = { workspace = true }rand = "0.8"-tower = "0.4"[[bench]]name = "db_benchmarks"diff --git a/screenpipe-server/src/bin/screenpipe-server.rs b/screenpipe-server/src/bin/screenpipe-server.rsindex 17e8091f7..a7d47720b 100644--- a/screenpipe-server/src/bin/screenpipe-server.rs+++ b/screenpipe-server/src/bin/screenpipe-server.rs@@ -36,7 +36,7 @@ struct Cli {/// FPS for continuous recording/// 1 FPS = 30 GB / month/// 5 FPS = 150 GB / month- /// Optimise based on your needs.+ /// Optimise based on your needs./// You rarely change change more than 1 times within a second, right?#[arg(short, long, default_value_t = 1.0)]fps: f64,@@ -267,15 +267,21 @@ async fn main() -> anyhow::Result<()> {});tokio::spawn(async move {+ let api_plugin = |req: &axum::http::Request<axum::body::Body>| {+ // Custom plugin logic here+ // For example, using PostHog for tracking:+ if req.uri().path() == "/search" {+ // Track search requests+ // posthog.capture("search_request", {...})+ }+ };let server = Server::new(db_server,SocketAddr::from(([0, 0, 0, 0], cli.port)),vision_control_server_clone,audio_devices_control_sender_server,);- // TODO- // server.start(HashMap::new()).await.unwrap();- server.start(devices_status).await.unwrap();+ server.start(devices_status, api_plugin).await.unwrap();});// Wait for the server to startdiff --git a/screenpipe-server/src/lib.rs b/screenpipe-server/src/lib.rsindex 4225c8df2..607c411d3 100644--- a/screenpipe-server/src/lib.rs+++ b/screenpipe-server/src/lib.rs@@ -1,5 +1,6 @@mod core;mod db;+mod plugin;mod resource_monitor;mod server;mod video;diff --git a/screenpipe-server/src/plugin.rs b/screenpipe-server/src/plugin.rsnew file mode 100644index 000000000..8d0012e6b--- /dev/null+++ b/screenpipe-server/src/plugin.rs@@ -0,0 +1,60 @@+use futures::future::BoxFuture;+use std::task::{Context, Poll};+use tower::{Layer, Service};++#[derive(Clone)]+pub struct ApiPluginLayer<F> {+ plugin: F,+}++impl<F> ApiPluginLayer<F>+where+ F: Clone,+{+ pub fn new(plugin: F) -> Self {+ Self { plugin }+ }+}++impl<S, F> Layer<S> for ApiPluginLayer<F>+where+ F: Clone + Send + Sync + 'static,+{+ type Service = ApiPluginService<S, F>;++ fn layer(&self, service: S) -> <Self as Layer<S>>::Service {+ ApiPluginService {+ inner: service,+ plugin: self.plugin.clone(),+ }+ }+}++#[derive(Clone)]+pub struct ApiPluginService<S, F> {+ inner: S,+ plugin: F,+}++impl<S, F, R> Service<R> for ApiPluginService<S, F>+where+ S: Service<R> + Clone + Send + 'static,+ F: Fn(&R) + Clone + Send + Sync + 'static,+ R: Send + 'static,+ S::Future: Send,+{+ type Response = S::Response;+ type Error = S::Error;+ type Future = BoxFuture<'static, Result<Self::Response, Self::Error>>;++ fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {+ self.inner.poll_ready(cx)+ }++ fn call(&mut self, request: R) -> Self::Future {+ let plugin = self.plugin.clone();+ let mut inner = self.inner.clone();+ plugin(&request);+ Box::pin(async move { inner.call(request).await })+ }+}diff --git a/screenpipe-server/src/server.rs b/screenpipe-server/src/server.rsindex 19c368fd1..729290679 100644--- a/screenpipe-server/src/server.rs+++ b/screenpipe-server/src/server.rs@@ -29,6 +29,8 @@ use tower_http::{LatencyUnit,};+use crate::plugin::ApiPluginLayer;+// App statepub(crate) struct AppState {db: Arc<DatabaseManager>,@@ -370,10 +372,14 @@ impl Server {}}- pub async fn start(+ pub async fn start<F>(self,device_status: HashMap<AudioDevice, DeviceControl>,- ) -> Result<(), std::io::Error> {+ api_plugin: F,+ ) -> Result<(), std::io::Error>+ where+ F: Fn(&axum::http::Request<axum::body::Body>) + Clone + Send + Sync + 'static,+ {// TODO could init w audio deviceslet app_state = Arc::new(AppState {db: self.db,@@ -392,6 +398,7 @@ impl Server {.route("/vision/start", post(start_recording)).route("/vision/stop", post(stop_recording)).route("/vision/status", get(get_recording_status))+ .layer(ApiPluginLayer::new(api_plugin)).layer(CorsLayer::permissive()).layer(// https://github.com/tokio-rs/axum/blob/main/examples/tracing-aka-logging/src/main.rs@@ -468,3 +475,45 @@ impl Server {// # 12. Get recording status// # curl "http://localhost:3030/vision/status"++/*++echo "Listing audio devices:"+curl "http://localhost:3030/audio/list" | jq++echo "Starting vision recording:"+curl -X POST "http://localhost:3030/vision/start" | jq++echo "Stopping all audio devices:"+DEVICES=$(curl "http://localhost:3030/audio/list" | jq -r '.[].id')+echo "$DEVICES" | while IFS= read -r DEVICE; do+ echo "Stopping device: $DEVICE"+ curl -X POST "http://localhost:3030/audio/stop" -H "Content-Type: application/json" -d "{\"device_id\": \"$DEVICE\"}" | jq+done++echo "Checking statuses:"+curl "http://localhost:3030/vision/status" | jq+echo "$DEVICES" | while IFS= read -r DEVICE; do+ echo "Checking status of device: $DEVICE"+ curl -X POST "http://localhost:3030/audio/status" -H "Content-Type: application/json" -d "{\"device_id\": \"$DEVICE\"}" | jq+done++echo "Stopping vision recording:"+curl -X POST "http://localhost:3030/vision/stop" | jq++echo "Checking statuses again:"+curl "http://localhost:3030/vision/status" | jq+curl -X POST "http://localhost:3030/audio/status" -H "Content-Type: application/json" -d "{\"device_id\": \"$DEVICE\"}" | jq++echo "Stopping audio device:"+curl -X POST "http://localhost:3030/audio/stop" -H "Content-Type: application/json" -d "{\"device_id\": \"$DEVICE\"}" | jq++echo "Final status check:"+curl "http://localhost:3030/vision/status" | jq+curl -X POST "http://localhost:3030/audio/status" -H "Content-Type: application/json" -d "{\"device_id\": \"$DEVICE\"}" | jq++echo "Searching for content:"+curl "http://localhost:3030/search?q=test&limit=5&offset=0&content_type=all" | jq+++*/