fix(llm): guard getMaxStopWords against undefined apiBase
Description
OpenAI.getMaxStopWords() builds new URL(this.apiBase!) at the very top of the method. That non-null assertion is unsafe: apiBase can legitimately be undefined. The clearest case is continue-proxy, whose static defaultOptions (core/llm/llms/stubs/ContinueProxy.ts) replace the parent OpenAI.defaultOptions and do not carry an apiBase. When it is undefined, new URL(undefined) throws:
TypeError: Invalid URL
…which crashes stop-word trimming (getMaxStopWords() is called when building the request args for every completion).
This is one of the root causes called out by @RomneyDa in #11872, who suggested exactly this fix:
guard getMaxStopWords() to handle undefined apiBase gracefully (return a safe default like Infinity)
Fix
- Move the
new URL(...)parse below themaxStopWordsshort-circuit, so it is only built when actually needed (previously it was parsed even whenmaxStopWordswas set and the URL went unused). - Return
InfinitywhenapiBaseis absent — the same safe default already used for unknown hosts — and drop the!assertion.
Checklist
- I've read the contributing guide
- The relevant docs, if any, have been updated or created
- The relevant tests, if any, have been updated or created
Screen recording or screenshot
N/A — backend crash fix; covered by the regression tests below.
Tests
Added a getMaxStopWords suite in core/llm/llms/OpenAI.vitest.ts:
- returns
Infinity(instead of throwing) whenapiBaseis undefined - still honors an explicit
maxStopWordswhenapiBaseis undefined
Verified both fail on the old code with TypeError: Invalid URL and pass with the fix. npx tsc -p ./ --noEmit reports 0 errors, prettier --check passes, and the full OpenAI.vitest.ts suite is green (10/10) locally.
Made with Cursor
Summary by cubic
Fixes a crash in OpenAI.getMaxStopWords() when apiBase is undefined by returning Infinity and only parsing the URL when needed. Prevents "TypeError: Invalid URL" during stop-word trimming.
- Bug Fixes
- Return
InfinitywhenapiBaseis missing; remove the non-null assertion. - Parse the URL only after honoring
maxStopWords. - Add regression tests for undefined
apiBaseand explicitmaxStopWords.
- Return