Skip to content

Commit 1364c33

Browse files
liulei.88innsd
authored andcommitted
feat(config): allow disabling strict region validation
(cherry picked from commit 46cfabd2ac0a8c21889d2f523666a2933f732fea)
1 parent 5c9d4b2 commit 1364c33

File tree

4 files changed

+100
-18
lines changed

4 files changed

+100
-18
lines changed

agentkit/toolkit/cli/cli_config.py

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,25 @@ def _interactive_config(config_file: Optional[str] = None):
319319
console.print(f"[red]❌ Unknown launch type: {strategy_name}[/red]")
320320
raise typer.Exit(1)
321321

322+
# When disabling strict region restrictions, default show global region
323+
try:
324+
from agentkit.toolkit.config.global_config import get_global_config
325+
326+
global_conf = get_global_config()
327+
disabled = bool(
328+
getattr(global_conf.defaults, "disable_region_strict_restrictions", False)
329+
)
330+
if disabled and isinstance(current_strategy_config_dict, dict):
331+
global_region = (global_conf.region or "").strip()
332+
if global_region:
333+
if "region" in current_strategy_config_dict:
334+
if not current_strategy_config_dict.get("region"):
335+
current_strategy_config_dict["region"] = global_region
336+
else:
337+
current_strategy_config_dict["region"] = global_region
338+
except Exception:
339+
pass
340+
322341
# Generate new strategy config
323342
strategy_config = generate_config_from_dataclass(
324343
config_class, current_strategy_config_dict
@@ -472,6 +491,8 @@ def _show_global_config():
472491
console.print(
473492
f" Region: [yellow]{config.region or '[dim](not set)[/dim]'}[/yellow]"
474493
)
494+
if config.defaults.disable_region_strict_restrictions:
495+
console.print(" Disable Region Restrictions: [yellow]True[/yellow]")
475496
console.print()
476497

477498
# Display Volcengine credentials
@@ -581,22 +602,9 @@ def _set_global_field(field_value: str):
581602
)
582603
config_dict[section][field] = clean_value
583604
elif field == "cr_public_endpoint_check":
584-
clean_value = value.strip().lower() if value is not None else None
585-
if clean_value == "":
586-
clean_value = None
587-
if clean_value is None:
588-
config_dict[section][field] = None
589-
else:
590-
truthy = {"true", "1", "yes", "y"}
591-
falsy = {"false", "0", "no", "n"}
592-
if clean_value in truthy:
593-
config_dict[section][field] = True
594-
elif clean_value in falsy:
595-
config_dict[section][field] = False
596-
else:
597-
raise AttributeError(
598-
f"Invalid boolean value for cr_public_endpoint_check: {value}"
599-
)
605+
_set_bool_field(config_dict[section], field, value)
606+
elif field == "disable_region_strict_restrictions":
607+
_set_bool_field(config_dict[section], field, value)
600608
elif field == "iam_role_policies":
601609
# Simple list parsing for CLI convenience
602610
if not value:
@@ -631,3 +639,22 @@ def _set_global_field(field_value: str):
631639
except Exception as e:
632640
console.print(f"[red]❌ Failed to set config: {e}[/red]")
633641
raise typer.Exit(code=1)
642+
643+
644+
def _set_bool_field(target_dict: dict, field: str, value: str):
645+
"""Helper to set boolean field from string value."""
646+
clean_value = value.strip().lower() if value is not None else None
647+
if clean_value == "":
648+
clean_value = None
649+
650+
if clean_value is None:
651+
target_dict[field] = None
652+
else:
653+
truthy = {"true", "1", "yes", "y"}
654+
falsy = {"false", "0", "no", "n"}
655+
if clean_value in truthy:
656+
target_dict[field] = True
657+
elif clean_value in falsy:
658+
target_dict[field] = False
659+
else:
660+
raise AttributeError(f"Invalid boolean value for {field}: {value}")

agentkit/toolkit/cli/interactive_config.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ def __init__(self):
107107
dict: self._handle_dict,
108108
Dict: self._handle_dict,
109109
}
110+
self.current_dataclass_type = None
110111

