Setup project correctly for development and release

This commit is contained in:
2025-06-28 00:45:24 -05:00
parent ba36877f03
commit 5b73f1bb47
14 changed files with 3352 additions and 1902 deletions

1
.env.example Normal file
View File

@@ -0,0 +1 @@
PORT=11434

5
.gitignore vendored
View File

@@ -18,4 +18,7 @@ profile/
.eslintcache .eslintcache
# build output # build output
dist/ dist/
# Environment variables
.env

7
bump.config.ts Normal file
View File

@@ -0,0 +1,7 @@
import { defineConfig } from 'bumpp';
export default defineConfig({
all: false,
release: 'patch',
commit: 'Tag v%s',
});

104
eslint.config.ts Normal file
View File

@@ -0,0 +1,104 @@
import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';
import stylistic from '@stylistic/eslint-plugin';
import nodePlugin from 'eslint-plugin-n';
export default tseslint.config(
eslint.configs.recommended,
nodePlugin.configs['flat/recommended-script'],
...tseslint.configs.strictTypeChecked,
...tseslint.configs.stylisticTypeChecked,
{
ignores: ['**/node_modules/*', '**/*.mjs', '**/*.js', 'src/mapper.ts'],
},
{
languageOptions: {
parserOptions: {
project: './tsconfig.json',
warnOnUnsupportedTypeScriptVersion: false,
},
},
},
{
settings: {
n: {
// Specify the Node.js version for eslint-plugin-n
// Node.js 20+ has fetch API stable
version: '21.0.0',
},
},
},
{
plugins: {
'@stylistic/js': stylistic,
'@stylistic/ts': stylistic,
},
},
{
files: ['**/*.ts'],
},
{
rules: {
'@typescript-eslint/explicit-member-accessibility': 'warn',
'@typescript-eslint/no-misused-promises': 0,
'@typescript-eslint/no-floating-promises': 0,
'@typescript-eslint/no-confusing-void-expression': 0,
'@typescript-eslint/no-unnecessary-condition': 0,
'@typescript-eslint/restrict-template-expressions': [
'error',
{ allowNumber: true },
],
'@typescript-eslint/restrict-plus-operands': [
'warn',
{ allowNumberAndString: true },
],
'@typescript-eslint/no-unused-vars': 'warn',
'@typescript-eslint/no-unsafe-enum-comparison': 0,
'@typescript-eslint/no-unnecessary-type-parameters': 0,
'@stylistic/js/no-extra-semi': 'warn',
'max-len': [
'warn',
{
code: 80,
},
],
'@stylistic/ts/semi': ['warn', 'always'],
'@stylistic/ts/member-delimiter-style': [
'warn',
{
multiline: {
delimiter: 'comma',
requireLast: true,
},
singleline: {
delimiter: 'comma',
requireLast: false,
},
overrides: {
interface: {
singleline: {
delimiter: 'semi',
requireLast: false,
},
multiline: {
delimiter: 'semi',
requireLast: true,
},
},
},
},
],
'@typescript-eslint/no-non-null-assertion': 0,
'@typescript-eslint/no-unused-expressions': 'warn',
'comma-dangle': ['warn', 'always-multiline'],
'no-console': 1,
'no-extra-boolean-cast': 0,
indent: ['warn', 2],
quotes: ['warn', 'single'],
'n/no-process-env': 1,
'n/no-missing-import': 0,
'n/no-unpublished-import': 0,
'prefer-const': 'warn',
},
},
);

7
knip.config.ts Normal file
View File

@@ -0,0 +1,7 @@
import type { KnipConfig } from 'knip';
const config: KnipConfig = {
ignore: ['bump.config.ts'],
};
export default config;

4998
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,20 +1,36 @@
{ {
"name": "gemini-cli-openai-api", "name": "gemini-cli-openai-api",
"version": "1.0.0", "version": "0.0.1",
"main": "server.ts", "main": "server.ts",
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1" "build": "tsdown",
"bump-release": "bumpp",
"dev": "tsx watch ./src/server.ts",
"start": "node ./dist/server.js",
"knip": "knip",
"lint": "eslint --fix ."
}, },
"keywords": [], "keywords": [],
"author": "Stefano Fiorini", "author": "Stefano Fiorini",
"license": "MIT", "license": "MIT",
"description": "", "description": "",
"dependencies": { "dependencies": {
"@google/gemini-cli": "^0.1.3" "@google/gemini-cli-core": "^0.1.7",
"dotenv": "^17.0.0",
"zod": "^3.25.67"
}, },
"devDependencies": { "devDependencies": {
"@types/node": "^24.0.4", "@eslint/js": "^9.30.0",
"ts-node": "^10.9.2", "@stylistic/eslint-plugin": "^5.0.0",
"typescript": "^5.8.3" "@types/node": "^24.0.6",
"bumpp": "^10.2.0",
"eslint": "^9.30.0",
"eslint-plugin-n": "^17.20.0",
"jiti": "^2.4.2",
"knip": "^5.61.2",
"tsdown": "^0.12.9",
"tsx": "^4.20.3",
"typescript": "^5.8.3",
"typescript-eslint": "^8.35.0"
} }
} }

