Skip to main content

Typed, auto-generated client for the OpenSwarm product-analytics ingest API.

Project description

swarm_analytics

A typed, auto-generated Python client for the OpenSwarm product-analytics ingest API. It is the single network egress the desktop FastAPI backend imports to send analytics — identity comes from a bearer token, payloads are validated against the exact pydantic models the server enforces, and delivery is fire-and-forget with background retry.

Why it's hard to call wrong

  • Identity is impossible to pass. No method takes install_id/user_id; the server resolves them from the token.
  • Per-request meta is auto-filled. ts and submission_id never appear in a signature — the transport stamps them (and reuses submission_id on every retry for idempotency).
  • Enums stay enums. status, action, role, etc. are Literals. A bad value raises pydantic.ValidationError synchronously, in your stack, before any network I/O.
  • Models are vendored verbatim from the service, so the client validates with the same schema the server uses.

Install

pip install ./sdk          # from the repo root

Usage

from swarm_analytics import AnalyticsClient, AgentMessage

# One-time bootstrap on first launch (unauthenticated, blocking)
token = AnalyticsClient.register(base_url="https://analytics.openswarm.ai", install_id=install_id)
# persist `token` in settings; reuse forever

client = AnalyticsClient(base_url="https://analytics.openswarm.ai", token=token)

client.events.app_lifecycle.opened(os="darwin", os_version="25.3.0", app_version="1.2.0")
client.events.agent.create(id="sess_123", name="Refactor auth", dashboard_id="dash_1")
client.events.agent.message(agent_id="sess_123", seq=0,
                            message=AgentMessage(id="m1", role="user", content="hello"))
client.events.onboarding.step(step_id="connect_provider", status="completed")
client.events.dashboard.event(dashboard_id="dash_1", action="create")
client.logs.write(tag="agent", subtag="tool", data={"name": "shell"})
client.identify.link_email(email="user@example.com")

# On shutdown
client.events.app_lifecycle.closed()
client.flush(timeout=2.0)
client.close()

Durability (optional)

By default events live in an in-memory queue and are lost if the process dies with deliveries pending. Pass a spool for crash/offline durability:

from swarm_analytics import SqliteSpool
client = AnalyticsClient(base_url=..., token=..., spool=SqliteSpool("service_spool.db"))

Opt-out

client = AnalyticsClient(base_url=..., token=..., mode="minimal")  # mutes product events; diagnostics still flow

Regenerating (auto-generated — do not hand-edit _generated/)

The models, route table, and namespaces under src/swarm_analytics/_generated/ are produced from the live service. Regenerate whenever the backend's ingest models or routes change:

PYTHONPATH=<repo_root> python sdk/generate.py

ROUTE_SPECS in generate.py (nice method name + category per endpoint) is the only human input; it is cross-checked against the live app, so a new or removed endpoint fails generation rather than drifting silently.

Drift check (CI)

PYTHONPATH=<repo_root> python sdk/generate.py --check

Exits non-zero if the committed _generated/ output is stale. The same guard runs as tests/test_drift.py.

Tests

cd sdk && PYTHONPATH=<repo_root> python -m pytest tests -q

Covers synchronous validation, meta auto-fill, identity-from-token, idempotent submission_id reuse across retries, opt-out gating, the drift check, and an end-to-end pass through the real FastAPI app.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

swarm_analytics-0.1.1.tar.gz (22.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

swarm_analytics-0.1.1-py3-none-any.whl (20.3 kB view details)

Uploaded Python 3

File details

Details for the file swarm_analytics-0.1.1.tar.gz.

File metadata

  • Download URL: swarm_analytics-0.1.1.tar.gz
  • Upload date:
  • Size: 22.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for swarm_analytics-0.1.1.tar.gz
Algorithm Hash digest
SHA256 c1d368905a8b53a555bb53bd60c6707323fba4beff3ea2e79a8f83fb91b11cab
MD5 91fd8dc1e7d799945007ca737afe3634
BLAKE2b-256 2fd2fd9e1ba034c9d404507fb511ba8f7fbb36e3d4e040860fe60aea74bf98c9

See more details on using hashes here.

File details

Details for the file swarm_analytics-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for swarm_analytics-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 49255c5a0962ba1eca3c14471b7df8746f4258d8cd83c8c73931e62381b2be8e
MD5 ce639d6f30673fb04d85d85da4c8a2c3
BLAKE2b-256 1b94f871b9067f9dbe6595505098fb06f2087b0906505f4673536392bb94c03b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page