Skip to content

Commit 57f7fd3

Browse files
committed
feat: enhance Gemini API handling by extracting model from URL and enforcing model requirement
1 parent 3907986 commit 57f7fd3

3 files changed

Lines changed: 30 additions & 9 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@ node_modules/
1414
dist/
1515
logs/gemini-debug.log
1616
logs/gemini-translation.log
17+
.env
18+
logs/gemini-errors.log

src/routes/messages/gemini-handler.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ import {
3131
type GeminiResponse,
3232
} from "./gemini-types"
3333

34+
// Helper function to extract model from URL path
35+
function extractModelFromUrl(url: string): string | undefined {
36+
const path = new URL(url).pathname
37+
const match = path.match(/^\/v1beta\/models\/([^:]+):/)
38+
return match?.[1]
39+
}
40+
3441
// Debug logging interface
3542
interface GeminiDebugLog {
3643
timestamp: string
@@ -280,7 +287,11 @@ function getErrorStatusAndMessage(error: unknown): {
280287
// Standard generation endpoint
281288
export async function handleGeminiGeneration(c: Context) {
282289
const endpoint = c.req.url
283-
const model = c.req.param("model")
290+
const model = extractModelFromUrl(endpoint)
291+
292+
if (!model) {
293+
return c.json({ error: "Model name is required in URL path" }, 400)
294+
}
284295

285296
// IMMEDIATE DEBUG: Log that we entered this handler
286297
logGeminiDebug("handler_entry_GENERATION", endpoint, {
@@ -525,7 +536,11 @@ async function ensureCompleteStream(
525536
// Streaming generation endpoint
526537
export async function handleGeminiStreamGeneration(c: Context) {
527538
const endpoint = c.req.url
528-
const model = c.req.param("model")
539+
const model = extractModelFromUrl(endpoint)
540+
541+
if (!model) {
542+
return c.json({ error: "Model name is required in URL path" }, 400)
543+
}
529544

530545
logGeminiDebug("handler_entry", endpoint, {
531546
data: {
@@ -581,7 +596,11 @@ export async function handleGeminiStreamGeneration(c: Context) {
581596
// Token counting endpoint
582597
export async function handleGeminiCountTokens(c: Context) {
583598
const endpoint = c.req.url
584-
const model = c.req.param("model")
599+
const model = extractModelFromUrl(endpoint)
600+
601+
if (!model) {
602+
return c.json({ error: "Model name is required in URL path" }, 400)
603+
}
585604

586605
// IMMEDIATE DEBUG: Log that we entered this handler
587606
logGeminiDebug("handler_entry_TOKENS", endpoint, {

src/routes/messages/gemini-translation.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ import {
2626

2727
export function translateGeminiToOpenAINonStream(
2828
payload: GeminiRequest,
29-
model?: string,
29+
model: string,
3030
): ChatCompletionsPayload {
3131
return {
32-
model: model || "claude-sonnet-4", // Use provided model or default
32+
model, // Always use the model from the URL
3333
messages: translateGeminiContentsToOpenAI(
3434
payload.contents,
3535
payload.systemInstruction,
@@ -46,10 +46,10 @@ export function translateGeminiToOpenAINonStream(
4646

4747
export function translateGeminiToOpenAIStream(
4848
payload: GeminiRequest,
49-
model?: string,
49+
model: string,
5050
): ChatCompletionsPayload {
5151
const result = {
52-
model: model || "claude-sonnet-4", // Use provided model or default
52+
model, // Always use the model from the URL
5353
messages: translateGeminiContentsToOpenAI(
5454
payload.contents,
5555
payload.systemInstruction,
@@ -417,10 +417,10 @@ export function translateOpenAIChunkToGemini(chunk: ChatCompletionChunk): {
417417

418418
export function translateGeminiCountTokensToOpenAI(
419419
request: GeminiCountTokensRequest,
420-
model?: string,
420+
model: string,
421421
): ChatCompletionsPayload {
422422
return {
423-
model: model || "claude-sonnet-4",
423+
model,
424424
messages: translateGeminiContentsToOpenAI(
425425
request.contents,
426426
request.systemInstruction,

0 commit comments

Comments
 (0)