sync: PR #28 - tkinter GUI restore, proxy feature toggles, CROF guard fixes, gRPC test skip

This commit is contained in:
Roman | RyzenAdvanced
2026-05-29 13:14:48 +04:00
Unverified
parent 64049f5c94
commit 2535ed83d0
2 changed files with 337 additions and 95 deletions

View File

@@ -311,6 +311,9 @@ FORCE_MODEL = ""
BGP_ROUTES = []
CAVEMAN_MODE = False
RTK_COMPRESSION = False
AUTO_COMPACT = False
ADAPTIVE_COMPACT = False
TOOL_OUTPUT_TRUNCATION = True
PROMPT_ENHANCER = False
PROMPT_ENHANCER_MODE = "offline"
PROMPT_ENHANCER_MODEL = ""
@@ -1179,7 +1182,7 @@ def _init_runtime():
global MODELS, CC_VERSION, REASONING_ENABLED, REASONING_EFFORT, BGP_ROUTES
global _api_key_pool, PROMPT_ENHANCER
global VISION_FALLBACK_URL, VISION_FALLBACK_MODEL, VISION_FALLBACK_KEY
global CAVEMAN_MODE, RTK_COMPRESSION
global CAVEMAN_MODE, RTK_COMPRESSION, AUTO_COMPACT, ADAPTIVE_COMPACT, TOOL_OUTPUT_TRUNCATION
CONFIG = load_config()
PORT = CONFIG["port"]
@@ -1215,6 +1218,11 @@ def _init_runtime():
BGP_ROUTES = CONFIG.get("bgp_routes", [])
CAVEMAN_MODE = CONFIG.get("caveman_mode", False) or os.environ.get("CAVEMAN_MODE") == "1"
RTK_COMPRESSION = CONFIG.get("rtk_compression", False) or os.environ.get("RTK_COMPRESSION") == "1"
AUTO_COMPACT = CONFIG.get("auto_compact", False) or os.environ.get("AUTO_COMPACT") == "1"
ADAPTIVE_COMPACT = CONFIG.get("adaptive_compact", False) or os.environ.get("ADAPTIVE_COMPACT") == "1"
TOOL_OUTPUT_TRUNCATION = CONFIG.get("tool_output_truncation", True)
if os.environ.get("TOOL_OUTPUT_TRUNCATION") == "0":
TOOL_OUTPUT_TRUNCATION = False
_api_key_pool = None
if API_KEY and "," in API_KEY and not OAUTH_PROVIDER.startswith("google") and BACKEND not in ("codebuff", "freebuff"):
_api_key_pool = APIKeyPool(BACKEND, API_KEY)
@@ -1962,6 +1970,8 @@ def _crof_item_limit(model):
return min(per_model, _CROF_ADAPTIVE["global_item_limit"])
def _crof_compact_for_retry(input_data, model, aggression=0):
if "crof.ai" not in TARGET_URL:
return input_data
limit = _crof_item_limit(model)
if not isinstance(input_data, list) or len(input_data) < 2:
return input_data
@@ -2149,10 +2159,11 @@ def _compact_input(input_data):
compressed_data.append(item)
input_data = compressed_data
if not isinstance(input_data, list) or len(input_data) <= _MAX_INPUT_ITEMS:
# Skip compaction entirely — just do optional tool output truncation
if not AUTO_COMPACT or not isinstance(input_data, list) or len(input_data) <= _MAX_INPUT_ITEMS:
out = []
for item in input_data:
if isinstance(item, dict) and item.get("type") == "function_call_output":
if TOOL_OUTPUT_TRUNCATION and isinstance(item, dict) and item.get("type") == "function_call_output":
o = item.get("output", "")
if len(o) > _MAX_TOOL_OUTPUT_CHARS:
item = dict(item)
@@ -2191,7 +2202,7 @@ def _compact_input(input_data):
return head + tail
for item in tail:
if isinstance(item, dict) and item.get("type") == "function_call_output":
if TOOL_OUTPUT_TRUNCATION and isinstance(item, dict) and item.get("type") == "function_call_output":
o = item.get("output", "")
if len(o) > _MAX_TOOL_OUTPUT_CHARS:
item["output"] = o[:_MAX_TOOL_OUTPUT_CHARS] + f"\n... [truncated {len(o) - _MAX_TOOL_OUTPUT_CHARS} chars]"
@@ -5703,7 +5714,6 @@ class Handler(http.server.BaseHTTPRequestHandler):
raw_types = [i.get("type") for i in raw_input] if isinstance(raw_input, list) else "str"
resolved_types = [i.get("type") for i in input_data] if isinstance(input_data, list) else "str"
print(f"[{_sid}] prev_id={prev_id} raw={raw_types} resolved={resolved_types}", file=sys.stderr)
with open(_log_path, "a", encoding="utf-8") as _lf:
_lf.write(f"\n{'='*60}\n{_ts} [session={_sid}] REQUEST {self.path}\n")
_lf.write(f" prev_id={prev_id}\n")
@@ -5807,7 +5817,7 @@ class Handler(http.server.BaseHTTPRequestHandler):
body["input"] = input_data
compacted = False
if policy.get("compaction") and isinstance(input_data, list) and "claude" not in model.lower():
if ADAPTIVE_COMPACT and policy.get("compaction") and isinstance(input_data, list) and "claude" not in model.lower():
input_data, compacted = _adaptive_compact(input_data, model, policy)
if compacted:
body = dict(body)
@@ -5819,7 +5829,7 @@ class Handler(http.server.BaseHTTPRequestHandler):
body["input"] = input_data
crof_limit = _crof_item_limit(model)
_crof_eligible = True
_crof_eligible = "crof.ai" in TARGET_URL
if _crof_eligible and not compacted and isinstance(input_data, list):
_needs_compact = len(input_data) > crof_limit
max_tok = _get_model_max_tokens(model)
@@ -6771,7 +6781,7 @@ class Handler(http.server.BaseHTTPRequestHandler):
body["input"] = input_data
compacted = False
if policy.get("compaction") and isinstance(input_data, list) and "claude" not in model.lower():
if ADAPTIVE_COMPACT and policy.get("compaction") and isinstance(input_data, list) and "claude" not in model.lower():
input_data, compacted = _adaptive_compact(input_data, model, policy)
if compacted:
body = dict(body)
@@ -7662,9 +7672,10 @@ class Handler(http.server.BaseHTTPRequestHandler):
has_content = False
has_message = False
has_tool_call = False
_last_stream_usage = {}
def _observe_event(event):
nonlocal last_resp_id, last_output, last_status, finish_reason, has_content, has_message, has_tool_call
nonlocal last_resp_id, last_output, last_status, finish_reason, has_content, has_message, has_tool_call, _last_stream_usage
for line in event.strip().split("\n"):
if line.startswith("data: "):
try:
@@ -7677,6 +7688,9 @@ class Handler(http.server.BaseHTTPRequestHandler):
has_tool_call = any(o.get("type") == "function_call" for o in (last_output or []))
has_message = any(o.get("type") == "message" for o in (last_output or []))
has_content = has_message or has_tool_call
resp_usage = d.get("response", {}).get("usage")
if resp_usage:
_last_stream_usage = resp_usage
except Exception:
pass
@@ -9044,7 +9058,7 @@ def main():
allow_reuse_address = True
daemon_threads = True
request_queue_size = 64
BIND_HOST = getattr(_args, "host", None) or os.environ.get("CODEX_HOST", "127.0.0.1")
BIND_HOST = os.environ.get("CODEX_HOST", "127.0.0.1")
SERVER = ReusableHTTPServer((BIND_HOST, PORT), Handler)
print(f"translate-proxy ({BACKEND}) listening on http://{BIND_HOST}:{PORT}", flush=True)
print(f"Target: {TARGET_URL}", flush=True)