Metadata-Version: 2.4
Name: glean-importer
Version: 0.1.0
Summary: Imports what your sources hold into orbit (a personal CRM) and sift (local RAG search), one sandboxed Claude Code agent run per item. Email works today: IMAP with OAuth2 (Outlook.com, Hotmail) or .eml files.
Keywords: email,imap,oauth2,outlook,hotmail,crm,personal-crm,rag,mcp,claude-code,ai-agents,local-first
Author: Tobias Würth
License-Expression: MIT
License-File: LICENSE
Classifier: Development Status :: 3 - Alpha
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: End Users/Desktop
Classifier: Operating System :: MacOS
Classifier: Operating System :: Microsoft :: Windows
Classifier: Operating System :: POSIX :: Linux
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Classifier: Natural Language :: English
Classifier: Topic :: Communications :: Email
Classifier: Topic :: Office/Business
Classifier: Topic :: Text Processing :: Markup :: Markdown
Requires-Dist: msal>=1.31
Requires-Dist: msal-extensions>=1.2
Requires-Dist: inscriptis>=2.5
Requires-Dist: filelock>=3.16
Requires-Python: >=3.11
Project-URL: Homepage, https://github.com/tobiaswuerth/glean
Project-URL: Documentation, https://github.com/tobiaswuerth/glean#readme
Project-URL: Source, https://github.com/tobiaswuerth/glean
Project-URL: Issues, https://github.com/tobiaswuerth/glean/issues
Project-URL: Changelog, https://github.com/tobiaswuerth/glean/blob/main/CHANGELOG.md
Description-Content-Type: text/markdown

# glean: imports what your sources hold into orbit and sift

