-
Notifications
You must be signed in to change notification settings - Fork 364
fix(web): disambiguate MCP language model selection for ask_codebase (#1137) #1622
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
pranav718
wants to merge
2
commits into
sourcebot-dev:main
Choose a base branch
from
pranav718: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.
+291
−22
Open
Changes from all commits
Commits
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
182 changes: 182 additions & 0 deletions
182
packages/web/src/features/chat/selectConfiguredLanguageModel.test.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,182 @@ | ||
| import { describe, expect, test } from "vitest"; | ||
| import { selectConfiguredLanguageModel } from "./selectConfiguredLanguageModel"; | ||
| import { StatusCodes } from "http-status-codes"; | ||
| import { ErrorCode } from "@/lib/errorCodes"; | ||
|
|
||
| describe("selectConfiguredLanguageModel", () => { | ||
| const model1 = { | ||
| provider: "anthropic", | ||
| model: "claude-sonnet-4-6", | ||
| displayName: "Claude Sonnet 4.6", | ||
| }; | ||
|
|
||
| const model2 = { | ||
| provider: "anthropic", | ||
| model: "claude-opus-4-7", | ||
| displayName: "Claude Opus 4.7 (Fast)", | ||
| }; | ||
|
|
||
| const model3 = { | ||
| provider: "anthropic", | ||
| model: "claude-opus-4-7", | ||
| displayName: "Claude Opus 4.7 (Thinking)", | ||
| }; | ||
|
|
||
| const model4 = { | ||
| provider: "openai", | ||
| model: "gpt-4o", | ||
| }; | ||
|
|
||
| const configuredModels = [model1, model2, model3, model4]; | ||
|
|
||
| test("returns error when no models are configured", () => { | ||
| const result = selectConfiguredLanguageModel([], { | ||
| provider: "anthropic", | ||
| model: "claude-sonnet-4-6", | ||
| }); | ||
|
|
||
| expect(result.success).toBe(false); | ||
| if (!result.success) { | ||
| expect(result.error.statusCode).toBe(StatusCodes.BAD_REQUEST); | ||
| expect(result.error.errorCode).toBe(ErrorCode.INVALID_REQUEST_BODY); | ||
| expect(result.error.message).toContain("No language models are configured"); | ||
| } | ||
| }); | ||
|
|
||
| test("defaults to first configured model when no requested model is provided", () => { | ||
| const result = selectConfiguredLanguageModel(configuredModels); | ||
|
|
||
| expect(result.success).toBe(true); | ||
| if (result.success) { | ||
| expect(result.model).toEqual(model1); | ||
| } | ||
| }); | ||
|
|
||
| test("defaults to first configured model when empty object is provided", () => { | ||
| const result = selectConfiguredLanguageModel(configuredModels, {}); | ||
|
|
||
| expect(result.success).toBe(true); | ||
| if (result.success) { | ||
| expect(result.model).toEqual(model1); | ||
| } | ||
| }); | ||
|
|
||
| test("matches model uniquely by provider and model when displayName is omitted", () => { | ||
| const result = selectConfiguredLanguageModel(configuredModels, { | ||
| provider: "anthropic", | ||
| model: "claude-sonnet-4-6", | ||
| }); | ||
|
|
||
| expect(result.success).toBe(true); | ||
| if (result.success) { | ||
| expect(result.model).toEqual(model1); | ||
| } | ||
| }); | ||
|
|
||
| test("matches model uniquely when configured model has no displayName and request omits it", () => { | ||
| const result = selectConfiguredLanguageModel(configuredModels, { | ||
| provider: "openai", | ||
| model: "gpt-4o", | ||
| }); | ||
|
|
||
| expect(result.success).toBe(true); | ||
| if (result.success) { | ||
| expect(result.model).toEqual(model4); | ||
| } | ||
| }); | ||
|
|
||
| test("matches model strictly by exact provider, model, and displayName", () => { | ||
| const result = selectConfiguredLanguageModel(configuredModels, { | ||
| provider: "anthropic", | ||
| model: "claude-opus-4-7", | ||
| displayName: "Claude Opus 4.7 (Thinking)", | ||
| }); | ||
|
|
||
| expect(result.success).toBe(true); | ||
| if (result.success) { | ||
| expect(result.model).toEqual(model3); | ||
| } | ||
| }); | ||
|
|
||
| test("returns 400 with disambiguation message when multiple models match provider/model and displayName is omitted", () => { | ||
| const result = selectConfiguredLanguageModel(configuredModels, { | ||
| provider: "anthropic", | ||
| model: "claude-opus-4-7", | ||
| }); | ||
|
|
||
| expect(result.success).toBe(false); | ||
| if (!result.success) { | ||
| expect(result.error.statusCode).toBe(StatusCodes.BAD_REQUEST); | ||
| expect(result.error.errorCode).toBe(ErrorCode.INVALID_REQUEST_BODY); | ||
| expect(result.error.message).toContain("Multiple configurations found for language model 'anthropic/claude-opus-4-7'"); | ||
| expect(result.error.message).toContain("'Claude Opus 4.7 (Fast)'"); | ||
| expect(result.error.message).toContain("'Claude Opus 4.7 (Thinking)'"); | ||
| expect(result.error.message).not.toContain("(default)"); | ||
| } | ||
| }); | ||
|
|
||
| test("handles multiple models with identical provider/model when none have displayName", () => { | ||
| const duplicateUnnamed = [ | ||
| { provider: "ollama", model: "llama3" }, | ||
| { provider: "ollama", model: "llama3" }, | ||
| ]; | ||
|
|
||
| const result = selectConfiguredLanguageModel(duplicateUnnamed, { | ||
| provider: "ollama", | ||
| model: "llama3", | ||
| }); | ||
|
|
||
| expect(result.success).toBe(false); | ||
| if (!result.success) { | ||
| expect(result.error.statusCode).toBe(StatusCodes.BAD_REQUEST); | ||
| expect(result.error.errorCode).toBe(ErrorCode.INVALID_REQUEST_BODY); | ||
| expect(result.error.message).toContain("Please configure distinct displayNames in your configuration"); | ||
| } | ||
| }); | ||
|
|
||
| test("returns 400 when model is not configured", () => { | ||
| const result = selectConfiguredLanguageModel(configuredModels, { | ||
| provider: "google", | ||
| model: "gemini-2.0-flash", | ||
| }); | ||
|
|
||
| expect(result.success).toBe(false); | ||
| if (!result.success) { | ||
| expect(result.error.statusCode).toBe(StatusCodes.BAD_REQUEST); | ||
| expect(result.error.errorCode).toBe(ErrorCode.INVALID_REQUEST_BODY); | ||
| expect(result.error.message).toBe("Language model 'google/gemini-2.0-flash' is not configured."); | ||
| } | ||
| }); | ||
|
|
||
| test("returns 400 when provider/model matches but displayName does not match any config", () => { | ||
| const result = selectConfiguredLanguageModel(configuredModels, { | ||
| provider: "anthropic", | ||
| model: "claude-sonnet-4-6", | ||
| displayName: "Nonexistent Display Name", | ||
| }); | ||
|
|
||
| expect(result.success).toBe(false); | ||
| if (!result.success) { | ||
| expect(result.error.statusCode).toBe(StatusCodes.BAD_REQUEST); | ||
| expect(result.error.errorCode).toBe(ErrorCode.INVALID_REQUEST_BODY); | ||
| expect(result.error.message).toBe("Language model 'anthropic/claude-sonnet-4-6' ('Nonexistent Display Name') is not configured."); | ||
| } | ||
| }); | ||
|
|
||
| test("matches when displayName is empty string if configured with empty string", () => { | ||
| const modelsWithEmpty = [ | ||
| { provider: "openai", model: "gpt-4o", displayName: "" }, | ||
| ]; | ||
|
|
||
| const result = selectConfiguredLanguageModel(modelsWithEmpty, { | ||
| provider: "openai", | ||
| model: "gpt-4o", | ||
| displayName: "", | ||
| }); | ||
|
|
||
| expect(result.success).toBe(true); | ||
| if (result.success) { | ||
| expect(result.model.displayName).toBe(""); | ||
| } | ||
| }); | ||
| }); |
100 changes: 100 additions & 0 deletions
100
packages/web/src/features/chat/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,100 @@ | ||
| import { StatusCodes } from "http-status-codes"; | ||
| import { ErrorCode } from "@/lib/errorCodes"; | ||
| import { ServiceError } from "@/lib/serviceError"; | ||
|
|
||
| export type SelectConfiguredLanguageModelResult<T> = | ||
| | { success: true; model: T } | ||
| | { success: false; error: ServiceError }; | ||
|
|
||
| type MatchableModel = { | ||
| provider: string; | ||
| model: string; | ||
| displayName?: string; | ||
| }; | ||
|
|
||
| export const selectConfiguredLanguageModel = <T extends MatchableModel>( | ||
| configuredModels: T[], | ||
| requestedLanguageModel?: Partial<MatchableModel> | ||
| ): SelectConfiguredLanguageModelResult<T> => { | ||
| if (configuredModels.length === 0) { | ||
| return { | ||
| success: false, | ||
| error: { | ||
| statusCode: StatusCodes.BAD_REQUEST, | ||
| errorCode: ErrorCode.INVALID_REQUEST_BODY, | ||
| message: "No language models are configured. Please configure at least one language model. See: https://docs.sourcebot.dev/docs/configuration/language-model-providers", | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| if (!requestedLanguageModel || (!requestedLanguageModel.provider && !requestedLanguageModel.model)) { | ||
| return { | ||
| success: true, | ||
| model: configuredModels[0], | ||
| }; | ||
| } | ||
|
|
||
| const { provider, model, displayName } = requestedLanguageModel; | ||
|
|
||
| if (displayName !== undefined) { | ||
| const exactMatch = configuredModels.find((m) => { | ||
| return m.provider === provider && m.model === model && m.displayName === displayName; | ||
| }); | ||
|
|
||
| if (exactMatch) { | ||
| return { | ||
| success: true, | ||
| model: exactMatch, | ||
| }; | ||
| } | ||
|
|
||
| return { | ||
| success: false, | ||
| error: { | ||
| statusCode: StatusCodes.BAD_REQUEST, | ||
| errorCode: ErrorCode.INVALID_REQUEST_BODY, | ||
| message: `Language model '${provider}/${model}' ('${displayName}') is not configured.`, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| const matchingModels = configuredModels.filter((m) => { | ||
| return m.provider === provider && m.model === model; | ||
| }); | ||
|
|
||
| if (matchingModels.length === 1) { | ||
| return { | ||
| success: true, | ||
| model: matchingModels[0], | ||
| }; | ||
| } | ||
|
|
||
| if (matchingModels.length > 1) { | ||
| const availableNames = matchingModels | ||
| .map((m) => m.displayName) | ||
| .filter((name): name is string => typeof name === "string" && name.length > 0) | ||
| .map((name) => `'${name}'`); | ||
|
|
||
| const hint = availableNames.length > 0 | ||
| ? `Please specify a displayName (${availableNames.join(', ')}) to disambiguate.` | ||
| : `Please configure distinct displayNames in your configuration to disambiguate.`; | ||
|
|
||
| return { | ||
| success: false, | ||
| error: { | ||
| statusCode: StatusCodes.BAD_REQUEST, | ||
| errorCode: ErrorCode.INVALID_REQUEST_BODY, | ||
| message: `Multiple configurations found for language model '${provider}/${model}'. ${hint}`, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| return { | ||
| success: false, | ||
| error: { | ||
| statusCode: StatusCodes.BAD_REQUEST, | ||
| errorCode: ErrorCode.INVALID_REQUEST_BODY, | ||
| message: `Language model '${provider}/${model}' is not configured.`, | ||
| }, | ||
| }; | ||
| }; | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: When matching configurations include an unnamed or empty-name entry alongside named entries, this hint hides that entry and tells callers to specify only the listed names. Preserve empty-string names and explicitly tell callers to configure a distinct name whenever any matching configuration has no
displayName.Prompt for AI agents