From 01fb01e72300434657356918520e5c3a46c94fc6 Mon Sep 17 00:00:00 2001 From: goulustis Date: Tue, 10 Feb 2026 16:12:53 +0800 Subject: [PATCH] fix the sock issue --- scripts/demo_chat.py | 83 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/scripts/demo_chat.py b/scripts/demo_chat.py index 4ed6aab..b2e8b51 100644 --- a/scripts/demo_chat.py +++ b/scripts/demo_chat.py @@ -7,6 +7,88 @@ from lang_agent.pipeline import Pipeline, PipelineConfig from lang_agent.config import load_tyro_conf from lang_agent.components.conv_store import use_printer +import os +import re +from typing import Dict, List, Tuple + +def fix_proxy_env_vars( + dry_run: bool = False, + verbose: bool = True +) -> Tuple[Dict[str, Tuple[str, str]], Dict[str, str]]: + """ + Auto-fix proxy environment variables in-place. + + Fixes invalid schemes: + - socks:// → socks5:// + - socks5:// → socks5:// (unchanged) + - socks4:// → socks4:// (unchanged) + - http:// → http:// (unchanged) + - https:// → https:// (unchanged) + + Args: + dry_run: If True, only report changes without modifying env vars. + verbose: If True, print changes to stdout. + + Returns: + Tuple of: + - Dict of {var_name: (old_value, new_value)} for changed vars + - Dict of current proxy env vars after fix + """ + proxy_vars = [ + "HTTP_PROXY", "http_proxy", + "HTTPS_PROXY", "https_proxy", + "ALL_PROXY", "all_proxy" + ] + + scheme_fixes = [ + (re.compile(r'^socks5?://', re.IGNORECASE), 'socks5://'), # socks:// or socks5:// → socks5:// + (re.compile(r'^socks4a?://', re.IGNORECASE), 'socks4://'), # socks4:// or socks4a:// → socks4:// + ] + + changed = {} + current_proxies = {} + + for var in proxy_vars: + value = os.environ.get(var) + if not value: + continue + + current_proxies[var] = value + new_value = value + + # Apply scheme fixes + for pattern, replacement in scheme_fixes: + if pattern.match(value): + new_value = replacement + value[pattern.match(value).end():] + break + + # Only update if value actually changed + if new_value != value: + changed[var] = (value, new_value) + if not dry_run: + os.environ[var] = new_value + current_proxies[var] = new_value + + # Report changes + if verbose: + if changed: + action = "Would fix" if dry_run else "Fixed" + print(f"✅ {action} proxy environment variables:") + for var, (old, new) in changed.items(): + print(f" {var}: {old} → {new}") + else: + print("✅ No proxy environment variables need fixing.") + + if current_proxies and not dry_run: + print("\n🔧 Current proxy settings:") + for var in sorted(set(k.upper() for k in current_proxies)): + val = os.environ.get(var, os.environ.get(var.lower(), "")) + if val: + print(f" {var}: {val}") + + return changed, current_proxies + + def main( conf: PipelineConfig, @@ -18,6 +100,7 @@ def main( conf: Pipeline configuration stream: Enable streaming mode for chat responses """ + fix_proxy_env_vars() use_printer() if conf.config_f is not None: conf = load_tyro_conf(conf.config_f)