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 deduplication SDK + embeddings endpoint
Louis Beaumont0afb51313c57
diff --git a/screenpipe-js/browser-sdk/src/index.ts b/screenpipe-js/browser-sdk/src/index.tsindex 65f931ea7..5cf425a19 100644--- a/screenpipe-js/browser-sdk/src/index.ts+++ b/screenpipe-js/browser-sdk/src/index.ts@@ -11,7 +11,18 @@ import type {VisionStreamResponse,} from "../../common/types";import { toSnakeCase, convertToCamelCase } from "../../common/utils";-import { captureEvent, captureMainFeatureEvent } from "../../common/analytics";+import {+ captureEvent,+ captureMainFeatureEvent,+ setAnalyticsClient,+} from "../../common/analytics";+import posthog from "posthog-js";++setAnalyticsClient({+ init: posthog.init.bind(posthog),+ identify: posthog.identify.bind(posthog),+ capture: posthog.capture.bind(posthog),+});import { PipesManager } from "../../common/PipesManager";type Result<T> = { success: true; data: T } | { success: false; error: any };@@ -29,7 +40,10 @@ async function* wsEvents(let ws = includeImages ? wsWithImages : wsWithoutImages;if (!ws || ws.readyState === WebSocket.CLOSED) {- console.log("creating new websocket connection, includeImages:", includeImages)+ console.log(+ "creating new websocket connection, includeImages:",+ includeImages+ );ws = new WebSocket(`${WS_URL}?images=${includeImages}`);if (includeImages) {wsWithImages = ws;@@ -40,11 +54,11 @@ async function* wsEvents(// Wait for connection to establishawait new Promise((resolve, reject) => {const onOpen = () => {- console.log("websocket connected")+ console.log("websocket connected");resolve(undefined);};const onError = (error: Event) => {- console.error("websocket connection error:", error)+ console.error("websocket connection error:", error);reject(error);};ws!.addEventListener("open", onOpen, { once: true });@@ -76,7 +90,7 @@ async function* wsEvents(resolveMessage = resolve;}});-+yield JSON.parse(message.data);}} finally {@@ -138,12 +152,12 @@ export interface BrowserPipe {list: () => Promise<Result<string[]>>;enable: (pipeId: string) => Promise<boolean>;disable: (pipeId: string) => Promise<boolean>;- delete: (pipeId: string,) => Promise<boolean>;+ delete: (pipeId: string) => Promise<boolean>;download: (url: string) => Promise<Result<Record<string, any>>>;info: (pipeId: string) => Promise<Result<Record<string, any>>>;update: (pipeId: string,- config: { [key: string]: string },+ config: { [key: string]: string }) => Promise<boolean>;downloadPrivate: (url: string,@@ -151,6 +165,10 @@ export interface BrowserPipe {pipeId: string) => Promise<Result<Record<string, any>>>;};+ deduplicateText(texts: string[]): Promise<{+ groups: { text: string; similar: string[] }[];+ error?: string;+ }>;}class BrowserPipeImpl implements BrowserPipe {@@ -310,12 +328,12 @@ class BrowserPipeImpl implements BrowserPipe {list: () => Promise<Result<string[]>>;enable: (pipeId: string) => Promise<boolean>;disable: (pipeId: string) => Promise<boolean>;- delete: (pipeId: string,) => Promise<boolean>;+ delete: (pipeId: string) => Promise<boolean>;download: (url: string) => Promise<Result<Record<string, any>>>;info: (pipeId: string) => Promise<Result<Record<string, any>>>;update: (pipeId: string,- config: { [key: string]: string },+ config: { [key: string]: string }) => Promise<boolean>;downloadPrivate: (url: string,@@ -332,7 +350,7 @@ class BrowserPipeImpl implements BrowserPipe {if (!response.ok) {throw new Error(`http error! status: ${response.status}`);- }+ }const data = await response.json();return { success: true, data: data.data };@@ -353,7 +371,7 @@ class BrowserPipeImpl implements BrowserPipe {if (!response.ok) {throw new Error(`http error! status: ${response.status}`);- }+ }const data: Record<string, any> = await response.json();return { success: true, data: data.data };@@ -396,8 +414,8 @@ class BrowserPipeImpl implements BrowserPipe {},update: async (pipeId: string,- config: { [key: string]: string- }): Promise<boolean> => {+ config: { [key: string]: string }+ ): Promise<boolean> => {try {const response = await fetch(`http://localhost:3030/pipes/update`, {method: "POST",@@ -406,7 +424,7 @@ class BrowserPipeImpl implements BrowserPipe {pipe_id: pipeId,config,}),- });+ });return response.ok;} catch (error) {@@ -416,10 +434,13 @@ class BrowserPipeImpl implements BrowserPipe {},info: async (pipeId: string): Promise<Result<Record<string, any>>> => {try {- const response = await fetch(`http://localhost:3030/pipes/info/${pipeId}`, {- method: "GET",- headers: { "Content-Type": "application/json" },- });+ const response = await fetch(+ `http://localhost:3030/pipes/info/${pipeId}`,+ {+ method: "GET",+ headers: { "Content-Type": "application/json" },+ }+ );if (!response.ok) {throw new Error(`http error! status: ${response.status}`);@@ -438,7 +459,8 @@ class BrowserPipeImpl implements BrowserPipe {pipeId: string): Promise<Result<Record<string, any>>> => {try {- const apiUrl = process.env.SCREENPIPE_SERVER_URL || "http://localhost:3030";+ const apiUrl =+ process.env.SCREENPIPE_SERVER_URL || "http://localhost:3030";const response = await fetch(`${apiUrl}/pipes/download-private`, {method: "POST",headers: { "Content-Type": "application/json" },@@ -462,7 +484,8 @@ class BrowserPipeImpl implements BrowserPipe {},delete: async (pipeId: string): Promise<boolean> => {try {- const apiUrl = process.env.SCREENPIPE_SERVER_URL || "http://localhost:3030";+ const apiUrl =+ process.env.SCREENPIPE_SERVER_URL || "http://localhost:3030";const response = await fetch(`${apiUrl}/pipes/delete`, {method: "POST",headers: { "Content-Type": "application/json" },@@ -476,7 +499,7 @@ class BrowserPipeImpl implements BrowserPipe {console.error("failed to delete pipe:", error);return false;}- }+ },};async *streamTranscriptions(): AsyncGenerator<@@ -570,6 +593,78 @@ class BrowserPipeImpl implements BrowserPipe {wsWithoutImages = null;}}++ async deduplicateText(texts: string[]): Promise<{+ groups: { text: string; similar: string[] }[];+ error?: string;+ }> {+ // Early return for empty arrays+ if (texts.length === 0) {+ return { groups: [], error: undefined };+ }++ try {+ const response = await fetch("http://localhost:3035/v1/embeddings", {+ method: "POST",+ headers: { "Content-Type": "application/json" },+ body: JSON.stringify({+ model: "all-MiniLM-L6-v2",+ // Send as Multiple variant even for single strings to be consistent+ input: texts.length === 1 ? texts[0] : texts,+ encoding_format: "float",+ }),+ });++ if (!response.ok) {+ const errorText = await response.text();+ throw new Error(+ `http error! status: ${response.status}, error: ${errorText}`+ );+ }++ const data = await response.json();+ const embeddings = data.data.map((d: any) => d.embedding);++ // group similar texts using cosine similarity+ const similarityThreshold = 0.9;+ const groups: { text: string; similar: string[] }[] = [];+ const used = new Set<number>();++ for (let i = 0; i < texts.length; i++) {+ if (used.has(i)) continue;++ const group = { text: texts[i], similar: [] as string[] };+ used.add(i);++ for (let j = i + 1; j < texts.length; j++) {+ if (used.has(j)) continue;++ const similarity = cosineSimilarity(embeddings[i], embeddings[j]);+ if (similarity > similarityThreshold) {+ group.similar.push(texts[j]);+ used.add(j);+ }+ }++ if (group.similar.length > 0) {+ groups.push(group);+ }+ }++ return { groups };+ } catch (error) {+ console.error("failed to deduplicate texts:", error);+ return { groups: [], error: error?.toString() };+ }+ }+}++// Helper function to compute cosine similarity between two vectors+function cosineSimilarity(a: number[], b: number[]): number {+ const dotProduct = a.reduce((sum, val, i) => sum + val * b[i], 0);+ const normA = Math.sqrt(a.reduce((sum, val) => sum + val * val, 0));+ const normB = Math.sqrt(b.reduce((sum, val) => sum + val * val, 0));+ return dotProduct / (normA * normB);}const pipeImpl = new BrowserPipeImpl();diff --git a/screenpipe-js/browser-sdk/tests/deduplicateText.test.ts b/screenpipe-js/browser-sdk/tests/deduplicateText.test.tsnew file mode 100644index 000000000..579523777--- /dev/null+++ b/screenpipe-js/browser-sdk/tests/deduplicateText.test.ts@@ -0,0 +1,44 @@+import { describe, expect, test } from "bun:test";+import { pipe } from "../src/index";++describe("deduplicateText", () => {+ test("should group similar texts correctly", async () => {+ const texts = [+ "hello world",+ "hello world!",+ "completely different text",+ "hello there world",+ "unique text here",+ ];++ const result = await pipe.deduplicateText(texts);++ expect(result.error).toBeUndefined();+ expect(result.groups).toBeDefined();+ expect(result.groups.length).toBeGreaterThan(0);++ // verify at least one group contains similar hello world variants+ const helloGroup = result.groups.find((g) => g.text.includes("hello"));+ expect(helloGroup).toBeDefined();+ expect(helloGroup?.similar.length).toBeGreaterThan(0);+ });++ test("should handle empty input", async () => {+ const result = await pipe.deduplicateText([]);+ expect(result.groups).toHaveLength(0);+ expect(result.error).toBeUndefined();+ });++ test("should handle single text input", async () => {+ const result = await pipe.deduplicateText(["unique text"]);+ expect(result.groups).toHaveLength(0);+ expect(result.error).toBeUndefined();+ });++ // test("should handle server errors gracefully", async () => {+ // // Force an error by passing invalid input+ // const result = await pipe.deduplicateText([null as unknown as string]);+ // expect(result.error).toBeDefined();+ // expect(result.groups).toHaveLength(0);+ // });+});diff --git a/screenpipe-js/browser-sdk/tests/streamVision.test.ts b/screenpipe-js/browser-sdk/tests/streamVision.test.tsindex 5505890a2..18e425f3f 100644--- a/screenpipe-js/browser-sdk/tests/streamVision.test.ts+++ b/screenpipe-js/browser-sdk/tests/streamVision.test.ts@@ -1,5 +1,5 @@import { describe, expect, test } from "bun:test";-import { pipe } from "../../node-sdk/src/index";+import { pipe } from "../src/index";describe("streamVision", () => {test(diff --git a/screenpipe-js/common/analytics.ts b/screenpipe-js/common/analytics.tsindex f31fe17d6..f86af5be8 100644--- a/screenpipe-js/common/analytics.ts+++ b/screenpipe-js/common/analytics.ts@@ -1,18 +1,27 @@-import posthog from "posthog-js";+export interface AnalyticsClient {+ init: (key: string, config: any) => void;+ identify: (userId?: string, properties?: any) => void;+ capture: (name: string, properties?: any) => void;+}++let initialized = false;+let analyticsClient: AnalyticsClient | null = null;const POSTHOG_KEY = "phc_Bt8GoTBPgkCpDrbaIZzJIEYt0CrJjhBiuLaBck1clce";const POSTHOG_HOST = "https://eu.i.posthog.com";-let initialized = false;+export function setAnalyticsClient(client: AnalyticsClient) {+ analyticsClient = client;+}-function initPosthog(userId?: string, email?: string) {- if (!initialized) {- posthog.init(POSTHOG_KEY, {+function initAnalytics(userId?: string, email?: string) {+ if (!initialized && analyticsClient) {+ analyticsClient.init(POSTHOG_KEY, {api_host: POSTHOG_HOST,distinct_id: userId,email: email,});- posthog.identify(userId, { email: email });+ analyticsClient.identify(userId, { email: email });initialized = true;}}@@ -21,18 +30,20 @@ export async function captureEvent(name: string,properties?: Record<string, any>): Promise<void> {- initPosthog(properties?.distinct_id, properties?.email);+ if (!analyticsClient) return;+ initAnalytics(properties?.distinct_id, properties?.email);const { distinct_id, ...restProperties } = properties || {};- posthog.capture(name, restProperties);+ analyticsClient.capture(name, restProperties);}export async function captureMainFeatureEvent(name: string,properties?: Record<string, any>): Promise<void> {- initPosthog(properties?.distinct_id, properties?.email);+ if (!analyticsClient) return;+ initAnalytics(properties?.distinct_id, properties?.email);const { distinct_id, ...restProperties } = properties || {};- posthog.capture(name, {+ analyticsClient.capture(name, {feature: "main",...restProperties,});diff --git a/screenpipe-js/node-sdk/src/index.ts b/screenpipe-js/node-sdk/src/index.tsindex 4104bd89f..344c3c172 100644--- a/screenpipe-js/node-sdk/src/index.ts+++ b/screenpipe-js/node-sdk/src/index.ts@@ -9,8 +9,18 @@ import { toSnakeCase, convertToCamelCase } from "../../common/utils";import { SettingsManager } from "./SettingsManager";import { InboxManager } from "./InboxManager";import { PipesManager } from "../../common/PipesManager";-import { captureEvent, captureMainFeatureEvent } from "../../common/analytics";+import {+ captureEvent,+ captureMainFeatureEvent,+ setAnalyticsClient,+} from "../../common/analytics";+import posthog from "posthog-js";+setAnalyticsClient({+ init: posthog.init.bind(posthog),+ identify: posthog.identify.bind(posthog),+ capture: posthog.capture.bind(posthog),+});class NodePipe {private analyticsInitialized = false;private analyticsEnabled = true;