fix(llm): guard getMaxStopWords against undefined apiBase

PL
5mo ago
+37-3

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 the maxStopWords short-circuit, so it is only built when actually needed (previously it was parsed even when maxStopWords was set and the URL went unused).
  • Return Infinity when apiBase is 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) when apiBase is undefined
  • still honors an explicit maxStopWords when apiBase is 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 Infinity when apiBase is missing; remove the non-null assertion.
    • Parse the URL only after honoring maxStopWords.
    • Add regression tests for undefined apiBase and explicit maxStopWords.