Skip to content

feat(ai-gateway): pool Claude and ChatGPT subscriptions - #3452

Merged
yannickmonney merged 8 commits into
mainfrom
feat/ai-gateway-service
Sep 21, 2026
Merged

yannickmonney merged 8 commits into
mainfrom
feat/ai-gateway-service

Conversation

@yannickmonney

@yannickmonney yannickmonney commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

A new service, services/ai-gateway: a gateway that holds the AI subscriptions a team already pays for — Claude Pro/Max and ChatGPT Plus/Pro accounts — and hands their OAuth tokens out through one endpoint.

It is the job cc-gateway does for Claude accounts, rebuilt here on the Tale stack and generalized so a second vendor is a module rather than a rewrite.

What it does

One token endpoint GET /api/tokens answers with every account's access token, its expiry, its status, and the environment variable that hands it to the vendor's CLI
Two providers, one shape Anthropic and OpenAI differ in their OAuth callback, their identity claims and their usage payload; the panel and the endpoint do not
Always-fresh tokens A background pass refreshes each access token ahead of its expiry, so an account stays usable as long as its refresh token does
Usage in view Each account's session and weekly windows — plus any per-model cap the vendor reports — as live bars
Encrypted at rest AES-256-GCM at a pinned 16-byte tag; a tampered store fails loudly rather than decrypting to something plausible
Two doors, kept apart The panel is behind a password; the token endpoint is behind an API key. Neither opens the other

Why it is shaped this way

Both subscriptions are reached through the public OAuth client their own CLI ships with, and they disagree on almost everything below that: Anthropic's console callback prints a code to copy, OpenAI's redirects to a loopback the browser cannot load; Anthropic names the account on a profile endpoint, OpenAI puts it in the id_token; Anthropic reports two general windows plus per-model caps, OpenAI two windows measured in minutes.

A provider module owns those five moves — authorize, exchange, refresh, identity, usage — and everything above it is vendor-agnostic. A third provider is a module and a registry line; the panel reads the catalog from GET /api/providers, so only its providers.<id> label is the client's to add.

The pool itself is one JSON document under AI_GATEWAY_DATA_DIR, written atomically and serialized against itself. A credential pool is a handful of records, and keeping it a file is what lets the service be one process with nothing to provision.

The panel

One screen on @tale/ui: the accounts as a DataTable, each with its vendor mark, its plan, its status, its usage windows as bars, and the three verbs an operator needs — copy the CLI command, re-authenticate, remove. EN/DE/FR throughout, light and dark.

Also in here

  • fix(plop) ×2 — the react service generator produced a service that was red on arrival: bun run gen wrote into tools/plop/ instead of the repo root, vitest.config.ts used yamlImports without importing it, keys-dynamic.yml parsed to null where the i18n check wants a list, four files disagreed with oxfmt, the workspace lint config linted its own dist/, the container image started a server.ts whose imports it could not resolve, and the entrypoint set PORT without exporting it. The scaffold commit is separated from the implementation so the generator's raw output is readable on its own. A test now holds gen to --dest ., because that one is silent when it regresses.
  • feat(ui) — an OpenAIIcon beside the existing ClaudeIcon, same shape and the same path services/web already draws.
  • ci(ai-gateway) — the service shipped a Dockerfile nothing built, so tale-ai-gateway would never have existed and the fleet deployment that pulls it could not run. release.yml publishes it beside the other services; build.yml's filter runs a PR's checks when the service or @tale/ui moves.
  • feat(ai-gateway) monitoring — the fleet gives it a GlitchTip project of its own (ops 787620a), so the server reports failures to it the way docs and ui-docs do.
  • compose.ai-gateway.yml — standalone, following the compose.ui-docs.yml pattern, with a named volume for the pool.

Deploying it

tale-project/ops@787620a adds deployments/tale/ai-gateway/pr on its own droplet at ai.tale.dev and retires cc-gateway. Its image pin is a placeholder until this merges and a release publishes tale-ai-gateway — replace it with the real digest before the first deploy.

