Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: CI

on:
pull_request:
push:
branches: [main]

permissions:
contents: read

jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
with:
version: 11.2.2
- uses: actions/setup-node@v4
with:
node-version: 22.19.0
cache: pnpm
- run: pnpm install --frozen-lockfile
- run: pnpm check
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ build/Release
# Dependency directories
node_modules/
jspm_packages/
.worktrees/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/
Expand Down
1 change: 1 addition & 0 deletions .pebbles/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pebbles.db
3 changes: 3 additions & 0 deletions .pebbles/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"prefix": "gmail"
}
42 changes: 42 additions & 0 deletions .pebbles/events.jsonl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{"type":"create","timestamp":"2026-07-12T13:39:02.188201Z","issue_id":"gmail-883","payload":{"description":"Create a modern external OpenClaw channel plugin where each Gmail thread maps to one isolated OpenClaw session. Deliver package scaffolding, secure account configuration, Gmail API transport, polling ingress, threaded replies, tests, documentation, and package validation.","priority":"1","title":"Build Gmail-backed OpenClaw channel","type":"epic"}}
{"type":"create","timestamp":"2026-07-12T13:39:16.864391Z","issue_id":"gmail-e98","payload":{"description":"Create TypeScript ESM package metadata, manifest, build/test/lint scripts, runtime entry, CI, and pinned OpenClaw compatibility metadata. Acceptance: package installs dependencies, entry follows defineChannelPluginEntry, build and an entry-shape test pass. Verification: pnpm test; pnpm build; pnpm typecheck.","priority":"1","title":"Scaffold the modern OpenClaw plugin package","type":"task"}}
{"type":"create","timestamp":"2026-07-12T13:39:16.954953Z","issue_id":"gmail-c95","payload":{"description":"Define strict Zod channel schemas, secret-input-aware account resolution, target parsing, and deny-by-default inbound/outbound email policy. Acceptance: named/default accounts resolve without exposing secret values; exact/domain allowlists are case-insensitive; malformed targets/config fail closed. Verification: focused config and target tests plus typecheck.","priority":"1","title":"Implement secure Gmail account configuration","type":"task"}}
{"type":"create","timestamp":"2026-07-12T13:39:17.028583Z","issue_id":"gmail-227","payload":{"description":"Add a narrow Gmail client boundary, bounded MIME extraction, RFC 2822/base64url construction, unread listing/get/mark-read, and send/reply operations. Acceptance: untrusted API data is validated; text/plain and safe HTML fallback parse; new sends and replies produce correct Gmail payloads and return message/thread IDs. Verification: transport tests with a fake API, build, npm audit.","priority":"1","title":"Implement Gmail API message transport","type":"task"}}
{"type":"create","timestamp":"2026-07-12T13:39:17.117982Z","issue_id":"gmail-499","payload":{"description":"Implement polling gateway and inbound dispatch through current runtime helpers, using Gmail thread IDs as channel peers so sessions are isolated per thread. Acceptance: same thread produces same session route; different threads differ; self/disallowed senders never dispatch; successful dispatch marks read; reply delivery stays in the source thread. Verification: gateway/inbound tests and typecheck.","priority":"1","title":"Route polled Gmail threads through OpenClaw","type":"task"}}
{"type":"create","timestamp":"2026-07-12T13:39:17.257903Z","issue_id":"gmail-aa0","payload":{"description":"Write setup/configuration documentation, limitations and security notes; pack the built plugin and inspect/install it against current OpenClaw; run full validation and pre-ship reviews. Acceptance: README explains OAuth and target grammar; npm pack contains only required artifacts; OpenClaw runtime inspection recognizes gmail; all quality gates pass. Verification: pnpm check; npm pack --dry-run; OpenClaw plugin inspection.","priority":"1","title":"Document, package, and validate the first milestone","type":"task"}}
{"type":"rename","timestamp":"2026-07-12T13:39:28.320175Z","issue_id":"gmail-e98","payload":{"new_id":"gmail-883.1"}}
{"type":"dep_add","timestamp":"2026-07-12T13:39:28.320175Z","issue_id":"gmail-883.1","payload":{"dep_type":"parent-child","depends_on":"gmail-883"}}
{"type":"rename","timestamp":"2026-07-12T13:39:28.340705Z","issue_id":"gmail-c95","payload":{"new_id":"gmail-883.2"}}
{"type":"dep_add","timestamp":"2026-07-12T13:39:28.340705Z","issue_id":"gmail-883.2","payload":{"dep_type":"parent-child","depends_on":"gmail-883"}}
{"type":"rename","timestamp":"2026-07-12T13:39:28.360951Z","issue_id":"gmail-227","payload":{"new_id":"gmail-883.3"}}
{"type":"dep_add","timestamp":"2026-07-12T13:39:28.360951Z","issue_id":"gmail-883.3","payload":{"dep_type":"parent-child","depends_on":"gmail-883"}}
{"type":"rename","timestamp":"2026-07-12T13:39:28.381351Z","issue_id":"gmail-499","payload":{"new_id":"gmail-883.4"}}
{"type":"dep_add","timestamp":"2026-07-12T13:39:28.381351Z","issue_id":"gmail-883.4","payload":{"dep_type":"parent-child","depends_on":"gmail-883"}}
{"type":"rename","timestamp":"2026-07-12T13:39:28.403392Z","issue_id":"gmail-aa0","payload":{"new_id":"gmail-883.5"}}
{"type":"dep_add","timestamp":"2026-07-12T13:39:28.403392Z","issue_id":"gmail-883.5","payload":{"dep_type":"parent-child","depends_on":"gmail-883"}}
{"type":"dep_add","timestamp":"2026-07-12T13:39:28.426286Z","issue_id":"gmail-883","payload":{"dep_type":"blocks","depends_on":"gmail-883.1"}}
{"type":"dep_add","timestamp":"2026-07-12T13:39:28.449699Z","issue_id":"gmail-883","payload":{"dep_type":"blocks","depends_on":"gmail-883.2"}}
{"type":"dep_add","timestamp":"2026-07-12T13:39:28.474009Z","issue_id":"gmail-883","payload":{"dep_type":"blocks","depends_on":"gmail-883.3"}}
{"type":"dep_add","timestamp":"2026-07-12T13:39:28.497556Z","issue_id":"gmail-883","payload":{"dep_type":"blocks","depends_on":"gmail-883.4"}}
{"type":"dep_add","timestamp":"2026-07-12T13:39:28.520338Z","issue_id":"gmail-883","payload":{"dep_type":"blocks","depends_on":"gmail-883.5"}}
{"type":"dep_add","timestamp":"2026-07-12T13:39:28.545139Z","issue_id":"gmail-883.2","payload":{"dep_type":"blocks","depends_on":"gmail-883.1"}}
{"type":"dep_add","timestamp":"2026-07-12T13:39:28.569058Z","issue_id":"gmail-883.3","payload":{"dep_type":"blocks","depends_on":"gmail-883.2"}}
{"type":"dep_add","timestamp":"2026-07-12T13:39:28.593206Z","issue_id":"gmail-883.4","payload":{"dep_type":"blocks","depends_on":"gmail-883.3"}}
{"type":"dep_add","timestamp":"2026-07-12T13:39:28.62949Z","issue_id":"gmail-883.5","payload":{"dep_type":"blocks","depends_on":"gmail-883.4"}}
{"type":"status_update","timestamp":"2026-07-12T13:41:23.915971Z","issue_id":"gmail-883.1","payload":{"status":"in_progress"}}
{"type":"close","timestamp":"2026-07-12T13:47:21.620738Z","issue_id":"gmail-883.1","payload":{}}
{"type":"status_update","timestamp":"2026-07-12T13:53:15.347607Z","issue_id":"gmail-883.2","payload":{"status":"in_progress"}}
{"type":"close","timestamp":"2026-07-12T13:53:20.356718Z","issue_id":"gmail-883.2","payload":{}}
{"type":"status_update","timestamp":"2026-07-12T13:57:05.108169Z","issue_id":"gmail-883.3","payload":{"status":"in_progress"}}
{"type":"close","timestamp":"2026-07-12T13:57:05.134847Z","issue_id":"gmail-883.3","payload":{}}
{"type":"status_update","timestamp":"2026-07-12T14:03:11.788559Z","issue_id":"gmail-883.4","payload":{"status":"in_progress"}}
{"type":"close","timestamp":"2026-07-12T14:03:11.817066Z","issue_id":"gmail-883.4","payload":{}}
{"type":"create","timestamp":"2026-07-12T14:04:34.757377Z","issue_id":"gmail-92e","payload":{"description":"Prove how OpenClaw should replace a provisional mailto recipient route with the canonical Gmail thread ID returned after messages.send. Add plugin-owned mapping or propose the smallest SDK enhancement so subsequent replies continue the outbound-created session.","priority":"1","title":"Reconcile outbound-originated Gmail thread sessions","type":"task"}}
{"type":"create","timestamp":"2026-07-12T14:04:34.86125Z","issue_id":"gmail-7a7","payload":{"description":"Implement an OpenClaw setup entry and installed-app OAuth flow with PKCE, least-privilege gmail.modify scope, secret-provider storage, cancellation, and doctor diagnostics. Do not expose refresh tokens in logs or config summaries.","priority":"2","title":"Add interactive Gmail OAuth setup","type":"task"}}
{"type":"create","timestamp":"2026-07-12T14:04:34.94706Z","issue_id":"gmail-178","payload":{"description":"Add bounded attachment download/storage plus multi-recipient reply-all with independent allowTo checks for every To/Cc recipient. Cover MIME nesting, filename safety, size limits, and unsupported attachment behavior.","priority":"2","title":"Add Gmail attachments and reply-all","type":"task"}}
{"type":"create","timestamp":"2026-07-12T14:04:35.041849Z","issue_id":"gmail-b74","payload":{"description":"Compare polling with History API cursors and Pub/Sub push notifications. Specify durable cursor storage, missed-history recovery, webhook authentication, operational requirements, and rate-limit behavior before implementation.","priority":"3","title":"Evaluate Gmail History API and push delivery","type":"task"}}
{"type":"status_update","timestamp":"2026-07-12T14:08:41.12632Z","issue_id":"gmail-883.5","payload":{"status":"in_progress"}}
{"type":"comment","timestamp":"2026-07-12T16:34:53.232558Z","issue_id":"gmail-883.5","payload":{"body":"Validated with pnpm check (38 tests), dependency audit, package inspection, independent review, and runtime loading against OpenClaw origin/main 10853e82fdeb4939c8f3ed0f20697790a52fb617."}}
{"type":"close","timestamp":"2026-07-12T16:34:53.26207Z","issue_id":"gmail-883.5","payload":{}}
{"type":"comment","timestamp":"2026-07-12T16:34:53.290442Z","issue_id":"gmail-883","payload":{"body":"First milestone complete. Enhancements for outbound session reconciliation, OAuth setup, attachments/reply-all, and push delivery remain tracked separately."}}
{"type":"close","timestamp":"2026-07-12T16:34:53.317901Z","issue_id":"gmail-883","payload":{}}
4 changes: 4 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.pebbles/
dist/
node_modules/
pnpm-lock.yaml
204 changes: 203 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,204 @@
# gmail-chan
OpenClaw communication channel backed by Gmail threads

