Skip to content

An AI Agent that interacts with Windows OS at GUI level.

Python
2.5 KB
Raw Blame
import atexit
import os
from pathlib import Path

from dotenv import load_dotenv
from posthog import Posthog
from uuid_extensions import uuid7str

from windows_use.telemetry.views import BaseTelemetryEvent

load_dotenv()


class ProductTelemetry:
    PROJECT_API_KEY = "phc_uxdCItyVTjXNU0sMPr97dq3tcz39scQNt3qjTYw5vLV"
    HOST = "https://us.i.posthog.com"

    def __init__(self):
        self._client = None
        self._user_id = None
        self._enabled = os.getenv("ANONYMIZED_TELEMETRY", "True").lower() == "true"

    @property
    def client(self):
        if self._enabled and self._client is None:
            try:
                self._client = Posthog(
                    project_api_key=self.PROJECT_API_KEY,
                    host=self.HOST,
                    disable_geoip=False,
                    enable_exception_autocapture=True,
                    flush_at=10,
                    flush_interval=5.0,
                )
                # Unregister Posthog's atexit join handler to prevent hanging on exit
                try:
                    atexit.unregister(self._client.join)
                except Exception:
                    pass
            except Exception:
                self._enabled = False
        return self._client

    @property
    def user_id(self):
        if self._user_id is not None:
            return self._user_id

        import tempfile

        temp_dir = Path(os.path.join(tempfile.gettempdir(), ".windows-use"))
        temp_dir.mkdir(parents=True, exist_ok=True)
        user_id_file = temp_dir / ".windows-use-user-id"

        if user_id_file.exists():
            try:
                self._user_id = user_id_file.read_text(encoding="utf-8").strip()
            except Exception:
                pass

        if not self._user_id:
            self._user_id = uuid7str()
            try:
                user_id_file.write_text(self._user_id, encoding="utf-8")
            except Exception:
                pass

        return self._user_id

    def capture(self, event: BaseTelemetryEvent):
        client = self.client
        if not client:
            return
        try:
            client.capture(
                distinct_id=self.user_id,
                event=event.event_name,
                properties={**event.properties, "process_person_profile": True},
            )
        except Exception:
            pass

    def flush(self):
        client = self.client
        if client:
            try:
                client.flush()
            except Exception:
                pass