Linting fixes. Fixed crash when hitting rate limit

This commit is contained in:
2025-06-28 15:47:33 -05:00
parent 75dc51bcb1
commit 10a6502f73
5 changed files with 162 additions and 50 deletions

View File

@@ -2,7 +2,7 @@ import consola from 'consola';
import http from 'http';
import { listModels, sendChat, sendChatStream } from './chatwrapper';
import { mapRequest, mapResponse, mapStreamChunk } from './mapper.js';
import { RequestBody, GeminiResponse } from './types';
import { RequestBody, GeminiResponse, GeminiStreamChunk, Part } from './types';
import { config } from './config';
/* ── basic config ─────────────────────────────────────────────────── */
@@ -41,8 +41,11 @@ function readJSON(
error: { message: 'Request body is missing for POST request' },
}),
);
resolve(null);
return;
}
return resolve(null);
resolve(null);
return;
}
try {
resolve(JSON.parse(data) as RequestBody);
@@ -51,6 +54,7 @@ function readJSON(
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: { message: 'Malformed JSON' } }));
resolve(null);
return;
}
});
});
@@ -101,7 +105,35 @@ http
});
for await (const chunk of sendChatStream({ ...geminiReq, tools })) {
res.write(`data: ${JSON.stringify(mapStreamChunk(chunk))}\n\n`);
// Transform the chunk to match our expected type
const transformedParts =
chunk.candidates?.[0]?.content?.parts?.map(part => {
const transformedPart: Part = {
text: part.text,
thought: part.text?.startsWith?.('<think>') ?? false,
};
if (part.inlineData?.data) {
transformedPart.inlineData = {
mimeType: part.inlineData.mimeType ?? 'text/plain',
data: part.inlineData.data,
};
}
return transformedPart;
}) ?? [];
const streamChunk: GeminiStreamChunk = {
candidates: [{
content: {
parts: transformedParts,
},
}],
};
res.write(
`data: ${JSON.stringify(mapStreamChunk(streamChunk))}\n\n`,
);
}
res.end('data: [DONE]\n\n');
} else {
@@ -112,10 +144,23 @@ http
} catch (err) {
const error = err as Error;
consola.error('Proxy error ➜', error);
res.writeHead(500, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: { message: error.message } }));
// For streaming responses, send error in stream format
if (body.stream && res.headersSent) {
res.write(`data: ${JSON.stringify({
error: {
message: error.message,
type: 'error',
},
})}\n\n`);
res.end('data: [DONE]\n\n');
return;
} else {
// For non-streaming responses or if headers haven't been sent yet
res.writeHead(500, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: { message: error.message } }));
}
}
return;
}
/* ---- anything else ---------- */