Skip to content

Self-improving AI Employees for everyone

refactor(plugin): Wave 11.D.3 — inline HTTP + Proxy handler bodies

trohitg2d190017c383
diff --git a/server/nodes/proxy/_usage.py b/server/nodes/proxy/_usage.py
new file mode 100644
index 00000000..581720a1
--- /dev/null
+++ b/server/nodes/proxy/_usage.py
@@ -0,0 +1,52 @@
+"""Shared proxy-usage tracker.
+
+``_track_proxy_usage`` is used by both the ``proxyRequest`` plugin
+and (soon) the http_request + scraper plugins when ``useProxy=True``.
+Computes cost from ``pricing.json:proxy.<provider>.cost_per_gb`` and
+persists an ``APIUsageMetric`` row.
+"""
+
+from __future__ import annotations
+
+from typing import Any, Dict, Optional
+
+from core.logging import get_logger
+
+logger = get_logger(__name__)
+
+
+async def track_proxy_usage(
+ node_id: str,
+ provider_name: str,
+ bytes_transferred: int,
+ *,
+ workflow_id: Optional[str] = None,
+ session_id: str = "default",
+) -> Dict[str, float]:
+ from core.container import container
+ from services.pricing import get_pricing_service
+
+ pricing = get_pricing_service()
+ proxy_pricing = pricing._config.get("proxy", {})
+ provider_pricing = proxy_pricing.get(provider_name, {})
+ cost_per_gb = provider_pricing.get("cost_per_gb", 0.0)
+
+ gb = bytes_transferred / (1024 ** 3)
+ total_cost = round(gb * cost_per_gb, 8)
+
+ db = container.database()
+ await db.save_api_usage_metric({
+ "session_id": session_id,
+ "node_id": node_id,
+ "workflow_id": workflow_id,
+ "service": f"proxy_{provider_name}" if provider_name else "proxy",
+ "operation": "proxy_request",
+ "endpoint": "proxy",
+ "resource_count": 1,
+ "cost": total_cost,
+ })
+ logger.debug(
+ f"[Proxy] Tracked {provider_name} usage: {bytes_transferred} bytes "
+ f"= ${total_cost:.8f}"
+ )
+ return {"cost_per_gb": cost_per_gb, "bytes": bytes_transferred, "total_cost": total_cost}
diff --git a/server/nodes/proxy/proxy_config.py b/server/nodes/proxy/proxy_config.py
index a35bc82f..4bc3dd07 100644
--- a/server/nodes/proxy/proxy_config.py
+++ b/server/nodes/proxy/proxy_config.py
@@ -52,11 +52,16 @@ class ProxyConfigNode(ActionNode):
@Operation("dispatch")
async def dispatch(self, ctx: NodeContext, params: ProxyConfigParams) -> Any:
- from services.handlers.proxy import handle_proxy_config
- response = await handle_proxy_config(
- node_id=ctx.node_id, node_type=self.type,
- parameters=params.model_dump(by_alias=True), context=ctx.raw,
- )
- if response.get("success"):
- return response.get("result") or response
- raise RuntimeError(response.get("error") or "Proxy config failed")
+ """Wave 11.D.3: routes directly to tools.py dispatcher which
+ owns the 10-operation matrix (list_providers / add_provider /
+ update_provider / ... / list_routing_rules). Plugin body stays
+ thin — the operation dispatcher is legacy AI-tool dispatch
+ that predates the plugin fast-path and is scheduled for
+ removal in 11.D.13."""
+ from services.handlers.tools import _execute_proxy_config
+
+ raw = params.model_dump(by_alias=True)
+ result = await _execute_proxy_config(raw, raw)
+ if not result.get("success"):
+ raise RuntimeError(result.get("error") or "Proxy config failed")
+ return result
diff --git a/server/nodes/proxy/proxy_request.py b/server/nodes/proxy/proxy_request.py
index eb80a586..5fcd0a39 100644
--- a/server/nodes/proxy/proxy_request.py
+++ b/server/nodes/proxy/proxy_request.py
@@ -54,11 +54,110 @@ class ProxyRequestNode(ActionNode):
@Operation("request")
async def request(self, ctx: NodeContext, params: ProxyRequestParams) -> Any:
- from services.handlers.proxy import handle_proxy_request
- response = await handle_proxy_request(
- node_id=ctx.node_id, node_type=self.type,
- parameters=params.model_dump(by_alias=True), context=ctx.raw,
+ """Inlined from handlers/proxy.py (Wave 11.D.3).
+
+ Retry/failover loop: on each attempt asks ProxyService for a
+ URL, executes the request, and reports result (success +
+ latency_ms / bytes for success, error for failure) back so
+ the health scorer updates. Usage is persisted per request.
+ """
+ import json as json_module
+ import time
+ from core.logging import get_logger
+ import httpx
+
+ from services.proxy.models import ProxyResult
+ from services.proxy.service import get_proxy_service
+ from ._usage import track_proxy_usage
+
+ log = get_logger(__name__)
+ svc = get_proxy_service()
+ if not svc or not svc.is_enabled():
+ raise RuntimeError(
+ "Proxy service not initialized. Use proxy_config tool to add a "
+ "provider first.",
+ )
+
+ raw = params.model_dump(by_alias=True)
+ proxy_url = await svc.get_proxy_url(params.url, raw)
+ if not proxy_url:
+ raise RuntimeError("No proxy provider available")
+
+ max_retries = params.max_retries
+ failover = raw.get("proxyFailover", True)
+ provider_name = params.proxy_provider or ""
+
+ last_error: str = ""
+ for attempt in range(max_retries + 1):
+ req_start = time.monotonic()
+ try:
+ async with httpx.AsyncClient(proxy=proxy_url, timeout=float(params.timeout)) as client:
+ kwargs = {
+ "method": params.method,
+ "url": params.url,
+ "headers": params.headers,
+ }
+ if params.method in ("POST", "PUT", "PATCH") and params.body is not None:
+ body = params.body
+ if isinstance(body, str):
+ try:
+ kwargs["json"] = json_module.loads(body)
+ except json_module.JSONDecodeError:
+ kwargs["content"] = body
+ else:
+ kwargs["json"] = body
+ response = await client.request(**kwargs)
+
+ latency_ms = (time.monotonic() - req_start) * 1000
+ bytes_transferred = len(response.content) if response.content else 0
+
+ svc.report_result(provider_name, ProxyResult(
+ success=response.status_code < 400,
+ latency_ms=latency_ms,
+ bytes_transferred=bytes_transferred,
+ status_code=response.status_code,
+ ))
+ await track_proxy_usage(
+ ctx.node_id, provider_name, bytes_transferred,
+ workflow_id=ctx.workflow_id, session_id=ctx.session_id,
+ )
+ try:
+ response_data = response.json()
+ except Exception:
+ response_data = response.text
+
+ if response.status_code >= 400:
+ raise RuntimeError(f"HTTP {response.status_code}: {response_data!r}")
+ return {
+ "status": response.status_code,
+ "data": response_data,
+ "headers": dict(response.headers),
+ "url": str(response.url),
+ "method": params.method,
+ "proxy_provider": provider_name,
+ "latency_ms": round(latency_ms, 1),
+ "bytes_transferred": bytes_transferred,
+ "attempt": attempt + 1,
+ }
+
+ except Exception as e:
+ latency_ms = (time.monotonic() - req_start) * 1000
+ last_error = str(e)
+ svc.report_result(provider_name, ProxyResult(
+ success=False, latency_ms=latency_ms, error=last_error,
+ ))
+ log.warning(
+ "Proxy request attempt failed",
+ node_id=ctx.node_id, attempt=attempt + 1,
+ max_retries=max_retries, error=last_error,
+ )
+ if not failover or attempt >= max_retries:
+ break
+ try:
+ proxy_url = await svc.get_proxy_url(params.url, raw)
+ except Exception:
+ break
+
+ raise RuntimeError(
+ f"All {max_retries + 1} attempts failed. Last error: {last_error}",
)
- if response.get("success"):
- return response.get("result") or response
- raise RuntimeError(response.get("error") or "Proxy request failed")
diff --git a/server/nodes/proxy/proxy_status.py b/server/nodes/proxy/proxy_status.py
index d23a60a8..3ec46c4d 100644
--- a/server/nodes/proxy/proxy_status.py
+++ b/server/nodes/proxy/proxy_status.py
@@ -43,11 +43,14 @@ class ProxyStatusNode(ActionNode):
@Operation("status")
async def status(self, ctx: NodeContext, params: ProxyStatusParams) -> Any:
- from services.handlers.proxy import handle_proxy_status
- response = await handle_proxy_status(
- node_id=ctx.node_id, node_type=self.type,
- parameters=params.model_dump(by_alias=True), context=ctx.raw,
- )
- if response.get("success"):
- return response.get("result") or response
- raise RuntimeError(response.get("error") or "Proxy status failed")
+ """Inlined from handlers/proxy.py (Wave 11.D.3)."""
+ from services.proxy.service import get_proxy_service
+
+ svc = get_proxy_service()
+ if not svc or not svc.is_enabled():
+ return {"enabled": False, "providers": [], "stats": {}}
+ return {
+ "enabled": True,
+ "providers": [p.model_dump() for p in svc.get_providers()],
+ "stats": svc.get_stats(),
+ }
diff --git a/server/nodes/utility/http_request.py b/server/nodes/utility/http_request.py
index 07b52fac..c9bb618f 100644
--- a/server/nodes/utility/http_request.py
+++ b/server/nodes/utility/http_request.py
@@ -50,11 +50,80 @@ class HttpRequestNode(ActionNode):
@Operation("request")
async def request(self, ctx: NodeContext, params: HttpRequestParams) -> Any:
- from services.handlers.http import handle_http_request
- response = await handle_http_request(
- node_id=ctx.node_id, node_type=self.type,
- parameters=params.model_dump(by_alias=True), context=ctx.raw,
+ """Inlined from handlers/http.py (Wave 11.D.3).
+
+ Supports transparent proxy injection via ``useProxy`` — when
+ enabled, the ProxyService picks a provider and returns a
+ proxy URL; failures fall back to a direct request.
+ """
+ import json as json_module
+ from core.logging import get_logger
+ import httpx
+
+ log = get_logger(__name__)
+ if not params.url:
+ raise RuntimeError("URL is required")
+
+ proxy_url = await _resolve_proxy_url(params.url, params.use_proxy,
+ params.model_dump(by_alias=True))
+ log.info("[HTTP Request] Executing", node_id=ctx.node_id,
+ method=params.method, url=params.url, proxy=bool(proxy_url))
+
+ client_kwargs: dict = {"timeout": float(params.timeout)}
+ if proxy_url:
+ client_kwargs["proxy"] = proxy_url
+
+ async with httpx.AsyncClient(**client_kwargs) as client:
+ kwargs: dict = {
+ "method": params.method,
+ "url": params.url,
+ "headers": params.headers,
+ }
+ body = params.body
+ if params.method in ("POST", "PUT", "PATCH") and body is not None:
+ if isinstance(body, str):
+ try:
+ kwargs["json"] = json_module.loads(body)
+ except json_module.JSONDecodeError:
+ kwargs["content"] = body
+ else:
+ kwargs["json"] = body
+
+ response = await client.request(**kwargs)
+ try:
+ response_data = response.json()
+ except Exception:
+ response_data = response.text
+
+ result = {
+ "status": response.status_code,
+ "data": response_data,
+ "headers": dict(response.headers),
+ "url": str(response.url),
+ "method": params.method,
+ "proxied": proxy_url is not None,
+ }
+ if response.status_code >= 400:
+ raise RuntimeError(f"HTTP {response.status_code}: {response_data!r}")
+ return result
+
+
+async def _resolve_proxy_url(url: str, use_proxy: bool, parameters: dict) -> Any:
+ """Return a proxy URL if ``use_proxy`` is True and a provider is
+ configured; otherwise None. Exceptions are swallowed so the
+ request proceeds without a proxy."""
+ from core.logging import get_logger
+
+ if not use_proxy:
+ return None
+ try:
+ from services.proxy.service import get_proxy_service
+ svc = get_proxy_service()
+ if not svc or not svc.is_enabled():
+ return None
+ return await svc.get_proxy_url(url, parameters)
+ except Exception as e:
+ get_logger(__name__).warning(
+ "Proxy URL lookup failed, proceeding without proxy", error=str(e),
)
- if response.get("success"):
- return response.get("result") or response
- raise RuntimeError(response.get("error") or "HTTP request failed")
+ return None
diff --git a/server/nodes/utility/webhook_response.py b/server/nodes/utility/webhook_response.py
index e151b80e..73e2e93a 100644
--- a/server/nodes/utility/webhook_response.py
+++ b/server/nodes/utility/webhook_response.py
@@ -45,13 +45,43 @@ class WebhookResponseNode(ActionNode):
@Operation("respond")
async def respond(self, ctx: NodeContext, params: WebhookResponseParams) -> Any:
- from services.handlers.http import handle_webhook_response
+ """Inlined from handlers/http.py (Wave 11.D.3).
+
+ Resolves ``{{input.key}}`` and ``{{nodeType.key}}`` templates
+ against upstream outputs, then calls into ``routers.webhook``
+ to release the pending HTTP request.
+ """
+ import json as json_module
+ from core.logging import get_logger
+ from routers.webhook import resolve_webhook_response
+
+ log = get_logger(__name__)
outputs = ctx.raw.get("connected_outputs") or {}
- response = await handle_webhook_response(
- node_id=ctx.node_id, node_type=self.type,
- parameters=params.model_dump(by_alias=True),
- context=ctx.raw, connected_outputs=outputs,
+ body = params.body
+ if isinstance(body, str) and outputs:
+ for node_type_key, output_data in outputs.items():
+ if isinstance(output_data, dict):
+ for key, value in output_data.items():
+ body = body.replace(f"{{{{input.{key}}}}}", str(value))
+ body = body.replace(f"{{{{{node_type_key}.{key}}}}}", str(value))
+ if not body and outputs:
+ first_output = next(iter(outputs.values()), {})
+ body = json_module.dumps(first_output, default=str)
+
+ body_text = body if isinstance(body, str) else json_module.dumps(body, default=str)
+ log.info(
+ "[Webhook Response] Sending", node_id=ctx.node_id,
+ status_code=params.status_code, content_type=params.content_type,
+ body_length=len(body_text),
)
- if response.get("success") is False:
- raise RuntimeError(response.get("error") or "Webhook response failed")
- return response.get("result") or {}
+ resolve_webhook_response(ctx.node_id, {
+ "statusCode": params.status_code,
+ "body": body_text,
+ "contentType": params.content_type,
+ })
+ return {
+ "sent": True,
+ "statusCode": params.status_code,
+ "contentType": params.content_type,
+ "bodyLength": len(body_text),
+ }