> For the complete documentation index, see [llms.txt](https://swarmnexus.gitbook.io/swarmnexus/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://swarmnexus.gitbook.io/swarmnexus/documentation/readme/technical-documentation/publish-your-docs.md).

# The Collector

The Collector is the primary data ingestion engine of Swarm Nexus. It is a Node.js service that uses Puppeteer to automate scraping, ensuring that valuable community conversations are captured and stored in the Swarm Memory.

#### Core Responsibilities

* **Data Ingestion:** The Collector's main job is to find and process mentions of `@SwarmNexus` on Twitter (X).
* **Context Hydration:** It doesn't just save the mention itself; it fetches the entire parent thread to capture the full context of the conversation.
* **Metadata Extraction:** It parses the text to identify and extract key metadata, such as cryptocurrency tickers (`$TICKER`) and other relevant patterns.
* **Database Writing:** It is the **only** component with write access to the production database, where it upserts data into the `predictions` and `tickers` tables.
* **Health Reporting:** After every successful collection cycle, it writes to a `collector_heartbeat.json` file. This file is monitored by a separate process to ensure the Collector is alive and functioning correctly.

#### How It Works

1. **Authentication:** The Collector logs into Twitter using a set of pre-configured browser cookies from a dedicated scraping account.
2. **Polling:** It polls Twitter's notifications endpoint at a configurable interval (e.g., every 60 seconds).
3. **Scraping:** Using Puppeteer, it controls a headless browser to navigate the Twitter UI, read notifications, and click through to parent tweets.
4. **Data Processing:** The raw HTML is parsed, and the relevant text, authors, timestamps, and URLs are extracted and structured.
5. **Database Upsert:** The structured data is written to the `swarm.db` SQLite database. An "upsert" operation is used to avoid creating duplicate entries.

#### Configuration

The Collector's behavior is controlled by environment variables defined in `/opt/swarmnexus/collector/.env`.

| Variable                 | Description                                                            | Default Value          |
| ------------------------ | ---------------------------------------------------------------------- | ---------------------- |
| `SOURCE`                 | The scraping source (e.g., `notifications`).                           | `notifications`        |
| `POLL_SECONDS`           | The interval in seconds between scraping cycles.                       | `60`                   |
| `MAX_MENTIONS_PER_CYCLE` | The maximum number of mentions to process in a single run.             | `80`                   |
| `BACKOFF_BASE_MS`        | The base time in milliseconds for exponential backoff if rate-limited. | `120000`               |
| `TW_COOKIES_PATH`        | The path to the Twitter cookies file.                                  | `twitter-cookies.json` |


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://swarmnexus.gitbook.io/swarmnexus/documentation/readme/technical-documentation/publish-your-docs.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