111112
def _safe_input(self, prompt_text, default: str = "") -> str:
112113
"""Safe input method that protects prompt text from being deleted by Backspace.
@@ -175,6 +176,7 @@ def prefill():
175176
def generate_config(
176177
self, dataclass_type: type, existing_config: Optional[Dict[str, Any]] = None
177178
) -> Dict[str, Any]:
179+
self.current_dataclass_type = dataclass_type
178180
if not is_dataclass(dataclass_type):
179181
raise ValueError(f"{dataclass_type} must be a dataclass")
180182

@@ -270,6 +272,7 @@ def generate_config(
270272
if not isinstance(value, type(MISSING)):
271273
filtered_config[key] = value
272274

275+
self.current_dataclass_type = None
273276
return filtered_config
274277

275278
def _prompt_for_field(
@@ -326,7 +329,35 @@ def _prompt_for_field(
326329
default = None
327330

328331
choices = metadata.get("choices")
329-
if choices:
332+
# Allow free region input when global defaults disable strict restrictions
333+
allow_free_region = False
334+
try:
335+
from agentkit.toolkit.config.global_config import get_global_config
336+
337+
global_conf = get_global_config()
338+
disabled = bool(
339+
getattr(
340+
global_conf.defaults, "disable_region_strict_restrictions", False
341+
)
342+
)
343+
if disabled and name == "region":
344+
# Limit to Cloud/Hybrid strategy types only
345+
try:
346+
from agentkit.toolkit.config.strategy_configs import (
347+
CloudStrategyConfig,
348+
HybridStrategyConfig,
349+
)
350+
351+
allow_free_region = self.current_dataclass_type in (
352+
CloudStrategyConfig,
353+
HybridStrategyConfig,
354+
)
355+
except Exception:
356+
allow_free_region = False
357+
except Exception:
358+
allow_free_region = False
359+
360+
if choices and not allow_free_region:
330361
return self._handle_choice_selection(
331362
description, default, choices, metadata, current, total
332363
)

agentkit/toolkit/config/config_validator.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,18 @@ def validate_dataclass(config: Any) -> List[str]:
8888

8989
errors: List[str] = []
9090

91+
# Global flag: disable strict region choices
92+
disable_region_restrictions = False
93+
try:
94+
from agentkit.toolkit.config.global_config import get_global_config
95+
96+
gc = get_global_config()
97+
disable_region_restrictions = bool(
98+
getattr(gc.defaults, "disable_region_strict_restrictions", False)
99+
)
100+
except Exception:
101+
disable_region_restrictions = False
102+
91103
for field in fields(config):
92104
if field.name.startswith("_"):
93105
continue
@@ -114,7 +126,11 @@ def validate_dataclass(config: Any) -> List[str]:
114126
errors.append(f"{desc}: {msg}")
115127

116128
choices = field.metadata.get("choices")
117-
if choices and value:
129+
if (
130+
choices
131+
and value
132+
and not (disable_region_restrictions and field.name == "region")
133+
):
118134
valid_values = []
119135
if isinstance(choices, list):
120136
if choices and isinstance(choices[0], dict):

agentkit/toolkit/config/global_config.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ class Defaults:
134134
preflight_mode: Optional[str] = None
135135
cr_public_endpoint_check: Optional[bool] = None
136136
iam_role_policies: Optional[list] = None
137+
disable_region_strict_restrictions: Optional[bool] = None
137138

138139
def to_dict(self):
139140
data = {}
@@ -145,6 +146,10 @@ def to_dict(self):
145146
data["cr_public_endpoint_check"] = self.cr_public_endpoint_check
146147
if self.iam_role_policies is not None:
147148
data["iam_role_policies"] = self.iam_role_policies
149+
if self.disable_region_strict_restrictions is not None:
150+
data["disable_region_strict_restrictions"] = (
151+
self.disable_region_strict_restrictions
152+
)
148153
return data
149154

150155
@classmethod
@@ -154,6 +159,9 @@ def from_dict(cls, data: dict):
154159
preflight_mode=data.get("preflight_mode"),
155160
cr_public_endpoint_check=data.get("cr_public_endpoint_check"),
156161
iam_role_policies=data.get("iam_role_policies"),
162+
disable_region_strict_restrictions=data.get(
163+
"disable_region_strict_restrictions"
164+
),
157165
)
158166

159167
defaults: "GlobalConfig.Defaults" = field(

0 commit comments

Comments
 (0)