Skip to content

Commit 631fe16

Browse files
committed
feat(web): point provider environment variables at a secret provider
Signed-off-by: Yordis Prieto <[email protected]>
1 parent 61b2f69 commit 631fe16

5 files changed

Lines changed: 197 additions & 6 deletions

File tree

‎apps/web/src/components/settings/AddProviderInstanceDialog.tsx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import { resolveOfficialAcpRegistryIconUrl } from "./AcpRegistryIcon";
5555
import { AddManagedCodexAccountDialog } from "./CodexSetupSection";
5656
import { MultipleAccountsNote } from "./MultipleAccountsNote";
5757
import { ProviderEnvironmentSection } from "./ProviderInstanceCard";
58+
import { useSecretSources } from "./SecretSourceInput";
5859

5960
/** Limit the slug to 48 chars so the composed ProviderInstanceId fits its 64-char cap. */
6061
function slugifyLabel(value: string): string {
@@ -160,6 +161,7 @@ export function AddProviderInstanceDialog({
160161
onCreated,
161162
}: AddProviderInstanceDialogProps) {
162163
const settings = useEnvironmentSettings(environmentId);
164+
const secretSources = useSecretSources(environmentId);
163165
const persistProviderInstance = usePersistEnvironmentProviderInstanceMutation(environmentId);
164166
const canManageProviders = useEnvironmentScope(environmentId, AuthProvidersManageScope);
165167

@@ -536,6 +538,7 @@ export function AddProviderInstanceDialog({
536538
<ProviderEnvironmentSection
537539
environment={localEnvironment}
538540
onChange={setLocalEnvironment}
541+
secretSources={secretSources}
539542
/>
540543
) : null}
541544
</SettingsGroup>

‎apps/web/src/components/settings/ProviderInstanceCard.test.ts‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { describe, expect, it } from "vite-plus/test";
2-
import type { ServerProviderModel } from "@t3tools/contracts";
2+
import { SecretProviderId, type ServerProviderModel } from "@t3tools/contracts";
33

44
import {
55
deriveProviderModelsForDisplay,
6+
nextProviderEnvironmentWithFieldSecret,
67
nextProviderEnvironmentWithFieldValue,
78
} from "./ProviderInstanceCard";
89

@@ -75,6 +76,20 @@ describe("provider environment helpers", () => {
7576
).toEqual([{ name: "CURSOR_API_KEY", value: "new-key", sensitive: true }]);
7677
});
7778

79+
it("points a stored provider secret at a secret provider and drops the stored value", () => {
80+
const secret = {
81+
provider: SecretProviderId.make("personal"),
82+
reference: "op://Private/Cursor/credential",
83+
};
84+
const stored = [{ name: "CURSOR_API_KEY", value: "", sensitive: true, valueRedacted: true }];
85+
const referenced = nextProviderEnvironmentWithFieldSecret(stored, cursorApiKeyField, secret);
86+
87+
expect(referenced).toEqual([{ name: "CURSOR_API_KEY", value: "", sensitive: true, secret }]);
88+
expect(
89+
nextProviderEnvironmentWithFieldSecret(referenced, cursorApiKeyField, undefined),
90+
).toEqual([]);
91+
});
92+
7893
it("applies the secure field default when replacing an existing non-sensitive value", () => {
7994
expect(
8095
nextProviderEnvironmentWithFieldValue(

‎apps/web/src/components/settings/ProviderInstanceCard.tsx‎

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
type EnvironmentId,
2727
type ProjectId,
2828
type ProviderDriverKind,
29+
type SecretReference,
2930
type ServerProvider,
3031
type ServerProviderModel,
3132
} from "@t3tools/contracts";
@@ -43,7 +44,7 @@ import { Badge } from "../ui/badge";
4344
import { Button } from "../ui/button";
4445
import { MorphIcon } from "~/components/MorphIcon";
4546
import { DraftInput } from "../ui/draft-input";
46-
import { SecretInput } from "./SecretInput";
47+
import { SecretSourceInput, type SecretSource } from "./SecretSourceInput";
4748
import { Popover, PopoverPopup, PopoverTrigger } from "../ui/popover";
4849
import { Switch } from "../ui/switch";
4950
import { stackedThreadToast, toastManager } from "../ui/toast";
@@ -101,6 +102,7 @@ type EnvironmentDraftRow = {
101102
readonly value: string;
102103
readonly sensitive: boolean;
103104
readonly valueRedacted?: boolean;
105+
readonly secret?: SecretReference;
104106
};
105107

106108
function makeEnvironmentDraftRow(
@@ -113,6 +115,7 @@ function makeEnvironmentDraftRow(
113115
value: variable.value,
114116
sensitive: variable.sensitive,
115117
...(variable.valueRedacted !== undefined ? { valueRedacted: variable.valueRedacted } : {}),
118+
...(variable.secret !== undefined ? { secret: variable.secret } : {}),
116119
};
117120
}
118121

@@ -132,7 +135,9 @@ function providerEnvironmentsEqual(
132135
variable.name === other.name &&
133136
variable.value === other.value &&
134137
variable.sensitive === other.sensitive &&
135-
variable.valueRedacted === other.valueRedacted
138+
variable.valueRedacted === other.valueRedacted &&
139+
variable.secret?.provider === other.secret?.provider &&
140+
variable.secret?.reference === other.secret?.reference
136141
);
137142
})
138143
);
@@ -247,12 +252,28 @@ export function nextProviderEnvironmentWithFieldValue(
247252
return next;
248253
}
249254

255+
export function nextProviderEnvironmentWithFieldSecret(
256+
environment: ReadonlyArray<ProviderInstanceEnvironmentVariable> | undefined,
257+
field: ProviderEnvironmentField,
258+
secret: SecretReference | undefined,
259+
): ReadonlyArray<ProviderInstanceEnvironmentVariable> {
260+
const others = providerEnvironmentWithoutNames(environment, new Set([field.name]));
261+
return secret === undefined
262+
? others
263+
: [...others, { name: field.name, value: "", sensitive: true, secret }];
264+
}
265+
250266
function ProviderEnvironmentFieldRow(props: {
251267
readonly field: ProviderEnvironmentField;
252268
readonly variable: ProviderInstanceEnvironmentVariable | undefined;
253269
readonly idPrefix: string;
254270
readonly onCommit: (field: ProviderEnvironmentField, value: string) => void;
271+
readonly onSecretChange: (
272+
field: ProviderEnvironmentField,
273+
secret: SecretReference | undefined,
274+
) => void;
255275
readonly onRemove: (field: ProviderEnvironmentField) => void;
276+
readonly secretSources: ReadonlyArray<SecretSource>;
256277
}) {
257278
const inputId = `${props.idPrefix}-environment-${props.field.name}`;
258279
const value = props.variable?.valueRedacted ? "" : (props.variable?.value ?? "");
@@ -275,7 +296,7 @@ function ProviderEnvironmentFieldRow(props: {
275296
spellCheck={false}
276297
/>
277298
) : (
278-
<SecretInput
299+
<SecretSourceInput
279300
id={inputId}
280301
size="sm"
281302
className="min-w-0 flex-1"
@@ -284,6 +305,10 @@ function ProviderEnvironmentFieldRow(props: {
284305
placeholder={props.field.placeholder}
285306
configured={props.variable?.valueRedacted === true}
286307
spellCheck={false}
308+
aria-label={props.field.label}
309+
sources={props.secretSources}
310+
secret={props.variable?.secret}
311+
onSecretChange={(secret) => props.onSecretChange(props.field, secret)}
287312
/>
288313
)}
289314
{props.variable ? (
@@ -306,6 +331,7 @@ function ProviderEnvironmentFieldRow(props: {
306331
export function ProviderEnvironmentSection(props: {
307332
readonly environment: ReadonlyArray<ProviderInstanceEnvironmentVariable>;
308333
readonly onChange: (environment: ReadonlyArray<ProviderInstanceEnvironmentVariable>) => void;
334+
readonly secretSources?: ReadonlyArray<SecretSource>;
309335
}) {
310336
const [storedDraft, setDraft] = useState(() =>
311337
seedRowsDraft(props.environment, toEnvironmentDraftRows),
@@ -330,7 +356,8 @@ export function ProviderEnvironmentSection(props: {
330356
name.length > 0 ||
331357
row.value.length > 0 ||
332358
row.sensitive !== true ||
333-
row.valueRedacted !== undefined
359+
row.valueRedacted !== undefined ||
360+
row.secret !== undefined
334361
) {
335362
return;
336363
}
@@ -357,6 +384,16 @@ export function ProviderEnvironmentSection(props: {
357384
publishRows(nextRows);
358385
};
359386

387+
const setVariableSecret = (id: string, secret: SecretReference | undefined) => {
388+
const nextRows = rows.map((row) => {
389+
if (row.id !== id) return row;
390+
const { secret: _secret, valueRedacted: _valueRedacted, ...rest } = row;
391+
return { ...rest, value: "", ...(secret !== undefined ? { secret } : {}) };
392+
});
393+
setRows(nextRows);
394+
publishRows(nextRows);
395+
};
396+
360397
const removeVariable = (id: string) => {
361398
const nextRows = rows.filter((row) => row.id !== id);
362399
setRows(nextRows);
@@ -403,7 +440,7 @@ export function ProviderEnvironmentSection(props: {
403440
=
404441
</span>
405442
{variable.sensitive ? (
406-
<SecretInput
443+
<SecretSourceInput
407444
size="sm"
408445
font="mono"
409446
className="min-w-0 flex-1"
@@ -413,6 +450,9 @@ export function ProviderEnvironmentSection(props: {
413450
configured={variable.valueRedacted === true}
414451
spellCheck={false}
415452
aria-label={`Environment variable value ${index + 1}`}
453+
sources={props.secretSources ?? []}
454+
secret={variable.secret}
455+
onSecretChange={(secret) => setVariableSecret(variable.id, secret)}
416456
/>
417457
) : (
418458
<DraftInput
@@ -444,6 +484,7 @@ export function ProviderEnvironmentSection(props: {
444484
});
445485
}}
446486
aria-pressed={variable.sensitive}
487+
disabled={variable.secret !== undefined}
447488
aria-label={`Mark environment variable ${variable.name || index + 1} as sensitive`}
448489
>
449490
<MorphIcon
@@ -584,6 +625,7 @@ interface ProviderInstanceCardProps {
584625
readonly isUpdating?: boolean | undefined;
585626
readonly onAcceptUrlAuth?: ((action: AcpRegistryUrlAuthAction) => void) | undefined;
586627
readonly environmentId?: EnvironmentId | undefined;
628+
readonly secretSources?: ReadonlyArray<SecretSource>;
587629
readonly projectOverride?: ProjectOverrideControl | undefined;
588630
readonly acpProjects?:
589631
| ReadonlyArray<{
@@ -595,6 +637,7 @@ interface ProviderInstanceCardProps {
595637
}
596638

597639
const EMPTY_ACP_PROJECTS: NonNullable<ProviderInstanceCardProps["acpProjects"]> = [];
640+
const NO_SECRET_SOURCES: ReadonlyArray<SecretSource> = [];
598641
const NO_CUSTOM_MODELS: ReadonlyArray<CustomModelDefinition> = [];
599642

600643
/** Unknown drivers stay read-only to preserve fork config. projectOverride switches affect only the
@@ -627,6 +670,7 @@ export function ProviderInstanceCard({
627670
onAcceptUrlAuth,
628671
environmentId,
629672
acpProjects = EMPTY_ACP_PROJECTS,
673+
secretSources = NO_SECRET_SOURCES,
630674
projectOverride,
631675
}: ProviderInstanceCardProps) {
632676
const enabled = projectOverride?.effectiveEnabled ?? resolveProviderInstanceEnabled(instance);
@@ -781,6 +825,12 @@ export function ProviderInstanceCard({
781825
const updateEnvironmentField = (field: ProviderEnvironmentField, value: string) => {
782826
updateEnvironment(nextProviderEnvironmentWithFieldValue(instance.environment, field, value));
783827
};
828+
const updateEnvironmentFieldSecret = (
829+
field: ProviderEnvironmentField,
830+
secret: SecretReference | undefined,
831+
) => {
832+
updateEnvironment(nextProviderEnvironmentWithFieldSecret(instance.environment, field, secret));
833+
};
784834
const removeEnvironmentField = (field: ProviderEnvironmentField) => {
785835
updateEnvironment(providerEnvironmentWithoutNames(instance.environment, new Set([field.name])));
786836
};
@@ -1182,7 +1232,9 @@ export function ProviderInstanceCard({
11821232
variable={readProviderEnvironmentVariable(instance.environment, field.name)}
11831233
idPrefix={`provider-instance-${instanceId}`}
11841234
onCommit={updateEnvironmentField}
1235+
onSecretChange={updateEnvironmentFieldSecret}
11851236
onRemove={removeEnvironmentField}
1237+
secretSources={secretSources}
11861238
/>
11871239
))}
11881240
</div>
@@ -1225,6 +1277,7 @@ export function ProviderInstanceCard({
12251277
<ProviderEnvironmentSection
12261278
environment={genericEnvironment}
12271279
onChange={updateGenericEnvironment}
1280+
secretSources={secretSources}
12281281
/>
12291282
</SettingsSection>
12301283
{environmentId !== undefined && liveProvider?.driver === "acpRegistry" ? (

‎apps/web/src/components/settings/ProviderSettingsPanel.tsx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ import { stackedThreadToast, toastManager } from "../ui/toast";
9898
import { AddProviderInstanceDialog } from "./AddProviderInstanceDialog";
9999
import { ExpandableText } from "./ExpandableText";
100100
import { ProviderInstanceCard } from "./ProviderInstanceCard";
101+
import { useSecretSources } from "./SecretSourceInput";
101102
import { UsageProviderSettings } from "./UsageProviderSettings";
102103
import { ProviderSetupSection, readAntigravityAuthMethod } from "./ProviderSetupSection";
103104
import { ProviderAuthenticationSection } from "./ProviderAuthenticationSection";
@@ -1247,6 +1248,7 @@ function ProviderInstanceRow({
12471248
readonly actions: ProviderInstanceRowActions;
12481249
}) {
12491250
const allProjects = useProjects();
1251+
const secretSources = useSecretSources(environmentId);
12501252
const acpProjects =
12511253
mode === "editor" && liveProvider?.driver === "acpRegistry"
12521254
? allProjects.filter((project) => project.environmentId === environmentId)
@@ -1285,6 +1287,7 @@ function ProviderInstanceRow({
12851287
return (
12861288
<ProviderInstanceCard
12871289
environmentId={environmentId}
1290+
secretSources={secretSources}
12881291
acpProjects={acpProjects}
12891292
projectOverride={projectOverride}
12901293
onAcceptUrlAuth={

0 commit comments

Comments
 (0)