Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/core/webview/__tests__/webviewMessageHandler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import type { Mock } from "vitest"

// Mock dependencies - must come before imports
vi.mock("../../../api/providers/fetchers/modelCache")
vi.mock("../../../api/providers/fetchers/lmstudio", () => ({
getLMStudioModels: vi.fn(),
}))

vi.mock("../../../integrations/openai-codex/oauth", () => ({
openAiCodexOAuthManager: {
Expand Down Expand Up @@ -42,11 +45,13 @@ import type { ModelRecord } from "@roo-code/types"
import { webviewMessageHandler } from "../webviewMessageHandler"
import type { ClineProvider } from "../ClineProvider"
import { getModels } from "../../../api/providers/fetchers/modelCache"
import { getLMStudioModels } from "../../../api/providers/fetchers/lmstudio"
import { getCommands } from "../../../services/command/commands"
const { openAiCodexOAuthManager } = await import("../../../integrations/openai-codex/oauth")
const { fetchOpenAiCodexRateLimitInfo } = await import("../../../integrations/openai-codex/rate-limits")

const mockGetModels = getModels as Mock<typeof getModels>
const mockGetLMStudioModels = getLMStudioModels as Mock<typeof getLMStudioModels>
const mockGetCommands = vi.mocked(getCommands)
const mockGetAccessToken = vi.mocked(openAiCodexOAuthManager.getAccessToken)
const mockGetAccountId = vi.mocked(openAiCodexOAuthManager.getAccountId)
Expand Down Expand Up @@ -166,6 +171,7 @@ import { resolveImageMentions } from "../../mentions/resolveImageMentions"
describe("webviewMessageHandler - requestLmStudioModels", () => {
beforeEach(() => {
vi.clearAllMocks()
mockGetLMStudioModels.mockReset()
mockClineProvider.getState = vi.fn().mockResolvedValue({
apiConfiguration: {
lmStudioModelId: "model-1",
Expand Down Expand Up @@ -203,6 +209,30 @@ describe("webviewMessageHandler - requestLmStudioModels", () => {
lmStudioModels: mockModels,
})
})

it("prefers the request payload base URL over persisted settings", async () => {
mockGetLMStudioModels.mockResolvedValue({})

await webviewMessageHandler(mockClineProvider, {
type: "requestLmStudioModels",
values: { baseUrl: "http://127.0.0.1:4321" },
})

expect(mockGetLMStudioModels).toHaveBeenCalledWith("http://127.0.0.1:4321")
expect(mockGetModels).not.toHaveBeenCalled()
})

it("treats an empty-string base URL as an explicit preview request", async () => {
mockGetLMStudioModels.mockResolvedValue({})

await webviewMessageHandler(mockClineProvider, {
type: "requestLmStudioModels",
values: { baseUrl: "" },
})

expect(mockGetLMStudioModels).toHaveBeenCalledWith("")
expect(mockGetModels).not.toHaveBeenCalled()
})
})

describe("webviewMessageHandler - image mentions", () => {
Expand Down
21 changes: 14 additions & 7 deletions src/core/webview/webviewMessageHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ import { GetModelsOptions } from "../../shared/api"
import { generateSystemPrompt } from "./generateSystemPrompt"
import { resolveDefaultSaveUri, saveLastExportPath } from "../../utils/export"
import { getCommand } from "../../utils/commands"
import { getLMStudioModels } from "../../api/providers/fetchers/lmstudio"

const ALLOWED_VSCODE_SETTINGS = new Set(["terminal.integrated.inheritEnv"])

Expand Down Expand Up @@ -1086,14 +1087,20 @@ export const webviewMessageHandler = async (
// Specific handler for LM Studio models only.
const { apiConfiguration: lmStudioApiConfig } = await provider.getState()
try {
const lmStudioOptions = {
provider: "lmstudio" as const,
baseUrl: lmStudioApiConfig.lmStudioBaseUrl,
const requestedBaseUrl = message.values?.baseUrl
const hasPreviewBaseUrl = typeof requestedBaseUrl === "string"
let lmStudioModels: ModelRecord
if (hasPreviewBaseUrl) {
lmStudioModels = await getLMStudioModels(requestedBaseUrl)
} else {
const lmStudioOptions = {
provider: "lmstudio" as const,
baseUrl: lmStudioApiConfig.lmStudioBaseUrl,
}
// Flush cache and refresh to ensure fresh models.
await flushModels(lmStudioOptions, true)
lmStudioModels = await getModels(lmStudioOptions)
}
// Flush cache and refresh to ensure fresh models.
await flushModels(lmStudioOptions, true)

const lmStudioModels = await getModels(lmStudioOptions)

if (Object.keys(lmStudioModels).length > 0) {
provider.postMessageToWebview({
Expand Down
3 changes: 2 additions & 1 deletion webview-ui/src/components/settings/ApiOptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import { validateApiConfigurationExcludingModelErrors, getModelValidationError }
import { useAppTranslation } from "@src/i18n/TranslationContext"
import { useRouterModels } from "@src/components/ui/hooks/useRouterModels"
import { useSelectedModel } from "@src/components/ui/hooks/useSelectedModel"
import { requestLmStudioModels } from "@src/components/ui/hooks/useLmStudioModels"
import { useExtensionState } from "@src/context/ExtensionStateContext"
import {
useOpenRouterModelProviders,
Expand Down Expand Up @@ -236,7 +237,7 @@ const ApiOptions = ({
} else if (selectedProvider === "ollama") {
vscode.postMessage({ type: "requestOllamaModels" })
} else if (selectedProvider === "lmstudio") {
vscode.postMessage({ type: "requestLmStudioModels" })
requestLmStudioModels(apiConfiguration?.lmStudioBaseUrl)
} else if (selectedProvider === "vscode-lm") {
vscode.postMessage({ type: "requestVsCodeLmModels" })
} else if (selectedProvider === "litellm" || selectedProvider === "poe") {
Expand Down
7 changes: 4 additions & 3 deletions webview-ui/src/components/settings/providers/LMStudio.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback, useState, useMemo, useEffect } from "react"
import { useCallback, useState, useMemo, useEffect, useRef } from "react"
import { useEvent } from "react-use"
import { Trans } from "react-i18next"
import { Checkbox } from "vscrui"
Expand All @@ -7,8 +7,8 @@ import { VSCodeLink, VSCodeTextField } from "@vscode/webview-ui-toolkit/react"
import type { ProviderSettings, ExtensionMessage, ModelRecord } from "@roo-code/types"

import { useAppTranslation } from "@src/i18n/TranslationContext"
import { requestLmStudioModels } from "@src/components/ui/hooks/useLmStudioModels"
import { useRouterModels } from "@src/components/ui/hooks/useRouterModels"
import { vscode } from "@src/utils/vscode"

import { inputEventTransform } from "../transforms"
import { ModelPicker } from "../ModelPicker"
Expand All @@ -23,6 +23,7 @@ export const LMStudio = ({ apiConfiguration, setApiConfigurationField }: LMStudi

const [lmStudioModels, setLmStudioModels] = useState<ModelRecord>({})
const routerModels = useRouterModels()
const initialBaseUrlRef = useRef(apiConfiguration?.lmStudioBaseUrl)

const handleInputChange = useCallback(
<K extends keyof ProviderSettings, E>(
Expand Down Expand Up @@ -53,7 +54,7 @@ export const LMStudio = ({ apiConfiguration, setApiConfigurationField }: LMStudi
// Refresh models on mount
useEffect(() => {
// Request fresh models - the handler now flushes cache automatically
vscode.postMessage({ type: "requestLmStudioModels" })
requestLmStudioModels(initialBaseUrlRef.current)
}, [])

// Check if the selected model exists in the fetched models
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
vi.mock("@src/utils/vscode", () => ({
vscode: {
postMessage: vi.fn(),
},
}))

import { vscode } from "@src/utils/vscode"

import { requestLmStudioModels } from "../useLmStudioModels"

describe("requestLmStudioModels", () => {
beforeEach(() => {
vi.clearAllMocks()
})

it("includes the current unsaved base URL when requesting models", () => {
requestLmStudioModels("http://127.0.0.1:1234")

expect(vscode.postMessage).toHaveBeenCalledWith({
type: "requestLmStudioModels",
values: { baseUrl: "http://127.0.0.1:1234" },
})
})

it("preserves an empty base URL so the extension can fall back to the default", () => {
requestLmStudioModels("")

expect(vscode.postMessage).toHaveBeenCalledWith({
type: "requestLmStudioModels",
values: { baseUrl: "" },
})
})
})
15 changes: 12 additions & 3 deletions webview-ui/src/components/ui/hooks/useLmStudioModels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import { type ModelRecord, type ExtensionMessage } from "@roo-code/types"

import { vscode } from "@src/utils/vscode"

const getLmStudioModels = async () =>
export const requestLmStudioModels = (baseUrl?: string) =>
vscode.postMessage({
type: "requestLmStudioModels",
values: typeof baseUrl === "string" ? { baseUrl } : undefined,
})

const getLmStudioModels = async (baseUrl?: string) =>
new Promise<ModelRecord>((resolve, reject) => {
const cleanup = () => {
window.removeEventListener("message", handler)
Expand All @@ -31,8 +37,11 @@ const getLmStudioModels = async () =>
}

window.addEventListener("message", handler)
vscode.postMessage({ type: "requestLmStudioModels" })
requestLmStudioModels(baseUrl)
})

export const useLmStudioModels = (modelId?: string) =>
useQuery({ queryKey: ["lmStudioModels"], queryFn: () => (modelId ? getLmStudioModels() : {}) })
useQuery({
queryKey: ["lmStudioModels"],
queryFn: () => (modelId ? getLmStudioModels() : {}),
})
2 changes: 1 addition & 1 deletion webview-ui/src/i18n/locales/en/chat.json
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@
"support": "Please support Zoo Code by starring us on <githubLink>GitHub</githubLink>.",
"handoff": {
"heading": "Roo Code is back! Now as a community-maintained plugin called Zoo Code!!",
"description": "If you haven't been following, the Roo Code team recently announced they are sun setting the development of Roo Code and are archiving the work they have done. But fear not, the community has stepped up to continue the legacy of Roo Code with a new name and a new home! We are not just a single \"Roo\" anymore, we are a community, a \"Zoo\" if you will, Zoo Code is a community-maintained plugin that picks up where Zoo Code left off, and we're committed to keeping the spirit of Roo alive while also introducing new features and improvements. We want to give a huge shoutout to the entire Roo Code team for their incredible work and for creating such an amazing tool for developers. We're excited to continue building on their foundation and to see where the community takes Zoo Code in the future!",
"description": "If you haven't been following, the Roo Code team recently announced they are sun setting the development of Roo Code and are archiving the work they have done. But fear not, the community has stepped up to continue the legacy of Roo Code with a new name and a new home! We are not just a single \"Roo\" anymore, we are a community, a \"Zoo\" if you will, Zoo Code is a community-maintained plugin that picks up where Roo Code left off, and we're committed to keeping the spirit of Roo alive while also introducing new features and improvements. We want to give a huge shoutout to the entire Roo Code team for their incredible work and for creating such an amazing tool for developers. We're excited to continue building on their foundation and to see where the community takes Zoo Code in the future!",
"readMore": "See the new home page of Zoo Code and read the full announcement"
},
"release": {
Expand Down
Loading