|
| 1 | +# @posthog/harness |
| 2 | + |
| 3 | +Spawn the [pi.dev](https://pi.dev) coding agent — both its **CLI** and its **SDK** — against the |
| 4 | +PostHog LLM gateway, authenticated with the same OAuth flow as PostHog Code. |
| 5 | + |
| 6 | +Harness registers a pi provider named `posthog` that: |
| 7 | + |
| 8 | +- points pi's `anthropic-messages` API at the region's LLM gateway |
| 9 | + (`https://gateway.<region>.posthog.com/posthog_code`), |
| 10 | +- authenticates with a PostHog OAuth access token (`pha_…`), obtained through the same |
| 11 | + Authorization-Code + PKCE flow PostHog Code uses (same client IDs, scopes, and `/oauth/authorize` |
| 12 | + + `/oauth/token` endpoints from `@posthog/shared`), and |
| 13 | +- lets pi own credential storage and refresh via its provider `oauth` hooks. |
| 14 | + |
| 15 | +Because the token, OAuth client, and gateway product (`posthog_code`) are identical to PostHog Code, |
| 16 | +gateway results are identical as well. |
| 17 | + |
| 18 | +## Models |
| 19 | + |
| 20 | +The model list is fetched from the gateway's `/{product}/v1/models` at startup, so harness exposes |
| 21 | +whatever models the gateway currently serves — including OpenAI + codex (`gpt-5.5`, `gpt-5.4`, |
| 22 | +`gpt-5.3-codex`, …) and GLM (`@cf/zai-org/glm-5.2`). Each model is routed by owner: |
| 23 | + |
| 24 | +- Anthropic + Cloudflare/GLM models → pi's `anthropic-messages` API on `<gateway>/posthog_code` |
| 25 | +- OpenAI + codex models → pi's `openai-responses` API on `<gateway>/posthog_code/v1` |
| 26 | + |
| 27 | +If the fetch fails, returns no models, or `PI_OFFLINE` / `HARNESS_STATIC_MODELS` is set, a bundled |
| 28 | +fallback list is used instead. Select any model with `--model posthog/<id>` (e.g. |
| 29 | +`--model posthog/gpt-5.3-codex`, `--model "posthog/@cf/zai-org/glm-5.2"`). |
| 30 | + |
| 31 | +## OAuth flow and region selection |
| 32 | + |
| 33 | +`harness /login` runs an Authorization-Code + PKCE flow: |
| 34 | + |
| 35 | +1. Determines the region: if `POSTHOG_REGION` (or an explicit `region` option) is set, it's used |
| 36 | + directly; otherwise the login prompts interactively for the region to use, offering `United |
| 37 | + States` and `European Union` (`dev` is not offered interactively — it's reachable only via |
| 38 | + `POSTHOG_REGION=dev`). |
| 39 | +2. Generates a PKCE code verifier/challenge (`S256`) and a random `state`. |
| 40 | +3. Starts a loopback HTTP server on `127.0.0.1:<port>` at `/callback` |
| 41 | + (port from `HARNESS_OAUTH_PORT`, default `8237`). |
| 42 | +4. Builds the authorize URL for the resolved region with the same `client_id`, `scope`, and |
| 43 | + `required_access_level=project` as PostHog Code, and opens it in the default browser. |
| 44 | +5. Waits for the browser redirect to hit `/callback` with `code` and matching `state` (rejects on an |
| 45 | + `error` param, missing `code`, a `state` mismatch, a 180s timeout, or cancellation). |
| 46 | +6. Exchanges the code for tokens via `POST <cloudUrl>/oauth/token`. |
| 47 | +7. Stores `OAuthCredentials` (`access`, `refresh`, `expires`, `region`) for pi to reuse and refresh. |
| 48 | + |
| 49 | +Token refresh posts `grant_type=refresh_token` to the same token endpoint, using the region stored in |
| 50 | +the credentials. |
| 51 | + |
| 52 | +The provider also implements pi's `oauth.modifyModels` hook: whenever pi (re)loads models for this |
| 53 | +provider — at startup with a previously-stored credential, and again immediately after a successful |
| 54 | +login — it rewrites every model's `baseUrl` to match the region stored in that credential. This means |
| 55 | +the region chosen at login always wins for routing requests, regardless of what region the provider |
| 56 | +was initially registered with (e.g. before any login had happened). |
| 57 | + |
| 58 | +## CLI |
| 59 | + |
| 60 | +```bash |
| 61 | +harness # interactive pi, with the posthog provider available |
| 62 | +harness /login # sign in via the PostHog OAuth flow; prompts for a region if none is set |
| 63 | +harness -p "hi" --model posthog/claude-opus-4-8 |
| 64 | +``` |
| 65 | + |
| 66 | +`POSTHOG_REGION` (`us` / `eu` / `dev`) is optional: if set, it's used directly (skipping the region |
| 67 | +prompt at login) and the interactive prompt is skipped entirely. If unset, the initial (pre-login) |
| 68 | +provider registration defaults to `us` for model discovery, and `/login` prompts for the actual |
| 69 | +region to authenticate against — which then takes over routing via `modifyModels` above. The OAuth |
| 70 | +loopback callback port can be overridden with `HARNESS_OAUTH_PORT` (default `8237`). |
| 71 | + |
| 72 | +## Spawn the CLI as a subprocess |
| 73 | + |
| 74 | +```ts |
| 75 | +import { spawnPiCli } from "@posthog/harness/spawn"; |
| 76 | + |
| 77 | +const child = spawnPiCli(["-p", "list the files", "--model", "posthog/claude-opus-4-8"], { |
| 78 | + env: { POSTHOG_REGION: "us" }, |
| 79 | +}); |
| 80 | +``` |
| 81 | + |
| 82 | +`spawnPiCli` launches the real `pi` binary with the PostHog provider loaded as an extension. |
| 83 | + |
| 84 | +## SDK |
| 85 | + |
| 86 | +```ts |
| 87 | +import { createHarnessSession } from "@posthog/harness/session"; |
| 88 | + |
| 89 | +const session = await createHarnessSession({ region: "us", model: "claude-opus-4-8" }); |
| 90 | +session.subscribe((event) => { |
| 91 | + if (event.type === "message_update" && event.assistantMessageEvent.type === "text_delta") { |
| 92 | + process.stdout.write(event.assistantMessageEvent.delta); |
| 93 | + } |
| 94 | +}); |
| 95 | +await session.prompt("What files are in the current directory?"); |
| 96 | +``` |
| 97 | + |
| 98 | +The SDK reuses whatever credential `harness /login` stored, or accepts a static `apiKey` (a `pha_` |
| 99 | +token) for headless use. |
| 100 | + |
| 101 | +## Entry points |
| 102 | + |
| 103 | +| Import | What | |
| 104 | +| --- | --- | |
| 105 | +| `@posthog/harness/cli` (bin `harness`) | pi CLI in-process with the PostHog provider | |
| 106 | +| `@posthog/harness/spawn` | `spawnPiCli()` — spawn pi as a subprocess | |
| 107 | +| `@posthog/harness/session` | `createHarnessSession()` — pi SDK `AgentSession` | |
| 108 | +| `@posthog/harness/extensions` | extension registry | |
| 109 | +| `@posthog/harness/extensions/hog-branding` | startup header rebrand — `createHogBrandingExtension()` | |
| 110 | +| `@posthog/harness/extensions/posthog-provider` | default pi extension — `createPosthogProviderExtension()` | |
| 111 | +| `@posthog/harness/extensions/posthog-provider/provider` | `POSTHOG_PROVIDER_NAME`, `buildPosthogProvider()`, `resolvePosthogProvider()` | |
| 112 | +| `@posthog/harness/extensions/posthog-provider/oauth` | `loginPosthog()`, `refreshPosthog()`, `buildAuthorizeUrl()`, `getRedirectUri()`, `getCallbackPort()` | |
| 113 | +| `@posthog/harness/extensions/posthog-provider/gateway` | `getGatewayBaseUrl()`, `getLlmGatewayUrl()`, `resolveRegion()`, `GATEWAY_PRODUCT` | |
| 114 | +| `@posthog/harness/extensions/posthog-provider/models` | `resolveModelConfigs()`, `fallbackModelConfigs()`, `DEFAULT_MODEL`, `GatewayModel` | |
0 commit comments