Skip to content

Commit 3c44634

Browse files
committed
feat: Add model cache and auto max_tokens based on model selection
1 parent 728ff30 commit 3c44634

4 files changed

Lines changed: 38 additions & 1 deletion

File tree

src/lib/is-nullish.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const isNullish = (value: unknown): value is null | undefined =>
2+
value === null || value === undefined

src/lib/models.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import type { GetModelsResponse } from "~/services/copilot/get-models/types"
2+
3+
export const modelsCache = {
4+
_models: null as GetModelsResponse | null,
5+
6+
setModels(models: GetModelsResponse) {
7+
this._models = models
8+
},
9+
10+
getModels() {
11+
return this._models
12+
},
13+
}

src/routes/chat-completions/handler.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import { streamSSE, type SSEMessage } from "hono/streaming"
66
import type { ChatCompletionsPayload } from "~/services/copilot/chat-completions/types"
77
import type { ChatCompletionChunk } from "~/services/copilot/chat-completions/types-streaming"
88

9+
import { isNullish } from "~/lib/is-nullish"
910
import { logger } from "~/lib/logger"
11+
import { modelsCache } from "~/lib/models"
1012
import { chatCompletions } from "~/services/copilot/chat-completions/service"
1113
import { chatCompletionsStream } from "~/services/copilot/chat-completions/service-streaming"
1214

@@ -36,7 +38,19 @@ function createCondensedStreamingResponse(
3638
}
3739

3840
export async function handlerStreaming(c: Context) {
39-
const payload = await c.req.json<ChatCompletionsPayload>()
41+
const models = modelsCache.getModels()
42+
let payload = await c.req.json<ChatCompletionsPayload>()
43+
44+
if (isNullish(payload.max_tokens)) {
45+
const selectedModel = models?.data.find(
46+
(model) => model.id === payload.model,
47+
)
48+
49+
payload = {
50+
...payload,
51+
max_tokens: selectedModel?.capabilities.limits.max_output_tokens,
52+
}
53+
}
4054

4155
// Convert request headers to a regular object from Headers
4256
const requestHeaders = c.req.header()

src/services/api-instance.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
GITHUB_API_CONFIG,
77
GITHUB_WEB_API_CONFIG,
88
} from "~/lib/constants"
9+
import { modelsCache } from "~/lib/models"
910
import { tokenService } from "~/lib/token"
1011

1112
export const copilot = ofetch.create({
@@ -28,6 +29,13 @@ export const copilot = ofetch.create({
2829
}
2930
},
3031

32+
onResponse({ response }) {
33+
if (response.url.endsWith("/models") && response._data) {
34+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
35+
modelsCache.setModels(response._data)
36+
}
37+
},
38+
3139
onResponseError({ error, response, options }) {
3240
if (error instanceof FetchError) {
3341
consola.error(

0 commit comments

Comments
 (0)