`gmail-chan` is an external [OpenClaw](https://github.com/openclaw/openclaw)
channel plugin that uses Gmail threads as conversations.

One Gmail thread maps to one OpenClaw session. Every email in that thread
continues the session, and a different Gmail thread creates a different session.

The `0.1.x` milestone supports plain-text messages through the Gmail API:

- poll unread inbox messages;
- admit senders through a deny-by-default allowlist;
- dispatch each Gmail thread through an isolated OpenClaw session;
- send the agent's result as a reply in the source Gmail thread;
- start a new Gmail thread through an explicit `mailto:` target;
- return Gmail message and thread IDs in durable OpenClaw receipts.

## Requirements

- OpenClaw `2026.7.2` or newer
- Node.js 22.19+, 23.11+, or 24+
- A Google Cloud project with the Gmail API enabled
- An OAuth 2.0 Desktop app client and refresh token

The plugin requests `https://www.googleapis.com/auth/gmail.modify`. Google
classifies this as a restricted scope because it permits reading messages,
sending messages, and changing labels. Review Google's
[Gmail scope documentation](https://developers.google.com/workspace/gmail/api/auth/scopes)
before distributing one OAuth client to other users.

## Install for development

```bash
git clone git@github.com:Martian-Engineering/gmail-chan.git
cd gmail-chan
pnpm install
pnpm build
openclaw plugins install --link "$PWD"
```

Published packages use `dist/index.js`. Linked source checkouts use `index.ts`,
which OpenClaw supports for local plugin development.

## Create Gmail credentials

1. Enable the Gmail API in a Google Cloud project.
2. Configure the OAuth consent screen for the account that owns the mailbox.
3. Create an OAuth client with application type **Desktop app**.
4. Obtain an offline refresh token for
`https://www.googleapis.com/auth/gmail.modify` through an installed-app OAuth
flow. Google's
[desktop OAuth guide](https://developers.google.com/identity/protocols/oauth2/native-app)
documents the authorization-code and refresh-token exchange.
5. Store the client ID, client secret, and refresh token as environment-backed
OpenClaw secret inputs.

For a single-operator development setup, the
[Google OAuth 2.0 Playground](https://developers.google.com/oauthplayground/)
can perform step 4 with **Use your own OAuth credentials** enabled. Do not use a
shared refresh token or commit one to this repository.

## Configure OpenClaw

Environment variables:

```bash
export GMAIL_CLIENT_ID='your-client-id.apps.googleusercontent.com'
export GMAIL_CLIENT_SECRET='your-client-secret'
export GMAIL_REFRESH_TOKEN='your-refresh-token'
```

OpenClaw config:

```json5
{
channels: {
gmail: {
defaultAccount: "work",
accounts: {
work: {
email: "agent@example.com",
oauth: {
clientId: {
source: "env",
provider: "default",
id: "GMAIL_CLIENT_ID",
},
clientSecret: {
source: "env",
provider: "default",
id: "GMAIL_CLIENT_SECRET",
},
refreshToken: {
source: "env",
provider: "default",
id: "GMAIL_REFRESH_TOKEN",
},
},
allowFrom: ["person@example.com", "@martian.engineering"],
allowTo: ["person@example.com", "@martian.engineering"],
pollIntervalSeconds: 30,
},
},
},
},
}
```

Restart the OpenClaw gateway after changing channel configuration.

### Address policy

Both `allowFrom` and `allowTo` default to `[]`, which denies every address.

- `person@example.com` matches one address without case sensitivity.
- `@example.com` matches that domain and its subdomains.
- `*` matches every syntactically valid email address.

`allowFrom` controls messages delivered to the agent. `allowTo` controls new
outbound threads and replies. A permitted inbound sender must also be in
`allowTo` for the agent to reply. Admission additionally fails closed unless a
Google-generated `Authentication-Results` header reports aligned DMARC success
for the normalized `From` domain. This prevents the address policy from trusting
an unauthenticated, spoofed `From` header; mail from domains without passing
DMARC is ignored.

## Targets and sessions

The shared OpenClaw `message` tool accepts two Gmail target forms:

- `thread:<gmail-thread-id>` replies in an existing Gmail thread.
- `mailto:<email-address>` starts a new Gmail thread with subject
`OpenClaw message`.

OpenClaw-qualified forms such as `gmail:thread:<gmail-thread-id>` and
`gmail:mailto:<email-address>` normalize to the same targets.

Bare email addresses normalize to `mailto:` targets. Thread IDs require the
`thread:` prefix.

Inbound routing models each Gmail thread as an OpenClaw channel peer. This keeps
thread sessions isolated even when the operator's direct-message session policy
uses a shared main session.

Gmail assigns the canonical thread ID after a new outbound send. The plugin
returns that ID in the message receipt. Reconciliation between an
outbound-originated provisional OpenClaw route and the canonical Gmail thread
session remains tracked work.

## Delivery behavior

- Successfully dispatched messages are marked read.
- Self-authored and denied messages are not sent to the agent and are marked
read, which prevents a denied message from producing a polling loop.
- A dispatch failure leaves the Gmail message unread for a later poll.
- Processing is sequential per account. An in-process guard prevents concurrent
handling of one Gmail message ID.
- Gmail responses, MIME structures, headers, and bodies are validated and
bounded before use.
- HTML email is reduced to text. Script and style blocks are removed; HTML is
never executed.

The Gmail API groups replies by thread only when the request includes the thread
ID, the subject matches, and `References` plus `In-Reply-To` follow RFC 2822.
The plugin sets all three requirements. See Google's
[thread guide](https://developers.google.com/workspace/gmail/api/guides/threads)
and [sending guide](https://developers.google.com/workspace/gmail/api/guides/sending).
If the source message has no `Message-ID`, the plugin fails closed instead of
claiming a reply that Gmail may place in a different thread.

## Limitations

- Plain-text inbound and outbound content only
- One external reply recipient per thread; reply-all is not implemented
- No attachments or inline images
- No interactive OpenClaw OAuth wizard
- Polling only; no Gmail History API cursor or push notifications
- No automatic thread-history backfill into the model context
- New outbound messages use the fixed subject `OpenClaw message`

## Development

```bash
pnpm test
pnpm typecheck
pnpm lint
pnpm format:check
pnpm build
pnpm check
pnpm audit --audit-level high
npm pack --dry-run
```

Tests use fake Gmail API boundaries and do not require Google credentials or
network access.

The implementation targets OpenClaw `main` commit
`1318cf1fdef0f86fa186ab30be9f0a60e24c7868`, whose package version is
`2026.7.2`. See [docs/spec.md](docs/spec.md) for the contract and
[docs/plan.md](docs/plan.md) for the milestone task graph.

## License

MIT
Loading
Loading