[![CI](https://github.com/tobiaswuerth/glean/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/tobiaswuerth/glean/actions/workflows/ci.yml)
[![PyPI](https://img.shields.io/pypi/v/glean-importer)](https://pypi.org/project/glean-importer/)
[![Python](https://img.shields.io/pypi/pyversions/glean-importer?logo=python&logoColor=white)](https://pypi.org/project/glean-importer/)
[![Coverage](https://img.shields.io/badge/coverage-100%25-brightgreen)](https://github.com/tobiaswuerth/glean/actions/workflows/ci.yml)
[![License](https://img.shields.io/github/license/tobiaswuerth/glean)](https://github.com/tobiaswuerth/glean/blob/main/LICENSE)
[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)
[![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit)](https://github.com/pre-commit/pre-commit)

glean reads information from your sources and records each item in two local stores:
[orbit](https://github.com/tobiaswuerth/orbit), a personal CRM that holds the structured view (people, organizations,
interactions, tasks, topics), and [sift](https://github.com/tobiaswuerth/sift), a local search that holds the full
text. Email works today: from an IMAP mailbox such as Outlook.com or Hotmail, signed in with OAuth2, or from a folder
of saved `.eml` files.

For every item, glean writes a Markdown document, then starts a fresh [Claude Code](https://claude.com/claude-code)
agent run whose only tools are orbit's and sift's MCP tools. The agent decides who is who, logs the interaction,
updates people, organizations and tasks, and makes the item searchable. Neither orbit nor sift knows glean exists.

- **One run per item, oldest first, one at a time.** Each run starts with a fresh context, so it knows only what orbit
  and sift hold; the people an earlier email created exist when a later one in the thread mentions them.
- **The source is never changed.** Mail folders are opened read-only and messages fetched without marking them read;
  what glean processed is recorded only in its own state database, which can be inspected and reset.
- **A sandbox that is checked.** The agent has no shell, file or web tools, and none of orbit's destructive ones
  (merge, rename). Every run's first event lists the tools it really has; a run with anything else is stopped before
  it acts. Items are treated as data, never as instructions
  ([how](https://github.com/tobiaswuerth/glean/blob/main/docs/how-it-works.md#the-agent-run)).
- **Cheap where it can be.** Newsletters, notifications and automatic replies are skipped without an agent run.
- **Local.** The archive, the state database and the sign-in tokens stay on your machine; the tokens are encrypted
  with your Windows login (on macOS and Linux, kept in a file only you may read).

## Sources

| Source | Commands | Reads |
|---|---|---|
| Email | `glean mail …` | An IMAP mailbox with OAuth2 (Outlook.com, Hotmail: every folder but junk, trash and drafts), or a folder of `.eml` files |

## Requirements

- Python 3.11 or newer.
- [Claude Code](https://claude.com/claude-code) 2.1.259 or newer, signed in (`claude auth status`). On Windows, the
  native installer's `claude.exe`.
- [orbit](https://github.com/tobiaswuerth/orbit) and [sift](https://github.com/tobiaswuerth/sift), each serving MCP
  over HTTP: `orbit serve <vault>` (port 8775) and `sift serve` (port 8765).
- For an Outlook.com or Hotmail mailbox: an app registered in Microsoft Entra (free, about five minutes; step 1
  below), and IMAP turned on in Outlook's settings.

## Install

With [uv](https://docs.astral.sh/uv/):

```bash
uv tool install glean-importer   # installs the `glean` command
glean --version
```

Or with pip into a virtual environment: `pip install glean-importer`. The package is called `glean-importer`; the
command is `glean`. To update: `uv tool upgrade glean-importer`.

## Set up

### 1. Register glean with Microsoft (Outlook.com, Hotmail)

Outlook.com and Hotmail accept only OAuth2 for IMAP: Microsoft turned off passwords and app passwords for IMAP on
personal accounts in September 2024. A program that signs in with OAuth2 needs a client ID, which an app registered in
Microsoft Entra gives it. The app gives no one but you access: glean still signs in as you, in your browser.

1. Open the [Microsoft Entra admin center](https://entra.microsoft.com) and sign in with your Microsoft account. If it
   says your account has no directory, create a free Azure account first, which makes one.
2. **Applications > App registrations > New registration.** Name: `glean`. Supported account types: *Personal
   Microsoft accounts only*. Redirect URI: platform *Public client/native (mobile & desktop)*, `http://localhost`.
   **Register.**
3. **API permissions > Add a permission > Microsoft Graph > Delegated permissions:** `IMAP.AccessAsUser.All` and
   `offline_access`. **Add permissions.**
4. On the app's **Overview**, copy the **Application (client) ID**. It is not a secret: a public client has none.
5. In [Outlook on the web](https://outlook.live.com): **Settings > Mail > Forwarding and IMAP > Let devices and apps
   use IMAP:** on.

For a folder of `.eml` files instead of a mailbox, skip this step and set `[mail] eml_folder` after step 3.
[docs/microsoft-app.md](https://github.com/tobiaswuerth/glean/blob/main/docs/microsoft-app.md) has the same steps
with troubleshooting.

### 2. Choose glean's home

The folder for glean's configuration (`glean.toml`), its state database, its sign-in tokens and, by default, the
archive of downloaded items. It is `~/.glean` unless `GLEAN_HOME` names another. Keep it out of synced folders: it
holds your mail. (In a clone of this repository, `data/` is ignored by git for this purpose.)

```powershell
setx GLEAN_HOME D:\glean-data   # Windows, optional; open a new terminal afterwards
```

### 3. Run the setup

```bash
glean setup
```

It asks for your address, the client ID and your other addresses, writes `glean.toml`, and opens the browser to sign
you in (`glean mail login` does that again later; `--device-code` signs in without a browser). It also starts by
itself the first time you use glean. The tokens are kept in glean's home, encrypted with your Windows login (on
macOS and Linux, in a file only you may read).

### 4. Make the archive and the vault searchable in sift

Once: the agent indexes each email's files and the notes it changed, and sift only accepts files inside its
collections.

```bash
sift add <GLEAN_HOME>/mail --name mail --title-from name
sift add <orbit vault> --name crm --title-from name
```

Other names work too: set them as `[mail] collection` and `[sift] crm_collection` in `glean.toml`.

### 5. Check everything

```bash
glean doctor
```

It checks Claude Code and its sign-in, that orbit and sift answer, that an agent run gets exactly the allowed tools,
and the mail sign-in. Every line should start with `ok`.

## Use

```bash
glean mail download            # fetch what is new; the first time, everything
glean mail list --latest 20    # the 20 newest not yet imported, in the order run takes them
glean mail run --latest 20     # import them, oldest first, one agent run each
glean mail show <id>           # one email: its state, files, and the agent's report
glean mail ignore <id>         # never import it
glean mail reset <id>          # import it again
glean status                   # counts per source and status, the agents' cost so far
```

`run` also takes ids, `--retry-failed` for emails whose run failed, `--include-skipped` for those the skip rules set
aside, and `--dry-run` to write the documents and show what the agent would get, without running it.

**Try it on a test vault first.** Point `[orbit] url` at a copy of your vault (`orbit serve D:\crm-test --port 8776`),
import a few emails, read the reports (`glean mail show <id>`) and the notes, and only then use the real vault. In a
clone of this repository, [`experiments/`](https://github.com/tobiaswuerth/glean/blob/main/experiments/README.md) imports a made-up mailbox into a new vault with
the real agent and prints each run's report.

## Configuration

`glean.toml` in glean's home; the setup writes it with every setting and a comment on each. The main ones:

| Setting | Default | |
|---|---|---|
| `[agent] model` | `sonnet` | the model of each run |
| `[agent] max_turns`, `timeout_minutes`, `max_budget_usd` | 60, 5, 1.0 | limits per item; a run that hits one fails, and is retried with `--retry-failed` |
| `[orbit] url`, `[sift] url` | ports 8775, 8765 | the MCP servers |
| `[sift] crm_collection`, `document_collections` | `crm`, all | where the vault is, and where to look for documents an item refers to |
| `[mail] address`, `client_id` | | the account, and the Entra app's client ID |
| `[mail] my_addresses` | | your other addresses: mail from them is outbound |
| `[mail] folders`, `exclude_folders` | all; Junk, Deleted, Drafts, Outbox, Notes | which folders to read |
| `[mail] archive`, `collection` | `mail` | where mail is kept, and its sift collection |
| `[mail] skip_bulk`, `skip_senders` | on; `noreply@*`, … | the pre-filter |
| `[mail] save_attachments` | `.pdf`, `.docx`, `.xlsx`, `.pptx`, … | attachments saved beside the email for sift |
| `[mail] eml_folder` | | read `.eml` files from this folder instead of the IMAP server |

## How it works

[docs/how-it-works.md](https://github.com/tobiaswuerth/glean/blob/main/docs/how-it-works.md) describes the pipeline, the archive and its Markdown documents, the state
database, the agent's sandbox and how it is checked, and how a source is added.

## Security and privacy

Items are untrusted input: an email can carry text written to steer an agent. glean limits what such text can do (no
dangerous tools, no destructive ones, every run's tools checked, uncertainty turned into a question for you in orbit's
review queue) and keeps everything local. See [SECURITY.md](https://github.com/tobiaswuerth/glean/blob/main/SECURITY.md).

## Contributing

Bug reports and ideas are welcome as [issues](https://github.com/tobiaswuerth/glean/issues);
[CONTRIBUTING.md](https://github.com/tobiaswuerth/glean/blob/main/CONTRIBUTING.md) describes the setup for development, the code layout and how a release is made.
The [changelog](https://github.com/tobiaswuerth/glean/blob/main/CHANGELOG.md) lists what changed.

## License

[MIT](https://github.com/tobiaswuerth/glean/blob/main/LICENSE)
