Files
buzz/desktop/src/features/agents/ui/cardMintKeyUtils.ts
Will Pfleger f810a2f49e fix(desktop): make OpenAI key re-enterable after first save in card mint dialog (#4140)
Fixes a write-once dead-end in the card mint dialog where a user with an
expired OpenAI key had no way to replace it.

**Source-aware key status (Rust + TypeScript).** `card_mint_key_status`
returns a layer discriminant (`"none" | "global" | "persona" | "agent" |
"process"`) instead of a boolean. A pure `resolve_key_layer()` helper in
`card.rs` owns the classification logic; `card_mint_key_status`
delegates to it, so the production path is under direct test with no
duplicate logic.

**Mint form always reachable.** The key panel replaces the mint form
only for `none` (first-time setup) or when the user explicitly opens the
edit panel (`editingKey`). Keys from agent/persona/process layers show
an inline provenance row on the mint form with a "Why?" affordance;
clicking it shows the read-only redirect in a panel with a Cancel button
that returns to the mint form — never a terminal state.

**Precise auth-error matching.** The 401 handling in `cardMintStore.ts`
matches `startsWith("Card mint failed (HTTP 401 ")` plus the specific
`Incorrect API key` text, so avatar-fetch 401 errors pass through
unchanged.

**Tri-state key status row.** "Using your saved OpenAI key · Update"
renders only when `keyLayer === "global"` (confirmed writable key).
Query pending or errored hides the row without asserting key existence.

**Real tests.** Panel visibility derivations live in
`cardMintKeyUtils.ts`, which `AgentCardMintDialog.tsx` imports directly.
Tests cover all layers including the mint-reachability invariant (Mint
reachable for every resolved layer; only `none` gates setup).

- `card.rs` — new `resolve_key_layer()` pure helper;
`card_mint_key_status` delegates to it; 999 lines (under the 1000-line
ratchet)
- `card/tests.rs` — precedence test calls `resolve_key_layer()` directly
(no test-local closure); adds process-layer and blank-value cases
- `tauriPersonas.ts` — `CardMintKeyLayer` type; updated
`cardMintKeyStatus` signature
- `cardMintKeyUtils.ts` — `showKeyPanel`, `showReadOnlyRow`,
`showCancelButton`, `keyPanelTitle`, and helpers; component imports all
of them
- `AgentCardMintDialog.tsx` — inline provenance rows for all key
sources; key panel only for setup/edit; no unused variables
- `cardMintStore.ts` — precise 401 prefix matching
- `e2eBridge.ts` — `card_mint_key_status` stub returns `"global"` (not
boolean)
- Tests: 3959 JS passing, 2089 Rust passing, `tsc --noEmit` clean

Related: [block/buzz#4406](https://github.com/block/buzz/pull/4406)

---------

Signed-off-by: Will Pfleger <pfleger.will@gmail.com>
Co-authored-by: npub1ng3jzsaqxdhrfq22dg85j3lpr0zsh3jp7g2h9jyxl59wraayapnsu6kvfg <9a232143a0336e34814a6a0f4947e11bc50bc641f21572c886fd0ae1f7a4e867@buzz.block.builderlab.xyz>
2026-08-03 11:12:09 -04:00

76 lines
2.3 KiB
TypeScript

import type { CardMintKeyLayer } from "@/shared/api/tauriPersonas";
/**
* Pure derivations for the key-setup panel visibility in `AgentCardMintDialog`.
*
* Extracted so that: (a) the component imports and uses the exact same logic
* as the tests verify, and (b) changes to panel conditions are caught by
* test failures rather than silently diverging.
*/
/** Whether the key panel should be shown (setup or user-initiated update only). */
export function showKeyPanel(
keyLayer: CardMintKeyLayer | undefined,
editingKey: boolean,
): boolean {
return keyLayer === "none" || editingKey;
}
/**
* Whether the resolved layer is writable from the dialog.
* Only `"global"` (and unset/`"none"`) can be updated via the dialog seam.
*/
export function isWritableLayer(
keyLayer: CardMintKeyLayer | undefined,
): boolean {
return keyLayer === "global" || keyLayer === "none";
}
/**
* Whether the resolved layer cannot be updated from the dialog (key would be
* shadowed by the higher-priority layer even if global were updated).
*/
export function isReadOnlyLayer(
keyLayer: CardMintKeyLayer | undefined,
): boolean {
return (
keyLayer === "agent" || keyLayer === "persona" || keyLayer === "process"
);
}
/** Whether the "Cancel" button should be shown (update mode only, with a mint form to return to). */
export function showCancelButton(
keyLayer: CardMintKeyLayer | undefined,
editingKey: boolean,
): boolean {
return editingKey && keyLayer !== "none";
}
/** Whether the "Using your saved OpenAI key · Update" status row should be shown. */
export function showKeyStatusRow(
keyLayer: CardMintKeyLayer | undefined,
editingKey: boolean,
): boolean {
return keyLayer === "global" && !editingKey;
}
/** Whether the read-only provenance row should be shown on the mint form. */
export function showReadOnlyRow(
keyLayer: CardMintKeyLayer | undefined,
editingKey: boolean,
): boolean {
return isReadOnlyLayer(keyLayer) && !editingKey;
}
/** The header title for the key-setup panel. */
export function keyPanelTitle(
keyLayer: CardMintKeyLayer | undefined,
editingKey: boolean,
): string {
if (isReadOnlyLayer(keyLayer)) return "OpenAI API key";
if (keyLayer === "none") return "One-time setup: OpenAI API key";
return editingKey
? "Update OpenAI API key"
: "One-time setup: OpenAI API key";
}