View File

@@ -13,7 +13,7 @@ const generatorPromise = (async () => {
// Pass undefined for model so the helper falls back to DEFAULT_GEMINI_MODEL // Pass undefined for model so the helper falls back to DEFAULT_GEMINI_MODEL
const cfg = await createContentGeneratorConfig( const cfg = await createContentGeneratorConfig(
undefined, // let helper pick default (Gemini-2.5-Pro) undefined, // let helper pick default (Gemini-2.5-Pro)
AuthType.LOGIN_WITH_GOOGLE_PERSONAL // same mode the CLI defaults to AuthType.LOGIN_WITH_GOOGLE_PERSONAL, // same mode the CLI defaults to
); );
modelName = cfg.model; // remember the actual model string modelName = cfg.model; // remember the actual model string
return await createContentGenerator(cfg); return await createContentGenerator(cfg);
@@ -28,9 +28,9 @@ export async function sendChat({
contents, contents,
generationConfig = {}, generationConfig = {},
}: { }: {
contents: any[]; contents: any[],
generationConfig?: GenConfig; generationConfig?: GenConfig,
tools?: unknown; // accepted but ignored for now tools?: unknown, // accepted but ignored for now
}) { }) {
const generator: any = await generatorPromise; const generator: any = await generatorPromise;
return await generator.generateContent({ return await generator.generateContent({
@@ -44,9 +44,9 @@ export async function* sendChatStream({
contents, contents,
generationConfig = {}, generationConfig = {},
}: { }: {
contents: any[]; contents: any[],
generationConfig?: GenConfig; generationConfig?: GenConfig,
tools?: unknown; tools?: unknown,
}) { }) {
const generator: any = await generatorPromise; const generator: any = await generatorPromise;
const stream = await generator.generateContentStream({ const stream = await generator.generateContentStream({
@@ -58,12 +58,12 @@ export async function* sendChatStream({
} }
/* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */
/* 3. Minimal stubs so server.ts compiles (extend later) */ /* 3. Additional stubs to implement later */
/* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */
export function listModels() { // export function listModels() {
return [{ id: modelName }]; // return [{ id: modelName }];
} // }
export async function embed(_input: unknown) { // export async function embed(_input: unknown) {
throw new Error('Embeddings endpoint not implemented yet.'); // throw new Error('Embeddings endpoint not implemented yet.');
} // }

8
src/config.ts Normal file
View File

@@ -0,0 +1,8 @@
import dotenv from 'dotenv';
dotenv.config();
export const config = {
// eslint-disable-next-line n/no-process-env
PORT: Number(process.env.PORT ?? 11434),
};

View File

@@ -6,10 +6,10 @@ import { z } from 'zod';
import { ToolRegistry } from '@google/gemini-cli-core/dist/src/tools/tool-registry.js'; import { ToolRegistry } from '@google/gemini-cli-core/dist/src/tools/tool-registry.js';
/* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */
type Part = { text?: string; inlineData?: { mimeType: string; data: string } }; interface Part { text?: string; inlineData?: { mimeType: string, data: string } }
/* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */
async function callLocalFunction(_name: string, _args: unknown) { function callLocalFunction(_name: string, _args: unknown) {
return { ok: true }; return { ok: true };
} }
@@ -41,10 +41,10 @@ export async function mapRequest(body: any) {
topP: body.top_p, topP: body.top_p,
...(body.generationConfig ?? {}), // copy anything ST already merged ...(body.generationConfig ?? {}), // copy anything ST already merged
}; };
if (body.include_reasoning === true) { if (body.include_reasoning === true) {
generationConfig.enable_thoughts = true; // ← current flag generationConfig.enable_thoughts = true; // ← current flag
generationConfig.thinking_budget ??= 2048; // optional limit generationConfig.thinking_budget ??= 2048; // optional limit
} }
/* ---- auto-enable reasoning & 1 M context ----------------------- */ /* ---- auto-enable reasoning & 1 M context ----------------------- */
if (body.include_reasoning === true && generationConfig.thinking !== true) { if (body.include_reasoning === true && generationConfig.thinking !== true) {
@@ -72,7 +72,7 @@ if (body.include_reasoning === true) {
description: fn.description ?? '', description: fn.description ?? '',
inputSchema: z.object(fn.parameters?.properties ?? {}), inputSchema: z.object(fn.parameters?.properties ?? {}),
}, },
async (args: unknown) => callLocalFunction(fn.name, args), (args: unknown) => callLocalFunction(fn.name, args),
), ),
); );
} }

View File

@@ -1,9 +1,7 @@
import { fetch } from 'undici'; // Node ≥18 has global fetch; otherwise add undici
export async function fetchAndEncode(url: string) { export async function fetchAndEncode(url: string) {
const res = await fetch(url); const res = await fetch(url);
if (!res.ok) throw new Error(`Failed to fetch image: ${url}`); if (!res.ok) throw new Error(`Failed to fetch image: ${url}`);
const buf = Buffer.from(await res.arrayBuffer()); const buf = Buffer.from(await res.arrayBuffer());
const mimeType = res.headers.get('content-type') || 'image/png'; const mimeType = res.headers.get('content-type') ?? 'image/png';
return { mimeType, data: buf.toString('base64') }; return { mimeType, data: buf.toString('base64') };
} }

View File

@@ -1,9 +1,10 @@
import http from 'http'; import http from 'http';
import { sendChat, sendChatStream } from './chatwrapper'; import { sendChat, sendChatStream } from './chatwrapper';
import { mapRequest, mapResponse, mapStreamChunk } from './mapper'; import { mapRequest, mapResponse, mapStreamChunk } from './mapper';
import { config } from './config';
/* ── basic config ─────────────────────────────────────────────────── */ /* ── basic config ─────────────────────────────────────────────────── */
const PORT = Number(process.env.PORT ?? 11434); const PORT = config.PORT;
/* ── CORS helper ──────────────────────────────────────────────────── */ /* ── CORS helper ──────────────────────────────────────────────────── */
function allowCors(res: http.ServerResponse) { function allowCors(res: http.ServerResponse) {
@@ -58,55 +59,52 @@ http
} }
/* -------- /v1/models ---------- */ /* -------- /v1/models ---------- */
if (pathname === "/v1/models" || pathname === "/models") { if (pathname === '/v1/models' || pathname === '/models') {
res.writeHead(200, { "Content-Type": "application/json" }); res.writeHead(200, { 'Content-Type': 'application/json' });
res.end( res.end(
JSON.stringify({ JSON.stringify({
data: [ data: [
{ {
id: "gemini-2.5-pro", id: 'gemini-2.5-pro',
object: "model", object: 'model',
owned_by: "google", owned_by: 'google',
}, },
], ],
}) }),
); );
return; return;
} }
/* ---- /v1/chat/completions ---- */ /* ---- /v1/chat/completions ---- */
if ( if (
(pathname === "/chat/completions" || (pathname === '/chat/completions' ||
(pathname === "/v1/chat/completions" ) && req.method === "POST") (pathname === '/v1/chat/completions' ) && req.method === 'POST')
) { ) {
const body = await readJSON(req, res); const body = await readJSON(req, res);
console.log("Request body:", body);
if (!body) return; if (!body) return;
try { try {
const { geminiReq, tools } = await mapRequest(body); const { geminiReq, tools } = await mapRequest(body);
console.log("Mapped Gemini request:", geminiReq);
if (body.stream) { if (body.stream) {
res.writeHead(200, { res.writeHead(200, {
"Content-Type": "text/event-stream", 'Content-Type': 'text/event-stream',
"Cache-Control": "no-cache", 'Cache-Control': 'no-cache',
Connection: "keep-alive", Connection: 'keep-alive',
}); });
for await (const chunk of sendChatStream({ ...geminiReq, tools })) { for await (const chunk of sendChatStream({ ...geminiReq, tools })) {
console.log("Stream chunk:", chunk);
res.write(`data: ${JSON.stringify(mapStreamChunk(chunk))}\n\n`); res.write(`data: ${JSON.stringify(mapStreamChunk(chunk))}\n\n`);
} }
res.end("data: [DONE]\n\n"); res.end('data: [DONE]\n\n');
} else { } else {
const gResp = await sendChat({ ...geminiReq, tools }); const gResp = await sendChat({ ...geminiReq, tools });
res.writeHead(200, { "Content-Type": "application/json" }); res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(mapResponse(gResp))); res.end(JSON.stringify(mapResponse(gResp)));
} }
} catch (err: any) { } catch (err: any) {
console.error("Proxy error ➜", err); console.error('Proxy error ➜', err);
res.writeHead(500, { "Content-Type": "application/json" }); res.writeHead(500, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: { message: err.message } })); res.end(JSON.stringify({ error: { message: err.message } }));
} }
return; return;

View File

@@ -9,9 +9,10 @@
"esModuleInterop": true, "esModuleInterop": true,
"skipLibCheck": true, "skipLibCheck": true,
"forceConsistentCasingInFileNames": true, "forceConsistentCasingInFileNames": true,
"strictNullChecks": true,
/* ---- output dir ---- */ /* ---- output dir ---- */
"outDir": "dist" "outDir": "dist"
}, },
"include": ["src/**/*"] "include": ["src/**/*", "tsdown.config.ts", "eslint.config.ts", "knip.config.ts", "bump.config.ts"]
} }

11
tsdown.config.ts Normal file
View File

@@ -0,0 +1,11 @@
import { defineConfig } from 'tsdown';
export default defineConfig({
entry: ['src/server.ts'],
format: ['cjs'],
target: 'es2020',
platform: 'node',
sourcemap: true,
});