Skip to content

feat(chat): manage own API keys from Chat, with Use for and routing by the model's provider (W4-06) - #2033

Merged
gkhngyk merged 18 commits into
ansvisor:mainfrom
cihan-gyk:feature/chat-own-keys-window
Nov 4, 2026
Merged

gkhngyk merged 18 commits into
ansvisor:mainfrom
cihan-gyk:feature/chat-own-keys-window

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

Own API keys from Chat (CHAT-16 items 19–23, Wave 4 plan W4-06): a Your API keys window, an Add API key form card, Use for per key, routing by the picked model's provider, and Use Ansvisor's limit when a key fails. Own keys remain Enterprise's (the 2026-10-30 decision).

  • Use for: Chat / Images on each saved key (organization_ai_keys.settings.useFor; no migration).

    • A key saved before this counts as Chat only.
    • Anthropic's key is for Chat only, because Anthropic has no image models.
    • PATCH /api/settings/ai-providers { provider, useFor } sets it. A PUT that names no uses keeps the saved ones, so Settings' own form doesn't wipe them.
  • Routing a picked model's steps (resolvePickedModelKey):

    1. the workspace's own key for the model's provider, when it is for Chat;
    2. else its own OpenRouter key, which runs the model by its catalog id;
    3. else Ansvisor's key, on the AI limit.

    Auto keeps running on the key Chat uses (active), now only when that key is for Chat. A key that can't be read stops the step, as before; there is never a silent fallback.

  • Your API keys (your-keys-dialog.tsx) opens from the model menu's own-key row, for Admins where own keys are open. It lists each key with its last four characters, marks the one Auto runs on, and offers the Use for toggles, Test, Add API key and a link to Settings.

  • Add API key (add-key-card.tsx) is a form, never a tool call:

    • provider (Anthropic, OpenAI, Google, OpenRouter; Azure stays in Settings, as it needs its endpoint);
    • the key in a masked field, plus the OpenRouter model;
    • Use for, then Test & save.

    The key goes straight to Settings' route, which tests it before it keeps it; it never reaches the model, the chat or a log. Non-Admins read "Ask your Admin"; below Enterprise, "Own API keys come with Enterprise."

  • From the composer:

    • a message such as "use my OpenAI key" opens the card instead of being sent;
    • W4-05's Save securely now shows for Admins and opens the card with the pasted key and its provider; the key leaves the box.
  • When an own key fails mid-run: the error card keeps "Open AI Providers" and adds Use Ansvisor's limit for Admins while the limit has room. It sends the question again with keySource: 'ansvisor'; the engine takes it from an Admin only. Chat never switches by itself (AGENT-OWN-KEY).

  • Help: help/chat/models.md (Your API keys, when your key stops working) and help/chat/chat.md (Save securely); both approved: false.

Stacked on #2032 (with #2020 merged in, for Save securely). Merged, not rebased.

Related issue

None. Chat Wave 4 plan, W4-06.

Type of change

  • feat — New feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

How to test

On an Enterprise workspace, as an Admin:

  1. Open the model menu and click Included in your plan: Your API keys lists your keys. Toggle Images on an OpenAI key, then Test it.
  2. Click Add API key, pick OpenAI, paste a key, and click Test & save: the card says whether it works.
  3. In the composer, type "use my OpenAI key" and press Enter: the card opens and nothing is sent. Paste a key into a message and click Save securely: the card opens with it.
  4. Pick Sonnet 5.5 with only an OpenRouter key saved: the run's Debug view shows OpenRouter.
  5. As a Manager: no Save securely, and the card says to ask an Admin.
  6. cd web && yarn vitest run src/lib/chat/key-routing.test.ts src/app/api/settings/ai-providers src/components/chat/run src/components/chat/composer src/lib/agent/providers.test.ts.

Screenshots (light and dark, 1024 px): Your API keys, the Add API key card, and the error card with Use Ansvisor's limit.

