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

3
.gitignore vendored
View File

@@ -19,3 +19,6 @@ profile/
# build output
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",
"version": "1.0.0",
"version": "0.0.1",
"main": "server.ts",
"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": [],
"author": "Stefano Fiorini",
"license": "MIT",
"description": "",
"dependencies": {
"@google/gemini-cli": "^0.1.3"
"@google/gemini-cli-core": "^0.1.7",
"dotenv": "^17.0.0",
"zod": "^3.25.67"
},
"devDependencies": {
"@types/node": "^24.0.4",
"ts-node": "^10.9.2",
"typescript": "^5.8.3"
"@eslint/js": "^9.30.0",
"@stylistic/eslint-plugin": "^5.0.0",
"@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
const cfg = await createContentGeneratorConfig(
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
return await createContentGenerator(cfg);
@@ -28,9 +28,9 @@ export async function sendChat({
contents,
generationConfig = {},
}: {
contents: any[];
generationConfig?: GenConfig;
tools?: unknown; // accepted but ignored for now
contents: any[],
generationConfig?: GenConfig,
tools?: unknown, // accepted but ignored for now
}) {
const generator: any = await generatorPromise;
return await generator.generateContent({
@@ -44,9 +44,9 @@ export async function* sendChatStream({
contents,
generationConfig = {},
}: {
contents: any[];
generationConfig?: GenConfig;
tools?: unknown;
contents: any[],
generationConfig?: GenConfig,
tools?: unknown,
}) {
const generator: any = await generatorPromise;
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() {
return [{ id: modelName }];
}
// export function listModels() {
// return [{ id: modelName }];
// }
export async function embed(_input: unknown) {
throw new Error('Embeddings endpoint not implemented yet.');
}
// export async function embed(_input: unknown) {
// 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';
/* ------------------------------------------------------------------ */
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 };
}
@@ -41,10 +41,10 @@ export async function mapRequest(body: any) {
topP: body.top_p,
...(body.generationConfig ?? {}), // copy anything ST already merged
};
if (body.include_reasoning === true) {
if (body.include_reasoning === true) {
generationConfig.enable_thoughts = true; // ← current flag
generationConfig.thinking_budget ??= 2048; // optional limit
}
}
/* ---- auto-enable reasoning & 1 M context ----------------------- */
if (body.include_reasoning === true && generationConfig.thinking !== true) {
@@ -72,7 +72,7 @@ if (body.include_reasoning === true) {
description: fn.description ?? '',
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) {
const res = await fetch(url);
if (!res.ok) throw new Error(`Failed to fetch image: ${url}`);
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') };
}

View File

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

View File

@@ -9,9 +9,10 @@
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"strictNullChecks": true,
/* ---- output dir ---- */
"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,
});