Skip to content

Commit e6ef0d0

Browse files
author
曾长安
committed
fix: filter models by model_picker_enabled and deduplicate by id
- Add getAvailableModels() helper in utils.ts that filters out models with model_picker_enabled=false and removes duplicate model IDs - Update start.ts to use filtered models for display and Claude Code prompts - Update models route to use filtered models in API response - Fix dev script to include 'start' subcommand
1 parent 0ea08fe commit e6ef0d0

File tree

4 files changed

+30
-9
lines changed

4 files changed

+30
-9
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
],
2424
"scripts": {
2525
"build": "tsdown",
26-
"dev": "bun run --watch ./src/main.ts",
26+
"dev": "bun run --watch ./src/main.ts start",
2727
"knip": "knip-bun",
2828
"lint": "eslint --cache",
2929
"lint:all": "eslint --cache .",

src/lib/utils.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import consola from "consola"
22

3-
import { getModels } from "~/services/copilot/get-models"
3+
import { getModels, type Model } from "~/services/copilot/get-models"
44
import { getVSCodeVersion } from "~/services/get-vscode-version"
55

66
import { state } from "./state"
@@ -18,6 +18,21 @@ export async function cacheModels(): Promise<void> {
1818
state.models = models
1919
}
2020

21+
/**
22+
* Returns models filtered by model_picker_enabled and deduplicated by id.
23+
*/
24+
export function getAvailableModels(): Model[] {
25+
if (!state.models) return []
26+
27+
const seen = new Set<string>()
28+
return state.models.data.filter((model) => {
29+
if (!model.model_picker_enabled) return false
30+
if (seen.has(model.id)) return false
31+
seen.add(model.id)
32+
return true
33+
})
34+
}
35+
2136
export const cacheVSCodeVersion = async () => {
2237
const response = await getVSCodeVersion()
2338
state.vsCodeVersion = response

src/routes/models/route.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Hono } from "hono"
22

33
import { forwardError } from "~/lib/error"
4+
import { cacheModels, getAvailableModels } from "~/lib/utils"
45
import { state } from "~/lib/state"
5-
import { cacheModels } from "~/lib/utils"
66

77
export const modelRoutes = new Hono()
88

@@ -13,7 +13,7 @@ modelRoutes.get("/", async (c) => {
1313
await cacheModels()
1414
}
1515

16-
const models = state.models?.data.map((model) => ({
16+
const models = getAvailableModels().map((model) => ({
1717
id: model.id,
1818
object: "model",
1919
type: "model",

src/start.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ import { initProxyFromEnv } from "./lib/proxy"
1111
import { generateEnvScript } from "./lib/shell"
1212
import { state } from "./lib/state"
1313
import { setupCopilotToken, setupGitHubToken } from "./lib/token"
14-
import { cacheModels, cacheVSCodeVersion } from "./lib/utils"
14+
import {
15+
cacheModels,
16+
cacheVSCodeVersion,
17+
getAvailableModels,
18+
} from "./lib/utils"
1519
import { server } from "./server"
1620

1721
interface RunServerOptions {
@@ -60,28 +64,30 @@ export async function runServer(options: RunServerOptions): Promise<void> {
6064
await setupCopilotToken()
6165
await cacheModels()
6266

67+
const availableModels = getAvailableModels()
68+
6369
consola.info(
64-
`Available models: \n${state.models?.data.map((model) => `- ${model.id}`).join("\n")}`,
70+
`Available models: \n${availableModels.map((model) => `- ${model.id}`).join("\n")}`,
6571
)
6672

6773
const serverUrl = `http://localhost:${options.port}`
6874

6975
if (options.claudeCode) {
70-
invariant(state.models, "Models should be loaded by now")
76+
invariant(availableModels.length > 0, "Models should be loaded by now")
7177

7278
const selectedModel = await consola.prompt(
7379
"Select a model to use with Claude Code",
7480
{
7581
type: "select",
76-
options: state.models.data.map((model) => model.id),
82+
options: availableModels.map((model) => model.id),
7783
},
7884
)
7985

8086
const selectedSmallModel = await consola.prompt(
8187
"Select a small model to use with Claude Code",
8288
{
8389
type: "select",
84-
options: state.models.data.map((model) => model.id),
90+
options: availableModels.map((model) => model.id),
8591
},
8692
)
8793

0 commit comments

Comments
 (0)