-
Notifications
You must be signed in to change notification settings - Fork 322
fix(web): disambiguate MCP language model selection for ask_codebase #1414
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
Open
Harsh23Kashyap
wants to merge
6
commits into
sourcebot-dev:main
Choose a base branch
from
Harsh23Kashyap:Harsh23Kashyap/fix-mcp-language-model-matching-1137
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+174
−10
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5b16723
fix(web): resolve MCP language model lookups without displayName
19bb076
test(web): cover MCP language model disambiguation
c0b1aeb
chore: add changelog entry for MCP language model selection fix
f638723
refactor(web): extract MCP language model selector for unit tests
00a4bda
test(web): cover not-configured branch in MCP language model selector
392ad3c
refactor(web): use discriminated union for selectConfiguredLanguageMo…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| import { ErrorCode } from "@/lib/errorCodes"; | ||
| import { StatusCodes } from "http-status-codes"; | ||
| import { describe, expect, it } from "vitest"; | ||
| import { selectConfiguredLanguageModel } from "./selectConfiguredLanguageModel"; | ||
|
|
||
| type ConfiguredLanguageModel = Parameters<typeof selectConfiguredLanguageModel>[0][number]; | ||
|
|
||
| const createConfiguredModel = (overrides: Partial<{ | ||
| provider: string; | ||
| model: string; | ||
| displayName?: string; | ||
| }> = {}) => ({ | ||
| provider: 'anthropic', | ||
| model: 'claude-opus-4-7', | ||
| displayName: 'Claude Opus 4.7', | ||
| ...overrides, | ||
| }) as ConfiguredLanguageModel; | ||
|
|
||
| describe("selectConfiguredLanguageModel", () => { | ||
| it("matches a configured model by provider and model when displayName is omitted", () => { | ||
| const configuredModel = createConfiguredModel(); | ||
|
|
||
| const result = selectConfiguredLanguageModel( | ||
| [configuredModel], | ||
| { provider: configuredModel.provider, model: configuredModel.model } | ||
| ); | ||
|
|
||
| expect(result).toEqual({ languageModelConfig: configuredModel }); | ||
| }); | ||
|
|
||
| it("uses displayName to disambiguate duplicate provider/model entries", () => { | ||
| const firstModel = createConfiguredModel({ displayName: 'Claude Opus 4.7 (slow)' }); | ||
| const secondModel = createConfiguredModel({ displayName: 'Claude Opus 4.7 (fast)' }); | ||
|
|
||
| const result = selectConfiguredLanguageModel( | ||
| [firstModel, secondModel], | ||
| { | ||
| provider: firstModel.provider, | ||
| model: firstModel.model, | ||
| displayName: secondModel.displayName, | ||
| } | ||
| ); | ||
|
|
||
| expect(result).toEqual({ languageModelConfig: secondModel }); | ||
| }); | ||
|
|
||
| it("returns an error when no configured model matches provider and model", () => { | ||
| const configuredModel = createConfiguredModel(); | ||
|
|
||
| const result = selectConfiguredLanguageModel( | ||
| [configuredModel], | ||
| { provider: 'openai', model: 'gpt-5' } | ||
| ); | ||
|
|
||
| expect(result).toEqual({ | ||
| error: { | ||
| statusCode: StatusCodes.BAD_REQUEST, | ||
| errorCode: ErrorCode.INVALID_REQUEST_BODY, | ||
| message: "Language model 'openai/gpt-5' is not configured.", | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it("returns a disambiguation error when multiple configured models match", () => { | ||
| const firstModel = createConfiguredModel({ displayName: 'Claude Opus 4.7 (slow)' }); | ||
| const secondModel = createConfiguredModel({ displayName: 'Claude Opus 4.7 (fast)' }); | ||
|
|
||
| const result = selectConfiguredLanguageModel( | ||
| [firstModel, secondModel], | ||
| { provider: firstModel.provider, model: firstModel.model } | ||
| ); | ||
|
|
||
| expect(result).toEqual({ | ||
| error: { | ||
| statusCode: StatusCodes.BAD_REQUEST, | ||
| errorCode: ErrorCode.INVALID_REQUEST_BODY, | ||
| message: "Language model 'anthropic/claude-opus-4-7' matches multiple configured models. Pass displayName to disambiguate. Available matches: 'Claude Opus 4.7 (slow)', 'Claude Opus 4.7 (fast)'.", | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it("returns an error when displayName does not match any configured model", () => { | ||
| const configuredModel = createConfiguredModel(); | ||
|
|
||
| const result = selectConfiguredLanguageModel( | ||
| [configuredModel], | ||
| { | ||
| provider: configuredModel.provider, | ||
| model: configuredModel.model, | ||
| displayName: 'Claude Sonnet 4.6', | ||
| } | ||
| ); | ||
|
|
||
| expect(result).toEqual({ | ||
| error: { | ||
| statusCode: StatusCodes.BAD_REQUEST, | ||
| errorCode: ErrorCode.INVALID_REQUEST_BODY, | ||
| message: "Language model 'anthropic/claude-opus-4-7' is configured, but not with displayName 'Claude Sonnet 4.6'. Available matches: 'Claude Opus 4.7'.", | ||
| }, | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
packages/web/src/ee/features/mcp/selectConfiguredLanguageModel.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import { LanguageModelInfo } from "@/features/chat/types"; | ||
| import { ErrorCode } from "@/lib/errorCodes"; | ||
| import { ServiceError } from "@/lib/serviceError"; | ||
| import { StatusCodes } from "http-status-codes"; | ||
|
|
||
| const formatLanguageModelName = (model: Pick<LanguageModelInfo, 'provider' | 'model'>) => | ||
| `${model.provider}/${model.model}`; | ||
|
|
||
| const formatConfiguredLanguageModelLabel = (model: Pick<LanguageModelInfo, 'provider' | 'model' | 'displayName'>) => | ||
| model.displayName ? `'${model.displayName}'` : formatLanguageModelName(model); | ||
|
|
||
| export const selectConfiguredLanguageModel = <T extends Pick<LanguageModelInfo, 'provider' | 'model' | 'displayName'>>( | ||
| configuredModels: T[], | ||
| requestedLanguageModel: Pick<LanguageModelInfo, 'provider' | 'model' | 'displayName'> | ||
| ): ( | ||
| | { languageModelConfig: T; error?: never } | ||
| | { languageModelConfig?: never; error: ServiceError } | ||
| ) => { | ||
| const candidateModels = configuredModels.filter( | ||
| (model) => model.provider === requestedLanguageModel.provider && model.model === requestedLanguageModel.model | ||
| ); | ||
| if (candidateModels.length === 0) { | ||
| return { | ||
| error: { | ||
| statusCode: StatusCodes.BAD_REQUEST, | ||
| errorCode: ErrorCode.INVALID_REQUEST_BODY, | ||
| message: `Language model '${formatLanguageModelName(requestedLanguageModel)}' is not configured.`, | ||
| } satisfies ServiceError, | ||
| }; | ||
| } | ||
|
|
||
| if (requestedLanguageModel.displayName) { | ||
| const matchingModel = candidateModels.find( | ||
| (model) => model.displayName === requestedLanguageModel.displayName | ||
| ); | ||
| if (!matchingModel) { | ||
| const availableDisplayNames = candidateModels.map(formatConfiguredLanguageModelLabel).join(', '); | ||
| return { | ||
| error: { | ||
| statusCode: StatusCodes.BAD_REQUEST, | ||
| errorCode: ErrorCode.INVALID_REQUEST_BODY, | ||
| message: `Language model '${formatLanguageModelName(requestedLanguageModel)}' is configured, but not with displayName '${requestedLanguageModel.displayName}'. Available matches: ${availableDisplayNames}.`, | ||
| } satisfies ServiceError, | ||
| }; | ||
| } | ||
|
|
||
| return { languageModelConfig: matchingModel }; | ||
| } | ||
|
|
||
| if (candidateModels.length > 1) { | ||
| const availableDisplayNames = candidateModels.map(formatConfiguredLanguageModelLabel).join(', '); | ||
| return { | ||
| error: { | ||
| statusCode: StatusCodes.BAD_REQUEST, | ||
| errorCode: ErrorCode.INVALID_REQUEST_BODY, | ||
| message: `Language model '${formatLanguageModelName(requestedLanguageModel)}' matches multiple configured models. Pass displayName to disambiguate. Available matches: ${availableDisplayNames}.`, | ||
| } satisfies ServiceError, | ||
| }; | ||
| } | ||
|
|
||
| return { languageModelConfig: candidateModels[0] }; | ||
| }; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.