Fix port connection issues on proxy urls#1411
Conversation
📝 WalkthroughWalkthroughThese changes enhance API URL resolution across the renderer layer. They implement environment-aware fallbacks for API endpoints when TL_API_URL is undefined or default: using localhost:8338, frontend port 1212 → 8338, cloud/hosted origins with port preservation, and a new ConnectionLostModal fallback mechanism for port 8338 recovery attempts. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Suggested labels
Suggested reviewers
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 3
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/renderer/components/Shared/ConnectionLostModal.tsx (1)
71-78:⚠️ Potential issue | 🟠 MajorReset
fallbackTriedRefwhen connection changes.After one disconnect cycle,
fallbackTriedRef.currentstaystrue, so later reconnect attempts skip the 8338 fallback entirely.💡 Proposed fix
checkCountRef.current = 0; setCheckCount(0); + fallbackTriedRef.current = false; let interval: ReturnType<typeof setInterval> | null = null;🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/renderer/components/Shared/ConnectionLostModal.tsx` around lines 71 - 78, The useEffect that runs on connection changes resets checkCountRef and checkCount but does not reset fallbackTriedRef, causing the 8338 fallback to be skipped on later reconnects; inside the same useEffect body (the one referencing checkCountRef, setCheckCount, and interval), set fallbackTriedRef.current = false when a new connection value is observed so the fallback logic can run again on subsequent disconnect cycles.
🧹 Nitpick comments (1)
src/renderer/App.tsx (1)
211-251: Extract this URL resolver into a shared utility and add unit tests.This logic is now duplicated across files and is easy to drift. Please centralize it (e.g.,
resolveApiBaseUrl) and add focused cases for localhost,:1212, Runpod proxy hostnames, default ports, and explicit env URLs.As per coding guidelines, "Write unit tests for all utility functions and complex hooks in frontend".
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/renderer/App.tsx` around lines 211 - 251, Extract the inline URL resolution logic from App.tsx into a shared utility function named resolveApiBaseUrl(envUrl?: string, location?: {protocol: string, hostname: string, port: string}) that returns a normalized base URL with a trailing slash; inside the function replicate the existing rules (fallbacks for undefined/'default'/empty TL_API_URL, localhost/127.0.0.1 -> port 8338, frontend port '1212' -> API 8338, same-origin for default http/https ports, preserving explicit port otherwise, and ensuring trailing slash), then replace the IIFE in App.tsx to call resolveApiBaseUrl(process.env.TL_API_URL, window.location). Add unit tests for resolveApiBaseUrl covering: undefined/null/'default'/empty env, hostname localhost and 127.0.0.1, frontend port '1212', Runpod/proxy-style hostnames (ensure same-origin behavior), default http/https ports, explicit env URL (with and without trailing slash), and edge cases (whitespace in env); keep tests deterministic by passing a mock location object.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/renderer/App.tsx`:
- Around line 227-243: The URL resolution block in src/renderer/App.tsx (the
code that builds the API base URL using protocol, hostname, and port—the
variables port, hostname, protocol and the isDefaultPort logic) fails for Runpod
proxy hosts like pod-1212.proxy.runpod.net because the browser port is empty;
add a host check (e.g., detect hostnames matching
/^pod-\d+\.proxy\.runpod\.net$/ or containing ".proxy.runpod.net" with a pod-
prefix) and when matched return `${protocol}//${hostname}:8338/` (same routing
as the existing port === '1212' branch), then add unit tests for the hostname
cases (pod-1212.proxy.runpod.net, normal localhost:1212, default-origin hosts)
to cover the new logic and prevent regressions.
In `@src/renderer/components/Shared/ConnectionLostModal.tsx`:
- Around line 101-106: The code currently uses unsafe casts "(window as
any).TransformerLab" when setting TransformerLab.API_URL; remove the casts and
access the typed global directly (use window.TransformerLab) inside the existing
typeof window !== 'undefined' check, updating references to set
window.TransformerLab.API_URL = fallbackBase so the assignment uses the declared
Window/TransformerLab types (no any).
- Around line 42-58: The buildPortFallback function incorrectly appends :8338 as
a port for Runpod hostnames — update its logic to detect Runpod patterns (e.g.,
pod-<id>.proxy.runpod.net) and map port 8338 into the hostname
(pod-8338.proxy.runpod.net) instead of using a port parameter; also ensure
buildPortFallback still returns null for localhost and when port already equals
'8338'. In the component useEffect that triggers fallbacks, reset
fallbackTriedRef.current = false at the start of the effect when the connection
prop changes so a new connection can attempt a fallback. Finally, remove all
(window as any).TransformerLab casts by declaring a proper Window interface
extension (e.g., interface Window { TransformerLab?: ... }) and use
window.TransformerLab with the correct typing instead of any to satisfy
TypeScript guidelines.
---
Outside diff comments:
In `@src/renderer/components/Shared/ConnectionLostModal.tsx`:
- Around line 71-78: The useEffect that runs on connection changes resets
checkCountRef and checkCount but does not reset fallbackTriedRef, causing the
8338 fallback to be skipped on later reconnects; inside the same useEffect body
(the one referencing checkCountRef, setCheckCount, and interval), set
fallbackTriedRef.current = false when a new connection value is observed so the
fallback logic can run again on subsequent disconnect cycles.
---
Nitpick comments:
In `@src/renderer/App.tsx`:
- Around line 211-251: Extract the inline URL resolution logic from App.tsx into
a shared utility function named resolveApiBaseUrl(envUrl?: string, location?:
{protocol: string, hostname: string, port: string}) that returns a normalized
base URL with a trailing slash; inside the function replicate the existing rules
(fallbacks for undefined/'default'/empty TL_API_URL, localhost/127.0.0.1 -> port
8338, frontend port '1212' -> API 8338, same-origin for default http/https
ports, preserving explicit port otherwise, and ensuring trailing slash), then
replace the IIFE in App.tsx to call resolveApiBaseUrl(process.env.TL_API_URL,
window.location). Add unit tests for resolveApiBaseUrl covering:
undefined/null/'default'/empty env, hostname localhost and 127.0.0.1, frontend
port '1212', Runpod/proxy-style hostnames (ensure same-origin behavior), default
http/https ports, explicit env URL (with and without trailing slash), and edge
cases (whitespace in env); keep tests deterministic by passing a mock location
object.
ℹ️ Review info
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
src/renderer/App.tsxsrc/renderer/components/Login/LoginPage.tsxsrc/renderer/components/Shared/ConnectionLostModal.tsx
Summary by CodeRabbit