# pi-chatgpt-web
Use your logged-in ChatGPT browser session as a model provider in
[pi](https://www.npmjs.com/package/@earendil-works/pi-coding-agent), with
optional bridging so ChatGPT can drive pi's native tools.
No API key. Turns go through Chrome via
[opencli](https://opencli.info/docs/adapters/browser/chatgpt.html), so the
conversation is a real ChatGPT thread you can open and read in the browser.
```mermaid
flowchart LR
You[You in pi] --> Pi[pi agent loop]
Pi --> Ext[opencli-chatgpt-provider.ts]
Ext --> Gateway[session gateway
lease + registry]
Gateway --> OC[opencli chatgpt ask/detail]
OC --> Chrome[chatgpt.com in Chrome]
```
## Two modes
| Mode | Flag | Tools | Use for |
| --- | --- | --- | --- |
| **B1 chat-only** | default | disabled | Chat-shaped work, where the model does not need to touch your files. |
| **B2 bridge** | `--chatgpt-bridge` | enabled | Agent loops. ChatGPT emits a tool call, pi runs it, then pi injects the result back into the thread. |
In B2 the browser transcript stays readable, and pi shows the same turns as
native tool invocations.
**B2 runs every tool call without a confirmation prompt**, including `bash` and
`write`. Do not point B2 at a repo you are unwilling to let a model edit.
## Requirements
| Requirement | Notes |
| --- | --- |
| macOS | Tested on macOS. Linux should work wherever the opencli browser bridge does. |
| Node.js 20+ | pi and opencli are npm packages. |
| Google Chrome | Logged into [chatgpt.com](https://chatgpt.com). |
| pi 0.83+ | `@earendil-works/pi-coding-agent` |
| opencli 1.8.6 | `apply-overlay.mjs` hash-gates the patches to this exact version. |
## Install
```bash
npm install -g @earendil-works/pi-coding-agent
npm install -g @jackwener/opencli@1.8.6
git clone https://github.com/tzachbon/pi-chatgpt-web.git
cd pi-chatgpt-web
cp -R extensions/* ~/.pi/agent/extensions/
node overlays/apply-overlay.mjs
```
Confirm the bridge works:
```bash
opencli doctor
opencli chatgpt status -f json # expect Login: Yes
```
Start pi and pick `chatgpt-web/default` in `/model`, or:
```bash
pi --provider chatgpt-web # B1 chat-only
pi --provider chatgpt-web --chatgpt-bridge # B2 tools enabled
```
Re-run `node overlays/apply-overlay.mjs` after any `npm install -g
@jackwener/opencli`, since npm overwrites the patched files.
[`overlays/README.md`](overlays/README.md) covers what the patches change and
how to revert them.
## Wire format
In B2, the model calls a tool by opening a reply with a bare `tool` line and one
JSON object:
```
tool
{"name": "read", "arguments": {"path": "/tmp/notes.txt"}}
```
The parser also accepts a fenced ` ```tool ` block, but ChatGPT strips fences
from replies, so the startup brief specifies the bare form. One tool call per
reply. pi posts each result back as a `[Tool result: ]` user message,
which keeps the browser transcript reading as an ordinary conversation.
The provider injects the startup brief on the first turn of a session: the
environment, the tool catalog, the call format, and the result loop. You do not
write it.
## Session gateway
`extensions/chatgpt-gateway/` stops concurrent pi sessions from fighting over
the same Chrome tab.
* **Registry** at `~/.pi/agent/chatgpt-gateway/registry.json` tracks active
sessions, their pids, and their ChatGPT `conversationId`.
* **Lease** serializes turns. A second session reaching for the bridge mid-turn
gets `gateway_busy` naming the current owner, instead of interleaving two
conversations in one tab.
* **Reaping** drops entries whose process has exited or whose last activity
predates the TTL, so a crashed session does not wedge the gateway.
The overlay adds `--browser-session`, which gives each pi session its own
persistent tab.
## Configuration
| Variable | Default | Effect |
| --- | --- | --- |
| `OPENCLI_CHATGPT_DEBUG` | unset | `1` logs every opencli invocation, poll, and gateway transition. |
| `PI_CHATGPT_BRIDGE` | unset | `1` turns on B2 without the flag. |
| `OPENCLI_CHATGPT_MODEL` | unset | Model slug passed through to the ChatGPT UI. |
| `CHATGPT_GATEWAY_LEASE_TTL_SEC` | `120` | Seconds of inactivity before reaping a lease. |
| `OPENCLI_ROOT` | global npm root | Where `apply-overlay.mjs` looks for opencli. |
## Known limits
* **Latency.** The provider polls `opencli chatgpt detail` instead of streaming
tokens. Text arrives per turn, not per token.
* **DOM fragility.** The read path tries ChatGPT's authenticated messages
endpoint first and falls back to scraping. A ChatGPT UI change can break both.
* **One tool call per reply.** The parser reads the first tool fence in a reply.
* **No token accounting.** Cost and token counts report zero. The browser
session does not expose them.
* **Version pin.** `apply-overlay.mjs` compares md5s against opencli 1.8.6 and
aborts on anything else.
* **One gateway holder, no knob.** The lease throws `gateway_busy` on any active
registry entry that is not the current turn. Concurrent holders would drive
the same Chrome tab, so there is nothing to tune.
## Development
Symlink the checkout into pi's extensions directory so your edits land in the
git working tree without a copy step:
```bash
cd ~/.pi/agent/extensions
mv opencli-chatgpt-provider.ts opencli-chatgpt-provider.ts.bak
mv chatgpt-gateway chatgpt-gateway.bak
ln -s /path/to/pi-chatgpt-web/extensions/opencli-chatgpt-provider.ts .
ln -s /path/to/pi-chatgpt-web/extensions/chatgpt-gateway .
```
pi resolves both links, and the relative `./chatgpt-gateway/*` imports keep
working. Confirm the provider still registers:
```bash
pi --list-models | grep chatgpt-web # expect: chatgpt-web default 128K 8.2K
```
An editor that saves by writing a temp file and renaming replaces the symlink
with a regular file. If your edits stop appearing in `git status`, run `ls -l
~/.pi/agent/extensions` and look for a link that turned into a file.
## Traycer
The provider recognizes Traycer A2A agent-message envelopes and routes replies
through `traycer_send_message` when the envelope expects one. Skip this section
if you do not run Traycer.
## License
Apache-2.0. See [`LICENSE`](LICENSE) and [`NOTICE`](NOTICE). The files under
`overlays/patches/` modify Apache-2.0 code from
[opencli](https://github.com/jackwener/opencli). They patch the copy you
installed; this repo redistributes no upstream source.