extractAPITargetHost extracts the hostname from a custom API base URL in engine.env. This supports custom OpenAI-compatible or Anthropic-compatible endpoints (e.g., internal LLM routers, Azure OpenAI) while preserving AWF's credential isolation and firewall features. The function: 1. Checks if the
(workflowData *WorkflowData, envVar string)
| 33 | // extractAPITargetHost(workflowData, "OPENAI_BASE_URL") |
| 34 | // // Returns: "llm-router.internal.example.com" |
| 35 | func extractAPITargetHost(workflowData *WorkflowData, envVar string) string { |
| 36 | // Check if engine config and env are available |
| 37 | if workflowData == nil || workflowData.EngineConfig == nil || workflowData.EngineConfig.Env == nil { |
| 38 | return "" |
| 39 | } |
| 40 | |
| 41 | // Get the custom base URL from engine.env |
| 42 | baseURL, exists := workflowData.EngineConfig.Env[envVar] |
| 43 | if !exists || baseURL == "" { |
| 44 | return "" |
| 45 | } |
| 46 | |
| 47 | // Extract hostname from URL |
| 48 | // URLs can be: |
| 49 | // - "https://llm-router.internal.example.com/v1" → "llm-router.internal.example.com" |
| 50 | // - "http://localhost:8080/v1" → "localhost:8080" |
| 51 | // - "api.openai.com" → "api.openai.com" (treated as hostname) |
| 52 | |
| 53 | // Remove protocol prefix if present |
| 54 | host := baseURL |
| 55 | if idx := strings.Index(host, "://"); idx != -1 { |
| 56 | host = host[idx+3:] |
| 57 | } |
| 58 | |
| 59 | // Remove path suffix if present (everything after first /) |
| 60 | if idx := strings.Index(host, "/"); idx != -1 { |
| 61 | host = host[:idx] |
| 62 | } |
| 63 | |
| 64 | // Validate that we have a non-empty hostname |
| 65 | if host == "" { |
| 66 | awfHelpersLog.Printf("Invalid %s URL (no hostname): %s", envVar, baseURL) |
| 67 | return "" |
| 68 | } |
| 69 | |
| 70 | awfHelpersLog.Printf("Extracted API target host from %s: %s", envVar, host) |
| 71 | return host |
| 72 | } |
| 73 | |
| 74 | // extractAPIBasePath extracts the path component from a custom API base URL in engine.env. |
| 75 | // Returns the path prefix (e.g., "/serving-endpoints") or empty string if no path is present. |