-
Notifications
You must be signed in to change notification settings - Fork 3.3k
fix(router): use getBaseUrl() helper #1515
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Greptile Overview
Summary
This PR addresses a production environment bug by standardizing URL construction across the codebase. The change replaces direct access to the `NEXT_PUBLIC_APP_URL` environment variable with the centralized `getBaseUrl()` helper function in two critical handler files.The getBaseUrl() helper provides several advantages over direct environment variable access: it automatically detects whether code is running in browser or server context, handles protocol selection (http vs https based on production environment), validates URL formats, and provides fallback mechanisms when environment variables are missing or malformed. The helper defaults to http://localhost:3000 for development and constructs proper URLs with protocol prefixes when needed.
In the router handler, the previous implementation could fail when env.NEXT_PUBLIC_APP_URL was undefined or empty, resulting in malformed URLs for the /api/providers endpoint. The agent handler had a mix of approaches - some places already used getBaseUrl() while others directly accessed environment variables, creating inconsistency.
This change brings consistency to URL construction patterns across the executor handlers and ensures reliable URL generation in all deployment environments, particularly addressing production deployment issues where environment variables might not be properly configured.
Important Files Changed
Changed Files
| Filename | Score | Overview |
|---|---|---|
| apps/sim/executor/handlers/agent/agent-handler.ts | 5/5 | Replaced direct environment variable access with getBaseUrl() helper in three locations for MCP tools and provider API calls |
| apps/sim/executor/handlers/router/router-handler.ts | 5/5 | Standardized URL construction by replacing direct env.NEXT_PUBLIC_APP_URL access with getBaseUrl() helper for provider API endpoint |
Confidence score: 5/5
- This PR is safe to merge with minimal risk as it addresses a well-defined production bug with a proven solution
- Score reflects the straightforward nature of the changes, use of existing tested helper functions, and clear problem-solution mapping
- No files require special attention as both changes follow established patterns and use well-tested utility functions
Sequence Diagram
sequenceDiagram
participant User
participant RouterHandler as "Router Handler"
participant API as "Provider API"
participant AgentHandler as "Agent Handler"
participant MCP as "MCP Service"
User->>RouterHandler: "Execute router block with prompt"
RouterHandler->>RouterHandler: "Generate router prompt from target blocks"
RouterHandler->>API: "POST /api/providers (routing decision)"
API-->>RouterHandler: "Return selected block ID"
RouterHandler->>RouterHandler: "Find chosen block from targets"
RouterHandler-->>User: "Return routing decision with metadata"
alt If Agent Block Selected
User->>AgentHandler: "Execute agent block with inputs"
AgentHandler->>AgentHandler: "Format tools and build messages"
alt MCP Tool Discovery
AgentHandler->>MCP: "GET /api/mcp/tools/discover"
MCP-->>AgentHandler: "Return available MCP tools"
end
alt MCP Tool Execution
AgentHandler->>MCP: "POST /api/mcp/tools/execute"
MCP-->>AgentHandler: "Return tool execution result"
end
AgentHandler->>API: "POST /api/providers (LLM request)"
alt Streaming Response
API-->>AgentHandler: "Stream response with execution data"
else Regular Response
API-->>AgentHandler: "Return JSON response"
end
AgentHandler-->>User: "Return processed output or streaming execution"
end
2 files reviewed, no comments
Summary
Use getBaseUrl() helper that uses getEnv --> router path was not using that causing errors on prod.
Type of Change
Testing
Tested manually, and already used by agent handler
Checklist