No migration. No new environment variables (it uses W4-03's optional OPENROUTER_API_KEY).

Checklist

  • Branch follows the naming convention
  • Commits follow Conventional Commits
  • yarn lint passes
  • yarn typecheck passes
  • yarn format:check passes
  • Changes are focused — one concern per PR
  • Help article updated under help/ (or no user-visible change)

Screens: shown to the owner in a local preview (1024px, light and dark) and approved on 2026-11-02.

CI passed on this branch (210d0d5), run on the fork (https://github.com/cihan-gyk/ansvisor/actions/runs/41525032198): schema is up to date; web format:check, lint, typecheck, all tests and yarn build pass; server lint, format:check and all tests pass.

🤖 Generated with Claude Code

cihan-gyk and others added 7 commits November 2, 2026 19:02
…take the key out (W4-05)

The composer finds a pasted key by its provider's shape (sk-ant-, sk-or-v1-,
sk-proj-, sk-, AIza): Send waits, a line says why, and Remove takes the key
out of the text. Save securely shows only where the page offers a key form.
The run routes refuse such a message or typed answer with api_key before
anything keeps it, and never log it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…indow

# Conflicts:
#	web/src/components/chat/composer/chat-composer.tsx
…y the model's provider (W4-06)

- Use for (Chat, Images) on each saved key, in its settings; Anthropic's is
  for Chat only, and a key saved before counts as Chat. PATCH sets it; a
  save that names none keeps it.
- A picked model's steps run on the own key for its provider, then the own
  OpenRouter key, then Ansvisor's; Auto runs on the active key only when it
  is for Chat. An unreadable key still stops the step.
- Your API keys, from the model menu (Admins, where own keys are open):
  each key with Use for, Test and Add API key.
- Add API key: a form straight to Settings' route, tested before it is
  kept, never through the model. "Use my OpenAI key" in the composer and
  Save securely on a pasted key open it; non-Admins and other plans read
  why not.
- When the own key fails, Admins may click "Use Ansvisor's limit" to ask
  again on the AI limit; Chat never switches by itself.
- Help: help/chat/models.md and help/chat/chat.md.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…indow

Opus 5.5 hidden until tested: no key is looked for a pick not offered; it
runs Auto.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added the needs-migration Adds or edits a migration — apply it to the hosted database label Nov 2, 2026
@github-actions github-actions Bot removed the needs-migration Adds or edits a migration — apply it to the hosted database label Nov 3, 2026
cihan-gyk and others added 2 commits November 3, 2026 05:44
Main's replay masking (ansvisor#2009) wants every plain password input to carry ph-no-capture.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… and routing

- asksToAddKey needs an API key, a provider's or own key, or a bare "key"
  that ends the sentence, so "our key competitors" or "key topics" are sent.
- A message that does open the Add API key card stays in the box; sending it
  again sends it to Chat.
- ownChatKeyProvider (and so chatRunsOnOwnKey) counts the active key only
  when it is for Chat, as resolveChatModelKey does.
- The model menu marks Your key per model in resolvePickedModelKey's order:
  an own key for its provider, else an own OpenRouter key, both for Chat
  (ownChatKeyProviders).
gkhngyk
gkhngyk previously approved these changes Nov 3, 2026
…indow

# Conflicts:
#	help/chat/chat.md
#	web/src/app/api/chat/runs/route.ts
#	web/src/components/chat/composer/chat-composer.tsx
#	web/src/lib/chat/engine-model.test.ts
#	web/src/lib/chat/engine.ts
#	web/src/lib/chat/key-paste.test.ts
#	web/src/lib/chat/key-paste.ts
#	web/src/lib/chat/run-api.ts
#	web/src/lib/help/approved-articles.json
gkhngyk
gkhngyk previously approved these changes Nov 4, 2026

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

…indow

# Conflicts:
#	web/src/lib/chat/engine.ts
gkhngyk
gkhngyk previously approved these changes Nov 4, 2026

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@cihan-gyk
cihan-gyk dismissed gkhngyk’s stale review November 4, 2026 17:12

The merge-base changed after approval.

gkhngyk
gkhngyk previously approved these changes Nov 4, 2026

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@cihan-gyk
cihan-gyk dismissed gkhngyk’s stale review November 4, 2026 17:26

The merge-base changed after approval.

…ys-window

# Conflicts:
#	web/src/lib/chat/engine.ts

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@gkhngyk
gkhngyk merged commit 87d0187 into ansvisor:main Nov 4, 2026
7 checks passed
gkhngyk pushed a commit that referenced this pull request Nov 5, 2026
The Agent Center article had "Let an agent publish on its own" twice,
from two pull requests. Merge them into one section that keeps both
sections' facts: publishing new pages on Webflow, WordPress and Wix as
well as CMS drafts, the two switches with Full auto, the Manager's
read-only switch, At most a day (1 to 20, 5 by default), the reasons a
page still waits for a click, and the reset after a change. Add that it
needs Growth and up and only shows where the workspace has it.

"On your own AI key" said every agent runs on the workspace's own key.
Since agents run on their own model (#2032) and a picked model runs on
its provider's key, then the OpenRouter key, then the AI limit (#2033),
that holds for agents on Auto only: say so.


Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9

Co-authored-by: Claude <[email protected]>
gkhngyk pushed a commit that referenced this pull request Nov 5, 2026
The Model step of the agent form (Agent Center > new or edit agent) said
"Runs on your {provider} key" from Auto's key alone. Since #2032 an agent
runs on its own model, and since #2033 a picked model runs on the key for
its provider, then the workspace's OpenRouter key, then Ansvisor's key on
the monthly AI limit. So an agent on Fast or Best writing in a workspace
whose own key is OpenAI ran on the AI limit while the form said OpenAI.

The line and Max per run's note now follow the model the form has chosen,
as the engine picks its key:
- Auto: the key Auto runs on, else the monthly AI limit;
- a picked model: the own key for its provider, else the OpenRouter key,
  else the monthly AI limit;
- the free trial, and a model no longer offered, run Auto: Auto's key.

The order lives in one place: pickedModelKeyOrder and pickedModelOwnKey
(lib/chat/models.ts, safe in the browser). resolvePickedModelKey loops
over pickedModelKeyOrder, the model menu's "Your key" uses
pickedModelOwnKey, and the form's modelKeyProvider uses it with
runChoiceOf. The form's setup carries the providers of the own keys for
Chat (ownChatKeyProviders, no key decrypted) and whether the plan runs a
picked model (modelChoiceOpen).


Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9

Co-authored-by: Claude <[email protected]>
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.

3 participants