@@ -18,6 +18,7 @@ import { PersonaDropdownField } from "./PersonaDropdownField";
1818import type { EnvVarsValue } from "./EnvVarsEditor" ;
1919import { PersonaAdvancedFields } from "./PersonaAdvancedFields" ;
2020import { PersonaModelField } from "./PersonaModelField" ;
21+ import { PersonaProviderApiKeyField } from "./PersonaProviderApiKeyField" ;
2122import {
2223 canSubmitPersonaDialog ,
2324 formatPersonaNamePoolText ,
@@ -67,8 +68,12 @@ import {
6768 usePersonaModelDiscovery ,
6869} from "./usePersonaModelDiscovery" ;
6970import { useBakedBuildEnvKeysQuery , useRuntimeFileConfigQuery } from "../hooks" ;
70- import { useGlobalAgentConfig } from "../useGlobalAgentConfig" ;
71- import { isBuzzAgentRuntime } from "./buzzAgentConfig" ;
71+ import {
72+ getBakedModelInheritLabel ,
73+ getBakedProviderInheritLabel ,
74+ } from "./bakedEnvHelpers" ;
75+ import { useAgentDialogDefaults } from "./useAgentDialogDefaults" ;
76+ import { useProviderApiKeyFieldState } from "./providerApiKeyFieldState" ;
7277import { buildRuntimeModelProviderPayload } from "./agentDefinitionSubmitPayload" ;
7378
7479type AgentDefinitionDialogProps = {
@@ -147,7 +152,6 @@ export function AgentDefinitionDialog({
147152 const [ showAdvancedFields , setShowAdvancedFields ] = React . useState ( false ) ;
148153 const [ isAvatarUploadPending , setIsAvatarUploadPending ] =
149154 React . useState ( false ) ;
150- const { globalConfig } = useGlobalAgentConfig ( ) ;
151155 const defaultRuntime = React . useMemo (
152156 ( ) => getDefaultPersonaRuntime ( runtimes ) ,
153157 [ runtimes ] ,
@@ -184,12 +188,10 @@ export function AgentDefinitionDialog({
184188 setBehaviorDraft ( nextBehaviorDraft ) ;
185189 setNamePoolText ( nextNamePoolText ) ;
186190 setEnvVars ( nextEnvVars ) ;
187- setShowAdvancedFields (
188- nextNamePoolText . trim ( ) . length > 0 ||
189- Object . keys ( nextEnvVars ) . length > 0 ||
190- nextBehaviorDraft . respondTo !== null ||
191- nextBehaviorDraft . parallelism . trim ( ) . length > 0 ,
192- ) ;
191+ // Item 5: collapsed by default in edit mode — only expand if a non-default
192+ // behavior value demands attention. Having env vars or a name pool is not
193+ // sufficient reason to auto-open.
194+ setShowAdvancedFields ( false ) ;
193195 setIsAvatarUploadPending ( false ) ;
194196 isRuntimeAutoSeededRef . current = false ;
195197 hasSeededForOpenRef . current = false ;
@@ -243,13 +245,9 @@ export function AgentDefinitionDialog({
243245 }
244246
245247 async function handleSubmit ( ) {
246- if (
247- ! initialValues ||
248- ! canSubmitPersonaDialog ( { displayName, isPending } ) ||
249- isAvatarUploadPending
250- ) {
251- return ;
252- }
248+ // D1: the same localModeSatisfied gate as canSubmit prevents form-submit
249+ // (Enter) from bypassing a missing credential.
250+ if ( ! initialValues || ! localModeSatisfied || ! canSubmit ) return ;
253251
254252 const {
255253 runtime : runtimeForSubmit ,
@@ -320,18 +318,28 @@ export function AgentDefinitionDialog({
320318 // locked rows in the env vars editor.
321319 // File-layer config for the selected runtime (e.g. goose config.yaml).
322320 // Used to silence requirements already satisfied there.
323- const { data : runtimeFileConfig } = useRuntimeFileConfigQuery ( runtime , {
324- enabled : open ,
325- } ) ;
326- const { data : bakedEnvKeys } = useBakedBuildEnvKeysQuery ( { enabled : open } ) ;
321+ const { data : runtimeFileConfig , isLoading : fileConfigLoading } =
322+ useRuntimeFileConfigQuery ( runtime , { enabled : open } ) ;
323+ const {
324+ advancedInheritedSummary,
325+ globalConfig,
326+ inheritedDefaults : {
327+ provider : inheritedProviderDefault ,
328+ model : inheritedModelDefault ,
329+ } ,
330+ inheritedEnvVars : inheritedEnvVarsForAdvanced ,
331+ } = useAgentDialogDefaults ( { open } ) ;
332+ const { data : bakedEnvKeys , isLoading : bakedLoading } =
333+ useBakedBuildEnvKeysQuery ( { enabled : open } ) ;
334+ const credentialSettled = ! fileConfigLoading && ! bakedLoading ;
327335 const localModeGate = React . useMemo (
328336 ( ) =>
329337 computeLocalModeGate ( {
330338 bakedEnvKeys,
331339 envVars,
332340 globalEnvVars : globalConfig . env_vars ,
333- globalProvider : globalConfig . provider ?? "" ,
334- globalModel : globalConfig . model ?? "" ,
341+ globalProvider : inheritedProviderDefault . value ,
342+ globalModel : inheritedModelDefault . value ,
335343 isProviderMode : false ,
336344 model,
337345 provider : trimmedProvider ,
@@ -344,8 +352,8 @@ export function AgentDefinitionDialog({
344352 createRunOnMesh ,
345353 envVars ,
346354 globalConfig . env_vars ,
347- globalConfig . provider ,
348- globalConfig . model ,
355+ inheritedModelDefault . value ,
356+ inheritedProviderDefault . value ,
349357 model ,
350358 trimmedProvider ,
351359 runtime ,
@@ -354,13 +362,36 @@ export function AgentDefinitionDialog({
354362 ) ;
355363 // requiredEnvKeys: the gate already handles baked-, global-, and file-
356364 // satisfied keys so no further filtering is needed.
357- const { requiredEnvKeys, missingNormalizedFields } = localModeGate ;
365+ const { requiredEnvKeys } = localModeGate ;
366+ // D1: single boolean for both canSubmit and handleSubmit — never recompose.
367+ const localModeSatisfied = localModeGate . satisfied ;
358368 // Effective provider: agent value → global fallback → file fallback.
359369 // Mirrors the chain inside computeLocalModeGate so model-option scoping and
360370 // model requiredness are consistent with the readiness gate.
361371 const fileProvider = runtimeFileConfig ?. provider ?. trim ( ) ?? "" ;
362372 const effectiveProvider =
363- trimmedProvider || ( globalConfig . provider ?? "" ) . trim ( ) || fileProvider ;
373+ trimmedProvider || inheritedProviderDefault . value || fileProvider ;
374+ // D2: the top-level API key owns display while the full gate remains intact.
375+ const apiKeyFieldState = useProviderApiKeyFieldState ( {
376+ bakedEnvKeys,
377+ effectiveEnvVars : envVars ,
378+ envVars,
379+ fileSatisfiedEnvKeys : localModeGate . fileSatisfiedEnvKeys ,
380+ globalEnvVars : globalConfig . env_vars ,
381+ open,
382+ provider : effectiveProvider ,
383+ requiredEnvKeys,
384+ satisfactionSettled : credentialSettled ,
385+ setShowAdvancedFields,
386+ } ) ;
387+ const {
388+ advancedRequiredEnvKeys,
389+ inheritedLabel : apiKeyInheritedLabel ,
390+ isInherited : apiKeyIsInherited ,
391+ isRequired : apiKeyIsRequired ,
392+ secretEnvVar : topLevelSecretEnvVar ,
393+ value : apiKeyValue ,
394+ } = apiKeyFieldState ;
364395 // Provider required-ness is a static property of the runtime — it does not
365396 // change based on whether the field is currently filled. Using the dynamic
366397 // missingNormalizedFields check would flip the asterisk off once a value is
@@ -387,39 +418,11 @@ export function AgentDefinitionDialog({
387418 // Crash-loop guard, create AND edit: an empty allowlist would crash
388419 // every instance minted from this definition at startup.
389420 personaBehaviorDraftValid ( behaviorDraft ) &&
390- missingNormalizedFields . length === 0 &&
421+ // D1: localModeSatisfied covers both missingNormalizedFields AND
422+ // missingEnvKeys — credential env keys now block submit, not just display.
423+ localModeSatisfied &&
391424 ! isAvatarUploadPending ;
392425
393- // Auto-expand the Advanced section once per dialog-open cycle when required
394- // env keys are present, so the user sees a clear signal that action is
395- // needed (e.g. provider API key required). Does not re-open if the user
396- // manually collapses the section afterward.
397- const hasAutoOpenedAdvancedRef = React . useRef ( false ) ;
398- React . useEffect ( ( ) => {
399- if ( ! open ) {
400- hasAutoOpenedAdvancedRef . current = false ;
401- return ;
402- }
403- if ( requiredEnvKeys . length > 0 && ! hasAutoOpenedAdvancedRef . current ) {
404- hasAutoOpenedAdvancedRef . current = true ;
405- setShowAdvancedFields ( true ) ;
406- }
407- } , [ open , requiredEnvKeys . length ] ) ;
408-
409- // Auto-expand Advanced once per open when the selected runtime is buzz-agent
410- // so the model-tuning knobs are immediately reachable — mirrors the agent
411- // instance dialogs' behavior.
412- const hasAutoOpenedForBuzzAgentRef = React . useRef ( false ) ;
413- React . useEffect ( ( ) => {
414- if ( ! open ) {
415- hasAutoOpenedForBuzzAgentRef . current = false ;
416- return ;
417- }
418- if ( isBuzzAgentRuntime ( runtime ) && ! hasAutoOpenedForBuzzAgentRef . current ) {
419- hasAutoOpenedForBuzzAgentRef . current = true ;
420- setShowAdvancedFields ( true ) ;
421- }
422- } , [ open , runtime ] ) ;
423426 // Merge global env as the base layer so credential keys satisfied via global
424427 // config are available to model discovery — same rationale as in AgentInstanceEditDialog.
425428 const envVarsForDiscovery = React . useMemo (
@@ -471,13 +474,18 @@ export function AgentDefinitionDialog({
471474 const providerOptions = getPersonaProviderOptions (
472475 trimmedProvider ,
473476 runtime ,
474- globalConfig . provider ?? "" ,
477+ inheritedProviderDefault . source === "global"
478+ ? inheritedProviderDefault . value
479+ : "" ,
475480 hideProviderIds ,
476481 ) ;
477- const defaultLlmProviderLabel = getDefaultLlmProviderLabel (
478- runtime ,
479- globalConfig . provider ?? "" ,
480- ) ;
482+ const defaultLlmProviderLabel =
483+ inheritedProviderDefault . source === "build"
484+ ? getBakedProviderInheritLabel (
485+ inheritedProviderDefault . value ,
486+ providerOptions ,
487+ )
488+ : getDefaultLlmProviderLabel ( runtime , inheritedProviderDefault . value ) ;
481489 const providerSelectValue = isCustomProviderEditing
482490 ? CUSTOM_PROVIDER_DROPDOWN_VALUE
483491 : trimmedProvider || AUTO_PROVIDER_DROPDOWN_VALUE ;
@@ -521,15 +529,20 @@ export function AgentDefinitionDialog({
521529 }
522530 const providerDropdownOptions : PersonaDropdownOption [ ] = [
523531 ...providerOptions . map ( ( option ) => ( {
524- label : option . label ,
532+ label : option . id === "" ? defaultLlmProviderLabel : option . label ,
525533 value : option . id || AUTO_PROVIDER_DROPDOWN_VALUE ,
526534 } ) ) ,
527535 { label : "Custom provider..." , value : CUSTOM_PROVIDER_DROPDOWN_VALUE } ,
528536 ] ;
537+ const inheritedModelLabel =
538+ inheritedModelDefault . source === "build"
539+ ? getBakedModelInheritLabel ( inheritedModelDefault . value )
540+ : undefined ;
529541 const modelDropdownOptions : PersonaDropdownOption [ ] = [
530542 ...buildTemplateModelDropdownOptions (
531543 modelOptions ,
532- globalConfig . model ?? "" ,
544+ inheritedModelDefault . value ,
545+ inheritedModelLabel ,
533546 ) ,
534547 ...( modelDiscoveryLoading && discoveredModelOptions === null
535548 ? [
@@ -811,11 +824,30 @@ export function AgentDefinitionDialog({
811824 />
812825 </ div >
813826 ) : null }
814- { /* Provider API key is now surfaced as an amber required row
815- in EnvVarsEditor — no dedicated field needed. */ }
816827 </ div >
817828 ) : null }
818829
830+ { llmProviderFieldVisible && topLevelSecretEnvVar ? (
831+ < PersonaProviderApiKeyField
832+ disabled = { isPending }
833+ isInherited = { apiKeyIsInherited }
834+ inheritedLabel = { apiKeyInheritedLabel }
835+ isRequired = { apiKeyIsRequired }
836+ label = {
837+ effectiveProvider === "anthropic"
838+ ? "Anthropic API Key"
839+ : "OpenAI API Key"
840+ }
841+ onValueChange = { ( next ) => {
842+ setEnvVars ( ( prev ) => ( {
843+ ...prev ,
844+ [ topLevelSecretEnvVar ] : next ,
845+ } ) ) ;
846+ } }
847+ value = { apiKeyValue }
848+ />
849+ ) : null }
850+
819851 < AnimatePresence initial = { false } >
820852 { modelFieldVisible ? (
821853 < PersonaModelField
@@ -850,6 +882,11 @@ export function AgentDefinitionDialog({
850882 ) }
851883 />
852884 </ button >
885+ { ! showAdvancedFields && advancedInheritedSummary ? (
886+ < p className = "text-xs text-muted-foreground" >
887+ { advancedInheritedSummary }
888+ </ p >
889+ ) : null }
853890
854891 < AnimatePresence initial = { false } >
855892 { showAdvancedFields ? (
@@ -866,15 +903,18 @@ export function AgentDefinitionDialog({
866903 disabled = { isPending }
867904 envVars = { envVars }
868905 fileSatisfiedEnvKeys = { localModeGate . fileSatisfiedEnvKeys }
869- inheritedEnvVars = { globalConfig . env_vars }
906+ hiddenEnvKeys = {
907+ topLevelSecretEnvVar ? [ topLevelSecretEnvVar ] : [ ]
908+ }
909+ inheritedEnvVars = { inheritedEnvVarsForAdvanced }
870910 model = { model }
871911 modelTuningRuntimeId = { runtime }
872912 namePoolText = { namePoolText }
873913 onBehaviorDraftChange = { setBehaviorDraft }
874914 onEnvVarsChange = { setEnvVars }
875915 onNamePoolTextChange = { setNamePoolText }
876- provider = { provider }
877- requiredEnvKeys = { requiredEnvKeys }
916+ provider = { effectiveProvider }
917+ requiredEnvKeys = { advancedRequiredEnvKeys }
878918 />
879919 </ motion . div >
880920 ) : null }
0 commit comments