Verification

  • bun run check, bun run knip:check, bun run lint:sast, bun run lint:manual — green. (@tale/cli's runtime-configuration.test.ts alternate-mount case is red on this machine before and after; nothing here touches tools/cli.)
  • 156 unit tests across the providers, the store, the crypto, the lifecycle and both doors; 6 Playwright specs against the running panel.
  • Driven in a real browser: sign-in, the accounts table with two seeded providers, both dialog steps, the row menu, the CLI-command copy, light/dark, EN/FR.
  • The bundled production server (bun build server.ts → server.js, which is what the image runs) booted and answered /api/health, /api/tokens and the SPA; the missing-secrets path refuses with the four names.
  • The SAST gate caught a real one: GCM's tag length was left to Node's default, which accepts a forgeable 4-byte tag. Pinned to 16, with a test.

Not in here

No inference proxy, no account rotation, no multi-user panel, no usage history — each recorded with its reason in tests/manual/reference/not-a-finding.md. The OAuth round trip itself cannot be automated (a vendor's consent screen, a real subscription), so it is the manual accounts suite.

`bun run gen` resolved every action path against the plopfile's own
directory, so the documented root-level run wrote `services/<name>/…`
into `tools/plop/`. Point plop at the repo root with `--dest .`.

Three template defects made a freshly generated react service red:

- `vitest.config.ts` called `yamlImports()` but imported it only in the
  Storybook branch, so a service scaffolded without Storybook failed
  `typecheck`.
- `keys-dynamic.yml` ended at a valueless `entries:`, which parses to
  null where the i18n usage check wants a list, so `test` failed on the
  generated messages suite.
- Four files disagreed with `oxfmt` on import and key order, so `bun run
  format:check` failed on the untouched output.
`bun run gen:service ai-gateway … react 3004 false`, plus the pieces the
generator cannot emit itself: the three binary favicons (Handlebars would
corrupt them) and the workspace's `.commitlintrc.json` scope.

Nothing is wired yet — the index route renders the package name. The
gateway itself lands next.
The runner stage carried no `node_modules` but started `bun server.ts`, so
the very first import — `@tale/ui/server` — could not resolve and the image
died on boot. Bundle the server the way `services/web` and `services/docs`
do (`bun build --target=bun --outfile=server.js`) and ship that instead, so
every import the entry makes is inlined.

Two smaller things in the same shape: the entrypoint set `PORT` without
exporting it, so the server never saw it and always took its default; and
the workspace lint config carried no `ignorePatterns`, so a generated
service linted its own `dist/` and its generated route tree and failed
`unicorn/filename-case` on arrival.
Same shape as `ClaudeIcon`, and the same path `services/web` already draws
in its connector set, so the two families stay visually aligned. Registered
in the icon gallery and in the accessibility sweep that renders every mark.
A gateway that holds the AI subscriptions a team already pays for and hands
their OAuth tokens out through one endpoint — the job `cc-gateway` does for
Claude Pro/Max accounts, generalized so a second vendor is a module rather
than a rewrite.

Both subscriptions are reached through the public OAuth client their own CLI
ships with, and they disagree on almost everything below that: Anthropic's
console callback prints a code to copy, OpenAI's redirects to a loopback the
browser cannot load; Anthropic names the account on a profile endpoint,
OpenAI puts it in the id_token; Anthropic reports two general windows plus
per-model caps, OpenAI two windows measured in minutes. A provider module
owns those five moves — authorize, exchange, refresh, identity, usage — and
everything above it is vendor-agnostic, so the panel, the refresh loop and
the token endpoint have one code path. A third provider is a module and a
registry line; the panel reads the catalog from the server, so only its
label is the client's to add.

Two doors, kept apart: the panel is behind one password and never returns a
token, `GET /api/tokens` is behind an API key and returns nothing else.
Neither opens the other — a leaked browser session cannot exfiltrate the
pool, and a machine holding the key cannot add or remove accounts.

Tokens are sealed with AES-256-GCM at a pinned 16-byte tag, so a truncated
tag is refused rather than verified, and a tampered store fails loudly
instead of decrypting to something plausible. The pool itself is one JSON
document under `AI_GATEWAY_DATA_DIR`, written atomically and serialized
against itself — a credential pool is a handful of records, and keeping it a
file is what lets the service be one process with nothing to provision.

The panel is one screen on `@tale/ui`: the accounts as a `DataTable`, each
with its vendor mark, its plan, its status, its usage windows as bars, and
the three verbs an operator needs — copy the CLI command, re-authenticate,
remove. EN/DE/FR throughout.

Production refuses to boot without its four secrets and says which are
missing; development generates them for the process and prints them once.
The Vite dev server mounts the same API the Bun server does, so a route
behaves identically in both.
The `--dest .` half of the invocation is silent when it goes missing: plop
prints the `/services/<name>/…` path it did not use and writes the tree
inside `tools/plop/` instead. A test is the only thing that notices.
The service shipped a Dockerfile and a compose file that nothing built, so
`ghcr.io/tale-project/tale/tale-ai-gateway` would never have existed and the
fleet deployment that pulls it could not run.

`release.yml` publishes it beside the other services, and `build.yml`'s
change filter runs a pull request's checks when the service or the component
library it builds on moves.
The fleet gives the gateway a GlitchTip project of its own; without this
nothing reported to it. Same wiring as docs and ui-docs: absent a DSN it
reports nothing and costs nothing, which is every local run.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant