Skip to content

Commit cd58aad

Browse files
committed
More cleanup
1 parent 1170466 commit cd58aad

File tree

11 files changed

+56
-105
lines changed

11 files changed

+56
-105
lines changed

src/api/providers/fetchers/cache.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,15 @@ import NodeCache from "node-cache"
55

66
import { ContextProxy } from "../../../core/config/ContextProxy"
77
import { getCacheDirectoryPath } from "../../../shared/storagePathManager"
8+
import { RouterName, ModelRecord } from "../../../shared/api"
89
import { fileExistsAtPath } from "../../../utils/fs"
9-
import type { ModelInfo } from "../../../schemas"
10+
1011
import { getOpenRouterModels } from "./openrouter"
1112
import { getRequestyModels } from "./requesty"
1213
import { getGlamaModels } from "./glama"
1314
import { getUnboundModels } from "./unbound"
1415

15-
export type RouterName = "openrouter" | "requesty" | "glama" | "unbound"
16-
17-
export type ModelRecord = Record<string, ModelInfo>
18-
19-
const memoryCache = new NodeCache({
20-
stdTTL: 5 * 60,
21-
checkperiod: 5 * 60,
22-
})
16+
const memoryCache = new NodeCache({ stdTTL: 5 * 60, checkperiod: 5 * 60 })
2317

2418
async function writeModels(router: RouterName, data: ModelRecord) {
2519
const filename = `${router}_models.json`
@@ -48,6 +42,7 @@ export const getModels = async (router: RouterName): Promise<ModelRecord> => {
4842
let models = memoryCache.get<ModelRecord>(router)
4943

5044
if (models) {
45+
// console.log(`[getModels] NodeCache hit for ${router} -> ${Object.keys(models).length}`)
5146
return models
5247
}
5348

@@ -67,17 +62,20 @@ export const getModels = async (router: RouterName): Promise<ModelRecord> => {
6762
}
6863

6964
if (Object.keys(models).length > 0) {
65+
// console.log(`[getModels] API fetch for ${router} -> ${Object.keys(models).length}`)
7066
memoryCache.set(router, models)
7167

7268
try {
7369
await writeModels(router, models)
70+
// console.log(`[getModels] wrote ${router} models to file cache`)
7471
} catch (error) {}
7572

7673
return models
7774
}
7875

7976
try {
8077
models = await readModels(router)
78+
// console.log(`[getModels] read ${router} models from file cache`)
8179
} catch (error) {}
8280

8381
return models ?? {}

src/api/providers/openrouter.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import OpenAI from "openai"
44

55
import {
66
ApiHandlerOptions,
7+
ModelRecord,
78
openRouterDefaultModelId,
89
openRouterDefaultModelInfo,
910
PROMPT_CACHING_MODELS,
@@ -16,7 +17,7 @@ import { convertToR1Format } from "../transform/r1-format"
1617
import { getModelParams, SingleCompletionHandler } from "../index"
1718
import { DEFAULT_HEADERS, DEEP_SEEK_DEFAULT_TEMPERATURE } from "./constants"
1819
import { BaseProvider } from "./base-provider"
19-
import { ModelRecord, getModels } from "./fetchers/cache"
20+
import { getModels } from "./fetchers/cache"
2021

2122
const OPENROUTER_DEFAULT_PROVIDER_NAME = "[default]"
2223

@@ -130,8 +131,6 @@ export class OpenRouterHandler extends BaseProvider implements SingleCompletionH
130131
}
131132

132133
// https://openrouter.ai/docs/transforms
133-
let fullResponseText = ""
134-
135134
const completionParams: OpenRouterChatCompletionParams = {
136135
model: modelId,
137136
max_tokens: maxTokens,
@@ -170,7 +169,6 @@ export class OpenRouterHandler extends BaseProvider implements SingleCompletionH
170169
}
171170

172171
if (delta?.content) {
173-
fullResponseText += delta.content
174172
yield { type: "text", text: delta.content }
175173
}
176174

src/api/providers/requesty.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { Anthropic } from "@anthropic-ai/sdk"
22
import OpenAI from "openai"
33

4-
import { ModelInfo, requestyDefaultModelId, requestyDefaultModelInfo } from "../../shared/api"
4+
import { ModelInfo, ModelRecord, requestyDefaultModelId, requestyDefaultModelInfo } from "../../shared/api"
55
import { calculateApiCostOpenAI } from "../../utils/cost"
66
import { ApiStream, ApiStreamUsageChunk } from "../transform/stream"
77
import { OpenAiHandler, OpenAiHandlerOptions } from "./openai"
8-
import { ModelRecord, getModels } from "./fetchers/cache"
8+
import { getModels } from "./fetchers/cache"
99

1010
// Requesty usage includes an extra field for Anthropic use cases.
1111
// Safely cast the prompt token details section to the appropriate structure.

src/api/providers/router-provider.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import OpenAI from "openai"
22

3-
import { ApiHandlerOptions, ModelInfo } from "../../shared/api"
3+
import { ApiHandlerOptions, RouterName, ModelRecord, ModelInfo } from "../../shared/api"
44
import { BaseProvider } from "./base-provider"
5-
import { RouterName, ModelRecord, getModels } from "./fetchers/cache"
5+
import { getModels } from "./fetchers/cache"
66

77
type RouterProviderOptions = {
88
name: RouterName

src/shared/ExtensionMessage.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import { GitCommit } from "../utils/git"
2+
13
import {
2-
ModelInfo,
34
GlobalSettings,
45
ApiConfigMeta,
56
ProviderSettings as ApiConfiguration,
@@ -13,8 +14,8 @@ import {
1314
ClineMessage,
1415
} from "../schemas"
1516
import { McpServer } from "./mcp"
16-
import { GitCommit } from "../utils/git"
1717
import { Mode } from "./modes"
18+
import { RouterModels } from "./api"
1819

1920
export type { ApiConfigMeta, ToolProgressStatus }
2021

@@ -84,7 +85,7 @@ export interface ExtensionMessage {
8485
path?: string
8586
}>
8687
partialMessage?: ClineMessage
87-
routerModels?: Record<"openrouter" | "requesty" | "glama" | "unbound", Record<string, ModelInfo>>
88+
routerModels?: RouterModels
8889
openAiModels?: string[]
8990
ollamaModels?: string[]
9091
lmStudioModels?: string[]

src/shared/api.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1446,3 +1446,13 @@ export const COMPUTER_USE_MODELS = new Set([
14461446
"anthropic/claude-3.7-sonnet:beta",
14471447
"anthropic/claude-3.7-sonnet:thinking",
14481448
])
1449+
1450+
const routerNames = ["openrouter", "requesty", "glama", "unbound"] as const
1451+
1452+
export type RouterName = (typeof routerNames)[number]
1453+
1454+
export const isRouterName = (value: string): value is RouterName => routerNames.includes(value as RouterName)
1455+
1456+
export type ModelRecord = Record<string, ModelInfo>
1457+
1458+
export type RouterModels = Record<RouterName, ModelRecord>

webview-ui/src/components/settings/__tests__/SettingsView.test.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@ import { ExtensionStateContextProvider } from "@/context/ExtensionStateContext"
99
import SettingsView from "../SettingsView"
1010

1111
// Mock vscode API
12-
jest.mock("@src/utils/vscode", () => ({
13-
vscode: {
14-
postMessage: jest.fn(),
15-
},
16-
}))
12+
jest.mock("@src/utils/vscode", () => ({ vscode: { postMessage: jest.fn() } }))
1713

1814
// Mock all lucide-react icons with a proxy to handle any icon requested
1915
jest.mock("lucide-react", () => {
@@ -79,10 +75,10 @@ jest.mock("@vscode/webview-ui-toolkit/react", () => ({
7975
/>
8076
),
8177
VSCodeLink: ({ children, href }: any) => <a href={href || "#"}>{children}</a>,
82-
VSCodeRadio: ({ children, value, checked, onChange }: any) => (
78+
VSCodeRadio: ({ value, checked, onChange }: any) => (
8379
<input type="radio" value={value} checked={checked} onChange={onChange} />
8480
),
85-
VSCodeRadioGroup: ({ children, value, onChange }: any) => <div onChange={onChange}>{children}</div>,
81+
VSCodeRadioGroup: ({ children, onChange }: any) => <div onChange={onChange}>{children}</div>,
8682
}))
8783

8884
// Mock Slider component

webview-ui/src/components/ui/hooks/useRouterModels.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import { ModelInfo } from "@roo/shared/api"
1+
import { RouterModels } from "@roo/shared/api"
22

33
import { vscode } from "@src/utils/vscode"
44
import { ExtensionMessage } from "@roo/shared/ExtensionMessage"
55
import { useQuery } from "@tanstack/react-query"
66

7-
export type RouterModels = Record<"openrouter" | "requesty" | "glama" | "unbound", Record<string, ModelInfo>>
8-
97
const getRouterModels = async () =>
108
new Promise<RouterModels>((resolve, reject) => {
119
const cleanup = () => {

webview-ui/src/components/ui/hooks/useSelectedModel.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
ApiConfiguration,
3+
RouterModels,
34
ModelInfo,
45
anthropicDefaultModelId,
56
anthropicModels,
@@ -26,7 +27,7 @@ import {
2627
unboundDefaultModelId,
2728
} from "@roo/shared/api"
2829

29-
import { type RouterModels, useRouterModels } from "./useRouterModels"
30+
import { useRouterModels } from "./useRouterModels"
3031

3132
export const useSelectedModel = (apiConfiguration?: ApiConfiguration) => {
3233
const { data: routerModels, isLoading, isError } = useRouterModels()

webview-ui/src/components/welcome/WelcomeView.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const WelcomeView = () => {
1717
const [errorMessage, setErrorMessage] = useState<string | undefined>(undefined)
1818

1919
const handleSubmit = useCallback(() => {
20-
const error = validateApiConfiguration(apiConfiguration)
20+
const error = apiConfiguration ? validateApiConfiguration(apiConfiguration) : undefined
2121

2222
if (error) {
2323
setErrorMessage(error)

0 commit comments

Comments
 (0)