@@ -71,6 +71,8 @@ def _sigint_handler() -> None:
71
71
signal .signal (signal .SIGINT , lambda * _ : _sigint_handler ())
72
72
73
73
# Handle inputs (i.e. env variables injection)
74
+ resolved_inputs : dict [str , str ] = {}
75
+
74
76
if len (inputs ) > 0 :
75
77
print (
76
78
"[bold blue]Some initial inputs are required by the agent. "
@@ -79,19 +81,26 @@ def _sigint_handler() -> None:
79
81
for input_item in inputs :
80
82
input_id = input_item ["id" ]
81
83
description = input_item ["description" ]
82
- env_special_value = "${input:" + input_id + "}" # Special value to indicate env variable injection
84
+ env_special_value = f "${{ input:{ input_id } }}"
83
85
84
- # Check env variables that will use this input
85
- input_vars = set ()
86
+ # Check if the input is used by any server or as an apiKey
87
+ input_usages = set ()
86
88
for server in servers :
87
89
# Check stdio's "env" and http/sse's "headers" mappings
88
90
env_or_headers = server .get ("env" , {}) if server ["type" ] == "stdio" else server .get ("headers" , {})
89
91
for key , value in env_or_headers .items ():
90
92
if env_special_value in value :
91
- input_vars .add (key )
93
+ input_usages .add (key )
94
+
95
+ raw_api_key = config .get ("apiKey" )
96
+ if isinstance (raw_api_key , str ) and env_special_value in raw_api_key :
97
+ input_usages .add ("apiKey" )
92
98
93
- if not input_vars :
94
- print (f"[yellow]Input { input_id } defined in config but not used by any server.[/yellow]" )
99
+ if not input_usages :
100
+ print (
101
+ f"[yellow]Input '{ input_id } ' defined in config but not used by any server or as an API key."
102
+ " Skipping.[/yellow]"
103
+ )
95
104
continue
96
105
97
106
# Prompt user for input
@@ -104,30 +113,39 @@ def _sigint_handler() -> None:
104
113
if exit_event .is_set ():
105
114
return
106
115
107
- # Inject user input (or env variable) into stdio's env or http/sse's headers
116
+ # Fallback to environment variable when user left blank
117
+ final_value = user_input
118
+ if not final_value :
119
+ final_value = os .getenv (env_variable_key , "" )
120
+ if final_value :
121
+ print (f"[green]Value successfully loaded from '{ env_variable_key } '[/green]" )
122
+ else :
123
+ print (
124
+ f"[yellow]No value found for '{ env_variable_key } ' in environment variables. Continuing.[/yellow]"
125
+ )
126
+ resolved_inputs [input_id ] = final_value
127
+
128
+ # Inject resolved value (can be empty) into stdio's env or http/sse's headers
108
129
for server in servers :
109
130
env_or_headers = server .get ("env" , {}) if server ["type" ] == "stdio" else server .get ("headers" , {})
110
131
for key , value in env_or_headers .items ():
111
132
if env_special_value in value :
112
- if user_input :
113
- env_or_headers [key ] = env_or_headers [key ].replace (env_special_value , user_input )
114
- else :
115
- value_from_env = os .getenv (env_variable_key , "" )
116
- env_or_headers [key ] = env_or_headers [key ].replace (env_special_value , value_from_env )
117
- if value_from_env :
118
- print (f"[green]Value successfully loaded from '{ env_variable_key } '[/green]" )
119
- else :
120
- print (
121
- f"[yellow]No value found for '{ env_variable_key } ' in environment variables. Continuing.[/yellow]"
122
- )
133
+ env_or_headers [key ] = env_or_headers [key ].replace (env_special_value , final_value )
123
134
124
135
print ()
125
136
137
+ raw_api_key = config .get ("apiKey" )
138
+ if isinstance (raw_api_key , str ):
139
+ substituted_api_key = raw_api_key
140
+ for input_id , val in resolved_inputs .items ():
141
+ substituted_api_key = substituted_api_key .replace (f"${{input:{ input_id } }}" , val )
142
+ config ["apiKey" ] = substituted_api_key
126
143
# Main agent loop
127
144
async with Agent (
128
145
provider = config .get ("provider" ), # type: ignore[arg-type]
129
146
model = config .get ("model" ),
130
147
base_url = config .get ("endpointUrl" ), # type: ignore[arg-type]
148
+ api_key = config .get ("apiKey" ),
131
149
servers = servers , # type: ignore[arg-type]
132
150
prompt = prompt ,
133
151
) as agent :
0 commit comments