opencode
opencode copied to clipboard
fix(provider): validate baseURL format to prevent ERR_INVALID_URL
Summary
- Adds URL format validation for provider
baseURL/apifields - Prevents cryptic
ERR_INVALID_URLerrors when invalid values like"anthropic"or""reach the SDK - Defense-in-depth with two validation layers
Changes
Phase 1: Runtime Validation (provider.ts)
- Validates
baseURLis a proper URL (http://orhttps://) before passing to SDK - Invalid values are filtered out, letting SDK use its default endpoint
Phase 2: Config-Time Validation (config.ts)
- Catches invalid
apifield values at config load time - Hybrid strategy: validates against models.dev cache when available, falls back to URL format check
- Provides helpful error messages with hints
Root Cause
The @ai-sdk/* SDKs use nullish coalescing (??) for baseURL defaults, which doesn't catch empty strings or non-URL values—only null/undefined.
Test Plan
- [x] Typecheck passes
- [x] Invalid config values (
"api": "anthropic") caught with clear error message - [x] Valid configs load normally
- [x] SDK uses default URL when invalid baseURL is filtered
Example Error Message
Invalid provider configuration:
provider.anthropic.api: Invalid value "anthropic"
Hint: Remove the "api" field to use the default anthropic endpoint
🤖 Generated with Claude Code